Skip to content
Snippets Groups Projects

Allow property and parent lists to be filtered.

Merged Henrik tom Wörden requested to merge f-lists into dev
Files
3
@@ -35,19 +35,19 @@ transactions.
from __future__ import annotations # Can be removed with 3.10.
from __future__ import print_function, unicode_literals
from enum import Enum
import re
import sys
import warnings
from builtins import str
from copy import deepcopy
from enum import Enum
from functools import cmp_to_key
from hashlib import sha512
from os import listdir
from os.path import isdir
from random import randint
from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING
if TYPE_CHECKING and sys.version_info > (3, 7):
@@ -57,7 +57,6 @@ if TYPE_CHECKING and sys.version_info > (3, 7):
from tempfile import _TemporaryFileWrapper
from io import BufferedWriter
from warnings import warn
from lxml import etree
@@ -154,8 +153,8 @@ class Entity:
self.datatype: Optional[DATATYPE] = datatype
self.value = value
self.messages = Messages()
self.properties = _Properties()
self.parents = _ParentList()
self.properties = PropertyList()
self.parents = ParentList()
self.path: Optional[str] = None
self.file: Optional[File] = None
self.unit: Optional[str] = None
@@ -899,7 +898,7 @@ out: bool
def get_parents(self):
"""Get all parents of this entity.
@return: _ParentList(list)
@return: ParentList(list)
"""
return self.parents
@@ -999,7 +998,7 @@ out: List[Entity]
def get_properties(self):
"""Get all properties of this entity.
@return: _Properties(list)
@return: PropertyList(list)
"""
return self.properties
@@ -2363,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()
@@ -2458,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.
@@ -2515,7 +2527,7 @@ class _Properties(list):
raise KeyError(str(prop) + " not found.")
class _ParentList(list):
class ParentList(list):
# TODO unclear why this class is private. Isn't it use full for users?
def _get_entity_by_cuid(self, cuid):
@@ -2532,10 +2544,15 @@ class _ParentList(list):
return e
raise KeyError("No entity with that cuid in this container.")
def __init__(self):
list.__init__(self)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._element_by_name = dict()
self._element_by_id = dict()
for el in self:
if el.name is not None:
self._element_by_name[el.name] = el
if el.id is not None:
self._element_by_name[str(el.id)] = el
def extend(self, parents):
self.append(parents)
@@ -2596,6 +2613,16 @@ class _ParentList(list):
return xml2str(xml)
def __contains__(self, parent):
missing = False
if parent.name is not None:
if parent.name not in self._element_by_name:
missing = True
if parent.id is not None:
if str(parent.id) not in self._element_by_id:
missing = True
return not missing
def remove(self, parent: Union[Entity, int, str]):
if isinstance(parent, Entity):
if parent in self:
@@ -2637,6 +2664,19 @@ 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)."))
super().__init__(*args, **kwargs)
class Messages(list):
"""This specialization of list stores error, warning, info, and other
messages. The mentioned three messages types play a special role.
Loading