From 24bd613d428273bde84f27f04478a603b0c0f5c8 Mon Sep 17 00:00:00 2001 From: Daryl Antony Date: Mon, 27 Jan 2014 16:30:10 +1100 Subject: [PATCH 1/2] add django to setup requires --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d63430f..4075f1d 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,8 @@ def read_file(filename): keywords='faker fixtures data test django', long_description=read_file('README.rst'), install_requires=['django','fake-factory>=0.2'], + setup_requires=['django',], tests_require=['django','fake-factory>=0.2'], test_suite="runtests.runtests", zip_safe=False, -) \ No newline at end of file +) From 991a7d357132ca76022cb69d3c42297ee955bf64 Mon Sep 17 00:00:00 2001 From: Daryl Antony Date: Mon, 27 Jan 2014 16:38:57 +1100 Subject: [PATCH 2/2] catch the importerror on probable fresh setup.py & virtualenv install --- django_faker/__init__.py | 145 ++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 71 deletions(-) diff --git a/django_faker/__init__.py b/django_faker/__init__.py index c29ea3e..06f99e7 100644 --- a/django_faker/__init__.py +++ b/django_faker/__init__.py @@ -3,106 +3,109 @@ Django-faker uses python-faker to generate test data for Django models and templates. """ -from django.conf import settings - __version__ = '0.2' -class Faker(object): +try: + from django.conf import settings + - instance = None - populators = {} - generators = {} + class Faker(object): - @classmethod - def __new__(cls, *args, **kwargs): - if cls.instance is None: - cls.instance = super(Faker, cls).__new__(*args, **kwargs) - return cls.instance + instance = None + populators = {} + generators = {} - def __init__(self): -# assert False, "Cannot create a instance of Faker" - pass + @classmethod + def __new__(cls, *args, **kwargs): + if cls.instance is None: + cls.instance = super(Faker, cls).__new__(*args, **kwargs) + return cls.instance + def __init__(self): + # assert False, "Cannot create a instance of Faker" + pass - @staticmethod - def getCodename(locale=None, providers=None): - """ - codename = locale[-Provider]* - """ - # language - locale = locale or getattr(settings,'FAKER_LOCALE', getattr(settings,'LANGUAGE_CODE', None)) - # providers - providers = providers or getattr(settings,'FAKER_PROVIDERS', None) - codename = locale or 'default' + @staticmethod + def getCodename(locale=None, providers=None): + """ + codename = locale[-Provider]* + """ + # language + locale = locale or getattr(settings,'FAKER_LOCALE', getattr(settings,'LANGUAGE_CODE', None)) + # providers + providers = providers or getattr(settings,'FAKER_PROVIDERS', None) - if providers: - codename += "-" + "-".join(sorted(providers)) + codename = locale or 'default' - return codename + if providers: + codename += "-" + "-".join(sorted(providers)) + return codename - @classmethod - def getGenerator(cls, locale=None, providers=None, codename=None): - """ - use a codename to cache generators - """ - codename = codename or cls.getCodename(locale, providers) + @classmethod + def getGenerator(cls, locale=None, providers=None, codename=None): + """ + use a codename to cache generators + """ - if codename not in cls.generators: - from faker import Faker as FakerGenerator - # initialize with faker.generator.Generator instance - # and remember in cache - cls.generators[codename] = FakerGenerator( locale, providers ) - cls.generators[codename].seed( cls.generators[codename].randomInt() ) + codename = codename or cls.getCodename(locale, providers) - return cls.generators[codename] + if codename not in cls.generators: + from faker import Faker as FakerGenerator + # initialize with faker.generator.Generator instance + # and remember in cache + cls.generators[codename] = FakerGenerator( locale, providers ) + cls.generators[codename].seed( cls.generators[codename].randomInt() ) + return cls.generators[codename] - @classmethod - def getPopulator(cls, locale=None, providers=None): - """ - uses: + @classmethod + def getPopulator(cls, locale=None, providers=None): + """ - from django_faker import Faker - pop = Faker.getPopulator() + uses: - from myapp import models - pop.addEntity(models.MyModel, 10) - pop.addEntity(models.MyOtherModel, 10) - pop.execute() + from django_faker import Faker + pop = Faker.getPopulator() - pop = Faker.getPopulator('it_IT') + from myapp import models + pop.addEntity(models.MyModel, 10) + pop.addEntity(models.MyOtherModel, 10) + pop.execute() - pop.addEntity(models.MyModel, 10) - pop.addEntity(models.MyOtherModel, 10) - pop.execute() + pop = Faker.getPopulator('it_IT') - """ + pop.addEntity(models.MyModel, 10) + pop.addEntity(models.MyOtherModel, 10) + pop.execute() - codename = cls.getCodename(locale, providers) + """ - if codename not in cls.populators: + codename = cls.getCodename(locale, providers) - generator = cls.generators.get(codename, None) or cls.getGenerator(codename=codename) + if codename not in cls.populators: - from django_faker import populator + generator = cls.generators.get(codename, None) or cls.getGenerator(codename=codename) - cls.populators[codename] = populator.Populator( generator ) + from django_faker import populator - return cls.populators[codename] + cls.populators[codename] = populator.Populator( generator ) -# if not cls.populator: -# cls.populator= populators.Populator( -# # initialize with faker.generator.Generator instance -# FakerGenerator( -# -# getattr(settings,'FAKER_LOCALE', getattr(settings,'LANGUAGE_CODE', locale)), -# -# getattr(settings,'FAKER_PROVIDERS', providers) -# ) -# ) + return cls.populators[codename] + # if not cls.populator: + # cls.populator= populators.Populator( + # # initialize with faker.generator.Generator instance + # FakerGenerator( + # + # getattr(settings,'FAKER_LOCALE', getattr(settings,'LANGUAGE_CODE', locale)), + # + # getattr(settings,'FAKER_PROVIDERS', providers) + # ) + # ) +except ImportError: + print ("Django is not yet installed we're probably running setup.py on a fresh virtualenv install right?")