From 6c9b8183817b9c8feae00a5e3122a49b605abf6f Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Fri, 29 Jan 2021 14:53:28 +0100 Subject: [PATCH 1/2] Allow to pass pathlike-objects to FSFile When constructing an FSFile object, allow to pass not only strings, but any object meeting the os.PathLike protocol. --- satpy/readers/__init__.py | 8 +++++--- satpy/tests/test_readers.py | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/satpy/readers/__init__.py b/satpy/readers/__init__.py index 3c0f788aab..2b25df8cef 100644 --- a/satpy/readers/__init__.py +++ b/satpy/readers/__init__.py @@ -546,7 +546,9 @@ class FSFile(os.PathLike): def __init__(self, file, fs=None): """Initialise the FSFile instance. - *file* can be string or an fsspec.OpenFile instance. In the latter case, the follow argument *fs* has no effect. + *file* can be string, an object implementing the PathLike protocol, or + an fsspec.OpenFile instance. In the latter case, the follow argument + *fs* has no effect. *fs* can be None or a fsspec filesystem instance. """ try: @@ -558,11 +560,11 @@ def __init__(self, file, fs=None): def __str__(self): """Return the string version of the filename.""" - return self._file + return os.fspath(self._file) def __fspath__(self): """Comply with PathLike.""" - return self._file + return os.fspath(self._file) def __repr__(self): """Representation of the object.""" diff --git a/satpy/tests/test_readers.py b/satpy/tests/test_readers.py index ec28b6aff0..f6b84b3a2e 100644 --- a/satpy/tests/test_readers.py +++ b/satpy/tests/test_readers.py @@ -896,6 +896,12 @@ def test_fsfile_with_regular_filename_and_fs_spec_abides_pathlike(self): from satpy.readers import FSFile assert os.fspath(FSFile(self.random_string, fs=None)) == self.random_string + def test_fsfile_with_pathlike(self): + from satpy.readers import FSFile + from pathlib import Path + f = FSFile(Path(self.local_filename)) + assert str(f) == os.fspath(f) == self.local_filename + def test_fsfile_with_fs_open_file_abides_pathlike(self): """Test that FSFile abides PathLike for fsspec OpenFile instances.""" from satpy.readers import FSFile From 3dcf2408da25fd046c2b152a51015b6300778615 Mon Sep 17 00:00:00 2001 From: Gerrit Holl Date: Fri, 29 Jan 2021 16:33:39 +0100 Subject: [PATCH 2/2] Adapt docstring syntax in FSFile --- satpy/readers/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/satpy/readers/__init__.py b/satpy/readers/__init__.py index 2b25df8cef..6cb6327d6f 100644 --- a/satpy/readers/__init__.py +++ b/satpy/readers/__init__.py @@ -546,10 +546,14 @@ class FSFile(os.PathLike): def __init__(self, file, fs=None): """Initialise the FSFile instance. - *file* can be string, an object implementing the PathLike protocol, or - an fsspec.OpenFile instance. In the latter case, the follow argument - *fs* has no effect. - *fs* can be None or a fsspec filesystem instance. + Args: + file (str, Pathlike, or OpenFile): + String, object implementing the `os.PathLike` protocol, or + an `fsspec.OpenFile` instance. If passed an instance of + `fsspec.OpenFile`, the following argument ``fs`` has no + effect. + fs (fsspec filesystem, optional) + Object implementing the fsspec filesystem protocol. """ try: self._file = file.path