forked from frequenz-floss/frequenz-client-assets-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_connection_proto.py
More file actions
95 lines (76 loc) · 3.14 KB
/
_connection_proto.py
File metadata and controls
95 lines (76 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# License: MIT
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
"""Loading of ComponentConnection objects from protobuf messages."""
import logging
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
electrical_components_pb2,
)
from frequenz.client.common.microgrid.electrical_components import ElectricalComponentId
from .._lifetime import Lifetime
from .._lifetime_proto import lifetime_from_proto
from ._connection import ComponentConnection
_logger = logging.getLogger(__name__)
def component_connection_from_proto(
message: electrical_components_pb2.ElectricalComponentConnection,
) -> ComponentConnection | None:
"""Create a `ComponentConnection` from a protobuf message."""
major_issues: list[str] = []
minor_issues: list[str] = []
connection = component_connection_from_proto_with_issues(
message, major_issues=major_issues
)
if major_issues:
_logger.warning(
"Found issues in component connection: %s | Protobuf message:\n%s",
", ".join(major_issues),
message,
)
if minor_issues:
_logger.debug(
"Found minor issues in component connection: %s | Protobuf message:\n%s",
", ".join(minor_issues),
message,
)
return connection
def component_connection_from_proto_with_issues(
message: electrical_components_pb2.ElectricalComponentConnection,
*,
major_issues: list[str],
) -> ComponentConnection | None:
"""Create a `ComponentConnection` from a protobuf message collecting issues.
This function is useful when you want to collect issues during the parsing
of multiple connections, rather than logging them immediately.
Args:
message: The protobuf message to parse.
major_issues: A list to collect major issues found during parsing.
Returns:
A `ComponentConnection` object created from the protobuf message, or
`None` if the protobuf message is completely invalid and a
`ComponentConnection` cannot be created.
"""
source_component_id = ElectricalComponentId(message.source_electrical_component_id)
destination_component_id = ElectricalComponentId(
message.destination_electrical_component_id
)
if source_component_id == destination_component_id:
major_issues.append(
f"connection ignored: source and destination are the same ({source_component_id})",
)
return None
try:
lifetime = _get_operational_lifetime_from_proto(message)
except ValueError as exc:
major_issues.append(f"connection ignored: invalid operational lifetime ({exc})")
return None
return ComponentConnection(
source=source_component_id,
destination=destination_component_id,
operational_lifetime=lifetime,
)
def _get_operational_lifetime_from_proto(
message: electrical_components_pb2.ElectricalComponentConnection,
) -> Lifetime:
"""Get the operational lifetime from a protobuf message."""
if message.HasField("operational_lifetime"):
return lifetime_from_proto(message.operational_lifetime)
return Lifetime()