diff options
Diffstat (limited to 'factory/containers.py')
-rw-r--r-- | factory/containers.py | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/factory/containers.py b/factory/containers.py index 4127851..497e98c 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -104,20 +104,35 @@ class LazyStub(object): 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("_")) + 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 attributes starting with '_'. + instances; ignores all class/staticmethods and private attributes + (starting with '_'). Returns a dict containing all remaining elements. """ remaining = {} for k, v in d.iteritems(): - if k.startswith('_') and not isinstance(v, declarations.OrderedDeclaration): - remaining[k] = v - else: + if self.is_declaration(k, v): self[k] = v + else: + remaining[k] = v return remaining def copy(self, extra=None): |