Skip to content

Commit abddc4b

Browse files
Recognize XT file types in parser selection
1 parent 72e012c commit abddc4b

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

bletl/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636
NoMeasurementData,
3737
)
3838

39-
__version__ = "1.4.3"
39+
__version__ = "1.5.0"

bletl/core.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Specifies the base types for parsing and representing BioLector CSV files."""
2+
import json
3+
import zipfile
24
from collections.abc import Iterable
35
from pathlib import Path
46
from typing import Optional, Sequence, Union
@@ -38,6 +40,26 @@ def get_parser(filepath: Union[str, Path]) -> BLDParser:
3840
NotImlementedError
3941
When the file contents do not match with a known BioLector refult file format.
4042
"""
43+
44+
model = None
45+
version = None
46+
47+
# Check for XT file types first because they are zipped
48+
if Path(filepath).suffix.lower() == ".zip":
49+
with zipfile.ZipFile(filepath, "r") as zfile:
50+
for fp in zfile.namelist():
51+
if not fp.endswith(".meta"):
52+
continue
53+
with zfile.open(fp) as jfile:
54+
metadict = json.load(jfile)
55+
version = metadict["CsvFileVersion"]
56+
model = BioLectorModel.XT
57+
if not (model, version) in parsers:
58+
raise NotImplementedError(f"Unsupported {model} file version: {version}")
59+
return parsers[(model, version)]()
60+
raise IncompatibleFileError("Unable to detect the BioLector model from the file contents.")
61+
62+
# Now check I/II/Pro file which are plain text
4163
try:
4264
# Note:
4365
# BioLector II files are encoded as UTF8-BOM
@@ -48,9 +70,6 @@ def get_parser(filepath: Union[str, Path]) -> BLDParser:
4870
with open(filepath, "r", encoding="latin-1") as f:
4971
lines = f.readlines()
5072

51-
model = None
52-
version = None
53-
5473
if "=====" in lines[0]:
5574
if "parameters" in lines[0]:
5675
raise IncompatibleFileError(

tests/data/BLXT/4_BG_test.zip

2.02 MB
Binary file not shown.
8.06 KB
Binary file not shown.

tests/test_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ def test_selects_parsers_ii(self, fp):
107107
def test_incompatible_file_detecion(self):
108108
with pytest.raises(bletl.IncompatibleFileError):
109109
bletl.get_parser(incompatible_file)
110+
with pytest.raises(bletl.IncompatibleFileError):
111+
bletl.get_parser(dir_testfiles / "incompatible_files" / "other.zip")
112+
113+
def test_xt_file_recognition(self):
114+
fp = dir_testfiles / "BLXT" / "4_BG_test.zip"
115+
with pytest.raises(NotImplementedError, match=r"XT.*?version: 1\.0\.0"):
116+
bletl.get_parser(fp)
117+
pass
110118

111119

112120
class TestUtils:

0 commit comments

Comments
 (0)