Skip to content

Commit 1a7fd85

Browse files
committed
add .to_pb decorator too, and docs
Signed-off-by: Max Chesterfield <max.chesterfield@zepben.com>
1 parent a19104f commit 1a7fd85

3 files changed

Lines changed: 110 additions & 143 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ to prevent the test from timing out while you step through the code:
6262
* ```import <new_class_name> as PB<new_class_name>```
6363
* Add ```def <new_class_name>_to_pb```
6464
* Add ```"<new_class_name>_to_pb"``` to ```__all__```
65-
* Add ```<new_class_name>.to_pb = <new_class_name>_to_pb```
65+
* Annotate ```<new_class_name>_to_pb``` with the ```@bind_to_pb``` decorator
6666
1. Update [network_proto2cim.py](src/zepben/ewb/services/network/translator/network_proto2cim.py)
6767
* ```import <new_class_name> as PB<new_class_name>```
6868
* Add ```def <new_class_name>_to_pb```

src/zepben/ewb/services/common/translator/base_cim2proto.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
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
912
from google.protobuf.timestamp_pb2 import Timestamp as PBTimestamp
1013
from zepben.protobuf.cim.iec61968.common.Document_pb2 import Document as PBDocument
@@ -23,10 +26,23 @@
2326
from 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
3046
def 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
4764
def organisation_to_pb(cim: Organisation) -> PBOrganisation:
4865
return PBOrganisation(io=identified_object_to_pb(cim))
4966

5067

68+
@bind_to_pb
5169
def 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
6781
def 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
7691
def 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
8399
def 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

Comments
 (0)