##// END OF EJS Templates
Move globlist and its test under utils.path
Takafumi Arakaki -
Show More
@@ -19,7 +19,6 b' import os'
19 19 import sys
20 20 import time
21 21 from StringIO import StringIO
22 from glob import glob
23 22
24 23 # cProfile was added in Python2.5
25 24 try:
@@ -45,24 +44,11 b' from IPython.utils import py3compat'
45 44 from IPython.utils.io import capture_output
46 45 from IPython.utils.ipstruct import Struct
47 46 from IPython.utils.module_paths import find_mod
48 from IPython.utils.path import get_py_filename, unquote_filename
47 from IPython.utils.path import get_py_filename, unquote_filename, globlist
49 48 from IPython.utils.timing import clock, clock2
50 49 from IPython.utils.warn import warn, error
51 50
52 51
53 def globlist(args):
54 """
55 Do glob expansion for each element in `args` and return a flattened list.
56
57 Unmatched glob pattern will remain as-is in the returned list.
58
59 """
60 expanded = []
61 for a in args:
62 expanded.extend(glob(a) or [a])
63 return expanded
64
65
66 52 #-----------------------------------------------------------------------------
67 53 # Magic implementation classes
68 54 #-----------------------------------------------------------------------------
@@ -29,7 +29,6 b' from IPython.core.magic import (Magics, magics_class, line_magic,'
29 29 register_line_magic, register_cell_magic,
30 30 register_line_cell_magic)
31 31 from IPython.core.magics import execution, script
32 from IPython.core.magics.execution import globlist
33 32 from IPython.nbformat.v3.tests.nbexamples import nb0
34 33 from IPython.nbformat import current
35 34 from IPython.testing import decorators as dec
@@ -87,37 +86,6 b' def test_magic_parse_long_options():'
87 86 nt.assert_true(opts['bar'], "bubble")
88 87
89 88
90 def test_globlist():
91 """Test glob expansion for %run magic."""
92 filenames_start_with_a = map('a{0}'.format, range(3))
93 filenames_end_with_b = map('{0}b'.format, range(3))
94 filenames = filenames_start_with_a + filenames_end_with_b
95
96 with TemporaryDirectory() as td:
97 save = os.getcwdu()
98 try:
99 os.chdir(td)
100
101 # Create empty files
102 for fname in filenames:
103 open(os.path.join(td, fname), 'w').close()
104
105 def assert_match(patterns, matches):
106 # glob returns unordered list. that's why sorted is required.
107 nt.assert_equals(sorted(globlist(patterns)), sorted(matches))
108
109 assert_match(['*'], filenames)
110 assert_match(['a*'], filenames_start_with_a)
111 assert_match(['*c'], ['*c'])
112 assert_match(['*', 'a*', '*b', '*c'],
113 filenames
114 + filenames_start_with_a
115 + filenames_end_with_b
116 + ['*c'])
117 finally:
118 os.chdir(save)
119
120
121 89 @dec.skip_without('sqlite3')
122 90 def doctest_hist_f():
123 91 """Test %hist -f with temporary filename.
@@ -19,6 +19,7 b' import sys'
19 19 import tempfile
20 20 import warnings
21 21 from hashlib import md5
22 from glob import glob
22 23
23 24 import IPython
24 25 from IPython.testing.skipdoctest import skip_doctest
@@ -355,6 +356,19 b' def expand_path(s):'
355 356 return s
356 357
357 358
359 def globlist(args):
360 """
361 Do glob expansion for each element in `args` and return a flattened list.
362
363 Unmatched glob pattern will remain as-is in the returned list.
364
365 """
366 expanded = []
367 for a in args:
368 expanded.extend(glob(a) or [a])
369 return expanded
370
371
358 372 def target_outdated(target,deps):
359 373 """Determine whether a target is out of date.
360 374
@@ -32,6 +32,7 b' from IPython.testing.decorators import skip_if_not_win32, skip_win32'
32 32 from IPython.testing.tools import make_tempfile, AssertPrints
33 33 from IPython.utils import path, io
34 34 from IPython.utils import py3compat
35 from IPython.utils.tempdir import TemporaryDirectory
35 36
36 37 # Platform-dependent imports
37 38 try:
@@ -444,3 +445,35 b' def test_unicode_in_filename():'
444 445 path.get_py_filename(u'fooéè.py', force_win32=False)
445 446 except IOError as ex:
446 447 str(ex)
448
449
450 def test_globlist():
451 """Test glob expansion for %run magic."""
452 filenames_start_with_a = map('a{0}'.format, range(3))
453 filenames_end_with_b = map('{0}b'.format, range(3))
454 filenames = filenames_start_with_a + filenames_end_with_b
455
456 with TemporaryDirectory() as td:
457 save = os.getcwdu()
458 try:
459 os.chdir(td)
460
461 # Create empty files
462 for fname in filenames:
463 open(os.path.join(td, fname), 'w').close()
464
465 def assert_match(patterns, matches):
466 # glob returns unordered list. that's why sorted is required.
467 nt.assert_equals(sorted(path.globlist(patterns)),
468 sorted(matches))
469
470 assert_match(['*'], filenames)
471 assert_match(['a*'], filenames_start_with_a)
472 assert_match(['*c'], ['*c'])
473 assert_match(['*', 'a*', '*b', '*c'],
474 filenames
475 + filenames_start_with_a
476 + filenames_end_with_b
477 + ['*c'])
478 finally:
479 os.chdir(save)
General Comments 0
You need to be logged in to leave comments. Login now