From a7c450c057bcdc25e4e068956aab5b86fda968ae 2012-07-23 15:06:35 From: Takafumi Arakaki Date: 2012-07-23 15:06:35 Subject: [PATCH] Move globlist and its test under utils.path --- diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index ce8e00b..949e5e9 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -19,7 +19,6 @@ import os import sys import time from StringIO import StringIO -from glob import glob # cProfile was added in Python2.5 try: @@ -45,24 +44,11 @@ from IPython.utils import py3compat from IPython.utils.io import capture_output from IPython.utils.ipstruct import Struct from IPython.utils.module_paths import find_mod -from IPython.utils.path import get_py_filename, unquote_filename +from IPython.utils.path import get_py_filename, unquote_filename, globlist from IPython.utils.timing import clock, clock2 from IPython.utils.warn import warn, error -def globlist(args): - """ - Do glob expansion for each element in `args` and return a flattened list. - - Unmatched glob pattern will remain as-is in the returned list. - - """ - expanded = [] - for a in args: - expanded.extend(glob(a) or [a]) - return expanded - - #----------------------------------------------------------------------------- # Magic implementation classes #----------------------------------------------------------------------------- diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index e5332a8..b74236d 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -29,7 +29,6 @@ from IPython.core.magic import (Magics, magics_class, line_magic, register_line_magic, register_cell_magic, register_line_cell_magic) from IPython.core.magics import execution, script -from IPython.core.magics.execution import globlist from IPython.nbformat.v3.tests.nbexamples import nb0 from IPython.nbformat import current from IPython.testing import decorators as dec @@ -87,37 +86,6 @@ def test_magic_parse_long_options(): nt.assert_true(opts['bar'], "bubble") -def test_globlist(): - """Test glob expansion for %run magic.""" - filenames_start_with_a = map('a{0}'.format, range(3)) - filenames_end_with_b = map('{0}b'.format, range(3)) - filenames = filenames_start_with_a + filenames_end_with_b - - with TemporaryDirectory() as td: - save = os.getcwdu() - try: - os.chdir(td) - - # Create empty files - for fname in filenames: - open(os.path.join(td, fname), 'w').close() - - def assert_match(patterns, matches): - # glob returns unordered list. that's why sorted is required. - nt.assert_equals(sorted(globlist(patterns)), sorted(matches)) - - assert_match(['*'], filenames) - assert_match(['a*'], filenames_start_with_a) - assert_match(['*c'], ['*c']) - assert_match(['*', 'a*', '*b', '*c'], - filenames - + filenames_start_with_a - + filenames_end_with_b - + ['*c']) - finally: - os.chdir(save) - - @dec.skip_without('sqlite3') def doctest_hist_f(): """Test %hist -f with temporary filename. diff --git a/IPython/utils/path.py b/IPython/utils/path.py index 79cff4c..54b7480 100644 --- a/IPython/utils/path.py +++ b/IPython/utils/path.py @@ -19,6 +19,7 @@ import sys import tempfile import warnings from hashlib import md5 +from glob import glob import IPython from IPython.testing.skipdoctest import skip_doctest @@ -355,6 +356,19 @@ def expand_path(s): return s +def globlist(args): + """ + Do glob expansion for each element in `args` and return a flattened list. + + Unmatched glob pattern will remain as-is in the returned list. + + """ + expanded = [] + for a in args: + expanded.extend(glob(a) or [a]) + return expanded + + def target_outdated(target,deps): """Determine whether a target is out of date. diff --git a/IPython/utils/tests/test_path.py b/IPython/utils/tests/test_path.py index 94e9755..36a6203 100644 --- a/IPython/utils/tests/test_path.py +++ b/IPython/utils/tests/test_path.py @@ -32,6 +32,7 @@ from IPython.testing.decorators import skip_if_not_win32, skip_win32 from IPython.testing.tools import make_tempfile, AssertPrints from IPython.utils import path, io from IPython.utils import py3compat +from IPython.utils.tempdir import TemporaryDirectory # Platform-dependent imports try: @@ -444,3 +445,35 @@ def test_unicode_in_filename(): path.get_py_filename(u'fooéè.py', force_win32=False) except IOError as ex: str(ex) + + +def test_globlist(): + """Test glob expansion for %run magic.""" + filenames_start_with_a = map('a{0}'.format, range(3)) + filenames_end_with_b = map('{0}b'.format, range(3)) + filenames = filenames_start_with_a + filenames_end_with_b + + with TemporaryDirectory() as td: + save = os.getcwdu() + try: + os.chdir(td) + + # Create empty files + for fname in filenames: + open(os.path.join(td, fname), 'w').close() + + def assert_match(patterns, matches): + # glob returns unordered list. that's why sorted is required. + nt.assert_equals(sorted(path.globlist(patterns)), + sorted(matches)) + + assert_match(['*'], filenames) + assert_match(['a*'], filenames_start_with_a) + assert_match(['*c'], ['*c']) + assert_match(['*', 'a*', '*b', '*c'], + filenames + + filenames_start_with_a + + filenames_end_with_b + + ['*c']) + finally: + os.chdir(save)