From 3c02b59d875dee29564ca2d93522fc7a358ed29b Mon Sep 17 00:00:00 2001 From: Michael Hahn Date: Fri, 30 Aug 2024 13:09:27 +0200 Subject: [PATCH 1/4] Remove params from constructor and add possibility to load PDX file from IO. Signed-off-by: Michael Hahn Signed-off-by: Andreas Lauser --- odxtools/database.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/odxtools/database.py b/odxtools/database.py index f6ec1e45..563d2326 100644 --- a/odxtools/database.py +++ b/odxtools/database.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: MIT from itertools import chain from pathlib import Path -from typing import IO, Any, Dict, List, Optional, OrderedDict +from typing import IO, Any, Dict, List, Optional, OrderedDict, Union from xml.etree import ElementTree from zipfile import ZipFile @@ -27,10 +27,7 @@ class Database: into a single PDX file. """ - def __init__(self, - *, - pdx_zip: Optional[ZipFile] = None, - odx_d_file_name: Optional[str] = None) -> None: + def __init__(self) -> None: self.model_version: Optional[Version] = None self.auxiliary_files: OrderedDict[str, IO[bytes]] = OrderedDict() @@ -39,9 +36,14 @@ def __init__(self, self._comparam_subsets = NamedItemList[ComparamSubset]() self._comparam_specs = NamedItemList[ComparamSpec]() - def add_pdx_file(self, pdx_file_name: str) -> None: - pdx_zip = ZipFile(pdx_file_name) + def add_pdx_file(self, pdx_file: Union[str, IO[bytes]]) -> None: + """Add PDX file to database. + Either pass the path to the file or an IO with the file content. + """ + pdx_zip = ZipFile(pdx_file) + self.add_pdx_zip(pdx_zip) + def add_pdx_zip(self, pdx_zip: ZipFile) -> None: for zip_member in pdx_zip.namelist(): # The name of ODX files can end with .odx, .odx-d, # .odx-c, .odx-cs, .odx-e, .odx-f, .odx-fd, .odx-m, From dffa9542331ec020eecf4dddc402deff450e3602 Mon Sep 17 00:00:00 2001 From: Michael Hahn Date: Fri, 30 Aug 2024 15:56:27 +0200 Subject: [PATCH 2/4] Add PathLike and ZipFile to type union for pdx file parameter and merge both add_pdx_*() functions again. Signed-off-by: Michael Hahn --- odxtools/database.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/odxtools/database.py b/odxtools/database.py index 563d2326..5f169566 100644 --- a/odxtools/database.py +++ b/odxtools/database.py @@ -4,6 +4,7 @@ from typing import IO, Any, Dict, List, Optional, OrderedDict, Union from xml.etree import ElementTree from zipfile import ZipFile +from os import PathLike from packaging.version import Version @@ -36,25 +37,23 @@ def __init__(self) -> None: self._comparam_subsets = NamedItemList[ComparamSubset]() self._comparam_specs = NamedItemList[ComparamSpec]() - def add_pdx_file(self, pdx_file: Union[str, IO[bytes]]) -> None: + def add_pdx_file(self, pdx_file: Union[str, PathLike[str], IO[bytes], ZipFile]) -> None: """Add PDX file to database. - Either pass the path to the file or an IO with the file content. + Either pass the path to the file, an IO with the file content or a ZipFile object. """ - pdx_zip = ZipFile(pdx_file) - self.add_pdx_zip(pdx_zip) - - def add_pdx_zip(self, pdx_zip: ZipFile) -> None: - for zip_member in pdx_zip.namelist(): + if not isinstance(pdx_file, ZipFile): + pdx_file = ZipFile(pdx_file) + for zip_member in pdx_file.namelist(): # The name of ODX files can end with .odx, .odx-d, # .odx-c, .odx-cs, .odx-e, .odx-f, .odx-fd, .odx-m, # .odx-v . We could test for all that, or just make # sure that the file's suffix starts with .odx p = Path(zip_member) if p.suffix.lower().startswith(".odx"): - root = ElementTree.parse(pdx_zip.open(zip_member)).getroot() + root = ElementTree.parse(pdx_file.open(zip_member)).getroot() self._process_xml_tree(root) elif p.name.lower() != "index.xml": - self.add_auxiliary_file(zip_member, pdx_zip.open(zip_member)) + self.add_auxiliary_file(zip_member, pdx_file.open(zip_member)) def add_odx_file(self, odx_file_name: str) -> None: self._process_xml_tree(ElementTree.parse(odx_file_name).getroot()) From ce1fbe4c307363eb0e17e3365f643ed1c58ac6c5 Mon Sep 17 00:00:00 2001 From: Michael Hahn Date: Fri, 30 Aug 2024 17:10:03 +0200 Subject: [PATCH 3/4] Apply requested proposals from PR review. Signed-off-by: Michael Hahn --- odxtools/database.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/odxtools/database.py b/odxtools/database.py index 5f169566..e1016d18 100644 --- a/odxtools/database.py +++ b/odxtools/database.py @@ -37,34 +37,36 @@ def __init__(self) -> None: self._comparam_subsets = NamedItemList[ComparamSubset]() self._comparam_specs = NamedItemList[ComparamSpec]() - def add_pdx_file(self, pdx_file: Union[str, PathLike[str], IO[bytes], ZipFile]) -> None: + def add_pdx_file(self, pdx_file: Union[str, PathLike, IO[bytes], ZipFile]) -> None: """Add PDX file to database. Either pass the path to the file, an IO with the file content or a ZipFile object. """ - if not isinstance(pdx_file, ZipFile): - pdx_file = ZipFile(pdx_file) - for zip_member in pdx_file.namelist(): + if isinstance(pdx_file, ZipFile): + pdx_zip = pdx_file + else: + pdx_zip = ZipFile(pdx_file) + for zip_member in pdx_zip.namelist(): # The name of ODX files can end with .odx, .odx-d, # .odx-c, .odx-cs, .odx-e, .odx-f, .odx-fd, .odx-m, # .odx-v . We could test for all that, or just make # sure that the file's suffix starts with .odx p = Path(zip_member) if p.suffix.lower().startswith(".odx"): - root = ElementTree.parse(pdx_file.open(zip_member)).getroot() + root = ElementTree.parse(pdx_zip.open(zip_member)).getroot() self._process_xml_tree(root) elif p.name.lower() != "index.xml": - self.add_auxiliary_file(zip_member, pdx_file.open(zip_member)) + self.add_auxiliary_file(zip_member, pdx_zip.open(zip_member)) - def add_odx_file(self, odx_file_name: str) -> None: + def add_odx_file(self, odx_file_name: Union[str, PathLike]) -> None: self._process_xml_tree(ElementTree.parse(odx_file_name).getroot()) def add_auxiliary_file(self, - aux_file_name: str, + aux_file_name: Union[str, PathLike], aux_file_obj: Optional[IO[bytes]] = None) -> None: if aux_file_obj is None: aux_file_obj = open(aux_file_name, "rb") - self.auxiliary_files[aux_file_name] = aux_file_obj + self.auxiliary_files[str(aux_file_name)] = aux_file_obj def _process_xml_tree(self, root: ElementTree.Element) -> None: dlcs: List[DiagLayerContainer] = [] From 3370a83d545fbfe4873e4f143fbb43059d4981e9 Mon Sep 17 00:00:00 2001 From: Michael Hahn Date: Mon, 2 Sep 2024 07:17:09 +0200 Subject: [PATCH 4/4] Fix incompatibility of PathLike type signature between python interpreter and mypy linter type checking. Signed-off-by: Michael Hahn --- odxtools/database.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/odxtools/database.py b/odxtools/database.py index e1016d18..dafc0449 100644 --- a/odxtools/database.py +++ b/odxtools/database.py @@ -37,7 +37,7 @@ def __init__(self) -> None: self._comparam_subsets = NamedItemList[ComparamSubset]() self._comparam_specs = NamedItemList[ComparamSpec]() - def add_pdx_file(self, pdx_file: Union[str, PathLike, IO[bytes], ZipFile]) -> None: + def add_pdx_file(self, pdx_file: Union[str, "PathLike[Any]", IO[bytes], ZipFile]) -> None: """Add PDX file to database. Either pass the path to the file, an IO with the file content or a ZipFile object. """ @@ -57,11 +57,11 @@ def add_pdx_file(self, pdx_file: Union[str, PathLike, IO[bytes], ZipFile]) -> No elif p.name.lower() != "index.xml": self.add_auxiliary_file(zip_member, pdx_zip.open(zip_member)) - def add_odx_file(self, odx_file_name: Union[str, PathLike]) -> None: + def add_odx_file(self, odx_file_name: Union[str, "PathLike[Any]"]) -> None: self._process_xml_tree(ElementTree.parse(odx_file_name).getroot()) def add_auxiliary_file(self, - aux_file_name: Union[str, PathLike], + aux_file_name: Union[str, "PathLike[Any]"], aux_file_obj: Optional[IO[bytes]] = None) -> None: if aux_file_obj is None: aux_file_obj = open(aux_file_name, "rb")