-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Primitives, enum and CIM_datatype (#39)
## Enum classes Correctly defining classes with defined values, using Enum. (Issue: #38 ) Examples: `UnitSymbol` or `UnitMultiplier` ## Primitives classes The current model generates classes for primitives, which are then rarely used to define the other classes. Instead of creating individual classes for each primitives, a generic `Primitive` class is created. Examples: `Float = Primitive(name="Float", type=float, profiles=[Profile.EQBD, Profile.OP, Profile.SSH, Profile.EQ, Profile.DY, Profile.DL, Profile.SV, Profile.SC, ])` ## Common CIMDatatype class The current model generates datatypes that are then never used in the cim classes (example: `CurrentFlow`). Instead of creating multiple unused classes with the same attributes (multiplier, unit, value), this pr creates a generic `CIMDatatype` class. (Issue: #38 ) Examples: `CurrentFlow = CIMDatatype(name="CurrentFlow", type=float, symbol=UnitSymbol.A, multiplier=UnitMultiplier.none, profiles=[Profile.SSH,Profile.EQ,Profile.SV,Profile.SC,])` ## Annotation of the data type As said above, the newly created datatypes are added as annotation (`data_type`) when creating the other classes. ``` @DataClass class CurrentLimit(OperationalLimit): """ Operational limit on current. normalValue: The normal value for limit on current flow. The attribute shall be a positive value or zero. value: Limit on current flow. The attribute shall be a positive value or zero. """ normalValue : float = Field(default=0.0, data_type = CurrentFlow, json_schema_extra={"in_profiles":[Profile.EQ, ]}) value : float = Field(default=0.0, data_type = CurrentFlow, json_schema_extra={"in_profiles":[Profile.SSH, ]}) @cached_property def possible_profiles(self)->set[BaseProfile]: """ A resource can be used by multiple profiles. This is the set of profiles where this element can be found. """ return { Profile.EQ, Profile.SSH, } ```
- Loading branch information
Showing
11 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ __pycache__/ | |
.vscode/* | ||
*/.DS_Store | ||
.DS_Store | ||
tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
cimgen/languages/modernpython/templates/datatype_template.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen | ||
""" | ||
|
||
from ..utils.datatypes import CIMDatatype | ||
from ..utils.profile import Profile | ||
{{#isFixed_imports}} | ||
from .{{.}} import {{.}} | ||
{{/isFixed_imports}} | ||
|
||
|
||
{{class_name}} = CIMDatatype( | ||
name="{{class_name}}", | ||
type={{python_type}}, | ||
{{#attributes}} | ||
{{#isFixed}} | ||
{{label}}={{attribute_class}}.{{isFixed}}, | ||
{{/isFixed}} | ||
{{/attributes}} | ||
profiles=[{{#class_origin}} | ||
Profile.{{origin}},{{/class_origin}} | ||
], | ||
) | ||
|
||
""" | ||
{{{wrapped_class_comment}}} | ||
""" |
15 changes: 15 additions & 0 deletions
15
cimgen/languages/modernpython/templates/enum_template.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen | ||
""" | ||
|
||
from enum import Enum | ||
|
||
|
||
class {{class_name}}(str, Enum): | ||
""" | ||
{{{class_comment}}} # noqa: E501 | ||
""" | ||
|
||
{{#enum_instances}} | ||
{{label}} = "{{label}}"{{#comment}} # {{comment}}{{/comment}} # noqa: E501 | ||
{{/enum_instances}} |
19 changes: 19 additions & 0 deletions
19
cimgen/languages/modernpython/templates/primitive_template.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen | ||
""" | ||
|
||
from datetime import date, datetime, time | ||
from ..utils.datatypes import Primitive | ||
from ..utils.profile import Profile | ||
|
||
{{class_name}} = Primitive( | ||
name="{{class_name}}", | ||
type={{python_type}}, | ||
profiles=[{{#class_origin}} | ||
Profile.{{origin}},{{/class_origin}} | ||
], | ||
) | ||
|
||
""" | ||
{{{wrapped_class_comment}}} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pydantic import Field | ||
from typing import List, Optional, Union | ||
|
||
from .constants import NAMESPACES | ||
from pydantic.dataclasses import dataclass | ||
|
||
from .config import cgmes_resource_config | ||
from .profile import BaseProfile | ||
from ..resources.UnitMultiplier import UnitMultiplier | ||
from ..resources.UnitSymbol import UnitSymbol | ||
from ..resources.Currency import Currency | ||
|
||
|
||
@dataclass(config=cgmes_resource_config) | ||
class Primitive: | ||
|
||
name: str = Field(frozen=True) | ||
type: object = Field(frozen=True) | ||
namespace: str = Field(frozen=True, default=NAMESPACES["cim"]) | ||
profiles: List[BaseProfile] = Field(frozen=True) | ||
|
||
|
||
@dataclass(config=cgmes_resource_config) | ||
class CIMDatatype(Primitive): | ||
|
||
multiplier: Optional[UnitMultiplier] = Field(default=None, frozen=True) | ||
unit: Optional[Union[UnitSymbol, Currency]] = Field(default=None, frozen=True) | ||
denominatorMultiplier: Optional[UnitMultiplier] = Field(default=None, frozen=True) | ||
denominatorUnit: Optional[UnitSymbol] = Field(default=None, frozen=True) |