Show More
@@ -1,257 +1,257 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 | 28 | from IPython.core.error import UsageError |
|
29 | 29 | |
|
30 | 30 | from IPython.utils.py3compat import string_types |
|
31 | 31 | from traitlets import List, Instance |
|
32 | 32 | from logging import error |
|
33 | 33 | |
|
34 | 34 | #----------------------------------------------------------------------------- |
|
35 | 35 | # Utilities |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 | 38 | # This is used as the pattern for calls to split_user_input. |
|
39 | 39 | shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)') |
|
40 | 40 | |
|
41 | 41 | def default_aliases(): |
|
42 | 42 | """Return list of shell aliases to auto-define. |
|
43 | 43 | """ |
|
44 | 44 | # Note: the aliases defined here should be safe to use on a kernel |
|
45 | 45 | # regardless of what frontend it is attached to. Frontends that use a |
|
46 | 46 | # kernel in-process can define additional aliases that will only work in |
|
47 | 47 | # their case. For example, things like 'less' or 'clear' that manipulate |
|
48 | 48 | # the terminal should NOT be declared here, as they will only work if the |
|
49 | 49 | # kernel is running inside a true terminal, and not over the network. |
|
50 | 50 | |
|
51 | 51 | if os.name == 'posix': |
|
52 | 52 | default_aliases = [('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
53 | 53 | ('mv', 'mv'), ('rm', 'rm'), ('cp', 'cp'), |
|
54 | 54 | ('cat', 'cat'), |
|
55 | 55 | ] |
|
56 | 56 | # Useful set of ls aliases. The GNU and BSD options are a little |
|
57 | 57 | # different, so we make aliases that provide as similar as possible |
|
58 | 58 | # behavior in ipython, by passing the right flags for each platform |
|
59 | 59 | if sys.platform.startswith('linux'): |
|
60 | 60 | ls_aliases = [('ls', 'ls -F --color'), |
|
61 | 61 | # long ls |
|
62 | 62 | ('ll', 'ls -F -o --color'), |
|
63 | 63 | # ls normal files only |
|
64 | 64 | ('lf', 'ls -F -o --color %l | grep ^-'), |
|
65 | 65 | # ls symbolic links |
|
66 | 66 | ('lk', 'ls -F -o --color %l | grep ^l'), |
|
67 | 67 | # directories or links to directories, |
|
68 | 68 | ('ldir', 'ls -F -o --color %l | grep /$'), |
|
69 | 69 | # things which are executable |
|
70 | 70 | ('lx', 'ls -F -o --color %l | grep ^-..x'), |
|
71 | 71 | ] |
|
72 | 72 | elif sys.platform.startswith('openbsd') or sys.platform.startswith('netbsd'): |
|
73 | 73 | # OpenBSD, NetBSD. The ls implementation on these platforms do not support |
|
74 | 74 | # the -G switch and lack the ability to use colorized output. |
|
75 | 75 | ls_aliases = [('ls', 'ls -F'), |
|
76 | 76 | # long ls |
|
77 | 77 | ('ll', 'ls -F -l'), |
|
78 | 78 | # ls normal files only |
|
79 | 79 | ('lf', 'ls -F -l %l | grep ^-'), |
|
80 | 80 | # ls symbolic links |
|
81 | 81 | ('lk', 'ls -F -l %l | grep ^l'), |
|
82 | 82 | # directories or links to directories, |
|
83 | 83 | ('ldir', 'ls -F -l %l | grep /$'), |
|
84 | 84 | # things which are executable |
|
85 | 85 | ('lx', 'ls -F -l %l | grep ^-..x'), |
|
86 | 86 | ] |
|
87 | 87 | else: |
|
88 | 88 | # BSD, OSX, etc. |
|
89 | 89 | ls_aliases = [('ls', 'ls -F -G'), |
|
90 | 90 | # long ls |
|
91 | 91 | ('ll', 'ls -F -l -G'), |
|
92 | 92 | # ls normal files only |
|
93 | 93 | ('lf', 'ls -F -l -G %l | grep ^-'), |
|
94 | 94 | # ls symbolic links |
|
95 | 95 | ('lk', 'ls -F -l -G %l | grep ^l'), |
|
96 | 96 | # directories or links to directories, |
|
97 | 97 | ('ldir', 'ls -F -G -l %l | grep /$'), |
|
98 | 98 | # things which are executable |
|
99 | 99 | ('lx', 'ls -F -l -G %l | grep ^-..x'), |
|
100 | 100 | ] |
|
101 | 101 | default_aliases = default_aliases + ls_aliases |
|
102 | 102 | elif os.name in ['nt', 'dos']: |
|
103 | 103 | default_aliases = [('ls', 'dir /on'), |
|
104 | 104 | ('ddir', 'dir /ad /on'), ('ldir', 'dir /ad /on'), |
|
105 | 105 | ('mkdir', 'mkdir'), ('rmdir', 'rmdir'), |
|
106 | 106 | ('echo', 'echo'), ('ren', 'ren'), ('copy', 'copy'), |
|
107 | 107 | ] |
|
108 | 108 | else: |
|
109 | 109 | default_aliases = [] |
|
110 | 110 | |
|
111 | 111 | return default_aliases |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | class AliasError(Exception): |
|
115 | 115 | pass |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | class InvalidAliasError(AliasError): |
|
119 | 119 | pass |
|
120 | 120 | |
|
121 | 121 | class Alias(object): |
|
122 | 122 | """Callable object storing the details of one alias. |
|
123 | 123 | |
|
124 | 124 | Instances are registered as magic functions to allow use of aliases. |
|
125 | 125 | """ |
|
126 | 126 | |
|
127 | 127 | # Prepare blacklist |
|
128 | 128 | blacklist = {'cd','popd','pushd','dhist','alias','unalias'} |
|
129 | 129 | |
|
130 | 130 | def __init__(self, shell, name, cmd): |
|
131 | 131 | self.shell = shell |
|
132 | 132 | self.name = name |
|
133 | 133 | self.cmd = cmd |
|
134 | 134 | self.__doc__ = "Alias for `!{}`".format(cmd) |
|
135 | 135 | self.nargs = self.validate() |
|
136 | 136 | |
|
137 | 137 | def validate(self): |
|
138 | 138 | """Validate the alias, and return the number of arguments.""" |
|
139 | 139 | if self.name in self.blacklist: |
|
140 | 140 | raise InvalidAliasError("The name %s can't be aliased " |
|
141 | 141 | "because it is a keyword or builtin." % self.name) |
|
142 | 142 | try: |
|
143 | 143 | caller = self.shell.magics_manager.magics['line'][self.name] |
|
144 | 144 | except KeyError: |
|
145 | 145 | pass |
|
146 | 146 | else: |
|
147 | 147 | if not isinstance(caller, Alias): |
|
148 | 148 | raise InvalidAliasError("The name %s can't be aliased " |
|
149 | 149 | "because it is another magic command." % self.name) |
|
150 | 150 | |
|
151 | 151 | if not (isinstance(self.cmd, string_types)): |
|
152 | 152 | raise InvalidAliasError("An alias command must be a string, " |
|
153 | 153 | "got: %r" % self.cmd) |
|
154 | 154 | |
|
155 | 155 | nargs = self.cmd.count('%s') - self.cmd.count('%%s') |
|
156 | 156 | |
|
157 | 157 | if (nargs > 0) and (self.cmd.find('%l') >= 0): |
|
158 | 158 | raise InvalidAliasError('The %s and %l specifiers are mutually ' |
|
159 | 159 | 'exclusive in alias definitions.') |
|
160 | 160 | |
|
161 | 161 | return nargs |
|
162 | 162 | |
|
163 | 163 | def __repr__(self): |
|
164 | 164 | return "<alias {} for {!r}>".format(self.name, self.cmd) |
|
165 | 165 | |
|
166 | 166 | def __call__(self, rest=''): |
|
167 | 167 | cmd = self.cmd |
|
168 | 168 | nargs = self.nargs |
|
169 | 169 | # Expand the %l special to be the user's input line |
|
170 | 170 | if cmd.find('%l') >= 0: |
|
171 | 171 | cmd = cmd.replace('%l', rest) |
|
172 | 172 | rest = '' |
|
173 | 173 | |
|
174 | 174 | if nargs==0: |
|
175 | 175 | if cmd.find('%%s') >= 1: |
|
176 | 176 | cmd = cmd.replace('%%s', '%s') |
|
177 | 177 | # Simple, argument-less aliases |
|
178 | 178 | cmd = '%s %s' % (cmd, rest) |
|
179 | 179 | else: |
|
180 | 180 | # Handle aliases with positional arguments |
|
181 | 181 | args = rest.split(None, nargs) |
|
182 | 182 | if len(args) < nargs: |
|
183 | 183 | raise UsageError('Alias <%s> requires %s arguments, %s given.' % |
|
184 | 184 | (self.name, nargs, len(args))) |
|
185 | 185 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
186 | 186 | |
|
187 | 187 | self.shell.system(cmd) |
|
188 | 188 | |
|
189 | 189 | #----------------------------------------------------------------------------- |
|
190 | 190 | # Main AliasManager class |
|
191 | 191 | #----------------------------------------------------------------------------- |
|
192 | 192 | |
|
193 | 193 | class AliasManager(Configurable): |
|
194 | 194 | |
|
195 |
default_aliases = List(default_aliases() |
|
|
196 |
user_aliases = List(default_value=[] |
|
|
195 | default_aliases = List(default_aliases()).tag(config=True) | |
|
196 | user_aliases = List(default_value=[]).tag(config=True) | |
|
197 | 197 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
198 | 198 | |
|
199 | 199 | def __init__(self, shell=None, **kwargs): |
|
200 | 200 | super(AliasManager, self).__init__(shell=shell, **kwargs) |
|
201 | 201 | # For convenient access |
|
202 | 202 | self.linemagics = self.shell.magics_manager.magics['line'] |
|
203 | 203 | self.init_aliases() |
|
204 | 204 | |
|
205 | 205 | def init_aliases(self): |
|
206 | 206 | # Load default & user aliases |
|
207 | 207 | for name, cmd in self.default_aliases + self.user_aliases: |
|
208 | 208 | self.soft_define_alias(name, cmd) |
|
209 | 209 | |
|
210 | 210 | @property |
|
211 | 211 | def aliases(self): |
|
212 | 212 | return [(n, func.cmd) for (n, func) in self.linemagics.items() |
|
213 | 213 | if isinstance(func, Alias)] |
|
214 | 214 | |
|
215 | 215 | def soft_define_alias(self, name, cmd): |
|
216 | 216 | """Define an alias, but don't raise on an AliasError.""" |
|
217 | 217 | try: |
|
218 | 218 | self.define_alias(name, cmd) |
|
219 | 219 | except AliasError as e: |
|
220 | 220 | error("Invalid alias: %s" % e) |
|
221 | 221 | |
|
222 | 222 | def define_alias(self, name, cmd): |
|
223 | 223 | """Define a new alias after validating it. |
|
224 | 224 | |
|
225 | 225 | This will raise an :exc:`AliasError` if there are validation |
|
226 | 226 | problems. |
|
227 | 227 | """ |
|
228 | 228 | caller = Alias(shell=self.shell, name=name, cmd=cmd) |
|
229 | 229 | self.shell.magics_manager.register_function(caller, magic_kind='line', |
|
230 | 230 | magic_name=name) |
|
231 | 231 | |
|
232 | 232 | def get_alias(self, name): |
|
233 | 233 | """Return an alias, or None if no alias by that name exists.""" |
|
234 | 234 | aname = self.linemagics.get(name, None) |
|
235 | 235 | return aname if isinstance(aname, Alias) else None |
|
236 | 236 | |
|
237 | 237 | def is_alias(self, name): |
|
238 | 238 | """Return whether or not a given name has been defined as an alias""" |
|
239 | 239 | return self.get_alias(name) is not None |
|
240 | 240 | |
|
241 | 241 | def undefine_alias(self, name): |
|
242 | 242 | if self.is_alias(name): |
|
243 | 243 | del self.linemagics[name] |
|
244 | 244 | else: |
|
245 | 245 | raise ValueError('%s is not an alias' % name) |
|
246 | 246 | |
|
247 | 247 | def clear_aliases(self): |
|
248 | 248 | for name, cmd in self.aliases: |
|
249 | 249 | self.undefine_alias(name) |
|
250 | 250 | |
|
251 | 251 | def retrieve_alias(self, name): |
|
252 | 252 | """Retrieve the command to which an alias expands.""" |
|
253 | 253 | caller = self.get_alias(name) |
|
254 | 254 | if caller: |
|
255 | 255 | return caller.cmd |
|
256 | 256 | else: |
|
257 | 257 | raise ValueError('%s is not an alias' % name) |
@@ -1,407 +1,428 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | An application for IPython. |
|
4 | 4 | |
|
5 | 5 | All top-level applications should use the classes in this module for |
|
6 | 6 | handling configuration and creating configurables. |
|
7 | 7 | |
|
8 | 8 | The job of an :class:`Application` is to create the master configuration |
|
9 | 9 | object and then create the configurable objects, passing the config to them. |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | # Copyright (c) IPython Development Team. |
|
13 | 13 | # Distributed under the terms of the Modified BSD License. |
|
14 | 14 | |
|
15 | 15 | import atexit |
|
16 | 16 | import glob |
|
17 | 17 | import logging |
|
18 | 18 | import os |
|
19 | 19 | import shutil |
|
20 | 20 | import sys |
|
21 | 21 | |
|
22 | 22 | from traitlets.config.application import Application, catch_config_error |
|
23 | 23 | from traitlets.config.loader import ConfigFileNotFound, PyFileConfigLoader |
|
24 | 24 | from IPython.core import release, crashhandler |
|
25 | 25 | from IPython.core.profiledir import ProfileDir, ProfileDirError |
|
26 | 26 | from IPython.paths import get_ipython_dir, get_ipython_package_dir |
|
27 | 27 | from IPython.utils.path import ensure_dir_exists |
|
28 | 28 | from IPython.utils import py3compat |
|
29 | from traitlets import List, Unicode, Type, Bool, Dict, Set, Instance, Undefined | |
|
29 | from traitlets import ( | |
|
30 | List, Unicode, Type, Bool, Dict, Set, Instance, Undefined, | |
|
31 | default, observe, | |
|
32 | ) | |
|
30 | 33 | |
|
31 | 34 | if os.name == 'nt': |
|
32 | 35 | programdata = os.environ.get('PROGRAMDATA', None) |
|
33 | 36 | if programdata: |
|
34 | 37 | SYSTEM_CONFIG_DIRS = [os.path.join(programdata, 'ipython')] |
|
35 | 38 | else: # PROGRAMDATA is not defined by default on XP. |
|
36 | 39 | SYSTEM_CONFIG_DIRS = [] |
|
37 | 40 | else: |
|
38 | 41 | SYSTEM_CONFIG_DIRS = [ |
|
39 | 42 | "/usr/local/etc/ipython", |
|
40 | 43 | "/etc/ipython", |
|
41 | 44 | ] |
|
42 | 45 | |
|
43 | 46 | |
|
44 | 47 | # aliases and flags |
|
45 | 48 | |
|
46 | 49 | base_aliases = { |
|
47 | 50 | 'profile-dir' : 'ProfileDir.location', |
|
48 | 51 | 'profile' : 'BaseIPythonApplication.profile', |
|
49 | 52 | 'ipython-dir' : 'BaseIPythonApplication.ipython_dir', |
|
50 | 53 | 'log-level' : 'Application.log_level', |
|
51 | 54 | 'config' : 'BaseIPythonApplication.extra_config_file', |
|
52 | 55 | } |
|
53 | 56 | |
|
54 | 57 | base_flags = dict( |
|
55 | 58 | debug = ({'Application' : {'log_level' : logging.DEBUG}}, |
|
56 | 59 | "set log level to logging.DEBUG (maximize logging output)"), |
|
57 | 60 | quiet = ({'Application' : {'log_level' : logging.CRITICAL}}, |
|
58 | 61 | "set log level to logging.CRITICAL (minimize logging output)"), |
|
59 | 62 | init = ({'BaseIPythonApplication' : { |
|
60 | 63 | 'copy_config_files' : True, |
|
61 | 64 | 'auto_create' : True} |
|
62 | 65 | }, """Initialize profile with default config files. This is equivalent |
|
63 | 66 | to running `ipython profile create <profile>` prior to startup. |
|
64 | 67 | """) |
|
65 | 68 | ) |
|
66 | 69 | |
|
67 | 70 | class ProfileAwareConfigLoader(PyFileConfigLoader): |
|
68 | 71 | """A Python file config loader that is aware of IPython profiles.""" |
|
69 | 72 | def load_subconfig(self, fname, path=None, profile=None): |
|
70 | 73 | if profile is not None: |
|
71 | 74 | try: |
|
72 | 75 | profile_dir = ProfileDir.find_profile_dir_by_name( |
|
73 | 76 | get_ipython_dir(), |
|
74 | 77 | profile, |
|
75 | 78 | ) |
|
76 | 79 | except ProfileDirError: |
|
77 | 80 | return |
|
78 | 81 | path = profile_dir.location |
|
79 | 82 | return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path) |
|
80 | 83 | |
|
81 | 84 | class BaseIPythonApplication(Application): |
|
82 | 85 | |
|
83 | 86 | name = Unicode(u'ipython') |
|
84 | 87 | description = Unicode(u'IPython: an enhanced interactive Python shell.') |
|
85 | 88 | version = Unicode(release.version) |
|
86 | 89 | |
|
87 | 90 | aliases = Dict(base_aliases) |
|
88 | 91 | flags = Dict(base_flags) |
|
89 | 92 | classes = List([ProfileDir]) |
|
90 | 93 | |
|
91 | 94 | # enable `load_subconfig('cfg.py', profile='name')` |
|
92 | 95 | python_config_loader_class = ProfileAwareConfigLoader |
|
93 | 96 | |
|
94 | 97 | # Track whether the config_file has changed, |
|
95 | 98 | # because some logic happens only if we aren't using the default. |
|
96 | 99 | config_file_specified = Set() |
|
97 | 100 | |
|
98 | 101 | config_file_name = Unicode() |
|
102 | @default('config_file_name') | |
|
99 | 103 | def _config_file_name_default(self): |
|
100 | 104 | return self.name.replace('-','_') + u'_config.py' |
|
101 | def _config_file_name_changed(self, name, old, new): | |
|
102 | if new != old: | |
|
103 | self.config_file_specified.add(new) | |
|
105 | @observe('config_file_name') | |
|
106 | def _config_file_name_changed(self, change): | |
|
107 | if change['new'] != change['old']: | |
|
108 | self.config_file_specified.add(change['new']) | |
|
104 | 109 | |
|
105 | 110 | # The directory that contains IPython's builtin profiles. |
|
106 | 111 | builtin_profile_dir = Unicode( |
|
107 | 112 | os.path.join(get_ipython_package_dir(), u'config', u'profile', u'default') |
|
108 | 113 | ) |
|
109 | 114 | |
|
110 | 115 | config_file_paths = List(Unicode()) |
|
116 | @default('config_file_paths') | |
|
111 | 117 | def _config_file_paths_default(self): |
|
112 | 118 | return [py3compat.getcwd()] |
|
113 | 119 | |
|
114 |
extra_config_file = Unicode( |
|
|
120 | extra_config_file = Unicode( | |
|
115 | 121 | help="""Path to an extra config file to load. |
|
116 | 122 | |
|
117 | 123 | If specified, load this config file in addition to any other IPython config. |
|
118 | """) | |
|
119 | def _extra_config_file_changed(self, name, old, new): | |
|
124 | """).tag(config=True) | |
|
125 | @observe('extra_config_file') | |
|
126 | def _extra_config_file_changed(self, change): | |
|
127 | old = change['old'] | |
|
128 | new = change['new'] | |
|
120 | 129 | try: |
|
121 | 130 | self.config_files.remove(old) |
|
122 | 131 | except ValueError: |
|
123 | 132 | pass |
|
124 | 133 | self.config_file_specified.add(new) |
|
125 | 134 | self.config_files.append(new) |
|
126 | 135 | |
|
127 |
profile = Unicode(u'default', |
|
|
136 | profile = Unicode(u'default', | |
|
128 | 137 | help="""The IPython profile to use.""" |
|
129 | ) | |
|
130 | ||
|
131 | def _profile_changed(self, name, old, new): | |
|
138 | ).tag(config=True) | |
|
139 | ||
|
140 | @observe('profile') | |
|
141 | def _profile_changed(self, change): | |
|
132 | 142 | self.builtin_profile_dir = os.path.join( |
|
133 | get_ipython_package_dir(), u'config', u'profile', new | |
|
143 | get_ipython_package_dir(), u'config', u'profile', change['new'] | |
|
134 | 144 | ) |
|
135 | 145 | |
|
136 |
ipython_dir = Unicode( |
|
|
146 | ipython_dir = Unicode( | |
|
137 | 147 | help=""" |
|
138 | 148 | The name of the IPython directory. This directory is used for logging |
|
139 | 149 | configuration (through profiles), history storage, etc. The default |
|
140 | 150 | is usually $HOME/.ipython. This option can also be specified through |
|
141 | 151 | the environment variable IPYTHONDIR. |
|
142 | 152 | """ |
|
143 | ) | |
|
153 | ).tag(config=True) | |
|
154 | @default('ipython_dir') | |
|
144 | 155 | def _ipython_dir_default(self): |
|
145 | 156 | d = get_ipython_dir() |
|
146 |
self._ipython_dir_changed( |
|
|
157 | self._ipython_dir_changed({ | |
|
158 | 'name': 'ipython_dir', | |
|
159 | 'old': d, | |
|
160 | 'new': d, | |
|
161 | }) | |
|
147 | 162 | return d |
|
148 | 163 | |
|
149 | 164 | _in_init_profile_dir = False |
|
150 | 165 | profile_dir = Instance(ProfileDir, allow_none=True) |
|
166 | @default('profile_dir') | |
|
151 | 167 | def _profile_dir_default(self): |
|
152 | 168 | # avoid recursion |
|
153 | 169 | if self._in_init_profile_dir: |
|
154 | 170 | return |
|
155 | 171 | # profile_dir requested early, force initialization |
|
156 | 172 | self.init_profile_dir() |
|
157 | 173 | return self.profile_dir |
|
158 | 174 | |
|
159 |
overwrite = Bool(False, |
|
|
160 |
help="""Whether to overwrite existing config files when copying""" |
|
|
161 | auto_create = Bool(False, config=True, | |
|
162 | help="""Whether to create profile dir if it doesn't exist""") | |
|
175 | overwrite = Bool(False, | |
|
176 | help="""Whether to overwrite existing config files when copying""" | |
|
177 | ).tag(config=True) | |
|
178 | auto_create = Bool(False, | |
|
179 | help="""Whether to create profile dir if it doesn't exist""" | |
|
180 | ).tag(config=True) | |
|
163 | 181 | |
|
164 | 182 | config_files = List(Unicode()) |
|
183 | @default('config_files') | |
|
165 | 184 | def _config_files_default(self): |
|
166 | 185 | return [self.config_file_name] |
|
167 | 186 | |
|
168 |
copy_config_files = Bool(False, |
|
|
187 | copy_config_files = Bool(False, | |
|
169 | 188 | help="""Whether to install the default config files into the profile dir. |
|
170 | 189 | If a new profile is being created, and IPython contains config files for that |
|
171 | 190 | profile, then they will be staged into the new directory. Otherwise, |
|
172 | 191 | default config files will be automatically generated. |
|
173 | """) | |
|
192 | """).tag(config=True) | |
|
174 | 193 | |
|
175 |
verbose_crash = Bool(False, |
|
|
194 | verbose_crash = Bool(False, | |
|
176 | 195 | help="""Create a massive crash report when IPython encounters what may be an |
|
177 | 196 | internal error. The default is to append a short message to the |
|
178 | usual traceback""") | |
|
197 | usual traceback""").tag(config=True) | |
|
179 | 198 | |
|
180 | 199 | # The class to use as the crash handler. |
|
181 | 200 | crash_handler_class = Type(crashhandler.CrashHandler) |
|
182 | 201 | |
|
183 | 202 | @catch_config_error |
|
184 | 203 | def __init__(self, **kwargs): |
|
185 | 204 | super(BaseIPythonApplication, self).__init__(**kwargs) |
|
186 | 205 | # ensure current working directory exists |
|
187 | 206 | try: |
|
188 | 207 | py3compat.getcwd() |
|
189 | 208 | except: |
|
190 | 209 | # exit if cwd doesn't exist |
|
191 | 210 | self.log.error("Current working directory doesn't exist.") |
|
192 | 211 | self.exit(1) |
|
193 | 212 | |
|
194 | 213 | #------------------------------------------------------------------------- |
|
195 | 214 | # Various stages of Application creation |
|
196 | 215 | #------------------------------------------------------------------------- |
|
197 | 216 | |
|
198 | 217 | deprecated_subcommands = {} |
|
199 | 218 | |
|
200 | 219 | def initialize_subcommand(self, subc, argv=None): |
|
201 | 220 | if subc in self.deprecated_subcommands: |
|
202 | import time | |
|
203 | 221 | self.log.warning("Subcommand `ipython {sub}` is deprecated and will be removed " |
|
204 | 222 | "in future versions.".format(sub=subc)) |
|
205 | 223 | self.log.warning("You likely want to use `jupyter {sub}` in the " |
|
206 | 224 | "future".format(sub=subc)) |
|
207 | 225 | return super(BaseIPythonApplication, self).initialize_subcommand(subc, argv) |
|
208 | 226 | |
|
209 | 227 | def init_crash_handler(self): |
|
210 | 228 | """Create a crash handler, typically setting sys.excepthook to it.""" |
|
211 | 229 | self.crash_handler = self.crash_handler_class(self) |
|
212 | 230 | sys.excepthook = self.excepthook |
|
213 | 231 | def unset_crashhandler(): |
|
214 | 232 | sys.excepthook = sys.__excepthook__ |
|
215 | 233 | atexit.register(unset_crashhandler) |
|
216 | 234 | |
|
217 | 235 | def excepthook(self, etype, evalue, tb): |
|
218 | 236 | """this is sys.excepthook after init_crashhandler |
|
219 | 237 | |
|
220 | 238 | set self.verbose_crash=True to use our full crashhandler, instead of |
|
221 | 239 | a regular traceback with a short message (crash_handler_lite) |
|
222 | 240 | """ |
|
223 | 241 | |
|
224 | 242 | if self.verbose_crash: |
|
225 | 243 | return self.crash_handler(etype, evalue, tb) |
|
226 | 244 | else: |
|
227 | 245 | return crashhandler.crash_handler_lite(etype, evalue, tb) |
|
228 | ||
|
229 | def _ipython_dir_changed(self, name, old, new): | |
|
246 | ||
|
247 | @observe('ipython_dir') | |
|
248 | def _ipython_dir_changed(self, change): | |
|
249 | old = change['old'] | |
|
250 | new = change['new'] | |
|
230 | 251 | if old is not Undefined: |
|
231 | 252 | str_old = py3compat.cast_bytes_py2(os.path.abspath(old), |
|
232 | 253 | sys.getfilesystemencoding() |
|
233 | 254 | ) |
|
234 | 255 | if str_old in sys.path: |
|
235 | 256 | sys.path.remove(str_old) |
|
236 | 257 | str_path = py3compat.cast_bytes_py2(os.path.abspath(new), |
|
237 | 258 | sys.getfilesystemencoding() |
|
238 | 259 | ) |
|
239 | 260 | sys.path.append(str_path) |
|
240 | 261 | ensure_dir_exists(new) |
|
241 | 262 | readme = os.path.join(new, 'README') |
|
242 | 263 | readme_src = os.path.join(get_ipython_package_dir(), u'config', u'profile', 'README') |
|
243 | 264 | if not os.path.exists(readme) and os.path.exists(readme_src): |
|
244 | 265 | shutil.copy(readme_src, readme) |
|
245 | 266 | for d in ('extensions', 'nbextensions'): |
|
246 | 267 | path = os.path.join(new, d) |
|
247 | 268 | try: |
|
248 | 269 | ensure_dir_exists(path) |
|
249 | 270 | except OSError as e: |
|
250 | 271 | # this will not be EEXIST |
|
251 | 272 | self.log.error("couldn't create path %s: %s", path, e) |
|
252 | 273 | self.log.debug("IPYTHONDIR set to: %s" % new) |
|
253 | 274 | |
|
254 | 275 | def load_config_file(self, suppress_errors=True): |
|
255 | 276 | """Load the config file. |
|
256 | 277 | |
|
257 | 278 | By default, errors in loading config are handled, and a warning |
|
258 | 279 | printed on screen. For testing, the suppress_errors option is set |
|
259 | 280 | to False, so errors will make tests fail. |
|
260 | 281 | """ |
|
261 | 282 | self.log.debug("Searching path %s for config files", self.config_file_paths) |
|
262 | 283 | base_config = 'ipython_config.py' |
|
263 | 284 | self.log.debug("Attempting to load config file: %s" % |
|
264 | 285 | base_config) |
|
265 | 286 | try: |
|
266 | 287 | Application.load_config_file( |
|
267 | 288 | self, |
|
268 | 289 | base_config, |
|
269 | 290 | path=self.config_file_paths |
|
270 | 291 | ) |
|
271 | 292 | except ConfigFileNotFound: |
|
272 | 293 | # ignore errors loading parent |
|
273 | 294 | self.log.debug("Config file %s not found", base_config) |
|
274 | 295 | pass |
|
275 | 296 | |
|
276 | 297 | for config_file_name in self.config_files: |
|
277 | 298 | if not config_file_name or config_file_name == base_config: |
|
278 | 299 | continue |
|
279 | 300 | self.log.debug("Attempting to load config file: %s" % |
|
280 | 301 | self.config_file_name) |
|
281 | 302 | try: |
|
282 | 303 | Application.load_config_file( |
|
283 | 304 | self, |
|
284 | 305 | config_file_name, |
|
285 | 306 | path=self.config_file_paths |
|
286 | 307 | ) |
|
287 | 308 | except ConfigFileNotFound: |
|
288 | 309 | # Only warn if the default config file was NOT being used. |
|
289 | 310 | if config_file_name in self.config_file_specified: |
|
290 | 311 | msg = self.log.warning |
|
291 | 312 | else: |
|
292 | 313 | msg = self.log.debug |
|
293 | 314 | msg("Config file not found, skipping: %s", config_file_name) |
|
294 | 315 | except Exception: |
|
295 | 316 | # For testing purposes. |
|
296 | 317 | if not suppress_errors: |
|
297 | 318 | raise |
|
298 | 319 | self.log.warning("Error loading config file: %s" % |
|
299 | 320 | self.config_file_name, exc_info=True) |
|
300 | 321 | |
|
301 | 322 | def init_profile_dir(self): |
|
302 | 323 | """initialize the profile dir""" |
|
303 | 324 | self._in_init_profile_dir = True |
|
304 | 325 | if self.profile_dir is not None: |
|
305 | 326 | # already ran |
|
306 | 327 | return |
|
307 | 328 | if 'ProfileDir.location' not in self.config: |
|
308 | 329 | # location not specified, find by profile name |
|
309 | 330 | try: |
|
310 | 331 | p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
311 | 332 | except ProfileDirError: |
|
312 | 333 | # not found, maybe create it (always create default profile) |
|
313 | 334 | if self.auto_create or self.profile == 'default': |
|
314 | 335 | try: |
|
315 | 336 | p = ProfileDir.create_profile_dir_by_name(self.ipython_dir, self.profile, self.config) |
|
316 | 337 | except ProfileDirError: |
|
317 | 338 | self.log.fatal("Could not create profile: %r"%self.profile) |
|
318 | 339 | self.exit(1) |
|
319 | 340 | else: |
|
320 | 341 | self.log.info("Created profile dir: %r"%p.location) |
|
321 | 342 | else: |
|
322 | 343 | self.log.fatal("Profile %r not found."%self.profile) |
|
323 | 344 | self.exit(1) |
|
324 | 345 | else: |
|
325 | 346 | self.log.debug("Using existing profile dir: %r"%p.location) |
|
326 | 347 | else: |
|
327 | 348 | location = self.config.ProfileDir.location |
|
328 | 349 | # location is fully specified |
|
329 | 350 | try: |
|
330 | 351 | p = ProfileDir.find_profile_dir(location, self.config) |
|
331 | 352 | except ProfileDirError: |
|
332 | 353 | # not found, maybe create it |
|
333 | 354 | if self.auto_create: |
|
334 | 355 | try: |
|
335 | 356 | p = ProfileDir.create_profile_dir(location, self.config) |
|
336 | 357 | except ProfileDirError: |
|
337 | 358 | self.log.fatal("Could not create profile directory: %r"%location) |
|
338 | 359 | self.exit(1) |
|
339 | 360 | else: |
|
340 | 361 | self.log.debug("Creating new profile dir: %r"%location) |
|
341 | 362 | else: |
|
342 | 363 | self.log.fatal("Profile directory %r not found."%location) |
|
343 | 364 | self.exit(1) |
|
344 | 365 | else: |
|
345 | 366 | self.log.info("Using existing profile dir: %r"%location) |
|
346 | 367 | # if profile_dir is specified explicitly, set profile name |
|
347 | 368 | dir_name = os.path.basename(p.location) |
|
348 | 369 | if dir_name.startswith('profile_'): |
|
349 | 370 | self.profile = dir_name[8:] |
|
350 | 371 | |
|
351 | 372 | self.profile_dir = p |
|
352 | 373 | self.config_file_paths.append(p.location) |
|
353 | 374 | self._in_init_profile_dir = False |
|
354 | 375 | |
|
355 | 376 | def init_config_files(self): |
|
356 | 377 | """[optionally] copy default config files into profile dir.""" |
|
357 | 378 | self.config_file_paths.extend(SYSTEM_CONFIG_DIRS) |
|
358 | 379 | # copy config files |
|
359 | 380 | path = self.builtin_profile_dir |
|
360 | 381 | if self.copy_config_files: |
|
361 | 382 | src = self.profile |
|
362 | 383 | |
|
363 | 384 | cfg = self.config_file_name |
|
364 | 385 | if path and os.path.exists(os.path.join(path, cfg)): |
|
365 | 386 | self.log.warning("Staging %r from %s into %r [overwrite=%s]"%( |
|
366 | 387 | cfg, src, self.profile_dir.location, self.overwrite) |
|
367 | 388 | ) |
|
368 | 389 | self.profile_dir.copy_config_file(cfg, path=path, overwrite=self.overwrite) |
|
369 | 390 | else: |
|
370 | 391 | self.stage_default_config_file() |
|
371 | 392 | else: |
|
372 | 393 | # Still stage *bundled* config files, but not generated ones |
|
373 | 394 | # This is necessary for `ipython profile=sympy` to load the profile |
|
374 | 395 | # on the first go |
|
375 | 396 | files = glob.glob(os.path.join(path, '*.py')) |
|
376 | 397 | for fullpath in files: |
|
377 | 398 | cfg = os.path.basename(fullpath) |
|
378 | 399 | if self.profile_dir.copy_config_file(cfg, path=path, overwrite=False): |
|
379 | 400 | # file was copied |
|
380 | 401 | self.log.warning("Staging bundled %s from %s into %r"%( |
|
381 | 402 | cfg, self.profile, self.profile_dir.location) |
|
382 | 403 | ) |
|
383 | 404 | |
|
384 | 405 | |
|
385 | 406 | def stage_default_config_file(self): |
|
386 | 407 | """auto generate default config file, and stage it into the profile.""" |
|
387 | 408 | s = self.generate_config_file() |
|
388 | 409 | fname = os.path.join(self.profile_dir.location, self.config_file_name) |
|
389 | 410 | if self.overwrite or not os.path.exists(fname): |
|
390 | 411 | self.log.warning("Generating default config file: %r"%(fname)) |
|
391 | 412 | with open(fname, 'w') as f: |
|
392 | 413 | f.write(s) |
|
393 | 414 | |
|
394 | 415 | @catch_config_error |
|
395 | 416 | def initialize(self, argv=None): |
|
396 | 417 | # don't hook up crash handler before parsing command-line |
|
397 | 418 | self.parse_command_line(argv) |
|
398 | 419 | self.init_crash_handler() |
|
399 | 420 | if self.subapp is not None: |
|
400 | 421 | # stop here if subapp is taking over |
|
401 | 422 | return |
|
402 | 423 | cl_config = self.config |
|
403 | 424 | self.init_profile_dir() |
|
404 | 425 | self.init_config_files() |
|
405 | 426 | self.load_config_file() |
|
406 | 427 | # enforce cl-opts override configfile opts: |
|
407 | 428 | self.update_config(cl_config) |
@@ -1,1325 +1,1325 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Word completion for IPython. |
|
3 | 3 | |
|
4 | 4 | This module is a fork of the rlcompleter module in the Python standard |
|
5 | 5 | library. The original enhancements made to rlcompleter have been sent |
|
6 | 6 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
7 | 7 | functionality specific to IPython, so this module will continue to live as an |
|
8 | 8 | IPython-specific utility. |
|
9 | 9 | |
|
10 | 10 | Original rlcompleter documentation: |
|
11 | 11 | |
|
12 | 12 | This requires the latest extension to the readline module (the |
|
13 | 13 | completes keywords, built-ins and globals in __main__; when completing |
|
14 | 14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
15 | 15 | completes its attributes. |
|
16 | 16 | |
|
17 | 17 | It's very cool to do "import string" type "string.", hit the |
|
18 | 18 | completion key (twice), and see the list of names defined by the |
|
19 | 19 | string module! |
|
20 | 20 | |
|
21 | 21 | Tip: to use the tab key as the completion key, call |
|
22 | 22 | |
|
23 | 23 | readline.parse_and_bind("tab: complete") |
|
24 | 24 | |
|
25 | 25 | Notes: |
|
26 | 26 | |
|
27 | 27 | - Exceptions raised by the completer function are *ignored* (and |
|
28 | 28 | generally cause the completion to fail). This is a feature -- since |
|
29 | 29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
30 | 30 | traceback wouldn't work well without some complicated hoopla to save, |
|
31 | 31 | reset and restore the tty state. |
|
32 | 32 | |
|
33 | 33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
34 | 34 | application defined code to be executed if an object with a |
|
35 | 35 | ``__getattr__`` hook is found. Since it is the responsibility of the |
|
36 | 36 | application (or the user) to enable this feature, I consider this an |
|
37 | 37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
38 | 38 | indexing operations) are *not* evaluated. |
|
39 | 39 | |
|
40 | 40 | - GNU readline is also used by the built-in functions input() and |
|
41 | 41 | raw_input(), and thus these also benefit/suffer from the completer |
|
42 | 42 | features. Clearly an interactive application can benefit by |
|
43 | 43 | specifying its own completer function and using raw_input() for all |
|
44 | 44 | its input. |
|
45 | 45 | |
|
46 | 46 | - When the original stdin is not a tty device, GNU readline is never |
|
47 | 47 | used, and this module (and the readline module) are silently inactive. |
|
48 | 48 | """ |
|
49 | 49 | |
|
50 | 50 | # Copyright (c) IPython Development Team. |
|
51 | 51 | # Distributed under the terms of the Modified BSD License. |
|
52 | 52 | # |
|
53 | 53 | # Some of this code originated from rlcompleter in the Python standard library |
|
54 | 54 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
55 | 55 | |
|
56 | 56 | from __future__ import print_function |
|
57 | 57 | |
|
58 | 58 | import __main__ |
|
59 | 59 | import glob |
|
60 | 60 | import inspect |
|
61 | 61 | import itertools |
|
62 | 62 | import keyword |
|
63 | 63 | import os |
|
64 | 64 | import re |
|
65 | 65 | import sys |
|
66 | 66 | import unicodedata |
|
67 | 67 | import string |
|
68 | 68 | |
|
69 |
from traitlets.config.configurable import Configurable |
|
|
69 | from traitlets.config.configurable import Configurable | |
|
70 | 70 | from IPython.core.error import TryNext |
|
71 | 71 | from IPython.core.inputsplitter import ESC_MAGIC |
|
72 | 72 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol |
|
73 | 73 | from IPython.utils import generics |
|
74 | 74 | from IPython.utils.decorators import undoc |
|
75 | 75 | from IPython.utils.dir2 import dir2, get_real_method |
|
76 | 76 | from IPython.utils.process import arg_split |
|
77 | 77 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 |
|
78 |
from traitlets import |
|
|
78 | from traitlets import Bool, Enum, observe | |
|
79 | 79 | |
|
80 | 80 | import jedi |
|
81 | 81 | import jedi.api.helpers |
|
82 | 82 | import jedi.parser.user_context |
|
83 | 83 | |
|
84 | 84 | #----------------------------------------------------------------------------- |
|
85 | 85 | # Globals |
|
86 | 86 | #----------------------------------------------------------------------------- |
|
87 | 87 | |
|
88 | 88 | # Public API |
|
89 | 89 | __all__ = ['Completer','IPCompleter'] |
|
90 | 90 | |
|
91 | 91 | if sys.platform == 'win32': |
|
92 | 92 | PROTECTABLES = ' ' |
|
93 | 93 | else: |
|
94 | 94 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | #----------------------------------------------------------------------------- |
|
98 | 98 | # Main functions and classes |
|
99 | 99 | #----------------------------------------------------------------------------- |
|
100 | 100 | |
|
101 | 101 | def has_open_quotes(s): |
|
102 | 102 | """Return whether a string has open quotes. |
|
103 | 103 | |
|
104 | 104 | This simply counts whether the number of quote characters of either type in |
|
105 | 105 | the string is odd. |
|
106 | 106 | |
|
107 | 107 | Returns |
|
108 | 108 | ------- |
|
109 | 109 | If there is an open quote, the quote character is returned. Else, return |
|
110 | 110 | False. |
|
111 | 111 | """ |
|
112 | 112 | # We check " first, then ', so complex cases with nested quotes will get |
|
113 | 113 | # the " to take precedence. |
|
114 | 114 | if s.count('"') % 2: |
|
115 | 115 | return '"' |
|
116 | 116 | elif s.count("'") % 2: |
|
117 | 117 | return "'" |
|
118 | 118 | else: |
|
119 | 119 | return False |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | def protect_filename(s): |
|
123 | 123 | """Escape a string to protect certain characters.""" |
|
124 | 124 | |
|
125 | 125 | return "".join([(ch in PROTECTABLES and '\\' + ch or ch) |
|
126 | 126 | for ch in s]) |
|
127 | 127 | |
|
128 | 128 | def expand_user(path): |
|
129 | 129 | """Expand '~'-style usernames in strings. |
|
130 | 130 | |
|
131 | 131 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
132 | 132 | extra information that will be useful if the input was being used in |
|
133 | 133 | computing completions, and you wish to return the completions with the |
|
134 | 134 | original '~' instead of its expanded value. |
|
135 | 135 | |
|
136 | 136 | Parameters |
|
137 | 137 | ---------- |
|
138 | 138 | path : str |
|
139 | 139 | String to be expanded. If no ~ is present, the output is the same as the |
|
140 | 140 | input. |
|
141 | 141 | |
|
142 | 142 | Returns |
|
143 | 143 | ------- |
|
144 | 144 | newpath : str |
|
145 | 145 | Result of ~ expansion in the input path. |
|
146 | 146 | tilde_expand : bool |
|
147 | 147 | Whether any expansion was performed or not. |
|
148 | 148 | tilde_val : str |
|
149 | 149 | The value that ~ was replaced with. |
|
150 | 150 | """ |
|
151 | 151 | # Default values |
|
152 | 152 | tilde_expand = False |
|
153 | 153 | tilde_val = '' |
|
154 | 154 | newpath = path |
|
155 | 155 | |
|
156 | 156 | if path.startswith('~'): |
|
157 | 157 | tilde_expand = True |
|
158 | 158 | rest = len(path)-1 |
|
159 | 159 | newpath = os.path.expanduser(path) |
|
160 | 160 | if rest: |
|
161 | 161 | tilde_val = newpath[:-rest] |
|
162 | 162 | else: |
|
163 | 163 | tilde_val = newpath |
|
164 | 164 | |
|
165 | 165 | return newpath, tilde_expand, tilde_val |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | def compress_user(path, tilde_expand, tilde_val): |
|
169 | 169 | """Does the opposite of expand_user, with its outputs. |
|
170 | 170 | """ |
|
171 | 171 | if tilde_expand: |
|
172 | 172 | return path.replace(tilde_val, '~') |
|
173 | 173 | else: |
|
174 | 174 | return path |
|
175 | 175 | |
|
176 | 176 | |
|
177 | ||
|
178 | 177 | def completions_sorting_key(word): |
|
179 | 178 | """key for sorting completions |
|
180 | 179 | |
|
181 | 180 | This does several things: |
|
182 | 181 | |
|
183 | 182 | - Lowercase all completions, so they are sorted alphabetically with |
|
184 | 183 | upper and lower case words mingled |
|
185 | 184 | - Demote any completions starting with underscores to the end |
|
186 | 185 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
187 | 186 | by their name |
|
188 | 187 | """ |
|
189 | 188 | # Case insensitive sort |
|
190 | 189 | word = word.lower() |
|
191 | 190 | |
|
192 | 191 | prio1, prio2 = 0, 0 |
|
193 | 192 | |
|
194 | 193 | if word.startswith('__'): |
|
195 | 194 | prio1 = 2 |
|
196 | 195 | elif word.startswith('_'): |
|
197 | 196 | prio1 = 1 |
|
198 | 197 | |
|
199 | 198 | if word.endswith('='): |
|
200 | 199 | prio1 = -1 |
|
201 | 200 | |
|
202 | 201 | if word.startswith('%%'): |
|
203 | 202 | # If there's another % in there, this is something else, so leave it alone |
|
204 | 203 | if not "%" in word[2:]: |
|
205 | 204 | word = word[2:] |
|
206 | 205 | prio2 = 2 |
|
207 | 206 | elif word.startswith('%'): |
|
208 | 207 | if not "%" in word[1:]: |
|
209 | 208 | word = word[1:] |
|
210 | 209 | prio2 = 1 |
|
211 | 210 | |
|
212 | 211 | return prio1, word, prio2 |
|
213 | 212 | |
|
214 | 213 | |
|
215 | 214 | @undoc |
|
216 | 215 | class Bunch(object): pass |
|
217 | 216 | |
|
218 | 217 | |
|
219 | 218 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
220 | 219 | GREEDY_DELIMS = ' =\r\n' |
|
221 | 220 | |
|
222 | 221 | |
|
223 | 222 | class CompletionSplitter(object): |
|
224 | 223 | """An object to split an input line in a manner similar to readline. |
|
225 | 224 | |
|
226 | 225 | By having our own implementation, we can expose readline-like completion in |
|
227 | 226 | a uniform manner to all frontends. This object only needs to be given the |
|
228 | 227 | line of text to be split and the cursor position on said line, and it |
|
229 | 228 | returns the 'word' to be completed on at the cursor after splitting the |
|
230 | 229 | entire line. |
|
231 | 230 | |
|
232 | 231 | What characters are used as splitting delimiters can be controlled by |
|
233 | 232 | setting the `delims` attribute (this is a property that internally |
|
234 | 233 | automatically builds the necessary regular expression)""" |
|
235 | 234 | |
|
236 | 235 | # Private interface |
|
237 | 236 | |
|
238 | 237 | # A string of delimiter characters. The default value makes sense for |
|
239 | 238 | # IPython's most typical usage patterns. |
|
240 | 239 | _delims = DELIMS |
|
241 | 240 | |
|
242 | 241 | # The expression (a normal string) to be compiled into a regular expression |
|
243 | 242 | # for actual splitting. We store it as an attribute mostly for ease of |
|
244 | 243 | # debugging, since this type of code can be so tricky to debug. |
|
245 | 244 | _delim_expr = None |
|
246 | 245 | |
|
247 | 246 | # The regular expression that does the actual splitting |
|
248 | 247 | _delim_re = None |
|
249 | 248 | |
|
250 | 249 | def __init__(self, delims=None): |
|
251 | 250 | delims = CompletionSplitter._delims if delims is None else delims |
|
252 | 251 | self.delims = delims |
|
253 | 252 | |
|
254 | 253 | @property |
|
255 | 254 | def delims(self): |
|
256 | 255 | """Return the string of delimiter characters.""" |
|
257 | 256 | return self._delims |
|
258 | 257 | |
|
259 | 258 | @delims.setter |
|
260 | 259 | def delims(self, delims): |
|
261 | 260 | """Set the delimiters for line splitting.""" |
|
262 | 261 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
263 | 262 | self._delim_re = re.compile(expr) |
|
264 | 263 | self._delims = delims |
|
265 | 264 | self._delim_expr = expr |
|
266 | 265 | |
|
267 | 266 | def split_line(self, line, cursor_pos=None): |
|
268 | 267 | """Split a line of text with a cursor at the given position. |
|
269 | 268 | """ |
|
270 | 269 | l = line if cursor_pos is None else line[:cursor_pos] |
|
271 | 270 | return self._delim_re.split(l)[-1] |
|
272 | 271 | |
|
273 | 272 | |
|
274 | 273 | class Completer(Configurable): |
|
275 | 274 | |
|
276 |
greedy = |
|
|
275 | greedy = Bool(False, | |
|
277 | 276 | help="""Activate greedy completion |
|
278 | 277 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
279 | 278 | |
|
280 | 279 | This will enable completion on elements of lists, results of function calls, etc., |
|
281 | 280 | but can be unsafe because the code is actually evaluated on TAB. |
|
282 | 281 | """ |
|
283 | ) | |
|
282 | ).tag(config=True) | |
|
284 | 283 | |
|
285 | 284 | |
|
286 | 285 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
287 | 286 | """Create a new completer for the command line. |
|
288 | 287 | |
|
289 | 288 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
290 | 289 | |
|
291 | 290 | If unspecified, the default namespace where completions are performed |
|
292 | 291 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
293 | 292 | given as dictionaries. |
|
294 | 293 | |
|
295 | 294 | An optional second namespace can be given. This allows the completer |
|
296 | 295 | to handle cases where both the local and global scopes need to be |
|
297 | 296 | distinguished. |
|
298 | 297 | |
|
299 | 298 | Completer instances should be used as the completion mechanism of |
|
300 | 299 | readline via the set_completer() call: |
|
301 | 300 | |
|
302 | 301 | readline.set_completer(Completer(my_namespace).complete) |
|
303 | 302 | """ |
|
304 | 303 | |
|
305 | 304 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
306 | 305 | # specific namespace or to use __main__.__dict__. This will allow us |
|
307 | 306 | # to bind to __main__.__dict__ at completion time, not now. |
|
308 | 307 | if namespace is None: |
|
309 | 308 | self.use_main_ns = 1 |
|
310 | 309 | else: |
|
311 | 310 | self.use_main_ns = 0 |
|
312 | 311 | self.namespace = namespace |
|
313 | 312 | |
|
314 | 313 | # The global namespace, if given, can be bound directly |
|
315 | 314 | if global_namespace is None: |
|
316 | 315 | self.global_namespace = {} |
|
317 | 316 | else: |
|
318 | 317 | self.global_namespace = global_namespace |
|
319 | 318 | |
|
320 | 319 | super(Completer, self).__init__(**kwargs) |
|
321 | 320 | |
|
322 | 321 | def complete(self, text, state): |
|
323 | 322 | """Return the next possible completion for 'text'. |
|
324 | 323 | |
|
325 | 324 | This is called successively with state == 0, 1, 2, ... until it |
|
326 | 325 | returns None. The completion should begin with 'text'. |
|
327 | 326 | |
|
328 | 327 | """ |
|
329 | 328 | if self.use_main_ns: |
|
330 | 329 | self.namespace = __main__.__dict__ |
|
331 | 330 | |
|
332 | 331 | if state == 0: |
|
333 | 332 | if "." in text: |
|
334 | 333 | self.matches = self.attr_matches(text) |
|
335 | 334 | else: |
|
336 | 335 | self.matches = self.global_matches(text) |
|
337 | 336 | try: |
|
338 | 337 | return self.matches[state] |
|
339 | 338 | except IndexError: |
|
340 | 339 | return None |
|
341 | 340 | |
|
342 | 341 | def global_matches(self, text): |
|
343 | 342 | """Compute matches when text is a simple name. |
|
344 | 343 | |
|
345 | 344 | Return a list of all keywords, built-in functions and names currently |
|
346 | 345 | defined in self.namespace or self.global_namespace that match. |
|
347 | 346 | |
|
348 | 347 | """ |
|
349 | 348 | matches = [] |
|
350 | 349 | match_append = matches.append |
|
351 | 350 | n = len(text) |
|
352 | 351 | for lst in [keyword.kwlist, |
|
353 | 352 | builtin_mod.__dict__.keys(), |
|
354 | 353 | self.namespace.keys(), |
|
355 | 354 | self.global_namespace.keys()]: |
|
356 | 355 | for word in lst: |
|
357 | 356 | if word[:n] == text and word != "__builtins__": |
|
358 | 357 | match_append(word) |
|
359 | 358 | return [cast_unicode_py2(m) for m in matches] |
|
360 | 359 | |
|
361 | 360 | def attr_matches(self, text): |
|
362 | 361 | """Compute matches when text contains a dot. |
|
363 | 362 | |
|
364 | 363 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
365 | 364 | evaluatable in self.namespace or self.global_namespace, it will be |
|
366 | 365 | evaluated and its attributes (as revealed by dir()) are used as |
|
367 | 366 | possible completions. (For class instances, class members are are |
|
368 | 367 | also considered.) |
|
369 | 368 | |
|
370 | 369 | WARNING: this can still invoke arbitrary C code, if an object |
|
371 | 370 | with a __getattr__ hook is evaluated. |
|
372 | 371 | |
|
373 | 372 | """ |
|
374 | 373 | |
|
375 | 374 | # Another option, seems to work great. Catches things like ''.<tab> |
|
376 | 375 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
377 | 376 | |
|
378 | 377 | if m: |
|
379 | 378 | expr, attr = m.group(1, 3) |
|
380 | 379 | elif self.greedy: |
|
381 | 380 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
382 | 381 | if not m2: |
|
383 | 382 | return [] |
|
384 | 383 | expr, attr = m2.group(1,2) |
|
385 | 384 | else: |
|
386 | 385 | return [] |
|
387 | 386 | |
|
388 | 387 | try: |
|
389 | 388 | obj = eval(expr, self.namespace) |
|
390 | 389 | except: |
|
391 | 390 | try: |
|
392 | 391 | obj = eval(expr, self.global_namespace) |
|
393 | 392 | except: |
|
394 | 393 | return [] |
|
395 | 394 | |
|
396 | 395 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
397 | 396 | words = get__all__entries(obj) |
|
398 | 397 | else: |
|
399 | 398 | words = dir2(obj) |
|
400 | 399 | |
|
401 | 400 | try: |
|
402 | 401 | words = generics.complete_object(obj, words) |
|
403 | 402 | except TryNext: |
|
404 | 403 | pass |
|
405 | 404 | except Exception: |
|
406 | 405 | # Silence errors from completion function |
|
407 | 406 | #raise # dbg |
|
408 | 407 | pass |
|
409 | 408 | # Build match list to return |
|
410 | 409 | n = len(attr) |
|
411 | 410 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
412 | 411 | |
|
413 | 412 | |
|
414 | 413 | def get__all__entries(obj): |
|
415 | 414 | """returns the strings in the __all__ attribute""" |
|
416 | 415 | try: |
|
417 | 416 | words = getattr(obj, '__all__') |
|
418 | 417 | except: |
|
419 | 418 | return [] |
|
420 | 419 | |
|
421 | 420 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] |
|
422 | 421 | |
|
423 | 422 | |
|
424 | 423 | def match_dict_keys(keys, prefix, delims): |
|
425 | 424 | """Used by dict_key_matches, matching the prefix to a list of keys""" |
|
426 | 425 | if not prefix: |
|
427 | 426 | return None, 0, [repr(k) for k in keys |
|
428 | 427 | if isinstance(k, (string_types, bytes))] |
|
429 | 428 | quote_match = re.search('["\']', prefix) |
|
430 | 429 | quote = quote_match.group() |
|
431 | 430 | try: |
|
432 | 431 | prefix_str = eval(prefix + quote, {}) |
|
433 | 432 | except Exception: |
|
434 | 433 | return None, 0, [] |
|
435 | 434 | |
|
436 | 435 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
437 | 436 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
438 | 437 | token_start = token_match.start() |
|
439 | 438 | token_prefix = token_match.group() |
|
440 | 439 | |
|
441 | 440 | # TODO: support bytes in Py3k |
|
442 | 441 | matched = [] |
|
443 | 442 | for key in keys: |
|
444 | 443 | try: |
|
445 | 444 | if not key.startswith(prefix_str): |
|
446 | 445 | continue |
|
447 | 446 | except (AttributeError, TypeError, UnicodeError): |
|
448 | 447 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
449 | 448 | continue |
|
450 | 449 | |
|
451 | 450 | # reformat remainder of key to begin with prefix |
|
452 | 451 | rem = key[len(prefix_str):] |
|
453 | 452 | # force repr wrapped in ' |
|
454 | 453 | rem_repr = repr(rem + '"') |
|
455 | 454 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
456 | 455 | # Found key is unicode, but prefix is Py2 string. |
|
457 | 456 | # Therefore attempt to interpret key as string. |
|
458 | 457 | try: |
|
459 | 458 | rem_repr = repr(rem.encode('ascii') + '"') |
|
460 | 459 | except UnicodeEncodeError: |
|
461 | 460 | continue |
|
462 | 461 | |
|
463 | 462 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
464 | 463 | if quote == '"': |
|
465 | 464 | # The entered prefix is quoted with ", |
|
466 | 465 | # but the match is quoted with '. |
|
467 | 466 | # A contained " hence needs escaping for comparison: |
|
468 | 467 | rem_repr = rem_repr.replace('"', '\\"') |
|
469 | 468 | |
|
470 | 469 | # then reinsert prefix from start of token |
|
471 | 470 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
472 | 471 | return quote, token_start, matched |
|
473 | 472 | |
|
474 | 473 | |
|
475 | 474 | def _safe_isinstance(obj, module, class_name): |
|
476 | 475 | """Checks if obj is an instance of module.class_name if loaded |
|
477 | 476 | """ |
|
478 | 477 | return (module in sys.modules and |
|
479 | 478 | isinstance(obj, getattr(__import__(module), class_name))) |
|
480 | 479 | |
|
481 | 480 | |
|
482 | 481 | def back_unicode_name_matches(text): |
|
483 | 482 | u"""Match unicode characters back to unicode name |
|
484 | 483 | |
|
485 | 484 | This does ☃ -> \\snowman |
|
486 | 485 | |
|
487 | 486 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
488 | 487 | Though it will not recombine back to the snowman character by the completion machinery. |
|
489 | 488 | |
|
490 | 489 | This will not either back-complete standard sequences like \\n, \\b ... |
|
491 | 490 | |
|
492 | 491 | Used on Python 3 only. |
|
493 | 492 | """ |
|
494 | 493 | if len(text)<2: |
|
495 | 494 | return u'', () |
|
496 | 495 | maybe_slash = text[-2] |
|
497 | 496 | if maybe_slash != '\\': |
|
498 | 497 | return u'', () |
|
499 | 498 | |
|
500 | 499 | char = text[-1] |
|
501 | 500 | # no expand on quote for completion in strings. |
|
502 | 501 | # nor backcomplete standard ascii keys |
|
503 | 502 | if char in string.ascii_letters or char in ['"',"'"]: |
|
504 | 503 | return u'', () |
|
505 | 504 | try : |
|
506 | 505 | unic = unicodedata.name(char) |
|
507 | 506 | return '\\'+char,['\\'+unic] |
|
508 |
except KeyError |
|
|
507 | except KeyError: | |
|
509 | 508 | pass |
|
510 | 509 | return u'', () |
|
511 | 510 | |
|
512 | 511 | def back_latex_name_matches(text): |
|
513 | 512 | u"""Match latex characters back to unicode name |
|
514 | 513 | |
|
515 | 514 | This does ->\\sqrt |
|
516 | 515 | |
|
517 | 516 | Used on Python 3 only. |
|
518 | 517 | """ |
|
519 | 518 | if len(text)<2: |
|
520 | 519 | return u'', () |
|
521 | 520 | maybe_slash = text[-2] |
|
522 | 521 | if maybe_slash != '\\': |
|
523 | 522 | return u'', () |
|
524 | 523 | |
|
525 | 524 | |
|
526 | 525 | char = text[-1] |
|
527 | 526 | # no expand on quote for completion in strings. |
|
528 | 527 | # nor backcomplete standard ascii keys |
|
529 | 528 | if char in string.ascii_letters or char in ['"',"'"]: |
|
530 | 529 | return u'', () |
|
531 | 530 | try : |
|
532 | 531 | latex = reverse_latex_symbol[char] |
|
533 | 532 | # '\\' replace the \ as well |
|
534 | 533 | return '\\'+char,[latex] |
|
535 |
except KeyError |
|
|
534 | except KeyError: | |
|
536 | 535 | pass |
|
537 | 536 | return u'', () |
|
538 | 537 | |
|
539 | 538 | |
|
540 | 539 | class IPCompleter(Completer): |
|
541 | 540 | """Extension of the completer class with IPython-specific features""" |
|
542 | ||
|
543 | def _greedy_changed(self, name, old, new): | |
|
541 | ||
|
542 | @observe('greedy') | |
|
543 | def _greedy_changed(self, change): | |
|
544 | 544 | """update the splitter and readline delims when greedy is changed""" |
|
545 | if new: | |
|
545 | if change['new']: | |
|
546 | 546 | self.splitter.delims = GREEDY_DELIMS |
|
547 | 547 | else: |
|
548 | 548 | self.splitter.delims = DELIMS |
|
549 | 549 | |
|
550 | 550 | if self.readline: |
|
551 | 551 | self.readline.set_completer_delims(self.splitter.delims) |
|
552 | 552 | |
|
553 |
merge_completions = |
|
|
553 | merge_completions = Bool(True, | |
|
554 | 554 | help="""Whether to merge completion results into a single list |
|
555 | 555 | |
|
556 | 556 | If False, only the completion results from the first non-empty |
|
557 | 557 | completer will be returned. |
|
558 | 558 | """ |
|
559 | ) | |
|
560 |
omit__names = Enum((0,1,2), default_value=2, |
|
|
559 | ).tag(config=True) | |
|
560 | omit__names = Enum((0,1,2), default_value=2, | |
|
561 | 561 | help="""Instruct the completer to omit private method names |
|
562 | 562 | |
|
563 | 563 | Specifically, when completing on ``object.<tab>``. |
|
564 | 564 | |
|
565 | 565 | When 2 [default]: all names that start with '_' will be excluded. |
|
566 | 566 | |
|
567 | 567 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
568 | 568 | |
|
569 | 569 | When 0: nothing will be excluded. |
|
570 | 570 | """ |
|
571 | ) | |
|
572 |
limit_to__all__ = |
|
|
571 | ).tag(config=True) | |
|
572 | limit_to__all__ = Bool(False, | |
|
573 | 573 | help=""" |
|
574 | 574 | DEPRECATED as of version 5.0. |
|
575 | 575 | |
|
576 | 576 | Instruct the completer to use __all__ for the completion |
|
577 | 577 | |
|
578 | 578 | Specifically, when completing on ``object.<tab>``. |
|
579 | 579 | |
|
580 | 580 | When True: only those names in obj.__all__ will be included. |
|
581 | 581 | |
|
582 | 582 | When False [default]: the __all__ attribute is ignored |
|
583 | """ | |
|
584 | ) | |
|
583 | """, | |
|
584 | ).tag(config=True) | |
|
585 | 585 | |
|
586 | 586 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
587 | 587 | use_readline=True, config=None, **kwargs): |
|
588 | 588 | """IPCompleter() -> completer |
|
589 | 589 | |
|
590 | 590 | Return a completer object suitable for use by the readline library |
|
591 | 591 | via readline.set_completer(). |
|
592 | 592 | |
|
593 | 593 | Inputs: |
|
594 | 594 | |
|
595 | 595 | - shell: a pointer to the ipython shell itself. This is needed |
|
596 | 596 | because this completer knows about magic functions, and those can |
|
597 | 597 | only be accessed via the ipython instance. |
|
598 | 598 | |
|
599 | 599 | - namespace: an optional dict where completions are performed. |
|
600 | 600 | |
|
601 | 601 | - global_namespace: secondary optional dict for completions, to |
|
602 | 602 | handle cases (such as IPython embedded inside functions) where |
|
603 | 603 | both Python scopes are visible. |
|
604 | 604 | |
|
605 | 605 | use_readline : bool, optional |
|
606 | 606 | If true, use the readline library. This completer can still function |
|
607 | 607 | without readline, though in that case callers must provide some extra |
|
608 | 608 | information on each call about the current line.""" |
|
609 | 609 | |
|
610 | 610 | self.magic_escape = ESC_MAGIC |
|
611 | 611 | self.splitter = CompletionSplitter() |
|
612 | 612 | |
|
613 | 613 | # Readline configuration, only used by the rlcompleter method. |
|
614 | 614 | if use_readline: |
|
615 | 615 | # We store the right version of readline so that later code |
|
616 | 616 | import IPython.utils.rlineimpl as readline |
|
617 | 617 | self.readline = readline |
|
618 | 618 | else: |
|
619 | 619 | self.readline = None |
|
620 | 620 | |
|
621 | 621 | # _greedy_changed() depends on splitter and readline being defined: |
|
622 | 622 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
623 | 623 | config=config, **kwargs) |
|
624 | 624 | |
|
625 | 625 | # List where completion matches will be stored |
|
626 | 626 | self.matches = [] |
|
627 | 627 | self.shell = shell |
|
628 | 628 | # Regexp to split filenames with spaces in them |
|
629 | 629 | self.space_name_re = re.compile(r'([^\\] )') |
|
630 | 630 | # Hold a local ref. to glob.glob for speed |
|
631 | 631 | self.glob = glob.glob |
|
632 | 632 | |
|
633 | 633 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
634 | 634 | # buffers, to avoid completion problems. |
|
635 | 635 | term = os.environ.get('TERM','xterm') |
|
636 | 636 | self.dumb_terminal = term in ['dumb','emacs'] |
|
637 | 637 | |
|
638 | 638 | # Special handling of backslashes needed in win32 platforms |
|
639 | 639 | if sys.platform == "win32": |
|
640 | 640 | self.clean_glob = self._clean_glob_win32 |
|
641 | 641 | else: |
|
642 | 642 | self.clean_glob = self._clean_glob |
|
643 | 643 | |
|
644 | 644 | #regexp to parse docstring for function signature |
|
645 | 645 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
646 | 646 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
647 | 647 | #use this if positional argument name is also needed |
|
648 | 648 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
649 | 649 | |
|
650 | 650 | # All active matcher routines for completion |
|
651 | 651 | self.matchers = [ |
|
652 | 652 | self.file_matches, |
|
653 | 653 | self.magic_matches, |
|
654 | 654 | self.python_func_kw_matches, |
|
655 | 655 | self.dict_key_matches, |
|
656 | 656 | ] |
|
657 | 657 | |
|
658 | 658 | def all_completions(self, text): |
|
659 | 659 | """ |
|
660 | 660 | Wrapper around the complete method for the benefit of emacs |
|
661 | 661 | and pydb. |
|
662 | 662 | """ |
|
663 | 663 | return self.complete(text)[1] |
|
664 | 664 | |
|
665 | 665 | def _clean_glob(self, text): |
|
666 | 666 | return self.glob("%s*" % text) |
|
667 | 667 | |
|
668 | 668 | def _clean_glob_win32(self,text): |
|
669 | 669 | return [f.replace("\\","/") |
|
670 | 670 | for f in self.glob("%s*" % text)] |
|
671 | 671 | |
|
672 | 672 | def file_matches(self, text): |
|
673 | 673 | """Match filenames, expanding ~USER type strings. |
|
674 | 674 | |
|
675 | 675 | Most of the seemingly convoluted logic in this completer is an |
|
676 | 676 | attempt to handle filenames with spaces in them. And yet it's not |
|
677 | 677 | quite perfect, because Python's readline doesn't expose all of the |
|
678 | 678 | GNU readline details needed for this to be done correctly. |
|
679 | 679 | |
|
680 | 680 | For a filename with a space in it, the printed completions will be |
|
681 | 681 | only the parts after what's already been typed (instead of the |
|
682 | 682 | full completions, as is normally done). I don't think with the |
|
683 | 683 | current (as of Python 2.3) Python readline it's possible to do |
|
684 | 684 | better.""" |
|
685 | 685 | |
|
686 | 686 | # chars that require escaping with backslash - i.e. chars |
|
687 | 687 | # that readline treats incorrectly as delimiters, but we |
|
688 | 688 | # don't want to treat as delimiters in filename matching |
|
689 | 689 | # when escaped with backslash |
|
690 | 690 | if text.startswith('!'): |
|
691 | 691 | text = text[1:] |
|
692 | 692 | text_prefix = u'!' |
|
693 | 693 | else: |
|
694 | 694 | text_prefix = u'' |
|
695 | 695 | |
|
696 | 696 | text_until_cursor = self.text_until_cursor |
|
697 | 697 | # track strings with open quotes |
|
698 | 698 | open_quotes = has_open_quotes(text_until_cursor) |
|
699 | 699 | |
|
700 | 700 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
701 | 701 | lsplit = text |
|
702 | 702 | else: |
|
703 | 703 | try: |
|
704 | 704 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
705 | 705 | lsplit = arg_split(text_until_cursor)[-1] |
|
706 | 706 | except ValueError: |
|
707 | 707 | # typically an unmatched ", or backslash without escaped char. |
|
708 | 708 | if open_quotes: |
|
709 | 709 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
710 | 710 | else: |
|
711 | 711 | return [] |
|
712 | 712 | except IndexError: |
|
713 | 713 | # tab pressed on empty line |
|
714 | 714 | lsplit = "" |
|
715 | 715 | |
|
716 | 716 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
717 | 717 | # if protectables are found, do matching on the whole escaped name |
|
718 | 718 | has_protectables = True |
|
719 | 719 | text0,text = text,lsplit |
|
720 | 720 | else: |
|
721 | 721 | has_protectables = False |
|
722 | 722 | text = os.path.expanduser(text) |
|
723 | 723 | |
|
724 | 724 | if text == "": |
|
725 | 725 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] |
|
726 | 726 | |
|
727 | 727 | # Compute the matches from the filesystem |
|
728 | 728 | m0 = self.clean_glob(text.replace('\\','')) |
|
729 | 729 | |
|
730 | 730 | if has_protectables: |
|
731 | 731 | # If we had protectables, we need to revert our changes to the |
|
732 | 732 | # beginning of filename so that we don't double-write the part |
|
733 | 733 | # of the filename we have so far |
|
734 | 734 | len_lsplit = len(lsplit) |
|
735 | 735 | matches = [text_prefix + text0 + |
|
736 | 736 | protect_filename(f[len_lsplit:]) for f in m0] |
|
737 | 737 | else: |
|
738 | 738 | if open_quotes: |
|
739 | 739 | # if we have a string with an open quote, we don't need to |
|
740 | 740 | # protect the names at all (and we _shouldn't_, as it |
|
741 | 741 | # would cause bugs when the filesystem call is made). |
|
742 | 742 | matches = m0 |
|
743 | 743 | else: |
|
744 | 744 | matches = [text_prefix + |
|
745 | 745 | protect_filename(f) for f in m0] |
|
746 | 746 | |
|
747 | 747 | # Mark directories in input list by appending '/' to their names. |
|
748 | 748 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] |
|
749 | 749 | |
|
750 | 750 | def magic_matches(self, text): |
|
751 | 751 | """Match magics""" |
|
752 | 752 | # Get all shell magics now rather than statically, so magics loaded at |
|
753 | 753 | # runtime show up too. |
|
754 | 754 | lsm = self.shell.magics_manager.lsmagic() |
|
755 | 755 | line_magics = lsm['line'] |
|
756 | 756 | cell_magics = lsm['cell'] |
|
757 | 757 | pre = self.magic_escape |
|
758 | 758 | pre2 = pre+pre |
|
759 | 759 | |
|
760 | 760 | # Completion logic: |
|
761 | 761 | # - user gives %%: only do cell magics |
|
762 | 762 | # - user gives %: do both line and cell magics |
|
763 | 763 | # - no prefix: do both |
|
764 | 764 | # In other words, line magics are skipped if the user gives %% explicitly |
|
765 | 765 | bare_text = text.lstrip(pre) |
|
766 | 766 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
767 | 767 | if not text.startswith(pre2): |
|
768 | 768 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
769 | 769 | return [cast_unicode_py2(c) for c in comp] |
|
770 | 770 | |
|
771 | 771 | def python_jedi_matches(self, text, line_buffer, cursor_pos): |
|
772 | 772 | """Match attributes or global Python names using Jedi.""" |
|
773 | 773 | if line_buffer.startswith('aimport ') or line_buffer.startswith('%aimport '): |
|
774 | 774 | return () |
|
775 | 775 | namespaces = [] |
|
776 | 776 | if self.namespace is None: |
|
777 | 777 | import __main__ |
|
778 | 778 | namespaces.append(__main__.__dict__) |
|
779 | 779 | else: |
|
780 | 780 | namespaces.append(self.namespace) |
|
781 | 781 | if self.global_namespace is not None: |
|
782 | 782 | namespaces.append(self.global_namespace) |
|
783 | 783 | |
|
784 | 784 | # cursor_pos is an it, jedi wants line and column |
|
785 | 785 | |
|
786 | 786 | interpreter = jedi.Interpreter(line_buffer, namespaces, column=cursor_pos) |
|
787 | 787 | path = jedi.parser.user_context.UserContext(line_buffer, \ |
|
788 | 788 | (1, len(line_buffer))).get_path_until_cursor() |
|
789 | 789 | path, dot, like = jedi.api.helpers.completion_parts(path) |
|
790 | 790 | if text.startswith('.'): |
|
791 | 791 | # text will be `.` on completions like `a[0].<tab>` |
|
792 | 792 | before = dot |
|
793 | 793 | else: |
|
794 | 794 | before = line_buffer[:len(line_buffer) - len(like)] |
|
795 | 795 | |
|
796 | 796 | |
|
797 | 797 | def trim_start(completion): |
|
798 | 798 | """completions need to start with `text`, trim the beginning until it does""" |
|
799 | 799 | ltext = text.lower() |
|
800 | 800 | lcomp = completion.lower() |
|
801 | 801 | if ltext in lcomp and not (lcomp.startswith(ltext)): |
|
802 | 802 | start_index = lcomp.index(ltext) |
|
803 | 803 | if cursor_pos: |
|
804 | 804 | if start_index >= cursor_pos: |
|
805 | 805 | start_index = min(start_index, cursor_pos) |
|
806 | 806 | return completion[start_index:] |
|
807 | 807 | return completion |
|
808 | 808 | |
|
809 | 809 | completions = interpreter.completions() |
|
810 | 810 | |
|
811 | 811 | completion_text = [c.name_with_symbols for c in completions] |
|
812 | 812 | |
|
813 | 813 | if self.omit__names: |
|
814 | 814 | if self.omit__names == 1: |
|
815 | 815 | # true if txt is _not_ a __ name, false otherwise: |
|
816 | 816 | no__name = lambda txt: not txt.startswith('__') |
|
817 | 817 | else: |
|
818 | 818 | # true if txt is _not_ a _ name, false otherwise: |
|
819 | 819 | no__name = lambda txt: not txt.startswith('_') |
|
820 | 820 | completion_text = filter(no__name, completion_text) |
|
821 | 821 | |
|
822 | 822 | |
|
823 | 823 | return [trim_start(before + c_text) for c_text in completion_text] |
|
824 | 824 | |
|
825 | 825 | |
|
826 | 826 | def python_matches(self, text): |
|
827 | 827 | """Match attributes or global python names""" |
|
828 | 828 | if "." in text: |
|
829 | 829 | try: |
|
830 | 830 | matches = self.attr_matches(text) |
|
831 | 831 | if text.endswith('.') and self.omit__names: |
|
832 | 832 | if self.omit__names == 1: |
|
833 | 833 | # true if txt is _not_ a __ name, false otherwise: |
|
834 | 834 | no__name = (lambda txt: |
|
835 | 835 | re.match(r'.*\.__.*?__',txt) is None) |
|
836 | 836 | else: |
|
837 | 837 | # true if txt is _not_ a _ name, false otherwise: |
|
838 | 838 | no__name = (lambda txt: |
|
839 | 839 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
840 | 840 | matches = filter(no__name, matches) |
|
841 | 841 | except NameError: |
|
842 | 842 | # catches <undefined attributes>.<tab> |
|
843 | 843 | matches = [] |
|
844 | 844 | else: |
|
845 | 845 | matches = self.global_matches(text) |
|
846 | 846 | return matches |
|
847 | 847 | |
|
848 | 848 | def _default_arguments_from_docstring(self, doc): |
|
849 | 849 | """Parse the first line of docstring for call signature. |
|
850 | 850 | |
|
851 | 851 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
852 | 852 | It can also parse cython docstring of the form |
|
853 | 853 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
854 | 854 | """ |
|
855 | 855 | if doc is None: |
|
856 | 856 | return [] |
|
857 | 857 | |
|
858 | 858 | #care only the firstline |
|
859 | 859 | line = doc.lstrip().splitlines()[0] |
|
860 | 860 | |
|
861 | 861 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
862 | 862 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
863 | 863 | sig = self.docstring_sig_re.search(line) |
|
864 | 864 | if sig is None: |
|
865 | 865 | return [] |
|
866 | 866 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
867 | 867 | sig = sig.groups()[0].split(',') |
|
868 | 868 | ret = [] |
|
869 | 869 | for s in sig: |
|
870 | 870 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
871 | 871 | ret += self.docstring_kwd_re.findall(s) |
|
872 | 872 | return ret |
|
873 | 873 | |
|
874 | 874 | def _default_arguments(self, obj): |
|
875 | 875 | """Return the list of default arguments of obj if it is callable, |
|
876 | 876 | or empty list otherwise.""" |
|
877 | 877 | call_obj = obj |
|
878 | 878 | ret = [] |
|
879 | 879 | if inspect.isbuiltin(obj): |
|
880 | 880 | pass |
|
881 | 881 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
882 | 882 | if inspect.isclass(obj): |
|
883 | 883 | #for cython embededsignature=True the constructor docstring |
|
884 | 884 | #belongs to the object itself not __init__ |
|
885 | 885 | ret += self._default_arguments_from_docstring( |
|
886 | 886 | getattr(obj, '__doc__', '')) |
|
887 | 887 | # for classes, check for __init__,__new__ |
|
888 | 888 | call_obj = (getattr(obj, '__init__', None) or |
|
889 | 889 | getattr(obj, '__new__', None)) |
|
890 | 890 | # for all others, check if they are __call__able |
|
891 | 891 | elif hasattr(obj, '__call__'): |
|
892 | 892 | call_obj = obj.__call__ |
|
893 | 893 | ret += self._default_arguments_from_docstring( |
|
894 | 894 | getattr(call_obj, '__doc__', '')) |
|
895 | 895 | |
|
896 | 896 | if PY3: |
|
897 | 897 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
898 | 898 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
899 | 899 | signature = inspect.signature |
|
900 | 900 | else: |
|
901 | 901 | import IPython.utils.signatures |
|
902 | 902 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, |
|
903 | 903 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) |
|
904 | 904 | signature = IPython.utils.signatures.signature |
|
905 | 905 | |
|
906 | 906 | try: |
|
907 | 907 | sig = signature(call_obj) |
|
908 | 908 | ret.extend(k for k, v in sig.parameters.items() if |
|
909 | 909 | v.kind in _keeps) |
|
910 | 910 | except ValueError: |
|
911 | 911 | pass |
|
912 | 912 | |
|
913 | 913 | return list(set(ret)) |
|
914 | 914 | |
|
915 | 915 | def python_func_kw_matches(self,text): |
|
916 | 916 | """Match named parameters (kwargs) of the last open function""" |
|
917 | 917 | |
|
918 | 918 | if "." in text: # a parameter cannot be dotted |
|
919 | 919 | return [] |
|
920 | 920 | try: regexp = self.__funcParamsRegex |
|
921 | 921 | except AttributeError: |
|
922 | 922 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
923 | 923 | '.*?(?<!\\)' | # single quoted strings or |
|
924 | 924 | ".*?(?<!\\)" | # double quoted strings or |
|
925 | 925 | \w+ | # identifier |
|
926 | 926 | \S # other characters |
|
927 | 927 | ''', re.VERBOSE | re.DOTALL) |
|
928 | 928 | # 1. find the nearest identifier that comes before an unclosed |
|
929 | 929 | # parenthesis before the cursor |
|
930 | 930 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
931 | 931 | tokens = regexp.findall(self.text_until_cursor) |
|
932 | 932 | tokens.reverse() |
|
933 | 933 | iterTokens = iter(tokens); openPar = 0 |
|
934 | 934 | |
|
935 | 935 | for token in iterTokens: |
|
936 | 936 | if token == ')': |
|
937 | 937 | openPar -= 1 |
|
938 | 938 | elif token == '(': |
|
939 | 939 | openPar += 1 |
|
940 | 940 | if openPar > 0: |
|
941 | 941 | # found the last unclosed parenthesis |
|
942 | 942 | break |
|
943 | 943 | else: |
|
944 | 944 | return [] |
|
945 | 945 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
946 | 946 | ids = [] |
|
947 | 947 | isId = re.compile(r'\w+$').match |
|
948 | 948 | |
|
949 | 949 | while True: |
|
950 | 950 | try: |
|
951 | 951 | ids.append(next(iterTokens)) |
|
952 | 952 | if not isId(ids[-1]): |
|
953 | 953 | ids.pop(); break |
|
954 | 954 | if not next(iterTokens) == '.': |
|
955 | 955 | break |
|
956 | 956 | except StopIteration: |
|
957 | 957 | break |
|
958 | 958 | # lookup the candidate callable matches either using global_matches |
|
959 | 959 | # or attr_matches for dotted names |
|
960 | 960 | if len(ids) == 1: |
|
961 | 961 | callableMatches = self.global_matches(ids[0]) |
|
962 | 962 | else: |
|
963 | 963 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
964 | 964 | argMatches = [] |
|
965 | 965 | for callableMatch in callableMatches: |
|
966 | 966 | try: |
|
967 | 967 | namedArgs = self._default_arguments(eval(callableMatch, |
|
968 | 968 | self.namespace)) |
|
969 | 969 | except: |
|
970 | 970 | continue |
|
971 | 971 | |
|
972 | 972 | for namedArg in namedArgs: |
|
973 | 973 | if namedArg.startswith(text): |
|
974 | 974 | argMatches.append(u"%s=" %namedArg) |
|
975 | 975 | return argMatches |
|
976 | 976 | |
|
977 | 977 | def dict_key_matches(self, text): |
|
978 | 978 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
979 | 979 | def get_keys(obj): |
|
980 | 980 | # Objects can define their own completions by defining an |
|
981 | 981 | # _ipy_key_completions_() method. |
|
982 | 982 | method = get_real_method(obj, '_ipython_key_completions_') |
|
983 | 983 | if method is not None: |
|
984 | 984 | return method() |
|
985 | 985 | |
|
986 | 986 | # Special case some common in-memory dict-like types |
|
987 | 987 | if isinstance(obj, dict) or\ |
|
988 | 988 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
989 | 989 | try: |
|
990 | 990 | return list(obj.keys()) |
|
991 | 991 | except Exception: |
|
992 | 992 | return [] |
|
993 | 993 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
994 | 994 | _safe_isinstance(obj, 'numpy', 'void'): |
|
995 | 995 | return obj.dtype.names or [] |
|
996 | 996 | return [] |
|
997 | 997 | |
|
998 | 998 | try: |
|
999 | 999 | regexps = self.__dict_key_regexps |
|
1000 | 1000 | except AttributeError: |
|
1001 | 1001 | dict_key_re_fmt = r'''(?x) |
|
1002 | 1002 | ( # match dict-referring expression wrt greedy setting |
|
1003 | 1003 | %s |
|
1004 | 1004 | ) |
|
1005 | 1005 | \[ # open bracket |
|
1006 | 1006 | \s* # and optional whitespace |
|
1007 | 1007 | ([uUbB]? # string prefix (r not handled) |
|
1008 | 1008 | (?: # unclosed string |
|
1009 | 1009 | '(?:[^']|(?<!\\)\\')* |
|
1010 | 1010 | | |
|
1011 | 1011 | "(?:[^"]|(?<!\\)\\")* |
|
1012 | 1012 | ) |
|
1013 | 1013 | )? |
|
1014 | 1014 | $ |
|
1015 | 1015 | ''' |
|
1016 | 1016 | regexps = self.__dict_key_regexps = { |
|
1017 | 1017 | False: re.compile(dict_key_re_fmt % ''' |
|
1018 | 1018 | # identifiers separated by . |
|
1019 | 1019 | (?!\d)\w+ |
|
1020 | 1020 | (?:\.(?!\d)\w+)* |
|
1021 | 1021 | '''), |
|
1022 | 1022 | True: re.compile(dict_key_re_fmt % ''' |
|
1023 | 1023 | .+ |
|
1024 | 1024 | ''') |
|
1025 | 1025 | } |
|
1026 | 1026 | |
|
1027 | 1027 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
1028 | 1028 | if match is None: |
|
1029 | 1029 | return [] |
|
1030 | 1030 | |
|
1031 | 1031 | expr, prefix = match.groups() |
|
1032 | 1032 | try: |
|
1033 | 1033 | obj = eval(expr, self.namespace) |
|
1034 | 1034 | except Exception: |
|
1035 | 1035 | try: |
|
1036 | 1036 | obj = eval(expr, self.global_namespace) |
|
1037 | 1037 | except Exception: |
|
1038 | 1038 | return [] |
|
1039 | 1039 | |
|
1040 | 1040 | keys = get_keys(obj) |
|
1041 | 1041 | if not keys: |
|
1042 | 1042 | return keys |
|
1043 | 1043 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
1044 | 1044 | if not matches: |
|
1045 | 1045 | return matches |
|
1046 | 1046 | |
|
1047 | 1047 | # get the cursor position of |
|
1048 | 1048 | # - the text being completed |
|
1049 | 1049 | # - the start of the key text |
|
1050 | 1050 | # - the start of the completion |
|
1051 | 1051 | text_start = len(self.text_until_cursor) - len(text) |
|
1052 | 1052 | if prefix: |
|
1053 | 1053 | key_start = match.start(2) |
|
1054 | 1054 | completion_start = key_start + token_offset |
|
1055 | 1055 | else: |
|
1056 | 1056 | key_start = completion_start = match.end() |
|
1057 | 1057 | |
|
1058 | 1058 | # grab the leading prefix, to make sure all completions start with `text` |
|
1059 | 1059 | if text_start > key_start: |
|
1060 | 1060 | leading = '' |
|
1061 | 1061 | else: |
|
1062 | 1062 | leading = text[text_start:completion_start] |
|
1063 | 1063 | |
|
1064 | 1064 | # the index of the `[` character |
|
1065 | 1065 | bracket_idx = match.end(1) |
|
1066 | 1066 | |
|
1067 | 1067 | # append closing quote and bracket as appropriate |
|
1068 | 1068 | # this is *not* appropriate if the opening quote or bracket is outside |
|
1069 | 1069 | # the text given to this method |
|
1070 | 1070 | suf = '' |
|
1071 | 1071 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
1072 | 1072 | if key_start > text_start and closing_quote: |
|
1073 | 1073 | # quotes were opened inside text, maybe close them |
|
1074 | 1074 | if continuation.startswith(closing_quote): |
|
1075 | 1075 | continuation = continuation[len(closing_quote):] |
|
1076 | 1076 | else: |
|
1077 | 1077 | suf += closing_quote |
|
1078 | 1078 | if bracket_idx > text_start: |
|
1079 | 1079 | # brackets were opened inside text, maybe close them |
|
1080 | 1080 | if not continuation.startswith(']'): |
|
1081 | 1081 | suf += ']' |
|
1082 | 1082 | |
|
1083 | 1083 | return [leading + k + suf for k in matches] |
|
1084 | 1084 | |
|
1085 | 1085 | def unicode_name_matches(self, text): |
|
1086 | 1086 | u"""Match Latex-like syntax for unicode characters base |
|
1087 | 1087 | on the name of the character. |
|
1088 | 1088 | |
|
1089 | 1089 | This does \\GREEK SMALL LETTER ETA -> η |
|
1090 | 1090 | |
|
1091 | 1091 | Works only on valid python 3 identifier, or on combining characters that |
|
1092 | 1092 | will combine to form a valid identifier. |
|
1093 | 1093 | |
|
1094 | 1094 | Used on Python 3 only. |
|
1095 | 1095 | """ |
|
1096 | 1096 | slashpos = text.rfind('\\') |
|
1097 | 1097 | if slashpos > -1: |
|
1098 | 1098 | s = text[slashpos+1:] |
|
1099 | 1099 | try : |
|
1100 | 1100 | unic = unicodedata.lookup(s) |
|
1101 | 1101 | # allow combining chars |
|
1102 | 1102 | if ('a'+unic).isidentifier(): |
|
1103 | 1103 | return '\\'+s,[unic] |
|
1104 |
except KeyError |
|
|
1104 | except KeyError: | |
|
1105 | 1105 | pass |
|
1106 | 1106 | return u'', [] |
|
1107 | 1107 | |
|
1108 | 1108 | |
|
1109 | 1109 | |
|
1110 | 1110 | |
|
1111 | 1111 | def latex_matches(self, text): |
|
1112 | 1112 | u"""Match Latex syntax for unicode characters. |
|
1113 | 1113 | |
|
1114 | 1114 | This does both \\alp -> \\alpha and \\alpha -> α |
|
1115 | 1115 | |
|
1116 | 1116 | Used on Python 3 only. |
|
1117 | 1117 | """ |
|
1118 | 1118 | slashpos = text.rfind('\\') |
|
1119 | 1119 | if slashpos > -1: |
|
1120 | 1120 | s = text[slashpos:] |
|
1121 | 1121 | if s in latex_symbols: |
|
1122 | 1122 | # Try to complete a full latex symbol to unicode |
|
1123 | 1123 | # \\alpha -> α |
|
1124 | 1124 | return s, [latex_symbols[s]] |
|
1125 | 1125 | else: |
|
1126 | 1126 | # If a user has partially typed a latex symbol, give them |
|
1127 | 1127 | # a full list of options \al -> [\aleph, \alpha] |
|
1128 | 1128 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1129 | 1129 | return s, matches |
|
1130 | 1130 | return u'', [] |
|
1131 | 1131 | |
|
1132 | 1132 | def dispatch_custom_completer(self, text): |
|
1133 | 1133 | line = self.line_buffer |
|
1134 | 1134 | if not line.strip(): |
|
1135 | 1135 | return None |
|
1136 | 1136 | |
|
1137 | 1137 | # Create a little structure to pass all the relevant information about |
|
1138 | 1138 | # the current completion to any custom completer. |
|
1139 | 1139 | event = Bunch() |
|
1140 | 1140 | event.line = line |
|
1141 | 1141 | event.symbol = text |
|
1142 | 1142 | cmd = line.split(None,1)[0] |
|
1143 | 1143 | event.command = cmd |
|
1144 | 1144 | event.text_until_cursor = self.text_until_cursor |
|
1145 | 1145 | |
|
1146 | 1146 | # for foo etc, try also to find completer for %foo |
|
1147 | 1147 | if not cmd.startswith(self.magic_escape): |
|
1148 | 1148 | try_magic = self.custom_completers.s_matches( |
|
1149 | 1149 | self.magic_escape + cmd) |
|
1150 | 1150 | else: |
|
1151 | 1151 | try_magic = [] |
|
1152 | 1152 | |
|
1153 | 1153 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1154 | 1154 | try_magic, |
|
1155 | 1155 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1156 | 1156 | try: |
|
1157 | 1157 | res = c(event) |
|
1158 | 1158 | if res: |
|
1159 | 1159 | # first, try case sensitive match |
|
1160 | 1160 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] |
|
1161 | 1161 | if withcase: |
|
1162 | 1162 | return withcase |
|
1163 | 1163 | # if none, then case insensitive ones are ok too |
|
1164 | 1164 | text_low = text.lower() |
|
1165 | 1165 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] |
|
1166 | 1166 | except TryNext: |
|
1167 | 1167 | pass |
|
1168 | 1168 | |
|
1169 | 1169 | return None |
|
1170 | 1170 | |
|
1171 | 1171 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1172 | 1172 | """Find completions for the given text and line context. |
|
1173 | 1173 | |
|
1174 | 1174 | Note that both the text and the line_buffer are optional, but at least |
|
1175 | 1175 | one of them must be given. |
|
1176 | 1176 | |
|
1177 | 1177 | Parameters |
|
1178 | 1178 | ---------- |
|
1179 | 1179 | text : string, optional |
|
1180 | 1180 | Text to perform the completion on. If not given, the line buffer |
|
1181 | 1181 | is split using the instance's CompletionSplitter object. |
|
1182 | 1182 | |
|
1183 | 1183 | line_buffer : string, optional |
|
1184 | 1184 | If not given, the completer attempts to obtain the current line |
|
1185 | 1185 | buffer via readline. This keyword allows clients which are |
|
1186 | 1186 | requesting for text completions in non-readline contexts to inform |
|
1187 | 1187 | the completer of the entire text. |
|
1188 | 1188 | |
|
1189 | 1189 | cursor_pos : int, optional |
|
1190 | 1190 | Index of the cursor in the full line buffer. Should be provided by |
|
1191 | 1191 | remote frontends where kernel has no access to frontend state. |
|
1192 | 1192 | |
|
1193 | 1193 | Returns |
|
1194 | 1194 | ------- |
|
1195 | 1195 | text : str |
|
1196 | 1196 | Text that was actually used in the completion. |
|
1197 | 1197 | |
|
1198 | 1198 | matches : list |
|
1199 | 1199 | A list of completion matches. |
|
1200 | 1200 | """ |
|
1201 | 1201 | # if the cursor position isn't given, the only sane assumption we can |
|
1202 | 1202 | # make is that it's at the end of the line (the common case) |
|
1203 | 1203 | if cursor_pos is None: |
|
1204 | 1204 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1205 | 1205 | |
|
1206 | 1206 | if PY3: |
|
1207 | 1207 | |
|
1208 | 1208 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1209 | 1209 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1210 | 1210 | if latex_matches: |
|
1211 | 1211 | return latex_text, latex_matches |
|
1212 | 1212 | name_text = '' |
|
1213 | 1213 | name_matches = [] |
|
1214 | 1214 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1215 | 1215 | name_text, name_matches = meth(base_text) |
|
1216 | 1216 | if name_text: |
|
1217 | 1217 | return name_text, name_matches |
|
1218 | 1218 | |
|
1219 | 1219 | # if text is either None or an empty string, rely on the line buffer |
|
1220 | 1220 | if not text: |
|
1221 | 1221 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1222 | 1222 | |
|
1223 | 1223 | # If no line buffer is given, assume the input text is all there was |
|
1224 | 1224 | if line_buffer is None: |
|
1225 | 1225 | line_buffer = text |
|
1226 | 1226 | |
|
1227 | 1227 | self.line_buffer = line_buffer |
|
1228 | 1228 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1229 | 1229 | |
|
1230 | 1230 | # Start with a clean slate of completions |
|
1231 | 1231 | self.matches[:] = [] |
|
1232 | 1232 | custom_res = self.dispatch_custom_completer(text) |
|
1233 | 1233 | if custom_res is not None: |
|
1234 | 1234 | # did custom completers produce something? |
|
1235 | 1235 | self.matches = custom_res |
|
1236 | 1236 | else: |
|
1237 | 1237 | # Extend the list of completions with the results of each |
|
1238 | 1238 | # matcher, so we return results to the user from all |
|
1239 | 1239 | # namespaces. |
|
1240 | 1240 | if self.merge_completions: |
|
1241 | 1241 | self.matches = [] |
|
1242 | 1242 | for matcher in self.matchers: |
|
1243 | 1243 | try: |
|
1244 | 1244 | self.matches.extend(matcher(text)) |
|
1245 | 1245 | except: |
|
1246 | 1246 | # Show the ugly traceback if the matcher causes an |
|
1247 | 1247 | # exception, but do NOT crash the kernel! |
|
1248 | 1248 | sys.excepthook(*sys.exc_info()) |
|
1249 | 1249 | else: |
|
1250 | 1250 | for matcher in self.matchers: |
|
1251 | 1251 | self.matches = matcher(text) |
|
1252 | 1252 | if self.matches: |
|
1253 | 1253 | break |
|
1254 | 1254 | # FIXME: we should extend our api to return a dict with completions for |
|
1255 | 1255 | # different types of objects. The rlcomplete() method could then |
|
1256 | 1256 | # simply collapse the dict into a list for readline, but we'd have |
|
1257 | 1257 | # richer completion semantics in other evironments. |
|
1258 | 1258 | self.matches.extend(self.python_jedi_matches(text, line_buffer, cursor_pos)) |
|
1259 | 1259 | |
|
1260 | 1260 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1261 | 1261 | |
|
1262 | 1262 | return text, self.matches |
|
1263 | 1263 | |
|
1264 | 1264 | def rlcomplete(self, text, state): |
|
1265 | 1265 | """Return the state-th possible completion for 'text'. |
|
1266 | 1266 | |
|
1267 | 1267 | This is called successively with state == 0, 1, 2, ... until it |
|
1268 | 1268 | returns None. The completion should begin with 'text'. |
|
1269 | 1269 | |
|
1270 | 1270 | Parameters |
|
1271 | 1271 | ---------- |
|
1272 | 1272 | text : string |
|
1273 | 1273 | Text to perform the completion on. |
|
1274 | 1274 | |
|
1275 | 1275 | state : int |
|
1276 | 1276 | Counter used by readline. |
|
1277 | 1277 | """ |
|
1278 | 1278 | if state==0: |
|
1279 | 1279 | |
|
1280 | 1280 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
1281 | 1281 | cursor_pos = self.readline.get_endidx() |
|
1282 | 1282 | |
|
1283 | 1283 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
1284 | 1284 | # (text, line_buffer, cursor_pos) ) # dbg |
|
1285 | 1285 | |
|
1286 | 1286 | # if there is only a tab on a line with only whitespace, instead of |
|
1287 | 1287 | # the mostly useless 'do you want to see all million completions' |
|
1288 | 1288 | # message, just do the right thing and give the user his tab! |
|
1289 | 1289 | # Incidentally, this enables pasting of tabbed text from an editor |
|
1290 | 1290 | # (as long as autoindent is off). |
|
1291 | 1291 | |
|
1292 | 1292 | # It should be noted that at least pyreadline still shows file |
|
1293 | 1293 | # completions - is there a way around it? |
|
1294 | 1294 | |
|
1295 | 1295 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
1296 | 1296 | # we don't interfere with their own tab-completion mechanism. |
|
1297 | 1297 | if not (self.dumb_terminal or line_buffer.strip()): |
|
1298 | 1298 | self.readline.insert_text('\t') |
|
1299 | 1299 | sys.stdout.flush() |
|
1300 | 1300 | return None |
|
1301 | 1301 | |
|
1302 | 1302 | # Note: debugging exceptions that may occur in completion is very |
|
1303 | 1303 | # tricky, because readline unconditionally silences them. So if |
|
1304 | 1304 | # during development you suspect a bug in the completion code, turn |
|
1305 | 1305 | # this flag on temporarily by uncommenting the second form (don't |
|
1306 | 1306 | # flip the value in the first line, as the '# dbg' marker can be |
|
1307 | 1307 | # automatically detected and is used elsewhere). |
|
1308 | 1308 | DEBUG = False |
|
1309 | 1309 | #DEBUG = True # dbg |
|
1310 | 1310 | if DEBUG: |
|
1311 | 1311 | try: |
|
1312 | 1312 | self.complete(text, line_buffer, cursor_pos) |
|
1313 | 1313 | except: |
|
1314 | 1314 | import traceback; traceback.print_exc() |
|
1315 | 1315 | else: |
|
1316 | 1316 | # The normal production version is here |
|
1317 | 1317 | |
|
1318 | 1318 | # This method computes the self.matches array |
|
1319 | 1319 | self.complete(text, line_buffer, cursor_pos) |
|
1320 | 1320 | |
|
1321 | 1321 | try: |
|
1322 | 1322 | return self.matches[state] |
|
1323 | 1323 | except IndexError: |
|
1324 | 1324 | return None |
|
1325 | 1325 |
@@ -1,951 +1,960 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 | |
|
19 | 19 | from decorator import decorator |
|
20 | 20 | |
|
21 | 21 | from traitlets.config.configurable import Configurable |
|
22 | 22 | from IPython.core.getipython import get_ipython |
|
23 | 23 | from IPython.utils.sentinel import Sentinel |
|
24 | 24 | from IPython.utils.dir2 import get_real_method |
|
25 | 25 | from IPython.lib import pretty |
|
26 | 26 | from traitlets import ( |
|
27 | 27 | Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List, |
|
28 | 28 | ForwardDeclaredInstance, |
|
29 | default, observe, | |
|
29 | 30 | ) |
|
30 | 31 | from IPython.utils.py3compat import ( |
|
31 | 32 | with_metaclass, string_types, unicode_type, |
|
32 | 33 | ) |
|
33 | 34 | |
|
34 | 35 | |
|
35 | 36 | class DisplayFormatter(Configurable): |
|
36 | 37 | |
|
37 | 38 | # When set to true only the default plain text formatter will be used. |
|
38 | 39 | plain_text_only = Bool(False).tag(config=True) |
|
39 | 40 | def _plain_text_only_changed(self, name, old, new): |
|
40 | 41 | warnings.warn("""DisplayFormatter.plain_text_only is deprecated. |
|
41 | 42 | |
|
42 | 43 | It will be removed in IPython 5.0 |
|
43 | 44 | |
|
44 | 45 | Use DisplayFormatter.active_types = ['text/plain'] |
|
45 | 46 | for the same effect. |
|
46 | 47 | """, DeprecationWarning) |
|
47 | 48 | if new: |
|
48 | 49 | self.active_types = ['text/plain'] |
|
49 | 50 | else: |
|
50 | 51 | self.active_types = self.format_types |
|
51 | 52 | |
|
52 |
active_types = List(Unicode() |
|
|
53 | active_types = List(Unicode(), | |
|
53 | 54 | help="""List of currently active mime-types to display. |
|
54 | 55 | You can use this to set a white-list for formats to display. |
|
55 | 56 | |
|
56 | 57 | Most users will not need to change this value. |
|
57 | """) | |
|
58 | """).tag(config=True) | |
|
59 | ||
|
60 | @default('active_types') | |
|
58 | 61 | def _active_types_default(self): |
|
59 | 62 | return self.format_types |
|
60 | ||
|
61 | def _active_types_changed(self, name, old, new): | |
|
63 | ||
|
64 | @observe('active_types') | |
|
65 | def _active_types_changed(self, change): | |
|
62 | 66 | for key, formatter in self.formatters.items(): |
|
63 | if key in new: | |
|
67 | if key in change['new']: | |
|
64 | 68 | formatter.enabled = True |
|
65 | 69 | else: |
|
66 | 70 | formatter.enabled = False |
|
67 | 71 | |
|
68 | 72 | ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') |
|
69 |
|
|
|
73 | @default('ipython_display_formatter') | |
|
74 | def _default_formatter(self): | |
|
70 | 75 | return IPythonDisplayFormatter(parent=self) |
|
71 | 76 | |
|
72 | 77 | # A dict of formatter whose keys are format types (MIME types) and whose |
|
73 | 78 | # values are subclasses of BaseFormatter. |
|
74 | 79 | formatters = Dict() |
|
80 | @default('formatters') | |
|
75 | 81 | def _formatters_default(self): |
|
76 | 82 | """Activate the default formatters.""" |
|
77 | 83 | formatter_classes = [ |
|
78 | 84 | PlainTextFormatter, |
|
79 | 85 | HTMLFormatter, |
|
80 | 86 | MarkdownFormatter, |
|
81 | 87 | SVGFormatter, |
|
82 | 88 | PNGFormatter, |
|
83 | 89 | PDFFormatter, |
|
84 | 90 | JPEGFormatter, |
|
85 | 91 | LatexFormatter, |
|
86 | 92 | JSONFormatter, |
|
87 | 93 | JavascriptFormatter |
|
88 | 94 | ] |
|
89 | 95 | d = {} |
|
90 | 96 | for cls in formatter_classes: |
|
91 | 97 | f = cls(parent=self) |
|
92 | 98 | d[f.format_type] = f |
|
93 | 99 | return d |
|
94 | 100 | |
|
95 | 101 | def format(self, obj, include=None, exclude=None): |
|
96 | 102 | """Return a format data dict for an object. |
|
97 | 103 | |
|
98 | 104 | By default all format types will be computed. |
|
99 | 105 | |
|
100 | 106 | The following MIME types are currently implemented: |
|
101 | 107 | |
|
102 | 108 | * text/plain |
|
103 | 109 | * text/html |
|
104 | 110 | * text/markdown |
|
105 | 111 | * text/latex |
|
106 | 112 | * application/json |
|
107 | 113 | * application/javascript |
|
108 | 114 | * application/pdf |
|
109 | 115 | * image/png |
|
110 | 116 | * image/jpeg |
|
111 | 117 | * image/svg+xml |
|
112 | 118 | |
|
113 | 119 | Parameters |
|
114 | 120 | ---------- |
|
115 | 121 | obj : object |
|
116 | 122 | The Python object whose format data will be computed. |
|
117 | 123 | include : list or tuple, optional |
|
118 | 124 | A list of format type strings (MIME types) to include in the |
|
119 | 125 | format data dict. If this is set *only* the format types included |
|
120 | 126 | in this list will be computed. |
|
121 | 127 | exclude : list or tuple, optional |
|
122 | 128 | A list of format type string (MIME types) to exclude in the format |
|
123 | 129 | data dict. If this is set all format types will be computed, |
|
124 | 130 | except for those included in this argument. |
|
125 | 131 | |
|
126 | 132 | Returns |
|
127 | 133 | ------- |
|
128 | 134 | (format_dict, metadata_dict) : tuple of two dicts |
|
129 | 135 | |
|
130 | 136 | format_dict is a dictionary of key/value pairs, one of each format that was |
|
131 | 137 | generated for the object. The keys are the format types, which |
|
132 | 138 | will usually be MIME type strings and the values and JSON'able |
|
133 | 139 | data structure containing the raw data for the representation in |
|
134 | 140 | that format. |
|
135 | 141 | |
|
136 | 142 | metadata_dict is a dictionary of metadata about each mime-type output. |
|
137 | 143 | Its keys will be a strict subset of the keys in format_dict. |
|
138 | 144 | """ |
|
139 | 145 | format_dict = {} |
|
140 | 146 | md_dict = {} |
|
141 | 147 | |
|
142 | 148 | if self.ipython_display_formatter(obj): |
|
143 | 149 | # object handled itself, don't proceed |
|
144 | 150 | return {}, {} |
|
145 | 151 | |
|
146 | 152 | for format_type, formatter in self.formatters.items(): |
|
147 | 153 | if include and format_type not in include: |
|
148 | 154 | continue |
|
149 | 155 | if exclude and format_type in exclude: |
|
150 | 156 | continue |
|
151 | 157 | |
|
152 | 158 | md = None |
|
153 | 159 | try: |
|
154 | 160 | data = formatter(obj) |
|
155 | 161 | except: |
|
156 | 162 | # FIXME: log the exception |
|
157 | 163 | raise |
|
158 | 164 | |
|
159 | 165 | # formatters can return raw data or (data, metadata) |
|
160 | 166 | if isinstance(data, tuple) and len(data) == 2: |
|
161 | 167 | data, md = data |
|
162 | 168 | |
|
163 | 169 | if data is not None: |
|
164 | 170 | format_dict[format_type] = data |
|
165 | 171 | if md is not None: |
|
166 | 172 | md_dict[format_type] = md |
|
167 | 173 | |
|
168 | 174 | return format_dict, md_dict |
|
169 | 175 | |
|
170 | 176 | @property |
|
171 | 177 | def format_types(self): |
|
172 | 178 | """Return the format types (MIME types) of the active formatters.""" |
|
173 | 179 | return list(self.formatters.keys()) |
|
174 | 180 | |
|
175 | 181 | |
|
176 | 182 | #----------------------------------------------------------------------------- |
|
177 | 183 | # Formatters for specific format types (text, html, svg, etc.) |
|
178 | 184 | #----------------------------------------------------------------------------- |
|
179 | 185 | |
|
180 | 186 | |
|
181 | 187 | def _safe_repr(obj): |
|
182 | 188 | """Try to return a repr of an object |
|
183 | 189 | |
|
184 | 190 | always returns a string, at least. |
|
185 | 191 | """ |
|
186 | 192 | try: |
|
187 | 193 | return repr(obj) |
|
188 | 194 | except Exception as e: |
|
189 | 195 | return "un-repr-able object (%r)" % e |
|
190 | 196 | |
|
191 | 197 | |
|
192 | 198 | class FormatterWarning(UserWarning): |
|
193 | 199 | """Warning class for errors in formatters""" |
|
194 | 200 | |
|
195 | 201 | @decorator |
|
196 | 202 | def catch_format_error(method, self, *args, **kwargs): |
|
197 | 203 | """show traceback on failed format call""" |
|
198 | 204 | try: |
|
199 | 205 | r = method(self, *args, **kwargs) |
|
200 | 206 | except NotImplementedError: |
|
201 | 207 | # don't warn on NotImplementedErrors |
|
202 | 208 | return None |
|
203 | 209 | except Exception: |
|
204 | 210 | exc_info = sys.exc_info() |
|
205 | 211 | ip = get_ipython() |
|
206 | 212 | if ip is not None: |
|
207 | 213 | ip.showtraceback(exc_info) |
|
208 | 214 | else: |
|
209 | 215 | traceback.print_exception(*exc_info) |
|
210 | 216 | return None |
|
211 | 217 | return self._check_return(r, args[0]) |
|
212 | 218 | |
|
213 | 219 | |
|
214 | 220 | class FormatterABC(with_metaclass(abc.ABCMeta, object)): |
|
215 | 221 | """ Abstract base class for Formatters. |
|
216 | 222 | |
|
217 | 223 | A formatter is a callable class that is responsible for computing the |
|
218 | 224 | raw format data for a particular format type (MIME type). For example, |
|
219 | 225 | an HTML formatter would have a format type of `text/html` and would return |
|
220 | 226 | the HTML representation of the object when called. |
|
221 | 227 | """ |
|
222 | 228 | |
|
223 | 229 | # The format type of the data returned, usually a MIME type. |
|
224 | 230 | format_type = 'text/plain' |
|
225 | 231 | |
|
226 | 232 | # Is the formatter enabled... |
|
227 | 233 | enabled = True |
|
228 | 234 | |
|
229 | 235 | @abc.abstractmethod |
|
230 | 236 | def __call__(self, obj): |
|
231 | 237 | """Return a JSON'able representation of the object. |
|
232 | 238 | |
|
233 | 239 | If the object cannot be formatted by this formatter, |
|
234 | 240 | warn and return None. |
|
235 | 241 | """ |
|
236 | 242 | return repr(obj) |
|
237 | 243 | |
|
238 | 244 | |
|
239 | 245 | def _mod_name_key(typ): |
|
240 | 246 | """Return a (__module__, __name__) tuple for a type. |
|
241 | 247 | |
|
242 | 248 | Used as key in Formatter.deferred_printers. |
|
243 | 249 | """ |
|
244 | 250 | module = getattr(typ, '__module__', None) |
|
245 | 251 | name = getattr(typ, '__name__', None) |
|
246 | 252 | return (module, name) |
|
247 | 253 | |
|
248 | 254 | |
|
249 | 255 | def _get_type(obj): |
|
250 | 256 | """Return the type of an instance (old and new-style)""" |
|
251 | 257 | return getattr(obj, '__class__', None) or type(obj) |
|
252 | 258 | |
|
253 | 259 | |
|
254 | 260 | _raise_key_error = Sentinel('_raise_key_error', __name__, |
|
255 | 261 | """ |
|
256 | 262 | Special value to raise a KeyError |
|
257 | 263 | |
|
258 | 264 | Raise KeyError in `BaseFormatter.pop` if passed as the default value to `pop` |
|
259 | 265 | """) |
|
260 | 266 | |
|
261 | 267 | |
|
262 | 268 | class BaseFormatter(Configurable): |
|
263 | 269 | """A base formatter class that is configurable. |
|
264 | 270 | |
|
265 | 271 | This formatter should usually be used as the base class of all formatters. |
|
266 | 272 | It is a traited :class:`Configurable` class and includes an extensible |
|
267 | 273 | API for users to determine how their objects are formatted. The following |
|
268 | 274 | logic is used to find a function to format an given object. |
|
269 | 275 | |
|
270 | 276 | 1. The object is introspected to see if it has a method with the name |
|
271 | 277 | :attr:`print_method`. If is does, that object is passed to that method |
|
272 | 278 | for formatting. |
|
273 | 279 | 2. If no print method is found, three internal dictionaries are consulted |
|
274 | 280 | to find print method: :attr:`singleton_printers`, :attr:`type_printers` |
|
275 | 281 | and :attr:`deferred_printers`. |
|
276 | 282 | |
|
277 | 283 | Users should use these dictionaries to register functions that will be |
|
278 | 284 | used to compute the format data for their objects (if those objects don't |
|
279 | 285 | have the special print methods). The easiest way of using these |
|
280 | 286 | dictionaries is through the :meth:`for_type` and :meth:`for_type_by_name` |
|
281 | 287 | methods. |
|
282 | 288 | |
|
283 | 289 | If no function/callable is found to compute the format data, ``None`` is |
|
284 | 290 | returned and this format type is not used. |
|
285 | 291 | """ |
|
286 | 292 | |
|
287 | 293 | format_type = Unicode('text/plain') |
|
288 | 294 | _return_type = string_types |
|
289 | 295 | |
|
290 | 296 | enabled = Bool(True).tag(config=True) |
|
291 | 297 | |
|
292 | 298 | print_method = ObjectName('__repr__') |
|
293 | 299 | |
|
294 | 300 | # The singleton printers. |
|
295 | 301 | # Maps the IDs of the builtin singleton objects to the format functions. |
|
296 | 302 | singleton_printers = Dict().tag(config=True) |
|
297 | 303 | |
|
298 | 304 | # The type-specific printers. |
|
299 | 305 | # Map type objects to the format functions. |
|
300 | 306 | type_printers = Dict().tag(config=True) |
|
301 | 307 | |
|
302 | 308 | # The deferred-import type-specific printers. |
|
303 | 309 | # Map (modulename, classname) pairs to the format functions. |
|
304 | 310 | deferred_printers = Dict().tag(config=True) |
|
305 | 311 | |
|
306 | 312 | @catch_format_error |
|
307 | 313 | def __call__(self, obj): |
|
308 | 314 | """Compute the format for an object.""" |
|
309 | 315 | if self.enabled: |
|
310 | 316 | # lookup registered printer |
|
311 | 317 | try: |
|
312 | 318 | printer = self.lookup(obj) |
|
313 | 319 | except KeyError: |
|
314 | 320 | pass |
|
315 | 321 | else: |
|
316 | 322 | return printer(obj) |
|
317 | 323 | # Finally look for special method names |
|
318 | 324 | method = get_real_method(obj, self.print_method) |
|
319 | 325 | if method is not None: |
|
320 | 326 | return method() |
|
321 | 327 | return None |
|
322 | 328 | else: |
|
323 | 329 | return None |
|
324 | 330 | |
|
325 | 331 | def __contains__(self, typ): |
|
326 | 332 | """map in to lookup_by_type""" |
|
327 | 333 | try: |
|
328 | 334 | self.lookup_by_type(typ) |
|
329 | 335 | except KeyError: |
|
330 | 336 | return False |
|
331 | 337 | else: |
|
332 | 338 | return True |
|
333 | 339 | |
|
334 | 340 | def _check_return(self, r, obj): |
|
335 | 341 | """Check that a return value is appropriate |
|
336 | 342 | |
|
337 | 343 | Return the value if so, None otherwise, warning if invalid. |
|
338 | 344 | """ |
|
339 | 345 | if r is None or isinstance(r, self._return_type) or \ |
|
340 | 346 | (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)): |
|
341 | 347 | return r |
|
342 | 348 | else: |
|
343 | 349 | warnings.warn( |
|
344 | 350 | "%s formatter returned invalid type %s (expected %s) for object: %s" % \ |
|
345 | 351 | (self.format_type, type(r), self._return_type, _safe_repr(obj)), |
|
346 | 352 | FormatterWarning |
|
347 | 353 | ) |
|
348 | 354 | |
|
349 | 355 | def lookup(self, obj): |
|
350 | 356 | """Look up the formatter for a given instance. |
|
351 | 357 | |
|
352 | 358 | Parameters |
|
353 | 359 | ---------- |
|
354 | 360 | obj : object instance |
|
355 | 361 | |
|
356 | 362 | Returns |
|
357 | 363 | ------- |
|
358 | 364 | f : callable |
|
359 | 365 | The registered formatting callable for the type. |
|
360 | 366 | |
|
361 | 367 | Raises |
|
362 | 368 | ------ |
|
363 | 369 | KeyError if the type has not been registered. |
|
364 | 370 | """ |
|
365 | 371 | # look for singleton first |
|
366 | 372 | obj_id = id(obj) |
|
367 | 373 | if obj_id in self.singleton_printers: |
|
368 | 374 | return self.singleton_printers[obj_id] |
|
369 | 375 | # then lookup by type |
|
370 | 376 | return self.lookup_by_type(_get_type(obj)) |
|
371 | 377 | |
|
372 | 378 | def lookup_by_type(self, typ): |
|
373 | 379 | """Look up the registered formatter for a type. |
|
374 | 380 | |
|
375 | 381 | Parameters |
|
376 | 382 | ---------- |
|
377 | 383 | typ : type or '__module__.__name__' string for a type |
|
378 | 384 | |
|
379 | 385 | Returns |
|
380 | 386 | ------- |
|
381 | 387 | f : callable |
|
382 | 388 | The registered formatting callable for the type. |
|
383 | 389 | |
|
384 | 390 | Raises |
|
385 | 391 | ------ |
|
386 | 392 | KeyError if the type has not been registered. |
|
387 | 393 | """ |
|
388 | 394 | if isinstance(typ, string_types): |
|
389 | 395 | typ_key = tuple(typ.rsplit('.',1)) |
|
390 | 396 | if typ_key not in self.deferred_printers: |
|
391 | 397 | # We may have it cached in the type map. We will have to |
|
392 | 398 | # iterate over all of the types to check. |
|
393 | 399 | for cls in self.type_printers: |
|
394 | 400 | if _mod_name_key(cls) == typ_key: |
|
395 | 401 | return self.type_printers[cls] |
|
396 | 402 | else: |
|
397 | 403 | return self.deferred_printers[typ_key] |
|
398 | 404 | else: |
|
399 | 405 | for cls in pretty._get_mro(typ): |
|
400 | 406 | if cls in self.type_printers or self._in_deferred_types(cls): |
|
401 | 407 | return self.type_printers[cls] |
|
402 | 408 | |
|
403 | 409 | # If we have reached here, the lookup failed. |
|
404 | 410 | raise KeyError("No registered printer for {0!r}".format(typ)) |
|
405 | 411 | |
|
406 | 412 | def for_type(self, typ, func=None): |
|
407 | 413 | """Add a format function for a given type. |
|
408 | 414 | |
|
409 | 415 | Parameters |
|
410 | 416 | ----------- |
|
411 | 417 | typ : type or '__module__.__name__' string for a type |
|
412 | 418 | The class of the object that will be formatted using `func`. |
|
413 | 419 | func : callable |
|
414 | 420 | A callable for computing the format data. |
|
415 | 421 | `func` will be called with the object to be formatted, |
|
416 | 422 | and will return the raw data in this formatter's format. |
|
417 | 423 | Subclasses may use a different call signature for the |
|
418 | 424 | `func` argument. |
|
419 | 425 | |
|
420 | 426 | If `func` is None or not specified, there will be no change, |
|
421 | 427 | only returning the current value. |
|
422 | 428 | |
|
423 | 429 | Returns |
|
424 | 430 | ------- |
|
425 | 431 | oldfunc : callable |
|
426 | 432 | The currently registered callable. |
|
427 | 433 | If you are registering a new formatter, |
|
428 | 434 | this will be the previous value (to enable restoring later). |
|
429 | 435 | """ |
|
430 | 436 | # if string given, interpret as 'pkg.module.class_name' |
|
431 | 437 | if isinstance(typ, string_types): |
|
432 | 438 | type_module, type_name = typ.rsplit('.', 1) |
|
433 | 439 | return self.for_type_by_name(type_module, type_name, func) |
|
434 | 440 | |
|
435 | 441 | try: |
|
436 | 442 | oldfunc = self.lookup_by_type(typ) |
|
437 | 443 | except KeyError: |
|
438 | 444 | oldfunc = None |
|
439 | 445 | |
|
440 | 446 | if func is not None: |
|
441 | 447 | self.type_printers[typ] = func |
|
442 | 448 | |
|
443 | 449 | return oldfunc |
|
444 | 450 | |
|
445 | 451 | def for_type_by_name(self, type_module, type_name, func=None): |
|
446 | 452 | """Add a format function for a type specified by the full dotted |
|
447 | 453 | module and name of the type, rather than the type of the object. |
|
448 | 454 | |
|
449 | 455 | Parameters |
|
450 | 456 | ---------- |
|
451 | 457 | type_module : str |
|
452 | 458 | The full dotted name of the module the type is defined in, like |
|
453 | 459 | ``numpy``. |
|
454 | 460 | type_name : str |
|
455 | 461 | The name of the type (the class name), like ``dtype`` |
|
456 | 462 | func : callable |
|
457 | 463 | A callable for computing the format data. |
|
458 | 464 | `func` will be called with the object to be formatted, |
|
459 | 465 | and will return the raw data in this formatter's format. |
|
460 | 466 | Subclasses may use a different call signature for the |
|
461 | 467 | `func` argument. |
|
462 | 468 | |
|
463 | 469 | If `func` is None or unspecified, there will be no change, |
|
464 | 470 | only returning the current value. |
|
465 | 471 | |
|
466 | 472 | Returns |
|
467 | 473 | ------- |
|
468 | 474 | oldfunc : callable |
|
469 | 475 | The currently registered callable. |
|
470 | 476 | If you are registering a new formatter, |
|
471 | 477 | this will be the previous value (to enable restoring later). |
|
472 | 478 | """ |
|
473 | 479 | key = (type_module, type_name) |
|
474 | 480 | |
|
475 | 481 | try: |
|
476 | 482 | oldfunc = self.lookup_by_type("%s.%s" % key) |
|
477 | 483 | except KeyError: |
|
478 | 484 | oldfunc = None |
|
479 | 485 | |
|
480 | 486 | if func is not None: |
|
481 | 487 | self.deferred_printers[key] = func |
|
482 | 488 | return oldfunc |
|
483 | 489 | |
|
484 | 490 | def pop(self, typ, default=_raise_key_error): |
|
485 | 491 | """Pop a formatter for the given type. |
|
486 | 492 | |
|
487 | 493 | Parameters |
|
488 | 494 | ---------- |
|
489 | 495 | typ : type or '__module__.__name__' string for a type |
|
490 | 496 | default : object |
|
491 | 497 | value to be returned if no formatter is registered for typ. |
|
492 | 498 | |
|
493 | 499 | Returns |
|
494 | 500 | ------- |
|
495 | 501 | obj : object |
|
496 | 502 | The last registered object for the type. |
|
497 | 503 | |
|
498 | 504 | Raises |
|
499 | 505 | ------ |
|
500 | 506 | KeyError if the type is not registered and default is not specified. |
|
501 | 507 | """ |
|
502 | 508 | |
|
503 | 509 | if isinstance(typ, string_types): |
|
504 | 510 | typ_key = tuple(typ.rsplit('.',1)) |
|
505 | 511 | if typ_key not in self.deferred_printers: |
|
506 | 512 | # We may have it cached in the type map. We will have to |
|
507 | 513 | # iterate over all of the types to check. |
|
508 | 514 | for cls in self.type_printers: |
|
509 | 515 | if _mod_name_key(cls) == typ_key: |
|
510 | 516 | old = self.type_printers.pop(cls) |
|
511 | 517 | break |
|
512 | 518 | else: |
|
513 | 519 | old = default |
|
514 | 520 | else: |
|
515 | 521 | old = self.deferred_printers.pop(typ_key) |
|
516 | 522 | else: |
|
517 | 523 | if typ in self.type_printers: |
|
518 | 524 | old = self.type_printers.pop(typ) |
|
519 | 525 | else: |
|
520 | 526 | old = self.deferred_printers.pop(_mod_name_key(typ), default) |
|
521 | 527 | if old is _raise_key_error: |
|
522 | 528 | raise KeyError("No registered value for {0!r}".format(typ)) |
|
523 | 529 | return old |
|
524 | 530 | |
|
525 | 531 | def _in_deferred_types(self, cls): |
|
526 | 532 | """ |
|
527 | 533 | Check if the given class is specified in the deferred type registry. |
|
528 | 534 | |
|
529 | 535 | Successful matches will be moved to the regular type registry for future use. |
|
530 | 536 | """ |
|
531 | 537 | mod = getattr(cls, '__module__', None) |
|
532 | 538 | name = getattr(cls, '__name__', None) |
|
533 | 539 | key = (mod, name) |
|
534 | 540 | if key in self.deferred_printers: |
|
535 | 541 | # Move the printer over to the regular registry. |
|
536 | 542 | printer = self.deferred_printers.pop(key) |
|
537 | 543 | self.type_printers[cls] = printer |
|
538 | 544 | return True |
|
539 | 545 | return False |
|
540 | 546 | |
|
541 | 547 | |
|
542 | 548 | class PlainTextFormatter(BaseFormatter): |
|
543 | 549 | """The default pretty-printer. |
|
544 | 550 | |
|
545 | 551 | This uses :mod:`IPython.lib.pretty` to compute the format data of |
|
546 | 552 | the object. If the object cannot be pretty printed, :func:`repr` is used. |
|
547 | 553 | See the documentation of :mod:`IPython.lib.pretty` for details on |
|
548 | 554 | how to write pretty printers. Here is a simple example:: |
|
549 | 555 | |
|
550 | 556 | def dtype_pprinter(obj, p, cycle): |
|
551 | 557 | if cycle: |
|
552 | 558 | return p.text('dtype(...)') |
|
553 | 559 | if hasattr(obj, 'fields'): |
|
554 | 560 | if obj.fields is None: |
|
555 | 561 | p.text(repr(obj)) |
|
556 | 562 | else: |
|
557 | 563 | p.begin_group(7, 'dtype([') |
|
558 | 564 | for i, field in enumerate(obj.descr): |
|
559 | 565 | if i > 0: |
|
560 | 566 | p.text(',') |
|
561 | 567 | p.breakable() |
|
562 | 568 | p.pretty(field) |
|
563 | 569 | p.end_group(7, '])') |
|
564 | 570 | """ |
|
565 | 571 | |
|
566 | 572 | # The format type of data returned. |
|
567 | 573 | format_type = Unicode('text/plain') |
|
568 | 574 | |
|
569 | 575 | # This subclass ignores this attribute as it always need to return |
|
570 | 576 | # something. |
|
571 | 577 | enabled = Bool(True).tag(config=False) |
|
572 | 578 | |
|
573 |
max_seq_length = Integer(pretty.MAX_SEQ_LENGTH |
|
|
579 | max_seq_length = Integer(pretty.MAX_SEQ_LENGTH, | |
|
574 | 580 | help="""Truncate large collections (lists, dicts, tuples, sets) to this size. |
|
575 | 581 | |
|
576 | 582 | Set to 0 to disable truncation. |
|
577 | 583 | """ |
|
578 | ) | |
|
584 | ).tag(config=True) | |
|
579 | 585 | |
|
580 | 586 | # Look for a _repr_pretty_ methods to use for pretty printing. |
|
581 | 587 | print_method = ObjectName('_repr_pretty_') |
|
582 | 588 | |
|
583 | 589 | # Whether to pretty-print or not. |
|
584 | 590 | pprint = Bool(True).tag(config=True) |
|
585 | 591 | |
|
586 | 592 | # Whether to be verbose or not. |
|
587 | 593 | verbose = Bool(False).tag(config=True) |
|
588 | 594 | |
|
589 | 595 | # The maximum width. |
|
590 | 596 | max_width = Integer(79).tag(config=True) |
|
591 | 597 | |
|
592 | 598 | # The newline character. |
|
593 | 599 | newline = Unicode('\n').tag(config=True) |
|
594 | 600 | |
|
595 | 601 | # format-string for pprinting floats |
|
596 | 602 | float_format = Unicode('%r') |
|
597 | 603 | # setter for float precision, either int or direct format-string |
|
598 | 604 | float_precision = CUnicode('').tag(config=True) |
|
599 | 605 | |
|
600 | 606 | def _float_precision_changed(self, name, old, new): |
|
601 | 607 | """float_precision changed, set float_format accordingly. |
|
602 | 608 | |
|
603 | 609 | float_precision can be set by int or str. |
|
604 | 610 | This will set float_format, after interpreting input. |
|
605 | 611 | If numpy has been imported, numpy print precision will also be set. |
|
606 | 612 | |
|
607 | 613 | integer `n` sets format to '%.nf', otherwise, format set directly. |
|
608 | 614 | |
|
609 | 615 | An empty string returns to defaults (repr for float, 8 for numpy). |
|
610 | 616 | |
|
611 | 617 | This parameter can be set via the '%precision' magic. |
|
612 | 618 | """ |
|
613 | 619 | |
|
614 | 620 | if '%' in new: |
|
615 | 621 | # got explicit format string |
|
616 | 622 | fmt = new |
|
617 | 623 | try: |
|
618 | 624 | fmt%3.14159 |
|
619 | 625 | except Exception: |
|
620 | 626 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
621 | 627 | elif new: |
|
622 | 628 | # otherwise, should be an int |
|
623 | 629 | try: |
|
624 | 630 | i = int(new) |
|
625 | 631 | assert i >= 0 |
|
626 | 632 | except ValueError: |
|
627 | 633 | raise ValueError("Precision must be int or format string, not %r"%new) |
|
628 | 634 | except AssertionError: |
|
629 | 635 | raise ValueError("int precision must be non-negative, not %r"%i) |
|
630 | 636 | |
|
631 | 637 | fmt = '%%.%if'%i |
|
632 | 638 | if 'numpy' in sys.modules: |
|
633 | 639 | # set numpy precision if it has been imported |
|
634 | 640 | import numpy |
|
635 | 641 | numpy.set_printoptions(precision=i) |
|
636 | 642 | else: |
|
637 | 643 | # default back to repr |
|
638 | 644 | fmt = '%r' |
|
639 | 645 | if 'numpy' in sys.modules: |
|
640 | 646 | import numpy |
|
641 | 647 | # numpy default is 8 |
|
642 | 648 | numpy.set_printoptions(precision=8) |
|
643 | 649 | self.float_format = fmt |
|
644 | 650 | |
|
645 | 651 | # Use the default pretty printers from IPython.lib.pretty. |
|
652 | @default('singleton_printers') | |
|
646 | 653 | def _singleton_printers_default(self): |
|
647 | 654 | return pretty._singleton_pprinters.copy() |
|
648 | 655 | |
|
656 | @default('type_printers') | |
|
649 | 657 | def _type_printers_default(self): |
|
650 | 658 | d = pretty._type_pprinters.copy() |
|
651 | 659 | d[float] = lambda obj,p,cycle: p.text(self.float_format%obj) |
|
652 | 660 | return d |
|
653 | 661 | |
|
662 | @default('deferred_printers') | |
|
654 | 663 | def _deferred_printers_default(self): |
|
655 | 664 | return pretty._deferred_type_pprinters.copy() |
|
656 | 665 | |
|
657 | 666 | #### FormatterABC interface #### |
|
658 | 667 | |
|
659 | 668 | @catch_format_error |
|
660 | 669 | def __call__(self, obj): |
|
661 | 670 | """Compute the pretty representation of the object.""" |
|
662 | 671 | if not self.pprint: |
|
663 | 672 | return repr(obj) |
|
664 | 673 | else: |
|
665 | 674 | # handle str and unicode on Python 2 |
|
666 | 675 | # io.StringIO only accepts unicode, |
|
667 | 676 | # cStringIO doesn't handle unicode on py2, |
|
668 | 677 | # StringIO allows str, unicode but only ascii str |
|
669 | 678 | stream = pretty.CUnicodeIO() |
|
670 | 679 | printer = pretty.RepresentationPrinter(stream, self.verbose, |
|
671 | 680 | self.max_width, self.newline, |
|
672 | 681 | max_seq_length=self.max_seq_length, |
|
673 | 682 | singleton_pprinters=self.singleton_printers, |
|
674 | 683 | type_pprinters=self.type_printers, |
|
675 | 684 | deferred_pprinters=self.deferred_printers) |
|
676 | 685 | printer.pretty(obj) |
|
677 | 686 | printer.flush() |
|
678 | 687 | return stream.getvalue() |
|
679 | 688 | |
|
680 | 689 | |
|
681 | 690 | class HTMLFormatter(BaseFormatter): |
|
682 | 691 | """An HTML formatter. |
|
683 | 692 | |
|
684 | 693 | To define the callables that compute the HTML representation of your |
|
685 | 694 | objects, define a :meth:`_repr_html_` method or use the :meth:`for_type` |
|
686 | 695 | or :meth:`for_type_by_name` methods to register functions that handle |
|
687 | 696 | this. |
|
688 | 697 | |
|
689 | 698 | The return value of this formatter should be a valid HTML snippet that |
|
690 | 699 | could be injected into an existing DOM. It should *not* include the |
|
691 | 700 | ```<html>`` or ```<body>`` tags. |
|
692 | 701 | """ |
|
693 | 702 | format_type = Unicode('text/html') |
|
694 | 703 | |
|
695 | 704 | print_method = ObjectName('_repr_html_') |
|
696 | 705 | |
|
697 | 706 | |
|
698 | 707 | class MarkdownFormatter(BaseFormatter): |
|
699 | 708 | """A Markdown formatter. |
|
700 | 709 | |
|
701 | 710 | To define the callables that compute the Markdown representation of your |
|
702 | 711 | objects, define a :meth:`_repr_markdown_` method or use the :meth:`for_type` |
|
703 | 712 | or :meth:`for_type_by_name` methods to register functions that handle |
|
704 | 713 | this. |
|
705 | 714 | |
|
706 | 715 | The return value of this formatter should be a valid Markdown. |
|
707 | 716 | """ |
|
708 | 717 | format_type = Unicode('text/markdown') |
|
709 | 718 | |
|
710 | 719 | print_method = ObjectName('_repr_markdown_') |
|
711 | 720 | |
|
712 | 721 | class SVGFormatter(BaseFormatter): |
|
713 | 722 | """An SVG formatter. |
|
714 | 723 | |
|
715 | 724 | To define the callables that compute the SVG representation of your |
|
716 | 725 | objects, define a :meth:`_repr_svg_` method or use the :meth:`for_type` |
|
717 | 726 | or :meth:`for_type_by_name` methods to register functions that handle |
|
718 | 727 | this. |
|
719 | 728 | |
|
720 | 729 | The return value of this formatter should be valid SVG enclosed in |
|
721 | 730 | ```<svg>``` tags, that could be injected into an existing DOM. It should |
|
722 | 731 | *not* include the ```<html>`` or ```<body>`` tags. |
|
723 | 732 | """ |
|
724 | 733 | format_type = Unicode('image/svg+xml') |
|
725 | 734 | |
|
726 | 735 | print_method = ObjectName('_repr_svg_') |
|
727 | 736 | |
|
728 | 737 | |
|
729 | 738 | class PNGFormatter(BaseFormatter): |
|
730 | 739 | """A PNG formatter. |
|
731 | 740 | |
|
732 | 741 | To define the callables that compute the PNG representation of your |
|
733 | 742 | objects, define a :meth:`_repr_png_` method or use the :meth:`for_type` |
|
734 | 743 | or :meth:`for_type_by_name` methods to register functions that handle |
|
735 | 744 | this. |
|
736 | 745 | |
|
737 | 746 | The return value of this formatter should be raw PNG data, *not* |
|
738 | 747 | base64 encoded. |
|
739 | 748 | """ |
|
740 | 749 | format_type = Unicode('image/png') |
|
741 | 750 | |
|
742 | 751 | print_method = ObjectName('_repr_png_') |
|
743 | 752 | |
|
744 | 753 | _return_type = (bytes, unicode_type) |
|
745 | 754 | |
|
746 | 755 | |
|
747 | 756 | class JPEGFormatter(BaseFormatter): |
|
748 | 757 | """A JPEG formatter. |
|
749 | 758 | |
|
750 | 759 | To define the callables that compute the JPEG representation of your |
|
751 | 760 | objects, define a :meth:`_repr_jpeg_` method or use the :meth:`for_type` |
|
752 | 761 | or :meth:`for_type_by_name` methods to register functions that handle |
|
753 | 762 | this. |
|
754 | 763 | |
|
755 | 764 | The return value of this formatter should be raw JPEG data, *not* |
|
756 | 765 | base64 encoded. |
|
757 | 766 | """ |
|
758 | 767 | format_type = Unicode('image/jpeg') |
|
759 | 768 | |
|
760 | 769 | print_method = ObjectName('_repr_jpeg_') |
|
761 | 770 | |
|
762 | 771 | _return_type = (bytes, unicode_type) |
|
763 | 772 | |
|
764 | 773 | |
|
765 | 774 | class LatexFormatter(BaseFormatter): |
|
766 | 775 | """A LaTeX formatter. |
|
767 | 776 | |
|
768 | 777 | To define the callables that compute the LaTeX representation of your |
|
769 | 778 | objects, define a :meth:`_repr_latex_` method or use the :meth:`for_type` |
|
770 | 779 | or :meth:`for_type_by_name` methods to register functions that handle |
|
771 | 780 | this. |
|
772 | 781 | |
|
773 | 782 | The return value of this formatter should be a valid LaTeX equation, |
|
774 | 783 | enclosed in either ```$```, ```$$``` or another LaTeX equation |
|
775 | 784 | environment. |
|
776 | 785 | """ |
|
777 | 786 | format_type = Unicode('text/latex') |
|
778 | 787 | |
|
779 | 788 | print_method = ObjectName('_repr_latex_') |
|
780 | 789 | |
|
781 | 790 | |
|
782 | 791 | class JSONFormatter(BaseFormatter): |
|
783 | 792 | """A JSON string formatter. |
|
784 | 793 | |
|
785 | 794 | To define the callables that compute the JSONable representation of |
|
786 | 795 | your objects, define a :meth:`_repr_json_` method or use the :meth:`for_type` |
|
787 | 796 | or :meth:`for_type_by_name` methods to register functions that handle |
|
788 | 797 | this. |
|
789 | 798 | |
|
790 | 799 | The return value of this formatter should be a JSONable list or dict. |
|
791 | 800 | JSON scalars (None, number, string) are not allowed, only dict or list containers. |
|
792 | 801 | """ |
|
793 | 802 | format_type = Unicode('application/json') |
|
794 | 803 | _return_type = (list, dict) |
|
795 | 804 | |
|
796 | 805 | print_method = ObjectName('_repr_json_') |
|
797 | 806 | |
|
798 | 807 | def _check_return(self, r, obj): |
|
799 | 808 | """Check that a return value is appropriate |
|
800 | 809 | |
|
801 | 810 | Return the value if so, None otherwise, warning if invalid. |
|
802 | 811 | """ |
|
803 | 812 | if r is None: |
|
804 | 813 | return |
|
805 | 814 | md = None |
|
806 | 815 | if isinstance(r, tuple): |
|
807 | 816 | # unpack data, metadata tuple for type checking on first element |
|
808 | 817 | r, md = r |
|
809 | 818 | |
|
810 | 819 | # handle deprecated JSON-as-string form from IPython < 3 |
|
811 | 820 | if isinstance(r, string_types): |
|
812 | 821 | warnings.warn("JSON expects JSONable list/dict containers, not JSON strings", |
|
813 | 822 | FormatterWarning) |
|
814 | 823 | r = json.loads(r) |
|
815 | 824 | |
|
816 | 825 | if md is not None: |
|
817 | 826 | # put the tuple back together |
|
818 | 827 | r = (r, md) |
|
819 | 828 | return super(JSONFormatter, self)._check_return(r, obj) |
|
820 | 829 | |
|
821 | 830 | |
|
822 | 831 | class JavascriptFormatter(BaseFormatter): |
|
823 | 832 | """A Javascript formatter. |
|
824 | 833 | |
|
825 | 834 | To define the callables that compute the Javascript representation of |
|
826 | 835 | your objects, define a :meth:`_repr_javascript_` method or use the |
|
827 | 836 | :meth:`for_type` or :meth:`for_type_by_name` methods to register functions |
|
828 | 837 | that handle this. |
|
829 | 838 | |
|
830 | 839 | The return value of this formatter should be valid Javascript code and |
|
831 | 840 | should *not* be enclosed in ```<script>``` tags. |
|
832 | 841 | """ |
|
833 | 842 | format_type = Unicode('application/javascript') |
|
834 | 843 | |
|
835 | 844 | print_method = ObjectName('_repr_javascript_') |
|
836 | 845 | |
|
837 | 846 | |
|
838 | 847 | class PDFFormatter(BaseFormatter): |
|
839 | 848 | """A PDF formatter. |
|
840 | 849 | |
|
841 | 850 | To define the callables that compute the PDF representation of your |
|
842 | 851 | objects, define a :meth:`_repr_pdf_` method or use the :meth:`for_type` |
|
843 | 852 | or :meth:`for_type_by_name` methods to register functions that handle |
|
844 | 853 | this. |
|
845 | 854 | |
|
846 | 855 | The return value of this formatter should be raw PDF data, *not* |
|
847 | 856 | base64 encoded. |
|
848 | 857 | """ |
|
849 | 858 | format_type = Unicode('application/pdf') |
|
850 | 859 | |
|
851 | 860 | print_method = ObjectName('_repr_pdf_') |
|
852 | 861 | |
|
853 | 862 | _return_type = (bytes, unicode_type) |
|
854 | 863 | |
|
855 | 864 | class IPythonDisplayFormatter(BaseFormatter): |
|
856 | 865 | """A Formatter for objects that know how to display themselves. |
|
857 | 866 | |
|
858 | 867 | To define the callables that compute the representation of your |
|
859 | 868 | objects, define a :meth:`_ipython_display_` method or use the :meth:`for_type` |
|
860 | 869 | or :meth:`for_type_by_name` methods to register functions that handle |
|
861 | 870 | this. Unlike mime-type displays, this method should not return anything, |
|
862 | 871 | instead calling any appropriate display methods itself. |
|
863 | 872 | |
|
864 | 873 | This display formatter has highest priority. |
|
865 | 874 | If it fires, no other display formatter will be called. |
|
866 | 875 | """ |
|
867 | 876 | print_method = ObjectName('_ipython_display_') |
|
868 | 877 | _return_type = (type(None), bool) |
|
869 | 878 | |
|
870 | 879 | |
|
871 | 880 | @catch_format_error |
|
872 | 881 | def __call__(self, obj): |
|
873 | 882 | """Compute the format for an object.""" |
|
874 | 883 | if self.enabled: |
|
875 | 884 | # lookup registered printer |
|
876 | 885 | try: |
|
877 | 886 | printer = self.lookup(obj) |
|
878 | 887 | except KeyError: |
|
879 | 888 | pass |
|
880 | 889 | else: |
|
881 | 890 | printer(obj) |
|
882 | 891 | return True |
|
883 | 892 | # Finally look for special method names |
|
884 | 893 | method = get_real_method(obj, self.print_method) |
|
885 | 894 | if method is not None: |
|
886 | 895 | method() |
|
887 | 896 | return True |
|
888 | 897 | |
|
889 | 898 | |
|
890 | 899 | FormatterABC.register(BaseFormatter) |
|
891 | 900 | FormatterABC.register(PlainTextFormatter) |
|
892 | 901 | FormatterABC.register(HTMLFormatter) |
|
893 | 902 | FormatterABC.register(MarkdownFormatter) |
|
894 | 903 | FormatterABC.register(SVGFormatter) |
|
895 | 904 | FormatterABC.register(PNGFormatter) |
|
896 | 905 | FormatterABC.register(PDFFormatter) |
|
897 | 906 | FormatterABC.register(JPEGFormatter) |
|
898 | 907 | FormatterABC.register(LatexFormatter) |
|
899 | 908 | FormatterABC.register(JSONFormatter) |
|
900 | 909 | FormatterABC.register(JavascriptFormatter) |
|
901 | 910 | FormatterABC.register(IPythonDisplayFormatter) |
|
902 | 911 | |
|
903 | 912 | |
|
904 | 913 | def format_display_data(obj, include=None, exclude=None): |
|
905 | 914 | """Return a format data dict for an object. |
|
906 | 915 | |
|
907 | 916 | By default all format types will be computed. |
|
908 | 917 | |
|
909 | 918 | The following MIME types are currently implemented: |
|
910 | 919 | |
|
911 | 920 | * text/plain |
|
912 | 921 | * text/html |
|
913 | 922 | * text/markdown |
|
914 | 923 | * text/latex |
|
915 | 924 | * application/json |
|
916 | 925 | * application/javascript |
|
917 | 926 | * application/pdf |
|
918 | 927 | * image/png |
|
919 | 928 | * image/jpeg |
|
920 | 929 | * image/svg+xml |
|
921 | 930 | |
|
922 | 931 | Parameters |
|
923 | 932 | ---------- |
|
924 | 933 | obj : object |
|
925 | 934 | The Python object whose format data will be computed. |
|
926 | 935 | |
|
927 | 936 | Returns |
|
928 | 937 | ------- |
|
929 | 938 | format_dict : dict |
|
930 | 939 | A dictionary of key/value pairs, one or each format that was |
|
931 | 940 | generated for the object. The keys are the format types, which |
|
932 | 941 | will usually be MIME type strings and the values and JSON'able |
|
933 | 942 | data structure containing the raw data for the representation in |
|
934 | 943 | that format. |
|
935 | 944 | include : list or tuple, optional |
|
936 | 945 | A list of format type strings (MIME types) to include in the |
|
937 | 946 | format data dict. If this is set *only* the format types included |
|
938 | 947 | in this list will be computed. |
|
939 | 948 | exclude : list or tuple, optional |
|
940 | 949 | A list of format type string (MIME types) to exclue in the format |
|
941 | 950 | data dict. If this is set all format types will be computed, |
|
942 | 951 | except for those included in this argument. |
|
943 | 952 | """ |
|
944 | 953 | from IPython.core.interactiveshell import InteractiveShell |
|
945 | 954 | |
|
946 | 955 | return InteractiveShell.instance().display_formatter.format( |
|
947 | 956 | obj, |
|
948 | 957 | include, |
|
949 | 958 | exclude |
|
950 | 959 | ) |
|
951 | 960 |
@@ -1,904 +1,908 b'' | |||
|
1 | 1 | """ History related magics and functionality """ |
|
2 | 2 | |
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | 6 | from __future__ import print_function |
|
7 | 7 | |
|
8 | 8 | import atexit |
|
9 | 9 | import datetime |
|
10 | 10 | import os |
|
11 | 11 | import re |
|
12 | 12 | try: |
|
13 | 13 | import sqlite3 |
|
14 | 14 | except ImportError: |
|
15 | 15 | try: |
|
16 | 16 | from pysqlite2 import dbapi2 as sqlite3 |
|
17 | 17 | except ImportError: |
|
18 | 18 | sqlite3 = None |
|
19 | 19 | import threading |
|
20 | 20 | |
|
21 | 21 | from traitlets.config.configurable import LoggingConfigurable |
|
22 | 22 | from decorator import decorator |
|
23 | 23 | from IPython.utils.decorators import undoc |
|
24 | 24 | from IPython.utils.path import locate_profile |
|
25 | 25 | from IPython.utils import py3compat |
|
26 | 26 | from traitlets import ( |
|
27 | 27 | Any, Bool, Dict, Instance, Integer, List, Unicode, TraitError, |
|
28 | default, observe, | |
|
28 | 29 | ) |
|
29 | 30 | from warnings import warn |
|
30 | 31 | |
|
31 | 32 | #----------------------------------------------------------------------------- |
|
32 | 33 | # Classes and functions |
|
33 | 34 | #----------------------------------------------------------------------------- |
|
34 | 35 | |
|
35 | 36 | @undoc |
|
36 | 37 | class DummyDB(object): |
|
37 | 38 | """Dummy DB that will act as a black hole for history. |
|
38 | 39 | |
|
39 | 40 | Only used in the absence of sqlite""" |
|
40 | 41 | def execute(*args, **kwargs): |
|
41 | 42 | return [] |
|
42 | 43 | |
|
43 | 44 | def commit(self, *args, **kwargs): |
|
44 | 45 | pass |
|
45 | 46 | |
|
46 | 47 | def __enter__(self, *args, **kwargs): |
|
47 | 48 | pass |
|
48 | 49 | |
|
49 | 50 | def __exit__(self, *args, **kwargs): |
|
50 | 51 | pass |
|
51 | 52 | |
|
52 | 53 | |
|
53 | 54 | @decorator |
|
54 | 55 | def needs_sqlite(f, self, *a, **kw): |
|
55 | 56 | """Decorator: return an empty list in the absence of sqlite.""" |
|
56 | 57 | if sqlite3 is None or not self.enabled: |
|
57 | 58 | return [] |
|
58 | 59 | else: |
|
59 | 60 | return f(self, *a, **kw) |
|
60 | 61 | |
|
61 | 62 | |
|
62 | 63 | if sqlite3 is not None: |
|
63 | 64 | DatabaseError = sqlite3.DatabaseError |
|
64 | 65 | OperationalError = sqlite3.OperationalError |
|
65 | 66 | else: |
|
66 | 67 | @undoc |
|
67 | 68 | class DatabaseError(Exception): |
|
68 | 69 | "Dummy exception when sqlite could not be imported. Should never occur." |
|
69 | 70 | |
|
70 | 71 | @undoc |
|
71 | 72 | class OperationalError(Exception): |
|
72 | 73 | "Dummy exception when sqlite could not be imported. Should never occur." |
|
73 | 74 | |
|
74 | 75 | # use 16kB as threshold for whether a corrupt history db should be saved |
|
75 | 76 | # that should be at least 100 entries or so |
|
76 | 77 | _SAVE_DB_SIZE = 16384 |
|
77 | 78 | |
|
78 | 79 | @decorator |
|
79 | 80 | def catch_corrupt_db(f, self, *a, **kw): |
|
80 | 81 | """A decorator which wraps HistoryAccessor method calls to catch errors from |
|
81 | 82 | a corrupt SQLite database, move the old database out of the way, and create |
|
82 | 83 | a new one. |
|
83 | 84 | |
|
84 | 85 | We avoid clobbering larger databases because this may be triggered due to filesystem issues, |
|
85 | 86 | not just a corrupt file. |
|
86 | 87 | """ |
|
87 | 88 | try: |
|
88 | 89 | return f(self, *a, **kw) |
|
89 | 90 | except (DatabaseError, OperationalError) as e: |
|
90 | 91 | self._corrupt_db_counter += 1 |
|
91 | 92 | self.log.error("Failed to open SQLite history %s (%s).", self.hist_file, e) |
|
92 | 93 | if self.hist_file != ':memory:': |
|
93 | 94 | if self._corrupt_db_counter > self._corrupt_db_limit: |
|
94 | 95 | self.hist_file = ':memory:' |
|
95 | 96 | self.log.error("Failed to load history too many times, history will not be saved.") |
|
96 | 97 | elif os.path.isfile(self.hist_file): |
|
97 | 98 | # move the file out of the way |
|
98 | 99 | base, ext = os.path.splitext(self.hist_file) |
|
99 | 100 | size = os.stat(self.hist_file).st_size |
|
100 | 101 | if size >= _SAVE_DB_SIZE: |
|
101 | 102 | # if there's significant content, avoid clobbering |
|
102 | 103 | now = datetime.datetime.now().isoformat().replace(':', '.') |
|
103 | 104 | newpath = base + '-corrupt-' + now + ext |
|
104 | 105 | # don't clobber previous corrupt backups |
|
105 | 106 | for i in range(100): |
|
106 | 107 | if not os.path.isfile(newpath): |
|
107 | 108 | break |
|
108 | 109 | else: |
|
109 | 110 | newpath = base + '-corrupt-' + now + (u'-%i' % i) + ext |
|
110 | 111 | else: |
|
111 | 112 | # not much content, possibly empty; don't worry about clobbering |
|
112 | 113 | # maybe we should just delete it? |
|
113 | 114 | newpath = base + '-corrupt' + ext |
|
114 | 115 | os.rename(self.hist_file, newpath) |
|
115 | 116 | self.log.error("History file was moved to %s and a new file created.", newpath) |
|
116 | 117 | self.init_db() |
|
117 | 118 | return [] |
|
118 | 119 | else: |
|
119 | 120 | # Failed with :memory:, something serious is wrong |
|
120 | 121 | raise |
|
121 | 122 | |
|
122 | 123 | class HistoryAccessorBase(LoggingConfigurable): |
|
123 | 124 | """An abstract class for History Accessors """ |
|
124 | 125 | |
|
125 | 126 | def get_tail(self, n=10, raw=True, output=False, include_latest=False): |
|
126 | 127 | raise NotImplementedError |
|
127 | 128 | |
|
128 | 129 | def search(self, pattern="*", raw=True, search_raw=True, |
|
129 | 130 | output=False, n=None, unique=False): |
|
130 | 131 | raise NotImplementedError |
|
131 | 132 | |
|
132 | 133 | def get_range(self, session, start=1, stop=None, raw=True,output=False): |
|
133 | 134 | raise NotImplementedError |
|
134 | 135 | |
|
135 | 136 | def get_range_by_str(self, rangestr, raw=True, output=False): |
|
136 | 137 | raise NotImplementedError |
|
137 | 138 | |
|
138 | 139 | |
|
139 | 140 | class HistoryAccessor(HistoryAccessorBase): |
|
140 | 141 | """Access the history database without adding to it. |
|
141 | 142 | |
|
142 | 143 | This is intended for use by standalone history tools. IPython shells use |
|
143 | 144 | HistoryManager, below, which is a subclass of this.""" |
|
144 | 145 | |
|
145 | 146 | # counter for init_db retries, so we don't keep trying over and over |
|
146 | 147 | _corrupt_db_counter = 0 |
|
147 | 148 | # after two failures, fallback on :memory: |
|
148 | 149 | _corrupt_db_limit = 2 |
|
149 | 150 | |
|
150 | 151 | # String holding the path to the history file |
|
151 |
hist_file = Unicode( |
|
|
152 | hist_file = Unicode( | |
|
152 | 153 | help="""Path to file to use for SQLite history database. |
|
153 | 154 | |
|
154 | 155 | By default, IPython will put the history database in the IPython |
|
155 | 156 | profile directory. If you would rather share one history among |
|
156 | 157 | profiles, you can set this value in each, so that they are consistent. |
|
157 | 158 | |
|
158 | 159 | Due to an issue with fcntl, SQLite is known to misbehave on some NFS |
|
159 | 160 | mounts. If you see IPython hanging, try setting this to something on a |
|
160 | 161 | local disk, e.g:: |
|
161 | 162 | |
|
162 | 163 | ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite |
|
163 | 164 | |
|
164 | """) | |
|
165 | """).tag(config=True) | |
|
165 | 166 | |
|
166 |
enabled = Bool(True, |
|
|
167 | enabled = Bool(True, | |
|
167 | 168 | help="""enable the SQLite history |
|
168 | 169 | |
|
169 | 170 | set enabled=False to disable the SQLite history, |
|
170 | 171 | in which case there will be no stored history, no SQLite connection, |
|
171 | 172 | and no background saving thread. This may be necessary in some |
|
172 | 173 | threaded environments where IPython is embedded. |
|
173 | 174 | """ |
|
174 | ) | |
|
175 | ).tag(config=True) | |
|
175 | 176 | |
|
176 |
connection_options = Dict( |
|
|
177 | connection_options = Dict( | |
|
177 | 178 | help="""Options for configuring the SQLite connection |
|
178 | 179 | |
|
179 | 180 | These options are passed as keyword args to sqlite3.connect |
|
180 | 181 | when establishing database conenctions. |
|
181 | 182 | """ |
|
182 | ) | |
|
183 | ).tag(config=True) | |
|
183 | 184 | |
|
184 | 185 | # The SQLite database |
|
185 | 186 | db = Any() |
|
186 | def _db_changed(self, name, old, new): | |
|
187 | @observe('db') | |
|
188 | def _db_changed(self, change): | |
|
187 | 189 | """validate the db, since it can be an Instance of two different types""" |
|
190 | new = change['new'] | |
|
188 | 191 | connection_types = (DummyDB,) |
|
189 | 192 | if sqlite3 is not None: |
|
190 | 193 | connection_types = (DummyDB, sqlite3.Connection) |
|
191 | 194 | if not isinstance(new, connection_types): |
|
192 | 195 | msg = "%s.db must be sqlite3 Connection or DummyDB, not %r" % \ |
|
193 | 196 | (self.__class__.__name__, new) |
|
194 | 197 | raise TraitError(msg) |
|
195 | 198 | |
|
196 | 199 | def __init__(self, profile='default', hist_file=u'', **traits): |
|
197 | 200 | """Create a new history accessor. |
|
198 | 201 | |
|
199 | 202 | Parameters |
|
200 | 203 | ---------- |
|
201 | 204 | profile : str |
|
202 | 205 | The name of the profile from which to open history. |
|
203 | 206 | hist_file : str |
|
204 | 207 | Path to an SQLite history database stored by IPython. If specified, |
|
205 | 208 | hist_file overrides profile. |
|
206 | 209 | config : :class:`~traitlets.config.loader.Config` |
|
207 | 210 | Config object. hist_file can also be set through this. |
|
208 | 211 | """ |
|
209 | 212 | # We need a pointer back to the shell for various tasks. |
|
210 | 213 | super(HistoryAccessor, self).__init__(**traits) |
|
211 | 214 | # defer setting hist_file from kwarg until after init, |
|
212 | 215 | # otherwise the default kwarg value would clobber any value |
|
213 | 216 | # set by config |
|
214 | 217 | if hist_file: |
|
215 | 218 | self.hist_file = hist_file |
|
216 | 219 | |
|
217 | 220 | if self.hist_file == u'': |
|
218 | 221 | # No one has set the hist_file, yet. |
|
219 | 222 | self.hist_file = self._get_hist_file_name(profile) |
|
220 | 223 | |
|
221 | 224 | if sqlite3 is None and self.enabled: |
|
222 | 225 | warn("IPython History requires SQLite, your history will not be saved") |
|
223 | 226 | self.enabled = False |
|
224 | 227 | |
|
225 | 228 | self.init_db() |
|
226 | 229 | |
|
227 | 230 | def _get_hist_file_name(self, profile='default'): |
|
228 | 231 | """Find the history file for the given profile name. |
|
229 | 232 | |
|
230 | 233 | This is overridden by the HistoryManager subclass, to use the shell's |
|
231 | 234 | active profile. |
|
232 | 235 | |
|
233 | 236 | Parameters |
|
234 | 237 | ---------- |
|
235 | 238 | profile : str |
|
236 | 239 | The name of a profile which has a history file. |
|
237 | 240 | """ |
|
238 | 241 | return os.path.join(locate_profile(profile), 'history.sqlite') |
|
239 | 242 | |
|
240 | 243 | @catch_corrupt_db |
|
241 | 244 | def init_db(self): |
|
242 | 245 | """Connect to the database, and create tables if necessary.""" |
|
243 | 246 | if not self.enabled: |
|
244 | 247 | self.db = DummyDB() |
|
245 | 248 | return |
|
246 | 249 | |
|
247 | 250 | # use detect_types so that timestamps return datetime objects |
|
248 | 251 | kwargs = dict(detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) |
|
249 | 252 | kwargs.update(self.connection_options) |
|
250 | 253 | self.db = sqlite3.connect(self.hist_file, **kwargs) |
|
251 | 254 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
252 | 255 | primary key autoincrement, start timestamp, |
|
253 | 256 | end timestamp, num_cmds integer, remark text)""") |
|
254 | 257 | self.db.execute("""CREATE TABLE IF NOT EXISTS history |
|
255 | 258 | (session integer, line integer, source text, source_raw text, |
|
256 | 259 | PRIMARY KEY (session, line))""") |
|
257 | 260 | # Output history is optional, but ensure the table's there so it can be |
|
258 | 261 | # enabled later. |
|
259 | 262 | self.db.execute("""CREATE TABLE IF NOT EXISTS output_history |
|
260 | 263 | (session integer, line integer, output text, |
|
261 | 264 | PRIMARY KEY (session, line))""") |
|
262 | 265 | self.db.commit() |
|
263 | 266 | # success! reset corrupt db count |
|
264 | 267 | self._corrupt_db_counter = 0 |
|
265 | 268 | |
|
266 | 269 | def writeout_cache(self): |
|
267 | 270 | """Overridden by HistoryManager to dump the cache before certain |
|
268 | 271 | database lookups.""" |
|
269 | 272 | pass |
|
270 | 273 | |
|
271 | 274 | ## ------------------------------- |
|
272 | 275 | ## Methods for retrieving history: |
|
273 | 276 | ## ------------------------------- |
|
274 | 277 | def _run_sql(self, sql, params, raw=True, output=False): |
|
275 | 278 | """Prepares and runs an SQL query for the history database. |
|
276 | 279 | |
|
277 | 280 | Parameters |
|
278 | 281 | ---------- |
|
279 | 282 | sql : str |
|
280 | 283 | Any filtering expressions to go after SELECT ... FROM ... |
|
281 | 284 | params : tuple |
|
282 | 285 | Parameters passed to the SQL query (to replace "?") |
|
283 | 286 | raw, output : bool |
|
284 | 287 | See :meth:`get_range` |
|
285 | 288 | |
|
286 | 289 | Returns |
|
287 | 290 | ------- |
|
288 | 291 | Tuples as :meth:`get_range` |
|
289 | 292 | """ |
|
290 | 293 | toget = 'source_raw' if raw else 'source' |
|
291 | 294 | sqlfrom = "history" |
|
292 | 295 | if output: |
|
293 | 296 | sqlfrom = "history LEFT JOIN output_history USING (session, line)" |
|
294 | 297 | toget = "history.%s, output_history.output" % toget |
|
295 | 298 | cur = self.db.execute("SELECT session, line, %s FROM %s " %\ |
|
296 | 299 | (toget, sqlfrom) + sql, params) |
|
297 | 300 | if output: # Regroup into 3-tuples, and parse JSON |
|
298 | 301 | return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur) |
|
299 | 302 | return cur |
|
300 | 303 | |
|
301 | 304 | @needs_sqlite |
|
302 | 305 | @catch_corrupt_db |
|
303 | 306 | def get_session_info(self, session): |
|
304 | 307 | """Get info about a session. |
|
305 | 308 | |
|
306 | 309 | Parameters |
|
307 | 310 | ---------- |
|
308 | 311 | |
|
309 | 312 | session : int |
|
310 | 313 | Session number to retrieve. |
|
311 | 314 | |
|
312 | 315 | Returns |
|
313 | 316 | ------- |
|
314 | 317 | |
|
315 | 318 | session_id : int |
|
316 | 319 | Session ID number |
|
317 | 320 | start : datetime |
|
318 | 321 | Timestamp for the start of the session. |
|
319 | 322 | end : datetime |
|
320 | 323 | Timestamp for the end of the session, or None if IPython crashed. |
|
321 | 324 | num_cmds : int |
|
322 | 325 | Number of commands run, or None if IPython crashed. |
|
323 | 326 | remark : unicode |
|
324 | 327 | A manually set description. |
|
325 | 328 | """ |
|
326 | 329 | query = "SELECT * from sessions where session == ?" |
|
327 | 330 | return self.db.execute(query, (session,)).fetchone() |
|
328 | 331 | |
|
329 | 332 | @catch_corrupt_db |
|
330 | 333 | def get_last_session_id(self): |
|
331 | 334 | """Get the last session ID currently in the database. |
|
332 | 335 | |
|
333 | 336 | Within IPython, this should be the same as the value stored in |
|
334 | 337 | :attr:`HistoryManager.session_number`. |
|
335 | 338 | """ |
|
336 | 339 | for record in self.get_tail(n=1, include_latest=True): |
|
337 | 340 | return record[0] |
|
338 | 341 | |
|
339 | 342 | @catch_corrupt_db |
|
340 | 343 | def get_tail(self, n=10, raw=True, output=False, include_latest=False): |
|
341 | 344 | """Get the last n lines from the history database. |
|
342 | 345 | |
|
343 | 346 | Parameters |
|
344 | 347 | ---------- |
|
345 | 348 | n : int |
|
346 | 349 | The number of lines to get |
|
347 | 350 | raw, output : bool |
|
348 | 351 | See :meth:`get_range` |
|
349 | 352 | include_latest : bool |
|
350 | 353 | If False (default), n+1 lines are fetched, and the latest one |
|
351 | 354 | is discarded. This is intended to be used where the function |
|
352 | 355 | is called by a user command, which it should not return. |
|
353 | 356 | |
|
354 | 357 | Returns |
|
355 | 358 | ------- |
|
356 | 359 | Tuples as :meth:`get_range` |
|
357 | 360 | """ |
|
358 | 361 | self.writeout_cache() |
|
359 | 362 | if not include_latest: |
|
360 | 363 | n += 1 |
|
361 | 364 | cur = self._run_sql("ORDER BY session DESC, line DESC LIMIT ?", |
|
362 | 365 | (n,), raw=raw, output=output) |
|
363 | 366 | if not include_latest: |
|
364 | 367 | return reversed(list(cur)[1:]) |
|
365 | 368 | return reversed(list(cur)) |
|
366 | 369 | |
|
367 | 370 | @catch_corrupt_db |
|
368 | 371 | def search(self, pattern="*", raw=True, search_raw=True, |
|
369 | 372 | output=False, n=None, unique=False): |
|
370 | 373 | """Search the database using unix glob-style matching (wildcards |
|
371 | 374 | * and ?). |
|
372 | 375 | |
|
373 | 376 | Parameters |
|
374 | 377 | ---------- |
|
375 | 378 | pattern : str |
|
376 | 379 | The wildcarded pattern to match when searching |
|
377 | 380 | search_raw : bool |
|
378 | 381 | If True, search the raw input, otherwise, the parsed input |
|
379 | 382 | raw, output : bool |
|
380 | 383 | See :meth:`get_range` |
|
381 | 384 | n : None or int |
|
382 | 385 | If an integer is given, it defines the limit of |
|
383 | 386 | returned entries. |
|
384 | 387 | unique : bool |
|
385 | 388 | When it is true, return only unique entries. |
|
386 | 389 | |
|
387 | 390 | Returns |
|
388 | 391 | ------- |
|
389 | 392 | Tuples as :meth:`get_range` |
|
390 | 393 | """ |
|
391 | 394 | tosearch = "source_raw" if search_raw else "source" |
|
392 | 395 | if output: |
|
393 | 396 | tosearch = "history." + tosearch |
|
394 | 397 | self.writeout_cache() |
|
395 | 398 | sqlform = "WHERE %s GLOB ?" % tosearch |
|
396 | 399 | params = (pattern,) |
|
397 | 400 | if unique: |
|
398 | 401 | sqlform += ' GROUP BY {0}'.format(tosearch) |
|
399 | 402 | if n is not None: |
|
400 | 403 | sqlform += " ORDER BY session DESC, line DESC LIMIT ?" |
|
401 | 404 | params += (n,) |
|
402 | 405 | elif unique: |
|
403 | 406 | sqlform += " ORDER BY session, line" |
|
404 | 407 | cur = self._run_sql(sqlform, params, raw=raw, output=output) |
|
405 | 408 | if n is not None: |
|
406 | 409 | return reversed(list(cur)) |
|
407 | 410 | return cur |
|
408 | 411 | |
|
409 | 412 | @catch_corrupt_db |
|
410 | 413 | def get_range(self, session, start=1, stop=None, raw=True,output=False): |
|
411 | 414 | """Retrieve input by session. |
|
412 | 415 | |
|
413 | 416 | Parameters |
|
414 | 417 | ---------- |
|
415 | 418 | session : int |
|
416 | 419 | Session number to retrieve. |
|
417 | 420 | start : int |
|
418 | 421 | First line to retrieve. |
|
419 | 422 | stop : int |
|
420 | 423 | End of line range (excluded from output itself). If None, retrieve |
|
421 | 424 | to the end of the session. |
|
422 | 425 | raw : bool |
|
423 | 426 | If True, return untranslated input |
|
424 | 427 | output : bool |
|
425 | 428 | If True, attempt to include output. This will be 'real' Python |
|
426 | 429 | objects for the current session, or text reprs from previous |
|
427 | 430 | sessions if db_log_output was enabled at the time. Where no output |
|
428 | 431 | is found, None is used. |
|
429 | 432 | |
|
430 | 433 | Returns |
|
431 | 434 | ------- |
|
432 | 435 | entries |
|
433 | 436 | An iterator over the desired lines. Each line is a 3-tuple, either |
|
434 | 437 | (session, line, input) if output is False, or |
|
435 | 438 | (session, line, (input, output)) if output is True. |
|
436 | 439 | """ |
|
437 | 440 | if stop: |
|
438 | 441 | lineclause = "line >= ? AND line < ?" |
|
439 | 442 | params = (session, start, stop) |
|
440 | 443 | else: |
|
441 | 444 | lineclause = "line>=?" |
|
442 | 445 | params = (session, start) |
|
443 | 446 | |
|
444 | 447 | return self._run_sql("WHERE session==? AND %s" % lineclause, |
|
445 | 448 | params, raw=raw, output=output) |
|
446 | 449 | |
|
447 | 450 | def get_range_by_str(self, rangestr, raw=True, output=False): |
|
448 | 451 | """Get lines of history from a string of ranges, as used by magic |
|
449 | 452 | commands %hist, %save, %macro, etc. |
|
450 | 453 | |
|
451 | 454 | Parameters |
|
452 | 455 | ---------- |
|
453 | 456 | rangestr : str |
|
454 | 457 | A string specifying ranges, e.g. "5 ~2/1-4". See |
|
455 | 458 | :func:`magic_history` for full details. |
|
456 | 459 | raw, output : bool |
|
457 | 460 | As :meth:`get_range` |
|
458 | 461 | |
|
459 | 462 | Returns |
|
460 | 463 | ------- |
|
461 | 464 | Tuples as :meth:`get_range` |
|
462 | 465 | """ |
|
463 | 466 | for sess, s, e in extract_hist_ranges(rangestr): |
|
464 | 467 | for line in self.get_range(sess, s, e, raw=raw, output=output): |
|
465 | 468 | yield line |
|
466 | 469 | |
|
467 | 470 | |
|
468 | 471 | class HistoryManager(HistoryAccessor): |
|
469 | 472 | """A class to organize all history-related functionality in one place. |
|
470 | 473 | """ |
|
471 | 474 | # Public interface |
|
472 | 475 | |
|
473 | 476 | # An instance of the IPython shell we are attached to |
|
474 | 477 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
475 | 478 | allow_none=True) |
|
476 | 479 | # Lists to hold processed and raw history. These start with a blank entry |
|
477 | 480 | # so that we can index them starting from 1 |
|
478 | 481 | input_hist_parsed = List([""]) |
|
479 | 482 | input_hist_raw = List([""]) |
|
480 | 483 | # A list of directories visited during session |
|
481 | 484 | dir_hist = List() |
|
485 | @default('dir_hist') | |
|
482 | 486 | def _dir_hist_default(self): |
|
483 | 487 | try: |
|
484 | 488 | return [py3compat.getcwd()] |
|
485 | 489 | except OSError: |
|
486 | 490 | return [] |
|
487 | 491 | |
|
488 | 492 | # A dict of output history, keyed with ints from the shell's |
|
489 | 493 | # execution count. |
|
490 | 494 | output_hist = Dict() |
|
491 | 495 | # The text/plain repr of outputs. |
|
492 | 496 | output_hist_reprs = Dict() |
|
493 | 497 | |
|
494 | 498 | # The number of the current session in the history database |
|
495 | 499 | session_number = Integer() |
|
496 | 500 | |
|
497 |
db_log_output = Bool(False, |
|
|
501 | db_log_output = Bool(False, | |
|
498 | 502 | help="Should the history database include output? (default: no)" |
|
499 | ) | |
|
500 |
db_cache_size = Integer(0, |
|
|
503 | ).tag(config=True) | |
|
504 | db_cache_size = Integer(0, | |
|
501 | 505 | help="Write to database every x commands (higher values save disk access & power).\n" |
|
502 | 506 | "Values of 1 or less effectively disable caching." |
|
503 | ) | |
|
507 | ).tag(config=True) | |
|
504 | 508 | # The input and output caches |
|
505 | 509 | db_input_cache = List() |
|
506 | 510 | db_output_cache = List() |
|
507 | 511 | |
|
508 | 512 | # History saving in separate thread |
|
509 | 513 | save_thread = Instance('IPython.core.history.HistorySavingThread', |
|
510 | 514 | allow_none=True) |
|
511 | 515 | try: # Event is a function returning an instance of _Event... |
|
512 | 516 | save_flag = Instance(threading._Event, allow_none=True) |
|
513 | 517 | except AttributeError: # ...until Python 3.3, when it's a class. |
|
514 | 518 | save_flag = Instance(threading.Event, allow_none=True) |
|
515 | 519 | |
|
516 | 520 | # Private interface |
|
517 | 521 | # Variables used to store the three last inputs from the user. On each new |
|
518 | 522 | # history update, we populate the user's namespace with these, shifted as |
|
519 | 523 | # necessary. |
|
520 | 524 | _i00 = Unicode(u'') |
|
521 | 525 | _i = Unicode(u'') |
|
522 | 526 | _ii = Unicode(u'') |
|
523 | 527 | _iii = Unicode(u'') |
|
524 | 528 | |
|
525 | 529 | # A regex matching all forms of the exit command, so that we don't store |
|
526 | 530 | # them in the history (it's annoying to rewind the first entry and land on |
|
527 | 531 | # an exit call). |
|
528 | 532 | _exit_re = re.compile(r"(exit|quit)(\s*\(.*\))?$") |
|
529 | 533 | |
|
530 | 534 | def __init__(self, shell=None, config=None, **traits): |
|
531 | 535 | """Create a new history manager associated with a shell instance. |
|
532 | 536 | """ |
|
533 | 537 | # We need a pointer back to the shell for various tasks. |
|
534 | 538 | super(HistoryManager, self).__init__(shell=shell, config=config, |
|
535 | 539 | **traits) |
|
536 | 540 | self.save_flag = threading.Event() |
|
537 | 541 | self.db_input_cache_lock = threading.Lock() |
|
538 | 542 | self.db_output_cache_lock = threading.Lock() |
|
539 | 543 | |
|
540 | 544 | try: |
|
541 | 545 | self.new_session() |
|
542 | 546 | except OperationalError: |
|
543 | 547 | self.log.error("Failed to create history session in %s. History will not be saved.", |
|
544 | 548 | self.hist_file, exc_info=True) |
|
545 | 549 | self.hist_file = ':memory:' |
|
546 | 550 | |
|
547 | 551 | if self.enabled and self.hist_file != ':memory:': |
|
548 | 552 | self.save_thread = HistorySavingThread(self) |
|
549 | 553 | self.save_thread.start() |
|
550 | 554 | |
|
551 | 555 | def _get_hist_file_name(self, profile=None): |
|
552 | 556 | """Get default history file name based on the Shell's profile. |
|
553 | 557 | |
|
554 | 558 | The profile parameter is ignored, but must exist for compatibility with |
|
555 | 559 | the parent class.""" |
|
556 | 560 | profile_dir = self.shell.profile_dir.location |
|
557 | 561 | return os.path.join(profile_dir, 'history.sqlite') |
|
558 | 562 | |
|
559 | 563 | @needs_sqlite |
|
560 | 564 | def new_session(self, conn=None): |
|
561 | 565 | """Get a new session number.""" |
|
562 | 566 | if conn is None: |
|
563 | 567 | conn = self.db |
|
564 | 568 | |
|
565 | 569 | with conn: |
|
566 | 570 | cur = conn.execute("""INSERT INTO sessions VALUES (NULL, ?, NULL, |
|
567 | 571 | NULL, "") """, (datetime.datetime.now(),)) |
|
568 | 572 | self.session_number = cur.lastrowid |
|
569 | 573 | |
|
570 | 574 | def end_session(self): |
|
571 | 575 | """Close the database session, filling in the end time and line count.""" |
|
572 | 576 | self.writeout_cache() |
|
573 | 577 | with self.db: |
|
574 | 578 | self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE |
|
575 | 579 | session==?""", (datetime.datetime.now(), |
|
576 | 580 | len(self.input_hist_parsed)-1, self.session_number)) |
|
577 | 581 | self.session_number = 0 |
|
578 | 582 | |
|
579 | 583 | def name_session(self, name): |
|
580 | 584 | """Give the current session a name in the history database.""" |
|
581 | 585 | with self.db: |
|
582 | 586 | self.db.execute("UPDATE sessions SET remark=? WHERE session==?", |
|
583 | 587 | (name, self.session_number)) |
|
584 | 588 | |
|
585 | 589 | def reset(self, new_session=True): |
|
586 | 590 | """Clear the session history, releasing all object references, and |
|
587 | 591 | optionally open a new session.""" |
|
588 | 592 | self.output_hist.clear() |
|
589 | 593 | # The directory history can't be completely empty |
|
590 | 594 | self.dir_hist[:] = [py3compat.getcwd()] |
|
591 | 595 | |
|
592 | 596 | if new_session: |
|
593 | 597 | if self.session_number: |
|
594 | 598 | self.end_session() |
|
595 | 599 | self.input_hist_parsed[:] = [""] |
|
596 | 600 | self.input_hist_raw[:] = [""] |
|
597 | 601 | self.new_session() |
|
598 | 602 | |
|
599 | 603 | # ------------------------------ |
|
600 | 604 | # Methods for retrieving history |
|
601 | 605 | # ------------------------------ |
|
602 | 606 | def get_session_info(self, session=0): |
|
603 | 607 | """Get info about a session. |
|
604 | 608 | |
|
605 | 609 | Parameters |
|
606 | 610 | ---------- |
|
607 | 611 | |
|
608 | 612 | session : int |
|
609 | 613 | Session number to retrieve. The current session is 0, and negative |
|
610 | 614 | numbers count back from current session, so -1 is the previous session. |
|
611 | 615 | |
|
612 | 616 | Returns |
|
613 | 617 | ------- |
|
614 | 618 | |
|
615 | 619 | session_id : int |
|
616 | 620 | Session ID number |
|
617 | 621 | start : datetime |
|
618 | 622 | Timestamp for the start of the session. |
|
619 | 623 | end : datetime |
|
620 | 624 | Timestamp for the end of the session, or None if IPython crashed. |
|
621 | 625 | num_cmds : int |
|
622 | 626 | Number of commands run, or None if IPython crashed. |
|
623 | 627 | remark : unicode |
|
624 | 628 | A manually set description. |
|
625 | 629 | """ |
|
626 | 630 | if session <= 0: |
|
627 | 631 | session += self.session_number |
|
628 | 632 | |
|
629 | 633 | return super(HistoryManager, self).get_session_info(session=session) |
|
630 | 634 | |
|
631 | 635 | def _get_range_session(self, start=1, stop=None, raw=True, output=False): |
|
632 | 636 | """Get input and output history from the current session. Called by |
|
633 | 637 | get_range, and takes similar parameters.""" |
|
634 | 638 | input_hist = self.input_hist_raw if raw else self.input_hist_parsed |
|
635 | 639 | |
|
636 | 640 | n = len(input_hist) |
|
637 | 641 | if start < 0: |
|
638 | 642 | start += n |
|
639 | 643 | if not stop or (stop > n): |
|
640 | 644 | stop = n |
|
641 | 645 | elif stop < 0: |
|
642 | 646 | stop += n |
|
643 | 647 | |
|
644 | 648 | for i in range(start, stop): |
|
645 | 649 | if output: |
|
646 | 650 | line = (input_hist[i], self.output_hist_reprs.get(i)) |
|
647 | 651 | else: |
|
648 | 652 | line = input_hist[i] |
|
649 | 653 | yield (0, i, line) |
|
650 | 654 | |
|
651 | 655 | def get_range(self, session=0, start=1, stop=None, raw=True,output=False): |
|
652 | 656 | """Retrieve input by session. |
|
653 | 657 | |
|
654 | 658 | Parameters |
|
655 | 659 | ---------- |
|
656 | 660 | session : int |
|
657 | 661 | Session number to retrieve. The current session is 0, and negative |
|
658 | 662 | numbers count back from current session, so -1 is previous session. |
|
659 | 663 | start : int |
|
660 | 664 | First line to retrieve. |
|
661 | 665 | stop : int |
|
662 | 666 | End of line range (excluded from output itself). If None, retrieve |
|
663 | 667 | to the end of the session. |
|
664 | 668 | raw : bool |
|
665 | 669 | If True, return untranslated input |
|
666 | 670 | output : bool |
|
667 | 671 | If True, attempt to include output. This will be 'real' Python |
|
668 | 672 | objects for the current session, or text reprs from previous |
|
669 | 673 | sessions if db_log_output was enabled at the time. Where no output |
|
670 | 674 | is found, None is used. |
|
671 | 675 | |
|
672 | 676 | Returns |
|
673 | 677 | ------- |
|
674 | 678 | entries |
|
675 | 679 | An iterator over the desired lines. Each line is a 3-tuple, either |
|
676 | 680 | (session, line, input) if output is False, or |
|
677 | 681 | (session, line, (input, output)) if output is True. |
|
678 | 682 | """ |
|
679 | 683 | if session <= 0: |
|
680 | 684 | session += self.session_number |
|
681 | 685 | if session==self.session_number: # Current session |
|
682 | 686 | return self._get_range_session(start, stop, raw, output) |
|
683 | 687 | return super(HistoryManager, self).get_range(session, start, stop, raw, |
|
684 | 688 | output) |
|
685 | 689 | |
|
686 | 690 | ## ---------------------------- |
|
687 | 691 | ## Methods for storing history: |
|
688 | 692 | ## ---------------------------- |
|
689 | 693 | def store_inputs(self, line_num, source, source_raw=None): |
|
690 | 694 | """Store source and raw input in history and create input cache |
|
691 | 695 | variables ``_i*``. |
|
692 | 696 | |
|
693 | 697 | Parameters |
|
694 | 698 | ---------- |
|
695 | 699 | line_num : int |
|
696 | 700 | The prompt number of this input. |
|
697 | 701 | |
|
698 | 702 | source : str |
|
699 | 703 | Python input. |
|
700 | 704 | |
|
701 | 705 | source_raw : str, optional |
|
702 | 706 | If given, this is the raw input without any IPython transformations |
|
703 | 707 | applied to it. If not given, ``source`` is used. |
|
704 | 708 | """ |
|
705 | 709 | if source_raw is None: |
|
706 | 710 | source_raw = source |
|
707 | 711 | source = source.rstrip('\n') |
|
708 | 712 | source_raw = source_raw.rstrip('\n') |
|
709 | 713 | |
|
710 | 714 | # do not store exit/quit commands |
|
711 | 715 | if self._exit_re.match(source_raw.strip()): |
|
712 | 716 | return |
|
713 | 717 | |
|
714 | 718 | self.input_hist_parsed.append(source) |
|
715 | 719 | self.input_hist_raw.append(source_raw) |
|
716 | 720 | |
|
717 | 721 | with self.db_input_cache_lock: |
|
718 | 722 | self.db_input_cache.append((line_num, source, source_raw)) |
|
719 | 723 | # Trigger to flush cache and write to DB. |
|
720 | 724 | if len(self.db_input_cache) >= self.db_cache_size: |
|
721 | 725 | self.save_flag.set() |
|
722 | 726 | |
|
723 | 727 | # update the auto _i variables |
|
724 | 728 | self._iii = self._ii |
|
725 | 729 | self._ii = self._i |
|
726 | 730 | self._i = self._i00 |
|
727 | 731 | self._i00 = source_raw |
|
728 | 732 | |
|
729 | 733 | # hackish access to user namespace to create _i1,_i2... dynamically |
|
730 | 734 | new_i = '_i%s' % line_num |
|
731 | 735 | to_main = {'_i': self._i, |
|
732 | 736 | '_ii': self._ii, |
|
733 | 737 | '_iii': self._iii, |
|
734 | 738 | new_i : self._i00 } |
|
735 | 739 | |
|
736 | 740 | if self.shell is not None: |
|
737 | 741 | self.shell.push(to_main, interactive=False) |
|
738 | 742 | |
|
739 | 743 | def store_output(self, line_num): |
|
740 | 744 | """If database output logging is enabled, this saves all the |
|
741 | 745 | outputs from the indicated prompt number to the database. It's |
|
742 | 746 | called by run_cell after code has been executed. |
|
743 | 747 | |
|
744 | 748 | Parameters |
|
745 | 749 | ---------- |
|
746 | 750 | line_num : int |
|
747 | 751 | The line number from which to save outputs |
|
748 | 752 | """ |
|
749 | 753 | if (not self.db_log_output) or (line_num not in self.output_hist_reprs): |
|
750 | 754 | return |
|
751 | 755 | output = self.output_hist_reprs[line_num] |
|
752 | 756 | |
|
753 | 757 | with self.db_output_cache_lock: |
|
754 | 758 | self.db_output_cache.append((line_num, output)) |
|
755 | 759 | if self.db_cache_size <= 1: |
|
756 | 760 | self.save_flag.set() |
|
757 | 761 | |
|
758 | 762 | def _writeout_input_cache(self, conn): |
|
759 | 763 | with conn: |
|
760 | 764 | for line in self.db_input_cache: |
|
761 | 765 | conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)", |
|
762 | 766 | (self.session_number,)+line) |
|
763 | 767 | |
|
764 | 768 | def _writeout_output_cache(self, conn): |
|
765 | 769 | with conn: |
|
766 | 770 | for line in self.db_output_cache: |
|
767 | 771 | conn.execute("INSERT INTO output_history VALUES (?, ?, ?)", |
|
768 | 772 | (self.session_number,)+line) |
|
769 | 773 | |
|
770 | 774 | @needs_sqlite |
|
771 | 775 | def writeout_cache(self, conn=None): |
|
772 | 776 | """Write any entries in the cache to the database.""" |
|
773 | 777 | if conn is None: |
|
774 | 778 | conn = self.db |
|
775 | 779 | |
|
776 | 780 | with self.db_input_cache_lock: |
|
777 | 781 | try: |
|
778 | 782 | self._writeout_input_cache(conn) |
|
779 | 783 | except sqlite3.IntegrityError: |
|
780 | 784 | self.new_session(conn) |
|
781 | 785 | print("ERROR! Session/line number was not unique in", |
|
782 | 786 | "database. History logging moved to new session", |
|
783 | 787 | self.session_number) |
|
784 | 788 | try: |
|
785 | 789 | # Try writing to the new session. If this fails, don't |
|
786 | 790 | # recurse |
|
787 | 791 | self._writeout_input_cache(conn) |
|
788 | 792 | except sqlite3.IntegrityError: |
|
789 | 793 | pass |
|
790 | 794 | finally: |
|
791 | 795 | self.db_input_cache = [] |
|
792 | 796 | |
|
793 | 797 | with self.db_output_cache_lock: |
|
794 | 798 | try: |
|
795 | 799 | self._writeout_output_cache(conn) |
|
796 | 800 | except sqlite3.IntegrityError: |
|
797 | 801 | print("!! Session/line number for output was not unique", |
|
798 | 802 | "in database. Output will not be stored.") |
|
799 | 803 | finally: |
|
800 | 804 | self.db_output_cache = [] |
|
801 | 805 | |
|
802 | 806 | |
|
803 | 807 | class HistorySavingThread(threading.Thread): |
|
804 | 808 | """This thread takes care of writing history to the database, so that |
|
805 | 809 | the UI isn't held up while that happens. |
|
806 | 810 | |
|
807 | 811 | It waits for the HistoryManager's save_flag to be set, then writes out |
|
808 | 812 | the history cache. The main thread is responsible for setting the flag when |
|
809 | 813 | the cache size reaches a defined threshold.""" |
|
810 | 814 | daemon = True |
|
811 | 815 | stop_now = False |
|
812 | 816 | enabled = True |
|
813 | 817 | def __init__(self, history_manager): |
|
814 | 818 | super(HistorySavingThread, self).__init__(name="IPythonHistorySavingThread") |
|
815 | 819 | self.history_manager = history_manager |
|
816 | 820 | self.enabled = history_manager.enabled |
|
817 | 821 | atexit.register(self.stop) |
|
818 | 822 | |
|
819 | 823 | @needs_sqlite |
|
820 | 824 | def run(self): |
|
821 | 825 | # We need a separate db connection per thread: |
|
822 | 826 | try: |
|
823 | 827 | self.db = sqlite3.connect(self.history_manager.hist_file, |
|
824 | 828 | **self.history_manager.connection_options |
|
825 | 829 | ) |
|
826 | 830 | while True: |
|
827 | 831 | self.history_manager.save_flag.wait() |
|
828 | 832 | if self.stop_now: |
|
829 | 833 | self.db.close() |
|
830 | 834 | return |
|
831 | 835 | self.history_manager.save_flag.clear() |
|
832 | 836 | self.history_manager.writeout_cache(self.db) |
|
833 | 837 | except Exception as e: |
|
834 | 838 | print(("The history saving thread hit an unexpected error (%s)." |
|
835 | 839 | "History will not be written to the database.") % repr(e)) |
|
836 | 840 | |
|
837 | 841 | def stop(self): |
|
838 | 842 | """This can be called from the main thread to safely stop this thread. |
|
839 | 843 | |
|
840 | 844 | Note that it does not attempt to write out remaining history before |
|
841 | 845 | exiting. That should be done by calling the HistoryManager's |
|
842 | 846 | end_session method.""" |
|
843 | 847 | self.stop_now = True |
|
844 | 848 | self.history_manager.save_flag.set() |
|
845 | 849 | self.join() |
|
846 | 850 | |
|
847 | 851 | |
|
848 | 852 | # To match, e.g. ~5/8-~2/3 |
|
849 | 853 | range_re = re.compile(r""" |
|
850 | 854 | ((?P<startsess>~?\d+)/)? |
|
851 | 855 | (?P<start>\d+)? |
|
852 | 856 | ((?P<sep>[\-:]) |
|
853 | 857 | ((?P<endsess>~?\d+)/)? |
|
854 | 858 | (?P<end>\d+))? |
|
855 | 859 | $""", re.VERBOSE) |
|
856 | 860 | |
|
857 | 861 | |
|
858 | 862 | def extract_hist_ranges(ranges_str): |
|
859 | 863 | """Turn a string of history ranges into 3-tuples of (session, start, stop). |
|
860 | 864 | |
|
861 | 865 | Examples |
|
862 | 866 | -------- |
|
863 | 867 | >>> list(extract_hist_ranges("~8/5-~7/4 2")) |
|
864 | 868 | [(-8, 5, None), (-7, 1, 5), (0, 2, 3)] |
|
865 | 869 | """ |
|
866 | 870 | for range_str in ranges_str.split(): |
|
867 | 871 | rmatch = range_re.match(range_str) |
|
868 | 872 | if not rmatch: |
|
869 | 873 | continue |
|
870 | 874 | start = rmatch.group("start") |
|
871 | 875 | if start: |
|
872 | 876 | start = int(start) |
|
873 | 877 | end = rmatch.group("end") |
|
874 | 878 | # If no end specified, get (a, a + 1) |
|
875 | 879 | end = int(end) if end else start + 1 |
|
876 | 880 | else: # start not specified |
|
877 | 881 | if not rmatch.group('startsess'): # no startsess |
|
878 | 882 | continue |
|
879 | 883 | start = 1 |
|
880 | 884 | end = None # provide the entire session hist |
|
881 | 885 | |
|
882 | 886 | if rmatch.group("sep") == "-": # 1-3 == 1:4 --> [1, 2, 3] |
|
883 | 887 | end += 1 |
|
884 | 888 | startsess = rmatch.group("startsess") or "0" |
|
885 | 889 | endsess = rmatch.group("endsess") or startsess |
|
886 | 890 | startsess = int(startsess.replace("~","-")) |
|
887 | 891 | endsess = int(endsess.replace("~","-")) |
|
888 | 892 | assert endsess >= startsess, "start session must be earlier than end session" |
|
889 | 893 | |
|
890 | 894 | if endsess == startsess: |
|
891 | 895 | yield (startsess, start, end) |
|
892 | 896 | continue |
|
893 | 897 | # Multiple sessions in one range: |
|
894 | 898 | yield (startsess, start, None) |
|
895 | 899 | for sess in range(startsess+1, endsess): |
|
896 | 900 | yield (sess, 1, None) |
|
897 | 901 | yield (endsess, 1, end) |
|
898 | 902 | |
|
899 | 903 | |
|
900 | 904 | def _format_lineno(session, line): |
|
901 | 905 | """Helper function to format line numbers properly.""" |
|
902 | 906 | if session == 0: |
|
903 | 907 | return str(line) |
|
904 | 908 | return "%s#%s" % (session, line) |
@@ -1,3241 +1,3246 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Main IPython class.""" |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
6 | 6 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | from __future__ import absolute_import, print_function |
|
14 | 14 | |
|
15 | 15 | import __future__ |
|
16 | 16 | import abc |
|
17 | 17 | import ast |
|
18 | 18 | import atexit |
|
19 | 19 | import functools |
|
20 | 20 | import os |
|
21 | 21 | import re |
|
22 | 22 | import runpy |
|
23 | 23 | import sys |
|
24 | 24 | import tempfile |
|
25 | 25 | import traceback |
|
26 | 26 | import types |
|
27 | 27 | import subprocess |
|
28 | 28 | import warnings |
|
29 | 29 | from io import open as io_open |
|
30 | 30 | |
|
31 | 31 | from pickleshare import PickleShareDB |
|
32 | 32 | |
|
33 | 33 | from traitlets.config.configurable import SingletonConfigurable |
|
34 | 34 | from IPython.core import debugger, oinspect |
|
35 | 35 | from IPython.core import magic |
|
36 | 36 | from IPython.core import page |
|
37 | 37 | from IPython.core import prefilter |
|
38 | 38 | from IPython.core import shadowns |
|
39 | 39 | from IPython.core import ultratb |
|
40 | 40 | from IPython.core.alias import Alias, AliasManager |
|
41 | 41 | from IPython.core.autocall import ExitAutocall |
|
42 | 42 | from IPython.core.builtin_trap import BuiltinTrap |
|
43 | 43 | from IPython.core.events import EventManager, available_events |
|
44 | 44 | from IPython.core.compilerop import CachingCompiler, check_linecache_ipython |
|
45 | 45 | from IPython.core.display_trap import DisplayTrap |
|
46 | 46 | from IPython.core.displayhook import DisplayHook |
|
47 | 47 | from IPython.core.displaypub import DisplayPublisher |
|
48 | 48 | from IPython.core.error import InputRejected, UsageError |
|
49 | 49 | from IPython.core.extensions import ExtensionManager |
|
50 | 50 | from IPython.core.formatters import DisplayFormatter |
|
51 | 51 | from IPython.core.history import HistoryManager |
|
52 |
from IPython.core.inputsplitter import |
|
|
52 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 | |
|
53 | 53 | from IPython.core.logger import Logger |
|
54 | 54 | from IPython.core.macro import Macro |
|
55 | 55 | from IPython.core.payload import PayloadManager |
|
56 | 56 | from IPython.core.prefilter import PrefilterManager |
|
57 | 57 | from IPython.core.profiledir import ProfileDir |
|
58 | 58 | from IPython.core.prompts import PromptManager |
|
59 | 59 | from IPython.core.usage import default_banner |
|
60 | 60 | from IPython.testing.skipdoctest import skip_doctest_py2, skip_doctest |
|
61 | 61 | from IPython.utils import PyColorize |
|
62 | 62 | from IPython.utils import io |
|
63 | 63 | from IPython.utils import py3compat |
|
64 | 64 | from IPython.utils import openpy |
|
65 | 65 | from IPython.utils.contexts import NoOpContext |
|
66 | 66 | from IPython.utils.decorators import undoc |
|
67 | 67 | from IPython.utils.io import ask_yes_no |
|
68 | 68 | from IPython.utils.ipstruct import Struct |
|
69 | 69 | from IPython.paths import get_ipython_dir |
|
70 | 70 | from IPython.utils.path import get_home_dir, get_py_filename, unquote_filename, ensure_dir_exists |
|
71 | 71 | from IPython.utils.process import system, getoutput |
|
72 | 72 | from IPython.utils.py3compat import (builtin_mod, unicode_type, string_types, |
|
73 | 73 | with_metaclass, iteritems) |
|
74 | 74 | from IPython.utils.strdispatch import StrDispatch |
|
75 | 75 | from IPython.utils.syspathcontext import prepended_to_syspath |
|
76 | 76 | from IPython.utils.text import (format_screen, LSString, SList, |
|
77 | 77 | DollarFormatter) |
|
78 | from traitlets import (Integer, Bool, CBool, CaselessStrEnum, Enum, | |
|
79 | List, Dict, Unicode, Instance, Type) | |
|
78 | from traitlets import ( | |
|
79 | Integer, Bool, CaselessStrEnum, Enum, List, Dict, Unicode, Instance, Type, | |
|
80 | observe, default, | |
|
81 | ) | |
|
80 | 82 | from warnings import warn |
|
81 | 83 | from logging import error |
|
82 | 84 | import IPython.core.hooks |
|
83 | 85 | |
|
84 | 86 | #----------------------------------------------------------------------------- |
|
85 | 87 | # Globals |
|
86 | 88 | #----------------------------------------------------------------------------- |
|
87 | 89 | |
|
88 | 90 | # compiled regexps for autoindent management |
|
89 | 91 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
90 | 92 | |
|
91 | 93 | #----------------------------------------------------------------------------- |
|
92 | 94 | # Utilities |
|
93 | 95 | #----------------------------------------------------------------------------- |
|
94 | 96 | |
|
95 | 97 | @undoc |
|
96 | 98 | def softspace(file, newvalue): |
|
97 | 99 | """Copied from code.py, to remove the dependency""" |
|
98 | 100 | |
|
99 | 101 | oldvalue = 0 |
|
100 | 102 | try: |
|
101 | 103 | oldvalue = file.softspace |
|
102 | 104 | except AttributeError: |
|
103 | 105 | pass |
|
104 | 106 | try: |
|
105 | 107 | file.softspace = newvalue |
|
106 | 108 | except (AttributeError, TypeError): |
|
107 | 109 | # "attribute-less object" or "read-only attributes" |
|
108 | 110 | pass |
|
109 | 111 | return oldvalue |
|
110 | 112 | |
|
111 | 113 | @undoc |
|
112 | 114 | def no_op(*a, **kw): pass |
|
113 | 115 | |
|
114 | 116 | |
|
115 | 117 | class SpaceInInput(Exception): pass |
|
116 | 118 | |
|
117 | @undoc | |
|
118 | class Bunch: pass | |
|
119 | ||
|
120 | 119 | |
|
121 | 120 | def get_default_colors(): |
|
122 | 121 | if sys.platform=='darwin': |
|
123 | 122 | return "LightBG" |
|
124 | 123 | elif os.name=='nt': |
|
125 | 124 | return 'Linux' |
|
126 | 125 | else: |
|
127 | 126 | return 'Linux' |
|
128 | 127 | |
|
129 | 128 | |
|
130 | 129 | class SeparateUnicode(Unicode): |
|
131 | 130 | r"""A Unicode subclass to validate separate_in, separate_out, etc. |
|
132 | 131 | |
|
133 | 132 | This is a Unicode based trait that converts '0'->'' and ``'\\n'->'\n'``. |
|
134 | 133 | """ |
|
135 | 134 | |
|
136 | 135 | def validate(self, obj, value): |
|
137 | 136 | if value == '0': value = '' |
|
138 | 137 | value = value.replace('\\n','\n') |
|
139 | 138 | return super(SeparateUnicode, self).validate(obj, value) |
|
140 | 139 | |
|
141 | 140 | |
|
142 | 141 | @undoc |
|
143 | 142 | class DummyMod(object): |
|
144 | 143 | """A dummy module used for IPython's interactive module when |
|
145 | 144 | a namespace must be assigned to the module's __dict__.""" |
|
146 | 145 | pass |
|
147 | 146 | |
|
148 | 147 | |
|
149 | 148 | class ExecutionResult(object): |
|
150 | 149 | """The result of a call to :meth:`InteractiveShell.run_cell` |
|
151 | 150 | |
|
152 | 151 | Stores information about what took place. |
|
153 | 152 | """ |
|
154 | 153 | execution_count = None |
|
155 | 154 | error_before_exec = None |
|
156 | 155 | error_in_exec = None |
|
157 | 156 | result = None |
|
158 | 157 | |
|
159 | 158 | @property |
|
160 | 159 | def success(self): |
|
161 | 160 | return (self.error_before_exec is None) and (self.error_in_exec is None) |
|
162 | 161 | |
|
163 | 162 | def raise_error(self): |
|
164 | 163 | """Reraises error if `success` is `False`, otherwise does nothing""" |
|
165 | 164 | if self.error_before_exec is not None: |
|
166 | 165 | raise self.error_before_exec |
|
167 | 166 | if self.error_in_exec is not None: |
|
168 | 167 | raise self.error_in_exec |
|
169 | 168 | |
|
170 | 169 | |
|
171 | 170 | class InteractiveShell(SingletonConfigurable): |
|
172 | 171 | """An enhanced, interactive shell for Python.""" |
|
173 | 172 | |
|
174 | 173 | _instance = None |
|
175 | 174 | |
|
176 |
ast_transformers = List([], |
|
|
175 | ast_transformers = List([], help= | |
|
177 | 176 | """ |
|
178 | 177 | A list of ast.NodeTransformer subclass instances, which will be applied |
|
179 | 178 | to user input before code is run. |
|
180 | 179 | """ |
|
181 | ) | |
|
180 | ).tag(config=True) | |
|
182 | 181 | |
|
183 |
autocall = Enum((0,1,2), default_value=0, |
|
|
182 | autocall = Enum((0,1,2), default_value=0, help= | |
|
184 | 183 | """ |
|
185 | 184 | Make IPython automatically call any callable object even if you didn't |
|
186 | 185 | type explicit parentheses. For example, 'str 43' becomes 'str(43)' |
|
187 | 186 | automatically. The value can be '0' to disable the feature, '1' for |
|
188 | 187 | 'smart' autocall, where it is not applied if there are no more |
|
189 | 188 | arguments on the line, and '2' for 'full' autocall, where all callable |
|
190 | 189 | objects are automatically called (even if no arguments are present). |
|
191 | 190 | """ |
|
192 | ) | |
|
191 | ).tag(config=True) | |
|
193 | 192 | # TODO: remove all autoindent logic and put into frontends. |
|
194 | 193 | # We can't do this yet because even runlines uses the autoindent. |
|
195 |
autoindent = |
|
|
194 | autoindent = Bool(True, help= | |
|
196 | 195 | """ |
|
197 | 196 | Autoindent IPython code entered interactively. |
|
198 | 197 | """ |
|
199 | ) | |
|
200 |
automagic = |
|
|
198 | ).tag(config=True) | |
|
199 | automagic = Bool(True, help= | |
|
201 | 200 | """ |
|
202 | 201 | Enable magic commands to be called without the leading %. |
|
203 | 202 | """ |
|
204 | ) | |
|
203 | ).tag(config=True) | |
|
205 | 204 | |
|
206 |
banner1 = Unicode(default_banner, |
|
|
205 | banner1 = Unicode(default_banner, | |
|
207 | 206 | help="""The part of the banner to be printed before the profile""" |
|
208 | ) | |
|
209 |
banner2 = Unicode('', |
|
|
207 | ).tag(config=True) | |
|
208 | banner2 = Unicode('', | |
|
210 | 209 | help="""The part of the banner to be printed after the profile""" |
|
211 | ) | |
|
210 | ).tag(config=True) | |
|
212 | 211 | |
|
213 |
cache_size = Integer(1000, |
|
|
212 | cache_size = Integer(1000, help= | |
|
214 | 213 | """ |
|
215 | 214 | Set the size of the output cache. The default is 1000, you can |
|
216 | 215 | change it permanently in your config file. Setting it to 0 completely |
|
217 | 216 | disables the caching system, and the minimum value accepted is 20 (if |
|
218 | 217 | you provide a value less than 20, it is reset to 0 and a warning is |
|
219 | 218 | issued). This limit is defined because otherwise you'll spend more |
|
220 | 219 | time re-flushing a too small cache than working |
|
221 | 220 | """ |
|
222 | ) | |
|
223 |
color_info = |
|
|
221 | ).tag(config=True) | |
|
222 | color_info = Bool(True, help= | |
|
224 | 223 | """ |
|
225 | 224 | Use colors for displaying information about objects. Because this |
|
226 | 225 | information is passed through a pager (like 'less'), and some pagers |
|
227 | 226 | get confused with color codes, this capability can be turned off. |
|
228 | 227 | """ |
|
229 | ) | |
|
228 | ).tag(config=True) | |
|
230 | 229 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
231 |
default_value=get_default_colors(), |
|
|
230 | default_value=get_default_colors(), | |
|
232 | 231 | help="Set the color scheme (NoColor, Linux, or LightBG)." |
|
233 | ) | |
|
234 |
colors_force = |
|
|
232 | ).tag(config=True) | |
|
233 | colors_force = Bool(False, help= | |
|
235 | 234 | """ |
|
236 | 235 | Force use of ANSI color codes, regardless of OS and readline |
|
237 | 236 | availability. |
|
238 | 237 | """ |
|
239 | 238 | # FIXME: This is essentially a hack to allow ZMQShell to show colors |
|
240 | 239 | # without readline on Win32. When the ZMQ formatting system is |
|
241 | 240 | # refactored, this should be removed. |
|
242 | 241 | ) |
|
243 |
debug = |
|
|
244 |
deep_reload = |
|
|
242 | debug = Bool(False).tag(config=True) | |
|
243 | deep_reload = Bool(False, help= | |
|
245 | 244 | """ |
|
246 | 245 | **Deprecated** |
|
247 | 246 | |
|
248 | 247 | Will be removed in IPython 6.0 |
|
249 | 248 | |
|
250 | 249 | Enable deep (recursive) reloading by default. IPython can use the |
|
251 | 250 | deep_reload module which reloads changes in modules recursively (it |
|
252 | 251 | replaces the reload() function, so you don't need to change anything to |
|
253 | 252 | use it). `deep_reload` forces a full reload of modules whose code may |
|
254 | 253 | have changed, which the default reload() function does not. When |
|
255 | 254 | deep_reload is off, IPython will use the normal reload(), but |
|
256 | 255 | deep_reload will still be available as dreload(). |
|
257 | 256 | """ |
|
258 | ) | |
|
259 |
disable_failing_post_execute = |
|
|
257 | ).tag(config=True) | |
|
258 | disable_failing_post_execute = Bool(False, | |
|
260 | 259 | help="Don't call post-execute functions that have failed in the past." |
|
261 | ) | |
|
260 | ).tag(config=True) | |
|
262 | 261 | display_formatter = Instance(DisplayFormatter, allow_none=True) |
|
263 | 262 | displayhook_class = Type(DisplayHook) |
|
264 | 263 | display_pub_class = Type(DisplayPublisher) |
|
265 | 264 | data_pub_class = None |
|
266 | 265 | |
|
267 |
exit_now = |
|
|
266 | exit_now = Bool(False) | |
|
268 | 267 | exiter = Instance(ExitAutocall) |
|
268 | @default('exiter') | |
|
269 | 269 | def _exiter_default(self): |
|
270 | 270 | return ExitAutocall(self) |
|
271 | 271 | # Monotonically increasing execution counter |
|
272 | 272 | execution_count = Integer(1) |
|
273 | 273 | filename = Unicode("<ipython console>") |
|
274 |
ipython_dir= Unicode('' |
|
|
274 | ipython_dir= Unicode('').tag(config=True) # Set to get_ipython_dir() in __init__ | |
|
275 | 275 | |
|
276 | 276 | # Input splitter, to transform input line by line and detect when a block |
|
277 | 277 | # is ready to be executed. |
|
278 | 278 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
279 | 279 | (), {'line_input_checker': True}) |
|
280 | 280 | |
|
281 | 281 | # This InputSplitter instance is used to transform completed cells before |
|
282 | 282 | # running them. It allows cell magics to contain blank lines. |
|
283 | 283 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
284 | 284 | (), {'line_input_checker': False}) |
|
285 | 285 | |
|
286 |
logstart = |
|
|
286 | logstart = Bool(False, help= | |
|
287 | 287 | """ |
|
288 | 288 | Start logging to the default log file in overwrite mode. |
|
289 | 289 | Use `logappend` to specify a log file to **append** logs to. |
|
290 | 290 | """ |
|
291 | ) | |
|
292 |
logfile = Unicode('', |
|
|
291 | ).tag(config=True) | |
|
292 | logfile = Unicode('', help= | |
|
293 | 293 | """ |
|
294 | 294 | The name of the logfile to use. |
|
295 | 295 | """ |
|
296 | ) | |
|
297 |
logappend = Unicode('', |
|
|
296 | ).tag(config=True) | |
|
297 | logappend = Unicode('', help= | |
|
298 | 298 | """ |
|
299 | 299 | Start logging to the given file in append mode. |
|
300 | 300 | Use `logfile` to specify a log file to **overwrite** logs to. |
|
301 | 301 | """ |
|
302 | ) | |
|
302 | ).tag(config=True) | |
|
303 | 303 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
304 | config=True) | |
|
305 |
pdb = |
|
|
304 | ).tag(config=True) | |
|
305 | pdb = Bool(False, help= | |
|
306 | 306 | """ |
|
307 | 307 | Automatically call the pdb debugger after every exception. |
|
308 | 308 | """ |
|
309 | ) | |
|
310 |
multiline_history = |
|
|
309 | ).tag(config=True) | |
|
310 | multiline_history = Bool(sys.platform != 'win32', | |
|
311 | 311 | help="Save multi-line entries as one entry in readline history" |
|
312 | ) | |
|
313 |
display_page = Bool(False, |
|
|
312 | ).tag(config=True) | |
|
313 | display_page = Bool(False, | |
|
314 | 314 | help="""If True, anything that would be passed to the pager |
|
315 | 315 | will be displayed as regular output instead.""" |
|
316 | ) | |
|
316 | ).tag(config=True) | |
|
317 | 317 | |
|
318 | 318 | # deprecated prompt traits: |
|
319 | 319 | |
|
320 |
prompt_in1 = Unicode('In [\\#]: ', |
|
|
321 |
help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" |
|
|
322 | prompt_in2 = Unicode(' .\\D.: ', config=True, | |
|
323 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template") | |
|
324 | prompt_out = Unicode('Out[\\#]: ', config=True, | |
|
325 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template") | |
|
326 | prompts_pad_left = CBool(True, config=True, | |
|
327 |
help="Deprecated, will be removed in IPython 5.0, use PromptManager. |
|
|
320 | prompt_in1 = Unicode('In [\\#]: ', | |
|
321 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in_template" | |
|
322 | ).tag(config=True) | |
|
323 | prompt_in2 = Unicode(' .\\D.: ', | |
|
324 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.in2_template" | |
|
325 | ).tag(config=True) | |
|
326 | prompt_out = Unicode('Out[\\#]: ', | |
|
327 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.out_template" | |
|
328 | ).tag(config=True) | |
|
329 | prompts_pad_left = Bool(True, | |
|
330 | help="Deprecated, will be removed in IPython 5.0, use PromptManager.justify" | |
|
331 | ).tag(config=True) | |
|
328 | 332 | |
|
329 | def _prompt_trait_changed(self, name, old, new): | |
|
333 | @observe('prompt_in1', 'prompt_in2', 'prompt_out', 'prompt_pad_left') | |
|
334 | def _prompt_trait_changed(self, change): | |
|
330 | 335 | table = { |
|
331 | 336 | 'prompt_in1' : 'in_template', |
|
332 | 337 | 'prompt_in2' : 'in2_template', |
|
333 | 338 | 'prompt_out' : 'out_template', |
|
334 | 339 | 'prompts_pad_left' : 'justify', |
|
335 | 340 | } |
|
341 | name = change['name'] | |
|
336 | 342 | warn("InteractiveShell.{name} is deprecated, use PromptManager.{newname}".format( |
|
337 | 343 | name=name, newname=table[name]) |
|
338 | 344 | ) |
|
339 | 345 | # protect against weird cases where self.config may not exist: |
|
340 | 346 | if self.config is not None: |
|
341 | 347 | # propagate to corresponding PromptManager trait |
|
342 | setattr(self.config.PromptManager, table[name], new) | |
|
343 | ||
|
344 | _prompt_in1_changed = _prompt_trait_changed | |
|
345 | _prompt_in2_changed = _prompt_trait_changed | |
|
346 | _prompt_out_changed = _prompt_trait_changed | |
|
347 | _prompt_pad_left_changed = _prompt_trait_changed | |
|
348 | setattr(self.config.PromptManager, table[name], change['new']) | |
|
348 | 349 | |
|
349 |
show_rewritten_input = |
|
|
350 | show_rewritten_input = Bool(True, | |
|
350 | 351 | help="Show rewritten input, e.g. for autocall." |
|
351 | ) | |
|
352 | ).tag(config=True) | |
|
352 | 353 | |
|
353 |
quiet = |
|
|
354 | quiet = Bool(False).tag(config=True) | |
|
354 | 355 | |
|
355 |
history_length = Integer(10000, |
|
|
356 | history_length = Integer(10000, | |
|
357 | help='Total length of command history' | |
|
358 | ).tag(config=True) | |
|
356 | 359 | |
|
357 |
history_load_length = Integer(1000, |
|
|
360 | history_load_length = Integer(1000, help= | |
|
358 | 361 | """ |
|
359 | 362 | The number of saved history entries to be loaded |
|
360 | 363 | into the readline buffer at startup. |
|
361 | 364 | """ |
|
362 | ) | |
|
365 | ).tag(config=True) | |
|
363 | 366 | |
|
364 | 367 | # The readline stuff will eventually be moved to the terminal subclass |
|
365 | 368 | # but for now, we can't do that as readline is welded in everywhere. |
|
366 |
readline_use = |
|
|
367 |
readline_remove_delims = Unicode('-/~' |
|
|
369 | readline_use = Bool(True).tag(config=True) | |
|
370 | readline_remove_delims = Unicode('-/~').tag(config=True) | |
|
368 | 371 | readline_delims = Unicode() # set by init_readline() |
|
369 | 372 | # don't use \M- bindings by default, because they |
|
370 | 373 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
371 | 374 | readline_parse_and_bind = List([ |
|
372 | 375 | 'tab: complete', |
|
373 | 376 | '"\C-l": clear-screen', |
|
374 | 377 | 'set show-all-if-ambiguous on', |
|
375 | 378 | '"\C-o": tab-insert', |
|
376 | 379 | '"\C-r": reverse-search-history', |
|
377 | 380 | '"\C-s": forward-search-history', |
|
378 | 381 | '"\C-p": history-search-backward', |
|
379 | 382 | '"\C-n": history-search-forward', |
|
380 | 383 | '"\e[A": history-search-backward', |
|
381 | 384 | '"\e[B": history-search-forward', |
|
382 | 385 | '"\C-k": kill-line', |
|
383 | 386 | '"\C-u": unix-line-discard', |
|
384 |
] |
|
|
387 | ]).tag(config=True) | |
|
385 | 388 | |
|
386 | 389 | _custom_readline_config = False |
|
387 | 390 | |
|
388 | def _readline_parse_and_bind_changed(self, name, old, new): | |
|
391 | @observe('readline_parse_and_bind') | |
|
392 | def _readline_parse_and_bind_changed(self, change): | |
|
389 | 393 | # notice that readline config is customized |
|
390 | 394 | # indicates that it should have higher priority than inputrc |
|
391 | 395 | self._custom_readline_config = True |
|
392 | 396 | |
|
393 | 397 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
394 |
default_value='last_expr', |
|
|
398 | default_value='last_expr', | |
|
395 | 399 | help=""" |
|
396 | 400 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
397 |
run interactively (displaying output from expressions).""" |
|
|
401 | run interactively (displaying output from expressions).""" | |
|
402 | ).tag(config=True) | |
|
398 | 403 | |
|
399 | 404 | # TODO: this part of prompt management should be moved to the frontends. |
|
400 | 405 | # Use custom TraitTypes that convert '0'->'' and '\\n'->'\n' |
|
401 |
separate_in = SeparateUnicode('\n' |
|
|
402 |
separate_out = SeparateUnicode('' |
|
|
403 |
separate_out2 = SeparateUnicode('' |
|
|
404 |
wildcards_case_sensitive = |
|
|
406 | separate_in = SeparateUnicode('\n').tag(config=True) | |
|
407 | separate_out = SeparateUnicode('').tag(config=True) | |
|
408 | separate_out2 = SeparateUnicode('').tag(config=True) | |
|
409 | wildcards_case_sensitive = Bool(True).tag(config=True) | |
|
405 | 410 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
406 |
default_value='Context' |
|
|
411 | default_value='Context').tag(config=True) | |
|
407 | 412 | |
|
408 | 413 | # Subcomponents of InteractiveShell |
|
409 | 414 | alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) |
|
410 | 415 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
411 | 416 | builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) |
|
412 | 417 | display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) |
|
413 | 418 | extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) |
|
414 | 419 | payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) |
|
415 | 420 | history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) |
|
416 | 421 | magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) |
|
417 | 422 | |
|
418 | 423 | profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) |
|
419 | 424 | @property |
|
420 | 425 | def profile(self): |
|
421 | 426 | if self.profile_dir is not None: |
|
422 | 427 | name = os.path.basename(self.profile_dir.location) |
|
423 | 428 | return name.replace('profile_','') |
|
424 | 429 | |
|
425 | 430 | |
|
426 | 431 | # Private interface |
|
427 | 432 | _post_execute = Dict() |
|
428 | 433 | |
|
429 | 434 | # Tracks any GUI loop loaded for pylab |
|
430 | 435 | pylab_gui_select = None |
|
431 | 436 | |
|
432 | 437 | def __init__(self, ipython_dir=None, profile_dir=None, |
|
433 | 438 | user_module=None, user_ns=None, |
|
434 | 439 | custom_exceptions=((), None), **kwargs): |
|
435 | 440 | |
|
436 | 441 | # This is where traits with a config_key argument are updated |
|
437 | 442 | # from the values on config. |
|
438 | 443 | super(InteractiveShell, self).__init__(**kwargs) |
|
439 | 444 | self.configurables = [self] |
|
440 | 445 | |
|
441 | 446 | # These are relatively independent and stateless |
|
442 | 447 | self.init_ipython_dir(ipython_dir) |
|
443 | 448 | self.init_profile_dir(profile_dir) |
|
444 | 449 | self.init_instance_attrs() |
|
445 | 450 | self.init_environment() |
|
446 | 451 | |
|
447 | 452 | # Check if we're in a virtualenv, and set up sys.path. |
|
448 | 453 | self.init_virtualenv() |
|
449 | 454 | |
|
450 | 455 | # Create namespaces (user_ns, user_global_ns, etc.) |
|
451 | 456 | self.init_create_namespaces(user_module, user_ns) |
|
452 | 457 | # This has to be done after init_create_namespaces because it uses |
|
453 | 458 | # something in self.user_ns, but before init_sys_modules, which |
|
454 | 459 | # is the first thing to modify sys. |
|
455 | 460 | # TODO: When we override sys.stdout and sys.stderr before this class |
|
456 | 461 | # is created, we are saving the overridden ones here. Not sure if this |
|
457 | 462 | # is what we want to do. |
|
458 | 463 | self.save_sys_module_state() |
|
459 | 464 | self.init_sys_modules() |
|
460 | 465 | |
|
461 | 466 | # While we're trying to have each part of the code directly access what |
|
462 | 467 | # it needs without keeping redundant references to objects, we have too |
|
463 | 468 | # much legacy code that expects ip.db to exist. |
|
464 | 469 | self.db = PickleShareDB(os.path.join(self.profile_dir.location, 'db')) |
|
465 | 470 | |
|
466 | 471 | self.init_history() |
|
467 | 472 | self.init_encoding() |
|
468 | 473 | self.init_prefilter() |
|
469 | 474 | |
|
470 | 475 | self.init_syntax_highlighting() |
|
471 | 476 | self.init_hooks() |
|
472 | 477 | self.init_events() |
|
473 | 478 | self.init_pushd_popd_magic() |
|
474 | 479 | # self.init_traceback_handlers use to be here, but we moved it below |
|
475 | 480 | # because it and init_io have to come after init_readline. |
|
476 | 481 | self.init_user_ns() |
|
477 | 482 | self.init_logger() |
|
478 | 483 | self.init_builtins() |
|
479 | 484 | |
|
480 | 485 | # The following was in post_config_initialization |
|
481 | 486 | self.init_inspector() |
|
482 | 487 | # init_readline() must come before init_io(), because init_io uses |
|
483 | 488 | # readline related things. |
|
484 | 489 | self.init_readline() |
|
485 | 490 | # We save this here in case user code replaces raw_input, but it needs |
|
486 | 491 | # to be after init_readline(), because PyPy's readline works by replacing |
|
487 | 492 | # raw_input. |
|
488 | 493 | if py3compat.PY3: |
|
489 | 494 | self.raw_input_original = input |
|
490 | 495 | else: |
|
491 | 496 | self.raw_input_original = raw_input |
|
492 | 497 | # init_completer must come after init_readline, because it needs to |
|
493 | 498 | # know whether readline is present or not system-wide to configure the |
|
494 | 499 | # completers, since the completion machinery can now operate |
|
495 | 500 | # independently of readline (e.g. over the network) |
|
496 | 501 | self.init_completer() |
|
497 | 502 | # TODO: init_io() needs to happen before init_traceback handlers |
|
498 | 503 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
499 | 504 | # This logic in in debugger.Pdb and should eventually be changed. |
|
500 | 505 | self.init_io() |
|
501 | 506 | self.init_traceback_handlers(custom_exceptions) |
|
502 | 507 | self.init_prompts() |
|
503 | 508 | self.init_display_formatter() |
|
504 | 509 | self.init_display_pub() |
|
505 | 510 | self.init_data_pub() |
|
506 | 511 | self.init_displayhook() |
|
507 | 512 | self.init_magics() |
|
508 | 513 | self.init_alias() |
|
509 | 514 | self.init_logstart() |
|
510 | 515 | self.init_pdb() |
|
511 | 516 | self.init_extension_manager() |
|
512 | 517 | self.init_payload() |
|
513 | 518 | self.init_deprecation_warnings() |
|
514 | 519 | self.hooks.late_startup_hook() |
|
515 | 520 | self.events.trigger('shell_initialized', self) |
|
516 | 521 | atexit.register(self.atexit_operations) |
|
517 | 522 | |
|
518 | 523 | def get_ipython(self): |
|
519 | 524 | """Return the currently running IPython instance.""" |
|
520 | 525 | return self |
|
521 | 526 | |
|
522 | 527 | #------------------------------------------------------------------------- |
|
523 | 528 | # Trait changed handlers |
|
524 | 529 | #------------------------------------------------------------------------- |
|
525 | ||
|
526 |
def _ipython_dir_changed(self, |
|
|
527 | ensure_dir_exists(new) | |
|
530 | @observe('ipython_dir') | |
|
531 | def _ipython_dir_changed(self, change): | |
|
532 | ensure_dir_exists(change['new']) | |
|
528 | 533 | |
|
529 | 534 | def set_autoindent(self,value=None): |
|
530 | 535 | """Set the autoindent flag. |
|
531 | 536 | |
|
532 | 537 | If called with no arguments, it acts as a toggle.""" |
|
533 | 538 | if value is None: |
|
534 | 539 | self.autoindent = not self.autoindent |
|
535 | 540 | else: |
|
536 | 541 | self.autoindent = value |
|
537 | 542 | |
|
538 | 543 | #------------------------------------------------------------------------- |
|
539 | 544 | # init_* methods called by __init__ |
|
540 | 545 | #------------------------------------------------------------------------- |
|
541 | 546 | |
|
542 | 547 | def init_ipython_dir(self, ipython_dir): |
|
543 | 548 | if ipython_dir is not None: |
|
544 | 549 | self.ipython_dir = ipython_dir |
|
545 | 550 | return |
|
546 | 551 | |
|
547 | 552 | self.ipython_dir = get_ipython_dir() |
|
548 | 553 | |
|
549 | 554 | def init_profile_dir(self, profile_dir): |
|
550 | 555 | if profile_dir is not None: |
|
551 | 556 | self.profile_dir = profile_dir |
|
552 | 557 | return |
|
553 | 558 | self.profile_dir =\ |
|
554 | 559 | ProfileDir.create_profile_dir_by_name(self.ipython_dir, 'default') |
|
555 | 560 | |
|
556 | 561 | def init_instance_attrs(self): |
|
557 | 562 | self.more = False |
|
558 | 563 | |
|
559 | 564 | # command compiler |
|
560 | 565 | self.compile = CachingCompiler() |
|
561 | 566 | |
|
562 | 567 | # Make an empty namespace, which extension writers can rely on both |
|
563 | 568 | # existing and NEVER being used by ipython itself. This gives them a |
|
564 | 569 | # convenient location for storing additional information and state |
|
565 | 570 | # their extensions may require, without fear of collisions with other |
|
566 | 571 | # ipython names that may develop later. |
|
567 | 572 | self.meta = Struct() |
|
568 | 573 | |
|
569 | 574 | # Temporary files used for various purposes. Deleted at exit. |
|
570 | 575 | self.tempfiles = [] |
|
571 | 576 | self.tempdirs = [] |
|
572 | 577 | |
|
573 | 578 | # Keep track of readline usage (later set by init_readline) |
|
574 | 579 | self.has_readline = False |
|
575 | 580 | |
|
576 | 581 | # keep track of where we started running (mainly for crash post-mortem) |
|
577 | 582 | # This is not being used anywhere currently. |
|
578 | 583 | self.starting_dir = py3compat.getcwd() |
|
579 | 584 | |
|
580 | 585 | # Indentation management |
|
581 | 586 | self.indent_current_nsp = 0 |
|
582 | 587 | |
|
583 | 588 | # Dict to track post-execution functions that have been registered |
|
584 | 589 | self._post_execute = {} |
|
585 | 590 | |
|
586 | 591 | def init_environment(self): |
|
587 | 592 | """Any changes we need to make to the user's environment.""" |
|
588 | 593 | pass |
|
589 | 594 | |
|
590 | 595 | def init_encoding(self): |
|
591 | 596 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
592 | 597 | # under Win32 have it set to None, and we need to have a known valid |
|
593 | 598 | # encoding to use in the raw_input() method |
|
594 | 599 | try: |
|
595 | 600 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
596 | 601 | except AttributeError: |
|
597 | 602 | self.stdin_encoding = 'ascii' |
|
598 | 603 | |
|
599 | 604 | def init_syntax_highlighting(self): |
|
600 | 605 | # Python source parser/formatter for syntax highlighting |
|
601 | 606 | pyformat = PyColorize.Parser().format |
|
602 | 607 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
603 | 608 | |
|
604 | 609 | def init_pushd_popd_magic(self): |
|
605 | 610 | # for pushd/popd management |
|
606 | 611 | self.home_dir = get_home_dir() |
|
607 | 612 | |
|
608 | 613 | self.dir_stack = [] |
|
609 | 614 | |
|
610 | 615 | def init_logger(self): |
|
611 | 616 | self.logger = Logger(self.home_dir, logfname='ipython_log.py', |
|
612 | 617 | logmode='rotate') |
|
613 | 618 | |
|
614 | 619 | def init_logstart(self): |
|
615 | 620 | """Initialize logging in case it was requested at the command line. |
|
616 | 621 | """ |
|
617 | 622 | if self.logappend: |
|
618 | 623 | self.magic('logstart %s append' % self.logappend) |
|
619 | 624 | elif self.logfile: |
|
620 | 625 | self.magic('logstart %s' % self.logfile) |
|
621 | 626 | elif self.logstart: |
|
622 | 627 | self.magic('logstart') |
|
623 | 628 | |
|
624 | 629 | def init_deprecation_warnings(self): |
|
625 | 630 | """ |
|
626 | 631 | register default filter for deprecation warning. |
|
627 | 632 | |
|
628 | 633 | This will allow deprecation warning of function used interactively to show |
|
629 | 634 | warning to users, and still hide deprecation warning from libraries import. |
|
630 | 635 | """ |
|
631 | 636 | warnings.filterwarnings("default", category=DeprecationWarning, module=self.user_ns.get("__name__")) |
|
632 | 637 | |
|
633 | 638 | def init_builtins(self): |
|
634 | 639 | # A single, static flag that we set to True. Its presence indicates |
|
635 | 640 | # that an IPython shell has been created, and we make no attempts at |
|
636 | 641 | # removing on exit or representing the existence of more than one |
|
637 | 642 | # IPython at a time. |
|
638 | 643 | builtin_mod.__dict__['__IPYTHON__'] = True |
|
639 | 644 | |
|
640 | 645 | # In 0.11 we introduced '__IPYTHON__active' as an integer we'd try to |
|
641 | 646 | # manage on enter/exit, but with all our shells it's virtually |
|
642 | 647 | # impossible to get all the cases right. We're leaving the name in for |
|
643 | 648 | # those who adapted their codes to check for this flag, but will |
|
644 | 649 | # eventually remove it after a few more releases. |
|
645 | 650 | builtin_mod.__dict__['__IPYTHON__active'] = \ |
|
646 | 651 | 'Deprecated, check for __IPYTHON__' |
|
647 | 652 | |
|
648 | 653 | self.builtin_trap = BuiltinTrap(shell=self) |
|
649 | 654 | |
|
650 | 655 | def init_inspector(self): |
|
651 | 656 | # Object inspector |
|
652 | 657 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
653 | 658 | PyColorize.ANSICodeColors, |
|
654 | 659 | 'NoColor', |
|
655 | 660 | self.object_info_string_level) |
|
656 | 661 | |
|
657 | 662 | def init_io(self): |
|
658 | 663 | # This will just use sys.stdout and sys.stderr. If you want to |
|
659 | 664 | # override sys.stdout and sys.stderr themselves, you need to do that |
|
660 | 665 | # *before* instantiating this class, because io holds onto |
|
661 | 666 | # references to the underlying streams. |
|
662 | 667 | if (sys.platform == 'win32' or sys.platform == 'cli') and self.has_readline: |
|
663 | 668 | io.stdout = io.stderr = io.IOStream(self.readline._outputfile) |
|
664 | 669 | else: |
|
665 | 670 | io.stdout = io.IOStream(sys.stdout) |
|
666 | 671 | io.stderr = io.IOStream(sys.stderr) |
|
667 | 672 | |
|
668 | 673 | def init_prompts(self): |
|
669 | 674 | self.prompt_manager = PromptManager(shell=self, parent=self) |
|
670 | 675 | self.configurables.append(self.prompt_manager) |
|
671 | 676 | # Set system prompts, so that scripts can decide if they are running |
|
672 | 677 | # interactively. |
|
673 | 678 | sys.ps1 = 'In : ' |
|
674 | 679 | sys.ps2 = '...: ' |
|
675 | 680 | sys.ps3 = 'Out: ' |
|
676 | 681 | |
|
677 | 682 | def init_display_formatter(self): |
|
678 | 683 | self.display_formatter = DisplayFormatter(parent=self) |
|
679 | 684 | self.configurables.append(self.display_formatter) |
|
680 | 685 | |
|
681 | 686 | def init_display_pub(self): |
|
682 | 687 | self.display_pub = self.display_pub_class(parent=self) |
|
683 | 688 | self.configurables.append(self.display_pub) |
|
684 | 689 | |
|
685 | 690 | def init_data_pub(self): |
|
686 | 691 | if not self.data_pub_class: |
|
687 | 692 | self.data_pub = None |
|
688 | 693 | return |
|
689 | 694 | self.data_pub = self.data_pub_class(parent=self) |
|
690 | 695 | self.configurables.append(self.data_pub) |
|
691 | 696 | |
|
692 | 697 | def init_displayhook(self): |
|
693 | 698 | # Initialize displayhook, set in/out prompts and printing system |
|
694 | 699 | self.displayhook = self.displayhook_class( |
|
695 | 700 | parent=self, |
|
696 | 701 | shell=self, |
|
697 | 702 | cache_size=self.cache_size, |
|
698 | 703 | ) |
|
699 | 704 | self.configurables.append(self.displayhook) |
|
700 | 705 | # This is a context manager that installs/revmoes the displayhook at |
|
701 | 706 | # the appropriate time. |
|
702 | 707 | self.display_trap = DisplayTrap(hook=self.displayhook) |
|
703 | 708 | |
|
704 | 709 | def init_virtualenv(self): |
|
705 | 710 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
706 | 711 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
707 | 712 | virtualenv was built, and it ignores the --no-site-packages option. A |
|
708 | 713 | warning will appear suggesting the user installs IPython in the |
|
709 | 714 | virtualenv, but for many cases, it probably works well enough. |
|
710 | 715 | |
|
711 | 716 | Adapted from code snippets online. |
|
712 | 717 | |
|
713 | 718 | http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv |
|
714 | 719 | """ |
|
715 | 720 | if 'VIRTUAL_ENV' not in os.environ: |
|
716 | 721 | # Not in a virtualenv |
|
717 | 722 | return |
|
718 | 723 | |
|
719 | 724 | # venv detection: |
|
720 | 725 | # stdlib venv may symlink sys.executable, so we can't use realpath. |
|
721 | 726 | # but others can symlink *to* the venv Python, so we can't just use sys.executable. |
|
722 | 727 | # So we just check every item in the symlink tree (generally <= 3) |
|
723 | 728 | p = os.path.normcase(sys.executable) |
|
724 | 729 | paths = [p] |
|
725 | 730 | while os.path.islink(p): |
|
726 | 731 | p = os.path.normcase(os.path.join(os.path.dirname(p), os.readlink(p))) |
|
727 | 732 | paths.append(p) |
|
728 | 733 | p_venv = os.path.normcase(os.environ['VIRTUAL_ENV']) |
|
729 | 734 | if any(p.startswith(p_venv) for p in paths): |
|
730 | 735 | # Running properly in the virtualenv, don't need to do anything |
|
731 | 736 | return |
|
732 | 737 | |
|
733 | 738 | warn("Attempting to work in a virtualenv. If you encounter problems, please " |
|
734 | 739 | "install IPython inside the virtualenv.") |
|
735 | 740 | if sys.platform == "win32": |
|
736 | 741 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'Lib', 'site-packages') |
|
737 | 742 | else: |
|
738 | 743 | virtual_env = os.path.join(os.environ['VIRTUAL_ENV'], 'lib', |
|
739 | 744 | 'python%d.%d' % sys.version_info[:2], 'site-packages') |
|
740 | 745 | |
|
741 | 746 | import site |
|
742 | 747 | sys.path.insert(0, virtual_env) |
|
743 | 748 | site.addsitedir(virtual_env) |
|
744 | 749 | |
|
745 | 750 | #------------------------------------------------------------------------- |
|
746 | 751 | # Things related to injections into the sys module |
|
747 | 752 | #------------------------------------------------------------------------- |
|
748 | 753 | |
|
749 | 754 | def save_sys_module_state(self): |
|
750 | 755 | """Save the state of hooks in the sys module. |
|
751 | 756 | |
|
752 | 757 | This has to be called after self.user_module is created. |
|
753 | 758 | """ |
|
754 | 759 | self._orig_sys_module_state = {'stdin': sys.stdin, |
|
755 | 760 | 'stdout': sys.stdout, |
|
756 | 761 | 'stderr': sys.stderr, |
|
757 | 762 | 'excepthook': sys.excepthook} |
|
758 | 763 | self._orig_sys_modules_main_name = self.user_module.__name__ |
|
759 | 764 | self._orig_sys_modules_main_mod = sys.modules.get(self.user_module.__name__) |
|
760 | 765 | |
|
761 | 766 | def restore_sys_module_state(self): |
|
762 | 767 | """Restore the state of the sys module.""" |
|
763 | 768 | try: |
|
764 | 769 | for k, v in iteritems(self._orig_sys_module_state): |
|
765 | 770 | setattr(sys, k, v) |
|
766 | 771 | except AttributeError: |
|
767 | 772 | pass |
|
768 | 773 | # Reset what what done in self.init_sys_modules |
|
769 | 774 | if self._orig_sys_modules_main_mod is not None: |
|
770 | 775 | sys.modules[self._orig_sys_modules_main_name] = self._orig_sys_modules_main_mod |
|
771 | 776 | |
|
772 | 777 | #------------------------------------------------------------------------- |
|
773 | 778 | # Things related to the banner |
|
774 | 779 | #------------------------------------------------------------------------- |
|
775 | 780 | |
|
776 | 781 | @property |
|
777 | 782 | def banner(self): |
|
778 | 783 | banner = self.banner1 |
|
779 | 784 | if self.profile and self.profile != 'default': |
|
780 | 785 | banner += '\nIPython profile: %s\n' % self.profile |
|
781 | 786 | if self.banner2: |
|
782 | 787 | banner += '\n' + self.banner2 |
|
783 | 788 | return banner |
|
784 | 789 | |
|
785 | 790 | def show_banner(self, banner=None): |
|
786 | 791 | if banner is None: |
|
787 | 792 | banner = self.banner |
|
788 |
s |
|
|
793 | sys.stdout.write(banner) | |
|
789 | 794 | |
|
790 | 795 | #------------------------------------------------------------------------- |
|
791 | 796 | # Things related to hooks |
|
792 | 797 | #------------------------------------------------------------------------- |
|
793 | 798 | |
|
794 | 799 | def init_hooks(self): |
|
795 | 800 | # hooks holds pointers used for user-side customizations |
|
796 | 801 | self.hooks = Struct() |
|
797 | 802 | |
|
798 | 803 | self.strdispatchers = {} |
|
799 | 804 | |
|
800 | 805 | # Set all default hooks, defined in the IPython.hooks module. |
|
801 | 806 | hooks = IPython.core.hooks |
|
802 | 807 | for hook_name in hooks.__all__: |
|
803 | 808 | # default hooks have priority 100, i.e. low; user hooks should have |
|
804 | 809 | # 0-100 priority |
|
805 | 810 | self.set_hook(hook_name,getattr(hooks,hook_name), 100, _warn_deprecated=False) |
|
806 | 811 | |
|
807 | 812 | if self.display_page: |
|
808 | 813 | self.set_hook('show_in_pager', page.as_hook(page.display_page), 90) |
|
809 | 814 | |
|
810 | 815 | def set_hook(self,name,hook, priority=50, str_key=None, re_key=None, |
|
811 | 816 | _warn_deprecated=True): |
|
812 | 817 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
813 | 818 | |
|
814 | 819 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
815 | 820 | adding your function to one of these hooks, you can modify IPython's |
|
816 | 821 | behavior to call at runtime your own routines.""" |
|
817 | 822 | |
|
818 | 823 | # At some point in the future, this should validate the hook before it |
|
819 | 824 | # accepts it. Probably at least check that the hook takes the number |
|
820 | 825 | # of args it's supposed to. |
|
821 | 826 | |
|
822 | 827 | f = types.MethodType(hook,self) |
|
823 | 828 | |
|
824 | 829 | # check if the hook is for strdispatcher first |
|
825 | 830 | if str_key is not None: |
|
826 | 831 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
827 | 832 | sdp.add_s(str_key, f, priority ) |
|
828 | 833 | self.strdispatchers[name] = sdp |
|
829 | 834 | return |
|
830 | 835 | if re_key is not None: |
|
831 | 836 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
832 | 837 | sdp.add_re(re.compile(re_key), f, priority ) |
|
833 | 838 | self.strdispatchers[name] = sdp |
|
834 | 839 | return |
|
835 | 840 | |
|
836 | 841 | dp = getattr(self.hooks, name, None) |
|
837 | 842 | if name not in IPython.core.hooks.__all__: |
|
838 | 843 | print("Warning! Hook '%s' is not one of %s" % \ |
|
839 | 844 | (name, IPython.core.hooks.__all__ )) |
|
840 | 845 | |
|
841 | 846 | if _warn_deprecated and (name in IPython.core.hooks.deprecated): |
|
842 | 847 | alternative = IPython.core.hooks.deprecated[name] |
|
843 | 848 | warn("Hook {} is deprecated. Use {} instead.".format(name, alternative)) |
|
844 | 849 | |
|
845 | 850 | if not dp: |
|
846 | 851 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
847 | 852 | |
|
848 | 853 | try: |
|
849 | 854 | dp.add(f,priority) |
|
850 | 855 | except AttributeError: |
|
851 | 856 | # it was not commandchain, plain old func - replace |
|
852 | 857 | dp = f |
|
853 | 858 | |
|
854 | 859 | setattr(self.hooks,name, dp) |
|
855 | 860 | |
|
856 | 861 | #------------------------------------------------------------------------- |
|
857 | 862 | # Things related to events |
|
858 | 863 | #------------------------------------------------------------------------- |
|
859 | 864 | |
|
860 | 865 | def init_events(self): |
|
861 | 866 | self.events = EventManager(self, available_events) |
|
862 | 867 | |
|
863 | 868 | self.events.register("pre_execute", self._clear_warning_registry) |
|
864 | 869 | |
|
865 | 870 | def register_post_execute(self, func): |
|
866 | 871 | """DEPRECATED: Use ip.events.register('post_run_cell', func) |
|
867 | 872 | |
|
868 | 873 | Register a function for calling after code execution. |
|
869 | 874 | """ |
|
870 | 875 | warn("ip.register_post_execute is deprecated, use " |
|
871 | 876 | "ip.events.register('post_run_cell', func) instead.") |
|
872 | 877 | self.events.register('post_run_cell', func) |
|
873 | 878 | |
|
874 | 879 | def _clear_warning_registry(self): |
|
875 | 880 | # clear the warning registry, so that different code blocks with |
|
876 | 881 | # overlapping line number ranges don't cause spurious suppression of |
|
877 | 882 | # warnings (see gh-6611 for details) |
|
878 | 883 | if "__warningregistry__" in self.user_global_ns: |
|
879 | 884 | del self.user_global_ns["__warningregistry__"] |
|
880 | 885 | |
|
881 | 886 | #------------------------------------------------------------------------- |
|
882 | 887 | # Things related to the "main" module |
|
883 | 888 | #------------------------------------------------------------------------- |
|
884 | 889 | |
|
885 | 890 | def new_main_mod(self, filename, modname): |
|
886 | 891 | """Return a new 'main' module object for user code execution. |
|
887 | 892 | |
|
888 | 893 | ``filename`` should be the path of the script which will be run in the |
|
889 | 894 | module. Requests with the same filename will get the same module, with |
|
890 | 895 | its namespace cleared. |
|
891 | 896 | |
|
892 | 897 | ``modname`` should be the module name - normally either '__main__' or |
|
893 | 898 | the basename of the file without the extension. |
|
894 | 899 | |
|
895 | 900 | When scripts are executed via %run, we must keep a reference to their |
|
896 | 901 | __main__ module around so that Python doesn't |
|
897 | 902 | clear it, rendering references to module globals useless. |
|
898 | 903 | |
|
899 | 904 | This method keeps said reference in a private dict, keyed by the |
|
900 | 905 | absolute path of the script. This way, for multiple executions of the |
|
901 | 906 | same script we only keep one copy of the namespace (the last one), |
|
902 | 907 | thus preventing memory leaks from old references while allowing the |
|
903 | 908 | objects from the last execution to be accessible. |
|
904 | 909 | """ |
|
905 | 910 | filename = os.path.abspath(filename) |
|
906 | 911 | try: |
|
907 | 912 | main_mod = self._main_mod_cache[filename] |
|
908 | 913 | except KeyError: |
|
909 | 914 | main_mod = self._main_mod_cache[filename] = types.ModuleType( |
|
910 | 915 | py3compat.cast_bytes_py2(modname), |
|
911 | 916 | doc="Module created for script run in IPython") |
|
912 | 917 | else: |
|
913 | 918 | main_mod.__dict__.clear() |
|
914 | 919 | main_mod.__name__ = modname |
|
915 | 920 | |
|
916 | 921 | main_mod.__file__ = filename |
|
917 | 922 | # It seems pydoc (and perhaps others) needs any module instance to |
|
918 | 923 | # implement a __nonzero__ method |
|
919 | 924 | main_mod.__nonzero__ = lambda : True |
|
920 | 925 | |
|
921 | 926 | return main_mod |
|
922 | 927 | |
|
923 | 928 | def clear_main_mod_cache(self): |
|
924 | 929 | """Clear the cache of main modules. |
|
925 | 930 | |
|
926 | 931 | Mainly for use by utilities like %reset. |
|
927 | 932 | |
|
928 | 933 | Examples |
|
929 | 934 | -------- |
|
930 | 935 | |
|
931 | 936 | In [15]: import IPython |
|
932 | 937 | |
|
933 | 938 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') |
|
934 | 939 | |
|
935 | 940 | In [17]: len(_ip._main_mod_cache) > 0 |
|
936 | 941 | Out[17]: True |
|
937 | 942 | |
|
938 | 943 | In [18]: _ip.clear_main_mod_cache() |
|
939 | 944 | |
|
940 | 945 | In [19]: len(_ip._main_mod_cache) == 0 |
|
941 | 946 | Out[19]: True |
|
942 | 947 | """ |
|
943 | 948 | self._main_mod_cache.clear() |
|
944 | 949 | |
|
945 | 950 | #------------------------------------------------------------------------- |
|
946 | 951 | # Things related to debugging |
|
947 | 952 | #------------------------------------------------------------------------- |
|
948 | 953 | |
|
949 | 954 | def init_pdb(self): |
|
950 | 955 | # Set calling of pdb on exceptions |
|
951 | 956 | # self.call_pdb is a property |
|
952 | 957 | self.call_pdb = self.pdb |
|
953 | 958 | |
|
954 | 959 | def _get_call_pdb(self): |
|
955 | 960 | return self._call_pdb |
|
956 | 961 | |
|
957 | 962 | def _set_call_pdb(self,val): |
|
958 | 963 | |
|
959 | 964 | if val not in (0,1,False,True): |
|
960 | 965 | raise ValueError('new call_pdb value must be boolean') |
|
961 | 966 | |
|
962 | 967 | # store value in instance |
|
963 | 968 | self._call_pdb = val |
|
964 | 969 | |
|
965 | 970 | # notify the actual exception handlers |
|
966 | 971 | self.InteractiveTB.call_pdb = val |
|
967 | 972 | |
|
968 | 973 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
969 | 974 | 'Control auto-activation of pdb at exceptions') |
|
970 | 975 | |
|
971 | 976 | def debugger(self,force=False): |
|
972 | 977 | """Call the pydb/pdb debugger. |
|
973 | 978 | |
|
974 | 979 | Keywords: |
|
975 | 980 | |
|
976 | 981 | - force(False): by default, this routine checks the instance call_pdb |
|
977 | 982 | flag and does not actually invoke the debugger if the flag is false. |
|
978 | 983 | The 'force' option forces the debugger to activate even if the flag |
|
979 | 984 | is false. |
|
980 | 985 | """ |
|
981 | 986 | |
|
982 | 987 | if not (force or self.call_pdb): |
|
983 | 988 | return |
|
984 | 989 | |
|
985 | 990 | if not hasattr(sys,'last_traceback'): |
|
986 | 991 | error('No traceback has been produced, nothing to debug.') |
|
987 | 992 | return |
|
988 | 993 | |
|
989 | 994 | # use pydb if available |
|
990 | 995 | if debugger.has_pydb: |
|
991 | 996 | from pydb import pm |
|
992 | 997 | else: |
|
993 | 998 | # fallback to our internal debugger |
|
994 | 999 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
995 | 1000 | |
|
996 | 1001 | with self.readline_no_record: |
|
997 | 1002 | pm() |
|
998 | 1003 | |
|
999 | 1004 | #------------------------------------------------------------------------- |
|
1000 | 1005 | # Things related to IPython's various namespaces |
|
1001 | 1006 | #------------------------------------------------------------------------- |
|
1002 | 1007 | default_user_namespaces = True |
|
1003 | 1008 | |
|
1004 | 1009 | def init_create_namespaces(self, user_module=None, user_ns=None): |
|
1005 | 1010 | # Create the namespace where the user will operate. user_ns is |
|
1006 | 1011 | # normally the only one used, and it is passed to the exec calls as |
|
1007 | 1012 | # the locals argument. But we do carry a user_global_ns namespace |
|
1008 | 1013 | # given as the exec 'globals' argument, This is useful in embedding |
|
1009 | 1014 | # situations where the ipython shell opens in a context where the |
|
1010 | 1015 | # distinction between locals and globals is meaningful. For |
|
1011 | 1016 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
1012 | 1017 | |
|
1013 | 1018 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
1014 | 1019 | # level as a dict instead of a module. This is a manual fix, but I |
|
1015 | 1020 | # should really track down where the problem is coming from. Alex |
|
1016 | 1021 | # Schmolck reported this problem first. |
|
1017 | 1022 | |
|
1018 | 1023 | # A useful post by Alex Martelli on this topic: |
|
1019 | 1024 | # Re: inconsistent value from __builtins__ |
|
1020 | 1025 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
1021 | 1026 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
1022 | 1027 | # Gruppen: comp.lang.python |
|
1023 | 1028 | |
|
1024 | 1029 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
1025 | 1030 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
1026 | 1031 | # > <type 'dict'> |
|
1027 | 1032 | # > >>> print type(__builtins__) |
|
1028 | 1033 | # > <type 'module'> |
|
1029 | 1034 | # > Is this difference in return value intentional? |
|
1030 | 1035 | |
|
1031 | 1036 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
1032 | 1037 | # or a module, and it's been that way for a long time. Whether it's |
|
1033 | 1038 | # intentional (or sensible), I don't know. In any case, the idea is |
|
1034 | 1039 | # that if you need to access the built-in namespace directly, you |
|
1035 | 1040 | # should start with "import __builtin__" (note, no 's') which will |
|
1036 | 1041 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
1037 | 1042 | |
|
1038 | 1043 | # These routines return a properly built module and dict as needed by |
|
1039 | 1044 | # the rest of the code, and can also be used by extension writers to |
|
1040 | 1045 | # generate properly initialized namespaces. |
|
1041 | 1046 | if (user_ns is not None) or (user_module is not None): |
|
1042 | 1047 | self.default_user_namespaces = False |
|
1043 | 1048 | self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns) |
|
1044 | 1049 | |
|
1045 | 1050 | # A record of hidden variables we have added to the user namespace, so |
|
1046 | 1051 | # we can list later only variables defined in actual interactive use. |
|
1047 | 1052 | self.user_ns_hidden = {} |
|
1048 | 1053 | |
|
1049 | 1054 | # Now that FakeModule produces a real module, we've run into a nasty |
|
1050 | 1055 | # problem: after script execution (via %run), the module where the user |
|
1051 | 1056 | # code ran is deleted. Now that this object is a true module (needed |
|
1052 | 1057 | # so doctest and other tools work correctly), the Python module |
|
1053 | 1058 | # teardown mechanism runs over it, and sets to None every variable |
|
1054 | 1059 | # present in that module. Top-level references to objects from the |
|
1055 | 1060 | # script survive, because the user_ns is updated with them. However, |
|
1056 | 1061 | # calling functions defined in the script that use other things from |
|
1057 | 1062 | # the script will fail, because the function's closure had references |
|
1058 | 1063 | # to the original objects, which are now all None. So we must protect |
|
1059 | 1064 | # these modules from deletion by keeping a cache. |
|
1060 | 1065 | # |
|
1061 | 1066 | # To avoid keeping stale modules around (we only need the one from the |
|
1062 | 1067 | # last run), we use a dict keyed with the full path to the script, so |
|
1063 | 1068 | # only the last version of the module is held in the cache. Note, |
|
1064 | 1069 | # however, that we must cache the module *namespace contents* (their |
|
1065 | 1070 | # __dict__). Because if we try to cache the actual modules, old ones |
|
1066 | 1071 | # (uncached) could be destroyed while still holding references (such as |
|
1067 | 1072 | # those held by GUI objects that tend to be long-lived)> |
|
1068 | 1073 | # |
|
1069 | 1074 | # The %reset command will flush this cache. See the cache_main_mod() |
|
1070 | 1075 | # and clear_main_mod_cache() methods for details on use. |
|
1071 | 1076 | |
|
1072 | 1077 | # This is the cache used for 'main' namespaces |
|
1073 | 1078 | self._main_mod_cache = {} |
|
1074 | 1079 | |
|
1075 | 1080 | # A table holding all the namespaces IPython deals with, so that |
|
1076 | 1081 | # introspection facilities can search easily. |
|
1077 | 1082 | self.ns_table = {'user_global':self.user_module.__dict__, |
|
1078 | 1083 | 'user_local':self.user_ns, |
|
1079 | 1084 | 'builtin':builtin_mod.__dict__ |
|
1080 | 1085 | } |
|
1081 | 1086 | |
|
1082 | 1087 | @property |
|
1083 | 1088 | def user_global_ns(self): |
|
1084 | 1089 | return self.user_module.__dict__ |
|
1085 | 1090 | |
|
1086 | 1091 | def prepare_user_module(self, user_module=None, user_ns=None): |
|
1087 | 1092 | """Prepare the module and namespace in which user code will be run. |
|
1088 | 1093 | |
|
1089 | 1094 | When IPython is started normally, both parameters are None: a new module |
|
1090 | 1095 | is created automatically, and its __dict__ used as the namespace. |
|
1091 | 1096 | |
|
1092 | 1097 | If only user_module is provided, its __dict__ is used as the namespace. |
|
1093 | 1098 | If only user_ns is provided, a dummy module is created, and user_ns |
|
1094 | 1099 | becomes the global namespace. If both are provided (as they may be |
|
1095 | 1100 | when embedding), user_ns is the local namespace, and user_module |
|
1096 | 1101 | provides the global namespace. |
|
1097 | 1102 | |
|
1098 | 1103 | Parameters |
|
1099 | 1104 | ---------- |
|
1100 | 1105 | user_module : module, optional |
|
1101 | 1106 | The current user module in which IPython is being run. If None, |
|
1102 | 1107 | a clean module will be created. |
|
1103 | 1108 | user_ns : dict, optional |
|
1104 | 1109 | A namespace in which to run interactive commands. |
|
1105 | 1110 | |
|
1106 | 1111 | Returns |
|
1107 | 1112 | ------- |
|
1108 | 1113 | A tuple of user_module and user_ns, each properly initialised. |
|
1109 | 1114 | """ |
|
1110 | 1115 | if user_module is None and user_ns is not None: |
|
1111 | 1116 | user_ns.setdefault("__name__", "__main__") |
|
1112 | 1117 | user_module = DummyMod() |
|
1113 | 1118 | user_module.__dict__ = user_ns |
|
1114 | 1119 | |
|
1115 | 1120 | if user_module is None: |
|
1116 | 1121 | user_module = types.ModuleType("__main__", |
|
1117 | 1122 | doc="Automatically created module for IPython interactive environment") |
|
1118 | 1123 | |
|
1119 | 1124 | # We must ensure that __builtin__ (without the final 's') is always |
|
1120 | 1125 | # available and pointing to the __builtin__ *module*. For more details: |
|
1121 | 1126 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1122 | 1127 | user_module.__dict__.setdefault('__builtin__', builtin_mod) |
|
1123 | 1128 | user_module.__dict__.setdefault('__builtins__', builtin_mod) |
|
1124 | 1129 | |
|
1125 | 1130 | if user_ns is None: |
|
1126 | 1131 | user_ns = user_module.__dict__ |
|
1127 | 1132 | |
|
1128 | 1133 | return user_module, user_ns |
|
1129 | 1134 | |
|
1130 | 1135 | def init_sys_modules(self): |
|
1131 | 1136 | # We need to insert into sys.modules something that looks like a |
|
1132 | 1137 | # module but which accesses the IPython namespace, for shelve and |
|
1133 | 1138 | # pickle to work interactively. Normally they rely on getting |
|
1134 | 1139 | # everything out of __main__, but for embedding purposes each IPython |
|
1135 | 1140 | # instance has its own private namespace, so we can't go shoving |
|
1136 | 1141 | # everything into __main__. |
|
1137 | 1142 | |
|
1138 | 1143 | # note, however, that we should only do this for non-embedded |
|
1139 | 1144 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
1140 | 1145 | # namespace. Embedded instances, on the other hand, should not do |
|
1141 | 1146 | # this because they need to manage the user local/global namespaces |
|
1142 | 1147 | # only, but they live within a 'normal' __main__ (meaning, they |
|
1143 | 1148 | # shouldn't overtake the execution environment of the script they're |
|
1144 | 1149 | # embedded in). |
|
1145 | 1150 | |
|
1146 | 1151 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
1147 | 1152 | main_name = self.user_module.__name__ |
|
1148 | 1153 | sys.modules[main_name] = self.user_module |
|
1149 | 1154 | |
|
1150 | 1155 | def init_user_ns(self): |
|
1151 | 1156 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1152 | 1157 | |
|
1153 | 1158 | Certain history lists are also initialized here, as they effectively |
|
1154 | 1159 | act as user namespaces. |
|
1155 | 1160 | |
|
1156 | 1161 | Notes |
|
1157 | 1162 | ----- |
|
1158 | 1163 | All data structures here are only filled in, they are NOT reset by this |
|
1159 | 1164 | method. If they were not empty before, data will simply be added to |
|
1160 | 1165 | therm. |
|
1161 | 1166 | """ |
|
1162 | 1167 | # This function works in two parts: first we put a few things in |
|
1163 | 1168 | # user_ns, and we sync that contents into user_ns_hidden so that these |
|
1164 | 1169 | # initial variables aren't shown by %who. After the sync, we add the |
|
1165 | 1170 | # rest of what we *do* want the user to see with %who even on a new |
|
1166 | 1171 | # session (probably nothing, so they really only see their own stuff) |
|
1167 | 1172 | |
|
1168 | 1173 | # The user dict must *always* have a __builtin__ reference to the |
|
1169 | 1174 | # Python standard __builtin__ namespace, which must be imported. |
|
1170 | 1175 | # This is so that certain operations in prompt evaluation can be |
|
1171 | 1176 | # reliably executed with builtins. Note that we can NOT use |
|
1172 | 1177 | # __builtins__ (note the 's'), because that can either be a dict or a |
|
1173 | 1178 | # module, and can even mutate at runtime, depending on the context |
|
1174 | 1179 | # (Python makes no guarantees on it). In contrast, __builtin__ is |
|
1175 | 1180 | # always a module object, though it must be explicitly imported. |
|
1176 | 1181 | |
|
1177 | 1182 | # For more details: |
|
1178 | 1183 | # http://mail.python.org/pipermail/python-dev/2001-April/014068.html |
|
1179 | 1184 | ns = dict() |
|
1180 | 1185 | |
|
1181 | 1186 | # make global variables for user access to the histories |
|
1182 | 1187 | ns['_ih'] = self.history_manager.input_hist_parsed |
|
1183 | 1188 | ns['_oh'] = self.history_manager.output_hist |
|
1184 | 1189 | ns['_dh'] = self.history_manager.dir_hist |
|
1185 | 1190 | |
|
1186 | 1191 | ns['_sh'] = shadowns |
|
1187 | 1192 | |
|
1188 | 1193 | # user aliases to input and output histories. These shouldn't show up |
|
1189 | 1194 | # in %who, as they can have very large reprs. |
|
1190 | 1195 | ns['In'] = self.history_manager.input_hist_parsed |
|
1191 | 1196 | ns['Out'] = self.history_manager.output_hist |
|
1192 | 1197 | |
|
1193 | 1198 | # Store myself as the public api!!! |
|
1194 | 1199 | ns['get_ipython'] = self.get_ipython |
|
1195 | 1200 | |
|
1196 | 1201 | ns['exit'] = self.exiter |
|
1197 | 1202 | ns['quit'] = self.exiter |
|
1198 | 1203 | |
|
1199 | 1204 | # Sync what we've added so far to user_ns_hidden so these aren't seen |
|
1200 | 1205 | # by %who |
|
1201 | 1206 | self.user_ns_hidden.update(ns) |
|
1202 | 1207 | |
|
1203 | 1208 | # Anything put into ns now would show up in %who. Think twice before |
|
1204 | 1209 | # putting anything here, as we really want %who to show the user their |
|
1205 | 1210 | # stuff, not our variables. |
|
1206 | 1211 | |
|
1207 | 1212 | # Finally, update the real user's namespace |
|
1208 | 1213 | self.user_ns.update(ns) |
|
1209 | 1214 | |
|
1210 | 1215 | @property |
|
1211 | 1216 | def all_ns_refs(self): |
|
1212 | 1217 | """Get a list of references to all the namespace dictionaries in which |
|
1213 | 1218 | IPython might store a user-created object. |
|
1214 | 1219 | |
|
1215 | 1220 | Note that this does not include the displayhook, which also caches |
|
1216 | 1221 | objects from the output.""" |
|
1217 | 1222 | return [self.user_ns, self.user_global_ns, self.user_ns_hidden] + \ |
|
1218 | 1223 | [m.__dict__ for m in self._main_mod_cache.values()] |
|
1219 | 1224 | |
|
1220 | 1225 | def reset(self, new_session=True): |
|
1221 | 1226 | """Clear all internal namespaces, and attempt to release references to |
|
1222 | 1227 | user objects. |
|
1223 | 1228 | |
|
1224 | 1229 | If new_session is True, a new history session will be opened. |
|
1225 | 1230 | """ |
|
1226 | 1231 | # Clear histories |
|
1227 | 1232 | self.history_manager.reset(new_session) |
|
1228 | 1233 | # Reset counter used to index all histories |
|
1229 | 1234 | if new_session: |
|
1230 | 1235 | self.execution_count = 1 |
|
1231 | 1236 | |
|
1232 | 1237 | # Flush cached output items |
|
1233 | 1238 | if self.displayhook.do_full_cache: |
|
1234 | 1239 | self.displayhook.flush() |
|
1235 | 1240 | |
|
1236 | 1241 | # The main execution namespaces must be cleared very carefully, |
|
1237 | 1242 | # skipping the deletion of the builtin-related keys, because doing so |
|
1238 | 1243 | # would cause errors in many object's __del__ methods. |
|
1239 | 1244 | if self.user_ns is not self.user_global_ns: |
|
1240 | 1245 | self.user_ns.clear() |
|
1241 | 1246 | ns = self.user_global_ns |
|
1242 | 1247 | drop_keys = set(ns.keys()) |
|
1243 | 1248 | drop_keys.discard('__builtin__') |
|
1244 | 1249 | drop_keys.discard('__builtins__') |
|
1245 | 1250 | drop_keys.discard('__name__') |
|
1246 | 1251 | for k in drop_keys: |
|
1247 | 1252 | del ns[k] |
|
1248 | 1253 | |
|
1249 | 1254 | self.user_ns_hidden.clear() |
|
1250 | 1255 | |
|
1251 | 1256 | # Restore the user namespaces to minimal usability |
|
1252 | 1257 | self.init_user_ns() |
|
1253 | 1258 | |
|
1254 | 1259 | # Restore the default and user aliases |
|
1255 | 1260 | self.alias_manager.clear_aliases() |
|
1256 | 1261 | self.alias_manager.init_aliases() |
|
1257 | 1262 | |
|
1258 | 1263 | # Flush the private list of module references kept for script |
|
1259 | 1264 | # execution protection |
|
1260 | 1265 | self.clear_main_mod_cache() |
|
1261 | 1266 | |
|
1262 | 1267 | def del_var(self, varname, by_name=False): |
|
1263 | 1268 | """Delete a variable from the various namespaces, so that, as |
|
1264 | 1269 | far as possible, we're not keeping any hidden references to it. |
|
1265 | 1270 | |
|
1266 | 1271 | Parameters |
|
1267 | 1272 | ---------- |
|
1268 | 1273 | varname : str |
|
1269 | 1274 | The name of the variable to delete. |
|
1270 | 1275 | by_name : bool |
|
1271 | 1276 | If True, delete variables with the given name in each |
|
1272 | 1277 | namespace. If False (default), find the variable in the user |
|
1273 | 1278 | namespace, and delete references to it. |
|
1274 | 1279 | """ |
|
1275 | 1280 | if varname in ('__builtin__', '__builtins__'): |
|
1276 | 1281 | raise ValueError("Refusing to delete %s" % varname) |
|
1277 | 1282 | |
|
1278 | 1283 | ns_refs = self.all_ns_refs |
|
1279 | 1284 | |
|
1280 | 1285 | if by_name: # Delete by name |
|
1281 | 1286 | for ns in ns_refs: |
|
1282 | 1287 | try: |
|
1283 | 1288 | del ns[varname] |
|
1284 | 1289 | except KeyError: |
|
1285 | 1290 | pass |
|
1286 | 1291 | else: # Delete by object |
|
1287 | 1292 | try: |
|
1288 | 1293 | obj = self.user_ns[varname] |
|
1289 | 1294 | except KeyError: |
|
1290 | 1295 | raise NameError("name '%s' is not defined" % varname) |
|
1291 | 1296 | # Also check in output history |
|
1292 | 1297 | ns_refs.append(self.history_manager.output_hist) |
|
1293 | 1298 | for ns in ns_refs: |
|
1294 | 1299 | to_delete = [n for n, o in iteritems(ns) if o is obj] |
|
1295 | 1300 | for name in to_delete: |
|
1296 | 1301 | del ns[name] |
|
1297 | 1302 | |
|
1298 | 1303 | # displayhook keeps extra references, but not in a dictionary |
|
1299 | 1304 | for name in ('_', '__', '___'): |
|
1300 | 1305 | if getattr(self.displayhook, name) is obj: |
|
1301 | 1306 | setattr(self.displayhook, name, None) |
|
1302 | 1307 | |
|
1303 | 1308 | def reset_selective(self, regex=None): |
|
1304 | 1309 | """Clear selective variables from internal namespaces based on a |
|
1305 | 1310 | specified regular expression. |
|
1306 | 1311 | |
|
1307 | 1312 | Parameters |
|
1308 | 1313 | ---------- |
|
1309 | 1314 | regex : string or compiled pattern, optional |
|
1310 | 1315 | A regular expression pattern that will be used in searching |
|
1311 | 1316 | variable names in the users namespaces. |
|
1312 | 1317 | """ |
|
1313 | 1318 | if regex is not None: |
|
1314 | 1319 | try: |
|
1315 | 1320 | m = re.compile(regex) |
|
1316 | 1321 | except TypeError: |
|
1317 | 1322 | raise TypeError('regex must be a string or compiled pattern') |
|
1318 | 1323 | # Search for keys in each namespace that match the given regex |
|
1319 | 1324 | # If a match is found, delete the key/value pair. |
|
1320 | 1325 | for ns in self.all_ns_refs: |
|
1321 | 1326 | for var in ns: |
|
1322 | 1327 | if m.search(var): |
|
1323 | 1328 | del ns[var] |
|
1324 | 1329 | |
|
1325 | 1330 | def push(self, variables, interactive=True): |
|
1326 | 1331 | """Inject a group of variables into the IPython user namespace. |
|
1327 | 1332 | |
|
1328 | 1333 | Parameters |
|
1329 | 1334 | ---------- |
|
1330 | 1335 | variables : dict, str or list/tuple of str |
|
1331 | 1336 | The variables to inject into the user's namespace. If a dict, a |
|
1332 | 1337 | simple update is done. If a str, the string is assumed to have |
|
1333 | 1338 | variable names separated by spaces. A list/tuple of str can also |
|
1334 | 1339 | be used to give the variable names. If just the variable names are |
|
1335 | 1340 | give (list/tuple/str) then the variable values looked up in the |
|
1336 | 1341 | callers frame. |
|
1337 | 1342 | interactive : bool |
|
1338 | 1343 | If True (default), the variables will be listed with the ``who`` |
|
1339 | 1344 | magic. |
|
1340 | 1345 | """ |
|
1341 | 1346 | vdict = None |
|
1342 | 1347 | |
|
1343 | 1348 | # We need a dict of name/value pairs to do namespace updates. |
|
1344 | 1349 | if isinstance(variables, dict): |
|
1345 | 1350 | vdict = variables |
|
1346 | 1351 | elif isinstance(variables, string_types+(list, tuple)): |
|
1347 | 1352 | if isinstance(variables, string_types): |
|
1348 | 1353 | vlist = variables.split() |
|
1349 | 1354 | else: |
|
1350 | 1355 | vlist = variables |
|
1351 | 1356 | vdict = {} |
|
1352 | 1357 | cf = sys._getframe(1) |
|
1353 | 1358 | for name in vlist: |
|
1354 | 1359 | try: |
|
1355 | 1360 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
1356 | 1361 | except: |
|
1357 | 1362 | print('Could not get variable %s from %s' % |
|
1358 | 1363 | (name,cf.f_code.co_name)) |
|
1359 | 1364 | else: |
|
1360 | 1365 | raise ValueError('variables must be a dict/str/list/tuple') |
|
1361 | 1366 | |
|
1362 | 1367 | # Propagate variables to user namespace |
|
1363 | 1368 | self.user_ns.update(vdict) |
|
1364 | 1369 | |
|
1365 | 1370 | # And configure interactive visibility |
|
1366 | 1371 | user_ns_hidden = self.user_ns_hidden |
|
1367 | 1372 | if interactive: |
|
1368 | 1373 | for name in vdict: |
|
1369 | 1374 | user_ns_hidden.pop(name, None) |
|
1370 | 1375 | else: |
|
1371 | 1376 | user_ns_hidden.update(vdict) |
|
1372 | 1377 | |
|
1373 | 1378 | def drop_by_id(self, variables): |
|
1374 | 1379 | """Remove a dict of variables from the user namespace, if they are the |
|
1375 | 1380 | same as the values in the dictionary. |
|
1376 | 1381 | |
|
1377 | 1382 | This is intended for use by extensions: variables that they've added can |
|
1378 | 1383 | be taken back out if they are unloaded, without removing any that the |
|
1379 | 1384 | user has overwritten. |
|
1380 | 1385 | |
|
1381 | 1386 | Parameters |
|
1382 | 1387 | ---------- |
|
1383 | 1388 | variables : dict |
|
1384 | 1389 | A dictionary mapping object names (as strings) to the objects. |
|
1385 | 1390 | """ |
|
1386 | 1391 | for name, obj in iteritems(variables): |
|
1387 | 1392 | if name in self.user_ns and self.user_ns[name] is obj: |
|
1388 | 1393 | del self.user_ns[name] |
|
1389 | 1394 | self.user_ns_hidden.pop(name, None) |
|
1390 | 1395 | |
|
1391 | 1396 | #------------------------------------------------------------------------- |
|
1392 | 1397 | # Things related to object introspection |
|
1393 | 1398 | #------------------------------------------------------------------------- |
|
1394 | 1399 | |
|
1395 | 1400 | def _ofind(self, oname, namespaces=None): |
|
1396 | 1401 | """Find an object in the available namespaces. |
|
1397 | 1402 | |
|
1398 | 1403 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
1399 | 1404 | |
|
1400 | 1405 | Has special code to detect magic functions. |
|
1401 | 1406 | """ |
|
1402 | 1407 | oname = oname.strip() |
|
1403 | 1408 | #print '1- oname: <%r>' % oname # dbg |
|
1404 | 1409 | if not oname.startswith(ESC_MAGIC) and \ |
|
1405 | 1410 | not oname.startswith(ESC_MAGIC2) and \ |
|
1406 | 1411 | not py3compat.isidentifier(oname, dotted=True): |
|
1407 | 1412 | return dict(found=False) |
|
1408 | 1413 | |
|
1409 | 1414 | if namespaces is None: |
|
1410 | 1415 | # Namespaces to search in: |
|
1411 | 1416 | # Put them in a list. The order is important so that we |
|
1412 | 1417 | # find things in the same order that Python finds them. |
|
1413 | 1418 | namespaces = [ ('Interactive', self.user_ns), |
|
1414 | 1419 | ('Interactive (global)', self.user_global_ns), |
|
1415 | 1420 | ('Python builtin', builtin_mod.__dict__), |
|
1416 | 1421 | ] |
|
1417 | 1422 | |
|
1418 | 1423 | # initialize results to 'null' |
|
1419 | 1424 | found = False; obj = None; ospace = None; |
|
1420 | 1425 | ismagic = False; isalias = False; parent = None |
|
1421 | 1426 | |
|
1422 | 1427 | # We need to special-case 'print', which as of python2.6 registers as a |
|
1423 | 1428 | # function but should only be treated as one if print_function was |
|
1424 | 1429 | # loaded with a future import. In this case, just bail. |
|
1425 | 1430 | if (oname == 'print' and not py3compat.PY3 and not \ |
|
1426 | 1431 | (self.compile.compiler_flags & __future__.CO_FUTURE_PRINT_FUNCTION)): |
|
1427 | 1432 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1428 | 1433 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1429 | 1434 | |
|
1430 | 1435 | # Look for the given name by splitting it in parts. If the head is |
|
1431 | 1436 | # found, then we look for all the remaining parts as members, and only |
|
1432 | 1437 | # declare success if we can find them all. |
|
1433 | 1438 | oname_parts = oname.split('.') |
|
1434 | 1439 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
1435 | 1440 | for nsname,ns in namespaces: |
|
1436 | 1441 | try: |
|
1437 | 1442 | obj = ns[oname_head] |
|
1438 | 1443 | except KeyError: |
|
1439 | 1444 | continue |
|
1440 | 1445 | else: |
|
1441 | 1446 | #print 'oname_rest:', oname_rest # dbg |
|
1442 | 1447 | for idx, part in enumerate(oname_rest): |
|
1443 | 1448 | try: |
|
1444 | 1449 | parent = obj |
|
1445 | 1450 | # The last part is looked up in a special way to avoid |
|
1446 | 1451 | # descriptor invocation as it may raise or have side |
|
1447 | 1452 | # effects. |
|
1448 | 1453 | if idx == len(oname_rest) - 1: |
|
1449 | 1454 | obj = self._getattr_property(obj, part) |
|
1450 | 1455 | else: |
|
1451 | 1456 | obj = getattr(obj, part) |
|
1452 | 1457 | except: |
|
1453 | 1458 | # Blanket except b/c some badly implemented objects |
|
1454 | 1459 | # allow __getattr__ to raise exceptions other than |
|
1455 | 1460 | # AttributeError, which then crashes IPython. |
|
1456 | 1461 | break |
|
1457 | 1462 | else: |
|
1458 | 1463 | # If we finish the for loop (no break), we got all members |
|
1459 | 1464 | found = True |
|
1460 | 1465 | ospace = nsname |
|
1461 | 1466 | break # namespace loop |
|
1462 | 1467 | |
|
1463 | 1468 | # Try to see if it's magic |
|
1464 | 1469 | if not found: |
|
1465 | 1470 | obj = None |
|
1466 | 1471 | if oname.startswith(ESC_MAGIC2): |
|
1467 | 1472 | oname = oname.lstrip(ESC_MAGIC2) |
|
1468 | 1473 | obj = self.find_cell_magic(oname) |
|
1469 | 1474 | elif oname.startswith(ESC_MAGIC): |
|
1470 | 1475 | oname = oname.lstrip(ESC_MAGIC) |
|
1471 | 1476 | obj = self.find_line_magic(oname) |
|
1472 | 1477 | else: |
|
1473 | 1478 | # search without prefix, so run? will find %run? |
|
1474 | 1479 | obj = self.find_line_magic(oname) |
|
1475 | 1480 | if obj is None: |
|
1476 | 1481 | obj = self.find_cell_magic(oname) |
|
1477 | 1482 | if obj is not None: |
|
1478 | 1483 | found = True |
|
1479 | 1484 | ospace = 'IPython internal' |
|
1480 | 1485 | ismagic = True |
|
1481 | 1486 | isalias = isinstance(obj, Alias) |
|
1482 | 1487 | |
|
1483 | 1488 | # Last try: special-case some literals like '', [], {}, etc: |
|
1484 | 1489 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
1485 | 1490 | obj = eval(oname_head) |
|
1486 | 1491 | found = True |
|
1487 | 1492 | ospace = 'Interactive' |
|
1488 | 1493 | |
|
1489 | 1494 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
1490 | 1495 | 'ismagic':ismagic, 'isalias':isalias, 'parent':parent} |
|
1491 | 1496 | |
|
1492 | 1497 | @staticmethod |
|
1493 | 1498 | def _getattr_property(obj, attrname): |
|
1494 | 1499 | """Property-aware getattr to use in object finding. |
|
1495 | 1500 | |
|
1496 | 1501 | If attrname represents a property, return it unevaluated (in case it has |
|
1497 | 1502 | side effects or raises an error. |
|
1498 | 1503 | |
|
1499 | 1504 | """ |
|
1500 | 1505 | if not isinstance(obj, type): |
|
1501 | 1506 | try: |
|
1502 | 1507 | # `getattr(type(obj), attrname)` is not guaranteed to return |
|
1503 | 1508 | # `obj`, but does so for property: |
|
1504 | 1509 | # |
|
1505 | 1510 | # property.__get__(self, None, cls) -> self |
|
1506 | 1511 | # |
|
1507 | 1512 | # The universal alternative is to traverse the mro manually |
|
1508 | 1513 | # searching for attrname in class dicts. |
|
1509 | 1514 | attr = getattr(type(obj), attrname) |
|
1510 | 1515 | except AttributeError: |
|
1511 | 1516 | pass |
|
1512 | 1517 | else: |
|
1513 | 1518 | # This relies on the fact that data descriptors (with both |
|
1514 | 1519 | # __get__ & __set__ magic methods) take precedence over |
|
1515 | 1520 | # instance-level attributes: |
|
1516 | 1521 | # |
|
1517 | 1522 | # class A(object): |
|
1518 | 1523 | # @property |
|
1519 | 1524 | # def foobar(self): return 123 |
|
1520 | 1525 | # a = A() |
|
1521 | 1526 | # a.__dict__['foobar'] = 345 |
|
1522 | 1527 | # a.foobar # == 123 |
|
1523 | 1528 | # |
|
1524 | 1529 | # So, a property may be returned right away. |
|
1525 | 1530 | if isinstance(attr, property): |
|
1526 | 1531 | return attr |
|
1527 | 1532 | |
|
1528 | 1533 | # Nothing helped, fall back. |
|
1529 | 1534 | return getattr(obj, attrname) |
|
1530 | 1535 | |
|
1531 | 1536 | def _object_find(self, oname, namespaces=None): |
|
1532 | 1537 | """Find an object and return a struct with info about it.""" |
|
1533 | 1538 | return Struct(self._ofind(oname, namespaces)) |
|
1534 | 1539 | |
|
1535 | 1540 | def _inspect(self, meth, oname, namespaces=None, **kw): |
|
1536 | 1541 | """Generic interface to the inspector system. |
|
1537 | 1542 | |
|
1538 | 1543 | This function is meant to be called by pdef, pdoc & friends.""" |
|
1539 | 1544 | info = self._object_find(oname, namespaces) |
|
1540 | 1545 | if info.found: |
|
1541 | 1546 | pmethod = getattr(self.inspector, meth) |
|
1542 | 1547 | formatter = format_screen if info.ismagic else None |
|
1543 | 1548 | if meth == 'pdoc': |
|
1544 | 1549 | pmethod(info.obj, oname, formatter) |
|
1545 | 1550 | elif meth == 'pinfo': |
|
1546 | 1551 | pmethod(info.obj, oname, formatter, info, **kw) |
|
1547 | 1552 | else: |
|
1548 | 1553 | pmethod(info.obj, oname) |
|
1549 | 1554 | else: |
|
1550 | 1555 | print('Object `%s` not found.' % oname) |
|
1551 | 1556 | return 'not found' # so callers can take other action |
|
1552 | 1557 | |
|
1553 | 1558 | def object_inspect(self, oname, detail_level=0): |
|
1554 | 1559 | """Get object info about oname""" |
|
1555 | 1560 | with self.builtin_trap: |
|
1556 | 1561 | info = self._object_find(oname) |
|
1557 | 1562 | if info.found: |
|
1558 | 1563 | return self.inspector.info(info.obj, oname, info=info, |
|
1559 | 1564 | detail_level=detail_level |
|
1560 | 1565 | ) |
|
1561 | 1566 | else: |
|
1562 | 1567 | return oinspect.object_info(name=oname, found=False) |
|
1563 | 1568 | |
|
1564 | 1569 | def object_inspect_text(self, oname, detail_level=0): |
|
1565 | 1570 | """Get object info as formatted text""" |
|
1566 | 1571 | with self.builtin_trap: |
|
1567 | 1572 | info = self._object_find(oname) |
|
1568 | 1573 | if info.found: |
|
1569 | 1574 | return self.inspector._format_info(info.obj, oname, info=info, |
|
1570 | 1575 | detail_level=detail_level |
|
1571 | 1576 | ) |
|
1572 | 1577 | else: |
|
1573 | 1578 | raise KeyError(oname) |
|
1574 | 1579 | |
|
1575 | 1580 | #------------------------------------------------------------------------- |
|
1576 | 1581 | # Things related to history management |
|
1577 | 1582 | #------------------------------------------------------------------------- |
|
1578 | 1583 | |
|
1579 | 1584 | def init_history(self): |
|
1580 | 1585 | """Sets up the command history, and starts regular autosaves.""" |
|
1581 | 1586 | self.history_manager = HistoryManager(shell=self, parent=self) |
|
1582 | 1587 | self.configurables.append(self.history_manager) |
|
1583 | 1588 | |
|
1584 | 1589 | #------------------------------------------------------------------------- |
|
1585 | 1590 | # Things related to exception handling and tracebacks (not debugging) |
|
1586 | 1591 | #------------------------------------------------------------------------- |
|
1587 | 1592 | |
|
1588 | 1593 | def init_traceback_handlers(self, custom_exceptions): |
|
1589 | 1594 | # Syntax error handler. |
|
1590 | 1595 | self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor') |
|
1591 | 1596 | |
|
1592 | 1597 | # The interactive one is initialized with an offset, meaning we always |
|
1593 | 1598 | # want to remove the topmost item in the traceback, which is our own |
|
1594 | 1599 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
1595 | 1600 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
1596 | 1601 | color_scheme='NoColor', |
|
1597 | 1602 | tb_offset = 1, |
|
1598 | 1603 | check_cache=check_linecache_ipython) |
|
1599 | 1604 | |
|
1600 | 1605 | # The instance will store a pointer to the system-wide exception hook, |
|
1601 | 1606 | # so that runtime code (such as magics) can access it. This is because |
|
1602 | 1607 | # during the read-eval loop, it may get temporarily overwritten. |
|
1603 | 1608 | self.sys_excepthook = sys.excepthook |
|
1604 | 1609 | |
|
1605 | 1610 | # and add any custom exception handlers the user may have specified |
|
1606 | 1611 | self.set_custom_exc(*custom_exceptions) |
|
1607 | 1612 | |
|
1608 | 1613 | # Set the exception mode |
|
1609 | 1614 | self.InteractiveTB.set_mode(mode=self.xmode) |
|
1610 | 1615 | |
|
1611 | 1616 | def set_custom_exc(self, exc_tuple, handler): |
|
1612 | 1617 | """set_custom_exc(exc_tuple,handler) |
|
1613 | 1618 | |
|
1614 | 1619 | Set a custom exception handler, which will be called if any of the |
|
1615 | 1620 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1616 | 1621 | run_code() method). |
|
1617 | 1622 | |
|
1618 | 1623 | Parameters |
|
1619 | 1624 | ---------- |
|
1620 | 1625 | |
|
1621 | 1626 | exc_tuple : tuple of exception classes |
|
1622 | 1627 | A *tuple* of exception classes, for which to call the defined |
|
1623 | 1628 | handler. It is very important that you use a tuple, and NOT A |
|
1624 | 1629 | LIST here, because of the way Python's except statement works. If |
|
1625 | 1630 | you only want to trap a single exception, use a singleton tuple:: |
|
1626 | 1631 | |
|
1627 | 1632 | exc_tuple == (MyCustomException,) |
|
1628 | 1633 | |
|
1629 | 1634 | handler : callable |
|
1630 | 1635 | handler must have the following signature:: |
|
1631 | 1636 | |
|
1632 | 1637 | def my_handler(self, etype, value, tb, tb_offset=None): |
|
1633 | 1638 | ... |
|
1634 | 1639 | return structured_traceback |
|
1635 | 1640 | |
|
1636 | 1641 | Your handler must return a structured traceback (a list of strings), |
|
1637 | 1642 | or None. |
|
1638 | 1643 | |
|
1639 | 1644 | This will be made into an instance method (via types.MethodType) |
|
1640 | 1645 | of IPython itself, and it will be called if any of the exceptions |
|
1641 | 1646 | listed in the exc_tuple are caught. If the handler is None, an |
|
1642 | 1647 | internal basic one is used, which just prints basic info. |
|
1643 | 1648 | |
|
1644 | 1649 | To protect IPython from crashes, if your handler ever raises an |
|
1645 | 1650 | exception or returns an invalid result, it will be immediately |
|
1646 | 1651 | disabled. |
|
1647 | 1652 | |
|
1648 | 1653 | WARNING: by putting in your own exception handler into IPython's main |
|
1649 | 1654 | execution loop, you run a very good chance of nasty crashes. This |
|
1650 | 1655 | facility should only be used if you really know what you are doing.""" |
|
1651 | 1656 | |
|
1652 | 1657 | assert type(exc_tuple)==type(()) , \ |
|
1653 | 1658 | "The custom exceptions must be given AS A TUPLE." |
|
1654 | 1659 | |
|
1655 | 1660 | def dummy_handler(self,etype,value,tb,tb_offset=None): |
|
1656 | 1661 | print('*** Simple custom exception handler ***') |
|
1657 | 1662 | print('Exception type :',etype) |
|
1658 | 1663 | print('Exception value:',value) |
|
1659 | 1664 | print('Traceback :',tb) |
|
1660 | 1665 | #print 'Source code :','\n'.join(self.buffer) |
|
1661 | 1666 | |
|
1662 | 1667 | def validate_stb(stb): |
|
1663 | 1668 | """validate structured traceback return type |
|
1664 | 1669 | |
|
1665 | 1670 | return type of CustomTB *should* be a list of strings, but allow |
|
1666 | 1671 | single strings or None, which are harmless. |
|
1667 | 1672 | |
|
1668 | 1673 | This function will *always* return a list of strings, |
|
1669 | 1674 | and will raise a TypeError if stb is inappropriate. |
|
1670 | 1675 | """ |
|
1671 | 1676 | msg = "CustomTB must return list of strings, not %r" % stb |
|
1672 | 1677 | if stb is None: |
|
1673 | 1678 | return [] |
|
1674 | 1679 | elif isinstance(stb, string_types): |
|
1675 | 1680 | return [stb] |
|
1676 | 1681 | elif not isinstance(stb, list): |
|
1677 | 1682 | raise TypeError(msg) |
|
1678 | 1683 | # it's a list |
|
1679 | 1684 | for line in stb: |
|
1680 | 1685 | # check every element |
|
1681 | 1686 | if not isinstance(line, string_types): |
|
1682 | 1687 | raise TypeError(msg) |
|
1683 | 1688 | return stb |
|
1684 | 1689 | |
|
1685 | 1690 | if handler is None: |
|
1686 | 1691 | wrapped = dummy_handler |
|
1687 | 1692 | else: |
|
1688 | 1693 | def wrapped(self,etype,value,tb,tb_offset=None): |
|
1689 | 1694 | """wrap CustomTB handler, to protect IPython from user code |
|
1690 | 1695 | |
|
1691 | 1696 | This makes it harder (but not impossible) for custom exception |
|
1692 | 1697 | handlers to crash IPython. |
|
1693 | 1698 | """ |
|
1694 | 1699 | try: |
|
1695 | 1700 | stb = handler(self,etype,value,tb,tb_offset=tb_offset) |
|
1696 | 1701 | return validate_stb(stb) |
|
1697 | 1702 | except: |
|
1698 | 1703 | # clear custom handler immediately |
|
1699 | 1704 | self.set_custom_exc((), None) |
|
1700 | 1705 | print("Custom TB Handler failed, unregistering", file=sys.stderr) |
|
1701 | 1706 | # show the exception in handler first |
|
1702 | 1707 | stb = self.InteractiveTB.structured_traceback(*sys.exc_info()) |
|
1703 | 1708 | print(self.InteractiveTB.stb2text(stb)) |
|
1704 | 1709 | print("The original exception:") |
|
1705 | 1710 | stb = self.InteractiveTB.structured_traceback( |
|
1706 | 1711 | (etype,value,tb), tb_offset=tb_offset |
|
1707 | 1712 | ) |
|
1708 | 1713 | return stb |
|
1709 | 1714 | |
|
1710 | 1715 | self.CustomTB = types.MethodType(wrapped,self) |
|
1711 | 1716 | self.custom_exceptions = exc_tuple |
|
1712 | 1717 | |
|
1713 | 1718 | def excepthook(self, etype, value, tb): |
|
1714 | 1719 | """One more defense for GUI apps that call sys.excepthook. |
|
1715 | 1720 | |
|
1716 | 1721 | GUI frameworks like wxPython trap exceptions and call |
|
1717 | 1722 | sys.excepthook themselves. I guess this is a feature that |
|
1718 | 1723 | enables them to keep running after exceptions that would |
|
1719 | 1724 | otherwise kill their mainloop. This is a bother for IPython |
|
1720 | 1725 | which excepts to catch all of the program exceptions with a try: |
|
1721 | 1726 | except: statement. |
|
1722 | 1727 | |
|
1723 | 1728 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1724 | 1729 | any app directly invokes sys.excepthook, it will look to the user like |
|
1725 | 1730 | IPython crashed. In order to work around this, we can disable the |
|
1726 | 1731 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1727 | 1732 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1728 | 1733 | call sys.excepthook will generate a regular-looking exception from |
|
1729 | 1734 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1730 | 1735 | crashes. |
|
1731 | 1736 | |
|
1732 | 1737 | This hook should be used sparingly, only in places which are not likely |
|
1733 | 1738 | to be true IPython errors. |
|
1734 | 1739 | """ |
|
1735 | 1740 | self.showtraceback((etype, value, tb), tb_offset=0) |
|
1736 | 1741 | |
|
1737 | 1742 | def _get_exc_info(self, exc_tuple=None): |
|
1738 | 1743 | """get exc_info from a given tuple, sys.exc_info() or sys.last_type etc. |
|
1739 | 1744 | |
|
1740 | 1745 | Ensures sys.last_type,value,traceback hold the exc_info we found, |
|
1741 | 1746 | from whichever source. |
|
1742 | 1747 | |
|
1743 | 1748 | raises ValueError if none of these contain any information |
|
1744 | 1749 | """ |
|
1745 | 1750 | if exc_tuple is None: |
|
1746 | 1751 | etype, value, tb = sys.exc_info() |
|
1747 | 1752 | else: |
|
1748 | 1753 | etype, value, tb = exc_tuple |
|
1749 | 1754 | |
|
1750 | 1755 | if etype is None: |
|
1751 | 1756 | if hasattr(sys, 'last_type'): |
|
1752 | 1757 | etype, value, tb = sys.last_type, sys.last_value, \ |
|
1753 | 1758 | sys.last_traceback |
|
1754 | 1759 | |
|
1755 | 1760 | if etype is None: |
|
1756 | 1761 | raise ValueError("No exception to find") |
|
1757 | 1762 | |
|
1758 | 1763 | # Now store the exception info in sys.last_type etc. |
|
1759 | 1764 | # WARNING: these variables are somewhat deprecated and not |
|
1760 | 1765 | # necessarily safe to use in a threaded environment, but tools |
|
1761 | 1766 | # like pdb depend on their existence, so let's set them. If we |
|
1762 | 1767 | # find problems in the field, we'll need to revisit their use. |
|
1763 | 1768 | sys.last_type = etype |
|
1764 | 1769 | sys.last_value = value |
|
1765 | 1770 | sys.last_traceback = tb |
|
1766 | 1771 | |
|
1767 | 1772 | return etype, value, tb |
|
1768 | 1773 | |
|
1769 | 1774 | def show_usage_error(self, exc): |
|
1770 | 1775 | """Show a short message for UsageErrors |
|
1771 | 1776 | |
|
1772 | 1777 | These are special exceptions that shouldn't show a traceback. |
|
1773 | 1778 | """ |
|
1774 | 1779 | print("UsageError: %s" % exc, file=sys.stderr) |
|
1775 | 1780 | |
|
1776 | 1781 | def get_exception_only(self, exc_tuple=None): |
|
1777 | 1782 | """ |
|
1778 | 1783 | Return as a string (ending with a newline) the exception that |
|
1779 | 1784 | just occurred, without any traceback. |
|
1780 | 1785 | """ |
|
1781 | 1786 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1782 | 1787 | msg = traceback.format_exception_only(etype, value) |
|
1783 | 1788 | return ''.join(msg) |
|
1784 | 1789 | |
|
1785 | 1790 | def showtraceback(self, exc_tuple=None, filename=None, tb_offset=None, |
|
1786 | 1791 | exception_only=False): |
|
1787 | 1792 | """Display the exception that just occurred. |
|
1788 | 1793 | |
|
1789 | 1794 | If nothing is known about the exception, this is the method which |
|
1790 | 1795 | should be used throughout the code for presenting user tracebacks, |
|
1791 | 1796 | rather than directly invoking the InteractiveTB object. |
|
1792 | 1797 | |
|
1793 | 1798 | A specific showsyntaxerror() also exists, but this method can take |
|
1794 | 1799 | care of calling it if needed, so unless you are explicitly catching a |
|
1795 | 1800 | SyntaxError exception, don't try to analyze the stack manually and |
|
1796 | 1801 | simply call this method.""" |
|
1797 | 1802 | |
|
1798 | 1803 | try: |
|
1799 | 1804 | try: |
|
1800 | 1805 | etype, value, tb = self._get_exc_info(exc_tuple) |
|
1801 | 1806 | except ValueError: |
|
1802 | 1807 | print('No traceback available to show.', file=sys.stderr) |
|
1803 | 1808 | return |
|
1804 | 1809 | |
|
1805 | 1810 | if issubclass(etype, SyntaxError): |
|
1806 | 1811 | # Though this won't be called by syntax errors in the input |
|
1807 | 1812 | # line, there may be SyntaxError cases with imported code. |
|
1808 | 1813 | self.showsyntaxerror(filename) |
|
1809 | 1814 | elif etype is UsageError: |
|
1810 | 1815 | self.show_usage_error(value) |
|
1811 | 1816 | else: |
|
1812 | 1817 | if exception_only: |
|
1813 | 1818 | stb = ['An exception has occurred, use %tb to see ' |
|
1814 | 1819 | 'the full traceback.\n'] |
|
1815 | 1820 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
|
1816 | 1821 | value)) |
|
1817 | 1822 | else: |
|
1818 | 1823 | try: |
|
1819 | 1824 | # Exception classes can customise their traceback - we |
|
1820 | 1825 | # use this in IPython.parallel for exceptions occurring |
|
1821 | 1826 | # in the engines. This should return a list of strings. |
|
1822 | 1827 | stb = value._render_traceback_() |
|
1823 | 1828 | except Exception: |
|
1824 | 1829 | stb = self.InteractiveTB.structured_traceback(etype, |
|
1825 | 1830 | value, tb, tb_offset=tb_offset) |
|
1826 | 1831 | |
|
1827 | 1832 | self._showtraceback(etype, value, stb) |
|
1828 | 1833 | if self.call_pdb: |
|
1829 | 1834 | # drop into debugger |
|
1830 | 1835 | self.debugger(force=True) |
|
1831 | 1836 | return |
|
1832 | 1837 | |
|
1833 | 1838 | # Actually show the traceback |
|
1834 | 1839 | self._showtraceback(etype, value, stb) |
|
1835 | 1840 | |
|
1836 | 1841 | except KeyboardInterrupt: |
|
1837 | 1842 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
1838 | 1843 | |
|
1839 | 1844 | def _showtraceback(self, etype, evalue, stb): |
|
1840 | 1845 | """Actually show a traceback. |
|
1841 | 1846 | |
|
1842 | 1847 | Subclasses may override this method to put the traceback on a different |
|
1843 | 1848 | place, like a side channel. |
|
1844 | 1849 | """ |
|
1845 | 1850 | print(self.InteractiveTB.stb2text(stb)) |
|
1846 | 1851 | |
|
1847 | 1852 | def showsyntaxerror(self, filename=None): |
|
1848 | 1853 | """Display the syntax error that just occurred. |
|
1849 | 1854 | |
|
1850 | 1855 | This doesn't display a stack trace because there isn't one. |
|
1851 | 1856 | |
|
1852 | 1857 | If a filename is given, it is stuffed in the exception instead |
|
1853 | 1858 | of what was there before (because Python's parser always uses |
|
1854 | 1859 | "<string>" when reading from a string). |
|
1855 | 1860 | """ |
|
1856 | 1861 | etype, value, last_traceback = self._get_exc_info() |
|
1857 | 1862 | |
|
1858 | 1863 | if filename and issubclass(etype, SyntaxError): |
|
1859 | 1864 | try: |
|
1860 | 1865 | value.filename = filename |
|
1861 | 1866 | except: |
|
1862 | 1867 | # Not the format we expect; leave it alone |
|
1863 | 1868 | pass |
|
1864 | 1869 | |
|
1865 | 1870 | stb = self.SyntaxTB.structured_traceback(etype, value, []) |
|
1866 | 1871 | self._showtraceback(etype, value, stb) |
|
1867 | 1872 | |
|
1868 | 1873 | # This is overridden in TerminalInteractiveShell to show a message about |
|
1869 | 1874 | # the %paste magic. |
|
1870 | 1875 | def showindentationerror(self): |
|
1871 | 1876 | """Called by run_cell when there's an IndentationError in code entered |
|
1872 | 1877 | at the prompt. |
|
1873 | 1878 | |
|
1874 | 1879 | This is overridden in TerminalInteractiveShell to show a message about |
|
1875 | 1880 | the %paste magic.""" |
|
1876 | 1881 | self.showsyntaxerror() |
|
1877 | 1882 | |
|
1878 | 1883 | #------------------------------------------------------------------------- |
|
1879 | 1884 | # Things related to readline |
|
1880 | 1885 | #------------------------------------------------------------------------- |
|
1881 | 1886 | |
|
1882 | 1887 | def init_readline(self): |
|
1883 | 1888 | """Moved to terminal subclass, here only to simplify the init logic.""" |
|
1884 | 1889 | self.readline = None |
|
1885 | 1890 | # Set a number of methods that depend on readline to be no-op |
|
1886 | 1891 | self.readline_no_record = NoOpContext() |
|
1887 | 1892 | self.set_readline_completer = no_op |
|
1888 | 1893 | self.set_custom_completer = no_op |
|
1889 | 1894 | |
|
1890 | 1895 | @skip_doctest |
|
1891 | 1896 | def set_next_input(self, s, replace=False): |
|
1892 | 1897 | """ Sets the 'default' input string for the next command line. |
|
1893 | 1898 | |
|
1894 | 1899 | Example:: |
|
1895 | 1900 | |
|
1896 | 1901 | In [1]: _ip.set_next_input("Hello Word") |
|
1897 | 1902 | In [2]: Hello Word_ # cursor is here |
|
1898 | 1903 | """ |
|
1899 | 1904 | self.rl_next_input = py3compat.cast_bytes_py2(s) |
|
1900 | 1905 | |
|
1901 | 1906 | def _indent_current_str(self): |
|
1902 | 1907 | """return the current level of indentation as a string""" |
|
1903 | 1908 | return self.input_splitter.indent_spaces * ' ' |
|
1904 | 1909 | |
|
1905 | 1910 | #------------------------------------------------------------------------- |
|
1906 | 1911 | # Things related to text completion |
|
1907 | 1912 | #------------------------------------------------------------------------- |
|
1908 | 1913 | |
|
1909 | 1914 | def init_completer(self): |
|
1910 | 1915 | """Initialize the completion machinery. |
|
1911 | 1916 | |
|
1912 | 1917 | This creates completion machinery that can be used by client code, |
|
1913 | 1918 | either interactively in-process (typically triggered by the readline |
|
1914 | 1919 | library), programmatically (such as in test suites) or out-of-process |
|
1915 | 1920 | (typically over the network by remote frontends). |
|
1916 | 1921 | """ |
|
1917 | 1922 | from IPython.core.completer import IPCompleter |
|
1918 | 1923 | from IPython.core.completerlib import (module_completer, |
|
1919 | 1924 | magic_run_completer, cd_completer, reset_completer) |
|
1920 | 1925 | |
|
1921 | 1926 | self.Completer = IPCompleter(shell=self, |
|
1922 | 1927 | namespace=self.user_ns, |
|
1923 | 1928 | global_namespace=self.user_global_ns, |
|
1924 | 1929 | use_readline=self.has_readline, |
|
1925 | 1930 | parent=self, |
|
1926 | 1931 | ) |
|
1927 | 1932 | self.configurables.append(self.Completer) |
|
1928 | 1933 | |
|
1929 | 1934 | # Add custom completers to the basic ones built into IPCompleter |
|
1930 | 1935 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
1931 | 1936 | self.strdispatchers['complete_command'] = sdisp |
|
1932 | 1937 | self.Completer.custom_completers = sdisp |
|
1933 | 1938 | |
|
1934 | 1939 | self.set_hook('complete_command', module_completer, str_key = 'import') |
|
1935 | 1940 | self.set_hook('complete_command', module_completer, str_key = 'from') |
|
1936 | 1941 | self.set_hook('complete_command', module_completer, str_key = '%aimport') |
|
1937 | 1942 | self.set_hook('complete_command', magic_run_completer, str_key = '%run') |
|
1938 | 1943 | self.set_hook('complete_command', cd_completer, str_key = '%cd') |
|
1939 | 1944 | self.set_hook('complete_command', reset_completer, str_key = '%reset') |
|
1940 | 1945 | |
|
1941 | 1946 | |
|
1942 | 1947 | @skip_doctest_py2 |
|
1943 | 1948 | def complete(self, text, line=None, cursor_pos=None): |
|
1944 | 1949 | """Return the completed text and a list of completions. |
|
1945 | 1950 | |
|
1946 | 1951 | Parameters |
|
1947 | 1952 | ---------- |
|
1948 | 1953 | |
|
1949 | 1954 | text : string |
|
1950 | 1955 | A string of text to be completed on. It can be given as empty and |
|
1951 | 1956 | instead a line/position pair are given. In this case, the |
|
1952 | 1957 | completer itself will split the line like readline does. |
|
1953 | 1958 | |
|
1954 | 1959 | line : string, optional |
|
1955 | 1960 | The complete line that text is part of. |
|
1956 | 1961 | |
|
1957 | 1962 | cursor_pos : int, optional |
|
1958 | 1963 | The position of the cursor on the input line. |
|
1959 | 1964 | |
|
1960 | 1965 | Returns |
|
1961 | 1966 | ------- |
|
1962 | 1967 | text : string |
|
1963 | 1968 | The actual text that was completed. |
|
1964 | 1969 | |
|
1965 | 1970 | matches : list |
|
1966 | 1971 | A sorted list with all possible completions. |
|
1967 | 1972 | |
|
1968 | 1973 | The optional arguments allow the completion to take more context into |
|
1969 | 1974 | account, and are part of the low-level completion API. |
|
1970 | 1975 | |
|
1971 | 1976 | This is a wrapper around the completion mechanism, similar to what |
|
1972 | 1977 | readline does at the command line when the TAB key is hit. By |
|
1973 | 1978 | exposing it as a method, it can be used by other non-readline |
|
1974 | 1979 | environments (such as GUIs) for text completion. |
|
1975 | 1980 | |
|
1976 | 1981 | Simple usage example: |
|
1977 | 1982 | |
|
1978 | 1983 | In [1]: x = 'hello' |
|
1979 | 1984 | |
|
1980 | 1985 | In [2]: _ip.complete('x.l') |
|
1981 | 1986 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
1982 | 1987 | """ |
|
1983 | 1988 | |
|
1984 | 1989 | # Inject names into __builtin__ so we can complete on the added names. |
|
1985 | 1990 | with self.builtin_trap: |
|
1986 | 1991 | return self.Completer.complete(text, line, cursor_pos) |
|
1987 | 1992 | |
|
1988 | 1993 | def set_custom_completer(self, completer, pos=0): |
|
1989 | 1994 | """Adds a new custom completer function. |
|
1990 | 1995 | |
|
1991 | 1996 | The position argument (defaults to 0) is the index in the completers |
|
1992 | 1997 | list where you want the completer to be inserted.""" |
|
1993 | 1998 | |
|
1994 | 1999 | newcomp = types.MethodType(completer,self.Completer) |
|
1995 | 2000 | self.Completer.matchers.insert(pos,newcomp) |
|
1996 | 2001 | |
|
1997 | 2002 | def set_completer_frame(self, frame=None): |
|
1998 | 2003 | """Set the frame of the completer.""" |
|
1999 | 2004 | if frame: |
|
2000 | 2005 | self.Completer.namespace = frame.f_locals |
|
2001 | 2006 | self.Completer.global_namespace = frame.f_globals |
|
2002 | 2007 | else: |
|
2003 | 2008 | self.Completer.namespace = self.user_ns |
|
2004 | 2009 | self.Completer.global_namespace = self.user_global_ns |
|
2005 | 2010 | |
|
2006 | 2011 | #------------------------------------------------------------------------- |
|
2007 | 2012 | # Things related to magics |
|
2008 | 2013 | #------------------------------------------------------------------------- |
|
2009 | 2014 | |
|
2010 | 2015 | def init_magics(self): |
|
2011 | 2016 | from IPython.core import magics as m |
|
2012 | 2017 | self.magics_manager = magic.MagicsManager(shell=self, |
|
2013 | 2018 | parent=self, |
|
2014 | 2019 | user_magics=m.UserMagics(self)) |
|
2015 | 2020 | self.configurables.append(self.magics_manager) |
|
2016 | 2021 | |
|
2017 | 2022 | # Expose as public API from the magics manager |
|
2018 | 2023 | self.register_magics = self.magics_manager.register |
|
2019 | 2024 | self.define_magic = self.magics_manager.define_magic |
|
2020 | 2025 | |
|
2021 | 2026 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, |
|
2022 | 2027 | m.ConfigMagics, m.DeprecatedMagics, m.DisplayMagics, m.ExecutionMagics, |
|
2023 | 2028 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, |
|
2024 | 2029 | m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics, |
|
2025 | 2030 | ) |
|
2026 | 2031 | |
|
2027 | 2032 | # Register Magic Aliases |
|
2028 | 2033 | mman = self.magics_manager |
|
2029 | 2034 | # FIXME: magic aliases should be defined by the Magics classes |
|
2030 | 2035 | # or in MagicsManager, not here |
|
2031 | 2036 | mman.register_alias('ed', 'edit') |
|
2032 | 2037 | mman.register_alias('hist', 'history') |
|
2033 | 2038 | mman.register_alias('rep', 'recall') |
|
2034 | 2039 | mman.register_alias('SVG', 'svg', 'cell') |
|
2035 | 2040 | mman.register_alias('HTML', 'html', 'cell') |
|
2036 | 2041 | mman.register_alias('file', 'writefile', 'cell') |
|
2037 | 2042 | |
|
2038 | 2043 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2039 | 2044 | # should be split into a prompt manager and displayhook. We probably |
|
2040 | 2045 | # even need a centralize colors management object. |
|
2041 | 2046 | self.magic('colors %s' % self.colors) |
|
2042 | 2047 | |
|
2043 | 2048 | # Defined here so that it's included in the documentation |
|
2044 | 2049 | @functools.wraps(magic.MagicsManager.register_function) |
|
2045 | 2050 | def register_magic_function(self, func, magic_kind='line', magic_name=None): |
|
2046 | 2051 | self.magics_manager.register_function(func, |
|
2047 | 2052 | magic_kind=magic_kind, magic_name=magic_name) |
|
2048 | 2053 | |
|
2049 | 2054 | def run_line_magic(self, magic_name, line): |
|
2050 | 2055 | """Execute the given line magic. |
|
2051 | 2056 | |
|
2052 | 2057 | Parameters |
|
2053 | 2058 | ---------- |
|
2054 | 2059 | magic_name : str |
|
2055 | 2060 | Name of the desired magic function, without '%' prefix. |
|
2056 | 2061 | |
|
2057 | 2062 | line : str |
|
2058 | 2063 | The rest of the input line as a single string. |
|
2059 | 2064 | """ |
|
2060 | 2065 | fn = self.find_line_magic(magic_name) |
|
2061 | 2066 | if fn is None: |
|
2062 | 2067 | cm = self.find_cell_magic(magic_name) |
|
2063 | 2068 | etpl = "Line magic function `%%%s` not found%s." |
|
2064 | 2069 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' |
|
2065 | 2070 | 'did you mean that instead?)' % magic_name ) |
|
2066 | 2071 | error(etpl % (magic_name, extra)) |
|
2067 | 2072 | else: |
|
2068 | 2073 | # Note: this is the distance in the stack to the user's frame. |
|
2069 | 2074 | # This will need to be updated if the internal calling logic gets |
|
2070 | 2075 | # refactored, or else we'll be expanding the wrong variables. |
|
2071 | 2076 | stack_depth = 2 |
|
2072 | 2077 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2073 | 2078 | # Put magic args in a list so we can call with f(*a) syntax |
|
2074 | 2079 | args = [magic_arg_s] |
|
2075 | 2080 | kwargs = {} |
|
2076 | 2081 | # Grab local namespace if we need it: |
|
2077 | 2082 | if getattr(fn, "needs_local_scope", False): |
|
2078 | 2083 | kwargs['local_ns'] = sys._getframe(stack_depth).f_locals |
|
2079 | 2084 | with self.builtin_trap: |
|
2080 | 2085 | result = fn(*args,**kwargs) |
|
2081 | 2086 | return result |
|
2082 | 2087 | |
|
2083 | 2088 | def run_cell_magic(self, magic_name, line, cell): |
|
2084 | 2089 | """Execute the given cell magic. |
|
2085 | 2090 | |
|
2086 | 2091 | Parameters |
|
2087 | 2092 | ---------- |
|
2088 | 2093 | magic_name : str |
|
2089 | 2094 | Name of the desired magic function, without '%' prefix. |
|
2090 | 2095 | |
|
2091 | 2096 | line : str |
|
2092 | 2097 | The rest of the first input line as a single string. |
|
2093 | 2098 | |
|
2094 | 2099 | cell : str |
|
2095 | 2100 | The body of the cell as a (possibly multiline) string. |
|
2096 | 2101 | """ |
|
2097 | 2102 | fn = self.find_cell_magic(magic_name) |
|
2098 | 2103 | if fn is None: |
|
2099 | 2104 | lm = self.find_line_magic(magic_name) |
|
2100 | 2105 | etpl = "Cell magic `%%{0}` not found{1}." |
|
2101 | 2106 | extra = '' if lm is None else (' (But line magic `%{0}` exists, ' |
|
2102 | 2107 | 'did you mean that instead?)'.format(magic_name)) |
|
2103 | 2108 | error(etpl.format(magic_name, extra)) |
|
2104 | 2109 | elif cell == '': |
|
2105 | 2110 | message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) |
|
2106 | 2111 | if self.find_line_magic(magic_name) is not None: |
|
2107 | 2112 | message += ' Did you mean the line magic %{0} (single %)?'.format(magic_name) |
|
2108 | 2113 | raise UsageError(message) |
|
2109 | 2114 | else: |
|
2110 | 2115 | # Note: this is the distance in the stack to the user's frame. |
|
2111 | 2116 | # This will need to be updated if the internal calling logic gets |
|
2112 | 2117 | # refactored, or else we'll be expanding the wrong variables. |
|
2113 | 2118 | stack_depth = 2 |
|
2114 | 2119 | magic_arg_s = self.var_expand(line, stack_depth) |
|
2115 | 2120 | with self.builtin_trap: |
|
2116 | 2121 | result = fn(magic_arg_s, cell) |
|
2117 | 2122 | return result |
|
2118 | 2123 | |
|
2119 | 2124 | def find_line_magic(self, magic_name): |
|
2120 | 2125 | """Find and return a line magic by name. |
|
2121 | 2126 | |
|
2122 | 2127 | Returns None if the magic isn't found.""" |
|
2123 | 2128 | return self.magics_manager.magics['line'].get(magic_name) |
|
2124 | 2129 | |
|
2125 | 2130 | def find_cell_magic(self, magic_name): |
|
2126 | 2131 | """Find and return a cell magic by name. |
|
2127 | 2132 | |
|
2128 | 2133 | Returns None if the magic isn't found.""" |
|
2129 | 2134 | return self.magics_manager.magics['cell'].get(magic_name) |
|
2130 | 2135 | |
|
2131 | 2136 | def find_magic(self, magic_name, magic_kind='line'): |
|
2132 | 2137 | """Find and return a magic of the given type by name. |
|
2133 | 2138 | |
|
2134 | 2139 | Returns None if the magic isn't found.""" |
|
2135 | 2140 | return self.magics_manager.magics[magic_kind].get(magic_name) |
|
2136 | 2141 | |
|
2137 | 2142 | def magic(self, arg_s): |
|
2138 | 2143 | """DEPRECATED. Use run_line_magic() instead. |
|
2139 | 2144 | |
|
2140 | 2145 | Call a magic function by name. |
|
2141 | 2146 | |
|
2142 | 2147 | Input: a string containing the name of the magic function to call and |
|
2143 | 2148 | any additional arguments to be passed to the magic. |
|
2144 | 2149 | |
|
2145 | 2150 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
2146 | 2151 | prompt: |
|
2147 | 2152 | |
|
2148 | 2153 | In[1]: %name -opt foo bar |
|
2149 | 2154 | |
|
2150 | 2155 | To call a magic without arguments, simply use magic('name'). |
|
2151 | 2156 | |
|
2152 | 2157 | This provides a proper Python function to call IPython's magics in any |
|
2153 | 2158 | valid Python code you can type at the interpreter, including loops and |
|
2154 | 2159 | compound statements. |
|
2155 | 2160 | """ |
|
2156 | 2161 | # TODO: should we issue a loud deprecation warning here? |
|
2157 | 2162 | magic_name, _, magic_arg_s = arg_s.partition(' ') |
|
2158 | 2163 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2159 | 2164 | return self.run_line_magic(magic_name, magic_arg_s) |
|
2160 | 2165 | |
|
2161 | 2166 | #------------------------------------------------------------------------- |
|
2162 | 2167 | # Things related to macros |
|
2163 | 2168 | #------------------------------------------------------------------------- |
|
2164 | 2169 | |
|
2165 | 2170 | def define_macro(self, name, themacro): |
|
2166 | 2171 | """Define a new macro |
|
2167 | 2172 | |
|
2168 | 2173 | Parameters |
|
2169 | 2174 | ---------- |
|
2170 | 2175 | name : str |
|
2171 | 2176 | The name of the macro. |
|
2172 | 2177 | themacro : str or Macro |
|
2173 | 2178 | The action to do upon invoking the macro. If a string, a new |
|
2174 | 2179 | Macro object is created by passing the string to it. |
|
2175 | 2180 | """ |
|
2176 | 2181 | |
|
2177 | 2182 | from IPython.core import macro |
|
2178 | 2183 | |
|
2179 | 2184 | if isinstance(themacro, string_types): |
|
2180 | 2185 | themacro = macro.Macro(themacro) |
|
2181 | 2186 | if not isinstance(themacro, macro.Macro): |
|
2182 | 2187 | raise ValueError('A macro must be a string or a Macro instance.') |
|
2183 | 2188 | self.user_ns[name] = themacro |
|
2184 | 2189 | |
|
2185 | 2190 | #------------------------------------------------------------------------- |
|
2186 | 2191 | # Things related to the running of system commands |
|
2187 | 2192 | #------------------------------------------------------------------------- |
|
2188 | 2193 | |
|
2189 | 2194 | def system_piped(self, cmd): |
|
2190 | 2195 | """Call the given cmd in a subprocess, piping stdout/err |
|
2191 | 2196 | |
|
2192 | 2197 | Parameters |
|
2193 | 2198 | ---------- |
|
2194 | 2199 | cmd : str |
|
2195 | 2200 | Command to execute (can not end in '&', as background processes are |
|
2196 | 2201 | not supported. Should not be a command that expects input |
|
2197 | 2202 | other than simple text. |
|
2198 | 2203 | """ |
|
2199 | 2204 | if cmd.rstrip().endswith('&'): |
|
2200 | 2205 | # this is *far* from a rigorous test |
|
2201 | 2206 | # We do not support backgrounding processes because we either use |
|
2202 | 2207 | # pexpect or pipes to read from. Users can always just call |
|
2203 | 2208 | # os.system() or use ip.system=ip.system_raw |
|
2204 | 2209 | # if they really want a background process. |
|
2205 | 2210 | raise OSError("Background processes not supported.") |
|
2206 | 2211 | |
|
2207 | 2212 | # we explicitly do NOT return the subprocess status code, because |
|
2208 | 2213 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2209 | 2214 | # Instead, we store the exit_code in user_ns. |
|
2210 | 2215 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
|
2211 | 2216 | |
|
2212 | 2217 | def system_raw(self, cmd): |
|
2213 | 2218 | """Call the given cmd in a subprocess using os.system on Windows or |
|
2214 | 2219 | subprocess.call using the system shell on other platforms. |
|
2215 | 2220 | |
|
2216 | 2221 | Parameters |
|
2217 | 2222 | ---------- |
|
2218 | 2223 | cmd : str |
|
2219 | 2224 | Command to execute. |
|
2220 | 2225 | """ |
|
2221 | 2226 | cmd = self.var_expand(cmd, depth=1) |
|
2222 | 2227 | # protect os.system from UNC paths on Windows, which it can't handle: |
|
2223 | 2228 | if sys.platform == 'win32': |
|
2224 | 2229 | from IPython.utils._process_win32 import AvoidUNCPath |
|
2225 | 2230 | with AvoidUNCPath() as path: |
|
2226 | 2231 | if path is not None: |
|
2227 | 2232 | cmd = '"pushd %s &&"%s' % (path, cmd) |
|
2228 | 2233 | cmd = py3compat.unicode_to_str(cmd) |
|
2229 | 2234 | try: |
|
2230 | 2235 | ec = os.system(cmd) |
|
2231 | 2236 | except KeyboardInterrupt: |
|
2232 | 2237 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2233 | 2238 | ec = -2 |
|
2234 | 2239 | else: |
|
2235 | 2240 | cmd = py3compat.unicode_to_str(cmd) |
|
2236 | 2241 | # For posix the result of the subprocess.call() below is an exit |
|
2237 | 2242 | # code, which by convention is zero for success, positive for |
|
2238 | 2243 | # program failure. Exit codes above 128 are reserved for signals, |
|
2239 | 2244 | # and the formula for converting a signal to an exit code is usually |
|
2240 | 2245 | # signal_number+128. To more easily differentiate between exit |
|
2241 | 2246 | # codes and signals, ipython uses negative numbers. For instance |
|
2242 | 2247 | # since control-c is signal 2 but exit code 130, ipython's |
|
2243 | 2248 | # _exit_code variable will read -2. Note that some shells like |
|
2244 | 2249 | # csh and fish don't follow sh/bash conventions for exit codes. |
|
2245 | 2250 | executable = os.environ.get('SHELL', None) |
|
2246 | 2251 | try: |
|
2247 | 2252 | # Use env shell instead of default /bin/sh |
|
2248 | 2253 | ec = subprocess.call(cmd, shell=True, executable=executable) |
|
2249 | 2254 | except KeyboardInterrupt: |
|
2250 | 2255 | # intercept control-C; a long traceback is not useful here |
|
2251 | 2256 | print('\n' + self.get_exception_only(), file=sys.stderr) |
|
2252 | 2257 | ec = 130 |
|
2253 | 2258 | if ec > 128: |
|
2254 | 2259 | ec = -(ec - 128) |
|
2255 | 2260 | |
|
2256 | 2261 | # We explicitly do NOT return the subprocess status code, because |
|
2257 | 2262 | # a non-None value would trigger :func:`sys.displayhook` calls. |
|
2258 | 2263 | # Instead, we store the exit_code in user_ns. Note the semantics |
|
2259 | 2264 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
|
2260 | 2265 | # but raising SystemExit(_exit_code) will give status 254! |
|
2261 | 2266 | self.user_ns['_exit_code'] = ec |
|
2262 | 2267 | |
|
2263 | 2268 | # use piped system by default, because it is better behaved |
|
2264 | 2269 | system = system_piped |
|
2265 | 2270 | |
|
2266 | 2271 | def getoutput(self, cmd, split=True, depth=0): |
|
2267 | 2272 | """Get output (possibly including stderr) from a subprocess. |
|
2268 | 2273 | |
|
2269 | 2274 | Parameters |
|
2270 | 2275 | ---------- |
|
2271 | 2276 | cmd : str |
|
2272 | 2277 | Command to execute (can not end in '&', as background processes are |
|
2273 | 2278 | not supported. |
|
2274 | 2279 | split : bool, optional |
|
2275 | 2280 | If True, split the output into an IPython SList. Otherwise, an |
|
2276 | 2281 | IPython LSString is returned. These are objects similar to normal |
|
2277 | 2282 | lists and strings, with a few convenience attributes for easier |
|
2278 | 2283 | manipulation of line-based output. You can use '?' on them for |
|
2279 | 2284 | details. |
|
2280 | 2285 | depth : int, optional |
|
2281 | 2286 | How many frames above the caller are the local variables which should |
|
2282 | 2287 | be expanded in the command string? The default (0) assumes that the |
|
2283 | 2288 | expansion variables are in the stack frame calling this function. |
|
2284 | 2289 | """ |
|
2285 | 2290 | if cmd.rstrip().endswith('&'): |
|
2286 | 2291 | # this is *far* from a rigorous test |
|
2287 | 2292 | raise OSError("Background processes not supported.") |
|
2288 | 2293 | out = getoutput(self.var_expand(cmd, depth=depth+1)) |
|
2289 | 2294 | if split: |
|
2290 | 2295 | out = SList(out.splitlines()) |
|
2291 | 2296 | else: |
|
2292 | 2297 | out = LSString(out) |
|
2293 | 2298 | return out |
|
2294 | 2299 | |
|
2295 | 2300 | #------------------------------------------------------------------------- |
|
2296 | 2301 | # Things related to aliases |
|
2297 | 2302 | #------------------------------------------------------------------------- |
|
2298 | 2303 | |
|
2299 | 2304 | def init_alias(self): |
|
2300 | 2305 | self.alias_manager = AliasManager(shell=self, parent=self) |
|
2301 | 2306 | self.configurables.append(self.alias_manager) |
|
2302 | 2307 | |
|
2303 | 2308 | #------------------------------------------------------------------------- |
|
2304 | 2309 | # Things related to extensions |
|
2305 | 2310 | #------------------------------------------------------------------------- |
|
2306 | 2311 | |
|
2307 | 2312 | def init_extension_manager(self): |
|
2308 | 2313 | self.extension_manager = ExtensionManager(shell=self, parent=self) |
|
2309 | 2314 | self.configurables.append(self.extension_manager) |
|
2310 | 2315 | |
|
2311 | 2316 | #------------------------------------------------------------------------- |
|
2312 | 2317 | # Things related to payloads |
|
2313 | 2318 | #------------------------------------------------------------------------- |
|
2314 | 2319 | |
|
2315 | 2320 | def init_payload(self): |
|
2316 | 2321 | self.payload_manager = PayloadManager(parent=self) |
|
2317 | 2322 | self.configurables.append(self.payload_manager) |
|
2318 | 2323 | |
|
2319 | 2324 | #------------------------------------------------------------------------- |
|
2320 | 2325 | # Things related to the prefilter |
|
2321 | 2326 | #------------------------------------------------------------------------- |
|
2322 | 2327 | |
|
2323 | 2328 | def init_prefilter(self): |
|
2324 | 2329 | self.prefilter_manager = PrefilterManager(shell=self, parent=self) |
|
2325 | 2330 | self.configurables.append(self.prefilter_manager) |
|
2326 | 2331 | # Ultimately this will be refactored in the new interpreter code, but |
|
2327 | 2332 | # for now, we should expose the main prefilter method (there's legacy |
|
2328 | 2333 | # code out there that may rely on this). |
|
2329 | 2334 | self.prefilter = self.prefilter_manager.prefilter_lines |
|
2330 | 2335 | |
|
2331 | 2336 | def auto_rewrite_input(self, cmd): |
|
2332 | 2337 | """Print to the screen the rewritten form of the user's command. |
|
2333 | 2338 | |
|
2334 | 2339 | This shows visual feedback by rewriting input lines that cause |
|
2335 | 2340 | automatic calling to kick in, like:: |
|
2336 | 2341 | |
|
2337 | 2342 | /f x |
|
2338 | 2343 | |
|
2339 | 2344 | into:: |
|
2340 | 2345 | |
|
2341 | 2346 | ------> f(x) |
|
2342 | 2347 | |
|
2343 | 2348 | after the user's input prompt. This helps the user understand that the |
|
2344 | 2349 | input line was transformed automatically by IPython. |
|
2345 | 2350 | """ |
|
2346 | 2351 | if not self.show_rewritten_input: |
|
2347 | 2352 | return |
|
2348 | 2353 | |
|
2349 | 2354 | rw = self.prompt_manager.render('rewrite') + cmd |
|
2350 | 2355 | |
|
2351 | 2356 | try: |
|
2352 | 2357 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2353 | 2358 | # we use it and only print uncolored rewrite if we have unicode |
|
2354 | 2359 | rw = str(rw) |
|
2355 | 2360 | print(rw) |
|
2356 | 2361 | except UnicodeEncodeError: |
|
2357 | 2362 | print("------> " + cmd) |
|
2358 | 2363 | |
|
2359 | 2364 | #------------------------------------------------------------------------- |
|
2360 | 2365 | # Things related to extracting values/expressions from kernel and user_ns |
|
2361 | 2366 | #------------------------------------------------------------------------- |
|
2362 | 2367 | |
|
2363 | 2368 | def _user_obj_error(self): |
|
2364 | 2369 | """return simple exception dict |
|
2365 | 2370 | |
|
2366 | 2371 | for use in user_expressions |
|
2367 | 2372 | """ |
|
2368 | 2373 | |
|
2369 | 2374 | etype, evalue, tb = self._get_exc_info() |
|
2370 | 2375 | stb = self.InteractiveTB.get_exception_only(etype, evalue) |
|
2371 | 2376 | |
|
2372 | 2377 | exc_info = { |
|
2373 | 2378 | u'status' : 'error', |
|
2374 | 2379 | u'traceback' : stb, |
|
2375 | 2380 | u'ename' : unicode_type(etype.__name__), |
|
2376 | 2381 | u'evalue' : py3compat.safe_unicode(evalue), |
|
2377 | 2382 | } |
|
2378 | 2383 | |
|
2379 | 2384 | return exc_info |
|
2380 | 2385 | |
|
2381 | 2386 | def _format_user_obj(self, obj): |
|
2382 | 2387 | """format a user object to display dict |
|
2383 | 2388 | |
|
2384 | 2389 | for use in user_expressions |
|
2385 | 2390 | """ |
|
2386 | 2391 | |
|
2387 | 2392 | data, md = self.display_formatter.format(obj) |
|
2388 | 2393 | value = { |
|
2389 | 2394 | 'status' : 'ok', |
|
2390 | 2395 | 'data' : data, |
|
2391 | 2396 | 'metadata' : md, |
|
2392 | 2397 | } |
|
2393 | 2398 | return value |
|
2394 | 2399 | |
|
2395 | 2400 | def user_expressions(self, expressions): |
|
2396 | 2401 | """Evaluate a dict of expressions in the user's namespace. |
|
2397 | 2402 | |
|
2398 | 2403 | Parameters |
|
2399 | 2404 | ---------- |
|
2400 | 2405 | expressions : dict |
|
2401 | 2406 | A dict with string keys and string values. The expression values |
|
2402 | 2407 | should be valid Python expressions, each of which will be evaluated |
|
2403 | 2408 | in the user namespace. |
|
2404 | 2409 | |
|
2405 | 2410 | Returns |
|
2406 | 2411 | ------- |
|
2407 | 2412 | A dict, keyed like the input expressions dict, with the rich mime-typed |
|
2408 | 2413 | display_data of each value. |
|
2409 | 2414 | """ |
|
2410 | 2415 | out = {} |
|
2411 | 2416 | user_ns = self.user_ns |
|
2412 | 2417 | global_ns = self.user_global_ns |
|
2413 | 2418 | |
|
2414 | 2419 | for key, expr in iteritems(expressions): |
|
2415 | 2420 | try: |
|
2416 | 2421 | value = self._format_user_obj(eval(expr, global_ns, user_ns)) |
|
2417 | 2422 | except: |
|
2418 | 2423 | value = self._user_obj_error() |
|
2419 | 2424 | out[key] = value |
|
2420 | 2425 | return out |
|
2421 | 2426 | |
|
2422 | 2427 | #------------------------------------------------------------------------- |
|
2423 | 2428 | # Things related to the running of code |
|
2424 | 2429 | #------------------------------------------------------------------------- |
|
2425 | 2430 | |
|
2426 | 2431 | def ex(self, cmd): |
|
2427 | 2432 | """Execute a normal python statement in user namespace.""" |
|
2428 | 2433 | with self.builtin_trap: |
|
2429 | 2434 | exec(cmd, self.user_global_ns, self.user_ns) |
|
2430 | 2435 | |
|
2431 | 2436 | def ev(self, expr): |
|
2432 | 2437 | """Evaluate python expression expr in user namespace. |
|
2433 | 2438 | |
|
2434 | 2439 | Returns the result of evaluation |
|
2435 | 2440 | """ |
|
2436 | 2441 | with self.builtin_trap: |
|
2437 | 2442 | return eval(expr, self.user_global_ns, self.user_ns) |
|
2438 | 2443 | |
|
2439 | 2444 | def safe_execfile(self, fname, *where, **kw): |
|
2440 | 2445 | """A safe version of the builtin execfile(). |
|
2441 | 2446 | |
|
2442 | 2447 | This version will never throw an exception, but instead print |
|
2443 | 2448 | helpful error messages to the screen. This only works on pure |
|
2444 | 2449 | Python files with the .py extension. |
|
2445 | 2450 | |
|
2446 | 2451 | Parameters |
|
2447 | 2452 | ---------- |
|
2448 | 2453 | fname : string |
|
2449 | 2454 | The name of the file to be executed. |
|
2450 | 2455 | where : tuple |
|
2451 | 2456 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2452 | 2457 | If only one is given, it is passed as both. |
|
2453 | 2458 | exit_ignore : bool (False) |
|
2454 | 2459 | If True, then silence SystemExit for non-zero status (it is always |
|
2455 | 2460 | silenced for zero status, as it is so common). |
|
2456 | 2461 | raise_exceptions : bool (False) |
|
2457 | 2462 | If True raise exceptions everywhere. Meant for testing. |
|
2458 | 2463 | shell_futures : bool (False) |
|
2459 | 2464 | If True, the code will share future statements with the interactive |
|
2460 | 2465 | shell. It will both be affected by previous __future__ imports, and |
|
2461 | 2466 | any __future__ imports in the code will affect the shell. If False, |
|
2462 | 2467 | __future__ imports are not shared in either direction. |
|
2463 | 2468 | |
|
2464 | 2469 | """ |
|
2465 | 2470 | kw.setdefault('exit_ignore', False) |
|
2466 | 2471 | kw.setdefault('raise_exceptions', False) |
|
2467 | 2472 | kw.setdefault('shell_futures', False) |
|
2468 | 2473 | |
|
2469 | 2474 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2470 | 2475 | |
|
2471 | 2476 | # Make sure we can open the file |
|
2472 | 2477 | try: |
|
2473 | 2478 | with open(fname): |
|
2474 | 2479 | pass |
|
2475 | 2480 | except: |
|
2476 | 2481 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2477 | 2482 | return |
|
2478 | 2483 | |
|
2479 | 2484 | # Find things also in current directory. This is needed to mimic the |
|
2480 | 2485 | # behavior of running a script from the system command line, where |
|
2481 | 2486 | # Python inserts the script's directory into sys.path |
|
2482 | 2487 | dname = os.path.dirname(fname) |
|
2483 | 2488 | |
|
2484 | 2489 | with prepended_to_syspath(dname): |
|
2485 | 2490 | try: |
|
2486 | 2491 | glob, loc = (where + (None, ))[:2] |
|
2487 | 2492 | py3compat.execfile( |
|
2488 | 2493 | fname, glob, loc, |
|
2489 | 2494 | self.compile if kw['shell_futures'] else None) |
|
2490 | 2495 | except SystemExit as status: |
|
2491 | 2496 | # If the call was made with 0 or None exit status (sys.exit(0) |
|
2492 | 2497 | # or sys.exit() ), don't bother showing a traceback, as both of |
|
2493 | 2498 | # these are considered normal by the OS: |
|
2494 | 2499 | # > python -c'import sys;sys.exit(0)'; echo $? |
|
2495 | 2500 | # 0 |
|
2496 | 2501 | # > python -c'import sys;sys.exit()'; echo $? |
|
2497 | 2502 | # 0 |
|
2498 | 2503 | # For other exit status, we show the exception unless |
|
2499 | 2504 | # explicitly silenced, but only in short form. |
|
2500 | 2505 | if status.code: |
|
2501 | 2506 | if kw['raise_exceptions']: |
|
2502 | 2507 | raise |
|
2503 | 2508 | if not kw['exit_ignore']: |
|
2504 | 2509 | self.showtraceback(exception_only=True) |
|
2505 | 2510 | except: |
|
2506 | 2511 | if kw['raise_exceptions']: |
|
2507 | 2512 | raise |
|
2508 | 2513 | # tb offset is 2 because we wrap execfile |
|
2509 | 2514 | self.showtraceback(tb_offset=2) |
|
2510 | 2515 | |
|
2511 | 2516 | def safe_execfile_ipy(self, fname, shell_futures=False, raise_exceptions=False): |
|
2512 | 2517 | """Like safe_execfile, but for .ipy or .ipynb files with IPython syntax. |
|
2513 | 2518 | |
|
2514 | 2519 | Parameters |
|
2515 | 2520 | ---------- |
|
2516 | 2521 | fname : str |
|
2517 | 2522 | The name of the file to execute. The filename must have a |
|
2518 | 2523 | .ipy or .ipynb extension. |
|
2519 | 2524 | shell_futures : bool (False) |
|
2520 | 2525 | If True, the code will share future statements with the interactive |
|
2521 | 2526 | shell. It will both be affected by previous __future__ imports, and |
|
2522 | 2527 | any __future__ imports in the code will affect the shell. If False, |
|
2523 | 2528 | __future__ imports are not shared in either direction. |
|
2524 | 2529 | raise_exceptions : bool (False) |
|
2525 | 2530 | If True raise exceptions everywhere. Meant for testing. |
|
2526 | 2531 | """ |
|
2527 | 2532 | fname = os.path.abspath(os.path.expanduser(fname)) |
|
2528 | 2533 | |
|
2529 | 2534 | # Make sure we can open the file |
|
2530 | 2535 | try: |
|
2531 | 2536 | with open(fname): |
|
2532 | 2537 | pass |
|
2533 | 2538 | except: |
|
2534 | 2539 | warn('Could not open file <%s> for safe execution.' % fname) |
|
2535 | 2540 | return |
|
2536 | 2541 | |
|
2537 | 2542 | # Find things also in current directory. This is needed to mimic the |
|
2538 | 2543 | # behavior of running a script from the system command line, where |
|
2539 | 2544 | # Python inserts the script's directory into sys.path |
|
2540 | 2545 | dname = os.path.dirname(fname) |
|
2541 | 2546 | |
|
2542 | 2547 | def get_cells(): |
|
2543 | 2548 | """generator for sequence of code blocks to run""" |
|
2544 | 2549 | if fname.endswith('.ipynb'): |
|
2545 | 2550 | from nbformat import read |
|
2546 | 2551 | with io_open(fname) as f: |
|
2547 | 2552 | nb = read(f, as_version=4) |
|
2548 | 2553 | if not nb.cells: |
|
2549 | 2554 | return |
|
2550 | 2555 | for cell in nb.cells: |
|
2551 | 2556 | if cell.cell_type == 'code': |
|
2552 | 2557 | yield cell.source |
|
2553 | 2558 | else: |
|
2554 | 2559 | with open(fname) as f: |
|
2555 | 2560 | yield f.read() |
|
2556 | 2561 | |
|
2557 | 2562 | with prepended_to_syspath(dname): |
|
2558 | 2563 | try: |
|
2559 | 2564 | for cell in get_cells(): |
|
2560 | 2565 | result = self.run_cell(cell, silent=True, shell_futures=shell_futures) |
|
2561 | 2566 | if raise_exceptions: |
|
2562 | 2567 | result.raise_error() |
|
2563 | 2568 | elif not result.success: |
|
2564 | 2569 | break |
|
2565 | 2570 | except: |
|
2566 | 2571 | if raise_exceptions: |
|
2567 | 2572 | raise |
|
2568 | 2573 | self.showtraceback() |
|
2569 | 2574 | warn('Unknown failure executing file: <%s>' % fname) |
|
2570 | 2575 | |
|
2571 | 2576 | def safe_run_module(self, mod_name, where): |
|
2572 | 2577 | """A safe version of runpy.run_module(). |
|
2573 | 2578 | |
|
2574 | 2579 | This version will never throw an exception, but instead print |
|
2575 | 2580 | helpful error messages to the screen. |
|
2576 | 2581 | |
|
2577 | 2582 | `SystemExit` exceptions with status code 0 or None are ignored. |
|
2578 | 2583 | |
|
2579 | 2584 | Parameters |
|
2580 | 2585 | ---------- |
|
2581 | 2586 | mod_name : string |
|
2582 | 2587 | The name of the module to be executed. |
|
2583 | 2588 | where : dict |
|
2584 | 2589 | The globals namespace. |
|
2585 | 2590 | """ |
|
2586 | 2591 | try: |
|
2587 | 2592 | try: |
|
2588 | 2593 | where.update( |
|
2589 | 2594 | runpy.run_module(str(mod_name), run_name="__main__", |
|
2590 | 2595 | alter_sys=True) |
|
2591 | 2596 | ) |
|
2592 | 2597 | except SystemExit as status: |
|
2593 | 2598 | if status.code: |
|
2594 | 2599 | raise |
|
2595 | 2600 | except: |
|
2596 | 2601 | self.showtraceback() |
|
2597 | 2602 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2598 | 2603 | |
|
2599 | 2604 | def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): |
|
2600 | 2605 | """Run a complete IPython cell. |
|
2601 | 2606 | |
|
2602 | 2607 | Parameters |
|
2603 | 2608 | ---------- |
|
2604 | 2609 | raw_cell : str |
|
2605 | 2610 | The code (including IPython code such as %magic functions) to run. |
|
2606 | 2611 | store_history : bool |
|
2607 | 2612 | If True, the raw and translated cell will be stored in IPython's |
|
2608 | 2613 | history. For user code calling back into IPython's machinery, this |
|
2609 | 2614 | should be set to False. |
|
2610 | 2615 | silent : bool |
|
2611 | 2616 | If True, avoid side-effects, such as implicit displayhooks and |
|
2612 | 2617 | and logging. silent=True forces store_history=False. |
|
2613 | 2618 | shell_futures : bool |
|
2614 | 2619 | If True, the code will share future statements with the interactive |
|
2615 | 2620 | shell. It will both be affected by previous __future__ imports, and |
|
2616 | 2621 | any __future__ imports in the code will affect the shell. If False, |
|
2617 | 2622 | __future__ imports are not shared in either direction. |
|
2618 | 2623 | |
|
2619 | 2624 | Returns |
|
2620 | 2625 | ------- |
|
2621 | 2626 | result : :class:`ExecutionResult` |
|
2622 | 2627 | """ |
|
2623 | 2628 | result = ExecutionResult() |
|
2624 | 2629 | |
|
2625 | 2630 | if (not raw_cell) or raw_cell.isspace(): |
|
2626 | 2631 | return result |
|
2627 | 2632 | |
|
2628 | 2633 | if silent: |
|
2629 | 2634 | store_history = False |
|
2630 | 2635 | |
|
2631 | 2636 | if store_history: |
|
2632 | 2637 | result.execution_count = self.execution_count |
|
2633 | 2638 | |
|
2634 | 2639 | def error_before_exec(value): |
|
2635 | 2640 | result.error_before_exec = value |
|
2636 | 2641 | return result |
|
2637 | 2642 | |
|
2638 | 2643 | self.events.trigger('pre_execute') |
|
2639 | 2644 | if not silent: |
|
2640 | 2645 | self.events.trigger('pre_run_cell') |
|
2641 | 2646 | |
|
2642 | 2647 | # If any of our input transformation (input_transformer_manager or |
|
2643 | 2648 | # prefilter_manager) raises an exception, we store it in this variable |
|
2644 | 2649 | # so that we can display the error after logging the input and storing |
|
2645 | 2650 | # it in the history. |
|
2646 | 2651 | preprocessing_exc_tuple = None |
|
2647 | 2652 | try: |
|
2648 | 2653 | # Static input transformations |
|
2649 | 2654 | cell = self.input_transformer_manager.transform_cell(raw_cell) |
|
2650 | 2655 | except SyntaxError: |
|
2651 | 2656 | preprocessing_exc_tuple = sys.exc_info() |
|
2652 | 2657 | cell = raw_cell # cell has to exist so it can be stored/logged |
|
2653 | 2658 | else: |
|
2654 | 2659 | if len(cell.splitlines()) == 1: |
|
2655 | 2660 | # Dynamic transformations - only applied for single line commands |
|
2656 | 2661 | with self.builtin_trap: |
|
2657 | 2662 | try: |
|
2658 | 2663 | # use prefilter_lines to handle trailing newlines |
|
2659 | 2664 | # restore trailing newline for ast.parse |
|
2660 | 2665 | cell = self.prefilter_manager.prefilter_lines(cell) + '\n' |
|
2661 | 2666 | except Exception: |
|
2662 | 2667 | # don't allow prefilter errors to crash IPython |
|
2663 | 2668 | preprocessing_exc_tuple = sys.exc_info() |
|
2664 | 2669 | |
|
2665 | 2670 | # Store raw and processed history |
|
2666 | 2671 | if store_history: |
|
2667 | 2672 | self.history_manager.store_inputs(self.execution_count, |
|
2668 | 2673 | cell, raw_cell) |
|
2669 | 2674 | if not silent: |
|
2670 | 2675 | self.logger.log(cell, raw_cell) |
|
2671 | 2676 | |
|
2672 | 2677 | # Display the exception if input processing failed. |
|
2673 | 2678 | if preprocessing_exc_tuple is not None: |
|
2674 | 2679 | self.showtraceback(preprocessing_exc_tuple) |
|
2675 | 2680 | if store_history: |
|
2676 | 2681 | self.execution_count += 1 |
|
2677 | 2682 | return error_before_exec(preprocessing_exc_tuple[2]) |
|
2678 | 2683 | |
|
2679 | 2684 | # Our own compiler remembers the __future__ environment. If we want to |
|
2680 | 2685 | # run code with a separate __future__ environment, use the default |
|
2681 | 2686 | # compiler |
|
2682 | 2687 | compiler = self.compile if shell_futures else CachingCompiler() |
|
2683 | 2688 | |
|
2684 | 2689 | with self.builtin_trap: |
|
2685 | 2690 | cell_name = self.compile.cache(cell, self.execution_count) |
|
2686 | 2691 | |
|
2687 | 2692 | with self.display_trap: |
|
2688 | 2693 | # Compile to bytecode |
|
2689 | 2694 | try: |
|
2690 | 2695 | code_ast = compiler.ast_parse(cell, filename=cell_name) |
|
2691 | 2696 | except IndentationError as e: |
|
2692 | 2697 | self.showindentationerror() |
|
2693 | 2698 | if store_history: |
|
2694 | 2699 | self.execution_count += 1 |
|
2695 | 2700 | return error_before_exec(e) |
|
2696 | 2701 | except (OverflowError, SyntaxError, ValueError, TypeError, |
|
2697 | 2702 | MemoryError) as e: |
|
2698 | 2703 | self.showsyntaxerror() |
|
2699 | 2704 | if store_history: |
|
2700 | 2705 | self.execution_count += 1 |
|
2701 | 2706 | return error_before_exec(e) |
|
2702 | 2707 | |
|
2703 | 2708 | # Apply AST transformations |
|
2704 | 2709 | try: |
|
2705 | 2710 | code_ast = self.transform_ast(code_ast) |
|
2706 | 2711 | except InputRejected as e: |
|
2707 | 2712 | self.showtraceback() |
|
2708 | 2713 | if store_history: |
|
2709 | 2714 | self.execution_count += 1 |
|
2710 | 2715 | return error_before_exec(e) |
|
2711 | 2716 | |
|
2712 | 2717 | # Give the displayhook a reference to our ExecutionResult so it |
|
2713 | 2718 | # can fill in the output value. |
|
2714 | 2719 | self.displayhook.exec_result = result |
|
2715 | 2720 | |
|
2716 | 2721 | # Execute the user code |
|
2717 | 2722 | interactivity = "none" if silent else self.ast_node_interactivity |
|
2718 | 2723 | self.run_ast_nodes(code_ast.body, cell_name, |
|
2719 | 2724 | interactivity=interactivity, compiler=compiler, result=result) |
|
2720 | 2725 | |
|
2721 | 2726 | # Reset this so later displayed values do not modify the |
|
2722 | 2727 | # ExecutionResult |
|
2723 | 2728 | self.displayhook.exec_result = None |
|
2724 | 2729 | |
|
2725 | 2730 | self.events.trigger('post_execute') |
|
2726 | 2731 | if not silent: |
|
2727 | 2732 | self.events.trigger('post_run_cell') |
|
2728 | 2733 | |
|
2729 | 2734 | if store_history: |
|
2730 | 2735 | # Write output to the database. Does nothing unless |
|
2731 | 2736 | # history output logging is enabled. |
|
2732 | 2737 | self.history_manager.store_output(self.execution_count) |
|
2733 | 2738 | # Each cell is a *single* input, regardless of how many lines it has |
|
2734 | 2739 | self.execution_count += 1 |
|
2735 | 2740 | |
|
2736 | 2741 | return result |
|
2737 | 2742 | |
|
2738 | 2743 | def transform_ast(self, node): |
|
2739 | 2744 | """Apply the AST transformations from self.ast_transformers |
|
2740 | 2745 | |
|
2741 | 2746 | Parameters |
|
2742 | 2747 | ---------- |
|
2743 | 2748 | node : ast.Node |
|
2744 | 2749 | The root node to be transformed. Typically called with the ast.Module |
|
2745 | 2750 | produced by parsing user input. |
|
2746 | 2751 | |
|
2747 | 2752 | Returns |
|
2748 | 2753 | ------- |
|
2749 | 2754 | An ast.Node corresponding to the node it was called with. Note that it |
|
2750 | 2755 | may also modify the passed object, so don't rely on references to the |
|
2751 | 2756 | original AST. |
|
2752 | 2757 | """ |
|
2753 | 2758 | for transformer in self.ast_transformers: |
|
2754 | 2759 | try: |
|
2755 | 2760 | node = transformer.visit(node) |
|
2756 | 2761 | except InputRejected: |
|
2757 | 2762 | # User-supplied AST transformers can reject an input by raising |
|
2758 | 2763 | # an InputRejected. Short-circuit in this case so that we |
|
2759 | 2764 | # don't unregister the transform. |
|
2760 | 2765 | raise |
|
2761 | 2766 | except Exception: |
|
2762 | 2767 | warn("AST transformer %r threw an error. It will be unregistered." % transformer) |
|
2763 | 2768 | self.ast_transformers.remove(transformer) |
|
2764 | 2769 | |
|
2765 | 2770 | if self.ast_transformers: |
|
2766 | 2771 | ast.fix_missing_locations(node) |
|
2767 | 2772 | return node |
|
2768 | 2773 | |
|
2769 | 2774 | |
|
2770 | 2775 | def run_ast_nodes(self, nodelist, cell_name, interactivity='last_expr', |
|
2771 | 2776 | compiler=compile, result=None): |
|
2772 | 2777 | """Run a sequence of AST nodes. The execution mode depends on the |
|
2773 | 2778 | interactivity parameter. |
|
2774 | 2779 | |
|
2775 | 2780 | Parameters |
|
2776 | 2781 | ---------- |
|
2777 | 2782 | nodelist : list |
|
2778 | 2783 | A sequence of AST nodes to run. |
|
2779 | 2784 | cell_name : str |
|
2780 | 2785 | Will be passed to the compiler as the filename of the cell. Typically |
|
2781 | 2786 | the value returned by ip.compile.cache(cell). |
|
2782 | 2787 | interactivity : str |
|
2783 | 2788 | 'all', 'last', 'last_expr' or 'none', specifying which nodes should be |
|
2784 | 2789 | run interactively (displaying output from expressions). 'last_expr' |
|
2785 | 2790 | will run the last node interactively only if it is an expression (i.e. |
|
2786 | 2791 | expressions in loops or other blocks are not displayed. Other values |
|
2787 | 2792 | for this parameter will raise a ValueError. |
|
2788 | 2793 | compiler : callable |
|
2789 | 2794 | A function with the same interface as the built-in compile(), to turn |
|
2790 | 2795 | the AST nodes into code objects. Default is the built-in compile(). |
|
2791 | 2796 | result : ExecutionResult, optional |
|
2792 | 2797 | An object to store exceptions that occur during execution. |
|
2793 | 2798 | |
|
2794 | 2799 | Returns |
|
2795 | 2800 | ------- |
|
2796 | 2801 | True if an exception occurred while running code, False if it finished |
|
2797 | 2802 | running. |
|
2798 | 2803 | """ |
|
2799 | 2804 | if not nodelist: |
|
2800 | 2805 | return |
|
2801 | 2806 | |
|
2802 | 2807 | if interactivity == 'last_expr': |
|
2803 | 2808 | if isinstance(nodelist[-1], ast.Expr): |
|
2804 | 2809 | interactivity = "last" |
|
2805 | 2810 | else: |
|
2806 | 2811 | interactivity = "none" |
|
2807 | 2812 | |
|
2808 | 2813 | if interactivity == 'none': |
|
2809 | 2814 | to_run_exec, to_run_interactive = nodelist, [] |
|
2810 | 2815 | elif interactivity == 'last': |
|
2811 | 2816 | to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:] |
|
2812 | 2817 | elif interactivity == 'all': |
|
2813 | 2818 | to_run_exec, to_run_interactive = [], nodelist |
|
2814 | 2819 | else: |
|
2815 | 2820 | raise ValueError("Interactivity was %r" % interactivity) |
|
2816 | 2821 | |
|
2817 | 2822 | try: |
|
2818 | 2823 | for i, node in enumerate(to_run_exec): |
|
2819 | 2824 | mod = ast.Module([node]) |
|
2820 | 2825 | code = compiler(mod, cell_name, "exec") |
|
2821 | 2826 | if self.run_code(code, result): |
|
2822 | 2827 | return True |
|
2823 | 2828 | |
|
2824 | 2829 | for i, node in enumerate(to_run_interactive): |
|
2825 | 2830 | mod = ast.Interactive([node]) |
|
2826 | 2831 | code = compiler(mod, cell_name, "single") |
|
2827 | 2832 | if self.run_code(code, result): |
|
2828 | 2833 | return True |
|
2829 | 2834 | |
|
2830 | 2835 | # Flush softspace |
|
2831 | 2836 | if softspace(sys.stdout, 0): |
|
2832 | 2837 | print() |
|
2833 | 2838 | |
|
2834 | 2839 | except: |
|
2835 | 2840 | # It's possible to have exceptions raised here, typically by |
|
2836 | 2841 | # compilation of odd code (such as a naked 'return' outside a |
|
2837 | 2842 | # function) that did parse but isn't valid. Typically the exception |
|
2838 | 2843 | # is a SyntaxError, but it's safest just to catch anything and show |
|
2839 | 2844 | # the user a traceback. |
|
2840 | 2845 | |
|
2841 | 2846 | # We do only one try/except outside the loop to minimize the impact |
|
2842 | 2847 | # on runtime, and also because if any node in the node list is |
|
2843 | 2848 | # broken, we should stop execution completely. |
|
2844 | 2849 | if result: |
|
2845 | 2850 | result.error_before_exec = sys.exc_info()[1] |
|
2846 | 2851 | self.showtraceback() |
|
2847 | 2852 | return True |
|
2848 | 2853 | |
|
2849 | 2854 | return False |
|
2850 | 2855 | |
|
2851 | 2856 | def run_code(self, code_obj, result=None): |
|
2852 | 2857 | """Execute a code object. |
|
2853 | 2858 | |
|
2854 | 2859 | When an exception occurs, self.showtraceback() is called to display a |
|
2855 | 2860 | traceback. |
|
2856 | 2861 | |
|
2857 | 2862 | Parameters |
|
2858 | 2863 | ---------- |
|
2859 | 2864 | code_obj : code object |
|
2860 | 2865 | A compiled code object, to be executed |
|
2861 | 2866 | result : ExecutionResult, optional |
|
2862 | 2867 | An object to store exceptions that occur during execution. |
|
2863 | 2868 | |
|
2864 | 2869 | Returns |
|
2865 | 2870 | ------- |
|
2866 | 2871 | False : successful execution. |
|
2867 | 2872 | True : an error occurred. |
|
2868 | 2873 | """ |
|
2869 | 2874 | # Set our own excepthook in case the user code tries to call it |
|
2870 | 2875 | # directly, so that the IPython crash handler doesn't get triggered |
|
2871 | 2876 | old_excepthook, sys.excepthook = sys.excepthook, self.excepthook |
|
2872 | 2877 | |
|
2873 | 2878 | # we save the original sys.excepthook in the instance, in case config |
|
2874 | 2879 | # code (such as magics) needs access to it. |
|
2875 | 2880 | self.sys_excepthook = old_excepthook |
|
2876 | 2881 | outflag = 1 # happens in more places, so it's easier as default |
|
2877 | 2882 | try: |
|
2878 | 2883 | try: |
|
2879 | 2884 | self.hooks.pre_run_code_hook() |
|
2880 | 2885 | #rprint('Running code', repr(code_obj)) # dbg |
|
2881 | 2886 | exec(code_obj, self.user_global_ns, self.user_ns) |
|
2882 | 2887 | finally: |
|
2883 | 2888 | # Reset our crash handler in place |
|
2884 | 2889 | sys.excepthook = old_excepthook |
|
2885 | 2890 | except SystemExit as e: |
|
2886 | 2891 | if result is not None: |
|
2887 | 2892 | result.error_in_exec = e |
|
2888 | 2893 | self.showtraceback(exception_only=True) |
|
2889 | 2894 | warn("To exit: use 'exit', 'quit', or Ctrl-D.", level=1) |
|
2890 | 2895 | except self.custom_exceptions: |
|
2891 | 2896 | etype, value, tb = sys.exc_info() |
|
2892 | 2897 | if result is not None: |
|
2893 | 2898 | result.error_in_exec = value |
|
2894 | 2899 | self.CustomTB(etype, value, tb) |
|
2895 | 2900 | except: |
|
2896 | 2901 | if result is not None: |
|
2897 | 2902 | result.error_in_exec = sys.exc_info()[1] |
|
2898 | 2903 | self.showtraceback() |
|
2899 | 2904 | else: |
|
2900 | 2905 | outflag = 0 |
|
2901 | 2906 | return outflag |
|
2902 | 2907 | |
|
2903 | 2908 | # For backwards compatibility |
|
2904 | 2909 | runcode = run_code |
|
2905 | 2910 | |
|
2906 | 2911 | #------------------------------------------------------------------------- |
|
2907 | 2912 | # Things related to GUI support and pylab |
|
2908 | 2913 | #------------------------------------------------------------------------- |
|
2909 | 2914 | |
|
2910 | 2915 | def enable_gui(self, gui=None): |
|
2911 | 2916 | raise NotImplementedError('Implement enable_gui in a subclass') |
|
2912 | 2917 | |
|
2913 | 2918 | def enable_matplotlib(self, gui=None): |
|
2914 | 2919 | """Enable interactive matplotlib and inline figure support. |
|
2915 | 2920 | |
|
2916 | 2921 | This takes the following steps: |
|
2917 | 2922 | |
|
2918 | 2923 | 1. select the appropriate eventloop and matplotlib backend |
|
2919 | 2924 | 2. set up matplotlib for interactive use with that backend |
|
2920 | 2925 | 3. configure formatters for inline figure display |
|
2921 | 2926 | 4. enable the selected gui eventloop |
|
2922 | 2927 | |
|
2923 | 2928 | Parameters |
|
2924 | 2929 | ---------- |
|
2925 | 2930 | gui : optional, string |
|
2926 | 2931 | If given, dictates the choice of matplotlib GUI backend to use |
|
2927 | 2932 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2928 | 2933 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2929 | 2934 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2930 | 2935 | user's matplotlibrc configuration file). Note that not all backends |
|
2931 | 2936 | make sense in all contexts, for example a terminal ipython can't |
|
2932 | 2937 | display figures inline. |
|
2933 | 2938 | """ |
|
2934 | 2939 | from IPython.core import pylabtools as pt |
|
2935 | 2940 | gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select) |
|
2936 | 2941 | |
|
2937 | 2942 | if gui != 'inline': |
|
2938 | 2943 | # If we have our first gui selection, store it |
|
2939 | 2944 | if self.pylab_gui_select is None: |
|
2940 | 2945 | self.pylab_gui_select = gui |
|
2941 | 2946 | # Otherwise if they are different |
|
2942 | 2947 | elif gui != self.pylab_gui_select: |
|
2943 | 2948 | print ('Warning: Cannot change to a different GUI toolkit: %s.' |
|
2944 | 2949 | ' Using %s instead.' % (gui, self.pylab_gui_select)) |
|
2945 | 2950 | gui, backend = pt.find_gui_and_backend(self.pylab_gui_select) |
|
2946 | 2951 | |
|
2947 | 2952 | pt.activate_matplotlib(backend) |
|
2948 | 2953 | pt.configure_inline_support(self, backend) |
|
2949 | 2954 | |
|
2950 | 2955 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2951 | 2956 | # plot updates into account |
|
2952 | 2957 | self.enable_gui(gui) |
|
2953 | 2958 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ |
|
2954 | 2959 | pt.mpl_runner(self.safe_execfile) |
|
2955 | 2960 | |
|
2956 | 2961 | return gui, backend |
|
2957 | 2962 | |
|
2958 | 2963 | def enable_pylab(self, gui=None, import_all=True, welcome_message=False): |
|
2959 | 2964 | """Activate pylab support at runtime. |
|
2960 | 2965 | |
|
2961 | 2966 | This turns on support for matplotlib, preloads into the interactive |
|
2962 | 2967 | namespace all of numpy and pylab, and configures IPython to correctly |
|
2963 | 2968 | interact with the GUI event loop. The GUI backend to be used can be |
|
2964 | 2969 | optionally selected with the optional ``gui`` argument. |
|
2965 | 2970 | |
|
2966 | 2971 | This method only adds preloading the namespace to InteractiveShell.enable_matplotlib. |
|
2967 | 2972 | |
|
2968 | 2973 | Parameters |
|
2969 | 2974 | ---------- |
|
2970 | 2975 | gui : optional, string |
|
2971 | 2976 | If given, dictates the choice of matplotlib GUI backend to use |
|
2972 | 2977 | (should be one of IPython's supported backends, 'qt', 'osx', 'tk', |
|
2973 | 2978 | 'gtk', 'wx' or 'inline'), otherwise we use the default chosen by |
|
2974 | 2979 | matplotlib (as dictated by the matplotlib build-time options plus the |
|
2975 | 2980 | user's matplotlibrc configuration file). Note that not all backends |
|
2976 | 2981 | make sense in all contexts, for example a terminal ipython can't |
|
2977 | 2982 | display figures inline. |
|
2978 | 2983 | import_all : optional, bool, default: True |
|
2979 | 2984 | Whether to do `from numpy import *` and `from pylab import *` |
|
2980 | 2985 | in addition to module imports. |
|
2981 | 2986 | welcome_message : deprecated |
|
2982 | 2987 | This argument is ignored, no welcome message will be displayed. |
|
2983 | 2988 | """ |
|
2984 | 2989 | from IPython.core.pylabtools import import_pylab |
|
2985 | 2990 | |
|
2986 | 2991 | gui, backend = self.enable_matplotlib(gui) |
|
2987 | 2992 | |
|
2988 | 2993 | # We want to prevent the loading of pylab to pollute the user's |
|
2989 | 2994 | # namespace as shown by the %who* magics, so we execute the activation |
|
2990 | 2995 | # code in an empty namespace, and we update *both* user_ns and |
|
2991 | 2996 | # user_ns_hidden with this information. |
|
2992 | 2997 | ns = {} |
|
2993 | 2998 | import_pylab(ns, import_all) |
|
2994 | 2999 | # warn about clobbered names |
|
2995 | 3000 | ignored = {"__builtins__"} |
|
2996 | 3001 | both = set(ns).intersection(self.user_ns).difference(ignored) |
|
2997 | 3002 | clobbered = [ name for name in both if self.user_ns[name] is not ns[name] ] |
|
2998 | 3003 | self.user_ns.update(ns) |
|
2999 | 3004 | self.user_ns_hidden.update(ns) |
|
3000 | 3005 | return gui, backend, clobbered |
|
3001 | 3006 | |
|
3002 | 3007 | #------------------------------------------------------------------------- |
|
3003 | 3008 | # Utilities |
|
3004 | 3009 | #------------------------------------------------------------------------- |
|
3005 | 3010 | |
|
3006 | 3011 | def var_expand(self, cmd, depth=0, formatter=DollarFormatter()): |
|
3007 | 3012 | """Expand python variables in a string. |
|
3008 | 3013 | |
|
3009 | 3014 | The depth argument indicates how many frames above the caller should |
|
3010 | 3015 | be walked to look for the local namespace where to expand variables. |
|
3011 | 3016 | |
|
3012 | 3017 | The global namespace for expansion is always the user's interactive |
|
3013 | 3018 | namespace. |
|
3014 | 3019 | """ |
|
3015 | 3020 | ns = self.user_ns.copy() |
|
3016 | 3021 | try: |
|
3017 | 3022 | frame = sys._getframe(depth+1) |
|
3018 | 3023 | except ValueError: |
|
3019 | 3024 | # This is thrown if there aren't that many frames on the stack, |
|
3020 | 3025 | # e.g. if a script called run_line_magic() directly. |
|
3021 | 3026 | pass |
|
3022 | 3027 | else: |
|
3023 | 3028 | ns.update(frame.f_locals) |
|
3024 | 3029 | |
|
3025 | 3030 | try: |
|
3026 | 3031 | # We have to use .vformat() here, because 'self' is a valid and common |
|
3027 | 3032 | # name, and expanding **ns for .format() would make it collide with |
|
3028 | 3033 | # the 'self' argument of the method. |
|
3029 | 3034 | cmd = formatter.vformat(cmd, args=[], kwargs=ns) |
|
3030 | 3035 | except Exception: |
|
3031 | 3036 | # if formatter couldn't format, just let it go untransformed |
|
3032 | 3037 | pass |
|
3033 | 3038 | return cmd |
|
3034 | 3039 | |
|
3035 | 3040 | def mktempfile(self, data=None, prefix='ipython_edit_'): |
|
3036 | 3041 | """Make a new tempfile and return its filename. |
|
3037 | 3042 | |
|
3038 | 3043 | This makes a call to tempfile.mkstemp (created in a tempfile.mkdtemp), |
|
3039 | 3044 | but it registers the created filename internally so ipython cleans it up |
|
3040 | 3045 | at exit time. |
|
3041 | 3046 | |
|
3042 | 3047 | Optional inputs: |
|
3043 | 3048 | |
|
3044 | 3049 | - data(None): if data is given, it gets written out to the temp file |
|
3045 | 3050 | immediately, and the file is closed again.""" |
|
3046 | 3051 | |
|
3047 | 3052 | dirname = tempfile.mkdtemp(prefix=prefix) |
|
3048 | 3053 | self.tempdirs.append(dirname) |
|
3049 | 3054 | |
|
3050 | 3055 | handle, filename = tempfile.mkstemp('.py', prefix, dir=dirname) |
|
3051 | 3056 | os.close(handle) # On Windows, there can only be one open handle on a file |
|
3052 | 3057 | self.tempfiles.append(filename) |
|
3053 | 3058 | |
|
3054 | 3059 | if data: |
|
3055 | 3060 | tmp_file = open(filename,'w') |
|
3056 | 3061 | tmp_file.write(data) |
|
3057 | 3062 | tmp_file.close() |
|
3058 | 3063 | return filename |
|
3059 | 3064 | |
|
3060 | 3065 | @undoc |
|
3061 | 3066 | def write(self,data): |
|
3062 | 3067 | """DEPRECATED: Write a string to the default output""" |
|
3063 | 3068 | warn('InteractiveShell.write() is deprecated, use sys.stdout instead', |
|
3064 | 3069 | DeprecationWarning, stacklevel=2) |
|
3065 | 3070 | sys.stdout.write(data) |
|
3066 | 3071 | |
|
3067 | 3072 | @undoc |
|
3068 | 3073 | def write_err(self,data): |
|
3069 | 3074 | """DEPRECATED: Write a string to the default error output""" |
|
3070 | 3075 | warn('InteractiveShell.write_err() is deprecated, use sys.stderr instead', |
|
3071 | 3076 | DeprecationWarning, stacklevel=2) |
|
3072 | 3077 | sys.stderr.write(data) |
|
3073 | 3078 | |
|
3074 | 3079 | def ask_yes_no(self, prompt, default=None, interrupt=None): |
|
3075 | 3080 | if self.quiet: |
|
3076 | 3081 | return True |
|
3077 | 3082 | return ask_yes_no(prompt,default,interrupt) |
|
3078 | 3083 | |
|
3079 | 3084 | def show_usage(self): |
|
3080 | 3085 | """Show a usage message""" |
|
3081 | 3086 | page.page(IPython.core.usage.interactive_usage) |
|
3082 | 3087 | |
|
3083 | 3088 | def extract_input_lines(self, range_str, raw=False): |
|
3084 | 3089 | """Return as a string a set of input history slices. |
|
3085 | 3090 | |
|
3086 | 3091 | Parameters |
|
3087 | 3092 | ---------- |
|
3088 | 3093 | range_str : string |
|
3089 | 3094 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
3090 | 3095 | since this function is for use by magic functions which get their |
|
3091 | 3096 | arguments as strings. The number before the / is the session |
|
3092 | 3097 | number: ~n goes n back from the current session. |
|
3093 | 3098 | |
|
3094 | 3099 | raw : bool, optional |
|
3095 | 3100 | By default, the processed input is used. If this is true, the raw |
|
3096 | 3101 | input history is used instead. |
|
3097 | 3102 | |
|
3098 | 3103 | Notes |
|
3099 | 3104 | ----- |
|
3100 | 3105 | |
|
3101 | 3106 | Slices can be described with two notations: |
|
3102 | 3107 | |
|
3103 | 3108 | * ``N:M`` -> standard python form, means including items N...(M-1). |
|
3104 | 3109 | * ``N-M`` -> include items N..M (closed endpoint). |
|
3105 | 3110 | """ |
|
3106 | 3111 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3107 | 3112 | return "\n".join(x for _, _, x in lines) |
|
3108 | 3113 | |
|
3109 | 3114 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): |
|
3110 | 3115 | """Get a code string from history, file, url, or a string or macro. |
|
3111 | 3116 | |
|
3112 | 3117 | This is mainly used by magic functions. |
|
3113 | 3118 | |
|
3114 | 3119 | Parameters |
|
3115 | 3120 | ---------- |
|
3116 | 3121 | |
|
3117 | 3122 | target : str |
|
3118 | 3123 | |
|
3119 | 3124 | A string specifying code to retrieve. This will be tried respectively |
|
3120 | 3125 | as: ranges of input history (see %history for syntax), url, |
|
3121 | 3126 | corresponding .py file, filename, or an expression evaluating to a |
|
3122 | 3127 | string or Macro in the user namespace. |
|
3123 | 3128 | |
|
3124 | 3129 | raw : bool |
|
3125 | 3130 | If true (default), retrieve raw history. Has no effect on the other |
|
3126 | 3131 | retrieval mechanisms. |
|
3127 | 3132 | |
|
3128 | 3133 | py_only : bool (default False) |
|
3129 | 3134 | Only try to fetch python code, do not try alternative methods to decode file |
|
3130 | 3135 | if unicode fails. |
|
3131 | 3136 | |
|
3132 | 3137 | Returns |
|
3133 | 3138 | ------- |
|
3134 | 3139 | A string of code. |
|
3135 | 3140 | |
|
3136 | 3141 | ValueError is raised if nothing is found, and TypeError if it evaluates |
|
3137 | 3142 | to an object of another type. In each case, .args[0] is a printable |
|
3138 | 3143 | message. |
|
3139 | 3144 | """ |
|
3140 | 3145 | code = self.extract_input_lines(target, raw=raw) # Grab history |
|
3141 | 3146 | if code: |
|
3142 | 3147 | return code |
|
3143 | 3148 | utarget = unquote_filename(target) |
|
3144 | 3149 | try: |
|
3145 | 3150 | if utarget.startswith(('http://', 'https://')): |
|
3146 | 3151 | return openpy.read_py_url(utarget, skip_encoding_cookie=skip_encoding_cookie) |
|
3147 | 3152 | except UnicodeDecodeError: |
|
3148 | 3153 | if not py_only : |
|
3149 | 3154 | # Deferred import |
|
3150 | 3155 | try: |
|
3151 | 3156 | from urllib.request import urlopen # Py3 |
|
3152 | 3157 | except ImportError: |
|
3153 | 3158 | from urllib import urlopen |
|
3154 | 3159 | response = urlopen(target) |
|
3155 | 3160 | return response.read().decode('latin1') |
|
3156 | 3161 | raise ValueError(("'%s' seem to be unreadable.") % utarget) |
|
3157 | 3162 | |
|
3158 | 3163 | potential_target = [target] |
|
3159 | 3164 | try : |
|
3160 | 3165 | potential_target.insert(0,get_py_filename(target)) |
|
3161 | 3166 | except IOError: |
|
3162 | 3167 | pass |
|
3163 | 3168 | |
|
3164 | 3169 | for tgt in potential_target : |
|
3165 | 3170 | if os.path.isfile(tgt): # Read file |
|
3166 | 3171 | try : |
|
3167 | 3172 | return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) |
|
3168 | 3173 | except UnicodeDecodeError : |
|
3169 | 3174 | if not py_only : |
|
3170 | 3175 | with io_open(tgt,'r', encoding='latin1') as f : |
|
3171 | 3176 | return f.read() |
|
3172 | 3177 | raise ValueError(("'%s' seem to be unreadable.") % target) |
|
3173 | 3178 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3174 | 3179 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3175 | 3180 | |
|
3176 | 3181 | if search_ns: |
|
3177 | 3182 | # Inspect namespace to load object source |
|
3178 | 3183 | object_info = self.object_inspect(target, detail_level=1) |
|
3179 | 3184 | if object_info['found'] and object_info['source']: |
|
3180 | 3185 | return object_info['source'] |
|
3181 | 3186 | |
|
3182 | 3187 | try: # User namespace |
|
3183 | 3188 | codeobj = eval(target, self.user_ns) |
|
3184 | 3189 | except Exception: |
|
3185 | 3190 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3186 | 3191 | "nor in the user namespace.") % target) |
|
3187 | 3192 | |
|
3188 | 3193 | if isinstance(codeobj, string_types): |
|
3189 | 3194 | return codeobj |
|
3190 | 3195 | elif isinstance(codeobj, Macro): |
|
3191 | 3196 | return codeobj.value |
|
3192 | 3197 | |
|
3193 | 3198 | raise TypeError("%s is neither a string nor a macro." % target, |
|
3194 | 3199 | codeobj) |
|
3195 | 3200 | |
|
3196 | 3201 | #------------------------------------------------------------------------- |
|
3197 | 3202 | # Things related to IPython exiting |
|
3198 | 3203 | #------------------------------------------------------------------------- |
|
3199 | 3204 | def atexit_operations(self): |
|
3200 | 3205 | """This will be executed at the time of exit. |
|
3201 | 3206 | |
|
3202 | 3207 | Cleanup operations and saving of persistent data that is done |
|
3203 | 3208 | unconditionally by IPython should be performed here. |
|
3204 | 3209 | |
|
3205 | 3210 | For things that may depend on startup flags or platform specifics (such |
|
3206 | 3211 | as having readline or not), register a separate atexit function in the |
|
3207 | 3212 | code that has the appropriate information, rather than trying to |
|
3208 | 3213 | clutter |
|
3209 | 3214 | """ |
|
3210 | 3215 | # Close the history session (this stores the end time and line count) |
|
3211 | 3216 | # this must be *before* the tempfile cleanup, in case of temporary |
|
3212 | 3217 | # history db |
|
3213 | 3218 | self.history_manager.end_session() |
|
3214 | 3219 | |
|
3215 | 3220 | # Cleanup all tempfiles and folders left around |
|
3216 | 3221 | for tfile in self.tempfiles: |
|
3217 | 3222 | try: |
|
3218 | 3223 | os.unlink(tfile) |
|
3219 | 3224 | except OSError: |
|
3220 | 3225 | pass |
|
3221 | 3226 | |
|
3222 | 3227 | for tdir in self.tempdirs: |
|
3223 | 3228 | try: |
|
3224 | 3229 | os.rmdir(tdir) |
|
3225 | 3230 | except OSError: |
|
3226 | 3231 | pass |
|
3227 | 3232 | |
|
3228 | 3233 | # Clear all user namespaces to release all references cleanly. |
|
3229 | 3234 | self.reset(new_session=False) |
|
3230 | 3235 | |
|
3231 | 3236 | # Run user hooks |
|
3232 | 3237 | self.hooks.shutdown_hook() |
|
3233 | 3238 | |
|
3234 | 3239 | def cleanup(self): |
|
3235 | 3240 | self.restore_sys_module_state() |
|
3236 | 3241 | |
|
3237 | 3242 | |
|
3238 | 3243 | class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)): |
|
3239 | 3244 | """An abstract base class for InteractiveShell.""" |
|
3240 | 3245 | |
|
3241 | 3246 | InteractiveShellABC.register(InteractiveShell) |
@@ -1,703 +1,699 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | """ |
|
4 | 4 | from __future__ import print_function |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
8 | 8 | # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu> |
|
9 | 9 | # Copyright (C) 2008 The IPython Development Team |
|
10 | 10 | |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
18 | # Stdlib | |
|
19 | 15 | import os |
|
20 | 16 | import re |
|
21 | 17 | import sys |
|
22 | 18 | import types |
|
23 | 19 | from getopt import getopt, GetoptError |
|
24 | 20 | |
|
25 | # Our own | |
|
26 | 21 | from traitlets.config.configurable import Configurable |
|
27 | 22 | from IPython.core import oinspect |
|
28 | 23 | from IPython.core.error import UsageError |
|
29 | 24 | from IPython.core.inputsplitter import ESC_MAGIC, ESC_MAGIC2 |
|
30 | 25 | from decorator import decorator |
|
31 | 26 | from IPython.utils.ipstruct import Struct |
|
32 | 27 | from IPython.utils.process import arg_split |
|
33 | 28 | from IPython.utils.py3compat import string_types, iteritems |
|
34 | 29 | from IPython.utils.text import dedent |
|
35 | from traitlets import Bool, Dict, Instance | |
|
30 | from traitlets import Bool, Dict, Instance, observe | |
|
36 | 31 | from logging import error |
|
37 | 32 | |
|
38 | 33 | #----------------------------------------------------------------------------- |
|
39 | 34 | # Globals |
|
40 | 35 | #----------------------------------------------------------------------------- |
|
41 | 36 | |
|
42 | 37 | # A dict we'll use for each class that has magics, used as temporary storage to |
|
43 | 38 | # pass information between the @line/cell_magic method decorators and the |
|
44 | 39 | # @magics_class class decorator, because the method decorators have no |
|
45 | 40 | # access to the class when they run. See for more details: |
|
46 | 41 | # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class |
|
47 | 42 | |
|
48 | 43 | magics = dict(line={}, cell={}) |
|
49 | 44 | |
|
50 | 45 | magic_kinds = ('line', 'cell') |
|
51 | 46 | magic_spec = ('line', 'cell', 'line_cell') |
|
52 | 47 | magic_escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC2) |
|
53 | 48 | |
|
54 | 49 | #----------------------------------------------------------------------------- |
|
55 | 50 | # Utility classes and functions |
|
56 | 51 | #----------------------------------------------------------------------------- |
|
57 | 52 | |
|
58 | 53 | class Bunch: pass |
|
59 | 54 | |
|
60 | 55 | |
|
61 | 56 | def on_off(tag): |
|
62 | 57 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
63 | 58 | return ['OFF','ON'][tag] |
|
64 | 59 | |
|
65 | 60 | |
|
66 | 61 | def compress_dhist(dh): |
|
67 | 62 | """Compress a directory history into a new one with at most 20 entries. |
|
68 | 63 | |
|
69 | 64 | Return a new list made from the first and last 10 elements of dhist after |
|
70 | 65 | removal of duplicates. |
|
71 | 66 | """ |
|
72 | 67 | head, tail = dh[:-10], dh[-10:] |
|
73 | 68 | |
|
74 | 69 | newhead = [] |
|
75 | 70 | done = set() |
|
76 | 71 | for h in head: |
|
77 | 72 | if h in done: |
|
78 | 73 | continue |
|
79 | 74 | newhead.append(h) |
|
80 | 75 | done.add(h) |
|
81 | 76 | |
|
82 | 77 | return newhead + tail |
|
83 | 78 | |
|
84 | 79 | |
|
85 | 80 | def needs_local_scope(func): |
|
86 | 81 | """Decorator to mark magic functions which need to local scope to run.""" |
|
87 | 82 | func.needs_local_scope = True |
|
88 | 83 | return func |
|
89 | 84 | |
|
90 | 85 | #----------------------------------------------------------------------------- |
|
91 | 86 | # Class and method decorators for registering magics |
|
92 | 87 | #----------------------------------------------------------------------------- |
|
93 | 88 | |
|
94 | 89 | def magics_class(cls): |
|
95 | 90 | """Class decorator for all subclasses of the main Magics class. |
|
96 | 91 | |
|
97 | 92 | Any class that subclasses Magics *must* also apply this decorator, to |
|
98 | 93 | ensure that all the methods that have been decorated as line/cell magics |
|
99 | 94 | get correctly registered in the class instance. This is necessary because |
|
100 | 95 | when method decorators run, the class does not exist yet, so they |
|
101 | 96 | temporarily store their information into a module global. Application of |
|
102 | 97 | this class decorator copies that global data to the class instance and |
|
103 | 98 | clears the global. |
|
104 | 99 | |
|
105 | 100 | Obviously, this mechanism is not thread-safe, which means that the |
|
106 | 101 | *creation* of subclasses of Magic should only be done in a single-thread |
|
107 | 102 | context. Instantiation of the classes has no restrictions. Given that |
|
108 | 103 | these classes are typically created at IPython startup time and before user |
|
109 | 104 | application code becomes active, in practice this should not pose any |
|
110 | 105 | problems. |
|
111 | 106 | """ |
|
112 | 107 | cls.registered = True |
|
113 | 108 | cls.magics = dict(line = magics['line'], |
|
114 | 109 | cell = magics['cell']) |
|
115 | 110 | magics['line'] = {} |
|
116 | 111 | magics['cell'] = {} |
|
117 | 112 | return cls |
|
118 | 113 | |
|
119 | 114 | |
|
120 | 115 | def record_magic(dct, magic_kind, magic_name, func): |
|
121 | 116 | """Utility function to store a function as a magic of a specific kind. |
|
122 | 117 | |
|
123 | 118 | Parameters |
|
124 | 119 | ---------- |
|
125 | 120 | dct : dict |
|
126 | 121 | A dictionary with 'line' and 'cell' subdicts. |
|
127 | 122 | |
|
128 | 123 | magic_kind : str |
|
129 | 124 | Kind of magic to be stored. |
|
130 | 125 | |
|
131 | 126 | magic_name : str |
|
132 | 127 | Key to store the magic as. |
|
133 | 128 | |
|
134 | 129 | func : function |
|
135 | 130 | Callable object to store. |
|
136 | 131 | """ |
|
137 | 132 | if magic_kind == 'line_cell': |
|
138 | 133 | dct['line'][magic_name] = dct['cell'][magic_name] = func |
|
139 | 134 | else: |
|
140 | 135 | dct[magic_kind][magic_name] = func |
|
141 | 136 | |
|
142 | 137 | |
|
143 | 138 | def validate_type(magic_kind): |
|
144 | 139 | """Ensure that the given magic_kind is valid. |
|
145 | 140 | |
|
146 | 141 | Check that the given magic_kind is one of the accepted spec types (stored |
|
147 | 142 | in the global `magic_spec`), raise ValueError otherwise. |
|
148 | 143 | """ |
|
149 | 144 | if magic_kind not in magic_spec: |
|
150 | 145 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
151 | 146 | magic_kinds, magic_kind) |
|
152 | 147 | |
|
153 | 148 | |
|
154 | 149 | # The docstrings for the decorator below will be fairly similar for the two |
|
155 | 150 | # types (method and function), so we generate them here once and reuse the |
|
156 | 151 | # templates below. |
|
157 | 152 | _docstring_template = \ |
|
158 | 153 | """Decorate the given {0} as {1} magic. |
|
159 | 154 | |
|
160 | 155 | The decorator can be used with or without arguments, as follows. |
|
161 | 156 | |
|
162 | 157 | i) without arguments: it will create a {1} magic named as the {0} being |
|
163 | 158 | decorated:: |
|
164 | 159 | |
|
165 | 160 | @deco |
|
166 | 161 | def foo(...) |
|
167 | 162 | |
|
168 | 163 | will create a {1} magic named `foo`. |
|
169 | 164 | |
|
170 | 165 | ii) with one string argument: which will be used as the actual name of the |
|
171 | 166 | resulting magic:: |
|
172 | 167 | |
|
173 | 168 | @deco('bar') |
|
174 | 169 | def foo(...) |
|
175 | 170 | |
|
176 | 171 | will create a {1} magic named `bar`. |
|
177 | 172 | """ |
|
178 | 173 | |
|
179 | 174 | # These two are decorator factories. While they are conceptually very similar, |
|
180 | 175 | # there are enough differences in the details that it's simpler to have them |
|
181 | 176 | # written as completely standalone functions rather than trying to share code |
|
182 | 177 | # and make a single one with convoluted logic. |
|
183 | 178 | |
|
184 | 179 | def _method_magic_marker(magic_kind): |
|
185 | 180 | """Decorator factory for methods in Magics subclasses. |
|
186 | 181 | """ |
|
187 | 182 | |
|
188 | 183 | validate_type(magic_kind) |
|
189 | 184 | |
|
190 | 185 | # This is a closure to capture the magic_kind. We could also use a class, |
|
191 | 186 | # but it's overkill for just that one bit of state. |
|
192 | 187 | def magic_deco(arg): |
|
193 | 188 | call = lambda f, *a, **k: f(*a, **k) |
|
194 | 189 | |
|
195 | 190 | if callable(arg): |
|
196 | 191 | # "Naked" decorator call (just @foo, no args) |
|
197 | 192 | func = arg |
|
198 | 193 | name = func.__name__ |
|
199 | 194 | retval = decorator(call, func) |
|
200 | 195 | record_magic(magics, magic_kind, name, name) |
|
201 | 196 | elif isinstance(arg, string_types): |
|
202 | 197 | # Decorator called with arguments (@foo('bar')) |
|
203 | 198 | name = arg |
|
204 | 199 | def mark(func, *a, **kw): |
|
205 | 200 | record_magic(magics, magic_kind, name, func.__name__) |
|
206 | 201 | return decorator(call, func) |
|
207 | 202 | retval = mark |
|
208 | 203 | else: |
|
209 | 204 | raise TypeError("Decorator can only be called with " |
|
210 | 205 | "string or function") |
|
211 | 206 | return retval |
|
212 | 207 | |
|
213 | 208 | # Ensure the resulting decorator has a usable docstring |
|
214 | 209 | magic_deco.__doc__ = _docstring_template.format('method', magic_kind) |
|
215 | 210 | return magic_deco |
|
216 | 211 | |
|
217 | 212 | |
|
218 | 213 | def _function_magic_marker(magic_kind): |
|
219 | 214 | """Decorator factory for standalone functions. |
|
220 | 215 | """ |
|
221 | 216 | validate_type(magic_kind) |
|
222 | 217 | |
|
223 | 218 | # This is a closure to capture the magic_kind. We could also use a class, |
|
224 | 219 | # but it's overkill for just that one bit of state. |
|
225 | 220 | def magic_deco(arg): |
|
226 | 221 | call = lambda f, *a, **k: f(*a, **k) |
|
227 | 222 | |
|
228 | 223 | # Find get_ipython() in the caller's namespace |
|
229 | 224 | caller = sys._getframe(1) |
|
230 | 225 | for ns in ['f_locals', 'f_globals', 'f_builtins']: |
|
231 | 226 | get_ipython = getattr(caller, ns).get('get_ipython') |
|
232 | 227 | if get_ipython is not None: |
|
233 | 228 | break |
|
234 | 229 | else: |
|
235 | 230 | raise NameError('Decorator can only run in context where ' |
|
236 | 231 | '`get_ipython` exists') |
|
237 | 232 | |
|
238 | 233 | ip = get_ipython() |
|
239 | 234 | |
|
240 | 235 | if callable(arg): |
|
241 | 236 | # "Naked" decorator call (just @foo, no args) |
|
242 | 237 | func = arg |
|
243 | 238 | name = func.__name__ |
|
244 | 239 | ip.register_magic_function(func, magic_kind, name) |
|
245 | 240 | retval = decorator(call, func) |
|
246 | 241 | elif isinstance(arg, string_types): |
|
247 | 242 | # Decorator called with arguments (@foo('bar')) |
|
248 | 243 | name = arg |
|
249 | 244 | def mark(func, *a, **kw): |
|
250 | 245 | ip.register_magic_function(func, magic_kind, name) |
|
251 | 246 | return decorator(call, func) |
|
252 | 247 | retval = mark |
|
253 | 248 | else: |
|
254 | 249 | raise TypeError("Decorator can only be called with " |
|
255 | 250 | "string or function") |
|
256 | 251 | return retval |
|
257 | 252 | |
|
258 | 253 | # Ensure the resulting decorator has a usable docstring |
|
259 | 254 | ds = _docstring_template.format('function', magic_kind) |
|
260 | 255 | |
|
261 | 256 | ds += dedent(""" |
|
262 | 257 | Note: this decorator can only be used in a context where IPython is already |
|
263 | 258 | active, so that the `get_ipython()` call succeeds. You can therefore use |
|
264 | 259 | it in your startup files loaded after IPython initializes, but *not* in the |
|
265 | 260 | IPython configuration file itself, which is executed before IPython is |
|
266 | 261 | fully up and running. Any file located in the `startup` subdirectory of |
|
267 | 262 | your configuration profile will be OK in this sense. |
|
268 | 263 | """) |
|
269 | 264 | |
|
270 | 265 | magic_deco.__doc__ = ds |
|
271 | 266 | return magic_deco |
|
272 | 267 | |
|
273 | 268 | |
|
274 | 269 | # Create the actual decorators for public use |
|
275 | 270 | |
|
276 | 271 | # These three are used to decorate methods in class definitions |
|
277 | 272 | line_magic = _method_magic_marker('line') |
|
278 | 273 | cell_magic = _method_magic_marker('cell') |
|
279 | 274 | line_cell_magic = _method_magic_marker('line_cell') |
|
280 | 275 | |
|
281 | 276 | # These three decorate standalone functions and perform the decoration |
|
282 | 277 | # immediately. They can only run where get_ipython() works |
|
283 | 278 | register_line_magic = _function_magic_marker('line') |
|
284 | 279 | register_cell_magic = _function_magic_marker('cell') |
|
285 | 280 | register_line_cell_magic = _function_magic_marker('line_cell') |
|
286 | 281 | |
|
287 | 282 | #----------------------------------------------------------------------------- |
|
288 | 283 | # Core Magic classes |
|
289 | 284 | #----------------------------------------------------------------------------- |
|
290 | 285 | |
|
291 | 286 | class MagicsManager(Configurable): |
|
292 | 287 | """Object that handles all magic-related functionality for IPython. |
|
293 | 288 | """ |
|
294 | 289 | # Non-configurable class attributes |
|
295 | 290 | |
|
296 | 291 | # A two-level dict, first keyed by magic type, then by magic function, and |
|
297 | 292 | # holding the actual callable object as value. This is the dict used for |
|
298 | 293 | # magic function dispatch |
|
299 | 294 | magics = Dict() |
|
300 | 295 | |
|
301 | 296 | # A registry of the original objects that we've been given holding magics. |
|
302 | 297 | registry = Dict() |
|
303 | 298 | |
|
304 | 299 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
305 | 300 | |
|
306 |
auto_magic = Bool(True, |
|
|
307 |
"Automatically call line magics without requiring explicit % prefix" |
|
|
308 | ||
|
309 | def _auto_magic_changed(self, name, value): | |
|
310 | self.shell.automagic = value | |
|
301 | auto_magic = Bool(True, help= | |
|
302 | "Automatically call line magics without requiring explicit % prefix" | |
|
303 | ).tag(config=True) | |
|
304 | @observe('auto_magic') | |
|
305 | def _auto_magic_changed(self, change): | |
|
306 | self.shell.automagic = change['new'] | |
|
311 | 307 | |
|
312 | 308 | _auto_status = [ |
|
313 | 309 | 'Automagic is OFF, % prefix IS needed for line magics.', |
|
314 | 310 | 'Automagic is ON, % prefix IS NOT needed for line magics.'] |
|
315 | 311 | |
|
316 | 312 | user_magics = Instance('IPython.core.magics.UserMagics', allow_none=True) |
|
317 | 313 | |
|
318 | 314 | def __init__(self, shell=None, config=None, user_magics=None, **traits): |
|
319 | 315 | |
|
320 | 316 | super(MagicsManager, self).__init__(shell=shell, config=config, |
|
321 | 317 | user_magics=user_magics, **traits) |
|
322 | 318 | self.magics = dict(line={}, cell={}) |
|
323 | 319 | # Let's add the user_magics to the registry for uniformity, so *all* |
|
324 | 320 | # registered magic containers can be found there. |
|
325 | 321 | self.registry[user_magics.__class__.__name__] = user_magics |
|
326 | 322 | |
|
327 | 323 | def auto_status(self): |
|
328 | 324 | """Return descriptive string with automagic status.""" |
|
329 | 325 | return self._auto_status[self.auto_magic] |
|
330 | 326 | |
|
331 | 327 | def lsmagic(self): |
|
332 | 328 | """Return a dict of currently available magic functions. |
|
333 | 329 | |
|
334 | 330 | The return dict has the keys 'line' and 'cell', corresponding to the |
|
335 | 331 | two types of magics we support. Each value is a list of names. |
|
336 | 332 | """ |
|
337 | 333 | return self.magics |
|
338 | 334 | |
|
339 | 335 | def lsmagic_docs(self, brief=False, missing=''): |
|
340 | 336 | """Return dict of documentation of magic functions. |
|
341 | 337 | |
|
342 | 338 | The return dict has the keys 'line' and 'cell', corresponding to the |
|
343 | 339 | two types of magics we support. Each value is a dict keyed by magic |
|
344 | 340 | name whose value is the function docstring. If a docstring is |
|
345 | 341 | unavailable, the value of `missing` is used instead. |
|
346 | 342 | |
|
347 | 343 | If brief is True, only the first line of each docstring will be returned. |
|
348 | 344 | """ |
|
349 | 345 | docs = {} |
|
350 | 346 | for m_type in self.magics: |
|
351 | 347 | m_docs = {} |
|
352 | 348 | for m_name, m_func in iteritems(self.magics[m_type]): |
|
353 | 349 | if m_func.__doc__: |
|
354 | 350 | if brief: |
|
355 | 351 | m_docs[m_name] = m_func.__doc__.split('\n', 1)[0] |
|
356 | 352 | else: |
|
357 | 353 | m_docs[m_name] = m_func.__doc__.rstrip() |
|
358 | 354 | else: |
|
359 | 355 | m_docs[m_name] = missing |
|
360 | 356 | docs[m_type] = m_docs |
|
361 | 357 | return docs |
|
362 | 358 | |
|
363 | 359 | def register(self, *magic_objects): |
|
364 | 360 | """Register one or more instances of Magics. |
|
365 | 361 | |
|
366 | 362 | Take one or more classes or instances of classes that subclass the main |
|
367 | 363 | `core.Magic` class, and register them with IPython to use the magic |
|
368 | 364 | functions they provide. The registration process will then ensure that |
|
369 | 365 | any methods that have decorated to provide line and/or cell magics will |
|
370 | 366 | be recognized with the `%x`/`%%x` syntax as a line/cell magic |
|
371 | 367 | respectively. |
|
372 | 368 | |
|
373 | 369 | If classes are given, they will be instantiated with the default |
|
374 | 370 | constructor. If your classes need a custom constructor, you should |
|
375 | 371 | instanitate them first and pass the instance. |
|
376 | 372 | |
|
377 | 373 | The provided arguments can be an arbitrary mix of classes and instances. |
|
378 | 374 | |
|
379 | 375 | Parameters |
|
380 | 376 | ---------- |
|
381 | 377 | magic_objects : one or more classes or instances |
|
382 | 378 | """ |
|
383 | 379 | # Start by validating them to ensure they have all had their magic |
|
384 | 380 | # methods registered at the instance level |
|
385 | 381 | for m in magic_objects: |
|
386 | 382 | if not m.registered: |
|
387 | 383 | raise ValueError("Class of magics %r was constructed without " |
|
388 | 384 | "the @register_magics class decorator") |
|
389 | 385 | if isinstance(m, type): |
|
390 | 386 | # If we're given an uninstantiated class |
|
391 | 387 | m = m(shell=self.shell) |
|
392 | 388 | |
|
393 | 389 | # Now that we have an instance, we can register it and update the |
|
394 | 390 | # table of callables |
|
395 | 391 | self.registry[m.__class__.__name__] = m |
|
396 | 392 | for mtype in magic_kinds: |
|
397 | 393 | self.magics[mtype].update(m.magics[mtype]) |
|
398 | 394 | |
|
399 | 395 | def register_function(self, func, magic_kind='line', magic_name=None): |
|
400 | 396 | """Expose a standalone function as magic function for IPython. |
|
401 | 397 | |
|
402 | 398 | This will create an IPython magic (line, cell or both) from a |
|
403 | 399 | standalone function. The functions should have the following |
|
404 | 400 | signatures: |
|
405 | 401 | |
|
406 | 402 | * For line magics: `def f(line)` |
|
407 | 403 | * For cell magics: `def f(line, cell)` |
|
408 | 404 | * For a function that does both: `def f(line, cell=None)` |
|
409 | 405 | |
|
410 | 406 | In the latter case, the function will be called with `cell==None` when |
|
411 | 407 | invoked as `%f`, and with cell as a string when invoked as `%%f`. |
|
412 | 408 | |
|
413 | 409 | Parameters |
|
414 | 410 | ---------- |
|
415 | 411 | func : callable |
|
416 | 412 | Function to be registered as a magic. |
|
417 | 413 | |
|
418 | 414 | magic_kind : str |
|
419 | 415 | Kind of magic, one of 'line', 'cell' or 'line_cell' |
|
420 | 416 | |
|
421 | 417 | magic_name : optional str |
|
422 | 418 | If given, the name the magic will have in the IPython namespace. By |
|
423 | 419 | default, the name of the function itself is used. |
|
424 | 420 | """ |
|
425 | 421 | |
|
426 | 422 | # Create the new method in the user_magics and register it in the |
|
427 | 423 | # global table |
|
428 | 424 | validate_type(magic_kind) |
|
429 | 425 | magic_name = func.__name__ if magic_name is None else magic_name |
|
430 | 426 | setattr(self.user_magics, magic_name, func) |
|
431 | 427 | record_magic(self.magics, magic_kind, magic_name, func) |
|
432 | 428 | |
|
433 | 429 | def define_magic(self, name, func): |
|
434 | 430 | """[Deprecated] Expose own function as magic function for IPython. |
|
435 | 431 | |
|
436 | 432 | Will be removed in IPython 5.0 |
|
437 | 433 | |
|
438 | 434 | Example:: |
|
439 | 435 | |
|
440 | 436 | def foo_impl(self, parameter_s=''): |
|
441 | 437 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
442 | 438 | print 'Magic function. Passed parameter is between < >:' |
|
443 | 439 | print '<%s>' % parameter_s |
|
444 | 440 | print 'The self object is:', self |
|
445 | 441 | |
|
446 | 442 | ip.define_magic('foo',foo_impl) |
|
447 | 443 | """ |
|
448 | 444 | meth = types.MethodType(func, self.user_magics) |
|
449 | 445 | setattr(self.user_magics, name, meth) |
|
450 | 446 | record_magic(self.magics, 'line', name, meth) |
|
451 | 447 | |
|
452 | 448 | def register_alias(self, alias_name, magic_name, magic_kind='line'): |
|
453 | 449 | """Register an alias to a magic function. |
|
454 | 450 | |
|
455 | 451 | The alias is an instance of :class:`MagicAlias`, which holds the |
|
456 | 452 | name and kind of the magic it should call. Binding is done at |
|
457 | 453 | call time, so if the underlying magic function is changed the alias |
|
458 | 454 | will call the new function. |
|
459 | 455 | |
|
460 | 456 | Parameters |
|
461 | 457 | ---------- |
|
462 | 458 | alias_name : str |
|
463 | 459 | The name of the magic to be registered. |
|
464 | 460 | |
|
465 | 461 | magic_name : str |
|
466 | 462 | The name of an existing magic. |
|
467 | 463 | |
|
468 | 464 | magic_kind : str |
|
469 | 465 | Kind of magic, one of 'line' or 'cell' |
|
470 | 466 | """ |
|
471 | 467 | |
|
472 | 468 | # `validate_type` is too permissive, as it allows 'line_cell' |
|
473 | 469 | # which we do not handle. |
|
474 | 470 | if magic_kind not in magic_kinds: |
|
475 | 471 | raise ValueError('magic_kind must be one of %s, %s given' % |
|
476 | 472 | magic_kinds, magic_kind) |
|
477 | 473 | |
|
478 | 474 | alias = MagicAlias(self.shell, magic_name, magic_kind) |
|
479 | 475 | setattr(self.user_magics, alias_name, alias) |
|
480 | 476 | record_magic(self.magics, magic_kind, alias_name, alias) |
|
481 | 477 | |
|
482 | 478 | # Key base class that provides the central functionality for magics. |
|
483 | 479 | |
|
484 | 480 | |
|
485 | 481 | class Magics(Configurable): |
|
486 | 482 | """Base class for implementing magic functions. |
|
487 | 483 | |
|
488 | 484 | Shell functions which can be reached as %function_name. All magic |
|
489 | 485 | functions should accept a string, which they can parse for their own |
|
490 | 486 | needs. This can make some functions easier to type, eg `%cd ../` |
|
491 | 487 | vs. `%cd("../")` |
|
492 | 488 | |
|
493 | 489 | Classes providing magic functions need to subclass this class, and they |
|
494 | 490 | MUST: |
|
495 | 491 | |
|
496 | 492 | - Use the method decorators `@line_magic` and `@cell_magic` to decorate |
|
497 | 493 | individual methods as magic functions, AND |
|
498 | 494 | |
|
499 | 495 | - Use the class decorator `@magics_class` to ensure that the magic |
|
500 | 496 | methods are properly registered at the instance level upon instance |
|
501 | 497 | initialization. |
|
502 | 498 | |
|
503 | 499 | See :mod:`magic_functions` for examples of actual implementation classes. |
|
504 | 500 | """ |
|
505 | 501 | # Dict holding all command-line options for each magic. |
|
506 | 502 | options_table = None |
|
507 | 503 | # Dict for the mapping of magic names to methods, set by class decorator |
|
508 | 504 | magics = None |
|
509 | 505 | # Flag to check that the class decorator was properly applied |
|
510 | 506 | registered = False |
|
511 | 507 | # Instance of IPython shell |
|
512 | 508 | shell = None |
|
513 | 509 | |
|
514 | 510 | def __init__(self, shell=None, **kwargs): |
|
515 | 511 | if not(self.__class__.registered): |
|
516 | 512 | raise ValueError('Magics subclass without registration - ' |
|
517 | 513 | 'did you forget to apply @magics_class?') |
|
518 | 514 | if shell is not None: |
|
519 | 515 | if hasattr(shell, 'configurables'): |
|
520 | 516 | shell.configurables.append(self) |
|
521 | 517 | if hasattr(shell, 'config'): |
|
522 | 518 | kwargs.setdefault('parent', shell) |
|
523 | 519 | |
|
524 | 520 | self.shell = shell |
|
525 | 521 | self.options_table = {} |
|
526 | 522 | # The method decorators are run when the instance doesn't exist yet, so |
|
527 | 523 | # they can only record the names of the methods they are supposed to |
|
528 | 524 | # grab. Only now, that the instance exists, can we create the proper |
|
529 | 525 | # mapping to bound methods. So we read the info off the original names |
|
530 | 526 | # table and replace each method name by the actual bound method. |
|
531 | 527 | # But we mustn't clobber the *class* mapping, in case of multiple instances. |
|
532 | 528 | class_magics = self.magics |
|
533 | 529 | self.magics = {} |
|
534 | 530 | for mtype in magic_kinds: |
|
535 | 531 | tab = self.magics[mtype] = {} |
|
536 | 532 | cls_tab = class_magics[mtype] |
|
537 | 533 | for magic_name, meth_name in iteritems(cls_tab): |
|
538 | 534 | if isinstance(meth_name, string_types): |
|
539 | 535 | # it's a method name, grab it |
|
540 | 536 | tab[magic_name] = getattr(self, meth_name) |
|
541 | 537 | else: |
|
542 | 538 | # it's the real thing |
|
543 | 539 | tab[magic_name] = meth_name |
|
544 | 540 | # Configurable **needs** to be initiated at the end or the config |
|
545 | 541 | # magics get screwed up. |
|
546 | 542 | super(Magics, self).__init__(**kwargs) |
|
547 | 543 | |
|
548 | 544 | def arg_err(self,func): |
|
549 | 545 | """Print docstring if incorrect arguments were passed""" |
|
550 | 546 | print('Error in arguments:') |
|
551 | 547 | print(oinspect.getdoc(func)) |
|
552 | 548 | |
|
553 | 549 | def format_latex(self, strng): |
|
554 | 550 | """Format a string for latex inclusion.""" |
|
555 | 551 | |
|
556 | 552 | # Characters that need to be escaped for latex: |
|
557 | 553 | escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE) |
|
558 | 554 | # Magic command names as headers: |
|
559 | 555 | cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC, |
|
560 | 556 | re.MULTILINE) |
|
561 | 557 | # Magic commands |
|
562 | 558 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC, |
|
563 | 559 | re.MULTILINE) |
|
564 | 560 | # Paragraph continue |
|
565 | 561 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
566 | 562 | |
|
567 | 563 | # The "\n" symbol |
|
568 | 564 | newline_re = re.compile(r'\\n') |
|
569 | 565 | |
|
570 | 566 | # Now build the string for output: |
|
571 | 567 | #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
572 | 568 | strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:', |
|
573 | 569 | strng) |
|
574 | 570 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
575 | 571 | strng = par_re.sub(r'\\\\',strng) |
|
576 | 572 | strng = escape_re.sub(r'\\\1',strng) |
|
577 | 573 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
578 | 574 | return strng |
|
579 | 575 | |
|
580 | 576 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): |
|
581 | 577 | """Parse options passed to an argument string. |
|
582 | 578 | |
|
583 | 579 | The interface is similar to that of :func:`getopt.getopt`, but it |
|
584 | 580 | returns a :class:`~IPython.utils.struct.Struct` with the options as keys |
|
585 | 581 | and the stripped argument string still as a string. |
|
586 | 582 | |
|
587 | 583 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
588 | 584 | This allows us to easily expand variables, glob files, quote |
|
589 | 585 | arguments, etc. |
|
590 | 586 | |
|
591 | 587 | Parameters |
|
592 | 588 | ---------- |
|
593 | 589 | |
|
594 | 590 | arg_str : str |
|
595 | 591 | The arguments to parse. |
|
596 | 592 | |
|
597 | 593 | opt_str : str |
|
598 | 594 | The options specification. |
|
599 | 595 | |
|
600 | 596 | mode : str, default 'string' |
|
601 | 597 | If given as 'list', the argument string is returned as a list (split |
|
602 | 598 | on whitespace) instead of a string. |
|
603 | 599 | |
|
604 | 600 | list_all : bool, default False |
|
605 | 601 | Put all option values in lists. Normally only options |
|
606 | 602 | appearing more than once are put in a list. |
|
607 | 603 | |
|
608 | 604 | posix : bool, default True |
|
609 | 605 | Whether to split the input line in POSIX mode or not, as per the |
|
610 | 606 | conventions outlined in the :mod:`shlex` module from the standard |
|
611 | 607 | library. |
|
612 | 608 | """ |
|
613 | 609 | |
|
614 | 610 | # inject default options at the beginning of the input line |
|
615 | 611 | caller = sys._getframe(1).f_code.co_name |
|
616 | 612 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
617 | 613 | |
|
618 | 614 | mode = kw.get('mode','string') |
|
619 | 615 | if mode not in ['string','list']: |
|
620 | 616 | raise ValueError('incorrect mode given: %s' % mode) |
|
621 | 617 | # Get options |
|
622 | 618 | list_all = kw.get('list_all',0) |
|
623 | 619 | posix = kw.get('posix', os.name == 'posix') |
|
624 | 620 | strict = kw.get('strict', True) |
|
625 | 621 | |
|
626 | 622 | # Check if we have more than one argument to warrant extra processing: |
|
627 | 623 | odict = {} # Dictionary with options |
|
628 | 624 | args = arg_str.split() |
|
629 | 625 | if len(args) >= 1: |
|
630 | 626 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
631 | 627 | # need to look for options |
|
632 | 628 | argv = arg_split(arg_str, posix, strict) |
|
633 | 629 | # Do regular option processing |
|
634 | 630 | try: |
|
635 | 631 | opts,args = getopt(argv, opt_str, long_opts) |
|
636 | 632 | except GetoptError as e: |
|
637 | 633 | raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |
|
638 | 634 | " ".join(long_opts))) |
|
639 | 635 | for o,a in opts: |
|
640 | 636 | if o.startswith('--'): |
|
641 | 637 | o = o[2:] |
|
642 | 638 | else: |
|
643 | 639 | o = o[1:] |
|
644 | 640 | try: |
|
645 | 641 | odict[o].append(a) |
|
646 | 642 | except AttributeError: |
|
647 | 643 | odict[o] = [odict[o],a] |
|
648 | 644 | except KeyError: |
|
649 | 645 | if list_all: |
|
650 | 646 | odict[o] = [a] |
|
651 | 647 | else: |
|
652 | 648 | odict[o] = a |
|
653 | 649 | |
|
654 | 650 | # Prepare opts,args for return |
|
655 | 651 | opts = Struct(odict) |
|
656 | 652 | if mode == 'string': |
|
657 | 653 | args = ' '.join(args) |
|
658 | 654 | |
|
659 | 655 | return opts,args |
|
660 | 656 | |
|
661 | 657 | def default_option(self, fn, optstr): |
|
662 | 658 | """Make an entry in the options_table for fn, with value optstr""" |
|
663 | 659 | |
|
664 | 660 | if fn not in self.lsmagic(): |
|
665 | 661 | error("%s is not a magic function" % fn) |
|
666 | 662 | self.options_table[fn] = optstr |
|
667 | 663 | |
|
668 | 664 | |
|
669 | 665 | class MagicAlias(object): |
|
670 | 666 | """An alias to another magic function. |
|
671 | 667 | |
|
672 | 668 | An alias is determined by its magic name and magic kind. Lookup |
|
673 | 669 | is done at call time, so if the underlying magic changes the alias |
|
674 | 670 | will call the new function. |
|
675 | 671 | |
|
676 | 672 | Use the :meth:`MagicsManager.register_alias` method or the |
|
677 | 673 | `%alias_magic` magic function to create and register a new alias. |
|
678 | 674 | """ |
|
679 | 675 | def __init__(self, shell, magic_name, magic_kind): |
|
680 | 676 | self.shell = shell |
|
681 | 677 | self.magic_name = magic_name |
|
682 | 678 | self.magic_kind = magic_kind |
|
683 | 679 | |
|
684 | 680 | self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name) |
|
685 | 681 | self.__doc__ = "Alias for `%s`." % self.pretty_target |
|
686 | 682 | |
|
687 | 683 | self._in_call = False |
|
688 | 684 | |
|
689 | 685 | def __call__(self, *args, **kwargs): |
|
690 | 686 | """Call the magic alias.""" |
|
691 | 687 | fn = self.shell.find_magic(self.magic_name, self.magic_kind) |
|
692 | 688 | if fn is None: |
|
693 | 689 | raise UsageError("Magic `%s` not found." % self.pretty_target) |
|
694 | 690 | |
|
695 | 691 | # Protect against infinite recursion. |
|
696 | 692 | if self._in_call: |
|
697 | 693 | raise UsageError("Infinite recursion detected; " |
|
698 | 694 | "magic aliases cannot call themselves.") |
|
699 | 695 | self._in_call = True |
|
700 | 696 | try: |
|
701 | 697 | return fn(*args, **kwargs) |
|
702 | 698 | finally: |
|
703 | 699 | self._in_call = False |
@@ -1,282 +1,272 b'' | |||
|
1 | 1 | """Magic functions for running cells in various scripts.""" |
|
2 | 2 | from __future__ import print_function |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2012 The IPython Development Team. | |
|
5 | # | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
7 | # | |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | 3 | |
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Imports | |
|
13 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) IPython Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
14 | 6 | |
|
15 | # Stdlib | |
|
16 | 7 | import errno |
|
17 | 8 | import os |
|
18 | 9 | import sys |
|
19 | 10 | import signal |
|
20 | 11 | import time |
|
21 | 12 | from subprocess import Popen, PIPE |
|
22 | 13 | import atexit |
|
23 | 14 | |
|
24 | # Our own packages | |
|
25 | from traitlets.config.configurable import Configurable | |
|
26 | 15 | from IPython.core import magic_arguments |
|
27 | 16 | from IPython.core.magic import ( |
|
28 | 17 | Magics, magics_class, line_magic, cell_magic |
|
29 | 18 | ) |
|
30 | 19 | from IPython.lib.backgroundjobs import BackgroundJobManager |
|
31 | 20 | from IPython.utils import py3compat |
|
32 | 21 | from IPython.utils.process import arg_split |
|
33 | from traitlets import List, Dict | |
|
22 | from traitlets import List, Dict, default | |
|
34 | 23 | |
|
35 | 24 | #----------------------------------------------------------------------------- |
|
36 | 25 | # Magic implementation classes |
|
37 | 26 | #----------------------------------------------------------------------------- |
|
38 | 27 | |
|
39 | 28 | def script_args(f): |
|
40 | 29 | """single decorator for adding script args""" |
|
41 | 30 | args = [ |
|
42 | 31 | magic_arguments.argument( |
|
43 | 32 | '--out', type=str, |
|
44 | 33 | help="""The variable in which to store stdout from the script. |
|
45 | 34 | If the script is backgrounded, this will be the stdout *pipe*, |
|
46 | 35 | instead of the stderr text itself. |
|
47 | 36 | """ |
|
48 | 37 | ), |
|
49 | 38 | magic_arguments.argument( |
|
50 | 39 | '--err', type=str, |
|
51 | 40 | help="""The variable in which to store stderr from the script. |
|
52 | 41 | If the script is backgrounded, this will be the stderr *pipe*, |
|
53 | 42 | instead of the stderr text itself. |
|
54 | 43 | """ |
|
55 | 44 | ), |
|
56 | 45 | magic_arguments.argument( |
|
57 | 46 | '--bg', action="store_true", |
|
58 | 47 | help="""Whether to run the script in the background. |
|
59 | 48 | If given, the only way to see the output of the command is |
|
60 | 49 | with --out/err. |
|
61 | 50 | """ |
|
62 | 51 | ), |
|
63 | 52 | magic_arguments.argument( |
|
64 | 53 | '--proc', type=str, |
|
65 | 54 | help="""The variable in which to store Popen instance. |
|
66 | 55 | This is used only when --bg option is given. |
|
67 | 56 | """ |
|
68 | 57 | ), |
|
69 | 58 | ] |
|
70 | 59 | for arg in args: |
|
71 | 60 | f = arg(f) |
|
72 | 61 | return f |
|
73 | 62 | |
|
74 | 63 | @magics_class |
|
75 | 64 | class ScriptMagics(Magics): |
|
76 | 65 | """Magics for talking to scripts |
|
77 | 66 | |
|
78 | 67 | This defines a base `%%script` cell magic for running a cell |
|
79 | 68 | with a program in a subprocess, and registers a few top-level |
|
80 | 69 | magics that call %%script with common interpreters. |
|
81 | 70 | """ |
|
82 |
script_magics = List( |
|
|
71 | script_magics = List( | |
|
83 | 72 | help="""Extra script cell magics to define |
|
84 | 73 | |
|
85 | 74 | This generates simple wrappers of `%%script foo` as `%%foo`. |
|
86 | 75 | |
|
87 | 76 | If you want to add script magics that aren't on your path, |
|
88 | 77 | specify them in script_paths |
|
89 | 78 | """, |
|
90 | ) | |
|
79 | ).tag(config=True) | |
|
80 | @default('script_magics') | |
|
91 | 81 | def _script_magics_default(self): |
|
92 | 82 | """default to a common list of programs""" |
|
93 | 83 | |
|
94 | 84 | defaults = [ |
|
95 | 85 | 'sh', |
|
96 | 86 | 'bash', |
|
97 | 87 | 'perl', |
|
98 | 88 | 'ruby', |
|
99 | 89 | 'python', |
|
100 | 90 | 'python2', |
|
101 | 91 | 'python3', |
|
102 | 92 | 'pypy', |
|
103 | 93 | ] |
|
104 | 94 | if os.name == 'nt': |
|
105 | 95 | defaults.extend([ |
|
106 | 96 | 'cmd', |
|
107 | 97 | ]) |
|
108 | 98 | |
|
109 | 99 | return defaults |
|
110 | 100 | |
|
111 |
script_paths = Dict( |
|
|
101 | script_paths = Dict( | |
|
112 | 102 | help="""Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby' |
|
113 | 103 | |
|
114 | 104 | Only necessary for items in script_magics where the default path will not |
|
115 | 105 | find the right interpreter. |
|
116 | 106 | """ |
|
117 | ) | |
|
107 | ).tag(config=True) | |
|
118 | 108 | |
|
119 | 109 | def __init__(self, shell=None): |
|
120 | 110 | super(ScriptMagics, self).__init__(shell=shell) |
|
121 | 111 | self._generate_script_magics() |
|
122 | 112 | self.job_manager = BackgroundJobManager() |
|
123 | 113 | self.bg_processes = [] |
|
124 | 114 | atexit.register(self.kill_bg_processes) |
|
125 | 115 | |
|
126 | 116 | def __del__(self): |
|
127 | 117 | self.kill_bg_processes() |
|
128 | 118 | |
|
129 | 119 | def _generate_script_magics(self): |
|
130 | 120 | cell_magics = self.magics['cell'] |
|
131 | 121 | for name in self.script_magics: |
|
132 | 122 | cell_magics[name] = self._make_script_magic(name) |
|
133 | 123 | |
|
134 | 124 | def _make_script_magic(self, name): |
|
135 | 125 | """make a named magic, that calls %%script with a particular program""" |
|
136 | 126 | # expand to explicit path if necessary: |
|
137 | 127 | script = self.script_paths.get(name, name) |
|
138 | 128 | |
|
139 | 129 | @magic_arguments.magic_arguments() |
|
140 | 130 | @script_args |
|
141 | 131 | def named_script_magic(line, cell): |
|
142 | 132 | # if line, add it as cl-flags |
|
143 | 133 | if line: |
|
144 | 134 | line = "%s %s" % (script, line) |
|
145 | 135 | else: |
|
146 | 136 | line = script |
|
147 | 137 | return self.shebang(line, cell) |
|
148 | 138 | |
|
149 | 139 | # write a basic docstring: |
|
150 | 140 | named_script_magic.__doc__ = \ |
|
151 | 141 | """%%{name} script magic |
|
152 | 142 | |
|
153 | 143 | Run cells with {script} in a subprocess. |
|
154 | 144 | |
|
155 | 145 | This is a shortcut for `%%script {script}` |
|
156 | 146 | """.format(**locals()) |
|
157 | 147 | |
|
158 | 148 | return named_script_magic |
|
159 | 149 | |
|
160 | 150 | @magic_arguments.magic_arguments() |
|
161 | 151 | @script_args |
|
162 | 152 | @cell_magic("script") |
|
163 | 153 | def shebang(self, line, cell): |
|
164 | 154 | """Run a cell via a shell command |
|
165 | 155 | |
|
166 | 156 | The `%%script` line is like the #! line of script, |
|
167 | 157 | specifying a program (bash, perl, ruby, etc.) with which to run. |
|
168 | 158 | |
|
169 | 159 | The rest of the cell is run by that program. |
|
170 | 160 | |
|
171 | 161 | Examples |
|
172 | 162 | -------- |
|
173 | 163 | :: |
|
174 | 164 | |
|
175 | 165 | In [1]: %%script bash |
|
176 | 166 | ...: for i in 1 2 3; do |
|
177 | 167 | ...: echo $i |
|
178 | 168 | ...: done |
|
179 | 169 | 1 |
|
180 | 170 | 2 |
|
181 | 171 | 3 |
|
182 | 172 | """ |
|
183 | 173 | argv = arg_split(line, posix = not sys.platform.startswith('win')) |
|
184 | 174 | args, cmd = self.shebang.parser.parse_known_args(argv) |
|
185 | 175 | |
|
186 | 176 | try: |
|
187 | 177 | p = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
|
188 | 178 | except OSError as e: |
|
189 | 179 | if e.errno == errno.ENOENT: |
|
190 | 180 | print("Couldn't find program: %r" % cmd[0]) |
|
191 | 181 | return |
|
192 | 182 | else: |
|
193 | 183 | raise |
|
194 | 184 | |
|
195 | 185 | if not cell.endswith('\n'): |
|
196 | 186 | cell += '\n' |
|
197 | 187 | cell = cell.encode('utf8', 'replace') |
|
198 | 188 | if args.bg: |
|
199 | 189 | self.bg_processes.append(p) |
|
200 | 190 | self._gc_bg_processes() |
|
201 | 191 | if args.out: |
|
202 | 192 | self.shell.user_ns[args.out] = p.stdout |
|
203 | 193 | if args.err: |
|
204 | 194 | self.shell.user_ns[args.err] = p.stderr |
|
205 | 195 | self.job_manager.new(self._run_script, p, cell, daemon=True) |
|
206 | 196 | if args.proc: |
|
207 | 197 | self.shell.user_ns[args.proc] = p |
|
208 | 198 | return |
|
209 | 199 | |
|
210 | 200 | try: |
|
211 | 201 | out, err = p.communicate(cell) |
|
212 | 202 | except KeyboardInterrupt: |
|
213 | 203 | try: |
|
214 | 204 | p.send_signal(signal.SIGINT) |
|
215 | 205 | time.sleep(0.1) |
|
216 | 206 | if p.poll() is not None: |
|
217 | 207 | print("Process is interrupted.") |
|
218 | 208 | return |
|
219 | 209 | p.terminate() |
|
220 | 210 | time.sleep(0.1) |
|
221 | 211 | if p.poll() is not None: |
|
222 | 212 | print("Process is terminated.") |
|
223 | 213 | return |
|
224 | 214 | p.kill() |
|
225 | 215 | print("Process is killed.") |
|
226 | 216 | except OSError: |
|
227 | 217 | pass |
|
228 | 218 | except Exception as e: |
|
229 | 219 | print("Error while terminating subprocess (pid=%i): %s" \ |
|
230 | 220 | % (p.pid, e)) |
|
231 | 221 | return |
|
232 | 222 | out = py3compat.bytes_to_str(out) |
|
233 | 223 | err = py3compat.bytes_to_str(err) |
|
234 | 224 | if args.out: |
|
235 | 225 | self.shell.user_ns[args.out] = out |
|
236 | 226 | else: |
|
237 | 227 | sys.stdout.write(out) |
|
238 | 228 | sys.stdout.flush() |
|
239 | 229 | if args.err: |
|
240 | 230 | self.shell.user_ns[args.err] = err |
|
241 | 231 | else: |
|
242 | 232 | sys.stderr.write(err) |
|
243 | 233 | sys.stderr.flush() |
|
244 | 234 | |
|
245 | 235 | def _run_script(self, p, cell): |
|
246 | 236 | """callback for running the script in the background""" |
|
247 | 237 | p.stdin.write(cell) |
|
248 | 238 | p.stdin.close() |
|
249 | 239 | p.wait() |
|
250 | 240 | |
|
251 | 241 | @line_magic("killbgscripts") |
|
252 | 242 | def killbgscripts(self, _nouse_=''): |
|
253 | 243 | """Kill all BG processes started by %%script and its family.""" |
|
254 | 244 | self.kill_bg_processes() |
|
255 | 245 | print("All background processes were killed.") |
|
256 | 246 | |
|
257 | 247 | def kill_bg_processes(self): |
|
258 | 248 | """Kill all BG processes which are still running.""" |
|
259 | 249 | for p in self.bg_processes: |
|
260 | 250 | if p.poll() is None: |
|
261 | 251 | try: |
|
262 | 252 | p.send_signal(signal.SIGINT) |
|
263 | 253 | except: |
|
264 | 254 | pass |
|
265 | 255 | time.sleep(0.1) |
|
266 | 256 | for p in self.bg_processes: |
|
267 | 257 | if p.poll() is None: |
|
268 | 258 | try: |
|
269 | 259 | p.terminate() |
|
270 | 260 | except: |
|
271 | 261 | pass |
|
272 | 262 | time.sleep(0.1) |
|
273 | 263 | for p in self.bg_processes: |
|
274 | 264 | if p.poll() is None: |
|
275 | 265 | try: |
|
276 | 266 | p.kill() |
|
277 | 267 | except: |
|
278 | 268 | pass |
|
279 | 269 | self._gc_bg_processes() |
|
280 | 270 | |
|
281 | 271 | def _gc_bg_processes(self): |
|
282 | 272 | self.bg_processes = [p for p in self.bg_processes if p.poll() is None] |
@@ -1,715 +1,700 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 | ||
|
8 | Authors: | |
|
9 | ||
|
10 | * Brian Granger | |
|
11 | * Fernando Perez | |
|
12 | * Dan Milstein | |
|
13 | * Ville Vainio | |
|
14 | 7 | """ |
|
15 | 8 | |
|
16 | #----------------------------------------------------------------------------- | |
|
17 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
18 | # | |
|
19 | # Distributed under the terms of the BSD License. The full license is in | |
|
20 | # the file COPYING, distributed as part of this software. | |
|
21 | #----------------------------------------------------------------------------- | |
|
22 | ||
|
23 | #----------------------------------------------------------------------------- | |
|
24 | # Imports | |
|
25 | #----------------------------------------------------------------------------- | |
|
9 | # Copyright (c) IPython Development Team. | |
|
10 | # Distributed under the terms of the Modified BSD License. | |
|
26 | 11 | |
|
27 | 12 | from keyword import iskeyword |
|
28 | 13 | import re |
|
29 | 14 | |
|
30 | 15 | from IPython.core.autocall import IPyAutocall |
|
31 | 16 | from traitlets.config.configurable import Configurable |
|
32 | 17 | from IPython.core.inputsplitter import ( |
|
33 | 18 | ESC_MAGIC, |
|
34 | 19 | ESC_QUOTE, |
|
35 | 20 | ESC_QUOTE2, |
|
36 | 21 | ESC_PAREN, |
|
37 | 22 | ) |
|
38 | 23 | from IPython.core.macro import Macro |
|
39 | 24 | from IPython.core.splitinput import LineInfo |
|
40 | 25 | |
|
41 | 26 | from traitlets import ( |
|
42 |
List, Integer, Unicode, |
|
|
27 | List, Integer, Unicode, Bool, Instance, CRegExp | |
|
43 | 28 | ) |
|
44 | 29 | |
|
45 | 30 | #----------------------------------------------------------------------------- |
|
46 | 31 | # Global utilities, errors and constants |
|
47 | 32 | #----------------------------------------------------------------------------- |
|
48 | 33 | |
|
49 | 34 | |
|
50 | 35 | class PrefilterError(Exception): |
|
51 | 36 | pass |
|
52 | 37 | |
|
53 | 38 | |
|
54 | 39 | # RegExp to identify potential function names |
|
55 | 40 | re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
56 | 41 | |
|
57 | 42 | # RegExp to exclude strings with this start from autocalling. In |
|
58 | 43 | # particular, all binary operators should be excluded, so that if foo is |
|
59 | 44 | # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The |
|
60 | 45 | # characters '!=()' don't need to be checked for, as the checkPythonChars |
|
61 | 46 | # routine explicitely does so, to catch direct calls and rebindings of |
|
62 | 47 | # existing names. |
|
63 | 48 | |
|
64 | 49 | # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise |
|
65 | 50 | # it affects the rest of the group in square brackets. |
|
66 | 51 | re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]' |
|
67 | 52 | r'|^is |^not |^in |^and |^or ') |
|
68 | 53 | |
|
69 | 54 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
70 | 55 | # (experimental). For this to work, the line_split regexp would need |
|
71 | 56 | # to be modified so it wouldn't break things at '['. That line is |
|
72 | 57 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
73 | 58 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
74 | 59 | |
|
75 | 60 | |
|
76 | 61 | # Handler Check Utilities |
|
77 | 62 | def is_shadowed(identifier, ip): |
|
78 | 63 | """Is the given identifier defined in one of the namespaces which shadow |
|
79 | 64 | the alias and magic namespaces? Note that an identifier is different |
|
80 | 65 | than ifun, because it can not contain a '.' character.""" |
|
81 | 66 | # This is much safer than calling ofind, which can change state |
|
82 | 67 | return (identifier in ip.user_ns \ |
|
83 | 68 | or identifier in ip.user_global_ns \ |
|
84 | 69 | or identifier in ip.ns_table['builtin']\ |
|
85 | 70 | or iskeyword(identifier)) |
|
86 | 71 | |
|
87 | 72 | |
|
88 | 73 | #----------------------------------------------------------------------------- |
|
89 | 74 | # Main Prefilter manager |
|
90 | 75 | #----------------------------------------------------------------------------- |
|
91 | 76 | |
|
92 | 77 | |
|
93 | 78 | class PrefilterManager(Configurable): |
|
94 | 79 | """Main prefilter component. |
|
95 | 80 | |
|
96 | 81 | The IPython prefilter is run on all user input before it is run. The |
|
97 | 82 | prefilter consumes lines of input and produces transformed lines of |
|
98 | 83 | input. |
|
99 | 84 | |
|
100 | 85 | The iplementation consists of two phases: |
|
101 | 86 | |
|
102 | 87 | 1. Transformers |
|
103 | 88 | 2. Checkers and handlers |
|
104 | 89 | |
|
105 | 90 | Over time, we plan on deprecating the checkers and handlers and doing |
|
106 | 91 | everything in the transformers. |
|
107 | 92 | |
|
108 | 93 | The transformers are instances of :class:`PrefilterTransformer` and have |
|
109 | 94 | a single method :meth:`transform` that takes a line and returns a |
|
110 | 95 | transformed line. The transformation can be accomplished using any |
|
111 | 96 | tool, but our current ones use regular expressions for speed. |
|
112 | 97 | |
|
113 | 98 | After all the transformers have been run, the line is fed to the checkers, |
|
114 | 99 | which are instances of :class:`PrefilterChecker`. The line is passed to |
|
115 | 100 | the :meth:`check` method, which either returns `None` or a |
|
116 | 101 | :class:`PrefilterHandler` instance. If `None` is returned, the other |
|
117 | 102 | checkers are tried. If an :class:`PrefilterHandler` instance is returned, |
|
118 | 103 | the line is passed to the :meth:`handle` method of the returned |
|
119 | 104 | handler and no further checkers are tried. |
|
120 | 105 | |
|
121 | 106 | Both transformers and checkers have a `priority` attribute, that determines |
|
122 | 107 | the order in which they are called. Smaller priorities are tried first. |
|
123 | 108 | |
|
124 | 109 | Both transformers and checkers also have `enabled` attribute, which is |
|
125 | 110 | a boolean that determines if the instance is used. |
|
126 | 111 | |
|
127 | 112 | Users or developers can change the priority or enabled attribute of |
|
128 | 113 | transformers or checkers, but they must call the :meth:`sort_checkers` |
|
129 | 114 | or :meth:`sort_transformers` method after changing the priority. |
|
130 | 115 | """ |
|
131 | 116 | |
|
132 |
multi_line_specials = |
|
|
117 | multi_line_specials = Bool(True).tag(config=True) | |
|
133 | 118 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
134 | 119 | |
|
135 | 120 | def __init__(self, shell=None, **kwargs): |
|
136 | 121 | super(PrefilterManager, self).__init__(shell=shell, **kwargs) |
|
137 | 122 | self.shell = shell |
|
138 | 123 | self.init_transformers() |
|
139 | 124 | self.init_handlers() |
|
140 | 125 | self.init_checkers() |
|
141 | 126 | |
|
142 | 127 | #------------------------------------------------------------------------- |
|
143 | 128 | # API for managing transformers |
|
144 | 129 | #------------------------------------------------------------------------- |
|
145 | 130 | |
|
146 | 131 | def init_transformers(self): |
|
147 | 132 | """Create the default transformers.""" |
|
148 | 133 | self._transformers = [] |
|
149 | 134 | for transformer_cls in _default_transformers: |
|
150 | 135 | transformer_cls( |
|
151 | 136 | shell=self.shell, prefilter_manager=self, parent=self |
|
152 | 137 | ) |
|
153 | 138 | |
|
154 | 139 | def sort_transformers(self): |
|
155 | 140 | """Sort the transformers by priority. |
|
156 | 141 | |
|
157 | 142 | This must be called after the priority of a transformer is changed. |
|
158 | 143 | The :meth:`register_transformer` method calls this automatically. |
|
159 | 144 | """ |
|
160 | 145 | self._transformers.sort(key=lambda x: x.priority) |
|
161 | 146 | |
|
162 | 147 | @property |
|
163 | 148 | def transformers(self): |
|
164 | 149 | """Return a list of checkers, sorted by priority.""" |
|
165 | 150 | return self._transformers |
|
166 | 151 | |
|
167 | 152 | def register_transformer(self, transformer): |
|
168 | 153 | """Register a transformer instance.""" |
|
169 | 154 | if transformer not in self._transformers: |
|
170 | 155 | self._transformers.append(transformer) |
|
171 | 156 | self.sort_transformers() |
|
172 | 157 | |
|
173 | 158 | def unregister_transformer(self, transformer): |
|
174 | 159 | """Unregister a transformer instance.""" |
|
175 | 160 | if transformer in self._transformers: |
|
176 | 161 | self._transformers.remove(transformer) |
|
177 | 162 | |
|
178 | 163 | #------------------------------------------------------------------------- |
|
179 | 164 | # API for managing checkers |
|
180 | 165 | #------------------------------------------------------------------------- |
|
181 | 166 | |
|
182 | 167 | def init_checkers(self): |
|
183 | 168 | """Create the default checkers.""" |
|
184 | 169 | self._checkers = [] |
|
185 | 170 | for checker in _default_checkers: |
|
186 | 171 | checker( |
|
187 | 172 | shell=self.shell, prefilter_manager=self, parent=self |
|
188 | 173 | ) |
|
189 | 174 | |
|
190 | 175 | def sort_checkers(self): |
|
191 | 176 | """Sort the checkers by priority. |
|
192 | 177 | |
|
193 | 178 | This must be called after the priority of a checker is changed. |
|
194 | 179 | The :meth:`register_checker` method calls this automatically. |
|
195 | 180 | """ |
|
196 | 181 | self._checkers.sort(key=lambda x: x.priority) |
|
197 | 182 | |
|
198 | 183 | @property |
|
199 | 184 | def checkers(self): |
|
200 | 185 | """Return a list of checkers, sorted by priority.""" |
|
201 | 186 | return self._checkers |
|
202 | 187 | |
|
203 | 188 | def register_checker(self, checker): |
|
204 | 189 | """Register a checker instance.""" |
|
205 | 190 | if checker not in self._checkers: |
|
206 | 191 | self._checkers.append(checker) |
|
207 | 192 | self.sort_checkers() |
|
208 | 193 | |
|
209 | 194 | def unregister_checker(self, checker): |
|
210 | 195 | """Unregister a checker instance.""" |
|
211 | 196 | if checker in self._checkers: |
|
212 | 197 | self._checkers.remove(checker) |
|
213 | 198 | |
|
214 | 199 | #------------------------------------------------------------------------- |
|
215 | 200 | # API for managing handlers |
|
216 | 201 | #------------------------------------------------------------------------- |
|
217 | 202 | |
|
218 | 203 | def init_handlers(self): |
|
219 | 204 | """Create the default handlers.""" |
|
220 | 205 | self._handlers = {} |
|
221 | 206 | self._esc_handlers = {} |
|
222 | 207 | for handler in _default_handlers: |
|
223 | 208 | handler( |
|
224 | 209 | shell=self.shell, prefilter_manager=self, parent=self |
|
225 | 210 | ) |
|
226 | 211 | |
|
227 | 212 | @property |
|
228 | 213 | def handlers(self): |
|
229 | 214 | """Return a dict of all the handlers.""" |
|
230 | 215 | return self._handlers |
|
231 | 216 | |
|
232 | 217 | def register_handler(self, name, handler, esc_strings): |
|
233 | 218 | """Register a handler instance by name with esc_strings.""" |
|
234 | 219 | self._handlers[name] = handler |
|
235 | 220 | for esc_str in esc_strings: |
|
236 | 221 | self._esc_handlers[esc_str] = handler |
|
237 | 222 | |
|
238 | 223 | def unregister_handler(self, name, handler, esc_strings): |
|
239 | 224 | """Unregister a handler instance by name with esc_strings.""" |
|
240 | 225 | try: |
|
241 | 226 | del self._handlers[name] |
|
242 | 227 | except KeyError: |
|
243 | 228 | pass |
|
244 | 229 | for esc_str in esc_strings: |
|
245 | 230 | h = self._esc_handlers.get(esc_str) |
|
246 | 231 | if h is handler: |
|
247 | 232 | del self._esc_handlers[esc_str] |
|
248 | 233 | |
|
249 | 234 | def get_handler_by_name(self, name): |
|
250 | 235 | """Get a handler by its name.""" |
|
251 | 236 | return self._handlers.get(name) |
|
252 | 237 | |
|
253 | 238 | def get_handler_by_esc(self, esc_str): |
|
254 | 239 | """Get a handler by its escape string.""" |
|
255 | 240 | return self._esc_handlers.get(esc_str) |
|
256 | 241 | |
|
257 | 242 | #------------------------------------------------------------------------- |
|
258 | 243 | # Main prefiltering API |
|
259 | 244 | #------------------------------------------------------------------------- |
|
260 | 245 | |
|
261 | 246 | def prefilter_line_info(self, line_info): |
|
262 | 247 | """Prefilter a line that has been converted to a LineInfo object. |
|
263 | 248 | |
|
264 | 249 | This implements the checker/handler part of the prefilter pipe. |
|
265 | 250 | """ |
|
266 | 251 | # print "prefilter_line_info: ", line_info |
|
267 | 252 | handler = self.find_handler(line_info) |
|
268 | 253 | return handler.handle(line_info) |
|
269 | 254 | |
|
270 | 255 | def find_handler(self, line_info): |
|
271 | 256 | """Find a handler for the line_info by trying checkers.""" |
|
272 | 257 | for checker in self.checkers: |
|
273 | 258 | if checker.enabled: |
|
274 | 259 | handler = checker.check(line_info) |
|
275 | 260 | if handler: |
|
276 | 261 | return handler |
|
277 | 262 | return self.get_handler_by_name('normal') |
|
278 | 263 | |
|
279 | 264 | def transform_line(self, line, continue_prompt): |
|
280 | 265 | """Calls the enabled transformers in order of increasing priority.""" |
|
281 | 266 | for transformer in self.transformers: |
|
282 | 267 | if transformer.enabled: |
|
283 | 268 | line = transformer.transform(line, continue_prompt) |
|
284 | 269 | return line |
|
285 | 270 | |
|
286 | 271 | def prefilter_line(self, line, continue_prompt=False): |
|
287 | 272 | """Prefilter a single input line as text. |
|
288 | 273 | |
|
289 | 274 | This method prefilters a single line of text by calling the |
|
290 | 275 | transformers and then the checkers/handlers. |
|
291 | 276 | """ |
|
292 | 277 | |
|
293 | 278 | # print "prefilter_line: ", line, continue_prompt |
|
294 | 279 | # All handlers *must* return a value, even if it's blank (''). |
|
295 | 280 | |
|
296 | 281 | # save the line away in case we crash, so the post-mortem handler can |
|
297 | 282 | # record it |
|
298 | 283 | self.shell._last_input_line = line |
|
299 | 284 | |
|
300 | 285 | if not line: |
|
301 | 286 | # Return immediately on purely empty lines, so that if the user |
|
302 | 287 | # previously typed some whitespace that started a continuation |
|
303 | 288 | # prompt, he can break out of that loop with just an empty line. |
|
304 | 289 | # This is how the default python prompt works. |
|
305 | 290 | return '' |
|
306 | 291 | |
|
307 | 292 | # At this point, we invoke our transformers. |
|
308 | 293 | if not continue_prompt or (continue_prompt and self.multi_line_specials): |
|
309 | 294 | line = self.transform_line(line, continue_prompt) |
|
310 | 295 | |
|
311 | 296 | # Now we compute line_info for the checkers and handlers |
|
312 | 297 | line_info = LineInfo(line, continue_prompt) |
|
313 | 298 | |
|
314 | 299 | # the input history needs to track even empty lines |
|
315 | 300 | stripped = line.strip() |
|
316 | 301 | |
|
317 | 302 | normal_handler = self.get_handler_by_name('normal') |
|
318 | 303 | if not stripped: |
|
319 | 304 | return normal_handler.handle(line_info) |
|
320 | 305 | |
|
321 | 306 | # special handlers are only allowed for single line statements |
|
322 | 307 | if continue_prompt and not self.multi_line_specials: |
|
323 | 308 | return normal_handler.handle(line_info) |
|
324 | 309 | |
|
325 | 310 | prefiltered = self.prefilter_line_info(line_info) |
|
326 | 311 | # print "prefiltered line: %r" % prefiltered |
|
327 | 312 | return prefiltered |
|
328 | 313 | |
|
329 | 314 | def prefilter_lines(self, lines, continue_prompt=False): |
|
330 | 315 | """Prefilter multiple input lines of text. |
|
331 | 316 | |
|
332 | 317 | This is the main entry point for prefiltering multiple lines of |
|
333 | 318 | input. This simply calls :meth:`prefilter_line` for each line of |
|
334 | 319 | input. |
|
335 | 320 | |
|
336 | 321 | This covers cases where there are multiple lines in the user entry, |
|
337 | 322 | which is the case when the user goes back to a multiline history |
|
338 | 323 | entry and presses enter. |
|
339 | 324 | """ |
|
340 | 325 | llines = lines.rstrip('\n').split('\n') |
|
341 | 326 | # We can get multiple lines in one shot, where multiline input 'blends' |
|
342 | 327 | # into one line, in cases like recalling from the readline history |
|
343 | 328 | # buffer. We need to make sure that in such cases, we correctly |
|
344 | 329 | # communicate downstream which line is first and which are continuation |
|
345 | 330 | # ones. |
|
346 | 331 | if len(llines) > 1: |
|
347 | 332 | out = '\n'.join([self.prefilter_line(line, lnum>0) |
|
348 | 333 | for lnum, line in enumerate(llines) ]) |
|
349 | 334 | else: |
|
350 | 335 | out = self.prefilter_line(llines[0], continue_prompt) |
|
351 | 336 | |
|
352 | 337 | return out |
|
353 | 338 | |
|
354 | 339 | #----------------------------------------------------------------------------- |
|
355 | 340 | # Prefilter transformers |
|
356 | 341 | #----------------------------------------------------------------------------- |
|
357 | 342 | |
|
358 | 343 | |
|
359 | 344 | class PrefilterTransformer(Configurable): |
|
360 | 345 | """Transform a line of user input.""" |
|
361 | 346 | |
|
362 | 347 | priority = Integer(100).tag(config=True) |
|
363 | 348 | # Transformers don't currently use shell or prefilter_manager, but as we |
|
364 | 349 | # move away from checkers and handlers, they will need them. |
|
365 | 350 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
366 | 351 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
367 | 352 | enabled = Bool(True).tag(config=True) |
|
368 | 353 | |
|
369 | 354 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
370 | 355 | super(PrefilterTransformer, self).__init__( |
|
371 | 356 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
372 | 357 | ) |
|
373 | 358 | self.prefilter_manager.register_transformer(self) |
|
374 | 359 | |
|
375 | 360 | def transform(self, line, continue_prompt): |
|
376 | 361 | """Transform a line, returning the new one.""" |
|
377 | 362 | return None |
|
378 | 363 | |
|
379 | 364 | def __repr__(self): |
|
380 | 365 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
381 | 366 | self.__class__.__name__, self.priority, self.enabled) |
|
382 | 367 | |
|
383 | 368 | |
|
384 | 369 | #----------------------------------------------------------------------------- |
|
385 | 370 | # Prefilter checkers |
|
386 | 371 | #----------------------------------------------------------------------------- |
|
387 | 372 | |
|
388 | 373 | |
|
389 | 374 | class PrefilterChecker(Configurable): |
|
390 | 375 | """Inspect an input line and return a handler for that line.""" |
|
391 | 376 | |
|
392 | 377 | priority = Integer(100).tag(config=True) |
|
393 | 378 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
394 | 379 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
395 | 380 | enabled = Bool(True).tag(config=True) |
|
396 | 381 | |
|
397 | 382 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
398 | 383 | super(PrefilterChecker, self).__init__( |
|
399 | 384 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
400 | 385 | ) |
|
401 | 386 | self.prefilter_manager.register_checker(self) |
|
402 | 387 | |
|
403 | 388 | def check(self, line_info): |
|
404 | 389 | """Inspect line_info and return a handler instance or None.""" |
|
405 | 390 | return None |
|
406 | 391 | |
|
407 | 392 | def __repr__(self): |
|
408 | 393 | return "<%s(priority=%r, enabled=%r)>" % ( |
|
409 | 394 | self.__class__.__name__, self.priority, self.enabled) |
|
410 | 395 | |
|
411 | 396 | |
|
412 | 397 | class EmacsChecker(PrefilterChecker): |
|
413 | 398 | |
|
414 | 399 | priority = Integer(100).tag(config=True) |
|
415 | 400 | enabled = Bool(False).tag(config=True) |
|
416 | 401 | |
|
417 | 402 | def check(self, line_info): |
|
418 | 403 | "Emacs ipython-mode tags certain input lines." |
|
419 | 404 | if line_info.line.endswith('# PYTHON-MODE'): |
|
420 | 405 | return self.prefilter_manager.get_handler_by_name('emacs') |
|
421 | 406 | else: |
|
422 | 407 | return None |
|
423 | 408 | |
|
424 | 409 | |
|
425 | 410 | class MacroChecker(PrefilterChecker): |
|
426 | 411 | |
|
427 | 412 | priority = Integer(250).tag(config=True) |
|
428 | 413 | |
|
429 | 414 | def check(self, line_info): |
|
430 | 415 | obj = self.shell.user_ns.get(line_info.ifun) |
|
431 | 416 | if isinstance(obj, Macro): |
|
432 | 417 | return self.prefilter_manager.get_handler_by_name('macro') |
|
433 | 418 | else: |
|
434 | 419 | return None |
|
435 | 420 | |
|
436 | 421 | |
|
437 | 422 | class IPyAutocallChecker(PrefilterChecker): |
|
438 | 423 | |
|
439 | 424 | priority = Integer(300).tag(config=True) |
|
440 | 425 | |
|
441 | 426 | def check(self, line_info): |
|
442 | 427 | "Instances of IPyAutocall in user_ns get autocalled immediately" |
|
443 | 428 | obj = self.shell.user_ns.get(line_info.ifun, None) |
|
444 | 429 | if isinstance(obj, IPyAutocall): |
|
445 | 430 | obj.set_ip(self.shell) |
|
446 | 431 | return self.prefilter_manager.get_handler_by_name('auto') |
|
447 | 432 | else: |
|
448 | 433 | return None |
|
449 | 434 | |
|
450 | 435 | |
|
451 | 436 | class AssignmentChecker(PrefilterChecker): |
|
452 | 437 | |
|
453 | 438 | priority = Integer(600).tag(config=True) |
|
454 | 439 | |
|
455 | 440 | def check(self, line_info): |
|
456 | 441 | """Check to see if user is assigning to a var for the first time, in |
|
457 | 442 | which case we want to avoid any sort of automagic / autocall games. |
|
458 | 443 | |
|
459 | 444 | This allows users to assign to either alias or magic names true python |
|
460 | 445 | variables (the magic/alias systems always take second seat to true |
|
461 | 446 | python code). E.g. ls='hi', or ls,that=1,2""" |
|
462 | 447 | if line_info.the_rest: |
|
463 | 448 | if line_info.the_rest[0] in '=,': |
|
464 | 449 | return self.prefilter_manager.get_handler_by_name('normal') |
|
465 | 450 | else: |
|
466 | 451 | return None |
|
467 | 452 | |
|
468 | 453 | |
|
469 | 454 | class AutoMagicChecker(PrefilterChecker): |
|
470 | 455 | |
|
471 | 456 | priority = Integer(700).tag(config=True) |
|
472 | 457 | |
|
473 | 458 | def check(self, line_info): |
|
474 | 459 | """If the ifun is magic, and automagic is on, run it. Note: normal, |
|
475 | 460 | non-auto magic would already have been triggered via '%' in |
|
476 | 461 | check_esc_chars. This just checks for automagic. Also, before |
|
477 | 462 | triggering the magic handler, make sure that there is nothing in the |
|
478 | 463 | user namespace which could shadow it.""" |
|
479 | 464 | if not self.shell.automagic or not self.shell.find_magic(line_info.ifun): |
|
480 | 465 | return None |
|
481 | 466 | |
|
482 | 467 | # We have a likely magic method. Make sure we should actually call it. |
|
483 | 468 | if line_info.continue_prompt and not self.prefilter_manager.multi_line_specials: |
|
484 | 469 | return None |
|
485 | 470 | |
|
486 | 471 | head = line_info.ifun.split('.',1)[0] |
|
487 | 472 | if is_shadowed(head, self.shell): |
|
488 | 473 | return None |
|
489 | 474 | |
|
490 | 475 | return self.prefilter_manager.get_handler_by_name('magic') |
|
491 | 476 | |
|
492 | 477 | |
|
493 | 478 | class PythonOpsChecker(PrefilterChecker): |
|
494 | 479 | |
|
495 | 480 | priority = Integer(900).tag(config=True) |
|
496 | 481 | |
|
497 | 482 | def check(self, line_info): |
|
498 | 483 | """If the 'rest' of the line begins with a function call or pretty much |
|
499 | 484 | any python operator, we should simply execute the line (regardless of |
|
500 | 485 | whether or not there's a possible autocall expansion). This avoids |
|
501 | 486 | spurious (and very confusing) geattr() accesses.""" |
|
502 | 487 | if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|': |
|
503 | 488 | return self.prefilter_manager.get_handler_by_name('normal') |
|
504 | 489 | else: |
|
505 | 490 | return None |
|
506 | 491 | |
|
507 | 492 | |
|
508 | 493 | class AutocallChecker(PrefilterChecker): |
|
509 | 494 | |
|
510 | 495 | priority = Integer(1000).tag(config=True) |
|
511 | 496 | |
|
512 | 497 | function_name_regexp = CRegExp(re_fun_name, |
|
513 | 498 | help="RegExp to identify potential function names." |
|
514 | 499 | ).tag(config=True) |
|
515 | 500 | exclude_regexp = CRegExp(re_exclude_auto, |
|
516 | 501 | help="RegExp to exclude strings with this start from autocalling." |
|
517 | 502 | ).tag(config=True) |
|
518 | 503 | |
|
519 | 504 | def check(self, line_info): |
|
520 | 505 | "Check if the initial word/function is callable and autocall is on." |
|
521 | 506 | if not self.shell.autocall: |
|
522 | 507 | return None |
|
523 | 508 | |
|
524 | 509 | oinfo = line_info.ofind(self.shell) # This can mutate state via getattr |
|
525 | 510 | if not oinfo['found']: |
|
526 | 511 | return None |
|
527 | 512 | |
|
528 | 513 | if callable(oinfo['obj']) \ |
|
529 | 514 | and (not self.exclude_regexp.match(line_info.the_rest)) \ |
|
530 | 515 | and self.function_name_regexp.match(line_info.ifun): |
|
531 | 516 | return self.prefilter_manager.get_handler_by_name('auto') |
|
532 | 517 | else: |
|
533 | 518 | return None |
|
534 | 519 | |
|
535 | 520 | |
|
536 | 521 | #----------------------------------------------------------------------------- |
|
537 | 522 | # Prefilter handlers |
|
538 | 523 | #----------------------------------------------------------------------------- |
|
539 | 524 | |
|
540 | 525 | |
|
541 | 526 | class PrefilterHandler(Configurable): |
|
542 | 527 | |
|
543 | 528 | handler_name = Unicode('normal') |
|
544 | 529 | esc_strings = List([]) |
|
545 | 530 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
546 | 531 | prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) |
|
547 | 532 | |
|
548 | 533 | def __init__(self, shell=None, prefilter_manager=None, **kwargs): |
|
549 | 534 | super(PrefilterHandler, self).__init__( |
|
550 | 535 | shell=shell, prefilter_manager=prefilter_manager, **kwargs |
|
551 | 536 | ) |
|
552 | 537 | self.prefilter_manager.register_handler( |
|
553 | 538 | self.handler_name, |
|
554 | 539 | self, |
|
555 | 540 | self.esc_strings |
|
556 | 541 | ) |
|
557 | 542 | |
|
558 | 543 | def handle(self, line_info): |
|
559 | 544 | # print "normal: ", line_info |
|
560 | 545 | """Handle normal input lines. Use as a template for handlers.""" |
|
561 | 546 | |
|
562 | 547 | # With autoindent on, we need some way to exit the input loop, and I |
|
563 | 548 | # don't want to force the user to have to backspace all the way to |
|
564 | 549 | # clear the line. The rule will be in this case, that either two |
|
565 | 550 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
566 | 551 | # of a size different to the indent level, will exit the input loop. |
|
567 | 552 | line = line_info.line |
|
568 | 553 | continue_prompt = line_info.continue_prompt |
|
569 | 554 | |
|
570 | 555 | if (continue_prompt and |
|
571 | 556 | self.shell.autoindent and |
|
572 | 557 | line.isspace() and |
|
573 | 558 | 0 < abs(len(line) - self.shell.indent_current_nsp) <= 2): |
|
574 | 559 | line = '' |
|
575 | 560 | |
|
576 | 561 | return line |
|
577 | 562 | |
|
578 | 563 | def __str__(self): |
|
579 | 564 | return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name) |
|
580 | 565 | |
|
581 | 566 | |
|
582 | 567 | class MacroHandler(PrefilterHandler): |
|
583 | 568 | handler_name = Unicode("macro") |
|
584 | 569 | |
|
585 | 570 | def handle(self, line_info): |
|
586 | 571 | obj = self.shell.user_ns.get(line_info.ifun) |
|
587 | 572 | pre_space = line_info.pre_whitespace |
|
588 | 573 | line_sep = "\n" + pre_space |
|
589 | 574 | return pre_space + line_sep.join(obj.value.splitlines()) |
|
590 | 575 | |
|
591 | 576 | |
|
592 | 577 | class MagicHandler(PrefilterHandler): |
|
593 | 578 | |
|
594 | 579 | handler_name = Unicode('magic') |
|
595 | 580 | esc_strings = List([ESC_MAGIC]) |
|
596 | 581 | |
|
597 | 582 | def handle(self, line_info): |
|
598 | 583 | """Execute magic functions.""" |
|
599 | 584 | ifun = line_info.ifun |
|
600 | 585 | the_rest = line_info.the_rest |
|
601 | 586 | cmd = '%sget_ipython().magic(%r)' % (line_info.pre_whitespace, |
|
602 | 587 | (ifun + " " + the_rest)) |
|
603 | 588 | return cmd |
|
604 | 589 | |
|
605 | 590 | |
|
606 | 591 | class AutoHandler(PrefilterHandler): |
|
607 | 592 | |
|
608 | 593 | handler_name = Unicode('auto') |
|
609 | 594 | esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2]) |
|
610 | 595 | |
|
611 | 596 | def handle(self, line_info): |
|
612 | 597 | """Handle lines which can be auto-executed, quoting if requested.""" |
|
613 | 598 | line = line_info.line |
|
614 | 599 | ifun = line_info.ifun |
|
615 | 600 | the_rest = line_info.the_rest |
|
616 | 601 | esc = line_info.esc |
|
617 | 602 | continue_prompt = line_info.continue_prompt |
|
618 | 603 | obj = line_info.ofind(self.shell)['obj'] |
|
619 | 604 | |
|
620 | 605 | # This should only be active for single-line input! |
|
621 | 606 | if continue_prompt: |
|
622 | 607 | return line |
|
623 | 608 | |
|
624 | 609 | force_auto = isinstance(obj, IPyAutocall) |
|
625 | 610 | |
|
626 | 611 | # User objects sometimes raise exceptions on attribute access other |
|
627 | 612 | # than AttributeError (we've seen it in the past), so it's safest to be |
|
628 | 613 | # ultra-conservative here and catch all. |
|
629 | 614 | try: |
|
630 | 615 | auto_rewrite = obj.rewrite |
|
631 | 616 | except Exception: |
|
632 | 617 | auto_rewrite = True |
|
633 | 618 | |
|
634 | 619 | if esc == ESC_QUOTE: |
|
635 | 620 | # Auto-quote splitting on whitespace |
|
636 | 621 | newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) ) |
|
637 | 622 | elif esc == ESC_QUOTE2: |
|
638 | 623 | # Auto-quote whole string |
|
639 | 624 | newcmd = '%s("%s")' % (ifun,the_rest) |
|
640 | 625 | elif esc == ESC_PAREN: |
|
641 | 626 | newcmd = '%s(%s)' % (ifun,",".join(the_rest.split())) |
|
642 | 627 | else: |
|
643 | 628 | # Auto-paren. |
|
644 | 629 | if force_auto: |
|
645 | 630 | # Don't rewrite if it is already a call. |
|
646 | 631 | do_rewrite = not the_rest.startswith('(') |
|
647 | 632 | else: |
|
648 | 633 | if not the_rest: |
|
649 | 634 | # We only apply it to argument-less calls if the autocall |
|
650 | 635 | # parameter is set to 2. |
|
651 | 636 | do_rewrite = (self.shell.autocall >= 2) |
|
652 | 637 | elif the_rest.startswith('[') and hasattr(obj, '__getitem__'): |
|
653 | 638 | # Don't autocall in this case: item access for an object |
|
654 | 639 | # which is BOTH callable and implements __getitem__. |
|
655 | 640 | do_rewrite = False |
|
656 | 641 | else: |
|
657 | 642 | do_rewrite = True |
|
658 | 643 | |
|
659 | 644 | # Figure out the rewritten command |
|
660 | 645 | if do_rewrite: |
|
661 | 646 | if the_rest.endswith(';'): |
|
662 | 647 | newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1]) |
|
663 | 648 | else: |
|
664 | 649 | newcmd = '%s(%s)' % (ifun.rstrip(), the_rest) |
|
665 | 650 | else: |
|
666 | 651 | normal_handler = self.prefilter_manager.get_handler_by_name('normal') |
|
667 | 652 | return normal_handler.handle(line_info) |
|
668 | 653 | |
|
669 | 654 | # Display the rewritten call |
|
670 | 655 | if auto_rewrite: |
|
671 | 656 | self.shell.auto_rewrite_input(newcmd) |
|
672 | 657 | |
|
673 | 658 | return newcmd |
|
674 | 659 | |
|
675 | 660 | |
|
676 | 661 | class EmacsHandler(PrefilterHandler): |
|
677 | 662 | |
|
678 | 663 | handler_name = Unicode('emacs') |
|
679 | 664 | esc_strings = List([]) |
|
680 | 665 | |
|
681 | 666 | def handle(self, line_info): |
|
682 | 667 | """Handle input lines marked by python-mode.""" |
|
683 | 668 | |
|
684 | 669 | # Currently, nothing is done. Later more functionality can be added |
|
685 | 670 | # here if needed. |
|
686 | 671 | |
|
687 | 672 | # The input cache shouldn't be updated |
|
688 | 673 | return line_info.line |
|
689 | 674 | |
|
690 | 675 | |
|
691 | 676 | #----------------------------------------------------------------------------- |
|
692 | 677 | # Defaults |
|
693 | 678 | #----------------------------------------------------------------------------- |
|
694 | 679 | |
|
695 | 680 | |
|
696 | 681 | _default_transformers = [ |
|
697 | 682 | ] |
|
698 | 683 | |
|
699 | 684 | _default_checkers = [ |
|
700 | 685 | EmacsChecker, |
|
701 | 686 | MacroChecker, |
|
702 | 687 | IPyAutocallChecker, |
|
703 | 688 | AssignmentChecker, |
|
704 | 689 | AutoMagicChecker, |
|
705 | 690 | PythonOpsChecker, |
|
706 | 691 | AutocallChecker |
|
707 | 692 | ] |
|
708 | 693 | |
|
709 | 694 | _default_handlers = [ |
|
710 | 695 | PrefilterHandler, |
|
711 | 696 | MacroHandler, |
|
712 | 697 | MagicHandler, |
|
713 | 698 | AutoHandler, |
|
714 | 699 | EmacsHandler |
|
715 | 700 | ] |
@@ -1,234 +1,224 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 | 12 | from IPython.paths import get_ipython_package_dir |
|
13 | 13 | from IPython.utils.path import expand_path, ensure_dir_exists |
|
14 | 14 | from IPython.utils import py3compat |
|
15 | from traitlets import Unicode, Bool | |
|
15 | from traitlets import Unicode, Bool, observe | |
|
16 | 16 | |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | # Module errors |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | class ProfileDirError(Exception): |
|
22 | 22 | pass |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Class for managing profile directories |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | class ProfileDir(LoggingConfigurable): |
|
30 | 30 | """An object to manage the profile directory and its resources. |
|
31 | 31 | |
|
32 | 32 | The profile directory is used by all IPython applications, to manage |
|
33 | 33 | configuration, logging and security. |
|
34 | 34 | |
|
35 | 35 | This object knows how to find, create and manage these directories. This |
|
36 | 36 | should be used by any code that wants to handle profiles. |
|
37 | 37 | """ |
|
38 | 38 | |
|
39 | 39 | security_dir_name = Unicode('security') |
|
40 | 40 | log_dir_name = Unicode('log') |
|
41 | 41 | startup_dir_name = Unicode('startup') |
|
42 | 42 | pid_dir_name = Unicode('pid') |
|
43 | 43 | static_dir_name = Unicode('static') |
|
44 | 44 | security_dir = Unicode(u'') |
|
45 | 45 | log_dir = Unicode(u'') |
|
46 | 46 | startup_dir = Unicode(u'') |
|
47 | 47 | pid_dir = Unicode(u'') |
|
48 | 48 | static_dir = Unicode(u'') |
|
49 | 49 | |
|
50 |
location = Unicode(u'', |
|
|
50 | location = Unicode(u'', | |
|
51 | 51 | help="""Set the profile location directly. This overrides the logic used by the |
|
52 | 52 | `profile` option.""", |
|
53 | ) | |
|
53 | ).tag(config=True) | |
|
54 | 54 | |
|
55 | 55 | _location_isset = Bool(False) # flag for detecting multiply set location |
|
56 | ||
|
57 |
def _location_changed(self, |
|
|
56 | @observe('location') | |
|
57 | def _location_changed(self, change): | |
|
58 | 58 | if self._location_isset: |
|
59 | 59 | raise RuntimeError("Cannot set profile location more than once.") |
|
60 | 60 | self._location_isset = True |
|
61 | new = change['new'] | |
|
61 | 62 | ensure_dir_exists(new) |
|
62 | 63 | |
|
63 | 64 | # ensure config files exist: |
|
64 | 65 | self.security_dir = os.path.join(new, self.security_dir_name) |
|
65 | 66 | self.log_dir = os.path.join(new, self.log_dir_name) |
|
66 | 67 | self.startup_dir = os.path.join(new, self.startup_dir_name) |
|
67 | 68 | self.pid_dir = os.path.join(new, self.pid_dir_name) |
|
68 | 69 | self.static_dir = os.path.join(new, self.static_dir_name) |
|
69 | 70 | self.check_dirs() |
|
70 | ||
|
71 | def _log_dir_changed(self, name, old, new): | |
|
72 | self.check_log_dir() | |
|
73 | ||
|
71 | ||
|
74 | 72 | def _mkdir(self, path, mode=None): |
|
75 | 73 | """ensure a directory exists at a given path |
|
76 | 74 | |
|
77 | 75 | This is a version of os.mkdir, with the following differences: |
|
78 | 76 | |
|
79 | 77 | - returns True if it created the directory, False otherwise |
|
80 | 78 | - ignores EEXIST, protecting against race conditions where |
|
81 | 79 | the dir may have been created in between the check and |
|
82 | 80 | the creation |
|
83 | 81 | - sets permissions if requested and the dir already exists |
|
84 | 82 | """ |
|
85 | 83 | if os.path.exists(path): |
|
86 | 84 | if mode and os.stat(path).st_mode != mode: |
|
87 | 85 | try: |
|
88 | 86 | os.chmod(path, mode) |
|
89 | 87 | except OSError: |
|
90 | 88 | self.log.warning( |
|
91 | 89 | "Could not set permissions on %s", |
|
92 | 90 | path |
|
93 | 91 | ) |
|
94 | 92 | return False |
|
95 | 93 | try: |
|
96 | 94 | if mode: |
|
97 | 95 | os.mkdir(path, mode) |
|
98 | 96 | else: |
|
99 | 97 | os.mkdir(path) |
|
100 | 98 | except OSError as e: |
|
101 | 99 | if e.errno == errno.EEXIST: |
|
102 | 100 | return False |
|
103 | 101 | else: |
|
104 | 102 | raise |
|
105 | 103 | |
|
106 | 104 | return True |
|
107 | ||
|
108 | def check_log_dir(self): | |
|
105 | ||
|
106 | @observe('log_dir') | |
|
107 | def check_log_dir(self, change=None): | |
|
109 | 108 | self._mkdir(self.log_dir) |
|
110 | ||
|
111 | def _startup_dir_changed(self, name, old, new): | |
|
112 |
|
|
|
113 | ||
|
114 | def check_startup_dir(self): | |
|
109 | ||
|
110 | @observe('startup_dir') | |
|
111 | def check_startup_dir(self, change=None): | |
|
115 | 112 | self._mkdir(self.startup_dir) |
|
116 | 113 | |
|
117 | 114 | readme = os.path.join(self.startup_dir, 'README') |
|
118 | 115 | src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') |
|
119 | 116 | |
|
120 | 117 | if not os.path.exists(src): |
|
121 | 118 | self.log.warning("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) |
|
122 | 119 | |
|
123 | 120 | if os.path.exists(src) and not os.path.exists(readme): |
|
124 | 121 | shutil.copy(src, readme) |
|
125 | 122 | |
|
126 | def _security_dir_changed(self, name, old, new): | |
|
127 |
|
|
|
128 | ||
|
129 | def check_security_dir(self): | |
|
123 | @observe('security_dir') | |
|
124 | def check_security_dir(self, change=None): | |
|
130 | 125 | self._mkdir(self.security_dir, 0o40700) |
|
131 | 126 | |
|
132 | def _pid_dir_changed(self, name, old, new): | |
|
133 | self.check_pid_dir() | |
|
134 | ||
|
135 | def check_pid_dir(self): | |
|
127 | @observe('pid_dir') | |
|
128 | def check_pid_dir(self, change=None): | |
|
136 | 129 | self._mkdir(self.pid_dir, 0o40700) |
|
137 | 130 | |
|
138 | def _static_dir_changed(self, name, old, new): | |
|
139 | self.check_startup_dir() | |
|
140 | ||
|
141 | 131 | def check_dirs(self): |
|
142 | 132 | self.check_security_dir() |
|
143 | 133 | self.check_log_dir() |
|
144 | 134 | self.check_pid_dir() |
|
145 | 135 | self.check_startup_dir() |
|
146 | 136 | |
|
147 | 137 | def copy_config_file(self, config_file, path=None, overwrite=False): |
|
148 | 138 | """Copy a default config file into the active profile directory. |
|
149 | 139 | |
|
150 | 140 | Default configuration files are kept in :mod:`IPython.core.profile`. |
|
151 | 141 | This function moves these from that location to the working profile |
|
152 | 142 | directory. |
|
153 | 143 | """ |
|
154 | 144 | dst = os.path.join(self.location, config_file) |
|
155 | 145 | if os.path.isfile(dst) and not overwrite: |
|
156 | 146 | return False |
|
157 | 147 | if path is None: |
|
158 | 148 | path = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'default') |
|
159 | 149 | src = os.path.join(path, config_file) |
|
160 | 150 | shutil.copy(src, dst) |
|
161 | 151 | return True |
|
162 | 152 | |
|
163 | 153 | @classmethod |
|
164 | 154 | def create_profile_dir(cls, profile_dir, config=None): |
|
165 | 155 | """Create a new profile directory given a full path. |
|
166 | 156 | |
|
167 | 157 | Parameters |
|
168 | 158 | ---------- |
|
169 | 159 | profile_dir : str |
|
170 | 160 | The full path to the profile directory. If it does exist, it will |
|
171 | 161 | be used. If not, it will be created. |
|
172 | 162 | """ |
|
173 | 163 | return cls(location=profile_dir, config=config) |
|
174 | 164 | |
|
175 | 165 | @classmethod |
|
176 | 166 | def create_profile_dir_by_name(cls, path, name=u'default', config=None): |
|
177 | 167 | """Create a profile dir by profile name and path. |
|
178 | 168 | |
|
179 | 169 | Parameters |
|
180 | 170 | ---------- |
|
181 | 171 | path : unicode |
|
182 | 172 | The path (directory) to put the profile directory in. |
|
183 | 173 | name : unicode |
|
184 | 174 | The name of the profile. The name of the profile directory will |
|
185 | 175 | be "profile_<profile>". |
|
186 | 176 | """ |
|
187 | 177 | if not os.path.isdir(path): |
|
188 | 178 | raise ProfileDirError('Directory not found: %s' % path) |
|
189 | 179 | profile_dir = os.path.join(path, u'profile_' + name) |
|
190 | 180 | return cls(location=profile_dir, config=config) |
|
191 | 181 | |
|
192 | 182 | @classmethod |
|
193 | 183 | def find_profile_dir_by_name(cls, ipython_dir, name=u'default', config=None): |
|
194 | 184 | """Find an existing profile dir by profile name, return its ProfileDir. |
|
195 | 185 | |
|
196 | 186 | This searches through a sequence of paths for a profile dir. If it |
|
197 | 187 | is not found, a :class:`ProfileDirError` exception will be raised. |
|
198 | 188 | |
|
199 | 189 | The search path algorithm is: |
|
200 | 190 | 1. ``py3compat.getcwd()`` |
|
201 | 191 | 2. ``ipython_dir`` |
|
202 | 192 | |
|
203 | 193 | Parameters |
|
204 | 194 | ---------- |
|
205 | 195 | ipython_dir : unicode or str |
|
206 | 196 | The IPython directory to use. |
|
207 | 197 | name : unicode or str |
|
208 | 198 | The name of the profile. The name of the profile directory |
|
209 | 199 | will be "profile_<profile>". |
|
210 | 200 | """ |
|
211 | 201 | dirname = u'profile_' + name |
|
212 | 202 | paths = [py3compat.getcwd(), ipython_dir] |
|
213 | 203 | for p in paths: |
|
214 | 204 | profile_dir = os.path.join(p, dirname) |
|
215 | 205 | if os.path.isdir(profile_dir): |
|
216 | 206 | return cls(location=profile_dir, config=config) |
|
217 | 207 | else: |
|
218 | 208 | raise ProfileDirError('Profile directory not found in paths: %s' % dirname) |
|
219 | 209 | |
|
220 | 210 | @classmethod |
|
221 | 211 | def find_profile_dir(cls, profile_dir, config=None): |
|
222 | 212 | """Find/create a profile dir and return its ProfileDir. |
|
223 | 213 | |
|
224 | 214 | This will create the profile directory if it doesn't exist. |
|
225 | 215 | |
|
226 | 216 | Parameters |
|
227 | 217 | ---------- |
|
228 | 218 | profile_dir : unicode or str |
|
229 | 219 | The path of the profile directory. |
|
230 | 220 | """ |
|
231 | 221 | profile_dir = expand_path(profile_dir) |
|
232 | 222 | if not os.path.isdir(profile_dir): |
|
233 | 223 | raise ProfileDirError('Profile directory not found: %s' % profile_dir) |
|
234 | 224 | return cls(location=profile_dir, config=config) |
@@ -1,420 +1,409 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | """Classes for handling input/output prompts. | |
|
2 | """Classes for handling input/output prompts.""" | |
|
3 | 3 | |
|
4 | Authors: | |
|
5 | ||
|
6 | * Fernando Perez | |
|
7 | * Brian Granger | |
|
8 | * Thomas Kluyver | |
|
9 | """ | |
|
10 | ||
|
11 | #----------------------------------------------------------------------------- | |
|
12 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
13 | # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu> | |
|
14 | # | |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
|
16 | # the file COPYING, distributed as part of this software. | |
|
17 | #----------------------------------------------------------------------------- | |
|
18 | ||
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # Imports | |
|
21 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (c) 2001-2007 Fernando Perez <fperez@colorado.edu> | |
|
5 | # Copyright (c) IPython Development Team. | |
|
6 | # Distributed under the terms of the Modified BSD License. | |
|
22 | 7 | |
|
23 | 8 | import os |
|
24 | 9 | import re |
|
25 | 10 | import socket |
|
26 | 11 | import sys |
|
27 | 12 | import time |
|
28 | 13 | |
|
29 | 14 | from string import Formatter |
|
30 | 15 | |
|
31 | 16 | from traitlets.config.configurable import Configurable |
|
32 | 17 | from IPython.core import release |
|
33 | 18 | from IPython.utils import coloransi, py3compat |
|
34 | from traitlets import Unicode, Instance, Dict, Bool, Int, observe | |
|
19 | from traitlets import Unicode, Instance, Dict, Bool, Int, observe, default | |
|
35 | 20 | |
|
36 | 21 | from IPython.utils.PyColorize import LightBGColors, LinuxColors, NoColor |
|
37 | 22 | |
|
38 | 23 | #----------------------------------------------------------------------------- |
|
39 | 24 | # Color schemes for prompts |
|
40 | 25 | #----------------------------------------------------------------------------- |
|
41 | 26 | |
|
42 | 27 | InputColors = coloransi.InputTermColors # just a shorthand |
|
43 | 28 | Colors = coloransi.TermColors # just a shorthand |
|
44 | 29 | |
|
45 | 30 | color_lists = dict(normal=Colors(), inp=InputColors(), nocolor=coloransi.NoColors()) |
|
46 | 31 | |
|
47 | 32 | #----------------------------------------------------------------------------- |
|
48 | 33 | # Utilities |
|
49 | 34 | #----------------------------------------------------------------------------- |
|
50 | 35 | |
|
51 | 36 | class LazyEvaluate(object): |
|
52 | 37 | """This is used for formatting strings with values that need to be updated |
|
53 | 38 | at that time, such as the current time or working directory.""" |
|
54 | 39 | def __init__(self, func, *args, **kwargs): |
|
55 | 40 | self.func = func |
|
56 | 41 | self.args = args |
|
57 | 42 | self.kwargs = kwargs |
|
58 | 43 | |
|
59 | 44 | def __call__(self, **kwargs): |
|
60 | 45 | self.kwargs.update(kwargs) |
|
61 | 46 | return self.func(*self.args, **self.kwargs) |
|
62 | 47 | |
|
63 | 48 | def __str__(self): |
|
64 | 49 | return str(self()) |
|
65 | 50 | |
|
66 | 51 | def __unicode__(self): |
|
67 | 52 | return py3compat.unicode_type(self()) |
|
68 | 53 | |
|
69 | 54 | def __format__(self, format_spec): |
|
70 | 55 | return format(self(), format_spec) |
|
71 | 56 | |
|
72 | 57 | def multiple_replace(dict, text): |
|
73 | 58 | """ Replace in 'text' all occurrences of any key in the given |
|
74 | 59 | dictionary by its corresponding value. Returns the new string.""" |
|
75 | 60 | |
|
76 | 61 | # Function by Xavier Defrang, originally found at: |
|
77 | 62 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 |
|
78 | 63 | |
|
79 | 64 | # Create a regular expression from the dictionary keys |
|
80 | 65 | regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) |
|
81 | 66 | # For each match, look-up corresponding value in dictionary |
|
82 | 67 | return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text) |
|
83 | 68 | |
|
84 | 69 | #----------------------------------------------------------------------------- |
|
85 | 70 | # Special characters that can be used in prompt templates, mainly bash-like |
|
86 | 71 | #----------------------------------------------------------------------------- |
|
87 | 72 | |
|
88 | 73 | # If $HOME isn't defined (Windows), make it an absurd string so that it can |
|
89 | 74 | # never be expanded out into '~'. Basically anything which can never be a |
|
90 | 75 | # reasonable directory name will do, we just want the $HOME -> '~' operation |
|
91 | 76 | # to become a no-op. We pre-compute $HOME here so it's not done on every |
|
92 | 77 | # prompt call. |
|
93 | 78 | |
|
94 | 79 | # FIXME: |
|
95 | 80 | |
|
96 | 81 | # - This should be turned into a class which does proper namespace management, |
|
97 | 82 | # since the prompt specials need to be evaluated in a certain namespace. |
|
98 | 83 | # Currently it's just globals, which need to be managed manually by code |
|
99 | 84 | # below. |
|
100 | 85 | |
|
101 | 86 | # - I also need to split up the color schemes from the prompt specials |
|
102 | 87 | # somehow. I don't have a clean design for that quite yet. |
|
103 | 88 | |
|
104 | 89 | HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")) |
|
105 | 90 | |
|
106 | 91 | # This is needed on FreeBSD, and maybe other systems which symlink /home to |
|
107 | 92 | # /usr/home, but retain the $HOME variable as pointing to /home |
|
108 | 93 | HOME = os.path.realpath(HOME) |
|
109 | 94 | |
|
110 | 95 | # We precompute a few more strings here for the prompt_specials, which are |
|
111 | 96 | # fixed once ipython starts. This reduces the runtime overhead of computing |
|
112 | 97 | # prompt strings. |
|
113 | 98 | USER = py3compat.str_to_unicode(os.environ.get("USER",'')) |
|
114 | 99 | HOSTNAME = py3compat.str_to_unicode(socket.gethostname()) |
|
115 | 100 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] |
|
116 | 101 | |
|
117 | 102 | # IronPython doesn't currently have os.getuid() even if |
|
118 | 103 | # os.name == 'posix'; 2/8/2014 |
|
119 | 104 | ROOT_SYMBOL = "#" if (os.name=='nt' or sys.platform=='cli' or os.getuid()==0) else "$" |
|
120 | 105 | |
|
121 | 106 | prompt_abbreviations = { |
|
122 | 107 | # Prompt/history count |
|
123 | 108 | '%n' : '{color.number}' '{count}' '{color.prompt}', |
|
124 | 109 | r'\#': '{color.number}' '{count}' '{color.prompt}', |
|
125 | 110 | # Just the prompt counter number, WITHOUT any coloring wrappers, so users |
|
126 | 111 | # can get numbers displayed in whatever color they want. |
|
127 | 112 | r'\N': '{count}', |
|
128 | 113 | |
|
129 | 114 | # Prompt/history count, with the actual digits replaced by dots or |
|
130 | 115 | # spaces. Used mainly in continuation prompts (prompt_in2). |
|
131 | 116 | r'\D': '{dots}', |
|
132 | 117 | r'\S': '{spaces}', |
|
133 | 118 | |
|
134 | 119 | # Current time |
|
135 | 120 | r'\T' : '{time}', |
|
136 | 121 | # Current working directory |
|
137 | 122 | r'\w': '{cwd}', |
|
138 | 123 | # Basename of current working directory. |
|
139 | 124 | # (use os.sep to make this portable across OSes) |
|
140 | 125 | r'\W' : '{cwd_last}', |
|
141 | 126 | # These X<N> are an extension to the normal bash prompts. They return |
|
142 | 127 | # N terms of the path, after replacing $HOME with '~' |
|
143 | 128 | r'\X0': '{cwd_x[0]}', |
|
144 | 129 | r'\X1': '{cwd_x[1]}', |
|
145 | 130 | r'\X2': '{cwd_x[2]}', |
|
146 | 131 | r'\X3': '{cwd_x[3]}', |
|
147 | 132 | r'\X4': '{cwd_x[4]}', |
|
148 | 133 | r'\X5': '{cwd_x[5]}', |
|
149 | 134 | # Y<N> are similar to X<N>, but they show '~' if it's the directory |
|
150 | 135 | # N+1 in the list. Somewhat like %cN in tcsh. |
|
151 | 136 | r'\Y0': '{cwd_y[0]}', |
|
152 | 137 | r'\Y1': '{cwd_y[1]}', |
|
153 | 138 | r'\Y2': '{cwd_y[2]}', |
|
154 | 139 | r'\Y3': '{cwd_y[3]}', |
|
155 | 140 | r'\Y4': '{cwd_y[4]}', |
|
156 | 141 | r'\Y5': '{cwd_y[5]}', |
|
157 | 142 | # Hostname up to first . |
|
158 | 143 | r'\h': HOSTNAME_SHORT, |
|
159 | 144 | # Full hostname |
|
160 | 145 | r'\H': HOSTNAME, |
|
161 | 146 | # Username of current user |
|
162 | 147 | r'\u': USER, |
|
163 | 148 | # Escaped '\' |
|
164 | 149 | '\\\\': '\\', |
|
165 | 150 | # Newline |
|
166 | 151 | r'\n': '\n', |
|
167 | 152 | # Carriage return |
|
168 | 153 | r'\r': '\r', |
|
169 | 154 | # Release version |
|
170 | 155 | r'\v': release.version, |
|
171 | 156 | # Root symbol ($ or #) |
|
172 | 157 | r'\$': ROOT_SYMBOL, |
|
173 | 158 | } |
|
174 | 159 | |
|
175 | 160 | #----------------------------------------------------------------------------- |
|
176 | 161 | # More utilities |
|
177 | 162 | #----------------------------------------------------------------------------- |
|
178 | 163 | |
|
179 | 164 | def cwd_filt(depth): |
|
180 | 165 | """Return the last depth elements of the current working directory. |
|
181 | 166 | |
|
182 | 167 | $HOME is always replaced with '~'. |
|
183 | 168 | If depth==0, the full path is returned.""" |
|
184 | 169 | |
|
185 | 170 | cwd = py3compat.getcwd().replace(HOME,"~") |
|
186 | 171 | out = os.sep.join(cwd.split(os.sep)[-depth:]) |
|
187 | 172 | return out or os.sep |
|
188 | 173 | |
|
189 | 174 | def cwd_filt2(depth): |
|
190 | 175 | """Return the last depth elements of the current working directory. |
|
191 | 176 | |
|
192 | 177 | $HOME is always replaced with '~'. |
|
193 | 178 | If depth==0, the full path is returned.""" |
|
194 | 179 | |
|
195 | 180 | full_cwd = py3compat.getcwd() |
|
196 | 181 | cwd = full_cwd.replace(HOME,"~").split(os.sep) |
|
197 | 182 | if '~' in cwd and len(cwd) == depth+1: |
|
198 | 183 | depth += 1 |
|
199 | 184 | drivepart = '' |
|
200 | 185 | if sys.platform == 'win32' and len(cwd) > depth: |
|
201 | 186 | drivepart = os.path.splitdrive(full_cwd)[0] |
|
202 | 187 | out = drivepart + '/'.join(cwd[-depth:]) |
|
203 | 188 | |
|
204 | 189 | return out or os.sep |
|
205 | 190 | |
|
206 | 191 | #----------------------------------------------------------------------------- |
|
207 | 192 | # Prompt classes |
|
208 | 193 | #----------------------------------------------------------------------------- |
|
209 | 194 | |
|
210 | 195 | lazily_evaluate = {'time': LazyEvaluate(time.strftime, "%H:%M:%S"), |
|
211 | 196 | 'cwd': LazyEvaluate(py3compat.getcwd), |
|
212 | 197 | 'cwd_last': LazyEvaluate(lambda: py3compat.getcwd().split(os.sep)[-1]), |
|
213 | 198 | 'cwd_x': [LazyEvaluate(lambda: py3compat.getcwd().replace(HOME,"~"))] +\ |
|
214 | 199 | [LazyEvaluate(cwd_filt, x) for x in range(1,6)], |
|
215 | 200 | 'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)] |
|
216 | 201 | } |
|
217 | 202 | |
|
218 | 203 | def _lenlastline(s): |
|
219 | 204 | """Get the length of the last line. More intelligent than |
|
220 | 205 | len(s.splitlines()[-1]). |
|
221 | 206 | """ |
|
222 | 207 | if not s or s.endswith(('\n', '\r')): |
|
223 | 208 | return 0 |
|
224 | 209 | return len(s.splitlines()[-1]) |
|
225 | 210 | |
|
226 | 211 | |
|
227 | 212 | invisible_chars_re = re.compile('\001[^\001\002]*\002') |
|
228 | 213 | def _invisible_characters(s): |
|
229 | 214 | """ |
|
230 | 215 | Get the number of invisible ANSI characters in s. Invisible characters |
|
231 | 216 | must be delimited by \001 and \002. |
|
232 | 217 | """ |
|
233 | 218 | return _lenlastline(s) - _lenlastline(invisible_chars_re.sub('', s)) |
|
234 | 219 | |
|
235 | 220 | class UserNSFormatter(Formatter): |
|
236 | 221 | """A Formatter that falls back on a shell's user_ns and __builtins__ for name resolution""" |
|
237 | 222 | def __init__(self, shell): |
|
238 | 223 | self.shell = shell |
|
239 | 224 | |
|
240 | 225 | def get_value(self, key, args, kwargs): |
|
241 | 226 | # try regular formatting first: |
|
242 | 227 | try: |
|
243 | 228 | return Formatter.get_value(self, key, args, kwargs) |
|
244 | 229 | except Exception: |
|
245 | 230 | pass |
|
246 | 231 | # next, look in user_ns and builtins: |
|
247 | 232 | for container in (self.shell.user_ns, __builtins__): |
|
248 | 233 | if key in container: |
|
249 | 234 | return container[key] |
|
250 | 235 | # nothing found, put error message in its place |
|
251 | 236 | return "<ERROR: '%s' not found>" % key |
|
252 | 237 | |
|
253 | 238 | |
|
254 | 239 | class PromptManager(Configurable): |
|
255 | 240 | """This is the primary interface for producing IPython's prompts.""" |
|
256 | 241 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) |
|
257 | 242 | |
|
258 | 243 | color_scheme_table = Instance(coloransi.ColorSchemeTable, allow_none=True) |
|
259 | 244 | color_scheme = Unicode('Linux').tag(config=True) |
|
260 | 245 | |
|
261 | 246 | @observe('color_scheme') |
|
262 | 247 | def _color_scheme_changed(self, change): |
|
263 | 248 | self.color_scheme_table.set_active_scheme(change['new']) |
|
264 | 249 | for pname in ['in', 'in2', 'out', 'rewrite']: |
|
265 | 250 | # We need to recalculate the number of invisible characters |
|
266 | 251 | self.update_prompt(pname) |
|
267 | 252 | |
|
268 | 253 | lazy_evaluate_fields = Dict(help=""" |
|
269 | 254 | This maps field names used in the prompt templates to functions which |
|
270 | 255 | will be called when the prompt is rendered. This allows us to include |
|
271 | 256 | things like the current time in the prompts. Functions are only called |
|
272 | 257 | if they are used in the prompt. |
|
273 | 258 | """) |
|
274 | def _lazy_evaluate_fields_default(self): return lazily_evaluate.copy() | |
|
275 | 259 | |
|
276 | 260 | in_template = Unicode('In [\\#]: ', |
|
277 | 261 | help="Input prompt. '\\#' will be transformed to the prompt number" |
|
278 | 262 | ).tag(config=True) |
|
279 | 263 | in2_template = Unicode(' .\\D.: ', |
|
280 | 264 | help="Continuation prompt.").tag(config=True) |
|
281 | 265 | out_template = Unicode('Out[\\#]: ', |
|
282 | 266 | help="Output prompt. '\\#' will be transformed to the prompt number" |
|
283 | 267 | ).tag(config=True) |
|
284 | 268 | |
|
269 | @default('lazy_evaluate_fields') | |
|
270 | def _lazy_evaluate_fields_default(self): | |
|
271 | return lazily_evaluate.copy() | |
|
272 | ||
|
285 | 273 | justify = Bool(True, help=""" |
|
286 | 274 | If True (default), each prompt will be right-aligned with the |
|
287 | 275 | preceding one. |
|
288 | 276 | """).tag(config=True) |
|
289 | 277 | |
|
290 | 278 | # We actually store the expanded templates here: |
|
291 | 279 | templates = Dict() |
|
292 | 280 | |
|
293 | 281 | # The number of characters in the last prompt rendered, not including |
|
294 | 282 | # colour characters. |
|
295 | 283 | width = Int() |
|
296 | 284 | txtwidth = Int() # Not including right-justification |
|
297 | 285 | |
|
298 | 286 | # The number of characters in each prompt which don't contribute to width |
|
299 | 287 | invisible_chars = Dict() |
|
288 | @default('invisible_chars') | |
|
300 | 289 | def _invisible_chars_default(self): |
|
301 | 290 | return {'in': 0, 'in2': 0, 'out': 0, 'rewrite':0} |
|
302 | 291 | |
|
303 | 292 | def __init__(self, shell, **kwargs): |
|
304 | 293 | super(PromptManager, self).__init__(shell=shell, **kwargs) |
|
305 | 294 | |
|
306 | 295 | # Prepare colour scheme table |
|
307 | 296 | self.color_scheme_table = coloransi.ColorSchemeTable([NoColor, |
|
308 | 297 | LinuxColors, LightBGColors], self.color_scheme) |
|
309 | 298 | |
|
310 | 299 | self._formatter = UserNSFormatter(shell) |
|
311 | 300 | # Prepare templates & numbers of invisible characters |
|
312 | 301 | self.update_prompt('in', self.in_template) |
|
313 | 302 | self.update_prompt('in2', self.in2_template) |
|
314 | 303 | self.update_prompt('out', self.out_template) |
|
315 | 304 | self.update_prompt('rewrite') |
|
316 |
self.o |
|
|
317 |
|
|
|
305 | self.observe(self._update_prompt_trait, | |
|
306 | names=['in_template', 'in2_template', 'out_template']) | |
|
318 | 307 | |
|
319 | 308 | def update_prompt(self, name, new_template=None): |
|
320 | 309 | """This is called when a prompt template is updated. It processes |
|
321 | 310 | abbreviations used in the prompt template (like \#) and calculates how |
|
322 | 311 | many invisible characters (ANSI colour escapes) the resulting prompt |
|
323 | 312 | contains. |
|
324 | 313 | |
|
325 | 314 | It is also called for each prompt on changing the colour scheme. In both |
|
326 | 315 | cases, traitlets should take care of calling this automatically. |
|
327 | 316 | """ |
|
328 | 317 | if new_template is not None: |
|
329 | 318 | self.templates[name] = multiple_replace(prompt_abbreviations, new_template) |
|
330 | 319 | # We count invisible characters (colour escapes) on the last line of the |
|
331 | 320 | # prompt, to calculate the width for lining up subsequent prompts. |
|
332 | 321 | invis_chars = _invisible_characters(self._render(name, color=True)) |
|
333 | 322 | self.invisible_chars[name] = invis_chars |
|
334 | 323 | |
|
335 | 324 | def _update_prompt_trait(self, traitname, new_template): |
|
336 | 325 | name = traitname[:-9] # Cut off '_template' |
|
337 | 326 | self.update_prompt(name, new_template) |
|
338 | 327 | |
|
339 | 328 | def _render(self, name, color=True, **kwargs): |
|
340 | 329 | """Render but don't justify, or update the width or txtwidth attributes. |
|
341 | 330 | """ |
|
342 | 331 | if name == 'rewrite': |
|
343 | 332 | return self._render_rewrite(color=color) |
|
344 | 333 | |
|
345 | 334 | if color: |
|
346 | 335 | scheme = self.color_scheme_table.active_colors |
|
347 | 336 | if name=='out': |
|
348 | 337 | colors = color_lists['normal'] |
|
349 | 338 | colors.number, colors.prompt, colors.normal = \ |
|
350 | 339 | scheme.out_number, scheme.out_prompt, scheme.normal |
|
351 | 340 | else: |
|
352 | 341 | colors = color_lists['inp'] |
|
353 | 342 | colors.number, colors.prompt, colors.normal = \ |
|
354 | 343 | scheme.in_number, scheme.in_prompt, scheme.in_normal |
|
355 | 344 | if name=='in2': |
|
356 | 345 | colors.prompt = scheme.in_prompt2 |
|
357 | 346 | else: |
|
358 | 347 | # No color |
|
359 | 348 | colors = color_lists['nocolor'] |
|
360 | 349 | colors.number, colors.prompt, colors.normal = '', '', '' |
|
361 | 350 | |
|
362 | 351 | count = self.shell.execution_count # Shorthand |
|
363 | 352 | # Build the dictionary to be passed to string formatting |
|
364 | 353 | fmtargs = dict(color=colors, count=count, |
|
365 | 354 | dots="."*len(str(count)), spaces=" "*len(str(count)), |
|
366 | 355 | width=self.width, txtwidth=self.txtwidth) |
|
367 | 356 | fmtargs.update(self.lazy_evaluate_fields) |
|
368 | 357 | fmtargs.update(kwargs) |
|
369 | 358 | |
|
370 | 359 | # Prepare the prompt |
|
371 | 360 | prompt = colors.prompt + self.templates[name] + colors.normal |
|
372 | 361 | |
|
373 | 362 | # Fill in required fields |
|
374 | 363 | return self._formatter.format(prompt, **fmtargs) |
|
375 | 364 | |
|
376 | 365 | def _render_rewrite(self, color=True): |
|
377 | 366 | """Render the ---> rewrite prompt.""" |
|
378 | 367 | if color: |
|
379 | 368 | scheme = self.color_scheme_table.active_colors |
|
380 | 369 | # We need a non-input version of these escapes |
|
381 | 370 | color_prompt = scheme.in_prompt.replace("\001","").replace("\002","") |
|
382 | 371 | color_normal = scheme.normal |
|
383 | 372 | else: |
|
384 | 373 | color_prompt, color_normal = '', '' |
|
385 | 374 | |
|
386 | 375 | return color_prompt + "-> ".rjust(self.txtwidth, "-") + color_normal |
|
387 | 376 | |
|
388 | 377 | def render(self, name, color=True, just=None, **kwargs): |
|
389 | 378 | """ |
|
390 | 379 | Render the selected prompt. |
|
391 | 380 | |
|
392 | 381 | Parameters |
|
393 | 382 | ---------- |
|
394 | 383 | name : str |
|
395 | 384 | Which prompt to render. One of 'in', 'in2', 'out', 'rewrite' |
|
396 | 385 | color : bool |
|
397 | 386 | If True (default), include ANSI escape sequences for a coloured prompt. |
|
398 | 387 | just : bool |
|
399 | 388 | If True, justify the prompt to the width of the last prompt. The |
|
400 | 389 | default is stored in self.justify. |
|
401 | 390 | **kwargs : |
|
402 | 391 | Additional arguments will be passed to the string formatting operation, |
|
403 | 392 | so they can override the values that would otherwise fill in the |
|
404 | 393 | template. |
|
405 | 394 | |
|
406 | 395 | Returns |
|
407 | 396 | ------- |
|
408 | 397 | A string containing the rendered prompt. |
|
409 | 398 | """ |
|
410 | 399 | res = self._render(name, color=color, **kwargs) |
|
411 | 400 | |
|
412 | 401 | # Handle justification of prompt |
|
413 | 402 | invis_chars = self.invisible_chars[name] if color else 0 |
|
414 | 403 | self.txtwidth = _lenlastline(res) - invis_chars |
|
415 | 404 | just = self.justify if (just is None) else just |
|
416 | 405 | # If the prompt spans more than one line, don't try to justify it: |
|
417 | 406 | if just and name != 'in' and ('\n' not in res) and ('\r' not in res): |
|
418 | 407 | res = res.rjust(self.width + invis_chars) |
|
419 | 408 | self.width = _lenlastline(res) - invis_chars |
|
420 | 409 | return res |
@@ -1,440 +1,437 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | A mixin for :class:`~IPython.core.application.Application` classes that |
|
4 | 4 | launch InteractiveShell instances, load extensions, etc. |
|
5 | 5 | """ |
|
6 | 6 | |
|
7 | 7 | # Copyright (c) IPython Development Team. |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | |
|
10 | 10 | from __future__ import absolute_import |
|
11 | 11 | from __future__ import print_function |
|
12 | 12 | |
|
13 | 13 | import glob |
|
14 | 14 | import os |
|
15 | 15 | import sys |
|
16 | 16 | |
|
17 | 17 | from traitlets.config.application import boolean_flag |
|
18 | 18 | from traitlets.config.configurable import Configurable |
|
19 | 19 | from traitlets.config.loader import Config |
|
20 | 20 | from IPython.core import pylabtools |
|
21 | 21 | from IPython.utils import py3compat |
|
22 | 22 | from IPython.utils.contexts import preserve_keys |
|
23 | 23 | from IPython.utils.path import filefind |
|
24 | 24 | from traitlets import ( |
|
25 | Unicode, Instance, List, Bool, CaselessStrEnum | |
|
25 | Unicode, Instance, List, Bool, CaselessStrEnum, default, observe, | |
|
26 | 26 | ) |
|
27 | 27 | from IPython.lib.inputhook import guis |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Aliases and Flags |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | gui_keys = tuple(sorted([ key for key in guis if key is not None ])) |
|
34 | 34 | |
|
35 | 35 | backend_keys = sorted(pylabtools.backends.keys()) |
|
36 | 36 | backend_keys.insert(0, 'auto') |
|
37 | 37 | |
|
38 | 38 | shell_flags = {} |
|
39 | 39 | |
|
40 | 40 | addflag = lambda *args: shell_flags.update(boolean_flag(*args)) |
|
41 | 41 | addflag('autoindent', 'InteractiveShell.autoindent', |
|
42 | 42 | 'Turn on autoindenting.', 'Turn off autoindenting.' |
|
43 | 43 | ) |
|
44 | 44 | addflag('automagic', 'InteractiveShell.automagic', |
|
45 | 45 | """Turn on the auto calling of magic commands. Type %%magic at the |
|
46 | 46 | IPython prompt for more information.""", |
|
47 | 47 | 'Turn off the auto calling of magic commands.' |
|
48 | 48 | ) |
|
49 | 49 | addflag('pdb', 'InteractiveShell.pdb', |
|
50 | 50 | "Enable auto calling the pdb debugger after every exception.", |
|
51 | 51 | "Disable auto calling the pdb debugger after every exception." |
|
52 | 52 | ) |
|
53 | 53 | # pydb flag doesn't do any config, as core.debugger switches on import, |
|
54 | 54 | # which is before parsing. This just allows the flag to be passed. |
|
55 | 55 | shell_flags.update(dict( |
|
56 | 56 | pydb = ({}, |
|
57 | 57 | """Use the third party 'pydb' package as debugger, instead of pdb. |
|
58 | 58 | Requires that pydb is installed.""" |
|
59 | 59 | ) |
|
60 | 60 | )) |
|
61 | 61 | addflag('pprint', 'PlainTextFormatter.pprint', |
|
62 | 62 | "Enable auto pretty printing of results.", |
|
63 | 63 | "Disable auto pretty printing of results." |
|
64 | 64 | ) |
|
65 | 65 | addflag('color-info', 'InteractiveShell.color_info', |
|
66 | 66 | """IPython can display information about objects via a set of functions, |
|
67 | 67 | and optionally can use colors for this, syntax highlighting |
|
68 | 68 | source code and various other elements. This is on by default, but can cause |
|
69 | 69 | problems with some pagers. If you see such problems, you can disable the |
|
70 | 70 | colours.""", |
|
71 | 71 | "Disable using colors for info related things." |
|
72 | 72 | ) |
|
73 | 73 | addflag('deep-reload', 'InteractiveShell.deep_reload', |
|
74 | 74 | """ **Deprecated** and will be removed in IPython 5.0. |
|
75 | 75 | |
|
76 | 76 | Enable deep (recursive) reloading by default. IPython can use the |
|
77 | 77 | deep_reload module which reloads changes in modules recursively (it |
|
78 | 78 | replaces the reload() function, so you don't need to change anything to |
|
79 | 79 | use it). deep_reload() forces a full reload of modules whose code may |
|
80 | 80 | have changed, which the default reload() function does not. When |
|
81 | 81 | deep_reload is off, IPython will use the normal reload(), but |
|
82 | 82 | deep_reload will still be available as dreload(). This feature is off |
|
83 | 83 | by default [which means that you have both normal reload() and |
|
84 | 84 | dreload()].""", |
|
85 | 85 | "Disable deep (recursive) reloading by default." |
|
86 | 86 | ) |
|
87 | 87 | nosep_config = Config() |
|
88 | 88 | nosep_config.InteractiveShell.separate_in = '' |
|
89 | 89 | nosep_config.InteractiveShell.separate_out = '' |
|
90 | 90 | nosep_config.InteractiveShell.separate_out2 = '' |
|
91 | 91 | |
|
92 | 92 | shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.") |
|
93 | 93 | shell_flags['pylab'] = ( |
|
94 | 94 | {'InteractiveShellApp' : {'pylab' : 'auto'}}, |
|
95 | 95 | """Pre-load matplotlib and numpy for interactive use with |
|
96 | 96 | the default matplotlib backend.""" |
|
97 | 97 | ) |
|
98 | 98 | shell_flags['matplotlib'] = ( |
|
99 | 99 | {'InteractiveShellApp' : {'matplotlib' : 'auto'}}, |
|
100 | 100 | """Configure matplotlib for interactive use with |
|
101 | 101 | the default matplotlib backend.""" |
|
102 | 102 | ) |
|
103 | 103 | |
|
104 | 104 | # it's possible we don't want short aliases for *all* of these: |
|
105 | 105 | shell_aliases = dict( |
|
106 | 106 | autocall='InteractiveShell.autocall', |
|
107 | 107 | colors='InteractiveShell.colors', |
|
108 | 108 | logfile='InteractiveShell.logfile', |
|
109 | 109 | logappend='InteractiveShell.logappend', |
|
110 | 110 | c='InteractiveShellApp.code_to_run', |
|
111 | 111 | m='InteractiveShellApp.module_to_run', |
|
112 | 112 | ext='InteractiveShellApp.extra_extension', |
|
113 | 113 | gui='InteractiveShellApp.gui', |
|
114 | 114 | pylab='InteractiveShellApp.pylab', |
|
115 | 115 | matplotlib='InteractiveShellApp.matplotlib', |
|
116 | 116 | ) |
|
117 | 117 | shell_aliases['cache-size'] = 'InteractiveShell.cache_size' |
|
118 | 118 | |
|
119 | 119 | #----------------------------------------------------------------------------- |
|
120 | 120 | # Main classes and functions |
|
121 | 121 | #----------------------------------------------------------------------------- |
|
122 | 122 | |
|
123 | 123 | class InteractiveShellApp(Configurable): |
|
124 | 124 | """A Mixin for applications that start InteractiveShell instances. |
|
125 | 125 | |
|
126 | 126 | Provides configurables for loading extensions and executing files |
|
127 | 127 | as part of configuring a Shell environment. |
|
128 | 128 | |
|
129 | 129 | The following methods should be called by the :meth:`initialize` method |
|
130 | 130 | of the subclass: |
|
131 | 131 | |
|
132 | 132 | - :meth:`init_path` |
|
133 | 133 | - :meth:`init_shell` (to be implemented by the subclass) |
|
134 | 134 | - :meth:`init_gui_pylab` |
|
135 | 135 | - :meth:`init_extensions` |
|
136 | 136 | - :meth:`init_code` |
|
137 | 137 | """ |
|
138 |
extensions = List(Unicode(), |
|
|
138 | extensions = List(Unicode(), | |
|
139 | 139 | help="A list of dotted module names of IPython extensions to load." |
|
140 | ) | |
|
141 |
extra_extension = Unicode('', |
|
|
140 | ).tag(config=True) | |
|
141 | extra_extension = Unicode('', | |
|
142 | 142 | help="dotted module name of an IPython extension to load." |
|
143 | ) | |
|
143 | ).tag(config=True) | |
|
144 | 144 | |
|
145 | reraise_ipython_extension_failures = Bool( | |
|
146 | False, | |
|
147 | config=True, | |
|
145 | reraise_ipython_extension_failures = Bool(False, | |
|
148 | 146 | help="Reraise exceptions encountered loading IPython extensions?", |
|
149 | ) | |
|
147 | ).tag(config=True) | |
|
150 | 148 | |
|
151 | 149 | # Extensions that are always loaded (not configurable) |
|
152 |
default_extensions = List(Unicode(), [u'storemagic'] |
|
|
150 | default_extensions = List(Unicode(), [u'storemagic']).tag(config=False) | |
|
153 | 151 | |
|
154 |
hide_initial_ns = Bool(True, |
|
|
152 | hide_initial_ns = Bool(True, | |
|
155 | 153 | help="""Should variables loaded at startup (by startup files, exec_lines, etc.) |
|
156 | 154 | be hidden from tools like %who?""" |
|
157 | ) | |
|
155 | ).tag(config=True) | |
|
158 | 156 | |
|
159 |
exec_files = List(Unicode(), |
|
|
157 | exec_files = List(Unicode(), | |
|
160 | 158 | help="""List of files to run at IPython startup.""" |
|
161 | ) | |
|
162 |
exec_PYTHONSTARTUP = Bool(True, |
|
|
159 | ).tag(config=True) | |
|
160 | exec_PYTHONSTARTUP = Bool(True, | |
|
163 | 161 | help="""Run the file referenced by the PYTHONSTARTUP environment |
|
164 | 162 | variable at IPython startup.""" |
|
165 | ) | |
|
166 |
file_to_run = Unicode('', |
|
|
167 | help="""A file to be run""") | |
|
163 | ).tag(config=True) | |
|
164 | file_to_run = Unicode('', | |
|
165 | help="""A file to be run""").tag(config=True) | |
|
168 | 166 | |
|
169 |
exec_lines = List(Unicode(), |
|
|
167 | exec_lines = List(Unicode(), | |
|
170 | 168 | help="""lines of code to run at IPython startup.""" |
|
171 | ) | |
|
172 |
code_to_run = Unicode('', |
|
|
169 | ).tag(config=True) | |
|
170 | code_to_run = Unicode('', | |
|
173 | 171 | help="Execute the given command string." |
|
174 | ) | |
|
175 |
module_to_run = Unicode('', |
|
|
172 | ).tag(config=True) | |
|
173 | module_to_run = Unicode('', | |
|
176 | 174 | help="Run the module as a script." |
|
177 | ) | |
|
178 |
gui = CaselessStrEnum(gui_keys, |
|
|
175 | ).tag(config=True) | |
|
176 | gui = CaselessStrEnum(gui_keys, allow_none=True, | |
|
179 | 177 | help="Enable GUI event loop integration with any of {0}.".format(gui_keys) |
|
180 | ) | |
|
178 | ).tag(config=True) | |
|
181 | 179 | matplotlib = CaselessStrEnum(backend_keys, allow_none=True, |
|
182 | config=True, | |
|
183 | 180 | help="""Configure matplotlib for interactive use with |
|
184 | 181 | the default matplotlib backend.""" |
|
185 | ) | |
|
182 | ).tag(config=True) | |
|
186 | 183 | pylab = CaselessStrEnum(backend_keys, allow_none=True, |
|
187 | config=True, | |
|
188 | 184 | help="""Pre-load matplotlib and numpy for interactive use, |
|
189 | 185 | selecting a particular matplotlib backend and loop integration. |
|
190 | 186 | """ |
|
191 | ) | |
|
192 |
pylab_import_all = Bool(True, |
|
|
187 | ).tag(config=True) | |
|
188 | pylab_import_all = Bool(True, | |
|
193 | 189 | help="""If true, IPython will populate the user namespace with numpy, pylab, etc. |
|
194 | 190 | and an ``import *`` is done from numpy and pylab, when using pylab mode. |
|
195 | 191 | |
|
196 | 192 | When False, pylab mode should not import any names into the user namespace. |
|
197 | 193 | """ |
|
198 | ) | |
|
194 | ).tag(config=True) | |
|
199 | 195 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', |
|
200 | 196 | allow_none=True) |
|
201 | 197 | # whether interact-loop should start |
|
202 | 198 | interact = Bool(True) |
|
203 | 199 | |
|
204 | 200 | user_ns = Instance(dict, args=None, allow_none=True) |
|
205 | def _user_ns_changed(self, name, old, new): | |
|
201 | @observe('user_ns') | |
|
202 | def _user_ns_changed(self, change): | |
|
206 | 203 | if self.shell is not None: |
|
207 | self.shell.user_ns = new | |
|
204 | self.shell.user_ns = change['new'] | |
|
208 | 205 | self.shell.init_user_ns() |
|
209 | 206 | |
|
210 | 207 | def init_path(self): |
|
211 | 208 | """Add current working directory, '', to sys.path""" |
|
212 | 209 | if sys.path[0] != '': |
|
213 | 210 | sys.path.insert(0, '') |
|
214 | 211 | |
|
215 | 212 | def init_shell(self): |
|
216 | 213 | raise NotImplementedError("Override in subclasses") |
|
217 | 214 | |
|
218 | 215 | def init_gui_pylab(self): |
|
219 | 216 | """Enable GUI event loop integration, taking pylab into account.""" |
|
220 | 217 | enable = False |
|
221 | 218 | shell = self.shell |
|
222 | 219 | if self.pylab: |
|
223 | 220 | enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all) |
|
224 | 221 | key = self.pylab |
|
225 | 222 | elif self.matplotlib: |
|
226 | 223 | enable = shell.enable_matplotlib |
|
227 | 224 | key = self.matplotlib |
|
228 | 225 | elif self.gui: |
|
229 | 226 | enable = shell.enable_gui |
|
230 | 227 | key = self.gui |
|
231 | 228 | |
|
232 | 229 | if not enable: |
|
233 | 230 | return |
|
234 | 231 | |
|
235 | 232 | try: |
|
236 | 233 | r = enable(key) |
|
237 | 234 | except ImportError: |
|
238 | 235 | self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?") |
|
239 | 236 | self.shell.showtraceback() |
|
240 | 237 | return |
|
241 | 238 | except Exception: |
|
242 | 239 | self.log.warning("GUI event loop or pylab initialization failed") |
|
243 | 240 | self.shell.showtraceback() |
|
244 | 241 | return |
|
245 | 242 | |
|
246 | 243 | if isinstance(r, tuple): |
|
247 | 244 | gui, backend = r[:2] |
|
248 | 245 | self.log.info("Enabling GUI event loop integration, " |
|
249 | 246 | "eventloop=%s, matplotlib=%s", gui, backend) |
|
250 | 247 | if key == "auto": |
|
251 | 248 | print("Using matplotlib backend: %s" % backend) |
|
252 | 249 | else: |
|
253 | 250 | gui = r |
|
254 | 251 | self.log.info("Enabling GUI event loop integration, " |
|
255 | 252 | "eventloop=%s", gui) |
|
256 | 253 | |
|
257 | 254 | def init_extensions(self): |
|
258 | 255 | """Load all IPython extensions in IPythonApp.extensions. |
|
259 | 256 | |
|
260 | 257 | This uses the :meth:`ExtensionManager.load_extensions` to load all |
|
261 | 258 | the extensions listed in ``self.extensions``. |
|
262 | 259 | """ |
|
263 | 260 | try: |
|
264 | 261 | self.log.debug("Loading IPython extensions...") |
|
265 | 262 | extensions = self.default_extensions + self.extensions |
|
266 | 263 | if self.extra_extension: |
|
267 | 264 | extensions.append(self.extra_extension) |
|
268 | 265 | for ext in extensions: |
|
269 | 266 | try: |
|
270 | 267 | self.log.info("Loading IPython extension: %s" % ext) |
|
271 | 268 | self.shell.extension_manager.load_extension(ext) |
|
272 | 269 | except: |
|
273 | 270 | if self.reraise_ipython_extension_failures: |
|
274 | 271 | raise |
|
275 | 272 | msg = ("Error in loading extension: {ext}\n" |
|
276 | 273 | "Check your config files in {location}".format( |
|
277 | 274 | ext=ext, |
|
278 | 275 | location=self.profile_dir.location |
|
279 | 276 | )) |
|
280 | 277 | self.log.warning(msg, exc_info=True) |
|
281 | 278 | except: |
|
282 | 279 | if self.reraise_ipython_extension_failures: |
|
283 | 280 | raise |
|
284 | 281 | self.log.warning("Unknown error in loading extensions:", exc_info=True) |
|
285 | 282 | |
|
286 | 283 | def init_code(self): |
|
287 | 284 | """run the pre-flight code, specified via exec_lines""" |
|
288 | 285 | self._run_startup_files() |
|
289 | 286 | self._run_exec_lines() |
|
290 | 287 | self._run_exec_files() |
|
291 | 288 | |
|
292 | 289 | # Hide variables defined here from %who etc. |
|
293 | 290 | if self.hide_initial_ns: |
|
294 | 291 | self.shell.user_ns_hidden.update(self.shell.user_ns) |
|
295 | 292 | |
|
296 | 293 | # command-line execution (ipython -i script.py, ipython -m module) |
|
297 | 294 | # should *not* be excluded from %whos |
|
298 | 295 | self._run_cmd_line_code() |
|
299 | 296 | self._run_module() |
|
300 | 297 | |
|
301 | 298 | # flush output, so itwon't be attached to the first cell |
|
302 | 299 | sys.stdout.flush() |
|
303 | 300 | sys.stderr.flush() |
|
304 | 301 | |
|
305 | 302 | def _run_exec_lines(self): |
|
306 | 303 | """Run lines of code in IPythonApp.exec_lines in the user's namespace.""" |
|
307 | 304 | if not self.exec_lines: |
|
308 | 305 | return |
|
309 | 306 | try: |
|
310 | 307 | self.log.debug("Running code from IPythonApp.exec_lines...") |
|
311 | 308 | for line in self.exec_lines: |
|
312 | 309 | try: |
|
313 | 310 | self.log.info("Running code in user namespace: %s" % |
|
314 | 311 | line) |
|
315 | 312 | self.shell.run_cell(line, store_history=False) |
|
316 | 313 | except: |
|
317 | 314 | self.log.warning("Error in executing line in user " |
|
318 | 315 | "namespace: %s" % line) |
|
319 | 316 | self.shell.showtraceback() |
|
320 | 317 | except: |
|
321 | 318 | self.log.warning("Unknown error in handling IPythonApp.exec_lines:") |
|
322 | 319 | self.shell.showtraceback() |
|
323 | 320 | |
|
324 | 321 | def _exec_file(self, fname, shell_futures=False): |
|
325 | 322 | try: |
|
326 | 323 | full_filename = filefind(fname, [u'.', self.ipython_dir]) |
|
327 | 324 | except IOError as e: |
|
328 | 325 | self.log.warning("File not found: %r"%fname) |
|
329 | 326 | return |
|
330 | 327 | # Make sure that the running script gets a proper sys.argv as if it |
|
331 | 328 | # were run from a system shell. |
|
332 | 329 | save_argv = sys.argv |
|
333 | 330 | sys.argv = [full_filename] + self.extra_args[1:] |
|
334 | 331 | # protect sys.argv from potential unicode strings on Python 2: |
|
335 | 332 | if not py3compat.PY3: |
|
336 | 333 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
337 | 334 | try: |
|
338 | 335 | if os.path.isfile(full_filename): |
|
339 | 336 | self.log.info("Running file in user namespace: %s" % |
|
340 | 337 | full_filename) |
|
341 | 338 | # Ensure that __file__ is always defined to match Python |
|
342 | 339 | # behavior. |
|
343 | 340 | with preserve_keys(self.shell.user_ns, '__file__'): |
|
344 | 341 | self.shell.user_ns['__file__'] = fname |
|
345 | 342 | if full_filename.endswith('.ipy'): |
|
346 | 343 | self.shell.safe_execfile_ipy(full_filename, |
|
347 | 344 | shell_futures=shell_futures) |
|
348 | 345 | else: |
|
349 | 346 | # default to python, even without extension |
|
350 | 347 | self.shell.safe_execfile(full_filename, |
|
351 | 348 | self.shell.user_ns, |
|
352 | 349 | shell_futures=shell_futures, |
|
353 | 350 | raise_exceptions=True) |
|
354 | 351 | finally: |
|
355 | 352 | sys.argv = save_argv |
|
356 | 353 | |
|
357 | 354 | def _run_startup_files(self): |
|
358 | 355 | """Run files from profile startup directory""" |
|
359 | 356 | startup_dir = self.profile_dir.startup_dir |
|
360 | 357 | startup_files = [] |
|
361 | 358 | |
|
362 | 359 | if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \ |
|
363 | 360 | not (self.file_to_run or self.code_to_run or self.module_to_run): |
|
364 | 361 | python_startup = os.environ['PYTHONSTARTUP'] |
|
365 | 362 | self.log.debug("Running PYTHONSTARTUP file %s...", python_startup) |
|
366 | 363 | try: |
|
367 | 364 | self._exec_file(python_startup) |
|
368 | 365 | except: |
|
369 | 366 | self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup) |
|
370 | 367 | self.shell.showtraceback() |
|
371 | 368 | finally: |
|
372 | 369 | # Many PYTHONSTARTUP files set up the readline completions, |
|
373 | 370 | # but this is often at odds with IPython's own completions. |
|
374 | 371 | # Do not allow PYTHONSTARTUP to set up readline. |
|
375 | 372 | if self.shell.has_readline: |
|
376 | 373 | self.shell.set_readline_completer() |
|
377 | 374 | |
|
378 | 375 | startup_files += glob.glob(os.path.join(startup_dir, '*.py')) |
|
379 | 376 | startup_files += glob.glob(os.path.join(startup_dir, '*.ipy')) |
|
380 | 377 | if not startup_files: |
|
381 | 378 | return |
|
382 | 379 | |
|
383 | 380 | self.log.debug("Running startup files from %s...", startup_dir) |
|
384 | 381 | try: |
|
385 | 382 | for fname in sorted(startup_files): |
|
386 | 383 | self._exec_file(fname) |
|
387 | 384 | except: |
|
388 | 385 | self.log.warning("Unknown error in handling startup files:") |
|
389 | 386 | self.shell.showtraceback() |
|
390 | 387 | |
|
391 | 388 | def _run_exec_files(self): |
|
392 | 389 | """Run files from IPythonApp.exec_files""" |
|
393 | 390 | if not self.exec_files: |
|
394 | 391 | return |
|
395 | 392 | |
|
396 | 393 | self.log.debug("Running files in IPythonApp.exec_files...") |
|
397 | 394 | try: |
|
398 | 395 | for fname in self.exec_files: |
|
399 | 396 | self._exec_file(fname) |
|
400 | 397 | except: |
|
401 | 398 | self.log.warning("Unknown error in handling IPythonApp.exec_files:") |
|
402 | 399 | self.shell.showtraceback() |
|
403 | 400 | |
|
404 | 401 | def _run_cmd_line_code(self): |
|
405 | 402 | """Run code or file specified at the command-line""" |
|
406 | 403 | if self.code_to_run: |
|
407 | 404 | line = self.code_to_run |
|
408 | 405 | try: |
|
409 | 406 | self.log.info("Running code given at command line (c=): %s" % |
|
410 | 407 | line) |
|
411 | 408 | self.shell.run_cell(line, store_history=False) |
|
412 | 409 | except: |
|
413 | 410 | self.log.warning("Error in executing line in user namespace: %s" % |
|
414 | 411 | line) |
|
415 | 412 | self.shell.showtraceback() |
|
416 | 413 | if not self.interact: |
|
417 | 414 | self.exit(1) |
|
418 | 415 | |
|
419 | 416 | # Like Python itself, ignore the second if the first of these is present |
|
420 | 417 | elif self.file_to_run: |
|
421 | 418 | fname = self.file_to_run |
|
422 | 419 | try: |
|
423 | 420 | self._exec_file(fname, shell_futures=True) |
|
424 | 421 | except: |
|
425 | 422 | self.shell.showtraceback(tb_offset=4) |
|
426 | 423 | if not self.interact: |
|
427 | 424 | self.exit(1) |
|
428 | 425 | |
|
429 | 426 | def _run_module(self): |
|
430 | 427 | """Run module specified at the command-line.""" |
|
431 | 428 | if self.module_to_run: |
|
432 | 429 | # Make sure that the module gets a proper sys.argv as if it were |
|
433 | 430 | # run using `python -m`. |
|
434 | 431 | save_argv = sys.argv |
|
435 | 432 | sys.argv = [sys.executable] + self.extra_args |
|
436 | 433 | try: |
|
437 | 434 | self.shell.safe_run_module(self.module_to_run, |
|
438 | 435 | self.shell.user_ns) |
|
439 | 436 | finally: |
|
440 | 437 | sys.argv = save_argv |
@@ -1,241 +1,228 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | %store magic for lightweight persistence. |
|
4 | 4 | |
|
5 | 5 | Stores variables, aliases and macros in IPython's database. |
|
6 | 6 | |
|
7 | 7 | To automatically restore stored variables at startup, add this to your |
|
8 | 8 | :file:`ipython_config.py` file:: |
|
9 | 9 | |
|
10 | 10 | c.StoreMagics.autorestore = True |
|
11 | 11 | """ |
|
12 | 12 | from __future__ import print_function |
|
13 | #----------------------------------------------------------------------------- | |
|
14 |
# |
|
|
15 | # | |
|
16 | # Distributed under the terms of the Modified BSD License. | |
|
17 | # | |
|
18 | # The full license is in the file COPYING.txt, distributed with this software. | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | ||
|
21 | #----------------------------------------------------------------------------- | |
|
22 | # Imports | |
|
23 | #----------------------------------------------------------------------------- | |
|
24 | ||
|
25 | # Stdlib | |
|
13 | ||
|
14 | # Copyright (c) IPython Development Team. | |
|
15 | # Distributed under the terms of the Modified BSD License. | |
|
16 | ||
|
26 | 17 | import inspect, os, sys, textwrap |
|
27 | 18 | |
|
28 | # Our own | |
|
29 | 19 | from IPython.core.error import UsageError |
|
30 | 20 | from IPython.core.magic import Magics, magics_class, line_magic |
|
31 | 21 | from traitlets import Bool |
|
32 | 22 | from IPython.utils.py3compat import string_types |
|
33 | 23 | |
|
34 | #----------------------------------------------------------------------------- | |
|
35 | # Functions and classes | |
|
36 | #----------------------------------------------------------------------------- | |
|
37 | 24 | |
|
38 | 25 | def restore_aliases(ip): |
|
39 | 26 | staliases = ip.db.get('stored_aliases', {}) |
|
40 | 27 | for k,v in staliases.items(): |
|
41 | 28 | #print "restore alias",k,v # dbg |
|
42 | 29 | #self.alias_table[k] = v |
|
43 | 30 | ip.alias_manager.define_alias(k,v) |
|
44 | 31 | |
|
45 | 32 | |
|
46 | 33 | def refresh_variables(ip): |
|
47 | 34 | db = ip.db |
|
48 | 35 | for key in db.keys('autorestore/*'): |
|
49 | 36 | # strip autorestore |
|
50 | 37 | justkey = os.path.basename(key) |
|
51 | 38 | try: |
|
52 | 39 | obj = db[key] |
|
53 | 40 | except KeyError: |
|
54 | 41 | print("Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey) |
|
55 | 42 | print("The error was:", sys.exc_info()[0]) |
|
56 | 43 | else: |
|
57 | 44 | #print "restored",justkey,"=",obj #dbg |
|
58 | 45 | ip.user_ns[justkey] = obj |
|
59 | 46 | |
|
60 | 47 | |
|
61 | 48 | def restore_dhist(ip): |
|
62 | 49 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) |
|
63 | 50 | |
|
64 | 51 | |
|
65 | 52 | def restore_data(ip): |
|
66 | 53 | refresh_variables(ip) |
|
67 | 54 | restore_aliases(ip) |
|
68 | 55 | restore_dhist(ip) |
|
69 | 56 | |
|
70 | 57 | |
|
71 | 58 | @magics_class |
|
72 | 59 | class StoreMagics(Magics): |
|
73 | 60 | """Lightweight persistence for python variables. |
|
74 | 61 | |
|
75 | 62 | Provides the %store magic.""" |
|
76 | 63 | |
|
77 |
autorestore = Bool(False, |
|
|
64 | autorestore = Bool(False, help= | |
|
78 | 65 | """If True, any %store-d variables will be automatically restored |
|
79 | 66 | when IPython starts. |
|
80 | 67 | """ |
|
81 | ) | |
|
68 | ).tag(config=True) | |
|
82 | 69 | |
|
83 | 70 | def __init__(self, shell): |
|
84 | 71 | super(StoreMagics, self).__init__(shell=shell) |
|
85 | 72 | self.shell.configurables.append(self) |
|
86 | 73 | if self.autorestore: |
|
87 | 74 | restore_data(self.shell) |
|
88 | 75 | |
|
89 | 76 | @line_magic |
|
90 | 77 | def store(self, parameter_s=''): |
|
91 | 78 | """Lightweight persistence for python variables. |
|
92 | 79 | |
|
93 | 80 | Example:: |
|
94 | 81 | |
|
95 | 82 | In [1]: l = ['hello',10,'world'] |
|
96 | 83 | In [2]: %store l |
|
97 | 84 | In [3]: exit |
|
98 | 85 | |
|
99 | 86 | (IPython session is closed and started again...) |
|
100 | 87 | |
|
101 | 88 | ville@badger:~$ ipython |
|
102 | 89 | In [1]: l |
|
103 | 90 | NameError: name 'l' is not defined |
|
104 | 91 | In [2]: %store -r |
|
105 | 92 | In [3]: l |
|
106 | 93 | Out[3]: ['hello', 10, 'world'] |
|
107 | 94 | |
|
108 | 95 | Usage: |
|
109 | 96 | |
|
110 | 97 | * ``%store`` - Show list of all variables and their current |
|
111 | 98 | values |
|
112 | 99 | * ``%store spam`` - Store the *current* value of the variable spam |
|
113 | 100 | to disk |
|
114 | 101 | * ``%store -d spam`` - Remove the variable and its value from storage |
|
115 | 102 | * ``%store -z`` - Remove all variables from storage |
|
116 | 103 | * ``%store -r`` - Refresh all variables from store (overwrite |
|
117 | 104 | current vals) |
|
118 | 105 | * ``%store -r spam bar`` - Refresh specified variables from store |
|
119 | 106 | (delete current val) |
|
120 | 107 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
|
121 | 108 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
|
122 | 109 | |
|
123 | 110 | It should be noted that if you change the value of a variable, you |
|
124 | 111 | need to %store it again if you want to persist the new value. |
|
125 | 112 | |
|
126 | 113 | Note also that the variables will need to be pickleable; most basic |
|
127 | 114 | python types can be safely %store'd. |
|
128 | 115 | |
|
129 | 116 | Also aliases can be %store'd across sessions. |
|
130 | 117 | """ |
|
131 | 118 | |
|
132 | 119 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
133 | 120 | args = argsl.split(None,1) |
|
134 | 121 | ip = self.shell |
|
135 | 122 | db = ip.db |
|
136 | 123 | # delete |
|
137 | 124 | if 'd' in opts: |
|
138 | 125 | try: |
|
139 | 126 | todel = args[0] |
|
140 | 127 | except IndexError: |
|
141 | 128 | raise UsageError('You must provide the variable to forget') |
|
142 | 129 | else: |
|
143 | 130 | try: |
|
144 | 131 | del db['autorestore/' + todel] |
|
145 | 132 | except: |
|
146 | 133 | raise UsageError("Can't delete variable '%s'" % todel) |
|
147 | 134 | # reset |
|
148 | 135 | elif 'z' in opts: |
|
149 | 136 | for k in db.keys('autorestore/*'): |
|
150 | 137 | del db[k] |
|
151 | 138 | |
|
152 | 139 | elif 'r' in opts: |
|
153 | 140 | if args: |
|
154 | 141 | for arg in args: |
|
155 | 142 | try: |
|
156 | 143 | obj = db['autorestore/' + arg] |
|
157 | 144 | except KeyError: |
|
158 | 145 | print("no stored variable %s" % arg) |
|
159 | 146 | else: |
|
160 | 147 | ip.user_ns[arg] = obj |
|
161 | 148 | else: |
|
162 | 149 | restore_data(ip) |
|
163 | 150 | |
|
164 | 151 | # run without arguments -> list variables & values |
|
165 | 152 | elif not args: |
|
166 | 153 | vars = db.keys('autorestore/*') |
|
167 | 154 | vars.sort() |
|
168 | 155 | if vars: |
|
169 | 156 | size = max(map(len, vars)) |
|
170 | 157 | else: |
|
171 | 158 | size = 0 |
|
172 | 159 | |
|
173 | 160 | print('Stored variables and their in-db values:') |
|
174 | 161 | fmt = '%-'+str(size)+'s -> %s' |
|
175 | 162 | get = db.get |
|
176 | 163 | for var in vars: |
|
177 | 164 | justkey = os.path.basename(var) |
|
178 | 165 | # print 30 first characters from every var |
|
179 | 166 | print(fmt % (justkey, repr(get(var, '<unavailable>'))[:50])) |
|
180 | 167 | |
|
181 | 168 | # default action - store the variable |
|
182 | 169 | else: |
|
183 | 170 | # %store foo >file.txt or >>file.txt |
|
184 | 171 | if len(args) > 1 and args[1].startswith('>'): |
|
185 | 172 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
186 | 173 | if args[1].startswith('>>'): |
|
187 | 174 | fil = open(fnam, 'a') |
|
188 | 175 | else: |
|
189 | 176 | fil = open(fnam, 'w') |
|
190 | 177 | obj = ip.ev(args[0]) |
|
191 | 178 | print("Writing '%s' (%s) to file '%s'." % (args[0], |
|
192 | 179 | obj.__class__.__name__, fnam)) |
|
193 | 180 | |
|
194 | 181 | |
|
195 | 182 | if not isinstance (obj, string_types): |
|
196 | 183 | from pprint import pprint |
|
197 | 184 | pprint(obj, fil) |
|
198 | 185 | else: |
|
199 | 186 | fil.write(obj) |
|
200 | 187 | if not obj.endswith('\n'): |
|
201 | 188 | fil.write('\n') |
|
202 | 189 | |
|
203 | 190 | fil.close() |
|
204 | 191 | return |
|
205 | 192 | |
|
206 | 193 | # %store foo |
|
207 | 194 | try: |
|
208 | 195 | obj = ip.user_ns[args[0]] |
|
209 | 196 | except KeyError: |
|
210 | 197 | # it might be an alias |
|
211 | 198 | name = args[0] |
|
212 | 199 | try: |
|
213 | 200 | cmd = ip.alias_manager.retrieve_alias(name) |
|
214 | 201 | except ValueError: |
|
215 | 202 | raise UsageError("Unknown variable '%s'" % name) |
|
216 | 203 | |
|
217 | 204 | staliases = db.get('stored_aliases',{}) |
|
218 | 205 | staliases[name] = cmd |
|
219 | 206 | db['stored_aliases'] = staliases |
|
220 | 207 | print("Alias stored: %s (%s)" % (name, cmd)) |
|
221 | 208 | return |
|
222 | 209 | |
|
223 | 210 | else: |
|
224 | 211 | modname = getattr(inspect.getmodule(obj), '__name__', '') |
|
225 | 212 | if modname == '__main__': |
|
226 | 213 | print(textwrap.dedent("""\ |
|
227 | 214 | Warning:%s is %s |
|
228 | 215 | Proper storage of interactively declared classes (or instances |
|
229 | 216 | of those classes) is not possible! Only instances |
|
230 | 217 | of classes in real modules on file system can be %%store'd. |
|
231 | 218 | """ % (args[0], obj) )) |
|
232 | 219 | return |
|
233 | 220 | #pickled = pickle.dumps(obj) |
|
234 | 221 | db[ 'autorestore/' + args[0] ] = obj |
|
235 | 222 | print("Stored '%s' (%s)" % (args[0], obj.__class__.__name__)) |
|
236 | 223 | |
|
237 | 224 | |
|
238 | 225 | def load_ipython_extension(ip): |
|
239 | 226 | """Load the extension in IPython.""" |
|
240 | 227 | ip.register_magics(StoreMagics) |
|
241 | 228 |
@@ -1,368 +1,371 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | """ |
|
4 | 4 | The :class:`~IPython.core.application.Application` object for the command |
|
5 | 5 | line :command:`ipython` program. |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | # Copyright (c) IPython Development Team. |
|
9 | 9 | # Distributed under the terms of the Modified BSD License. |
|
10 | 10 | |
|
11 | 11 | from __future__ import absolute_import |
|
12 | 12 | from __future__ import print_function |
|
13 | 13 | |
|
14 | 14 | import logging |
|
15 | 15 | import os |
|
16 | 16 | import sys |
|
17 | import warnings | |
|
17 | 18 | |
|
18 | 19 | from traitlets.config.loader import Config |
|
19 | 20 | from traitlets.config.application import boolean_flag, catch_config_error, Application |
|
20 | 21 | from IPython.core import release |
|
21 | 22 | from IPython.core import usage |
|
22 | 23 | from IPython.core.completer import IPCompleter |
|
23 | 24 | from IPython.core.crashhandler import CrashHandler |
|
24 | 25 | from IPython.core.formatters import PlainTextFormatter |
|
25 | 26 | from IPython.core.history import HistoryManager |
|
26 | 27 | from IPython.core.prompts import PromptManager |
|
27 | 28 | from IPython.core.application import ( |
|
28 | 29 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
29 | 30 | ) |
|
30 | 31 | from IPython.core.magics import ScriptMagics |
|
31 | 32 | from IPython.core.shellapp import ( |
|
32 | 33 | InteractiveShellApp, shell_flags, shell_aliases |
|
33 | 34 | ) |
|
34 | 35 | from IPython.extensions.storemagic import StoreMagics |
|
35 | 36 | from .ptshell import TerminalInteractiveShell |
|
36 | from IPython.utils import warn | |
|
37 | 37 | from IPython.paths import get_ipython_dir |
|
38 | 38 | from traitlets import ( |
|
39 | Bool, List, Dict, | |
|
39 | Bool, List, Dict, default, observe, | |
|
40 | 40 | ) |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | # Globals, utilities and helpers |
|
44 | 44 | #----------------------------------------------------------------------------- |
|
45 | 45 | |
|
46 | 46 | _examples = """ |
|
47 | 47 | ipython --matplotlib # enable matplotlib integration |
|
48 | 48 | ipython --matplotlib=qt # enable matplotlib integration with qt4 backend |
|
49 | 49 | |
|
50 | 50 | ipython --log-level=DEBUG # set logging to DEBUG |
|
51 | 51 | ipython --profile=foo # start with profile foo |
|
52 | 52 | |
|
53 | 53 | ipython profile create foo # create profile foo w/ default config files |
|
54 | 54 | ipython help profile # show the help for the profile subcmd |
|
55 | 55 | |
|
56 | 56 | ipython locate # print the path to the IPython directory |
|
57 | 57 | ipython locate profile foo # print the path to the directory for profile `foo` |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | 60 | #----------------------------------------------------------------------------- |
|
61 | 61 | # Crash handler for this application |
|
62 | 62 | #----------------------------------------------------------------------------- |
|
63 | 63 | |
|
64 | 64 | class IPAppCrashHandler(CrashHandler): |
|
65 | 65 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
66 | 66 | |
|
67 | 67 | def __init__(self, app): |
|
68 | 68 | contact_name = release.author |
|
69 | 69 | contact_email = release.author_email |
|
70 | 70 | bug_tracker = 'https://github.com/ipython/ipython/issues' |
|
71 | 71 | super(IPAppCrashHandler,self).__init__( |
|
72 | 72 | app, contact_name, contact_email, bug_tracker |
|
73 | 73 | ) |
|
74 | 74 | |
|
75 | 75 | def make_report(self,traceback): |
|
76 | 76 | """Return a string containing a crash report.""" |
|
77 | 77 | |
|
78 | 78 | sec_sep = self.section_sep |
|
79 | 79 | # Start with parent report |
|
80 | 80 | report = [super(IPAppCrashHandler, self).make_report(traceback)] |
|
81 | 81 | # Add interactive-specific info we may have |
|
82 | 82 | rpt_add = report.append |
|
83 | 83 | try: |
|
84 | 84 | rpt_add(sec_sep+"History of session input:") |
|
85 | 85 | for line in self.app.shell.user_ns['_ih']: |
|
86 | 86 | rpt_add(line) |
|
87 | 87 | rpt_add('\n*** Last line of input (may not be in above history):\n') |
|
88 | 88 | rpt_add(self.app.shell._last_input_line+'\n') |
|
89 | 89 | except: |
|
90 | 90 | pass |
|
91 | 91 | |
|
92 | 92 | return ''.join(report) |
|
93 | 93 | |
|
94 | 94 | #----------------------------------------------------------------------------- |
|
95 | 95 | # Aliases and Flags |
|
96 | 96 | #----------------------------------------------------------------------------- |
|
97 | 97 | flags = dict(base_flags) |
|
98 | 98 | flags.update(shell_flags) |
|
99 | 99 | frontend_flags = {} |
|
100 | 100 | addflag = lambda *args: frontend_flags.update(boolean_flag(*args)) |
|
101 | 101 | addflag('autoedit-syntax', 'TerminalInteractiveShell.autoedit_syntax', |
|
102 | 102 | 'Turn on auto editing of files with syntax errors.', |
|
103 | 103 | 'Turn off auto editing of files with syntax errors.' |
|
104 | 104 | ) |
|
105 | 105 | addflag('banner', 'TerminalIPythonApp.display_banner', |
|
106 | 106 | "Display a banner upon starting IPython.", |
|
107 | 107 | "Don't display a banner upon starting IPython." |
|
108 | 108 | ) |
|
109 | 109 | addflag('confirm-exit', 'TerminalInteractiveShell.confirm_exit', |
|
110 | 110 | """Set to confirm when you try to exit IPython with an EOF (Control-D |
|
111 | 111 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
112 | 112 | you can force a direct exit without any confirmation.""", |
|
113 | 113 | "Don't prompt the user when exiting." |
|
114 | 114 | ) |
|
115 | 115 | addflag('term-title', 'TerminalInteractiveShell.term_title', |
|
116 | 116 | "Enable auto setting the terminal title.", |
|
117 | 117 | "Disable auto setting the terminal title." |
|
118 | 118 | ) |
|
119 | 119 | classic_config = Config() |
|
120 | 120 | classic_config.InteractiveShell.cache_size = 0 |
|
121 | 121 | classic_config.PlainTextFormatter.pprint = False |
|
122 | 122 | classic_config.PromptManager.in_template = '>>> ' |
|
123 | 123 | classic_config.PromptManager.in2_template = '... ' |
|
124 | 124 | classic_config.PromptManager.out_template = '' |
|
125 | 125 | classic_config.InteractiveShell.separate_in = '' |
|
126 | 126 | classic_config.InteractiveShell.separate_out = '' |
|
127 | 127 | classic_config.InteractiveShell.separate_out2 = '' |
|
128 | 128 | classic_config.InteractiveShell.colors = 'NoColor' |
|
129 | 129 | classic_config.InteractiveShell.xmode = 'Plain' |
|
130 | 130 | |
|
131 | 131 | frontend_flags['classic']=( |
|
132 | 132 | classic_config, |
|
133 | 133 | "Gives IPython a similar feel to the classic Python prompt." |
|
134 | 134 | ) |
|
135 | 135 | # # log doesn't make so much sense this way anymore |
|
136 | 136 | # paa('--log','-l', |
|
137 | 137 | # action='store_true', dest='InteractiveShell.logstart', |
|
138 | 138 | # help="Start logging to the default log file (./ipython_log.py).") |
|
139 | 139 | # |
|
140 | 140 | # # quick is harder to implement |
|
141 | 141 | frontend_flags['quick']=( |
|
142 | 142 | {'TerminalIPythonApp' : {'quick' : True}}, |
|
143 | 143 | "Enable quick startup with no config files." |
|
144 | 144 | ) |
|
145 | 145 | |
|
146 | 146 | frontend_flags['i'] = ( |
|
147 | 147 | {'TerminalIPythonApp' : {'force_interact' : True}}, |
|
148 | 148 | """If running code from the command line, become interactive afterwards. |
|
149 | 149 | It is often useful to follow this with `--` to treat remaining flags as |
|
150 | 150 | script arguments. |
|
151 | 151 | """ |
|
152 | 152 | ) |
|
153 | 153 | flags.update(frontend_flags) |
|
154 | 154 | |
|
155 | 155 | aliases = dict(base_aliases) |
|
156 | 156 | aliases.update(shell_aliases) |
|
157 | 157 | |
|
158 | 158 | #----------------------------------------------------------------------------- |
|
159 | 159 | # Main classes and functions |
|
160 | 160 | #----------------------------------------------------------------------------- |
|
161 | 161 | |
|
162 | 162 | |
|
163 | 163 | class LocateIPythonApp(BaseIPythonApplication): |
|
164 | 164 | description = """print the path to the IPython dir""" |
|
165 | 165 | subcommands = Dict(dict( |
|
166 | 166 | profile=('IPython.core.profileapp.ProfileLocate', |
|
167 | 167 | "print the path to an IPython profile directory", |
|
168 | 168 | ), |
|
169 | 169 | )) |
|
170 | 170 | def start(self): |
|
171 | 171 | if self.subapp is not None: |
|
172 | 172 | return self.subapp.start() |
|
173 | 173 | else: |
|
174 | 174 | print(self.ipython_dir) |
|
175 | 175 | |
|
176 | 176 | |
|
177 | 177 | class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp): |
|
178 | 178 | name = u'ipython' |
|
179 | 179 | description = usage.cl_usage |
|
180 | 180 | crash_handler_class = IPAppCrashHandler |
|
181 | 181 | examples = _examples |
|
182 | 182 | |
|
183 | 183 | flags = Dict(flags) |
|
184 | 184 | aliases = Dict(aliases) |
|
185 | 185 | classes = List() |
|
186 | @default('classes') | |
|
186 | 187 | def _classes_default(self): |
|
187 | 188 | """This has to be in a method, for TerminalIPythonApp to be available.""" |
|
188 | 189 | return [ |
|
189 | 190 | InteractiveShellApp, # ShellApp comes before TerminalApp, because |
|
190 | 191 | self.__class__, # it will also affect subclasses (e.g. QtConsole) |
|
191 | 192 | TerminalInteractiveShell, |
|
192 | 193 | PromptManager, |
|
193 | 194 | HistoryManager, |
|
194 | 195 | ProfileDir, |
|
195 | 196 | PlainTextFormatter, |
|
196 | 197 | IPCompleter, |
|
197 | 198 | ScriptMagics, |
|
198 | 199 | StoreMagics, |
|
199 | 200 | ] |
|
200 | 201 | |
|
201 | 202 | deprecated_subcommands = dict( |
|
202 | 203 | qtconsole=('qtconsole.qtconsoleapp.JupyterQtConsoleApp', |
|
203 | 204 | """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter Qt Console.""" |
|
204 | 205 | ), |
|
205 | 206 | notebook=('notebook.notebookapp.NotebookApp', |
|
206 | 207 | """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter HTML Notebook Server.""" |
|
207 | 208 | ), |
|
208 | 209 | console=('jupyter_console.app.ZMQTerminalIPythonApp', |
|
209 | 210 | """DEPRECATED, Will be removed in IPython 6.0 : Launch the Jupyter terminal-based Console.""" |
|
210 | 211 | ), |
|
211 | 212 | nbconvert=('nbconvert.nbconvertapp.NbConvertApp', |
|
212 | 213 | "DEPRECATED, Will be removed in IPython 6.0 : Convert notebooks to/from other formats." |
|
213 | 214 | ), |
|
214 | 215 | trust=('nbformat.sign.TrustNotebookApp', |
|
215 | 216 | "DEPRECATED, Will be removed in IPython 6.0 : Sign notebooks to trust their potentially unsafe contents at load." |
|
216 | 217 | ), |
|
217 | 218 | kernelspec=('jupyter_client.kernelspecapp.KernelSpecApp', |
|
218 | 219 | "DEPRECATED, Will be removed in IPython 6.0 : Manage Jupyter kernel specifications." |
|
219 | 220 | ), |
|
220 | 221 | ) |
|
221 | 222 | subcommands = dict( |
|
222 | 223 | profile = ("IPython.core.profileapp.ProfileApp", |
|
223 | 224 | "Create and manage IPython profiles." |
|
224 | 225 | ), |
|
225 | 226 | kernel = ("ipykernel.kernelapp.IPKernelApp", |
|
226 | 227 | "Start a kernel without an attached frontend." |
|
227 | 228 | ), |
|
228 | 229 | locate=('IPython.terminal.ipapp.LocateIPythonApp', |
|
229 | 230 | LocateIPythonApp.description |
|
230 | 231 | ), |
|
231 | 232 | history=('IPython.core.historyapp.HistoryApp', |
|
232 | 233 | "Manage the IPython history database." |
|
233 | 234 | ), |
|
234 | 235 | ) |
|
235 | 236 | deprecated_subcommands['install-nbextension'] = ( |
|
236 | 237 | "notebook.nbextensions.InstallNBExtensionApp", |
|
237 | 238 | "DEPRECATED, Will be removed in IPython 6.0 : Install Jupyter notebook extension files" |
|
238 | 239 | ) |
|
239 | 240 | subcommands.update(deprecated_subcommands) |
|
240 | 241 | |
|
241 | 242 | # *do* autocreate requested profile, but don't create the config file. |
|
242 | 243 | auto_create=Bool(True) |
|
243 | 244 | # configurables |
|
244 |
quick = Bool(False, |
|
|
245 | quick = Bool(False, | |
|
245 | 246 | help="""Start IPython quickly by skipping the loading of config files.""" |
|
246 | ) | |
|
247 | def _quick_changed(self, name, old, new): | |
|
248 | if new: | |
|
247 | ).tag(config=True) | |
|
248 | @observe('quick') | |
|
249 | def _quick_changed(self, change): | |
|
250 | if change['new']: | |
|
249 | 251 | self.load_config_file = lambda *a, **kw: None |
|
250 | 252 | |
|
251 |
display_banner = Bool(True, |
|
|
253 | display_banner = Bool(True, | |
|
252 | 254 | help="Whether to display a banner upon starting IPython." |
|
253 | ) | |
|
255 | ).tag(config=True) | |
|
254 | 256 | |
|
255 | 257 | # if there is code of files to run from the cmd line, don't interact |
|
256 | 258 | # unless the --i flag (App.force_interact) is true. |
|
257 |
force_interact = Bool(False, |
|
|
259 | force_interact = Bool(False, | |
|
258 | 260 | help="""If a command or file is given via the command-line, |
|
259 | 261 | e.g. 'ipython foo.py', start an interactive shell after executing the |
|
260 | 262 | file or command.""" |
|
261 | ) | |
|
262 | def _force_interact_changed(self, name, old, new): | |
|
263 | if new: | |
|
263 | ).tag(config=True) | |
|
264 | @observe('force_interact') | |
|
265 | def _force_interact_changed(self, change): | |
|
266 | if change['new']: | |
|
264 | 267 | self.interact = True |
|
265 | 268 | |
|
266 | def _file_to_run_changed(self, name, old, new): | |
|
269 | @observe('file_to_run', 'code_to_run', 'module_to_run') | |
|
270 | def _file_to_run_changed(self, change): | |
|
271 | new = change['new'] | |
|
267 | 272 | if new: |
|
268 | 273 | self.something_to_run = True |
|
269 | 274 | if new and not self.force_interact: |
|
270 | 275 | self.interact = False |
|
271 | _code_to_run_changed = _file_to_run_changed | |
|
272 | _module_to_run_changed = _file_to_run_changed | |
|
273 | 276 | |
|
274 | 277 | # internal, not-configurable |
|
275 | 278 | something_to_run=Bool(False) |
|
276 | 279 | |
|
277 | 280 | def parse_command_line(self, argv=None): |
|
278 | 281 | """override to allow old '-pylab' flag with deprecation warning""" |
|
279 | 282 | |
|
280 | 283 | argv = sys.argv[1:] if argv is None else argv |
|
281 | 284 | |
|
282 | 285 | if '-pylab' in argv: |
|
283 | 286 | # deprecated `-pylab` given, |
|
284 | 287 | # warn and transform into current syntax |
|
285 | 288 | argv = argv[:] # copy, don't clobber |
|
286 | 289 | idx = argv.index('-pylab') |
|
287 | warn.warn("`-pylab` flag has been deprecated.\n" | |
|
290 | warnings.warn("`-pylab` flag has been deprecated.\n" | |
|
288 | 291 | " Use `--matplotlib <backend>` and import pylab manually.") |
|
289 | 292 | argv[idx] = '--pylab' |
|
290 | 293 | |
|
291 | 294 | return super(TerminalIPythonApp, self).parse_command_line(argv) |
|
292 | 295 | |
|
293 | 296 | @catch_config_error |
|
294 | 297 | def initialize(self, argv=None): |
|
295 | 298 | """Do actions after construct, but before starting the app.""" |
|
296 | 299 | super(TerminalIPythonApp, self).initialize(argv) |
|
297 | 300 | if self.subapp is not None: |
|
298 | 301 | # don't bother initializing further, starting subapp |
|
299 | 302 | return |
|
300 | 303 | # print self.extra_args |
|
301 | 304 | if self.extra_args and not self.something_to_run: |
|
302 | 305 | self.file_to_run = self.extra_args[0] |
|
303 | 306 | self.init_path() |
|
304 | 307 | # create the shell |
|
305 | 308 | self.init_shell() |
|
306 | 309 | # and draw the banner |
|
307 | 310 | self.init_banner() |
|
308 | 311 | # Now a variety of things that happen after the banner is printed. |
|
309 | 312 | self.init_gui_pylab() |
|
310 | 313 | self.init_extensions() |
|
311 | 314 | self.init_code() |
|
312 | 315 | |
|
313 | 316 | def init_shell(self): |
|
314 | 317 | """initialize the InteractiveShell instance""" |
|
315 | 318 | # Create an InteractiveShell instance. |
|
316 | 319 | # shell.display_banner should always be False for the terminal |
|
317 | 320 | # based app, because we call shell.show_banner() by hand below |
|
318 | 321 | # so the banner shows *before* all extension loading stuff. |
|
319 | 322 | self.shell = TerminalInteractiveShell.instance(parent=self, |
|
320 | 323 | profile_dir=self.profile_dir, |
|
321 | 324 | ipython_dir=self.ipython_dir, user_ns=self.user_ns) |
|
322 | 325 | self.shell.configurables.append(self) |
|
323 | 326 | |
|
324 | 327 | def init_banner(self): |
|
325 | 328 | """optionally display the banner""" |
|
326 | 329 | if self.display_banner and self.interact: |
|
327 | 330 | self.shell.show_banner() |
|
328 | 331 | # Make sure there is a space below the banner. |
|
329 | 332 | if self.log_level <= logging.INFO: print() |
|
330 | 333 | |
|
331 | 334 | def _pylab_changed(self, name, old, new): |
|
332 | 335 | """Replace --pylab='inline' with --pylab='auto'""" |
|
333 | 336 | if new == 'inline': |
|
334 | warn.warn("'inline' not available as pylab backend, " | |
|
337 | warnings.warn("'inline' not available as pylab backend, " | |
|
335 | 338 | "using 'auto' instead.") |
|
336 | 339 | self.pylab = 'auto' |
|
337 | 340 | |
|
338 | 341 | def start(self): |
|
339 | 342 | if self.subapp is not None: |
|
340 | 343 | return self.subapp.start() |
|
341 | 344 | # perform any prexec steps: |
|
342 | 345 | if self.interact: |
|
343 | 346 | self.log.debug("Starting IPython's mainloop...") |
|
344 | 347 | self.shell.mainloop() |
|
345 | 348 | else: |
|
346 | 349 | self.log.debug("IPython not interactive...") |
|
347 | 350 | |
|
348 | 351 | def load_default_config(ipython_dir=None): |
|
349 | 352 | """Load the default config file from the default ipython_dir. |
|
350 | 353 | |
|
351 | 354 | This is useful for embedded shells. |
|
352 | 355 | """ |
|
353 | 356 | if ipython_dir is None: |
|
354 | 357 | ipython_dir = get_ipython_dir() |
|
355 | 358 | |
|
356 | 359 | profile_dir = os.path.join(ipython_dir, 'profile_default') |
|
357 | 360 | |
|
358 | 361 | config = Config() |
|
359 | 362 | for cf in Application._load_config_files("ipython_config", path=profile_dir): |
|
360 | 363 | config.update(cf) |
|
361 | 364 | |
|
362 | 365 | return config |
|
363 | 366 | |
|
364 | 367 | launch_new_instance = TerminalIPythonApp.launch_instance |
|
365 | 368 | |
|
366 | 369 | |
|
367 | 370 | if __name__ == '__main__': |
|
368 | 371 | launch_new_instance() |
@@ -1,464 +1,467 b'' | |||
|
1 | 1 | """IPython terminal interface using prompt_toolkit in place of readline""" |
|
2 | 2 | from __future__ import print_function |
|
3 | 3 | |
|
4 | 4 | import os |
|
5 | 5 | import sys |
|
6 | 6 | import signal |
|
7 | 7 | import unicodedata |
|
8 | 8 | from warnings import warn |
|
9 | 9 | from wcwidth import wcwidth |
|
10 | 10 | |
|
11 | 11 | from IPython.core.error import TryNext |
|
12 | 12 | from IPython.core.interactiveshell import InteractiveShell |
|
13 | 13 | from IPython.utils.py3compat import PY3, cast_unicode_py2, input |
|
14 | 14 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
15 | 15 | from IPython.utils.process import abbrev_cwd |
|
16 |
from traitlets import Bool |
|
|
16 | from traitlets import Bool, Unicode, Dict, Integer, observe | |
|
17 | 17 | |
|
18 | 18 | from prompt_toolkit.completion import Completer, Completion |
|
19 | 19 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER, EditingMode |
|
20 | 20 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode |
|
21 | 21 | from prompt_toolkit.history import InMemoryHistory |
|
22 | 22 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout |
|
23 | 23 | from prompt_toolkit.interface import CommandLineInterface |
|
24 | 24 | from prompt_toolkit.key_binding.manager import KeyBindingManager |
|
25 | 25 | from prompt_toolkit.keys import Keys |
|
26 | 26 | from prompt_toolkit.layout.lexers import Lexer |
|
27 | 27 | from prompt_toolkit.layout.lexers import PygmentsLexer |
|
28 | 28 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle |
|
29 | 29 | |
|
30 | 30 | from pygments.styles import get_style_by_name, get_all_styles |
|
31 | 31 | from pygments.lexers import Python3Lexer, BashLexer, PythonLexer |
|
32 | 32 | from pygments.token import Token |
|
33 | 33 | |
|
34 | 34 | from .pt_inputhooks import get_inputhook_func |
|
35 | 35 | from .interactiveshell import get_default_editor, TerminalMagics |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | class IPythonPTCompleter(Completer): |
|
39 | 39 | """Adaptor to provide IPython completions to prompt_toolkit""" |
|
40 | 40 | def __init__(self, ipy_completer): |
|
41 | 41 | self.ipy_completer = ipy_completer |
|
42 | 42 | |
|
43 | 43 | def get_completions(self, document, complete_event): |
|
44 | 44 | if not document.current_line.strip(): |
|
45 | 45 | return |
|
46 | 46 | |
|
47 | 47 | used, matches = self.ipy_completer.complete( |
|
48 | 48 | line_buffer=document.current_line, |
|
49 | 49 | cursor_pos=document.cursor_position_col |
|
50 | 50 | ) |
|
51 | 51 | start_pos = -len(used) |
|
52 | 52 | for m in matches: |
|
53 | 53 | m = unicodedata.normalize('NFC', m) |
|
54 | 54 | |
|
55 | 55 | # When the first character of the completion has a zero length, |
|
56 | 56 | # then it's probably a decomposed unicode character. E.g. caused by |
|
57 | 57 | # the "\dot" completion. Try to compose again with the previous |
|
58 | 58 | # character. |
|
59 | 59 | if wcwidth(m[0]) == 0: |
|
60 | 60 | if document.cursor_position + start_pos > 0: |
|
61 | 61 | char_before = document.text[document.cursor_position + start_pos - 1] |
|
62 | 62 | m = unicodedata.normalize('NFC', char_before + m) |
|
63 | 63 | |
|
64 | 64 | # Yield the modified completion instead, if this worked. |
|
65 | 65 | if wcwidth(m[0:1]) == 1: |
|
66 | 66 | yield Completion(m, start_position=start_pos - 1) |
|
67 | 67 | continue |
|
68 | 68 | |
|
69 | 69 | # TODO: Use Jedi to determine meta_text |
|
70 | 70 | # (Jedi currently has a bug that results in incorrect information.) |
|
71 | 71 | # meta_text = '' |
|
72 | 72 | # yield Completion(m, start_position=start_pos, |
|
73 | 73 | # display_meta=meta_text) |
|
74 | 74 | yield Completion(m, start_position=start_pos) |
|
75 | 75 | |
|
76 | 76 | class IPythonPTLexer(Lexer): |
|
77 | 77 | """ |
|
78 | 78 | Wrapper around PythonLexer and BashLexer. |
|
79 | 79 | """ |
|
80 | 80 | def __init__(self): |
|
81 | 81 | self.python_lexer = PygmentsLexer(Python3Lexer if PY3 else PythonLexer) |
|
82 | 82 | self.shell_lexer = PygmentsLexer(BashLexer) |
|
83 | 83 | |
|
84 | 84 | def lex_document(self, cli, document): |
|
85 | 85 | if document.text.startswith('!'): |
|
86 | 86 | return self.shell_lexer.lex_document(cli, document) |
|
87 | 87 | else: |
|
88 | 88 | return self.python_lexer.lex_document(cli, document) |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | class TerminalInteractiveShell(InteractiveShell): |
|
92 | 92 | colors_force = True |
|
93 | 93 | |
|
94 | 94 | space_for_menu = Integer(6, help='Number of line at the bottom of the screen ' |
|
95 | 95 | 'to reserve for the completion menu' |
|
96 | 96 | ).tag(config=True) |
|
97 | 97 | |
|
98 | 98 | def _space_for_menu_changed(self, old, new): |
|
99 | 99 | self._update_layout() |
|
100 | 100 | |
|
101 | 101 | pt_cli = None |
|
102 | 102 | |
|
103 |
autoedit_syntax = |
|
|
104 |
|
|
|
103 | autoedit_syntax = Bool(False, | |
|
104 | help="auto editing of files with syntax errors.", | |
|
105 | ).tag(config=True) | |
|
106 | ||
|
105 | 107 | |
|
106 |
confirm_exit = |
|
|
108 | confirm_exit = Bool(True, | |
|
107 | 109 | help=""" |
|
108 | 110 | Set to confirm when you try to exit IPython with an EOF (Control-D |
|
109 | 111 | in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit', |
|
110 | 112 | you can force a direct exit without any confirmation.""", |
|
111 | ) | |
|
113 | ).tag(config=True) | |
|
114 | ||
|
112 | 115 | editing_mode = Unicode('emacs', |
|
113 | 116 | help="Shortcut style to use at the prompt. 'vi' or 'emacs'.", |
|
114 | 117 | ).tag(config=True) |
|
115 | 118 | |
|
116 | 119 | mouse_support = Bool(False, |
|
117 | 120 | help="Enable mouse support in the prompt" |
|
118 | 121 | ).tag(config=True) |
|
119 | 122 | |
|
120 | 123 | highlighting_style = Unicode('default', |
|
121 | 124 | help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles()) |
|
122 | 125 | ).tag(config=True) |
|
123 | 126 | |
|
124 | def _highlighting_style_changed(self, old, new): | |
|
127 | ||
|
128 | @observe('highlighting_style') | |
|
129 | def _highlighting_style_changed(self, change): | |
|
125 | 130 | self._style = self._make_style_from_name(self.highlighting_style) |
|
126 | 131 | |
|
127 | 132 | highlighting_style_overrides = Dict( |
|
128 | 133 | help="Override highlighting format for specific tokens" |
|
129 | 134 | ).tag(config=True) |
|
130 | 135 | |
|
131 | 136 | editor = Unicode(get_default_editor(), |
|
132 | 137 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." |
|
133 | 138 | ).tag(config=True) |
|
134 | 139 | |
|
135 | 140 | term_title = Bool(True, |
|
136 | 141 | help="Automatically set the terminal title" |
|
137 | 142 | ).tag(config=True) |
|
138 | 143 | |
|
139 | 144 | display_completions_in_columns = Bool(False, |
|
140 | 145 | help="Display a multi column completion menu.", |
|
141 | 146 | ).tag(config=True) |
|
142 | 147 | |
|
143 | @observe('term_title') | |
|
144 | def _term_title_changed(self, change): | |
|
145 | self.init_term_title() | |
|
146 | 148 | |
|
147 | def init_term_title(self): | |
|
149 | @observe('term_title') | |
|
150 | def init_term_title(self, change=None): | |
|
148 | 151 | # Enable or disable the terminal title. |
|
149 | 152 | if self.term_title: |
|
150 | 153 | toggle_set_term_title(True) |
|
151 | 154 | set_term_title('IPython: ' + abbrev_cwd()) |
|
152 | 155 | else: |
|
153 | 156 | toggle_set_term_title(False) |
|
154 | 157 | |
|
155 | 158 | def get_prompt_tokens(self, cli): |
|
156 | 159 | return [ |
|
157 | 160 | (Token.Prompt, 'In ['), |
|
158 | 161 | (Token.PromptNum, str(self.execution_count)), |
|
159 | 162 | (Token.Prompt, ']: '), |
|
160 | 163 | ] |
|
161 | 164 | |
|
162 | 165 | def get_continuation_tokens(self, cli, width): |
|
163 | 166 | return [ |
|
164 | 167 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
165 | 168 | ] |
|
166 | 169 | |
|
167 | 170 | def init_prompt_toolkit_cli(self): |
|
168 | 171 | if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty(): |
|
169 | 172 | # Fall back to plain non-interactive output for tests. |
|
170 | 173 | # This is very limited, and only accepts a single line. |
|
171 | 174 | def prompt(): |
|
172 | 175 | return cast_unicode_py2(input('In [%d]: ' % self.execution_count)) |
|
173 | 176 | self.prompt_for_code = prompt |
|
174 | 177 | return |
|
175 | 178 | |
|
176 | 179 | kbmanager = KeyBindingManager.for_prompt() |
|
177 | 180 | insert_mode = ViInsertMode() | EmacsInsertMode() |
|
178 | 181 | # Ctrl+J == Enter, seemingly |
|
179 | 182 | @kbmanager.registry.add_binding(Keys.ControlJ, |
|
180 | 183 | filter=(HasFocus(DEFAULT_BUFFER) |
|
181 | 184 | & ~HasSelection() |
|
182 | 185 | & insert_mode |
|
183 | 186 | )) |
|
184 | 187 | def _(event): |
|
185 | 188 | b = event.current_buffer |
|
186 | 189 | d = b.document |
|
187 | 190 | if not (d.on_last_line or d.cursor_position_row >= d.line_count |
|
188 | 191 | - d.empty_line_count_at_the_end()): |
|
189 | 192 | b.newline() |
|
190 | 193 | return |
|
191 | 194 | |
|
192 | 195 | status, indent = self.input_splitter.check_complete(d.text) |
|
193 | 196 | |
|
194 | 197 | if (status != 'incomplete') and b.accept_action.is_returnable: |
|
195 | 198 | b.accept_action.validate_and_handle(event.cli, b) |
|
196 | 199 | else: |
|
197 | 200 | b.insert_text('\n' + (' ' * (indent or 0))) |
|
198 | 201 | |
|
199 | 202 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)) |
|
200 | 203 | def _reset_buffer(event): |
|
201 | 204 | event.current_buffer.reset() |
|
202 | 205 | |
|
203 | 206 | @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)) |
|
204 | 207 | def _reset_search_buffer(event): |
|
205 | 208 | if event.current_buffer.document.text: |
|
206 | 209 | event.current_buffer.reset() |
|
207 | 210 | else: |
|
208 | 211 | event.cli.push_focus(DEFAULT_BUFFER) |
|
209 | 212 | |
|
210 | 213 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) |
|
211 | 214 | |
|
212 | 215 | @kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend) |
|
213 | 216 | def _suspend_to_bg(event): |
|
214 | 217 | event.cli.suspend_to_background() |
|
215 | 218 | |
|
216 | 219 | @Condition |
|
217 | 220 | def cursor_in_leading_ws(cli): |
|
218 | 221 | before = cli.application.buffer.document.current_line_before_cursor |
|
219 | 222 | return (not before) or before.isspace() |
|
220 | 223 | |
|
221 | 224 | # Ctrl+I == Tab |
|
222 | 225 | @kbmanager.registry.add_binding(Keys.ControlI, |
|
223 | 226 | filter=(HasFocus(DEFAULT_BUFFER) |
|
224 | 227 | & ~HasSelection() |
|
225 | 228 | & insert_mode |
|
226 | 229 | & cursor_in_leading_ws |
|
227 | 230 | )) |
|
228 | 231 | def _indent_buffer(event): |
|
229 | 232 | event.current_buffer.insert_text(' ' * 4) |
|
230 | 233 | |
|
231 | 234 | # Pre-populate history from IPython's history database |
|
232 | 235 | history = InMemoryHistory() |
|
233 | 236 | last_cell = u"" |
|
234 | 237 | for __, ___, cell in self.history_manager.get_tail(self.history_load_length, |
|
235 | 238 | include_latest=True): |
|
236 | 239 | # Ignore blank lines and consecutive duplicates |
|
237 | 240 | cell = cell.rstrip() |
|
238 | 241 | if cell and (cell != last_cell): |
|
239 | 242 | history.append(cell) |
|
240 | 243 | |
|
241 | 244 | self._style = self._make_style_from_name(self.highlighting_style) |
|
242 | 245 | style = DynamicStyle(lambda: self._style) |
|
243 | 246 | |
|
244 | 247 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) |
|
245 | 248 | |
|
246 | 249 | self._app = create_prompt_application( |
|
247 | 250 | editing_mode=editing_mode, |
|
248 | 251 | key_bindings_registry=kbmanager.registry, |
|
249 | 252 | history=history, |
|
250 | 253 | completer=IPythonPTCompleter(self.Completer), |
|
251 | 254 | enable_history_search=True, |
|
252 | 255 | style=style, |
|
253 | 256 | mouse_support=self.mouse_support, |
|
254 | 257 | **self._layout_options() |
|
255 | 258 | ) |
|
256 | 259 | self.pt_cli = CommandLineInterface(self._app, |
|
257 | 260 | eventloop=create_eventloop(self.inputhook)) |
|
258 | 261 | |
|
259 | 262 | def _make_style_from_name(self, name): |
|
260 | 263 | """ |
|
261 | 264 | Small wrapper that make an IPython compatible style from a style name |
|
262 | 265 | |
|
263 | 266 | We need that to add style for prompt ... etc. |
|
264 | 267 | """ |
|
265 | 268 | style_cls = get_style_by_name(name) |
|
266 | 269 | style_overrides = { |
|
267 | 270 | Token.Prompt: '#009900', |
|
268 | 271 | Token.PromptNum: '#00ff00 bold', |
|
269 | 272 | } |
|
270 | 273 | if name == 'default': |
|
271 | 274 | style_cls = get_style_by_name('default') |
|
272 | 275 | # The default theme needs to be visible on both a dark background |
|
273 | 276 | # and a light background, because we can't tell what the terminal |
|
274 | 277 | # looks like. These tweaks to the default theme help with that. |
|
275 | 278 | style_overrides.update({ |
|
276 | 279 | Token.Number: '#007700', |
|
277 | 280 | Token.Operator: 'noinherit', |
|
278 | 281 | Token.String: '#BB6622', |
|
279 | 282 | Token.Name.Function: '#2080D0', |
|
280 | 283 | Token.Name.Class: 'bold #2080D0', |
|
281 | 284 | Token.Name.Namespace: 'bold #2080D0', |
|
282 | 285 | }) |
|
283 | 286 | style_overrides.update(self.highlighting_style_overrides) |
|
284 | 287 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, |
|
285 | 288 | style_dict=style_overrides) |
|
286 | 289 | |
|
287 | 290 | return style |
|
288 | 291 | |
|
289 | 292 | def _layout_options(self): |
|
290 | 293 | """ |
|
291 | 294 | Return the current layout option for the current Terminal InteractiveShell |
|
292 | 295 | """ |
|
293 | 296 | return { |
|
294 | 297 | 'lexer':IPythonPTLexer(), |
|
295 | 298 | 'reserve_space_for_menu':self.space_for_menu, |
|
296 | 299 | 'get_prompt_tokens':self.get_prompt_tokens, |
|
297 | 300 | 'get_continuation_tokens':self.get_continuation_tokens, |
|
298 | 301 | 'multiline':True, |
|
299 | 302 | 'display_completions_in_columns': self.display_completions_in_columns, |
|
300 | 303 | } |
|
301 | 304 | |
|
302 | 305 | def _update_layout(self): |
|
303 | 306 | """ |
|
304 | 307 | Ask for a re computation of the application layout, if for example , |
|
305 | 308 | some configuration options have changed. |
|
306 | 309 | """ |
|
307 | 310 | self._app.layout = create_prompt_layout(**self._layout_options()) |
|
308 | 311 | |
|
309 | 312 | def prompt_for_code(self): |
|
310 | 313 | document = self.pt_cli.run( |
|
311 | 314 | pre_run=self.pre_prompt, reset_current_buffer=True) |
|
312 | 315 | return document.text |
|
313 | 316 | |
|
314 | 317 | def init_io(self): |
|
315 | 318 | if sys.platform not in {'win32', 'cli'}: |
|
316 | 319 | return |
|
317 | 320 | |
|
318 | 321 | import colorama |
|
319 | 322 | colorama.init() |
|
320 | 323 | |
|
321 | 324 | # For some reason we make these wrappers around stdout/stderr. |
|
322 | 325 | # For now, we need to reset them so all output gets coloured. |
|
323 | 326 | # https://github.com/ipython/ipython/issues/8669 |
|
324 | 327 | from IPython.utils import io |
|
325 | 328 | io.stdout = io.IOStream(sys.stdout) |
|
326 | 329 | io.stderr = io.IOStream(sys.stderr) |
|
327 | 330 | |
|
328 | 331 | def init_magics(self): |
|
329 | 332 | super(TerminalInteractiveShell, self).init_magics() |
|
330 | 333 | self.register_magics(TerminalMagics) |
|
331 | 334 | |
|
332 | 335 | def init_alias(self): |
|
333 | 336 | # The parent class defines aliases that can be safely used with any |
|
334 | 337 | # frontend. |
|
335 | 338 | super(TerminalInteractiveShell, self).init_alias() |
|
336 | 339 | |
|
337 | 340 | # Now define aliases that only make sense on the terminal, because they |
|
338 | 341 | # need direct access to the console in a way that we can't emulate in |
|
339 | 342 | # GUI or web frontend |
|
340 | 343 | if os.name == 'posix': |
|
341 | 344 | for cmd in ['clear', 'more', 'less', 'man']: |
|
342 | 345 | self.alias_manager.soft_define_alias(cmd, cmd) |
|
343 | 346 | |
|
344 | 347 | |
|
345 | 348 | def __init__(self, *args, **kwargs): |
|
346 | 349 | super(TerminalInteractiveShell, self).__init__(*args, **kwargs) |
|
347 | 350 | self.init_prompt_toolkit_cli() |
|
348 | 351 | self.init_term_title() |
|
349 | 352 | self.keep_running = True |
|
350 | 353 | |
|
351 | 354 | def ask_exit(self): |
|
352 | 355 | self.keep_running = False |
|
353 | 356 | |
|
354 | 357 | rl_next_input = None |
|
355 | 358 | |
|
356 | 359 | def pre_prompt(self): |
|
357 | 360 | if self.rl_next_input: |
|
358 | 361 | self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input) |
|
359 | 362 | self.rl_next_input = None |
|
360 | 363 | |
|
361 | 364 | def interact(self): |
|
362 | 365 | while self.keep_running: |
|
363 | 366 | print(self.separate_in, end='') |
|
364 | 367 | |
|
365 | 368 | try: |
|
366 | 369 | code = self.prompt_for_code() |
|
367 | 370 | except EOFError: |
|
368 | 371 | if (not self.confirm_exit) \ |
|
369 | 372 | or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'): |
|
370 | 373 | self.ask_exit() |
|
371 | 374 | |
|
372 | 375 | else: |
|
373 | 376 | if code: |
|
374 | 377 | self.run_cell(code, store_history=True) |
|
375 | 378 | if self.autoedit_syntax and self.SyntaxTB.last_syntax_error: |
|
376 | 379 | self.edit_syntax_error() |
|
377 | 380 | |
|
378 | 381 | def mainloop(self): |
|
379 | 382 | # An extra layer of protection in case someone mashing Ctrl-C breaks |
|
380 | 383 | # out of our internal code. |
|
381 | 384 | while True: |
|
382 | 385 | try: |
|
383 | 386 | self.interact() |
|
384 | 387 | break |
|
385 | 388 | except KeyboardInterrupt: |
|
386 | 389 | print("\nKeyboardInterrupt escaped interact()\n") |
|
387 | 390 | |
|
388 | 391 | _inputhook = None |
|
389 | 392 | def inputhook(self, context): |
|
390 | 393 | if self._inputhook is not None: |
|
391 | 394 | self._inputhook(context) |
|
392 | 395 | |
|
393 | 396 | def enable_gui(self, gui=None): |
|
394 | 397 | if gui: |
|
395 | 398 | self._inputhook = get_inputhook_func(gui) |
|
396 | 399 | else: |
|
397 | 400 | self._inputhook = None |
|
398 | 401 | |
|
399 | 402 | # Methods to support auto-editing of SyntaxErrors: |
|
400 | 403 | |
|
401 | 404 | def edit_syntax_error(self): |
|
402 | 405 | """The bottom half of the syntax error handler called in the main loop. |
|
403 | 406 | |
|
404 | 407 | Loop until syntax error is fixed or user cancels. |
|
405 | 408 | """ |
|
406 | 409 | |
|
407 | 410 | while self.SyntaxTB.last_syntax_error: |
|
408 | 411 | # copy and clear last_syntax_error |
|
409 | 412 | err = self.SyntaxTB.clear_err_state() |
|
410 | 413 | if not self._should_recompile(err): |
|
411 | 414 | return |
|
412 | 415 | try: |
|
413 | 416 | # may set last_syntax_error again if a SyntaxError is raised |
|
414 | 417 | self.safe_execfile(err.filename, self.user_ns) |
|
415 | 418 | except: |
|
416 | 419 | self.showtraceback() |
|
417 | 420 | else: |
|
418 | 421 | try: |
|
419 | 422 | with open(err.filename) as f: |
|
420 | 423 | # This should be inside a display_trap block and I |
|
421 | 424 | # think it is. |
|
422 | 425 | sys.displayhook(f.read()) |
|
423 | 426 | except: |
|
424 | 427 | self.showtraceback() |
|
425 | 428 | |
|
426 | 429 | def _should_recompile(self, e): |
|
427 | 430 | """Utility routine for edit_syntax_error""" |
|
428 | 431 | |
|
429 | 432 | if e.filename in ('<ipython console>', '<input>', '<string>', |
|
430 | 433 | '<console>', '<BackgroundJob compilation>', |
|
431 | 434 | None): |
|
432 | 435 | return False |
|
433 | 436 | try: |
|
434 | 437 | if (self.autoedit_syntax and |
|
435 | 438 | not self.ask_yes_no( |
|
436 | 439 | 'Return to editor to correct syntax error? ' |
|
437 | 440 | '[Y/n] ', 'y')): |
|
438 | 441 | return False |
|
439 | 442 | except EOFError: |
|
440 | 443 | return False |
|
441 | 444 | |
|
442 | 445 | def int0(x): |
|
443 | 446 | try: |
|
444 | 447 | return int(x) |
|
445 | 448 | except TypeError: |
|
446 | 449 | return 0 |
|
447 | 450 | |
|
448 | 451 | # always pass integer line and offset values to editor hook |
|
449 | 452 | try: |
|
450 | 453 | self.hooks.fix_error_editor(e.filename, |
|
451 | 454 | int0(e.lineno), int0(e.offset), |
|
452 | 455 | e.msg) |
|
453 | 456 | except TryNext: |
|
454 | 457 | warn('Could not open editor') |
|
455 | 458 | return False |
|
456 | 459 | return True |
|
457 | 460 | |
|
458 | 461 | # Run !system commands directly, not through pipes, so terminal programs |
|
459 | 462 | # work correctly. |
|
460 | 463 | system = InteractiveShell.system_raw |
|
461 | 464 | |
|
462 | 465 | |
|
463 | 466 | if __name__ == '__main__': |
|
464 | 467 | TerminalInteractiveShell.instance().interact() |
@@ -1,26 +1,26 b'' | |||
|
1 | 1 | #***************************************************************************** |
|
2 | 2 | # Copyright (C) 2016 The IPython Team <ipython-dev@scipy.org> |
|
3 | 3 | # |
|
4 | 4 | # Distributed under the terms of the BSD License. The full license is in |
|
5 | 5 | # the file COPYING, distributed as part of this software. |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | from __future__ import absolute_import |
|
8 | 8 | |
|
9 | 9 | """ |
|
10 | 10 | Color managing related utilities |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | import pygments |
|
14 | 14 | |
|
15 | 15 | from traitlets.config import Configurable |
|
16 | 16 | from traitlets import Unicode |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | available_themes = lambda : [s for s in pygments.styles.get_all_styles()]+['NoColor','LightBG','Linux'] |
|
20 | 20 | |
|
21 | 21 | class Colorable(Configurable): |
|
22 | 22 | """ |
|
23 | 23 | A subclass of configurable for all the classes that have a `default_scheme` |
|
24 | 24 | """ |
|
25 |
default_style=Unicode('lightbg' |
|
|
25 | default_style=Unicode('lightbg').tag(config=True) | |
|
26 | 26 |
General Comments 0
You need to be logged in to leave comments.
Login now