Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/iris/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ def expand_filespecs(file_specs):
File paths which may contain '~' elements or wildcards.

Returns:
A list of matching file paths. If any of the file-specs matches no
A list of matching absolute file paths. If any of the file-specs matches no
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have completed this PR, I think we should look at making the sort order of the returned filenames consistent and expected. The behaviour I think we need:

def expand_filespecs(file_specs):
    return list_of_expanded_filenames_sorted_by_filespecs_input_order

existing files, an exception is raised.

"""
# Remove any hostname component - currently unused
filenames = [os.path.expanduser(fn[2:] if fn.startswith('//') else fn)
# Remove any hostname component - currently unused - expand paths as absolutes
filenames = [os.path.abspath(os.path.expanduser(fn[2:] if fn.startswith('//') else fn))
for fn in file_specs]

# Try to expand all filenames as globs
Expand Down
76 changes: 76 additions & 0 deletions lib/iris/tests/unit/io/test_expand_filespecs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# (C) British Crown Copyright 2016, Met Office
#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the `iris.io.expand_filespecs` function."""

from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests

import os
import tempfile
import shutil

import iris.io


class TestExpandFilespecs(tests.IrisTest):
def setUp(self):
tests.IrisTest.setUp(self)
self.tmpdir = tempfile.mkdtemp()
self.fnames = ['a.foo', 'b.txt']
for fname in self.fnames:
with open(os.path.join(self.tmpdir, fname), 'w') as fh:
fh.write('anything')

def tearDown(self):
shutil.rmtree(self.tmpdir)

def test_absolute_path(self):
self.assertEqual(iris.io.expand_filespecs(
[os.path.join(self.tmpdir, '*')]),
[os.path.join(self.tmpdir, fname)
for fname in self.fnames])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is too complex to be easily readable. (The same is true for some other lines later in the file as well.) I'd recommend breaking it up into multiple lines and using descriptive names for the temporary variables. Perhaps:

    def test_absolute_path(self):
        result = iris.io.expand_filespecs([os.path.join(self.tmpdir, '*')])
        expected = [os.path.join(self.tmpdir, fname) for fname in self.fnames]
        self.assertEqual(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks, @rhattersley Those are good suggestions; I'll have a look into editing along those lines.


def test_double_slash(self):
self.assertEqual(iris.io.expand_filespecs(
['//' + os.path.join(self.tmpdir, '*')]),
[os.path.join(self.tmpdir, fname)
for fname in self.fnames])

def test_relative_path(self):
os.chdir(self.tmpdir)
self.assertEqual(iris.io.expand_filespecs(['*']),
[os.path.join(self.tmpdir, fname)
for fname in self.fnames])

def test_no_files_found(self):
msg = 'b expanded to empty'
with self.assertRaisesRegexp(IOError, msg):
iris.io.expand_filespecs([self.tmpdir + '_b'])

def test_files_and_none(self):
msg = 'b expanded to empty.*expanded to .*b.txt'
with self.assertRaisesRegexp(IOError, msg):
iris.io.expand_filespecs([self.tmpdir + '_b',
os.path.join(self.tmpdir, '*')])


if __name__ == "__main__":
tests.main()