diff options
Diffstat (limited to 'factory/containers.py')
-rw-r--r-- | factory/containers.py | 71 |
1 files changed, 8 insertions, 63 deletions
diff --git a/factory/containers.py b/factory/containers.py index 4537e44..0ae354b 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Copyright (c) 2010 Mark Sandstrom -# Copyright (c) 2011-2013 Raphaël Barrois +# Copyright (c) 2011-2015 Raphaël Barrois # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -47,27 +47,27 @@ class LazyStub(object): __containers (LazyStub list): "parents" of the LazyStub being built. This allows to have the field of a field depend on the value of another field - __target_class (type): the target class to build. + __model_class (type): the model class to build. """ __initialized = False - def __init__(self, attrs, containers=(), target_class=object, log_ctx=None): + def __init__(self, attrs, containers=(), model_class=object, log_ctx=None): self.__attrs = attrs self.__values = {} self.__pending = [] self.__containers = containers - self.__target_class = target_class - self.__log_ctx = log_ctx or '%s.%s' % (target_class.__module__, target_class.__name__) + self.__model_class = model_class + self.__log_ctx = log_ctx or '%s.%s' % (model_class.__module__, model_class.__name__) self.factory_parent = containers[0] if containers else None self.__initialized = True def __repr__(self): - return '<LazyStub for %s.%s>' % (self.__target_class.__module__, self.__target_class.__name__) + return '<LazyStub for %s.%s>' % (self.__model_class.__module__, self.__model_class.__name__) def __str__(self): return '<LazyStub for %s with %s>' % ( - self.__target_class.__name__, list(self.__attrs.keys())) + self.__model_class.__name__, list(self.__attrs.keys())) def __fill__(self): """Fill this LazyStub, computing values of all defined attributes. @@ -121,61 +121,6 @@ class LazyStub(object): raise AttributeError('Setting of object attributes is not allowed') -class DeclarationDict(dict): - """Slightly extended dict to work with OrderedDeclaration.""" - - def is_declaration(self, name, value): - """Determines if a class attribute is a field value declaration. - - Based on the name and value of the class attribute, return ``True`` if - it looks like a declaration of a default field value, ``False`` if it - is private (name starts with '_') or a classmethod or staticmethod. - - """ - if isinstance(value, (classmethod, staticmethod)): - return False - elif isinstance(value, declarations.OrderedDeclaration): - return True - return (not name.startswith("_") and not name.startswith("FACTORY_")) - - def update_with_public(self, d): - """Updates the DeclarationDict from a class definition dict. - - Takes into account all public attributes and OrderedDeclaration - instances; ignores all class/staticmethods and private attributes - (starting with '_'). - - Returns a dict containing all remaining elements. - """ - remaining = {} - for k, v in d.items(): - if self.is_declaration(k, v): - self[k] = v - else: - remaining[k] = v - return remaining - - def copy(self, extra=None): - """Copy this DeclarationDict into another one, including extra values. - - Args: - extra (dict): additional attributes to include in the copy. - """ - new = self.__class__() - new.update(self) - if extra: - new.update(extra) - return new - - -class PostGenerationDeclarationDict(DeclarationDict): - """Alternate DeclarationDict for PostGenerationDeclaration.""" - - def is_declaration(self, name, value): - """Captures instances of PostGenerationDeclaration.""" - return isinstance(value, declarations.PostGenerationDeclaration) - - class LazyValue(object): """Some kind of "lazy evaluating" object.""" @@ -279,7 +224,7 @@ class AttributeBuilder(object): wrapped_attrs[k] = v stub = LazyStub(wrapped_attrs, containers=self._containers, - target_class=self.factory, log_ctx=self._log_ctx) + model_class=self.factory, log_ctx=self._log_ctx) return stub.__fill__() |