Skip to content

Commit c658887

Browse files
committed
add optional 'strict' parameter to decompression functions
1 parent 2f2f413 commit c658887

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
See also [CHANGES.md](rnxcmp/docs/CHANGES.md) of the original RNXCMP software package.
44

5+
## [2.6.0] - 2022-01-21
6+
7+
- Added `strict` parameter to decompression methods, which defaults to False.
8+
`ValueError` for non-RINEX files is only raised when `strict=True`.
9+
510
## [2.5.0] - 2022-01-10
611

712
- Decompression now raises a `ValueError` if the decompressed file lacks a valid RINEX header record.
@@ -47,8 +52,9 @@ First release.
4752
- Provide Hatanaka decompression / compression support via `crx2rnx` and `rnx2crx` functions.
4853
- Install `crx2rnx` and `rnx2crx` as command line executables.
4954

55+
[2.6.0]: https://github.com/valgur/hatanaka/compare/v2.5.0...v2.6.0
56+
[2.5.0]: https://github.com/valgur/hatanaka/compare/v2.4.0...v2.5.0
5057
[2.4.0]: https://github.com/valgur/hatanaka/compare/v2.3.0...v2.4.0
51-
5258
[2.3.0]: https://github.com/valgur/hatanaka/compare/v2.2.0...v2.3.0
5359
[2.2.0]: https://github.com/valgur/hatanaka/compare/v2.1.0...v2.2.0
5460
[2.1.0]: https://github.com/valgur/hatanaka/compare/v2.0.0...v2.1.0

hatanaka/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .general_compression import *
22
from .hatanaka import *
33

4-
__version__ = '2.5.0'
4+
__version__ = '2.6.0'
55
rnxcmp_version = '4.0.8'

hatanaka/general_compression.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def decompress(content: Union[Path, str, bytes], *,
22-
skip_strange_epochs: bool = False) -> bytes:
22+
skip_strange_epochs: bool = False, strict: bool = False) -> bytes:
2323
"""Decompress compressed RINEX files.
2424
2525
Any RINEX files compressed with Hatanaka compression (.crx|.##d) and/or with a conventional
@@ -41,6 +41,8 @@ def decompress(content: Union[Path, str, bytes], *,
4141
Using this together with of reinit_every_nth option of rnx2crx may be effective.
4242
Caution: It is assumed that no change in the list of data types happens in the
4343
lost part of the data.
44+
strict : bool, default False
45+
If True, a ValueError is raised if the decoded file is not RINEX.
4446
4547
Returns
4648
-------
@@ -55,14 +57,14 @@ def decompress(content: Union[Path, str, bytes], *,
5557
For invalid file contents.
5658
"""
5759
if isinstance(content, (Path, str)):
58-
content = _decompress(Path(content).read_bytes(), skip_strange_epochs)[1]
60+
content = _decompress(Path(content).read_bytes(), skip_strange_epochs, strict)[1]
5961
elif not isinstance(content, bytes):
6062
raise ValueError('input must be either a path or a binary string')
61-
return _decompress(content, skip_strange_epochs)[1]
63+
return _decompress(content, skip_strange_epochs, strict)[1]
6264

6365

6466
def decompress_on_disk(path: Union[Path, str], *, delete: bool = False,
65-
skip_strange_epochs: bool = False) -> Path:
67+
skip_strange_epochs: bool = False, strict: bool = False) -> Path:
6668
"""Decompress compressed RINEX files and write the resulting file to disk.
6769
6870
Any RINEX files compressed with Hatanaka compression (.crx|.##d) and/or with a conventional
@@ -86,6 +88,8 @@ def decompress_on_disk(path: Union[Path, str], *, delete: bool = False,
8688
Using this together with of reinit_every_nth option of rnx2crx may be effective.
8789
Caution: It is assumed that no change in the list of data types happens in the
8890
lost part of the data.
91+
strict : bool, default False
92+
If True, a ValueError is raised if the decoded file is not RINEX.
8993
9094
Returns
9195
-------
@@ -101,7 +105,7 @@ def decompress_on_disk(path: Union[Path, str], *, delete: bool = False,
101105
"""
102106
path = Path(path)
103107
with _record_warnings() as warning_list:
104-
is_obs, txt = _decompress(path.read_bytes(), skip_strange_epochs=skip_strange_epochs)
108+
is_obs, txt = _decompress(path.read_bytes(), skip_strange_epochs, strict)
105109
out_path = get_decompressed_path(path)
106110
if out_path == path:
107111
# file does not need decompressing
@@ -307,15 +311,15 @@ def _is_bz2(magic_bytes: bytes) -> bool:
307311
return magic_bytes == b'\x42\x5A'
308312

309313

310-
def _decompress(txt: bytes, skip_strange_epochs: bool) -> (bool, bytes):
314+
def _decompress(txt: bytes, skip_strange_epochs: bool, strict: bool) -> (bool, bytes):
311315
if len(txt) < 2:
312316
raise ValueError('empty file')
313317
magic_bytes = txt[:2]
314318

315319
if _is_gz(magic_bytes):
316-
return _decompress_hatanaka(gzip.decompress(txt), skip_strange_epochs)
320+
return _decompress_hatanaka(gzip.decompress(txt), skip_strange_epochs, strict)
317321
if _is_bz2(magic_bytes):
318-
return _decompress_hatanaka(bz2.decompress(txt), skip_strange_epochs)
322+
return _decompress_hatanaka(bz2.decompress(txt), skip_strange_epochs, strict)
319323
elif _is_zip(magic_bytes):
320324
with zipfile.ZipFile(BytesIO(txt), 'r') as z:
321325
flist = z.namelist()
@@ -324,21 +328,21 @@ def _decompress(txt: bytes, skip_strange_epochs: bool) -> (bool, bytes):
324328
elif len(flist) > 1:
325329
raise ValueError('more than one file in zip archive')
326330
with z.open(flist[0], 'r') as f:
327-
return _decompress_hatanaka(f.read(), skip_strange_epochs)
331+
return _decompress_hatanaka(f.read(), skip_strange_epochs, strict)
328332
elif _is_lzw(magic_bytes):
329-
return _decompress_hatanaka(lzw.decompress(txt), skip_strange_epochs)
333+
return _decompress_hatanaka(lzw.decompress(txt), skip_strange_epochs, strict)
330334
else:
331-
return _decompress_hatanaka(txt, skip_strange_epochs)
335+
return _decompress_hatanaka(txt, skip_strange_epochs, strict)
332336

333337

334-
def _decompress_hatanaka(txt: bytes, skip_strange_epochs) -> (bool, bytes):
338+
def _decompress_hatanaka(txt: bytes, skip_strange_epochs, strict) -> (bool, bytes):
335339
if len(txt) < 80:
336340
raise ValueError('file is too short to be a valid RINEX file')
337341
header = txt[:80]
338342
is_crinex = b'COMPACT RINEX' in header
339343
if is_crinex:
340344
txt = crx2rnx(txt, skip_strange_epochs=skip_strange_epochs)
341-
elif not header.endswith(b'RINEX VERSION / TYPE'):
345+
elif strict and not header.endswith(b'RINEX VERSION / TYPE'):
342346
raise ValueError('not a valid RINEX file')
343347
is_obs = b'OBSERVATION DATA' in txt[:80]
344348
return is_obs, txt

hatanaka/test/test_on_disk_functions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_on_disk_invalid_input(tmp_path):
112112
path = tmp_path / 'sample.crx'
113113
path.write_bytes(b'blah' * 100)
114114
with pytest.raises(ValueError) as excinfo:
115-
decompress_on_disk(path)
115+
decompress_on_disk(path, strict=True)
116116
msg = excinfo.value.args[0]
117117
assert 'not a valid RINEX file' in msg
118118
assert not get_compressed_path(path).exists()

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = hatanaka
3-
version = 2.5.0
3+
version = 2.6.0
44
author = Martin Valgur
55
author_email = [email protected]
66
url = https://github.com/valgur/hatanaka

0 commit comments

Comments
 (0)