def process_edit_cmd(self, arg=''):
"""{EDIT_CMD} [object|filename]
Open {EDITOR} with session history, provided filename or
object's source file.
- without arguments, a temporary file containing session history is
created and opened in {EDITOR}. On quitting the editor, all
the non commented lines in the file are executed.
- with a filename argument, the file is opened in the editor. On
close, you are returned bay to the interpreter.
- with an object name argument, an attempt is made to lookup the
source file of the object and it is opened if found. Else the
argument is treated as a filename.
"""
line_num_opt = ''
if arg:
obj = self.lookup(arg)
try:
if obj:
filename = inspect.getsourcefile(obj)
_, line_no = inspect.getsourcelines(obj)
line_num_opt = config['LINE_NUM_OPT'].format(line_no=line_no)
else:
filename = arg
except (IOError, TypeError, NameError) as e:
return self.writeline(e)
else:
# - make a list of all lines in session history, commenting
# any non-blank lines.
filename = self._mktemp_buffer("# {}".format(line) if line else ''
for line in (line.strip('\n') for line in self.session_history))
# - shell out to the editor
os.system('{} {} {}'.format(config['EDITOR'], line_num_opt, filename))
# - if arg was not provided (we edited session history), execute
# it in the current namespace
if not arg:
self._exec_from_file(filename)
os.unlink(filename)
评论列表
文章目录