Skip to content

Commit

Permalink
Fix namespace of attributes after review comment
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Roger <guillaume.roger@alliander.com>
  • Loading branch information
guillaume-alliander committed Oct 20, 2023
1 parent f58edb1 commit 0213c8c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 207 deletions.
202 changes: 0 additions & 202 deletions modernpython/Base.py

This file was deleted.

31 changes: 27 additions & 4 deletions modernpython/utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from functools import cached_property
from typing import Any, TypeAlias, TypedDict

from pycgmes.utils.constants import NAMESPACES
from pydantic.dataclasses import dataclass

from .dataclassconfig import DataclassConfig
Expand Down Expand Up @@ -51,6 +52,13 @@ def resource_name(self) -> str:
"""Returns the resource type."""
return self.__class__.__name__

@cached_property
def namespace(self) -> str:
"""Returns the namespace. By default, the namespace is the cim namespace for all resources.
Custom resources can override this.
"""
return NAMESPACES["cim"]

@classmethod # From python 3.11, you cannot wrap @classmethod in @property anymore.
def apparent_name(cls) -> str:
"""
Expand Down Expand Up @@ -109,12 +117,25 @@ def cgmes_attributes_in_profile(self, profile: BaseProfile | None) -> dict[str,
# Wrong profile or already found from a parent.
continue
else:
# Namespace finding
# "class namespace" means the first namespace defined in the inheritance tree.
# This can go up to Base, which will give the default cim NS.
if (extra := getattr(f.default, "extra", None)) is None:
# The attribute does not have extra metadata. It might be a custom atttribute
# without it, or a base type (int...).
# Use the class namespace.
namespace = self.namespace
elif (attr_ns := extra.get("namespace", None)) is None:
# The attribute has some extras, but not namespace.
# Use the class namespace.
namespace = self.namespace
else:
# The attribute has an explicit namesapce
namespace = attr_ns

qual_attrs[qualname] = CgmesAttribute(
value=getattr(self, shortname),
# base types (e.g. int) do not have extras
namespace=extra.get("namespace", None)
if (extra := getattr(f.default, "extra", None))
else None,
namespace=namespace,
)
seen_attrs.add(shortname)

Expand All @@ -123,6 +144,8 @@ def cgmes_attributes_in_profile(self, profile: BaseProfile | None) -> dict[str,
def __str__(self) -> str:
"""Returns the string representation of this resource."""
return "\n".join([f"{k}={v}" for k, v in sorted(self.to_dict().items())])


CgmesAttributeTypes: TypeAlias = str | int | float | Base | list | None


Expand Down
3 changes: 2 additions & 1 deletion modernpython/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Default namespaces used by CGMES.
NAMESPACES = {
"cim": "http://iec.ch/TC57/2013/CIM-schema-cim16#",
"cim": "http://iec.ch/TC57/CIM100#",
"entsoe": "http://entsoe.eu/CIM/SchemaExtension/3/1#",
"md": "http://iec.ch/TC57/61970-552/ModelDescription/1#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
}
2 changes: 2 additions & 0 deletions modernpython/utils/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class BaseProfile(str, Enum):
Enum with fields cannot be inherited or composed, just create your own CustomProfile without
trying to extend Profile. It will work.
"""

@cached_property
def long_name(self) -> str:
"""Return the long name of the profile."""
return self.value


class Profile(BaseProfile):
"""
Enum containing all CGMES profiles and their export priority.
Expand Down

0 comments on commit 0213c8c

Please sign in to comment.