diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index 5e45c8dd2cc92c567cd08f3b8089b4b5de6aedb8..a5f10c6ee4be3cd52f250935a28d998242deca34 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -37,7 +37,7 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER, REFERENCE, TEXT, is_reference) from caosdb.common.models import (Container, Entity, File, Property, Query, Record, RecordType, execute_query, - get_config) + get_config, SPECIAL_ATTRIBUTES) def new_record(record_type, name=None, description=None, @@ -565,10 +565,6 @@ def getCommitIn(folder): return t.readline().strip() -SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description", - "id", "path", "checksum", "size"] - - def compare_entities(old_entity: Entity, new_entity: Entity): """ Compare two entites. @@ -681,48 +677,6 @@ def compare_entities(old_entity: Entity, new_entity: Entity): 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): """ Merge entity_b into entity_a such that they have the same parents and properties. diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 80a6ee11e707fb3776fc96b42a16b649ac575f66..3c5106356e9468e5ef3493f72b007a3e464baef7 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -72,6 +72,10 @@ ALL = "ALL" NONE = "NONE" +SPECIAL_ATTRIBUTES = ["name", "role", "datatype", "description", + "id", "path", "checksum", "size"] + + class Entity(object): """Entity is a generic CaosDB object. @@ -114,6 +118,48 @@ class Entity(object): self.id = id 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 def version(self): if self._version is not None or self._wrapped_entity is None: