Skip to content

Commit

Permalink
Merge pull request #451 from EXWEXs/fix-pathlib
Browse files Browse the repository at this point in the history
Allow readers to accept pathlib.Path instances as filenames.
  • Loading branch information
djhoese authored Oct 9, 2018
2 parents 2f45b54 + 3ad0503 commit 38fb064
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion satpy/readers/yaml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_filebase(path, pattern):
"""Get the end of *path* of same length as *pattern*."""
# A pattern can include directories
tail_len = len(pattern.split(os.path.sep))
return os.path.join(*path.split(os.path.sep)[-tail_len:])
return os.path.join(*str(path).split(os.path.sep)[-tail_len:])


def match_filenames(filenames, pattern):
Expand Down
7 changes: 4 additions & 3 deletions satpy/tests/reader_tests/test_hdf5_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ def __init__(self, filename, filename_info, filetype_info, **kwargs):
if HDF5FileHandler is object:
raise ImportError("Base 'HDF5FileHandler' could not be "
"imported.")
filename = str(filename)
super(HDF5FileHandler, self).__init__(filename, filename_info, filetype_info)
self.file_content = self.get_test_content(filename, filename_info, filetype_info)
self.file_content.update(kwargs)

def get_test_content(self, filename, filename_info, filetype_info):
"""Mimic reader input file content
Args:
filename (str): input filename
filename (str): input filename
filename_info (dict): Dict of metadata pulled from filename
filetype_info (dict): Dict of metadata from the reader's yaml config for this file type
Returns: dict of file content with keys like:
- 'dataset'
- '/attr/global_attr'
- 'dataset/attr/global_attr'
Expand Down
12 changes: 11 additions & 1 deletion satpy/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def tearDown(self):

def test_no_args(self):
"""Test no args provided.
This should check the local directory which should have no files.
"""
from satpy.readers import load_readers
Expand Down Expand Up @@ -243,6 +243,16 @@ def test_bad_reader_name_with_filenames(self):
'SVI01_npp_d20120225_t1801245_e1802487_b01708_c20120226002130255476_noaa_ops.h5',
])

@unittest.skipIf(sys.version_info < (3, 4), "pathlib added in Python 3.4")
def test_filenames_as_path(self):
"""Test with filenames specified as pathlib.Path"""
from pathlib import Path
from satpy.readers import load_readers
ri = load_readers(filenames=[
Path('SVI01_npp_d20120225_t1801245_e1802487_b01708_c20120226002130255476_noaa_ops.h5'),
])
self.assertListEqual(list(ri.keys()), ['viirs_sdr'])

def test_filenames_as_dict(self):
"""Test loading readers where filenames are organized by reader"""
from satpy.readers import load_readers
Expand Down

0 comments on commit 38fb064

Please sign in to comment.