##// END OF EJS Templates
BUG: break out the filename-unquoting from get_py_filename to be used in other contexts. Fix %save, in this respect.
Robert Kern -
Show More
@@ -53,7 +53,7 b' from IPython.core.prefilter import ESC_MAGIC'
53 from IPython.lib.pylabtools import mpl_runner
53 from IPython.lib.pylabtools import mpl_runner
54 from IPython.testing.skipdoctest import skip_doctest
54 from IPython.testing.skipdoctest import skip_doctest
55 from IPython.utils.io import file_read, nlprint
55 from IPython.utils.io import file_read, nlprint
56 from IPython.utils.path import get_py_filename
56 from IPython.utils.path import get_py_filename, unquote_filename
57 from IPython.utils.process import arg_split, abbrev_cwd
57 from IPython.utils.process import arg_split, abbrev_cwd
58 from IPython.utils.terminal import set_term_title
58 from IPython.utils.terminal import set_term_title
59 from IPython.utils.text import LSString, SList, format_screen
59 from IPython.utils.text import LSString, SList, format_screen
@@ -593,7 +593,7 b' Currently the magic system has the following functions:\\n"""'
593 # if not, try the input as a filename
593 # if not, try the input as a filename
594 if out == 'not found':
594 if out == 'not found':
595 try:
595 try:
596 filename = get_py_filename(parameter_s, sys.platform == 'win32')
596 filename = get_py_filename(parameter_s)
597 except IOError,msg:
597 except IOError,msg:
598 print msg
598 print msg
599 return
599 return
@@ -1369,7 +1369,7 b' Currently the magic system has the following functions:\\n"""'
1369 namespace = self.shell.user_ns
1369 namespace = self.shell.user_ns
1370 else: # called to run a program by %run -p
1370 else: # called to run a program by %run -p
1371 try:
1371 try:
1372 filename = get_py_filename(arg_lst[0], sys.platform == 'win32')
1372 filename = get_py_filename(arg_lst[0])
1373 except IOError,msg:
1373 except IOError,msg:
1374 error(msg)
1374 error(msg)
1375 return
1375 return
@@ -1426,10 +1426,12 b' Currently the magic system has the following functions:\\n"""'
1426 dump_file = opts.D[0]
1426 dump_file = opts.D[0]
1427 text_file = opts.T[0]
1427 text_file = opts.T[0]
1428 if dump_file:
1428 if dump_file:
1429 dump_file = unquote_filename(dump_file)
1429 prof.dump_stats(dump_file)
1430 prof.dump_stats(dump_file)
1430 print '\n*** Profile stats marshalled to file',\
1431 print '\n*** Profile stats marshalled to file',\
1431 `dump_file`+'.',sys_exit
1432 `dump_file`+'.',sys_exit
1432 if text_file:
1433 if text_file:
1434 text_file = unquote_filename(text_file)
1433 pfile = file(text_file,'w')
1435 pfile = file(text_file,'w')
1434 pfile.write(output)
1436 pfile.write(output)
1435 pfile.close()
1437 pfile.close()
@@ -1558,7 +1560,7 b' Currently the magic system has the following functions:\\n"""'
1558 mode='list',list_all=1)
1560 mode='list',list_all=1)
1559
1561
1560 try:
1562 try:
1561 filename = file_finder(arg_lst[0], sys.platform == 'win32')
1563 filename = file_finder(arg_lst[0])
1562 except IndexError:
1564 except IndexError:
1563 warn('you must provide at least a filename.')
1565 warn('you must provide at least a filename.')
1564 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1566 print '\n%run:\n',oinspect.getdoc(self.magic_run)
@@ -2071,7 +2073,7 b' Currently the magic system has the following functions:\\n"""'
2071 it asks for confirmation before overwriting existing files."""
2073 it asks for confirmation before overwriting existing files."""
2072
2074
2073 opts,args = self.parse_options(parameter_s,'r',mode='list')
2075 opts,args = self.parse_options(parameter_s,'r',mode='list')
2074 fname, codefrom = args[0], " ".join(args[1:])
2076 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
2075 if not fname.endswith('.py'):
2077 if not fname.endswith('.py'):
2076 fname += '.py'
2078 fname += '.py'
2077 if os.path.isfile(fname):
2079 if os.path.isfile(fname):
@@ -2111,6 +2113,7 b' Currently the magic system has the following functions:\\n"""'
2111 %loadpy myscript.py
2113 %loadpy myscript.py
2112 %loadpy http://www.example.com/myscript.py
2114 %loadpy http://www.example.com/myscript.py
2113 """
2115 """
2116 arg_s = unquote_filename(arg_s)
2114 if not arg_s.endswith('.py'):
2117 if not arg_s.endswith('.py'):
2115 raise ValueError('%%load only works with .py files: %s' % arg_s)
2118 raise ValueError('%%load only works with .py files: %s' % arg_s)
2116 if arg_s.startswith('http'):
2119 if arg_s.startswith('http'):
@@ -2127,12 +2130,13 b' Currently the magic system has the following functions:\\n"""'
2127
2130
2128 def make_filename(arg):
2131 def make_filename(arg):
2129 "Make a filename from the given args"
2132 "Make a filename from the given args"
2133 arg = unquote_filename(arg)
2130 try:
2134 try:
2131 filename = get_py_filename(arg, win32=sys.platform == 'win32')
2135 filename = get_py_filename(arg)
2132 except IOError:
2136 except IOError:
2133 # If it ends with .py but doesn't already exist, assume we want
2137 # If it ends with .py but doesn't already exist, assume we want
2134 # a new file.
2138 # a new file.
2135 if args.endswith('.py'):
2139 if arg.endswith('.py'):
2136 filename = arg
2140 filename = arg
2137 else:
2141 else:
2138 filename = None
2142 filename = None
@@ -2826,8 +2830,7 b' Defaulting color scheme to \'NoColor\'"""'
2826 "Use '%%bookmark -l' to see your bookmarks." % ps)
2830 "Use '%%bookmark -l' to see your bookmarks." % ps)
2827
2831
2828 # strip extra quotes on Windows, because os.chdir doesn't like them
2832 # strip extra quotes on Windows, because os.chdir doesn't like them
2829 if sys.platform == 'win32':
2833 ps = unquote_filename(ps)
2830 ps = ps.strip('\'"')
2831 # at this point ps should point to the target dir
2834 # at this point ps should point to the target dir
2832 if ps:
2835 if ps:
2833 try:
2836 try:
@@ -2870,7 +2873,7 b' Defaulting color scheme to \'NoColor\'"""'
2870 """
2873 """
2871
2874
2872 dir_s = self.shell.dir_stack
2875 dir_s = self.shell.dir_stack
2873 tgt = os.path.expanduser(parameter_s)
2876 tgt = os.path.expanduser(unquote_filename(parameter_s))
2874 cwd = os.getcwdu().replace(self.home_dir,'~')
2877 cwd = os.getcwdu().replace(self.home_dir,'~')
2875 if tgt:
2878 if tgt:
2876 self.magic_cd(parameter_s)
2879 self.magic_cd(parameter_s)
@@ -3148,7 +3151,7 b' Defaulting color scheme to \'NoColor\'"""'
3148 to be Python source and will show it with syntax highlighting. """
3151 to be Python source and will show it with syntax highlighting. """
3149
3152
3150 try:
3153 try:
3151 filename = get_py_filename(parameter_s, sys.platform == 'win32')
3154 filename = get_py_filename(parameter_s)
3152 cont = file_read(filename)
3155 cont = file_read(filename)
3153 except IOError:
3156 except IOError:
3154 try:
3157 try:
@@ -3531,6 +3534,7 b' Defaulting color scheme to \'NoColor\'"""'
3531 args = magic_arguments.parse_argstring(self.magic_notebook, s)
3534 args = magic_arguments.parse_argstring(self.magic_notebook, s)
3532
3535
3533 from IPython.nbformat import current
3536 from IPython.nbformat import current
3537 args.filename = unquote_filename(args.filename)
3534 if args.export:
3538 if args.export:
3535 fname, name, format = current.parse_filename(args.filename)
3539 fname, name, format = current.parse_filename(args.filename)
3536 cells = []
3540 cells = []
@@ -82,20 +82,32 b' def get_long_path_name(path):'
82 return _get_long_path_name(path)
82 return _get_long_path_name(path)
83
83
84
84
85 def get_py_filename(name, win32=False):
85 def unquote_filename(name, win32=(sys.platform=='win32')):
86 """ On Windows, remove leading and trailing quotes from filenames.
87 """
88 if win32:
89 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
90 name = name[1:-1]
91 return name
92
93
94 def get_py_filename(name, force_win32=None):
86 """Return a valid python filename in the current directory.
95 """Return a valid python filename in the current directory.
87
96
88 If the given name is not a file, it adds '.py' and searches again.
97 If the given name is not a file, it adds '.py' and searches again.
89 Raises IOError with an informative message if the file isn't found.
98 Raises IOError with an informative message if the file isn't found.
90
99
91 If the win32 argument is True, then apply Windows semantics to the filename.
100 On Windows, apply Windows semantics to the filename. In particular, remove
92 In particular, remove any quoting that has been applied to it.
101 any quoting that has been applied to it. This option can be forced for
102 testing purposes.
93 """
103 """
94
104
95 name = os.path.expanduser(name)
105 name = os.path.expanduser(name)
96 if win32:
106 if force_win32 is None:
97 if name.startswith(("'", '"')) and name.endswith(("'", '"')):
107 win32 = (sys.platform == 'win32')
98 name = name[1:-1]
108 else:
109 win32 = force_win32
110 name = unquote_filename(name, win32=win32)
99 if not os.path.isfile(name) and not name.endswith('.py'):
111 if not os.path.isfile(name) and not name.endswith('.py'):
100 name += '.py'
112 name += '.py'
101 if os.path.isfile(name):
113 if os.path.isfile(name):
@@ -406,25 +406,38 b' def test_not_writable_ipdir():'
406 nt.assert_true('WARNING' in pipe.getvalue())
406 nt.assert_true('WARNING' in pipe.getvalue())
407 env.pop('IPYTHON_DIR', None)
407 env.pop('IPYTHON_DIR', None)
408
408
409 def test_unquote_filename():
410 for win32 in (True, False):
411 nt.assert_equals(path.unquote_filename('foo.py', win32=win32), 'foo.py')
412 nt.assert_equals(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
413 nt.assert_equals(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
414 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
415 nt.assert_equals(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
416 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
417 nt.assert_equals(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
418 nt.assert_equals(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
419 nt.assert_equals(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
420 nt.assert_equals(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
421
409 @with_environment
422 @with_environment
410 def test_get_py_filename():
423 def test_get_py_filename():
411 os.chdir(TMP_TEST_DIR)
424 os.chdir(TMP_TEST_DIR)
412 for win32 in (True, False):
425 for win32 in (True, False):
413 with make_tempfile('foo.py'):
426 with make_tempfile('foo.py'):
414 nt.assert_equals(path.get_py_filename('foo.py', win32=win32), 'foo.py')
427 nt.assert_equals(path.get_py_filename('foo.py', force_win32=win32), 'foo.py')
415 nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo.py')
428 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo.py')
416 with make_tempfile('foo'):
429 with make_tempfile('foo'):
417 nt.assert_equals(path.get_py_filename('foo', win32=win32), 'foo')
430 nt.assert_equals(path.get_py_filename('foo', force_win32=win32), 'foo')
418 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
431 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
419 nt.assert_raises(IOError, path.get_py_filename, 'foo', win32=win32)
432 nt.assert_raises(IOError, path.get_py_filename, 'foo', force_win32=win32)
420 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', win32=win32)
433 nt.assert_raises(IOError, path.get_py_filename, 'foo.py', force_win32=win32)
421 true_fn = 'foo with spaces.py'
434 true_fn = 'foo with spaces.py'
422 with make_tempfile(true_fn):
435 with make_tempfile(true_fn):
423 nt.assert_equals(path.get_py_filename('foo with spaces', win32=win32), true_fn)
436 nt.assert_equals(path.get_py_filename('foo with spaces', force_win32=win32), true_fn)
424 nt.assert_equals(path.get_py_filename('foo with spaces.py', win32=win32), true_fn)
437 nt.assert_equals(path.get_py_filename('foo with spaces.py', force_win32=win32), true_fn)
425 if win32:
438 if win32:
426 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', win32=True), true_fn)
439 nt.assert_equals(path.get_py_filename('"foo with spaces.py"', force_win32=True), true_fn)
427 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", win32=True), true_fn)
440 nt.assert_equals(path.get_py_filename("'foo with spaces.py'", force_win32=True), true_fn)
428 else:
441 else:
429 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', win32=False)
442 nt.assert_raises(IOError, path.get_py_filename, '"foo with spaces.py"', force_win32=False)
430 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", win32=False)
443 nt.assert_raises(IOError, path.get_py_filename, "'foo with spaces.py'", force_win32=False)
General Comments 0
You need to be logged in to leave comments. Login now