Skip to content

Commit

Permalink
Add human-readable object representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanna-Shalamitskaya-EPAM committed Nov 29, 2024
1 parent f6924d5 commit 9db2999
Show file tree
Hide file tree
Showing 40 changed files with 450 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ def parent_elements(self, elements: list["Base"]) -> None:
@abstractmethod
def append_parent_element(self, element: "Base") -> None:
"""Add parent element."""

def __repr__(self):
"""Object representation."""
return f"{self.__class__.__name__}({self.name})"
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ def is_scalar(self) -> bool:
def is_complex(self) -> bool:
"""Is complex flag."""
return not self.is_scalar

def __repr__(self):
return f"{self.__class__.__name__}({self.urn})"
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ class Scalar(DataType, ABC):
def is_scalar(self) -> bool:
"""Is scalar flag."""
return True

def __repr__(self):
return f"{self.__class__.__name__}({self.urn})"
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
class BaseImpl(Base, metaclass=abc.ABCMeta):
"""Base Implemented class."""

SCALAR_ATTR_NAMES = ["meta_model_version", "urn", "preferred_names", "descriptions"]
LIST_ATTR_NAMES = ["see"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes):
self._meta_model_version = meta_model_base_attributes.meta_model_version
self._urn = meta_model_base_attributes.urn
Expand Down Expand Up @@ -75,3 +78,60 @@ def urn(self) -> Optional[str]:
def name(self) -> str:
"""Name."""
return self._name

def _get_base_message(self):
"""Get base string message."""
message = self.__class__.__name__
message = message.replace("Default", "")
message = f"({message}){self.name}"

return message

@staticmethod
def _prepare_attr_message(name, value):
"""Prepare a message with scalar attribute value."""
message = f"{name}: "
if isinstance(value, dict):
for k, v in value.items():
message += f"\n\t\t{k.upper()}: {v}"
else:
message += repr(value) if isinstance(value, BaseImpl) else str(value)

return message

def _get_scalar_attr_info(self):
"""Get info about all scalar attributes."""
message = ""
for attr_name in self.SCALAR_ATTR_NAMES:
attr_value = getattr(self, attr_name, None)
if attr_value:
message += f"\n\t{self._prepare_attr_message(attr_name, attr_value)}"

return message

@staticmethod
def _prepare_list_attr_message(name, value):
"""Prepare a message for the list data type attribute value."""
message = f"{name}:"
for elem in value:
message += f"\n\t\t{elem.name}"

return message

def _get_list_attr_info(self):
"""Get info about all list data type attributes."""
message = ""
for attr_name in self.LIST_ATTR_NAMES:
attr_value = getattr(self, attr_name, [])
if attr_value:
message += f"\n\t{self._prepare_list_attr_message(attr_name, attr_value)}"

return message

def __str__(self):
"""String representation."""
message = self._get_base_message()
message += self._get_scalar_attr_info()
message += self._get_list_attr_info()

return message
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class DefaultCollection(DefaultCharacteristic, Collection):
"""Default Collection class."""

SCALAR_ATTR_NAMES = DefaultCharacteristic.SCALAR_ATTR_NAMES + ["element_characteristic"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class DefaultCharacteristic(BaseImpl, Characteristic):
"""Default Characteristic class."""

SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["data_type"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, data_type: DataType):
super().__init__(meta_model_base_attributes)
self._data_type = data_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class DefaultEnumeration(DefaultCharacteristic, Enumeration):
"""Default Enumeration class."""

LIST_ATTR_NAMES = DefaultCharacteristic.LIST_ATTR_NAMES + ["values"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class DefaultState(DefaultEnumeration, State):
"""Default State class."""

SCALAR_ATTR_NAMES = DefaultEnumeration.SCALAR_ATTR_NAMES + ["default_value"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
class DefaultStructuredValue(DefaultCharacteristic, StructuredValue):
"""Default Structured Value class"""

SCALAR_ATTR_NAMES = DefaultCharacteristic.SCALAR_ATTR_NAMES + ["deconstruction_rule"]
LIST_ATTR_NAMES = DefaultCharacteristic.LIST_ATTR_NAMES + ["elements"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
class DefaultTrait(DefaultCharacteristic, Trait):
"""Default Trait class."""

SCALAR_ATTR_NAMES = DefaultCharacteristic.SCALAR_ATTR_NAMES + ["base_characteristic"]
LIST_ATTR_NAMES = DefaultCharacteristic.LIST_ATTR_NAMES + ["constraints"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class DefaultQuantifiable(DefaultCharacteristic, Quantifiable):
"""Default Quantifiable class."""

LIST_ATTR_NAMES = DefaultCharacteristic.LIST_ATTR_NAMES + ["unit"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class DefaultEncodingConstraint(DefaultConstraint, EncodingConstraint):
"""Default Encoding Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["value"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, value: str):
super().__init__(meta_model_base_attributes)
self._value = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class DefaultFixedPointConstraint(DefaultConstraint, FixedPointConstraint):
"""Default Fixed Point Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["scale", "integer"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class DefaultLanguageConstraint(DefaultConstraint, LanguageConstraint):
"""Default Language Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["language_code"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, language_code: str):
super().__init__(meta_model_base_attributes)
self._language_code = language_code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class DefaultLengthConstraint(DefaultConstraint, LengthConstraint):
"""Default Length Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["min_value", "max_value"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class DefaultLocaleConstraint(DefaultConstraint, LocaleConstraint):
"""Default Locale Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["locale_code"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, locale_code: str):
super().__init__(meta_model_base_attributes)
self._locale_code = locale_code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
class DefaultRangeConstraint(DefaultConstraint, RangeConstraint):
"""Default Range Constraint class."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + [
"min_value",
"max_value",
"lower_bound_definition",
"upper_bound_definition",
]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class DefaultRegularExpressionConstraint(DefaultConstraint, RegularExpressionConstraint):
"""Default Regular Expression Constraint."""

SCALAR_ATTR_NAMES = DefaultConstraint.SCALAR_ATTR_NAMES + ["value"]

def __init__(self, meta_model_base_attributes: MetaModelBaseAttributes, value: str):
super().__init__(meta_model_base_attributes)
self._value = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class DefaultAbstractEntity(DefaultComplexType, AbstractEntity):
"""Default Abstract Entity class."""

LIST_ATTR_NAMES = DefaultComplexType.LIST_ATTR_NAMES + ["extending_elements"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class DefaultComplexType(BaseImpl, ComplexType):
"""

_instances: Dict[str, ComplexType] = {}
SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["extends"]
LIST_ATTR_NAMES = BaseImpl.LIST_ATTR_NAMES + ["properties"]

def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ def urn(self) -> str:
def meta_model_version(self) -> str:
"""Meta model version."""
return self._meta_model_version

def __str__(self):
message = self.__class__.__name__
message = message.replace("Default", "")
message += f":\n\tmeta_model_version: {self.meta_model_version}\n\turn: {self.urn}"

return message
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ def urn(self) -> str:
def meta_model_version(self) -> str:
"""Meta model version."""
return self._meta_model_version

def __str__(self):
message = self.__class__.__name__
message = message.replace("Default", "")
message += f":\n\tmeta_model_version: {self.meta_model_version}\n\turn: {self.urn}"

return message
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
class DefaultAspect(Aspect, BaseImpl):
"""Default Aspect class."""

LIST_ATTR_NAMES = BaseImpl.LIST_ATTR_NAMES + ["properties", "operations", "events"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
class DefaultEither(BaseImpl, Either):
"""Default Either class."""

SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["left", "right"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class DefaultEvent(BaseImpl, Event):
"""Default Event class."""

LIST_ATTR_NAMES = BaseImpl.LIST_ATTR_NAMES + ["parameters"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
class DefaultOperation(BaseImpl, Operation):
"""Default Operation class."""

SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["output_property"]
LIST_ATTR_NAMES = BaseImpl.LIST_ATTR_NAMES + ["input_properties"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
class DefaultProperty(BaseImpl, Property):
"""Default Property class."""

SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + [
"characteristic",
"example_value",
"extends",
"optional",
"not_in_payload",
"payload_name",
]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
class DefaultUnit(BaseImpl, Unit):
"""Default Unit class."""

SCALAR_ATTR_NAMES = BaseImpl.SCALAR_ATTR_NAMES + ["symbol", "code", "reference_unit", "conversion_factor"]
LIST_ATTR_NAMES = BaseImpl.LIST_ATTR_NAMES + ["quantity_kinds"]

def __init__(
self,
meta_model_base_attributes: MetaModelBaseAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ def _get_data_type(self, element_node: Node) -> Optional[DataType]:
subject=element_characteristic_node,
predicate=self._samm.get_urn(SAMM.data_type),
)
if not data_type_node:
data_type_node = self._aspect_graph.value(
subject=element_characteristic_node,
predicate=rdflib.RDF.type,
)
else:
data_type_node = self._aspect_graph.value(
subject=element_node,
Expand Down
1 change: 1 addition & 0 deletions core/esmf-aspect-meta-model-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ documentation = "https://eclipse-esmf.github.io/python-sdk-guide/index.html"

[tool.poetry.dependencies]
python = "^3.10"
keyboard = "^0.13"
rdflib = "^6.2.0"
requests = "^2.28.1"
tox = "^4.5.2"
Expand Down
Empty file.
Empty file.
Loading

0 comments on commit 9db2999

Please sign in to comment.