Skip to content

Commit

Permalink
Merge pull request #343 from andlaus/convenience_fixes
Browse files Browse the repository at this point in the history
make `Multiplexer.cases` and `EnvironmentDataDescription.env_datas` `NamedItemLists`
  • Loading branch information
andlaus committed Sep 16, 2024
2 parents b452b60 + 42dc435 commit 92bceae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
17 changes: 11 additions & 6 deletions odxtools/environmentdatadescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .encodestate import EncodeState
from .environmentdata import EnvironmentData
from .exceptions import odxraise, odxrequire
from .nameditemlist import NamedItemList
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .odxtypes import ParameterValue, ParameterValueDict
from .parameters.parameter import Parameter
Expand All @@ -35,7 +36,7 @@ class EnvironmentDataDescription(ComplexDop):
# in ODX 2.0.0, ENV-DATAS seems to be a mandatory
# sub-element of ENV-DATA-DESC, in ODX 2.2 it is not
# present
env_datas: List[EnvironmentData]
env_datas: NamedItemList[EnvironmentData]
env_data_refs: List[OdxLinkRef]

@property
Expand All @@ -62,16 +63,20 @@ def from_et(et_element: ElementTree.Element,
param_snpathref = None
if (param_snpathref_elem := et_element.find("PARAM-SNPATHREF")) is not None:
param_snpathref = odxrequire(param_snpathref_elem.get("SHORT-NAME-PATH"))

# ODX 2.0 mandates ENV-DATA-DESC to contain a list of
# ENV-DATAS and no ENV-DATA-REFS while for ODX 2.2 the
# situation is reversed. This means that we will create one
# empty and one non-empty list here. (Which is which depends
# on the version of the standard used by the file.)
env_data_refs = [
odxrequire(OdxLinkRef.from_et(env_data_ref, doc_frags))
for env_data_ref in et_element.iterfind("ENV-DATA-REFS/ENV-DATA-REF")
]

# ODX 2.0.0 says ENV-DATA-DESC could contain a list of ENV-DATAS
env_datas = [
env_datas = NamedItemList([
EnvironmentData.from_et(env_data_elem, doc_frags)
for env_data_elem in et_element.iterfind("ENV-DATAS/ENV-DATA")
]
])

return EnvironmentDataDescription(
param_snref=param_snref,
Expand All @@ -93,7 +98,7 @@ def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
# ODX 2.0 specifies environment data objects here, ODX 2.2
# uses references
if self.env_data_refs:
self.env_datas = [odxlinks.resolve(x) for x in self.env_data_refs]
self.env_datas = NamedItemList([odxlinks.resolve(x) for x in self.env_data_refs])
else:
for ed in self.env_datas:
ed._resolve_odxlinks(odxlinks)
Expand Down
8 changes: 4 additions & 4 deletions odxtools/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .multiplexercase import MultiplexerCase
from .multiplexerdefaultcase import MultiplexerDefaultCase
from .multiplexerswitchkey import MultiplexerSwitchKey
from .nameditemlist import NamedItemList
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId
from .odxtypes import AtomicOdxType, ParameterValue, odxstr_to_bool
from .snrefcontext import SnRefContext
Expand All @@ -30,7 +31,7 @@ class Multiplexer(ComplexDop):
byte_position: int
switch_key: MultiplexerSwitchKey
default_case: Optional[MultiplexerDefaultCase]
cases: List[MultiplexerCase]
cases: NamedItemList[MultiplexerCase]
is_visible_raw: Optional[bool]

@staticmethod
Expand All @@ -48,9 +49,8 @@ def from_et(et_element: ElementTree.Element, doc_frags: List[OdxDocFragment]) ->
if (dc_elem := et_element.find("DEFAULT-CASE")) is not None:
default_case = MultiplexerDefaultCase.from_et(dc_elem, doc_frags)

cases = []
if (cases_elem := et_element.find("CASES")) is not None:
cases = [MultiplexerCase.from_et(el, doc_frags) for el in cases_elem.iterfind("CASE")]
cases = NamedItemList(
[MultiplexerCase.from_et(el, doc_frags) for el in et_element.iterfind("CASES/CASE")])

is_visible_raw = odxstr_to_bool(et_element.get("IS-VISIBLE"))

Expand Down
2 changes: 1 addition & 1 deletion odxtools/multiplexercase.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@dataclass
class MultiplexerCase(NamedElement):
"""This class represents a Case which represents multiple options in a Multiplexer."""
"""This class represents a case which represents a range of keys of a multiplexer."""

structure_ref: Optional[OdxLinkRef]
structure_snref: Optional[str]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_diag_data_dictionary_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def test_initialization(self) -> None:
sdgs=[],
param_snref="flip_speed",
param_snpathref=None,
env_datas=[],
env_datas=NamedItemList(),
env_data_refs=[OdxLinkRef.from_id(env_data.odx_id)],
)

Expand Down Expand Up @@ -317,7 +317,7 @@ def test_initialization(self) -> None:
structure_ref=None,
structure_snref=None,
),
cases=[
cases=NamedItemList([
MultiplexerCase(
short_name="forward_flip",
long_name="Preconditions for doing a Forward Flip",
Expand All @@ -340,7 +340,7 @@ def test_initialization(self) -> None:
structure_ref=OdxLinkRef.from_id(mux_case2_struct.odx_id),
structure_snref=None,
),
],
]),
is_visible_raw=None,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_encode_env_data_desc(self) -> None:
sdgs=[],
param_snref="DTC",
param_snpathref=None,
env_datas=[
env_datas=NamedItemList([
EnvironmentData(
odx_id=OdxLinkId("DTCs.trouble_explanation.boiler_plate", doc_frags),
oid=None,
Expand Down Expand Up @@ -608,7 +608,7 @@ def test_encode_env_data_desc(self) -> None:
sdgs=[],
),
])),
],
]),
env_data_refs=[],
)

Expand Down

0 comments on commit 92bceae

Please sign in to comment.