Skip to content

Commit

Permalink
restore loadpy to load
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed May 29, 2012
1 parent a136082 commit 87e04ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 31 deletions.
68 changes: 48 additions & 20 deletions IPython/core/magics/code.py
Expand Up @@ -68,8 +68,8 @@ def save(self, parameter_s=''):
if not fname.endswith('.py'):
fname += '.py'
if os.path.isfile(fname):
ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
if ans.lower() not in ['y','yes']:
overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
if not overwrite :
print 'Operation cancelled.'
return
try:
Expand Down Expand Up @@ -122,27 +122,55 @@ def pastebin(self, parameter_s=''):

@line_magic
def loadpy(self, arg_s):
"""Load a .py python script into the GUI console.
"""Alias of `%load`
This magic command can either take a local filename or a url::
`%loadpy` has gained some flexibility and droped the requirement of a `.py`
extension. So it has been renamed simply into %load. You can look at
`%load`'s docstring for more info.
"""
self.magic_load(arg_s)

@line_magic
def load(self, arg_s):
"""Load code into the current frontend.
%loadpy myscript.py
%loadpy http://www.example.com/myscript.py
Usage:\\
%load [options] source
where source can be a filename, URL, input history range or macro
Options:
--------
-y : Don't ask confirmation for loading source above 200 000 characters.
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::
%load myscript.py
%load 7-27
%load myMacro
%load http://www.example.com/myscript.py
"""
arg_s = unquote_filename(arg_s)
remote_url = arg_s.startswith(('http://', 'https://'))
local_url = not remote_url
if local_url and not arg_s.endswith('.py'):
# Local files must be .py; for remote URLs it's possible that the
# fetch URL doesn't have a .py in it (many servers have an opaque
# URL, such as scipy-central.org).
raise ValueError('%%loadpy only works with .py files: %s' % arg_s)

# openpy takes care of finding the source encoding (per PEP 263)
if remote_url:
contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
else:
contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
opts,args = self.parse_options(arg_s,'y')

contents = self.shell.find_user_code(args)
l = len(contents)

# 200 000 is ~ 2500 full 80 caracter lines
# 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:
#asume yes if raw input not implemented
ans = True

if ans is False :
print 'Operation cancelled.'
return

self.shell.set_next_input(contents)

Expand Down
25 changes: 14 additions & 11 deletions IPython/core/magics/osm.py
Expand Up @@ -657,18 +657,21 @@ def pycat(self, parameter_s=''):
"""Show a syntax-highlighted file through a pager.
This magic is similar to the cat utility, but it will assume the file
to be Python source and will show it with syntax highlighting. """
to be Python source and will show it with syntax highlighting.
try:
filename = get_py_filename(parameter_s)
cont = file_read(filename)
except IOError:
try:
cont = eval(parameter_s, self.shell.user_ns)
except NameError:
cont = None
if cont is None:
print "Error: no such file or variable"
This magic command can either take a local filename, an url,
an history range (see %history) or a macro as argument ::
%pycat myscript.py
%pycat 7-27
%pycat myMacro
%pycat http://www.example.com/myscript.py
"""

try :
cont = self.shell.find_user_code(parameter_s)
except ValueError, IOError:
print "Error: no such file, variable, URL, history range or macro"
return

page.page(self.shell.pycolorize(cont))

0 comments on commit 87e04ab

Please sign in to comment.