Alternate implementation of ObjectFactory.
parent
4dba41afc8
commit
c29acfa3ce
|
@ -4,24 +4,27 @@ class ObjectFactory(object):
|
||||||
def __init__(self, created_by_ref=None, created=None,
|
def __init__(self, created_by_ref=None, created=None,
|
||||||
external_references=None, object_marking_refs=None,
|
external_references=None, object_marking_refs=None,
|
||||||
granular_markings=None):
|
granular_markings=None):
|
||||||
self.created_by_ref = created_by_ref
|
|
||||||
self.created = created
|
self._defaults = {}
|
||||||
self.external_references = external_references
|
if created_by_ref:
|
||||||
self.object_marking_refs = object_marking_refs
|
self._defaults['created_by_ref'] = created_by_ref
|
||||||
self.granular_markings = granular_markings
|
if created:
|
||||||
|
self._defaults['created'] = created
|
||||||
|
# If the user provides a default "created" time, we also want to use
|
||||||
|
# that as the modified time.
|
||||||
|
self._defaults['modified'] = created
|
||||||
|
if external_references:
|
||||||
|
self._defaults['external_references'] = external_references
|
||||||
|
if object_marking_refs:
|
||||||
|
self._defaults['object_marking_refs'] = object_marking_refs
|
||||||
|
if granular_markings:
|
||||||
|
self._defaults['granular_markings'] = granular_markings
|
||||||
|
|
||||||
def create(self, cls, **kwargs):
|
def create(self, cls, **kwargs):
|
||||||
if 'created_by_ref' not in kwargs and self.created_by_ref is not None:
|
# Use self.defaults as the base, but update with any explicit args
|
||||||
kwargs['created_by_ref'] = self.created_by_ref
|
# provided by the user.
|
||||||
if 'created' not in kwargs and self.created is not None:
|
properties = dict(**self._defaults)
|
||||||
kwargs['created'] = self.created
|
if kwargs:
|
||||||
if 'modified' not in kwargs:
|
properties.update(**kwargs)
|
||||||
kwargs['modified'] = self.created
|
|
||||||
if 'external_references' not in kwargs and self.external_references is not None:
|
|
||||||
kwargs['external_references'] = self.external_references
|
|
||||||
if 'object_marking_refs' not in kwargs and self.object_marking_refs is not None:
|
|
||||||
kwargs['object_marking_refs'] = self.object_marking_refs
|
|
||||||
if 'granular_markings' not in kwargs and self.granular_markings is not None:
|
|
||||||
kwargs['granular_markings'] = self.granular_markings
|
|
||||||
|
|
||||||
return cls(**kwargs)
|
return cls(**properties)
|
||||||
|
|
Loading…
Reference in New Issue