Skip to content

Commit

Permalink
move resolve_snref() from utils.py to odxlink.py
Browse files Browse the repository at this point in the history
As usual, this was suggested by [at]kayoub5. Thanks!

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Gerrit Ecke <[email protected]>
  • Loading branch information
andlaus committed May 6, 2024
1 parent 7b8e733 commit ac56647
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 59 deletions.
4 changes: 2 additions & 2 deletions odxtools/diagcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from .exceptions import odxraise, odxrequire
from .functionalclass import FunctionalClass
from .nameditemlist import NamedItemList
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from .odxtypes import odxstr_to_bool
from .specialdatagroup import SpecialDataGroup
from .state import State
from .statetransition import StateTransition
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/diaglayerraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
from .exceptions import odxassert, odxraise, odxrequire
from .functionalclass import FunctionalClass
from .nameditemlist import NamedItemList
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from .parentref import ParentRef
from .protstack import ProtStack
from .request import Request
from .response import Response
from .singleecujob import SingleEcuJob
from .specialdatagroup import SpecialDataGroup
from .statechart import StateChart
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from .complexdop import ComplexDop
from .environmentdatadescription import EnvironmentDataDescription
from .exceptions import odxassert, odxrequire
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkRef
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkRef, resolve_snref
from .odxtypes import odxstr_to_bool
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/multiplexercase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from .compumethods.limit import Limit
from .element import NamedElement
from .exceptions import odxrequire
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from .odxtypes import AtomicOdxType, DataType
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/multiplexerdefaultcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from .basicstructure import BasicStructure
from .element import NamedElement
from .exceptions import odxrequire
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .utils import dataclass_fields_asdict, resolve_snref
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
36 changes: 34 additions & 2 deletions odxtools/odxlink.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# SPDX-License-Identifier: MIT
import warnings
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Type, TypeVar, overload
from typing import Any, Dict, Iterable, List, Optional, Type, TypeVar, overload
from xml.etree import ElementTree

from .exceptions import OdxWarning, odxassert
from .exceptions import OdxWarning, odxassert, odxraise
from .nameditemlist import OdxNamed, TNamed


@dataclass(frozen=True)
Expand Down Expand Up @@ -270,3 +271,34 @@ def update(self, new_entries: Dict[OdxLinkId, Any]) -> None:
self._db[doc_frag] = {}

self._db[doc_frag][odx_id] = obj


@overload
def resolve_snref(target_short_name: str,
items: Iterable[OdxNamed],
expected_type: None = None) -> Any:
"""Resolve a short name reference given a sequence of candidate objects"""
...


@overload
def resolve_snref(target_short_name: str, items: Iterable[OdxNamed],
expected_type: Type[TNamed]) -> TNamed:
...


def resolve_snref(target_short_name: str,
items: Iterable[OdxNamed],
expected_type: Any = None) -> Any:
candidates = [x for x in items if x.short_name == target_short_name]

if not candidates:
odxraise(f"Cannot resolve short name reference to '{target_short_name}'")
return None
elif len(candidates) > 1:
odxraise(f"Cannot uniquely resolve short name reference to '{target_short_name}'")
elif expected_type is not None and not isinstance(candidates[0], expected_type):
odxraise(f"Reference '{target_short_name}' points to a {type(candidates[0]).__name__}"
f"object while expecting {expected_type.__name__}")

return candidates[0]
4 changes: 2 additions & 2 deletions odxtools/parameters/parameterwithdop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from ..dtcdop import DtcDop
from ..encodestate import EncodeState
from ..exceptions import odxassert, odxrequire
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from ..odxtypes import AtomicOdxType, ParameterValue
from ..physicaltype import PhysicalType
from ..utils import dataclass_fields_asdict, resolve_snref
from ..utils import dataclass_fields_asdict
from .parameter import Parameter

if TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions odxtools/parameters/tablekeyparameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from ..decodestate import DecodeState
from ..encodestate import EncodeState
from ..exceptions import DecodeError, EncodeError, odxraise, odxrequire
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from ..odxtypes import ParameterValue
from ..utils import dataclass_fields_asdict, resolve_snref
from ..utils import dataclass_fields_asdict
from .parameter import Parameter, ParameterType

if TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions odxtools/parameters/tablestructparameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from ..decodestate import DecodeState
from ..encodestate import EncodeState
from ..exceptions import DecodeError, EncodeError, odxraise, odxrequire
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from ..odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from ..odxtypes import ParameterValue
from ..utils import dataclass_fields_asdict, resolve_snref
from ..utils import dataclass_fields_asdict
from .parameter import Parameter, ParameterType
from .tablekeyparameter import TableKeyParameter

Expand Down
4 changes: 2 additions & 2 deletions odxtools/statechart.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from .element import IdentifiableElement
from .exceptions import odxrequire
from .nameditemlist import NamedItemList
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, resolve_snref
from .state import State
from .statetransition import StateTransition
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/statetransition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from .element import IdentifiableElement
from .exceptions import odxrequire
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, resolve_snref
from .state import State
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
4 changes: 2 additions & 2 deletions odxtools/tablerow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from .dtcdop import DtcDop
from .element import IdentifiableElement
from .exceptions import odxassert, odxraise, odxrequire
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from .odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef, resolve_snref
from .odxtypes import AtomicOdxType
from .specialdatagroup import SpecialDataGroup
from .utils import dataclass_fields_asdict, resolve_snref
from .utils import dataclass_fields_asdict

if TYPE_CHECKING:
from .diaglayer import DiagLayer
Expand Down
36 changes: 1 addition & 35 deletions odxtools/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# SPDX-License-Identifier: MIT
import dataclasses
import re
from typing import Any, Dict, Iterable, Optional, Type, overload
from typing import Any, Dict, Optional
from xml.etree import ElementTree

from .exceptions import odxraise
from .nameditemlist import OdxNamed, TNamed


def create_description_from_et(et_element: Optional[ElementTree.Element],) -> Optional[str]:
"""Read a description tag.
Expand Down Expand Up @@ -63,34 +60,3 @@ def is_short_name_path(test_val: str) -> bool:
See also: ISO 22901 section 7.3.13.3
"""
return _short_name_path_pattern.fullmatch(test_val) is not None


@overload
def resolve_snref(target_short_name: str,
items: Iterable[OdxNamed],
expected_type: None = None) -> Any:
"""Properly resolve a short name reference"""
...


@overload
def resolve_snref(target_short_name: str, items: Iterable[OdxNamed],
expected_type: Type[TNamed]) -> TNamed:
...


def resolve_snref(target_short_name: str,
items: Iterable[OdxNamed],
expected_type: Any = None) -> Any:
candidates = [x for x in items if x.short_name == target_short_name]

if not candidates:
odxraise(f"Cannot resolve short name reference to '{target_short_name}'")
return None
elif len(candidates) > 1:
odxraise(f"Cannot uniquely resolve short name reference to '{target_short_name}'")
elif expected_type is not None and not isinstance(candidates[0], expected_type):
odxraise(f"Reference '{target_short_name}' points to a {type(candidates[0]).__name__}"
f"object while expecting {expected_type.__name__}")

return candidates[0]

0 comments on commit ac56647

Please sign in to comment.