Show More
@@ -50,6 +50,7 b' from IPython.core.formatters import DisplayFormatter' | |||||
50 | from IPython.core.history import HistoryManager |
|
50 | from IPython.core.history import HistoryManager | |
51 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
51 | from IPython.core.inputsplitter import IPythonInputSplitter | |
52 | from IPython.core.logger import Logger |
|
52 | from IPython.core.logger import Logger | |
|
53 | from IPython.core.macro import Macro | |||
53 | from IPython.core.magic import Magic |
|
54 | from IPython.core.magic import Magic | |
54 | from IPython.core.payload import PayloadManager |
|
55 | from IPython.core.payload import PayloadManager | |
55 | from IPython.core.plugin import PluginManager |
|
56 | from IPython.core.plugin import PluginManager | |
@@ -2503,6 +2504,48 b' class InteractiveShell(Configurable, Magic):' | |||||
2503 | def show_usage(self): |
|
2504 | def show_usage(self): | |
2504 | """Show a usage message""" |
|
2505 | """Show a usage message""" | |
2505 | page.page(IPython.core.usage.interactive_usage) |
|
2506 | page.page(IPython.core.usage.interactive_usage) | |
|
2507 | ||||
|
2508 | def _get_some_code(self, target, raw=True): | |||
|
2509 | """Get a code string from history, file, or a string or macro. | |||
|
2510 | ||||
|
2511 | This is mainly used by magic functions. | |||
|
2512 | ||||
|
2513 | Parameters | |||
|
2514 | ---------- | |||
|
2515 | target : str | |||
|
2516 | A string specifying code to retrieve. This will be tried respectively | |||
|
2517 | as: ranges of input history, a filename, and an expression evaluating | |||
|
2518 | to a string or Macro in the user namespace. | |||
|
2519 | raw : bool | |||
|
2520 | If true (default), retrieve raw history. Has no effect on the other | |||
|
2521 | retrieval mechanisms. | |||
|
2522 | ||||
|
2523 | Returns | |||
|
2524 | ------- | |||
|
2525 | A string of code. | |||
|
2526 | ||||
|
2527 | ValueError is raised if nothing is found, and TypeError if it evaluates | |||
|
2528 | to an object of another type. In each case, .args[0] is a printable | |||
|
2529 | message. | |||
|
2530 | """ | |||
|
2531 | code = self.extract_input_lines(target, raw=raw) # Grab history | |||
|
2532 | if code: | |||
|
2533 | return code | |||
|
2534 | if os.path.isfile(target): # Read file | |||
|
2535 | return open(target, "r").read() | |||
|
2536 | ||||
|
2537 | try: # User namespace | |||
|
2538 | codeobj = eval(target, self.user_ns) | |||
|
2539 | except Exception: | |||
|
2540 | raise ValueError(("'%s' was not found in history, as a file, nor in" | |||
|
2541 | " the user namespace.") % target) | |||
|
2542 | if isinstance(codeobj, basestring): | |||
|
2543 | return codeobj | |||
|
2544 | elif isinstance(codeobj, Macro): | |||
|
2545 | return codeobj.value | |||
|
2546 | ||||
|
2547 | raise TypeError("%s is neither a string nor a macro." % target, | |||
|
2548 | codeobj) | |||
2506 |
|
2549 | |||
2507 | #------------------------------------------------------------------------- |
|
2550 | #------------------------------------------------------------------------- | |
2508 | # Things related to IPython exiting |
|
2551 | # Things related to IPython exiting |
@@ -2030,7 +2030,7 b' Currently the magic system has the following functions:\\n"""' | |||||
2030 |
|
2030 | |||
2031 | #print 'rng',ranges # dbg |
|
2031 | #print 'rng',ranges # dbg | |
2032 | try: |
|
2032 | try: | |
2033 | lines = self._get_some_code(codefrom, 'r' in opts) |
|
2033 | lines = self.shell._get_some_code(codefrom, 'r' in opts) | |
2034 | except (ValueError, TypeError) as e: |
|
2034 | except (ValueError, TypeError) as e: | |
2035 | print e.args[0] |
|
2035 | print e.args[0] | |
2036 | return |
|
2036 | return | |
@@ -2069,7 +2069,7 b' Currently the magic system has the following functions:\\n"""' | |||||
2069 | print 'Operation cancelled.' |
|
2069 | print 'Operation cancelled.' | |
2070 | return |
|
2070 | return | |
2071 | try: |
|
2071 | try: | |
2072 | cmds = self._get_some_code(codefrom, 'r' in opts) |
|
2072 | cmds = self.shell._get_some_code(codefrom, 'r' in opts) | |
2073 | except (TypeError, ValueError) as e: |
|
2073 | except (TypeError, ValueError) as e: | |
2074 | print e.args[0] |
|
2074 | print e.args[0] | |
2075 | return |
|
2075 | return | |
@@ -2080,38 +2080,11 b' Currently the magic system has the following functions:\\n"""' | |||||
2080 | f.write(cmds) |
|
2080 | f.write(cmds) | |
2081 | print 'The following commands were written to file `%s`:' % fname |
|
2081 | print 'The following commands were written to file `%s`:' % fname | |
2082 | print cmds |
|
2082 | print cmds | |
2083 |
|
||||
2084 | def _get_some_code(self, target, raw=True): |
|
|||
2085 | """Utility function to get a code string, either from a range of |
|
|||
2086 | history lines, a filename, or an expression evaluating to a string or a |
|
|||
2087 | Macro in the user namespace. |
|
|||
2088 |
|
||||
2089 | ValueError is raised if none are found, and TypeError if it evaluates to |
|
|||
2090 | an object of another type. In each case, .args[0] is a printable |
|
|||
2091 | message.""" |
|
|||
2092 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
|||
2093 | if code: |
|
|||
2094 | return code |
|
|||
2095 | if os.path.isfile(target): # Read file |
|
|||
2096 | return open(target, "r").read() |
|
|||
2097 |
|
||||
2098 | try: # User namespace |
|
|||
2099 | codeobj = eval(target, self.shell.user_ns) |
|
|||
2100 | except Exception: |
|
|||
2101 | raise ValueError(("'%s' was not found in history, as a file, nor in" |
|
|||
2102 | " the user namespace.") % target) |
|
|||
2103 | if isinstance(codeobj, basestring): |
|
|||
2104 | return codeobj |
|
|||
2105 | elif isinstance(codeobj, Macro): |
|
|||
2106 | return codeobj.value |
|
|||
2107 |
|
||||
2108 | raise TypeError("%s is neither a string nor a macro." % target, |
|
|||
2109 | codeobj) |
|
|||
2110 |
|
2083 | |||
2111 | def magic_pastebin(self, parameter_s = ''): |
|
2084 | def magic_pastebin(self, parameter_s = ''): | |
2112 | """Upload code to the 'Lodge it' paste bin, returning the URL.""" |
|
2085 | """Upload code to the 'Lodge it' paste bin, returning the URL.""" | |
2113 | try: |
|
2086 | try: | |
2114 | code = self._get_some_code(parameter_s) |
|
2087 | code = self.shell._get_some_code(parameter_s) | |
2115 | except (ValueError, TypeError) as e: |
|
2088 | except (ValueError, TypeError) as e: | |
2116 | print e.args[0] |
|
2089 | print e.args[0] | |
2117 | return |
|
2090 | return |
General Comments 0
You need to be logged in to leave comments.
Login now