Skip to content

Commit

Permalink
Accept mod in open_file_or_filename
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Mar 7, 2024
1 parent 7c8e575 commit eb6e25f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions satpy/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,10 @@ def _get_compression(file):
return None


def open_file_or_filename(unknown_file_thing):
def open_file_or_filename(unknown_file_thing, mode="r"):
"""Try to open the *unknown_file_thing*, otherwise return the filename."""
try:
f_obj = unknown_file_thing.open()
f_obj = unknown_file_thing.open(mode=mode)
except AttributeError:
f_obj = unknown_file_thing
return f_obj
10 changes: 9 additions & 1 deletion satpy/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

from satpy.dataset.data_dict import get_key
from satpy.dataset.dataid import DataID, ModifierTuple, WavelengthRange
from satpy.readers import find_files_and_readers
from satpy.readers import find_files_and_readers, open_file_or_filename

# NOTE:
# The following fixtures are not defined in this file, but are used and injected by Pytest:
Expand Down Expand Up @@ -1088,3 +1088,11 @@ def test_hash(self):
assert len({hash(FSFile(fn, fs))
for fn in {self.local_filename, self.local_filename2}
for fs in [None, lfs, zfs, cfs]}) == 2*4


def test_open_file_or_filename_uses_mode(tmp_path):
"""Test that open_file_or_filename uses provided mode."""
with open(tmp_path / "hej", mode="wb") as fd:
fd.write(b"hej")
res = open_file_or_filename(tmp_path / "hej", mode="rb").read()
assert isinstance(res, bytes)

0 comments on commit eb6e25f

Please sign in to comment.