Skip to content

Commit cf27210

Browse files
author
Martin Durant
committed
Merge branch 'master' into after_release
2 parents 50e1ea0 + 0f700d4 commit cf27210

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

fsspec/implementations/local.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import tempfile
88

99
from fsspec import AbstractFileSystem
10+
from fsspec.compression import compr
11+
from fsspec.core import get_compression
1012
from fsspec.utils import stringify_path
1113

1214

@@ -214,19 +216,25 @@ def make_path_posix(path, sep=os.sep):
214216

215217

216218
class LocalFileOpener(io.IOBase):
217-
def __init__(self, path, mode, autocommit=True, fs=None, **kwargs):
219+
def __init__(
220+
self, path, mode, autocommit=True, fs=None, compression=None, **kwargs
221+
):
218222
self.path = path
219223
self.mode = mode
220224
self.fs = fs
221225
self.f = None
222226
self.autocommit = autocommit
227+
self.compression = get_compression(path, compression)
223228
self.blocksize = io.DEFAULT_BUFFER_SIZE
224229
self._open()
225230

226231
def _open(self):
227232
if self.f is None or self.f.closed:
228233
if self.autocommit or "w" not in self.mode:
229234
self.f = open(self.path, mode=self.mode)
235+
if self.compression:
236+
compress = compr[self.compression]
237+
self.f = compress(self.f, mode=self.mode)
230238
else:
231239
# TODO: check if path is writable?
232240
i, name = tempfile.mkstemp()

fsspec/implementations/tests/test_local.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, division, print_function
22

3+
import bz2
34
import gzip
45
import os
56
import os.path
@@ -673,3 +674,19 @@ def test_delete_cwd(tmpdir):
673674
fs.rm(".", recursive=True)
674675
finally:
675676
os.chdir(cwd)
677+
678+
679+
@pytest.mark.parametrize(
680+
"opener, ext", [(bz2.open, ".bz2"), (gzip.open, ".gz"), (open, "")]
681+
)
682+
def test_infer_compression(tmpdir, opener, ext):
683+
filename = str(tmpdir / f"test{ext}")
684+
content = b"hello world"
685+
with opener(filename, "wb") as fp:
686+
fp.write(content)
687+
688+
fs = LocalFileSystem()
689+
with fs.open(f"file://{filename}", "rb", compression="infer") as fp:
690+
read_content = fp.read()
691+
692+
assert content == read_content

0 commit comments

Comments
 (0)