##// END OF EJS Templates
adopt traitlets 4.2 API...
Min RK -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -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(), config=True)
196 user_aliases = List(default_value=[], config=True)
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(config=True,
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', config=True,
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(config=True,
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('ipython_dir', d, d)
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, config=True,
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, config=True,
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, config=True,
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 CBool, Enum
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 = CBool(False, config=True,
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 as e:
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 as e:
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 = CBool(True, config=True,
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, config=True,
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__ = CBool(default_value=False, config=True,
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 as e:
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()).tag(config=True,
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 def _ipython_display_formatter_default(self):
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).tag(config=True,
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(config=True,
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, config=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(config=True,
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, config=True,
501 db_log_output = Bool(False,
498 502 help="Should the history database include output? (default: no)"
499 )
500 db_cache_size = Integer(0, config=True,
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 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -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, config=True, help=
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(config=True,
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(config=True,
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, CBool, Bool, Instance, CRegExp
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 = CBool(True).tag(config=True)
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'', config=True,
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, name, old, new):
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 self.check_startup_dir()
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 self.check_security_dir()
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.on_trait_change(self._update_prompt_trait, ['in_template',
317 'in2_template', 'out_template'])
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(), config=True,
138 extensions = List(Unicode(),
139 139 help="A list of dotted module names of IPython extensions to load."
140 )
141 extra_extension = Unicode('', config=True,
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'], config=False)
150 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
153 151
154 hide_initial_ns = Bool(True, config=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(), config=True,
157 exec_files = List(Unicode(),
160 158 help="""List of files to run at IPython startup."""
161 )
162 exec_PYTHONSTARTUP = Bool(True, config=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('', config=True,
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(), config=True,
167 exec_lines = List(Unicode(),
170 168 help="""lines of code to run at IPython startup."""
171 )
172 code_to_run = Unicode('', config=True,
169 ).tag(config=True)
170 code_to_run = Unicode('',
173 171 help="Execute the given command string."
174 )
175 module_to_run = Unicode('', config=True,
172 ).tag(config=True)
173 module_to_run = Unicode('',
176 174 help="Run the module as a script."
177 )
178 gui = CaselessStrEnum(gui_keys, config=True, allow_none=True,
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, config=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 # Copyright (c) 2012, The IPython Development Team.
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, config=True, help=
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, config=True,
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, config=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, config=True,
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, CBool, Unicode, Dict, Integer, observe
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 = CBool(False).tag(config=True,
104 help="auto editing of files with syntax errors.")
103 autoedit_syntax = Bool(False,
104 help="auto editing of files with syntax errors.",
105 ).tag(config=True)
106
105 107
106 confirm_exit = CBool(True).tag(config=True,
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', config=True)
25 default_style=Unicode('lightbg').tag(config=True)
26 26
General Comments 0
You need to be logged in to leave comments. Login now