Skip to content

Commit

Permalink
Recognize XT file types in parser selection
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelosthege committed Oct 11, 2024
1 parent 72e012c commit abddc4b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bletl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
NoMeasurementData,
)

__version__ = "1.4.3"
__version__ = "1.5.0"
25 changes: 22 additions & 3 deletions bletl/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Specifies the base types for parsing and representing BioLector CSV files."""
import json
import zipfile
from collections.abc import Iterable
from pathlib import Path
from typing import Optional, Sequence, Union
Expand Down Expand Up @@ -38,6 +40,26 @@ def get_parser(filepath: Union[str, Path]) -> BLDParser:
NotImlementedError
When the file contents do not match with a known BioLector refult file format.
"""

model = None
version = None

# Check for XT file types first because they are zipped
if Path(filepath).suffix.lower() == ".zip":
with zipfile.ZipFile(filepath, "r") as zfile:
for fp in zfile.namelist():
if not fp.endswith(".meta"):
continue
with zfile.open(fp) as jfile:
metadict = json.load(jfile)
version = metadict["CsvFileVersion"]
model = BioLectorModel.XT
if not (model, version) in parsers:
raise NotImplementedError(f"Unsupported {model} file version: {version}")
return parsers[(model, version)]()
raise IncompatibleFileError("Unable to detect the BioLector model from the file contents.")

# Now check I/II/Pro file which are plain text
try:
# Note:
# BioLector II files are encoded as UTF8-BOM
Expand All @@ -48,9 +70,6 @@ def get_parser(filepath: Union[str, Path]) -> BLDParser:
with open(filepath, "r", encoding="latin-1") as f:
lines = f.readlines()

model = None
version = None

if "=====" in lines[0]:
if "parameters" in lines[0]:
raise IncompatibleFileError(
Expand Down
Binary file added tests/data/BLXT/4_BG_test.zip
Binary file not shown.
Binary file added tests/data/incompatible_files/other.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def test_selects_parsers_ii(self, fp):
def test_incompatible_file_detecion(self):
with pytest.raises(bletl.IncompatibleFileError):
bletl.get_parser(incompatible_file)
with pytest.raises(bletl.IncompatibleFileError):
bletl.get_parser(dir_testfiles / "incompatible_files" / "other.zip")

def test_xt_file_recognition(self):
fp = dir_testfiles / "BLXT" / "4_BG_test.zip"
with pytest.raises(NotImplementedError, match=r"XT.*?version: 1\.0\.0"):
bletl.get_parser(fp)
pass


class TestUtils:
Expand Down

0 comments on commit abddc4b

Please sign in to comment.