##// END OF EJS Templates
Use pathlib in edit magic
rchiodo -
Show More
@@ -22,6 +22,7 b' import ast'
22 from itertools import chain
22 from itertools import chain
23 from urllib.request import urlopen
23 from urllib.request import urlopen
24 from urllib.parse import urlencode
24 from urllib.parse import urlencode
25 from pathlib import Path
25
26
26 # Our own packages
27 # Our own packages
27 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
28 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
@@ -688,11 +689,14 b' class CodeMagics(Magics):'
688 # do actual editing here
689 # do actual editing here
689 print('Editing...', end=' ')
690 print('Editing...', end=' ')
690 sys.stdout.flush()
691 sys.stdout.flush()
692 filepath = Path(filename)
691 try:
693 try:
692 # Quote filenames that may have spaces in them
694 # Quote filenames that may have spaces in them when opening
693 if ' ' in filename:
695 # the editor
694 filename = "'%s'" % filename
696 quoted = filename = str(filepath.absolute())
695 self.shell.hooks.editor(filename,lineno)
697 if ' ' in quoted:
698 quoted = "'%s'" % quoted
699 self.shell.hooks.editor(quoted,lineno)
696 except TryNext:
700 except TryNext:
697 warn('Could not open editor')
701 warn('Could not open editor')
698 return
702 return
@@ -700,7 +704,7 b' class CodeMagics(Magics):'
700 # XXX TODO: should this be generalized for all string vars?
704 # XXX TODO: should this be generalized for all string vars?
701 # For now, this is special-cased to blocks created by cpaste
705 # For now, this is special-cased to blocks created by cpaste
702 if args.strip() == 'pasted_block':
706 if args.strip() == 'pasted_block':
703 with open(filename, 'r') as f:
707 with filepath.open('r') as f:
704 self.shell.user_ns['pasted_block'] = f.read()
708 self.shell.user_ns['pasted_block'] = f.read()
705
709
706 if 'x' in opts: # -x prevents actual execution
710 if 'x' in opts: # -x prevents actual execution
@@ -711,7 +715,7 b' class CodeMagics(Magics):'
711 if not is_temp:
715 if not is_temp:
712 self.shell.user_ns['__file__'] = filename
716 self.shell.user_ns['__file__'] = filename
713 if 'r' in opts: # Untranslated IPython code
717 if 'r' in opts: # Untranslated IPython code
714 with open(filename, 'r') as f:
718 with filepath.open('r') as f:
715 source = f.read()
719 source = f.read()
716 self.shell.run_cell(source, store_history=False)
720 self.shell.run_cell(source, store_history=False)
717 else:
721 else:
@@ -720,10 +724,10 b' class CodeMagics(Magics):'
720
724
721 if is_temp:
725 if is_temp:
722 try:
726 try:
723 with open(filename) as f:
727 with filepath.open() as f:
724 return f.read()
728 return f.read()
725 except IOError as msg:
729 except IOError as msg:
726 if msg.filename == filename:
730 if Path(msg.filename) == filepath:
727 warn('File not found. Did you forget to save?')
731 warn('File not found. Did you forget to save?')
728 return
732 return
729 else:
733 else:
@@ -1138,6 +1138,11 b' def test_edit_cell():'
1138 # test
1138 # test
1139 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1139 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1140
1140
1141 def test_edit_fname():
1142 """%edit file"""
1143 # test
1144 _run_edit_test("test file.py", exp_filename="test file.py")
1145
1141 def test_bookmark():
1146 def test_bookmark():
1142 ip = get_ipython()
1147 ip = get_ipython()
1143 ip.run_line_magic('bookmark', 'bmname')
1148 ip.run_line_magic('bookmark', 'bmname')
General Comments 0
You need to be logged in to leave comments. Login now