Repository: obspy/obspy

Issue:
Pop check_compression from argument list for readers?
I wrote a small io plugin for ObsPy events based on zipped files, see https://github.com/trichter/obspycsv. Because ObsPy automatically unpacks zip files, I had some difficulties to get it working.
I found the check_compression argument in the uncompress_file decorator with which its working fine. I think, however, that it should be popped from the argument list [here](https://github.com/obspy/obspy/blob/master/obspy/core/util/decorator.py#L139). Otherwise:

```py
In [1]: ev = read_events()
In [2]: ev.write('test.xml', 'QUAKEML')
In [3]: read_events('test.xml', check_compression=False)
TypeError: _read_quakeml() got an unexpected keyword argument 'check_compression'
```

Ideally, a plugin could define on its own if the compression check should be skipped, e.g. by setting an additional entry point. I see, however, that this feature needs quite some refactoring of the reader code.


Discussion:
> I think, however, that it should be popped from the argument list [here](https://github.com/obspy/obspy/blob/master/obspy/core/util/decorator.py#L139)

This is fixed now.

> Ideally, a plugin could define on its own if the compression check should be skipped, e.g. by setting an additional entry point. I see, however, that this feature needs quite some refactoring of the reader code.

This is not fixed, but I do not want to tackle it on my own. The `check_compression` work-around is good enough for me at the moment.

Therefore, I close the ticket for now.


> > Ideally, a plugin could define on its own if the compression check should be skipped, e.g. by setting an additional entry point. I see, however, that this feature needs quite some refactoring of the reader code.
> 
> This is not fixed, but I do not want to tackle it on my own. The `check_compression` work-around is good enough for me at the moment.

I have a new simple idea how to fix my problem. @megies In [decorator.py](https://github.com/obspy/obspy/blob/master/obspy/core/util/decorator.py#L167-L168) can we check for a comment in the zip file to prevent the unpacking?

```
            with zipfile.ZipFile(filename) as zip:
                if b'obspyarchive' in zip.comment.lower():
                    obj_list = None
                else:
                    obj_list = [zip.read(name) for name in zip.namelist()]
```
I'm not sure I get what the problem or above solution actually are..

It should just be possible to use `check_compression=False`, if that's not working for some reason (like `_read_quakeml()` protesting on unrecognized kwargs?) then we should just fix that I think?
I have this custom plugin to read/write a catalog with picks (see 1st post). The format is basically a bunch of csv files zipped together into a "csz" file. I want this format to be readable by `read_events` out of the box.

When trying to read a csz file, ObsPy recognizes that it is a zip file and it will automatically uncompress it and it is only passing the uncompressed csv files to the readers. But the CSZ reader is only working when it gets the file as whole. The reader has no way to tell ObsPy that its format is a zip file and it should not be uncompressed automatically by ObsPy before the reader is even called.

Passing `check_compression=False` is working, but it is only a work-around and ideally it should not be necessary.

> like _read_quakeml() protesting on unrecognized kwargs?

This is now solved, because `check_compression` is popped from the argument list (other PR).
How about internally setting `check_compression` to `False` if file format is explicitly specified?
> How about internally setting check_compression to False if file format is explicitly specified?

Yes, that would help, but even better would be if the file could be read by a simple `read_events('test.csz')`. You don't like the idea of checking the comment? npz, docx, xlsx -- all formats based on zip files, maybe we see more of these in seismology? ;) The comment could also be more expressive.
> You don't like the idea of checking the comment?

I don't really care, just always a bit wary when it comes to magic thingies. So you mean to put some comment in the zip metadata for that custom file format of yours and then have obspy not unzip when it sees that? Fine with me, doesnt hurt us any, in any case.

`obspyarchive` doesn't seem very self explanatory though, maybe somehting like `obspy_no_uncompress` or something similar?

Resolving patch:
diff --git a/obspy/core/util/decorator.py b/obspy/core/util/decorator.py
--- a/obspy/core/util/decorator.py
+++ b/obspy/core/util/decorator.py
@@ -164,8 +164,14 @@ def uncompress_file(func, filename, *args, **kwargs):
             pass
     elif zipfile.is_zipfile(filename):
         try:
-            zip = zipfile.ZipFile(filename)
-            obj_list = [zip.read(name) for name in zip.namelist()]
+            with zipfile.ZipFile(filename) as zip:
+                if b'obspy_no_uncompress' in zip.comment:
+                    # be nice to plugins based on zip format
+                    # do not uncompress the file if tag is present
+                    # see issue #3192
+                    obj_list = None
+                else:
+                    obj_list = [zip.read(name) for name in zip.namelist()]
         except Exception:
             pass
     elif filename.endswith('.bz2'):


Test patch: