From a0ab74e3de413526b9c9c1b4a51a020867527cbc 2012-05-29 20:31:32 From: Fernando Perez Date: 2012-05-29 20:31:32 Subject: [PATCH] Merge pull request #1784 from Carreau/loadpycat2 Restore loadpy to load. closes #1783, just the part of #1606 eaten by #1732, where some code was accidentally removed. --- diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index faad353..302dc71 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -68,8 +68,8 @@ class CodeMagics(Magics): 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: @@ -122,27 +122,55 @@ class CodeMagics(Magics): @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) diff --git a/IPython/core/magics/osm.py b/IPython/core/magics/osm.py index 723e12b..c2b64bd 100644 --- a/IPython/core/magics/osm.py +++ b/IPython/core/magics/osm.py @@ -657,18 +657,21 @@ class OSMagics(Magics): """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))