Skip to content

Commit

Permalink
Merge pull request #1519 from gerritholl/fsfile-fspath
Browse files Browse the repository at this point in the history
Allow to pass pathlike-objects to FSFile
  • Loading branch information
mraspaud authored Jan 29, 2021
2 parents 653afc0 + 3dcf240 commit e0f1fb8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions satpy/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,14 @@ 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.
*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
Expand All @@ -558,11 +564,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."""
Expand Down
6 changes: 6 additions & 0 deletions satpy/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0f1fb8

Please sign in to comment.