Skip to content

Commit 803c3ca

Browse files
committed
move the delete-on-success feature from CLI to the library
1 parent 9f3737c commit 803c3ca

File tree

3 files changed

+76
-23
lines changed

3 files changed

+76
-23
lines changed

hatanaka/cli.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import argparse
22
import sys
3-
import warnings
4-
from contextlib import contextmanager
53
from pathlib import Path
64
from typing import List, Optional
75

86
from hatanaka import __version__, compress, compress_on_disk, decompress, decompress_on_disk, \
97
rnxcmp_version
8+
from hatanaka.general_compression import _record_warnings
109

1110
__all__ = ['decompress_cli', 'compress_cli']
1211

@@ -83,16 +82,13 @@ def _run(func, func_on_disk, args, **kwargs):
8382

8483
for in_file in args.files:
8584
with _record_warnings() as warning_list:
86-
out_file = func_on_disk(in_file, **kwargs)
85+
out_file = func_on_disk(in_file, delete=args.delete, **kwargs)
8786
if out_file == in_file:
8887
print(f'{str(in_file)} is already {func.__name__}ed')
8988
else:
9089
print(f'Created {str(out_file)}')
91-
assert out_file.exists()
92-
if args.delete:
93-
if len(warning_list) == 0 and in_file != out_file:
94-
in_file.unlink()
95-
print(f'Deleted {str(in_file)}')
90+
if args.delete and not in_file.exists():
91+
print(f'Deleted {str(in_file)}')
9692

9793
if len(args.files) == 0:
9894
with _record_warnings() as warning_list:
@@ -110,12 +106,3 @@ def _add_common_args(parser):
110106
'finishes without any errors and warnings')
111107
parser.add_argument('--version', action='version', version=__version__)
112108
parser.add_argument('--rnxcmp-version', action='version', version=rnxcmp_version)
113-
114-
115-
@contextmanager
116-
def _record_warnings():
117-
with warnings.catch_warnings(record=True) as warning_list:
118-
yield warning_list
119-
for w in warning_list:
120-
warnings.showwarning(message=w.message, category=w.category, filename=w.filename,
121-
lineno=w.lineno, file=w.file, line=w.line)

hatanaka/general_compression.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import bz2
22
import gzip
33
import re
4+
import warnings
45
import zipfile
6+
from contextlib import contextmanager
57
from io import BytesIO
68
from pathlib import Path
79
from typing import Union
@@ -64,7 +66,8 @@ def decompress(content: Union[Path, str, bytes], *,
6466
return _decompress(content, skip_strange_epochs)[1]
6567

6668

67-
def decompress_on_disk(path: Union[Path, str], *, skip_strange_epochs: bool = False) -> Path:
69+
def decompress_on_disk(path: Union[Path, str], *, delete: bool = False,
70+
skip_strange_epochs: bool = False) -> Path:
6871
"""Decompress compressed RINEX files and write the resulting file to disk.
6972
7073
Any RINEX files compressed with Hatanaka compression (.crx|.##d) and/or with a conventional
@@ -77,6 +80,8 @@ def decompress_on_disk(path: Union[Path, str], *, skip_strange_epochs: bool = Fa
7780
----------
7881
path : Path or str
7982
Path to a compressed RINEX file.
83+
delete : bool, default False
84+
Delete the source file after successful decompression if no errors or warnings were raised.
8085
skip_strange_epochs : bool, default False
8186
For Hatanaka decompression.
8287
Warn and skip strange epochs instead of raising an exception.
@@ -100,13 +105,18 @@ def decompress_on_disk(path: Union[Path, str], *, skip_strange_epochs: bool = Fa
100105
For invalid file contents.
101106
"""
102107
path = Path(path)
103-
is_obs, txt = _decompress(path.read_bytes(), skip_strange_epochs=skip_strange_epochs)
108+
with _record_warnings() as warning_list:
109+
is_obs, txt = _decompress(path.read_bytes(), skip_strange_epochs=skip_strange_epochs)
104110
out_path = get_decompressed_path(path)
105111
if out_path == path:
106112
# file does not need decompressing
107113
return out_path
108114
with out_path.open('wb') as f_out:
109115
f_out.write(txt)
116+
assert out_path.exists()
117+
if delete:
118+
if len(warning_list) == 0 and out_path != path:
119+
path.unlink()
110120
return out_path
111121

112122

@@ -186,7 +196,7 @@ def compress(content: Union[Path, str, bytes], *, compression: str = 'gz',
186196
return _compress(content, compression, skip_strange_epochs, reinit_every_nth)[1]
187197

188198

189-
def compress_on_disk(path: Union[Path, str], *, compression: str = 'gz',
199+
def compress_on_disk(path: Union[Path, str], *, compression: str = 'gz', delete: bool = False,
190200
skip_strange_epochs: bool = False,
191201
reinit_every_nth: int = None) -> Path:
192202
"""Compress RINEX files.
@@ -200,6 +210,8 @@ def compress_on_disk(path: Union[Path, str], *, compression: str = 'gz',
200210
Path to a RINEX file.
201211
compression : 'gz' (default), 'bz2', or 'none'
202212
Which compression (if any) to apply in addition to the Hatanaka compression.
213+
delete : bool, default False
214+
Delete the source file after successful compression if no errors or warnings were raised.
203215
skip_strange_epochs : bool, default False
204216
For Hatanaka compression. Warn and skip strange epochs instead of raising an exception.
205217
reinit_every_nth : int, optional
@@ -225,11 +237,18 @@ def compress_on_disk(path: Union[Path, str], *, compression: str = 'gz',
225237
if path.name.lower().endswith(('.gz', '.bz2', '.z', '.zip')):
226238
# already compressed
227239
return path
228-
is_obs, txt = _compress(path.read_bytes(), compression=compression,
229-
skip_strange_epochs=skip_strange_epochs,
230-
reinit_every_nth=reinit_every_nth)
240+
with _record_warnings() as warning_list:
241+
is_obs, txt = _compress(path.read_bytes(), compression=compression,
242+
skip_strange_epochs=skip_strange_epochs,
243+
reinit_every_nth=reinit_every_nth)
231244
out_path = get_compressed_path(path, is_obs, compression)
245+
if out_path == path:
246+
return out_path
232247
out_path.write_bytes(txt)
248+
assert out_path.exists()
249+
if delete:
250+
if len(warning_list) == 0:
251+
path.unlink()
233252
return out_path
234253

235254

@@ -353,3 +372,12 @@ def _compress_hatanaka(txt: bytes, skip_strange_epochs, reinit_every_nth) -> (bo
353372
else:
354373
is_obs = b'COMPACT RINEX' in txt[:80]
355374
return is_obs, txt
375+
376+
377+
@contextmanager
378+
def _record_warnings():
379+
with warnings.catch_warnings(record=True) as warning_list:
380+
yield warning_list
381+
for w in warning_list:
382+
warnings.showwarning(message=w.message, category=w.category, filename=w.filename,
383+
lineno=w.lineno, file=w.file, line=w.line)

hatanaka/test/test_general_compression.py

+38
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,41 @@ def test_invalid_name(tmp_path, rnx_sample):
173173
compress_on_disk(sample_path)
174174
msg = excinfo.value.args[0]
175175
assert msg.endswith('is not a valid RINEX file name')
176+
177+
178+
def test_decompress_on_disk_delete(tmp_path, rnx_bytes):
179+
# prepare
180+
in_file = 'sample.crx.gz'
181+
sample_path = tmp_path / in_file
182+
shutil.copy(get_data_path(in_file), sample_path)
183+
# decompress and delete
184+
out_path = decompress_on_disk(sample_path, delete=True)
185+
# check
186+
expected_path = tmp_path / 'sample.rnx'
187+
assert not sample_path.exists()
188+
assert out_path == expected_path
189+
assert expected_path.exists()
190+
assert clean(decompress(expected_path)) == clean(rnx_bytes)
191+
# check that already decompressed is not deleted
192+
out_path = decompress_on_disk(expected_path, delete=True)
193+
assert out_path == expected_path
194+
assert out_path.exists()
195+
196+
197+
def test_compress_on_disk_delete(tmp_path, rnx_bytes):
198+
# prepare
199+
in_file = 'sample.rnx'
200+
sample_path = tmp_path / in_file
201+
shutil.copy(get_data_path(in_file), sample_path)
202+
# decompress and delete
203+
out_path = compress_on_disk(sample_path, delete=True)
204+
# check
205+
expected_path = tmp_path / 'sample.crx.gz'
206+
assert not sample_path.exists()
207+
assert out_path == expected_path
208+
assert expected_path.exists()
209+
assert clean(decompress(expected_path)) == clean(rnx_bytes)
210+
# check that already decompressed is not deleted
211+
out_path = compress_on_disk(expected_path, delete=True)
212+
assert out_path == expected_path
213+
assert out_path.exists()

0 commit comments

Comments
 (0)