diff --git a/src/linkahead/common/models.py b/src/linkahead/common/models.py index 2dc7f7a0d46f2d5f0b6ee868a92f71858cf9b984..084c7e9823fe9ddb2cffbc0eea440cf9abf54109 100644 --- a/src/linkahead/common/models.py +++ b/src/linkahead/common/models.py @@ -38,6 +38,7 @@ from __future__ import print_function, unicode_literals import re import sys +import warnings from builtins import str from copy import deepcopy from enum import Enum @@ -2361,11 +2362,14 @@ class File(Record): value=value, unit=unit, importance=importance, inheritance=inheritance) -class _Properties(list): - """FIXME: Add docstring.""" +class PropertyList(list): + """A list class for Property objects + + This class provides addional functionality like get/set_importance or get_by_name. + """ def __init__(self): - list.__init__(self) + super().__init__() self._importance: Dict[Entity, IMPORTANCE] = dict() self._inheritance: Dict[Entity, INHERITANCE] = dict() self._element_by_name: Dict[str, Entity] = dict() @@ -2456,6 +2460,16 @@ class _Properties(list): return xml2str(xml) + def __contains__(self, prop): + missing = False + if prop.name is not None: + if prop.name not in self._element_by_name: + missing = True + if prop.id is not None: + if str(prop.id) not in self._element_by_id: + missing = True + return not missing + def _get_entity_by_cuid(self, cuid: str): ''' Get the first entity which has the given cuid. @@ -2650,10 +2664,16 @@ class ParentList(list): raise KeyError(str(parent) + " not found.") +class _Properties(PropertyList): + def __init__(self, *args, **kwargs): + warnings.warn(DeprecationWarning("This class is depricated. Please use PropertyList.")) + super().__init__(*args, **kwargs) + + class _ParentList(ParentList): def __init__(self, *args, **kwargs): warnings.warn(DeprecationWarning("This class is depricated. Please use ParentList " - "(without underscore.")) + "(without underscore).")) super().__init__(*args, **kwargs) diff --git a/unittests/test_entity.py b/unittests/test_entity.py index d48c9ad71ad709b296ad48f529e6e11aaef87791..73b65df9d4347d13da8c673fbdd7ddbd39cd616e 100644 --- a/unittests/test_entity.py +++ b/unittests/test_entity.py @@ -116,3 +116,22 @@ def test_parent_list(): assert RecordType(id=101) in pl assert RecordType(id=103) in pl assert not RecordType(id=105, name="B") in pl + + +def test_property_list(): + p1 = Property(name="A") + pl = linkahead.common.models.PropertyList() + pl.append(p1) + assert p1 in pl + assert Property(name="A") in pl + assert not Property(id=101) in pl + pl.append(Property(id=101)) + assert Property(name="A") in pl + assert Property(id=101) in pl + assert not Property(id=102) in pl + pl.append(Property(id=103, name='B')) + assert Property(name="A") in pl + assert Property(name="B") in pl + assert Property(id=101) in pl + assert Property(id=103) in pl + assert not Property(id=105, name="B") in pl