MCPcopy Create free account
hub / github.com/ipython/ipython / _find_edit_target

Method _find_edit_target

IPython/core/magics/code.py:380–507  ·  view source on GitHub ↗

Utility method used by magic_edit to find what to edit.

(shell, args, opts, last_call)

Source from the content-addressed store, hash-verified

378
379 @staticmethod
380 def _find_edit_target(shell, args, opts, last_call):
381 """Utility method used by magic_edit to find what to edit."""
382
383 def make_filename(arg):
384 "Make a filename from the given args"
385 try:
386 filename = get_py_filename(arg)
387 except IOError:
388 # If it ends with .py but doesn't already exist, assume we want
389 # a new file.
390 if arg.endswith('.py'):
391 filename = arg
392 else:
393 filename = None
394 return filename
395
396 # Set a few locals from the options for convenience:
397 opts_prev = 'p' in opts
398 opts_raw = 'r' in opts
399
400 # custom exceptions
401 class DataIsObject(Exception): pass
402
403 # Default line number value
404 lineno = opts.get('n',None)
405
406 if opts_prev:
407 args = '_%s' % last_call[0]
408 if args not in shell.user_ns:
409 args = last_call[1]
410
411 # by default this is done with temp files, except when the given
412 # arg is a filename
413 use_temp = True
414
415 data = ''
416
417 # First, see if the arguments should be a filename.
418 filename = make_filename(args)
419 if filename:
420 use_temp = False
421 elif args:
422 # Mode where user specifies ranges of lines, like in %macro.
423 data = shell.extract_input_lines(args, opts_raw)
424 if not data:
425 try:
426 # Load the parameter given as a variable. If not a string,
427 # process it as an object instead (below)
428
429 #print '*** args',args,'type',type(args) # dbg
430 data = eval(args, shell.user_ns)
431 if not isinstance(data, str):
432 raise DataIsObject
433
434 except (NameError,SyntaxError):
435 # given argument is not a variable, try as a filename
436 filename = make_filename(args)
437 if filename is None:

Callers 2

_run_edit_testFunction · 0.95
editMethod · 0.95

Calls 6

find_fileFunction · 0.90
find_source_linesFunction · 0.90
MacroToEditClass · 0.85
extract_input_linesMethod · 0.80
mktempfileMethod · 0.80

Tested by 1

_run_edit_testFunction · 0.76