zepben.ewb 1.0.0b7__py3-none-any.whl → 1.0.0b8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,9 @@
5
5
 
6
6
  __all__ = ["identified_object_to_pb", "document_to_pb", "organisation_role_to_pb", "organisation_to_pb"]
7
7
 
8
+ import inspect
9
+ from typing import ParamSpec, TypeVar, Callable
10
+
8
11
  # noinspection PyPackageRequirements,PyUnresolvedReferences
9
12
  from google.protobuf.timestamp_pb2 import Timestamp as PBTimestamp
10
13
  from zepben.protobuf.cim.iec61968.common.Document_pb2 import Document as PBDocument
@@ -23,10 +26,23 @@ from zepben.ewb.model.cim.iec61970.base.core.name_type import NameType
23
26
  from zepben.ewb.services.common.translator.util import mrid_or_empty
24
27
 
25
28
 
29
+ P = ParamSpec("P")
30
+ R = TypeVar("R")
31
+
32
+
33
+ def bind_to_pb(func: Callable[P, R]) -> Callable[P, R]:
34
+ """
35
+ Get the object described in the type hint of the first argument of the function we are wrapping
36
+ set that object's `to_pb` function to be the function we are wrapping
37
+ """
38
+ inspect.get_annotations(func, eval_str=True)[func.__code__.co_varnames[0]].to_pb = func
39
+ return func
40
+
26
41
  ###################
27
42
  # IEC61968 Common #
28
43
  ###################
29
44
 
45
+ @bind_to_pb
30
46
  def document_to_pb(cim: Document) -> PBDocument:
31
47
  timestamp = None
32
48
  if cim.created_date_time:
@@ -44,10 +60,12 @@ def document_to_pb(cim: Document) -> PBDocument:
44
60
  )
45
61
 
46
62
 
63
+ @bind_to_pb
47
64
  def organisation_to_pb(cim: Organisation) -> PBOrganisation:
48
65
  return PBOrganisation(io=identified_object_to_pb(cim))
49
66
 
50
67
 
68
+ @bind_to_pb
51
69
  def organisation_role_to_pb(cim: OrganisationRole) -> PBOrganisationRole:
52
70
  return PBOrganisationRole(
53
71
  io=identified_object_to_pb(cim),
@@ -55,15 +73,11 @@ def organisation_role_to_pb(cim: OrganisationRole) -> PBOrganisationRole:
55
73
  )
56
74
 
57
75
 
58
- Document.to_pb = document_to_pb
59
- Organisation.to_pb = organisation_to_pb
60
- OrganisationRole.to_pb = organisation_role_to_pb
61
-
62
-
63
76
  ######################
64
77
  # IEC61970 Base Core #
65
78
  ######################
66
79
 
80
+ @bind_to_pb
67
81
  def identified_object_to_pb(cim: IdentifiedObject) -> PBIdentifiedObject:
68
82
  return PBIdentifiedObject(
69
83
  mRID=str(cim.mrid),
@@ -73,6 +87,7 @@ def identified_object_to_pb(cim: IdentifiedObject) -> PBIdentifiedObject:
73
87
  )
74
88
 
75
89
 
90
+ @bind_to_pb
76
91
  def name_to_pb(cim: Name) -> PBName:
77
92
  return PBName(
78
93
  name=cim.name,
@@ -80,13 +95,9 @@ def name_to_pb(cim: Name) -> PBName:
80
95
  )
81
96
 
82
97
 
98
+ @bind_to_pb
83
99
  def name_type_to_pb(cim: NameType) -> PBNameType:
84
100
  return PBNameType(
85
101
  name=cim.name,
86
102
  description=cim.description
87
103
  )
88
-
89
-
90
- IdentifiedObject.to_pb = identified_object_to_pb
91
- Name.to_pb = name_to_pb
92
- NameType.to_pb = name_type_to_pb
@@ -3,11 +3,18 @@
3
3
  # License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  # file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
 
6
- __all__ = ["identified_object_to_cim", "document_to_cim", "organisation_to_cim", "organisation_role_to_cim", "BaseProtoToCim"]
6
+ from __future__ import annotations
7
7
 
8
+ __all__ = ["identified_object_to_cim", "document_to_cim", "organisation_to_cim", "organisation_role_to_cim",
9
+ "BaseProtoToCim", "add_to_network_or_none", "bind_to_cim"]
10
+
11
+ import functools
12
+ import inspect
8
13
  from abc import ABCMeta
9
- from typing import Optional
14
+ from typing import Optional, Callable, TypeVar
10
15
 
16
+ from google.protobuf.message import Message
17
+ from typing_extensions import ParamSpec
11
18
  # noinspection PyPackageRequirements
12
19
  from zepben.protobuf.cim.iec61968.common.Document_pb2 import Document as PBDocument
13
20
  from zepben.protobuf.cim.iec61968.common.OrganisationRole_pb2 import OrganisationRole as PBOrganisationRole
@@ -23,10 +30,36 @@ from zepben.ewb.services.common import resolver
23
30
  from zepben.ewb.services.common.base_service import BaseService
24
31
 
25
32
 
33
+ TProtoToCimFunc = Callable[[Message, BaseService], Optional[IdentifiedObject]]
34
+ P = ParamSpec("P")
35
+ R = TypeVar("R")
36
+
37
+
38
+ def add_to_network_or_none(func: TProtoToCimFunc) -> TProtoToCimFunc:
39
+ """
40
+ This should wrap any leaf class of the hierarchy, for example, If you're porting over ewb-sdk-jvm
41
+ changes, any of the classes that get used in a `network.add(Class)`
42
+ """
43
+ @functools.wraps(func)
44
+ def wrapper(pb: Message, service: BaseService) -> Optional[IdentifiedObject]:
45
+ return cim if service.add(cim := func(pb, service)) else None
46
+ return wrapper
47
+
48
+
49
+ def bind_to_cim(func: Callable[P, R]) -> Callable[P, R]:
50
+ """
51
+ Get the object described in the type hint of the first argument of the function we are wrapping
52
+ set that object's `to_cim` function to be the function we are wrapping
53
+ """
54
+ inspect.get_annotations(func, eval_str=True)[func.__code__.co_varnames[0]].to_cim = func
55
+ return func
56
+
57
+
26
58
  ###################
27
59
  # IEC61968 Common #
28
60
  ###################
29
61
 
62
+ @bind_to_cim
30
63
  def document_to_cim(pb: PBDocument, cim: Document, service: BaseService):
31
64
  cim.title = pb.title
32
65
  cim.created_date_time = pb.createdDateTime.ToDatetime() if pb.HasField("createdDateTime") else None
@@ -38,28 +71,27 @@ def document_to_cim(pb: PBDocument, cim: Document, service: BaseService):
38
71
  identified_object_to_cim(pb.io, cim, service)
39
72
 
40
73
 
74
+ @bind_to_cim
75
+ @add_to_network_or_none
41
76
  def organisation_to_cim(pb: PBOrganisation, service: BaseService) -> Optional[Organisation]:
42
77
  cim = Organisation()
43
78
 
44
79
  identified_object_to_cim(pb.io, cim, service)
45
- return cim if service.add(cim) else None
80
+ return cim
46
81
 
47
82
 
83
+ @bind_to_cim
48
84
  def organisation_role_to_cim(pb: PBOrganisationRole, cim: OrganisationRole, service: BaseService):
49
85
  service.resolve_or_defer_reference(resolver.organisation(cim), pb.organisationMRID)
50
86
 
51
87
  identified_object_to_cim(pb.io, cim, service)
52
88
 
53
89
 
54
- PBDocument.to_cim = document_to_cim
55
- PBOrganisation.to_cim = organisation_to_cim
56
- PBOrganisationRole.to_cim = organisation_role_to_cim
57
-
58
-
59
90
  ######################
60
91
  # IEC61970 Base Core #
61
92
  ######################
62
93
 
94
+ @bind_to_cim
63
95
  def identified_object_to_cim(pb: PBIdentifiedObject, cim: IdentifiedObject, service: BaseService):
64
96
  cim.mrid = pb.mRID
65
97
  cim.name = pb.name
@@ -67,6 +99,7 @@ def identified_object_to_cim(pb: PBIdentifiedObject, cim: IdentifiedObject, serv
67
99
  [cim.add_name(name_to_cim(name, cim, service).type, name.name) for name in pb.names]
68
100
 
69
101
 
102
+ @bind_to_cim
70
103
  def name_to_cim(pb: PBName, io: IdentifiedObject, service: BaseService):
71
104
  try:
72
105
  nt = service.get_name_type(pb.type)
@@ -78,6 +111,7 @@ def name_to_cim(pb: PBName, io: IdentifiedObject, service: BaseService):
78
111
  return nt.get_or_add_name(pb.name, io)
79
112
 
80
113
 
114
+ @bind_to_cim
81
115
  def name_type_to_cim(pb: PBNameType, service: BaseService):
82
116
  try:
83
117
  nt = service.get_name_type(pb.name)
@@ -90,11 +124,6 @@ def name_type_to_cim(pb: PBNameType, service: BaseService):
90
124
  return nt
91
125
 
92
126
 
93
- PBIdentifiedObject.to_cim = identified_object_to_cim
94
- PBName.to_cim = name_to_cim
95
- PBNameType.to_cim = name_type_to_cim
96
-
97
-
98
127
  @dataclass(slots=True)
99
128
  class BaseProtoToCim(object, metaclass=ABCMeta):
100
129
  service: BaseService
@@ -49,7 +49,7 @@ def long_or_none(value: int) -> Optional[int]:
49
49
 
50
50
 
51
51
  def str_or_none(value: str) -> Optional[str]:
52
- return value if value else None
52
+ return value or None
53
53
 
54
54
 
55
55
  def from_nullable_int(value: Optional[int]) -> int:
@@ -69,10 +69,7 @@ def from_nullable_long(value: Optional[int]) -> int:
69
69
 
70
70
 
71
71
  def nullable_bool_settings(flag_name: str, value: Optional[bool]) -> Dict:
72
- settings = {}
73
72
  if value is None:
74
- settings[f"{flag_name}Null"] = NullValue.NULL_VALUE
73
+ return {f'{flag_name}Null': NullValue.NULL_VALUE}
75
74
  else:
76
- settings[f"{flag_name}Set"] = value
77
-
78
- return settings
75
+ return {f'{flag_name}Set': value}