55
66__all__ = ["identified_object_to_pb" , "document_to_pb" , "organisation_role_to_pb" , "organisation_to_pb" ]
77
8+ import inspect
9+ from typing import ParamSpec , TypeVar , Callable
10+
811# noinspection PyPackageRequirements,PyUnresolvedReferences
912from google .protobuf .timestamp_pb2 import Timestamp as PBTimestamp
1013from zepben .protobuf .cim .iec61968 .common .Document_pb2 import Document as PBDocument
2326from zepben .ewb .services .common .translator .util import mrid_or_empty
2427
2528
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+
2641###################
2742# IEC61968 Common #
2843###################
2944
45+ @bind_to_pb
3046def document_to_pb (cim : Document ) -> PBDocument :
3147 timestamp = None
3248 if cim .created_date_time :
@@ -44,26 +60,24 @@ def document_to_pb(cim: Document) -> PBDocument:
4460 )
4561
4662
63+ @bind_to_pb
4764def organisation_to_pb (cim : Organisation ) -> PBOrganisation :
4865 return PBOrganisation (io = identified_object_to_pb (cim ))
4966
5067
68+ @bind_to_pb
5169def organisation_role_to_pb (cim : OrganisationRole ) -> PBOrganisationRole :
5270 return PBOrganisationRole (
5371 io = identified_object_to_pb (cim ),
5472 organisationMRID = mrid_or_empty (cim .organisation )
5573 )
5674
5775
58- Document .to_pb = document_to_pb
59- Organisation .to_pb = organisation_to_pb
60- OrganisationRole .to_pb = organisation_role_to_pb
61-
62-
6376######################
6477# IEC61970 Base Core #
6578######################
6679
80+ @bind_to_pb
6781def identified_object_to_pb (cim : IdentifiedObject ) -> PBIdentifiedObject :
6882 return PBIdentifiedObject (
6983 mRID = str (cim .mrid ),
@@ -73,20 +87,17 @@ def identified_object_to_pb(cim: IdentifiedObject) -> PBIdentifiedObject:
7387 )
7488
7589
90+ @bind_to_pb
7691def name_to_pb (cim : Name ) -> PBName :
7792 return PBName (
7893 name = cim .name ,
7994 type = cim .type .name if cim .type else None
8095 )
8196
8297
98+ @bind_to_pb
8399def name_type_to_pb (cim : NameType ) -> PBNameType :
84100 return PBNameType (
85101 name = cim .name ,
86102 description = cim .description
87103 )
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
0 commit comments