Skip to content
Snippets Groups Projects
Commit e60c02bb authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

MAINT: moved copy_entity to member function in models

parent 7db9b86d
Branches
Tags
2 merge requests!57RELEASE 0.7.3,!45F copy entity
...@@ -37,7 +37,7 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER, ...@@ -37,7 +37,7 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER,
REFERENCE, TEXT, is_reference) REFERENCE, TEXT, is_reference)
from caosdb.common.models import (Container, Entity, File, Property, Query, from caosdb.common.models import (Container, Entity, File, Property, Query,
Record, RecordType, execute_query, Record, RecordType, execute_query,
get_config) get_config, SPECIAL_ATTRIBUTES)
def new_record(record_type, name=None, description=None, def new_record(record_type, name=None, description=None,
...@@ -565,10 +565,6 @@ def getCommitIn(folder): ...@@ -565,10 +565,6 @@ def getCommitIn(folder):
return t.readline().strip() return t.readline().strip()
SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description",
"id", "path", "checksum", "size"]
def compare_entities(old_entity: Entity, new_entity: Entity): def compare_entities(old_entity: Entity, new_entity: Entity):
""" """
Compare two entites. Compare two entites.
...@@ -681,48 +677,6 @@ def compare_entities(old_entity: Entity, new_entity: Entity): ...@@ -681,48 +677,6 @@ def compare_entities(old_entity: Entity, new_entity: Entity):
return (olddiff, newdiff) return (olddiff, newdiff)
def copy_entity(entity: Entity):
"""
Return a copy of entity.
If deep == True return a deep copy, recursively copying all sub entities.
Standard properties are copied using add_property.
Special attributes, as defined by the global variable SPECIAL_ATTRIBUTES and additionaly
the "value" are copied using setattr.
"""
if entity.role == "File":
new = File()
elif entity.role == "Property":
new = Property()
elif entity.role == "RecordType":
new = RecordType()
elif entity.role == "Record":
new = Record()
elif entity.role == "Entity":
new = Entity()
else:
raise RuntimeError("Unkonwn role.")
# Copy special attributes:
# TODO: this might rise an exception when copying
# special file attributes like checksum and size.
for attribute in SPECIAL_ATTRIBUTES + ["value"]:
val = getattr(entity, attribute)
if val is not None:
setattr(new, attribute, val)
# Copy parents:
for p in entity.parents:
new.add_parent(p)
# Copy properties:
for p in entity.properties:
new.add_property(p, importance=entity.get_importance(p))
return new
def merge_entities(entity_a: Entity, entity_b: Entity): def merge_entities(entity_a: Entity, entity_b: Entity):
""" """
Merge entity_b into entity_a such that they have the same parents and properties. Merge entity_b into entity_a such that they have the same parents and properties.
......
...@@ -72,6 +72,10 @@ ALL = "ALL" ...@@ -72,6 +72,10 @@ ALL = "ALL"
NONE = "NONE" NONE = "NONE"
SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description",
"id", "path", "checksum", "size"]
class Entity(object): class Entity(object):
"""Entity is a generic CaosDB object. """Entity is a generic CaosDB object.
...@@ -114,6 +118,48 @@ class Entity(object): ...@@ -114,6 +118,48 @@ class Entity(object):
self.id = id self.id = id
self.state = None self.state = None
def copy_entity(self):
"""
Return a copy of entity.
If deep == True return a deep copy, recursively copying all sub entities.
Standard properties are copied using add_property.
Special attributes, as defined by the global variable SPECIAL_ATTRIBUTES and additionaly
the "value" are copied using setattr.
"""
if self.role == "File":
new = File()
elif self.role == "Property":
new = Property()
elif self.role == "RecordType":
new = RecordType()
elif self.role == "Record":
new = Record()
elif self.role == "Entity":
new = Entity()
else:
raise RuntimeError("Unkonwn role.")
# Copy special attributes:
# TODO: this might rise an exception when copying
# special file attributes like checksum and size.
for attribute in SPECIAL_ATTRIBUTES + ["value"]:
val = getattr(self, attribute)
if val is not None:
setattr(new, attribute, val)
# Copy parents:
for p in self.parents:
new.add_parent(p)
# Copy properties:
for p in self.properties:
new.add_property(p, importance=self.get_importance(p))
return new
@property @property
def version(self): def version(self):
if self._version is not None or self._wrapped_entity is None: if self._version is not None or self._wrapped_entity is None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment