diff options
author | Thomas Goirand <thomas@goirand.fr> | 2013-05-12 05:38:51 +0000 |
---|---|---|
committer | Thomas Goirand <thomas@goirand.fr> | 2013-05-12 05:38:51 +0000 |
commit | 8e5ee2fb19058336afb5af61486e17f2603b56cb (patch) | |
tree | a73e60fd2443584475f4371083644373a8004192 /factory | |
parent | 91b2da50daf5a0e95eb7cfdb1fa3668ed2925201 (diff) | |
download | factory-boy-8e5ee2fb19058336afb5af61486e17f2603b56cb.tar factory-boy-8e5ee2fb19058336afb5af61486e17f2603b56cb.tar.gz |
Fixed differences with upstream branch.
Diffstat (limited to 'factory')
-rw-r--r-- | factory/__init__.py | 1 | ||||
-rw-r--r-- | factory/containers.py | 4 | ||||
-rw-r--r-- | factory/declarations.py | 29 | ||||
-rw-r--r-- | factory/utils.py | 32 |
4 files changed, 38 insertions, 28 deletions
diff --git a/factory/__init__.py b/factory/__init__.py index 88865ba..e1138fa 100644 --- a/factory/__init__.py +++ b/factory/__init__.py @@ -50,6 +50,7 @@ from .declarations import ( Dict, List, PostGeneration, + PostGenerationMethodCall, RelatedFactory, ) diff --git a/factory/containers.py b/factory/containers.py index c8be431..ee2ad82 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -124,7 +124,7 @@ class DeclarationDict(dict): return False elif isinstance(value, declarations.OrderedDeclaration): return True - return (not name.startswith("_")) + return (not name.startswith("_") and not name.startswith("FACTORY_")) def update_with_public(self, d): """Updates the DeclarationDict from a class definition dict. @@ -167,7 +167,7 @@ class PostGenerationDeclarationDict(DeclarationDict): class LazyValue(object): """Some kind of "lazy evaluating" object.""" - def evaluate(self, obj, containers=()): + def evaluate(self, obj, containers=()): # pragma: no cover """Compute the value, using the given object.""" raise NotImplementedError("This is an abstract method.") diff --git a/factory/declarations.py b/factory/declarations.py index 969d780..974b4ac 100644 --- a/factory/declarations.py +++ b/factory/declarations.py @@ -231,13 +231,17 @@ class ContainerAttribute(OrderedDeclaration): return self.function(obj, containers) -class SubFactory(OrderedDeclaration): - """Base class for attributes based upon a sub-factory. +class ParameteredAttribute(OrderedDeclaration): + """Base class for attributes expecting parameters. Attributes: - defaults (dict): Overrides to the defaults defined in the wrapped - factory - factory (base.Factory): the wrapped factory + defaults (dict): Default values for the paramters. + May be overridden by call-time parameters. + + Class attributes: + CONTAINERS_FIELD (str): name of the field, if any, where container + information (e.g for SubFactory) should be stored. If empty, + containers data isn't merged into generate() parameters. """ CONTAINERS_FIELD = '__containers' @@ -248,7 +252,6 @@ class SubFactory(OrderedDeclaration): def __init__(self, **kwargs): super(ParameteredAttribute, self).__init__() self.defaults = kwargs - self.factory = factory def _prepare_containers(self, obj, containers=()): if self.EXTEND_CONTAINERS: @@ -260,19 +263,17 @@ class SubFactory(OrderedDeclaration): """Evaluate the current definition and fill its attributes. Uses attributes definition in the following order: - - attributes defined in the wrapped factory class - - values defined when defining the SubFactory - - additional values defined in attributes + - values defined when defining the ParameteredAttribute + - additional values defined when instantiating the containing factory Args: - create (bool): whether the subfactory should call 'build' or - 'create' + create (bool): whether the parent factory is being 'built' or + 'created' extra (containers.DeclarationDict): extra values that should - override the wrapped factory's defaults + override the defaults containers (list of LazyStub): List of LazyStub for the chain of factories being evaluated, the calling stub being first. """ - defaults = dict(self.defaults) if extra: defaults.update(extra) @@ -393,7 +394,7 @@ class PostGenerationDeclaration(object): kwargs = utils.extract_dict(name, attrs) return extracted, kwargs - def call(self, obj, create, extracted=None, **kwargs): + def call(self, obj, create, extracted=None, **kwargs): # pragma: no cover """Call this hook; no return value is expected. Args: diff --git a/factory/utils.py b/factory/utils.py index f845f46..fb8cfef 100644 --- a/factory/utils.py +++ b/factory/utils.py @@ -58,20 +58,16 @@ def extract_dict(prefix, kwargs, pop=True, exclude=()): return extracted -def declength_compare(a, b): - """Compare objects, choosing longest first.""" - if len(a) > len(b): - return -1 - elif len(a) < len(b): - return 1 - else: - return cmp(a, b) - - def multi_extract_dict(prefixes, kwargs, pop=True, exclude=()): """Extracts all values from a given list of prefixes. - Arguments have the same meaning as for extract_dict. + Extraction will start with longer prefixes. + + Args: + prefixes (str list): the prefixes to use for lookups + kwargs (dict): the dict from which values should be extracted + pop (bool): whether to use pop (True) or get (False) + exclude (iterable): list of prefixed keys that shouldn't be extracted Returns: dict(str => dict): a dict mapping each prefix to the dict of extracted @@ -79,10 +75,22 @@ def multi_extract_dict(prefixes, kwargs, pop=True, exclude=()): """ results = {} exclude = list(exclude) - for prefix in sorted(prefixes, cmp=declength_compare): + for prefix in sorted(prefixes, key=lambda x: -len(x)): extracted = extract_dict(prefix, kwargs, pop=pop, exclude=exclude) results[prefix] = extracted exclude.extend( ['%s%s%s' % (prefix, ATTR_SPLITTER, key) for key in extracted]) return results + + +def import_object(module_name, attribute_name): + """Import an object from its absolute path. + + Example: + >>> import_object('datetime', 'datetime') + <type 'datetime.datetime'> + """ + module = __import__(module_name, {}, {}, [attribute_name], 0) + return getattr(module, attribute_name) + |