diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 157354b12608f8969d68739d8fefaba8e9921810..711b9ba66ac7e8418b178091a76aa0b6edfbf1dc 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -1162,7 +1162,7 @@ out: List[Entity] return ret - def get_errors_deep(self, roots=None): + def get_errors_deep(self, roots=None) -> List[Tuple[str, List[Entity]]]: """Get all error messages of this entity and all sub-entities / parents / properties. @@ -1196,9 +1196,9 @@ out: List[Entity] def to_xml( self, xml: Optional[etree._Element] = None, - add_properties=ALL, - local_serialization: bool=False, - )->etree._Element: + add_properties: Optional[INHERITANCE] = ALL, + local_serialization: bool = False, + ) -> etree._Element: """Generate an xml representation of this entity. If the parameter xml is given, all attributes, parents, properties, and messages of this entity will be added to it instead of creating a new element. @@ -1837,14 +1837,20 @@ class Parent(Entity): def affiliation(self, affiliation): self.__affiliation = affiliation - def __init__(self, id=None, name=None, description=None, inheritance=None): # @ReservedAssignment + def __init__( + self, + id: Optional[int] = None, + name: Optional[str] = None, + description: Optional[str] = None, + inheritance: Optional[INHERITANCE] = None, + ): # @ReservedAssignment Entity.__init__(self, id=id, name=name, description=description) if inheritance is not None: self.set_flag("inheritance", inheritance) self.__affiliation = None - def to_xml(self, xml=None, add_properties=None): + def to_xml(self, xml: Optional[etree._Element] = None, add_properties=None): if xml is None: xml = etree.Element("Parent") @@ -1901,13 +1907,20 @@ class Property(Entity): return super(Property, self).add_parent(parent=parent, id=id, name=name, inheritance=inheritance) - def __init__(self, name=None, id=None, description=None, datatype=None, - value=None, unit=None): + def __init__( + self, + name: Optional[str] = None, + id: Optional[int] = None, + description: Optional[str] = None, + datatype: Union[DATATYPE, None] = None, + value=None, + unit: Optional[str] = None, + ): Entity.__init__(self, id=id, name=name, description=description, datatype=datatype, value=value, role="Property") self.unit = unit - def to_xml(self, xml=None, add_properties=ALL): + def to_xml(self, xml: Optional[etree._Element] = None, add_properties=ALL): if xml is None: xml = etree.Element("Property") @@ -1969,7 +1982,7 @@ class Message(object): self.code = int(code) if code is not None else None self.body = body - def to_xml(self, xml=None): + def to_xml(self, xml: Optional[etree._Element] = None): if xml is None: xml = etree.Element(str(self.type)) @@ -2013,9 +2026,9 @@ class RecordType(Entity): def add_parent( self, - parent=None, - id=None, - name=None, + parent: Union[Entity, int, str, None] = None, + id: Optional[int] = None, + name: Optional[str] = None, inheritance: Union[INHERITANCE, str, None] = OBLIGATORY, ): """Add a parent to this RecordType @@ -2050,11 +2063,21 @@ class RecordType(Entity): return super().add_parent(parent=parent, id=id, name=name, inheritance=inheritance) - def __init__(self, name=None, id=None, description=None, datatype=None): # @ReservedAssignment + def __init__( + self, + name: Optional[str] = None, + id: Optional[int] = None, + description: Optional[str] = None, + datatype: Optional[DATATYPE] = None, + ): # @ReservedAssignment Entity.__init__(self, name=name, id=id, description=description, datatype=datatype, role="RecordType") - def to_xml(self, xml=None, add_properties=ALL): + def to_xml( + self, + xml: Optional[etree._Element] = None, + add_properties: Optional[INHERITANCE] = ALL, + ) -> etree._Element: if xml is None: xml = etree.Element("RecordType") @@ -2150,7 +2173,12 @@ class File(Record): if self.pickup is None: self.pickup = from_location - def to_xml(self, xml=None, add_properties=ALL, local_serialization=False): + def to_xml( + self, + xml: Optional[etree._Element] = None, + add_properties: Optional[INHERITANCE] = ALL, + local_serialization: bool = False, + ): """Convert this file to an xml element. @return: xml element @@ -2246,6 +2274,7 @@ class File(Record): class _Properties(list): + """FIXME: Add docstring.""" def __init__(self): list.__init__(self) @@ -2280,7 +2309,12 @@ class _Properties(list): return self - def append(self, property, importance=None, inheritance=None): # @ReservedAssignment + def append( + self, + property: Union[List[Entity], Entity], + importance=None, + inheritance: Union[str, INHERITANCE, None] = None, + ): # @ReservedAssignment if isinstance(property, list): for p in property: self.append(p, importance, inheritance) @@ -2919,7 +2953,7 @@ class Container(list): return error_list - def get_entity_by_name(self, name, case_sensitive=True): + def get_entity_by_name(self, name: str, case_sensitive: bool = True): """Get the first entity which has the given name. Note: If several entities are in this list which have the same name, this method will only return the first and ignore the others. @@ -3177,8 +3211,14 @@ class Container(list): raise LinkAheadException( "The server's response didn't contain the expected elements. The configuration of this client might be invalid (especially the url).") - def _sync(self, container, unique, raise_exception_on_error, - name_case_sensitive=False, strategy=_basic_sync): + def _sync( + self, + container: Container, + unique: bool, + raise_exception_on_error: bool, + name_case_sensitive: bool = False, + strategy=_basic_sync, + ): """Synchronize this container (C1) with another container (C2). That is: 1) Synchronize any entity e1 in C1 with the @@ -3224,13 +3264,18 @@ class Container(list): self._timestamp = container._timestamp self._srid = container._srid - def _calc_sync_dict(self, remote_container, unique, - raise_exception_on_error, name_case_sensitive): + def _calc_sync_dict( + self, + remote_container: Container, + unique: bool, + raise_exception_on_error: bool, + name_case_sensitive: bool, + ): # self is local, remote_container is remote. # which is to be synced with which: # sync_dict[local_entity]=sync_remote_enities - sync_dict = dict() + sync_dict: Dict[Entity, Optional[List[Entity]]] = dict() # list of remote entities which already have a local equivalent used_remote_entities = [] @@ -3371,7 +3416,7 @@ class Container(list): return sync_dict @staticmethod - def _find_dependencies_in_container(container): + def _find_dependencies_in_container(container: Container): """Find elements in a container that are a dependency of another element of the same. Parameters @@ -3564,8 +3609,14 @@ class Container(list): return self - def retrieve(self, query=None, unique=True, - raise_exception_on_error=True, sync=True, flags=None): + def retrieve( + self, + query: Union[str, list, None] = None, + unique: bool = True, + raise_exception_on_error: bool = True, + sync: bool = True, + flags=None, + ): """Retrieve all entities in this container identified via their id if present and via their name otherwise. Any locally already existing attributes (name, description, ...) will be preserved. Any such @@ -3853,25 +3904,25 @@ class Container(list): self._linearize() # TODO: This is a possible solution for ticket#137 -# retrieved = Container() -# for entity in self: -# if entity.is_valid(): -# retrieved.append(entity) -# if len(retrieved)>0: -# retrieved = retrieved.retrieve(raise_exception_on_error=False, sync=False) -# for e_remote in retrieved: -# if e_remote.id is not None: -# try: -# self.get_entity_by_id(e_remote.id).is_valid=e_remote.is_valid -# continue -# except KeyError: -# pass -# if e_remote.name is not None: -# try: -# self.get_entity_by_name(e_remote.name).is_valid=e_remote.is_valid -# continue -# except KeyError: -# pass + # retrieved = Container() + # for entity in self: + # if entity.is_valid(): + # retrieved.append(entity) + # if len(retrieved)>0: + # retrieved = retrieved.retrieve(raise_exception_on_error=False, sync=False) + # for e_remote in retrieved: + # if e_remote.id is not None: + # try: + # self.get_entity_by_id(e_remote.id).is_valid=e_remote.is_valid + # continue + # except KeyError: + # pass + # if e_remote.name is not None: + # try: + # self.get_entity_by_name(e_remote.name).is_valid=e_remote.is_valid + # continue + # except KeyError: + # pass for entity in self: if entity.is_valid(): continue @@ -4560,7 +4611,7 @@ class Query(): raise_exception_on_error: bool = True, cache: bool = True, page_length: Optional[int] = None, - ): + ) -> Union[Container, int]: """Execute a query (via a server-requests) and return the results. Parameters @@ -4654,7 +4705,7 @@ def execute_query( cache: bool = True, flags: Optional[Dict[str, str]] = None, page_length: Optional[int] = None, -): +) -> Union[Container, int]: """Execute a query (via a server-requests) and return the results. Parameters