Show More
@@ -1,19 +1,19 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Shim to maintain backwards compatibility with old IPython.config imports. |
|
3 | 3 | """ |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | import sys |
|
8 | 8 | from warnings import warn |
|
9 | 9 | |
|
10 |
from |
|
|
10 | from .utils.shimmodule import ShimModule, ShimWarning | |
|
11 | 11 | |
|
12 | 12 | warn("The `IPython.config` package has been deprecated since IPython 4.0. " |
|
13 | 13 | "You should import from traitlets.config instead.", ShimWarning) |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
17 | 17 | # trigger the custom attribute access above |
|
18 | 18 | |
|
19 | 19 | sys.modules['IPython.config'] = ShimModule(src='IPython.config', mirror='traitlets.config') |
@@ -1,69 +1,69 b'' | |||
|
1 | 1 | import types |
|
2 | 2 | import sys |
|
3 | 3 | import builtins |
|
4 | 4 | import os |
|
5 | 5 | import pytest |
|
6 | 6 | import pathlib |
|
7 | 7 | import shutil |
|
8 | 8 | |
|
9 |
from |
|
|
9 | from .testing import tools | |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | def get_ipython(): |
|
13 |
from |
|
|
13 | from .terminal.interactiveshell import TerminalInteractiveShell | |
|
14 | 14 | if TerminalInteractiveShell._instance: |
|
15 | 15 | return TerminalInteractiveShell.instance() |
|
16 | 16 | |
|
17 | 17 | config = tools.default_config() |
|
18 | 18 | config.TerminalInteractiveShell.simple_prompt = True |
|
19 | 19 | |
|
20 | 20 | # Create and initialize our test-friendly IPython instance. |
|
21 | 21 | shell = TerminalInteractiveShell.instance(config=config) |
|
22 | 22 | return shell |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | @pytest.fixture(scope='session', autouse=True) |
|
26 | 26 | def work_path(): |
|
27 | 27 | path = pathlib.Path("./tmp-ipython-pytest-profiledir") |
|
28 | 28 | os.environ["IPYTHONDIR"] = str(path.absolute()) |
|
29 | 29 | if path.exists(): |
|
30 | 30 | raise ValueError('IPython dir temporary path already exists ! Did previous test run exit successfully ?') |
|
31 | 31 | path.mkdir() |
|
32 | 32 | yield |
|
33 | 33 | shutil.rmtree(str(path.resolve())) |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def nopage(strng, start=0, screen_lines=0, pager_cmd=None): |
|
37 | 37 | if isinstance(strng, dict): |
|
38 | 38 | strng = strng.get("text/plain", "") |
|
39 | 39 | print(strng) |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def xsys(self, cmd): |
|
43 | 43 | """Replace the default system call with a capturing one for doctest. |
|
44 | 44 | """ |
|
45 | 45 | # We use getoutput, but we need to strip it because pexpect captures |
|
46 | 46 | # the trailing newline differently from commands.getoutput |
|
47 | 47 | print(self.getoutput(cmd, split=False, depth=1).rstrip(), end="", file=sys.stdout) |
|
48 | 48 | sys.stdout.flush() |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | # for things to work correctly we would need this as a session fixture; |
|
52 | 52 | # unfortunately this will fail on some test that get executed as _collection_ |
|
53 | 53 | # time (before the fixture run), in particular parametrized test that contain |
|
54 | 54 | # yields. so for now execute at import time. |
|
55 | 55 | #@pytest.fixture(autouse=True, scope='session') |
|
56 | 56 | def inject(): |
|
57 | 57 | |
|
58 | 58 | builtins.get_ipython = get_ipython |
|
59 | 59 | builtins._ip = get_ipython() |
|
60 | 60 | builtins.ip = get_ipython() |
|
61 | 61 | builtins.ip.system = types.MethodType(xsys, ip) |
|
62 | 62 | builtins.ip.builtin_trap.activate() |
|
63 |
from |
|
|
63 | from .core import page | |
|
64 | 64 | |
|
65 | 65 | page.pager_page = nopage |
|
66 | 66 | # yield |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | inject() |
@@ -1,258 +1,258 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | System command aliases. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Fernando Perez |
|
8 | 8 | * Brian Granger |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (C) 2008-2011 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. |
|
15 | 15 | # |
|
16 | 16 | # The full license is in the file COPYING.txt, distributed with this software. |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Imports |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | import os |
|
24 | 24 | import re |
|
25 | 25 | import sys |
|
26 | 26 | |
|
27 | 27 | from traitlets.config.configurable import Configurable |
|
28 |
from |
|
|
28 | from .error import UsageError | |
|
29 | 29 | |
|
30 | 30 | from traitlets import List, Instance |
|
31 | 31 | from logging import error |
|
32 | 32 | |
|
33 | 33 | #----------------------------------------------------------------------------- |
|
34 | 34 | # Utilities |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | 36 | |
|
37 | 37 | # This is used as the pattern for calls to split_user_input. |
|
38 | 38 | shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)') |
|
39 | 39 | |
|
40 | 40 | def default_aliases(): |
|
41 | 41 | """Return list of shell aliases to auto-define. |
|
42 | 42 | """ |
|
43 | 43 | # Note: the aliases defined here should be safe to use on a kernel |
|
44 | 44 | # regardless of what frontend it is attached to. Frontends that use a |
|
45 | 45 | # kernel in-process can define additional aliases that will only work in |
|
46 | 46 | # their case. For example, things like 'less' or 'clear' that manipulate |
|
47 | 47 | # the terminal should NOT be declared here, as they will only work if the |
|
48 | 48 | # kernel is running inside a true terminal, and not over the network. |
|
49 | 49 | |
|
50 | 50 | if os.name == 'posix': |
|
51 | 51 | default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
52 | 52 | ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'), |
|
53 | 53 | ('cat', 'cat'), |
|
54 | 54 | ] |
|
55 | 55 | # Useful set of ls aliases. The GNU and BSD options are a little |
|
56 | 56 | # different, so we make aliases that provide as similar as possible |
|
57 | 57 | # behavior in ipython, by passing the right flags for each platform |
|
58 | 58 | if sys.platform.startswith('linux'): |
|
59 | 59 | ls_aliases = [('ls', 'ls -F --color'), |
|
60 | 60 | # long ls |
|
61 | 61 | ('ll', 'ls -F -o --color'), |
|
62 | 62 | # ls normal files only |
|
63 | 63 | ('lf', 'ls -F -o --color %l | grep ^-'), |
|
64 | 64 | # ls symbolic links |
|
65 | 65 | ('lk', 'ls -F -o --color %l | grep ^l'), |
|
66 | 66 | # directories or links to directories, |
|
67 | 67 | ('ldir', 'ls -F -o --color %l | grep /$'), |
|
68 | 68 | # things which are executable |
|
69 | 69 | ('lx', 'ls -F -o --color %l | grep ^-..x'), |
|
70 | 70 | ] |
|
71 | 71 | elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'): |
|
72 | 72 | # OpenBSD, NetBSD. The ls implementation on these platforms do not support |
|
73 | 73 | # the -G switch and lack the ability to use colorized output. |
|
74 | 74 | ls_aliases = [('ls', 'ls -F'), |
|
75 | 75 | # long ls |
|
76 | 76 | ('ll', 'ls -F -l'), |
|
77 | 77 | # ls normal files only |
|
78 | 78 | ('lf', 'ls -F -l %l | grep ^-'), |
|
79 | 79 | # ls symbolic links |
|
80 | 80 | ('lk', 'ls -F -l %l | grep ^l'), |
|
81 | 81 | # directories or links to directories, |
|
82 | 82 | ('ldir', 'ls -F -l %l | grep /$'), |
|
83 | 83 | # things which are executable |
|
84 | 84 | ('lx', 'ls -F -l %l | grep ^-..x'), |
|
85 | 85 | ] |
|
86 | 86 | else: |
|
87 | 87 | # BSD, OSX, etc. |
|
88 | 88 | ls_aliases = [('ls', 'ls -F -G'), |
|
89 | 89 | # long ls |
|
90 | 90 | ('ll', 'ls -F -l -G'), |
|
91 | 91 | # ls normal files only |
|
92 | 92 | ('lf', 'ls -F -l -G %l | grep ^-'), |
|
93 | 93 | # ls symbolic links |
|
94 | 94 | ('lk', 'ls -F -l -G %l | grep ^l'), |
|
95 | 95 | # directories or links to directories, |
|
96 | 96 | ('ldir', 'ls -F -G -l %l | grep /$'), |
|
97 | 97 | # things which are executable |
|
98 | 98 | ('lx', 'ls -F -l -G %l | grep ^-..x'), |
|
99 | 99 | ] |
|
100 | 100 | default_aliases = default_aliases + ls_aliases |
|
101 | 101 | elif os.name in ['nt', 'dos']: |
|
102 | 102 | default_aliases = [('ls', 'dir /on'), |
|
103 | 103 | ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'), |
|
104 | 104 | ('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
105 | 105 | ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'), |
|
106 | 106 | ] |
|
107 | 107 | else: |
|
108 | 108 | default_aliases = [] |
|
109 | 109 | |
|
110 | 110 | return default_aliases |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | class AliasError(Exception): |
|
114 | 114 | pass |
|
115 | 115 | |
|
116 | 116 | |
|
117 | 117 | class InvalidAliasError(AliasError): |
|
118 | 118 | pass |
|
119 | 119 | |
|
120 | 120 | class Alias(object): |
|
121 | 121 | """Callable object storing the details of one alias. |
|
122 | 122 | |
|
123 | 123 | Instances are registered as magic functions to allow use of aliases. |
|
124 | 124 | """ |
|
125 | 125 | |
|
126 | 126 | # Prepare blacklist |
|
127 | 127 | blacklist = {'cd','popd','pushd','dhist','alias','unalias'} |
|
128 | 128 | |
|
129 | 129 | def __init__(self, shell, name, cmd): |
|
130 | 130 | self.shell = shell |
|
131 | 131 | self.name = name |
|
132 | 132 | self.cmd = cmd |
|
133 | 133 | self.__doc__ = "Alias for `!{}`".format(cmd) |
|
134 | 134 | self.nargs = self.validate() |
|
135 | 135 | |
|
136 | 136 | def validate(self): |
|
137 | 137 | """Validate the alias, and return the number of arguments.""" |
|
138 | 138 | if self.name in self.blacklist: |
|
139 | 139 | raise InvalidAliasError("The name %s can't be aliased " |
|
140 | 140 | "because it is a keyword or builtin." % self.name) |
|
141 | 141 | try: |
|
142 | 142 | caller = self.shell.magics_manager.magics['line'][self.name] |
|
143 | 143 | except KeyError: |
|
144 | 144 | pass |
|
145 | 145 | else: |
|
146 | 146 | if not isinstance(caller, Alias): |
|
147 | 147 | raise InvalidAliasError("The name %s can't be aliased " |
|
148 | 148 | "because it is another magic command." % self.name) |
|
149 | 149 | |
|
150 | 150 | if not (isinstance(self.cmd, str)): |
|
151 | 151 | raise InvalidAliasError("An alias command must be a string, " |
|
152 | 152 | "got: %r" % self.cmd) |
|
153 | 153 | |
|
154 | 154 | nargs = self.cmd.count('%s') - self.cmd.count('%%s') |
|
155 | 155 | |
|
156 | 156 | if (nargs > 0) and (self.cmd.find('%l') >= 0): |
|
157 | 157 | raise InvalidAliasError('The %s and %l specifiers are mutually ' |
|
158 | 158 | 'exclusive in alias definitions.') |
|
159 | 159 | |
|
160 | 160 | return nargs |
|
161 | 161 | |
|
162 | 162 | def __repr__(self): |
|
163 | 163 | return "<alias {} for {!r}>".format(self.name, self.cmd) |
|
164 | 164 | |
|
165 | 165 | def __call__(self, rest=''): |
|
166 | 166 | cmd = self.cmd |
|
167 | 167 | nargs = self.nargs |
|
168 | 168 | # Expand the %l special to be the user's input line |
|
169 | 169 | if cmd.find('%l') >= 0: |
|
170 | 170 | cmd = cmd.replace('%l', rest) |
|
171 | 171 | rest = '' |
|
172 | 172 | |
|
173 | 173 | if nargs==0: |
|
174 | 174 | if cmd.find('%%s') >= 1: |
|
175 | 175 | cmd = cmd.replace('%%s', '%s') |
|
176 | 176 | # Simple, argument-less aliases |
|
177 | 177 | cmd = '%s %s' % (cmd, rest) |
|
178 | 178 | else: |
|
179 | 179 | # Handle aliases with positional arguments |
|
180 | 180 | args = rest.split(None, nargs) |
|
181 | 181 | if len(args) < nargs: |
|
182 | 182 | raise UsageError('Alias <%s> requires %s arguments, %s given.' % |
|
183 | 183 | (self.name, nargs, len(args))) |
|
184 | 184 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
185 | 185 | |
|
186 | 186 | self.shell.system(cmd) |
|
187 | 187 | |
|
188 | 188 | #----------------------------------------------------------------------------- |
|
189 | 189 | # Main AliasManager class |
|
190 | 190 | #----------------------------------------------------------------------------- |
|
191 | 191 | |
|
192 | 192 | class AliasManager(Configurable): |
|
193 | 193 | |
|
194 | 194 | default_aliases = List(default_aliases()).tag(config=True) |
|
195 | 195 | user_aliases = List(default_value=[]).tag(config=True) |
|
196 | 196 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
197 | 197 | |
|
198 | 198 | def __init__(self, shell=None, **kwargs): |
|
199 | 199 | super(AliasManager, self).__init__(shell=shell, **kwargs) |
|
200 | 200 | # For convenient access |
|
201 | 201 | self.linemagics = self.shell.magics_manager.magics['line'] |
|
202 | 202 | self.init_aliases() |
|
203 | 203 | |
|
204 | 204 | def init_aliases(self): |
|
205 | 205 | # Load default & user aliases |
|
206 | 206 | for name, cmd in self.default_aliases + self.user_aliases: |
|
207 | 207 | if cmd.startswith('ls ') and self.shell.colors == 'NoColor': |
|
208 | 208 | cmd = cmd.replace(' --color', '') |
|
209 | 209 | self.soft_define_alias(name, cmd) |
|
210 | 210 | |
|
211 | 211 | @property |
|
212 | 212 | def aliases(self): |
|
213 | 213 | return [(n, func.cmd) for (n, func) in self.linemagics.items() |
|
214 | 214 | if isinstance(func, Alias)] |
|
215 | 215 | |
|
216 | 216 | def soft_define_alias(self, name, cmd): |
|
217 | 217 | """Define an alias, but don't raise on an AliasError.""" |
|
218 | 218 | try: |
|
219 | 219 | self.define_alias(name, cmd) |
|
220 | 220 | except AliasError as e: |
|
221 | 221 | error("Invalid alias: %s" % e) |
|
222 | 222 | |
|
223 | 223 | def define_alias(self, name, cmd): |
|
224 | 224 | """Define a new alias after validating it. |
|
225 | 225 | |
|
226 | 226 | This will raise an :exc:`AliasError` if there are validation |
|
227 | 227 | problems. |
|
228 | 228 | """ |
|
229 | 229 | caller = Alias(shell=self.shell, name=name, cmd=cmd) |
|
230 | 230 | self.shell.magics_manager.register_function(caller, magic_kind='line', |
|
231 | 231 | magic_name=name) |
|
232 | 232 | |
|
233 | 233 | def get_alias(self, name): |
|
234 | 234 | """Return an alias, or None if no alias by that name exists.""" |
|
235 | 235 | aname = self.linemagics.get(name, None) |
|
236 | 236 | return aname if isinstance(aname, Alias) else None |
|
237 | 237 | |
|
238 | 238 | def is_alias(self, name): |
|
239 | 239 | """Return whether or not a given name has been defined as an alias""" |
|
240 | 240 | return self.get_alias(name) is not None |
|
241 | 241 | |
|
242 | 242 | def undefine_alias(self, name): |
|
243 | 243 | if self.is_alias(name): |
|
244 | 244 | del self.linemagics[name] |
|
245 | 245 | else: |
|
246 | 246 | raise ValueError('%s is not an alias' % name) |
|
247 | 247 | |
|
248 | 248 | def clear_aliases(self): |
|
249 | 249 | for name, cmd in self.aliases: |
|
250 | 250 | self.undefine_alias(name) |
|
251 | 251 | |
|
252 | 252 | def retrieve_alias(self, name): |
|
253 | 253 | """Retrieve the command to which an alias expands.""" |
|
254 | 254 | caller = self.get_alias(name) |
|
255 | 255 | if caller: |
|
256 | 256 | return caller.cmd |
|
257 | 257 | else: |
|
258 | 258 | raise ValueError('%s is not an alias' % name) |
@@ -1,354 +1,354 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Implementations for various useful completers. |
|
3 | 3 | |
|
4 | 4 | These are all loaded by default by IPython. |
|
5 | 5 | """ |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2010-2011 The IPython Development Team. |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. |
|
10 | 10 | # |
|
11 | 11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | # Stdlib imports |
|
19 | 19 | import glob |
|
20 | 20 | import inspect |
|
21 | 21 | import os |
|
22 | 22 | import re |
|
23 | 23 | import sys |
|
24 | 24 | from importlib import import_module |
|
25 | 25 | from importlib.machinery import all_suffixes |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | # Third-party imports |
|
29 | 29 | from time import time |
|
30 | 30 | from zipimport import zipimporter |
|
31 | 31 | |
|
32 | 32 | # Our own imports |
|
33 |
from |
|
|
34 |
from |
|
|
35 |
from |
|
|
33 | from .completer import expand_user, compress_user | |
|
34 | from .error import TryNext | |
|
35 | from ..utils._process_common import arg_split | |
|
36 | 36 | |
|
37 | 37 | # FIXME: this should be pulled in with the right call via the component system |
|
38 | 38 | from IPython import get_ipython |
|
39 | 39 | |
|
40 | 40 | from typing import List |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | # Globals and constants |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | _suffixes = all_suffixes() |
|
46 | 46 | |
|
47 | 47 | # Time in seconds after which the rootmodules will be stored permanently in the |
|
48 | 48 | # ipython ip.db database (kept in the user's .ipython dir). |
|
49 | 49 | TIMEOUT_STORAGE = 2 |
|
50 | 50 | |
|
51 | 51 | # Time in seconds after which we give up |
|
52 | 52 | TIMEOUT_GIVEUP = 20 |
|
53 | 53 | |
|
54 | 54 | # Regular expression for the python import statement |
|
55 | 55 | import_re = re.compile(r'(?P<name>[a-zA-Z_][a-zA-Z0-9_]*?)' |
|
56 | 56 | r'(?P<package>[/\\]__init__)?' |
|
57 | 57 | r'(?P<suffix>%s)$' % |
|
58 | 58 | r'|'.join(re.escape(s) for s in _suffixes)) |
|
59 | 59 | |
|
60 | 60 | # RE for the ipython %run command (python + ipython scripts) |
|
61 | 61 | magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$') |
|
62 | 62 | |
|
63 | 63 | #----------------------------------------------------------------------------- |
|
64 | 64 | # Local utilities |
|
65 | 65 | #----------------------------------------------------------------------------- |
|
66 | 66 | |
|
67 | 67 | def module_list(path): |
|
68 | 68 | """ |
|
69 | 69 | Return the list containing the names of the modules available in the given |
|
70 | 70 | folder. |
|
71 | 71 | """ |
|
72 | 72 | # sys.path has the cwd as an empty string, but isdir/listdir need it as '.' |
|
73 | 73 | if path == '': |
|
74 | 74 | path = '.' |
|
75 | 75 | |
|
76 | 76 | # A few local constants to be used in loops below |
|
77 | 77 | pjoin = os.path.join |
|
78 | 78 | |
|
79 | 79 | if os.path.isdir(path): |
|
80 | 80 | # Build a list of all files in the directory and all files |
|
81 | 81 | # in its subdirectories. For performance reasons, do not |
|
82 | 82 | # recurse more than one level into subdirectories. |
|
83 | 83 | files = [] |
|
84 | 84 | for root, dirs, nondirs in os.walk(path, followlinks=True): |
|
85 | 85 | subdir = root[len(path)+1:] |
|
86 | 86 | if subdir: |
|
87 | 87 | files.extend(pjoin(subdir, f) for f in nondirs) |
|
88 | 88 | dirs[:] = [] # Do not recurse into additional subdirectories. |
|
89 | 89 | else: |
|
90 | 90 | files.extend(nondirs) |
|
91 | 91 | |
|
92 | 92 | else: |
|
93 | 93 | try: |
|
94 | 94 | files = list(zipimporter(path)._files.keys()) |
|
95 | 95 | except: |
|
96 | 96 | files = [] |
|
97 | 97 | |
|
98 | 98 | # Build a list of modules which match the import_re regex. |
|
99 | 99 | modules = [] |
|
100 | 100 | for f in files: |
|
101 | 101 | m = import_re.match(f) |
|
102 | 102 | if m: |
|
103 | 103 | modules.append(m.group('name')) |
|
104 | 104 | return list(set(modules)) |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | def get_root_modules(): |
|
108 | 108 | """ |
|
109 | 109 | Returns a list containing the names of all the modules available in the |
|
110 | 110 | folders of the pythonpath. |
|
111 | 111 | |
|
112 | 112 | ip.db['rootmodules_cache'] maps sys.path entries to list of modules. |
|
113 | 113 | """ |
|
114 | 114 | ip = get_ipython() |
|
115 | 115 | if ip is None: |
|
116 | 116 | # No global shell instance to store cached list of modules. |
|
117 | 117 | # Don't try to scan for modules every time. |
|
118 | 118 | return list(sys.builtin_module_names) |
|
119 | 119 | |
|
120 | 120 | rootmodules_cache = ip.db.get('rootmodules_cache', {}) |
|
121 | 121 | rootmodules = list(sys.builtin_module_names) |
|
122 | 122 | start_time = time() |
|
123 | 123 | store = False |
|
124 | 124 | for path in sys.path: |
|
125 | 125 | try: |
|
126 | 126 | modules = rootmodules_cache[path] |
|
127 | 127 | except KeyError: |
|
128 | 128 | modules = module_list(path) |
|
129 | 129 | try: |
|
130 | 130 | modules.remove('__init__') |
|
131 | 131 | except ValueError: |
|
132 | 132 | pass |
|
133 | 133 | if path not in ('', '.'): # cwd modules should not be cached |
|
134 | 134 | rootmodules_cache[path] = modules |
|
135 | 135 | if time() - start_time > TIMEOUT_STORAGE and not store: |
|
136 | 136 | store = True |
|
137 | 137 | print("\nCaching the list of root modules, please wait!") |
|
138 | 138 | print("(This will only be done once - type '%rehashx' to " |
|
139 | 139 | "reset cache!)\n") |
|
140 | 140 | sys.stdout.flush() |
|
141 | 141 | if time() - start_time > TIMEOUT_GIVEUP: |
|
142 | 142 | print("This is taking too long, we give up.\n") |
|
143 | 143 | return [] |
|
144 | 144 | rootmodules.extend(modules) |
|
145 | 145 | if store: |
|
146 | 146 | ip.db['rootmodules_cache'] = rootmodules_cache |
|
147 | 147 | rootmodules = list(set(rootmodules)) |
|
148 | 148 | return rootmodules |
|
149 | 149 | |
|
150 | 150 | |
|
151 | 151 | def is_importable(module, attr, only_modules): |
|
152 | 152 | if only_modules: |
|
153 | 153 | return inspect.ismodule(getattr(module, attr)) |
|
154 | 154 | else: |
|
155 | 155 | return not(attr[:2] == '__' and attr[-2:] == '__') |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | def try_import(mod: str, only_modules=False) -> List[str]: |
|
159 | 159 | """ |
|
160 | 160 | Try to import given module and return list of potential completions. |
|
161 | 161 | """ |
|
162 | 162 | mod = mod.rstrip('.') |
|
163 | 163 | try: |
|
164 | 164 | m = import_module(mod) |
|
165 | 165 | except: |
|
166 | 166 | return [] |
|
167 | 167 | |
|
168 | 168 | m_is_init = '__init__' in (getattr(m, '__file__', '') or '') |
|
169 | 169 | |
|
170 | 170 | completions = [] |
|
171 | 171 | if (not hasattr(m, '__file__')) or (not only_modules) or m_is_init: |
|
172 | 172 | completions.extend( [attr for attr in dir(m) if |
|
173 | 173 | is_importable(m, attr, only_modules)]) |
|
174 | 174 | |
|
175 | 175 | completions.extend(getattr(m, '__all__', [])) |
|
176 | 176 | if m_is_init: |
|
177 | 177 | completions.extend(module_list(os.path.dirname(m.__file__))) |
|
178 | 178 | completions_set = {c for c in completions if isinstance(c, str)} |
|
179 | 179 | completions_set.discard('__init__') |
|
180 | 180 | return list(completions_set) |
|
181 | 181 | |
|
182 | 182 | |
|
183 | 183 | #----------------------------------------------------------------------------- |
|
184 | 184 | # Completion-related functions. |
|
185 | 185 | #----------------------------------------------------------------------------- |
|
186 | 186 | |
|
187 | 187 | def quick_completer(cmd, completions): |
|
188 | 188 | r""" Easily create a trivial completer for a command. |
|
189 | 189 | |
|
190 | 190 | Takes either a list of completions, or all completions in string (that will |
|
191 | 191 | be split on whitespace). |
|
192 | 192 | |
|
193 | 193 | Example:: |
|
194 | 194 | |
|
195 | 195 | [d:\ipython]|1> import ipy_completers |
|
196 | 196 | [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz']) |
|
197 | 197 | [d:\ipython]|3> foo b<TAB> |
|
198 | 198 | bar baz |
|
199 | 199 | [d:\ipython]|3> foo ba |
|
200 | 200 | """ |
|
201 | 201 | |
|
202 | 202 | if isinstance(completions, str): |
|
203 | 203 | completions = completions.split() |
|
204 | 204 | |
|
205 | 205 | def do_complete(self, event): |
|
206 | 206 | return completions |
|
207 | 207 | |
|
208 | 208 | get_ipython().set_hook('complete_command',do_complete, str_key = cmd) |
|
209 | 209 | |
|
210 | 210 | def module_completion(line): |
|
211 | 211 | """ |
|
212 | 212 | Returns a list containing the completion possibilities for an import line. |
|
213 | 213 | |
|
214 | 214 | The line looks like this : |
|
215 | 215 | 'import xml.d' |
|
216 | 216 | 'from xml.dom import' |
|
217 | 217 | """ |
|
218 | 218 | |
|
219 | 219 | words = line.split(' ') |
|
220 | 220 | nwords = len(words) |
|
221 | 221 | |
|
222 | 222 | # from whatever <tab> -> 'import ' |
|
223 | 223 | if nwords == 3 and words[0] == 'from': |
|
224 | 224 | return ['import '] |
|
225 | 225 | |
|
226 | 226 | # 'from xy<tab>' or 'import xy<tab>' |
|
227 | 227 | if nwords < 3 and (words[0] in {'%aimport', 'import', 'from'}) : |
|
228 | 228 | if nwords == 1: |
|
229 | 229 | return get_root_modules() |
|
230 | 230 | mod = words[1].split('.') |
|
231 | 231 | if len(mod) < 2: |
|
232 | 232 | return get_root_modules() |
|
233 | 233 | completion_list = try_import('.'.join(mod[:-1]), True) |
|
234 | 234 | return ['.'.join(mod[:-1] + [el]) for el in completion_list] |
|
235 | 235 | |
|
236 | 236 | # 'from xyz import abc<tab>' |
|
237 | 237 | if nwords >= 3 and words[0] == 'from': |
|
238 | 238 | mod = words[1] |
|
239 | 239 | return try_import(mod) |
|
240 | 240 | |
|
241 | 241 | #----------------------------------------------------------------------------- |
|
242 | 242 | # Completers |
|
243 | 243 | #----------------------------------------------------------------------------- |
|
244 | 244 | # These all have the func(self, event) signature to be used as custom |
|
245 | 245 | # completers |
|
246 | 246 | |
|
247 | 247 | def module_completer(self,event): |
|
248 | 248 | """Give completions after user has typed 'import ...' or 'from ...'""" |
|
249 | 249 | |
|
250 | 250 | # This works in all versions of python. While 2.5 has |
|
251 | 251 | # pkgutil.walk_packages(), that particular routine is fairly dangerous, |
|
252 | 252 | # since it imports *EVERYTHING* on sys.path. That is: a) very slow b) full |
|
253 | 253 | # of possibly problematic side effects. |
|
254 | 254 | # This search the folders in the sys.path for available modules. |
|
255 | 255 | |
|
256 | 256 | return module_completion(event.line) |
|
257 | 257 | |
|
258 | 258 | # FIXME: there's a lot of logic common to the run, cd and builtin file |
|
259 | 259 | # completers, that is currently reimplemented in each. |
|
260 | 260 | |
|
261 | 261 | def magic_run_completer(self, event): |
|
262 | 262 | """Complete files that end in .py or .ipy or .ipynb for the %run command. |
|
263 | 263 | """ |
|
264 | 264 | comps = arg_split(event.line, strict=False) |
|
265 | 265 | # relpath should be the current token that we need to complete. |
|
266 | 266 | if (len(comps) > 1) and (not event.line.endswith(' ')): |
|
267 | 267 | relpath = comps[-1].strip("'\"") |
|
268 | 268 | else: |
|
269 | 269 | relpath = '' |
|
270 | 270 | |
|
271 | 271 | #print("\nev=", event) # dbg |
|
272 | 272 | #print("rp=", relpath) # dbg |
|
273 | 273 | #print('comps=', comps) # dbg |
|
274 | 274 | |
|
275 | 275 | lglob = glob.glob |
|
276 | 276 | isdir = os.path.isdir |
|
277 | 277 | relpath, tilde_expand, tilde_val = expand_user(relpath) |
|
278 | 278 | |
|
279 | 279 | # Find if the user has already typed the first filename, after which we |
|
280 | 280 | # should complete on all files, since after the first one other files may |
|
281 | 281 | # be arguments to the input script. |
|
282 | 282 | |
|
283 | 283 | if any(magic_run_re.match(c) for c in comps): |
|
284 | 284 | matches = [f.replace('\\','/') + ('/' if isdir(f) else '') |
|
285 | 285 | for f in lglob(relpath+'*')] |
|
286 | 286 | else: |
|
287 | 287 | dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)] |
|
288 | 288 | pys = [f.replace('\\','/') |
|
289 | 289 | for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + |
|
290 | 290 | lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')] |
|
291 | 291 | |
|
292 | 292 | matches = dirs + pys |
|
293 | 293 | |
|
294 | 294 | #print('run comp:', dirs+pys) # dbg |
|
295 | 295 | return [compress_user(p, tilde_expand, tilde_val) for p in matches] |
|
296 | 296 | |
|
297 | 297 | |
|
298 | 298 | def cd_completer(self, event): |
|
299 | 299 | """Completer function for cd, which only returns directories.""" |
|
300 | 300 | ip = get_ipython() |
|
301 | 301 | relpath = event.symbol |
|
302 | 302 | |
|
303 | 303 | #print(event) # dbg |
|
304 | 304 | if event.line.endswith('-b') or ' -b ' in event.line: |
|
305 | 305 | # return only bookmark completions |
|
306 | 306 | bkms = self.db.get('bookmarks', None) |
|
307 | 307 | if bkms: |
|
308 | 308 | return bkms.keys() |
|
309 | 309 | else: |
|
310 | 310 | return [] |
|
311 | 311 | |
|
312 | 312 | if event.symbol == '-': |
|
313 | 313 | width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) |
|
314 | 314 | # jump in directory history by number |
|
315 | 315 | fmt = '-%0' + width_dh +'d [%s]' |
|
316 | 316 | ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
|
317 | 317 | if len(ents) > 1: |
|
318 | 318 | return ents |
|
319 | 319 | return [] |
|
320 | 320 | |
|
321 | 321 | if event.symbol.startswith('--'): |
|
322 | 322 | return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']] |
|
323 | 323 | |
|
324 | 324 | # Expand ~ in path and normalize directory separators. |
|
325 | 325 | relpath, tilde_expand, tilde_val = expand_user(relpath) |
|
326 | 326 | relpath = relpath.replace('\\','/') |
|
327 | 327 | |
|
328 | 328 | found = [] |
|
329 | 329 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
|
330 | 330 | if os.path.isdir(f)]: |
|
331 | 331 | if ' ' in d: |
|
332 | 332 | # we don't want to deal with any of that, complex code |
|
333 | 333 | # for this is elsewhere |
|
334 | 334 | raise TryNext |
|
335 | 335 | |
|
336 | 336 | found.append(d) |
|
337 | 337 | |
|
338 | 338 | if not found: |
|
339 | 339 | if os.path.isdir(relpath): |
|
340 | 340 | return [compress_user(relpath, tilde_expand, tilde_val)] |
|
341 | 341 | |
|
342 | 342 | # if no completions so far, try bookmarks |
|
343 | 343 | bks = self.db.get('bookmarks',{}) |
|
344 | 344 | bkmatches = [s for s in bks if s.startswith(event.symbol)] |
|
345 | 345 | if bkmatches: |
|
346 | 346 | return bkmatches |
|
347 | 347 | |
|
348 | 348 | raise TryNext |
|
349 | 349 | |
|
350 | 350 | return [compress_user(p, tilde_expand, tilde_val) for p in found] |
|
351 | 351 | |
|
352 | 352 | def reset_completer(self, event): |
|
353 | 353 | "A completer for %reset magic" |
|
354 | 354 | return '-f -s in out array dhist'.split() |
@@ -1,1024 +1,1024 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Display formatters. |
|
3 | 3 | |
|
4 | 4 | Inheritance diagram: |
|
5 | 5 | |
|
6 | 6 | .. inheritance-diagram:: IPython.core.formatters |
|
7 | 7 | :parts: 3 |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | # Copyright (c) IPython Development Team. |
|
11 | 11 | # Distributed under the terms of the Modified BSD License. |
|
12 | 12 | |
|
13 | 13 | import abc |
|
14 | 14 | import json |
|
15 | 15 | import sys |
|
16 | 16 | import traceback |
|
17 | 17 | import warnings |
|
18 | 18 | from io import StringIO |
|
19 | 19 | |
|
20 | 20 | from decorator import decorator |
|
21 | 21 | |
|
22 | 22 | from traitlets.config.configurable import Configurable |
|
23 |
from |
|
|
24 |
from |
|
|
25 |
from |
|
|
26 |
from |
|
|
23 | from .getipython import get_ipython | |
|
24 | from ..utils.sentinel import Sentinel | |
|
25 | from ..utils.dir2 import get_real_method | |
|
26 | from ..lib import pretty | |
|
27 | 27 | from traitlets import ( |
|
28 | 28 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, |
|
29 | 29 | ForwardDeclaredInstance, |
|
30 | 30 | default, observe, |
|
31 | 31 | ) |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | class DisplayFormatter(Configurable): |
|
35 | 35 | |
|
36 | 36 | active_types = List(Unicode(), |
|
37 | 37 | help="""List of currently active mime-types to display. |
|
38 | 38 | You can use this to set a white-list for formats to display. |
|
39 | 39 | |
|
40 | 40 | Most users will not need to change this value. |
|
41 | 41 | """).tag(config=True) |
|
42 | 42 | |
|
43 | 43 | @default('active_types') |
|
44 | 44 | def _active_types_default(self): |
|
45 | 45 | return self.format_types |
|
46 | 46 | |
|
47 | 47 | @observe('active_types') |
|
48 | 48 | def _active_types_changed(self, change): |
|
49 | 49 | for key, formatter in self.formatters.items(): |
|
50 | 50 | if key in change['new']: |
|
51 | 51 | formatter.enabled = True |
|
52 | 52 | else: |
|
53 | 53 | formatter.enabled = False |
|
54 | 54 | |
|
55 | 55 | ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') |
|
56 | 56 | @default('ipython_display_formatter') |
|
57 | 57 | def _default_formatter(self): |
|
58 | 58 | return IPythonDisplayFormatter(parent=self) |
|
59 | 59 | |
|
60 | 60 | mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') |
|
61 | 61 | @default('mimebundle_formatter') |
|
62 | 62 | def _default_mime_formatter(self): |
|
63 | 63 | return MimeBundleFormatter(parent=self) |
|
64 | 64 | |
|
65 | 65 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
66 | 66 | # values are subclasses of BaseFormatter. |
|
67 | 67 | formatters = Dict() |
|
68 | 68 | @default('formatters') |
|
69 | 69 | def _formatters_default(self): |
|
70 | 70 | """Activate the default formatters.""" |
|
71 | 71 | formatter_classes = [ |
|
72 | 72 | PlainTextFormatter, |
|
73 | 73 | HTMLFormatter, |
|
74 | 74 | MarkdownFormatter, |
|
75 | 75 | SVGFormatter, |
|
76 | 76 | PNGFormatter, |
|
77 | 77 | PDFFormatter, |
|
78 | 78 | JPEGFormatter, |
|
79 | 79 | LatexFormatter, |
|
80 | 80 | JSONFormatter, |
|
81 | 81 | JavascriptFormatter |
|
82 | 82 | ] |
|
83 | 83 | d = {} |
|
84 | 84 | for cls in formatter_classes: |
|
85 | 85 | f = cls(parent=self) |
|
86 | 86 | d[f.format_type] = f |
|
87 | 87 | return d |
|
88 | 88 | |
|
89 | 89 | def format(self, obj, include=None, exclude=None): |
|
90 | 90 | """Return a format data dict for an object. |
|
91 | 91 | |
|
92 | 92 | By default all format types will be computed. |
|
93 | 93 | |
|
94 | 94 | The following MIME types are usually implemented: |
|
95 | 95 | |
|
96 | 96 | * text/plain |
|
97 | 97 | * text/html |
|
98 | 98 | * text/markdown |
|
99 | 99 | * text/latex |
|
100 | 100 | * application/json |
|
101 | 101 | * application/javascript |
|
102 | 102 | * application/pdf |
|
103 | 103 | * image/png |
|
104 | 104 | * image/jpeg |
|
105 | 105 | * image/svg+xml |
|
106 | 106 | |
|
107 | 107 | Parameters |
|
108 | 108 | ---------- |
|
109 | 109 | obj : object |
|
110 | 110 | The Python object whose format data will be computed. |
|
111 | 111 | include : list, tuple or set; optional |
|
112 | 112 | A list of format type strings (MIME types) to include in the |
|
113 | 113 | format data dict. If this is set *only* the format types included |
|
114 | 114 | in this list will be computed. |
|
115 | 115 | exclude : list, tuple or set; optional |
|
116 | 116 | A list of format type string (MIME types) to exclude in the format |
|
117 | 117 | data dict. If this is set all format types will be computed, |
|
118 | 118 | except for those included in this argument. |
|
119 | 119 | Mimetypes present in exclude will take precedence over the ones in include |
|
120 | 120 | |
|
121 | 121 | Returns |
|
122 | 122 | ------- |
|
123 | 123 | (format_dict, metadata_dict) : tuple of two dicts |
|
124 | 124 | |
|
125 | 125 | format_dict is a dictionary of key/value pairs, one of each format that was |
|
126 | 126 | generated for the object. The keys are the format types, which |
|
127 | 127 | will usually be MIME type strings and the values and JSON'able |
|
128 | 128 | data structure containing the raw data for the representation in |
|
129 | 129 | that format. |
|
130 | 130 | |
|
131 | 131 | metadata_dict is a dictionary of metadata about each mime-type output. |
|
132 | 132 | Its keys will be a strict subset of the keys in format_dict. |
|
133 | 133 | |
|
134 | 134 | Notes |
|
135 | 135 | ----- |
|
136 | 136 | |
|
137 | 137 | If an object implement `_repr_mimebundle_` as well as various |
|
138 | 138 | `_repr_*_`, the data returned by `_repr_mimebundle_` will take |
|
139 | 139 | precedence and the corresponding `_repr_*_` for this mimetype will |
|
140 | 140 | not be called. |
|
141 | 141 | |
|
142 | 142 | """ |
|
143 | 143 | format_dict = {} |
|
144 | 144 | md_dict = {} |
|
145 | 145 | |
|
146 | 146 | if self.ipython_display_formatter(obj): |
|
147 | 147 | # object handled itself, don't proceed |
|
148 | 148 | return {}, {} |
|
149 | 149 | |
|
150 | 150 | format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude) |
|
151 | 151 | |
|
152 | 152 | if format_dict or md_dict: |
|
153 | 153 | if include: |
|
154 | 154 | format_dict = {k:v for k,v in format_dict.items() if k in include} |
|
155 | 155 | md_dict = {k:v for k,v in md_dict.items() if k in include} |
|
156 | 156 | if exclude: |
|
157 | 157 | format_dict = {k:v for k,v in format_dict.items() if k not in exclude} |
|
158 | 158 | md_dict = {k:v for k,v in md_dict.items() if k not in exclude} |
|
159 | 159 | |
|
160 | 160 | for format_type, formatter in self.formatters.items(): |
|
161 | 161 | if format_type in format_dict: |
|
162 | 162 | # already got it from mimebundle, maybe don't render again. |
|
163 | 163 | # exception: manually registered per-mime renderer |
|
164 | 164 | # check priority: |
|
165 | 165 | # 1. user-registered per-mime formatter |
|
166 | 166 | # 2. mime-bundle (user-registered or repr method) |
|
167 | 167 | # 3. default per-mime formatter (e.g. repr method) |
|
168 | 168 | try: |
|
169 | 169 | formatter.lookup(obj) |
|
170 | 170 | except KeyError: |
|
171 | 171 | # no special formatter, use mime-bundle-provided value |
|
172 | 172 | continue |
|
173 | 173 | if include and format_type not in include: |
|
174 | 174 | continue |
|
175 | 175 | if exclude and format_type in exclude: |
|
176 | 176 | continue |
|
177 | 177 | |
|
178 | 178 | md = None |
|
179 | 179 | try: |
|
180 | 180 | data = formatter(obj) |
|
181 | 181 | except: |
|
182 | 182 | # FIXME: log the exception |
|
183 | 183 | raise |
|
184 | 184 | |
|
185 | 185 | # formatters can return raw data or (data, metadata) |
|
186 | 186 | if isinstance(data, tuple) and len(data) == 2: |
|
187 | 187 | data, md = data |
|
188 | 188 | |
|
189 | 189 | if data is not None: |
|
190 | 190 | format_dict[format_type] = data |
|
191 | 191 | if md is not None: |
|
192 | 192 | md_dict[format_type] = md |
|
193 | 193 | return format_dict, md_dict |
|
194 | 194 | |
|
195 | 195 | @property |
|
196 | 196 | def format_types(self): |
|
197 | 197 | """Return the format types (MIME types) of the active formatters.""" |
|
198 | 198 | return list(self.formatters.keys()) |
|
199 | 199 | |
|
200 | 200 | |
|
201 | 201 | #----------------------------------------------------------------------------- |
|
202 | 202 | # Formatters for specific format types (text, html, svg, etc.) |
|
203 | 203 | #----------------------------------------------------------------------------- |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | def _safe_repr(obj): |
|
207 | 207 | """Try to return a repr of an object |
|
208 | 208 | |
|
209 | 209 | always returns a string, at least. |
|
210 | 210 | """ |
|
211 | 211 | try: |
|
212 | 212 | return repr(obj) |
|
213 | 213 | except Exception as e: |
|
214 | 214 | return "un-repr-able object (%r)" % e |
|
215 | 215 | |
|
216 | 216 | |
|
217 | 217 | class FormatterWarning(UserWarning): |
|
218 | 218 | """Warning class for errors in formatters""" |
|
219 | 219 | |
|
220 | 220 | @decorator |
|
221 | 221 | def catch_format_error(method, self, *args, **kwargs): |
|
222 | 222 | """show traceback on failed format call""" |
|
223 | 223 | try: |
|
224 | 224 | r = method(self, *args, **kwargs) |
|
225 | 225 | except NotImplementedError: |
|
226 | 226 | # don't warn on NotImplementedErrors |
|
227 | 227 | return self._check_return(None, args[0]) |
|
228 | 228 | except Exception: |
|
229 | 229 | exc_info = sys.exc_info() |
|
230 | 230 | ip = get_ipython() |
|
231 | 231 | if ip is not None: |
|
232 | 232 | ip.showtraceback(exc_info) |
|
233 | 233 | else: |
|
234 | 234 | traceback.print_exception(*exc_info) |
|
235 | 235 | return self._check_return(None, args[0]) |
|
236 | 236 | return self._check_return(r, args[0]) |
|
237 | 237 | |
|
238 | 238 | |
|
239 | 239 | class FormatterABC(metaclass=abc.ABCMeta): |
|
240 | 240 | """ Abstract base class for Formatters. |
|
241 | 241 | |
|
242 | 242 | A formatter is a callable class that is responsible for computing the |
|
243 | 243 | raw format data for a particular format type (MIME type). For example, |
|
244 | 244 | an HTML formatter would have a format type of `text/html` and would return |
|
245 | 245 | the HTML representation of the object when called. |
|
246 | 246 | """ |
|
247 | 247 | |
|
248 | 248 | # The format type of the data returned, usually a MIME type. |
|
249 | 249 | format_type = 'text/plain' |
|
250 | 250 | |
|
251 | 251 | # Is the formatter enabled... |
|
252 | 252 | enabled = True |
|
253 | 253 | |
|
254 | 254 | @abc.abstractmethod |
|
255 | 255 | def __call__(self, obj): |
|
256 | 256 | """Return a JSON'able representation of the object. |
|
257 | 257 | |
|
258 | 258 | If the object cannot be formatted by this formatter, |
|
259 | 259 | warn and return None. |
|
260 | 260 | """ |
|
261 | 261 | return repr(obj) |
|
262 | 262 | |
|
263 | 263 | |
|
264 | 264 | def _mod_name_key(typ): |
|
265 | 265 | """Return a (__module__, __name__) tuple for a type. |
|
266 | 266 | |
|
267 | 267 | Used as key in Formatter.deferred_printers. |
|
268 | 268 | """ |
|
269 | 269 | module = getattr(typ, '__module__', None) |
|
270 | 270 | name = getattr(typ, '__name__', None) |
|
271 | 271 | return (module, name) |
|
272 | 272 | |
|
273 | 273 | |
|
274 | 274 | def _get_type(obj): |
|
275 | 275 | """Return the type of an instance (old and new-style)""" |
|
276 | 276 | return getattr(obj, '__class__', None) or type(obj) |
|
277 | 277 | |
|
278 | 278 | |
|
279 | 279 | _raise_key_error = Sentinel('_raise_key_error', __name__, |
|
280 | 280 | """ |
|
281 | 281 | Special value to raise a KeyError |
|
282 | 282 | |
|
283 | 283 | Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop` |
|
284 | 284 | """) |
|
285 | 285 | |
|
286 | 286 | |
|
287 | 287 | class BaseFormatter(Configurable): |
|
288 | 288 | """A base formatter class that is configurable. |
|
289 | 289 | |
|
290 | 290 | This formatter should usually be used as the base class of all formatters. |
|
291 | 291 | It is a traited :class:`Configurable` class and includes an extensible |
|
292 | 292 | API for users to determine how their objects are formatted. The following |
|
293 | 293 | logic is used to find a function to format an given object. |
|
294 | 294 | |
|
295 | 295 | 1. The object is introspected to see if it has a method with the name |
|
296 | 296 | :attr:`print_method`. If is does, that object is passed to that method |
|
297 | 297 | for formatting. |
|
298 | 298 | 2. If no print method is found, three internal dictionaries are consulted |
|
299 | 299 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
300 | 300 | and :attr:`deferred_printers`. |
|
301 | 301 | |
|
302 | 302 | Users should use these dictionaries to register functions that will be |
|
303 | 303 | used to compute the format data for their objects (if those objects don't |
|
304 | 304 | have the special print methods). The easiest way of using these |
|
305 | 305 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
306 | 306 | methods. |
|
307 | 307 | |
|
308 | 308 | If no function/callable is found to compute the format data, ``None`` is |
|
309 | 309 | returned and this format type is not used. |
|
310 | 310 | """ |
|
311 | 311 | |
|
312 | 312 | format_type = Unicode('text/plain') |
|
313 | 313 | _return_type = str |
|
314 | 314 | |
|
315 | 315 | enabled = Bool(True).tag(config=True) |
|
316 | 316 | |
|
317 | 317 | print_method = ObjectName('__repr__') |
|
318 | 318 | |
|
319 | 319 | # The singleton printers. |
|
320 | 320 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
321 | 321 | singleton_printers = Dict().tag(config=True) |
|
322 | 322 | |
|
323 | 323 | # The type-specific printers. |
|
324 | 324 | # Map type objects to the format functions. |
|
325 | 325 | type_printers = Dict().tag(config=True) |
|
326 | 326 | |
|
327 | 327 | # The deferred-import type-specific printers. |
|
328 | 328 | # Map (modulename, classname) pairs to the format functions. |
|
329 | 329 | deferred_printers = Dict().tag(config=True) |
|
330 | 330 | |
|
331 | 331 | @catch_format_error |
|
332 | 332 | def __call__(self, obj): |
|
333 | 333 | """Compute the format for an object.""" |
|
334 | 334 | if self.enabled: |
|
335 | 335 | # lookup registered printer |
|
336 | 336 | try: |
|
337 | 337 | printer = self.lookup(obj) |
|
338 | 338 | except KeyError: |
|
339 | 339 | pass |
|
340 | 340 | else: |
|
341 | 341 | return printer(obj) |
|
342 | 342 | # Finally look for special method names |
|
343 | 343 | method = get_real_method(obj, self.print_method) |
|
344 | 344 | if method is not None: |
|
345 | 345 | return method() |
|
346 | 346 | return None |
|
347 | 347 | else: |
|
348 | 348 | return None |
|
349 | 349 | |
|
350 | 350 | def __contains__(self, typ): |
|
351 | 351 | """map in to lookup_by_type""" |
|
352 | 352 | try: |
|
353 | 353 | self.lookup_by_type(typ) |
|
354 | 354 | except KeyError: |
|
355 | 355 | return False |
|
356 | 356 | else: |
|
357 | 357 | return True |
|
358 | 358 | |
|
359 | 359 | def _check_return(self, r, obj): |
|
360 | 360 | """Check that a return value is appropriate |
|
361 | 361 | |
|
362 | 362 | Return the value if so, None otherwise, warning if invalid. |
|
363 | 363 | """ |
|
364 | 364 | if r is None or isinstance(r, self._return_type) or \ |
|
365 | 365 | (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)): |
|
366 | 366 | return r |
|
367 | 367 | else: |
|
368 | 368 | warnings.warn( |
|
369 | 369 | "%s formatter returned invalid type %s (expected %s) for object: %s" % \ |
|
370 | 370 | (self.format_type, type(r), self._return_type, _safe_repr(obj)), |
|
371 | 371 | FormatterWarning |
|
372 | 372 | ) |
|
373 | 373 | |
|
374 | 374 | def lookup(self, obj): |
|
375 | 375 | """Look up the formatter for a given instance. |
|
376 | 376 | |
|
377 | 377 | Parameters |
|
378 | 378 | ---------- |
|
379 | 379 | obj : object instance |
|
380 | 380 | |
|
381 | 381 | Returns |
|
382 | 382 | ------- |
|
383 | 383 | f : callable |
|
384 | 384 | The registered formatting callable for the type. |
|
385 | 385 | |
|
386 | 386 | Raises |
|
387 | 387 | ------ |
|
388 | 388 | KeyError if the type has not been registered. |
|
389 | 389 | """ |
|
390 | 390 | # look for singleton first |
|
391 | 391 | obj_id = id(obj) |
|
392 | 392 | if obj_id in self.singleton_printers: |
|
393 | 393 | return self.singleton_printers[obj_id] |
|
394 | 394 | # then lookup by type |
|
395 | 395 | return self.lookup_by_type(_get_type(obj)) |
|
396 | 396 | |
|
397 | 397 | def lookup_by_type(self, typ): |
|
398 | 398 | """Look up the registered formatter for a type. |
|
399 | 399 | |
|
400 | 400 | Parameters |
|
401 | 401 | ---------- |
|
402 | 402 | typ : type or '__module__.__name__' string for a type |
|
403 | 403 | |
|
404 | 404 | Returns |
|
405 | 405 | ------- |
|
406 | 406 | f : callable |
|
407 | 407 | The registered formatting callable for the type. |
|
408 | 408 | |
|
409 | 409 | Raises |
|
410 | 410 | ------ |
|
411 | 411 | KeyError if the type has not been registered. |
|
412 | 412 | """ |
|
413 | 413 | if isinstance(typ, str): |
|
414 | 414 | typ_key = tuple(typ.rsplit('.',1)) |
|
415 | 415 | if typ_key not in self.deferred_printers: |
|
416 | 416 | # We may have it cached in the type map. We will have to |
|
417 | 417 | # iterate over all of the types to check. |
|
418 | 418 | for cls in self.type_printers: |
|
419 | 419 | if _mod_name_key(cls) == typ_key: |
|
420 | 420 | return self.type_printers[cls] |
|
421 | 421 | else: |
|
422 | 422 | return self.deferred_printers[typ_key] |
|
423 | 423 | else: |
|
424 | 424 | for cls in pretty._get_mro(typ): |
|
425 | 425 | if cls in self.type_printers or self._in_deferred_types(cls): |
|
426 | 426 | return self.type_printers[cls] |
|
427 | 427 | |
|
428 | 428 | # If we have reached here, the lookup failed. |
|
429 | 429 | raise KeyError("No registered printer for {0!r}".format(typ)) |
|
430 | 430 | |
|
431 | 431 | def for_type(self, typ, func=None): |
|
432 | 432 | """Add a format function for a given type. |
|
433 | 433 | |
|
434 | 434 | Parameters |
|
435 | 435 | ----------- |
|
436 | 436 | typ : type or '__module__.__name__' string for a type |
|
437 | 437 | The class of the object that will be formatted using `func`. |
|
438 | 438 | func : callable |
|
439 | 439 | A callable for computing the format data. |
|
440 | 440 | `func` will be called with the object to be formatted, |
|
441 | 441 | and will return the raw data in this formatter's format. |
|
442 | 442 | Subclasses may use a different call signature for the |
|
443 | 443 | `func` argument. |
|
444 | 444 | |
|
445 | 445 | If `func` is None or not specified, there will be no change, |
|
446 | 446 | only returning the current value. |
|
447 | 447 | |
|
448 | 448 | Returns |
|
449 | 449 | ------- |
|
450 | 450 | oldfunc : callable |
|
451 | 451 | The currently registered callable. |
|
452 | 452 | If you are registering a new formatter, |
|
453 | 453 | this will be the previous value (to enable restoring later). |
|
454 | 454 | """ |
|
455 | 455 | # if string given, interpret as 'pkg.module.class_name' |
|
456 | 456 | if isinstance(typ, str): |
|
457 | 457 | type_module, type_name = typ.rsplit('.', 1) |
|
458 | 458 | return self.for_type_by_name(type_module, type_name, func) |
|
459 | 459 | |
|
460 | 460 | try: |
|
461 | 461 | oldfunc = self.lookup_by_type(typ) |
|
462 | 462 | except KeyError: |
|
463 | 463 | oldfunc = None |
|
464 | 464 | |
|
465 | 465 | if func is not None: |
|
466 | 466 | self.type_printers[typ] = func |
|
467 | 467 | |
|
468 | 468 | return oldfunc |
|
469 | 469 | |
|
470 | 470 | def for_type_by_name(self, type_module, type_name, func=None): |
|
471 | 471 | """Add a format function for a type specified by the full dotted |
|
472 | 472 | module and name of the type, rather than the type of the object. |
|
473 | 473 | |
|
474 | 474 | Parameters |
|
475 | 475 | ---------- |
|
476 | 476 | type_module : str |
|
477 | 477 | The full dotted name of the module the type is defined in, like |
|
478 | 478 | ``numpy``. |
|
479 | 479 | type_name : str |
|
480 | 480 | The name of the type (the class name), like ``dtype`` |
|
481 | 481 | func : callable |
|
482 | 482 | A callable for computing the format data. |
|
483 | 483 | `func` will be called with the object to be formatted, |
|
484 | 484 | and will return the raw data in this formatter's format. |
|
485 | 485 | Subclasses may use a different call signature for the |
|
486 | 486 | `func` argument. |
|
487 | 487 | |
|
488 | 488 | If `func` is None or unspecified, there will be no change, |
|
489 | 489 | only returning the current value. |
|
490 | 490 | |
|
491 | 491 | Returns |
|
492 | 492 | ------- |
|
493 | 493 | oldfunc : callable |
|
494 | 494 | The currently registered callable. |
|
495 | 495 | If you are registering a new formatter, |
|
496 | 496 | this will be the previous value (to enable restoring later). |
|
497 | 497 | """ |
|
498 | 498 | key = (type_module, type_name) |
|
499 | 499 | |
|
500 | 500 | try: |
|
501 | 501 | oldfunc = self.lookup_by_type("%s.%s" % key) |
|
502 | 502 | except KeyError: |
|
503 | 503 | oldfunc = None |
|
504 | 504 | |
|
505 | 505 | if func is not None: |
|
506 | 506 | self.deferred_printers[key] = func |
|
507 | 507 | return oldfunc |
|
508 | 508 | |
|
509 | 509 | def pop(self, typ, default=_raise_key_error): |
|
510 | 510 | """Pop a formatter for the given type. |
|
511 | 511 | |
|
512 | 512 | Parameters |
|
513 | 513 | ---------- |
|
514 | 514 | typ : type or '__module__.__name__' string for a type |
|
515 | 515 | default : object |
|
516 | 516 | value to be returned if no formatter is registered for typ. |
|
517 | 517 | |
|
518 | 518 | Returns |
|
519 | 519 | ------- |
|
520 | 520 | obj : object |
|
521 | 521 | The last registered object for the type. |
|
522 | 522 | |
|
523 | 523 | Raises |
|
524 | 524 | ------ |
|
525 | 525 | KeyError if the type is not registered and default is not specified. |
|
526 | 526 | """ |
|
527 | 527 | |
|
528 | 528 | if isinstance(typ, str): |
|
529 | 529 | typ_key = tuple(typ.rsplit('.',1)) |
|
530 | 530 | if typ_key not in self.deferred_printers: |
|
531 | 531 | # We may have it cached in the type map. We will have to |
|
532 | 532 | # iterate over all of the types to check. |
|
533 | 533 | for cls in self.type_printers: |
|
534 | 534 | if _mod_name_key(cls) == typ_key: |
|
535 | 535 | old = self.type_printers.pop(cls) |
|
536 | 536 | break |
|
537 | 537 | else: |
|
538 | 538 | old = default |
|
539 | 539 | else: |
|
540 | 540 | old = self.deferred_printers.pop(typ_key) |
|
541 | 541 | else: |
|
542 | 542 | if typ in self.type_printers: |
|
543 | 543 | old = self.type_printers.pop(typ) |
|
544 | 544 | else: |
|
545 | 545 | old = self.deferred_printers.pop(_mod_name_key(typ), default) |
|
546 | 546 | if old is _raise_key_error: |
|
547 | 547 | raise KeyError("No registered value for {0!r}".format(typ)) |
|
548 | 548 | return old |
|
549 | 549 | |
|
550 | 550 | def _in_deferred_types(self, cls): |
|
551 | 551 | """ |
|
552 | 552 | Check if the given class is specified in the deferred type registry. |
|
553 | 553 | |
|
554 | 554 | Successful matches will be moved to the regular type registry for future use. |
|
555 | 555 | """ |
|
556 | 556 | mod = getattr(cls, '__module__', None) |
|
557 | 557 | name = getattr(cls, '__name__', None) |
|
558 | 558 | key = (mod, name) |
|
559 | 559 | if key in self.deferred_printers: |
|
560 | 560 | # Move the printer over to the regular registry. |
|
561 | 561 | printer = self.deferred_printers.pop(key) |
|
562 | 562 | self.type_printers[cls] = printer |
|
563 | 563 | return True |
|
564 | 564 | return False |
|
565 | 565 | |
|
566 | 566 | |
|
567 | 567 | class PlainTextFormatter(BaseFormatter): |
|
568 | 568 | """The default pretty-printer. |
|
569 | 569 | |
|
570 | 570 | This uses :mod:`IPython.lib.pretty` to compute the format data of |
|
571 | 571 | the object. If the object cannot be pretty printed, :func:`repr` is used. |
|
572 | 572 | See the documentation of :mod:`IPython.lib.pretty` for details on |
|
573 | 573 | how to write pretty printers. Here is a simple example:: |
|
574 | 574 | |
|
575 | 575 | def dtype_pprinter(obj, p, cycle): |
|
576 | 576 | if cycle: |
|
577 | 577 | return p.text('dtype(...)') |
|
578 | 578 | if hasattr(obj, 'fields'): |
|
579 | 579 | if obj.fields is None: |
|
580 | 580 | p.text(repr(obj)) |
|
581 | 581 | else: |
|
582 | 582 | p.begin_group(7, 'dtype([') |
|
583 | 583 | for i, field in enumerate(obj.descr): |
|
584 | 584 | if i > 0: |
|
585 | 585 | p.text(',') |
|
586 | 586 | p.breakable() |
|
587 | 587 | p.pretty(field) |
|
588 | 588 | p.end_group(7, '])') |
|
589 | 589 | """ |
|
590 | 590 | |
|
591 | 591 | # The format type of data returned. |
|
592 | 592 | format_type = Unicode('text/plain') |
|
593 | 593 | |
|
594 | 594 | # This subclass ignores this attribute as it always need to return |
|
595 | 595 | # something. |
|
596 | 596 | enabled = Bool(True).tag(config=False) |
|
597 | 597 | |
|
598 | 598 | max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, |
|
599 | 599 | help="""Truncate large collections (lists, dicts, tuples, sets) to this size. |
|
600 | 600 | |
|
601 | 601 | Set to 0 to disable truncation. |
|
602 | 602 | """ |
|
603 | 603 | ).tag(config=True) |
|
604 | 604 | |
|
605 | 605 | # Look for a _repr_pretty_ methods to use for pretty printing. |
|
606 | 606 | print_method = ObjectName('_repr_pretty_') |
|
607 | 607 | |
|
608 | 608 | # Whether to pretty-print or not. |
|
609 | 609 | pprint = Bool(True).tag(config=True) |
|
610 | 610 | |
|
611 | 611 | # Whether to be verbose or not. |
|
612 | 612 | verbose = Bool(False).tag(config=True) |
|
613 | 613 | |
|
614 | 614 | # The maximum width. |
|
615 | 615 | max_width = Integer(79).tag(config=True) |
|
616 | 616 | |
|
617 | 617 | # The newline character. |
|
618 | 618 | newline = Unicode('\n').tag(config=True) |
|
619 | 619 | |
|
620 | 620 | # format-string for pprinting floats |
|
621 | 621 | float_format = Unicode('%r') |
|
622 | 622 | # setter for float precision, either int or direct format-string |
|
623 | 623 | float_precision = CUnicode('').tag(config=True) |
|
624 | 624 | |
|
625 | 625 | @observe('float_precision') |
|
626 | 626 | def _float_precision_changed(self, change): |
|
627 | 627 | """float_precision changed, set float_format accordingly. |
|
628 | 628 | |
|
629 | 629 | float_precision can be set by int or str. |
|
630 | 630 | This will set float_format, after interpreting input. |
|
631 | 631 | If numpy has been imported, numpy print precision will also be set. |
|
632 | 632 | |
|
633 | 633 | integer `n` sets format to '%.nf', otherwise, format set directly. |
|
634 | 634 | |
|
635 | 635 | An empty string returns to defaults (repr for float, 8 for numpy). |
|
636 | 636 | |
|
637 | 637 | This parameter can be set via the '%precision' magic. |
|
638 | 638 | """ |
|
639 | 639 | |
|
640 | 640 | new = change['new'] |
|
641 | 641 | if '%' in new: |
|
642 | 642 | # got explicit format string |
|
643 | 643 | fmt = new |
|
644 | 644 | try: |
|
645 | 645 | fmt%3.14159 |
|
646 | 646 | except Exception: |
|
647 | 647 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
648 | 648 | elif new: |
|
649 | 649 | # otherwise, should be an int |
|
650 | 650 | try: |
|
651 | 651 | i = int(new) |
|
652 | 652 | assert i >= 0 |
|
653 | 653 | except ValueError: |
|
654 | 654 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
655 | 655 | except AssertionError: |
|
656 | 656 | raise ValueError("int precision must be non-negative, not %r"%i) |
|
657 | 657 | |
|
658 | 658 | fmt = '%%.%if'%i |
|
659 | 659 | if 'numpy' in sys.modules: |
|
660 | 660 | # set numpy precision if it has been imported |
|
661 | 661 | import numpy |
|
662 | 662 | numpy.set_printoptions(precision=i) |
|
663 | 663 | else: |
|
664 | 664 | # default back to repr |
|
665 | 665 | fmt = '%r' |
|
666 | 666 | if 'numpy' in sys.modules: |
|
667 | 667 | import numpy |
|
668 | 668 | # numpy default is 8 |
|
669 | 669 | numpy.set_printoptions(precision=8) |
|
670 | 670 | self.float_format = fmt |
|
671 | 671 | |
|
672 | 672 | # Use the default pretty printers from IPython.lib.pretty. |
|
673 | 673 | @default('singleton_printers') |
|
674 | 674 | def _singleton_printers_default(self): |
|
675 | 675 | return pretty._singleton_pprinters.copy() |
|
676 | 676 | |
|
677 | 677 | @default('type_printers') |
|
678 | 678 | def _type_printers_default(self): |
|
679 | 679 | d = pretty._type_pprinters.copy() |
|
680 | 680 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) |
|
681 | 681 | return d |
|
682 | 682 | |
|
683 | 683 | @default('deferred_printers') |
|
684 | 684 | def _deferred_printers_default(self): |
|
685 | 685 | return pretty._deferred_type_pprinters.copy() |
|
686 | 686 | |
|
687 | 687 | #### FormatterABC interface #### |
|
688 | 688 | |
|
689 | 689 | @catch_format_error |
|
690 | 690 | def __call__(self, obj): |
|
691 | 691 | """Compute the pretty representation of the object.""" |
|
692 | 692 | if not self.pprint: |
|
693 | 693 | return repr(obj) |
|
694 | 694 | else: |
|
695 | 695 | stream = StringIO() |
|
696 | 696 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
697 | 697 | self.max_width, self.newline, |
|
698 | 698 | max_seq_length=self.max_seq_length, |
|
699 | 699 | singleton_pprinters=self.singleton_printers, |
|
700 | 700 | type_pprinters=self.type_printers, |
|
701 | 701 | deferred_pprinters=self.deferred_printers) |
|
702 | 702 | printer.pretty(obj) |
|
703 | 703 | printer.flush() |
|
704 | 704 | return stream.getvalue() |
|
705 | 705 | |
|
706 | 706 | |
|
707 | 707 | class HTMLFormatter(BaseFormatter): |
|
708 | 708 | """An HTML formatter. |
|
709 | 709 | |
|
710 | 710 | To define the callables that compute the HTML representation of your |
|
711 | 711 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` |
|
712 | 712 | or :meth:`for_type_by_name` methods to register functions that handle |
|
713 | 713 | this. |
|
714 | 714 | |
|
715 | 715 | The return value of this formatter should be a valid HTML snippet that |
|
716 | 716 | could be injected into an existing DOM. It should *not* include the |
|
717 | 717 | ```<html>`` or ```<body>`` tags. |
|
718 | 718 | """ |
|
719 | 719 | format_type = Unicode('text/html') |
|
720 | 720 | |
|
721 | 721 | print_method = ObjectName('_repr_html_') |
|
722 | 722 | |
|
723 | 723 | |
|
724 | 724 | class MarkdownFormatter(BaseFormatter): |
|
725 | 725 | """A Markdown formatter. |
|
726 | 726 | |
|
727 | 727 | To define the callables that compute the Markdown representation of your |
|
728 | 728 | objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type` |
|
729 | 729 | or :meth:`for_type_by_name` methods to register functions that handle |
|
730 | 730 | this. |
|
731 | 731 | |
|
732 | 732 | The return value of this formatter should be a valid Markdown. |
|
733 | 733 | """ |
|
734 | 734 | format_type = Unicode('text/markdown') |
|
735 | 735 | |
|
736 | 736 | print_method = ObjectName('_repr_markdown_') |
|
737 | 737 | |
|
738 | 738 | class SVGFormatter(BaseFormatter): |
|
739 | 739 | """An SVG formatter. |
|
740 | 740 | |
|
741 | 741 | To define the callables that compute the SVG representation of your |
|
742 | 742 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` |
|
743 | 743 | or :meth:`for_type_by_name` methods to register functions that handle |
|
744 | 744 | this. |
|
745 | 745 | |
|
746 | 746 | The return value of this formatter should be valid SVG enclosed in |
|
747 | 747 | ```<svg>``` tags, that could be injected into an existing DOM. It should |
|
748 | 748 | *not* include the ```<html>`` or ```<body>`` tags. |
|
749 | 749 | """ |
|
750 | 750 | format_type = Unicode('image/svg+xml') |
|
751 | 751 | |
|
752 | 752 | print_method = ObjectName('_repr_svg_') |
|
753 | 753 | |
|
754 | 754 | |
|
755 | 755 | class PNGFormatter(BaseFormatter): |
|
756 | 756 | """A PNG formatter. |
|
757 | 757 | |
|
758 | 758 | To define the callables that compute the PNG representation of your |
|
759 | 759 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` |
|
760 | 760 | or :meth:`for_type_by_name` methods to register functions that handle |
|
761 | 761 | this. |
|
762 | 762 | |
|
763 | 763 | The return value of this formatter should be raw PNG data, *not* |
|
764 | 764 | base64 encoded. |
|
765 | 765 | """ |
|
766 | 766 | format_type = Unicode('image/png') |
|
767 | 767 | |
|
768 | 768 | print_method = ObjectName('_repr_png_') |
|
769 | 769 | |
|
770 | 770 | _return_type = (bytes, str) |
|
771 | 771 | |
|
772 | 772 | |
|
773 | 773 | class JPEGFormatter(BaseFormatter): |
|
774 | 774 | """A JPEG formatter. |
|
775 | 775 | |
|
776 | 776 | To define the callables that compute the JPEG representation of your |
|
777 | 777 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` |
|
778 | 778 | or :meth:`for_type_by_name` methods to register functions that handle |
|
779 | 779 | this. |
|
780 | 780 | |
|
781 | 781 | The return value of this formatter should be raw JPEG data, *not* |
|
782 | 782 | base64 encoded. |
|
783 | 783 | """ |
|
784 | 784 | format_type = Unicode('image/jpeg') |
|
785 | 785 | |
|
786 | 786 | print_method = ObjectName('_repr_jpeg_') |
|
787 | 787 | |
|
788 | 788 | _return_type = (bytes, str) |
|
789 | 789 | |
|
790 | 790 | |
|
791 | 791 | class LatexFormatter(BaseFormatter): |
|
792 | 792 | """A LaTeX formatter. |
|
793 | 793 | |
|
794 | 794 | To define the callables that compute the LaTeX representation of your |
|
795 | 795 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` |
|
796 | 796 | or :meth:`for_type_by_name` methods to register functions that handle |
|
797 | 797 | this. |
|
798 | 798 | |
|
799 | 799 | The return value of this formatter should be a valid LaTeX equation, |
|
800 | 800 | enclosed in either ```$```, ```$$``` or another LaTeX equation |
|
801 | 801 | environment. |
|
802 | 802 | """ |
|
803 | 803 | format_type = Unicode('text/latex') |
|
804 | 804 | |
|
805 | 805 | print_method = ObjectName('_repr_latex_') |
|
806 | 806 | |
|
807 | 807 | |
|
808 | 808 | class JSONFormatter(BaseFormatter): |
|
809 | 809 | """A JSON string formatter. |
|
810 | 810 | |
|
811 | 811 | To define the callables that compute the JSONable representation of |
|
812 | 812 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` |
|
813 | 813 | or :meth:`for_type_by_name` methods to register functions that handle |
|
814 | 814 | this. |
|
815 | 815 | |
|
816 | 816 | The return value of this formatter should be a JSONable list or dict. |
|
817 | 817 | JSON scalars (None, number, string) are not allowed, only dict or list containers. |
|
818 | 818 | """ |
|
819 | 819 | format_type = Unicode('application/json') |
|
820 | 820 | _return_type = (list, dict) |
|
821 | 821 | |
|
822 | 822 | print_method = ObjectName('_repr_json_') |
|
823 | 823 | |
|
824 | 824 | def _check_return(self, r, obj): |
|
825 | 825 | """Check that a return value is appropriate |
|
826 | 826 | |
|
827 | 827 | Return the value if so, None otherwise, warning if invalid. |
|
828 | 828 | """ |
|
829 | 829 | if r is None: |
|
830 | 830 | return |
|
831 | 831 | md = None |
|
832 | 832 | if isinstance(r, tuple): |
|
833 | 833 | # unpack data, metadata tuple for type checking on first element |
|
834 | 834 | r, md = r |
|
835 | 835 | |
|
836 | 836 | # handle deprecated JSON-as-string form from IPython < 3 |
|
837 | 837 | if isinstance(r, str): |
|
838 | 838 | warnings.warn("JSON expects JSONable list/dict containers, not JSON strings", |
|
839 | 839 | FormatterWarning) |
|
840 | 840 | r = json.loads(r) |
|
841 | 841 | |
|
842 | 842 | if md is not None: |
|
843 | 843 | # put the tuple back together |
|
844 | 844 | r = (r, md) |
|
845 | 845 | return super(JSONFormatter, self)._check_return(r, obj) |
|
846 | 846 | |
|
847 | 847 | |
|
848 | 848 | class JavascriptFormatter(BaseFormatter): |
|
849 | 849 | """A Javascript formatter. |
|
850 | 850 | |
|
851 | 851 | To define the callables that compute the Javascript representation of |
|
852 | 852 | your objects, define a :meth:`_repr_javascript_` method or use the |
|
853 | 853 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions |
|
854 | 854 | that handle this. |
|
855 | 855 | |
|
856 | 856 | The return value of this formatter should be valid Javascript code and |
|
857 | 857 | should *not* be enclosed in ```<script>``` tags. |
|
858 | 858 | """ |
|
859 | 859 | format_type = Unicode('application/javascript') |
|
860 | 860 | |
|
861 | 861 | print_method = ObjectName('_repr_javascript_') |
|
862 | 862 | |
|
863 | 863 | |
|
864 | 864 | class PDFFormatter(BaseFormatter): |
|
865 | 865 | """A PDF formatter. |
|
866 | 866 | |
|
867 | 867 | To define the callables that compute the PDF representation of your |
|
868 | 868 | objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type` |
|
869 | 869 | or :meth:`for_type_by_name` methods to register functions that handle |
|
870 | 870 | this. |
|
871 | 871 | |
|
872 | 872 | The return value of this formatter should be raw PDF data, *not* |
|
873 | 873 | base64 encoded. |
|
874 | 874 | """ |
|
875 | 875 | format_type = Unicode('application/pdf') |
|
876 | 876 | |
|
877 | 877 | print_method = ObjectName('_repr_pdf_') |
|
878 | 878 | |
|
879 | 879 | _return_type = (bytes, str) |
|
880 | 880 | |
|
881 | 881 | class IPythonDisplayFormatter(BaseFormatter): |
|
882 | 882 | """An escape-hatch Formatter for objects that know how to display themselves. |
|
883 | 883 | |
|
884 | 884 | To define the callables that compute the representation of your |
|
885 | 885 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` |
|
886 | 886 | or :meth:`for_type_by_name` methods to register functions that handle |
|
887 | 887 | this. Unlike mime-type displays, this method should not return anything, |
|
888 | 888 | instead calling any appropriate display methods itself. |
|
889 | 889 | |
|
890 | 890 | This display formatter has highest priority. |
|
891 | 891 | If it fires, no other display formatter will be called. |
|
892 | 892 | |
|
893 | 893 | Prior to IPython 6.1, `_ipython_display_` was the only way to display custom mime-types |
|
894 | 894 | without registering a new Formatter. |
|
895 | 895 | |
|
896 | 896 | IPython 6.1 introduces `_repr_mimebundle_` for displaying custom mime-types, |
|
897 | 897 | so `_ipython_display_` should only be used for objects that require unusual |
|
898 | 898 | display patterns, such as multiple display calls. |
|
899 | 899 | """ |
|
900 | 900 | print_method = ObjectName('_ipython_display_') |
|
901 | 901 | _return_type = (type(None), bool) |
|
902 | 902 | |
|
903 | 903 | @catch_format_error |
|
904 | 904 | def __call__(self, obj): |
|
905 | 905 | """Compute the format for an object.""" |
|
906 | 906 | if self.enabled: |
|
907 | 907 | # lookup registered printer |
|
908 | 908 | try: |
|
909 | 909 | printer = self.lookup(obj) |
|
910 | 910 | except KeyError: |
|
911 | 911 | pass |
|
912 | 912 | else: |
|
913 | 913 | printer(obj) |
|
914 | 914 | return True |
|
915 | 915 | # Finally look for special method names |
|
916 | 916 | method = get_real_method(obj, self.print_method) |
|
917 | 917 | if method is not None: |
|
918 | 918 | method() |
|
919 | 919 | return True |
|
920 | 920 | |
|
921 | 921 | |
|
922 | 922 | class MimeBundleFormatter(BaseFormatter): |
|
923 | 923 | """A Formatter for arbitrary mime-types. |
|
924 | 924 | |
|
925 | 925 | Unlike other `_repr_<mimetype>_` methods, |
|
926 | 926 | `_repr_mimebundle_` should return mime-bundle data, |
|
927 | 927 | either the mime-keyed `data` dictionary or the tuple `(data, metadata)`. |
|
928 | 928 | Any mime-type is valid. |
|
929 | 929 | |
|
930 | 930 | To define the callables that compute the mime-bundle representation of your |
|
931 | 931 | objects, define a :meth:`_repr_mimebundle_` method or use the :meth:`for_type` |
|
932 | 932 | or :meth:`for_type_by_name` methods to register functions that handle |
|
933 | 933 | this. |
|
934 | 934 | |
|
935 | 935 | .. versionadded:: 6.1 |
|
936 | 936 | """ |
|
937 | 937 | print_method = ObjectName('_repr_mimebundle_') |
|
938 | 938 | _return_type = dict |
|
939 | 939 | |
|
940 | 940 | def _check_return(self, r, obj): |
|
941 | 941 | r = super(MimeBundleFormatter, self)._check_return(r, obj) |
|
942 | 942 | # always return (data, metadata): |
|
943 | 943 | if r is None: |
|
944 | 944 | return {}, {} |
|
945 | 945 | if not isinstance(r, tuple): |
|
946 | 946 | return r, {} |
|
947 | 947 | return r |
|
948 | 948 | |
|
949 | 949 | @catch_format_error |
|
950 | 950 | def __call__(self, obj, include=None, exclude=None): |
|
951 | 951 | """Compute the format for an object. |
|
952 | 952 | |
|
953 | 953 | Identical to parent's method but we pass extra parameters to the method. |
|
954 | 954 | |
|
955 | 955 | Unlike other _repr_*_ `_repr_mimebundle_` should allow extra kwargs, in |
|
956 | 956 | particular `include` and `exclude`. |
|
957 | 957 | """ |
|
958 | 958 | if self.enabled: |
|
959 | 959 | # lookup registered printer |
|
960 | 960 | try: |
|
961 | 961 | printer = self.lookup(obj) |
|
962 | 962 | except KeyError: |
|
963 | 963 | pass |
|
964 | 964 | else: |
|
965 | 965 | return printer(obj) |
|
966 | 966 | # Finally look for special method names |
|
967 | 967 | method = get_real_method(obj, self.print_method) |
|
968 | 968 | |
|
969 | 969 | if method is not None: |
|
970 | 970 | return method(include=include, exclude=exclude) |
|
971 | 971 | return None |
|
972 | 972 | else: |
|
973 | 973 | return None |
|
974 | 974 | |
|
975 | 975 | |
|
976 | 976 | FormatterABC.register(BaseFormatter) |
|
977 | 977 | FormatterABC.register(PlainTextFormatter) |
|
978 | 978 | FormatterABC.register(HTMLFormatter) |
|
979 | 979 | FormatterABC.register(MarkdownFormatter) |
|
980 | 980 | FormatterABC.register(SVGFormatter) |
|
981 | 981 | FormatterABC.register(PNGFormatter) |
|
982 | 982 | FormatterABC.register(PDFFormatter) |
|
983 | 983 | FormatterABC.register(JPEGFormatter) |
|
984 | 984 | FormatterABC.register(LatexFormatter) |
|
985 | 985 | FormatterABC.register(JSONFormatter) |
|
986 | 986 | FormatterABC.register(JavascriptFormatter) |
|
987 | 987 | FormatterABC.register(IPythonDisplayFormatter) |
|
988 | 988 | FormatterABC.register(MimeBundleFormatter) |
|
989 | 989 | |
|
990 | 990 | |
|
991 | 991 | def format_display_data(obj, include=None, exclude=None): |
|
992 | 992 | """Return a format data dict for an object. |
|
993 | 993 | |
|
994 | 994 | By default all format types will be computed. |
|
995 | 995 | |
|
996 | 996 | Parameters |
|
997 | 997 | ---------- |
|
998 | 998 | obj : object |
|
999 | 999 | The Python object whose format data will be computed. |
|
1000 | 1000 | |
|
1001 | 1001 | Returns |
|
1002 | 1002 | ------- |
|
1003 | 1003 | format_dict : dict |
|
1004 | 1004 | A dictionary of key/value pairs, one or each format that was |
|
1005 | 1005 | generated for the object. The keys are the format types, which |
|
1006 | 1006 | will usually be MIME type strings and the values and JSON'able |
|
1007 | 1007 | data structure containing the raw data for the representation in |
|
1008 | 1008 | that format. |
|
1009 | 1009 | include : list or tuple, optional |
|
1010 | 1010 | A list of format type strings (MIME types) to include in the |
|
1011 | 1011 | format data dict. If this is set *only* the format types included |
|
1012 | 1012 | in this list will be computed. |
|
1013 | 1013 | exclude : list or tuple, optional |
|
1014 | 1014 | A list of format type string (MIME types) to exclude in the format |
|
1015 | 1015 | data dict. If this is set all format types will be computed, |
|
1016 | 1016 | except for those included in this argument. |
|
1017 | 1017 | """ |
|
1018 |
from |
|
|
1018 | from .interactiveshell import InteractiveShell | |
|
1019 | 1019 | |
|
1020 | 1020 | return InteractiveShell.instance().display_formatter.format( |
|
1021 | 1021 | obj, |
|
1022 | 1022 | include, |
|
1023 | 1023 | exclude |
|
1024 | 1024 | ) |
@@ -1,161 +1,161 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | An application for managing IPython history. |
|
4 | 4 | |
|
5 | 5 | To be invoked as the `ipython history` subcommand. |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | import os |
|
9 | 9 | import sqlite3 |
|
10 | 10 | |
|
11 | 11 | from traitlets.config.application import Application |
|
12 |
from |
|
|
12 | from .application import BaseIPythonApplication | |
|
13 | 13 | from traitlets import Bool, Int, Dict |
|
14 |
from |
|
|
14 | from ..utils.io import ask_yes_no | |
|
15 | 15 | |
|
16 | 16 | trim_hist_help = """Trim the IPython history database to the last 1000 entries. |
|
17 | 17 | |
|
18 | 18 | This actually copies the last 1000 entries to a new database, and then replaces |
|
19 | 19 | the old file with the new. Use the `--keep=` argument to specify a number |
|
20 | 20 | other than 1000. |
|
21 | 21 | """ |
|
22 | 22 | |
|
23 | 23 | clear_hist_help = """Clear the IPython history database, deleting all entries. |
|
24 | 24 | |
|
25 | 25 | Because this is a destructive operation, IPython will prompt the user if they |
|
26 | 26 | really want to do this. Passing a `-f` flag will force clearing without a |
|
27 | 27 | prompt. |
|
28 | 28 | |
|
29 | 29 | This is an handy alias to `ipython history trim --keep=0` |
|
30 | 30 | """ |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | class HistoryTrim(BaseIPythonApplication): |
|
34 | 34 | description = trim_hist_help |
|
35 | 35 | |
|
36 | 36 | backup = Bool(False, |
|
37 | 37 | help="Keep the old history file as history.sqlite.<N>" |
|
38 | 38 | ).tag(config=True) |
|
39 | 39 | |
|
40 | 40 | keep = Int(1000, |
|
41 | 41 | help="Number of recent lines to keep in the database." |
|
42 | 42 | ).tag(config=True) |
|
43 | 43 | |
|
44 | 44 | flags = Dict(dict( |
|
45 | 45 | backup = ({'HistoryTrim' : {'backup' : True}}, |
|
46 | 46 | backup.help |
|
47 | 47 | ) |
|
48 | 48 | )) |
|
49 | 49 | |
|
50 | 50 | aliases=Dict(dict( |
|
51 | 51 | keep = 'HistoryTrim.keep' |
|
52 | 52 | )) |
|
53 | 53 | |
|
54 | 54 | def start(self): |
|
55 | 55 | profile_dir = self.profile_dir.location |
|
56 | 56 | hist_file = os.path.join(profile_dir, 'history.sqlite') |
|
57 | 57 | con = sqlite3.connect(hist_file) |
|
58 | 58 | |
|
59 | 59 | # Grab the recent history from the current database. |
|
60 | 60 | inputs = list(con.execute('SELECT session, line, source, source_raw FROM ' |
|
61 | 61 | 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,))) |
|
62 | 62 | if len(inputs) <= self.keep: |
|
63 | 63 | print("There are already at most %d entries in the history database." % self.keep) |
|
64 | 64 | print("Not doing anything. Use --keep= argument to keep fewer entries") |
|
65 | 65 | return |
|
66 | 66 | |
|
67 | 67 | print("Trimming history to the most recent %d entries." % self.keep) |
|
68 | 68 | |
|
69 | 69 | inputs.pop() # Remove the extra element we got to check the length. |
|
70 | 70 | inputs.reverse() |
|
71 | 71 | if inputs: |
|
72 | 72 | first_session = inputs[0][0] |
|
73 | 73 | outputs = list(con.execute('SELECT session, line, output FROM ' |
|
74 | 74 | 'output_history WHERE session >= ?', (first_session,))) |
|
75 | 75 | sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM ' |
|
76 | 76 | 'sessions WHERE session >= ?', (first_session,))) |
|
77 | 77 | con.close() |
|
78 | 78 | |
|
79 | 79 | # Create the new history database. |
|
80 | 80 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new') |
|
81 | 81 | i = 0 |
|
82 | 82 | while os.path.exists(new_hist_file): |
|
83 | 83 | # Make sure we don't interfere with an existing file. |
|
84 | 84 | i += 1 |
|
85 | 85 | new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i)) |
|
86 | 86 | new_db = sqlite3.connect(new_hist_file) |
|
87 | 87 | new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
88 | 88 | primary key autoincrement, start timestamp, |
|
89 | 89 | end timestamp, num_cmds integer, remark text)""") |
|
90 | 90 | new_db.execute("""CREATE TABLE IF NOT EXISTS history |
|
91 | 91 | (session integer, line integer, source text, source_raw text, |
|
92 | 92 | PRIMARY KEY (session, line))""") |
|
93 | 93 | new_db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
94 | 94 | (session integer, line integer, output text, |
|
95 | 95 | PRIMARY KEY (session, line))""") |
|
96 | 96 | new_db.commit() |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | if inputs: |
|
100 | 100 | with new_db: |
|
101 | 101 | # Add the recent history into the new database. |
|
102 | 102 | new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions) |
|
103 | 103 | new_db.executemany('insert into history values (?,?,?,?)', inputs) |
|
104 | 104 | new_db.executemany('insert into output_history values (?,?,?)', outputs) |
|
105 | 105 | new_db.close() |
|
106 | 106 | |
|
107 | 107 | if self.backup: |
|
108 | 108 | i = 1 |
|
109 | 109 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) |
|
110 | 110 | while os.path.exists(backup_hist_file): |
|
111 | 111 | i += 1 |
|
112 | 112 | backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i) |
|
113 | 113 | os.rename(hist_file, backup_hist_file) |
|
114 | 114 | print("Backed up longer history file to", backup_hist_file) |
|
115 | 115 | else: |
|
116 | 116 | os.remove(hist_file) |
|
117 | 117 | |
|
118 | 118 | os.rename(new_hist_file, hist_file) |
|
119 | 119 | |
|
120 | 120 | class HistoryClear(HistoryTrim): |
|
121 | 121 | description = clear_hist_help |
|
122 | 122 | keep = Int(0, |
|
123 | 123 | help="Number of recent lines to keep in the database.") |
|
124 | 124 | |
|
125 | 125 | force = Bool(False, |
|
126 | 126 | help="Don't prompt user for confirmation" |
|
127 | 127 | ).tag(config=True) |
|
128 | 128 | |
|
129 | 129 | flags = Dict(dict( |
|
130 | 130 | force = ({'HistoryClear' : {'force' : True}}, |
|
131 | 131 | force.help), |
|
132 | 132 | f = ({'HistoryTrim' : {'force' : True}}, |
|
133 | 133 | force.help |
|
134 | 134 | ) |
|
135 | 135 | )) |
|
136 | 136 | aliases = Dict() |
|
137 | 137 | |
|
138 | 138 | def start(self): |
|
139 | 139 | if self.force or ask_yes_no("Really delete all ipython history? ", |
|
140 | 140 | default="no", interrupt="no"): |
|
141 | 141 | HistoryTrim.start(self) |
|
142 | 142 | |
|
143 | 143 | class HistoryApp(Application): |
|
144 | 144 | name = u'ipython-history' |
|
145 | 145 | description = "Manage the IPython history database." |
|
146 | 146 | |
|
147 | 147 | subcommands = Dict(dict( |
|
148 | 148 | trim = (HistoryTrim, HistoryTrim.description.splitlines()[0]), |
|
149 | 149 | clear = (HistoryClear, HistoryClear.description.splitlines()[0]), |
|
150 | 150 | )) |
|
151 | 151 | |
|
152 | 152 | def start(self): |
|
153 | 153 | if self.subapp is None: |
|
154 | 154 | print("No subcommand specified. Must specify one of: %s" % \ |
|
155 | 155 | (self.subcommands.keys())) |
|
156 | 156 | print() |
|
157 | 157 | self.print_description() |
|
158 | 158 | self.print_subcommands() |
|
159 | 159 | self.exit(1) |
|
160 | 160 | else: |
|
161 | 161 | return self.subapp.start() |
@@ -1,229 +1,229 b'' | |||
|
1 | 1 | """Hooks for IPython. |
|
2 | 2 | |
|
3 | 3 | In Python, it is possible to overwrite any method of any object if you really |
|
4 | 4 | want to. But IPython exposes a few 'hooks', methods which are *designed* to |
|
5 | 5 | be overwritten by users for customization purposes. This module defines the |
|
6 | 6 | default versions of all such hooks, which get used by IPython if not |
|
7 | 7 | overridden by the user. |
|
8 | 8 | |
|
9 | 9 | Hooks are simple functions, but they should be declared with ``self`` as their |
|
10 | 10 | first argument, because when activated they are registered into IPython as |
|
11 | 11 | instance methods. The self argument will be the IPython running instance |
|
12 | 12 | itself, so hooks have full access to the entire IPython object. |
|
13 | 13 | |
|
14 | 14 | If you wish to define a new hook and activate it, you can make an :doc:`extension |
|
15 | 15 | </config/extensions/index>` or a :ref:`startup script <startup_files>`. For |
|
16 | 16 | example, you could use a startup file like this:: |
|
17 | 17 | |
|
18 | 18 | import os |
|
19 | 19 | |
|
20 | 20 | def calljed(self,filename, linenum): |
|
21 | 21 | "My editor hook calls the jed editor directly." |
|
22 | 22 | print "Calling my own editor, jed ..." |
|
23 | 23 | if os.system('jed +%d %s' % (linenum,filename)) != 0: |
|
24 | 24 | raise TryNext() |
|
25 | 25 | |
|
26 | 26 | def load_ipython_extension(ip): |
|
27 | 27 | ip.set_hook('editor', calljed) |
|
28 | 28 | |
|
29 | 29 | """ |
|
30 | 30 | |
|
31 | 31 | #***************************************************************************** |
|
32 | 32 | # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu> |
|
33 | 33 | # |
|
34 | 34 | # Distributed under the terms of the BSD License. The full license is in |
|
35 | 35 | # the file COPYING, distributed as part of this software. |
|
36 | 36 | #***************************************************************************** |
|
37 | 37 | |
|
38 | 38 | import os |
|
39 | 39 | import subprocess |
|
40 | 40 | import warnings |
|
41 | 41 | import sys |
|
42 | 42 | |
|
43 |
from |
|
|
43 | from .error import TryNext | |
|
44 | 44 | |
|
45 | 45 | # List here all the default hooks. For now it's just the editor functions |
|
46 | 46 | # but over time we'll move here all the public API for user-accessible things. |
|
47 | 47 | |
|
48 | 48 | __all__ = ['editor', 'synchronize_with_editor', |
|
49 | 49 | 'shutdown_hook', 'late_startup_hook', |
|
50 | 50 | 'show_in_pager','pre_prompt_hook', |
|
51 | 51 | 'pre_run_code_hook', 'clipboard_get'] |
|
52 | 52 | |
|
53 | 53 | deprecated = {'pre_run_code_hook': "a callback for the 'pre_execute' or 'pre_run_cell' event", |
|
54 | 54 | 'late_startup_hook': "a callback for the 'shell_initialized' event", |
|
55 | 55 | 'shutdown_hook': "the atexit module", |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | def editor(self, filename, linenum=None, wait=True): |
|
59 | 59 | """Open the default editor at the given filename and linenumber. |
|
60 | 60 | |
|
61 | 61 | This is IPython's default editor hook, you can use it as an example to |
|
62 | 62 | write your own modified one. To set your own editor function as the |
|
63 | 63 | new editor hook, call ip.set_hook('editor',yourfunc).""" |
|
64 | 64 | |
|
65 | 65 | # IPython configures a default editor at startup by reading $EDITOR from |
|
66 | 66 | # the environment, and falling back on vi (unix) or notepad (win32). |
|
67 | 67 | editor = self.editor |
|
68 | 68 | |
|
69 | 69 | # marker for at which line to open the file (for existing objects) |
|
70 | 70 | if linenum is None or editor=='notepad': |
|
71 | 71 | linemark = '' |
|
72 | 72 | else: |
|
73 | 73 | linemark = '+%d' % int(linenum) |
|
74 | 74 | |
|
75 | 75 | # Enclose in quotes if necessary and legal |
|
76 | 76 | if ' ' in editor and os.path.isfile(editor) and editor[0] != '"': |
|
77 | 77 | editor = '"%s"' % editor |
|
78 | 78 | |
|
79 | 79 | # Call the actual editor |
|
80 | 80 | proc = subprocess.Popen('%s %s %s' % (editor, linemark, filename), |
|
81 | 81 | shell=True) |
|
82 | 82 | if wait and proc.wait() != 0: |
|
83 | 83 | raise TryNext() |
|
84 | 84 | |
|
85 | 85 | import tempfile |
|
86 |
from |
|
|
86 | from ..utils.decorators import undoc | |
|
87 | 87 | |
|
88 | 88 | @undoc |
|
89 | 89 | def fix_error_editor(self,filename,linenum,column,msg): |
|
90 | 90 | """DEPRECATED |
|
91 | 91 | |
|
92 | 92 | Open the editor at the given filename, linenumber, column and |
|
93 | 93 | show an error message. This is used for correcting syntax errors. |
|
94 | 94 | The current implementation only has special support for the VIM editor, |
|
95 | 95 | and falls back on the 'editor' hook if VIM is not used. |
|
96 | 96 | |
|
97 | 97 | Call ip.set_hook('fix_error_editor',yourfunc) to use your own function, |
|
98 | 98 | """ |
|
99 | 99 | |
|
100 | 100 | warnings.warn(""" |
|
101 | 101 | `fix_error_editor` is deprecated as of IPython 6.0 and will be removed |
|
102 | 102 | in future versions. It appears to be used only for automatically fixing syntax |
|
103 | 103 | error that has been broken for a few years and has thus been removed. If you |
|
104 | 104 | happened to use this function and still need it please make your voice heard on |
|
105 | 105 | the mailing list ipython-dev@python.org , or on the GitHub Issue tracker: |
|
106 | 106 | https://github.com/ipython/ipython/issues/9649 """, UserWarning) |
|
107 | 107 | |
|
108 | 108 | def vim_quickfix_file(): |
|
109 | 109 | t = tempfile.NamedTemporaryFile() |
|
110 | 110 | t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg)) |
|
111 | 111 | t.flush() |
|
112 | 112 | return t |
|
113 | 113 | if os.path.basename(self.editor) != 'vim': |
|
114 | 114 | self.hooks.editor(filename,linenum) |
|
115 | 115 | return |
|
116 | 116 | t = vim_quickfix_file() |
|
117 | 117 | try: |
|
118 | 118 | if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name): |
|
119 | 119 | raise TryNext() |
|
120 | 120 | finally: |
|
121 | 121 | t.close() |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def synchronize_with_editor(self, filename, linenum, column): |
|
125 | 125 | pass |
|
126 | 126 | |
|
127 | 127 | |
|
128 | 128 | class CommandChainDispatcher: |
|
129 | 129 | """ Dispatch calls to a chain of commands until some func can handle it |
|
130 | 130 | |
|
131 | 131 | Usage: instantiate, execute "add" to add commands (with optional |
|
132 | 132 | priority), execute normally via f() calling mechanism. |
|
133 | 133 | |
|
134 | 134 | """ |
|
135 | 135 | def __init__(self,commands=None): |
|
136 | 136 | if commands is None: |
|
137 | 137 | self.chain = [] |
|
138 | 138 | else: |
|
139 | 139 | self.chain = commands |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | def __call__(self,*args, **kw): |
|
143 | 143 | """ Command chain is called just like normal func. |
|
144 | 144 | |
|
145 | 145 | This will call all funcs in chain with the same args as were given to |
|
146 | 146 | this function, and return the result of first func that didn't raise |
|
147 | 147 | TryNext""" |
|
148 | 148 | last_exc = TryNext() |
|
149 | 149 | for prio,cmd in self.chain: |
|
150 | 150 | #print "prio",prio,"cmd",cmd #dbg |
|
151 | 151 | try: |
|
152 | 152 | return cmd(*args, **kw) |
|
153 | 153 | except TryNext as exc: |
|
154 | 154 | last_exc = exc |
|
155 | 155 | # if no function will accept it, raise TryNext up to the caller |
|
156 | 156 | raise last_exc |
|
157 | 157 | |
|
158 | 158 | def __str__(self): |
|
159 | 159 | return str(self.chain) |
|
160 | 160 | |
|
161 | 161 | def add(self, func, priority=0): |
|
162 | 162 | """ Add a func to the cmd chain with given priority """ |
|
163 | 163 | self.chain.append((priority, func)) |
|
164 | 164 | self.chain.sort(key=lambda x: x[0]) |
|
165 | 165 | |
|
166 | 166 | def __iter__(self): |
|
167 | 167 | """ Return all objects in chain. |
|
168 | 168 | |
|
169 | 169 | Handy if the objects are not callable. |
|
170 | 170 | """ |
|
171 | 171 | return iter(self.chain) |
|
172 | 172 | |
|
173 | 173 | |
|
174 | 174 | def shutdown_hook(self): |
|
175 | 175 | """ default shutdown hook |
|
176 | 176 | |
|
177 | 177 | Typically, shutdown hooks should raise TryNext so all shutdown ops are done |
|
178 | 178 | """ |
|
179 | 179 | |
|
180 | 180 | #print "default shutdown hook ok" # dbg |
|
181 | 181 | return |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | def late_startup_hook(self): |
|
185 | 185 | """ Executed after ipython has been constructed and configured |
|
186 | 186 | |
|
187 | 187 | """ |
|
188 | 188 | #print "default startup hook ok" # dbg |
|
189 | 189 | |
|
190 | 190 | |
|
191 | 191 | def show_in_pager(self, data, start, screen_lines): |
|
192 | 192 | """ Run a string through pager """ |
|
193 | 193 | # raising TryNext here will use the default paging functionality |
|
194 | 194 | raise TryNext |
|
195 | 195 | |
|
196 | 196 | |
|
197 | 197 | def pre_prompt_hook(self): |
|
198 | 198 | """ Run before displaying the next prompt |
|
199 | 199 | |
|
200 | 200 | Use this e.g. to display output from asynchronous operations (in order |
|
201 | 201 | to not mess up text entry) |
|
202 | 202 | """ |
|
203 | 203 | |
|
204 | 204 | return None |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | def pre_run_code_hook(self): |
|
208 | 208 | """ Executed before running the (prefiltered) code in IPython """ |
|
209 | 209 | return None |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def clipboard_get(self): |
|
213 | 213 | """ Get text from the clipboard. |
|
214 | 214 | """ |
|
215 |
from |
|
|
215 | from ..lib.clipboard import ( | |
|
216 | 216 | osx_clipboard_get, tkinter_clipboard_get, |
|
217 | 217 | win32_clipboard_get |
|
218 | 218 | ) |
|
219 | 219 | if sys.platform == 'win32': |
|
220 | 220 | chain = [win32_clipboard_get, tkinter_clipboard_get] |
|
221 | 221 | elif sys.platform == 'darwin': |
|
222 | 222 | chain = [osx_clipboard_get, tkinter_clipboard_get] |
|
223 | 223 | else: |
|
224 | 224 | chain = [tkinter_clipboard_get] |
|
225 | 225 | dispatcher = CommandChainDispatcher() |
|
226 | 226 | for func in chain: |
|
227 | 227 | dispatcher.add(func) |
|
228 | 228 | text = dispatcher() |
|
229 | 229 | return text |
@@ -1,703 +1,703 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
7 | 7 | # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu> |
|
8 | 8 | # Copyright (C) 2008 The IPython Development Team |
|
9 | 9 | |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | import os |
|
15 | 15 | import re |
|
16 | 16 | import sys |
|
17 | 17 | from getopt import getopt, GetoptError |
|
18 | 18 | |
|
19 | 19 | from traitlets.config.configurable import Configurable |
|
20 |
from |
|
|
21 |
from |
|
|
22 |
from |
|
|
20 | from . import oinspect | |
|
21 | from .error import UsageError | |
|
22 | from .inputtransformer2 import ESC_MAGIC, ESC_MAGIC2 | |
|
23 | 23 | from decorator import decorator |
|
24 |
from |
|
|
25 |
from |
|
|
26 |
from |
|
|
24 | from ..utils.ipstruct import Struct | |
|
25 | from ..utils.process import arg_split | |
|
26 | from ..utils.text import dedent | |
|
27 | 27 | from traitlets import Bool, Dict, Instance, observe |
|
28 | 28 | from logging import error |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | # Globals |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | |
|
34 | 34 | # A dict we'll use for each class that has magics, used as temporary storage to |
|
35 | 35 | # pass information between the @line/cell_magic method decorators and the |
|
36 | 36 | # @magics_class class decorator, because the method decorators have no |
|
37 | 37 | # access to the class when they run. See for more details: |
|
38 | 38 | # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class |
|
39 | 39 | |
|
40 | 40 | magics = dict(line={}, cell={}) |
|
41 | 41 | |
|
42 | 42 | magic_kinds = ('line', 'cell') |
|
43 | 43 | magic_spec = ('line', 'cell', 'line_cell') |
|
44 | 44 | magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2) |
|
45 | 45 | |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | # Utility classes and functions |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | class Bunch: pass |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def on_off(tag): |
|
54 | 54 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
55 | 55 | return ['OFF','ON'][tag] |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def compress_dhist(dh): |
|
59 | 59 | """Compress a directory history into a new one with at most 20 entries. |
|
60 | 60 | |
|
61 | 61 | Return a new list made from the first and last 10 elements of dhist after |
|
62 | 62 | removal of duplicates. |
|
63 | 63 | """ |
|
64 | 64 | head, tail = dh[:-10], dh[-10:] |
|
65 | 65 | |
|
66 | 66 | newhead = [] |
|
67 | 67 | done = set() |
|
68 | 68 | for h in head: |
|
69 | 69 | if h in done: |
|
70 | 70 | continue |
|
71 | 71 | newhead.append(h) |
|
72 | 72 | done.add(h) |
|
73 | 73 | |
|
74 | 74 | return newhead + tail |
|
75 | 75 | |
|
76 | 76 | |
|
77 | 77 | def needs_local_scope(func): |
|
78 | 78 | """Decorator to mark magic functions which need to local scope to run.""" |
|
79 | 79 | func.needs_local_scope = True |
|
80 | 80 | return func |
|
81 | 81 | |
|
82 | 82 | #----------------------------------------------------------------------------- |
|
83 | 83 | # Class and method decorators for registering magics |
|
84 | 84 | #----------------------------------------------------------------------------- |
|
85 | 85 | |
|
86 | 86 | def magics_class(cls): |
|
87 | 87 | """Class decorator for all subclasses of the main Magics class. |
|
88 | 88 | |
|
89 | 89 | Any class that subclasses Magics *must* also apply this decorator, to |
|
90 | 90 | ensure that all the methods that have been decorated as line/cell magics |
|
91 | 91 | get correctly registered in the class instance. This is necessary because |
|
92 | 92 | when method decorators run, the class does not exist yet, so they |
|
93 | 93 | temporarily store their information into a module global. Application of |
|
94 | 94 | this class decorator copies that global data to the class instance and |
|
95 | 95 | clears the global. |
|
96 | 96 | |
|
97 | 97 | Obviously, this mechanism is not thread-safe, which means that the |
|
98 | 98 | *creation* of subclasses of Magic should only be done in a single-thread |
|
99 | 99 | context. Instantiation of the classes has no restrictions. Given that |
|
100 | 100 | these classes are typically created at IPython startup time and before user |
|
101 | 101 | application code becomes active, in practice this should not pose any |
|
102 | 102 | problems. |
|
103 | 103 | """ |
|
104 | 104 | cls.registered = True |
|
105 | 105 | cls.magics = dict(line = magics['line'], |
|
106 | 106 | cell = magics['cell']) |
|
107 | 107 | magics['line'] = {} |
|
108 | 108 | magics['cell'] = {} |
|
109 | 109 | return cls |
|
110 | 110 | |
|
111 | 111 | |
|
112 | 112 | def record_magic(dct, magic_kind, magic_name, func): |
|
113 | 113 | """Utility function to store a function as a magic of a specific kind. |
|
114 | 114 | |
|
115 | 115 | Parameters |
|
116 | 116 | ---------- |
|
117 | 117 | dct : dict |
|
118 | 118 | A dictionary with 'line' and 'cell' subdicts. |
|
119 | 119 | |
|
120 | 120 | magic_kind : str |
|
121 | 121 | Kind of magic to be stored. |
|
122 | 122 | |
|
123 | 123 | magic_name : str |
|
124 | 124 | Key to store the magic as. |
|
125 | 125 | |
|
126 | 126 | func : function |
|
127 | 127 | Callable object to store. |
|
128 | 128 | """ |
|
129 | 129 | if magic_kind == 'line_cell': |
|
130 | 130 | dct['line'][magic_name] = dct['cell'][magic_name] = func |
|
131 | 131 | else: |
|
132 | 132 | dct[magic_kind][magic_name] = func |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | def validate_type(magic_kind): |
|
136 | 136 | """Ensure that the given magic_kind is valid. |
|
137 | 137 | |
|
138 | 138 | Check that the given magic_kind is one of the accepted spec types (stored |
|
139 | 139 | in the global `magic_spec`), raise ValueError otherwise. |
|
140 | 140 | """ |
|
141 | 141 | if magic_kind not in magic_spec: |
|
142 | 142 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
143 | 143 | magic_kinds, magic_kind) |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | # The docstrings for the decorator below will be fairly similar for the two |
|
147 | 147 | # types (method and function), so we generate them here once and reuse the |
|
148 | 148 | # templates below. |
|
149 | 149 | _docstring_template = \ |
|
150 | 150 | """Decorate the given {0} as {1} magic. |
|
151 | 151 | |
|
152 | 152 | The decorator can be used with or without arguments, as follows. |
|
153 | 153 | |
|
154 | 154 | i) without arguments: it will create a {1} magic named as the {0} being |
|
155 | 155 | decorated:: |
|
156 | 156 | |
|
157 | 157 | @deco |
|
158 | 158 | def foo(...) |
|
159 | 159 | |
|
160 | 160 | will create a {1} magic named `foo`. |
|
161 | 161 | |
|
162 | 162 | ii) with one string argument: which will be used as the actual name of the |
|
163 | 163 | resulting magic:: |
|
164 | 164 | |
|
165 | 165 | @deco('bar') |
|
166 | 166 | def foo(...) |
|
167 | 167 | |
|
168 | 168 | will create a {1} magic named `bar`. |
|
169 | 169 | |
|
170 | 170 | To register a class magic use ``Interactiveshell.register_magic(class or instance)``. |
|
171 | 171 | """ |
|
172 | 172 | |
|
173 | 173 | # These two are decorator factories. While they are conceptually very similar, |
|
174 | 174 | # there are enough differences in the details that it's simpler to have them |
|
175 | 175 | # written as completely standalone functions rather than trying to share code |
|
176 | 176 | # and make a single one with convoluted logic. |
|
177 | 177 | |
|
178 | 178 | def _method_magic_marker(magic_kind): |
|
179 | 179 | """Decorator factory for methods in Magics subclasses. |
|
180 | 180 | """ |
|
181 | 181 | |
|
182 | 182 | validate_type(magic_kind) |
|
183 | 183 | |
|
184 | 184 | # This is a closure to capture the magic_kind. We could also use a class, |
|
185 | 185 | # but it's overkill for just that one bit of state. |
|
186 | 186 | def magic_deco(arg): |
|
187 | 187 | call = lambda f, *a, **k: f(*a, **k) |
|
188 | 188 | |
|
189 | 189 | if callable(arg): |
|
190 | 190 | # "Naked" decorator call (just @foo, no args) |
|
191 | 191 | func = arg |
|
192 | 192 | name = func.__name__ |
|
193 | 193 | retval = decorator(call, func) |
|
194 | 194 | record_magic(magics, magic_kind, name, name) |
|
195 | 195 | elif isinstance(arg, str): |
|
196 | 196 | # Decorator called with arguments (@foo('bar')) |
|
197 | 197 | name = arg |
|
198 | 198 | def mark(func, *a, **kw): |
|
199 | 199 | record_magic(magics, magic_kind, name, func.__name__) |
|
200 | 200 | return decorator(call, func) |
|
201 | 201 | retval = mark |
|
202 | 202 | else: |
|
203 | 203 | raise TypeError("Decorator can only be called with " |
|
204 | 204 | "string or function") |
|
205 | 205 | return retval |
|
206 | 206 | |
|
207 | 207 | # Ensure the resulting decorator has a usable docstring |
|
208 | 208 | magic_deco.__doc__ = _docstring_template.format('method', magic_kind) |
|
209 | 209 | return magic_deco |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def _function_magic_marker(magic_kind): |
|
213 | 213 | """Decorator factory for standalone functions. |
|
214 | 214 | """ |
|
215 | 215 | validate_type(magic_kind) |
|
216 | 216 | |
|
217 | 217 | # This is a closure to capture the magic_kind. We could also use a class, |
|
218 | 218 | # but it's overkill for just that one bit of state. |
|
219 | 219 | def magic_deco(arg): |
|
220 | 220 | call = lambda f, *a, **k: f(*a, **k) |
|
221 | 221 | |
|
222 | 222 | # Find get_ipython() in the caller's namespace |
|
223 | 223 | caller = sys._getframe(1) |
|
224 | 224 | for ns in ['f_locals', 'f_globals', 'f_builtins']: |
|
225 | 225 | get_ipython = getattr(caller, ns).get('get_ipython') |
|
226 | 226 | if get_ipython is not None: |
|
227 | 227 | break |
|
228 | 228 | else: |
|
229 | 229 | raise NameError('Decorator can only run in context where ' |
|
230 | 230 | '`get_ipython` exists') |
|
231 | 231 | |
|
232 | 232 | ip = get_ipython() |
|
233 | 233 | |
|
234 | 234 | if callable(arg): |
|
235 | 235 | # "Naked" decorator call (just @foo, no args) |
|
236 | 236 | func = arg |
|
237 | 237 | name = func.__name__ |
|
238 | 238 | ip.register_magic_function(func, magic_kind, name) |
|
239 | 239 | retval = decorator(call, func) |
|
240 | 240 | elif isinstance(arg, str): |
|
241 | 241 | # Decorator called with arguments (@foo('bar')) |
|
242 | 242 | name = arg |
|
243 | 243 | def mark(func, *a, **kw): |
|
244 | 244 | ip.register_magic_function(func, magic_kind, name) |
|
245 | 245 | return decorator(call, func) |
|
246 | 246 | retval = mark |
|
247 | 247 | else: |
|
248 | 248 | raise TypeError("Decorator can only be called with " |
|
249 | 249 | "string or function") |
|
250 | 250 | return retval |
|
251 | 251 | |
|
252 | 252 | # Ensure the resulting decorator has a usable docstring |
|
253 | 253 | ds = _docstring_template.format('function', magic_kind) |
|
254 | 254 | |
|
255 | 255 | ds += dedent(""" |
|
256 | 256 | Note: this decorator can only be used in a context where IPython is already |
|
257 | 257 | active, so that the `get_ipython()` call succeeds. You can therefore use |
|
258 | 258 | it in your startup files loaded after IPython initializes, but *not* in the |
|
259 | 259 | IPython configuration file itself, which is executed before IPython is |
|
260 | 260 | fully up and running. Any file located in the `startup` subdirectory of |
|
261 | 261 | your configuration profile will be OK in this sense. |
|
262 | 262 | """) |
|
263 | 263 | |
|
264 | 264 | magic_deco.__doc__ = ds |
|
265 | 265 | return magic_deco |
|
266 | 266 | |
|
267 | 267 | |
|
268 | 268 | MAGIC_NO_VAR_EXPAND_ATTR = '_ipython_magic_no_var_expand' |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | def no_var_expand(magic_func): |
|
272 | 272 | """Mark a magic function as not needing variable expansion |
|
273 | 273 | |
|
274 | 274 | By default, IPython interprets `{a}` or `$a` in the line passed to magics |
|
275 | 275 | as variables that should be interpolated from the interactive namespace |
|
276 | 276 | before passing the line to the magic function. |
|
277 | 277 | This is not always desirable, e.g. when the magic executes Python code |
|
278 | 278 | (%timeit, %time, etc.). |
|
279 | 279 | Decorate magics with `@no_var_expand` to opt-out of variable expansion. |
|
280 | 280 | |
|
281 | 281 | .. versionadded:: 7.3 |
|
282 | 282 | """ |
|
283 | 283 | setattr(magic_func, MAGIC_NO_VAR_EXPAND_ATTR, True) |
|
284 | 284 | return magic_func |
|
285 | 285 | |
|
286 | 286 | |
|
287 | 287 | # Create the actual decorators for public use |
|
288 | 288 | |
|
289 | 289 | # These three are used to decorate methods in class definitions |
|
290 | 290 | line_magic = _method_magic_marker('line') |
|
291 | 291 | cell_magic = _method_magic_marker('cell') |
|
292 | 292 | line_cell_magic = _method_magic_marker('line_cell') |
|
293 | 293 | |
|
294 | 294 | # These three decorate standalone functions and perform the decoration |
|
295 | 295 | # immediately. They can only run where get_ipython() works |
|
296 | 296 | register_line_magic = _function_magic_marker('line') |
|
297 | 297 | register_cell_magic = _function_magic_marker('cell') |
|
298 | 298 | register_line_cell_magic = _function_magic_marker('line_cell') |
|
299 | 299 | |
|
300 | 300 | #----------------------------------------------------------------------------- |
|
301 | 301 | # Core Magic classes |
|
302 | 302 | #----------------------------------------------------------------------------- |
|
303 | 303 | |
|
304 | 304 | class MagicsManager(Configurable): |
|
305 | 305 | """Object that handles all magic-related functionality for IPython. |
|
306 | 306 | """ |
|
307 | 307 | # Non-configurable class attributes |
|
308 | 308 | |
|
309 | 309 | # A two-level dict, first keyed by magic type, then by magic function, and |
|
310 | 310 | # holding the actual callable object as value. This is the dict used for |
|
311 | 311 | # magic function dispatch |
|
312 | 312 | magics = Dict() |
|
313 | 313 | |
|
314 | 314 | # A registry of the original objects that we've been given holding magics. |
|
315 | 315 | registry = Dict() |
|
316 | 316 | |
|
317 | 317 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
318 | 318 | |
|
319 | 319 | auto_magic = Bool(True, help= |
|
320 | 320 | "Automatically call line magics without requiring explicit % prefix" |
|
321 | 321 | ).tag(config=True) |
|
322 | 322 | @observe('auto_magic') |
|
323 | 323 | def _auto_magic_changed(self, change): |
|
324 | 324 | self.shell.automagic = change['new'] |
|
325 | 325 | |
|
326 | 326 | _auto_status = [ |
|
327 | 327 | 'Automagic is OFF, % prefix IS needed for line magics.', |
|
328 | 328 | 'Automagic is ON, % prefix IS NOT needed for line magics.'] |
|
329 | 329 | |
|
330 | 330 | user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True) |
|
331 | 331 | |
|
332 | 332 | def __init__(self, shell=None, config=None, user_magics=None, **traits): |
|
333 | 333 | |
|
334 | 334 | super(MagicsManager, self).__init__(shell=shell, config=config, |
|
335 | 335 | user_magics=user_magics, **traits) |
|
336 | 336 | self.magics = dict(line={}, cell={}) |
|
337 | 337 | # Let's add the user_magics to the registry for uniformity, so *all* |
|
338 | 338 | # registered magic containers can be found there. |
|
339 | 339 | self.registry[user_magics.__class__.__name__] = user_magics |
|
340 | 340 | |
|
341 | 341 | def auto_status(self): |
|
342 | 342 | """Return descriptive string with automagic status.""" |
|
343 | 343 | return self._auto_status[self.auto_magic] |
|
344 | 344 | |
|
345 | 345 | def lsmagic(self): |
|
346 | 346 | """Return a dict of currently available magic functions. |
|
347 | 347 | |
|
348 | 348 | The return dict has the keys 'line' and 'cell', corresponding to the |
|
349 | 349 | two types of magics we support. Each value is a list of names. |
|
350 | 350 | """ |
|
351 | 351 | return self.magics |
|
352 | 352 | |
|
353 | 353 | def lsmagic_docs(self, brief=False, missing=''): |
|
354 | 354 | """Return dict of documentation of magic functions. |
|
355 | 355 | |
|
356 | 356 | The return dict has the keys 'line' and 'cell', corresponding to the |
|
357 | 357 | two types of magics we support. Each value is a dict keyed by magic |
|
358 | 358 | name whose value is the function docstring. If a docstring is |
|
359 | 359 | unavailable, the value of `missing` is used instead. |
|
360 | 360 | |
|
361 | 361 | If brief is True, only the first line of each docstring will be returned. |
|
362 | 362 | """ |
|
363 | 363 | docs = {} |
|
364 | 364 | for m_type in self.magics: |
|
365 | 365 | m_docs = {} |
|
366 | 366 | for m_name, m_func in self.magics[m_type].items(): |
|
367 | 367 | if m_func.__doc__: |
|
368 | 368 | if brief: |
|
369 | 369 | m_docs[m_name] = m_func.__doc__.split('\n', 1)[0] |
|
370 | 370 | else: |
|
371 | 371 | m_docs[m_name] = m_func.__doc__.rstrip() |
|
372 | 372 | else: |
|
373 | 373 | m_docs[m_name] = missing |
|
374 | 374 | docs[m_type] = m_docs |
|
375 | 375 | return docs |
|
376 | 376 | |
|
377 | 377 | def register(self, *magic_objects): |
|
378 | 378 | """Register one or more instances of Magics. |
|
379 | 379 | |
|
380 | 380 | Take one or more classes or instances of classes that subclass the main |
|
381 | 381 | `core.Magic` class, and register them with IPython to use the magic |
|
382 | 382 | functions they provide. The registration process will then ensure that |
|
383 | 383 | any methods that have decorated to provide line and/or cell magics will |
|
384 | 384 | be recognized with the `%x`/`%%x` syntax as a line/cell magic |
|
385 | 385 | respectively. |
|
386 | 386 | |
|
387 | 387 | If classes are given, they will be instantiated with the default |
|
388 | 388 | constructor. If your classes need a custom constructor, you should |
|
389 | 389 | instanitate them first and pass the instance. |
|
390 | 390 | |
|
391 | 391 | The provided arguments can be an arbitrary mix of classes and instances. |
|
392 | 392 | |
|
393 | 393 | Parameters |
|
394 | 394 | ---------- |
|
395 | 395 | magic_objects : one or more classes or instances |
|
396 | 396 | """ |
|
397 | 397 | # Start by validating them to ensure they have all had their magic |
|
398 | 398 | # methods registered at the instance level |
|
399 | 399 | for m in magic_objects: |
|
400 | 400 | if not m.registered: |
|
401 | 401 | raise ValueError("Class of magics %r was constructed without " |
|
402 | 402 | "the @register_magics class decorator") |
|
403 | 403 | if isinstance(m, type): |
|
404 | 404 | # If we're given an uninstantiated class |
|
405 | 405 | m = m(shell=self.shell) |
|
406 | 406 | |
|
407 | 407 | # Now that we have an instance, we can register it and update the |
|
408 | 408 | # table of callables |
|
409 | 409 | self.registry[m.__class__.__name__] = m |
|
410 | 410 | for mtype in magic_kinds: |
|
411 | 411 | self.magics[mtype].update(m.magics[mtype]) |
|
412 | 412 | |
|
413 | 413 | def register_function(self, func, magic_kind='line', magic_name=None): |
|
414 | 414 | """Expose a standalone function as magic function for IPython. |
|
415 | 415 | |
|
416 | 416 | This will create an IPython magic (line, cell or both) from a |
|
417 | 417 | standalone function. The functions should have the following |
|
418 | 418 | signatures: |
|
419 | 419 | |
|
420 | 420 | * For line magics: `def f(line)` |
|
421 | 421 | * For cell magics: `def f(line, cell)` |
|
422 | 422 | * For a function that does both: `def f(line, cell=None)` |
|
423 | 423 | |
|
424 | 424 | In the latter case, the function will be called with `cell==None` when |
|
425 | 425 | invoked as `%f`, and with cell as a string when invoked as `%%f`. |
|
426 | 426 | |
|
427 | 427 | Parameters |
|
428 | 428 | ---------- |
|
429 | 429 | func : callable |
|
430 | 430 | Function to be registered as a magic. |
|
431 | 431 | |
|
432 | 432 | magic_kind : str |
|
433 | 433 | Kind of magic, one of 'line', 'cell' or 'line_cell' |
|
434 | 434 | |
|
435 | 435 | magic_name : optional str |
|
436 | 436 | If given, the name the magic will have in the IPython namespace. By |
|
437 | 437 | default, the name of the function itself is used. |
|
438 | 438 | """ |
|
439 | 439 | |
|
440 | 440 | # Create the new method in the user_magics and register it in the |
|
441 | 441 | # global table |
|
442 | 442 | validate_type(magic_kind) |
|
443 | 443 | magic_name = func.__name__ if magic_name is None else magic_name |
|
444 | 444 | setattr(self.user_magics, magic_name, func) |
|
445 | 445 | record_magic(self.magics, magic_kind, magic_name, func) |
|
446 | 446 | |
|
447 | 447 | def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None): |
|
448 | 448 | """Register an alias to a magic function. |
|
449 | 449 | |
|
450 | 450 | The alias is an instance of :class:`MagicAlias`, which holds the |
|
451 | 451 | name and kind of the magic it should call. Binding is done at |
|
452 | 452 | call time, so if the underlying magic function is changed the alias |
|
453 | 453 | will call the new function. |
|
454 | 454 | |
|
455 | 455 | Parameters |
|
456 | 456 | ---------- |
|
457 | 457 | alias_name : str |
|
458 | 458 | The name of the magic to be registered. |
|
459 | 459 | |
|
460 | 460 | magic_name : str |
|
461 | 461 | The name of an existing magic. |
|
462 | 462 | |
|
463 | 463 | magic_kind : str |
|
464 | 464 | Kind of magic, one of 'line' or 'cell' |
|
465 | 465 | """ |
|
466 | 466 | |
|
467 | 467 | # `validate_type` is too permissive, as it allows 'line_cell' |
|
468 | 468 | # which we do not handle. |
|
469 | 469 | if magic_kind not in magic_kinds: |
|
470 | 470 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
471 | 471 | magic_kinds, magic_kind) |
|
472 | 472 | |
|
473 | 473 | alias = MagicAlias(self.shell, magic_name, magic_kind, magic_params) |
|
474 | 474 | setattr(self.user_magics, alias_name, alias) |
|
475 | 475 | record_magic(self.magics, magic_kind, alias_name, alias) |
|
476 | 476 | |
|
477 | 477 | # Key base class that provides the central functionality for magics. |
|
478 | 478 | |
|
479 | 479 | |
|
480 | 480 | class Magics(Configurable): |
|
481 | 481 | """Base class for implementing magic functions. |
|
482 | 482 | |
|
483 | 483 | Shell functions which can be reached as %function_name. All magic |
|
484 | 484 | functions should accept a string, which they can parse for their own |
|
485 | 485 | needs. This can make some functions easier to type, eg `%cd ../` |
|
486 | 486 | vs. `%cd("../")` |
|
487 | 487 | |
|
488 | 488 | Classes providing magic functions need to subclass this class, and they |
|
489 | 489 | MUST: |
|
490 | 490 | |
|
491 | 491 | - Use the method decorators `@line_magic` and `@cell_magic` to decorate |
|
492 | 492 | individual methods as magic functions, AND |
|
493 | 493 | |
|
494 | 494 | - Use the class decorator `@magics_class` to ensure that the magic |
|
495 | 495 | methods are properly registered at the instance level upon instance |
|
496 | 496 | initialization. |
|
497 | 497 | |
|
498 | 498 | See :mod:`magic_functions` for examples of actual implementation classes. |
|
499 | 499 | """ |
|
500 | 500 | # Dict holding all command-line options for each magic. |
|
501 | 501 | options_table = None |
|
502 | 502 | # Dict for the mapping of magic names to methods, set by class decorator |
|
503 | 503 | magics = None |
|
504 | 504 | # Flag to check that the class decorator was properly applied |
|
505 | 505 | registered = False |
|
506 | 506 | # Instance of IPython shell |
|
507 | 507 | shell = None |
|
508 | 508 | |
|
509 | 509 | def __init__(self, shell=None, **kwargs): |
|
510 | 510 | if not(self.__class__.registered): |
|
511 | 511 | raise ValueError('Magics subclass without registration - ' |
|
512 | 512 | 'did you forget to apply @magics_class?') |
|
513 | 513 | if shell is not None: |
|
514 | 514 | if hasattr(shell, 'configurables'): |
|
515 | 515 | shell.configurables.append(self) |
|
516 | 516 | if hasattr(shell, 'config'): |
|
517 | 517 | kwargs.setdefault('parent', shell) |
|
518 | 518 | |
|
519 | 519 | self.shell = shell |
|
520 | 520 | self.options_table = {} |
|
521 | 521 | # The method decorators are run when the instance doesn't exist yet, so |
|
522 | 522 | # they can only record the names of the methods they are supposed to |
|
523 | 523 | # grab. Only now, that the instance exists, can we create the proper |
|
524 | 524 | # mapping to bound methods. So we read the info off the original names |
|
525 | 525 | # table and replace each method name by the actual bound method. |
|
526 | 526 | # But we mustn't clobber the *class* mapping, in case of multiple instances. |
|
527 | 527 | class_magics = self.magics |
|
528 | 528 | self.magics = {} |
|
529 | 529 | for mtype in magic_kinds: |
|
530 | 530 | tab = self.magics[mtype] = {} |
|
531 | 531 | cls_tab = class_magics[mtype] |
|
532 | 532 | for magic_name, meth_name in cls_tab.items(): |
|
533 | 533 | if isinstance(meth_name, str): |
|
534 | 534 | # it's a method name, grab it |
|
535 | 535 | tab[magic_name] = getattr(self, meth_name) |
|
536 | 536 | else: |
|
537 | 537 | # it's the real thing |
|
538 | 538 | tab[magic_name] = meth_name |
|
539 | 539 | # Configurable **needs** to be initiated at the end or the config |
|
540 | 540 | # magics get screwed up. |
|
541 | 541 | super(Magics, self).__init__(**kwargs) |
|
542 | 542 | |
|
543 | 543 | def arg_err(self,func): |
|
544 | 544 | """Print docstring if incorrect arguments were passed""" |
|
545 | 545 | print('Error in arguments:') |
|
546 | 546 | print(oinspect.getdoc(func)) |
|
547 | 547 | |
|
548 | 548 | def format_latex(self, strng): |
|
549 | 549 | """Format a string for latex inclusion.""" |
|
550 | 550 | |
|
551 | 551 | # Characters that need to be escaped for latex: |
|
552 | 552 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) |
|
553 | 553 | # Magic command names as headers: |
|
554 | 554 | cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC, |
|
555 | 555 | re.MULTILINE) |
|
556 | 556 | # Magic commands |
|
557 | 557 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC, |
|
558 | 558 | re.MULTILINE) |
|
559 | 559 | # Paragraph continue |
|
560 | 560 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
561 | 561 | |
|
562 | 562 | # The "\n" symbol |
|
563 | 563 | newline_re = re.compile(r'\\n') |
|
564 | 564 | |
|
565 | 565 | # Now build the string for output: |
|
566 | 566 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
567 | 567 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
568 | 568 | strng) |
|
569 | 569 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
570 | 570 | strng = par_re.sub(r'\\\\',strng) |
|
571 | 571 | strng = escape_re.sub(r'\\\1',strng) |
|
572 | 572 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
573 | 573 | return strng |
|
574 | 574 | |
|
575 | 575 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): |
|
576 | 576 | """Parse options passed to an argument string. |
|
577 | 577 | |
|
578 | 578 | The interface is similar to that of :func:`getopt.getopt`, but it |
|
579 | 579 | returns a :class:`~IPython.utils.struct.Struct` with the options as keys |
|
580 | 580 | and the stripped argument string still as a string. |
|
581 | 581 | |
|
582 | 582 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
583 | 583 | This allows us to easily expand variables, glob files, quote |
|
584 | 584 | arguments, etc. |
|
585 | 585 | |
|
586 | 586 | Parameters |
|
587 | 587 | ---------- |
|
588 | 588 | |
|
589 | 589 | arg_str : str |
|
590 | 590 | The arguments to parse. |
|
591 | 591 | |
|
592 | 592 | opt_str : str |
|
593 | 593 | The options specification. |
|
594 | 594 | |
|
595 | 595 | mode : str, default 'string' |
|
596 | 596 | If given as 'list', the argument string is returned as a list (split |
|
597 | 597 | on whitespace) instead of a string. |
|
598 | 598 | |
|
599 | 599 | list_all : bool, default False |
|
600 | 600 | Put all option values in lists. Normally only options |
|
601 | 601 | appearing more than once are put in a list. |
|
602 | 602 | |
|
603 | 603 | posix : bool, default True |
|
604 | 604 | Whether to split the input line in POSIX mode or not, as per the |
|
605 | 605 | conventions outlined in the :mod:`shlex` module from the standard |
|
606 | 606 | library. |
|
607 | 607 | """ |
|
608 | 608 | |
|
609 | 609 | # inject default options at the beginning of the input line |
|
610 | 610 | caller = sys._getframe(1).f_code.co_name |
|
611 | 611 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
612 | 612 | |
|
613 | 613 | mode = kw.get('mode','string') |
|
614 | 614 | if mode not in ['string','list']: |
|
615 | 615 | raise ValueError('incorrect mode given: %s' % mode) |
|
616 | 616 | # Get options |
|
617 | 617 | list_all = kw.get('list_all',0) |
|
618 | 618 | posix = kw.get('posix', os.name == 'posix') |
|
619 | 619 | strict = kw.get('strict', True) |
|
620 | 620 | |
|
621 | 621 | # Check if we have more than one argument to warrant extra processing: |
|
622 | 622 | odict = {} # Dictionary with options |
|
623 | 623 | args = arg_str.split() |
|
624 | 624 | if len(args) >= 1: |
|
625 | 625 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
626 | 626 | # need to look for options |
|
627 | 627 | argv = arg_split(arg_str, posix, strict) |
|
628 | 628 | # Do regular option processing |
|
629 | 629 | try: |
|
630 | 630 | opts,args = getopt(argv, opt_str, long_opts) |
|
631 | 631 | except GetoptError as e: |
|
632 | 632 | raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |
|
633 | 633 | " ".join(long_opts))) |
|
634 | 634 | for o,a in opts: |
|
635 | 635 | if o.startswith('--'): |
|
636 | 636 | o = o[2:] |
|
637 | 637 | else: |
|
638 | 638 | o = o[1:] |
|
639 | 639 | try: |
|
640 | 640 | odict[o].append(a) |
|
641 | 641 | except AttributeError: |
|
642 | 642 | odict[o] = [odict[o],a] |
|
643 | 643 | except KeyError: |
|
644 | 644 | if list_all: |
|
645 | 645 | odict[o] = [a] |
|
646 | 646 | else: |
|
647 | 647 | odict[o] = a |
|
648 | 648 | |
|
649 | 649 | # Prepare opts,args for return |
|
650 | 650 | opts = Struct(odict) |
|
651 | 651 | if mode == 'string': |
|
652 | 652 | args = ' '.join(args) |
|
653 | 653 | |
|
654 | 654 | return opts,args |
|
655 | 655 | |
|
656 | 656 | def default_option(self, fn, optstr): |
|
657 | 657 | """Make an entry in the options_table for fn, with value optstr""" |
|
658 | 658 | |
|
659 | 659 | if fn not in self.lsmagic(): |
|
660 | 660 | error("%s is not a magic function" % fn) |
|
661 | 661 | self.options_table[fn] = optstr |
|
662 | 662 | |
|
663 | 663 | |
|
664 | 664 | class MagicAlias(object): |
|
665 | 665 | """An alias to another magic function. |
|
666 | 666 | |
|
667 | 667 | An alias is determined by its magic name and magic kind. Lookup |
|
668 | 668 | is done at call time, so if the underlying magic changes the alias |
|
669 | 669 | will call the new function. |
|
670 | 670 | |
|
671 | 671 | Use the :meth:`MagicsManager.register_alias` method or the |
|
672 | 672 | `%alias_magic` magic function to create and register a new alias. |
|
673 | 673 | """ |
|
674 | 674 | def __init__(self, shell, magic_name, magic_kind, magic_params=None): |
|
675 | 675 | self.shell = shell |
|
676 | 676 | self.magic_name = magic_name |
|
677 | 677 | self.magic_params = magic_params |
|
678 | 678 | self.magic_kind = magic_kind |
|
679 | 679 | |
|
680 | 680 | self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name) |
|
681 | 681 | self.__doc__ = "Alias for `%s`." % self.pretty_target |
|
682 | 682 | |
|
683 | 683 | self._in_call = False |
|
684 | 684 | |
|
685 | 685 | def __call__(self, *args, **kwargs): |
|
686 | 686 | """Call the magic alias.""" |
|
687 | 687 | fn = self.shell.find_magic(self.magic_name, self.magic_kind) |
|
688 | 688 | if fn is None: |
|
689 | 689 | raise UsageError("Magic `%s` not found." % self.pretty_target) |
|
690 | 690 | |
|
691 | 691 | # Protect against infinite recursion. |
|
692 | 692 | if self._in_call: |
|
693 | 693 | raise UsageError("Infinite recursion detected; " |
|
694 | 694 | "magic aliases cannot call themselves.") |
|
695 | 695 | self._in_call = True |
|
696 | 696 | try: |
|
697 | 697 | if self.magic_params: |
|
698 | 698 | args_list = list(args) |
|
699 | 699 | args_list[0] = self.magic_params + " " + args[0] |
|
700 | 700 | args = tuple(args_list) |
|
701 | 701 | return fn(*args, **kwargs) |
|
702 | 702 | finally: |
|
703 | 703 | self._in_call = False |
@@ -1,709 +1,709 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Prefiltering components. |
|
4 | 4 | |
|
5 | 5 | Prefilters transform user input before it is exec'd by Python. These |
|
6 | 6 | transforms are used to implement additional syntax such as !ls and %magic. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | # Copyright (c) IPython Development Team. |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | |
|
12 | 12 | from keyword import iskeyword |
|
13 | 13 | import re |
|
14 | 14 | |
|
15 |
from |
|
|
15 | from .autocall import IPyAutocall | |
|
16 | 16 | from traitlets.config.configurable import Configurable |
|
17 |
from |
|
|
17 | from .inputtransformer2 import ( | |
|
18 | 18 | ESC_MAGIC, |
|
19 | 19 | ESC_QUOTE, |
|
20 | 20 | ESC_QUOTE2, |
|
21 | 21 | ESC_PAREN, |
|
22 | 22 | ) |
|
23 |
from |
|
|
24 |
from |
|
|
23 | from .macro import Macro | |
|
24 | from .splitinput import LineInfo | |
|
25 | 25 | |
|
26 | 26 | from traitlets import ( |
|
27 | 27 | List, Integer, Unicode, Bool, Instance, CRegExp |
|
28 | 28 | ) |
|
29 | 29 | |
|
30 | 30 | #----------------------------------------------------------------------------- |
|
31 | 31 | # Global utilities, errors and constants |
|
32 | 32 | #----------------------------------------------------------------------------- |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class PrefilterError(Exception): |
|
36 | 36 | pass |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | # RegExp to identify potential function names |
|
40 | 40 | re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
41 | 41 | |
|
42 | 42 | # RegExp to exclude strings with this start from autocalling. In |
|
43 | 43 | # particular, all binary operators should be excluded, so that if foo is |
|
44 | 44 | # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The |
|
45 | 45 | # characters '!=()' don't need to be checked for, as the checkPythonChars |
|
46 | 46 | # routine explicitly does so, to catch direct calls and rebindings of |
|
47 | 47 | # existing names. |
|
48 | 48 | |
|
49 | 49 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
50 | 50 | # it affects the rest of the group in square brackets. |
|
51 | 51 | re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]' |
|
52 | 52 | r'|^is |^not |^in |^and |^or ') |
|
53 | 53 | |
|
54 | 54 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
55 | 55 | # (experimental). For this to work, the line_split regexp would need |
|
56 | 56 | # to be modified so it wouldn't break things at '['. That line is |
|
57 | 57 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
58 | 58 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | # Handler Check Utilities |
|
62 | 62 | def is_shadowed(identifier, ip): |
|
63 | 63 | """Is the given identifier defined in one of the namespaces which shadow |
|
64 | 64 | the alias and magic namespaces? Note that an identifier is different |
|
65 | 65 | than ifun, because it can not contain a '.' character.""" |
|
66 | 66 | # This is much safer than calling ofind, which can change state |
|
67 | 67 | return (identifier in ip.user_ns \ |
|
68 | 68 | or identifier in ip.user_global_ns \ |
|
69 | 69 | or identifier in ip.ns_table['builtin']\ |
|
70 | 70 | or iskeyword(identifier)) |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | #----------------------------------------------------------------------------- |
|
74 | 74 | # Main Prefilter manager |
|
75 | 75 | #----------------------------------------------------------------------------- |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | class PrefilterManager(Configurable): |
|
79 | 79 | """Main prefilter component. |
|
80 | 80 | |
|
81 | 81 | The IPython prefilter is run on all user input before it is run. The |
|
82 | 82 | prefilter consumes lines of input and produces transformed lines of |
|
83 | 83 | input. |
|
84 | 84 | |
|
85 | 85 | The implementation consists of two phases: |
|
86 | 86 | |
|
87 | 87 | 1. Transformers |
|
88 | 88 | 2. Checkers and handlers |
|
89 | 89 | |
|
90 | 90 | Over time, we plan on deprecating the checkers and handlers and doing |
|
91 | 91 | everything in the transformers. |
|
92 | 92 | |
|
93 | 93 | The transformers are instances of :class:`PrefilterTransformer` and have |
|
94 | 94 | a single method :meth:`transform` that takes a line and returns a |
|
95 | 95 | transformed line. The transformation can be accomplished using any |
|
96 | 96 | tool, but our current ones use regular expressions for speed. |
|
97 | 97 | |
|
98 | 98 | After all the transformers have been run, the line is fed to the checkers, |
|
99 | 99 | which are instances of :class:`PrefilterChecker`. The line is passed to |
|
100 | 100 | the :meth:`check` method, which either returns `None` or a |
|
101 | 101 | :class:`PrefilterHandler` instance. If `None` is returned, the other |
|
102 | 102 | checkers are tried. If an :class:`PrefilterHandler` instance is returned, |
|
103 | 103 | the line is passed to the :meth:`handle` method of the returned |
|
104 | 104 | handler and no further checkers are tried. |
|
105 | 105 | |
|
106 | 106 | Both transformers and checkers have a `priority` attribute, that determines |
|
107 | 107 | the order in which they are called. Smaller priorities are tried first. |
|
108 | 108 | |
|
109 | 109 | Both transformers and checkers also have `enabled` attribute, which is |
|
110 | 110 | a boolean that determines if the instance is used. |
|
111 | 111 | |
|
112 | 112 | Users or developers can change the priority or enabled attribute of |
|
113 | 113 | transformers or checkers, but they must call the :meth:`sort_checkers` |
|
114 | 114 | or :meth:`sort_transformers` method after changing the priority. |
|
115 | 115 | """ |
|
116 | 116 | |
|
117 | 117 | multi_line_specials = Bool(True).tag(config=True) |
|
118 | 118 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
119 | 119 | |
|
120 | 120 | def __init__(self, shell=None, **kwargs): |
|
121 | 121 | super(PrefilterManager, self).__init__(shell=shell, **kwargs) |
|
122 | 122 | self.shell = shell |
|
123 | 123 | self.init_transformers() |
|
124 | 124 | self.init_handlers() |
|
125 | 125 | self.init_checkers() |
|
126 | 126 | |
|
127 | 127 | #------------------------------------------------------------------------- |
|
128 | 128 | # API for managing transformers |
|
129 | 129 | #------------------------------------------------------------------------- |
|
130 | 130 | |
|
131 | 131 | def init_transformers(self): |
|
132 | 132 | """Create the default transformers.""" |
|
133 | 133 | self._transformers = [] |
|
134 | 134 | for transformer_cls in _default_transformers: |
|
135 | 135 | transformer_cls( |
|
136 | 136 | shell=self.shell, prefilter_manager=self, parent=self |
|
137 | 137 | ) |
|
138 | 138 | |
|
139 | 139 | def sort_transformers(self): |
|
140 | 140 | """Sort the transformers by priority. |
|
141 | 141 | |
|
142 | 142 | This must be called after the priority of a transformer is changed. |
|
143 | 143 | The :meth:`register_transformer` method calls this automatically. |
|
144 | 144 | """ |
|
145 | 145 | self._transformers.sort(key=lambda x: x.priority) |
|
146 | 146 | |
|
147 | 147 | @property |
|
148 | 148 | def transformers(self): |
|
149 | 149 | """Return a list of checkers, sorted by priority.""" |
|
150 | 150 | return self._transformers |
|
151 | 151 | |
|
152 | 152 | def register_transformer(self, transformer): |
|
153 | 153 | """Register a transformer instance.""" |
|
154 | 154 | if transformer not in self._transformers: |
|
155 | 155 | self._transformers.append(transformer) |
|
156 | 156 | self.sort_transformers() |
|
157 | 157 | |
|
158 | 158 | def unregister_transformer(self, transformer): |
|
159 | 159 | """Unregister a transformer instance.""" |
|
160 | 160 | if transformer in self._transformers: |
|
161 | 161 | self._transformers.remove(transformer) |
|
162 | 162 | |
|
163 | 163 | #------------------------------------------------------------------------- |
|
164 | 164 | # API for managing checkers |
|
165 | 165 | #------------------------------------------------------------------------- |
|
166 | 166 | |
|
167 | 167 | def init_checkers(self): |
|
168 | 168 | """Create the default checkers.""" |
|
169 | 169 | self._checkers = [] |
|
170 | 170 | for checker in _default_checkers: |
|
171 | 171 | checker( |
|
172 | 172 | shell=self.shell, prefilter_manager=self, parent=self |
|
173 | 173 | ) |
|
174 | 174 | |
|
175 | 175 | def sort_checkers(self): |
|
176 | 176 | """Sort the checkers by priority. |
|
177 | 177 | |
|
178 | 178 | This must be called after the priority of a checker is changed. |
|
179 | 179 | The :meth:`register_checker` method calls this automatically. |
|
180 | 180 | """ |
|
181 | 181 | self._checkers.sort(key=lambda x: x.priority) |
|
182 | 182 | |
|
183 | 183 | @property |
|
184 | 184 | def checkers(self): |
|
185 | 185 | """Return a list of checkers, sorted by priority.""" |
|
186 | 186 | return self._checkers |
|
187 | 187 | |
|
188 | 188 | def register_checker(self, checker): |
|
189 | 189 | """Register a checker instance.""" |
|
190 | 190 | if checker not in self._checkers: |
|
191 | 191 | self._checkers.append(checker) |
|
192 | 192 | self.sort_checkers() |
|
193 | 193 | |
|
194 | 194 | def unregister_checker(self, checker): |
|
195 | 195 | """Unregister a checker instance.""" |
|
196 | 196 | if checker in self._checkers: |
|
197 | 197 | self._checkers.remove(checker) |
|
198 | 198 | |
|
199 | 199 | #------------------------------------------------------------------------- |
|
200 | 200 | # API for managing handlers |
|
201 | 201 | #------------------------------------------------------------------------- |
|
202 | 202 | |
|
203 | 203 | def init_handlers(self): |
|
204 | 204 | """Create the default handlers.""" |
|
205 | 205 | self._handlers = {} |
|
206 | 206 | self._esc_handlers = {} |
|
207 | 207 | for handler in _default_handlers: |
|
208 | 208 | handler( |
|
209 | 209 | shell=self.shell, prefilter_manager=self, parent=self |
|
210 | 210 | ) |
|
211 | 211 | |
|
212 | 212 | @property |
|
213 | 213 | def handlers(self): |
|
214 | 214 | """Return a dict of all the handlers.""" |
|
215 | 215 | return self._handlers |
|
216 | 216 | |
|
217 | 217 | def register_handler(self, name, handler, esc_strings): |
|
218 | 218 | """Register a handler instance by name with esc_strings.""" |
|
219 | 219 | self._handlers[name] = handler |
|
220 | 220 | for esc_str in esc_strings: |
|
221 | 221 | self._esc_handlers[esc_str] = handler |
|
222 | 222 | |
|
223 | 223 | def unregister_handler(self, name, handler, esc_strings): |
|
224 | 224 | """Unregister a handler instance by name with esc_strings.""" |
|
225 | 225 | try: |
|
226 | 226 | del self._handlers[name] |
|
227 | 227 | except KeyError: |
|
228 | 228 | pass |
|
229 | 229 | for esc_str in esc_strings: |
|
230 | 230 | h = self._esc_handlers.get(esc_str) |
|
231 | 231 | if h is handler: |
|
232 | 232 | del self._esc_handlers[esc_str] |
|
233 | 233 | |
|
234 | 234 | def get_handler_by_name(self, name): |
|
235 | 235 | """Get a handler by its name.""" |
|
236 | 236 | return self._handlers.get(name) |
|
237 | 237 | |
|
238 | 238 | def get_handler_by_esc(self, esc_str): |
|
239 | 239 | """Get a handler by its escape string.""" |
|
240 | 240 | return self._esc_handlers.get(esc_str) |
|
241 | 241 | |
|
242 | 242 | #------------------------------------------------------------------------- |
|
243 | 243 | # Main prefiltering API |
|
244 | 244 | #------------------------------------------------------------------------- |
|
245 | 245 | |
|
246 | 246 | def prefilter_line_info(self, line_info): |
|
247 | 247 | """Prefilter a line that has been converted to a LineInfo object. |
|
248 | 248 | |
|
249 | 249 | This implements the checker/handler part of the prefilter pipe. |
|
250 | 250 | """ |
|
251 | 251 | # print "prefilter_line_info: ", line_info |
|
252 | 252 | handler = self.find_handler(line_info) |
|
253 | 253 | return handler.handle(line_info) |
|
254 | 254 | |
|
255 | 255 | def find_handler(self, line_info): |
|
256 | 256 | """Find a handler for the line_info by trying checkers.""" |
|
257 | 257 | for checker in self.checkers: |
|
258 | 258 | if checker.enabled: |
|
259 | 259 | handler = checker.check(line_info) |
|
260 | 260 | if handler: |
|
261 | 261 | return handler |
|
262 | 262 | return self.get_handler_by_name('normal') |
|
263 | 263 | |
|
264 | 264 | def transform_line(self, line, continue_prompt): |
|
265 | 265 | """Calls the enabled transformers in order of increasing priority.""" |
|
266 | 266 | for transformer in self.transformers: |
|
267 | 267 | if transformer.enabled: |
|
268 | 268 | line = transformer.transform(line, continue_prompt) |
|
269 | 269 | return line |
|
270 | 270 | |
|
271 | 271 | def prefilter_line(self, line, continue_prompt=False): |
|
272 | 272 | """Prefilter a single input line as text. |
|
273 | 273 | |
|
274 | 274 | This method prefilters a single line of text by calling the |
|
275 | 275 | transformers and then the checkers/handlers. |
|
276 | 276 | """ |
|
277 | 277 | |
|
278 | 278 | # print "prefilter_line: ", line, continue_prompt |
|
279 | 279 | # All handlers *must* return a value, even if it's blank (''). |
|
280 | 280 | |
|
281 | 281 | # save the line away in case we crash, so the post-mortem handler can |
|
282 | 282 | # record it |
|
283 | 283 | self.shell._last_input_line = line |
|
284 | 284 | |
|
285 | 285 | if not line: |
|
286 | 286 | # Return immediately on purely empty lines, so that if the user |
|
287 | 287 | # previously typed some whitespace that started a continuation |
|
288 | 288 | # prompt, he can break out of that loop with just an empty line. |
|
289 | 289 | # This is how the default python prompt works. |
|
290 | 290 | return '' |
|
291 | 291 | |
|
292 | 292 | # At this point, we invoke our transformers. |
|
293 | 293 | if not continue_prompt or (continue_prompt and self.multi_line_specials): |
|
294 | 294 | line = self.transform_line(line, continue_prompt) |
|
295 | 295 | |
|
296 | 296 | # Now we compute line_info for the checkers and handlers |
|
297 | 297 | line_info = LineInfo(line, continue_prompt) |
|
298 | 298 | |
|
299 | 299 | # the input history needs to track even empty lines |
|
300 | 300 | stripped = line.strip() |
|
301 | 301 | |
|
302 | 302 | normal_handler = self.get_handler_by_name('normal') |
|
303 | 303 | if not stripped: |
|
304 | 304 | return normal_handler.handle(line_info) |
|
305 | 305 | |
|
306 | 306 | # special handlers are only allowed for single line statements |
|
307 | 307 | if continue_prompt and not self.multi_line_specials: |
|
308 | 308 | return normal_handler.handle(line_info) |
|
309 | 309 | |
|
310 | 310 | prefiltered = self.prefilter_line_info(line_info) |
|
311 | 311 | # print "prefiltered line: %r" % prefiltered |
|
312 | 312 | return prefiltered |
|
313 | 313 | |
|
314 | 314 | def prefilter_lines(self, lines, continue_prompt=False): |
|
315 | 315 | """Prefilter multiple input lines of text. |
|
316 | 316 | |
|
317 | 317 | This is the main entry point for prefiltering multiple lines of |
|
318 | 318 | input. This simply calls :meth:`prefilter_line` for each line of |
|
319 | 319 | input. |
|
320 | 320 | |
|
321 | 321 | This covers cases where there are multiple lines in the user entry, |
|
322 | 322 | which is the case when the user goes back to a multiline history |
|
323 | 323 | entry and presses enter. |
|
324 | 324 | """ |
|
325 | 325 | llines = lines.rstrip('\n').split('\n') |
|
326 | 326 | # We can get multiple lines in one shot, where multiline input 'blends' |
|
327 | 327 | # into one line, in cases like recalling from the readline history |
|
328 | 328 | # buffer. We need to make sure that in such cases, we correctly |
|
329 | 329 | # communicate downstream which line is first and which are continuation |
|
330 | 330 | # ones. |
|
331 | 331 | if len(llines) > 1: |
|
332 | 332 | out = '\n'.join([self.prefilter_line(line, lnum>0) |
|
333 | 333 | for lnum, line in enumerate(llines) ]) |
|
334 | 334 | else: |
|
335 | 335 | out = self.prefilter_line(llines[0], continue_prompt) |
|
336 | 336 | |
|
337 | 337 | return out |
|
338 | 338 | |
|
339 | 339 | #----------------------------------------------------------------------------- |
|
340 | 340 | # Prefilter transformers |
|
341 | 341 | #----------------------------------------------------------------------------- |
|
342 | 342 | |
|
343 | 343 | |
|
344 | 344 | class PrefilterTransformer(Configurable): |
|
345 | 345 | """Transform a line of user input.""" |
|
346 | 346 | |
|
347 | 347 | priority = Integer(100).tag(config=True) |
|
348 | 348 | # Transformers don't currently use shell or prefilter_manager, but as we |
|
349 | 349 | # move away from checkers and handlers, they will need them. |
|
350 | 350 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
351 | 351 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
352 | 352 | enabled = Bool(True).tag(config=True) |
|
353 | 353 | |
|
354 | 354 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
355 | 355 | super(PrefilterTransformer, self).__init__( |
|
356 | 356 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
357 | 357 | ) |
|
358 | 358 | self.prefilter_manager.register_transformer(self) |
|
359 | 359 | |
|
360 | 360 | def transform(self, line, continue_prompt): |
|
361 | 361 | """Transform a line, returning the new one.""" |
|
362 | 362 | return None |
|
363 | 363 | |
|
364 | 364 | def __repr__(self): |
|
365 | 365 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
366 | 366 | self.__class__.__name__, self.priority, self.enabled) |
|
367 | 367 | |
|
368 | 368 | |
|
369 | 369 | #----------------------------------------------------------------------------- |
|
370 | 370 | # Prefilter checkers |
|
371 | 371 | #----------------------------------------------------------------------------- |
|
372 | 372 | |
|
373 | 373 | |
|
374 | 374 | class PrefilterChecker(Configurable): |
|
375 | 375 | """Inspect an input line and return a handler for that line.""" |
|
376 | 376 | |
|
377 | 377 | priority = Integer(100).tag(config=True) |
|
378 | 378 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
379 | 379 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
380 | 380 | enabled = Bool(True).tag(config=True) |
|
381 | 381 | |
|
382 | 382 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
383 | 383 | super(PrefilterChecker, self).__init__( |
|
384 | 384 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
385 | 385 | ) |
|
386 | 386 | self.prefilter_manager.register_checker(self) |
|
387 | 387 | |
|
388 | 388 | def check(self, line_info): |
|
389 | 389 | """Inspect line_info and return a handler instance or None.""" |
|
390 | 390 | return None |
|
391 | 391 | |
|
392 | 392 | def __repr__(self): |
|
393 | 393 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
394 | 394 | self.__class__.__name__, self.priority, self.enabled) |
|
395 | 395 | |
|
396 | 396 | |
|
397 | 397 | class EmacsChecker(PrefilterChecker): |
|
398 | 398 | |
|
399 | 399 | priority = Integer(100).tag(config=True) |
|
400 | 400 | enabled = Bool(False).tag(config=True) |
|
401 | 401 | |
|
402 | 402 | def check(self, line_info): |
|
403 | 403 | "Emacs ipython-mode tags certain input lines." |
|
404 | 404 | if line_info.line.endswith('# PYTHON-MODE'): |
|
405 | 405 | return self.prefilter_manager.get_handler_by_name('emacs') |
|
406 | 406 | else: |
|
407 | 407 | return None |
|
408 | 408 | |
|
409 | 409 | |
|
410 | 410 | class MacroChecker(PrefilterChecker): |
|
411 | 411 | |
|
412 | 412 | priority = Integer(250).tag(config=True) |
|
413 | 413 | |
|
414 | 414 | def check(self, line_info): |
|
415 | 415 | obj = self.shell.user_ns.get(line_info.ifun) |
|
416 | 416 | if isinstance(obj, Macro): |
|
417 | 417 | return self.prefilter_manager.get_handler_by_name('macro') |
|
418 | 418 | else: |
|
419 | 419 | return None |
|
420 | 420 | |
|
421 | 421 | |
|
422 | 422 | class IPyAutocallChecker(PrefilterChecker): |
|
423 | 423 | |
|
424 | 424 | priority = Integer(300).tag(config=True) |
|
425 | 425 | |
|
426 | 426 | def check(self, line_info): |
|
427 | 427 | "Instances of IPyAutocall in user_ns get autocalled immediately" |
|
428 | 428 | obj = self.shell.user_ns.get(line_info.ifun, None) |
|
429 | 429 | if isinstance(obj, IPyAutocall): |
|
430 | 430 | obj.set_ip(self.shell) |
|
431 | 431 | return self.prefilter_manager.get_handler_by_name('auto') |
|
432 | 432 | else: |
|
433 | 433 | return None |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | class AssignmentChecker(PrefilterChecker): |
|
437 | 437 | |
|
438 | 438 | priority = Integer(600).tag(config=True) |
|
439 | 439 | |
|
440 | 440 | def check(self, line_info): |
|
441 | 441 | """Check to see if user is assigning to a var for the first time, in |
|
442 | 442 | which case we want to avoid any sort of automagic / autocall games. |
|
443 | 443 | |
|
444 | 444 | This allows users to assign to either alias or magic names true python |
|
445 | 445 | variables (the magic/alias systems always take second seat to true |
|
446 | 446 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
447 | 447 | if line_info.the_rest: |
|
448 | 448 | if line_info.the_rest[0] in '=,': |
|
449 | 449 | return self.prefilter_manager.get_handler_by_name('normal') |
|
450 | 450 | else: |
|
451 | 451 | return None |
|
452 | 452 | |
|
453 | 453 | |
|
454 | 454 | class AutoMagicChecker(PrefilterChecker): |
|
455 | 455 | |
|
456 | 456 | priority = Integer(700).tag(config=True) |
|
457 | 457 | |
|
458 | 458 | def check(self, line_info): |
|
459 | 459 | """If the ifun is magic, and automagic is on, run it. Note: normal, |
|
460 | 460 | non-auto magic would already have been triggered via '%' in |
|
461 | 461 | check_esc_chars. This just checks for automagic. Also, before |
|
462 | 462 | triggering the magic handler, make sure that there is nothing in the |
|
463 | 463 | user namespace which could shadow it.""" |
|
464 | 464 | if not self.shell.automagic or not self.shell.find_magic(line_info.ifun): |
|
465 | 465 | return None |
|
466 | 466 | |
|
467 | 467 | # We have a likely magic method. Make sure we should actually call it. |
|
468 | 468 | if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials: |
|
469 | 469 | return None |
|
470 | 470 | |
|
471 | 471 | head = line_info.ifun.split('.',1)[0] |
|
472 | 472 | if is_shadowed(head, self.shell): |
|
473 | 473 | return None |
|
474 | 474 | |
|
475 | 475 | return self.prefilter_manager.get_handler_by_name('magic') |
|
476 | 476 | |
|
477 | 477 | |
|
478 | 478 | class PythonOpsChecker(PrefilterChecker): |
|
479 | 479 | |
|
480 | 480 | priority = Integer(900).tag(config=True) |
|
481 | 481 | |
|
482 | 482 | def check(self, line_info): |
|
483 | 483 | """If the 'rest' of the line begins with a function call or pretty much |
|
484 | 484 | any python operator, we should simply execute the line (regardless of |
|
485 | 485 | whether or not there's a possible autocall expansion). This avoids |
|
486 | 486 | spurious (and very confusing) geattr() accesses.""" |
|
487 | 487 | if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|': |
|
488 | 488 | return self.prefilter_manager.get_handler_by_name('normal') |
|
489 | 489 | else: |
|
490 | 490 | return None |
|
491 | 491 | |
|
492 | 492 | |
|
493 | 493 | class AutocallChecker(PrefilterChecker): |
|
494 | 494 | |
|
495 | 495 | priority = Integer(1000).tag(config=True) |
|
496 | 496 | |
|
497 | 497 | function_name_regexp = CRegExp(re_fun_name, |
|
498 | 498 | help="RegExp to identify potential function names." |
|
499 | 499 | ).tag(config=True) |
|
500 | 500 | exclude_regexp = CRegExp(re_exclude_auto, |
|
501 | 501 | help="RegExp to exclude strings with this start from autocalling." |
|
502 | 502 | ).tag(config=True) |
|
503 | 503 | |
|
504 | 504 | def check(self, line_info): |
|
505 | 505 | "Check if the initial word/function is callable and autocall is on." |
|
506 | 506 | if not self.shell.autocall: |
|
507 | 507 | return None |
|
508 | 508 | |
|
509 | 509 | oinfo = line_info.ofind(self.shell) # This can mutate state via getattr |
|
510 | 510 | if not oinfo['found']: |
|
511 | 511 | return None |
|
512 | 512 | |
|
513 | 513 | ignored_funs = ['b', 'f', 'r', 'u', 'br', 'rb', 'fr', 'rf'] |
|
514 | 514 | ifun = line_info.ifun |
|
515 | 515 | line = line_info.line |
|
516 | 516 | if ifun.lower() in ignored_funs and (line.startswith(ifun + "'") or line.startswith(ifun + '"')): |
|
517 | 517 | return None |
|
518 | 518 | |
|
519 | 519 | if callable(oinfo['obj']) \ |
|
520 | 520 | and (not self.exclude_regexp.match(line_info.the_rest)) \ |
|
521 | 521 | and self.function_name_regexp.match(line_info.ifun): |
|
522 | 522 | return self.prefilter_manager.get_handler_by_name('auto') |
|
523 | 523 | else: |
|
524 | 524 | return None |
|
525 | 525 | |
|
526 | 526 | |
|
527 | 527 | #----------------------------------------------------------------------------- |
|
528 | 528 | # Prefilter handlers |
|
529 | 529 | #----------------------------------------------------------------------------- |
|
530 | 530 | |
|
531 | 531 | |
|
532 | 532 | class PrefilterHandler(Configurable): |
|
533 | 533 | |
|
534 | 534 | handler_name = Unicode('normal') |
|
535 | 535 | esc_strings = List([]) |
|
536 | 536 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
537 | 537 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
538 | 538 | |
|
539 | 539 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
540 | 540 | super(PrefilterHandler, self).__init__( |
|
541 | 541 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
542 | 542 | ) |
|
543 | 543 | self.prefilter_manager.register_handler( |
|
544 | 544 | self.handler_name, |
|
545 | 545 | self, |
|
546 | 546 | self.esc_strings |
|
547 | 547 | ) |
|
548 | 548 | |
|
549 | 549 | def handle(self, line_info): |
|
550 | 550 | # print "normal: ", line_info |
|
551 | 551 | """Handle normal input lines. Use as a template for handlers.""" |
|
552 | 552 | |
|
553 | 553 | # With autoindent on, we need some way to exit the input loop, and I |
|
554 | 554 | # don't want to force the user to have to backspace all the way to |
|
555 | 555 | # clear the line. The rule will be in this case, that either two |
|
556 | 556 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
557 | 557 | # of a size different to the indent level, will exit the input loop. |
|
558 | 558 | line = line_info.line |
|
559 | 559 | continue_prompt = line_info.continue_prompt |
|
560 | 560 | |
|
561 | 561 | if (continue_prompt and |
|
562 | 562 | self.shell.autoindent and |
|
563 | 563 | line.isspace() and |
|
564 | 564 | 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2): |
|
565 | 565 | line = '' |
|
566 | 566 | |
|
567 | 567 | return line |
|
568 | 568 | |
|
569 | 569 | def __str__(self): |
|
570 | 570 | return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name) |
|
571 | 571 | |
|
572 | 572 | |
|
573 | 573 | class MacroHandler(PrefilterHandler): |
|
574 | 574 | handler_name = Unicode("macro") |
|
575 | 575 | |
|
576 | 576 | def handle(self, line_info): |
|
577 | 577 | obj = self.shell.user_ns.get(line_info.ifun) |
|
578 | 578 | pre_space = line_info.pre_whitespace |
|
579 | 579 | line_sep = "\n" + pre_space |
|
580 | 580 | return pre_space + line_sep.join(obj.value.splitlines()) |
|
581 | 581 | |
|
582 | 582 | |
|
583 | 583 | class MagicHandler(PrefilterHandler): |
|
584 | 584 | |
|
585 | 585 | handler_name = Unicode('magic') |
|
586 | 586 | esc_strings = List([ESC_MAGIC]) |
|
587 | 587 | |
|
588 | 588 | def handle(self, line_info): |
|
589 | 589 | """Execute magic functions.""" |
|
590 | 590 | ifun = line_info.ifun |
|
591 | 591 | the_rest = line_info.the_rest |
|
592 | 592 | #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args) |
|
593 | 593 | t_arg_s = ifun + " " + the_rest |
|
594 | 594 | t_magic_name, _, t_magic_arg_s = t_arg_s.partition(' ') |
|
595 | 595 | t_magic_name = t_magic_name.lstrip(ESC_MAGIC) |
|
596 | 596 | cmd = '%sget_ipython().run_line_magic(%r, %r)' % (line_info.pre_whitespace, t_magic_name, t_magic_arg_s) |
|
597 | 597 | return cmd |
|
598 | 598 | |
|
599 | 599 | |
|
600 | 600 | class AutoHandler(PrefilterHandler): |
|
601 | 601 | |
|
602 | 602 | handler_name = Unicode('auto') |
|
603 | 603 | esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2]) |
|
604 | 604 | |
|
605 | 605 | def handle(self, line_info): |
|
606 | 606 | """Handle lines which can be auto-executed, quoting if requested.""" |
|
607 | 607 | line = line_info.line |
|
608 | 608 | ifun = line_info.ifun |
|
609 | 609 | the_rest = line_info.the_rest |
|
610 | 610 | esc = line_info.esc |
|
611 | 611 | continue_prompt = line_info.continue_prompt |
|
612 | 612 | obj = line_info.ofind(self.shell)['obj'] |
|
613 | 613 | |
|
614 | 614 | # This should only be active for single-line input! |
|
615 | 615 | if continue_prompt: |
|
616 | 616 | return line |
|
617 | 617 | |
|
618 | 618 | force_auto = isinstance(obj, IPyAutocall) |
|
619 | 619 | |
|
620 | 620 | # User objects sometimes raise exceptions on attribute access other |
|
621 | 621 | # than AttributeError (we've seen it in the past), so it's safest to be |
|
622 | 622 | # ultra-conservative here and catch all. |
|
623 | 623 | try: |
|
624 | 624 | auto_rewrite = obj.rewrite |
|
625 | 625 | except Exception: |
|
626 | 626 | auto_rewrite = True |
|
627 | 627 | |
|
628 | 628 | if esc == ESC_QUOTE: |
|
629 | 629 | # Auto-quote splitting on whitespace |
|
630 | 630 | newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) ) |
|
631 | 631 | elif esc == ESC_QUOTE2: |
|
632 | 632 | # Auto-quote whole string |
|
633 | 633 | newcmd = '%s("%s")' % (ifun,the_rest) |
|
634 | 634 | elif esc == ESC_PAREN: |
|
635 | 635 | newcmd = '%s(%s)' % (ifun,",".join(the_rest.split())) |
|
636 | 636 | else: |
|
637 | 637 | # Auto-paren. |
|
638 | 638 | if force_auto: |
|
639 | 639 | # Don't rewrite if it is already a call. |
|
640 | 640 | do_rewrite = not the_rest.startswith('(') |
|
641 | 641 | else: |
|
642 | 642 | if not the_rest: |
|
643 | 643 | # We only apply it to argument-less calls if the autocall |
|
644 | 644 | # parameter is set to 2. |
|
645 | 645 | do_rewrite = (self.shell.autocall >= 2) |
|
646 | 646 | elif the_rest.startswith('[') and hasattr(obj, '__getitem__'): |
|
647 | 647 | # Don't autocall in this case: item access for an object |
|
648 | 648 | # which is BOTH callable and implements __getitem__. |
|
649 | 649 | do_rewrite = False |
|
650 | 650 | else: |
|
651 | 651 | do_rewrite = True |
|
652 | 652 | |
|
653 | 653 | # Figure out the rewritten command |
|
654 | 654 | if do_rewrite: |
|
655 | 655 | if the_rest.endswith(';'): |
|
656 | 656 | newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1]) |
|
657 | 657 | else: |
|
658 | 658 | newcmd = '%s(%s)' % (ifun.rstrip(), the_rest) |
|
659 | 659 | else: |
|
660 | 660 | normal_handler = self.prefilter_manager.get_handler_by_name('normal') |
|
661 | 661 | return normal_handler.handle(line_info) |
|
662 | 662 | |
|
663 | 663 | # Display the rewritten call |
|
664 | 664 | if auto_rewrite: |
|
665 | 665 | self.shell.auto_rewrite_input(newcmd) |
|
666 | 666 | |
|
667 | 667 | return newcmd |
|
668 | 668 | |
|
669 | 669 | |
|
670 | 670 | class EmacsHandler(PrefilterHandler): |
|
671 | 671 | |
|
672 | 672 | handler_name = Unicode('emacs') |
|
673 | 673 | esc_strings = List([]) |
|
674 | 674 | |
|
675 | 675 | def handle(self, line_info): |
|
676 | 676 | """Handle input lines marked by python-mode.""" |
|
677 | 677 | |
|
678 | 678 | # Currently, nothing is done. Later more functionality can be added |
|
679 | 679 | # here if needed. |
|
680 | 680 | |
|
681 | 681 | # The input cache shouldn't be updated |
|
682 | 682 | return line_info.line |
|
683 | 683 | |
|
684 | 684 | |
|
685 | 685 | #----------------------------------------------------------------------------- |
|
686 | 686 | # Defaults |
|
687 | 687 | #----------------------------------------------------------------------------- |
|
688 | 688 | |
|
689 | 689 | |
|
690 | 690 | _default_transformers = [ |
|
691 | 691 | ] |
|
692 | 692 | |
|
693 | 693 | _default_checkers = [ |
|
694 | 694 | EmacsChecker, |
|
695 | 695 | MacroChecker, |
|
696 | 696 | IPyAutocallChecker, |
|
697 | 697 | AssignmentChecker, |
|
698 | 698 | AutoMagicChecker, |
|
699 | 699 | PythonOpsChecker, |
|
700 | 700 | AutocallChecker |
|
701 | 701 | ] |
|
702 | 702 | |
|
703 | 703 | _default_handlers = [ |
|
704 | 704 | PrefilterHandler, |
|
705 | 705 | MacroHandler, |
|
706 | 706 | MagicHandler, |
|
707 | 707 | AutoHandler, |
|
708 | 708 | EmacsHandler |
|
709 | 709 | ] |
@@ -1,223 +1,223 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """An object for managing IPython profile directories.""" |
|
3 | 3 | |
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | 7 | import os |
|
8 | 8 | import shutil |
|
9 | 9 | import errno |
|
10 | 10 | |
|
11 | 11 | from traitlets.config.configurable import LoggingConfigurable |
|
12 |
from |
|
|
13 |
from |
|
|
12 | from ..paths import get_ipython_package_dir | |
|
13 | from ..utils.path import expand_path, ensure_dir_exists | |
|
14 | 14 | from traitlets import Unicode, Bool, observe |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Module errors |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | class ProfileDirError(Exception): |
|
21 | 21 | pass |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Class for managing profile directories |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | class ProfileDir(LoggingConfigurable): |
|
29 | 29 | """An object to manage the profile directory and its resources. |
|
30 | 30 | |
|
31 | 31 | The profile directory is used by all IPython applications, to manage |
|
32 | 32 | configuration, logging and security. |
|
33 | 33 | |
|
34 | 34 | This object knows how to find, create and manage these directories. This |
|
35 | 35 | should be used by any code that wants to handle profiles. |
|
36 | 36 | """ |
|
37 | 37 | |
|
38 | 38 | security_dir_name = Unicode('security') |
|
39 | 39 | log_dir_name = Unicode('log') |
|
40 | 40 | startup_dir_name = Unicode('startup') |
|
41 | 41 | pid_dir_name = Unicode('pid') |
|
42 | 42 | static_dir_name = Unicode('static') |
|
43 | 43 | security_dir = Unicode(u'') |
|
44 | 44 | log_dir = Unicode(u'') |
|
45 | 45 | startup_dir = Unicode(u'') |
|
46 | 46 | pid_dir = Unicode(u'') |
|
47 | 47 | static_dir = Unicode(u'') |
|
48 | 48 | |
|
49 | 49 | location = Unicode(u'', |
|
50 | 50 | help="""Set the profile location directly. This overrides the logic used by the |
|
51 | 51 | `profile` option.""", |
|
52 | 52 | ).tag(config=True) |
|
53 | 53 | |
|
54 | 54 | _location_isset = Bool(False) # flag for detecting multiply set location |
|
55 | 55 | @observe('location') |
|
56 | 56 | def _location_changed(self, change): |
|
57 | 57 | if self._location_isset: |
|
58 | 58 | raise RuntimeError("Cannot set profile location more than once.") |
|
59 | 59 | self._location_isset = True |
|
60 | 60 | new = change['new'] |
|
61 | 61 | ensure_dir_exists(new) |
|
62 | 62 | |
|
63 | 63 | # ensure config files exist: |
|
64 | 64 | self.security_dir = os.path.join(new, self.security_dir_name) |
|
65 | 65 | self.log_dir = os.path.join(new, self.log_dir_name) |
|
66 | 66 | self.startup_dir = os.path.join(new, self.startup_dir_name) |
|
67 | 67 | self.pid_dir = os.path.join(new, self.pid_dir_name) |
|
68 | 68 | self.static_dir = os.path.join(new, self.static_dir_name) |
|
69 | 69 | self.check_dirs() |
|
70 | 70 | |
|
71 | 71 | def _mkdir(self, path, mode=None): |
|
72 | 72 | """ensure a directory exists at a given path |
|
73 | 73 | |
|
74 | 74 | This is a version of os.mkdir, with the following differences: |
|
75 | 75 | |
|
76 | 76 | - returns True if it created the directory, False otherwise |
|
77 | 77 | - ignores EEXIST, protecting against race conditions where |
|
78 | 78 | the dir may have been created in between the check and |
|
79 | 79 | the creation |
|
80 | 80 | - sets permissions if requested and the dir already exists |
|
81 | 81 | """ |
|
82 | 82 | if os.path.exists(path): |
|
83 | 83 | if mode and os.stat(path).st_mode != mode: |
|
84 | 84 | try: |
|
85 | 85 | os.chmod(path, mode) |
|
86 | 86 | except OSError: |
|
87 | 87 | self.log.warning( |
|
88 | 88 | "Could not set permissions on %s", |
|
89 | 89 | path |
|
90 | 90 | ) |
|
91 | 91 | return False |
|
92 | 92 | try: |
|
93 | 93 | if mode: |
|
94 | 94 | os.mkdir(path, mode) |
|
95 | 95 | else: |
|
96 | 96 | os.mkdir(path) |
|
97 | 97 | except OSError as e: |
|
98 | 98 | if e.errno == errno.EEXIST: |
|
99 | 99 | return False |
|
100 | 100 | else: |
|
101 | 101 | raise |
|
102 | 102 | |
|
103 | 103 | return True |
|
104 | 104 | |
|
105 | 105 | @observe('log_dir') |
|
106 | 106 | def check_log_dir(self, change=None): |
|
107 | 107 | self._mkdir(self.log_dir) |
|
108 | 108 | |
|
109 | 109 | @observe('startup_dir') |
|
110 | 110 | def check_startup_dir(self, change=None): |
|
111 | 111 | self._mkdir(self.startup_dir) |
|
112 | 112 | |
|
113 | 113 | readme = os.path.join(self.startup_dir, 'README') |
|
114 | 114 | src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') |
|
115 | 115 | |
|
116 | 116 | if not os.path.exists(src): |
|
117 | 117 | self.log.warning("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) |
|
118 | 118 | |
|
119 | 119 | if os.path.exists(src) and not os.path.exists(readme): |
|
120 | 120 | shutil.copy(src, readme) |
|
121 | 121 | |
|
122 | 122 | @observe('security_dir') |
|
123 | 123 | def check_security_dir(self, change=None): |
|
124 | 124 | self._mkdir(self.security_dir, 0o40700) |
|
125 | 125 | |
|
126 | 126 | @observe('pid_dir') |
|
127 | 127 | def check_pid_dir(self, change=None): |
|
128 | 128 | self._mkdir(self.pid_dir, 0o40700) |
|
129 | 129 | |
|
130 | 130 | def check_dirs(self): |
|
131 | 131 | self.check_security_dir() |
|
132 | 132 | self.check_log_dir() |
|
133 | 133 | self.check_pid_dir() |
|
134 | 134 | self.check_startup_dir() |
|
135 | 135 | |
|
136 | 136 | def copy_config_file(self, config_file, path=None, overwrite=False): |
|
137 | 137 | """Copy a default config file into the active profile directory. |
|
138 | 138 | |
|
139 | 139 | Default configuration files are kept in :mod:`IPython.core.profile`. |
|
140 | 140 | This function moves these from that location to the working profile |
|
141 | 141 | directory. |
|
142 | 142 | """ |
|
143 | 143 | dst = os.path.join(self.location, config_file) |
|
144 | 144 | if os.path.isfile(dst) and not overwrite: |
|
145 | 145 | return False |
|
146 | 146 | if path is None: |
|
147 | 147 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') |
|
148 | 148 | src = os.path.join(path, config_file) |
|
149 | 149 | shutil.copy(src, dst) |
|
150 | 150 | return True |
|
151 | 151 | |
|
152 | 152 | @classmethod |
|
153 | 153 | def create_profile_dir(cls, profile_dir, config=None): |
|
154 | 154 | """Create a new profile directory given a full path. |
|
155 | 155 | |
|
156 | 156 | Parameters |
|
157 | 157 | ---------- |
|
158 | 158 | profile_dir : str |
|
159 | 159 | The full path to the profile directory. If it does exist, it will |
|
160 | 160 | be used. If not, it will be created. |
|
161 | 161 | """ |
|
162 | 162 | return cls(location=profile_dir, config=config) |
|
163 | 163 | |
|
164 | 164 | @classmethod |
|
165 | 165 | def create_profile_dir_by_name(cls, path, name=u'default', config=None): |
|
166 | 166 | """Create a profile dir by profile name and path. |
|
167 | 167 | |
|
168 | 168 | Parameters |
|
169 | 169 | ---------- |
|
170 | 170 | path : unicode |
|
171 | 171 | The path (directory) to put the profile directory in. |
|
172 | 172 | name : unicode |
|
173 | 173 | The name of the profile. The name of the profile directory will |
|
174 | 174 | be "profile_<profile>". |
|
175 | 175 | """ |
|
176 | 176 | if not os.path.isdir(path): |
|
177 | 177 | raise ProfileDirError('Directory not found: %s' % path) |
|
178 | 178 | profile_dir = os.path.join(path, u'profile_' + name) |
|
179 | 179 | return cls(location=profile_dir, config=config) |
|
180 | 180 | |
|
181 | 181 | @classmethod |
|
182 | 182 | def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): |
|
183 | 183 | """Find an existing profile dir by profile name, return its ProfileDir. |
|
184 | 184 | |
|
185 | 185 | This searches through a sequence of paths for a profile dir. If it |
|
186 | 186 | is not found, a :class:`ProfileDirError` exception will be raised. |
|
187 | 187 | |
|
188 | 188 | The search path algorithm is: |
|
189 | 189 | 1. ``os.getcwd()`` |
|
190 | 190 | 2. ``ipython_dir`` |
|
191 | 191 | |
|
192 | 192 | Parameters |
|
193 | 193 | ---------- |
|
194 | 194 | ipython_dir : unicode or str |
|
195 | 195 | The IPython directory to use. |
|
196 | 196 | name : unicode or str |
|
197 | 197 | The name of the profile. The name of the profile directory |
|
198 | 198 | will be "profile_<profile>". |
|
199 | 199 | """ |
|
200 | 200 | dirname = u'profile_' + name |
|
201 | 201 | paths = [os.getcwd(), ipython_dir] |
|
202 | 202 | for p in paths: |
|
203 | 203 | profile_dir = os.path.join(p, dirname) |
|
204 | 204 | if os.path.isdir(profile_dir): |
|
205 | 205 | return cls(location=profile_dir, config=config) |
|
206 | 206 | else: |
|
207 | 207 | raise ProfileDirError('Profile directory not found in paths: %s' % dirname) |
|
208 | 208 | |
|
209 | 209 | @classmethod |
|
210 | 210 | def find_profile_dir(cls, profile_dir, config=None): |
|
211 | 211 | """Find/create a profile dir and return its ProfileDir. |
|
212 | 212 | |
|
213 | 213 | This will create the profile directory if it doesn't exist. |
|
214 | 214 | |
|
215 | 215 | Parameters |
|
216 | 216 | ---------- |
|
217 | 217 | profile_dir : unicode or str |
|
218 | 218 | The path of the profile directory. |
|
219 | 219 | """ |
|
220 | 220 | profile_dir = expand_path(profile_dir) |
|
221 | 221 | if not os.path.isdir(profile_dir): |
|
222 | 222 | raise ProfileDirError('Profile directory not found: %s' % profile_dir) |
|
223 | 223 | return cls(location=profile_dir, config=config) |
General Comments 0
You need to be logged in to leave comments.
Login now