Skip to content

Commit f2adf0f

Browse files
improve: allow parsing different kw6 version
1 parent 0dc2b7d commit f2adf0f

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

kw6/reader.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ class Config:
4141
arbitrary_types_allowed = True
4242

4343
@staticmethod
44-
def from_file_like(file: Any, header_file: Optional[Any] = None) -> "Reader":
44+
def from_file_like(
45+
file: Any,
46+
header_file: Optional[Any] = None,
47+
raise_on_version_mismatch: bool = True,
48+
) -> "Reader":
4549
"""
4650
Create a Reader instance from a file-like object.
4751
@@ -56,7 +60,7 @@ def from_file_like(file: Any, header_file: Optional[Any] = None) -> "Reader":
5660
ValueError: If the file version is unexpected.
5761
"""
5862
version = file.read(settings.N_BYTES_VERSION).decode().strip()
59-
if version != "KW6FileClassVer1.0":
63+
if version != "KW6FileClassVer1.0" and raise_on_version_mismatch:
6064
raise ValueError(f"Unexpected file version {version}")
6165

6266
initial_position_header = PositionHeader.from_stream_(file)
@@ -81,7 +85,9 @@ def from_file_like(file: Any, header_file: Optional[Any] = None) -> "Reader":
8185

8286
@staticmethod
8387
def from_path(
84-
path: Union[str, Path], header_path: Optional[Union[str, Path]] = None
88+
path: Union[str, Path],
89+
header_path: Optional[Union[str, Path]] = None,
90+
raise_on_version_mismatch: bool = True,
8591
) -> "Reader":
8692
"""
8793
Create a Reader instance from a file path.
@@ -97,8 +103,9 @@ def from_path(
97103
header_path = Path(header_path) if isinstance(header_path, str) else header_path
98104

99105
return Reader.from_file_like(
100-
path.open("rb"),
101-
None if header_path is None else header_path.open("rb"),
106+
file=path.open("rb"),
107+
header_file=None if header_path is None else header_path.open("rb"),
108+
raise_on_version_mismatch=raise_on_version_mismatch,
102109
)
103110

104111
def __iter__(self) -> Iterable[Position]:

0 commit comments

Comments
 (0)