##// 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 if not fname.endswith('.py'):
68 if not fname.endswith('.py'):
69 fname += '.py'
69 fname += '.py'
70 if os.path.isfile(fname):
70 if os.path.isfile(fname):
71 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
71 overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
72 if ans.lower() not in ['y','yes']:
72 if not overwrite :
73 print 'Operation cancelled.'
73 print 'Operation cancelled.'
74 return
74 return
75 try:
75 try:
@@ -122,27 +122,55 b' class CodeMagics(Magics):'
122
122
123 @line_magic
123 @line_magic
124 def loadpy(self, arg_s):
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
137 Usage:\\
130 %loadpy http://www.example.com/myscript.py
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)
156 opts,args = self.parse_options(arg_s,'y')
133 remote_url = arg_s.startswith(('http://', 'https://'))
157
134 local_url = not remote_url
158 contents = self.shell.find_user_code(args)
135 if local_url and not arg_s.endswith('.py'):
159 l = len(contents)
136 # Local files must be .py; for remote URLs it's possible that the
160
137 # fetch URL doesn't have a .py in it (many servers have an opaque
161 # 200 000 is ~ 2500 full 80 caracter lines
138 # URL, such as scipy-central.org).
162 # so in average, more than 5000 lines
139 raise ValueError('%%loadpy only works with .py files: %s' % arg_s)
163 if l > 200000 and 'y' not in opts:
140
164 try:
141 # openpy takes care of finding the source encoding (per PEP 263)
165 ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\
142 if remote_url:
166 " (%d characters). Continue (y/[N]) ?" % l), default='n' )
143 contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
167 except StdinNotImplementedError:
144 else:
168 #asume yes if raw input not implemented
145 contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
169 ans = True
170
171 if ans is False :
172 print 'Operation cancelled.'
173 return
146
174
147 self.shell.set_next_input(contents)
175 self.shell.set_next_input(contents)
148
176
@@ -657,18 +657,21 b' class OSMagics(Magics):'
657 """Show a syntax-highlighted file through a pager.
657 """Show a syntax-highlighted file through a pager.
658
658
659 This magic is similar to the cat utility, but it will assume the file
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
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 """
661
670
662 try:
671 try :
663 filename = get_py_filename(parameter_s)
672 cont = self.shell.find_user_code(parameter_s)
664 cont = file_read(filename)
673 except ValueError, IOError:
665 except IOError:
674 print "Error: no such file, variable, URL, history range or macro"
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"
672 return
675 return
673
676
674 page.page(self.shell.pycolorize(cont))
677 page.page(self.shell.pycolorize(cont))
General Comments 0
You need to be logged in to leave comments. Login now