Skip to content

Commit b44e638

Browse files
committed
allow get_file_names to glob
1 parent 4bec792 commit b44e638

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

climada/util/files_handler.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import math
1212
import requests
1313
import tqdm
14+
import glob
1415

1516
LOGGER = logging.getLogger(__name__)
1617

@@ -74,11 +75,14 @@ def to_list(num_exp, values, val_name):
7475
return val_list
7576

7677
def get_file_names(file_name):
77-
"""Return list of files contained.
78+
""" Return list of files contained. Supports globbing.
7879
7980
Parameters:
80-
file_name (str or list(str)): file name, or list of file names or name
81-
of the folder containing the files
81+
file_name (str or list(str)): Either a single string or a list of
82+
strings that are either
83+
- a file path
84+
- or the path of the folder containing the files
85+
- or a globbing pattern.
8286
8387
Returns:
8488
list
@@ -92,12 +96,18 @@ def get_file_names(file_name):
9296
return file_list
9397

9498
def _process_one_file_name(name, file_list):
95-
"""Apend to input list the file contained in name"""
96-
if os.path.splitext(name)[1] == '':
97-
tmp_files = os.listdir(name)
98-
# append only files (absolute path), not folders
99+
""" Apend to input list the file contained in name
100+
Tries globbing if name is neither dir nor file.
101+
"""
102+
if os.path.isdir(name):
103+
tmp_files = glob.glob(os.path.join(name, '*'))
99104
for file in tmp_files:
100-
if os.path.splitext(file)[1] != '':
101-
file_list.append(os.path.join(name, file))
102-
else:
105+
if os.path.isfile(file):
106+
file_list.append(file)
107+
if os.path.isfile(name):
103108
file_list.append(name)
109+
else:
110+
tmp_files = sorted(glob.glob(name))
111+
for file in tmp_files:
112+
if os.path.isfile(file):
113+
file_list.append(file)

climada/util/test/test_files.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77

88
from climada.util.files_handler import to_list, get_file_names, download_file
9-
from climada.util.constants import DATA_DIR
9+
from climada.util.constants import DATA_DIR, GLB_CENTROIDS_MAT, ENT_TEMPLATE_XLS
1010

1111
class TestDownloadUrl(unittest.TestCase):
1212
"""Test download_file function """
@@ -53,16 +53,17 @@ def test_list_wrong_length_fail(self):
5353
self.assertIn("Provide one or 3 values.", cm.output[0])
5454

5555
class TestGetFileNames(unittest.TestCase):
56-
"""Test get_file_names function"""
56+
""" Test get_file_names function. Only works with actually existing
57+
files and directories. """
5758
def test_one_file_copy(self):
5859
"""If input is one file name, return a list with this file name"""
59-
file_name = "test.mat"
60+
file_name = GLB_CENTROIDS_MAT
6061
out = get_file_names(file_name)
6162
self.assertEqual([file_name], out)
6263

6364
def test_several_file_copy(self):
6465
"""If input is a list with several file names, return the same list"""
65-
file_name = ["test1.mat", "test2.mat", "test3.mat", "test4.mat"]
66+
file_name = [GLB_CENTROIDS_MAT, ENT_TEMPLATE_XLS]
6667
out = get_file_names(file_name)
6768
self.assertEqual(file_name, out)
6869

@@ -79,6 +80,19 @@ def test_folder_contents(self):
7980
for file in out:
8081
self.assertNotEqual('', os.path.splitext(file)[1])
8182

83+
def test_globbing(self):
84+
""" If input is a glob pattern, return a list of matching visible
85+
files; omit folders.
86+
"""
87+
file_name = os.path.join(DATA_DIR, 'demo/')
88+
out = get_file_names(file_name + '*')
89+
90+
tmp_files = os.listdir(file_name)
91+
tmp_files = [file_name + f for f in tmp_files]
92+
tmp_files = [f for f in tmp_files if not os.path.isdir(f)
93+
and not f.startswith('.')]
94+
self.assertEqual(tmp_files, out)
95+
8296
# Execute Tests
8397
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestToStrList)
8498
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestGetFileNames))

0 commit comments

Comments
 (0)