Skip to content

Commit

Permalink
Apply requested proposals from PR review.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hahn <[email protected]>
  • Loading branch information
hahm1c committed Aug 30, 2024
1 parent dffa954 commit ce1fbe4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions odxtools/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down

0 comments on commit ce1fbe4

Please sign in to comment.