##// END OF EJS Templates
Merge pull request #13386 from Carreau/remove-ipython-kernel...
Merge pull request #13386 from Carreau/remove-ipython-kernel remove al IPython/kernel module

File last commit:

r27282:04456d7f
r27292:6de82eda merge
Show More
code.py
755 lines | 27.3 KiB | text/x-python | PythonLexer
Fernando Perez
Create core.magics.config according to new API.
r6961 """Implementation of code management magic functions.
Fernando Perez
Create core.magics.code according to new API.
r6960 """
#-----------------------------------------------------------------------------
# Copyright (c) 2012 The IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Stdlib
import inspect
import io
import os
MinRK
fix %edit foo for interactively defined variables...
r8547 import re
Fernando Perez
Create core.magics.code according to new API.
r6960 import sys
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 import ast
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835 from itertools import chain
Paul Bissex
Corrected old "python3" syntax key to "python"; added UA header per dpaste TOS
r26397 from urllib.request import Request, urlopen
Anatol Ulrich
fix #11082 - use dpaste.com, also remove py2 compatibility imports
r24384 from urllib.parse import urlencode
rchiodo
Use pathlib in edit magic
r26001 from pathlib import Path
Fernando Perez
Create core.magics.code according to new API.
r6960
# Our own packages
Bradley M. Froehle
Better error messages for common magic commands....
r8278 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
Fernando Perez
Create core.magics.code according to new API.
r6960 from IPython.core.macro import Macro
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 from IPython.core.magic import Magics, magics_class, line_magic
Takafumi Arakaki
Use oinspect in CodeMagics._find_edit_target...
r7462 from IPython.core.oinspect import find_file, find_source_lines
Paul Bissex
Corrected old "python3" syntax key to "python"; added UA header per dpaste TOS
r26397 from IPython.core.release import version
Fernando Perez
Create core.magics.code according to new API.
r6960 from IPython.testing.skipdoctest import skip_doctest
Bradley M. Froehle
Define __file__ when we %edit a real file....
r9023 from IPython.utils.contexts import preserve_keys
Antony Lee
On Windows, quote paths instead of escaping them.
r22418 from IPython.utils.path import get_py_filename
Pierre Gerold
Replace all import of IPython.utils.warn module
r22092 from warnings import warn
from logging import error
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 from IPython.utils.text import get_text_list
Fernando Perez
Create core.magics.code according to new API.
r6960
#-----------------------------------------------------------------------------
# Magic implementation classes
#-----------------------------------------------------------------------------
# Used for exception handling in magic_edit
class MacroToEdit(ValueError): pass
MinRK
fix %edit foo for interactively defined variables...
r8547 ipython_input_pat = re.compile(r"<ipython\-input\-(\d+)-[a-z\d]+>$")
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835 # To match, e.g. 8-10 1:5 :10 3-
range_re = re.compile(r"""
(?P<start>\d+)?
((?P<sep>[\-:])
(?P<end>\d+)?)?
$""", re.VERBOSE)
def extract_code_ranges(ranges_str):
"""Turn a string of range for %%load into 2-tuples of (start, stop)
luz.paz
Misc. typos fixes...
r24132 ready to use as a slice of the content split by lines.
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835
Examples
--------
list(extract_input_ranges("5-10 2"))
[(4, 10), (1, 2)]
"""
for range_str in ranges_str.split():
rmatch = range_re.match(range_str)
if not rmatch:
continue
sep = rmatch.group("sep")
start = rmatch.group("start")
end = rmatch.group("end")
if sep == '-':
start = int(start) - 1 if start else None
end = int(end) if end else None
elif sep == ':':
start = int(start) - 1 if start else None
end = int(end) - 1 if end else None
else:
end = int(start)
start = int(start) - 1
yield (start, end)
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 def extract_symbols(code, symbols):
"""
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 Return a tuple (blocks, not_found)
where ``blocks`` is a list of code fragments
for each symbol parsed from code, and ``not_found`` are
symbols not found in the code.
For example::
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841
ryan thielke
Fix non-deterministic tests in IPython.core.magics #8032
r23634 In [1]: code = '''a = 10
...: def b(): return 42
...: class A: pass'''
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841
ryan thielke
Fix non-deterministic tests in IPython.core.magics #8032
r23634 In [2]: extract_symbols(code, 'A,b,z')
Out[2]: (['class A: pass\\n', 'def b(): return 42\\n'], ['z'])
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 """
Martín Gaitán
bugfix. parse of non python code returns a tuple as expected
r12943 symbols = symbols.split(',')
Martín Gaitán
return an specific error for non python
r12945
# this will raise SyntaxError if code isn't valid Python
py_code = ast.parse(code)
Martín Gaitán
skip doctests. comments on the function
r12844
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 marks = [(getattr(s, 'name', None), s.lineno) for s in py_code.body]
Martín Gaitán
better end block detection, ignoring blank lines
r12847 code = code.split('\n')
Martín Gaitán
skip doctests. comments on the function
r12844
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 symbols_lines = {}
Martín Gaitán
better comment for the algorithm
r12927
# we already know the start_lineno of each symbol (marks).
# To find each end_lineno, we traverse in reverse order until each
# non-blank line
end = len(code)
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 for name, start in reversed(marks):
Martín Gaitán
better end block detection, ignoring blank lines
r12847 while not code[end - 1].strip():
end -= 1
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 if name:
symbols_lines[name] = (start - 1, end)
end = start - 1
Martín Gaitán
skip doctests. comments on the function
r12844
Martín Gaitán
better comment for the algorithm
r12927 # Now symbols_lines is a map
# {'symbol_name': (start_lineno, end_lineno), ...}
# fill a list with chunks of codes for each requested symbol
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 blocks = []
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 not_found = []
Martín Gaitán
bugfix. parse of non python code returns a tuple as expected
r12943 for symbol in symbols:
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 if symbol in symbols_lines:
Martín Gaitán
skip doctests. comments on the function
r12844 start, end = symbols_lines[symbol]
Martín Gaitán
better end block detection, ignoring blank lines
r12847 blocks.append('\n'.join(code[start:end]) + '\n')
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 else:
not_found.append(symbol)
Martín Gaitán
skip doctests. comments on the function
r12844
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 return blocks, not_found
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841
Thomas Kluyver
Dedent leading lines on %load -r...
r22702 def strip_initial_indent(lines):
"""For %load, strip indent from lines until finding an unindented line.
https://github.com/ipython/ipython/issues/9775
"""
indent_re = re.compile(r'\s+')
it = iter(lines)
first_line = next(it)
indent_match = indent_re.match(first_line)
if indent_match:
# First line was indented
indent = indent_match.group()
yield first_line[len(indent):]
for line in it:
if line.startswith(indent):
yield line[len(indent):]
else:
# Less indented than the first line - stop dedenting
yield line
break
else:
yield first_line
# Pass the remaining lines through without dedenting
for line in it:
yield line
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835
MinRK
fix %edit foo for interactively defined variables...
r8547 class InteractivelyDefined(Exception):
"""Exception for interactively defined variable in magic_edit"""
def __init__(self, index):
self.index = index
Fernando Perez
Create core.magics.code according to new API.
r6960
Fernando Perez
Renamed @register_magics to @magics_class to avoid confusion....
r6973 @magics_class
Fernando Perez
Create core.magics.code according to new API.
r6960 class CodeMagics(Magics):
"""Magics related to code management (loading, saving, editing, ...)."""
Matthias Bussonnier
Remember list of tempfile for %edit...
r21966 def __init__(self, *args, **kwargs):
self._knowntemps = set()
super(CodeMagics, self).__init__(*args, **kwargs)
Fernando Perez
Create core.magics.code according to new API.
r6960 @line_magic
def save(self, parameter_s=''):
"""Save a set of lines or a macro to a given filename.
Usage:\\
Paulo S. Costa
Adhere to usage style when documenting save args
r26930 %save [options] filename [history]
Fernando Perez
Create core.magics.code according to new API.
r6960
Options:
-r: use 'raw' input. By default, the 'processed' history is used,
so that magics are loaded in their transformed version to valid
Python. If this option is given, the raw input as typed as the
command line is used instead.
MinRK
add %save -f...
r7528
-f: force overwrite. If file exists, %save will prompt for overwrite
unless -f is given.
Fernando Perez
Create core.magics.code according to new API.
r6960
Dominik Dabrowski
improve docstring for %save -a
r7851 -a: append to the file instead of overwriting it.
Dominik Dabrowski
Add option append (-a) to %save...
r7841
Paulo S. Costa
Adhere to usage style when documenting save args
r26930 The history argument uses the same syntax as %history for input ranges,
Fernando Perez
Create core.magics.code according to new API.
r6960 then saves the lines to the filename you specify.
Blazej Michalik
Docs: mention empty histrange behavior of `%save`
r26635 If no ranges are specified, saves history of the current session up to
this point.
Fernando Perez
Create core.magics.code according to new API.
r6960 It adds a '.py' extension to the file if you don't do so yourself, and
Matthias BUSSONNIER
change default extension to .ipy for %save -r
r7255 it asks for confirmation before overwriting existing files.
If `-r` option is used, the default extension is `.ipy`.
"""
Fernando Perez
Create core.magics.code according to new API.
r6960
Dominik Dabrowski
Add option append (-a) to %save...
r7841 opts,args = self.parse_options(parameter_s,'fra',mode='list')
Bradley M. Froehle
Better error messages for common magic commands....
r8278 if not args:
raise UsageError('Missing filename.')
Matthias BUSSONNIER
change default extension to .ipy for %save -r
r7255 raw = 'r' in opts
MinRK
add %save -f...
r7528 force = 'f' in opts
Dominik Dabrowski
Add option append (-a) to %save...
r7841 append = 'a' in opts
mode = 'a' if append else 'w'
Matthias Bussonnier
Remove Py3compat from the save magic code....
r25352 ext = '.ipy' if raw else '.py'
Antony Lee
On Windows, quote paths instead of escaping them.
r22418 fname, codefrom = args[0], " ".join(args[1:])
Matthias Bussonnier
Remove Py3compat from the save magic code....
r25352 if not fname.endswith(('.py','.ipy')):
Matthias BUSSONNIER
change default extension to .ipy for %save -r
r7255 fname += ext
nicolaslazo
Expand ~ in filename arguments for %notebook, %save and %history magics
r27282 fname = os.path.expanduser(fname)
Dominik Dabrowski
Ensure encoding header gets written at start of file...
r7848 file_exists = os.path.isfile(fname)
if file_exists and not force and not append:
MinRK
add %save -f...
r7528 try:
overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
except StdinNotImplementedError:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
MinRK
add %save -f...
r7528 return
Matthias BUSSONNIER
restore loadpy to load...
r7061 if not overwrite :
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('Operation cancelled.')
Fernando Perez
Create core.magics.code according to new API.
r6960 return
try:
Matthias BUSSONNIER
change default extension to .ipy for %save -r
r7255 cmds = self.shell.find_user_code(codefrom,raw)
Fernando Perez
Create core.magics.code according to new API.
r6960 except (TypeError, ValueError) as e:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(e.args[0])
Fernando Perez
Create core.magics.code according to new API.
r6960 return
Dominik Dabrowski
Add option append (-a) to %save...
r7841 with io.open(fname, mode, encoding="utf-8") as f:
Dominik Dabrowski
Ensure encoding header gets written at start of file...
r7848 if not file_exists or not append:
Matthias Bussonnier
Remove Py3compat from the save magic code....
r25352 f.write("# coding: utf-8\n")
f.write(cmds)
Dominik Dabrowski
always add a newline upon %save
r7842 # make sure we end on a newline
Matthias Bussonnier
Remove Py3compat from the save magic code....
r25352 if not cmds.endswith('\n'):
f.write('\n')
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('The following commands were written to file `%s`:' % fname)
print(cmds)
Fernando Perez
Create core.magics.code according to new API.
r6960
@line_magic
def pastebin(self, parameter_s=''):
Paul Bissex
Disambiguated docstring, since there have been other dpastes
r26398 """Upload code to dpaste.com, returning the URL.
Fernando Perez
Create core.magics.code according to new API.
r6960
Usage:\\
Eyenpi
add expiry days feature to pastebin magic
r26653 %pastebin [-d "Custom description"][-e 24] 1-7
Fernando Perez
Create core.magics.code according to new API.
r6960
The argument can be an input history range, a filename, or the name of a
string or macro.
Blazej Michalik
Docs: mention empty histrange behavior of `%pastebin`
r26636 If no arguments are given, uploads the history of this session up to
this point.
Fernando Perez
Create core.magics.code according to new API.
r6960 Options:
Paul Bissex
Corrected old "python3" syntax key to "python"; added UA header per dpaste TOS
r26397 -d: Pass a custom description. The default will say
Fernando Perez
Create core.magics.code according to new API.
r6960 "Pasted from IPython".
Eyenpi
add expiry days feature to pastebin magic
r26653 -e: Pass number of days for the link to be expired.
The default will be 7 days.
Fernando Perez
Create core.magics.code according to new API.
r6960 """
Eyenpi
fix the lint with darker check
r26655 opts, args = self.parse_options(parameter_s, "d:e:")
Fernando Perez
Create core.magics.code according to new API.
r6960
try:
code = self.shell.find_user_code(args)
except (ValueError, TypeError) as e:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print(e.args[0])
Fernando Perez
Create core.magics.code according to new API.
r6960 return
Eyenpi
add expiry days feature to pastebin magic
r26653 expiry_days = 7
Eyenpi
remove the unneeded if statment
r26658 try:
expiry_days = int(opts.get("e", 7))
except ValueError as e:
print(e.args[0].capitalize())
return
if expiry_days < 1 or expiry_days > 365:
print("Expiry days should be in range of 1 to 365")
return
Eyenpi
add expiry days feature to pastebin magic
r26653
Paul Bissex
Fixed lint issue
r26399 post_data = urlencode(
{
"title": opts.get("d", "Pasted from IPython"),
"syntax": "python",
"content": code,
Eyenpi
fix the lint with darker check
r26655 "expiry_days": expiry_days,
Paul Bissex
Fixed lint issue
r26399 }
).encode("utf-8")
request = Request(
Eyenpi
change dpaste api protocol from http to https
r26654 "https://dpaste.com/api/v2/",
Paul Bissex
Fixed lint issue
r26399 headers={"User-Agent": "IPython v{}".format(version)},
)
Paul Bissex
Corrected old "python3" syntax key to "python"; added UA header per dpaste TOS
r26397 response = urlopen(request, post_data)
Anatol Ulrich
fix #11082 - use dpaste.com, also remove py2 compatibility imports
r24384 return response.headers.get('Location')
Fernando Perez
Create core.magics.code according to new API.
r6960
@line_magic
def loadpy(self, arg_s):
Matthias BUSSONNIER
restore loadpy to load...
r7061 """Alias of `%load`
Fernando Perez
Create core.magics.code according to new API.
r6960
MinRK
fix typo in %load docstring...
r11444 `%loadpy` has gained some flexibility and dropped the requirement of a `.py`
Matthias BUSSONNIER
restore loadpy to load...
r7061 extension. So it has been renamed simply into %load. You can look at
`%load`'s docstring for more info.
"""
Matthias BUSSONNIER
fix loadpy calling unexisting function
r7104 self.load(arg_s)
Matthias BUSSONNIER
restore loadpy to load...
r7061
@line_magic
def load(self, arg_s):
"""Load code into the current frontend.
Usage:\\
%load [options] source
Fernando Perez
Create core.magics.code according to new API.
r6960
George Titsworth
Added new capability to the `%load` magic to search the user's namespace for modules, classes, or functions and inspects those to load source....
r16318 where source can be a filename, URL, input history range, macro, or
element in the user namespace
Matthias BUSSONNIER
restore loadpy to load...
r7061
Blazej Michalik
Load whole history on `%load` without parameters
r26637 If no arguments are given, loads the history of this session up to this
point.
Matthias BUSSONNIER
restore loadpy to load...
r7061 Options:
Thomas Kluyver
Clean up numpydoc section headers
r13587
Martín Gaitán
better docstring for the -r option
r12837 -r <lines>: Specify lines or ranges of lines to load from the source.
Ranges could be specified as x-y (x..y) or in python-style x:y
(x..(y-1)). Both limits x and y can be left blank (meaning the
beginning and end of the file, respectively).
Martín Gaitán
fix docstring
r12846 -s <symbols>: Specify function or classes to load from python source.
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841
Matthias BUSSONNIER
restore loadpy to load...
r7061 -y : Don't ask confirmation for loading source above 200 000 characters.
George Titsworth
Added new capability to the `%load` magic to search the user's namespace for modules, classes, or functions and inspects those to load source....
r16318 -n : Include the user's namespace when searching for source code.
Matthias BUSSONNIER
restore loadpy to load...
r7061 This magic command can either take a local filename, a URL, an history
range (see %history) or a macro as argument, it will prompt for
confirmation before loading source with more than 200 000 characters, unless
-y flag is passed or if the frontend does not support raw_input::
Blazej Michalik
Load whole history on `%load` without parameters
r26637 %load
Matthias BUSSONNIER
restore loadpy to load...
r7061 %load myscript.py
%load 7-27
%load myMacro
%load http://www.example.com/myscript.py
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835 %load -r 5-10 myscript.py
Martín Gaitán
better docstring for the -r option
r12837 %load -r 10-20,30,40: foo.py
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 %load -s MyClass,wonder_function myscript.py
George Titsworth
Added new capability to the `%load` magic to search the user's namespace for modules, classes, or functions and inspects those to load source....
r16318 %load -n MyClass
%load -n my_module.wonder_function
Fernando Perez
Create core.magics.code according to new API.
r6960 """
George Titsworth
Added new capability to the `%load` magic to search the user's namespace for modules, classes, or functions and inspects those to load source....
r16318 opts,args = self.parse_options(arg_s,'yns:r:')
search_ns = 'n' in opts
contents = self.shell.find_user_code(args, search_ns=search_ns)
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841 if 's' in opts:
Martín Gaitán
return an specific error for non python
r12945 try:
blocks, not_found = extract_symbols(contents, opts['s'])
except SyntaxError:
# non python code
error("Unable to parse the input as valid Python code")
return
Martín Gaitán
As @minrk recommends, warning if one or more symbol are not found
r12920 if len(not_found) == 1:
warn('The symbol `%s` was not found' % not_found[0])
elif len(not_found) > 1:
warn('The symbols %s were not found' % get_text_list(not_found,
wrap_item_with='`')
)
contents = '\n'.join(blocks)
Martín Gaitán
adds the optional parameter -s to the magic %load that loads specific function or classes from the python source given. forcing the patch ignoring whitespace changes
r12841
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835 if 'r' in opts:
ranges = opts['r'].replace(',', ' ')
lines = contents.split('\n')
slices = extract_code_ranges(ranges)
contents = [lines[slice(*slc)] for slc in slices]
Thomas Kluyver
Dedent leading lines on %load -r...
r22702 contents = '\n'.join(strip_initial_indent(chain.from_iterable(contents)))
Martín Gaitán
Add -r option to %load. Applying a new patch ignoring whitespace changes
r12835
Matthias BUSSONNIER
restore loadpy to load...
r7061 l = len(contents)
luzpaz
Misc. typo fixes...
r24084 # 200 000 is ~ 2500 full 80 character lines
Matthias BUSSONNIER
restore loadpy to load...
r7061 # so in average, more than 5000 lines
if l > 200000 and 'y' not in opts:
try:
ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
" (%d characters). Continue (y/[N]) ?" % l), default='n' )
except StdinNotImplementedError:
luzpaz
Misc. typo fixes...
r24084 #assume yes if raw input not implemented
Matthias BUSSONNIER
restore loadpy to load...
r7061 ans = True
if ans is False :
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('Operation cancelled.')
Matthias BUSSONNIER
restore loadpy to load...
r7061 return
Fernando Perez
Create core.magics.code according to new API.
r6960
Thomas Kluyver
Replace cell with %load magic instead of adding cell
r19251 contents = "# %load {}\n".format(arg_s) + contents
self.shell.set_next_input(contents, replace=True)
Fernando Perez
Create core.magics.code according to new API.
r6960
MinRK
CodeMagics._find_edit_target is a static method...
r7077 @staticmethod
def _find_edit_target(shell, args, opts, last_call):
Fernando Perez
Create core.magics.code according to new API.
r6960 """Utility method used by magic_edit to find what to edit."""
def make_filename(arg):
"Make a filename from the given args"
try:
filename = get_py_filename(arg)
except IOError:
# If it ends with .py but doesn't already exist, assume we want
# a new file.
if arg.endswith('.py'):
filename = arg
else:
filename = None
return filename
# Set a few locals from the options for convenience:
opts_prev = 'p' in opts
opts_raw = 'r' in opts
# custom exceptions
class DataIsObject(Exception): pass
# Default line number value
lineno = opts.get('n',None)
if opts_prev:
args = '_%s' % last_call[0]
Bradley M. Froehle
2to3: Apply has_key fixer.
r7859 if args not in shell.user_ns:
Fernando Perez
Create core.magics.code according to new API.
r6960 args = last_call[1]
# by default this is done with temp files, except when the given
# arg is a filename
use_temp = True
data = ''
# First, see if the arguments should be a filename.
filename = make_filename(args)
if filename:
use_temp = False
elif args:
# Mode where user specifies ranges of lines, like in %macro.
MinRK
CodeMagics._find_edit_target is a static method...
r7077 data = shell.extract_input_lines(args, opts_raw)
Fernando Perez
Create core.magics.code according to new API.
r6960 if not data:
try:
# Load the parameter given as a variable. If not a string,
# process it as an object instead (below)
#print '*** args',args,'type',type(args) # dbg
MinRK
CodeMagics._find_edit_target is a static method...
r7077 data = eval(args, shell.user_ns)
Srinivas Reddy Thatiparthy
convert string_types to str
r23037 if not isinstance(data, str):
Fernando Perez
Create core.magics.code according to new API.
r6960 raise DataIsObject
except (NameError,SyntaxError):
# given argument is not a variable, try as a filename
filename = make_filename(args)
if filename is None:
warn("Argument given (%s) can't be found as a variable "
"or as a filename." % args)
MinRK
cleanup warning/error messages when nothing is found to %edit
r9021 return (None, None, None)
Fernando Perez
Create core.magics.code according to new API.
r6960 use_temp = False
Ram Rachum
Fix exception causes in 7 modules
r25827 except DataIsObject as e:
Fernando Perez
Create core.magics.code according to new API.
r6960 # macros have a special edit function
if isinstance(data, Macro):
Ram Rachum
Fix exception causes in 7 modules
r25827 raise MacroToEdit(data) from e
Fernando Perez
Create core.magics.code according to new API.
r6960
# For objects, try to edit the file where they are defined
Takafumi Arakaki
Use oinspect in CodeMagics._find_edit_target...
r7462 filename = find_file(data)
if filename:
Fernando Perez
Create core.magics.code according to new API.
r6960 if 'fakemodule' in filename.lower() and \
inspect.isclass(data):
# class created by %edit? Try to find source
# by looking for method definitions instead, the
# __module__ in those classes is FakeModule.
attrs = [getattr(data, aname) for aname in dir(data)]
for attr in attrs:
if not inspect.ismethod(attr):
continue
Takafumi Arakaki
Use oinspect in CodeMagics._find_edit_target...
r7462 filename = find_file(attr)
Fernando Perez
Create core.magics.code according to new API.
r6960 if filename and \
'fakemodule' not in filename.lower():
# change the attribute to be the edit
# target instead
data = attr
break
MinRK
fix %edit foo for interactively defined variables...
r8547
m = ipython_input_pat.match(os.path.basename(filename))
if m:
Ram Rachum
Fix exception causes in 7 modules
r25827 raise InteractivelyDefined(int(m.groups()[0])) from e
Fernando Perez
Create core.magics.code according to new API.
r6960 datafile = 1
Takafumi Arakaki
Use oinspect in CodeMagics._find_edit_target...
r7462 if filename is None:
Fernando Perez
Create core.magics.code according to new API.
r6960 filename = make_filename(args)
datafile = 1
MinRK
cleanup warning/error messages when nothing is found to %edit
r9021 if filename is not None:
# only warn about this if we get a real name
warn('Could not find file where `%s` is defined.\n'
Fernando Perez
Small fixes as per @certik's review.
r6986 'Opening a file named `%s`' % (args, filename))
Fernando Perez
Create core.magics.code according to new API.
r6960 # Now, make sure we can actually read the source (if it was
# in a temp file it's gone by now).
if datafile:
Takafumi Arakaki
Use oinspect in CodeMagics._find_edit_target...
r7462 if lineno is None:
lineno = find_source_lines(data)
if lineno is None:
Fernando Perez
Create core.magics.code according to new API.
r6960 filename = make_filename(args)
if filename is None:
MinRK
cleanup warning/error messages when nothing is found to %edit
r9021 warn('The file where `%s` was defined '
'cannot be read or found.' % data)
return (None, None, None)
Fernando Perez
Create core.magics.code according to new API.
r6960 use_temp = False
if use_temp:
MinRK
CodeMagics._find_edit_target is a static method...
r7077 filename = shell.mktempfile(data)
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('IPython will make a temporary file named:',filename)
Fernando Perez
Create core.magics.code according to new API.
r6960
MinRK
in %edit, don't save "last_call" unless last call succeeded...
r9189 # use last_call to remember the state of the previous call, but don't
# let it be clobbered by successive '-p' calls.
try:
last_call[0] = shell.displayhook.prompt_count
if not opts_prev:
last_call[1] = args
except:
pass
Fernando Perez
Create core.magics.code according to new API.
r6960 return filename, lineno, use_temp
def _edit_macro(self,mname,macro):
"""open an editor with the macro data in a file"""
filename = self.shell.mktempfile(macro.value)
self.shell.hooks.editor(filename)
# and make a new macro object, to replace the old one
Matthias Bussonnier
Apply suggestions from code review...
r26010 mvalue = Path(filename).read_text()
Fernando Perez
Create core.magics.code according to new API.
r6960 self.shell.user_ns[mname] = Macro(mvalue)
@skip_doctest
@line_magic
def edit(self, parameter_s='',last_call=['','']):
"""Bring up an editor and execute the resulting code.
Usage:
%edit [options] [args]
%edit runs IPython's editor hook. The default version of this hook is
set to call the editor specified by your $EDITOR environment variable.
If this isn't found, it will default to vi under Linux/Unix and to
notepad under Windows. See the end of this docstring for how to change
the editor hook.
You can also set the value of this editor via the
``TerminalInteractiveShell.editor`` option in your configuration file.
This is useful if you wish to use a different editor from your typical
default with IPython (and for Windows users who typically don't set
environment variables).
This command allows you to conveniently edit multi-line code right in
your IPython session.
If called without arguments, %edit opens up an empty editor with a
temporary file and will execute the contents of this file when you
close it (don't forget to save it!).
Options:
-n <number>: open the editor at a specified line number. By default,
the IPython editor hook uses the unix syntax 'editor +N filename', but
you can configure this by providing your own modified hook if your
favorite editor supports line-number specifications with a different
syntax.
-p: this will call the editor with the same data as the previous time
it was used, regardless of how long ago (in your current session) it
was.
-r: use 'raw' input. This option only applies to input taken from the
user's history. By default, the 'processed' history is used, so that
magics are loaded in their transformed version to valid Python. If
this option is given, the raw input as typed as the command line is
used instead. When you exit the editor, it will be executed by
IPython's own processor.
-x: do not execute the edited code immediately upon exit. This is
mainly useful if you are editing programs which need to be called with
command line arguments, which you can then do using %run.
Arguments:
If arguments are given, the following possibilities exist:
- If the argument is a filename, IPython will load that into the
editor. It will execute its contents with execfile() when you exit,
loading any code in the file into your interactive namespace.
- The arguments are ranges of input history, e.g. "7 ~1/4-6".
The syntax is the same as in the %history magic.
- If the argument is a string variable, its contents are loaded
into the editor. You can thus edit any string which contains
python code (including the result of previous edits).
- If the argument is the name of an object (other than a string),
IPython will try to locate the file where it was defined and open the
editor at the point where it is defined. You can use `%edit function`
to load an editor exactly at the point where 'function' is defined,
edit it and have the file be executed automatically.
- If the object is a macro (see %macro for details), this opens up your
specified editor with a temporary file containing the macro's data.
Upon exit, the macro is reloaded with the contents of the file.
Note: opening at an exact line is only supported under Unix, and some
editors (like kedit and gedit up to Gnome 2.8) do not understand the
'+NUMBER' parameter necessary for this feature. Good editors like
(X)Emacs, vi, jed, pico and joe all do.
After executing your code, %edit will return as output the code you
typed in the editor (except when it was an existing file). This way
you can reload the code in further invocations of %edit as a variable,
via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
the output.
Note that %edit is also available through the alias %ed.
This is an example of creating a simple function inside the editor and
then modifying it. First, start up the editor::
Bradley M. Froehle
Use magic alias api to register magic aliases.
r7933 In [1]: edit
Fernando Perez
Create core.magics.code according to new API.
r6960 Editing... done. Executing edited code...
Out[1]: 'def foo():\\n print "foo() was defined in an editing
session"\\n'
We can then call the function foo()::
In [2]: foo()
foo() was defined in an editing session
Now we edit foo. IPython automatically loads the editor with the
(temporary) file where foo() was previously defined::
Bradley M. Froehle
Use magic alias api to register magic aliases.
r7933 In [3]: edit foo
Fernando Perez
Create core.magics.code according to new API.
r6960 Editing... done. Executing edited code...
And if we call foo() again we get the modified version::
In [4]: foo()
foo() has now been changed!
Here is an example of how to edit a code snippet successive
times. First we call the editor::
Bradley M. Froehle
Use magic alias api to register magic aliases.
r7933 In [5]: edit
Fernando Perez
Create core.magics.code according to new API.
r6960 Editing... done. Executing edited code...
hello
Out[5]: "print 'hello'\\n"
Now we call it again with the previous output (stored in _)::
Bradley M. Froehle
Use magic alias api to register magic aliases.
r7933 In [6]: edit _
Fernando Perez
Create core.magics.code according to new API.
r6960 Editing... done. Executing edited code...
hello world
Out[6]: "print 'hello world'\\n"
Now we call it with the output #8 (stored in _8, also as Out[8])::
Bradley M. Froehle
Use magic alias api to register magic aliases.
r7933 In [7]: edit _8
Fernando Perez
Create core.magics.code according to new API.
r6960 Editing... done. Executing edited code...
hello again
Out[7]: "print 'hello again'\\n"
Changing the default editor hook:
If you wish to write your own editor hook, you can put it in a
configuration file which you load at startup time. The default hook
is defined in the IPython.core.hooks module, and you can use that as a
starting example for further modifications. That file also has
general instructions on how to set a new hook for use once you've
defined it."""
opts,args = self.parse_options(parameter_s,'prxn:')
try:
Fernando Perez
Fix %edit which got broken in magics refactoring....
r7282 filename, lineno, is_temp = self._find_edit_target(self.shell,
args, opts, last_call)
Fernando Perez
Create core.magics.code according to new API.
r6960 except MacroToEdit as e:
self._edit_macro(args, e.args[0])
return
MinRK
fix %edit foo for interactively defined variables...
r8547 except InteractivelyDefined as e:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("Editing In[%i]" % e.index)
MinRK
fix %edit foo for interactively defined variables...
r8547 args = str(e.index)
filename, lineno, is_temp = self._find_edit_target(self.shell,
args, opts, last_call)
MinRK
cleanup warning/error messages when nothing is found to %edit
r9021 if filename is None:
# nothing was found, warnings have already been issued,
# just give up.
return
Fernando Perez
Create core.magics.code according to new API.
r6960
Matthias Bussonnier
Remember list of tempfile for %edit...
r21966 if is_temp:
self._knowntemps.add(filename)
elif (filename in self._knowntemps):
is_temp = True
Fernando Perez
Create core.magics.code according to new API.
r6960 # do actual editing here
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('Editing...', end=' ')
Fernando Perez
Create core.magics.code according to new API.
r6960 sys.stdout.flush()
rchiodo
Use pathlib in edit magic
r26001 filepath = Path(filename)
Fernando Perez
Create core.magics.code according to new API.
r6960 try:
rchiodo
Use pathlib in edit magic
r26001 # Quote filenames that may have spaces in them when opening
# the editor
quoted = filename = str(filepath.absolute())
Matthias Bussonnier
Fix 2 more occurences of Path to use Pathlib,...
r26011 if " " in quoted:
rchiodo
Use pathlib in edit magic
r26001 quoted = "'%s'" % quoted
Matthias Bussonnier
Fix 2 more occurences of Path to use Pathlib,...
r26011 self.shell.hooks.editor(quoted, lineno)
Fernando Perez
Create core.magics.code according to new API.
r6960 except TryNext:
warn('Could not open editor')
return
# XXX TODO: should this be generalized for all string vars?
# For now, this is special-cased to blocks created by cpaste
Matthias Bussonnier
Fix 2 more occurences of Path to use Pathlib,...
r26011 if args.strip() == "pasted_block":
self.shell.user_ns["pasted_block"] = filepath.read_text()
Fernando Perez
Create core.magics.code according to new API.
r6960
if 'x' in opts: # -x prevents actual execution
Thomas Kluyver
Convert print statements to print function calls...
r13348 print()
Fernando Perez
Create core.magics.code according to new API.
r6960 else:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print('done. Executing edited code...')
Bradley M. Froehle
Define __file__ when we %edit a real file....
r9023 with preserve_keys(self.shell.user_ns, '__file__'):
if not is_temp:
self.shell.user_ns['__file__'] = filename
if 'r' in opts: # Untranslated IPython code
Matthias Bussonnier
Fix 2 more occurences of Path to use Pathlib,...
r26011 source = filepath.read_text()
Thomas Kluyver
Inline trivial file_read() function
r9475 self.shell.run_cell(source, store_history=False)
Bradley M. Froehle
Define __file__ when we %edit a real file....
r9023 else:
self.shell.safe_execfile(filename, self.shell.user_ns,
self.shell.user_ns)
Fernando Perez
Create core.magics.code according to new API.
r6960
if is_temp:
try:
Matthias Bussonnier
Fix 2 more occurences of Path to use Pathlib,...
r26011 return filepath.read_text()
Matthias BUSSONNIER
conform to pep 3110...
r7787 except IOError as msg:
rchiodo
Use pathlib in edit magic
r26001 if Path(msg.filename) == filepath:
Fernando Perez
Create core.magics.code according to new API.
r6960 warn('File not found. Did you forget to save?')
return
else:
self.shell.showtraceback()