Skip to content

Commit

Permalink
ProgCodeSyntax: use an enum class instead of a string literal
Browse files Browse the repository at this point in the history
this ensures that only legal values are specified at runtime.

thanks to [at]kayoub5 for naggig about this.

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Gerrit Ecke <[email protected]>
  • Loading branch information
andlaus committed Aug 2, 2023
1 parent d18fd62 commit c645aba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/somersaultecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from odxtools.parentref import ParentRef
from odxtools.physicaltype import PhysicalType
from odxtools.service import DiagService
from odxtools.singleecujob import ProgCode, SingleEcuJob
from odxtools.singleecujob import ProgCode, ProgCodeSyntax, SingleEcuJob
from odxtools.structures import Request, Response
from odxtools.table import Table, TableRow
from odxtools.units import PhysicalDimension, Unit, UnitGroup, UnitSpec
Expand Down Expand Up @@ -1781,7 +1781,7 @@ class SomersaultSID(IntEnum):
ProgCode(
code_file="jobs.jar",
encryption=None,
syntax="JAR",
syntax=ProgCodeSyntax.JAR,
entrypoint="com.supervisor.jobs.CompulsoryProgram",
revision="1.23.4",
library_refs=[],
Expand Down
17 changes: 15 additions & 2 deletions odxtools/singleecujob.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,17 @@ def dop(self) -> Optional[DopBase]:
return self._dop


class ProgCodeSyntax(Enum):
JAVA = "JAVA"
CLASS = "CLASS"
JAR = "JAR"


@dataclass
class ProgCode:
"""A reference to code that is executed by a single ECU job"""
code_file: str
syntax: Literal["JAVA", "CLASS", "JAR"]
syntax: ProgCodeSyntax
revision: str
encryption: Optional[str]
entrypoint: Optional[str]
Expand All @@ -183,7 +189,14 @@ def from_et(et_element: Element, doc_frags: List[OdxDocFragment]) -> "ProgCode":

encryption = et_element.findtext("ENCRYPTION")

syntax = et_element.findtext("SYNTAX")
syntax = ProgCodeSyntax("JAR")
try:
syntax = ProgCodeSyntax(odxrequire(et_element.findtext("SYNTAX")))
except ValueError as e:
try:
odxraise(f"Could not parse program code syntax")
except Exception as ee:
raise ee from e

revision = odxrequire(et_element.findtext("REVISION"))
entrypoint = et_element.findtext("ENTRYPOINT")
Expand Down
2 changes: 1 addition & 1 deletion odxtools/templates/macros/printSingleEcuJob.xml.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
{%- if prog.encryption %}
<ENCRYPTION>{{prog.encryption}}</ENCRYPTION>
{%- endif %}
<SYNTAX>{{prog.syntax}}</SYNTAX>
<SYNTAX>{{prog.syntax.value}}</SYNTAX>
<REVISION>{{prog.revision}}</REVISION>
{%- if prog.entrypoint %}
<ENTRYPOINT>{{prog.entrypoint}}</ENTRYPOINT>
Expand Down
7 changes: 4 additions & 3 deletions tests/test_singleecujob.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from odxtools.odxlink import OdxDocFragment, OdxLinkDatabase, OdxLinkId, OdxLinkRef
from odxtools.odxtypes import DataType
from odxtools.physicaltype import PhysicalType
from odxtools.singleecujob import InputParam, NegOutputParam, OutputParam, ProgCode, SingleEcuJob
from odxtools.singleecujob import (InputParam, NegOutputParam, OutputParam, ProgCode,
ProgCodeSyntax, SingleEcuJob)
from odxtools.utils import short_name_as_id
from odxtools.write_pdx_file import jinja2_odxraise_helper, make_bool_xml_attrib, make_xml_attrib

Expand Down Expand Up @@ -198,7 +199,7 @@ class Context(NamedTuple):
ProgCode(
code_file="abc.jar",
encryption="RSA512",
syntax="JAR",
syntax=ProgCodeSyntax.JAR,
revision="0.12.34",
entrypoint="CalledClass",
library_refs=[OdxLinkRef("my.favourite.lib", doc_frags)],
Expand Down Expand Up @@ -228,7 +229,7 @@ class Context(NamedTuple):
<PROG-CODE>
<CODE-FILE>{self.singleecujob_object.prog_codes[0].code_file}</CODE-FILE>
<ENCRYPTION>{self.singleecujob_object.prog_codes[0].encryption}</ENCRYPTION>
<SYNTAX>{self.singleecujob_object.prog_codes[0].syntax}</SYNTAX>
<SYNTAX>{self.singleecujob_object.prog_codes[0].syntax.value}</SYNTAX>
<REVISION>{self.singleecujob_object.prog_codes[0].revision}</REVISION>
<ENTRYPOINT>{self.singleecujob_object.prog_codes[0].entrypoint}</ENTRYPOINT>
<LIBRARY-REFS>
Expand Down

0 comments on commit c645aba

Please sign in to comment.