Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of paths with forward slashes on Windows #1049

Merged
merged 3 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions satpy/readers/yaml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def listify_string(something):

def get_filebase(path, pattern):
"""Get the end of *path* of same length as *pattern*."""
# convert any `/` on Windows to `\\`
path = os.path.normpath(path)
# A pattern can include directories
tail_len = len(pattern.split(os.path.sep))
return os.path.join(*str(path).split(os.path.sep)[-tail_len:])
Expand Down
29 changes: 24 additions & 5 deletions satpy/tests/test_yaml_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@
import unittest
from datetime import datetime
from tempfile import mkdtemp
from unittest.mock import MagicMock, patch

import satpy.readers.yaml_reader as yr
from satpy.readers.file_handlers import BaseFileHandler
from satpy.dataset import DatasetID

try:
from unittest.mock import MagicMock, patch
except ImportError:
from mock import MagicMock, patch


class FakeFH(BaseFileHandler):
"""Fake file handler class."""
Expand Down Expand Up @@ -97,6 +93,29 @@ def test_match_filenames(self):
expected = os.path.join(base_dir, 'geo_coordinates.nc')
self.assertEqual(yr.match_filenames(filenames, pattern), [expected])

def test_match_filenames_windows_forward_slash(self):
"""Check that matching filenames works on Windows with forward slashes.

This is common from Qt5 which internally uses forward slashes everywhere.

"""
# just a fake path for testing that doesn't have to exist
base_dir = os.path.join(os.path.expanduser('~'), 'data',
'satellite', 'Sentinel-3')
base_data = ('S3A_OL_1_EFR____20161020T081224_20161020T081524_'
'20161020T102406_0179_010_078_2340_SVL_O_NR_002.SEN3')
base_dir = os.path.join(base_dir, base_data)
pattern = ('{mission_id:3s}_OL_{processing_level:1s}_{datatype_id:_<6s'
'}_{start_time:%Y%m%dT%H%M%S}_{end_time:%Y%m%dT%H%M%S}_{cre'
'ation_time:%Y%m%dT%H%M%S}_{duration:4d}_{cycle:3d}_{relati'
've_orbit:3d}_{frame:4d}_{centre:3s}_{mode:1s}_{timeliness:'
'2s}_{collection:3s}.SEN3/geo_coordinates.nc')
pattern = os.path.join(*pattern.split('/'))
filenames = [os.path.join(base_dir, 'Oa05_radiance.nc').replace(os.sep, '/'),
os.path.join(base_dir, 'geo_coordinates.nc').replace(os.sep, '/')]
expected = os.path.join(base_dir, 'geo_coordinates.nc').replace(os.sep, '/')
self.assertEqual(yr.match_filenames(filenames, pattern), [expected])

def test_listify_string(self):
"""Check listify_string."""
self.assertEqual(yr.listify_string(None), [])
Expand Down