Skip to content
Snippets Groups Projects
Commit bb7be0ca authored by schulmax's avatar schulmax
Browse files

[TASK] Introduces UUID-Transformer

parent 2102ccae
No related branches found
No related tags found
No related merge requests found
...@@ -521,6 +521,7 @@ class Result(object): ...@@ -521,6 +521,7 @@ class Result(object):
'''Initializes the necessary properties.''' '''Initializes the necessary properties.'''
self.entity = entity self.entity = entity
self.source = NameResolver(self.entity, 'source', source)() self.source = NameResolver(self.entity, 'source', source)()
'''source is a Result, NOT a container'''
self.update = EntityList(self.source) self.update = EntityList(self.source)
self.insert = EntityList(self.source) self.insert = EntityList(self.source)
...@@ -571,7 +572,7 @@ class EntityList(list): ...@@ -571,7 +572,7 @@ class EntityList(list):
''' '''
def __init__(self, result): def __init__(self, result):
'''Default constructor''' '''Expects a Result instance'''
self.result = result self.result = result
self.source = result.source_container self.source = result.source_container
self.target = result.target_container self.target = result.target_container
......
...@@ -291,3 +291,68 @@ class PropertiesTransformer(AbstractTransformer): ...@@ -291,3 +291,68 @@ class PropertiesTransformer(AbstractTransformer):
transformed_properties[prop] = eval('pt.{}.value'.format(operation), globals(), dict(properties.items() + locals().items())) transformed_properties[prop] = eval('pt.{}.value'.format(operation), globals(), dict(properties.items() + locals().items()))
return transformed_properties return transformed_properties
@yamlify
class UuidTransformer(AbstractTransformer):
'''Creates an uuid for entity.
Applies all operations to the fields in the new data container.
.. function:: __init__(source[, *args[, **kwargs]])
Constructor
:param source: The source container.
:param **kwargs: Accepts parameters from :class:`.AbstractTransformer`.
:type source: string
A loader inside of a YAML job definition with more parameters:
.. code-block:: yaml
transformer: !uuid
entity: !entity
id: string
source: foobar
'''
yaml_tag = u'!uuid'
def __init__(self, source, uuid_attribute, mapping = {}, *args, **kwargs):
super(UuidTransformer, self).__init__(*args, **kwargs)
self.source = source
self.uuid_attr = uuid_attribute
self.mapping = mapping
'''The source container to work on.'''
if self.entity is not None:
self.source = NameResolver(self.entity, 'source', source)()
def create_container(self):
if isinstance(self.source, str) and self.entity is not None:
self.source = NameResolver(self.entity, 'source', self.source)()
else:
raise Exception("Source is of type string and entity is None, so the NameResolver cannot find a correspinding container.")
if self.mapping is not {}:
mapped_identifiers = [x if x not in self.mapping.keys() else self.mapping[x] for x in self.source.identifiers]
self.result = Container(name = self.name,
identifiers = mapped_identifiers,
properties = self.entity.properties)
self.entity.add(self.result)
def can_execute(self):
'''Whether the Transformer has everything set needed for its transform operation.'''
return self.entity is not None and self.source is not None
def _execute(self):
for record in self.entity.record_repository.itervalues():
identifier = record.get_container_identifier(self.source)
if identifier is None: continue # ignore records not known by source.
transformed = self._execute_operations(record)
record.load(identifier, transformed, self.result)
def _execute_operations(self, record):
properties = record.get_properties(self.source)
transformed_properties = properties.copy()
transformed_properties[self.uuid_attr] = uuid().hex
return transformed_properties
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment