Show More
@@ -2276,20 +2276,21 b' Currently the magic system has the following functions:\\n"""' | |||||
2276 | %loadpy myscript.py |
|
2276 | %loadpy myscript.py | |
2277 | %loadpy http://www.example.com/myscript.py |
|
2277 | %loadpy http://www.example.com/myscript.py | |
2278 | """ |
|
2278 | """ | |
2279 | arg_s = unquote_filename(arg_s) |
|
2279 | ||
2280 | remote_url = arg_s.startswith(('http://', 'https://')) |
|
2280 | fileorurl = self._get_file_or_url(arg_s) | |
2281 | local_url = not remote_url |
|
2281 | contents = fileorurl['content'] | |
2282 | if local_url and not arg_s.endswith('.py'): |
|
2282 | local_url = fileorurl['local'] | |
|
2283 | filename = fileorurl['filename'] | |||
|
2284 | ||||
|
2285 | if local_url and not filename.endswith('.py'): | |||
2283 | # Local files must be .py; for remote URLs it's possible that the |
|
2286 | # Local files must be .py; for remote URLs it's possible that the | |
2284 | # fetch URL doesn't have a .py in it (many servers have an opaque |
|
2287 | # fetch URL doesn't have a .py in it (many servers have an opaque | |
2285 | # URL, such as scipy-central.org). |
|
2288 | # URL, such as scipy-central.org). | |
2286 | raise ValueError('%%loadpy only works with .py files: %s' % arg_s) |
|
2289 | raise ValueError('%%loadpy only works with .py files: %s' % arg_s) | |
2287 |
|
2290 | |||
2288 | # openpy takes care of finding the source encoding (per PEP 263) |
|
2291 | if not contents : | |
2289 | if remote_url: |
|
2292 | print "Error: no such file, variable or URL" | |
2290 | contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True) |
|
2293 | return | |
2291 | else: |
|
|||
2292 | contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True) |
|
|||
2293 |
|
2294 | |||
2294 | self.set_next_input(contents) |
|
2295 | self.set_next_input(contents) | |
2295 |
|
2296 | |||
@@ -3321,22 +3322,54 b' Defaulting color scheme to \'NoColor\'"""' | |||||
3321 | bkms[args[0]] = args[1] |
|
3322 | bkms[args[0]] = args[1] | |
3322 | self.db['bookmarks'] = bkms |
|
3323 | self.db['bookmarks'] = bkms | |
3323 |
|
3324 | |||
|
3325 | def _get_file_or_url(self, parameter_s=''): | |||
|
3326 | """Try to find the content of a file or URL | |||
|
3327 | ||||
|
3328 | return dict with key: | |||
|
3329 | ==================== | |||
|
3330 | content : file or url content | |||
|
3331 | filename : filename if local | |||
|
3332 | local : (bool) true if local file | |||
|
3333 | ||||
|
3334 | """ | |||
|
3335 | ||||
|
3336 | parameter_s = unquote_filename(parameter_s) | |||
|
3337 | remote_url = parameter_s.startswith(('http://', 'https://')) | |||
|
3338 | ||||
|
3339 | # openpy takes care of finding the source encoding (per PEP 263) | |||
|
3340 | filename = None | |||
|
3341 | if remote_url: | |||
|
3342 | try : | |||
|
3343 | cont = openpy.read_py_url(parameter_s, skip_encoding_cookie=True) | |||
|
3344 | except IOError : | |||
|
3345 | cont = None | |||
|
3346 | else: | |||
|
3347 | try: | |||
|
3348 | filename = get_py_filename(parameter_s) | |||
|
3349 | cont = file_read(filename) | |||
|
3350 | except IOError: | |||
|
3351 | try: | |||
|
3352 | cont = eval(parameter_s,self.user_ns) | |||
|
3353 | except NameError: | |||
|
3354 | cont = None | |||
|
3355 | return {'content': cont, 'filename':filename , 'local': not remote_url} | |||
|
3356 | ||||
|
3357 | ||||
3324 | def magic_pycat(self, parameter_s=''): |
|
3358 | def magic_pycat(self, parameter_s=''): | |
3325 | """Show a syntax-highlighted file through a pager. |
|
3359 | """Show a syntax-highlighted file through a pager. | |
3326 |
|
3360 | |||
3327 | This magic is similar to the cat utility, but it will assume the file |
|
3361 | This magic is similar to the cat utility, but it will assume the file | |
3328 |
to be Python source and will show it with syntax highlighting. |
|
3362 | to be Python source and will show it with syntax highlighting. | |
3329 |
|
|
3363 | ||
3330 | try: |
|
3364 | This magic command can either take a local filename or a url:: | |
3331 | filename = get_py_filename(parameter_s) |
|
3365 | ||
3332 | cont = file_read(filename) |
|
3366 | %pycat myscript.py | |
3333 | except IOError: |
|
3367 | %pycat http://www.example.com/myscript.py | |
3334 | try: |
|
3368 | """ | |
3335 | cont = eval(parameter_s,self.user_ns) |
|
3369 | ||
3336 | except NameError: |
|
3370 | cont = self._get_file_or_url(parameter_s)['content'] | |
3337 | cont = None |
|
|||
3338 | if cont is None: |
|
3371 | if cont is None: | |
3339 |
print "Error: no such file |
|
3372 | print "Error: no such file, variable or URL" | |
3340 | return |
|
3373 | return | |
3341 |
|
3374 | |||
3342 | page.page(self.shell.pycolorize(cont)) |
|
3375 | page.page(self.shell.pycolorize(cont)) |
General Comments 0
You need to be logged in to leave comments.
Login now