##// END OF EJS Templates
Merge pull request #1784 from Carreau/loadpycat2...
Fernando Perez -
r7062:a0ab74e3 merge
parent child Browse files
Show More
@@ -68,8 +68,8 b' class CodeMagics(Magics):'
68 68 if not fname.endswith('.py'):
69 69 fname += '.py'
70 70 if os.path.isfile(fname):
71 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
72 if ans.lower() not in ['y','yes']:
71 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
72 if not overwrite :
73 73 print 'Operation cancelled.'
74 74 return
75 75 try:
@@ -122,27 +122,55 b' class CodeMagics(Magics):'
122 122
123 123 @line_magic
124 124 def loadpy(self, arg_s):
125 """Load a .py python script into the GUI console.
125 """Alias of `%load`
126 126
127 This magic command can either take a local filename or a url::
127 `%loadpy` has gained some flexibility and droped the requirement of a `.py`
128 extension. So it has been renamed simply into %load. You can look at
129 `%load`'s docstring for more info.
130 """
131 self.magic_load(arg_s)
132
133 @line_magic
134 def load(self, arg_s):
135 """Load code into the current frontend.
128 136
129 %loadpy myscript.py
130 %loadpy http://www.example.com/myscript.py
137 Usage:\\
138 %load [options] source
139
140 where source can be a filename, URL, input history range or macro
141
142 Options:
143 --------
144 -y : Don't ask confirmation for loading source above 200 000 characters.
145
146 This magic command can either take a local filename, a URL, an history
147 range (see %history) or a macro as argument, it will prompt for
148 confirmation before loading source with more than 200 000 characters, unless
149 -y flag is passed or if the frontend does not support raw_input::
150
151 %load myscript.py
152 %load 7-27
153 %load myMacro
154 %load http://www.example.com/myscript.py
131 155 """
132 arg_s = unquote_filename(arg_s)
133 remote_url = arg_s.startswith(('http://', 'https://'))
134 local_url = not remote_url
135 if local_url and not arg_s.endswith('.py'):
136 # Local files must be .py; for remote URLs it's possible that the
137 # fetch URL doesn't have a .py in it (many servers have an opaque
138 # URL, such as scipy-central.org).
139 raise ValueError('%%loadpy only works with .py files: %s' % arg_s)
140
141 # openpy takes care of finding the source encoding (per PEP 263)
142 if remote_url:
143 contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
144 else:
145 contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
156 opts,args = self.parse_options(arg_s,'y')
157
158 contents = self.shell.find_user_code(args)
159 l = len(contents)
160
161 # 200 000 is ~ 2500 full 80 caracter lines
162 # so in average, more than 5000 lines
163 if l > 200000 and 'y' not in opts:
164 try:
165 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
166 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
167 except StdinNotImplementedError:
168 #asume yes if raw input not implemented
169 ans = True
170
171 if ans is False :
172 print 'Operation cancelled.'
173 return
146 174
147 175 self.shell.set_next_input(contents)
148 176
@@ -657,18 +657,21 b' class OSMagics(Magics):'
657 657 """Show a syntax-highlighted file through a pager.
658 658
659 659 This magic is similar to the cat utility, but it will assume the file
660 to be Python source and will show it with syntax highlighting. """
660 to be Python source and will show it with syntax highlighting.
661 661
662 try:
663 filename = get_py_filename(parameter_s)
664 cont = file_read(filename)
665 except IOError:
666 try:
667 cont = eval(parameter_s, self.shell.user_ns)
668 except NameError:
669 cont = None
670 if cont is None:
671 print "Error: no such file or variable"
662 This magic command can either take a local filename, an url,
663 an history range (see %history) or a macro as argument ::
664
665 %pycat myscript.py
666 %pycat 7-27
667 %pycat myMacro
668 %pycat http://www.example.com/myscript.py
669 """
670
671 try :
672 cont = self.shell.find_user_code(parameter_s)
673 except ValueError, IOError:
674 print "Error: no such file, variable, URL, history range or macro"
672 675 return
673 676
674 677 page.page(self.shell.pycolorize(cont))
General Comments 0
You need to be logged in to leave comments. Login now