##// END OF EJS Templates
Massive refactoring of of the core....
Brian Granger -
Show More
@@ -1,120 +1,115 b''
1 1 #-----------------------------------------------------------------------------
2 # IPython Shell Configuration Defaults
2 # Global options
3 3 #-----------------------------------------------------------------------------
4 4
5 Global.classic = False
6 Global.nosep = False
7
5 8 #-----------------------------------------------------------------------------
6 # Startup
9 # InteractiveShell options
7 10 #-----------------------------------------------------------------------------
8 11
9 AUTOCALL = True
10
11 AUTOEDIT_SYNTAX = False
12
13 AUTOINDENT = True
14
15 AUTOMAGIC = True
16
17 CACHE_SIZE = 1000
18 12
19 CLASSIC = False
13 InteractiveShell.autocall = 1
20 14
21 COLORS = 'Linux'
15 InteractiveShell.autoedit_syntax = False
22 16
23 COLOR_INFO = True
17 InteractiveShell.autoindent = True
24 18
25 CONFIRM_EXIT = True
19 InteractiveShell.automagic = False
26 20
27 DEEP_RELOAD = False
21 InteractiveShell.banner1 = 'This if for overriding the default IPython banner'
28 22
29 EDITOR = 0
23 InteractiveShell.banner2 = "This is for extra banner text"
30 24
31 LOG = True
25 InteractiveShell.cache_size = 1000
32 26
33 LOGFILE = ''
27 InteractiveShell.colors = 'LightBG'
34 28
35 BANNER = True
29 InteractiveShell.color_info = True
36 30
37 MESSAGES = True
31 InteractiveShell.confirm_exit = True
38 32
39 PDB = False
33 InteractiveShell.deep_reload = False
40 34
41 PPRINT = True
35 InteractiveShell.display_banner = True
42 36
43 PROMPT_IN1 = 'In [\#]: '
37 InteractiveShell.editor = 'nano'
44 38
45 PROMPT_IN2 = ' .\D.: '
39 InteractiveShell.logstart = True
46 40
47 PROMPT_OUT = 'Out[\#]: '
41 InteractiveShell.logfile = 'ipython_log.py'
48 42
49 PROMPTS_PAD_LEFT = True
43 InteractiveShell.logplay = 'mylog.py'
50 44
51 QUICK = False
45 InteractiveShell.object_info_string_level = 0
52 46
53 SCREEN_LENGTH = 0
47 InteractiveShell.pager = 'less'
54 48
55 SEPARATE_IN = '\n'
56 SEPARATE_OUT = ''
57 SEPARATE_OUT2 = ''
58 NOSEP = False
49 InteractiveShell.pdb = False
59 50
60 WILDCARDS_CASE_SENSITIVE = True
51 InteractiveShell.pprint = True
61 52
62 OBJECT_INFO_STRING_LEVEL = 0
53 InteractiveShell.prompt_in1 = 'In [\#]: '
54 InteractiveShell.prompt_in2 = ' .\D.: '
55 InteractiveShell.prompt_out = 'Out[\#]: '
56 InteractiveShell.prompts_pad_left = True
63 57
64 XMODE = 'Context'
58 InteractiveShell.quiet = False
65 59
66 MULTI_LINE_SPECIALS = True
67
68 SYSTEM_HEADER = "IPython system call: "
69
70 SYSTEM_VERBOSE = True
71
72 #-----------------------------------------------------------------------------
73 60 # Readline
74 #-----------------------------------------------------------------------------
75
76 READLINE = True
61 InteractiveShell.readline_use = False
77 62
78 READLINE_PARSE_AND_BIND = [
63 InteractiveShell.readline_parse_and_bind = [
79 64 'tab: complete',
80 65 '"\C-l": possible-completions',
81 66 'set show-all-if-ambiguous on',
82 67 '"\C-o": tab-insert',
83 68 '"\M-i": " "',
84 69 '"\M-o": "\d\d\d\d"',
85 70 '"\M-I": "\d\d\d\d"',
86 71 '"\C-r": reverse-search-history',
87 72 '"\C-s": forward-search-history',
88 73 '"\C-p": history-search-backward',
89 74 '"\C-n": history-search-forward',
90 75 '"\e[A": history-search-backward',
91 76 '"\e[B": history-search-forward',
92 77 '"\C-k": kill-line',
93 78 '"\C-u": unix-line-discard',
94 79 ]
80 InteractiveShell.readline_remove_delims = '-/~'
81 InteractiveShell.readline_merge_completions = True
82 InteractiveShell.readline_omit_names = 0
83
84 InteractiveShell.screen_length = 0
85
86 InteractiveShell.separate_in = '\n'
87 InteractiveShell.separate_out = ''
88 InteractiveShell.separate_out2 = ''
89
90 InteractiveShell.system_header = "IPython system call: "
95 91
96 READLINE_REMOVE_DELIMS = '-/~'
92 InteractiveShell.system_verbose = True
97 93
98 READLINE_MERGE_COMPLETIONS = True
94 InteractiveShell.term_title = False
99 95
100 READLINE_OMIT_NAMES = 0
96 InteractiveShell.wildcards_case_sensitive = True
97
98 InteractiveShell.xmode = 'Context'
101 99
102 100 #-----------------------------------------------------------------------------
103 # Code to execute
101 # PrefilterManager options
104 102 #-----------------------------------------------------------------------------
105 103
106 EXECUTE = [
107 'import numpy as np',
108 'import sympy',
109 'a = 10'
110 ]
111
112 EXECFILE = []
104 PrefilterManager.multi_line_specials = True
113 105
114 106 #-----------------------------------------------------------------------------
115 # Alias
107 # AliasManager options
116 108 #-----------------------------------------------------------------------------
117 109
118 ALIAS = [
119 ('myls', 'ls -la')
110 # Do this to enable all defaults
111 # AliasManager.default_aliases = []
112
113 AliasManger.user_aliases = [
114 ('foo', 'echo Hi')
120 115 ] No newline at end of file
@@ -1,203 +1,305 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 """A factory for creating configuration objects.
3 """A simple configuration system.
4
5 Authors:
6
7 * Brian Granger
4 8 """
5 9
6 10 #-----------------------------------------------------------------------------
7 11 # Copyright (C) 2008-2009 The IPython Development Team
8 12 #
9 13 # Distributed under the terms of the BSD License. The full license is in
10 14 # the file COPYING, distributed as part of this software.
11 15 #-----------------------------------------------------------------------------
12 16
13 17 #-----------------------------------------------------------------------------
14 18 # Imports
15 19 #-----------------------------------------------------------------------------
16 20
21 import __builtin__
17 22 import os
18 23 import sys
19 24
20 25 from IPython.external import argparse
21 from IPython.utils.ipstruct import Struct
22 26 from IPython.utils.genutils import filefind
23 27
24 28 #-----------------------------------------------------------------------------
25 # Code
29 # Exceptions
26 30 #-----------------------------------------------------------------------------
27 31
28 32
29 class ConfigLoaderError(Exception):
33 class ConfigError(Exception):
34 pass
35
36
37 class ConfigLoaderError(ConfigError):
30 38 pass
31 39
32 40
41 #-----------------------------------------------------------------------------
42 # Config class for holding config information
43 #-----------------------------------------------------------------------------
44
45
46 class Config(dict):
47 """An attribute based dict that can do smart merges."""
48
49 def __init__(self, *args, **kwds):
50 dict.__init__(self, *args, **kwds)
51 # This sets self.__dict__ = self, but it has to be done this way
52 # because we are also overriding __setattr__.
53 dict.__setattr__(self, '__dict__', self)
54
55 def _merge(self, other):
56 to_update = {}
57 for k, v in other.items():
58 if not self.has_key(k):
59 to_update[k] = v
60 else: # I have this key
61 if isinstance(v, Config):
62 # Recursively merge common sub Configs
63 self[k]._merge(v)
64 else:
65 # Plain updates for non-Configs
66 to_update[k] = v
67
68 self.update(to_update)
69
70 def _is_section_key(self, key):
71 if key[0].upper()==key[0] and not key.startswith('_'):
72 return True
73 else:
74 return False
75
76 def has_key(self, key):
77 if self._is_section_key(key):
78 return True
79 else:
80 return dict.has_key(self, key)
81
82 def _has_section(self, key):
83 if self._is_section_key(key):
84 if dict.has_key(self, key):
85 return True
86 return False
87
88 def copy(self):
89 return type(self)(dict.copy(self))
90
91 def __copy__(self):
92 return self.copy()
93
94 def __deepcopy__(self, memo):
95 import copy
96 return type(self)(copy.deepcopy(self.items()))
97
98 def __getitem__(self, key):
99 # Because we use this for an exec namespace, we need to delegate
100 # the lookup of names in __builtin__ to itself. This means
101 # that you can't have section or attribute names that are
102 # builtins.
103 try:
104 return getattr(__builtin__, key)
105 except AttributeError:
106 pass
107 if self._is_section_key(key):
108 try:
109 return dict.__getitem__(self, key)
110 except KeyError:
111 c = Config()
112 dict.__setitem__(self, key, c)
113 return c
114 else:
115 return dict.__getitem__(self, key)
116
117 def __setitem__(self, key, value):
118 # Don't allow names in __builtin__ to be modified.
119 if hasattr(__builtin__, key):
120 raise ConfigError('Config variable names cannot have the same name '
121 'as a Python builtin: %s' % key)
122 if self._is_section_key(key):
123 if not isinstance(value, Config):
124 raise ValueError('values whose keys begin with an uppercase '
125 'char must be Config instances: %r, %r' % (key, value))
126 else:
127 dict.__setitem__(self, key, value)
128
129 def __getattr__(self, key):
130 try:
131 return self.__getitem__(key)
132 except KeyError, e:
133 raise AttributeError(e)
134
135 def __setattr__(self, key, value):
136 try:
137 self.__setitem__(key, value)
138 except KeyError, e:
139 raise AttributeError(e)
140
141 def __delattr__(self, key):
142 try:
143 dict.__delitem__(self, key)
144 except KeyError, e:
145 raise AttributeError(e)
146
147
148 #-----------------------------------------------------------------------------
149 # Config loading classes
150 #-----------------------------------------------------------------------------
151
152
33 153 class ConfigLoader(object):
34 154 """A object for loading configurations from just about anywhere.
35 155
36 156 The resulting configuration is packaged as a :class:`Struct`.
37 157
38 158 Notes
39 159 -----
40 160 A :class:`ConfigLoader` does one thing: load a config from a source
41 161 (file, command line arguments) and returns the data as a :class:`Struct`.
42 162 There are lots of things that :class:`ConfigLoader` does not do. It does
43 163 not implement complex logic for finding config files. It does not handle
44 164 default values or merge multiple configs. These things need to be
45 165 handled elsewhere.
46 166 """
47 167
48 168 def __init__(self):
49 169 """A base class for config loaders.
50 170
51 171 Examples
52 172 --------
53 173
54 174 >>> cl = ConfigLoader()
55 175 >>> config = cl.load_config()
56 176 >>> config
57 177 {}
58 178 """
59 179 self.clear()
60 180
61 181 def clear(self):
62 self.config = Struct()
182 self.config = Config()
63 183
64 184 def load_config(self):
65 185 """Load a config from somewhere, return a Struct.
66 186
67 187 Usually, this will cause self.config to be set and then returned.
68 188 """
69 189 return self.config
70 190
71 191
72 192 class FileConfigLoader(ConfigLoader):
73 193 """A base class for file based configurations.
74 194
75 195 As we add more file based config loaders, the common logic should go
76 196 here.
77 197 """
78 198 pass
79 199
80 200
81 201 class PyFileConfigLoader(FileConfigLoader):
82 202 """A config loader for pure python files.
83 203
84 204 This calls execfile on a plain python file and looks for attributes
85 205 that are all caps. These attribute are added to the config Struct.
86 206 """
87 207
88 208 def __init__(self, filename, path=None):
89 209 """Build a config loader for a filename and path.
90 210
91 211 Parameters
92 212 ----------
93 213 filename : str
94 214 The file name of the config file.
95 215 path : str, list, tuple
96 216 The path to search for the config file on, or a sequence of
97 217 paths to try in order.
98 218 """
99 219 super(PyFileConfigLoader, self).__init__()
100 220 self.filename = filename
101 221 self.path = path
102 222 self.full_filename = ''
103 223 self.data = None
104 224
105 225 def load_config(self):
106 226 """Load the config from a file and return it as a Struct."""
107 227 self._find_file()
108 228 self._read_file_as_dict()
109 self._convert_to_struct()
229 self._convert_to_config()
110 230 return self.config
111 231
112 232 def _find_file(self):
113 233 """Try to find the file by searching the paths."""
114 234 self.full_filename = filefind(self.filename, self.path)
115 235
116 236 def _read_file_as_dict(self):
117 self.data = {}
118 execfile(self.full_filename, self.data)
237 execfile(self.full_filename, self.config)
119 238
120 def _convert_to_struct(self):
239 def _convert_to_config(self):
121 240 if self.data is None:
122 241 ConfigLoaderError('self.data does not exist')
123 for k, v in self.data.iteritems():
124 if k == k.upper():
125 self.config[k] = v
242 del self.config['__builtins__']
126 243
127 244
128 245 class CommandLineConfigLoader(ConfigLoader):
129 246 """A config loader for command line arguments.
130 247
131 248 As we add more command line based loaders, the common logic should go
132 249 here.
133 250 """
134 251
135 252
136 class NoDefault(object): pass
137 NoDefault = NoDefault()
253 class NoConfigDefault(object): pass
254 NoConfigDefault = NoConfigDefault()
138 255
139 256 class ArgParseConfigLoader(CommandLineConfigLoader):
140 257
141 258 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
142 259 arguments = ()
143 260
144 261 def __init__(self, *args, **kw):
145 262 """Create a config loader for use with argparse.
146 263
147 264 The args and kwargs arguments here are passed onto the constructor
148 265 of :class:`argparse.ArgumentParser`.
149 266 """
150 267 super(CommandLineConfigLoader, self).__init__()
151 268 self.args = args
152 269 self.kw = kw
153 270
154 271 def load_config(self, args=None):
155 272 """Parse command line arguments and return as a Struct."""
156 273 self._create_parser()
157 274 self._parse_args(args)
158 self._convert_to_struct()
275 self._convert_to_config()
159 276 return self.config
160 277
161 278 def _create_parser(self):
162 279 self.parser = argparse.ArgumentParser(*self.args, **self.kw)
163 280 self._add_arguments()
164 281 self._add_other_arguments()
165 282
166 283 def _add_other_arguments(self):
167 284 pass
168 285
169 286 def _add_arguments(self):
170 287 for argument in self.arguments:
171 288 if not argument[1].has_key('default'):
172 argument[1]['default'] = NoDefault
289 argument[1]['default'] = NoConfigDefault
173 290 self.parser.add_argument(*argument[0],**argument[1])
174 291
175 292 def _parse_args(self, args=None):
176 293 """self.parser->self.parsed_data"""
177 294 if args is None:
178 295 self.parsed_data = self.parser.parse_args()
179 296 else:
180 297 self.parsed_data = self.parser.parse_args(args)
181 298
182 def _convert_to_struct(self):
299 def _convert_to_config(self):
183 300 """self.parsed_data->self.config"""
184 self.config = Struct()
185 301 for k, v in vars(self.parsed_data).items():
186 if v is not NoDefault:
187 setattr(self.config, k, v)
188
189 class IPythonArgParseConfigLoader(ArgParseConfigLoader):
302 if v is not NoConfigDefault:
303 exec_str = 'self.config.' + k + '= v'
304 exec exec_str in locals(), globals()
190 305
191 def _add_other_arguments(self):
192 self.parser.add_argument('-ipythondir',dest='IPYTHONDIR',type=str,
193 help='Set to override default location of IPYTHONDIR.',
194 default=NoDefault)
195 self.parser.add_argument('-p','-profile',dest='PROFILE',type=str,
196 help='The string name of the ipython profile to be used.',
197 default=NoDefault)
198 self.parser.add_argument('-debug',dest="DEBUG",action='store_true',
199 help='Debug the application startup process.',
200 default=NoDefault)
201 self.parser.add_argument('-config_file',dest='CONFIG_FILE',type=str,
202 help='Set the config file name to override default.',
203 default=NoDefault)
@@ -1,93 +1,163 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Tests for IPython.config.loader
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez (design help)
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2009 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import os
24 24 from tempfile import mkstemp
25 25 from unittest import TestCase
26 26
27 from IPython.config.loader import PyFileConfigLoader, ArgParseConfigLoader
27 from IPython.config.loader import (
28 Config,
29 PyFileConfigLoader,
30 ArgParseConfigLoader,
31 ConfigError
32 )
28 33
29 34 #-----------------------------------------------------------------------------
30 35 # Actual tests
31 36 #-----------------------------------------------------------------------------
32 37
33 38
34 39 pyfile = """
35 A = 10
36 B = range(10)
37 C = True
38 D = 'hi there'
40 a = 10
41 b = 20
42 Foo.Bar.value = 10
43 Foo.Bam.value = range(10)
44 D.C.value = 'hi there'
39 45 """
40 46
41 47 class TestPyFileCL(TestCase):
42 48
43 49 def test_basic(self):
44 50 fd, fname = mkstemp()
45 51 f = os.fdopen(fd, 'w')
46 52 f.write(pyfile)
47 53 f.close()
48 54 # Unlink the file
49 55 cl = PyFileConfigLoader(fname)
50 56 config = cl.load_config()
51 self.assertEquals(config.A, 10)
52 self.assertEquals(config.B, range(10))
53 self.assertEquals(config.C, True)
54 self.assertEquals(config.D, 'hi there')
57 self.assertEquals(config.a, 10)
58 self.assertEquals(config.b, 20)
59 self.assertEquals(config.Foo.Bar.value, 10)
60 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.D.C.value, 'hi there')
55 62
56 63
57 64 class TestArgParseCL(TestCase):
58 65
59 66 def test_basic(self):
60 67
61 68 class MyLoader(ArgParseConfigLoader):
62 69 arguments = (
63 (('-f','--foo'), dict(dest='FOO', type=str)),
64 (('-b',), dict(dest='BAR', type=int)),
65 (('-n',), dict(dest='N', action='store_true')),
66 (('BAM',), dict(type=str))
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),
72 (('-n',), dict(dest='n', action='store_true')),
73 (('Global.bam',), dict(type=str))
67 74 )
68 75
69 76 cl = MyLoader()
70 77 config = cl.load_config('-f hi -b 10 -n wow'.split())
71 self.assertEquals(config.FOO, 'hi')
72 self.assertEquals(config.BAR, 10)
73 self.assertEquals(config.N, True)
74 self.assertEquals(config.BAM, 'wow')
78 self.assertEquals(config.Global.foo, 'hi')
79 self.assertEquals(config.MyClass.bar, 10)
80 self.assertEquals(config.n, True)
81 self.assertEquals(config.Global.bam, 'wow')
75 82
76 83 def test_add_arguments(self):
77 84
78 85 class MyLoader(ArgParseConfigLoader):
79 86 def _add_arguments(self):
80 87 subparsers = self.parser.add_subparsers(dest='subparser_name')
81 88 subparser1 = subparsers.add_parser('1')
82 subparser1.add_argument('-x')
89 subparser1.add_argument('-x',dest='Global.x')
83 90 subparser2 = subparsers.add_parser('2')
84 91 subparser2.add_argument('y')
85 92
86 93 cl = MyLoader()
87 94 config = cl.load_config('2 frobble'.split())
88 95 self.assertEquals(config.subparser_name, '2')
89 96 self.assertEquals(config.y, 'frobble')
90 97 config = cl.load_config('1 -x frobble'.split())
91 98 self.assertEquals(config.subparser_name, '1')
92 self.assertEquals(config.x, 'frobble')
93
99 self.assertEquals(config.Global.x, 'frobble')
100
101 class TestConfig(TestCase):
102
103 def test_setget(self):
104 c = Config()
105 c.a = 10
106 self.assertEquals(c.a, 10)
107 self.assertEquals(c.has_key('b'), False)
108
109 def test_auto_section(self):
110 c = Config()
111 self.assertEquals(c.has_key('A'), True)
112 self.assertEquals(c._has_section('A'), False)
113 A = c.A
114 A.foo = 'hi there'
115 self.assertEquals(c._has_section('A'), True)
116 self.assertEquals(c.A.foo, 'hi there')
117 del c.A
118 self.assertEquals(len(c.A.keys()),0)
119
120 def test_merge_doesnt_exist(self):
121 c1 = Config()
122 c2 = Config()
123 c2.bar = 10
124 c2.Foo.bar = 10
125 c1._merge(c2)
126 self.assertEquals(c1.Foo.bar, 10)
127 self.assertEquals(c1.bar, 10)
128 c2.Bar.bar = 10
129 c1._merge(c2)
130 self.assertEquals(c1.Bar.bar, 10)
131
132 def test_merge_exists(self):
133 c1 = Config()
134 c2 = Config()
135 c1.Foo.bar = 10
136 c1.Foo.bam = 30
137 c2.Foo.bar = 20
138 c2.Foo.wow = 40
139 c1._merge(c2)
140 self.assertEquals(c1.Foo.bam, 30)
141 self.assertEquals(c1.Foo.bar, 20)
142 self.assertEquals(c1.Foo.wow, 40)
143 c2.Foo.Bam.bam = 10
144 c1._merge(c2)
145 self.assertEquals(c1.Foo.Bam.bam, 10)
146
147 def test_deepcopy(self):
148 c1 = Config()
149 c1.Foo.bar = 10
150 c1.Foo.bam = 30
151 c1.a = 'asdf'
152 c1.b = range(10)
153 import copy
154 c2 = copy.deepcopy(c1)
155 self.assertEquals(c1, c2)
156 self.assert_(c1 is not c2)
157 self.assert_(c1.Foo is not c2.Foo)
158
159 def test_builtin(self):
160 c1 = Config()
161 exec 'foo = True' in c1
162 self.assertEquals(c1.foo, True)
163 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
@@ -1,257 +1,264 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 IPython's alias component
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 23 import keyword
24 24 import os
25 25 import re
26 26 import sys
27 27
28 28 from IPython.core.component import Component
29 29 from IPython.core.splitinput import split_user_input
30 30
31 31 from IPython.utils.traitlets import CBool, List, Instance
32 32 from IPython.utils.genutils import error
33 33 from IPython.utils.autoattr import auto_attr
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Utilities
37 37 #-----------------------------------------------------------------------------
38 38
39 39 # This is used as the pattern for calls to split_user_input.
40 40 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41 41
42 42 def default_aliases():
43 43 # Make some aliases automatically
44 44 # Prepare list of shell aliases to auto-define
45 45 if os.name == 'posix':
46 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
46 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
47 47 'mv mv -i','rm rm -i','cp cp -i',
48 48 'cat cat','less less','clear clear',
49 49 # a better ls
50 50 'ls ls -F',
51 51 # long ls
52 52 'll ls -lF')
53 53 # Extra ls aliases with color, which need special treatment on BSD
54 54 # variants
55 55 ls_extra = ( # color ls
56 56 'lc ls -F -o --color',
57 57 # ls normal files only
58 58 'lf ls -F -o --color %l | grep ^-',
59 59 # ls symbolic links
60 60 'lk ls -F -o --color %l | grep ^l',
61 61 # directories or links to directories,
62 62 'ldir ls -F -o --color %l | grep /$',
63 63 # things which are executable
64 64 'lx ls -F -o --color %l | grep ^-..x',
65 65 )
66 66 # The BSDs don't ship GNU ls, so they don't understand the
67 67 # --color switch out of the box
68 68 if 'bsd' in sys.platform:
69 69 ls_extra = ( # ls normal files only
70 70 'lf ls -lF | grep ^-',
71 71 # ls symbolic links
72 72 'lk ls -lF | grep ^l',
73 73 # directories or links to directories,
74 74 'ldir ls -lF | grep /$',
75 75 # things which are executable
76 76 'lx ls -lF | grep ^-..x',
77 77 )
78 auto_alias = auto_alias + ls_extra
78 default_aliases = default_aliases + ls_extra
79 79 elif os.name in ['nt','dos']:
80 auto_alias = ('ls dir /on',
80 default_aliases = ('ls dir /on',
81 81 'ddir dir /ad /on', 'ldir dir /ad /on',
82 82 'mkdir mkdir','rmdir rmdir','echo echo',
83 83 'ren ren','cls cls','copy copy')
84 84 else:
85 auto_alias = ()
86 return [s.split(None,1) for s in auto_alias]
85 default_aliases = ()
86 return [s.split(None,1) for s in default_aliases]
87 87
88 88
89 89 class AliasError(Exception):
90 90 pass
91 91
92 92
93 93 class InvalidAliasError(AliasError):
94 94 pass
95 95
96 96
97 97 #-----------------------------------------------------------------------------
98 98 # Main AliasManager class
99 99 #-----------------------------------------------------------------------------
100 100
101 101
102 102 class AliasManager(Component):
103 103
104 auto_alias = List(default_aliases())
105 user_alias = List(default_value=[], config_key='USER_ALIAS')
104 default_aliases = List(default_aliases(), config=True)
105 user_aliases = List(default_value=[], config=True)
106 106
107 107 def __init__(self, parent, config=None):
108 108 super(AliasManager, self).__init__(parent, config=config)
109 109 self.alias_table = {}
110 110 self.exclude_aliases()
111 111 self.init_aliases()
112 112
113 113 @auto_attr
114 114 def shell(self):
115 115 shell = Component.get_instances(
116 116 root=self.root,
117 117 klass='IPython.core.iplib.InteractiveShell'
118 118 )[0]
119 119 return shell
120 120
121 121 def __contains__(self, name):
122 122 if name in self.alias_table:
123 123 return True
124 124 else:
125 125 return False
126 126
127 127 @property
128 128 def aliases(self):
129 129 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
130 130
131 131 def exclude_aliases(self):
132 132 # set of things NOT to alias (keywords, builtins and some magics)
133 133 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
134 134 no_alias.update(set(keyword.kwlist))
135 135 no_alias.update(set(__builtin__.__dict__.keys()))
136 136 self.no_alias = no_alias
137 137
138 138 def init_aliases(self):
139 139 # Load default aliases
140 for name, cmd in self.auto_alias:
140 for name, cmd in self.default_aliases:
141 141 self.soft_define_alias(name, cmd)
142 142
143 143 # Load user aliases
144 for name, cmd in self.user_alias:
144 for name, cmd in self.user_aliases:
145 145 self.soft_define_alias(name, cmd)
146 146
147 def clear_aliases(self):
148 self.alias_table.clear()
149
147 150 def soft_define_alias(self, name, cmd):
148 151 """Define an alias, but don't raise on an AliasError."""
149 152 try:
150 153 self.define_alias(name, cmd)
151 154 except AliasError, e:
152 155 error("Invalid alias: %s" % e)
153 156
154 157 def define_alias(self, name, cmd):
155 158 """Define a new alias after validating it.
156 159
157 160 This will raise an :exc:`AliasError` if there are validation
158 161 problems.
159 162 """
160 163 nargs = self.validate_alias(name, cmd)
161 164 self.alias_table[name] = (nargs, cmd)
162 165
166 def undefine_alias(self, name):
167 if self.alias_table.has_key(name):
168 del self.alias_table[name]
169
163 170 def validate_alias(self, name, cmd):
164 171 """Validate an alias and return the its number of arguments."""
165 172 if name in self.no_alias:
166 173 raise InvalidAliasError("The name %s can't be aliased "
167 174 "because it is a keyword or builtin." % name)
168 175 if not (isinstance(cmd, basestring)):
169 176 raise InvalidAliasError("An alias command must be a string, "
170 177 "got: %r" % name)
171 178 nargs = cmd.count('%s')
172 179 if nargs>0 and cmd.find('%l')>=0:
173 180 raise InvalidAliasError('The %s and %l specifiers are mutually '
174 181 'exclusive in alias definitions.')
175 182 return nargs
176 183
177 184 def call_alias(self, alias, rest=''):
178 185 """Call an alias given its name and the rest of the line."""
179 186 cmd = self.transform_alias(alias, rest)
180 187 try:
181 188 self.shell.system(cmd)
182 189 except:
183 190 self.shell.showtraceback()
184 191
185 192 def transform_alias(self, alias,rest=''):
186 193 """Transform alias to system command string."""
187 194 nargs, cmd = self.alias_table[alias]
188 195
189 196 if ' ' in cmd and os.path.isfile(cmd):
190 197 cmd = '"%s"' % cmd
191 198
192 199 # Expand the %l special to be the user's input line
193 200 if cmd.find('%l') >= 0:
194 201 cmd = cmd.replace('%l', rest)
195 202 rest = ''
196 203 if nargs==0:
197 204 # Simple, argument-less aliases
198 205 cmd = '%s %s' % (cmd, rest)
199 206 else:
200 207 # Handle aliases with positional arguments
201 208 args = rest.split(None, nargs)
202 209 if len(args) < nargs:
203 210 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
204 211 (alias, nargs, len(args)))
205 212 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
206 213 return cmd
207 214
208 215 def expand_alias(self, line):
209 216 """ Expand an alias in the command line
210 217
211 218 Returns the provided command line, possibly with the first word
212 219 (command) translated according to alias expansion rules.
213 220
214 221 [ipython]|16> _ip.expand_aliases("np myfile.txt")
215 222 <16> 'q:/opt/np/notepad++.exe myfile.txt'
216 223 """
217 224
218 225 pre,fn,rest = split_user_input(line)
219 226 res = pre + self.expand_aliases(fn, rest)
220 227 return res
221 228
222 229 def expand_aliases(self, fn, rest):
223 230 """Expand multiple levels of aliases:
224 231
225 232 if:
226 233
227 234 alias foo bar /tmp
228 235 alias baz foo
229 236
230 237 then:
231 238
232 239 baz huhhahhei -> bar /tmp huhhahhei
233 240
234 241 """
235 242 line = fn + " " + rest
236 243
237 244 done = set()
238 245 while 1:
239 246 pre,fn,rest = split_user_input(line, shell_line_split)
240 247 if fn in self.alias_table:
241 248 if fn in done:
242 249 warn("Cyclic alias definition, repeated '%s'" % fn)
243 250 return ""
244 251 done.add(fn)
245 252
246 253 l2 = self.transform_alias(fn, rest)
247 254 if l2 == line:
248 255 break
249 256 # ls -> ls -F should not recurse forever
250 257 if l2.split(None,1)[0] == line.split(None,1)[0]:
251 258 line = l2
252 259 break
253 260 line=l2
254 261 else:
255 262 break
256 263
257 264 return line
@@ -1,242 +1,265 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 An application for IPython
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10
11 11 Notes
12 12 -----
13 13 """
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Copyright (C) 2008-2009 The IPython Development Team
17 17 #
18 18 # Distributed under the terms of the BSD License. The full license is in
19 19 # the file COPYING, distributed as part of this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 import os
27 27 import sys
28 28 import traceback
29
30 29 from copy import deepcopy
31 from IPython.utils.ipstruct import Struct
30
32 31 from IPython.utils.genutils import get_ipython_dir, filefind
33 32 from IPython.config.loader import (
34 IPythonArgParseConfigLoader,
35 PyFileConfigLoader
33 PyFileConfigLoader,
34 ArgParseConfigLoader,
35 Config,
36 NoConfigDefault
36 37 )
37 38
38 39 #-----------------------------------------------------------------------------
39 40 # Classes and functions
40 41 #-----------------------------------------------------------------------------
41 42
42 43
44 class IPythonArgParseConfigLoader(ArgParseConfigLoader):
45 """Default command line options for IPython based applications."""
46
47 def _add_other_arguments(self):
48 self.parser.add_argument('-ipythondir',dest='Global.ipythondir',type=str,
49 help='Set to override default location of Global.ipythondir.',
50 default=NoConfigDefault,
51 metavar='Global.ipythondir')
52 self.parser.add_argument('-p','-profile',dest='Global.profile',type=str,
53 help='The string name of the ipython profile to be used.',
54 default=NoConfigDefault,
55 metavar='Global.profile')
56 self.parser.add_argument('-debug',dest="Global.debug",action='store_true',
57 help='Debug the application startup process.',
58 default=NoConfigDefault)
59 self.parser.add_argument('-config_file',dest='Global.config_file',type=str,
60 help='Set the config file name to override default.',
61 default=NoConfigDefault,
62 metavar='Global.config_file')
63
64
43 65 class ApplicationError(Exception):
44 66 pass
45 67
46 68
47 69 class Application(object):
48 70 """Load a config, construct an app and run it.
49 71 """
50 72
51 73 config_file_name = 'ipython_config.py'
52 74 name = 'ipython'
53 75 debug = False
54 76
55 77 def __init__(self):
56 78 pass
57 79
58 80 def start(self):
59 81 """Start the application."""
60 82 self.attempt(self.create_default_config)
61 83 self.attempt(self.pre_load_command_line_config)
62 84 self.attempt(self.load_command_line_config, action='abort')
63 85 self.attempt(self.post_load_command_line_config)
64 86 self.attempt(self.find_ipythondir)
65 87 self.attempt(self.find_config_file_name)
66 88 self.attempt(self.find_config_file_paths)
67 89 self.attempt(self.pre_load_file_config)
68 90 self.attempt(self.load_file_config)
69 91 self.attempt(self.post_load_file_config)
70 92 self.attempt(self.merge_configs)
71 93 self.attempt(self.pre_construct)
72 94 self.attempt(self.construct)
73 95 self.attempt(self.post_construct)
74 96 self.attempt(self.start_app)
75 97
76 98 #-------------------------------------------------------------------------
77 99 # Various stages of Application creation
78 100 #-------------------------------------------------------------------------
79 101
80 102 def create_default_config(self):
81 103 """Create defaults that can't be set elsewhere."""
82 self.default_config = Struct()
83 self.default_config.IPYTHONDIR = get_ipython_dir()
104 self.default_config = Config()
105 self.default_config.Global.ipythondir = get_ipython_dir()
84 106
85 107 def create_command_line_config(self):
86 108 """Create and return a command line config loader."""
87 109 return IPythonArgParseConfigLoader(description=self.name)
88 110
89 111 def pre_load_command_line_config(self):
90 112 """Do actions just before loading the command line config."""
91 113 pass
92 114
93 115 def load_command_line_config(self):
94 116 """Load the command line config.
95 117
96 118 This method also sets ``self.debug``.
97 119 """
98 120
99 121 loader = self.create_command_line_config()
100 122 self.command_line_config = loader.load_config()
101 123 try:
102 self.debug = self.command_line_config.DEBUG
124 self.debug = self.command_line_config.Global.debug
103 125 except AttributeError:
104 126 pass # use class default
105 127 self.log("Default config loaded:", self.default_config)
106 128 self.log("Command line config loaded:", self.command_line_config)
107 129
108 130 def post_load_command_line_config(self):
109 131 """Do actions just after loading the command line config."""
110 132 pass
111 133
112 134 def find_ipythondir(self):
113 135 """Set the IPython directory.
114 136
115 137 This sets ``self.ipythondir``, but the actual value that is passed
116 138 to the application is kept in either ``self.default_config`` or
117 139 ``self.command_line_config``. This also added ``self.ipythondir`` to
118 140 ``sys.path`` so config files there can be references by other config
119 141 files.
120 142 """
121 143
122 144 try:
123 self.ipythondir = self.command_line_config.IPYTHONDIR
145 self.ipythondir = self.command_line_config.Global.ipythondir
124 146 except AttributeError:
125 self.ipythondir = self.default_config.IPYTHONDIR
147 self.ipythondir = self.default_config.Global.ipythondir
126 148 sys.path.append(os.path.abspath(self.ipythondir))
127 149 if not os.path.isdir(self.ipythondir):
128 150 os.makedirs(self.ipythondir, mode = 0777)
129 151 self.log("IPYTHONDIR set to: %s" % self.ipythondir)
130 152
131 153 def find_config_file_name(self):
132 154 """Find the config file name for this application.
133 155
134 156 If a profile has been set at the command line, this will resolve
135 157 it. The search paths for the config file are set in
136 158 :meth:`find_config_file_paths` and then passed to the config file
137 159 loader where they are resolved to an absolute path.
138 160 """
139 161
140 162 try:
141 self.config_file_name = self.command_line_config.CONFIG_FILE
163 self.config_file_name = self.command_line_config.Global.config_file
142 164 except AttributeError:
143 165 pass
144 166
145 167 try:
146 self.profile_name = self.command_line_config.PROFILE
168 self.profile_name = self.command_line_config.Global.profile
147 169 name_parts = self.config_file_name.split('.')
148 170 name_parts.insert(1, '_' + self.profile_name + '.')
149 171 self.config_file_name = ''.join(name_parts)
150 172 except AttributeError:
151 173 pass
152 174
153 175 def find_config_file_paths(self):
154 176 """Set the search paths for resolving the config file."""
155 177 self.config_file_paths = (os.getcwd(), self.ipythondir)
156 178
157 179 def pre_load_file_config(self):
158 180 """Do actions before the config file is loaded."""
159 181 pass
160 182
161 183 def load_file_config(self):
162 184 """Load the config file.
163 185
164 186 This tries to load the config file from disk. If successful, the
165 187 ``CONFIG_FILE`` config variable is set to the resolved config file
166 188 location. If not successful, an empty config is used.
167 189 """
168 190 loader = PyFileConfigLoader(self.config_file_name,
169 self.config_file_paths)
191 path=self.config_file_paths)
170 192 try:
171 193 self.file_config = loader.load_config()
172 self.file_config.CONFIG_FILE = loader.full_filename
194 self.file_config.Global.config_file = loader.full_filename
173 195 except IOError:
174 196 self.log("Config file not found, skipping: %s" % \
175 197 self.config_file_name)
176 self.file_config = Struct()
198 self.file_config = Config()
177 199 else:
178 200 self.log("Config file loaded: %s" % loader.full_filename,
179 201 self.file_config)
180 202
181 203 def post_load_file_config(self):
182 204 """Do actions after the config file is loaded."""
183 205 pass
184 206
185 207 def merge_configs(self):
186 208 """Merge the default, command line and file config objects."""
187 config = Struct()
188 config.update(self.default_config)
189 config.update(self.file_config)
190 config.update(self.command_line_config)
209 config = Config()
210 config._merge(self.default_config)
211 config._merge(self.file_config)
212 config._merge(self.command_line_config)
191 213 self.master_config = config
192 214 self.log("Master config created:", self.master_config)
193 215
194 216 def pre_construct(self):
195 217 """Do actions after the config has been built, but before construct."""
196 218 pass
197 219
198 220 def construct(self):
199 221 """Construct the main components that make up this app."""
200 222 self.log("Constructing components for application...")
201 223
202 224 def post_construct(self):
203 225 """Do actions after construct, but before starting the app."""
204 226 pass
205 227
206 228 def start_app(self):
207 229 """Actually start the app."""
208 230 self.log("Starting application...")
209 231
210 232 #-------------------------------------------------------------------------
211 233 # Utility methods
212 234 #-------------------------------------------------------------------------
213 235
214 236 def abort(self):
215 237 """Abort the starting of the application."""
216 238 print "Aborting application: ", self.name
217 239 sys.exit(1)
218 240
219 241 def exit(self):
220 242 print "Exiting application: ", self.name
221 243 sys.exit(1)
222 244
223 245 def attempt(self, func, action='abort'):
224 246 try:
225 247 func()
248 except SystemExit:
249 self.exit()
226 250 except:
227 251 if action == 'abort':
228 252 self.print_traceback()
229 253 self.abort()
230 254 elif action == 'exit':
231 self.print_traceback()
232 255 self.exit()
233 256
234 257 def print_traceback(self):
235 258 print "Error in appliction startup: ", self.name
236 259 print
237 260 traceback.print_exc()
238 261
239 262 def log(self, *args):
240 263 if self.debug:
241 264 for arg in args:
242 265 print "[%s] %s" % (self.name, arg) No newline at end of file
@@ -1,111 +1,118 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 A context manager for managing things injected into :mod:`__builtin__`.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 """
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2008-2009 The IPython Development Team
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 import __builtin__
23 23
24 24 from IPython.core.component import Component
25 25 from IPython.core.quitter import Quitter
26 26
27 27 from IPython.utils.autoattr import auto_attr
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes and functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33
34 34 class BuiltinUndefined(object): pass
35 35 BuiltinUndefined = BuiltinUndefined()
36 36
37 37
38 38 class BuiltinTrap(Component):
39 39
40 40 def __init__(self, parent):
41 41 super(BuiltinTrap, self).__init__(parent, None, None)
42 42 self._orig_builtins = {}
43 # We define this to track if a single BuiltinTrap is nested.
44 # Only turn off the trap when the outermost call to __exit__ is made.
45 self._nested_level = 0
43 46
44 47 @auto_attr
45 48 def shell(self):
46 49 shell = Component.get_instances(
47 50 root=self.root,
48 51 klass='IPython.core.iplib.InteractiveShell'
49 52 )[0]
50 53 return shell
51 54
52 55 def __enter__(self):
56 if self._nested_level == 0:
53 57 self.set()
58 self._nested_level += 1
54 59 # I return self, so callers can use add_builtin in a with clause.
55 60 return self
56 61
57 62 def __exit__(self, type, value, traceback):
63 if self._nested_level == 1:
58 64 self.unset()
65 self._nested_level -= 1
59 66 return True
60 67
61 68 def add_builtin(self, key, value):
62 69 """Add a builtin and save the original."""
63 70 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
64 71 self._orig_builtins[key] = orig
65 72 __builtin__.__dict__[key] = value
66 73
67 74 def remove_builtin(self, key):
68 75 """Remove an added builtin and re-set the original."""
69 76 try:
70 77 orig = self._orig_builtins.pop(key)
71 78 except KeyError:
72 79 pass
73 80 else:
74 81 if orig is BuiltinUndefined:
75 82 del __builtin__.__dict__[key]
76 83 else:
77 84 __builtin__.__dict__[key] = orig
78 85
79 86 def set(self):
80 87 """Store ipython references in the __builtin__ namespace."""
81 88 self.add_builtin('exit', Quitter(self.shell, 'exit'))
82 89 self.add_builtin('quit', Quitter(self.shell, 'quit'))
83 90
84 91 # Recursive reload function
85 92 try:
86 93 from IPython.lib import deepreload
87 94 if self.shell.deep_reload:
88 95 self.add_builtin('reload', deepreload.reload)
89 96 else:
90 97 self.add_builtin('dreload', deepreload.reload)
91 98 del deepreload
92 99 except ImportError:
93 100 pass
94 101
95 102 # Keep in the builtins a flag for when IPython is active. We set it
96 103 # with setdefault so that multiple nested IPythons don't clobber one
97 104 # another. Each will increase its value by one upon being activated,
98 105 # which also gives us a way to determine the nesting level.
99 106 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
100 107
101 108 def unset(self):
102 109 """Remove any builtins which might have been added by add_builtins, or
103 110 restore overwritten ones to their previous values."""
104 111 for key in self._orig_builtins.keys():
105 112 self.remove_builtin(key)
106 113 self._orig_builtins.clear()
107 114 self._builtins_added = False
108 115 try:
109 116 del __builtin__.__dict__['__IPYTHON__active']
110 117 except KeyError:
111 118 pass
@@ -1,304 +1,309 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 A lightweight component system for IPython.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2009 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 from copy import deepcopy
24 24 import datetime
25 25 from weakref import WeakValueDictionary
26 26
27 27 from IPython.utils.importstring import import_item
28 from IPython.utils.ipstruct import Struct
28 from IPython.config.loader import Config
29 29 from IPython.utils.traitlets import (
30 30 HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This
31 31 )
32 32
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # Helper classes for Components
36 36 #-----------------------------------------------------------------------------
37 37
38 38
39 39 class ComponentError(Exception):
40 40 pass
41 41
42 42 class MetaComponentTracker(type):
43 43 """A metaclass that tracks instances of Components and its subclasses."""
44 44
45 45 def __init__(cls, name, bases, d):
46 46 super(MetaComponentTracker, cls).__init__(name, bases, d)
47 47 cls.__instance_refs = WeakValueDictionary()
48 48 cls.__numcreated = 0
49 49
50 50 def __call__(cls, *args, **kw):
51 51 """Called when a class is called (instantiated)!!!
52 52
53 53 When a Component or subclass is instantiated, this is called and
54 54 the instance is saved in a WeakValueDictionary for tracking.
55 55 """
56 56 instance = cls.__new__(cls, *args, **kw)
57 57
58 58 # Register the instance before __init__ is called so get_instances
59 59 # works inside __init__ methods!
60 60 indices = cls.register_instance(instance)
61 61
62 62 # This is in a try/except because of the __init__ method fails, the
63 63 # instance is discarded and shouldn't be tracked.
64 64 try:
65 65 if isinstance(instance, cls):
66 66 cls.__init__(instance, *args, **kw)
67 67 except:
68 68 # Unregister the instance because __init__ failed!
69 69 cls.unregister_instances(indices)
70 70 raise
71 71 else:
72 72 return instance
73 73
74 74 def register_instance(cls, instance):
75 75 """Register instance with cls and its subclasses."""
76 76 # indices is a list of the keys used to register the instance
77 77 # with. This list is needed if the instance needs to be unregistered.
78 78 indices = []
79 79 for c in cls.__mro__:
80 80 if issubclass(cls, c) and issubclass(c, Component):
81 81 c.__numcreated += 1
82 82 indices.append(c.__numcreated)
83 83 c.__instance_refs[c.__numcreated] = instance
84 84 else:
85 85 break
86 86 return indices
87 87
88 88 def unregister_instances(cls, indices):
89 89 """Unregister instance with cls and its subclasses."""
90 90 for c, index in zip(cls.__mro__, indices):
91 91 try:
92 92 del c.__instance_refs[index]
93 93 except KeyError:
94 94 pass
95 95
96 96 def clear_instances(cls):
97 97 """Clear all instances tracked by cls."""
98 98 cls.__instance_refs.clear()
99 99 cls.__numcreated = 0
100 100
101 101 def get_instances(cls, name=None, root=None, klass=None):
102 102 """Get all instances of cls and its subclasses.
103 103
104 104 Parameters
105 105 ----------
106 106 name : str
107 107 Limit to components with this name.
108 108 root : Component or subclass
109 109 Limit to components having this root.
110 110 klass : class or str
111 111 Limits to instances of the class or its subclasses. If a str
112 112 is given ut must be in the form 'foo.bar.MyClass'. The str
113 113 form of this argument is useful for forward declarations.
114 114 """
115 115 if klass is not None:
116 116 if isinstance(klass, basestring):
117 117 klass = import_item(klass)
118 118 # Limit search to instances of klass for performance
119 119 if issubclass(klass, Component):
120 120 return klass.get_instances(name=name, root=root)
121 121 instances = cls.__instance_refs.values()
122 122 if name is not None:
123 123 instances = [i for i in instances if i.name == name]
124 124 if klass is not None:
125 125 instances = [i for i in instances if isinstance(i, klass)]
126 126 if root is not None:
127 127 instances = [i for i in instances if i.root == root]
128 128 return instances
129 129
130 130 def get_instances_by_condition(cls, call, name=None, root=None,
131 131 klass=None):
132 132 """Get all instances of cls, i such that call(i)==True.
133 133
134 134 This also takes the ``name`` and ``root`` and ``classname``
135 135 arguments of :meth:`get_instance`
136 136 """
137 137 return [i for i in cls.get_instances(name, root, klass) if call(i)]
138 138
139 139
140 140 def masquerade_as(instance, cls):
141 141 """Let instance masquerade as an instance of cls.
142 142
143 143 Sometimes, such as in testing code, it is useful to let a class
144 144 masquerade as another. Python, being duck typed, allows this by
145 145 default. But, instances of components are tracked by their class type.
146 146
147 147 After calling this, cls.get_instances() will return ``instance``. This
148 148 does not, however, cause isinstance(instance, cls) to return ``True``.
149 149
150 150 Parameters
151 151 ----------
152 152 instance : an instance of a Component or Component subclass
153 153 The instance that will pretend to be a cls.
154 154 cls : subclass of Component
155 155 The Component subclass that instance will pretend to be.
156 156 """
157 157 cls.register_instance(instance)
158 158
159 159
160 160 class ComponentNameGenerator(object):
161 161 """A Singleton to generate unique component names."""
162 162
163 163 def __init__(self, prefix):
164 164 self.prefix = prefix
165 165 self.i = 0
166 166
167 167 def __call__(self):
168 168 count = self.i
169 169 self.i += 1
170 170 return "%s%s" % (self.prefix, count)
171 171
172 172
173 173 ComponentNameGenerator = ComponentNameGenerator('ipython.component')
174 174
175 175
176 176 class MetaComponent(MetaHasTraitlets, MetaComponentTracker):
177 177 pass
178 178
179 179
180 180 #-----------------------------------------------------------------------------
181 181 # Component implementation
182 182 #-----------------------------------------------------------------------------
183 183
184 184
185 185 class Component(HasTraitlets):
186 186
187 187 __metaclass__ = MetaComponent
188 188
189 189 # Traitlets are fun!
190 config = Instance(Struct,(),{})
190 config = Instance(Config,(),{})
191 191 parent = This()
192 192 root = This()
193 193 created = None
194 194
195 195 def __init__(self, parent, name=None, config=None):
196 196 """Create a component given a parent and possibly and name and config.
197 197
198 198 Parameters
199 199 ----------
200 200 parent : Component subclass
201 201 The parent in the component graph. The parent is used
202 202 to get the root of the component graph.
203 203 name : str
204 204 The unique name of the component. If empty, then a unique
205 205 one will be autogenerated.
206 config : Struct
206 config : Config
207 207 If this is empty, self.config = parent.config, otherwise
208 208 self.config = config and root.config is ignored. This argument
209 209 should only be used to *override* the automatic inheritance of
210 210 parent.config. If a caller wants to modify parent.config
211 211 (not override), the caller should make a copy and change
212 212 attributes and then pass the copy to this argument.
213 213
214 214 Notes
215 215 -----
216 216 Subclasses of Component must call the :meth:`__init__` method of
217 217 :class:`Component` *before* doing anything else and using
218 218 :func:`super`::
219 219
220 220 class MyComponent(Component):
221 221 def __init__(self, parent, name=None, config=None):
222 222 super(MyComponent, self).__init__(parent, name, config)
223 223 # Then any other code you need to finish initialization.
224 224
225 225 This ensures that the :attr:`parent`, :attr:`name` and :attr:`config`
226 226 attributes are handled properly.
227 227 """
228 228 super(Component, self).__init__()
229 229 self._children = []
230 230 if name is None:
231 231 self.name = ComponentNameGenerator()
232 232 else:
233 233 self.name = name
234 234 self.root = self # This is the default, it is set when parent is set
235 235 self.parent = parent
236 236 if config is not None:
237 237 self.config = deepcopy(config)
238 238 else:
239 239 if self.parent is not None:
240 240 self.config = deepcopy(self.parent.config)
241 241
242 242 self.created = datetime.datetime.now()
243 243
244 244 #-------------------------------------------------------------------------
245 245 # Static traitlet notifiations
246 246 #-------------------------------------------------------------------------
247 247
248 248 def _parent_changed(self, name, old, new):
249 249 if old is not None:
250 250 old._remove_child(self)
251 251 if new is not None:
252 252 new._add_child(self)
253 253
254 254 if new is None:
255 255 self.root = self
256 256 else:
257 257 self.root = new.root
258 258
259 259 def _root_changed(self, name, old, new):
260 260 if self.parent is None:
261 261 if not (new is self):
262 262 raise ComponentError("Root not self, but parent is None.")
263 263 else:
264 264 if not self.parent.root is new:
265 265 raise ComponentError("Error in setting the root attribute: "
266 266 "root != parent.root")
267 267
268 268 def _config_changed(self, name, old, new):
269 269 """Update all the class traits having a config_key with the config.
270 270
271 271 For any class traitlet with a ``config_key`` metadata attribute, we
272 272 update the traitlet with the value of the corresponding config entry.
273 273
274 274 In the future, we might want to do a pop here so stale config info
275 275 is not passed onto children.
276 276 """
277 # Get all traitlets with a config_key metadata entry
278 traitlets = self.traitlets('config_key')
277 # Get all traitlets with a config metadata entry that is True
278 traitlets = self.traitlets(config=True)
279
280 # Don't do a blind getattr as that would cause the config to
281 # dynamically create the section with name self.__class__.__name__.
282 if new._has_section(self.__class__.__name__):
283 my_config = new[self.__class__.__name__]
279 284 for k, v in traitlets.items():
280 285 try:
281 config_value = new[v.get_metadata('config_key')]
286 config_value = my_config[k]
282 287 except KeyError:
283 288 pass
284 289 else:
285 290 setattr(self, k, config_value)
286 291
287 292 @property
288 293 def children(self):
289 294 """A list of all my child components."""
290 295 return self._children
291 296
292 297 def _remove_child(self, child):
293 298 """A private method for removing children components."""
294 299 if child in self._children:
295 300 index = self._children.index(child)
296 301 del self._children[index]
297 302
298 303 def _add_child(self, child):
299 304 """A private method for adding children components."""
300 305 if child not in self._children:
301 306 self._children.append(child)
302 307
303 308 def __repr__(self):
304 309 return "<%s('%s')>" % (self.__class__.__name__, self.name)
@@ -1,72 +1,78 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 A context manager for handling sys.displayhook.
5 5
6 6 Authors:
7 7
8 8 * Robert Kern
9 9 * Brian Granger
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2009 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 import sys
24 24
25 25 from IPython.core.component import Component
26 26
27 27 from IPython.utils.autoattr import auto_attr
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes and functions
31 31 #-----------------------------------------------------------------------------
32 32
33 33
34 34 class DisplayTrap(Component):
35 35 """Object to manage sys.displayhook.
36 36
37 37 This came from IPython.core.kernel.display_hook, but is simplified
38 38 (no callbacks or formatters) until more of the core is refactored.
39 39 """
40 40
41 41 def __init__(self, parent, hook):
42 42 super(DisplayTrap, self).__init__(parent, None, None)
43
44 43 self.hook = hook
45 44 self.old_hook = None
45 # We define this to track if a single BuiltinTrap is nested.
46 # Only turn off the trap when the outermost call to __exit__ is made.
47 self._nested_level = 0
46 48
47 49 @auto_attr
48 50 def shell(self):
49 51 shell = Component.get_instances(
50 52 root=self.root,
51 53 klass='IPython.core.iplib.InteractiveShell'
52 54 )[0]
53 55 return shell
54 56
55 57 def __enter__(self):
58 if self._nested_level == 0:
56 59 self.set()
60 self._nested_level += 1
57 61 return self
58 62
59 63 def __exit__(self, type, value, traceback):
64 if self._nested_level == 1:
60 65 self.unset()
66 self._nested_level -= 1
61 67 return True
62 68
63 69 def set(self):
64 70 """Set the hook."""
65 71 if sys.displayhook is not self.hook:
66 72 self.old_hook = sys.displayhook
67 73 sys.displayhook = self.hook
68 74
69 75 def unset(self):
70 76 """Unset the hook."""
71 77 sys.displayhook = self.old_hook
72 78
@@ -1,268 +1,272 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 An embedded IPython shell.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10
11 11 Notes
12 12 -----
13 13 """
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Copyright (C) 2008-2009 The IPython Development Team
17 17 #
18 18 # Distributed under the terms of the BSD License. The full license is in
19 19 # the file COPYING, distributed as part of this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 from __future__ import with_statement
27 27
28 28 import sys
29 29 from contextlib import nested
30 30
31 31 from IPython.core import ultratb
32 32 from IPython.core.iplib import InteractiveShell
33 from IPython.core.ipapp import load_default_config
33 34
34 35 from IPython.utils.traitlets import Bool, Str, CBool
35 36 from IPython.utils.genutils import ask_yes_no
36 37
38
37 39 #-----------------------------------------------------------------------------
38 40 # Classes and functions
39 41 #-----------------------------------------------------------------------------
40 42
41 43 # This is an additional magic that is exposed in embedded shells.
42 44 def kill_embedded(self,parameter_s=''):
43 45 """%kill_embedded : deactivate for good the current embedded IPython.
44 46
45 47 This function (after asking for confirmation) sets an internal flag so that
46 48 an embedded IPython will never activate again. This is useful to
47 49 permanently disable a shell that is being called inside a loop: once you've
48 50 figured out what you needed from it, you may then kill it and the program
49 51 will then continue to run without the interactive shell interfering again.
50 52 """
51 53
52 54 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
53 55 "(y/n)? [y/N] ",'n')
54 56 if kill:
55 57 self.embedded_active = False
56 58 print "This embedded IPython will not reactivate anymore once you exit."
57 59
58 60
59 61 class InteractiveShellEmbed(InteractiveShell):
60 62
61 63 dummy_mode = Bool(False)
62 64 exit_msg = Str('')
63 65 embedded = CBool(True)
64 66 embedded_active = CBool(True)
65 67
66 68 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
67 69 user_ns=None, user_global_ns=None,
68 70 banner1=None, banner2=None,
69 71 custom_exceptions=((),None), exit_msg=''):
70 72
71 73 self.save_sys_ipcompleter()
72 74
73 75 super(InteractiveShellEmbed,self).__init__(
74 76 parent=parent, config=config, ipythondir=ipythondir, usage=usage,
75 77 user_ns=user_ns, user_global_ns=user_global_ns,
76 78 banner1=banner1, banner2=banner2,
77 79 custom_exceptions=custom_exceptions)
78 80
79 81 self.exit_msg = exit_msg
80 82 self.define_magic("kill_embedded", kill_embedded)
81 83
82 84 # don't use the ipython crash handler so that user exceptions aren't
83 85 # trapped
84 86 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
85 87 mode=self.xmode,
86 88 call_pdb=self.pdb)
87 89
88 90 self.restore_sys_ipcompleter()
89 91
90 92 def init_sys_modules(self):
91 93 pass
92 94
93 95 def save_sys_ipcompleter(self):
94 96 """Save readline completer status."""
95 97 try:
96 98 #print 'Save completer',sys.ipcompleter # dbg
97 99 self.sys_ipcompleter_orig = sys.ipcompleter
98 100 except:
99 101 pass # not nested with IPython
100 102
101 103 def restore_sys_ipcompleter(self):
102 104 """Restores the readline completer which was in place.
103 105
104 106 This allows embedded IPython within IPython not to disrupt the
105 107 parent's completion.
106 108 """
107 109 try:
108 110 self.readline.set_completer(self.sys_ipcompleter_orig)
109 111 sys.ipcompleter = self.sys_ipcompleter_orig
110 112 except:
111 113 pass
112 114
113 115 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
114 116 stack_depth=1):
115 117 """Activate the interactive interpreter.
116 118
117 119 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
118 120 the interpreter shell with the given local and global namespaces, and
119 121 optionally print a header string at startup.
120 122
121 123 The shell can be globally activated/deactivated using the
122 124 set/get_dummy_mode methods. This allows you to turn off a shell used
123 125 for debugging globally.
124 126
125 127 However, *each* time you call the shell you can override the current
126 128 state of dummy_mode with the optional keyword parameter 'dummy'. For
127 129 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
128 130 can still have a specific call work by making it as IPShell(dummy=0).
129 131
130 132 The optional keyword parameter dummy controls whether the call
131 133 actually does anything.
132 134 """
133 135
134 136 # If the user has turned it off, go away
135 137 if not self.embedded_active:
136 138 return
137 139
138 140 # Normal exits from interactive mode set this flag, so the shell can't
139 141 # re-enter (it checks this variable at the start of interactive mode).
140 142 self.exit_now = False
141 143
142 144 # Allow the dummy parameter to override the global __dummy_mode
143 145 if dummy or (dummy != 0 and self.dummy_mode):
144 146 return
145 147
146 148 if self.has_readline:
147 149 self.set_completer()
148 150
149 151 if self.banner and header:
150 152 format = '%s\n%s\n'
151 153 else:
152 154 format = '%s%s\n'
153 155 banner = format % (self.banner,header)
154 156
155 157 # Call the embedding code with a stack depth of 1 so it can skip over
156 158 # our call and get the original caller's namespaces.
157 159 self.mainloop(banner, local_ns, global_ns,
158 160 stack_depth=stack_depth)
159 161
160 162 if self.exit_msg is not None:
161 163 print self.exit_msg
162 164
163 165 self.restore_sys_ipcompleter()
164 166
165 167 def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
166 168 """Embeds IPython into a running python program.
167 169
168 170 Input:
169 171
170 172 - header: An optional header message can be specified.
171 173
172 174 - local_ns, global_ns: working namespaces. If given as None, the
173 175 IPython-initialized one is updated with __main__.__dict__, so that
174 176 program variables become visible but user-specific configuration
175 177 remains possible.
176 178
177 179 - stack_depth: specifies how many levels in the stack to go to
178 180 looking for namespaces (when local_ns and global_ns are None). This
179 181 allows an intermediate caller to make sure that this function gets
180 182 the namespace from the intended level in the stack. By default (0)
181 183 it will get its locals and globals from the immediate caller.
182 184
183 185 Warning: it's possible to use this in a program which is being run by
184 186 IPython itself (via %run), but some funny things will happen (a few
185 187 globals get overwritten). In the future this will be cleaned up, as
186 188 there is no fundamental reason why it can't work perfectly."""
187 189
188 190 # Get locals and globals from caller
189 191 if local_ns is None or global_ns is None:
190 192 call_frame = sys._getframe(stack_depth).f_back
191 193
192 194 if local_ns is None:
193 195 local_ns = call_frame.f_locals
194 196 if global_ns is None:
195 197 global_ns = call_frame.f_globals
196 198
197 199 # Update namespaces and fire up interpreter
198 200
199 201 # The global one is easy, we can just throw it in
200 202 self.user_global_ns = global_ns
201 203
202 204 # but the user/local one is tricky: ipython needs it to store internal
203 205 # data, but we also need the locals. We'll copy locals in the user
204 206 # one, but will track what got copied so we can delete them at exit.
205 207 # This is so that a later embedded call doesn't see locals from a
206 208 # previous call (which most likely existed in a separate scope).
207 209 local_varnames = local_ns.keys()
208 210 self.user_ns.update(local_ns)
209 211 #self.user_ns['local_ns'] = local_ns # dbg
210 212
211 213 # Patch for global embedding to make sure that things don't overwrite
212 214 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
213 215 # FIXME. Test this a bit more carefully (the if.. is new)
214 216 if local_ns is None and global_ns is None:
215 217 self.user_global_ns.update(__main__.__dict__)
216 218
217 219 # make sure the tab-completer has the correct frame information, so it
218 220 # actually completes using the frame's locals/globals
219 221 self.set_completer_frame()
220 222
221 223 with nested(self.builtin_trap, self.display_trap):
222 224 self.interact(header)
223 225
224 226 # now, purge out the user namespace from anything we might have added
225 227 # from the caller's local namespace
226 228 delvar = self.user_ns.pop
227 229 for var in local_varnames:
228 230 delvar(var,None)
229 231
230 232 def set_completer_frame(self, frame=None):
231 233 if frame:
232 234 self.Completer.namespace = frame.f_locals
233 235 self.Completer.global_namespace = frame.f_globals
234 236 else:
235 237 self.Completer.namespace = self.user_ns
236 238 self.Completer.global_namespace = self.user_global_ns
237 239
238 240
239 241 _embedded_shell = None
240 242
241 243
242 244 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
243 245 exit_msg=''):
244 246 """Call this to embed IPython at the current point in your program.
245 247
246 248 The first invocation of this will create an :class:`InteractiveShellEmbed`
247 249 instance and then call it. Consecutive calls just call the already
248 250 created instance.
249 251
250 252 Here is a simple example::
251 253
252 254 from IPython import embed
253 255 a = 10
254 256 b = 20
255 257 embed('First time')
256 258 c = 30
257 259 d = 40
258 260 embed
259 261
260 262 Full customization can be done by passing a :class:`Struct` in as the
261 263 config argument.
262 264 """
265 if config is None:
266 config = load_default_config()
263 267 global _embedded_shell
264 268 if _embedded_shell is None:
265 269 _embedded_shell = InteractiveShellEmbed(config=config,
266 270 usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg)
267 271 _embedded_shell(header=header, stack_depth=2)
268 272
@@ -1,318 +1,349 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 The main IPython application object
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10
11 11 Notes
12 12 -----
13 13 """
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Copyright (C) 2008-2009 The IPython Development Team
17 17 #
18 18 # Distributed under the terms of the BSD License. The full license is in
19 19 # the file COPYING, distributed as part of this software.
20 20 #-----------------------------------------------------------------------------
21 21
22 22 #-----------------------------------------------------------------------------
23 23 # Imports
24 24 #-----------------------------------------------------------------------------
25 25
26 26 import os
27 27 import sys
28 28 import warnings
29 29
30 from IPython.core.application import Application
30 from IPython.core.application import Application, IPythonArgParseConfigLoader
31 31 from IPython.core import release
32 32 from IPython.core.iplib import InteractiveShell
33 from IPython.config.loader import IPythonArgParseConfigLoader, NoDefault
33 from IPython.config.loader import (
34 NoConfigDefault,
35 Config,
36 PyFileConfigLoader
37 )
34 38
35 39 from IPython.utils.ipstruct import Struct
36
40 from IPython.utils.genutils import get_ipython_dir
37 41
38 42 #-----------------------------------------------------------------------------
39 43 # Utilities and helpers
40 44 #-----------------------------------------------------------------------------
41 45
42 46
43 47 ipython_desc = """
44 48 A Python shell with automatic history (input and output), dynamic object
45 49 introspection, easier configuration, command completion, access to the system
46 50 shell and more.
47 51 """
48 52
49 53 def threaded_shell_warning():
50 54 msg = """
51 55
52 56 The IPython threaded shells and their associated command line
53 57 arguments (pylab/wthread/gthread/qthread/q4thread) have been
54 58 deprecated. See the %gui magic for information on the new interface.
55 59 """
56 60 warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
57 61
58 62
59 63 #-----------------------------------------------------------------------------
60 64 # Main classes and functions
61 65 #-----------------------------------------------------------------------------
62 66
63 67 cl_args = (
64 68 (('-autocall',), dict(
65 type=int, dest='AUTOCALL', default=NoDefault,
66 help='Set the autocall value (0,1,2).')
69 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
70 help='Set the autocall value (0,1,2).',
71 metavar='InteractiveShell.autocall')
67 72 ),
68 73 (('-autoindent',), dict(
69 action='store_true', dest='AUTOINDENT', default=NoDefault,
74 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
70 75 help='Turn on autoindenting.')
71 76 ),
72 77 (('-noautoindent',), dict(
73 action='store_false', dest='AUTOINDENT', default=NoDefault,
78 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
74 79 help='Turn off autoindenting.')
75 80 ),
76 81 (('-automagic',), dict(
77 action='store_true', dest='AUTOMAGIC', default=NoDefault,
82 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
78 83 help='Turn on the auto calling of magic commands.')
79 84 ),
80 85 (('-noautomagic',), dict(
81 action='store_false', dest='AUTOMAGIC', default=NoDefault,
86 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
82 87 help='Turn off the auto calling of magic commands.')
83 88 ),
84 89 (('-autoedit_syntax',), dict(
85 action='store_true', dest='AUTOEDIT_SYNTAX', default=NoDefault,
90 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
86 91 help='Turn on auto editing of files with syntax errors.')
87 92 ),
88 93 (('-noautoedit_syntax',), dict(
89 action='store_false', dest='AUTOEDIT_SYNTAX', default=NoDefault,
94 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
90 95 help='Turn off auto editing of files with syntax errors.')
91 96 ),
92 97 (('-banner',), dict(
93 action='store_true', dest='DISPLAY_BANNER', default=NoDefault,
98 action='store_true', dest='InteractiveShell.display_banner', default=NoConfigDefault,
94 99 help='Display a banner upon starting IPython.')
95 100 ),
96 101 (('-nobanner',), dict(
97 action='store_false', dest='DISPLAY_BANNER', default=NoDefault,
102 action='store_false', dest='InteractiveShell.display_banner', default=NoConfigDefault,
98 103 help="Don't display a banner upon starting IPython.")
99 104 ),
100 105 (('-c',), dict(
101 type=str, dest='C', default=NoDefault,
102 help="Execute the given command string.")
106 type=str, dest='InteractiveShell.c', default=NoConfigDefault,
107 help="Execute the given command string.",
108 metavar='InteractiveShell.c')
103 109 ),
104 110 (('-cache_size',), dict(
105 type=int, dest='CACHE_SIZE', default=NoDefault,
106 help="Set the size of the output cache.")
111 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
112 help="Set the size of the output cache.",
113 metavar='InteractiveShell.cache_size')
107 114 ),
108 115 (('-classic',), dict(
109 action='store_true', dest='CLASSIC', default=NoDefault,
116 action='store_true', dest='Global.classic', default=NoConfigDefault,
110 117 help="Gives IPython a similar feel to the classic Python prompt.")
111 118 ),
112 119 (('-colors',), dict(
113 type=str, dest='COLORS', default=NoDefault,
114 help="Set the color scheme (NoColor, Linux, and LightBG).")
120 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
121 help="Set the color scheme (NoColor, Linux, and LightBG).",
122 metavar='InteractiveShell.colors')
115 123 ),
116 124 (('-color_info',), dict(
117 action='store_true', dest='COLOR_INFO', default=NoDefault,
125 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
118 126 help="Enable using colors for info related things.")
119 127 ),
120 128 (('-nocolor_info',), dict(
121 action='store_false', dest='COLOR_INFO', default=NoDefault,
129 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
122 130 help="Disable using colors for info related things.")
123 131 ),
124 132 (('-confirm_exit',), dict(
125 action='store_true', dest='CONFIRM_EXIT', default=NoDefault,
133 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
126 134 help="Prompt the user when existing.")
127 135 ),
128 136 (('-noconfirm_exit',), dict(
129 action='store_false', dest='CONFIRM_EXIT', default=NoDefault,
137 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
130 138 help="Don't prompt the user when existing.")
131 139 ),
132 140 (('-deep_reload',), dict(
133 action='store_true', dest='DEEP_RELOAD', default=NoDefault,
141 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
134 142 help="Enable deep (recursive) reloading by default.")
135 143 ),
136 144 (('-nodeep_reload',), dict(
137 action='store_false', dest='DEEP_RELOAD', default=NoDefault,
145 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
138 146 help="Disable deep (recursive) reloading by default.")
139 147 ),
140 148 (('-editor',), dict(
141 type=str, dest='EDITOR', default=NoDefault,
142 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).")
149 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
150 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
151 metavar='InteractiveShell.editor')
143 152 ),
144 153 (('-log','-l'), dict(
145 action='store_true', dest='LOGSTART', default=NoDefault,
154 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
146 155 help="Start logging to the default file (./ipython_log.py).")
147 156 ),
148 157 (('-logfile','-lf'), dict(
149 type=str, dest='LOGFILE', default=NoDefault,
150 help="Specify the name of your logfile.")
158 type=str, dest='InteractiveShell.logfile', default=NoConfigDefault,
159 help="Specify the name of your logfile.",
160 metavar='InteractiveShell.logfile')
151 161 ),
152 162 (('-logplay','-lp'), dict(
153 type=str, dest='LOGPLAY', default=NoDefault,
154 help="Re-play a log file and then append to it.")
163 type=str, dest='InteractiveShell.logplay', default=NoConfigDefault,
164 help="Re-play a log file and then append to it.",
165 metavar='InteractiveShell.logplay')
155 166 ),
156 167 (('-pdb',), dict(
157 action='store_true', dest='PDB', default=NoDefault,
168 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
158 169 help="Enable auto calling the pdb debugger after every exception.")
159 170 ),
160 171 (('-nopdb',), dict(
161 action='store_false', dest='PDB', default=NoDefault,
172 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
162 173 help="Disable auto calling the pdb debugger after every exception.")
163 174 ),
164 175 (('-pprint',), dict(
165 action='store_true', dest='PPRINT', default=NoDefault,
176 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
166 177 help="Enable auto pretty printing of results.")
167 178 ),
168 179 (('-nopprint',), dict(
169 action='store_false', dest='PPRINT', default=NoDefault,
180 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
170 181 help="Disable auto auto pretty printing of results.")
171 182 ),
172 183 (('-prompt_in1','-pi1'), dict(
173 type=str, dest='PROMPT_IN1', default=NoDefault,
174 help="Set the main input prompt ('In [\#]: ')")
184 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
185 help="Set the main input prompt ('In [\#]: ')",
186 metavar='InteractiveShell.prompt_in1')
175 187 ),
176 188 (('-prompt_in2','-pi2'), dict(
177 type=str, dest='PROMPT_IN2', default=NoDefault,
178 help="Set the secondary input prompt (' .\D.: ')")
189 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
190 help="Set the secondary input prompt (' .\D.: ')",
191 metavar='InteractiveShell.prompt_in2')
179 192 ),
180 193 (('-prompt_out','-po'), dict(
181 type=str, dest='PROMPT_OUT', default=NoDefault,
182 help="Set the output prompt ('Out[\#]:')")
194 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
195 help="Set the output prompt ('Out[\#]:')",
196 metavar='InteractiveShell.prompt_out')
183 197 ),
184 198 (('-quick',), dict(
185 action='store_true', dest='QUICK', default=NoDefault,
199 action='store_true', dest='Global.quick', default=NoConfigDefault,
186 200 help="Enable quick startup with no config files.")
187 201 ),
188 202 (('-readline',), dict(
189 action='store_true', dest='READLINE_USE', default=NoDefault,
203 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
190 204 help="Enable readline for command line usage.")
191 205 ),
192 206 (('-noreadline',), dict(
193 action='store_false', dest='READLINE_USE', default=NoDefault,
207 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
194 208 help="Disable readline for command line usage.")
195 209 ),
196 210 (('-screen_length','-sl'), dict(
197 type=int, dest='SCREEN_LENGTH', default=NoDefault,
198 help='Number of lines on screen, used to control printing of long strings.')
211 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
212 help='Number of lines on screen, used to control printing of long strings.',
213 metavar='InteractiveShell.screen_length')
199 214 ),
200 215 (('-separate_in','-si'), dict(
201 type=str, dest='SEPARATE_IN', default=NoDefault,
202 help="Separator before input prompts. Default '\n'.")
216 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
217 help="Separator before input prompts. Default '\n'.",
218 metavar='InteractiveShell.separate_in')
203 219 ),
204 220 (('-separate_out','-so'), dict(
205 type=str, dest='SEPARATE_OUT', default=NoDefault,
206 help="Separator before output prompts. Default 0 (nothing).")
221 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
222 help="Separator before output prompts. Default 0 (nothing).",
223 metavar='InteractiveShell.separate_out')
207 224 ),
208 225 (('-separate_out2','-so2'), dict(
209 type=str, dest='SEPARATE_OUT2', default=NoDefault,
210 help="Separator after output prompts. Default 0 (nonight).")
226 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
227 help="Separator after output prompts. Default 0 (nonight).",
228 metavar='InteractiveShell.separate_out2')
211 229 ),
212 230 (('-nosep',), dict(
213 action='store_true', dest='NOSEP', default=NoDefault,
231 action='store_true', dest='Global.nosep', default=NoConfigDefault,
214 232 help="Eliminate all spacing between prompts.")
215 233 ),
216 234 (('-term_title',), dict(
217 action='store_true', dest='TERM_TITLE', default=NoDefault,
235 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
218 236 help="Enable auto setting the terminal title.")
219 237 ),
220 238 (('-noterm_title',), dict(
221 action='store_false', dest='TERM_TITLE', default=NoDefault,
239 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
222 240 help="Disable auto setting the terminal title.")
223 241 ),
224 242 (('-xmode',), dict(
225 type=str, dest='XMODE', default=NoDefault,
226 help="Exception mode ('Plain','Context','Verbose')")
243 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
244 help="Exception mode ('Plain','Context','Verbose')",
245 metavar='InteractiveShell.xmode')
227 246 ),
228 247 # These are only here to get the proper deprecation warnings
229 248 (('-pylab','-wthread','-qthread','-q4thread','-gthread'), dict(
230 action='store_true', dest='THREADED_SHELL', default=NoDefault,
249 action='store_true', dest='Global.threaded_shell', default=NoConfigDefault,
231 250 help="These command line flags are deprecated, see the 'gui' magic.")
232 251 ),
233 252 )
234 253
235 254
236 255 class IPythonAppCLConfigLoader(IPythonArgParseConfigLoader):
237 256
238 257 arguments = cl_args
239 258
240 259
260 _default_config_file_name = 'ipython_config.py'
261
241 262 class IPythonApp(Application):
242 263 name = 'ipython'
243 config_file_name = 'ipython_config.py'
264 config_file_name = _default_config_file_name
244 265
245 266 def create_command_line_config(self):
246 267 """Create and return a command line config loader."""
247 268 return IPythonAppCLConfigLoader(
248 269 description=ipython_desc,
249 270 version=release.version)
250 271
251 272 def post_load_command_line_config(self):
252 273 """Do actions after loading cl config."""
253 274 clc = self.command_line_config
254 275
255 # This needs to be set here, the rest are set in pre_construct.
256 if hasattr(clc, 'CLASSIC'):
257 if clc.CLASSIC: clc.QUICK = 1
258
259 276 # Display the deprecation warnings about threaded shells
260 if hasattr(clc, 'THREADED_SHELL'):
277 if hasattr(clc.Global, 'threaded_shell'):
261 278 threaded_shell_warning()
262 del clc['THREADED_SHELL']
279 del clc.Global['threaded_shell']
263 280
264 281 def load_file_config(self):
265 if hasattr(self.command_line_config, 'QUICK'):
266 if self.command_line_config.QUICK:
267 self.file_config = Struct()
282 if hasattr(self.command_line_config.Global, 'quick'):
283 if self.command_line_config.Global.quick:
284 self.file_config = Config()
268 285 return
269 286 super(IPythonApp, self).load_file_config()
270 287
271 288 def post_load_file_config(self):
272 289 """Logic goes here."""
273 290
274 291 def pre_construct(self):
275 292 config = self.master_config
276 293
277 if hasattr(config, 'CLASSIC'):
278 if config.CLASSIC:
279 config.QUICK = 1
280 config.CACHE_SIZE = 0
281 config.PPRINT = 0
282 config.PROMPT_IN1 = '>>> '
283 config.PROMPT_IN2 = '... '
284 config.PROMPT_OUT = ''
285 config.SEPARATE_IN = config.SEPARATE_OUT = config.SEPARATE_OUT2 = ''
286 config.COLORS = 'NoColor'
287 config.XMODE = 'Plain'
294 if hasattr(config.Global, 'classic'):
295 if config.Global.classic:
296 config.InteractiveShell.cache_size = 0
297 config.InteractiveShell.pprint = 0
298 config.InteractiveShell.prompt_in1 = '>>> '
299 config.InteractiveShell.prompt_in2 = '... '
300 config.InteractiveShell.prompt_out = ''
301 config.InteractiveShell.separate_in = \
302 config.InteractiveShell.separate_out = \
303 config.InteractiveShell.separate_out2 = ''
304 config.InteractiveShell.colors = 'NoColor'
305 config.InteractiveShell.xmode = 'Plain'
288 306
289 307 # All this should be moved to traitlet handlers in InteractiveShell
290 308 # But, currently InteractiveShell doesn't have support for changing
291 309 # these values at runtime. Once we support that, this should
292 310 # be moved there!!!
293 if hasattr(config, 'NOSEP'):
294 if config.NOSEP:
295 config.SEPARATE_IN = config.SEPARATE_OUT = config.SEPARATE_OUT2 = '0'
311 if hasattr(config.Global, 'nosep'):
312 if config.Global.nosep:
313 config.InteractiveShell.separate_in = \
314 config.InteractiveShell.separate_out = \
315 config.InteractiveShell.separate_out2 = '0'
296 316
297 317 def construct(self):
298 318 # I am a little hesitant to put these into InteractiveShell itself.
299 319 # But that might be the place for them
300 320 sys.path.insert(0, '')
301 321 # add personal ipythondir to sys.path so that users can put things in
302 322 # there for customization
303 323 sys.path.append(os.path.abspath(self.ipythondir))
304 324
305 325 # Create an InteractiveShell instance
306 326 self.shell = InteractiveShell(
307 327 parent=None,
308 328 config=self.master_config
309 329 )
310 print self.shell
311 330
312 331 def start_app(self):
313 332 self.shell.mainloop()
314 333
315 334
335 def load_default_config(ipythondir=None):
336 """Load the default config file from the default ipythondir.
337
338 This is useful for embedded shells.
339 """
340 if ipythondir is None:
341 ipythondir = get_ipython_dir()
342 cl = PyFileConfigLoader(_default_config_file_name, ipythondir)
343 config = cl.load_config()
344 return config
345
346
316 347 if __name__ == '__main__':
317 348 app = IPythonApp()
318 349 app.start() No newline at end of file
@@ -1,2506 +1,2503 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Main IPython Component
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 9 # Copyright (C) 2008-2009 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 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from __future__ import with_statement
20 20
21 21 import __main__
22 22 import __builtin__
23 23 import StringIO
24 24 import bdb
25 25 import codeop
26 26 import exceptions
27 27 import glob
28 28 import keyword
29 29 import new
30 30 import os
31 31 import re
32 32 import shutil
33 33 import string
34 34 import sys
35 35 import tempfile
36 36 from contextlib import nested
37 37
38 38 from IPython.core import ultratb
39 39 from IPython.core import debugger, oinspect
40 40 from IPython.core import shadowns
41 41 from IPython.core import history as ipcorehist
42 42 from IPython.core import prefilter
43 43 from IPython.core.alias import AliasManager
44 44 from IPython.core.autocall import IPyAutocall
45 45 from IPython.core.builtin_trap import BuiltinTrap
46 46 from IPython.core.display_trap import DisplayTrap
47 47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 48 from IPython.core.logger import Logger
49 49 from IPython.core.magic import Magic
50 50 from IPython.core.prompts import CachedOutput
51 51 from IPython.core.page import page
52 52 from IPython.core.prefilter import PrefilterManager
53 53 from IPython.core.component import Component
54 54 from IPython.core.oldusersetup import user_setup
55 55 from IPython.core.usage import interactive_usage, default_banner
56 56 from IPython.core.error import TryNext, UsageError
57 57 from IPython.core.splitinput import split_user_input
58 58
59 59 from IPython.extensions import pickleshare
60 60 from IPython.external.Itpl import ItplNS
61 61 from IPython.lib.backgroundjobs import BackgroundJobManager
62 62 from IPython.utils.ipstruct import Struct
63 63 from IPython.utils import PyColorize
64 64 from IPython.utils.genutils import *
65 from IPython.utils.genutils import get_ipython_dir
65 66 from IPython.utils.strdispatch import StrDispatch
66 67 from IPython.utils.platutils import toggle_set_term_title, set_term_title
67 68
68 69 # from IPython.utils import growl
69 70 # growl.start("IPython")
70 71
71 72 from IPython.utils.traitlets import (
72 73 Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode
73 74 )
74 75
75 76 #-----------------------------------------------------------------------------
76 77 # Globals
77 78 #-----------------------------------------------------------------------------
78 79
79 80
80 81 # store the builtin raw_input globally, and use this always, in case user code
81 82 # overwrites it (like wx.py.PyShell does)
82 83 raw_input_original = raw_input
83 84
84 85 # compiled regexps for autoindent management
85 86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86 87
87 88
88 89 #-----------------------------------------------------------------------------
89 90 # Utilities
90 91 #-----------------------------------------------------------------------------
91 92
92 93
93 94 ini_spaces_re = re.compile(r'^(\s+)')
94 95
95 96
96 97 def num_ini_spaces(strng):
97 98 """Return the number of initial spaces in a string"""
98 99
99 100 ini_spaces = ini_spaces_re.match(strng)
100 101 if ini_spaces:
101 102 return ini_spaces.end()
102 103 else:
103 104 return 0
104 105
105 106
106 107 def softspace(file, newvalue):
107 108 """Copied from code.py, to remove the dependency"""
108 109
109 110 oldvalue = 0
110 111 try:
111 112 oldvalue = file.softspace
112 113 except AttributeError:
113 114 pass
114 115 try:
115 116 file.softspace = newvalue
116 117 except (AttributeError, TypeError):
117 118 # "attribute-less object" or "read-only attributes"
118 119 pass
119 120 return oldvalue
120 121
121 122
122 123 class SpaceInInput(exceptions.Exception): pass
123 124
124 125 class Bunch: pass
125 126
126 127 class InputList(list):
127 128 """Class to store user input.
128 129
129 130 It's basically a list, but slices return a string instead of a list, thus
130 131 allowing things like (assuming 'In' is an instance):
131 132
132 133 exec In[4:7]
133 134
134 135 or
135 136
136 137 exec In[5:9] + In[14] + In[21:25]"""
137 138
138 139 def __getslice__(self,i,j):
139 140 return ''.join(list.__getslice__(self,i,j))
140 141
141 142
142 143 class SyntaxTB(ultratb.ListTB):
143 144 """Extension which holds some state: the last exception value"""
144 145
145 146 def __init__(self,color_scheme = 'NoColor'):
146 147 ultratb.ListTB.__init__(self,color_scheme)
147 148 self.last_syntax_error = None
148 149
149 150 def __call__(self, etype, value, elist):
150 151 self.last_syntax_error = value
151 152 ultratb.ListTB.__call__(self,etype,value,elist)
152 153
153 154 def clear_err_state(self):
154 155 """Return the current error state and clear it"""
155 156 e = self.last_syntax_error
156 157 self.last_syntax_error = None
157 158 return e
158 159
159 160
160 161 def get_default_editor():
161 162 try:
162 163 ed = os.environ['EDITOR']
163 164 except KeyError:
164 165 if os.name == 'posix':
165 166 ed = 'vi' # the only one guaranteed to be there!
166 167 else:
167 168 ed = 'notepad' # same in Windows!
168 169 return ed
169 170
170 171
171 172 class SeparateStr(Str):
172 173 """A Str subclass to validate separate_in, separate_out, etc.
173 174
174 175 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
175 176 """
176 177
177 178 def validate(self, obj, value):
178 179 if value == '0': value = ''
179 180 value = value.replace('\\n','\n')
180 181 return super(SeparateStr, self).validate(obj, value)
181 182
182 183
183 184 #-----------------------------------------------------------------------------
184 185 # Main IPython class
185 186 #-----------------------------------------------------------------------------
186 187
187 188
188 189 class InteractiveShell(Component, Magic):
189 190 """An enhanced, interactive shell for Python."""
190 191
191 autocall = Enum((0,1,2), config_key='AUTOCALL')
192 autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX')
193 autoindent = CBool(True, config_key='AUTOINDENT')
194 automagic = CBool(True, config_key='AUTOMAGIC')
195 display_banner = CBool(True, config_key='DISPLAY_BANNER')
192 autocall = Enum((0,1,2), config=True)
193 autoedit_syntax = CBool(False, config=True)
194 autoindent = CBool(True, config=True)
195 automagic = CBool(True, config=True)
196 display_banner = CBool(True, config=True)
196 197 banner = Str('')
197 banner1 = Str(default_banner, config_key='BANNER1')
198 banner2 = Str('', config_key='BANNER2')
199 c = Str('', config_key='C')
200 cache_size = Int(1000, config_key='CACHE_SIZE')
201 classic = CBool(False, config_key='CLASSIC')
202 color_info = CBool(True, config_key='COLOR_INFO')
198 banner1 = Str(default_banner, config=True)
199 banner2 = Str('', config=True)
200 c = Str('', config=True)
201 cache_size = Int(1000, config=True)
202 color_info = CBool(True, config=True)
203 203 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
204 default_value='LightBG', config_key='COLORS')
205 confirm_exit = CBool(True, config_key='CONFIRM_EXIT')
206 debug = CBool(False, config_key='DEBUG')
207 deep_reload = CBool(False, config_key='DEEP_RELOAD')
204 default_value='LightBG', config=True)
205 confirm_exit = CBool(True, config=True)
206 debug = CBool(False, config=True)
207 deep_reload = CBool(False, config=True)
208 208 embedded = CBool(False)
209 209 embedded_active = CBool(False)
210 editor = Str(get_default_editor(), config_key='EDITOR')
210 editor = Str(get_default_editor(), config=True)
211 211 filename = Str("<ipython console>")
212 interactive = CBool(False, config_key='INTERACTIVE')
213 ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__
214 logstart = CBool(False, config_key='LOGSTART')
215 logfile = Str('', config_key='LOGFILE')
216 logplay = Str('', config_key='LOGPLAY')
212 interactive = CBool(False, config=True)
213 ipythondir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
214 logstart = CBool(False, config=True)
215 logfile = Str('', config=True)
216 logplay = Str('', config=True)
217 217 object_info_string_level = Enum((0,1,2), default_value=0,
218 config_keys='OBJECT_INFO_STRING_LEVEL')
219 pager = Str('less', config_key='PAGER')
220 pdb = CBool(False, config_key='PDB')
221 pprint = CBool(True, config_key='PPRINT')
222 profile = Str('', config_key='PROFILE')
223 prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1')
224 prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2')
225 prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1')
226 prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT')
227 quiet = CBool(False, config_key='QUIET')
228
229 readline_use = CBool(True, config_key='READLINE_USE')
230 readline_merge_completions = CBool(True,
231 config_key='READLINE_MERGE_COMPLETIONS')
232 readline_omit__names = Enum((0,1,2), default_value=0,
233 config_key='READLINE_OMIT_NAMES')
234 readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS')
218 config=True)
219 pager = Str('less', config=True)
220 pdb = CBool(False, config=True)
221 pprint = CBool(True, config=True)
222 profile = Str('', config=True)
223 prompt_in1 = Str('In [\\#]: ', config=True)
224 prompt_in2 = Str(' .\\D.: ', config=True)
225 prompt_out = Str('Out[\\#]: ', config=True)
226 prompts_pad_left = CBool(True, config=True)
227 quiet = CBool(False, config=True)
228
229 readline_use = CBool(True, config=True)
230 readline_merge_completions = CBool(True, config=True)
231 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
232 readline_remove_delims = Str('-/~', config=True)
235 233 readline_parse_and_bind = List([
236 234 'tab: complete',
237 235 '"\C-l": possible-completions',
238 236 'set show-all-if-ambiguous on',
239 237 '"\C-o": tab-insert',
240 238 '"\M-i": " "',
241 239 '"\M-o": "\d\d\d\d"',
242 240 '"\M-I": "\d\d\d\d"',
243 241 '"\C-r": reverse-search-history',
244 242 '"\C-s": forward-search-history',
245 243 '"\C-p": history-search-backward',
246 244 '"\C-n": history-search-forward',
247 245 '"\e[A": history-search-backward',
248 246 '"\e[B": history-search-forward',
249 247 '"\C-k": kill-line',
250 248 '"\C-u": unix-line-discard',
251 ], allow_none=False, config_key='READLINE_PARSE_AND_BIND'
252 )
249 ], allow_none=False, config=True)
253 250
254 screen_length = Int(0, config_key='SCREEN_LENGTH')
251 screen_length = Int(0, config=True)
255 252
256 253 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
257 separate_in = SeparateStr('\n', config_key='SEPARATE_IN')
258 separate_out = SeparateStr('', config_key='SEPARATE_OUT')
259 separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2')
260
261 system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER')
262 system_verbose = CBool(False, config_key='SYSTEM_VERBOSE')
263 term_title = CBool(False, config_key='TERM_TITLE')
264 wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE')
254 separate_in = SeparateStr('\n', config=True)
255 separate_out = SeparateStr('', config=True)
256 separate_out2 = SeparateStr('', config=True)
257
258 system_header = Str('IPython system call: ', config=True)
259 system_verbose = CBool(False, config=True)
260 term_title = CBool(False, config=True)
261 wildcards_case_sensitive = CBool(True, config=True)
265 262 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
266 default_value='Context', config_key='XMODE')
263 default_value='Context', config=True)
267 264
268 alias = List(allow_none=False, config_key='ALIAS')
269 265 autoexec = List(allow_none=False)
270 266
271 267 # class attribute to indicate whether the class supports threads or not.
272 268 # Subclasses with thread support should override this as needed.
273 269 isthreaded = False
274 270
275 271 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
276 272 user_ns=None, user_global_ns=None,
277 273 banner1=None, banner2=None,
278 274 custom_exceptions=((),None)):
279 275
280 276 # This is where traitlets with a config_key argument are updated
281 277 # from the values on config.
282 super(InteractiveShell, self).__init__(parent, config=config, name='__IP')
278 super(InteractiveShell, self).__init__(parent, config=config)
283 279
284 280 # These are relatively independent and stateless
285 281 self.init_ipythondir(ipythondir)
286 282 self.init_instance_attrs()
287 283 self.init_term_title()
288 284 self.init_usage(usage)
289 285 self.init_banner(banner1, banner2)
290 286
291 # Create namespaces (user_ns, user_global_ns, alias_table, etc.)
287 # Create namespaces (user_ns, user_global_ns, etc.)
292 288 self.init_create_namespaces(user_ns, user_global_ns)
293 289 # This has to be done after init_create_namespaces because it uses
294 290 # something in self.user_ns, but before init_sys_modules, which
295 291 # is the first thing to modify sys.
296 292 self.save_sys_module_state()
297 293 self.init_sys_modules()
298 294
299 295 self.init_history()
300 296 self.init_encoding()
301 297 self.init_prefilter()
302 298
303 299 Magic.__init__(self, self)
304 300
305 301 self.init_syntax_highlighting()
306 302 self.init_hooks()
307 303 self.init_pushd_popd_magic()
308 304 self.init_traceback_handlers(custom_exceptions)
309 305 self.init_user_ns()
310 306 self.init_logger()
311 307 self.init_alias()
312 308 self.init_builtins()
313 309
314 310 # pre_config_initialization
315 311 self.init_shadow_hist()
316 312
317 313 # The next section should contain averything that was in ipmaker.
318 314 self.init_logstart()
319 315
320 316 # The following was in post_config_initialization
321 317 self.init_inspector()
322 318 self.init_readline()
323 319 self.init_prompts()
324 320 self.init_displayhook()
325 321 self.init_reload_doctest()
326 322 self.init_magics()
327 323 self.init_pdb()
328 324 self.hooks.late_startup_hook()
329 325
326 def get_ipython(self):
327 return self
328
330 329 #-------------------------------------------------------------------------
331 330 # Traitlet changed handlers
332 331 #-------------------------------------------------------------------------
333 332
334 333 def _banner1_changed(self):
335 334 self.compute_banner()
336 335
337 336 def _banner2_changed(self):
338 337 self.compute_banner()
339 338
339 def _ipythondir_changed(self, name, new):
340 if not os.path.isdir(new):
341 os.makedirs(new, mode = 0777)
342
340 343 @property
341 344 def usable_screen_length(self):
342 345 if self.screen_length == 0:
343 346 return 0
344 347 else:
345 348 num_lines_bot = self.separate_in.count('\n')+1
346 349 return self.screen_length - num_lines_bot
347 350
348 351 def _term_title_changed(self, name, new_value):
349 352 self.init_term_title()
350 353
351 354 def set_autoindent(self,value=None):
352 355 """Set the autoindent flag, checking for readline support.
353 356
354 357 If called with no arguments, it acts as a toggle."""
355 358
356 359 if not self.has_readline:
357 360 if os.name == 'posix':
358 361 warn("The auto-indent feature requires the readline library")
359 362 self.autoindent = 0
360 363 return
361 364 if value is None:
362 365 self.autoindent = not self.autoindent
363 366 else:
364 367 self.autoindent = value
365 368
366 369 #-------------------------------------------------------------------------
367 370 # init_* methods called by __init__
368 371 #-------------------------------------------------------------------------
369 372
370 373 def init_ipythondir(self, ipythondir):
371 374 if ipythondir is not None:
372 375 self.ipythondir = ipythondir
373 self.config.IPYTHONDIR = self.ipythondir
376 self.config.Global.ipythondir = self.ipythondir
374 377 return
375 378
376 if hasattr(self.config, 'IPYTHONDIR'):
377 self.ipythondir = self.config.IPYTHONDIR
378 if not hasattr(self.config, 'IPYTHONDIR'):
379 # cdw is always defined
380 self.ipythondir = os.getcwd()
381
382 # The caller must make sure that ipythondir exists. We should
383 # probably handle this using a Dir traitlet.
384 if not os.path.isdir(self.ipythondir):
385 raise IOError('IPython dir does not exist: %s' % self.ipythondir)
379 if hasattr(self.config.Global, 'ipythondir'):
380 self.ipythondir = self.config.Global.ipythondir
381 else:
382 self.ipythondir = get_ipython_dir()
386 383
387 384 # All children can just read this
388 self.config.IPYTHONDIR = self.ipythondir
385 self.config.Global.ipythondir = self.ipythondir
389 386
390 387 def init_instance_attrs(self):
391 388 self.jobs = BackgroundJobManager()
392 389 self.more = False
393 390
394 391 # command compiler
395 392 self.compile = codeop.CommandCompiler()
396 393
397 394 # User input buffer
398 395 self.buffer = []
399 396
400 397 # Make an empty namespace, which extension writers can rely on both
401 398 # existing and NEVER being used by ipython itself. This gives them a
402 399 # convenient location for storing additional information and state
403 400 # their extensions may require, without fear of collisions with other
404 401 # ipython names that may develop later.
405 402 self.meta = Struct()
406 403
407 404 # Object variable to store code object waiting execution. This is
408 405 # used mainly by the multithreaded shells, but it can come in handy in
409 406 # other situations. No need to use a Queue here, since it's a single
410 407 # item which gets cleared once run.
411 408 self.code_to_run = None
412 409
413 410 # Flag to mark unconditional exit
414 411 self.exit_now = False
415 412
416 413 # Temporary files used for various purposes. Deleted at exit.
417 414 self.tempfiles = []
418 415
419 416 # Keep track of readline usage (later set by init_readline)
420 417 self.has_readline = False
421 418
422 419 # keep track of where we started running (mainly for crash post-mortem)
423 420 # This is not being used anywhere currently.
424 421 self.starting_dir = os.getcwd()
425 422
426 423 # Indentation management
427 424 self.indent_current_nsp = 0
428 425
429 426 def init_term_title(self):
430 427 # Enable or disable the terminal title.
431 428 if self.term_title:
432 429 toggle_set_term_title(True)
433 430 set_term_title('IPython: ' + abbrev_cwd())
434 431 else:
435 432 toggle_set_term_title(False)
436 433
437 434 def init_usage(self, usage=None):
438 435 if usage is None:
439 436 self.usage = interactive_usage
440 437 else:
441 438 self.usage = usage
442 439
443 440 def init_banner(self, banner1, banner2):
444 441 if self.c: # regular python doesn't print the banner with -c
445 442 self.display_banner = False
446 443 if banner1 is not None:
447 444 self.banner1 = banner1
448 445 if banner2 is not None:
449 446 self.banner2 = banner2
450 447 self.compute_banner()
451 448
452 449 def compute_banner(self):
453 450 self.banner = self.banner1 + '\n'
454 451 if self.profile:
455 452 self.banner += '\nIPython profile: %s\n' % self.profile
456 453 if self.banner2:
457 454 self.banner += '\n' + self.banner2 + '\n'
458 455
459 456 def init_encoding(self):
460 457 # Get system encoding at startup time. Certain terminals (like Emacs
461 458 # under Win32 have it set to None, and we need to have a known valid
462 459 # encoding to use in the raw_input() method
463 460 try:
464 461 self.stdin_encoding = sys.stdin.encoding or 'ascii'
465 462 except AttributeError:
466 463 self.stdin_encoding = 'ascii'
467 464
468 465 def init_syntax_highlighting(self):
469 466 # Python source parser/formatter for syntax highlighting
470 467 pyformat = PyColorize.Parser().format
471 468 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
472 469
473 470 def init_pushd_popd_magic(self):
474 471 # for pushd/popd management
475 472 try:
476 473 self.home_dir = get_home_dir()
477 474 except HomeDirError, msg:
478 475 fatal(msg)
479 476
480 477 self.dir_stack = []
481 478
482 479 def init_logger(self):
483 480 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
484 481 # local shortcut, this is used a LOT
485 482 self.log = self.logger.log
486 483 # template for logfile headers. It gets resolved at runtime by the
487 484 # logstart method.
488 485 self.loghead_tpl = \
489 486 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
490 487 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
491 488 #log# opts = %s
492 489 #log# args = %s
493 490 #log# It is safe to make manual edits below here.
494 491 #log#-----------------------------------------------------------------------
495 492 """
496 493
497 494 def init_logstart(self):
498 495 if self.logplay:
499 496 self.magic_logstart(self.logplay + ' append')
500 497 elif self.logfile:
501 498 self.magic_logstart(self.logfile)
502 499 elif self.logstart:
503 500 self.magic_logstart()
504 501
505 502 def init_builtins(self):
506 503 self.builtin_trap = BuiltinTrap(self)
507 504
508 505 def init_inspector(self):
509 506 # Object inspector
510 507 self.inspector = oinspect.Inspector(oinspect.InspectColors,
511 508 PyColorize.ANSICodeColors,
512 509 'NoColor',
513 510 self.object_info_string_level)
514 511
515 512 def init_prompts(self):
516 513 # Initialize cache, set in/out prompts and printing system
517 514 self.outputcache = CachedOutput(self,
518 515 self.cache_size,
519 516 self.pprint,
520 517 input_sep = self.separate_in,
521 518 output_sep = self.separate_out,
522 519 output_sep2 = self.separate_out2,
523 520 ps1 = self.prompt_in1,
524 521 ps2 = self.prompt_in2,
525 522 ps_out = self.prompt_out,
526 523 pad_left = self.prompts_pad_left)
527 524
528 525 # user may have over-ridden the default print hook:
529 526 try:
530 527 self.outputcache.__class__.display = self.hooks.display
531 528 except AttributeError:
532 529 pass
533 530
534 531 def init_displayhook(self):
535 532 self.display_trap = DisplayTrap(self, self.outputcache)
536 533
537 534 def init_reload_doctest(self):
538 535 # Do a proper resetting of doctest, including the necessary displayhook
539 536 # monkeypatching
540 537 try:
541 538 doctest_reload()
542 539 except ImportError:
543 540 warn("doctest module does not exist.")
544 541
545 542 #-------------------------------------------------------------------------
546 543 # Things related to injections into the sys module
547 544 #-------------------------------------------------------------------------
548 545
549 546 def save_sys_module_state(self):
550 547 """Save the state of hooks in the sys module.
551 548
552 549 This has to be called after self.user_ns is created.
553 550 """
554 551 self._orig_sys_module_state = {}
555 552 self._orig_sys_module_state['stdin'] = sys.stdin
556 553 self._orig_sys_module_state['stdout'] = sys.stdout
557 554 self._orig_sys_module_state['stderr'] = sys.stderr
558 555 self._orig_sys_module_state['excepthook'] = sys.excepthook
559 556 try:
560 557 self._orig_sys_modules_main_name = self.user_ns['__name__']
561 558 except KeyError:
562 559 pass
563 560
564 561 def restore_sys_module_state(self):
565 562 """Restore the state of the sys module."""
566 563 try:
567 564 for k, v in self._orig_sys_module_state.items():
568 565 setattr(sys, k, v)
569 566 except AttributeError:
570 567 pass
571 568 try:
572 569 delattr(sys, 'ipcompleter')
573 570 except AttributeError:
574 571 pass
575 572 # Reset what what done in self.init_sys_modules
576 573 try:
577 574 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
578 575 except (AttributeError, KeyError):
579 576 pass
580 577
581 578 #-------------------------------------------------------------------------
582 579 # Things related to hooks
583 580 #-------------------------------------------------------------------------
584 581
585 582 def init_hooks(self):
586 583 # hooks holds pointers used for user-side customizations
587 584 self.hooks = Struct()
588 585
589 586 self.strdispatchers = {}
590 587
591 588 # Set all default hooks, defined in the IPython.hooks module.
592 589 import IPython.core.hooks
593 590 hooks = IPython.core.hooks
594 591 for hook_name in hooks.__all__:
595 592 # default hooks have priority 100, i.e. low; user hooks should have
596 593 # 0-100 priority
597 594 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
598 595
599 596 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
600 597 """set_hook(name,hook) -> sets an internal IPython hook.
601 598
602 599 IPython exposes some of its internal API as user-modifiable hooks. By
603 600 adding your function to one of these hooks, you can modify IPython's
604 601 behavior to call at runtime your own routines."""
605 602
606 603 # At some point in the future, this should validate the hook before it
607 604 # accepts it. Probably at least check that the hook takes the number
608 605 # of args it's supposed to.
609 606
610 607 f = new.instancemethod(hook,self,self.__class__)
611 608
612 609 # check if the hook is for strdispatcher first
613 610 if str_key is not None:
614 611 sdp = self.strdispatchers.get(name, StrDispatch())
615 612 sdp.add_s(str_key, f, priority )
616 613 self.strdispatchers[name] = sdp
617 614 return
618 615 if re_key is not None:
619 616 sdp = self.strdispatchers.get(name, StrDispatch())
620 617 sdp.add_re(re.compile(re_key), f, priority )
621 618 self.strdispatchers[name] = sdp
622 619 return
623 620
624 621 dp = getattr(self.hooks, name, None)
625 622 if name not in IPython.core.hooks.__all__:
626 623 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
627 624 if not dp:
628 625 dp = IPython.core.hooks.CommandChainDispatcher()
629 626
630 627 try:
631 628 dp.add(f,priority)
632 629 except AttributeError:
633 630 # it was not commandchain, plain old func - replace
634 631 dp = f
635 632
636 633 setattr(self.hooks,name, dp)
637 634
638 635 #-------------------------------------------------------------------------
639 636 # Things related to the "main" module
640 637 #-------------------------------------------------------------------------
641 638
642 639 def new_main_mod(self,ns=None):
643 640 """Return a new 'main' module object for user code execution.
644 641 """
645 642 main_mod = self._user_main_module
646 643 init_fakemod_dict(main_mod,ns)
647 644 return main_mod
648 645
649 646 def cache_main_mod(self,ns,fname):
650 647 """Cache a main module's namespace.
651 648
652 649 When scripts are executed via %run, we must keep a reference to the
653 650 namespace of their __main__ module (a FakeModule instance) around so
654 651 that Python doesn't clear it, rendering objects defined therein
655 652 useless.
656 653
657 654 This method keeps said reference in a private dict, keyed by the
658 655 absolute path of the module object (which corresponds to the script
659 656 path). This way, for multiple executions of the same script we only
660 657 keep one copy of the namespace (the last one), thus preventing memory
661 658 leaks from old references while allowing the objects from the last
662 659 execution to be accessible.
663 660
664 661 Note: we can not allow the actual FakeModule instances to be deleted,
665 662 because of how Python tears down modules (it hard-sets all their
666 663 references to None without regard for reference counts). This method
667 664 must therefore make a *copy* of the given namespace, to allow the
668 665 original module's __dict__ to be cleared and reused.
669 666
670 667
671 668 Parameters
672 669 ----------
673 670 ns : a namespace (a dict, typically)
674 671
675 672 fname : str
676 673 Filename associated with the namespace.
677 674
678 675 Examples
679 676 --------
680 677
681 678 In [10]: import IPython
682 679
683 680 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
684 681
685 682 In [12]: IPython.__file__ in _ip._main_ns_cache
686 683 Out[12]: True
687 684 """
688 685 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
689 686
690 687 def clear_main_mod_cache(self):
691 688 """Clear the cache of main modules.
692 689
693 690 Mainly for use by utilities like %reset.
694 691
695 692 Examples
696 693 --------
697 694
698 695 In [15]: import IPython
699 696
700 697 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
701 698
702 699 In [17]: len(_ip._main_ns_cache) > 0
703 700 Out[17]: True
704 701
705 702 In [18]: _ip.clear_main_mod_cache()
706 703
707 704 In [19]: len(_ip._main_ns_cache) == 0
708 705 Out[19]: True
709 706 """
710 707 self._main_ns_cache.clear()
711 708
712 709 #-------------------------------------------------------------------------
713 710 # Things related to debugging
714 711 #-------------------------------------------------------------------------
715 712
716 713 def init_pdb(self):
717 714 # Set calling of pdb on exceptions
718 715 # self.call_pdb is a property
719 716 self.call_pdb = self.pdb
720 717
721 718 def _get_call_pdb(self):
722 719 return self._call_pdb
723 720
724 721 def _set_call_pdb(self,val):
725 722
726 723 if val not in (0,1,False,True):
727 724 raise ValueError,'new call_pdb value must be boolean'
728 725
729 726 # store value in instance
730 727 self._call_pdb = val
731 728
732 729 # notify the actual exception handlers
733 730 self.InteractiveTB.call_pdb = val
734 731 if self.isthreaded:
735 732 try:
736 733 self.sys_excepthook.call_pdb = val
737 734 except:
738 735 warn('Failed to activate pdb for threaded exception handler')
739 736
740 737 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
741 738 'Control auto-activation of pdb at exceptions')
742 739
743 740 def debugger(self,force=False):
744 741 """Call the pydb/pdb debugger.
745 742
746 743 Keywords:
747 744
748 745 - force(False): by default, this routine checks the instance call_pdb
749 746 flag and does not actually invoke the debugger if the flag is false.
750 747 The 'force' option forces the debugger to activate even if the flag
751 748 is false.
752 749 """
753 750
754 751 if not (force or self.call_pdb):
755 752 return
756 753
757 754 if not hasattr(sys,'last_traceback'):
758 755 error('No traceback has been produced, nothing to debug.')
759 756 return
760 757
761 758 # use pydb if available
762 759 if debugger.has_pydb:
763 760 from pydb import pm
764 761 else:
765 762 # fallback to our internal debugger
766 763 pm = lambda : self.InteractiveTB.debugger(force=True)
767 764 self.history_saving_wrapper(pm)()
768 765
769 766 #-------------------------------------------------------------------------
770 767 # Things related to IPython's various namespaces
771 768 #-------------------------------------------------------------------------
772 769
773 770 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
774 771 # Create the namespace where the user will operate. user_ns is
775 772 # normally the only one used, and it is passed to the exec calls as
776 773 # the locals argument. But we do carry a user_global_ns namespace
777 774 # given as the exec 'globals' argument, This is useful in embedding
778 775 # situations where the ipython shell opens in a context where the
779 776 # distinction between locals and globals is meaningful. For
780 777 # non-embedded contexts, it is just the same object as the user_ns dict.
781 778
782 779 # FIXME. For some strange reason, __builtins__ is showing up at user
783 780 # level as a dict instead of a module. This is a manual fix, but I
784 781 # should really track down where the problem is coming from. Alex
785 782 # Schmolck reported this problem first.
786 783
787 784 # A useful post by Alex Martelli on this topic:
788 785 # Re: inconsistent value from __builtins__
789 786 # Von: Alex Martelli <aleaxit@yahoo.com>
790 787 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
791 788 # Gruppen: comp.lang.python
792 789
793 790 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
794 791 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
795 792 # > <type 'dict'>
796 793 # > >>> print type(__builtins__)
797 794 # > <type 'module'>
798 795 # > Is this difference in return value intentional?
799 796
800 797 # Well, it's documented that '__builtins__' can be either a dictionary
801 798 # or a module, and it's been that way for a long time. Whether it's
802 799 # intentional (or sensible), I don't know. In any case, the idea is
803 800 # that if you need to access the built-in namespace directly, you
804 801 # should start with "import __builtin__" (note, no 's') which will
805 802 # definitely give you a module. Yeah, it's somewhat confusing:-(.
806 803
807 804 # These routines return properly built dicts as needed by the rest of
808 805 # the code, and can also be used by extension writers to generate
809 806 # properly initialized namespaces.
810 807 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
811 808 user_global_ns)
812 809
813 810 # Assign namespaces
814 811 # This is the namespace where all normal user variables live
815 812 self.user_ns = user_ns
816 813 self.user_global_ns = user_global_ns
817 814
818 815 # An auxiliary namespace that checks what parts of the user_ns were
819 816 # loaded at startup, so we can list later only variables defined in
820 817 # actual interactive use. Since it is always a subset of user_ns, it
821 818 # doesn't need to be seaparately tracked in the ns_table
822 819 self.user_config_ns = {}
823 820
824 821 # A namespace to keep track of internal data structures to prevent
825 822 # them from cluttering user-visible stuff. Will be updated later
826 823 self.internal_ns = {}
827 824
828 # Namespace of system aliases. Each entry in the alias
829 # table must be a 2-tuple of the form (N,name), where N is the number
830 # of positional arguments of the alias.
831 self.alias_table = {}
832
833 825 # Now that FakeModule produces a real module, we've run into a nasty
834 826 # problem: after script execution (via %run), the module where the user
835 827 # code ran is deleted. Now that this object is a true module (needed
836 828 # so docetst and other tools work correctly), the Python module
837 829 # teardown mechanism runs over it, and sets to None every variable
838 830 # present in that module. Top-level references to objects from the
839 831 # script survive, because the user_ns is updated with them. However,
840 832 # calling functions defined in the script that use other things from
841 833 # the script will fail, because the function's closure had references
842 834 # to the original objects, which are now all None. So we must protect
843 835 # these modules from deletion by keeping a cache.
844 836 #
845 837 # To avoid keeping stale modules around (we only need the one from the
846 838 # last run), we use a dict keyed with the full path to the script, so
847 839 # only the last version of the module is held in the cache. Note,
848 840 # however, that we must cache the module *namespace contents* (their
849 841 # __dict__). Because if we try to cache the actual modules, old ones
850 842 # (uncached) could be destroyed while still holding references (such as
851 843 # those held by GUI objects that tend to be long-lived)>
852 844 #
853 845 # The %reset command will flush this cache. See the cache_main_mod()
854 846 # and clear_main_mod_cache() methods for details on use.
855 847
856 848 # This is the cache used for 'main' namespaces
857 849 self._main_ns_cache = {}
858 850 # And this is the single instance of FakeModule whose __dict__ we keep
859 851 # copying and clearing for reuse on each %run
860 852 self._user_main_module = FakeModule()
861 853
862 854 # A table holding all the namespaces IPython deals with, so that
863 855 # introspection facilities can search easily.
864 856 self.ns_table = {'user':user_ns,
865 857 'user_global':user_global_ns,
866 'alias':self.alias_table,
867 858 'internal':self.internal_ns,
868 859 'builtin':__builtin__.__dict__
869 860 }
870 861
871 862 # Similarly, track all namespaces where references can be held and that
872 863 # we can safely clear (so it can NOT include builtin). This one can be
873 864 # a simple list.
874 865 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
875 self.alias_table, self.internal_ns,
876 self._main_ns_cache ]
866 self.internal_ns, self._main_ns_cache ]
877 867
878 868 def init_sys_modules(self):
879 869 # We need to insert into sys.modules something that looks like a
880 870 # module but which accesses the IPython namespace, for shelve and
881 871 # pickle to work interactively. Normally they rely on getting
882 872 # everything out of __main__, but for embedding purposes each IPython
883 873 # instance has its own private namespace, so we can't go shoving
884 874 # everything into __main__.
885 875
886 876 # note, however, that we should only do this for non-embedded
887 877 # ipythons, which really mimic the __main__.__dict__ with their own
888 878 # namespace. Embedded instances, on the other hand, should not do
889 879 # this because they need to manage the user local/global namespaces
890 880 # only, but they live within a 'normal' __main__ (meaning, they
891 881 # shouldn't overtake the execution environment of the script they're
892 882 # embedded in).
893 883
894 884 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
895 885
896 886 try:
897 887 main_name = self.user_ns['__name__']
898 888 except KeyError:
899 889 raise KeyError('user_ns dictionary MUST have a "__name__" key')
900 890 else:
901 891 sys.modules[main_name] = FakeModule(self.user_ns)
902 892
903 893 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
904 894 """Return a valid local and global user interactive namespaces.
905 895
906 896 This builds a dict with the minimal information needed to operate as a
907 897 valid IPython user namespace, which you can pass to the various
908 898 embedding classes in ipython. The default implementation returns the
909 899 same dict for both the locals and the globals to allow functions to
910 900 refer to variables in the namespace. Customized implementations can
911 901 return different dicts. The locals dictionary can actually be anything
912 902 following the basic mapping protocol of a dict, but the globals dict
913 903 must be a true dict, not even a subclass. It is recommended that any
914 904 custom object for the locals namespace synchronize with the globals
915 905 dict somehow.
916 906
917 907 Raises TypeError if the provided globals namespace is not a true dict.
918 908
919 909 :Parameters:
920 910 user_ns : dict-like, optional
921 911 The current user namespace. The items in this namespace should
922 912 be included in the output. If None, an appropriate blank
923 913 namespace should be created.
924 914 user_global_ns : dict, optional
925 915 The current user global namespace. The items in this namespace
926 916 should be included in the output. If None, an appropriate
927 917 blank namespace should be created.
928 918
929 919 :Returns:
930 920 A tuple pair of dictionary-like object to be used as the local namespace
931 921 of the interpreter and a dict to be used as the global namespace.
932 922 """
933 923
934 924 if user_ns is None:
935 925 # Set __name__ to __main__ to better match the behavior of the
936 926 # normal interpreter.
937 927 user_ns = {'__name__' :'__main__',
938 928 '__builtins__' : __builtin__,
939 929 }
940 930 else:
941 931 user_ns.setdefault('__name__','__main__')
942 932 user_ns.setdefault('__builtins__',__builtin__)
943 933
944 934 if user_global_ns is None:
945 935 user_global_ns = user_ns
946 936 if type(user_global_ns) is not dict:
947 937 raise TypeError("user_global_ns must be a true dict; got %r"
948 938 % type(user_global_ns))
949 939
950 940 return user_ns, user_global_ns
951 941
952 942 def init_user_ns(self):
953 943 """Initialize all user-visible namespaces to their minimum defaults.
954 944
955 945 Certain history lists are also initialized here, as they effectively
956 946 act as user namespaces.
957 947
958 948 Notes
959 949 -----
960 950 All data structures here are only filled in, they are NOT reset by this
961 951 method. If they were not empty before, data will simply be added to
962 952 therm.
963 953 """
964 # The user namespace MUST have a pointer to the shell itself.
965 self.user_ns[self.name] = self
966
967 954 # Store myself as the public api!!!
968 self.user_ns['_ip'] = self
955 self.user_ns['get_ipython'] = self.get_ipython
969 956
970 957 # make global variables for user access to the histories
971 958 self.user_ns['_ih'] = self.input_hist
972 959 self.user_ns['_oh'] = self.output_hist
973 960 self.user_ns['_dh'] = self.dir_hist
974 961
975 962 # user aliases to input and output histories
976 963 self.user_ns['In'] = self.input_hist
977 964 self.user_ns['Out'] = self.output_hist
978 965
979 966 self.user_ns['_sh'] = shadowns
980 967
981 968 # Put 'help' in the user namespace
982 969 try:
983 970 from site import _Helper
984 971 self.user_ns['help'] = _Helper()
985 972 except ImportError:
986 973 warn('help() not available - check site.py')
987 974
988 975 def reset(self):
989 976 """Clear all internal namespaces.
990 977
991 978 Note that this is much more aggressive than %reset, since it clears
992 979 fully all namespaces, as well as all input/output lists.
993 980 """
994 981 for ns in self.ns_refs_table:
995 982 ns.clear()
996 983
984 self.alias_manager.clear_aliases()
985
997 986 # Clear input and output histories
998 987 self.input_hist[:] = []
999 988 self.input_hist_raw[:] = []
1000 989 self.output_hist.clear()
990
1001 991 # Restore the user namespaces to minimal usability
1002 992 self.init_user_ns()
1003 993
994 # Restore the default and user aliases
995 self.alias_manager.init_aliases()
996
1004 997 def push(self, variables, interactive=True):
1005 998 """Inject a group of variables into the IPython user namespace.
1006 999
1007 1000 Parameters
1008 1001 ----------
1009 1002 variables : dict, str or list/tuple of str
1010 1003 The variables to inject into the user's namespace. If a dict,
1011 1004 a simple update is done. If a str, the string is assumed to
1012 1005 have variable names separated by spaces. A list/tuple of str
1013 1006 can also be used to give the variable names. If just the variable
1014 1007 names are give (list/tuple/str) then the variable values looked
1015 1008 up in the callers frame.
1016 1009 interactive : bool
1017 1010 If True (default), the variables will be listed with the ``who``
1018 1011 magic.
1019 1012 """
1020 1013 vdict = None
1021 1014
1022 1015 # We need a dict of name/value pairs to do namespace updates.
1023 1016 if isinstance(variables, dict):
1024 1017 vdict = variables
1025 1018 elif isinstance(variables, (basestring, list, tuple)):
1026 1019 if isinstance(variables, basestring):
1027 1020 vlist = variables.split()
1028 1021 else:
1029 1022 vlist = variables
1030 1023 vdict = {}
1031 1024 cf = sys._getframe(1)
1032 1025 for name in vlist:
1033 1026 try:
1034 1027 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1035 1028 except:
1036 1029 print ('Could not get variable %s from %s' %
1037 1030 (name,cf.f_code.co_name))
1038 1031 else:
1039 1032 raise ValueError('variables must be a dict/str/list/tuple')
1040 1033
1041 1034 # Propagate variables to user namespace
1042 1035 self.user_ns.update(vdict)
1043 1036
1044 1037 # And configure interactive visibility
1045 1038 config_ns = self.user_config_ns
1046 1039 if interactive:
1047 1040 for name, val in vdict.iteritems():
1048 1041 config_ns.pop(name, None)
1049 1042 else:
1050 1043 for name,val in vdict.iteritems():
1051 1044 config_ns[name] = val
1052 1045
1053 1046 #-------------------------------------------------------------------------
1054 1047 # Things related to history management
1055 1048 #-------------------------------------------------------------------------
1056 1049
1057 1050 def init_history(self):
1058 1051 # List of input with multi-line handling.
1059 1052 self.input_hist = InputList()
1060 1053 # This one will hold the 'raw' input history, without any
1061 1054 # pre-processing. This will allow users to retrieve the input just as
1062 1055 # it was exactly typed in by the user, with %hist -r.
1063 1056 self.input_hist_raw = InputList()
1064 1057
1065 1058 # list of visited directories
1066 1059 try:
1067 1060 self.dir_hist = [os.getcwd()]
1068 1061 except OSError:
1069 1062 self.dir_hist = []
1070 1063
1071 1064 # dict of output history
1072 1065 self.output_hist = {}
1073 1066
1074 1067 # Now the history file
1075 1068 try:
1076 1069 histfname = 'history-%s' % self.profile
1077 1070 except AttributeError:
1078 1071 histfname = 'history'
1079 self.histfile = os.path.join(self.config.IPYTHONDIR, histfname)
1072 self.histfile = os.path.join(self.ipythondir, histfname)
1080 1073
1081 1074 # Fill the history zero entry, user counter starts at 1
1082 1075 self.input_hist.append('\n')
1083 1076 self.input_hist_raw.append('\n')
1084 1077
1085 1078 def init_shadow_hist(self):
1086 1079 try:
1087 self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db")
1080 self.db = pickleshare.PickleShareDB(self.ipythondir + "/db")
1088 1081 except exceptions.UnicodeDecodeError:
1089 1082 print "Your ipythondir can't be decoded to unicode!"
1090 1083 print "Please set HOME environment variable to something that"
1091 1084 print r"only has ASCII characters, e.g. c:\home"
1092 print "Now it is", self.config.IPYTHONDIR
1085 print "Now it is", self.ipythondir
1093 1086 sys.exit()
1094 1087 self.shadowhist = ipcorehist.ShadowHist(self.db)
1095 1088
1096 1089 def savehist(self):
1097 1090 """Save input history to a file (via readline library)."""
1098 1091
1099 1092 if not self.has_readline:
1100 1093 return
1101 1094
1102 1095 try:
1103 1096 self.readline.write_history_file(self.histfile)
1104 1097 except:
1105 1098 print 'Unable to save IPython command history to file: ' + \
1106 1099 `self.histfile`
1107 1100
1108 1101 def reloadhist(self):
1109 1102 """Reload the input history from disk file."""
1110 1103
1111 1104 if self.has_readline:
1112 1105 try:
1113 1106 self.readline.clear_history()
1114 1107 self.readline.read_history_file(self.shell.histfile)
1115 1108 except AttributeError:
1116 1109 pass
1117 1110
1118 1111 def history_saving_wrapper(self, func):
1119 1112 """ Wrap func for readline history saving
1120 1113
1121 1114 Convert func into callable that saves & restores
1122 1115 history around the call """
1123 1116
1124 1117 if not self.has_readline:
1125 1118 return func
1126 1119
1127 1120 def wrapper():
1128 1121 self.savehist()
1129 1122 try:
1130 1123 func()
1131 1124 finally:
1132 1125 readline.read_history_file(self.histfile)
1133 1126 return wrapper
1134 1127
1135 1128 #-------------------------------------------------------------------------
1136 1129 # Things related to exception handling and tracebacks (not debugging)
1137 1130 #-------------------------------------------------------------------------
1138 1131
1139 1132 def init_traceback_handlers(self, custom_exceptions):
1140 1133 # Syntax error handler.
1141 1134 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1142 1135
1143 1136 # The interactive one is initialized with an offset, meaning we always
1144 1137 # want to remove the topmost item in the traceback, which is our own
1145 1138 # internal code. Valid modes: ['Plain','Context','Verbose']
1146 1139 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1147 1140 color_scheme='NoColor',
1148 1141 tb_offset = 1)
1149 1142
1150 1143 # IPython itself shouldn't crash. This will produce a detailed
1151 1144 # post-mortem if it does. But we only install the crash handler for
1152 1145 # non-threaded shells, the threaded ones use a normal verbose reporter
1153 1146 # and lose the crash handler. This is because exceptions in the main
1154 1147 # thread (such as in GUI code) propagate directly to sys.excepthook,
1155 1148 # and there's no point in printing crash dumps for every user exception.
1156 1149 if self.isthreaded:
1157 1150 ipCrashHandler = ultratb.FormattedTB()
1158 1151 else:
1159 1152 from IPython.core import crashhandler
1160 1153 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1161 1154 self.set_crash_handler(ipCrashHandler)
1162 1155
1163 1156 # and add any custom exception handlers the user may have specified
1164 1157 self.set_custom_exc(*custom_exceptions)
1165 1158
1166 1159 def set_crash_handler(self, crashHandler):
1167 1160 """Set the IPython crash handler.
1168 1161
1169 1162 This must be a callable with a signature suitable for use as
1170 1163 sys.excepthook."""
1171 1164
1172 1165 # Install the given crash handler as the Python exception hook
1173 1166 sys.excepthook = crashHandler
1174 1167
1175 1168 # The instance will store a pointer to this, so that runtime code
1176 1169 # (such as magics) can access it. This is because during the
1177 1170 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1178 1171 # frameworks).
1179 1172 self.sys_excepthook = sys.excepthook
1180 1173
1181 1174 def set_custom_exc(self,exc_tuple,handler):
1182 1175 """set_custom_exc(exc_tuple,handler)
1183 1176
1184 1177 Set a custom exception handler, which will be called if any of the
1185 1178 exceptions in exc_tuple occur in the mainloop (specifically, in the
1186 1179 runcode() method.
1187 1180
1188 1181 Inputs:
1189 1182
1190 1183 - exc_tuple: a *tuple* of valid exceptions to call the defined
1191 1184 handler for. It is very important that you use a tuple, and NOT A
1192 1185 LIST here, because of the way Python's except statement works. If
1193 1186 you only want to trap a single exception, use a singleton tuple:
1194 1187
1195 1188 exc_tuple == (MyCustomException,)
1196 1189
1197 1190 - handler: this must be defined as a function with the following
1198 1191 basic interface: def my_handler(self,etype,value,tb).
1199 1192
1200 1193 This will be made into an instance method (via new.instancemethod)
1201 1194 of IPython itself, and it will be called if any of the exceptions
1202 1195 listed in the exc_tuple are caught. If the handler is None, an
1203 1196 internal basic one is used, which just prints basic info.
1204 1197
1205 1198 WARNING: by putting in your own exception handler into IPython's main
1206 1199 execution loop, you run a very good chance of nasty crashes. This
1207 1200 facility should only be used if you really know what you are doing."""
1208 1201
1209 1202 assert type(exc_tuple)==type(()) , \
1210 1203 "The custom exceptions must be given AS A TUPLE."
1211 1204
1212 1205 def dummy_handler(self,etype,value,tb):
1213 1206 print '*** Simple custom exception handler ***'
1214 1207 print 'Exception type :',etype
1215 1208 print 'Exception value:',value
1216 1209 print 'Traceback :',tb
1217 1210 print 'Source code :','\n'.join(self.buffer)
1218 1211
1219 1212 if handler is None: handler = dummy_handler
1220 1213
1221 1214 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1222 1215 self.custom_exceptions = exc_tuple
1223 1216
1224 1217 def excepthook(self, etype, value, tb):
1225 1218 """One more defense for GUI apps that call sys.excepthook.
1226 1219
1227 1220 GUI frameworks like wxPython trap exceptions and call
1228 1221 sys.excepthook themselves. I guess this is a feature that
1229 1222 enables them to keep running after exceptions that would
1230 1223 otherwise kill their mainloop. This is a bother for IPython
1231 1224 which excepts to catch all of the program exceptions with a try:
1232 1225 except: statement.
1233 1226
1234 1227 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1235 1228 any app directly invokes sys.excepthook, it will look to the user like
1236 1229 IPython crashed. In order to work around this, we can disable the
1237 1230 CrashHandler and replace it with this excepthook instead, which prints a
1238 1231 regular traceback using our InteractiveTB. In this fashion, apps which
1239 1232 call sys.excepthook will generate a regular-looking exception from
1240 1233 IPython, and the CrashHandler will only be triggered by real IPython
1241 1234 crashes.
1242 1235
1243 1236 This hook should be used sparingly, only in places which are not likely
1244 1237 to be true IPython errors.
1245 1238 """
1246 1239 self.showtraceback((etype,value,tb),tb_offset=0)
1247 1240
1248 1241 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1249 1242 """Display the exception that just occurred.
1250 1243
1251 1244 If nothing is known about the exception, this is the method which
1252 1245 should be used throughout the code for presenting user tracebacks,
1253 1246 rather than directly invoking the InteractiveTB object.
1254 1247
1255 1248 A specific showsyntaxerror() also exists, but this method can take
1256 1249 care of calling it if needed, so unless you are explicitly catching a
1257 1250 SyntaxError exception, don't try to analyze the stack manually and
1258 1251 simply call this method."""
1259 1252
1260 1253
1261 1254 # Though this won't be called by syntax errors in the input line,
1262 1255 # there may be SyntaxError cases whith imported code.
1263 1256
1264 1257 try:
1265 1258 if exc_tuple is None:
1266 1259 etype, value, tb = sys.exc_info()
1267 1260 else:
1268 1261 etype, value, tb = exc_tuple
1269 1262
1270 1263 if etype is SyntaxError:
1271 1264 self.showsyntaxerror(filename)
1272 1265 elif etype is UsageError:
1273 1266 print "UsageError:", value
1274 1267 else:
1275 1268 # WARNING: these variables are somewhat deprecated and not
1276 1269 # necessarily safe to use in a threaded environment, but tools
1277 1270 # like pdb depend on their existence, so let's set them. If we
1278 1271 # find problems in the field, we'll need to revisit their use.
1279 1272 sys.last_type = etype
1280 1273 sys.last_value = value
1281 1274 sys.last_traceback = tb
1282 1275
1283 1276 if etype in self.custom_exceptions:
1284 1277 self.CustomTB(etype,value,tb)
1285 1278 else:
1286 1279 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1287 1280 if self.InteractiveTB.call_pdb and self.has_readline:
1288 1281 # pdb mucks up readline, fix it back
1289 1282 self.set_completer()
1290 1283 except KeyboardInterrupt:
1291 1284 self.write("\nKeyboardInterrupt\n")
1292 1285
1293 1286 def showsyntaxerror(self, filename=None):
1294 1287 """Display the syntax error that just occurred.
1295 1288
1296 1289 This doesn't display a stack trace because there isn't one.
1297 1290
1298 1291 If a filename is given, it is stuffed in the exception instead
1299 1292 of what was there before (because Python's parser always uses
1300 1293 "<string>" when reading from a string).
1301 1294 """
1302 1295 etype, value, last_traceback = sys.exc_info()
1303 1296
1304 1297 # See note about these variables in showtraceback() below
1305 1298 sys.last_type = etype
1306 1299 sys.last_value = value
1307 1300 sys.last_traceback = last_traceback
1308 1301
1309 1302 if filename and etype is SyntaxError:
1310 1303 # Work hard to stuff the correct filename in the exception
1311 1304 try:
1312 1305 msg, (dummy_filename, lineno, offset, line) = value
1313 1306 except:
1314 1307 # Not the format we expect; leave it alone
1315 1308 pass
1316 1309 else:
1317 1310 # Stuff in the right filename
1318 1311 try:
1319 1312 # Assume SyntaxError is a class exception
1320 1313 value = SyntaxError(msg, (filename, lineno, offset, line))
1321 1314 except:
1322 1315 # If that failed, assume SyntaxError is a string
1323 1316 value = msg, (filename, lineno, offset, line)
1324 1317 self.SyntaxTB(etype,value,[])
1325 1318
1326 1319 def edit_syntax_error(self):
1327 1320 """The bottom half of the syntax error handler called in the main loop.
1328 1321
1329 1322 Loop until syntax error is fixed or user cancels.
1330 1323 """
1331 1324
1332 1325 while self.SyntaxTB.last_syntax_error:
1333 1326 # copy and clear last_syntax_error
1334 1327 err = self.SyntaxTB.clear_err_state()
1335 1328 if not self._should_recompile(err):
1336 1329 return
1337 1330 try:
1338 1331 # may set last_syntax_error again if a SyntaxError is raised
1339 1332 self.safe_execfile(err.filename,self.user_ns)
1340 1333 except:
1341 1334 self.showtraceback()
1342 1335 else:
1343 1336 try:
1344 1337 f = file(err.filename)
1345 1338 try:
1346 1339 # This should be inside a display_trap block and I
1347 1340 # think it is.
1348 1341 sys.displayhook(f.read())
1349 1342 finally:
1350 1343 f.close()
1351 1344 except:
1352 1345 self.showtraceback()
1353 1346
1354 1347 def _should_recompile(self,e):
1355 1348 """Utility routine for edit_syntax_error"""
1356 1349
1357 1350 if e.filename in ('<ipython console>','<input>','<string>',
1358 1351 '<console>','<BackgroundJob compilation>',
1359 1352 None):
1360 1353
1361 1354 return False
1362 1355 try:
1363 1356 if (self.autoedit_syntax and
1364 1357 not self.ask_yes_no('Return to editor to correct syntax error? '
1365 1358 '[Y/n] ','y')):
1366 1359 return False
1367 1360 except EOFError:
1368 1361 return False
1369 1362
1370 1363 def int0(x):
1371 1364 try:
1372 1365 return int(x)
1373 1366 except TypeError:
1374 1367 return 0
1375 1368 # always pass integer line and offset values to editor hook
1376 1369 try:
1377 1370 self.hooks.fix_error_editor(e.filename,
1378 1371 int0(e.lineno),int0(e.offset),e.msg)
1379 1372 except TryNext:
1380 1373 warn('Could not open editor')
1381 1374 return False
1382 1375 return True
1383 1376
1384 1377 #-------------------------------------------------------------------------
1385 1378 # Things related to tab completion
1386 1379 #-------------------------------------------------------------------------
1387 1380
1388 1381 def complete(self, text):
1389 1382 """Return a sorted list of all possible completions on text.
1390 1383
1391 1384 Inputs:
1392 1385
1393 1386 - text: a string of text to be completed on.
1394 1387
1395 1388 This is a wrapper around the completion mechanism, similar to what
1396 1389 readline does at the command line when the TAB key is hit. By
1397 1390 exposing it as a method, it can be used by other non-readline
1398 1391 environments (such as GUIs) for text completion.
1399 1392
1400 1393 Simple usage example:
1401 1394
1402 1395 In [7]: x = 'hello'
1403 1396
1404 1397 In [8]: x
1405 1398 Out[8]: 'hello'
1406 1399
1407 1400 In [9]: print x
1408 1401 hello
1409 1402
1410 1403 In [10]: _ip.complete('x.l')
1411 1404 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1412 1405 """
1413 1406
1414 1407 # Inject names into __builtin__ so we can complete on the added names.
1415 1408 with self.builtin_trap:
1416 1409 complete = self.Completer.complete
1417 1410 state = 0
1418 1411 # use a dict so we get unique keys, since ipyhton's multiple
1419 1412 # completers can return duplicates. When we make 2.4 a requirement,
1420 1413 # start using sets instead, which are faster.
1421 1414 comps = {}
1422 1415 while True:
1423 1416 newcomp = complete(text,state,line_buffer=text)
1424 1417 if newcomp is None:
1425 1418 break
1426 1419 comps[newcomp] = 1
1427 1420 state += 1
1428 1421 outcomps = comps.keys()
1429 1422 outcomps.sort()
1430 1423 #print "T:",text,"OC:",outcomps # dbg
1431 1424 #print "vars:",self.user_ns.keys()
1432 1425 return outcomps
1433 1426
1434 1427 def set_custom_completer(self,completer,pos=0):
1435 1428 """set_custom_completer(completer,pos=0)
1436 1429
1437 1430 Adds a new custom completer function.
1438 1431
1439 1432 The position argument (defaults to 0) is the index in the completers
1440 1433 list where you want the completer to be inserted."""
1441 1434
1442 1435 newcomp = new.instancemethod(completer,self.Completer,
1443 1436 self.Completer.__class__)
1444 1437 self.Completer.matchers.insert(pos,newcomp)
1445 1438
1446 1439 def set_completer(self):
1447 1440 """reset readline's completer to be our own."""
1448 1441 self.readline.set_completer(self.Completer.complete)
1449 1442
1450 1443 #-------------------------------------------------------------------------
1451 1444 # Things related to readline
1452 1445 #-------------------------------------------------------------------------
1453 1446
1454 1447 def init_readline(self):
1455 1448 """Command history completion/saving/reloading."""
1456 1449
1457 1450 self.rl_next_input = None
1458 1451 self.rl_do_indent = False
1459 1452
1460 1453 if not self.readline_use:
1461 1454 return
1462 1455
1463 1456 import IPython.utils.rlineimpl as readline
1464 1457
1465 1458 if not readline.have_readline:
1466 1459 self.has_readline = 0
1467 1460 self.readline = None
1468 1461 # no point in bugging windows users with this every time:
1469 1462 warn('Readline services not available on this platform.')
1470 1463 else:
1471 1464 sys.modules['readline'] = readline
1472 1465 import atexit
1473 1466 from IPython.core.completer import IPCompleter
1474 1467 self.Completer = IPCompleter(self,
1475 1468 self.user_ns,
1476 1469 self.user_global_ns,
1477 1470 self.readline_omit__names,
1478 self.alias_table)
1471 self.alias_manager.alias_table)
1479 1472 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1480 1473 self.strdispatchers['complete_command'] = sdisp
1481 1474 self.Completer.custom_completers = sdisp
1482 1475 # Platform-specific configuration
1483 1476 if os.name == 'nt':
1484 1477 self.readline_startup_hook = readline.set_pre_input_hook
1485 1478 else:
1486 1479 self.readline_startup_hook = readline.set_startup_hook
1487 1480
1488 1481 # Load user's initrc file (readline config)
1489 1482 # Or if libedit is used, load editrc.
1490 1483 inputrc_name = os.environ.get('INPUTRC')
1491 1484 if inputrc_name is None:
1492 1485 home_dir = get_home_dir()
1493 1486 if home_dir is not None:
1494 1487 inputrc_name = '.inputrc'
1495 1488 if readline.uses_libedit:
1496 1489 inputrc_name = '.editrc'
1497 1490 inputrc_name = os.path.join(home_dir, inputrc_name)
1498 1491 if os.path.isfile(inputrc_name):
1499 1492 try:
1500 1493 readline.read_init_file(inputrc_name)
1501 1494 except:
1502 1495 warn('Problems reading readline initialization file <%s>'
1503 1496 % inputrc_name)
1504 1497
1505 1498 self.has_readline = 1
1506 1499 self.readline = readline
1507 1500 # save this in sys so embedded copies can restore it properly
1508 1501 sys.ipcompleter = self.Completer.complete
1509 1502 self.set_completer()
1510 1503
1511 1504 # Configure readline according to user's prefs
1512 1505 # This is only done if GNU readline is being used. If libedit
1513 1506 # is being used (as on Leopard) the readline config is
1514 1507 # not run as the syntax for libedit is different.
1515 1508 if not readline.uses_libedit:
1516 1509 for rlcommand in self.readline_parse_and_bind:
1517 1510 #print "loading rl:",rlcommand # dbg
1518 1511 readline.parse_and_bind(rlcommand)
1519 1512
1520 1513 # Remove some chars from the delimiters list. If we encounter
1521 1514 # unicode chars, discard them.
1522 1515 delims = readline.get_completer_delims().encode("ascii", "ignore")
1523 1516 delims = delims.translate(string._idmap,
1524 1517 self.readline_remove_delims)
1525 1518 readline.set_completer_delims(delims)
1526 1519 # otherwise we end up with a monster history after a while:
1527 1520 readline.set_history_length(1000)
1528 1521 try:
1529 1522 #print '*** Reading readline history' # dbg
1530 1523 readline.read_history_file(self.histfile)
1531 1524 except IOError:
1532 1525 pass # It doesn't exist yet.
1533 1526
1534 1527 atexit.register(self.atexit_operations)
1535 1528 del atexit
1536 1529
1537 1530 # Configure auto-indent for all platforms
1538 1531 self.set_autoindent(self.autoindent)
1539 1532
1540 1533 def set_next_input(self, s):
1541 1534 """ Sets the 'default' input string for the next command line.
1542 1535
1543 1536 Requires readline.
1544 1537
1545 1538 Example:
1546 1539
1547 1540 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1548 1541 [D:\ipython]|2> Hello Word_ # cursor is here
1549 1542 """
1550 1543
1551 1544 self.rl_next_input = s
1552 1545
1553 1546 def pre_readline(self):
1554 1547 """readline hook to be used at the start of each line.
1555 1548
1556 1549 Currently it handles auto-indent only."""
1557 1550
1558 1551 #debugx('self.indent_current_nsp','pre_readline:')
1559 1552
1560 1553 if self.rl_do_indent:
1561 1554 self.readline.insert_text(self._indent_current_str())
1562 1555 if self.rl_next_input is not None:
1563 1556 self.readline.insert_text(self.rl_next_input)
1564 1557 self.rl_next_input = None
1565 1558
1566 1559 def _indent_current_str(self):
1567 1560 """return the current level of indentation as a string"""
1568 1561 return self.indent_current_nsp * ' '
1569 1562
1570 1563 #-------------------------------------------------------------------------
1571 1564 # Things related to magics
1572 1565 #-------------------------------------------------------------------------
1573 1566
1574 1567 def init_magics(self):
1575 1568 # Set user colors (don't do it in the constructor above so that it
1576 1569 # doesn't crash if colors option is invalid)
1577 1570 self.magic_colors(self.colors)
1578 1571
1579 1572 def magic(self,arg_s):
1580 1573 """Call a magic function by name.
1581 1574
1582 1575 Input: a string containing the name of the magic function to call and any
1583 1576 additional arguments to be passed to the magic.
1584 1577
1585 1578 magic('name -opt foo bar') is equivalent to typing at the ipython
1586 1579 prompt:
1587 1580
1588 1581 In[1]: %name -opt foo bar
1589 1582
1590 1583 To call a magic without arguments, simply use magic('name').
1591 1584
1592 1585 This provides a proper Python function to call IPython's magics in any
1593 1586 valid Python code you can type at the interpreter, including loops and
1594 1587 compound statements.
1595 1588 """
1596 1589
1597 1590 args = arg_s.split(' ',1)
1598 1591 magic_name = args[0]
1599 1592 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1600 1593
1601 1594 try:
1602 1595 magic_args = args[1]
1603 1596 except IndexError:
1604 1597 magic_args = ''
1605 1598 fn = getattr(self,'magic_'+magic_name,None)
1606 1599 if fn is None:
1607 1600 error("Magic function `%s` not found." % magic_name)
1608 1601 else:
1609 1602 magic_args = self.var_expand(magic_args,1)
1610 with nested(self.builtin_trap, self.display_trap):
1611 return fn(magic_args)
1603 with nested(self.builtin_trap,):
1604 result = fn(magic_args)
1605 # Unfortunately, the return statement is what will trigger
1606 # the displayhook, but it is no longer set!
1607 return result
1612 1608
1613 1609 def define_magic(self, magicname, func):
1614 1610 """Expose own function as magic function for ipython
1615 1611
1616 1612 def foo_impl(self,parameter_s=''):
1617 1613 'My very own magic!. (Use docstrings, IPython reads them).'
1618 1614 print 'Magic function. Passed parameter is between < >:'
1619 1615 print '<%s>' % parameter_s
1620 1616 print 'The self object is:',self
1621 1617
1622 1618 self.define_magic('foo',foo_impl)
1623 1619 """
1624 1620
1625 1621 import new
1626 1622 im = new.instancemethod(func,self, self.__class__)
1627 1623 old = getattr(self, "magic_" + magicname, None)
1628 1624 setattr(self, "magic_" + magicname, im)
1629 1625 return old
1630 1626
1631 1627 #-------------------------------------------------------------------------
1632 1628 # Things related to macros
1633 1629 #-------------------------------------------------------------------------
1634 1630
1635 1631 def define_macro(self, name, themacro):
1636 1632 """Define a new macro
1637 1633
1638 1634 Parameters
1639 1635 ----------
1640 1636 name : str
1641 1637 The name of the macro.
1642 1638 themacro : str or Macro
1643 1639 The action to do upon invoking the macro. If a string, a new
1644 1640 Macro object is created by passing the string to it.
1645 1641 """
1646 1642
1647 1643 from IPython.core import macro
1648 1644
1649 1645 if isinstance(themacro, basestring):
1650 1646 themacro = macro.Macro(themacro)
1651 1647 if not isinstance(themacro, macro.Macro):
1652 1648 raise ValueError('A macro must be a string or a Macro instance.')
1653 1649 self.user_ns[name] = themacro
1654 1650
1655 1651 #-------------------------------------------------------------------------
1656 1652 # Things related to the running of system commands
1657 1653 #-------------------------------------------------------------------------
1658 1654
1659 1655 def system(self, cmd):
1660 1656 """Make a system call, using IPython."""
1661 1657 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1662 1658
1663 1659 #-------------------------------------------------------------------------
1664 1660 # Things related to aliases
1665 1661 #-------------------------------------------------------------------------
1666 1662
1667 1663 def init_alias(self):
1668 1664 self.alias_manager = AliasManager(self, config=self.config)
1665 self.ns_table['alias'] = self.alias_manager.alias_table,
1669 1666
1670 1667 #-------------------------------------------------------------------------
1671 1668 # Things related to the running of code
1672 1669 #-------------------------------------------------------------------------
1673 1670
1674 1671 def ex(self, cmd):
1675 1672 """Execute a normal python statement in user namespace."""
1676 with nested(self.builtin_trap, self.display_trap):
1673 with nested(self.builtin_trap,):
1677 1674 exec cmd in self.user_global_ns, self.user_ns
1678 1675
1679 1676 def ev(self, expr):
1680 1677 """Evaluate python expression expr in user namespace.
1681 1678
1682 1679 Returns the result of evaluation
1683 1680 """
1684 with nested(self.builtin_trap, self.display_trap):
1681 with nested(self.builtin_trap,):
1685 1682 return eval(expr, self.user_global_ns, self.user_ns)
1686 1683
1687 1684 def mainloop(self, banner=None):
1688 1685 """Start the mainloop.
1689 1686
1690 1687 If an optional banner argument is given, it will override the
1691 1688 internally created default banner.
1692 1689 """
1693 1690
1694 1691 with nested(self.builtin_trap, self.display_trap):
1695 1692 if self.c: # Emulate Python's -c option
1696 1693 self.exec_init_cmd()
1697 1694
1698 1695 if self.display_banner:
1699 1696 if banner is None:
1700 1697 banner = self.banner
1701 1698
1702 1699 # if you run stuff with -c <cmd>, raw hist is not updated
1703 1700 # ensure that it's in sync
1704 1701 if len(self.input_hist) != len (self.input_hist_raw):
1705 1702 self.input_hist_raw = InputList(self.input_hist)
1706 1703
1707 1704 while 1:
1708 1705 try:
1709 1706 self.interact()
1710 1707 #self.interact_with_readline()
1711 1708 # XXX for testing of a readline-decoupled repl loop, call
1712 1709 # interact_with_readline above
1713 1710 break
1714 1711 except KeyboardInterrupt:
1715 1712 # this should not be necessary, but KeyboardInterrupt
1716 1713 # handling seems rather unpredictable...
1717 1714 self.write("\nKeyboardInterrupt in interact()\n")
1718 1715
1719 1716 def exec_init_cmd(self):
1720 1717 """Execute a command given at the command line.
1721 1718
1722 1719 This emulates Python's -c option."""
1723 1720
1724 1721 #sys.argv = ['-c']
1725 1722 self.push_line(self.prefilter_manager.prefilter_lines(self.c, False))
1726 1723 if not self.interactive:
1727 1724 self.ask_exit()
1728 1725
1729 1726 def interact_prompt(self):
1730 1727 """ Print the prompt (in read-eval-print loop)
1731 1728
1732 1729 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1733 1730 used in standard IPython flow.
1734 1731 """
1735 1732 if self.more:
1736 1733 try:
1737 1734 prompt = self.hooks.generate_prompt(True)
1738 1735 except:
1739 1736 self.showtraceback()
1740 1737 if self.autoindent:
1741 1738 self.rl_do_indent = True
1742 1739
1743 1740 else:
1744 1741 try:
1745 1742 prompt = self.hooks.generate_prompt(False)
1746 1743 except:
1747 1744 self.showtraceback()
1748 1745 self.write(prompt)
1749 1746
1750 1747 def interact_handle_input(self,line):
1751 1748 """ Handle the input line (in read-eval-print loop)
1752 1749
1753 1750 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1754 1751 used in standard IPython flow.
1755 1752 """
1756 1753 if line.lstrip() == line:
1757 1754 self.shadowhist.add(line.strip())
1758 1755 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1759 1756
1760 1757 if line.strip():
1761 1758 if self.more:
1762 1759 self.input_hist_raw[-1] += '%s\n' % line
1763 1760 else:
1764 1761 self.input_hist_raw.append('%s\n' % line)
1765 1762
1766 1763
1767 1764 self.more = self.push_line(lineout)
1768 1765 if (self.SyntaxTB.last_syntax_error and
1769 1766 self.autoedit_syntax):
1770 1767 self.edit_syntax_error()
1771 1768
1772 1769 def interact_with_readline(self):
1773 1770 """ Demo of using interact_handle_input, interact_prompt
1774 1771
1775 1772 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1776 1773 it should work like this.
1777 1774 """
1778 1775 self.readline_startup_hook(self.pre_readline)
1779 1776 while not self.exit_now:
1780 1777 self.interact_prompt()
1781 1778 if self.more:
1782 1779 self.rl_do_indent = True
1783 1780 else:
1784 1781 self.rl_do_indent = False
1785 1782 line = raw_input_original().decode(self.stdin_encoding)
1786 1783 self.interact_handle_input(line)
1787 1784
1788 1785 def interact(self, banner=None):
1789 1786 """Closely emulate the interactive Python console."""
1790 1787
1791 1788 # batch run -> do not interact
1792 1789 if self.exit_now:
1793 1790 return
1794 1791
1795 1792 if self.display_banner:
1796 1793 if banner is None:
1797 1794 banner = self.banner
1798 1795 self.write(banner)
1799 1796
1800 1797 more = 0
1801 1798
1802 1799 # Mark activity in the builtins
1803 1800 __builtin__.__dict__['__IPYTHON__active'] += 1
1804 1801
1805 1802 if self.has_readline:
1806 1803 self.readline_startup_hook(self.pre_readline)
1807 1804 # exit_now is set by a call to %Exit or %Quit, through the
1808 1805 # ask_exit callback.
1809 1806
1810 1807 while not self.exit_now:
1811 1808 self.hooks.pre_prompt_hook()
1812 1809 if more:
1813 1810 try:
1814 1811 prompt = self.hooks.generate_prompt(True)
1815 1812 except:
1816 1813 self.showtraceback()
1817 1814 if self.autoindent:
1818 1815 self.rl_do_indent = True
1819 1816
1820 1817 else:
1821 1818 try:
1822 1819 prompt = self.hooks.generate_prompt(False)
1823 1820 except:
1824 1821 self.showtraceback()
1825 1822 try:
1826 1823 line = self.raw_input(prompt, more)
1827 1824 if self.exit_now:
1828 1825 # quick exit on sys.std[in|out] close
1829 1826 break
1830 1827 if self.autoindent:
1831 1828 self.rl_do_indent = False
1832 1829
1833 1830 except KeyboardInterrupt:
1834 1831 #double-guard against keyboardinterrupts during kbdint handling
1835 1832 try:
1836 1833 self.write('\nKeyboardInterrupt\n')
1837 1834 self.resetbuffer()
1838 1835 # keep cache in sync with the prompt counter:
1839 1836 self.outputcache.prompt_count -= 1
1840 1837
1841 1838 if self.autoindent:
1842 1839 self.indent_current_nsp = 0
1843 1840 more = 0
1844 1841 except KeyboardInterrupt:
1845 1842 pass
1846 1843 except EOFError:
1847 1844 if self.autoindent:
1848 1845 self.rl_do_indent = False
1849 1846 self.readline_startup_hook(None)
1850 1847 self.write('\n')
1851 1848 self.exit()
1852 1849 except bdb.BdbQuit:
1853 1850 warn('The Python debugger has exited with a BdbQuit exception.\n'
1854 1851 'Because of how pdb handles the stack, it is impossible\n'
1855 1852 'for IPython to properly format this particular exception.\n'
1856 1853 'IPython will resume normal operation.')
1857 1854 except:
1858 1855 # exceptions here are VERY RARE, but they can be triggered
1859 1856 # asynchronously by signal handlers, for example.
1860 1857 self.showtraceback()
1861 1858 else:
1862 1859 more = self.push_line(line)
1863 1860 if (self.SyntaxTB.last_syntax_error and
1864 1861 self.autoedit_syntax):
1865 1862 self.edit_syntax_error()
1866 1863
1867 1864 # We are off again...
1868 1865 __builtin__.__dict__['__IPYTHON__active'] -= 1
1869 1866
1870 1867 def safe_execfile(self,fname,*where,**kw):
1871 1868 """A safe version of the builtin execfile().
1872 1869
1873 1870 This version will never throw an exception, and knows how to handle
1874 1871 ipython logs as well.
1875 1872
1876 1873 :Parameters:
1877 1874 fname : string
1878 1875 Name of the file to be executed.
1879 1876
1880 1877 where : tuple
1881 1878 One or two namespaces, passed to execfile() as (globals,locals).
1882 1879 If only one is given, it is passed as both.
1883 1880
1884 1881 :Keywords:
1885 1882 islog : boolean (False)
1886 1883
1887 1884 quiet : boolean (True)
1888 1885
1889 1886 exit_ignore : boolean (False)
1890 1887 """
1891 1888
1892 1889 def syspath_cleanup():
1893 1890 """Internal cleanup routine for sys.path."""
1894 1891 if add_dname:
1895 1892 try:
1896 1893 sys.path.remove(dname)
1897 1894 except ValueError:
1898 1895 # For some reason the user has already removed it, ignore.
1899 1896 pass
1900 1897
1901 1898 fname = os.path.expanduser(fname)
1902 1899
1903 1900 # Find things also in current directory. This is needed to mimic the
1904 1901 # behavior of running a script from the system command line, where
1905 1902 # Python inserts the script's directory into sys.path
1906 1903 dname = os.path.dirname(os.path.abspath(fname))
1907 1904 add_dname = False
1908 1905 if dname not in sys.path:
1909 1906 sys.path.insert(0,dname)
1910 1907 add_dname = True
1911 1908
1912 1909 try:
1913 1910 xfile = open(fname)
1914 1911 except:
1915 1912 print >> Term.cerr, \
1916 1913 'Could not open file <%s> for safe execution.' % fname
1917 1914 syspath_cleanup()
1918 1915 return None
1919 1916
1920 1917 kw.setdefault('islog',0)
1921 1918 kw.setdefault('quiet',1)
1922 1919 kw.setdefault('exit_ignore',0)
1923 1920
1924 1921 first = xfile.readline()
1925 1922 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
1926 1923 xfile.close()
1927 1924 # line by line execution
1928 1925 if first.startswith(loghead) or kw['islog']:
1929 1926 print 'Loading log file <%s> one line at a time...' % fname
1930 1927 if kw['quiet']:
1931 1928 stdout_save = sys.stdout
1932 1929 sys.stdout = StringIO.StringIO()
1933 1930 try:
1934 1931 globs,locs = where[0:2]
1935 1932 except:
1936 1933 try:
1937 1934 globs = locs = where[0]
1938 1935 except:
1939 1936 globs = locs = globals()
1940 1937 badblocks = []
1941 1938
1942 1939 # we also need to identify indented blocks of code when replaying
1943 1940 # logs and put them together before passing them to an exec
1944 1941 # statement. This takes a bit of regexp and look-ahead work in the
1945 1942 # file. It's easiest if we swallow the whole thing in memory
1946 1943 # first, and manually walk through the lines list moving the
1947 1944 # counter ourselves.
1948 1945 indent_re = re.compile('\s+\S')
1949 1946 xfile = open(fname)
1950 1947 filelines = xfile.readlines()
1951 1948 xfile.close()
1952 1949 nlines = len(filelines)
1953 1950 lnum = 0
1954 1951 while lnum < nlines:
1955 1952 line = filelines[lnum]
1956 1953 lnum += 1
1957 1954 # don't re-insert logger status info into cache
1958 1955 if line.startswith('#log#'):
1959 1956 continue
1960 1957 else:
1961 1958 # build a block of code (maybe a single line) for execution
1962 1959 block = line
1963 1960 try:
1964 1961 next = filelines[lnum] # lnum has already incremented
1965 1962 except:
1966 1963 next = None
1967 1964 while next and indent_re.match(next):
1968 1965 block += next
1969 1966 lnum += 1
1970 1967 try:
1971 1968 next = filelines[lnum]
1972 1969 except:
1973 1970 next = None
1974 1971 # now execute the block of one or more lines
1975 1972 try:
1976 1973 exec block in globs,locs
1977 1974 except SystemExit:
1978 1975 pass
1979 1976 except:
1980 1977 badblocks.append(block.rstrip())
1981 1978 if kw['quiet']: # restore stdout
1982 1979 sys.stdout.close()
1983 1980 sys.stdout = stdout_save
1984 1981 print 'Finished replaying log file <%s>' % fname
1985 1982 if badblocks:
1986 1983 print >> sys.stderr, ('\nThe following lines/blocks in file '
1987 1984 '<%s> reported errors:' % fname)
1988 1985
1989 1986 for badline in badblocks:
1990 1987 print >> sys.stderr, badline
1991 1988 else: # regular file execution
1992 1989 try:
1993 1990 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1994 1991 # Work around a bug in Python for Windows. The bug was
1995 1992 # fixed in in Python 2.5 r54159 and 54158, but that's still
1996 1993 # SVN Python as of March/07. For details, see:
1997 1994 # http://projects.scipy.org/ipython/ipython/ticket/123
1998 1995 try:
1999 1996 globs,locs = where[0:2]
2000 1997 except:
2001 1998 try:
2002 1999 globs = locs = where[0]
2003 2000 except:
2004 2001 globs = locs = globals()
2005 2002 exec file(fname) in globs,locs
2006 2003 else:
2007 2004 execfile(fname,*where)
2008 2005 except SyntaxError:
2009 2006 self.showsyntaxerror()
2010 2007 warn('Failure executing file: <%s>' % fname)
2011 2008 except SystemExit,status:
2012 2009 # Code that correctly sets the exit status flag to success (0)
2013 2010 # shouldn't be bothered with a traceback. Note that a plain
2014 2011 # sys.exit() does NOT set the message to 0 (it's empty) so that
2015 2012 # will still get a traceback. Note that the structure of the
2016 2013 # SystemExit exception changed between Python 2.4 and 2.5, so
2017 2014 # the checks must be done in a version-dependent way.
2018 2015 show = False
2019 2016
2020 2017 if sys.version_info[:2] > (2,5):
2021 2018 if status.message!=0 and not kw['exit_ignore']:
2022 2019 show = True
2023 2020 else:
2024 2021 if status.code and not kw['exit_ignore']:
2025 2022 show = True
2026 2023 if show:
2027 2024 self.showtraceback()
2028 2025 warn('Failure executing file: <%s>' % fname)
2029 2026 except:
2030 2027 self.showtraceback()
2031 2028 warn('Failure executing file: <%s>' % fname)
2032 2029
2033 2030 syspath_cleanup()
2034 2031
2035 2032 def cleanup_ipy_script(self, script):
2036 2033 """Make a script safe for self.runlines()
2037 2034
2038 2035 Notes
2039 2036 -----
2040 2037 This was copied over from the old ipapi and probably can be done
2041 2038 away with once we move to block based interpreter.
2042 2039
2043 2040 - Removes empty lines Suffixes all indented blocks that end with
2044 2041 - unindented lines with empty lines
2045 2042 """
2046 2043
2047 2044 res = []
2048 2045 lines = script.splitlines()
2049 2046
2050 2047 level = 0
2051 2048 for l in lines:
2052 2049 lstripped = l.lstrip()
2053 2050 stripped = l.strip()
2054 2051 if not stripped:
2055 2052 continue
2056 2053 newlevel = len(l) - len(lstripped)
2057 2054 def is_secondary_block_start(s):
2058 2055 if not s.endswith(':'):
2059 2056 return False
2060 2057 if (s.startswith('elif') or
2061 2058 s.startswith('else') or
2062 2059 s.startswith('except') or
2063 2060 s.startswith('finally')):
2064 2061 return True
2065 2062
2066 2063 if level > 0 and newlevel == 0 and \
2067 2064 not is_secondary_block_start(stripped):
2068 2065 # add empty line
2069 2066 res.append('')
2070 2067
2071 2068 res.append(l)
2072 2069 level = newlevel
2073 2070 return '\n'.join(res) + '\n'
2074 2071
2075 2072 def runlines(self, lines, clean=False):
2076 2073 """Run a string of one or more lines of source.
2077 2074
2078 2075 This method is capable of running a string containing multiple source
2079 2076 lines, as if they had been entered at the IPython prompt. Since it
2080 2077 exposes IPython's processing machinery, the given strings can contain
2081 2078 magic calls (%magic), special shell access (!cmd), etc.
2082 2079 """
2083 2080
2084 2081 if isinstance(lines, (list, tuple)):
2085 2082 lines = '\n'.join(lines)
2086 2083
2087 2084 if clean:
2088 2085 lines = self.cleanup_ipy_script(lines)
2089 2086
2090 2087 # We must start with a clean buffer, in case this is run from an
2091 2088 # interactive IPython session (via a magic, for example).
2092 2089 self.resetbuffer()
2093 2090 lines = lines.splitlines()
2094 2091 more = 0
2095 2092
2096 2093 with nested(self.builtin_trap, self.display_trap):
2097 2094 for line in lines:
2098 2095 # skip blank lines so we don't mess up the prompt counter, but do
2099 2096 # NOT skip even a blank line if we are in a code block (more is
2100 2097 # true)
2101 2098
2102 2099 if line or more:
2103 2100 # push to raw history, so hist line numbers stay in sync
2104 2101 self.input_hist_raw.append("# " + line + "\n")
2105 2102 more = self.push_line(self.prefilter_manager.prefilter_lines(line,more))
2106 2103 # IPython's runsource returns None if there was an error
2107 2104 # compiling the code. This allows us to stop processing right
2108 2105 # away, so the user gets the error message at the right place.
2109 2106 if more is None:
2110 2107 break
2111 2108 else:
2112 2109 self.input_hist_raw.append("\n")
2113 2110 # final newline in case the input didn't have it, so that the code
2114 2111 # actually does get executed
2115 2112 if more:
2116 2113 self.push_line('\n')
2117 2114
2118 2115 def runsource(self, source, filename='<input>', symbol='single'):
2119 2116 """Compile and run some source in the interpreter.
2120 2117
2121 2118 Arguments are as for compile_command().
2122 2119
2123 2120 One several things can happen:
2124 2121
2125 2122 1) The input is incorrect; compile_command() raised an
2126 2123 exception (SyntaxError or OverflowError). A syntax traceback
2127 2124 will be printed by calling the showsyntaxerror() method.
2128 2125
2129 2126 2) The input is incomplete, and more input is required;
2130 2127 compile_command() returned None. Nothing happens.
2131 2128
2132 2129 3) The input is complete; compile_command() returned a code
2133 2130 object. The code is executed by calling self.runcode() (which
2134 2131 also handles run-time exceptions, except for SystemExit).
2135 2132
2136 2133 The return value is:
2137 2134
2138 2135 - True in case 2
2139 2136
2140 2137 - False in the other cases, unless an exception is raised, where
2141 2138 None is returned instead. This can be used by external callers to
2142 2139 know whether to continue feeding input or not.
2143 2140
2144 2141 The return value can be used to decide whether to use sys.ps1 or
2145 2142 sys.ps2 to prompt the next line."""
2146 2143
2147 2144 # if the source code has leading blanks, add 'if 1:\n' to it
2148 2145 # this allows execution of indented pasted code. It is tempting
2149 2146 # to add '\n' at the end of source to run commands like ' a=1'
2150 2147 # directly, but this fails for more complicated scenarios
2151 2148 source=source.encode(self.stdin_encoding)
2152 2149 if source[:1] in [' ', '\t']:
2153 2150 source = 'if 1:\n%s' % source
2154 2151
2155 2152 try:
2156 2153 code = self.compile(source,filename,symbol)
2157 2154 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2158 2155 # Case 1
2159 2156 self.showsyntaxerror(filename)
2160 2157 return None
2161 2158
2162 2159 if code is None:
2163 2160 # Case 2
2164 2161 return True
2165 2162
2166 2163 # Case 3
2167 2164 # We store the code object so that threaded shells and
2168 2165 # custom exception handlers can access all this info if needed.
2169 2166 # The source corresponding to this can be obtained from the
2170 2167 # buffer attribute as '\n'.join(self.buffer).
2171 2168 self.code_to_run = code
2172 2169 # now actually execute the code object
2173 2170 if self.runcode(code) == 0:
2174 2171 return False
2175 2172 else:
2176 2173 return None
2177 2174
2178 2175 def runcode(self,code_obj):
2179 2176 """Execute a code object.
2180 2177
2181 2178 When an exception occurs, self.showtraceback() is called to display a
2182 2179 traceback.
2183 2180
2184 2181 Return value: a flag indicating whether the code to be run completed
2185 2182 successfully:
2186 2183
2187 2184 - 0: successful execution.
2188 2185 - 1: an error occurred.
2189 2186 """
2190 2187
2191 2188 # Set our own excepthook in case the user code tries to call it
2192 2189 # directly, so that the IPython crash handler doesn't get triggered
2193 2190 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2194 2191
2195 2192 # we save the original sys.excepthook in the instance, in case config
2196 2193 # code (such as magics) needs access to it.
2197 2194 self.sys_excepthook = old_excepthook
2198 2195 outflag = 1 # happens in more places, so it's easier as default
2199 2196 try:
2200 2197 try:
2201 2198 self.hooks.pre_runcode_hook()
2202 2199 exec code_obj in self.user_global_ns, self.user_ns
2203 2200 finally:
2204 2201 # Reset our crash handler in place
2205 2202 sys.excepthook = old_excepthook
2206 2203 except SystemExit:
2207 2204 self.resetbuffer()
2208 2205 self.showtraceback()
2209 2206 warn("Type %exit or %quit to exit IPython "
2210 2207 "(%Exit or %Quit do so unconditionally).",level=1)
2211 2208 except self.custom_exceptions:
2212 2209 etype,value,tb = sys.exc_info()
2213 2210 self.CustomTB(etype,value,tb)
2214 2211 except:
2215 2212 self.showtraceback()
2216 2213 else:
2217 2214 outflag = 0
2218 2215 if softspace(sys.stdout, 0):
2219 2216 print
2220 2217 # Flush out code object which has been run (and source)
2221 2218 self.code_to_run = None
2222 2219 return outflag
2223 2220
2224 2221 def push_line(self, line):
2225 2222 """Push a line to the interpreter.
2226 2223
2227 2224 The line should not have a trailing newline; it may have
2228 2225 internal newlines. The line is appended to a buffer and the
2229 2226 interpreter's runsource() method is called with the
2230 2227 concatenated contents of the buffer as source. If this
2231 2228 indicates that the command was executed or invalid, the buffer
2232 2229 is reset; otherwise, the command is incomplete, and the buffer
2233 2230 is left as it was after the line was appended. The return
2234 2231 value is 1 if more input is required, 0 if the line was dealt
2235 2232 with in some way (this is the same as runsource()).
2236 2233 """
2237 2234
2238 2235 # autoindent management should be done here, and not in the
2239 2236 # interactive loop, since that one is only seen by keyboard input. We
2240 2237 # need this done correctly even for code run via runlines (which uses
2241 2238 # push).
2242 2239
2243 2240 #print 'push line: <%s>' % line # dbg
2244 2241 for subline in line.splitlines():
2245 2242 self._autoindent_update(subline)
2246 2243 self.buffer.append(line)
2247 2244 more = self.runsource('\n'.join(self.buffer), self.filename)
2248 2245 if not more:
2249 2246 self.resetbuffer()
2250 2247 return more
2251 2248
2252 2249 def _autoindent_update(self,line):
2253 2250 """Keep track of the indent level."""
2254 2251
2255 2252 #debugx('line')
2256 2253 #debugx('self.indent_current_nsp')
2257 2254 if self.autoindent:
2258 2255 if line:
2259 2256 inisp = num_ini_spaces(line)
2260 2257 if inisp < self.indent_current_nsp:
2261 2258 self.indent_current_nsp = inisp
2262 2259
2263 2260 if line[-1] == ':':
2264 2261 self.indent_current_nsp += 4
2265 2262 elif dedent_re.match(line):
2266 2263 self.indent_current_nsp -= 4
2267 2264 else:
2268 2265 self.indent_current_nsp = 0
2269 2266
2270 2267 def resetbuffer(self):
2271 2268 """Reset the input buffer."""
2272 2269 self.buffer[:] = []
2273 2270
2274 2271 def raw_input(self,prompt='',continue_prompt=False):
2275 2272 """Write a prompt and read a line.
2276 2273
2277 2274 The returned line does not include the trailing newline.
2278 2275 When the user enters the EOF key sequence, EOFError is raised.
2279 2276
2280 2277 Optional inputs:
2281 2278
2282 2279 - prompt(''): a string to be printed to prompt the user.
2283 2280
2284 2281 - continue_prompt(False): whether this line is the first one or a
2285 2282 continuation in a sequence of inputs.
2286 2283 """
2287 2284 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2288 2285
2289 2286 # Code run by the user may have modified the readline completer state.
2290 2287 # We must ensure that our completer is back in place.
2291 2288
2292 2289 if self.has_readline:
2293 2290 self.set_completer()
2294 2291
2295 2292 try:
2296 2293 line = raw_input_original(prompt).decode(self.stdin_encoding)
2297 2294 except ValueError:
2298 2295 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2299 2296 " or sys.stdout.close()!\nExiting IPython!")
2300 2297 self.ask_exit()
2301 2298 return ""
2302 2299
2303 2300 # Try to be reasonably smart about not re-indenting pasted input more
2304 2301 # than necessary. We do this by trimming out the auto-indent initial
2305 2302 # spaces, if the user's actual input started itself with whitespace.
2306 2303 #debugx('self.buffer[-1]')
2307 2304
2308 2305 if self.autoindent:
2309 2306 if num_ini_spaces(line) > self.indent_current_nsp:
2310 2307 line = line[self.indent_current_nsp:]
2311 2308 self.indent_current_nsp = 0
2312 2309
2313 2310 # store the unfiltered input before the user has any chance to modify
2314 2311 # it.
2315 2312 if line.strip():
2316 2313 if continue_prompt:
2317 2314 self.input_hist_raw[-1] += '%s\n' % line
2318 2315 if self.has_readline: # and some config option is set?
2319 2316 try:
2320 2317 histlen = self.readline.get_current_history_length()
2321 2318 if histlen > 1:
2322 2319 newhist = self.input_hist_raw[-1].rstrip()
2323 2320 self.readline.remove_history_item(histlen-1)
2324 2321 self.readline.replace_history_item(histlen-2,
2325 2322 newhist.encode(self.stdin_encoding))
2326 2323 except AttributeError:
2327 2324 pass # re{move,place}_history_item are new in 2.4.
2328 2325 else:
2329 2326 self.input_hist_raw.append('%s\n' % line)
2330 2327 # only entries starting at first column go to shadow history
2331 2328 if line.lstrip() == line:
2332 2329 self.shadowhist.add(line.strip())
2333 2330 elif not continue_prompt:
2334 2331 self.input_hist_raw.append('\n')
2335 2332 try:
2336 2333 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2337 2334 except:
2338 2335 # blanket except, in case a user-defined prefilter crashes, so it
2339 2336 # can't take all of ipython with it.
2340 2337 self.showtraceback()
2341 2338 return ''
2342 2339 else:
2343 2340 return lineout
2344 2341
2345 2342 # def init_exec_commands(self):
2346 2343 # for cmd in self.config.EXECUTE:
2347 2344 # print "execute:", cmd
2348 2345 # self.api.runlines(cmd)
2349 2346 #
2350 2347 # batchrun = False
2351 2348 # if self.config.has_key('EXECFILE'):
2352 2349 # for batchfile in [path(arg) for arg in self.config.EXECFILE
2353 2350 # if arg.lower().endswith('.ipy')]:
2354 2351 # if not batchfile.isfile():
2355 2352 # print "No such batch file:", batchfile
2356 2353 # continue
2357 2354 # self.api.runlines(batchfile.text())
2358 2355 # batchrun = True
2359 2356 # # without -i option, exit after running the batch file
2360 2357 # if batchrun and not self.interactive:
2361 2358 # self.ask_exit()
2362 2359
2363 2360 # def load(self, mod):
2364 2361 # """ Load an extension.
2365 2362 #
2366 2363 # Some modules should (or must) be 'load()':ed, rather than just imported.
2367 2364 #
2368 2365 # Loading will do:
2369 2366 #
2370 2367 # - run init_ipython(ip)
2371 2368 # - run ipython_firstrun(ip)
2372 2369 # """
2373 2370 #
2374 2371 # if mod in self.extensions:
2375 2372 # # just to make sure we don't init it twice
2376 2373 # # note that if you 'load' a module that has already been
2377 2374 # # imported, init_ipython gets run anyway
2378 2375 #
2379 2376 # return self.extensions[mod]
2380 2377 # __import__(mod)
2381 2378 # m = sys.modules[mod]
2382 2379 # if hasattr(m,'init_ipython'):
2383 2380 # m.init_ipython(self)
2384 2381 #
2385 2382 # if hasattr(m,'ipython_firstrun'):
2386 2383 # already_loaded = self.db.get('firstrun_done', set())
2387 2384 # if mod not in already_loaded:
2388 2385 # m.ipython_firstrun(self)
2389 2386 # already_loaded.add(mod)
2390 2387 # self.db['firstrun_done'] = already_loaded
2391 2388 #
2392 2389 # self.extensions[mod] = m
2393 2390 # return m
2394 2391
2395 2392 #-------------------------------------------------------------------------
2396 2393 # Things related to the prefilter
2397 2394 #-------------------------------------------------------------------------
2398 2395
2399 2396 def init_prefilter(self):
2400 2397 self.prefilter_manager = PrefilterManager(self, config=self.config)
2401 2398
2402 2399 #-------------------------------------------------------------------------
2403 2400 # Utilities
2404 2401 #-------------------------------------------------------------------------
2405 2402
2406 2403 def getoutput(self, cmd):
2407 2404 return getoutput(self.var_expand(cmd,depth=2),
2408 2405 header=self.system_header,
2409 2406 verbose=self.system_verbose)
2410 2407
2411 2408 def getoutputerror(self, cmd):
2412 2409 return getoutputerror(self.var_expand(cmd,depth=2),
2413 2410 header=self.system_header,
2414 2411 verbose=self.system_verbose)
2415 2412
2416 2413 def var_expand(self,cmd,depth=0):
2417 2414 """Expand python variables in a string.
2418 2415
2419 2416 The depth argument indicates how many frames above the caller should
2420 2417 be walked to look for the local namespace where to expand variables.
2421 2418
2422 2419 The global namespace for expansion is always the user's interactive
2423 2420 namespace.
2424 2421 """
2425 2422
2426 2423 return str(ItplNS(cmd,
2427 2424 self.user_ns, # globals
2428 2425 # Skip our own frame in searching for locals:
2429 2426 sys._getframe(depth+1).f_locals # locals
2430 2427 ))
2431 2428
2432 2429 def mktempfile(self,data=None):
2433 2430 """Make a new tempfile and return its filename.
2434 2431
2435 2432 This makes a call to tempfile.mktemp, but it registers the created
2436 2433 filename internally so ipython cleans it up at exit time.
2437 2434
2438 2435 Optional inputs:
2439 2436
2440 2437 - data(None): if data is given, it gets written out to the temp file
2441 2438 immediately, and the file is closed again."""
2442 2439
2443 2440 filename = tempfile.mktemp('.py','ipython_edit_')
2444 2441 self.tempfiles.append(filename)
2445 2442
2446 2443 if data:
2447 2444 tmp_file = open(filename,'w')
2448 2445 tmp_file.write(data)
2449 2446 tmp_file.close()
2450 2447 return filename
2451 2448
2452 2449 def write(self,data):
2453 2450 """Write a string to the default output"""
2454 2451 Term.cout.write(data)
2455 2452
2456 2453 def write_err(self,data):
2457 2454 """Write a string to the default error output"""
2458 2455 Term.cerr.write(data)
2459 2456
2460 2457 def ask_yes_no(self,prompt,default=True):
2461 2458 if self.quiet:
2462 2459 return True
2463 2460 return ask_yes_no(prompt,default)
2464 2461
2465 2462 #-------------------------------------------------------------------------
2466 2463 # Things related to IPython exiting
2467 2464 #-------------------------------------------------------------------------
2468 2465
2469 2466 def ask_exit(self):
2470 2467 """ Call for exiting. Can be overiden and used as a callback. """
2471 2468 self.exit_now = True
2472 2469
2473 2470 def exit(self):
2474 2471 """Handle interactive exit.
2475 2472
2476 2473 This method calls the ask_exit callback."""
2477 2474 if self.confirm_exit:
2478 2475 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2479 2476 self.ask_exit()
2480 2477 else:
2481 2478 self.ask_exit()
2482 2479
2483 2480 def atexit_operations(self):
2484 2481 """This will be executed at the time of exit.
2485 2482
2486 2483 Saving of persistent data should be performed here.
2487 2484 """
2488 2485 self.savehist()
2489 2486
2490 2487 # Cleanup all tempfiles left around
2491 2488 for tfile in self.tempfiles:
2492 2489 try:
2493 2490 os.unlink(tfile)
2494 2491 except OSError:
2495 2492 pass
2496 2493
2497 2494 # Clear all user namespaces to release all references cleanly.
2498 2495 self.reset()
2499 2496
2500 2497 # Run user hooks
2501 2498 self.hooks.shutdown_hook()
2502 2499
2503 2500 def cleanup(self):
2504 2501 self.restore_sys_module_state()
2505 2502
2506 2503
@@ -1,3585 +1,3563 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #*****************************************************************************
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #*****************************************************************************
12 12
13 13 #****************************************************************************
14 14 # Modules and globals
15 15
16 16 # Python standard modules
17 17 import __builtin__
18 18 import bdb
19 19 import inspect
20 20 import os
21 21 import pdb
22 22 import pydoc
23 23 import sys
24 24 import re
25 25 import tempfile
26 26 import time
27 27 import cPickle as pickle
28 28 import textwrap
29 29 from cStringIO import StringIO
30 30 from getopt import getopt,GetoptError
31 31 from pprint import pprint, pformat
32 32
33 33 # cProfile was added in Python2.5
34 34 try:
35 35 import cProfile as profile
36 36 import pstats
37 37 except ImportError:
38 38 # profile isn't bundled by default in Debian for license reasons
39 39 try:
40 40 import profile,pstats
41 41 except ImportError:
42 42 profile = pstats = None
43 43
44 44 # Homebrewed
45 45 import IPython
46 46 from IPython.utils import wildcard
47 47 from IPython.core import debugger, oinspect
48 48 from IPython.core.error import TryNext
49 49 from IPython.core.fakemodule import FakeModule
50 from IPython.core.prefilter import ESC_MAGIC
50 51 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
51 52 from IPython.utils.PyColorize import Parser
52 53 from IPython.utils.ipstruct import Struct
53 54 from IPython.core.macro import Macro
54 55 from IPython.utils.genutils import *
55 56 from IPython.core.page import page
56 57 from IPython.utils import platutils
57 58 import IPython.utils.generics
58 59 from IPython.core.error import UsageError
59 60 from IPython.testing import decorators as testdec
60 61
61 62 #***************************************************************************
62 63 # Utility functions
63 64 def on_off(tag):
64 65 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
65 66 return ['OFF','ON'][tag]
66 67
67 68 class Bunch: pass
68 69
69 70 def compress_dhist(dh):
70 71 head, tail = dh[:-10], dh[-10:]
71 72
72 73 newhead = []
73 74 done = set()
74 75 for h in head:
75 76 if h in done:
76 77 continue
77 78 newhead.append(h)
78 79 done.add(h)
79 80
80 81 return newhead + tail
81 82
82 83
83 84 #***************************************************************************
84 85 # Main class implementing Magic functionality
85 86 class Magic:
86 87 """Magic functions for InteractiveShell.
87 88
88 89 Shell functions which can be reached as %function_name. All magic
89 90 functions should accept a string, which they can parse for their own
90 91 needs. This can make some functions easier to type, eg `%cd ../`
91 92 vs. `%cd("../")`
92 93
93 94 ALL definitions MUST begin with the prefix magic_. The user won't need it
94 95 at the command line, but it is is needed in the definition. """
95 96
96 97 # class globals
97 98 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
98 99 'Automagic is ON, % prefix NOT needed for magic functions.']
99 100
100 101 #......................................................................
101 102 # some utility functions
102 103
103 104 def __init__(self,shell):
104 105
105 106 self.options_table = {}
106 107 if profile is None:
107 108 self.magic_prun = self.profile_missing_notice
108 109 self.shell = shell
109 110
110 111 # namespace for holding state we may need
111 112 self._magic_state = Bunch()
112 113
113 114 def profile_missing_notice(self, *args, **kwargs):
114 115 error("""\
115 116 The profile module could not be found. It has been removed from the standard
116 117 python packages because of its non-free license. To use profiling, install the
117 118 python-profiler package from non-free.""")
118 119
119 120 def default_option(self,fn,optstr):
120 121 """Make an entry in the options_table for fn, with value optstr"""
121 122
122 123 if fn not in self.lsmagic():
123 124 error("%s is not a magic function" % fn)
124 125 self.options_table[fn] = optstr
125 126
126 127 def lsmagic(self):
127 128 """Return a list of currently available magic functions.
128 129
129 130 Gives a list of the bare names after mangling (['ls','cd', ...], not
130 131 ['magic_ls','magic_cd',...]"""
131 132
132 133 # FIXME. This needs a cleanup, in the way the magics list is built.
133 134
134 135 # magics in class definition
135 136 class_magic = lambda fn: fn.startswith('magic_') and \
136 137 callable(Magic.__dict__[fn])
137 138 # in instance namespace (run-time user additions)
138 139 inst_magic = lambda fn: fn.startswith('magic_') and \
139 140 callable(self.__dict__[fn])
140 141 # and bound magics by user (so they can access self):
141 142 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
142 143 callable(self.__class__.__dict__[fn])
143 144 magics = filter(class_magic,Magic.__dict__.keys()) + \
144 145 filter(inst_magic,self.__dict__.keys()) + \
145 146 filter(inst_bound_magic,self.__class__.__dict__.keys())
146 147 out = []
147 148 for fn in set(magics):
148 149 out.append(fn.replace('magic_','',1))
149 150 out.sort()
150 151 return out
151 152
152 153 def extract_input_slices(self,slices,raw=False):
153 154 """Return as a string a set of input history slices.
154 155
155 156 Inputs:
156 157
157 158 - slices: the set of slices is given as a list of strings (like
158 159 ['1','4:8','9'], since this function is for use by magic functions
159 160 which get their arguments as strings.
160 161
161 162 Optional inputs:
162 163
163 164 - raw(False): by default, the processed input is used. If this is
164 165 true, the raw input history is used instead.
165 166
166 167 Note that slices can be called with two notations:
167 168
168 169 N:M -> standard python form, means including items N...(M-1).
169 170
170 171 N-M -> include items N..M (closed endpoint)."""
171 172
172 173 if raw:
173 174 hist = self.shell.input_hist_raw
174 175 else:
175 176 hist = self.shell.input_hist
176 177
177 178 cmds = []
178 179 for chunk in slices:
179 180 if ':' in chunk:
180 181 ini,fin = map(int,chunk.split(':'))
181 182 elif '-' in chunk:
182 183 ini,fin = map(int,chunk.split('-'))
183 184 fin += 1
184 185 else:
185 186 ini = int(chunk)
186 187 fin = ini+1
187 188 cmds.append(hist[ini:fin])
188 189 return cmds
189 190
190 191 def _ofind(self, oname, namespaces=None):
191 192 """Find an object in the available namespaces.
192 193
193 194 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
194 195
195 196 Has special code to detect magic functions.
196 197 """
197 198
198 199 oname = oname.strip()
199 200
200 201 alias_ns = None
201 202 if namespaces is None:
202 203 # Namespaces to search in:
203 204 # Put them in a list. The order is important so that we
204 205 # find things in the same order that Python finds them.
205 206 namespaces = [ ('Interactive', self.shell.user_ns),
206 207 ('IPython internal', self.shell.internal_ns),
207 208 ('Python builtin', __builtin__.__dict__),
208 ('Alias', self.shell.alias_table),
209 ('Alias', self.shell.alias_manager.alias_table),
209 210 ]
210 alias_ns = self.shell.alias_table
211 alias_ns = self.shell.alias_manager.alias_table
211 212
212 213 # initialize results to 'null'
213 214 found = 0; obj = None; ospace = None; ds = None;
214 215 ismagic = 0; isalias = 0; parent = None
215 216
216 217 # Look for the given name by splitting it in parts. If the head is
217 218 # found, then we look for all the remaining parts as members, and only
218 219 # declare success if we can find them all.
219 220 oname_parts = oname.split('.')
220 221 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
221 222 for nsname,ns in namespaces:
222 223 try:
223 224 obj = ns[oname_head]
224 225 except KeyError:
225 226 continue
226 227 else:
227 228 #print 'oname_rest:', oname_rest # dbg
228 229 for part in oname_rest:
229 230 try:
230 231 parent = obj
231 232 obj = getattr(obj,part)
232 233 except:
233 234 # Blanket except b/c some badly implemented objects
234 235 # allow __getattr__ to raise exceptions other than
235 236 # AttributeError, which then crashes IPython.
236 237 break
237 238 else:
238 239 # If we finish the for loop (no break), we got all members
239 240 found = 1
240 241 ospace = nsname
241 242 if ns == alias_ns:
242 243 isalias = 1
243 244 break # namespace loop
244 245
245 246 # Try to see if it's magic
246 247 if not found:
247 if oname.startswith(self.shell.ESC_MAGIC):
248 if oname.startswith(ESC_MAGIC):
248 249 oname = oname[1:]
249 250 obj = getattr(self,'magic_'+oname,None)
250 251 if obj is not None:
251 252 found = 1
252 253 ospace = 'IPython internal'
253 254 ismagic = 1
254 255
255 256 # Last try: special-case some literals like '', [], {}, etc:
256 257 if not found and oname_head in ["''",'""','[]','{}','()']:
257 258 obj = eval(oname_head)
258 259 found = 1
259 260 ospace = 'Interactive'
260 261
261 262 return {'found':found, 'obj':obj, 'namespace':ospace,
262 263 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
263 264
264 265 def arg_err(self,func):
265 266 """Print docstring if incorrect arguments were passed"""
266 267 print 'Error in arguments:'
267 268 print OInspect.getdoc(func)
268 269
269 270 def format_latex(self,strng):
270 271 """Format a string for latex inclusion."""
271 272
272 273 # Characters that need to be escaped for latex:
273 274 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
274 275 # Magic command names as headers:
275 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
276 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
276 277 re.MULTILINE)
277 278 # Magic commands
278 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
279 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
279 280 re.MULTILINE)
280 281 # Paragraph continue
281 282 par_re = re.compile(r'\\$',re.MULTILINE)
282 283
283 284 # The "\n" symbol
284 285 newline_re = re.compile(r'\\n')
285 286
286 287 # Now build the string for output:
287 288 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
288 289 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
289 290 strng)
290 291 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
291 292 strng = par_re.sub(r'\\\\',strng)
292 293 strng = escape_re.sub(r'\\\1',strng)
293 294 strng = newline_re.sub(r'\\textbackslash{}n',strng)
294 295 return strng
295 296
296 297 def format_screen(self,strng):
297 298 """Format a string for screen printing.
298 299
299 300 This removes some latex-type format codes."""
300 301 # Paragraph continue
301 302 par_re = re.compile(r'\\$',re.MULTILINE)
302 303 strng = par_re.sub('',strng)
303 304 return strng
304 305
305 306 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
306 307 """Parse options passed to an argument string.
307 308
308 309 The interface is similar to that of getopt(), but it returns back a
309 310 Struct with the options as keys and the stripped argument string still
310 311 as a string.
311 312
312 313 arg_str is quoted as a true sys.argv vector by using shlex.split.
313 314 This allows us to easily expand variables, glob files, quote
314 315 arguments, etc.
315 316
316 317 Options:
317 318 -mode: default 'string'. If given as 'list', the argument string is
318 319 returned as a list (split on whitespace) instead of a string.
319 320
320 321 -list_all: put all option values in lists. Normally only options
321 322 appearing more than once are put in a list.
322 323
323 324 -posix (True): whether to split the input line in POSIX mode or not,
324 325 as per the conventions outlined in the shlex module from the
325 326 standard library."""
326 327
327 328 # inject default options at the beginning of the input line
328 329 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
329 330 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
330 331
331 332 mode = kw.get('mode','string')
332 333 if mode not in ['string','list']:
333 334 raise ValueError,'incorrect mode given: %s' % mode
334 335 # Get options
335 336 list_all = kw.get('list_all',0)
336 337 posix = kw.get('posix',True)
337 338
338 339 # Check if we have more than one argument to warrant extra processing:
339 340 odict = {} # Dictionary with options
340 341 args = arg_str.split()
341 342 if len(args) >= 1:
342 343 # If the list of inputs only has 0 or 1 thing in it, there's no
343 344 # need to look for options
344 345 argv = arg_split(arg_str,posix)
345 346 # Do regular option processing
346 347 try:
347 348 opts,args = getopt(argv,opt_str,*long_opts)
348 349 except GetoptError,e:
349 350 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
350 351 " ".join(long_opts)))
351 352 for o,a in opts:
352 353 if o.startswith('--'):
353 354 o = o[2:]
354 355 else:
355 356 o = o[1:]
356 357 try:
357 358 odict[o].append(a)
358 359 except AttributeError:
359 360 odict[o] = [odict[o],a]
360 361 except KeyError:
361 362 if list_all:
362 363 odict[o] = [a]
363 364 else:
364 365 odict[o] = a
365 366
366 367 # Prepare opts,args for return
367 368 opts = Struct(odict)
368 369 if mode == 'string':
369 370 args = ' '.join(args)
370 371
371 372 return opts,args
372 373
373 374 #......................................................................
374 375 # And now the actual magic functions
375 376
376 377 # Functions for IPython shell work (vars,funcs, config, etc)
377 378 def magic_lsmagic(self, parameter_s = ''):
378 379 """List currently available magic functions."""
379 mesc = self.shell.ESC_MAGIC
380 mesc = ESC_MAGIC
380 381 print 'Available magic functions:\n'+mesc+\
381 382 (' '+mesc).join(self.lsmagic())
382 383 print '\n' + Magic.auto_status[self.shell.automagic]
383 384 return None
384 385
385 386 def magic_magic(self, parameter_s = ''):
386 387 """Print information about the magic function system.
387 388
388 389 Supported formats: -latex, -brief, -rest
389 390 """
390 391
391 392 mode = ''
392 393 try:
393 394 if parameter_s.split()[0] == '-latex':
394 395 mode = 'latex'
395 396 if parameter_s.split()[0] == '-brief':
396 397 mode = 'brief'
397 398 if parameter_s.split()[0] == '-rest':
398 399 mode = 'rest'
399 400 rest_docs = []
400 401 except:
401 402 pass
402 403
403 404 magic_docs = []
404 405 for fname in self.lsmagic():
405 406 mname = 'magic_' + fname
406 407 for space in (Magic,self,self.__class__):
407 408 try:
408 409 fn = space.__dict__[mname]
409 410 except KeyError:
410 411 pass
411 412 else:
412 413 break
413 414 if mode == 'brief':
414 415 # only first line
415 416 if fn.__doc__:
416 417 fndoc = fn.__doc__.split('\n',1)[0]
417 418 else:
418 419 fndoc = 'No documentation'
419 420 else:
420 421 if fn.__doc__:
421 422 fndoc = fn.__doc__.rstrip()
422 423 else:
423 424 fndoc = 'No documentation'
424 425
425 426
426 427 if mode == 'rest':
427 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(self.shell.ESC_MAGIC,
428 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
428 429 fname,fndoc))
429 430
430 431 else:
431 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
432 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
432 433 fname,fndoc))
433 434
434 435 magic_docs = ''.join(magic_docs)
435 436
436 437 if mode == 'rest':
437 438 return "".join(rest_docs)
438 439
439 440 if mode == 'latex':
440 441 print self.format_latex(magic_docs)
441 442 return
442 443 else:
443 444 magic_docs = self.format_screen(magic_docs)
444 445 if mode == 'brief':
445 446 return magic_docs
446 447
447 448 outmsg = """
448 449 IPython's 'magic' functions
449 450 ===========================
450 451
451 452 The magic function system provides a series of functions which allow you to
452 453 control the behavior of IPython itself, plus a lot of system-type
453 454 features. All these functions are prefixed with a % character, but parameters
454 455 are given without parentheses or quotes.
455 456
456 457 NOTE: If you have 'automagic' enabled (via the command line option or with the
457 458 %automagic function), you don't need to type in the % explicitly. By default,
458 459 IPython ships with automagic on, so you should only rarely need the % escape.
459 460
460 461 Example: typing '%cd mydir' (without the quotes) changes you working directory
461 462 to 'mydir', if it exists.
462 463
463 464 You can define your own magic functions to extend the system. See the supplied
464 465 ipythonrc and example-magic.py files for details (in your ipython
465 466 configuration directory, typically $HOME/.ipython/).
466 467
467 468 You can also define your own aliased names for magic functions. In your
468 469 ipythonrc file, placing a line like:
469 470
470 471 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
471 472
472 473 will define %pf as a new name for %profile.
473 474
474 475 You can also call magics in code using the magic() function, which IPython
475 476 automatically adds to the builtin namespace. Type 'magic?' for details.
476 477
477 478 For a list of the available magic functions, use %lsmagic. For a description
478 479 of any of them, type %magic_name?, e.g. '%cd?'.
479 480
480 481 Currently the magic system has the following functions:\n"""
481 482
482 mesc = self.shell.ESC_MAGIC
483 mesc = ESC_MAGIC
483 484 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
484 485 "\n\n%s%s\n\n%s" % (outmsg,
485 486 magic_docs,mesc,mesc,
486 487 (' '+mesc).join(self.lsmagic()),
487 488 Magic.auto_status[self.shell.automagic] ) )
488 489
489 490 page(outmsg,screen_lines=self.shell.usable_screen_length)
490 491
491 492
492 493 def magic_autoindent(self, parameter_s = ''):
493 494 """Toggle autoindent on/off (if available)."""
494 495
495 496 self.shell.set_autoindent()
496 497 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
497 498
498 499
499 500 def magic_automagic(self, parameter_s = ''):
500 501 """Make magic functions callable without having to type the initial %.
501 502
502 503 Without argumentsl toggles on/off (when off, you must call it as
503 504 %automagic, of course). With arguments it sets the value, and you can
504 505 use any of (case insensitive):
505 506
506 507 - on,1,True: to activate
507 508
508 509 - off,0,False: to deactivate.
509 510
510 511 Note that magic functions have lowest priority, so if there's a
511 512 variable whose name collides with that of a magic fn, automagic won't
512 513 work for that function (you get the variable instead). However, if you
513 514 delete the variable (del var), the previously shadowed magic function
514 515 becomes visible to automagic again."""
515 516
516 517 arg = parameter_s.lower()
517 518 if parameter_s in ('on','1','true'):
518 519 self.shell.automagic = True
519 520 elif parameter_s in ('off','0','false'):
520 521 self.shell.automagic = False
521 522 else:
522 523 self.shell.automagic = not self.shell.automagic
523 524 print '\n' + Magic.auto_status[self.shell.automagic]
524 525
525 526 @testdec.skip_doctest
526 527 def magic_autocall(self, parameter_s = ''):
527 528 """Make functions callable without having to type parentheses.
528 529
529 530 Usage:
530 531
531 532 %autocall [mode]
532 533
533 534 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
534 535 value is toggled on and off (remembering the previous state).
535 536
536 537 In more detail, these values mean:
537 538
538 539 0 -> fully disabled
539 540
540 541 1 -> active, but do not apply if there are no arguments on the line.
541 542
542 543 In this mode, you get:
543 544
544 545 In [1]: callable
545 546 Out[1]: <built-in function callable>
546 547
547 548 In [2]: callable 'hello'
548 549 ------> callable('hello')
549 550 Out[2]: False
550 551
551 552 2 -> Active always. Even if no arguments are present, the callable
552 553 object is called:
553 554
554 555 In [2]: float
555 556 ------> float()
556 557 Out[2]: 0.0
557 558
558 559 Note that even with autocall off, you can still use '/' at the start of
559 560 a line to treat the first argument on the command line as a function
560 561 and add parentheses to it:
561 562
562 563 In [8]: /str 43
563 564 ------> str(43)
564 565 Out[8]: '43'
565 566
566 567 # all-random (note for auto-testing)
567 568 """
568 569
569 570 if parameter_s:
570 571 arg = int(parameter_s)
571 572 else:
572 573 arg = 'toggle'
573 574
574 575 if not arg in (0,1,2,'toggle'):
575 576 error('Valid modes: (0->Off, 1->Smart, 2->Full')
576 577 return
577 578
578 579 if arg in (0,1,2):
579 580 self.shell.autocall = arg
580 581 else: # toggle
581 582 if self.shell.autocall:
582 583 self._magic_state.autocall_save = self.shell.autocall
583 584 self.shell.autocall = 0
584 585 else:
585 586 try:
586 587 self.shell.autocall = self._magic_state.autocall_save
587 588 except AttributeError:
588 589 self.shell.autocall = self._magic_state.autocall_save = 1
589 590
590 591 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
591 592
592 593 def magic_system_verbose(self, parameter_s = ''):
593 594 """Set verbose printing of system calls.
594 595
595 596 If called without an argument, act as a toggle"""
596 597
597 598 if parameter_s:
598 599 val = bool(eval(parameter_s))
599 600 else:
600 601 val = None
601 602
602 603 if self.shell.system_verbose:
603 604 self.shell.system_verbose = False
604 605 else:
605 606 self.shell.system_verbose = True
606 607 print "System verbose printing is:",\
607 608 ['OFF','ON'][self.shell.system_verbose]
608 609
609 610
610 611 def magic_page(self, parameter_s=''):
611 612 """Pretty print the object and display it through a pager.
612 613
613 614 %page [options] OBJECT
614 615
615 616 If no object is given, use _ (last output).
616 617
617 618 Options:
618 619
619 620 -r: page str(object), don't pretty-print it."""
620 621
621 622 # After a function contributed by Olivier Aubert, slightly modified.
622 623
623 624 # Process options/args
624 625 opts,args = self.parse_options(parameter_s,'r')
625 626 raw = 'r' in opts
626 627
627 628 oname = args and args or '_'
628 629 info = self._ofind(oname)
629 630 if info['found']:
630 631 txt = (raw and str or pformat)( info['obj'] )
631 632 page(txt)
632 633 else:
633 634 print 'Object `%s` not found' % oname
634 635
635 636 def magic_profile(self, parameter_s=''):
636 637 """Print your currently active IPyhton profile."""
637 638 if self.shell.profile:
638 639 printpl('Current IPython profile: $self.shell.profile.')
639 640 else:
640 641 print 'No profile active.'
641 642
642 643 def magic_pinfo(self, parameter_s='', namespaces=None):
643 644 """Provide detailed information about an object.
644 645
645 646 '%pinfo object' is just a synonym for object? or ?object."""
646 647
647 648 #print 'pinfo par: <%s>' % parameter_s # dbg
648 649
649 650
650 651 # detail_level: 0 -> obj? , 1 -> obj??
651 652 detail_level = 0
652 653 # We need to detect if we got called as 'pinfo pinfo foo', which can
653 654 # happen if the user types 'pinfo foo?' at the cmd line.
654 655 pinfo,qmark1,oname,qmark2 = \
655 656 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
656 657 if pinfo or qmark1 or qmark2:
657 658 detail_level = 1
658 659 if "*" in oname:
659 660 self.magic_psearch(oname)
660 661 else:
661 662 self._inspect('pinfo', oname, detail_level=detail_level,
662 663 namespaces=namespaces)
663 664
664 665 def magic_pdef(self, parameter_s='', namespaces=None):
665 666 """Print the definition header for any callable object.
666 667
667 668 If the object is a class, print the constructor information."""
668 669 self._inspect('pdef',parameter_s, namespaces)
669 670
670 671 def magic_pdoc(self, parameter_s='', namespaces=None):
671 672 """Print the docstring for an object.
672 673
673 674 If the given object is a class, it will print both the class and the
674 675 constructor docstrings."""
675 676 self._inspect('pdoc',parameter_s, namespaces)
676 677
677 678 def magic_psource(self, parameter_s='', namespaces=None):
678 679 """Print (or run through pager) the source code for an object."""
679 680 self._inspect('psource',parameter_s, namespaces)
680 681
681 682 def magic_pfile(self, parameter_s=''):
682 683 """Print (or run through pager) the file where an object is defined.
683 684
684 685 The file opens at the line where the object definition begins. IPython
685 686 will honor the environment variable PAGER if set, and otherwise will
686 687 do its best to print the file in a convenient form.
687 688
688 689 If the given argument is not an object currently defined, IPython will
689 690 try to interpret it as a filename (automatically adding a .py extension
690 691 if needed). You can thus use %pfile as a syntax highlighting code
691 692 viewer."""
692 693
693 694 # first interpret argument as an object name
694 695 out = self._inspect('pfile',parameter_s)
695 696 # if not, try the input as a filename
696 697 if out == 'not found':
697 698 try:
698 699 filename = get_py_filename(parameter_s)
699 700 except IOError,msg:
700 701 print msg
701 702 return
702 703 page(self.shell.inspector.format(file(filename).read()))
703 704
704 705 def _inspect(self,meth,oname,namespaces=None,**kw):
705 706 """Generic interface to the inspector system.
706 707
707 708 This function is meant to be called by pdef, pdoc & friends."""
708 709
709 710 #oname = oname.strip()
710 711 #print '1- oname: <%r>' % oname # dbg
711 712 try:
712 713 oname = oname.strip().encode('ascii')
713 714 #print '2- oname: <%r>' % oname # dbg
714 715 except UnicodeEncodeError:
715 716 print 'Python identifiers can only contain ascii characters.'
716 717 return 'not found'
717 718
718 719 info = Struct(self._ofind(oname, namespaces))
719 720
720 721 if info.found:
721 722 try:
722 723 IPython.utils.generics.inspect_object(info.obj)
723 724 return
724 725 except TryNext:
725 726 pass
726 727 # Get the docstring of the class property if it exists.
727 728 path = oname.split('.')
728 729 root = '.'.join(path[:-1])
729 730 if info.parent is not None:
730 731 try:
731 732 target = getattr(info.parent, '__class__')
732 733 # The object belongs to a class instance.
733 734 try:
734 735 target = getattr(target, path[-1])
735 736 # The class defines the object.
736 737 if isinstance(target, property):
737 738 oname = root + '.__class__.' + path[-1]
738 739 info = Struct(self._ofind(oname))
739 740 except AttributeError: pass
740 741 except AttributeError: pass
741 742
742 743 pmethod = getattr(self.shell.inspector,meth)
743 744 formatter = info.ismagic and self.format_screen or None
744 745 if meth == 'pdoc':
745 746 pmethod(info.obj,oname,formatter)
746 747 elif meth == 'pinfo':
747 748 pmethod(info.obj,oname,formatter,info,**kw)
748 749 else:
749 750 pmethod(info.obj,oname)
750 751 else:
751 752 print 'Object `%s` not found.' % oname
752 753 return 'not found' # so callers can take other action
753 754
754 755 def magic_psearch(self, parameter_s=''):
755 756 """Search for object in namespaces by wildcard.
756 757
757 758 %psearch [options] PATTERN [OBJECT TYPE]
758 759
759 760 Note: ? can be used as a synonym for %psearch, at the beginning or at
760 761 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
761 762 rest of the command line must be unchanged (options come first), so
762 763 for example the following forms are equivalent
763 764
764 765 %psearch -i a* function
765 766 -i a* function?
766 767 ?-i a* function
767 768
768 769 Arguments:
769 770
770 771 PATTERN
771 772
772 773 where PATTERN is a string containing * as a wildcard similar to its
773 774 use in a shell. The pattern is matched in all namespaces on the
774 775 search path. By default objects starting with a single _ are not
775 776 matched, many IPython generated objects have a single
776 777 underscore. The default is case insensitive matching. Matching is
777 778 also done on the attributes of objects and not only on the objects
778 779 in a module.
779 780
780 781 [OBJECT TYPE]
781 782
782 783 Is the name of a python type from the types module. The name is
783 784 given in lowercase without the ending type, ex. StringType is
784 785 written string. By adding a type here only objects matching the
785 786 given type are matched. Using all here makes the pattern match all
786 787 types (this is the default).
787 788
788 789 Options:
789 790
790 791 -a: makes the pattern match even objects whose names start with a
791 792 single underscore. These names are normally ommitted from the
792 793 search.
793 794
794 795 -i/-c: make the pattern case insensitive/sensitive. If neither of
795 796 these options is given, the default is read from your ipythonrc
796 797 file. The option name which sets this value is
797 798 'wildcards_case_sensitive'. If this option is not specified in your
798 799 ipythonrc file, IPython's internal default is to do a case sensitive
799 800 search.
800 801
801 802 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
802 803 specifiy can be searched in any of the following namespaces:
803 804 'builtin', 'user', 'user_global','internal', 'alias', where
804 805 'builtin' and 'user' are the search defaults. Note that you should
805 806 not use quotes when specifying namespaces.
806 807
807 808 'Builtin' contains the python module builtin, 'user' contains all
808 809 user data, 'alias' only contain the shell aliases and no python
809 810 objects, 'internal' contains objects used by IPython. The
810 811 'user_global' namespace is only used by embedded IPython instances,
811 812 and it contains module-level globals. You can add namespaces to the
812 813 search with -s or exclude them with -e (these options can be given
813 814 more than once).
814 815
815 816 Examples:
816 817
817 818 %psearch a* -> objects beginning with an a
818 819 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
819 820 %psearch a* function -> all functions beginning with an a
820 821 %psearch re.e* -> objects beginning with an e in module re
821 822 %psearch r*.e* -> objects that start with e in modules starting in r
822 823 %psearch r*.* string -> all strings in modules beginning with r
823 824
824 825 Case sensitve search:
825 826
826 827 %psearch -c a* list all object beginning with lower case a
827 828
828 829 Show objects beginning with a single _:
829 830
830 831 %psearch -a _* list objects beginning with a single underscore"""
831 832 try:
832 833 parameter_s = parameter_s.encode('ascii')
833 834 except UnicodeEncodeError:
834 835 print 'Python identifiers can only contain ascii characters.'
835 836 return
836 837
837 838 # default namespaces to be searched
838 839 def_search = ['user','builtin']
839 840
840 841 # Process options/args
841 842 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
842 843 opt = opts.get
843 844 shell = self.shell
844 845 psearch = shell.inspector.psearch
845 846
846 847 # select case options
847 848 if opts.has_key('i'):
848 849 ignore_case = True
849 850 elif opts.has_key('c'):
850 851 ignore_case = False
851 852 else:
852 853 ignore_case = not shell.wildcards_case_sensitive
853 854
854 855 # Build list of namespaces to search from user options
855 856 def_search.extend(opt('s',[]))
856 857 ns_exclude = ns_exclude=opt('e',[])
857 858 ns_search = [nm for nm in def_search if nm not in ns_exclude]
858 859
859 860 # Call the actual search
860 861 try:
861 862 psearch(args,shell.ns_table,ns_search,
862 863 show_all=opt('a'),ignore_case=ignore_case)
863 864 except:
864 865 shell.showtraceback()
865 866
866 867 def magic_who_ls(self, parameter_s=''):
867 868 """Return a sorted list of all interactive variables.
868 869
869 870 If arguments are given, only variables of types matching these
870 871 arguments are returned."""
871 872
872 873 user_ns = self.shell.user_ns
873 874 internal_ns = self.shell.internal_ns
874 875 user_config_ns = self.shell.user_config_ns
875 876 out = []
876 877 typelist = parameter_s.split()
877 878
878 879 for i in user_ns:
879 880 if not (i.startswith('_') or i.startswith('_i')) \
880 881 and not (i in internal_ns or i in user_config_ns):
881 882 if typelist:
882 883 if type(user_ns[i]).__name__ in typelist:
883 884 out.append(i)
884 885 else:
885 886 out.append(i)
886 887 out.sort()
887 888 return out
888 889
889 890 def magic_who(self, parameter_s=''):
890 891 """Print all interactive variables, with some minimal formatting.
891 892
892 893 If any arguments are given, only variables whose type matches one of
893 894 these are printed. For example:
894 895
895 896 %who function str
896 897
897 898 will only list functions and strings, excluding all other types of
898 899 variables. To find the proper type names, simply use type(var) at a
899 900 command line to see how python prints type names. For example:
900 901
901 902 In [1]: type('hello')\\
902 903 Out[1]: <type 'str'>
903 904
904 905 indicates that the type name for strings is 'str'.
905 906
906 907 %who always excludes executed names loaded through your configuration
907 908 file and things which are internal to IPython.
908 909
909 910 This is deliberate, as typically you may load many modules and the
910 911 purpose of %who is to show you only what you've manually defined."""
911 912
912 913 varlist = self.magic_who_ls(parameter_s)
913 914 if not varlist:
914 915 if parameter_s:
915 916 print 'No variables match your requested type.'
916 917 else:
917 918 print 'Interactive namespace is empty.'
918 919 return
919 920
920 921 # if we have variables, move on...
921 922 count = 0
922 923 for i in varlist:
923 924 print i+'\t',
924 925 count += 1
925 926 if count > 8:
926 927 count = 0
927 928 print
928 929 print
929 930
930 931 def magic_whos(self, parameter_s=''):
931 932 """Like %who, but gives some extra information about each variable.
932 933
933 934 The same type filtering of %who can be applied here.
934 935
935 936 For all variables, the type is printed. Additionally it prints:
936 937
937 938 - For {},[],(): their length.
938 939
939 940 - For numpy and Numeric arrays, a summary with shape, number of
940 941 elements, typecode and size in memory.
941 942
942 943 - Everything else: a string representation, snipping their middle if
943 944 too long."""
944 945
945 946 varnames = self.magic_who_ls(parameter_s)
946 947 if not varnames:
947 948 if parameter_s:
948 949 print 'No variables match your requested type.'
949 950 else:
950 951 print 'Interactive namespace is empty.'
951 952 return
952 953
953 954 # if we have variables, move on...
954 955
955 956 # for these types, show len() instead of data:
956 957 seq_types = [types.DictType,types.ListType,types.TupleType]
957 958
958 959 # for numpy/Numeric arrays, display summary info
959 960 try:
960 961 import numpy
961 962 except ImportError:
962 963 ndarray_type = None
963 964 else:
964 965 ndarray_type = numpy.ndarray.__name__
965 966 try:
966 967 import Numeric
967 968 except ImportError:
968 969 array_type = None
969 970 else:
970 971 array_type = Numeric.ArrayType.__name__
971 972
972 973 # Find all variable names and types so we can figure out column sizes
973 974 def get_vars(i):
974 975 return self.shell.user_ns[i]
975 976
976 977 # some types are well known and can be shorter
977 978 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
978 979 def type_name(v):
979 980 tn = type(v).__name__
980 981 return abbrevs.get(tn,tn)
981 982
982 983 varlist = map(get_vars,varnames)
983 984
984 985 typelist = []
985 986 for vv in varlist:
986 987 tt = type_name(vv)
987 988
988 989 if tt=='instance':
989 990 typelist.append( abbrevs.get(str(vv.__class__),
990 991 str(vv.__class__)))
991 992 else:
992 993 typelist.append(tt)
993 994
994 995 # column labels and # of spaces as separator
995 996 varlabel = 'Variable'
996 997 typelabel = 'Type'
997 998 datalabel = 'Data/Info'
998 999 colsep = 3
999 1000 # variable format strings
1000 1001 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1001 1002 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1002 1003 aformat = "%s: %s elems, type `%s`, %s bytes"
1003 1004 # find the size of the columns to format the output nicely
1004 1005 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1005 1006 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1006 1007 # table header
1007 1008 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1008 1009 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1009 1010 # and the table itself
1010 1011 kb = 1024
1011 1012 Mb = 1048576 # kb**2
1012 1013 for vname,var,vtype in zip(varnames,varlist,typelist):
1013 1014 print itpl(vformat),
1014 1015 if vtype in seq_types:
1015 1016 print len(var)
1016 1017 elif vtype in [array_type,ndarray_type]:
1017 1018 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1018 1019 if vtype==ndarray_type:
1019 1020 # numpy
1020 1021 vsize = var.size
1021 1022 vbytes = vsize*var.itemsize
1022 1023 vdtype = var.dtype
1023 1024 else:
1024 1025 # Numeric
1025 1026 vsize = Numeric.size(var)
1026 1027 vbytes = vsize*var.itemsize()
1027 1028 vdtype = var.typecode()
1028 1029
1029 1030 if vbytes < 100000:
1030 1031 print aformat % (vshape,vsize,vdtype,vbytes)
1031 1032 else:
1032 1033 print aformat % (vshape,vsize,vdtype,vbytes),
1033 1034 if vbytes < Mb:
1034 1035 print '(%s kb)' % (vbytes/kb,)
1035 1036 else:
1036 1037 print '(%s Mb)' % (vbytes/Mb,)
1037 1038 else:
1038 1039 try:
1039 1040 vstr = str(var)
1040 1041 except UnicodeEncodeError:
1041 1042 vstr = unicode(var).encode(sys.getdefaultencoding(),
1042 1043 'backslashreplace')
1043 1044 vstr = vstr.replace('\n','\\n')
1044 1045 if len(vstr) < 50:
1045 1046 print vstr
1046 1047 else:
1047 1048 printpl(vfmt_short)
1048 1049
1049 1050 def magic_reset(self, parameter_s=''):
1050 1051 """Resets the namespace by removing all names defined by the user.
1051 1052
1052 1053 Input/Output history are left around in case you need them.
1053 1054
1054 1055 Parameters
1055 1056 ----------
1056 1057 -y : force reset without asking for confirmation.
1057 1058
1058 1059 Examples
1059 1060 --------
1060 1061 In [6]: a = 1
1061 1062
1062 1063 In [7]: a
1063 1064 Out[7]: 1
1064 1065
1065 1066 In [8]: 'a' in _ip.user_ns
1066 1067 Out[8]: True
1067 1068
1068 1069 In [9]: %reset -f
1069 1070
1070 1071 In [10]: 'a' in _ip.user_ns
1071 1072 Out[10]: False
1072 1073 """
1073 1074
1074 1075 if parameter_s == '-f':
1075 1076 ans = True
1076 1077 else:
1077 1078 ans = self.shell.ask_yes_no(
1078 1079 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1079 1080 if not ans:
1080 1081 print 'Nothing done.'
1081 1082 return
1082 1083 user_ns = self.shell.user_ns
1083 1084 for i in self.magic_who_ls():
1084 1085 del(user_ns[i])
1085 1086
1086 1087 # Also flush the private list of module references kept for script
1087 1088 # execution protection
1088 1089 self.shell.clear_main_mod_cache()
1089 1090
1090 1091 def magic_logstart(self,parameter_s=''):
1091 1092 """Start logging anywhere in a session.
1092 1093
1093 1094 %logstart [-o|-r|-t] [log_name [log_mode]]
1094 1095
1095 1096 If no name is given, it defaults to a file named 'ipython_log.py' in your
1096 1097 current directory, in 'rotate' mode (see below).
1097 1098
1098 1099 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1099 1100 history up to that point and then continues logging.
1100 1101
1101 1102 %logstart takes a second optional parameter: logging mode. This can be one
1102 1103 of (note that the modes are given unquoted):\\
1103 1104 append: well, that says it.\\
1104 1105 backup: rename (if exists) to name~ and start name.\\
1105 1106 global: single logfile in your home dir, appended to.\\
1106 1107 over : overwrite existing log.\\
1107 1108 rotate: create rotating logs name.1~, name.2~, etc.
1108 1109
1109 1110 Options:
1110 1111
1111 1112 -o: log also IPython's output. In this mode, all commands which
1112 1113 generate an Out[NN] prompt are recorded to the logfile, right after
1113 1114 their corresponding input line. The output lines are always
1114 1115 prepended with a '#[Out]# ' marker, so that the log remains valid
1115 1116 Python code.
1116 1117
1117 1118 Since this marker is always the same, filtering only the output from
1118 1119 a log is very easy, using for example a simple awk call:
1119 1120
1120 1121 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1121 1122
1122 1123 -r: log 'raw' input. Normally, IPython's logs contain the processed
1123 1124 input, so that user lines are logged in their final form, converted
1124 1125 into valid Python. For example, %Exit is logged as
1125 1126 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1126 1127 exactly as typed, with no transformations applied.
1127 1128
1128 1129 -t: put timestamps before each input line logged (these are put in
1129 1130 comments)."""
1130 1131
1131 1132 opts,par = self.parse_options(parameter_s,'ort')
1132 1133 log_output = 'o' in opts
1133 1134 log_raw_input = 'r' in opts
1134 1135 timestamp = 't' in opts
1135 1136
1136 1137 logger = self.shell.logger
1137 1138
1138 1139 # if no args are given, the defaults set in the logger constructor by
1139 1140 # ipytohn remain valid
1140 1141 if par:
1141 1142 try:
1142 1143 logfname,logmode = par.split()
1143 1144 except:
1144 1145 logfname = par
1145 1146 logmode = 'backup'
1146 1147 else:
1147 1148 logfname = logger.logfname
1148 1149 logmode = logger.logmode
1149 1150 # put logfname into rc struct as if it had been called on the command
1150 1151 # line, so it ends up saved in the log header Save it in case we need
1151 1152 # to restore it...
1152 1153 old_logfile = self.shell.logfile
1153 1154 if logfname:
1154 1155 logfname = os.path.expanduser(logfname)
1155 1156 self.shell.logfile = logfname
1156 1157 # TODO: we need to re-think how logs with args/opts are replayed
1157 1158 # and tracked.
1158 1159 # loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1159 1160 loghead = self.shell.loghead_tpl % ('','')
1160 1161 try:
1161 1162 started = logger.logstart(logfname,loghead,logmode,
1162 1163 log_output,timestamp,log_raw_input)
1163 1164 except:
1164 1165 rc.opts.logfile = old_logfile
1165 1166 warn("Couldn't start log: %s" % sys.exc_info()[1])
1166 1167 else:
1167 1168 # log input history up to this point, optionally interleaving
1168 1169 # output if requested
1169 1170
1170 1171 if timestamp:
1171 1172 # disable timestamping for the previous history, since we've
1172 1173 # lost those already (no time machine here).
1173 1174 logger.timestamp = False
1174 1175
1175 1176 if log_raw_input:
1176 1177 input_hist = self.shell.input_hist_raw
1177 1178 else:
1178 1179 input_hist = self.shell.input_hist
1179 1180
1180 1181 if log_output:
1181 1182 log_write = logger.log_write
1182 1183 output_hist = self.shell.output_hist
1183 1184 for n in range(1,len(input_hist)-1):
1184 1185 log_write(input_hist[n].rstrip())
1185 1186 if n in output_hist:
1186 1187 log_write(repr(output_hist[n]),'output')
1187 1188 else:
1188 1189 logger.log_write(input_hist[1:])
1189 1190 if timestamp:
1190 1191 # re-enable timestamping
1191 1192 logger.timestamp = True
1192 1193
1193 1194 print ('Activating auto-logging. '
1194 1195 'Current session state plus future input saved.')
1195 1196 logger.logstate()
1196 1197
1197 1198 def magic_logstop(self,parameter_s=''):
1198 1199 """Fully stop logging and close log file.
1199 1200
1200 1201 In order to start logging again, a new %logstart call needs to be made,
1201 1202 possibly (though not necessarily) with a new filename, mode and other
1202 1203 options."""
1203 1204 self.logger.logstop()
1204 1205
1205 1206 def magic_logoff(self,parameter_s=''):
1206 1207 """Temporarily stop logging.
1207 1208
1208 1209 You must have previously started logging."""
1209 1210 self.shell.logger.switch_log(0)
1210 1211
1211 1212 def magic_logon(self,parameter_s=''):
1212 1213 """Restart logging.
1213 1214
1214 1215 This function is for restarting logging which you've temporarily
1215 1216 stopped with %logoff. For starting logging for the first time, you
1216 1217 must use the %logstart function, which allows you to specify an
1217 1218 optional log filename."""
1218 1219
1219 1220 self.shell.logger.switch_log(1)
1220 1221
1221 1222 def magic_logstate(self,parameter_s=''):
1222 1223 """Print the status of the logging system."""
1223 1224
1224 1225 self.shell.logger.logstate()
1225 1226
1226 1227 def magic_pdb(self, parameter_s=''):
1227 1228 """Control the automatic calling of the pdb interactive debugger.
1228 1229
1229 1230 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1230 1231 argument it works as a toggle.
1231 1232
1232 1233 When an exception is triggered, IPython can optionally call the
1233 1234 interactive pdb debugger after the traceback printout. %pdb toggles
1234 1235 this feature on and off.
1235 1236
1236 1237 The initial state of this feature is set in your ipythonrc
1237 1238 configuration file (the variable is called 'pdb').
1238 1239
1239 1240 If you want to just activate the debugger AFTER an exception has fired,
1240 1241 without having to type '%pdb on' and rerunning your code, you can use
1241 1242 the %debug magic."""
1242 1243
1243 1244 par = parameter_s.strip().lower()
1244 1245
1245 1246 if par:
1246 1247 try:
1247 1248 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1248 1249 except KeyError:
1249 1250 print ('Incorrect argument. Use on/1, off/0, '
1250 1251 'or nothing for a toggle.')
1251 1252 return
1252 1253 else:
1253 1254 # toggle
1254 1255 new_pdb = not self.shell.call_pdb
1255 1256
1256 1257 # set on the shell
1257 1258 self.shell.call_pdb = new_pdb
1258 1259 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1259 1260
1260 1261 def magic_debug(self, parameter_s=''):
1261 1262 """Activate the interactive debugger in post-mortem mode.
1262 1263
1263 1264 If an exception has just occurred, this lets you inspect its stack
1264 1265 frames interactively. Note that this will always work only on the last
1265 1266 traceback that occurred, so you must call this quickly after an
1266 1267 exception that you wish to inspect has fired, because if another one
1267 1268 occurs, it clobbers the previous one.
1268 1269
1269 1270 If you want IPython to automatically do this on every exception, see
1270 1271 the %pdb magic for more details.
1271 1272 """
1272 1273
1273 1274 self.shell.debugger(force=True)
1274 1275
1275 1276 @testdec.skip_doctest
1276 1277 def magic_prun(self, parameter_s ='',user_mode=1,
1277 1278 opts=None,arg_lst=None,prog_ns=None):
1278 1279
1279 1280 """Run a statement through the python code profiler.
1280 1281
1281 1282 Usage:
1282 1283 %prun [options] statement
1283 1284
1284 1285 The given statement (which doesn't require quote marks) is run via the
1285 1286 python profiler in a manner similar to the profile.run() function.
1286 1287 Namespaces are internally managed to work correctly; profile.run
1287 1288 cannot be used in IPython because it makes certain assumptions about
1288 1289 namespaces which do not hold under IPython.
1289 1290
1290 1291 Options:
1291 1292
1292 1293 -l <limit>: you can place restrictions on what or how much of the
1293 1294 profile gets printed. The limit value can be:
1294 1295
1295 1296 * A string: only information for function names containing this string
1296 1297 is printed.
1297 1298
1298 1299 * An integer: only these many lines are printed.
1299 1300
1300 1301 * A float (between 0 and 1): this fraction of the report is printed
1301 1302 (for example, use a limit of 0.4 to see the topmost 40% only).
1302 1303
1303 1304 You can combine several limits with repeated use of the option. For
1304 1305 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1305 1306 information about class constructors.
1306 1307
1307 1308 -r: return the pstats.Stats object generated by the profiling. This
1308 1309 object has all the information about the profile in it, and you can
1309 1310 later use it for further analysis or in other functions.
1310 1311
1311 1312 -s <key>: sort profile by given key. You can provide more than one key
1312 1313 by using the option several times: '-s key1 -s key2 -s key3...'. The
1313 1314 default sorting key is 'time'.
1314 1315
1315 1316 The following is copied verbatim from the profile documentation
1316 1317 referenced below:
1317 1318
1318 1319 When more than one key is provided, additional keys are used as
1319 1320 secondary criteria when the there is equality in all keys selected
1320 1321 before them.
1321 1322
1322 1323 Abbreviations can be used for any key names, as long as the
1323 1324 abbreviation is unambiguous. The following are the keys currently
1324 1325 defined:
1325 1326
1326 1327 Valid Arg Meaning
1327 1328 "calls" call count
1328 1329 "cumulative" cumulative time
1329 1330 "file" file name
1330 1331 "module" file name
1331 1332 "pcalls" primitive call count
1332 1333 "line" line number
1333 1334 "name" function name
1334 1335 "nfl" name/file/line
1335 1336 "stdname" standard name
1336 1337 "time" internal time
1337 1338
1338 1339 Note that all sorts on statistics are in descending order (placing
1339 1340 most time consuming items first), where as name, file, and line number
1340 1341 searches are in ascending order (i.e., alphabetical). The subtle
1341 1342 distinction between "nfl" and "stdname" is that the standard name is a
1342 1343 sort of the name as printed, which means that the embedded line
1343 1344 numbers get compared in an odd way. For example, lines 3, 20, and 40
1344 1345 would (if the file names were the same) appear in the string order
1345 1346 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1346 1347 line numbers. In fact, sort_stats("nfl") is the same as
1347 1348 sort_stats("name", "file", "line").
1348 1349
1349 1350 -T <filename>: save profile results as shown on screen to a text
1350 1351 file. The profile is still shown on screen.
1351 1352
1352 1353 -D <filename>: save (via dump_stats) profile statistics to given
1353 1354 filename. This data is in a format understod by the pstats module, and
1354 1355 is generated by a call to the dump_stats() method of profile
1355 1356 objects. The profile is still shown on screen.
1356 1357
1357 1358 If you want to run complete programs under the profiler's control, use
1358 1359 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1359 1360 contains profiler specific options as described here.
1360 1361
1361 1362 You can read the complete documentation for the profile module with::
1362 1363
1363 1364 In [1]: import profile; profile.help()
1364 1365 """
1365 1366
1366 1367 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1367 1368 # protect user quote marks
1368 1369 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1369 1370
1370 1371 if user_mode: # regular user call
1371 1372 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1372 1373 list_all=1)
1373 1374 namespace = self.shell.user_ns
1374 1375 else: # called to run a program by %run -p
1375 1376 try:
1376 1377 filename = get_py_filename(arg_lst[0])
1377 1378 except IOError,msg:
1378 1379 error(msg)
1379 1380 return
1380 1381
1381 1382 arg_str = 'execfile(filename,prog_ns)'
1382 1383 namespace = locals()
1383 1384
1384 1385 opts.merge(opts_def)
1385 1386
1386 1387 prof = profile.Profile()
1387 1388 try:
1388 1389 prof = prof.runctx(arg_str,namespace,namespace)
1389 1390 sys_exit = ''
1390 1391 except SystemExit:
1391 1392 sys_exit = """*** SystemExit exception caught in code being profiled."""
1392 1393
1393 1394 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1394 1395
1395 1396 lims = opts.l
1396 1397 if lims:
1397 1398 lims = [] # rebuild lims with ints/floats/strings
1398 1399 for lim in opts.l:
1399 1400 try:
1400 1401 lims.append(int(lim))
1401 1402 except ValueError:
1402 1403 try:
1403 1404 lims.append(float(lim))
1404 1405 except ValueError:
1405 1406 lims.append(lim)
1406 1407
1407 1408 # Trap output.
1408 1409 stdout_trap = StringIO()
1409 1410
1410 1411 if hasattr(stats,'stream'):
1411 1412 # In newer versions of python, the stats object has a 'stream'
1412 1413 # attribute to write into.
1413 1414 stats.stream = stdout_trap
1414 1415 stats.print_stats(*lims)
1415 1416 else:
1416 1417 # For older versions, we manually redirect stdout during printing
1417 1418 sys_stdout = sys.stdout
1418 1419 try:
1419 1420 sys.stdout = stdout_trap
1420 1421 stats.print_stats(*lims)
1421 1422 finally:
1422 1423 sys.stdout = sys_stdout
1423 1424
1424 1425 output = stdout_trap.getvalue()
1425 1426 output = output.rstrip()
1426 1427
1427 1428 page(output,screen_lines=self.shell.usable_screen_length)
1428 1429 print sys_exit,
1429 1430
1430 1431 dump_file = opts.D[0]
1431 1432 text_file = opts.T[0]
1432 1433 if dump_file:
1433 1434 prof.dump_stats(dump_file)
1434 1435 print '\n*** Profile stats marshalled to file',\
1435 1436 `dump_file`+'.',sys_exit
1436 1437 if text_file:
1437 1438 pfile = file(text_file,'w')
1438 1439 pfile.write(output)
1439 1440 pfile.close()
1440 1441 print '\n*** Profile printout saved to text file',\
1441 1442 `text_file`+'.',sys_exit
1442 1443
1443 1444 if opts.has_key('r'):
1444 1445 return stats
1445 1446 else:
1446 1447 return None
1447 1448
1448 1449 @testdec.skip_doctest
1449 1450 def magic_run(self, parameter_s ='',runner=None,
1450 1451 file_finder=get_py_filename):
1451 1452 """Run the named file inside IPython as a program.
1452 1453
1453 1454 Usage:\\
1454 1455 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1455 1456
1456 1457 Parameters after the filename are passed as command-line arguments to
1457 1458 the program (put in sys.argv). Then, control returns to IPython's
1458 1459 prompt.
1459 1460
1460 1461 This is similar to running at a system prompt:\\
1461 1462 $ python file args\\
1462 1463 but with the advantage of giving you IPython's tracebacks, and of
1463 1464 loading all variables into your interactive namespace for further use
1464 1465 (unless -p is used, see below).
1465 1466
1466 1467 The file is executed in a namespace initially consisting only of
1467 1468 __name__=='__main__' and sys.argv constructed as indicated. It thus
1468 1469 sees its environment as if it were being run as a stand-alone program
1469 1470 (except for sharing global objects such as previously imported
1470 1471 modules). But after execution, the IPython interactive namespace gets
1471 1472 updated with all variables defined in the program (except for __name__
1472 1473 and sys.argv). This allows for very convenient loading of code for
1473 1474 interactive work, while giving each program a 'clean sheet' to run in.
1474 1475
1475 1476 Options:
1476 1477
1477 1478 -n: __name__ is NOT set to '__main__', but to the running file's name
1478 1479 without extension (as python does under import). This allows running
1479 1480 scripts and reloading the definitions in them without calling code
1480 1481 protected by an ' if __name__ == "__main__" ' clause.
1481 1482
1482 1483 -i: run the file in IPython's namespace instead of an empty one. This
1483 1484 is useful if you are experimenting with code written in a text editor
1484 1485 which depends on variables defined interactively.
1485 1486
1486 1487 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1487 1488 being run. This is particularly useful if IPython is being used to
1488 1489 run unittests, which always exit with a sys.exit() call. In such
1489 1490 cases you are interested in the output of the test results, not in
1490 1491 seeing a traceback of the unittest module.
1491 1492
1492 1493 -t: print timing information at the end of the run. IPython will give
1493 1494 you an estimated CPU time consumption for your script, which under
1494 1495 Unix uses the resource module to avoid the wraparound problems of
1495 1496 time.clock(). Under Unix, an estimate of time spent on system tasks
1496 1497 is also given (for Windows platforms this is reported as 0.0).
1497 1498
1498 1499 If -t is given, an additional -N<N> option can be given, where <N>
1499 1500 must be an integer indicating how many times you want the script to
1500 1501 run. The final timing report will include total and per run results.
1501 1502
1502 1503 For example (testing the script uniq_stable.py):
1503 1504
1504 1505 In [1]: run -t uniq_stable
1505 1506
1506 1507 IPython CPU timings (estimated):\\
1507 1508 User : 0.19597 s.\\
1508 1509 System: 0.0 s.\\
1509 1510
1510 1511 In [2]: run -t -N5 uniq_stable
1511 1512
1512 1513 IPython CPU timings (estimated):\\
1513 1514 Total runs performed: 5\\
1514 1515 Times : Total Per run\\
1515 1516 User : 0.910862 s, 0.1821724 s.\\
1516 1517 System: 0.0 s, 0.0 s.
1517 1518
1518 1519 -d: run your program under the control of pdb, the Python debugger.
1519 1520 This allows you to execute your program step by step, watch variables,
1520 1521 etc. Internally, what IPython does is similar to calling:
1521 1522
1522 1523 pdb.run('execfile("YOURFILENAME")')
1523 1524
1524 1525 with a breakpoint set on line 1 of your file. You can change the line
1525 1526 number for this automatic breakpoint to be <N> by using the -bN option
1526 1527 (where N must be an integer). For example:
1527 1528
1528 1529 %run -d -b40 myscript
1529 1530
1530 1531 will set the first breakpoint at line 40 in myscript.py. Note that
1531 1532 the first breakpoint must be set on a line which actually does
1532 1533 something (not a comment or docstring) for it to stop execution.
1533 1534
1534 1535 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1535 1536 first enter 'c' (without qoutes) to start execution up to the first
1536 1537 breakpoint.
1537 1538
1538 1539 Entering 'help' gives information about the use of the debugger. You
1539 1540 can easily see pdb's full documentation with "import pdb;pdb.help()"
1540 1541 at a prompt.
1541 1542
1542 1543 -p: run program under the control of the Python profiler module (which
1543 1544 prints a detailed report of execution times, function calls, etc).
1544 1545
1545 1546 You can pass other options after -p which affect the behavior of the
1546 1547 profiler itself. See the docs for %prun for details.
1547 1548
1548 1549 In this mode, the program's variables do NOT propagate back to the
1549 1550 IPython interactive namespace (because they remain in the namespace
1550 1551 where the profiler executes them).
1551 1552
1552 1553 Internally this triggers a call to %prun, see its documentation for
1553 1554 details on the options available specifically for profiling.
1554 1555
1555 1556 There is one special usage for which the text above doesn't apply:
1556 1557 if the filename ends with .ipy, the file is run as ipython script,
1557 1558 just as if the commands were written on IPython prompt.
1558 1559 """
1559 1560
1560 1561 # get arguments and set sys.argv for program to be run.
1561 1562 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1562 1563 mode='list',list_all=1)
1563 1564
1564 1565 try:
1565 1566 filename = file_finder(arg_lst[0])
1566 1567 except IndexError:
1567 1568 warn('you must provide at least a filename.')
1568 1569 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1569 1570 return
1570 1571 except IOError,msg:
1571 1572 error(msg)
1572 1573 return
1573 1574
1574 1575 if filename.lower().endswith('.ipy'):
1575 1576 self.runlines(open(filename).read(), clean=True)
1576 1577 return
1577 1578
1578 1579 # Control the response to exit() calls made by the script being run
1579 1580 exit_ignore = opts.has_key('e')
1580 1581
1581 1582 # Make sure that the running script gets a proper sys.argv as if it
1582 1583 # were run from a system shell.
1583 1584 save_argv = sys.argv # save it for later restoring
1584 1585 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1585 1586
1586 1587 if opts.has_key('i'):
1587 1588 # Run in user's interactive namespace
1588 1589 prog_ns = self.shell.user_ns
1589 1590 __name__save = self.shell.user_ns['__name__']
1590 1591 prog_ns['__name__'] = '__main__'
1591 1592 main_mod = self.shell.new_main_mod(prog_ns)
1592 1593 else:
1593 1594 # Run in a fresh, empty namespace
1594 1595 if opts.has_key('n'):
1595 1596 name = os.path.splitext(os.path.basename(filename))[0]
1596 1597 else:
1597 1598 name = '__main__'
1598 1599
1599 1600 main_mod = self.shell.new_main_mod()
1600 1601 prog_ns = main_mod.__dict__
1601 1602 prog_ns['__name__'] = name
1602 1603
1603 1604 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1604 1605 # set the __file__ global in the script's namespace
1605 1606 prog_ns['__file__'] = filename
1606 1607
1607 1608 # pickle fix. See iplib for an explanation. But we need to make sure
1608 1609 # that, if we overwrite __main__, we replace it at the end
1609 1610 main_mod_name = prog_ns['__name__']
1610 1611
1611 1612 if main_mod_name == '__main__':
1612 1613 restore_main = sys.modules['__main__']
1613 1614 else:
1614 1615 restore_main = False
1615 1616
1616 1617 # This needs to be undone at the end to prevent holding references to
1617 1618 # every single object ever created.
1618 1619 sys.modules[main_mod_name] = main_mod
1619 1620
1620 1621 stats = None
1621 1622 try:
1622 1623 self.shell.savehist()
1623 1624
1624 1625 if opts.has_key('p'):
1625 1626 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1626 1627 else:
1627 1628 if opts.has_key('d'):
1628 1629 deb = debugger.Pdb(self.shell.colors)
1629 1630 # reset Breakpoint state, which is moronically kept
1630 1631 # in a class
1631 1632 bdb.Breakpoint.next = 1
1632 1633 bdb.Breakpoint.bplist = {}
1633 1634 bdb.Breakpoint.bpbynumber = [None]
1634 1635 # Set an initial breakpoint to stop execution
1635 1636 maxtries = 10
1636 1637 bp = int(opts.get('b',[1])[0])
1637 1638 checkline = deb.checkline(filename,bp)
1638 1639 if not checkline:
1639 1640 for bp in range(bp+1,bp+maxtries+1):
1640 1641 if deb.checkline(filename,bp):
1641 1642 break
1642 1643 else:
1643 1644 msg = ("\nI failed to find a valid line to set "
1644 1645 "a breakpoint\n"
1645 1646 "after trying up to line: %s.\n"
1646 1647 "Please set a valid breakpoint manually "
1647 1648 "with the -b option." % bp)
1648 1649 error(msg)
1649 1650 return
1650 1651 # if we find a good linenumber, set the breakpoint
1651 1652 deb.do_break('%s:%s' % (filename,bp))
1652 1653 # Start file run
1653 1654 print "NOTE: Enter 'c' at the",
1654 1655 print "%s prompt to start your script." % deb.prompt
1655 1656 try:
1656 1657 deb.run('execfile("%s")' % filename,prog_ns)
1657 1658
1658 1659 except:
1659 1660 etype, value, tb = sys.exc_info()
1660 1661 # Skip three frames in the traceback: the %run one,
1661 1662 # one inside bdb.py, and the command-line typed by the
1662 1663 # user (run by exec in pdb itself).
1663 1664 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1664 1665 else:
1665 1666 if runner is None:
1666 1667 runner = self.shell.safe_execfile
1667 1668 if opts.has_key('t'):
1668 1669 # timed execution
1669 1670 try:
1670 1671 nruns = int(opts['N'][0])
1671 1672 if nruns < 1:
1672 1673 error('Number of runs must be >=1')
1673 1674 return
1674 1675 except (KeyError):
1675 1676 nruns = 1
1676 1677 if nruns == 1:
1677 1678 t0 = clock2()
1678 1679 runner(filename,prog_ns,prog_ns,
1679 1680 exit_ignore=exit_ignore)
1680 1681 t1 = clock2()
1681 1682 t_usr = t1[0]-t0[0]
1682 1683 t_sys = t1[1]-t0[1]
1683 1684 print "\nIPython CPU timings (estimated):"
1684 1685 print " User : %10s s." % t_usr
1685 1686 print " System: %10s s." % t_sys
1686 1687 else:
1687 1688 runs = range(nruns)
1688 1689 t0 = clock2()
1689 1690 for nr in runs:
1690 1691 runner(filename,prog_ns,prog_ns,
1691 1692 exit_ignore=exit_ignore)
1692 1693 t1 = clock2()
1693 1694 t_usr = t1[0]-t0[0]
1694 1695 t_sys = t1[1]-t0[1]
1695 1696 print "\nIPython CPU timings (estimated):"
1696 1697 print "Total runs performed:",nruns
1697 1698 print " Times : %10s %10s" % ('Total','Per run')
1698 1699 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1699 1700 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1700 1701
1701 1702 else:
1702 1703 # regular execution
1703 1704 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1704 1705
1705 1706 if opts.has_key('i'):
1706 1707 self.shell.user_ns['__name__'] = __name__save
1707 1708 else:
1708 1709 # The shell MUST hold a reference to prog_ns so after %run
1709 1710 # exits, the python deletion mechanism doesn't zero it out
1710 1711 # (leaving dangling references).
1711 1712 self.shell.cache_main_mod(prog_ns,filename)
1712 1713 # update IPython interactive namespace
1713 1714
1714 1715 # Some forms of read errors on the file may mean the
1715 1716 # __name__ key was never set; using pop we don't have to
1716 1717 # worry about a possible KeyError.
1717 1718 prog_ns.pop('__name__', None)
1718 1719
1719 1720 self.shell.user_ns.update(prog_ns)
1720 1721 finally:
1721 1722 # It's a bit of a mystery why, but __builtins__ can change from
1722 1723 # being a module to becoming a dict missing some key data after
1723 1724 # %run. As best I can see, this is NOT something IPython is doing
1724 1725 # at all, and similar problems have been reported before:
1725 1726 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1726 1727 # Since this seems to be done by the interpreter itself, the best
1727 1728 # we can do is to at least restore __builtins__ for the user on
1728 1729 # exit.
1729 1730 self.shell.user_ns['__builtins__'] = __builtin__
1730 1731
1731 1732 # Ensure key global structures are restored
1732 1733 sys.argv = save_argv
1733 1734 if restore_main:
1734 1735 sys.modules['__main__'] = restore_main
1735 1736 else:
1736 1737 # Remove from sys.modules the reference to main_mod we'd
1737 1738 # added. Otherwise it will trap references to objects
1738 1739 # contained therein.
1739 1740 del sys.modules[main_mod_name]
1740 1741
1741 1742 self.shell.reloadhist()
1742 1743
1743 1744 return stats
1744 1745
1745 1746 def magic_runlog(self, parameter_s =''):
1746 1747 """Run files as logs.
1747 1748
1748 1749 Usage:\\
1749 1750 %runlog file1 file2 ...
1750 1751
1751 1752 Run the named files (treating them as log files) in sequence inside
1752 1753 the interpreter, and return to the prompt. This is much slower than
1753 1754 %run because each line is executed in a try/except block, but it
1754 1755 allows running files with syntax errors in them.
1755 1756
1756 1757 Normally IPython will guess when a file is one of its own logfiles, so
1757 1758 you can typically use %run even for logs. This shorthand allows you to
1758 1759 force any file to be treated as a log file."""
1759 1760
1760 1761 for f in parameter_s.split():
1761 1762 self.shell.safe_execfile(f,self.shell.user_ns,
1762 1763 self.shell.user_ns,islog=1)
1763 1764
1764 1765 @testdec.skip_doctest
1765 1766 def magic_timeit(self, parameter_s =''):
1766 1767 """Time execution of a Python statement or expression
1767 1768
1768 1769 Usage:\\
1769 1770 %timeit [-n<N> -r<R> [-t|-c]] statement
1770 1771
1771 1772 Time execution of a Python statement or expression using the timeit
1772 1773 module.
1773 1774
1774 1775 Options:
1775 1776 -n<N>: execute the given statement <N> times in a loop. If this value
1776 1777 is not given, a fitting value is chosen.
1777 1778
1778 1779 -r<R>: repeat the loop iteration <R> times and take the best result.
1779 1780 Default: 3
1780 1781
1781 1782 -t: use time.time to measure the time, which is the default on Unix.
1782 1783 This function measures wall time.
1783 1784
1784 1785 -c: use time.clock to measure the time, which is the default on
1785 1786 Windows and measures wall time. On Unix, resource.getrusage is used
1786 1787 instead and returns the CPU user time.
1787 1788
1788 1789 -p<P>: use a precision of <P> digits to display the timing result.
1789 1790 Default: 3
1790 1791
1791 1792
1792 1793 Examples:
1793 1794
1794 1795 In [1]: %timeit pass
1795 1796 10000000 loops, best of 3: 53.3 ns per loop
1796 1797
1797 1798 In [2]: u = None
1798 1799
1799 1800 In [3]: %timeit u is None
1800 1801 10000000 loops, best of 3: 184 ns per loop
1801 1802
1802 1803 In [4]: %timeit -r 4 u == None
1803 1804 1000000 loops, best of 4: 242 ns per loop
1804 1805
1805 1806 In [5]: import time
1806 1807
1807 1808 In [6]: %timeit -n1 time.sleep(2)
1808 1809 1 loops, best of 3: 2 s per loop
1809 1810
1810 1811
1811 1812 The times reported by %timeit will be slightly higher than those
1812 1813 reported by the timeit.py script when variables are accessed. This is
1813 1814 due to the fact that %timeit executes the statement in the namespace
1814 1815 of the shell, compared with timeit.py, which uses a single setup
1815 1816 statement to import function or create variables. Generally, the bias
1816 1817 does not matter as long as results from timeit.py are not mixed with
1817 1818 those from %timeit."""
1818 1819
1819 1820 import timeit
1820 1821 import math
1821 1822
1822 1823 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1823 1824 # certain terminals. Until we figure out a robust way of
1824 1825 # auto-detecting if the terminal can deal with it, use plain 'us' for
1825 1826 # microseconds. I am really NOT happy about disabling the proper
1826 1827 # 'micro' prefix, but crashing is worse... If anyone knows what the
1827 1828 # right solution for this is, I'm all ears...
1828 1829 #
1829 1830 # Note: using
1830 1831 #
1831 1832 # s = u'\xb5'
1832 1833 # s.encode(sys.getdefaultencoding())
1833 1834 #
1834 1835 # is not sufficient, as I've seen terminals where that fails but
1835 1836 # print s
1836 1837 #
1837 1838 # succeeds
1838 1839 #
1839 1840 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1840 1841
1841 1842 #units = [u"s", u"ms",u'\xb5',"ns"]
1842 1843 units = [u"s", u"ms",u'us',"ns"]
1843 1844
1844 1845 scaling = [1, 1e3, 1e6, 1e9]
1845 1846
1846 1847 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1847 1848 posix=False)
1848 1849 if stmt == "":
1849 1850 return
1850 1851 timefunc = timeit.default_timer
1851 1852 number = int(getattr(opts, "n", 0))
1852 1853 repeat = int(getattr(opts, "r", timeit.default_repeat))
1853 1854 precision = int(getattr(opts, "p", 3))
1854 1855 if hasattr(opts, "t"):
1855 1856 timefunc = time.time
1856 1857 if hasattr(opts, "c"):
1857 1858 timefunc = clock
1858 1859
1859 1860 timer = timeit.Timer(timer=timefunc)
1860 1861 # this code has tight coupling to the inner workings of timeit.Timer,
1861 1862 # but is there a better way to achieve that the code stmt has access
1862 1863 # to the shell namespace?
1863 1864
1864 1865 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1865 1866 'setup': "pass"}
1866 1867 # Track compilation time so it can be reported if too long
1867 1868 # Minimum time above which compilation time will be reported
1868 1869 tc_min = 0.1
1869 1870
1870 1871 t0 = clock()
1871 1872 code = compile(src, "<magic-timeit>", "exec")
1872 1873 tc = clock()-t0
1873 1874
1874 1875 ns = {}
1875 1876 exec code in self.shell.user_ns, ns
1876 1877 timer.inner = ns["inner"]
1877 1878
1878 1879 if number == 0:
1879 1880 # determine number so that 0.2 <= total time < 2.0
1880 1881 number = 1
1881 1882 for i in range(1, 10):
1882 1883 if timer.timeit(number) >= 0.2:
1883 1884 break
1884 1885 number *= 10
1885 1886
1886 1887 best = min(timer.repeat(repeat, number)) / number
1887 1888
1888 1889 if best > 0.0:
1889 1890 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1890 1891 else:
1891 1892 order = 3
1892 1893 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1893 1894 precision,
1894 1895 best * scaling[order],
1895 1896 units[order])
1896 1897 if tc > tc_min:
1897 1898 print "Compiler time: %.2f s" % tc
1898 1899
1899 1900 @testdec.skip_doctest
1900 1901 def magic_time(self,parameter_s = ''):
1901 1902 """Time execution of a Python statement or expression.
1902 1903
1903 1904 The CPU and wall clock times are printed, and the value of the
1904 1905 expression (if any) is returned. Note that under Win32, system time
1905 1906 is always reported as 0, since it can not be measured.
1906 1907
1907 1908 This function provides very basic timing functionality. In Python
1908 1909 2.3, the timeit module offers more control and sophistication, so this
1909 1910 could be rewritten to use it (patches welcome).
1910 1911
1911 1912 Some examples:
1912 1913
1913 1914 In [1]: time 2**128
1914 1915 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1915 1916 Wall time: 0.00
1916 1917 Out[1]: 340282366920938463463374607431768211456L
1917 1918
1918 1919 In [2]: n = 1000000
1919 1920
1920 1921 In [3]: time sum(range(n))
1921 1922 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1922 1923 Wall time: 1.37
1923 1924 Out[3]: 499999500000L
1924 1925
1925 1926 In [4]: time print 'hello world'
1926 1927 hello world
1927 1928 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1928 1929 Wall time: 0.00
1929 1930
1930 1931 Note that the time needed by Python to compile the given expression
1931 1932 will be reported if it is more than 0.1s. In this example, the
1932 1933 actual exponentiation is done by Python at compilation time, so while
1933 1934 the expression can take a noticeable amount of time to compute, that
1934 1935 time is purely due to the compilation:
1935 1936
1936 1937 In [5]: time 3**9999;
1937 1938 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1938 1939 Wall time: 0.00 s
1939 1940
1940 1941 In [6]: time 3**999999;
1941 1942 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1942 1943 Wall time: 0.00 s
1943 1944 Compiler : 0.78 s
1944 1945 """
1945 1946
1946 1947 # fail immediately if the given expression can't be compiled
1947 1948
1948 1949 expr = self.shell.prefilter(parameter_s,False)
1949 1950
1950 1951 # Minimum time above which compilation time will be reported
1951 1952 tc_min = 0.1
1952 1953
1953 1954 try:
1954 1955 mode = 'eval'
1955 1956 t0 = clock()
1956 1957 code = compile(expr,'<timed eval>',mode)
1957 1958 tc = clock()-t0
1958 1959 except SyntaxError:
1959 1960 mode = 'exec'
1960 1961 t0 = clock()
1961 1962 code = compile(expr,'<timed exec>',mode)
1962 1963 tc = clock()-t0
1963 1964 # skew measurement as little as possible
1964 1965 glob = self.shell.user_ns
1965 1966 clk = clock2
1966 1967 wtime = time.time
1967 1968 # time execution
1968 1969 wall_st = wtime()
1969 1970 if mode=='eval':
1970 1971 st = clk()
1971 1972 out = eval(code,glob)
1972 1973 end = clk()
1973 1974 else:
1974 1975 st = clk()
1975 1976 exec code in glob
1976 1977 end = clk()
1977 1978 out = None
1978 1979 wall_end = wtime()
1979 1980 # Compute actual times and report
1980 1981 wall_time = wall_end-wall_st
1981 1982 cpu_user = end[0]-st[0]
1982 1983 cpu_sys = end[1]-st[1]
1983 1984 cpu_tot = cpu_user+cpu_sys
1984 1985 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1985 1986 (cpu_user,cpu_sys,cpu_tot)
1986 1987 print "Wall time: %.2f s" % wall_time
1987 1988 if tc > tc_min:
1988 1989 print "Compiler : %.2f s" % tc
1989 1990 return out
1990 1991
1991 1992 @testdec.skip_doctest
1992 1993 def magic_macro(self,parameter_s = ''):
1993 1994 """Define a set of input lines as a macro for future re-execution.
1994 1995
1995 1996 Usage:\\
1996 1997 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1997 1998
1998 1999 Options:
1999 2000
2000 2001 -r: use 'raw' input. By default, the 'processed' history is used,
2001 2002 so that magics are loaded in their transformed version to valid
2002 2003 Python. If this option is given, the raw input as typed as the
2003 2004 command line is used instead.
2004 2005
2005 2006 This will define a global variable called `name` which is a string
2006 2007 made of joining the slices and lines you specify (n1,n2,... numbers
2007 2008 above) from your input history into a single string. This variable
2008 2009 acts like an automatic function which re-executes those lines as if
2009 2010 you had typed them. You just type 'name' at the prompt and the code
2010 2011 executes.
2011 2012
2012 2013 The notation for indicating number ranges is: n1-n2 means 'use line
2013 2014 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2014 2015 using the lines numbered 5,6 and 7.
2015 2016
2016 2017 Note: as a 'hidden' feature, you can also use traditional python slice
2017 2018 notation, where N:M means numbers N through M-1.
2018 2019
2019 2020 For example, if your history contains (%hist prints it):
2020 2021
2021 2022 44: x=1
2022 2023 45: y=3
2023 2024 46: z=x+y
2024 2025 47: print x
2025 2026 48: a=5
2026 2027 49: print 'x',x,'y',y
2027 2028
2028 2029 you can create a macro with lines 44 through 47 (included) and line 49
2029 2030 called my_macro with:
2030 2031
2031 2032 In [55]: %macro my_macro 44-47 49
2032 2033
2033 2034 Now, typing `my_macro` (without quotes) will re-execute all this code
2034 2035 in one pass.
2035 2036
2036 2037 You don't need to give the line-numbers in order, and any given line
2037 2038 number can appear multiple times. You can assemble macros with any
2038 2039 lines from your input history in any order.
2039 2040
2040 2041 The macro is a simple object which holds its value in an attribute,
2041 2042 but IPython's display system checks for macros and executes them as
2042 2043 code instead of printing them when you type their name.
2043 2044
2044 2045 You can view a macro's contents by explicitly printing it with:
2045 2046
2046 2047 'print macro_name'.
2047 2048
2048 2049 For one-off cases which DON'T contain magic function calls in them you
2049 2050 can obtain similar results by explicitly executing slices from your
2050 2051 input history with:
2051 2052
2052 2053 In [60]: exec In[44:48]+In[49]"""
2053 2054
2054 2055 opts,args = self.parse_options(parameter_s,'r',mode='list')
2055 2056 if not args:
2056 2057 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2057 2058 macs.sort()
2058 2059 return macs
2059 2060 if len(args) == 1:
2060 2061 raise UsageError(
2061 2062 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2062 2063 name,ranges = args[0], args[1:]
2063 2064
2064 2065 #print 'rng',ranges # dbg
2065 2066 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2066 2067 macro = Macro(lines)
2067 2068 self.shell.define_macro(name, macro)
2068 2069 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2069 2070 print 'Macro contents:'
2070 2071 print macro,
2071 2072
2072 2073 def magic_save(self,parameter_s = ''):
2073 2074 """Save a set of lines to a given filename.
2074 2075
2075 2076 Usage:\\
2076 2077 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2077 2078
2078 2079 Options:
2079 2080
2080 2081 -r: use 'raw' input. By default, the 'processed' history is used,
2081 2082 so that magics are loaded in their transformed version to valid
2082 2083 Python. If this option is given, the raw input as typed as the
2083 2084 command line is used instead.
2084 2085
2085 2086 This function uses the same syntax as %macro for line extraction, but
2086 2087 instead of creating a macro it saves the resulting string to the
2087 2088 filename you specify.
2088 2089
2089 2090 It adds a '.py' extension to the file if you don't do so yourself, and
2090 2091 it asks for confirmation before overwriting existing files."""
2091 2092
2092 2093 opts,args = self.parse_options(parameter_s,'r',mode='list')
2093 2094 fname,ranges = args[0], args[1:]
2094 2095 if not fname.endswith('.py'):
2095 2096 fname += '.py'
2096 2097 if os.path.isfile(fname):
2097 2098 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2098 2099 if ans.lower() not in ['y','yes']:
2099 2100 print 'Operation cancelled.'
2100 2101 return
2101 2102 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2102 2103 f = file(fname,'w')
2103 2104 f.write(cmds)
2104 2105 f.close()
2105 2106 print 'The following commands were written to file `%s`:' % fname
2106 2107 print cmds
2107 2108
2108 2109 def _edit_macro(self,mname,macro):
2109 2110 """open an editor with the macro data in a file"""
2110 2111 filename = self.shell.mktempfile(macro.value)
2111 2112 self.shell.hooks.editor(filename)
2112 2113
2113 2114 # and make a new macro object, to replace the old one
2114 2115 mfile = open(filename)
2115 2116 mvalue = mfile.read()
2116 2117 mfile.close()
2117 2118 self.shell.user_ns[mname] = Macro(mvalue)
2118 2119
2119 2120 def magic_ed(self,parameter_s=''):
2120 2121 """Alias to %edit."""
2121 2122 return self.magic_edit(parameter_s)
2122 2123
2123 2124 @testdec.skip_doctest
2124 2125 def magic_edit(self,parameter_s='',last_call=['','']):
2125 2126 """Bring up an editor and execute the resulting code.
2126 2127
2127 2128 Usage:
2128 2129 %edit [options] [args]
2129 2130
2130 2131 %edit runs IPython's editor hook. The default version of this hook is
2131 2132 set to call the __IPYTHON__.rc.editor command. This is read from your
2132 2133 environment variable $EDITOR. If this isn't found, it will default to
2133 2134 vi under Linux/Unix and to notepad under Windows. See the end of this
2134 2135 docstring for how to change the editor hook.
2135 2136
2136 2137 You can also set the value of this editor via the command line option
2137 2138 '-editor' or in your ipythonrc file. This is useful if you wish to use
2138 2139 specifically for IPython an editor different from your typical default
2139 2140 (and for Windows users who typically don't set environment variables).
2140 2141
2141 2142 This command allows you to conveniently edit multi-line code right in
2142 2143 your IPython session.
2143 2144
2144 2145 If called without arguments, %edit opens up an empty editor with a
2145 2146 temporary file and will execute the contents of this file when you
2146 2147 close it (don't forget to save it!).
2147 2148
2148 2149
2149 2150 Options:
2150 2151
2151 2152 -n <number>: open the editor at a specified line number. By default,
2152 2153 the IPython editor hook uses the unix syntax 'editor +N filename', but
2153 2154 you can configure this by providing your own modified hook if your
2154 2155 favorite editor supports line-number specifications with a different
2155 2156 syntax.
2156 2157
2157 2158 -p: this will call the editor with the same data as the previous time
2158 2159 it was used, regardless of how long ago (in your current session) it
2159 2160 was.
2160 2161
2161 2162 -r: use 'raw' input. This option only applies to input taken from the
2162 2163 user's history. By default, the 'processed' history is used, so that
2163 2164 magics are loaded in their transformed version to valid Python. If
2164 2165 this option is given, the raw input as typed as the command line is
2165 2166 used instead. When you exit the editor, it will be executed by
2166 2167 IPython's own processor.
2167 2168
2168 2169 -x: do not execute the edited code immediately upon exit. This is
2169 2170 mainly useful if you are editing programs which need to be called with
2170 2171 command line arguments, which you can then do using %run.
2171 2172
2172 2173
2173 2174 Arguments:
2174 2175
2175 2176 If arguments are given, the following possibilites exist:
2176 2177
2177 2178 - The arguments are numbers or pairs of colon-separated numbers (like
2178 2179 1 4:8 9). These are interpreted as lines of previous input to be
2179 2180 loaded into the editor. The syntax is the same of the %macro command.
2180 2181
2181 2182 - If the argument doesn't start with a number, it is evaluated as a
2182 2183 variable and its contents loaded into the editor. You can thus edit
2183 2184 any string which contains python code (including the result of
2184 2185 previous edits).
2185 2186
2186 2187 - If the argument is the name of an object (other than a string),
2187 2188 IPython will try to locate the file where it was defined and open the
2188 2189 editor at the point where it is defined. You can use `%edit function`
2189 2190 to load an editor exactly at the point where 'function' is defined,
2190 2191 edit it and have the file be executed automatically.
2191 2192
2192 2193 If the object is a macro (see %macro for details), this opens up your
2193 2194 specified editor with a temporary file containing the macro's data.
2194 2195 Upon exit, the macro is reloaded with the contents of the file.
2195 2196
2196 2197 Note: opening at an exact line is only supported under Unix, and some
2197 2198 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2198 2199 '+NUMBER' parameter necessary for this feature. Good editors like
2199 2200 (X)Emacs, vi, jed, pico and joe all do.
2200 2201
2201 2202 - If the argument is not found as a variable, IPython will look for a
2202 2203 file with that name (adding .py if necessary) and load it into the
2203 2204 editor. It will execute its contents with execfile() when you exit,
2204 2205 loading any code in the file into your interactive namespace.
2205 2206
2206 2207 After executing your code, %edit will return as output the code you
2207 2208 typed in the editor (except when it was an existing file). This way
2208 2209 you can reload the code in further invocations of %edit as a variable,
2209 2210 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2210 2211 the output.
2211 2212
2212 2213 Note that %edit is also available through the alias %ed.
2213 2214
2214 2215 This is an example of creating a simple function inside the editor and
2215 2216 then modifying it. First, start up the editor:
2216 2217
2217 2218 In [1]: ed
2218 2219 Editing... done. Executing edited code...
2219 2220 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2220 2221
2221 2222 We can then call the function foo():
2222 2223
2223 2224 In [2]: foo()
2224 2225 foo() was defined in an editing session
2225 2226
2226 2227 Now we edit foo. IPython automatically loads the editor with the
2227 2228 (temporary) file where foo() was previously defined:
2228 2229
2229 2230 In [3]: ed foo
2230 2231 Editing... done. Executing edited code...
2231 2232
2232 2233 And if we call foo() again we get the modified version:
2233 2234
2234 2235 In [4]: foo()
2235 2236 foo() has now been changed!
2236 2237
2237 2238 Here is an example of how to edit a code snippet successive
2238 2239 times. First we call the editor:
2239 2240
2240 2241 In [5]: ed
2241 2242 Editing... done. Executing edited code...
2242 2243 hello
2243 2244 Out[5]: "print 'hello'n"
2244 2245
2245 2246 Now we call it again with the previous output (stored in _):
2246 2247
2247 2248 In [6]: ed _
2248 2249 Editing... done. Executing edited code...
2249 2250 hello world
2250 2251 Out[6]: "print 'hello world'n"
2251 2252
2252 2253 Now we call it with the output #8 (stored in _8, also as Out[8]):
2253 2254
2254 2255 In [7]: ed _8
2255 2256 Editing... done. Executing edited code...
2256 2257 hello again
2257 2258 Out[7]: "print 'hello again'n"
2258 2259
2259 2260
2260 2261 Changing the default editor hook:
2261 2262
2262 2263 If you wish to write your own editor hook, you can put it in a
2263 2264 configuration file which you load at startup time. The default hook
2264 2265 is defined in the IPython.core.hooks module, and you can use that as a
2265 2266 starting example for further modifications. That file also has
2266 2267 general instructions on how to set a new hook for use once you've
2267 2268 defined it."""
2268 2269
2269 2270 # FIXME: This function has become a convoluted mess. It needs a
2270 2271 # ground-up rewrite with clean, simple logic.
2271 2272
2272 2273 def make_filename(arg):
2273 2274 "Make a filename from the given args"
2274 2275 try:
2275 2276 filename = get_py_filename(arg)
2276 2277 except IOError:
2277 2278 if args.endswith('.py'):
2278 2279 filename = arg
2279 2280 else:
2280 2281 filename = None
2281 2282 return filename
2282 2283
2283 2284 # custom exceptions
2284 2285 class DataIsObject(Exception): pass
2285 2286
2286 2287 opts,args = self.parse_options(parameter_s,'prxn:')
2287 2288 # Set a few locals from the options for convenience:
2288 2289 opts_p = opts.has_key('p')
2289 2290 opts_r = opts.has_key('r')
2290 2291
2291 2292 # Default line number value
2292 2293 lineno = opts.get('n',None)
2293 2294
2294 2295 if opts_p:
2295 2296 args = '_%s' % last_call[0]
2296 2297 if not self.shell.user_ns.has_key(args):
2297 2298 args = last_call[1]
2298 2299
2299 2300 # use last_call to remember the state of the previous call, but don't
2300 2301 # let it be clobbered by successive '-p' calls.
2301 2302 try:
2302 2303 last_call[0] = self.shell.outputcache.prompt_count
2303 2304 if not opts_p:
2304 2305 last_call[1] = parameter_s
2305 2306 except:
2306 2307 pass
2307 2308
2308 2309 # by default this is done with temp files, except when the given
2309 2310 # arg is a filename
2310 2311 use_temp = 1
2311 2312
2312 2313 if re.match(r'\d',args):
2313 2314 # Mode where user specifies ranges of lines, like in %macro.
2314 2315 # This means that you can't edit files whose names begin with
2315 2316 # numbers this way. Tough.
2316 2317 ranges = args.split()
2317 2318 data = ''.join(self.extract_input_slices(ranges,opts_r))
2318 2319 elif args.endswith('.py'):
2319 2320 filename = make_filename(args)
2320 2321 data = ''
2321 2322 use_temp = 0
2322 2323 elif args:
2323 2324 try:
2324 2325 # Load the parameter given as a variable. If not a string,
2325 2326 # process it as an object instead (below)
2326 2327
2327 2328 #print '*** args',args,'type',type(args) # dbg
2328 2329 data = eval(args,self.shell.user_ns)
2329 2330 if not type(data) in StringTypes:
2330 2331 raise DataIsObject
2331 2332
2332 2333 except (NameError,SyntaxError):
2333 2334 # given argument is not a variable, try as a filename
2334 2335 filename = make_filename(args)
2335 2336 if filename is None:
2336 2337 warn("Argument given (%s) can't be found as a variable "
2337 2338 "or as a filename." % args)
2338 2339 return
2339 2340
2340 2341 data = ''
2341 2342 use_temp = 0
2342 2343 except DataIsObject:
2343 2344
2344 2345 # macros have a special edit function
2345 2346 if isinstance(data,Macro):
2346 2347 self._edit_macro(args,data)
2347 2348 return
2348 2349
2349 2350 # For objects, try to edit the file where they are defined
2350 2351 try:
2351 2352 filename = inspect.getabsfile(data)
2352 2353 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2353 2354 # class created by %edit? Try to find source
2354 2355 # by looking for method definitions instead, the
2355 2356 # __module__ in those classes is FakeModule.
2356 2357 attrs = [getattr(data, aname) for aname in dir(data)]
2357 2358 for attr in attrs:
2358 2359 if not inspect.ismethod(attr):
2359 2360 continue
2360 2361 filename = inspect.getabsfile(attr)
2361 2362 if filename and 'fakemodule' not in filename.lower():
2362 2363 # change the attribute to be the edit target instead
2363 2364 data = attr
2364 2365 break
2365 2366
2366 2367 datafile = 1
2367 2368 except TypeError:
2368 2369 filename = make_filename(args)
2369 2370 datafile = 1
2370 2371 warn('Could not find file where `%s` is defined.\n'
2371 2372 'Opening a file named `%s`' % (args,filename))
2372 2373 # Now, make sure we can actually read the source (if it was in
2373 2374 # a temp file it's gone by now).
2374 2375 if datafile:
2375 2376 try:
2376 2377 if lineno is None:
2377 2378 lineno = inspect.getsourcelines(data)[1]
2378 2379 except IOError:
2379 2380 filename = make_filename(args)
2380 2381 if filename is None:
2381 2382 warn('The file `%s` where `%s` was defined cannot '
2382 2383 'be read.' % (filename,data))
2383 2384 return
2384 2385 use_temp = 0
2385 2386 else:
2386 2387 data = ''
2387 2388
2388 2389 if use_temp:
2389 2390 filename = self.shell.mktempfile(data)
2390 2391 print 'IPython will make a temporary file named:',filename
2391 2392
2392 2393 # do actual editing here
2393 2394 print 'Editing...',
2394 2395 sys.stdout.flush()
2395 2396 try:
2396 2397 self.shell.hooks.editor(filename,lineno)
2397 2398 except TryNext:
2398 2399 warn('Could not open editor')
2399 2400 return
2400 2401
2401 2402 # XXX TODO: should this be generalized for all string vars?
2402 2403 # For now, this is special-cased to blocks created by cpaste
2403 2404 if args.strip() == 'pasted_block':
2404 2405 self.shell.user_ns['pasted_block'] = file_read(filename)
2405 2406
2406 2407 if opts.has_key('x'): # -x prevents actual execution
2407 2408 print
2408 2409 else:
2409 2410 print 'done. Executing edited code...'
2410 2411 if opts_r:
2411 2412 self.shell.runlines(file_read(filename))
2412 2413 else:
2413 2414 self.shell.safe_execfile(filename,self.shell.user_ns,
2414 2415 self.shell.user_ns)
2415 2416
2416 2417
2417 2418 if use_temp:
2418 2419 try:
2419 2420 return open(filename).read()
2420 2421 except IOError,msg:
2421 2422 if msg.filename == filename:
2422 2423 warn('File not found. Did you forget to save?')
2423 2424 return
2424 2425 else:
2425 2426 self.shell.showtraceback()
2426 2427
2427 2428 def magic_xmode(self,parameter_s = ''):
2428 2429 """Switch modes for the exception handlers.
2429 2430
2430 2431 Valid modes: Plain, Context and Verbose.
2431 2432
2432 2433 If called without arguments, acts as a toggle."""
2433 2434
2434 2435 def xmode_switch_err(name):
2435 2436 warn('Error changing %s exception modes.\n%s' %
2436 2437 (name,sys.exc_info()[1]))
2437 2438
2438 2439 shell = self.shell
2439 2440 new_mode = parameter_s.strip().capitalize()
2440 2441 try:
2441 2442 shell.InteractiveTB.set_mode(mode=new_mode)
2442 2443 print 'Exception reporting mode:',shell.InteractiveTB.mode
2443 2444 except:
2444 2445 xmode_switch_err('user')
2445 2446
2446 2447 # threaded shells use a special handler in sys.excepthook
2447 2448 if shell.isthreaded:
2448 2449 try:
2449 2450 shell.sys_excepthook.set_mode(mode=new_mode)
2450 2451 except:
2451 2452 xmode_switch_err('threaded')
2452 2453
2453 2454 def magic_colors(self,parameter_s = ''):
2454 2455 """Switch color scheme for prompts, info system and exception handlers.
2455 2456
2456 2457 Currently implemented schemes: NoColor, Linux, LightBG.
2457 2458
2458 2459 Color scheme names are not case-sensitive."""
2459 2460
2460 2461 def color_switch_err(name):
2461 2462 warn('Error changing %s color schemes.\n%s' %
2462 2463 (name,sys.exc_info()[1]))
2463 2464
2464 2465
2465 2466 new_scheme = parameter_s.strip()
2466 2467 if not new_scheme:
2467 2468 raise UsageError(
2468 2469 "%colors: you must specify a color scheme. See '%colors?'")
2469 2470 return
2470 2471 # local shortcut
2471 2472 shell = self.shell
2472 2473
2473 2474 import IPython.utils.rlineimpl as readline
2474 2475
2475 2476 if not readline.have_readline and sys.platform == "win32":
2476 2477 msg = """\
2477 2478 Proper color support under MS Windows requires the pyreadline library.
2478 2479 You can find it at:
2479 2480 http://ipython.scipy.org/moin/PyReadline/Intro
2480 2481 Gary's readline needs the ctypes module, from:
2481 2482 http://starship.python.net/crew/theller/ctypes
2482 2483 (Note that ctypes is already part of Python versions 2.5 and newer).
2483 2484
2484 2485 Defaulting color scheme to 'NoColor'"""
2485 2486 new_scheme = 'NoColor'
2486 2487 warn(msg)
2487 2488
2488 2489 # readline option is 0
2489 2490 if not shell.has_readline:
2490 2491 new_scheme = 'NoColor'
2491 2492
2492 2493 # Set prompt colors
2493 2494 try:
2494 2495 shell.outputcache.set_colors(new_scheme)
2495 2496 except:
2496 2497 color_switch_err('prompt')
2497 2498 else:
2498 2499 shell.colors = \
2499 2500 shell.outputcache.color_table.active_scheme_name
2500 2501 # Set exception colors
2501 2502 try:
2502 2503 shell.InteractiveTB.set_colors(scheme = new_scheme)
2503 2504 shell.SyntaxTB.set_colors(scheme = new_scheme)
2504 2505 except:
2505 2506 color_switch_err('exception')
2506 2507
2507 2508 # threaded shells use a verbose traceback in sys.excepthook
2508 2509 if shell.isthreaded:
2509 2510 try:
2510 2511 shell.sys_excepthook.set_colors(scheme=new_scheme)
2511 2512 except:
2512 2513 color_switch_err('system exception handler')
2513 2514
2514 2515 # Set info (for 'object?') colors
2515 2516 if shell.color_info:
2516 2517 try:
2517 2518 shell.inspector.set_active_scheme(new_scheme)
2518 2519 except:
2519 2520 color_switch_err('object inspector')
2520 2521 else:
2521 2522 shell.inspector.set_active_scheme('NoColor')
2522 2523
2523 2524 def magic_color_info(self,parameter_s = ''):
2524 2525 """Toggle color_info.
2525 2526
2526 2527 The color_info configuration parameter controls whether colors are
2527 2528 used for displaying object details (by things like %psource, %pfile or
2528 2529 the '?' system). This function toggles this value with each call.
2529 2530
2530 2531 Note that unless you have a fairly recent pager (less works better
2531 2532 than more) in your system, using colored object information displays
2532 2533 will not work properly. Test it and see."""
2533 2534
2534 2535 self.shell.color_info = not self.shell.color_info
2535 2536 self.magic_colors(self.shell.colors)
2536 2537 print 'Object introspection functions have now coloring:',
2537 2538 print ['OFF','ON'][int(self.shell.color_info)]
2538 2539
2539 2540 def magic_Pprint(self, parameter_s=''):
2540 2541 """Toggle pretty printing on/off."""
2541 2542
2542 2543 self.shell.pprint = 1 - self.shell.pprint
2543 2544 print 'Pretty printing has been turned', \
2544 2545 ['OFF','ON'][self.shell.pprint]
2545 2546
2546 2547 def magic_exit(self, parameter_s=''):
2547 2548 """Exit IPython, confirming if configured to do so.
2548 2549
2549 2550 You can configure whether IPython asks for confirmation upon exit by
2550 2551 setting the confirm_exit flag in the ipythonrc file."""
2551 2552
2552 2553 self.shell.exit()
2553 2554
2554 2555 def magic_quit(self, parameter_s=''):
2555 2556 """Exit IPython, confirming if configured to do so (like %exit)"""
2556 2557
2557 2558 self.shell.exit()
2558 2559
2559 2560 def magic_Exit(self, parameter_s=''):
2560 2561 """Exit IPython without confirmation."""
2561 2562
2562 2563 self.shell.ask_exit()
2563 2564
2564 2565 #......................................................................
2565 2566 # Functions to implement unix shell-type things
2566 2567
2567 2568 @testdec.skip_doctest
2568 2569 def magic_alias(self, parameter_s = ''):
2569 2570 """Define an alias for a system command.
2570 2571
2571 2572 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2572 2573
2573 2574 Then, typing 'alias_name params' will execute the system command 'cmd
2574 2575 params' (from your underlying operating system).
2575 2576
2576 2577 Aliases have lower precedence than magic functions and Python normal
2577 2578 variables, so if 'foo' is both a Python variable and an alias, the
2578 2579 alias can not be executed until 'del foo' removes the Python variable.
2579 2580
2580 2581 You can use the %l specifier in an alias definition to represent the
2581 2582 whole line when the alias is called. For example:
2582 2583
2583 2584 In [2]: alias all echo "Input in brackets: <%l>"
2584 2585 In [3]: all hello world
2585 2586 Input in brackets: <hello world>
2586 2587
2587 2588 You can also define aliases with parameters using %s specifiers (one
2588 2589 per parameter):
2589 2590
2590 2591 In [1]: alias parts echo first %s second %s
2591 2592 In [2]: %parts A B
2592 2593 first A second B
2593 2594 In [3]: %parts A
2594 2595 Incorrect number of arguments: 2 expected.
2595 2596 parts is an alias to: 'echo first %s second %s'
2596 2597
2597 2598 Note that %l and %s are mutually exclusive. You can only use one or
2598 2599 the other in your aliases.
2599 2600
2600 2601 Aliases expand Python variables just like system calls using ! or !!
2601 2602 do: all expressions prefixed with '$' get expanded. For details of
2602 2603 the semantic rules, see PEP-215:
2603 2604 http://www.python.org/peps/pep-0215.html. This is the library used by
2604 2605 IPython for variable expansion. If you want to access a true shell
2605 2606 variable, an extra $ is necessary to prevent its expansion by IPython:
2606 2607
2607 2608 In [6]: alias show echo
2608 2609 In [7]: PATH='A Python string'
2609 2610 In [8]: show $PATH
2610 2611 A Python string
2611 2612 In [9]: show $$PATH
2612 2613 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2613 2614
2614 2615 You can use the alias facility to acess all of $PATH. See the %rehash
2615 2616 and %rehashx functions, which automatically create aliases for the
2616 2617 contents of your $PATH.
2617 2618
2618 2619 If called with no parameters, %alias prints the current alias table."""
2619 2620
2620 2621 par = parameter_s.strip()
2621 2622 if not par:
2622 2623 stored = self.db.get('stored_aliases', {} )
2623 atab = self.shell.alias_table
2624 aliases = atab.keys()
2625 aliases.sort()
2626 res = []
2627 showlast = []
2628 for alias in aliases:
2629 special = False
2630 try:
2631 tgt = atab[alias][1]
2632 except (TypeError, AttributeError):
2633 # unsubscriptable? probably a callable
2634 tgt = atab[alias]
2635 special = True
2636 # 'interesting' aliases
2637 if (alias in stored or
2638 special or
2639 alias.lower() != os.path.splitext(tgt)[0].lower() or
2640 ' ' in tgt):
2641 showlast.append((alias, tgt))
2642 else:
2643 res.append((alias, tgt ))
2644
2645 # show most interesting aliases last
2646 res.extend(showlast)
2624 aliases = sorted(self.shell.alias_manager.aliases)
2625 # for k, v in stored:
2626 # atab.append(k, v[0])
2627
2647 2628 print "Total number of aliases:",len(aliases)
2648 return res
2629 return aliases
2630
2631 # Now try to define a new one
2649 2632 try:
2650 2633 alias,cmd = par.split(None,1)
2651 2634 except:
2652 2635 print oinspect.getdoc(self.magic_alias)
2653 2636 else:
2654 nargs = cmd.count('%s')
2655 if nargs>0 and cmd.find('%l')>=0:
2656 error('The %s and %l specifiers are mutually exclusive '
2657 'in alias definitions.')
2658 else: # all looks OK
2659 self.shell.alias_table[alias] = (nargs,cmd)
2660 self.shell.alias_table_validate(verbose=0)
2637 self.shell.alias_manager.soft_define_alias(alias, cmd)
2661 2638 # end magic_alias
2662 2639
2663 2640 def magic_unalias(self, parameter_s = ''):
2664 2641 """Remove an alias"""
2665 2642
2666 2643 aname = parameter_s.strip()
2667 if aname in self.shell.alias_table:
2668 del self.shell.alias_table[aname]
2644 self.shell.alias_manager.undefine_alias(aname)
2669 2645 stored = self.db.get('stored_aliases', {} )
2670 2646 if aname in stored:
2671 2647 print "Removing %stored alias",aname
2672 2648 del stored[aname]
2673 2649 self.db['stored_aliases'] = stored
2674 2650
2675 2651
2676 2652 def magic_rehashx(self, parameter_s = ''):
2677 2653 """Update the alias table with all executable files in $PATH.
2678 2654
2679 2655 This version explicitly checks that every entry in $PATH is a file
2680 2656 with execute access (os.X_OK), so it is much slower than %rehash.
2681 2657
2682 2658 Under Windows, it checks executability as a match agains a
2683 2659 '|'-separated string of extensions, stored in the IPython config
2684 2660 variable win_exec_ext. This defaults to 'exe|com|bat'.
2685 2661
2686 2662 This function also resets the root module cache of module completer,
2687 2663 used on slow filesystems.
2688 2664 """
2665 from IPython.core.alias import InvalidAliasError
2689 2666
2690 2667 # for the benefit of module completer in ipy_completers.py
2691 2668 del self.db['rootmodules']
2692 2669
2693 2670 path = [os.path.abspath(os.path.expanduser(p)) for p in
2694 2671 os.environ.get('PATH','').split(os.pathsep)]
2695 2672 path = filter(os.path.isdir,path)
2696 2673
2697 alias_table = self.shell.alias_table
2698 2674 syscmdlist = []
2675 # Now define isexec in a cross platform manner.
2699 2676 if os.name == 'posix':
2700 2677 isexec = lambda fname:os.path.isfile(fname) and \
2701 2678 os.access(fname,os.X_OK)
2702 2679 else:
2703
2704 2680 try:
2705 2681 winext = os.environ['pathext'].replace(';','|').replace('.','')
2706 2682 except KeyError:
2707 2683 winext = 'exe|com|bat|py'
2708 2684 if 'py' not in winext:
2709 2685 winext += '|py'
2710 2686 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2711 2687 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2712 2688 savedir = os.getcwd()
2689
2690 # Now walk the paths looking for executables to alias.
2713 2691 try:
2714 2692 # write the whole loop for posix/Windows so we don't have an if in
2715 2693 # the innermost part
2716 2694 if os.name == 'posix':
2717 2695 for pdir in path:
2718 2696 os.chdir(pdir)
2719 2697 for ff in os.listdir(pdir):
2720 if isexec(ff) and ff not in self.shell.no_alias:
2721 # each entry in the alias table must be (N,name),
2722 # where N is the number of positional arguments of the
2723 # alias.
2724 # Dots will be removed from alias names, since ipython
2725 # assumes names with dots to be python code
2726 alias_table[ff.replace('.','')] = (0,ff)
2698 if isexec(ff):
2699 try:
2700 # Removes dots from the name since ipython
2701 # will assume names with dots to be python.
2702 self.shell.alias_manager.define_alias(
2703 ff.replace('.',''), ff)
2704 except InvalidAliasError:
2705 pass
2706 else:
2727 2707 syscmdlist.append(ff)
2728 2708 else:
2729 2709 for pdir in path:
2730 2710 os.chdir(pdir)
2731 2711 for ff in os.listdir(pdir):
2732 2712 base, ext = os.path.splitext(ff)
2733 2713 if isexec(ff) and base.lower() not in self.shell.no_alias:
2734 2714 if ext.lower() == '.exe':
2735 2715 ff = base
2736 alias_table[base.lower().replace('.','')] = (0,ff)
2716 try:
2717 # Removes dots from the name since ipython
2718 # will assume names with dots to be python.
2719 self.shell.alias_manager.define_alias(
2720 base.lower().replace('.',''), ff)
2721 except InvalidAliasError:
2722 pass
2737 2723 syscmdlist.append(ff)
2738 # Make sure the alias table doesn't contain keywords or builtins
2739 self.shell.alias_table_validate()
2740 # Call again init_auto_alias() so we get 'rm -i' and other
2741 # modified aliases since %rehashx will probably clobber them
2742
2743 # no, we don't want them. if %rehashx clobbers them, good,
2744 # we'll probably get better versions
2745 # self.shell.init_auto_alias()
2746 2724 db = self.db
2747 2725 db['syscmdlist'] = syscmdlist
2748 2726 finally:
2749 2727 os.chdir(savedir)
2750 2728
2751 2729 def magic_pwd(self, parameter_s = ''):
2752 2730 """Return the current working directory path."""
2753 2731 return os.getcwd()
2754 2732
2755 2733 def magic_cd(self, parameter_s=''):
2756 2734 """Change the current working directory.
2757 2735
2758 2736 This command automatically maintains an internal list of directories
2759 2737 you visit during your IPython session, in the variable _dh. The
2760 2738 command %dhist shows this history nicely formatted. You can also
2761 2739 do 'cd -<tab>' to see directory history conveniently.
2762 2740
2763 2741 Usage:
2764 2742
2765 2743 cd 'dir': changes to directory 'dir'.
2766 2744
2767 2745 cd -: changes to the last visited directory.
2768 2746
2769 2747 cd -<n>: changes to the n-th directory in the directory history.
2770 2748
2771 2749 cd --foo: change to directory that matches 'foo' in history
2772 2750
2773 2751 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2774 2752 (note: cd <bookmark_name> is enough if there is no
2775 2753 directory <bookmark_name>, but a bookmark with the name exists.)
2776 2754 'cd -b <tab>' allows you to tab-complete bookmark names.
2777 2755
2778 2756 Options:
2779 2757
2780 2758 -q: quiet. Do not print the working directory after the cd command is
2781 2759 executed. By default IPython's cd command does print this directory,
2782 2760 since the default prompts do not display path information.
2783 2761
2784 2762 Note that !cd doesn't work for this purpose because the shell where
2785 2763 !command runs is immediately discarded after executing 'command'."""
2786 2764
2787 2765 parameter_s = parameter_s.strip()
2788 2766 #bkms = self.shell.persist.get("bookmarks",{})
2789 2767
2790 2768 oldcwd = os.getcwd()
2791 2769 numcd = re.match(r'(-)(\d+)$',parameter_s)
2792 2770 # jump in directory history by number
2793 2771 if numcd:
2794 2772 nn = int(numcd.group(2))
2795 2773 try:
2796 2774 ps = self.shell.user_ns['_dh'][nn]
2797 2775 except IndexError:
2798 2776 print 'The requested directory does not exist in history.'
2799 2777 return
2800 2778 else:
2801 2779 opts = {}
2802 2780 elif parameter_s.startswith('--'):
2803 2781 ps = None
2804 2782 fallback = None
2805 2783 pat = parameter_s[2:]
2806 2784 dh = self.shell.user_ns['_dh']
2807 2785 # first search only by basename (last component)
2808 2786 for ent in reversed(dh):
2809 2787 if pat in os.path.basename(ent) and os.path.isdir(ent):
2810 2788 ps = ent
2811 2789 break
2812 2790
2813 2791 if fallback is None and pat in ent and os.path.isdir(ent):
2814 2792 fallback = ent
2815 2793
2816 2794 # if we have no last part match, pick the first full path match
2817 2795 if ps is None:
2818 2796 ps = fallback
2819 2797
2820 2798 if ps is None:
2821 2799 print "No matching entry in directory history"
2822 2800 return
2823 2801 else:
2824 2802 opts = {}
2825 2803
2826 2804
2827 2805 else:
2828 2806 #turn all non-space-escaping backslashes to slashes,
2829 2807 # for c:\windows\directory\names\
2830 2808 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2831 2809 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2832 2810 # jump to previous
2833 2811 if ps == '-':
2834 2812 try:
2835 2813 ps = self.shell.user_ns['_dh'][-2]
2836 2814 except IndexError:
2837 2815 raise UsageError('%cd -: No previous directory to change to.')
2838 2816 # jump to bookmark if needed
2839 2817 else:
2840 2818 if not os.path.isdir(ps) or opts.has_key('b'):
2841 2819 bkms = self.db.get('bookmarks', {})
2842 2820
2843 2821 if bkms.has_key(ps):
2844 2822 target = bkms[ps]
2845 2823 print '(bookmark:%s) -> %s' % (ps,target)
2846 2824 ps = target
2847 2825 else:
2848 2826 if opts.has_key('b'):
2849 2827 raise UsageError("Bookmark '%s' not found. "
2850 2828 "Use '%%bookmark -l' to see your bookmarks." % ps)
2851 2829
2852 2830 # at this point ps should point to the target dir
2853 2831 if ps:
2854 2832 try:
2855 2833 os.chdir(os.path.expanduser(ps))
2856 2834 if self.shell.term_title:
2857 2835 platutils.set_term_title('IPython: ' + abbrev_cwd())
2858 2836 except OSError:
2859 2837 print sys.exc_info()[1]
2860 2838 else:
2861 2839 cwd = os.getcwd()
2862 2840 dhist = self.shell.user_ns['_dh']
2863 2841 if oldcwd != cwd:
2864 2842 dhist.append(cwd)
2865 2843 self.db['dhist'] = compress_dhist(dhist)[-100:]
2866 2844
2867 2845 else:
2868 2846 os.chdir(self.shell.home_dir)
2869 2847 if self.shell.term_title:
2870 2848 platutils.set_term_title('IPython: ' + '~')
2871 2849 cwd = os.getcwd()
2872 2850 dhist = self.shell.user_ns['_dh']
2873 2851
2874 2852 if oldcwd != cwd:
2875 2853 dhist.append(cwd)
2876 2854 self.db['dhist'] = compress_dhist(dhist)[-100:]
2877 2855 if not 'q' in opts and self.shell.user_ns['_dh']:
2878 2856 print self.shell.user_ns['_dh'][-1]
2879 2857
2880 2858
2881 2859 def magic_env(self, parameter_s=''):
2882 2860 """List environment variables."""
2883 2861
2884 2862 return os.environ.data
2885 2863
2886 2864 def magic_pushd(self, parameter_s=''):
2887 2865 """Place the current dir on stack and change directory.
2888 2866
2889 2867 Usage:\\
2890 2868 %pushd ['dirname']
2891 2869 """
2892 2870
2893 2871 dir_s = self.shell.dir_stack
2894 2872 tgt = os.path.expanduser(parameter_s)
2895 2873 cwd = os.getcwd().replace(self.home_dir,'~')
2896 2874 if tgt:
2897 2875 self.magic_cd(parameter_s)
2898 2876 dir_s.insert(0,cwd)
2899 2877 return self.magic_dirs()
2900 2878
2901 2879 def magic_popd(self, parameter_s=''):
2902 2880 """Change to directory popped off the top of the stack.
2903 2881 """
2904 2882 if not self.shell.dir_stack:
2905 2883 raise UsageError("%popd on empty stack")
2906 2884 top = self.shell.dir_stack.pop(0)
2907 2885 self.magic_cd(top)
2908 2886 print "popd ->",top
2909 2887
2910 2888 def magic_dirs(self, parameter_s=''):
2911 2889 """Return the current directory stack."""
2912 2890
2913 2891 return self.shell.dir_stack
2914 2892
2915 2893 def magic_dhist(self, parameter_s=''):
2916 2894 """Print your history of visited directories.
2917 2895
2918 2896 %dhist -> print full history\\
2919 2897 %dhist n -> print last n entries only\\
2920 2898 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2921 2899
2922 2900 This history is automatically maintained by the %cd command, and
2923 2901 always available as the global list variable _dh. You can use %cd -<n>
2924 2902 to go to directory number <n>.
2925 2903
2926 2904 Note that most of time, you should view directory history by entering
2927 2905 cd -<TAB>.
2928 2906
2929 2907 """
2930 2908
2931 2909 dh = self.shell.user_ns['_dh']
2932 2910 if parameter_s:
2933 2911 try:
2934 2912 args = map(int,parameter_s.split())
2935 2913 except:
2936 2914 self.arg_err(Magic.magic_dhist)
2937 2915 return
2938 2916 if len(args) == 1:
2939 2917 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2940 2918 elif len(args) == 2:
2941 2919 ini,fin = args
2942 2920 else:
2943 2921 self.arg_err(Magic.magic_dhist)
2944 2922 return
2945 2923 else:
2946 2924 ini,fin = 0,len(dh)
2947 2925 nlprint(dh,
2948 2926 header = 'Directory history (kept in _dh)',
2949 2927 start=ini,stop=fin)
2950 2928
2951 2929 @testdec.skip_doctest
2952 2930 def magic_sc(self, parameter_s=''):
2953 2931 """Shell capture - execute a shell command and capture its output.
2954 2932
2955 2933 DEPRECATED. Suboptimal, retained for backwards compatibility.
2956 2934
2957 2935 You should use the form 'var = !command' instead. Example:
2958 2936
2959 2937 "%sc -l myfiles = ls ~" should now be written as
2960 2938
2961 2939 "myfiles = !ls ~"
2962 2940
2963 2941 myfiles.s, myfiles.l and myfiles.n still apply as documented
2964 2942 below.
2965 2943
2966 2944 --
2967 2945 %sc [options] varname=command
2968 2946
2969 2947 IPython will run the given command using commands.getoutput(), and
2970 2948 will then update the user's interactive namespace with a variable
2971 2949 called varname, containing the value of the call. Your command can
2972 2950 contain shell wildcards, pipes, etc.
2973 2951
2974 2952 The '=' sign in the syntax is mandatory, and the variable name you
2975 2953 supply must follow Python's standard conventions for valid names.
2976 2954
2977 2955 (A special format without variable name exists for internal use)
2978 2956
2979 2957 Options:
2980 2958
2981 2959 -l: list output. Split the output on newlines into a list before
2982 2960 assigning it to the given variable. By default the output is stored
2983 2961 as a single string.
2984 2962
2985 2963 -v: verbose. Print the contents of the variable.
2986 2964
2987 2965 In most cases you should not need to split as a list, because the
2988 2966 returned value is a special type of string which can automatically
2989 2967 provide its contents either as a list (split on newlines) or as a
2990 2968 space-separated string. These are convenient, respectively, either
2991 2969 for sequential processing or to be passed to a shell command.
2992 2970
2993 2971 For example:
2994 2972
2995 2973 # all-random
2996 2974
2997 2975 # Capture into variable a
2998 2976 In [1]: sc a=ls *py
2999 2977
3000 2978 # a is a string with embedded newlines
3001 2979 In [2]: a
3002 2980 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3003 2981
3004 2982 # which can be seen as a list:
3005 2983 In [3]: a.l
3006 2984 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3007 2985
3008 2986 # or as a whitespace-separated string:
3009 2987 In [4]: a.s
3010 2988 Out[4]: 'setup.py win32_manual_post_install.py'
3011 2989
3012 2990 # a.s is useful to pass as a single command line:
3013 2991 In [5]: !wc -l $a.s
3014 2992 146 setup.py
3015 2993 130 win32_manual_post_install.py
3016 2994 276 total
3017 2995
3018 2996 # while the list form is useful to loop over:
3019 2997 In [6]: for f in a.l:
3020 2998 ...: !wc -l $f
3021 2999 ...:
3022 3000 146 setup.py
3023 3001 130 win32_manual_post_install.py
3024 3002
3025 3003 Similiarly, the lists returned by the -l option are also special, in
3026 3004 the sense that you can equally invoke the .s attribute on them to
3027 3005 automatically get a whitespace-separated string from their contents:
3028 3006
3029 3007 In [7]: sc -l b=ls *py
3030 3008
3031 3009 In [8]: b
3032 3010 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3033 3011
3034 3012 In [9]: b.s
3035 3013 Out[9]: 'setup.py win32_manual_post_install.py'
3036 3014
3037 3015 In summary, both the lists and strings used for ouptut capture have
3038 3016 the following special attributes:
3039 3017
3040 3018 .l (or .list) : value as list.
3041 3019 .n (or .nlstr): value as newline-separated string.
3042 3020 .s (or .spstr): value as space-separated string.
3043 3021 """
3044 3022
3045 3023 opts,args = self.parse_options(parameter_s,'lv')
3046 3024 # Try to get a variable name and command to run
3047 3025 try:
3048 3026 # the variable name must be obtained from the parse_options
3049 3027 # output, which uses shlex.split to strip options out.
3050 3028 var,_ = args.split('=',1)
3051 3029 var = var.strip()
3052 3030 # But the the command has to be extracted from the original input
3053 3031 # parameter_s, not on what parse_options returns, to avoid the
3054 3032 # quote stripping which shlex.split performs on it.
3055 3033 _,cmd = parameter_s.split('=',1)
3056 3034 except ValueError:
3057 3035 var,cmd = '',''
3058 3036 # If all looks ok, proceed
3059 3037 out,err = self.shell.getoutputerror(cmd)
3060 3038 if err:
3061 3039 print >> Term.cerr,err
3062 3040 if opts.has_key('l'):
3063 3041 out = SList(out.split('\n'))
3064 3042 else:
3065 3043 out = LSString(out)
3066 3044 if opts.has_key('v'):
3067 3045 print '%s ==\n%s' % (var,pformat(out))
3068 3046 if var:
3069 3047 self.shell.user_ns.update({var:out})
3070 3048 else:
3071 3049 return out
3072 3050
3073 3051 def magic_sx(self, parameter_s=''):
3074 3052 """Shell execute - run a shell command and capture its output.
3075 3053
3076 3054 %sx command
3077 3055
3078 3056 IPython will run the given command using commands.getoutput(), and
3079 3057 return the result formatted as a list (split on '\\n'). Since the
3080 3058 output is _returned_, it will be stored in ipython's regular output
3081 3059 cache Out[N] and in the '_N' automatic variables.
3082 3060
3083 3061 Notes:
3084 3062
3085 3063 1) If an input line begins with '!!', then %sx is automatically
3086 3064 invoked. That is, while:
3087 3065 !ls
3088 3066 causes ipython to simply issue system('ls'), typing
3089 3067 !!ls
3090 3068 is a shorthand equivalent to:
3091 3069 %sx ls
3092 3070
3093 3071 2) %sx differs from %sc in that %sx automatically splits into a list,
3094 3072 like '%sc -l'. The reason for this is to make it as easy as possible
3095 3073 to process line-oriented shell output via further python commands.
3096 3074 %sc is meant to provide much finer control, but requires more
3097 3075 typing.
3098 3076
3099 3077 3) Just like %sc -l, this is a list with special attributes:
3100 3078
3101 3079 .l (or .list) : value as list.
3102 3080 .n (or .nlstr): value as newline-separated string.
3103 3081 .s (or .spstr): value as whitespace-separated string.
3104 3082
3105 3083 This is very useful when trying to use such lists as arguments to
3106 3084 system commands."""
3107 3085
3108 3086 if parameter_s:
3109 3087 out,err = self.shell.getoutputerror(parameter_s)
3110 3088 if err:
3111 3089 print >> Term.cerr,err
3112 3090 return SList(out.split('\n'))
3113 3091
3114 3092 def magic_bg(self, parameter_s=''):
3115 3093 """Run a job in the background, in a separate thread.
3116 3094
3117 3095 For example,
3118 3096
3119 3097 %bg myfunc(x,y,z=1)
3120 3098
3121 3099 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3122 3100 execution starts, a message will be printed indicating the job
3123 3101 number. If your job number is 5, you can use
3124 3102
3125 3103 myvar = jobs.result(5) or myvar = jobs[5].result
3126 3104
3127 3105 to assign this result to variable 'myvar'.
3128 3106
3129 3107 IPython has a job manager, accessible via the 'jobs' object. You can
3130 3108 type jobs? to get more information about it, and use jobs.<TAB> to see
3131 3109 its attributes. All attributes not starting with an underscore are
3132 3110 meant for public use.
3133 3111
3134 3112 In particular, look at the jobs.new() method, which is used to create
3135 3113 new jobs. This magic %bg function is just a convenience wrapper
3136 3114 around jobs.new(), for expression-based jobs. If you want to create a
3137 3115 new job with an explicit function object and arguments, you must call
3138 3116 jobs.new() directly.
3139 3117
3140 3118 The jobs.new docstring also describes in detail several important
3141 3119 caveats associated with a thread-based model for background job
3142 3120 execution. Type jobs.new? for details.
3143 3121
3144 3122 You can check the status of all jobs with jobs.status().
3145 3123
3146 3124 The jobs variable is set by IPython into the Python builtin namespace.
3147 3125 If you ever declare a variable named 'jobs', you will shadow this
3148 3126 name. You can either delete your global jobs variable to regain
3149 3127 access to the job manager, or make a new name and assign it manually
3150 3128 to the manager (stored in IPython's namespace). For example, to
3151 3129 assign the job manager to the Jobs name, use:
3152 3130
3153 3131 Jobs = __builtins__.jobs"""
3154 3132
3155 3133 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3156 3134
3157 3135 def magic_r(self, parameter_s=''):
3158 3136 """Repeat previous input.
3159 3137
3160 3138 Note: Consider using the more powerfull %rep instead!
3161 3139
3162 3140 If given an argument, repeats the previous command which starts with
3163 3141 the same string, otherwise it just repeats the previous input.
3164 3142
3165 3143 Shell escaped commands (with ! as first character) are not recognized
3166 3144 by this system, only pure python code and magic commands.
3167 3145 """
3168 3146
3169 3147 start = parameter_s.strip()
3170 esc_magic = self.shell.ESC_MAGIC
3148 esc_magic = ESC_MAGIC
3171 3149 # Identify magic commands even if automagic is on (which means
3172 3150 # the in-memory version is different from that typed by the user).
3173 3151 if self.shell.automagic:
3174 3152 start_magic = esc_magic+start
3175 3153 else:
3176 3154 start_magic = start
3177 3155 # Look through the input history in reverse
3178 3156 for n in range(len(self.shell.input_hist)-2,0,-1):
3179 3157 input = self.shell.input_hist[n]
3180 3158 # skip plain 'r' lines so we don't recurse to infinity
3181 3159 if input != '_ip.magic("r")\n' and \
3182 3160 (input.startswith(start) or input.startswith(start_magic)):
3183 3161 #print 'match',`input` # dbg
3184 3162 print 'Executing:',input,
3185 3163 self.shell.runlines(input)
3186 3164 return
3187 3165 print 'No previous input matching `%s` found.' % start
3188 3166
3189 3167
3190 3168 def magic_bookmark(self, parameter_s=''):
3191 3169 """Manage IPython's bookmark system.
3192 3170
3193 3171 %bookmark <name> - set bookmark to current dir
3194 3172 %bookmark <name> <dir> - set bookmark to <dir>
3195 3173 %bookmark -l - list all bookmarks
3196 3174 %bookmark -d <name> - remove bookmark
3197 3175 %bookmark -r - remove all bookmarks
3198 3176
3199 3177 You can later on access a bookmarked folder with:
3200 3178 %cd -b <name>
3201 3179 or simply '%cd <name>' if there is no directory called <name> AND
3202 3180 there is such a bookmark defined.
3203 3181
3204 3182 Your bookmarks persist through IPython sessions, but they are
3205 3183 associated with each profile."""
3206 3184
3207 3185 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3208 3186 if len(args) > 2:
3209 3187 raise UsageError("%bookmark: too many arguments")
3210 3188
3211 3189 bkms = self.db.get('bookmarks',{})
3212 3190
3213 3191 if opts.has_key('d'):
3214 3192 try:
3215 3193 todel = args[0]
3216 3194 except IndexError:
3217 3195 raise UsageError(
3218 3196 "%bookmark -d: must provide a bookmark to delete")
3219 3197 else:
3220 3198 try:
3221 3199 del bkms[todel]
3222 3200 except KeyError:
3223 3201 raise UsageError(
3224 3202 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3225 3203
3226 3204 elif opts.has_key('r'):
3227 3205 bkms = {}
3228 3206 elif opts.has_key('l'):
3229 3207 bks = bkms.keys()
3230 3208 bks.sort()
3231 3209 if bks:
3232 3210 size = max(map(len,bks))
3233 3211 else:
3234 3212 size = 0
3235 3213 fmt = '%-'+str(size)+'s -> %s'
3236 3214 print 'Current bookmarks:'
3237 3215 for bk in bks:
3238 3216 print fmt % (bk,bkms[bk])
3239 3217 else:
3240 3218 if not args:
3241 3219 raise UsageError("%bookmark: You must specify the bookmark name")
3242 3220 elif len(args)==1:
3243 3221 bkms[args[0]] = os.getcwd()
3244 3222 elif len(args)==2:
3245 3223 bkms[args[0]] = args[1]
3246 3224 self.db['bookmarks'] = bkms
3247 3225
3248 3226 def magic_pycat(self, parameter_s=''):
3249 3227 """Show a syntax-highlighted file through a pager.
3250 3228
3251 3229 This magic is similar to the cat utility, but it will assume the file
3252 3230 to be Python source and will show it with syntax highlighting. """
3253 3231
3254 3232 try:
3255 3233 filename = get_py_filename(parameter_s)
3256 3234 cont = file_read(filename)
3257 3235 except IOError:
3258 3236 try:
3259 3237 cont = eval(parameter_s,self.user_ns)
3260 3238 except NameError:
3261 3239 cont = None
3262 3240 if cont is None:
3263 3241 print "Error: no such file or variable"
3264 3242 return
3265 3243
3266 3244 page(self.shell.pycolorize(cont),
3267 3245 screen_lines=self.shell.usable_screen_length)
3268 3246
3269 3247 def _rerun_pasted(self):
3270 3248 """ Rerun a previously pasted command.
3271 3249 """
3272 3250 b = self.user_ns.get('pasted_block', None)
3273 3251 if b is None:
3274 3252 raise UsageError('No previous pasted block available')
3275 3253 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3276 3254 exec b in self.user_ns
3277 3255
3278 3256 def _get_pasted_lines(self, sentinel):
3279 3257 """ Yield pasted lines until the user enters the given sentinel value.
3280 3258 """
3281 3259 from IPython.core import iplib
3282 3260 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3283 3261 while True:
3284 3262 l = iplib.raw_input_original(':')
3285 3263 if l == sentinel:
3286 3264 return
3287 3265 else:
3288 3266 yield l
3289 3267
3290 3268 def _strip_pasted_lines_for_code(self, raw_lines):
3291 3269 """ Strip non-code parts of a sequence of lines to return a block of
3292 3270 code.
3293 3271 """
3294 3272 # Regular expressions that declare text we strip from the input:
3295 3273 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3296 3274 r'^\s*(\s?>)+', # Python input prompt
3297 3275 r'^\s*\.{3,}', # Continuation prompts
3298 3276 r'^\++',
3299 3277 ]
3300 3278
3301 3279 strip_from_start = map(re.compile,strip_re)
3302 3280
3303 3281 lines = []
3304 3282 for l in raw_lines:
3305 3283 for pat in strip_from_start:
3306 3284 l = pat.sub('',l)
3307 3285 lines.append(l)
3308 3286
3309 3287 block = "\n".join(lines) + '\n'
3310 3288 #print "block:\n",block
3311 3289 return block
3312 3290
3313 3291 def _execute_block(self, block, par):
3314 3292 """ Execute a block, or store it in a variable, per the user's request.
3315 3293 """
3316 3294 if not par:
3317 3295 b = textwrap.dedent(block)
3318 3296 self.user_ns['pasted_block'] = b
3319 3297 exec b in self.user_ns
3320 3298 else:
3321 3299 self.user_ns[par] = SList(block.splitlines())
3322 3300 print "Block assigned to '%s'" % par
3323 3301
3324 3302 def magic_cpaste(self, parameter_s=''):
3325 3303 """Allows you to paste & execute a pre-formatted code block from clipboard.
3326 3304
3327 3305 You must terminate the block with '--' (two minus-signs) alone on the
3328 3306 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3329 3307 is the new sentinel for this operation)
3330 3308
3331 3309 The block is dedented prior to execution to enable execution of method
3332 3310 definitions. '>' and '+' characters at the beginning of a line are
3333 3311 ignored, to allow pasting directly from e-mails, diff files and
3334 3312 doctests (the '...' continuation prompt is also stripped). The
3335 3313 executed block is also assigned to variable named 'pasted_block' for
3336 3314 later editing with '%edit pasted_block'.
3337 3315
3338 3316 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3339 3317 This assigns the pasted block to variable 'foo' as string, without
3340 3318 dedenting or executing it (preceding >>> and + is still stripped)
3341 3319
3342 3320 '%cpaste -r' re-executes the block previously entered by cpaste.
3343 3321
3344 3322 Do not be alarmed by garbled output on Windows (it's a readline bug).
3345 3323 Just press enter and type -- (and press enter again) and the block
3346 3324 will be what was just pasted.
3347 3325
3348 3326 IPython statements (magics, shell escapes) are not supported (yet).
3349 3327
3350 3328 See also
3351 3329 --------
3352 3330 paste: automatically pull code from clipboard.
3353 3331 """
3354 3332
3355 3333 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3356 3334 par = args.strip()
3357 3335 if opts.has_key('r'):
3358 3336 self._rerun_pasted()
3359 3337 return
3360 3338
3361 3339 sentinel = opts.get('s','--')
3362 3340
3363 3341 block = self._strip_pasted_lines_for_code(
3364 3342 self._get_pasted_lines(sentinel))
3365 3343
3366 3344 self._execute_block(block, par)
3367 3345
3368 3346 def magic_paste(self, parameter_s=''):
3369 3347 """Allows you to paste & execute a pre-formatted code block from clipboard.
3370 3348
3371 3349 The text is pulled directly from the clipboard without user
3372 3350 intervention and printed back on the screen before execution (unless
3373 3351 the -q flag is given to force quiet mode).
3374 3352
3375 3353 The block is dedented prior to execution to enable execution of method
3376 3354 definitions. '>' and '+' characters at the beginning of a line are
3377 3355 ignored, to allow pasting directly from e-mails, diff files and
3378 3356 doctests (the '...' continuation prompt is also stripped). The
3379 3357 executed block is also assigned to variable named 'pasted_block' for
3380 3358 later editing with '%edit pasted_block'.
3381 3359
3382 3360 You can also pass a variable name as an argument, e.g. '%paste foo'.
3383 3361 This assigns the pasted block to variable 'foo' as string, without
3384 3362 dedenting or executing it (preceding >>> and + is still stripped)
3385 3363
3386 3364 Options
3387 3365 -------
3388 3366
3389 3367 -r: re-executes the block previously entered by cpaste.
3390 3368
3391 3369 -q: quiet mode: do not echo the pasted text back to the terminal.
3392 3370
3393 3371 IPython statements (magics, shell escapes) are not supported (yet).
3394 3372
3395 3373 See also
3396 3374 --------
3397 3375 cpaste: manually paste code into terminal until you mark its end.
3398 3376 """
3399 3377 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3400 3378 par = args.strip()
3401 3379 if opts.has_key('r'):
3402 3380 self._rerun_pasted()
3403 3381 return
3404 3382
3405 3383 text = self.shell.hooks.clipboard_get()
3406 3384 block = self._strip_pasted_lines_for_code(text.splitlines())
3407 3385
3408 3386 # By default, echo back to terminal unless quiet mode is requested
3409 3387 if not opts.has_key('q'):
3410 3388 write = self.shell.write
3411 3389 write(block)
3412 3390 if not block.endswith('\n'):
3413 3391 write('\n')
3414 3392 write("## -- End pasted text --\n")
3415 3393
3416 3394 self._execute_block(block, par)
3417 3395
3418 3396 def magic_quickref(self,arg):
3419 3397 """ Show a quick reference sheet """
3420 3398 import IPython.core.usage
3421 3399 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3422 3400
3423 3401 page(qr)
3424 3402
3425 3403 def magic_upgrade(self,arg):
3426 3404 """ Upgrade your IPython installation
3427 3405
3428 3406 This will copy the config files that don't yet exist in your
3429 3407 ipython dir from the system config dir. Use this after upgrading
3430 3408 IPython if you don't wish to delete your .ipython dir.
3431 3409
3432 3410 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3433 3411 new users)
3434 3412
3435 3413 """
3436 3414 ip = self.getapi()
3437 3415 ipinstallation = path(IPython.__file__).dirname()
3438 3416 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'utils' / 'upgradedir.py')
3439 3417 src_config = ipinstallation / 'config' / 'userconfig'
3440 3418 userdir = path(ip.config.IPYTHONDIR)
3441 3419 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3442 3420 print ">",cmd
3443 3421 shell(cmd)
3444 3422 if arg == '-nolegacy':
3445 3423 legacy = userdir.files('ipythonrc*')
3446 3424 print "Nuking legacy files:",legacy
3447 3425
3448 3426 [p.remove() for p in legacy]
3449 3427 suffix = (sys.platform == 'win32' and '.ini' or '')
3450 3428 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3451 3429
3452 3430
3453 3431 def magic_doctest_mode(self,parameter_s=''):
3454 3432 """Toggle doctest mode on and off.
3455 3433
3456 3434 This mode allows you to toggle the prompt behavior between normal
3457 3435 IPython prompts and ones that are as similar to the default IPython
3458 3436 interpreter as possible.
3459 3437
3460 3438 It also supports the pasting of code snippets that have leading '>>>'
3461 3439 and '...' prompts in them. This means that you can paste doctests from
3462 3440 files or docstrings (even if they have leading whitespace), and the
3463 3441 code will execute correctly. You can then use '%history -tn' to see
3464 3442 the translated history without line numbers; this will give you the
3465 3443 input after removal of all the leading prompts and whitespace, which
3466 3444 can be pasted back into an editor.
3467 3445
3468 3446 With these features, you can switch into this mode easily whenever you
3469 3447 need to do testing and changes to doctests, without having to leave
3470 3448 your existing IPython session.
3471 3449 """
3472 3450
3473 3451 # XXX - Fix this to have cleaner activate/deactivate calls.
3474 3452 from IPython.extensions import InterpreterPasteInput as ipaste
3475 3453 from IPython.utils.ipstruct import Struct
3476 3454
3477 3455 # Shorthands
3478 3456 shell = self.shell
3479 3457 oc = shell.outputcache
3480 3458 meta = shell.meta
3481 3459 # dstore is a data store kept in the instance metadata bag to track any
3482 3460 # changes we make, so we can undo them later.
3483 3461 dstore = meta.setdefault('doctest_mode',Struct())
3484 3462 save_dstore = dstore.setdefault
3485 3463
3486 3464 # save a few values we'll need to recover later
3487 3465 mode = save_dstore('mode',False)
3488 3466 save_dstore('rc_pprint',shell.pprint)
3489 3467 save_dstore('xmode',shell.InteractiveTB.mode)
3490 3468 save_dstore('rc_separate_out',shell.separate_out)
3491 3469 save_dstore('rc_separate_out2',shell.separate_out2)
3492 3470 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3493 3471 save_dstore('rc_separate_in',shell.separate_in)
3494 3472
3495 3473 if mode == False:
3496 3474 # turn on
3497 3475 ipaste.activate_prefilter()
3498 3476
3499 3477 oc.prompt1.p_template = '>>> '
3500 3478 oc.prompt2.p_template = '... '
3501 3479 oc.prompt_out.p_template = ''
3502 3480
3503 3481 # Prompt separators like plain python
3504 3482 oc.input_sep = oc.prompt1.sep = ''
3505 3483 oc.output_sep = ''
3506 3484 oc.output_sep2 = ''
3507 3485
3508 3486 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3509 3487 oc.prompt_out.pad_left = False
3510 3488
3511 3489 shell.pprint = False
3512 3490
3513 3491 shell.magic_xmode('Plain')
3514 3492
3515 3493 else:
3516 3494 # turn off
3517 3495 ipaste.deactivate_prefilter()
3518 3496
3519 3497 oc.prompt1.p_template = shell.prompt_in1
3520 3498 oc.prompt2.p_template = shell.prompt_in2
3521 3499 oc.prompt_out.p_template = shell.prompt_out
3522 3500
3523 3501 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3524 3502
3525 3503 oc.output_sep = dstore.rc_separate_out
3526 3504 oc.output_sep2 = dstore.rc_separate_out2
3527 3505
3528 3506 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3529 3507 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3530 3508
3531 3509 rc.pprint = dstore.rc_pprint
3532 3510
3533 3511 shell.magic_xmode(dstore.xmode)
3534 3512
3535 3513 # Store new mode and inform
3536 3514 dstore.mode = bool(1-int(mode))
3537 3515 print 'Doctest mode is:',
3538 3516 print ['OFF','ON'][dstore.mode]
3539 3517
3540 3518 def magic_gui(self, parameter_s=''):
3541 3519 """Enable or disable IPython GUI event loop integration.
3542 3520
3543 3521 %gui [-a] [GUINAME]
3544 3522
3545 3523 This magic replaces IPython's threaded shells that were activated
3546 3524 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3547 3525 can now be enabled, disabled and swtiched at runtime and keyboard
3548 3526 interrupts should work without any problems. The following toolkits
3549 3527 are supports: wxPython, PyQt4, PyGTK, and Tk::
3550 3528
3551 3529 %gui wx # enable wxPython event loop integration
3552 3530 %gui qt4|qt # enable PyQt4 event loop integration
3553 3531 %gui gtk # enable PyGTK event loop integration
3554 3532 %gui tk # enable Tk event loop integration
3555 3533 %gui # disable all event loop integration
3556 3534
3557 3535 WARNING: after any of these has been called you can simply create
3558 3536 an application object, but DO NOT start the event loop yourself, as
3559 3537 we have already handled that.
3560 3538
3561 3539 If you want us to create an appropriate application object add the
3562 3540 "-a" flag to your command::
3563 3541
3564 3542 %gui -a wx
3565 3543
3566 3544 This is highly recommended for most users.
3567 3545 """
3568 3546 from IPython.lib import inputhook
3569 3547 if "-a" in parameter_s:
3570 3548 app = True
3571 3549 else:
3572 3550 app = False
3573 3551 if not parameter_s:
3574 3552 inputhook.clear_inputhook()
3575 3553 elif 'wx' in parameter_s:
3576 3554 return inputhook.enable_wx(app)
3577 3555 elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3578 3556 return inputhook.enable_qt4(app)
3579 3557 elif 'gtk' in parameter_s:
3580 3558 return inputhook.enable_gtk(app)
3581 3559 elif 'tk' in parameter_s:
3582 3560 return inputhook.enable_tk(app)
3583 3561
3584 3562
3585 3563 # end Magic
@@ -1,777 +1,777 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Prefiltering components.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez
10 10 * Dan Milstein
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Copyright (C) 2008-2009 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Imports
22 22 #-----------------------------------------------------------------------------
23 23
24 24 import __builtin__
25 25 import codeop
26 26 import keyword
27 27 import os
28 28 import re
29 29 import sys
30 30
31 31 from IPython.core.alias import AliasManager
32 32 from IPython.core.autocall import IPyAutocall
33 33 from IPython.core.component import Component
34 34 from IPython.core.splitinput import split_user_input
35 35
36 36 from IPython.utils.traitlets import List, Int, Any, Str, CBool
37 37 from IPython.utils.genutils import make_quoted_expr
38 38 from IPython.utils.autoattr import auto_attr
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Global utilities, errors and constants
42 42 #-----------------------------------------------------------------------------
43 43
44 44
45 45 ESC_SHELL = '!'
46 46 ESC_SH_CAP = '!!'
47 47 ESC_HELP = '?'
48 48 ESC_MAGIC = '%'
49 49 ESC_QUOTE = ','
50 50 ESC_QUOTE2 = ';'
51 51 ESC_PAREN = '/'
52 52
53 53
54 54 class PrefilterError(Exception):
55 55 pass
56 56
57 57
58 58 # RegExp to identify potential function names
59 59 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
60 60
61 61 # RegExp to exclude strings with this start from autocalling. In
62 62 # particular, all binary operators should be excluded, so that if foo is
63 63 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
64 64 # characters '!=()' don't need to be checked for, as the checkPythonChars
65 65 # routine explicitely does so, to catch direct calls and rebindings of
66 66 # existing names.
67 67
68 68 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
69 69 # it affects the rest of the group in square brackets.
70 70 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
71 71 r'|^is |^not |^in |^and |^or ')
72 72
73 73 # try to catch also methods for stuff in lists/tuples/dicts: off
74 74 # (experimental). For this to work, the line_split regexp would need
75 75 # to be modified so it wouldn't break things at '['. That line is
76 76 # nasty enough that I shouldn't change it until I can test it _well_.
77 77 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
78 78
79 79
80 80 # Handler Check Utilities
81 81 def is_shadowed(identifier, ip):
82 82 """Is the given identifier defined in one of the namespaces which shadow
83 83 the alias and magic namespaces? Note that an identifier is different
84 84 than ifun, because it can not contain a '.' character."""
85 85 # This is much safer than calling ofind, which can change state
86 86 return (identifier in ip.user_ns \
87 87 or identifier in ip.internal_ns \
88 88 or identifier in ip.ns_table['builtin'])
89 89
90 90
91 91 #-----------------------------------------------------------------------------
92 92 # The LineInfo class used throughout
93 93 #-----------------------------------------------------------------------------
94 94
95 95
96 96 class LineInfo(object):
97 97 """A single line of input and associated info.
98 98
99 99 Includes the following as properties:
100 100
101 101 line
102 102 The original, raw line
103 103
104 104 continue_prompt
105 105 Is this line a continuation in a sequence of multiline input?
106 106
107 107 pre
108 108 The initial esc character or whitespace.
109 109
110 110 pre_char
111 111 The escape character(s) in pre or the empty string if there isn't one.
112 112 Note that '!!' is a possible value for pre_char. Otherwise it will
113 113 always be a single character.
114 114
115 115 pre_whitespace
116 116 The leading whitespace from pre if it exists. If there is a pre_char,
117 117 this is just ''.
118 118
119 119 ifun
120 120 The 'function part', which is basically the maximal initial sequence
121 121 of valid python identifiers and the '.' character. This is what is
122 122 checked for alias and magic transformations, used for auto-calling,
123 123 etc.
124 124
125 125 the_rest
126 126 Everything else on the line.
127 127 """
128 128 def __init__(self, line, continue_prompt):
129 129 self.line = line
130 130 self.continue_prompt = continue_prompt
131 131 self.pre, self.ifun, self.the_rest = split_user_input(line)
132 132
133 133 self.pre_char = self.pre.strip()
134 134 if self.pre_char:
135 135 self.pre_whitespace = '' # No whitespace allowd before esc chars
136 136 else:
137 137 self.pre_whitespace = self.pre
138 138
139 139 self._oinfo = None
140 140
141 141 def ofind(self, ip):
142 142 """Do a full, attribute-walking lookup of the ifun in the various
143 143 namespaces for the given IPython InteractiveShell instance.
144 144
145 145 Return a dict with keys: found,obj,ospace,ismagic
146 146
147 147 Note: can cause state changes because of calling getattr, but should
148 148 only be run if autocall is on and if the line hasn't matched any
149 149 other, less dangerous handlers.
150 150
151 151 Does cache the results of the call, so can be called multiple times
152 152 without worrying about *further* damaging state.
153 153 """
154 154 if not self._oinfo:
155 155 self._oinfo = ip._ofind(self.ifun)
156 156 return self._oinfo
157 157
158 158 def __str__(self):
159 159 return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest)
160 160
161 161
162 162 #-----------------------------------------------------------------------------
163 163 # Main Prefilter manager
164 164 #-----------------------------------------------------------------------------
165 165
166 166
167 167 class PrefilterManager(Component):
168 168 """Main prefilter component.
169 169
170 170 The IPython prefilter is run on all user input before it is run. The
171 171 prefilter consumes lines of input and produces transformed lines of
172 172 input. The implementation consists of checkers and handlers. The
173 173 checkers inspect the input line and select which handler will be used
174 174 to transform the input line.
175 175 """
176 176
177 multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS')
177 multi_line_specials = CBool(True, config=True)
178 178
179 179 def __init__(self, parent, config=None):
180 180 super(PrefilterManager, self).__init__(parent, config=config)
181 181 self.init_handlers()
182 182 self.init_checkers()
183 183
184 184 @auto_attr
185 185 def shell(self):
186 186 shell = Component.get_instances(
187 187 root=self.root,
188 188 klass='IPython.core.iplib.InteractiveShell'
189 189 )[0]
190 190 return shell
191 191
192 192 def init_checkers(self):
193 193 self._checkers = []
194 194 for checker in _default_checkers:
195 195 self._checkers.append(checker(self, config=self.config))
196 196
197 197 def init_handlers(self):
198 198 self._handlers = {}
199 199 self._esc_handlers = {}
200 200 for handler in _default_handlers:
201 201 handler(self, config=self.config)
202 202
203 203 @property
204 204 def sorted_checkers(self):
205 205 """Return a list of checkers, sorted by priority."""
206 206 return sorted(self._checkers, cmp=lambda x,y: x.priority-y.priority)
207 207
208 208 def register_handler(self, name, handler, esc_strings):
209 209 """Register a handler instance by name with esc_strings."""
210 210 self._handlers[name] = handler
211 211 for esc_str in esc_strings:
212 212 self._esc_handlers[esc_str] = handler
213 213
214 214 def unregister_handler(self, name, handler, esc_strings):
215 215 """Unregister a handler instance by name with esc_strings."""
216 216 try:
217 217 del self._handlers[name]
218 218 except KeyError:
219 219 pass
220 220 for esc_str in esc_strings:
221 221 h = self._esc_handlers.get(esc_str)
222 222 if h is handler:
223 223 del self._esc_handlers[esc_str]
224 224
225 225 def get_handler_by_name(self, name):
226 226 """Get a handler by its name."""
227 227 return self._handlers.get(name)
228 228
229 229 def get_handler_by_esc(self, esc_str):
230 230 """Get a handler by its escape string."""
231 231 return self._esc_handlers.get(esc_str)
232 232
233 233 def prefilter_line_info(self, line_info):
234 234 """Prefilter a line that has been converted to a LineInfo object."""
235 235 handler = self.find_handler(line_info)
236 236 return handler.handle(line_info)
237 237
238 238 def find_handler(self, line_info):
239 239 """Find a handler for the line_info by trying checkers."""
240 240 for checker in self.sorted_checkers:
241 241 handler = checker.check(line_info)
242 242 if handler:
243 243 return handler
244 244 return self.get_handler_by_name('normal')
245 245
246 246 def prefilter_line(self, line, continue_prompt):
247 247 """Prefilter a single input line as text."""
248 248
249 249 # All handlers *must* return a value, even if it's blank ('').
250 250
251 251 # Lines are NOT logged here. Handlers should process the line as
252 252 # needed, update the cache AND log it (so that the input cache array
253 253 # stays synced).
254 254
255 255 # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt))
256 256
257 257 # save the line away in case we crash, so the post-mortem handler can
258 258 # record it
259 259 self.shell._last_input_line = line
260 260
261 261 if not line:
262 262 # Return immediately on purely empty lines, so that if the user
263 263 # previously typed some whitespace that started a continuation
264 264 # prompt, he can break out of that loop with just an empty line.
265 265 # This is how the default python prompt works.
266 266
267 267 # Only return if the accumulated input buffer was just whitespace!
268 268 if ''.join(self.shell.buffer).isspace():
269 269 self.shell.buffer[:] = []
270 270 return ''
271 271
272 272 line_info = LineInfo(line, continue_prompt)
273 273
274 274 # the input history needs to track even empty lines
275 275 stripped = line.strip()
276 276
277 handle_normal = self.get_handler_by_name('normal')
277 normal_handler = self.get_handler_by_name('normal')
278 278 if not stripped:
279 279 if not continue_prompt:
280 280 self.shell.outputcache.prompt_count -= 1
281 281
282 return handle_normal(line_info)
282 return normal_handler.handle(line_info)
283 283
284 284 # special handlers are only allowed for single line statements
285 285 if continue_prompt and not self.multi_line_specials:
286 return handle_normal(line_info)
286 return normal_handler.handle(line_info)
287 287
288 288 return self.prefilter_line_info(line_info)
289 289
290 290 def prefilter_lines(self, lines, continue_prompt):
291 291 """Prefilter multiple input lines of text.
292 292
293 293 Covers cases where there are multiple lines in the user entry,
294 294 which is the case when the user goes back to a multiline history
295 295 entry and presses enter.
296 296 """
297 297 # growl.notify("multiline_prefilter: ", "%s\n%s" % (line, continue_prompt))
298 298 out = []
299 299 for line in lines.rstrip('\n').split('\n'):
300 300 out.append(self.prefilter_line(line, continue_prompt))
301 301 # growl.notify("multiline_prefilter return: ", '\n'.join(out))
302 302 return '\n'.join(out)
303 303
304 304
305 305 #-----------------------------------------------------------------------------
306 306 # Prefilter checkers
307 307 #-----------------------------------------------------------------------------
308 308
309 309
310 310 class PrefilterChecker(Component):
311 311 """Inspect an input line and return a handler for that line."""
312 312
313 priority = Int(100)
313 priority = Int(100, config=True)
314 314 shell = Any
315 315 prefilter_manager = Any
316 316
317 317 def __init__(self, parent, config=None):
318 318 super(PrefilterChecker, self).__init__(parent, config=config)
319 319
320 320 @auto_attr
321 321 def shell(self):
322 322 shell = Component.get_instances(
323 323 root=self.root,
324 324 klass='IPython.core.iplib.InteractiveShell'
325 325 )[0]
326 326 return shell
327 327
328 328 @auto_attr
329 329 def prefilter_manager(self):
330 330 return PrefilterManager.get_instances(root=self.root)[0]
331 331
332 332 def check(self, line_info):
333 333 """Inspect line_info and return a handler or None."""
334 334 return None
335 335
336 336
337 337 class EmacsChecker(PrefilterChecker):
338 338
339 priority = Int(100)
339 priority = Int(100, config=True)
340 340
341 341 def check(self, line_info):
342 342 "Emacs ipython-mode tags certain input lines."
343 343 if line_info.line.endswith('# PYTHON-MODE'):
344 344 return self.prefilter_manager.get_handler_by_name('emacs')
345 345 else:
346 346 return None
347 347
348 348
349 349 class ShellEscapeChecker(PrefilterChecker):
350 350
351 priority = Int(200)
351 priority = Int(200, config=True)
352 352
353 353 def check(self, line_info):
354 354 if line_info.line.lstrip().startswith(ESC_SHELL):
355 355 return self.prefilter_manager.get_handler_by_name('shell')
356 356
357 357
358 358 class IPyAutocallChecker(PrefilterChecker):
359 359
360 priority = Int(300)
360 priority = Int(300, config=True)
361 361
362 362 def check(self, line_info):
363 363 "Instances of IPyAutocall in user_ns get autocalled immediately"
364 364 obj = self.shell.user_ns.get(line_info.ifun, None)
365 365 if isinstance(obj, IPyAutocall):
366 366 obj.set_ip(self.shell)
367 367 return self.prefilter_manager.get_handler_by_name('auto')
368 368 else:
369 369 return None
370 370
371 371
372 372 class MultiLineMagicChecker(PrefilterChecker):
373 373
374 priority = Int(400)
374 priority = Int(400, config=True)
375 375
376 376 def check(self, line_info):
377 377 "Allow ! and !! in multi-line statements if multi_line_specials is on"
378 378 # Note that this one of the only places we check the first character of
379 379 # ifun and *not* the pre_char. Also note that the below test matches
380 380 # both ! and !!.
381 381 if line_info.continue_prompt \
382 382 and self.prefilter_manager.multi_line_specials:
383 383 if line_info.ifun.startswith(ESC_MAGIC):
384 384 return self.prefilter_manager.get_handler_by_name('magic')
385 385 else:
386 386 return None
387 387
388 388
389 389 class EscCharsChecker(PrefilterChecker):
390 390
391 priority = Int(500)
391 priority = Int(500, config=True)
392 392
393 393 def check(self, line_info):
394 394 """Check for escape character and return either a handler to handle it,
395 395 or None if there is no escape char."""
396 396 if line_info.line[-1] == ESC_HELP \
397 397 and line_info.pre_char != ESC_SHELL \
398 398 and line_info.pre_char != ESC_SH_CAP:
399 399 # the ? can be at the end, but *not* for either kind of shell escape,
400 400 # because a ? can be a vaild final char in a shell cmd
401 401 return self.prefilter_manager.get_handler_by_name('help')
402 402 else:
403 403 # This returns None like it should if no handler exists
404 404 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
405 405
406 406
407 407 class AssignmentChecker(PrefilterChecker):
408 408
409 priority = Int(600)
409 priority = Int(600, config=True)
410 410
411 411 def check(self, line_info):
412 412 """Check to see if user is assigning to a var for the first time, in
413 413 which case we want to avoid any sort of automagic / autocall games.
414 414
415 415 This allows users to assign to either alias or magic names true python
416 416 variables (the magic/alias systems always take second seat to true
417 417 python code). E.g. ls='hi', or ls,that=1,2"""
418 418 if line_info.the_rest and line_info.the_rest[0] in '=,':
419 419 return self.prefilter_manager.get_handler_by_name('normal')
420 420 else:
421 421 return None
422 422
423 423
424 424 class AutoMagicChecker(PrefilterChecker):
425 425
426 priority = Int(700)
426 priority = Int(700, config=True)
427 427
428 428 def check(self, line_info):
429 429 """If the ifun is magic, and automagic is on, run it. Note: normal,
430 430 non-auto magic would already have been triggered via '%' in
431 431 check_esc_chars. This just checks for automagic. Also, before
432 432 triggering the magic handler, make sure that there is nothing in the
433 433 user namespace which could shadow it."""
434 434 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
435 435 return None
436 436
437 437 # We have a likely magic method. Make sure we should actually call it.
438 438 if line_info.continue_prompt and not self.shell.multi_line_specials:
439 439 return None
440 440
441 441 head = line_info.ifun.split('.',1)[0]
442 442 if is_shadowed(head, self.shell):
443 443 return None
444 444
445 445 return self.prefilter_manager.get_handler_by_name('magic')
446 446
447 447
448 448 class AliasChecker(PrefilterChecker):
449 449
450 priority = Int(800)
450 priority = Int(800, config=True)
451 451
452 452 @auto_attr
453 453 def alias_manager(self):
454 454 return AliasManager.get_instances(root=self.root)[0]
455 455
456 456 def check(self, line_info):
457 457 "Check if the initital identifier on the line is an alias."
458 458 # Note: aliases can not contain '.'
459 459 head = line_info.ifun.split('.',1)[0]
460 460 if line_info.ifun not in self.alias_manager \
461 461 or head not in self.alias_manager \
462 462 or is_shadowed(head, self.shell):
463 463 return None
464 464
465 465 return self.prefilter_manager.get_handler_by_name('alias')
466 466
467 467
468 468 class PythonOpsChecker(PrefilterChecker):
469 469
470 priority = Int(900)
470 priority = Int(900, config=True)
471 471
472 472 def check(self, line_info):
473 473 """If the 'rest' of the line begins with a function call or pretty much
474 474 any python operator, we should simply execute the line (regardless of
475 475 whether or not there's a possible autocall expansion). This avoids
476 476 spurious (and very confusing) geattr() accesses."""
477 477 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
478 478 return self.prefilter_manager.get_handler_by_name('normal')
479 479 else:
480 480 return None
481 481
482 482
483 483 class AutocallChecker(PrefilterChecker):
484 484
485 priority = Int(1000)
485 priority = Int(1000, config=True)
486 486
487 487 def check(self, line_info):
488 488 "Check if the initial word/function is callable and autocall is on."
489 489 if not self.shell.autocall:
490 490 return None
491 491
492 492 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
493 493 if not oinfo['found']:
494 494 return None
495 495
496 496 if callable(oinfo['obj']) \
497 497 and (not re_exclude_auto.match(line_info.the_rest)) \
498 498 and re_fun_name.match(line_info.ifun):
499 499 return self.prefilter_manager.get_handler_by_name('auto')
500 500 else:
501 501 return None
502 502
503 503
504 504 #-----------------------------------------------------------------------------
505 505 # Prefilter handlers
506 506 #-----------------------------------------------------------------------------
507 507
508 508
509 509 class PrefilterHandler(Component):
510 510
511 511 handler_name = Str('normal')
512 512 esc_strings = List([])
513 513 shell = Any
514 514 prefilter_manager = Any
515 515
516 516 def __init__(self, parent, config=None):
517 517 super(PrefilterHandler, self).__init__(parent, config=config)
518 518 self.prefilter_manager.register_handler(
519 519 self.handler_name,
520 520 self,
521 521 self.esc_strings
522 522 )
523 523
524 524 @auto_attr
525 525 def shell(self):
526 526 shell = Component.get_instances(
527 527 root=self.root,
528 528 klass='IPython.core.iplib.InteractiveShell'
529 529 )[0]
530 530 return shell
531 531
532 532 @auto_attr
533 533 def prefilter_manager(self):
534 534 return PrefilterManager.get_instances(root=self.root)[0]
535 535
536 536 def handle(self, line_info):
537 537 """Handle normal input lines. Use as a template for handlers."""
538 538
539 539 # With autoindent on, we need some way to exit the input loop, and I
540 540 # don't want to force the user to have to backspace all the way to
541 541 # clear the line. The rule will be in this case, that either two
542 542 # lines of pure whitespace in a row, or a line of pure whitespace but
543 543 # of a size different to the indent level, will exit the input loop.
544 544 line = line_info.line
545 545 continue_prompt = line_info.continue_prompt
546 546
547 547 if (continue_prompt and self.shell.autoindent and line.isspace() and
548 548 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or
549 549 (self.shell.buffer[-1]).isspace() )):
550 550 line = ''
551 551
552 552 self.shell.log(line, line, continue_prompt)
553 553 return line
554 554
555 555
556 556 class AliasHandler(PrefilterHandler):
557 557
558 558 handler_name = Str('alias')
559 559 esc_strings = List([])
560 560
561 561 @auto_attr
562 562 def alias_manager(self):
563 563 return AliasManager.get_instances(root=self.root)[0]
564 564
565 565 def handle(self, line_info):
566 566 """Handle alias input lines. """
567 567 transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
568 568 # pre is needed, because it carries the leading whitespace. Otherwise
569 569 # aliases won't work in indented sections.
570 line_out = '%s_ip.system(%s)' % (line_info.pre_whitespace,
570 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
571 571 make_quoted_expr(transformed))
572 572
573 573 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
574 574 return line_out
575 575
576 576
577 577 class ShellEscapeHandler(PrefilterHandler):
578 578
579 579 handler_name = Str('shell')
580 580 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
581 581
582 582 def handle(self, line_info):
583 583 """Execute the line in a shell, empty return value"""
584 584 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
585 585
586 586 line = line_info.line
587 587 if line.lstrip().startswith(ESC_SH_CAP):
588 588 # rewrite LineInfo's line, ifun and the_rest to properly hold the
589 589 # call to %sx and the actual command to be executed, so
590 590 # handle_magic can work correctly. Note that this works even if
591 591 # the line is indented, so it handles multi_line_specials
592 592 # properly.
593 593 new_rest = line.lstrip()[2:]
594 594 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
595 595 line_info.ifun = 'sx'
596 596 line_info.the_rest = new_rest
597 597 return magic_handler.handle(line_info)
598 598 else:
599 599 cmd = line.lstrip().lstrip(ESC_SHELL)
600 line_out = '%s_ip.system(%s)' % (line_info.pre_whitespace,
600 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
601 601 make_quoted_expr(cmd))
602 602 # update cache/log and return
603 603 self.shell.log(line, line_out, line_info.continue_prompt)
604 604 return line_out
605 605
606 606
607 607 class MagicHandler(PrefilterHandler):
608 608
609 609 handler_name = Str('magic')
610 esc_strings = List(['%'])
610 esc_strings = List([ESC_MAGIC])
611 611
612 612 def handle(self, line_info):
613 613 """Execute magic functions."""
614 614 ifun = line_info.ifun
615 615 the_rest = line_info.the_rest
616 cmd = '%s_ip.magic(%s)' % (line_info.pre_whitespace,
616 cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace,
617 617 make_quoted_expr(ifun + " " + the_rest))
618 618 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
619 619 return cmd
620 620
621 621
622 622 class AutoHandler(PrefilterHandler):
623 623
624 624 handler_name = Str('auto')
625 625 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
626 626
627 627 def handle(self, line_info):
628 628 """Hande lines which can be auto-executed, quoting if requested."""
629 629 line = line_info.line
630 630 ifun = line_info.ifun
631 631 the_rest = line_info.the_rest
632 632 pre = line_info.pre
633 633 continue_prompt = line_info.continue_prompt
634 634 obj = line_info.ofind(self)['obj']
635 635
636 636 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
637 637
638 638 # This should only be active for single-line input!
639 639 if continue_prompt:
640 640 self.log(line,line,continue_prompt)
641 641 return line
642 642
643 643 force_auto = isinstance(obj, IPyAutocall)
644 644 auto_rewrite = True
645 645
646 646 if pre == ESC_QUOTE:
647 647 # Auto-quote splitting on whitespace
648 648 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
649 649 elif pre == ESC_QUOTE2:
650 650 # Auto-quote whole string
651 651 newcmd = '%s("%s")' % (ifun,the_rest)
652 652 elif pre == ESC_PAREN:
653 653 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
654 654 else:
655 655 # Auto-paren.
656 656 # We only apply it to argument-less calls if the autocall
657 657 # parameter is set to 2. We only need to check that autocall is <
658 658 # 2, since this function isn't called unless it's at least 1.
659 if not the_rest and (self.autocall < 2) and not force_auto:
659 if not the_rest and (self.shell.autocall < 2) and not force_auto:
660 660 newcmd = '%s %s' % (ifun,the_rest)
661 661 auto_rewrite = False
662 662 else:
663 663 if not force_auto and the_rest.startswith('['):
664 664 if hasattr(obj,'__getitem__'):
665 665 # Don't autocall in this case: item access for an object
666 666 # which is BOTH callable and implements __getitem__.
667 667 newcmd = '%s %s' % (ifun,the_rest)
668 668 auto_rewrite = False
669 669 else:
670 670 # if the object doesn't support [] access, go ahead and
671 671 # autocall
672 672 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
673 673 elif the_rest.endswith(';'):
674 674 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
675 675 else:
676 676 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
677 677
678 678 if auto_rewrite:
679 679 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
680 680
681 681 try:
682 682 # plain ascii works better w/ pyreadline, on some machines, so
683 683 # we use it and only print uncolored rewrite if we have unicode
684 684 rw = str(rw)
685 685 print >>Term.cout, rw
686 686 except UnicodeEncodeError:
687 687 print "-------------->" + newcmd
688 688
689 689 # log what is now valid Python, not the actual user input (without the
690 690 # final newline)
691 691 self.shell.log(line,newcmd,continue_prompt)
692 692 return newcmd
693 693
694 694
695 695 class HelpHandler(PrefilterHandler):
696 696
697 697 handler_name = Str('help')
698 698 esc_strings = List([ESC_HELP])
699 699
700 700 def handle(self, line_info):
701 701 """Try to get some help for the object.
702 702
703 703 obj? or ?obj -> basic information.
704 704 obj?? or ??obj -> more details.
705 705 """
706 706 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
707 707 line = line_info.line
708 708 # We need to make sure that we don't process lines which would be
709 709 # otherwise valid python, such as "x=1 # what?"
710 710 try:
711 711 codeop.compile_command(line)
712 712 except SyntaxError:
713 713 # We should only handle as help stuff which is NOT valid syntax
714 714 if line[0]==ESC_HELP:
715 715 line = line[1:]
716 716 elif line[-1]==ESC_HELP:
717 717 line = line[:-1]
718 718 self.shell.log(line, '#?'+line, line_info.continue_prompt)
719 719 if line:
720 720 #print 'line:<%r>' % line # dbg
721 721 self.shell.magic_pinfo(line)
722 722 else:
723 723 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
724 724 return '' # Empty string is needed here!
725 725 except:
726 726 raise
727 727 # Pass any other exceptions through to the normal handler
728 728 return normal_handler.handle(line_info)
729 729 else:
730 730 raise
731 731 # If the code compiles ok, we should handle it normally
732 732 return normal_handler.handle(line_info)
733 733
734 734
735 735 class EmacsHandler(PrefilterHandler):
736 736
737 737 handler_name = Str('emacs')
738 738 esc_strings = List([])
739 739
740 740 def handle(self, line_info):
741 741 """Handle input lines marked by python-mode."""
742 742
743 743 # Currently, nothing is done. Later more functionality can be added
744 744 # here if needed.
745 745
746 746 # The input cache shouldn't be updated
747 747 return line_info.line
748 748
749 749
750 750 #-----------------------------------------------------------------------------
751 751 # Defaults
752 752 #-----------------------------------------------------------------------------
753 753
754 754
755 755 _default_checkers = [
756 756 EmacsChecker,
757 757 ShellEscapeChecker,
758 758 IPyAutocallChecker,
759 759 MultiLineMagicChecker,
760 760 EscCharsChecker,
761 761 AssignmentChecker,
762 762 AutoMagicChecker,
763 763 AliasChecker,
764 764 PythonOpsChecker,
765 765 AutocallChecker
766 766 ]
767 767
768 768 _default_handlers = [
769 769 PrefilterHandler,
770 770 AliasHandler,
771 771 ShellEscapeHandler,
772 772 MagicHandler,
773 773 AutoHandler,
774 774 HelpHandler,
775 775 EmacsHandler
776 776 ]
777 777
@@ -1,627 +1,630 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Classes for handling input/output prompts.
4 4 """
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2008-2009 The IPython Development Team
8 8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #*****************************************************************************
13 13
14 14 #****************************************************************************
15 15 # Required modules
16 16 import __builtin__
17 17 import os
18 18 import socket
19 19 import sys
20 20 import time
21 21
22 22 # IPython's own
23 23 from IPython.utils import coloransi
24 24 from IPython.core import release
25 25 from IPython.external.Itpl import ItplNS
26 26 from IPython.core.error import TryNext
27 27 from IPython.utils.ipstruct import Struct
28 28 from IPython.core.macro import Macro
29 29 import IPython.utils.generics
30 30
31 31 from IPython.utils.genutils import *
32 32
33 33 #****************************************************************************
34 34 #Color schemes for Prompts.
35 35
36 36 PromptColors = coloransi.ColorSchemeTable()
37 37 InputColors = coloransi.InputTermColors # just a shorthand
38 38 Colors = coloransi.TermColors # just a shorthand
39 39
40 40 PromptColors.add_scheme(coloransi.ColorScheme(
41 41 'NoColor',
42 42 in_prompt = InputColors.NoColor, # Input prompt
43 43 in_number = InputColors.NoColor, # Input prompt number
44 44 in_prompt2 = InputColors.NoColor, # Continuation prompt
45 45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
46 46
47 47 out_prompt = Colors.NoColor, # Output prompt
48 48 out_number = Colors.NoColor, # Output prompt number
49 49
50 50 normal = Colors.NoColor # color off (usu. Colors.Normal)
51 51 ))
52 52
53 53 # make some schemes as instances so we can copy them for modification easily:
54 54 __PColLinux = coloransi.ColorScheme(
55 55 'Linux',
56 56 in_prompt = InputColors.Green,
57 57 in_number = InputColors.LightGreen,
58 58 in_prompt2 = InputColors.Green,
59 59 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
60 60
61 61 out_prompt = Colors.Red,
62 62 out_number = Colors.LightRed,
63 63
64 64 normal = Colors.Normal
65 65 )
66 66 # Don't forget to enter it into the table!
67 67 PromptColors.add_scheme(__PColLinux)
68 68
69 69 # Slightly modified Linux for light backgrounds
70 70 __PColLightBG = __PColLinux.copy('LightBG')
71 71
72 72 __PColLightBG.colors.update(
73 73 in_prompt = InputColors.Blue,
74 74 in_number = InputColors.LightBlue,
75 75 in_prompt2 = InputColors.Blue
76 76 )
77 77 PromptColors.add_scheme(__PColLightBG)
78 78
79 79 del Colors,InputColors
80 80
81 81 #-----------------------------------------------------------------------------
82 82 def multiple_replace(dict, text):
83 83 """ Replace in 'text' all occurences of any key in the given
84 84 dictionary by its corresponding value. Returns the new string."""
85 85
86 86 # Function by Xavier Defrang, originally found at:
87 87 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
88 88
89 89 # Create a regular expression from the dictionary keys
90 90 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
91 91 # For each match, look-up corresponding value in dictionary
92 92 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
93 93
94 94 #-----------------------------------------------------------------------------
95 95 # Special characters that can be used in prompt templates, mainly bash-like
96 96
97 97 # If $HOME isn't defined (Windows), make it an absurd string so that it can
98 98 # never be expanded out into '~'. Basically anything which can never be a
99 99 # reasonable directory name will do, we just want the $HOME -> '~' operation
100 100 # to become a no-op. We pre-compute $HOME here so it's not done on every
101 101 # prompt call.
102 102
103 103 # FIXME:
104 104
105 105 # - This should be turned into a class which does proper namespace management,
106 106 # since the prompt specials need to be evaluated in a certain namespace.
107 107 # Currently it's just globals, which need to be managed manually by code
108 108 # below.
109 109
110 110 # - I also need to split up the color schemes from the prompt specials
111 111 # somehow. I don't have a clean design for that quite yet.
112 112
113 113 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
114 114
115 115 # We precompute a few more strings here for the prompt_specials, which are
116 116 # fixed once ipython starts. This reduces the runtime overhead of computing
117 117 # prompt strings.
118 118 USER = os.environ.get("USER")
119 119 HOSTNAME = socket.gethostname()
120 120 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
121 121 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
122 122
123 123 prompt_specials_color = {
124 124 # Prompt/history count
125 125 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
126 126 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
127 127 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
128 128 # can get numbers displayed in whatever color they want.
129 129 r'\N': '${self.cache.prompt_count}',
130 130
131 131 # Prompt/history count, with the actual digits replaced by dots. Used
132 132 # mainly in continuation prompts (prompt_in2)
133 133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
134 134 # More robust form of the above expression, that uses __builtins__
135 135 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
136 136
137 137 # Current working directory
138 138 r'\w': '${os.getcwd()}',
139 139 # Current time
140 140 r'\t' : '${time.strftime("%H:%M:%S")}',
141 141 # Basename of current working directory.
142 142 # (use os.sep to make this portable across OSes)
143 143 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
144 144 # These X<N> are an extension to the normal bash prompts. They return
145 145 # N terms of the path, after replacing $HOME with '~'
146 146 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
147 147 r'\X1': '${self.cwd_filt(1)}',
148 148 r'\X2': '${self.cwd_filt(2)}',
149 149 r'\X3': '${self.cwd_filt(3)}',
150 150 r'\X4': '${self.cwd_filt(4)}',
151 151 r'\X5': '${self.cwd_filt(5)}',
152 152 # Y<N> are similar to X<N>, but they show '~' if it's the directory
153 153 # N+1 in the list. Somewhat like %cN in tcsh.
154 154 r'\Y0': '${self.cwd_filt2(0)}',
155 155 r'\Y1': '${self.cwd_filt2(1)}',
156 156 r'\Y2': '${self.cwd_filt2(2)}',
157 157 r'\Y3': '${self.cwd_filt2(3)}',
158 158 r'\Y4': '${self.cwd_filt2(4)}',
159 159 r'\Y5': '${self.cwd_filt2(5)}',
160 160 # Hostname up to first .
161 161 r'\h': HOSTNAME_SHORT,
162 162 # Full hostname
163 163 r'\H': HOSTNAME,
164 164 # Username of current user
165 165 r'\u': USER,
166 166 # Escaped '\'
167 167 '\\\\': '\\',
168 168 # Newline
169 169 r'\n': '\n',
170 170 # Carriage return
171 171 r'\r': '\r',
172 172 # Release version
173 173 r'\v': release.version,
174 174 # Root symbol ($ or #)
175 175 r'\$': ROOT_SYMBOL,
176 176 }
177 177
178 178 # A copy of the prompt_specials dictionary but with all color escapes removed,
179 179 # so we can correctly compute the prompt length for the auto_rewrite method.
180 180 prompt_specials_nocolor = prompt_specials_color.copy()
181 181 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
182 182 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
183 183
184 184 # Add in all the InputTermColors color escapes as valid prompt characters.
185 185 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
186 186 # with a color name which may begin with a letter used by any other of the
187 187 # allowed specials. This of course means that \\C will never be allowed for
188 188 # anything else.
189 189 input_colors = coloransi.InputTermColors
190 190 for _color in dir(input_colors):
191 191 if _color[0] != '_':
192 192 c_name = r'\C_'+_color
193 193 prompt_specials_color[c_name] = getattr(input_colors,_color)
194 194 prompt_specials_nocolor[c_name] = ''
195 195
196 196 # we default to no color for safety. Note that prompt_specials is a global
197 197 # variable used by all prompt objects.
198 198 prompt_specials = prompt_specials_nocolor
199 199
200 200 #-----------------------------------------------------------------------------
201 201 def str_safe(arg):
202 202 """Convert to a string, without ever raising an exception.
203 203
204 204 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
205 205 error message."""
206 206
207 207 try:
208 208 out = str(arg)
209 209 except UnicodeError:
210 210 try:
211 211 out = arg.encode('utf_8','replace')
212 212 except Exception,msg:
213 213 # let's keep this little duplication here, so that the most common
214 214 # case doesn't suffer from a double try wrapping.
215 215 out = '<ERROR: %s>' % msg
216 216 except Exception,msg:
217 217 out = '<ERROR: %s>' % msg
218 218 return out
219 219
220 220 class BasePrompt(object):
221 221 """Interactive prompt similar to Mathematica's."""
222 222
223 223 def _get_p_template(self):
224 224 return self._p_template
225 225
226 226 def _set_p_template(self,val):
227 227 self._p_template = val
228 228 self.set_p_str()
229 229
230 230 p_template = property(_get_p_template,_set_p_template,
231 231 doc='Template for prompt string creation')
232 232
233 233 def __init__(self,cache,sep,prompt,pad_left=False):
234 234
235 235 # Hack: we access information about the primary prompt through the
236 236 # cache argument. We need this, because we want the secondary prompt
237 237 # to be aligned with the primary one. Color table info is also shared
238 238 # by all prompt classes through the cache. Nice OO spaghetti code!
239 239 self.cache = cache
240 240 self.sep = sep
241 241
242 242 # regexp to count the number of spaces at the end of a prompt
243 243 # expression, useful for prompt auto-rewriting
244 244 self.rspace = re.compile(r'(\s*)$')
245 245 # Flag to left-pad prompt strings to match the length of the primary
246 246 # prompt
247 247 self.pad_left = pad_left
248 248
249 249 # Set template to create each actual prompt (where numbers change).
250 250 # Use a property
251 251 self.p_template = prompt
252 252 self.set_p_str()
253 253
254 254 def set_p_str(self):
255 255 """ Set the interpolating prompt strings.
256 256
257 257 This must be called every time the color settings change, because the
258 258 prompt_specials global may have changed."""
259 259
260 260 import os,time # needed in locals for prompt string handling
261 261 loc = locals()
262 262 try:
263 263 self.p_str = ItplNS('%s%s%s' %
264 264 ('${self.sep}${self.col_p}',
265 265 multiple_replace(prompt_specials, self.p_template),
266 266 '${self.col_norm}'),self.cache.user_ns,loc)
267 267
268 268 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
269 269 self.p_template),
270 270 self.cache.user_ns,loc)
271 271 except:
272 272 print "Illegal prompt template (check $ usage!):",self.p_template
273 273 self.p_str = self.p_template
274 274 self.p_str_nocolor = self.p_template
275 275
276 276 def write(self,msg): # dbg
277 277 sys.stdout.write(msg)
278 278 return ''
279 279
280 280 def __str__(self):
281 281 """Return a string form of the prompt.
282 282
283 283 This for is useful for continuation and output prompts, since it is
284 284 left-padded to match lengths with the primary one (if the
285 285 self.pad_left attribute is set)."""
286 286
287 287 out_str = str_safe(self.p_str)
288 288 if self.pad_left:
289 289 # We must find the amount of padding required to match lengths,
290 290 # taking the color escapes (which are invisible on-screen) into
291 291 # account.
292 292 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
293 293 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
294 294 return format % out_str
295 295 else:
296 296 return out_str
297 297
298 298 # these path filters are put in as methods so that we can control the
299 299 # namespace where the prompt strings get evaluated
300 300 def cwd_filt(self,depth):
301 301 """Return the last depth elements of the current working directory.
302 302
303 303 $HOME is always replaced with '~'.
304 304 If depth==0, the full path is returned."""
305 305
306 306 cwd = os.getcwd().replace(HOME,"~")
307 307 out = os.sep.join(cwd.split(os.sep)[-depth:])
308 308 if out:
309 309 return out
310 310 else:
311 311 return os.sep
312 312
313 313 def cwd_filt2(self,depth):
314 314 """Return the last depth elements of the current working directory.
315 315
316 316 $HOME is always replaced with '~'.
317 317 If depth==0, the full path is returned."""
318 318
319 319 full_cwd = os.getcwd()
320 320 cwd = full_cwd.replace(HOME,"~").split(os.sep)
321 321 if '~' in cwd and len(cwd) == depth+1:
322 322 depth += 1
323 323 drivepart = ''
324 324 if sys.platform == 'win32' and len(cwd) > depth:
325 325 drivepart = os.path.splitdrive(full_cwd)[0]
326 326 out = drivepart + '/'.join(cwd[-depth:])
327 327
328 328 if out:
329 329 return out
330 330 else:
331 331 return os.sep
332 332
333 333 def __nonzero__(self):
334 334 """Implement boolean behavior.
335 335
336 336 Checks whether the p_str attribute is non-empty"""
337 337
338 338 return bool(self.p_template)
339 339
340 340 class Prompt1(BasePrompt):
341 341 """Input interactive prompt similar to Mathematica's."""
342 342
343 343 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
344 344 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
345 345
346 346 def set_colors(self):
347 347 self.set_p_str()
348 348 Colors = self.cache.color_table.active_colors # shorthand
349 349 self.col_p = Colors.in_prompt
350 350 self.col_num = Colors.in_number
351 351 self.col_norm = Colors.in_normal
352 352 # We need a non-input version of these escapes for the '--->'
353 353 # auto-call prompts used in the auto_rewrite() method.
354 354 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
355 355 self.col_norm_ni = Colors.normal
356 356
357 357 def __str__(self):
358 358 self.cache.prompt_count += 1
359 359 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
360 360 return str_safe(self.p_str)
361 361
362 362 def auto_rewrite(self):
363 363 """Print a string of the form '--->' which lines up with the previous
364 364 input string. Useful for systems which re-write the user input when
365 365 handling automatically special syntaxes."""
366 366
367 367 curr = str(self.cache.last_prompt)
368 368 nrspaces = len(self.rspace.search(curr).group())
369 369 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
370 370 ' '*nrspaces,self.col_norm_ni)
371 371
372 372 class PromptOut(BasePrompt):
373 373 """Output interactive prompt similar to Mathematica's."""
374 374
375 375 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
376 376 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
377 377 if not self.p_template:
378 378 self.__str__ = lambda: ''
379 379
380 380 def set_colors(self):
381 381 self.set_p_str()
382 382 Colors = self.cache.color_table.active_colors # shorthand
383 383 self.col_p = Colors.out_prompt
384 384 self.col_num = Colors.out_number
385 385 self.col_norm = Colors.normal
386 386
387 387 class Prompt2(BasePrompt):
388 388 """Interactive continuation prompt."""
389 389
390 390 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
391 391 self.cache = cache
392 392 self.p_template = prompt
393 393 self.pad_left = pad_left
394 394 self.set_p_str()
395 395
396 396 def set_p_str(self):
397 397 import os,time # needed in locals for prompt string handling
398 398 loc = locals()
399 399 self.p_str = ItplNS('%s%s%s' %
400 400 ('${self.col_p2}',
401 401 multiple_replace(prompt_specials, self.p_template),
402 402 '$self.col_norm'),
403 403 self.cache.user_ns,loc)
404 404 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
405 405 self.p_template),
406 406 self.cache.user_ns,loc)
407 407
408 408 def set_colors(self):
409 409 self.set_p_str()
410 410 Colors = self.cache.color_table.active_colors
411 411 self.col_p2 = Colors.in_prompt2
412 412 self.col_norm = Colors.in_normal
413 413 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
414 414 # updated their prompt_in2 definitions. Remove eventually.
415 415 self.col_p = Colors.out_prompt
416 416 self.col_num = Colors.out_number
417 417
418 418
419 419 #-----------------------------------------------------------------------------
420 420 class CachedOutput:
421 421 """Class for printing output from calculations while keeping a cache of
422 422 reults. It dynamically creates global variables prefixed with _ which
423 423 contain these results.
424 424
425 425 Meant to be used as a sys.displayhook replacement, providing numbered
426 426 prompts and cache services.
427 427
428 428 Initialize with initial and final values for cache counter (this defines
429 429 the maximum size of the cache."""
430 430
431 431 def __init__(self,shell,cache_size,Pprint,
432 432 colors='NoColor',input_sep='\n',
433 433 output_sep='\n',output_sep2='',
434 434 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
435 435
436 436 cache_size_min = 3
437 437 if cache_size <= 0:
438 438 self.do_full_cache = 0
439 439 cache_size = 0
440 440 elif cache_size < cache_size_min:
441 441 self.do_full_cache = 0
442 442 cache_size = 0
443 443 warn('caching was disabled (min value for cache size is %s).' %
444 444 cache_size_min,level=3)
445 445 else:
446 446 self.do_full_cache = 1
447 447
448 448 self.cache_size = cache_size
449 449 self.input_sep = input_sep
450 450
451 451 # we need a reference to the user-level namespace
452 452 self.shell = shell
453 453 self.user_ns = shell.user_ns
454 454 # and to the user's input
455 455 self.input_hist = shell.input_hist
456 456 # and to the user's logger, for logging output
457 457 self.logger = shell.logger
458 458
459 459 # Set input prompt strings and colors
460 460 if cache_size == 0:
461 461 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
462 462 or ps1.find(r'\N') > -1:
463 463 ps1 = '>>> '
464 464 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
465 465 or ps2.find(r'\N') > -1:
466 466 ps2 = '... '
467 467 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
468 468 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
469 469 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
470 470
471 471 self.color_table = PromptColors
472 472 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
473 473 pad_left=pad_left)
474 474 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
475 475 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
476 476 pad_left=pad_left)
477 477 self.set_colors(colors)
478 478
479 479 # other more normal stuff
480 480 # b/c each call to the In[] prompt raises it by 1, even the first.
481 481 self.prompt_count = 0
482 482 # Store the last prompt string each time, we need it for aligning
483 483 # continuation and auto-rewrite prompts
484 484 self.last_prompt = ''
485 485 self.Pprint = Pprint
486 486 self.output_sep = output_sep
487 487 self.output_sep2 = output_sep2
488 488 self._,self.__,self.___ = '','',''
489 489 self.pprint_types = map(type,[(),[],{}])
490 490
491 491 # these are deliberately global:
492 492 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
493 493 self.user_ns.update(to_user_ns)
494 494
495 495 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
496 496 if p_str is None:
497 497 if self.do_full_cache:
498 498 return cache_def
499 499 else:
500 500 return no_cache_def
501 501 else:
502 502 return p_str
503 503
504 504 def set_colors(self,colors):
505 505 """Set the active color scheme and configure colors for the three
506 506 prompt subsystems."""
507 507
508 508 # FIXME: the prompt_specials global should be gobbled inside this
509 509 # class instead. Do it when cleaning up the whole 3-prompt system.
510 510 global prompt_specials
511 511 if colors.lower()=='nocolor':
512 512 prompt_specials = prompt_specials_nocolor
513 513 else:
514 514 prompt_specials = prompt_specials_color
515 515
516 516 self.color_table.set_active_scheme(colors)
517 517 self.prompt1.set_colors()
518 518 self.prompt2.set_colors()
519 519 self.prompt_out.set_colors()
520 520
521 521 def __call__(self,arg=None):
522 522 """Printing with history cache management.
523 523
524 524 This is invoked everytime the interpreter needs to print, and is
525 525 activated by setting the variable sys.displayhook to it."""
526 526
527 527 # If something injected a '_' variable in __builtin__, delete
528 528 # ipython's automatic one so we don't clobber that. gettext() in
529 529 # particular uses _, so we need to stay away from it.
530 530 if '_' in __builtin__.__dict__:
531 531 try:
532 532 del self.user_ns['_']
533 533 except KeyError:
534 534 pass
535 535 if arg is not None:
536 536 cout_write = Term.cout.write # fast lookup
537 537 # first handle the cache and counters
538 538
539 539 # do not print output if input ends in ';'
540 540 try:
541 541 if self.input_hist[self.prompt_count].endswith(';\n'):
542 542 return
543 543 except IndexError:
544 544 # some uses of ipshellembed may fail here
545 545 pass
546 546 # don't use print, puts an extra space
547 547 cout_write(self.output_sep)
548 548 outprompt = self.shell.hooks.generate_output_prompt()
549 # print "Got prompt: ", outprompt
549 550 if self.do_full_cache:
550 551 cout_write(outprompt)
552 else:
553 print "self.do_full_cache = False"
551 554
552 555 # and now call a possibly user-defined print mechanism
553 556 manipulated_val = self.display(arg)
554 557
555 558 # user display hooks can change the variable to be stored in
556 559 # output history
557 560
558 561 if manipulated_val is not None:
559 562 arg = manipulated_val
560 563
561 564 # avoid recursive reference when displaying _oh/Out
562 565 if arg is not self.user_ns['_oh']:
563 566 self.update(arg)
564 567
565 568 if self.logger.log_output:
566 569 self.logger.log_write(repr(arg),'output')
567 570 cout_write(self.output_sep2)
568 571 Term.cout.flush()
569 572
570 573 def _display(self,arg):
571 574 """Default printer method, uses pprint.
572 575
573 576 Do ip.set_hook("result_display", my_displayhook) for custom result
574 577 display, e.g. when your own objects need special formatting.
575 578 """
576 579 try:
577 580 return IPython.utils.generics.result_display(arg)
578 581 except TryNext:
579 582 return self.shell.hooks.result_display(arg)
580 583
581 584 # Assign the default display method:
582 585 display = _display
583 586
584 587 def update(self,arg):
585 588 #print '***cache_count', self.cache_count # dbg
586 589 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
587 590 warn('Output cache limit (currently '+
588 591 `self.cache_size`+' entries) hit.\n'
589 592 'Flushing cache and resetting history counter...\n'
590 593 'The only history variables available will be _,__,___ and _1\n'
591 594 'with the current result.')
592 595
593 596 self.flush()
594 597 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
595 598 # we cause buggy behavior for things like gettext).
596 599 if '_' not in __builtin__.__dict__:
597 600 self.___ = self.__
598 601 self.__ = self._
599 602 self._ = arg
600 603 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
601 604
602 605 # hackish access to top-level namespace to create _1,_2... dynamically
603 606 to_main = {}
604 607 if self.do_full_cache:
605 608 new_result = '_'+`self.prompt_count`
606 609 to_main[new_result] = arg
607 610 self.user_ns.update(to_main)
608 611 self.user_ns['_oh'][self.prompt_count] = arg
609 612
610 613 def flush(self):
611 614 if not self.do_full_cache:
612 615 raise ValueError,"You shouldn't have reached the cache flush "\
613 616 "if full caching is not enabled!"
614 617 # delete auto-generated vars from global namespace
615 618
616 619 for n in range(1,self.prompt_count + 1):
617 620 key = '_'+`n`
618 621 try:
619 622 del self.user_ns[key]
620 623 except: pass
621 624 self.user_ns['_oh'].clear()
622 625
623 626 if '_' not in __builtin__.__dict__:
624 627 self.user_ns.update({'_':None,'__':None, '___':None})
625 628 import gc
626 629 gc.collect() # xxx needed?
627 630
@@ -1,34 +1,35 b''
1 1 """Test code for https://bugs.launchpad.net/ipython/+bug/239054
2 2
3 3 WARNING: this script exits IPython! It MUST be run in a subprocess.
4 4
5 5 When you run the following script from CPython it prints:
6 6 __init__ is here
7 7 __del__ is here
8 8
9 9 and creates the __del__.txt file
10 10
11 11 When you run it from IPython it prints:
12 12 __init__ is here
13 13
14 14 When you exit() or Exit from IPython neothing is printed and no file is created
15 15 (the file thing is to make sure __del__ is really never called and not that
16 16 just the output is eaten).
17 17
18 18 Note that if you call %reset in IPython then everything is Ok.
19 19
20 20 IPython should do the equivalent of %reset and release all the references it
21 21 holds before exit. This behavior is important when working with binding objects
22 22 that rely on __del__. If the current behavior has some use case then I suggest
23 23 to add a configuration option to IPython to control it.
24 24 """
25 25 import sys
26 26
27 27 class A(object):
28 28 def __del__(self):
29 29 print 'obj_del.py: object A deleted'
30 30
31 31 a = A()
32 32
33 33 # Now, we force an exit, the caller will check that the del printout was given
34 _ip = get_ipython()
34 35 _ip.ask_exit()
@@ -1,194 +1,194 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Tests for IPython.core.component
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Fernando Perez (design help)
10 10 """
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Copyright (C) 2008-2009 The IPython Development Team
14 14 #
15 15 # Distributed under the terms of the BSD License. The full license is in
16 16 # the file COPYING, distributed as part of this software.
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22
23 23 from unittest import TestCase
24 24
25 25 from IPython.core.component import Component, ComponentError
26 26 from IPython.utils.traitlets import (
27 27 TraitletError, Int, Float, Str
28 28 )
29 from IPython.utils.ipstruct import Struct
29 from IPython.config.loader import Config
30 30
31 31
32 32 #-----------------------------------------------------------------------------
33 33 # Test cases
34 34 #-----------------------------------------------------------------------------
35 35
36 36
37 37 class TestComponentMeta(TestCase):
38 38
39 39 def test_get_instances(self):
40 40 class BaseComponent(Component):
41 41 pass
42 42 c1 = BaseComponent(None)
43 43 c2 = BaseComponent(c1)
44 44 self.assertEquals(BaseComponent.get_instances(),[c1,c2])
45 45
46 46 def test_get_instances_subclass(self):
47 47 class MyComponent(Component):
48 48 pass
49 49 class MyOtherComponent(MyComponent):
50 50 pass
51 51 c1 = MyComponent(None)
52 52 c2 = MyOtherComponent(c1)
53 53 c3 = MyOtherComponent(c2)
54 54 self.assertEquals(MyComponent.get_instances(), [c1, c2, c3])
55 55 self.assertEquals(MyOtherComponent.get_instances(), [c2, c3])
56 56
57 57 def test_get_instances_root(self):
58 58 class MyComponent(Component):
59 59 pass
60 60 class MyOtherComponent(MyComponent):
61 61 pass
62 62 c1 = MyComponent(None)
63 63 c2 = MyOtherComponent(c1)
64 64 c3 = MyOtherComponent(c2)
65 65 c4 = MyComponent(None)
66 66 c5 = MyComponent(c4)
67 67 self.assertEquals(MyComponent.get_instances(root=c1), [c1, c2, c3])
68 68 self.assertEquals(MyComponent.get_instances(root=c4), [c4, c5])
69 69
70 70
71 71 class TestComponent(TestCase):
72 72
73 73 def test_parent_child(self):
74 74 c1 = Component(None)
75 75 c2 = Component(c1)
76 76 c3 = Component(c1)
77 77 c4 = Component(c3)
78 78 self.assertEquals(c1.parent, None)
79 79 self.assertEquals(c2.parent, c1)
80 80 self.assertEquals(c3.parent, c1)
81 81 self.assertEquals(c4.parent, c3)
82 82 self.assertEquals(c1.children, [c2, c3])
83 83 self.assertEquals(c2.children, [])
84 84 self.assertEquals(c3.children, [c4])
85 85 self.assertEquals(c4.children, [])
86 86
87 87 def test_root(self):
88 88 c1 = Component(None)
89 89 c2 = Component(c1)
90 90 c3 = Component(c1)
91 91 c4 = Component(c3)
92 92 self.assertEquals(c1.root, c1.root)
93 93 self.assertEquals(c2.root, c1)
94 94 self.assertEquals(c3.root, c1)
95 95 self.assertEquals(c4.root, c1)
96 96
97 97 def test_change_parent(self):
98 98 c1 = Component(None)
99 99 c2 = Component(None)
100 100 c3 = Component(c1)
101 101 self.assertEquals(c3.root, c1)
102 102 self.assertEquals(c3.parent, c1)
103 103 self.assertEquals(c1.children,[c3])
104 104 c3.parent = c2
105 105 self.assertEquals(c3.root, c2)
106 106 self.assertEquals(c3.parent, c2)
107 107 self.assertEquals(c2.children,[c3])
108 108 self.assertEquals(c1.children,[])
109 109
110 110 def test_subclass_parent(self):
111 111 c1 = Component(None)
112 112 self.assertRaises(TraitletError, setattr, c1, 'parent', 10)
113 113
114 114 class MyComponent(Component):
115 115 pass
116 116 c1 = Component(None)
117 117 c2 = MyComponent(c1)
118 118 self.assertEquals(MyComponent.parent.this_class, Component)
119 119 self.assertEquals(c2.parent, c1)
120 120
121 121 def test_bad_root(self):
122 122 c1 = Component(None)
123 123 c2 = Component(None)
124 124 c3 = Component(None)
125 125 self.assertRaises(ComponentError, setattr, c1, 'root', c2)
126 126 c1.parent = c2
127 127 self.assertEquals(c1.root, c2)
128 128 self.assertRaises(ComponentError, setattr, c1, 'root', c3)
129 129
130 130
131 131 class TestComponentConfig(TestCase):
132 132
133 133 def test_default(self):
134 134 c1 = Component(None)
135 135 c2 = Component(c1)
136 136 c3 = Component(c2)
137 137 self.assertEquals(c1.config, c2.config)
138 138 self.assertEquals(c2.config, c3.config)
139 139
140 140 def test_custom(self):
141 config = Struct()
142 config.FOO = 'foo'
143 config.BAR = 'bar'
141 config = Config()
142 config.foo = 'foo'
143 config.bar = 'bar'
144 144 c1 = Component(None, config=config)
145 145 c2 = Component(c1)
146 146 c3 = Component(c2)
147 147 self.assertEquals(c1.config, config)
148 148 self.assertEquals(c2.config, config)
149 149 self.assertEquals(c3.config, config)
150 150 # Test that we always make copies
151 151 self.assert_(c1.config is not config)
152 152 self.assert_(c2.config is not config)
153 153 self.assert_(c3.config is not config)
154 154 self.assert_(c1.config is not c2.config)
155 155 self.assert_(c2.config is not c3.config)
156 156
157 157 def test_inheritance(self):
158 158 class MyComponent(Component):
159 a = Int(1, config_key='A')
160 b = Float(1.0, config_key='B')
159 a = Int(1, config=True)
160 b = Float(1.0, config=True)
161 161 c = Str('no config')
162 config = Struct()
163 config.A = 2
164 config.B = 2.0
162 config = Config()
163 config.MyComponent.a = 2
164 config.MyComponent.b = 2.0
165 165 c1 = MyComponent(None, config=config)
166 166 c2 = MyComponent(c1)
167 self.assertEquals(c1.a, config.A)
168 self.assertEquals(c1.b, config.B)
169 self.assertEquals(c2.a, config.A)
170 self.assertEquals(c2.b, config.B)
171 c4 = MyComponent(c2, config=Struct())
167 self.assertEquals(c1.a, config.MyComponent.a)
168 self.assertEquals(c1.b, config.MyComponent.b)
169 self.assertEquals(c2.a, config.MyComponent.a)
170 self.assertEquals(c2.b, config.MyComponent.b)
171 c4 = MyComponent(c2, config=Config())
172 172 self.assertEquals(c4.a, 1)
173 173 self.assertEquals(c4.b, 1.0)
174 174
175 175 class TestComponentName(TestCase):
176 176
177 177 def test_default(self):
178 178 class MyComponent(Component):
179 179 pass
180 180 c1 = Component(None)
181 181 c2 = MyComponent(None)
182 182 c3 = Component(c2)
183 183 self.assertNotEquals(c1.name, c2.name)
184 184 self.assertNotEquals(c1.name, c3.name)
185 185
186 186 def test_manual(self):
187 187 class MyComponent(Component):
188 188 pass
189 189 c1 = Component(None, name='foo')
190 190 c2 = MyComponent(None, name='bar')
191 191 c3 = Component(c2, name='bah')
192 192 self.assertEquals(c1.name, 'foo')
193 193 self.assertEquals(c2.name, 'bar')
194 194 self.assertEquals(c3.name, 'bah')
@@ -1,326 +1,331 b''
1 1 """Tests for various magic functions.
2 2
3 3 Needs to be run by nose (to make ipython session available).
4 4 """
5 5
6 6 import os
7 7 import sys
8 8 import tempfile
9 9 import types
10 10 from cStringIO import StringIO
11 11
12 12 import nose.tools as nt
13 13
14 14 from IPython.utils.platutils import find_cmd, get_long_path_name
15 15 from IPython.testing import decorators as dec
16 16 from IPython.testing import tools as tt
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Test functions begin
20 20
21 21 def test_rehashx():
22 22 # clear up everything
23 _ip.alias_table.clear()
23 _ip = get_ipython()
24 _ip.alias_manager.alias_table.clear()
24 25 del _ip.db['syscmdlist']
25 26
26 27 _ip.magic('rehashx')
27 28 # Practically ALL ipython development systems will have more than 10 aliases
28 29
29 yield (nt.assert_true, len(_ip.alias_table) > 10)
30 for key, val in _ip.alias_table.items():
30 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
31 for key, val in _ip.alias_manager.alias_table.items():
31 32 # we must strip dots from alias names
32 33 nt.assert_true('.' not in key)
33 34
34 35 # rehashx must fill up syscmdlist
35 36 scoms = _ip.db['syscmdlist']
36 37 yield (nt.assert_true, len(scoms) > 10)
37 38
38 39
39 40 def doctest_hist_f():
40 41 """Test %hist -f with temporary filename.
41 42
42 43 In [9]: import tempfile
43 44
44 45 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
45 46
46 47 In [11]: %hist -n -f $tfile 3
47 48 """
48 49
49 50
50 51 def doctest_hist_r():
51 52 """Test %hist -r
52 53
53 54 XXX - This test is not recording the output correctly. Not sure why...
54 55
55 56 In [20]: 'hist' in _ip.lsmagic()
56 57 Out[20]: True
57 58
58 59 In [6]: x=1
59 60
60 61 In [7]: %hist -n -r 2
61 62 x=1 # random
62 63 hist -n -r 2 # random
63 64 """
64 65
65 66 # This test is known to fail on win32.
66 67 # See ticket https://bugs.launchpad.net/bugs/366334
67 68 def test_obj_del():
69 _ip = get_ipython()
68 70 """Test that object's __del__ methods are called on exit."""
69 71 test_dir = os.path.dirname(__file__)
70 72 del_file = os.path.join(test_dir,'obj_del.py')
71 73 ipython_cmd = find_cmd('ipython')
72 74 out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))
73 75 nt.assert_equals(out,'obj_del.py: object A deleted')
74 76
75 77
76 78 def test_shist():
77 79 # Simple tests of ShadowHist class - test generator.
78 80 import os, shutil, tempfile
79 81
80 82 from IPython.extensions import pickleshare
81 83 from IPython.core.history import ShadowHist
82 84
83 85 tfile = tempfile.mktemp('','tmp-ipython-')
84 86
85 87 db = pickleshare.PickleShareDB(tfile)
86 88 s = ShadowHist(db)
87 89 s.add('hello')
88 90 s.add('world')
89 91 s.add('hello')
90 92 s.add('hello')
91 93 s.add('karhu')
92 94
93 95 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
94 96
95 97 yield nt.assert_equal,s.get(2),'world'
96 98
97 99 shutil.rmtree(tfile)
98 100
99 101 @dec.skipif_not_numpy
100 102 def test_numpy_clear_array_undec():
101 103 from IPython.extensions import clearcmd
102 104
103 105 _ip.ex('import numpy as np')
104 106 _ip.ex('a = np.empty(2)')
105 107 yield (nt.assert_true, 'a' in _ip.user_ns)
106 108 _ip.magic('clear array')
107 109 yield (nt.assert_false, 'a' in _ip.user_ns)
108 110
109 111
110 112 @dec.skip()
111 113 def test_fail_dec(*a,**k):
112 114 yield nt.assert_true, False
113 115
114 116 @dec.skip('This one shouldn not run')
115 117 def test_fail_dec2(*a,**k):
116 118 yield nt.assert_true, False
117 119
118 120 @dec.skipknownfailure
119 121 def test_fail_dec3(*a,**k):
120 122 yield nt.assert_true, False
121 123
122 124
123 125 def doctest_refbug():
124 126 """Very nasty problem with references held by multiple runs of a script.
125 127 See: https://bugs.launchpad.net/ipython/+bug/269966
126 128
127 129 In [1]: _ip.clear_main_mod_cache()
128 130
129 131 In [2]: run refbug
130 132
131 133 In [3]: call_f()
132 134 lowercased: hello
133 135
134 136 In [4]: run refbug
135 137
136 138 In [5]: call_f()
137 139 lowercased: hello
138 140 lowercased: hello
139 141 """
140 142
141 143 #-----------------------------------------------------------------------------
142 144 # Tests for %run
143 145 #-----------------------------------------------------------------------------
144 146
145 147 # %run is critical enough that it's a good idea to have a solid collection of
146 148 # tests for it, some as doctests and some as normal tests.
147 149
148 150 def doctest_run_ns():
149 151 """Classes declared %run scripts must be instantiable afterwards.
150 152
151 153 In [11]: run tclass foo
152 154
153 155 In [12]: isinstance(f(),foo)
154 156 Out[12]: True
155 157 """
156 158
157 159
158 160 def doctest_run_ns2():
159 161 """Classes declared %run scripts must be instantiable afterwards.
160 162
161 163 In [4]: run tclass C-first_pass
162 164
163 165 In [5]: run tclass C-second_pass
164 166 tclass.py: deleting object: C-first_pass
165 167 """
166 168
167 169 def doctest_run_builtins():
168 170 """Check that %run doesn't damage __builtins__ via a doctest.
169 171
170 172 This is similar to the test_run_builtins, but I want *both* forms of the
171 173 test to catch any possible glitches in our testing machinery, since that
172 174 modifies %run somewhat. So for this, we have both a normal test (below)
173 175 and a doctest (this one).
174 176
175 177 In [1]: import tempfile
176 178
177 179 In [2]: bid1 = id(__builtins__)
178 180
179 181 In [3]: fname = tempfile.mkstemp()[1]
180 182
181 183 In [3]: f = open(fname,'w')
182 184
183 185 In [4]: f.write('pass\\n')
184 186
185 187 In [5]: f.flush()
186 188
187 189 In [6]: print type(__builtins__)
188 190 <type 'module'>
189 191
190 192 In [7]: %run "$fname"
191 193
192 194 In [7]: f.close()
193 195
194 196 In [8]: bid2 = id(__builtins__)
195 197
196 198 In [9]: print type(__builtins__)
197 199 <type 'module'>
198 200
199 201 In [10]: bid1 == bid2
200 202 Out[10]: True
201 203
202 204 In [12]: try:
203 205 ....: os.unlink(fname)
204 206 ....: except:
205 207 ....: pass
206 208 ....:
207 209 """
208 210
209 211 # For some tests, it will be handy to organize them in a class with a common
210 212 # setup that makes a temp file
211 213
212 214 class TestMagicRun(object):
213 215
214 216 def setup(self):
215 217 """Make a valid python temp file."""
216 218 fname = tempfile.mkstemp()[1]
217 219 f = open(fname,'w')
218 220 f.write('pass\n')
219 221 f.flush()
220 222 self.tmpfile = f
221 223 self.fname = fname
222 224
223 225 def run_tmpfile(self):
226 _ip = get_ipython()
224 227 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
225 228 # See below and ticket https://bugs.launchpad.net/bugs/366353
226 229 _ip.magic('run "%s"' % self.fname)
227 230
228 231 def test_builtins_id(self):
229 232 """Check that %run doesn't damage __builtins__ """
230
233 _ip = get_ipython()
231 234 # Test that the id of __builtins__ is not modified by %run
232 235 bid1 = id(_ip.user_ns['__builtins__'])
233 236 self.run_tmpfile()
234 237 bid2 = id(_ip.user_ns['__builtins__'])
235 238 tt.assert_equals(bid1, bid2)
236 239
237 240 def test_builtins_type(self):
238 241 """Check that the type of __builtins__ doesn't change with %run.
239 242
240 243 However, the above could pass if __builtins__ was already modified to
241 244 be a dict (it should be a module) by a previous use of %run. So we
242 245 also check explicitly that it really is a module:
243 246 """
247 _ip = get_ipython()
244 248 self.run_tmpfile()
245 249 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
246 250
247 251 def test_prompts(self):
248 252 """Test that prompts correctly generate after %run"""
249 253 self.run_tmpfile()
254 _ip = get_ipython()
250 255 p2 = str(_ip.outputcache.prompt2).strip()
251 256 nt.assert_equals(p2[:3], '...')
252 257
253 258 def teardown(self):
254 259 self.tmpfile.close()
255 260 try:
256 261 os.unlink(self.fname)
257 262 except:
258 263 # On Windows, even though we close the file, we still can't delete
259 264 # it. I have no clue why
260 265 pass
261 266
262 267 # Multiple tests for clipboard pasting
263 268 def test_paste():
264
269 _ip = get_ipython()
265 270 def paste(txt, flags='-q'):
266 271 """Paste input text, by default in quiet mode"""
267 272 hooks.clipboard_get = lambda : txt
268 273 _ip.magic('paste '+flags)
269 274
270 275 # Inject fake clipboard hook but save original so we can restore it later
271 276 hooks = _ip.hooks
272 277 user_ns = _ip.user_ns
273 278 original_clip = hooks.clipboard_get
274 279
275 280 try:
276 281 # This try/except with an emtpy except clause is here only because
277 282 # try/yield/finally is invalid syntax in Python 2.4. This will be
278 283 # removed when we drop 2.4-compatibility, and the emtpy except below
279 284 # will be changed to a finally.
280 285
281 286 # Run tests with fake clipboard function
282 287 user_ns.pop('x', None)
283 288 paste('x=1')
284 289 yield (nt.assert_equal, user_ns['x'], 1)
285 290
286 291 user_ns.pop('x', None)
287 292 paste('>>> x=2')
288 293 yield (nt.assert_equal, user_ns['x'], 2)
289 294
290 295 paste("""
291 296 >>> x = [1,2,3]
292 297 >>> y = []
293 298 >>> for i in x:
294 299 ... y.append(i**2)
295 300 ...
296 301 """)
297 302 yield (nt.assert_equal, user_ns['x'], [1,2,3])
298 303 yield (nt.assert_equal, user_ns['y'], [1,4,9])
299 304
300 305 # Now, test that paste -r works
301 306 user_ns.pop('x', None)
302 307 yield (nt.assert_false, 'x' in user_ns)
303 308 _ip.magic('paste -r')
304 309 yield (nt.assert_equal, user_ns['x'], [1,2,3])
305 310
306 311 # Also test paste echoing, by temporarily faking the writer
307 312 w = StringIO()
308 313 writer = _ip.write
309 314 _ip.write = w.write
310 315 code = """
311 316 a = 100
312 317 b = 200"""
313 318 try:
314 319 paste(code,'')
315 320 out = w.getvalue()
316 321 finally:
317 322 _ip.write = writer
318 323 yield (nt.assert_equal, user_ns['a'], 100)
319 324 yield (nt.assert_equal, user_ns['b'], 200)
320 325 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
321 326
322 327 finally:
323 328 # This should be in a finally clause, instead of the bare except above.
324 329 # Restore original hook
325 330 hooks.clipboard_get = original_clip
326 331
@@ -1,58 +1,58 b''
1 1 # -*- coding: utf-8 -*-
2 2 """ IPython extension: new prefilters for output grabbing
3 3
4 4 Provides
5 5
6 6 var = %magic blah blah
7 7
8 8 var = !ls
9 9 """
10 10
11 11 from IPython.core import ipapi
12 12 from IPython.core.error import TryNext
13 13 from IPython.utils.genutils import *
14 14
15 15 ip = ipapi.get()
16 16
17 17 import re
18 18
19 19 def hnd_magic(line,mo):
20 20 """ Handle a = %mymagic blah blah """
21 21 var = mo.group('varname')
22 22 cmd = mo.group('cmd')
23 23 expr = make_quoted_expr(cmd)
24 return itpl('$var = _ip.magic($expr)')
24 return itpl('$var = get_ipython().magic($expr)')
25 25
26 26 def hnd_syscmd(line,mo):
27 27 """ Handle a = !ls """
28 28 var = mo.group('varname')
29 29 cmd = mo.group('cmd')
30 30 expr = make_quoted_expr(itpl("sc -l =$cmd"))
31 return itpl('$var = _ip.magic($expr)')
31 return itpl('$var = get_ipython().magic($expr)')
32 32
33 33 def install_re_handler(pat, hnd):
34 34 ip.meta.re_prefilters.append((re.compile(pat), hnd))
35 35
36 36 def init_handlers():
37 37
38 38 ip.meta.re_prefilters = []
39 39
40 40 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
41 41 hnd_magic
42 42 )
43 43
44 44 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
45 45 hnd_syscmd
46 46 )
47 47
48 48 init_handlers()
49 49
50 50 def regex_prefilter_f(self,line):
51 51 for pat, handler in ip.meta.re_prefilters:
52 52 mo = pat.match(line)
53 53 if mo:
54 54 return handler(line,mo)
55 55
56 56 raise TryNext
57 57
58 58 ip.set_hook('input_prefilter', regex_prefilter_f)
@@ -1,2328 +1,2328 b''
1 1 # -*- coding: iso-8859-1 -*-
2 2
3 3 """
4 4 ``ipipe`` provides classes to be used in an interactive Python session. Doing a
5 5 ``from ipipe import *`` is the preferred way to do this. The name of all
6 6 objects imported this way starts with ``i`` to minimize collisions.
7 7
8 8 ``ipipe`` supports "pipeline expressions", which is something resembling Unix
9 9 pipes. An example is::
10 10
11 11 >>> ienv | isort("key.lower()")
12 12
13 13 This gives a listing of all environment variables sorted by name.
14 14
15 15
16 16 There are three types of objects in a pipeline expression:
17 17
18 18 * ``Table``s: These objects produce items. Examples are ``ils`` (listing the
19 19 current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing
20 20 user accounts) and ``igrp`` (listing user groups). A ``Table`` must be the
21 21 first object in a pipe expression.
22 22
23 23 * ``Pipe``s: These objects sit in the middle of a pipe expression. They
24 24 transform the input in some way (e.g. filtering or sorting it). Examples are:
25 25 ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input
26 26 pipe) and ``ieval`` (which evaluates a function or expression for each object
27 27 in the input pipe).
28 28
29 29 * ``Display``s: These objects can be put as the last object in a pipeline
30 30 expression. There are responsible for displaying the result of the pipeline
31 31 expression. If a pipeline expression doesn't end in a display object a default
32 32 display objects will be used. One example is ``ibrowse`` which is a ``curses``
33 33 based browser.
34 34
35 35
36 36 Adding support for pipeline expressions to your own objects can be done through
37 37 three extensions points (all of them optional):
38 38
39 39 * An object that will be displayed as a row by a ``Display`` object should
40 40 implement the method ``__xattrs__(self, mode)`` method or register an
41 41 implementation of the generic function ``xattrs``. For more info see ``xattrs``.
42 42
43 43 * When an object ``foo`` is displayed by a ``Display`` object, the generic
44 44 function ``xrepr`` is used.
45 45
46 46 * Objects that can be iterated by ``Pipe``s must iterable. For special cases,
47 47 where iteration for display is different than the normal iteration a special
48 48 implementation can be registered with the generic function ``xiter``. This
49 49 makes it possible to use dictionaries and modules in pipeline expressions,
50 50 for example::
51 51
52 52 >>> import sys
53 53 >>> sys | ifilter("isinstance(value, int)") | idump
54 54 key |value
55 55 api_version| 1012
56 56 dllhandle | 503316480
57 57 hexversion | 33817328
58 58 maxint |2147483647
59 59 maxunicode | 65535
60 60 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
61 61 ...
62 62
63 63 Note: The expression strings passed to ``ifilter()`` and ``isort()`` can
64 64 refer to the object to be filtered or sorted via the variable ``_`` and to any
65 65 of the attributes of the object, i.e.::
66 66
67 67 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
68 68
69 69 does the same as::
70 70
71 71 >>> sys.modules | ifilter("value is not None") | isort("key.lower()")
72 72
73 73 In addition to expression strings, it's possible to pass callables (taking
74 74 the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``::
75 75
76 76 >>> sys | ifilter(lambda _:isinstance(_.value, int)) \
77 77 ... | ieval(lambda _: (_.key, hex(_.value))) | idump
78 78 0 |1
79 79 api_version|0x3f4
80 80 dllhandle |0x1e000000
81 81 hexversion |0x20402f0
82 82 maxint |0x7fffffff
83 83 maxunicode |0xffff
84 84 """
85 85
86 86 skip_doctest = True # ignore top-level docstring as a doctest.
87 87
88 88 import sys, os, os.path, stat, glob, new, csv, datetime, types
89 89 import itertools, mimetypes, StringIO
90 90
91 91 try: # Python 2.3 compatibility
92 92 import collections
93 93 except ImportError:
94 94 deque = list
95 95 else:
96 96 deque = collections.deque
97 97
98 98 try: # Python 2.3 compatibility
99 99 set
100 100 except NameError:
101 101 import sets
102 102 set = sets.Set
103 103
104 104 try: # Python 2.3 compatibility
105 105 sorted
106 106 except NameError:
107 107 def sorted(iterator, key=None, reverse=False):
108 108 items = list(iterator)
109 109 if key is not None:
110 110 items.sort(lambda i1, i2: cmp(key(i1), key(i2)))
111 111 else:
112 112 items.sort()
113 113 if reverse:
114 114 items.reverse()
115 115 return items
116 116
117 117 try: # Python 2.4 compatibility
118 118 GeneratorExit
119 119 except NameError:
120 120 GeneratorExit = SystemExit
121 121
122 122 try:
123 123 import pwd
124 124 except ImportError:
125 125 pwd = None
126 126
127 127 try:
128 128 import grp
129 129 except ImportError:
130 130 grp = None
131 131
132 132 from IPython.external import simplegeneric
133 133 from IPython.external import path
134 134
135 135 try:
136 136 from IPython.utils import genutils
137 137 from IPython.utils import generics
138 138 except ImportError:
139 139 genutils = None
140 140 generics = None
141 141
142 142 from IPython.core import ipapi
143 143
144 144
145 145 __all__ = [
146 146 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
147 147 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum",
148 148 "ienv", "ihist", "ialias", "icap", "idump", "iless"
149 149 ]
150 150
151 151
152 152 os.stat_float_times(True) # enable microseconds
153 153
154 154
155 155 class AttrNamespace(object):
156 156 """
157 157 Helper class that is used for providing a namespace for evaluating
158 158 expressions containing attribute names of an object.
159 159 """
160 160 def __init__(self, wrapped):
161 161 self.wrapped = wrapped
162 162
163 163 def __getitem__(self, name):
164 164 if name == "_":
165 165 return self.wrapped
166 166 try:
167 167 return getattr(self.wrapped, name)
168 168 except AttributeError:
169 169 raise KeyError(name)
170 170
171 171 # Python 2.3 compatibility
172 172 # use eval workaround to find out which names are used in the
173 173 # eval string and put them into the locals. This works for most
174 174 # normal uses case, bizarre ones like accessing the locals()
175 175 # will fail
176 176 try:
177 177 eval("_", None, AttrNamespace(None))
178 178 except TypeError:
179 179 real_eval = eval
180 180 def eval(codestring, _globals, _locals):
181 181 """
182 182 eval(source[, globals[, locals]]) -> value
183 183
184 184 Evaluate the source in the context of globals and locals.
185 185 The source may be a string representing a Python expression
186 186 or a code object as returned by compile().
187 187 The globals must be a dictionary and locals can be any mappping.
188 188
189 189 This function is a workaround for the shortcomings of
190 190 Python 2.3's eval.
191 191 """
192 192
193 193 if isinstance(codestring, basestring):
194 194 code = compile(codestring, "_eval", "eval")
195 195 else:
196 196 code = codestring
197 197 newlocals = {}
198 198 for name in code.co_names:
199 199 try:
200 200 newlocals[name] = _locals[name]
201 201 except KeyError:
202 202 pass
203 203 return real_eval(code, _globals, newlocals)
204 204
205 205
206 206 noitem = object()
207 207
208 208
209 209 def item(iterator, index, default=noitem):
210 210 """
211 211 Return the ``index``th item from the iterator ``iterator``.
212 212 ``index`` must be an integer (negative integers are relative to the
213 213 end (i.e. the last items produced by the iterator)).
214 214
215 215 If ``default`` is given, this will be the default value when
216 216 the iterator doesn't contain an item at this position. Otherwise an
217 217 ``IndexError`` will be raised.
218 218
219 219 Note that using this function will partially or totally exhaust the
220 220 iterator.
221 221 """
222 222 i = index
223 223 if i>=0:
224 224 for item in iterator:
225 225 if not i:
226 226 return item
227 227 i -= 1
228 228 else:
229 229 i = -index
230 230 cache = deque()
231 231 for item in iterator:
232 232 cache.append(item)
233 233 if len(cache)>i:
234 234 cache.popleft()
235 235 if len(cache)==i:
236 236 return cache.popleft()
237 237 if default is noitem:
238 238 raise IndexError(index)
239 239 else:
240 240 return default
241 241
242 242
243 243 def getglobals(g):
244 244 """
245 245 Return the global namespace that is used for expression strings in
246 246 ``ifilter`` and others. This is ``g`` or (if ``g`` is ``None``) IPython's
247 247 user namespace.
248 248 """
249 249 if g is None:
250 250 if ipapi is not None:
251 251 api = ipapi.get()
252 252 if api is not None:
253 253 return api.user_ns
254 254 return globals()
255 255 return g
256 256
257 257
258 258 class Descriptor(object):
259 259 """
260 260 A ``Descriptor`` object is used for describing the attributes of objects.
261 261 """
262 262 def __hash__(self):
263 263 return hash(self.__class__) ^ hash(self.key())
264 264
265 265 def __eq__(self, other):
266 266 return self.__class__ is other.__class__ and self.key() == other.key()
267 267
268 268 def __ne__(self, other):
269 269 return self.__class__ is not other.__class__ or self.key() != other.key()
270 270
271 271 def key(self):
272 272 pass
273 273
274 274 def name(self):
275 275 """
276 276 Return the name of this attribute for display by a ``Display`` object
277 277 (e.g. as a column title).
278 278 """
279 279 key = self.key()
280 280 if key is None:
281 281 return "_"
282 282 return str(key)
283 283
284 284 def attrtype(self, obj):
285 285 """
286 286 Return the type of this attribute (i.e. something like "attribute" or
287 287 "method").
288 288 """
289 289
290 290 def valuetype(self, obj):
291 291 """
292 292 Return the type of this attribute value of the object ``obj``.
293 293 """
294 294
295 295 def value(self, obj):
296 296 """
297 297 Return the value of this attribute of the object ``obj``.
298 298 """
299 299
300 300 def doc(self, obj):
301 301 """
302 302 Return the documentation for this attribute.
303 303 """
304 304
305 305 def shortdoc(self, obj):
306 306 """
307 307 Return a short documentation for this attribute (defaulting to the
308 308 first line).
309 309 """
310 310 doc = self.doc(obj)
311 311 if doc is not None:
312 312 doc = doc.strip().splitlines()[0].strip()
313 313 return doc
314 314
315 315 def iter(self, obj):
316 316 """
317 317 Return an iterator for this attribute of the object ``obj``.
318 318 """
319 319 return xiter(self.value(obj))
320 320
321 321
322 322 class SelfDescriptor(Descriptor):
323 323 """
324 324 A ``SelfDescriptor`` describes the object itself.
325 325 """
326 326 def key(self):
327 327 return None
328 328
329 329 def attrtype(self, obj):
330 330 return "self"
331 331
332 332 def valuetype(self, obj):
333 333 return type(obj)
334 334
335 335 def value(self, obj):
336 336 return obj
337 337
338 338 def __repr__(self):
339 339 return "Self"
340 340
341 341 selfdescriptor = SelfDescriptor() # there's no need for more than one
342 342
343 343
344 344 class AttributeDescriptor(Descriptor):
345 345 """
346 346 An ``AttributeDescriptor`` describes a simple attribute of an object.
347 347 """
348 348 __slots__ = ("_name", "_doc")
349 349
350 350 def __init__(self, name, doc=None):
351 351 self._name = name
352 352 self._doc = doc
353 353
354 354 def key(self):
355 355 return self._name
356 356
357 357 def doc(self, obj):
358 358 return self._doc
359 359
360 360 def attrtype(self, obj):
361 361 return "attr"
362 362
363 363 def valuetype(self, obj):
364 364 return type(getattr(obj, self._name))
365 365
366 366 def value(self, obj):
367 367 return getattr(obj, self._name)
368 368
369 369 def __repr__(self):
370 370 if self._doc is None:
371 371 return "Attribute(%r)" % self._name
372 372 else:
373 373 return "Attribute(%r, %r)" % (self._name, self._doc)
374 374
375 375
376 376 class IndexDescriptor(Descriptor):
377 377 """
378 378 An ``IndexDescriptor`` describes an "attribute" of an object that is fetched
379 379 via ``__getitem__``.
380 380 """
381 381 __slots__ = ("_index",)
382 382
383 383 def __init__(self, index):
384 384 self._index = index
385 385
386 386 def key(self):
387 387 return self._index
388 388
389 389 def attrtype(self, obj):
390 390 return "item"
391 391
392 392 def valuetype(self, obj):
393 393 return type(obj[self._index])
394 394
395 395 def value(self, obj):
396 396 return obj[self._index]
397 397
398 398 def __repr__(self):
399 399 return "Index(%r)" % self._index
400 400
401 401
402 402 class MethodDescriptor(Descriptor):
403 403 """
404 404 A ``MethodDescriptor`` describes a method of an object that can be called
405 405 without argument. Note that this method shouldn't change the object.
406 406 """
407 407 __slots__ = ("_name", "_doc")
408 408
409 409 def __init__(self, name, doc=None):
410 410 self._name = name
411 411 self._doc = doc
412 412
413 413 def key(self):
414 414 return self._name
415 415
416 416 def doc(self, obj):
417 417 if self._doc is None:
418 418 return getattr(obj, self._name).__doc__
419 419 return self._doc
420 420
421 421 def attrtype(self, obj):
422 422 return "method"
423 423
424 424 def valuetype(self, obj):
425 425 return type(self.value(obj))
426 426
427 427 def value(self, obj):
428 428 return getattr(obj, self._name)()
429 429
430 430 def __repr__(self):
431 431 if self._doc is None:
432 432 return "Method(%r)" % self._name
433 433 else:
434 434 return "Method(%r, %r)" % (self._name, self._doc)
435 435
436 436
437 437 class IterAttributeDescriptor(Descriptor):
438 438 """
439 439 An ``IterAttributeDescriptor`` works like an ``AttributeDescriptor`` but
440 440 doesn't return an attribute values (because this value might be e.g. a large
441 441 list).
442 442 """
443 443 __slots__ = ("_name", "_doc")
444 444
445 445 def __init__(self, name, doc=None):
446 446 self._name = name
447 447 self._doc = doc
448 448
449 449 def key(self):
450 450 return self._name
451 451
452 452 def doc(self, obj):
453 453 return self._doc
454 454
455 455 def attrtype(self, obj):
456 456 return "iter"
457 457
458 458 def valuetype(self, obj):
459 459 return noitem
460 460
461 461 def value(self, obj):
462 462 return noitem
463 463
464 464 def iter(self, obj):
465 465 return xiter(getattr(obj, self._name))
466 466
467 467 def __repr__(self):
468 468 if self._doc is None:
469 469 return "IterAttribute(%r)" % self._name
470 470 else:
471 471 return "IterAttribute(%r, %r)" % (self._name, self._doc)
472 472
473 473
474 474 class IterMethodDescriptor(Descriptor):
475 475 """
476 476 An ``IterMethodDescriptor`` works like an ``MethodDescriptor`` but doesn't
477 477 return an attribute values (because this value might be e.g. a large list).
478 478 """
479 479 __slots__ = ("_name", "_doc")
480 480
481 481 def __init__(self, name, doc=None):
482 482 self._name = name
483 483 self._doc = doc
484 484
485 485 def key(self):
486 486 return self._name
487 487
488 488 def doc(self, obj):
489 489 if self._doc is None:
490 490 return getattr(obj, self._name).__doc__
491 491 return self._doc
492 492
493 493 def attrtype(self, obj):
494 494 return "itermethod"
495 495
496 496 def valuetype(self, obj):
497 497 return noitem
498 498
499 499 def value(self, obj):
500 500 return noitem
501 501
502 502 def iter(self, obj):
503 503 return xiter(getattr(obj, self._name)())
504 504
505 505 def __repr__(self):
506 506 if self._doc is None:
507 507 return "IterMethod(%r)" % self._name
508 508 else:
509 509 return "IterMethod(%r, %r)" % (self._name, self._doc)
510 510
511 511
512 512 class FunctionDescriptor(Descriptor):
513 513 """
514 514 A ``FunctionDescriptor`` turns a function into a descriptor. The function
515 515 will be called with the object to get the type and value of the attribute.
516 516 """
517 517 __slots__ = ("_function", "_name", "_doc")
518 518
519 519 def __init__(self, function, name=None, doc=None):
520 520 self._function = function
521 521 self._name = name
522 522 self._doc = doc
523 523
524 524 def key(self):
525 525 return self._function
526 526
527 527 def name(self):
528 528 if self._name is not None:
529 529 return self._name
530 530 return getattr(self._function, "__xname__", self._function.__name__)
531 531
532 532 def doc(self, obj):
533 533 if self._doc is None:
534 534 return self._function.__doc__
535 535 return self._doc
536 536
537 537 def attrtype(self, obj):
538 538 return "function"
539 539
540 540 def valuetype(self, obj):
541 541 return type(self._function(obj))
542 542
543 543 def value(self, obj):
544 544 return self._function(obj)
545 545
546 546 def __repr__(self):
547 547 if self._doc is None:
548 548 return "Function(%r)" % self._name
549 549 else:
550 550 return "Function(%r, %r)" % (self._name, self._doc)
551 551
552 552
553 553 class Table(object):
554 554 """
555 555 A ``Table`` is an object that produces items (just like a normal Python
556 556 iterator/generator does) and can be used as the first object in a pipeline
557 557 expression. The displayhook will open the default browser for such an object
558 558 (instead of simply printing the ``repr()`` result).
559 559 """
560 560
561 561 # We want to support ``foo`` and ``foo()`` in pipeline expression:
562 562 # So we implement the required operators (``|`` and ``+``) in the metaclass,
563 563 # instantiate the class and forward the operator to the instance
564 564 class __metaclass__(type):
565 565 def __iter__(self):
566 566 return iter(self())
567 567
568 568 def __or__(self, other):
569 569 return self() | other
570 570
571 571 def __add__(self, other):
572 572 return self() + other
573 573
574 574 def __radd__(self, other):
575 575 return other + self()
576 576
577 577 def __getitem__(self, index):
578 578 return self()[index]
579 579
580 580 def __getitem__(self, index):
581 581 return item(self, index)
582 582
583 583 def __contains__(self, item):
584 584 for haveitem in self:
585 585 if item == haveitem:
586 586 return True
587 587 return False
588 588
589 589 def __or__(self, other):
590 590 # autoinstantiate right hand side
591 591 if isinstance(other, type) and issubclass(other, (Table, Display)):
592 592 other = other()
593 593 # treat simple strings and functions as ``ieval`` instances
594 594 elif not isinstance(other, Display) and not isinstance(other, Table):
595 595 other = ieval(other)
596 596 # forward operations to the right hand side
597 597 return other.__ror__(self)
598 598
599 599 def __add__(self, other):
600 600 # autoinstantiate right hand side
601 601 if isinstance(other, type) and issubclass(other, Table):
602 602 other = other()
603 603 return ichain(self, other)
604 604
605 605 def __radd__(self, other):
606 606 # autoinstantiate left hand side
607 607 if isinstance(other, type) and issubclass(other, Table):
608 608 other = other()
609 609 return ichain(other, self)
610 610
611 611
612 612 class Pipe(Table):
613 613 """
614 614 A ``Pipe`` is an object that can be used in a pipeline expression. It
615 615 processes the objects it gets from its input ``Table``/``Pipe``. Note that
616 616 a ``Pipe`` object can't be used as the first object in a pipeline
617 617 expression, as it doesn't produces items itself.
618 618 """
619 619 class __metaclass__(Table.__metaclass__):
620 620 def __ror__(self, input):
621 621 return input | self()
622 622
623 623 def __ror__(self, input):
624 624 # autoinstantiate left hand side
625 625 if isinstance(input, type) and issubclass(input, Table):
626 626 input = input()
627 627 self.input = input
628 628 return self
629 629
630 630
631 631 def xrepr(item, mode="default"):
632 632 """
633 633 Generic function that adds color output and different display modes to ``repr``.
634 634
635 635 The result of an ``xrepr`` call is iterable and consists of ``(style, string)``
636 636 tuples. The ``style`` in this tuple must be a ``Style`` object from the
637 637 ``astring`` module. To reconfigure the output the first yielded tuple can be
638 638 a ``(aligment, full)`` tuple instead of a ``(style, string)`` tuple.
639 639 ``alignment`` can be -1 for left aligned, 0 for centered and 1 for right
640 640 aligned (the default is left alignment). ``full`` is a boolean that specifies
641 641 whether the complete output must be displayed or the ``Display`` object is
642 642 allowed to stop output after enough text has been produced (e.g. a syntax
643 643 highlighted text line would use ``True``, but for a large data structure
644 644 (i.e. a nested list, tuple or dictionary) ``False`` would be used).
645 645 The default is full output.
646 646
647 647 There are four different possible values for ``mode`` depending on where
648 648 the ``Display`` object will display ``item``:
649 649
650 650 ``"header"``
651 651 ``item`` will be displayed in a header line (this is used by ``ibrowse``).
652 652
653 653 ``"footer"``
654 654 ``item`` will be displayed in a footer line (this is used by ``ibrowse``).
655 655
656 656 ``"cell"``
657 657 ``item`` will be displayed in a table cell/list.
658 658
659 659 ``"default"``
660 660 default mode. If an ``xrepr`` implementation recursively outputs objects,
661 661 ``"default"`` must be passed in the recursive calls to ``xrepr``.
662 662
663 663 If no implementation is registered for ``item``, ``xrepr`` will try the
664 664 ``__xrepr__`` method on ``item``. If ``item`` doesn't have an ``__xrepr__``
665 665 method it falls back to ``repr``/``__repr__`` for all modes.
666 666 """
667 667 try:
668 668 func = item.__xrepr__
669 669 except AttributeError:
670 670 yield (astyle.style_default, repr(item))
671 671 else:
672 672 try:
673 673 for x in func(mode):
674 674 yield x
675 675 except (KeyboardInterrupt, SystemExit, GeneratorExit):
676 676 raise
677 677 except Exception:
678 678 yield (astyle.style_default, repr(item))
679 679 xrepr = simplegeneric.generic(xrepr)
680 680
681 681
682 682 def xrepr_none(self, mode="default"):
683 683 yield (astyle.style_type_none, repr(self))
684 684 xrepr.when_object(None)(xrepr_none)
685 685
686 686
687 687 def xrepr_noitem(self, mode="default"):
688 688 yield (2, True)
689 689 yield (astyle.style_nodata, "<?>")
690 690 xrepr.when_object(noitem)(xrepr_noitem)
691 691
692 692
693 693 def xrepr_bool(self, mode="default"):
694 694 yield (astyle.style_type_bool, repr(self))
695 695 xrepr.when_type(bool)(xrepr_bool)
696 696
697 697
698 698 def xrepr_str(self, mode="default"):
699 699 if mode == "cell":
700 700 yield (astyle.style_default, repr(self.expandtabs(tab))[1:-1])
701 701 else:
702 702 yield (astyle.style_default, repr(self))
703 703 xrepr.when_type(str)(xrepr_str)
704 704
705 705
706 706 def xrepr_unicode(self, mode="default"):
707 707 if mode == "cell":
708 708 yield (astyle.style_default, repr(self.expandtabs(tab))[2:-1])
709 709 else:
710 710 yield (astyle.style_default, repr(self))
711 711 xrepr.when_type(unicode)(xrepr_unicode)
712 712
713 713
714 714 def xrepr_number(self, mode="default"):
715 715 yield (1, True)
716 716 yield (astyle.style_type_number, repr(self))
717 717 xrepr.when_type(int)(xrepr_number)
718 718 xrepr.when_type(long)(xrepr_number)
719 719 xrepr.when_type(float)(xrepr_number)
720 720
721 721
722 722 def xrepr_complex(self, mode="default"):
723 723 yield (astyle.style_type_number, repr(self))
724 724 xrepr.when_type(complex)(xrepr_number)
725 725
726 726
727 727 def xrepr_datetime(self, mode="default"):
728 728 if mode == "cell":
729 729 # Don't use strftime() here, as this requires year >= 1900
730 730 yield (astyle.style_type_datetime,
731 731 "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
732 732 (self.year, self.month, self.day,
733 733 self.hour, self.minute, self.second,
734 734 self.microsecond),
735 735 )
736 736 else:
737 737 yield (astyle.style_type_datetime, repr(self))
738 738 xrepr.when_type(datetime.datetime)(xrepr_datetime)
739 739
740 740
741 741 def xrepr_date(self, mode="default"):
742 742 if mode == "cell":
743 743 yield (astyle.style_type_datetime,
744 744 "%04d-%02d-%02d" % (self.year, self.month, self.day))
745 745 else:
746 746 yield (astyle.style_type_datetime, repr(self))
747 747 xrepr.when_type(datetime.date)(xrepr_date)
748 748
749 749
750 750 def xrepr_time(self, mode="default"):
751 751 if mode == "cell":
752 752 yield (astyle.style_type_datetime,
753 753 "%02d:%02d:%02d.%06d" % \
754 754 (self.hour, self.minute, self.second, self.microsecond))
755 755 else:
756 756 yield (astyle.style_type_datetime, repr(self))
757 757 xrepr.when_type(datetime.time)(xrepr_time)
758 758
759 759
760 760 def xrepr_timedelta(self, mode="default"):
761 761 yield (astyle.style_type_datetime, repr(self))
762 762 xrepr.when_type(datetime.timedelta)(xrepr_timedelta)
763 763
764 764
765 765 def xrepr_type(self, mode="default"):
766 766 if self.__module__ == "__builtin__":
767 767 yield (astyle.style_type_type, self.__name__)
768 768 else:
769 769 yield (astyle.style_type_type, "%s.%s" % (self.__module__, self.__name__))
770 770 xrepr.when_type(type)(xrepr_type)
771 771
772 772
773 773 def xrepr_exception(self, mode="default"):
774 774 if self.__class__.__module__ == "exceptions":
775 775 classname = self.__class__.__name__
776 776 else:
777 777 classname = "%s.%s" % \
778 778 (self.__class__.__module__, self.__class__.__name__)
779 779 if mode == "header" or mode == "footer":
780 780 yield (astyle.style_error, "%s: %s" % (classname, self))
781 781 else:
782 782 yield (astyle.style_error, classname)
783 783 xrepr.when_type(Exception)(xrepr_exception)
784 784
785 785
786 786 def xrepr_listtuple(self, mode="default"):
787 787 if mode == "header" or mode == "footer":
788 788 if self.__class__.__module__ == "__builtin__":
789 789 classname = self.__class__.__name__
790 790 else:
791 791 classname = "%s.%s" % \
792 792 (self.__class__.__module__,self.__class__.__name__)
793 793 yield (astyle.style_default,
794 794 "<%s object with %d items at 0x%x>" % \
795 795 (classname, len(self), id(self)))
796 796 else:
797 797 yield (-1, False)
798 798 if isinstance(self, list):
799 799 yield (astyle.style_default, "[")
800 800 end = "]"
801 801 else:
802 802 yield (astyle.style_default, "(")
803 803 end = ")"
804 804 for (i, subself) in enumerate(self):
805 805 if i:
806 806 yield (astyle.style_default, ", ")
807 807 for part in xrepr(subself, "default"):
808 808 yield part
809 809 yield (astyle.style_default, end)
810 810 xrepr.when_type(list)(xrepr_listtuple)
811 811 xrepr.when_type(tuple)(xrepr_listtuple)
812 812
813 813
814 814 def xrepr_dict(self, mode="default"):
815 815 if mode == "header" or mode == "footer":
816 816 if self.__class__.__module__ == "__builtin__":
817 817 classname = self.__class__.__name__
818 818 else:
819 819 classname = "%s.%s" % \
820 820 (self.__class__.__module__,self.__class__.__name__)
821 821 yield (astyle.style_default,
822 822 "<%s object with %d items at 0x%x>" % \
823 823 (classname, len(self), id(self)))
824 824 else:
825 825 yield (-1, False)
826 826 if isinstance(self, dict):
827 827 yield (astyle.style_default, "{")
828 828 end = "}"
829 829 else:
830 830 yield (astyle.style_default, "dictproxy((")
831 831 end = "})"
832 832 for (i, (key, value)) in enumerate(self.iteritems()):
833 833 if i:
834 834 yield (astyle.style_default, ", ")
835 835 for part in xrepr(key, "default"):
836 836 yield part
837 837 yield (astyle.style_default, ": ")
838 838 for part in xrepr(value, "default"):
839 839 yield part
840 840 yield (astyle.style_default, end)
841 841 xrepr.when_type(dict)(xrepr_dict)
842 842 xrepr.when_type(types.DictProxyType)(xrepr_dict)
843 843
844 844
845 845 def upgradexattr(attr):
846 846 """
847 847 Convert an attribute descriptor string to a real descriptor object.
848 848
849 849 If attr already is a descriptor object return it unmodified. A
850 850 ``SelfDescriptor`` will be returned if ``attr`` is ``None``. ``"foo"``
851 851 returns an ``AttributeDescriptor`` for the attribute named ``"foo"``.
852 852 ``"foo()"`` returns a ``MethodDescriptor`` for the method named ``"foo"``.
853 853 ``"-foo"`` will return an ``IterAttributeDescriptor`` for the attribute
854 854 named ``"foo"`` and ``"-foo()"`` will return an ``IterMethodDescriptor``
855 855 for the method named ``"foo"``. Furthermore integers will return the appropriate
856 856 ``IndexDescriptor`` and callables will return a ``FunctionDescriptor``.
857 857 """
858 858 if attr is None:
859 859 return selfdescriptor
860 860 elif isinstance(attr, Descriptor):
861 861 return attr
862 862 elif isinstance(attr, basestring):
863 863 if attr.endswith("()"):
864 864 if attr.startswith("-"):
865 865 return IterMethodDescriptor(attr[1:-2])
866 866 else:
867 867 return MethodDescriptor(attr[:-2])
868 868 else:
869 869 if attr.startswith("-"):
870 870 return IterAttributeDescriptor(attr[1:])
871 871 else:
872 872 return AttributeDescriptor(attr)
873 873 elif isinstance(attr, (int, long)):
874 874 return IndexDescriptor(attr)
875 875 elif callable(attr):
876 876 return FunctionDescriptor(attr)
877 877 else:
878 878 raise TypeError("can't handle descriptor %r" % attr)
879 879
880 880
881 881 def xattrs(item, mode="default"):
882 882 """
883 883 Generic function that returns an iterable of attribute descriptors
884 884 to be used for displaying the attributes ob the object ``item`` in display
885 885 mode ``mode``.
886 886
887 887 There are two possible modes:
888 888
889 889 ``"detail"``
890 890 The ``Display`` object wants to display a detailed list of the object
891 891 attributes.
892 892
893 893 ``"default"``
894 894 The ``Display`` object wants to display the object in a list view.
895 895
896 896 If no implementation is registered for the object ``item`` ``xattrs`` falls
897 897 back to trying the ``__xattrs__`` method of the object. If this doesn't
898 898 exist either, ``dir(item)`` is used for ``"detail"`` mode and ``(None,)``
899 899 for ``"default"`` mode.
900 900
901 901 The implementation must yield attribute descriptors (see the class
902 902 ``Descriptor`` for more info). The ``__xattrs__`` method may also return
903 903 attribute descriptor strings (and ``None``) which will be converted to real
904 904 descriptors by ``upgradexattr()``.
905 905 """
906 906 try:
907 907 func = item.__xattrs__
908 908 except AttributeError:
909 909 if mode == "detail":
910 910 for attrname in dir(item):
911 911 yield AttributeDescriptor(attrname)
912 912 else:
913 913 yield selfdescriptor
914 914 else:
915 915 for attr in func(mode):
916 916 yield upgradexattr(attr)
917 917 xattrs = simplegeneric.generic(xattrs)
918 918
919 919
920 920 def xattrs_complex(self, mode="default"):
921 921 if mode == "detail":
922 922 return (AttributeDescriptor("real"), AttributeDescriptor("imag"))
923 923 return (selfdescriptor,)
924 924 xattrs.when_type(complex)(xattrs_complex)
925 925
926 926
927 927 def _isdict(item):
928 928 try:
929 929 itermeth = item.__class__.__iter__
930 930 except (AttributeError, TypeError):
931 931 return False
932 932 return itermeth is dict.__iter__ or itermeth is types.DictProxyType.__iter__
933 933
934 934
935 935 def _isstr(item):
936 936 if not isinstance(item, basestring):
937 937 return False
938 938 try:
939 939 itermeth = item.__class__.__iter__
940 940 except AttributeError:
941 941 return True
942 942 return False # ``__iter__`` has been redefined
943 943
944 944
945 945 def xiter(item):
946 946 """
947 947 Generic function that implements iteration for pipeline expression. If no
948 948 implementation is registered for ``item`` ``xiter`` falls back to ``iter``.
949 949 """
950 950 try:
951 951 func = item.__xiter__
952 952 except AttributeError:
953 953 if _isdict(item):
954 954 def items(item):
955 955 fields = ("key", "value")
956 956 for (key, value) in item.iteritems():
957 957 yield Fields(fields, key=key, value=value)
958 958 return items(item)
959 959 elif isinstance(item, new.module):
960 960 def items(item):
961 961 fields = ("key", "value")
962 962 for key in sorted(item.__dict__):
963 963 yield Fields(fields, key=key, value=getattr(item, key))
964 964 return items(item)
965 965 elif _isstr(item):
966 966 if not item:
967 967 raise ValueError("can't enter empty string")
968 968 lines = item.splitlines()
969 969 if len(lines) == 1:
970 970 def iterone(item):
971 971 yield item
972 972 return iterone(item)
973 973 else:
974 974 return iter(lines)
975 975 return iter(item)
976 976 else:
977 977 return iter(func()) # iter() just to be safe
978 978 xiter = simplegeneric.generic(xiter)
979 979
980 980
981 981 class ichain(Pipe):
982 982 """
983 983 Chains multiple ``Table``s into one.
984 984 """
985 985
986 986 def __init__(self, *iters):
987 987 self.iters = iters
988 988
989 989 def __iter__(self):
990 990 return itertools.chain(*self.iters)
991 991
992 992 def __xrepr__(self, mode="default"):
993 993 if mode == "header" or mode == "footer":
994 994 for (i, item) in enumerate(self.iters):
995 995 if i:
996 996 yield (astyle.style_default, "+")
997 997 if isinstance(item, Pipe):
998 998 yield (astyle.style_default, "(")
999 999 for part in xrepr(item, mode):
1000 1000 yield part
1001 1001 if isinstance(item, Pipe):
1002 1002 yield (astyle.style_default, ")")
1003 1003 else:
1004 1004 yield (astyle.style_default, repr(self))
1005 1005
1006 1006 def __repr__(self):
1007 1007 args = ", ".join([repr(it) for it in self.iters])
1008 1008 return "%s.%s(%s)" % \
1009 1009 (self.__class__.__module__, self.__class__.__name__, args)
1010 1010
1011 1011
1012 1012 class ifile(path.path):
1013 1013 """
1014 1014 file (or directory) object.
1015 1015 """
1016 1016
1017 1017 def getmode(self):
1018 1018 return self.stat().st_mode
1019 1019 mode = property(getmode, None, None, "Access mode")
1020 1020
1021 1021 def gettype(self):
1022 1022 data = [
1023 1023 (stat.S_ISREG, "file"),
1024 1024 (stat.S_ISDIR, "dir"),
1025 1025 (stat.S_ISCHR, "chardev"),
1026 1026 (stat.S_ISBLK, "blockdev"),
1027 1027 (stat.S_ISFIFO, "fifo"),
1028 1028 (stat.S_ISLNK, "symlink"),
1029 1029 (stat.S_ISSOCK,"socket"),
1030 1030 ]
1031 1031 lstat = self.lstat()
1032 1032 if lstat is not None:
1033 1033 types = set([text for (func, text) in data if func(lstat.st_mode)])
1034 1034 else:
1035 1035 types = set()
1036 1036 m = self.mode
1037 1037 types.update([text for (func, text) in data if func(m)])
1038 1038 return ", ".join(types)
1039 1039 type = property(gettype, None, None, "file type (file, directory, link, etc.)")
1040 1040
1041 1041 def getmodestr(self):
1042 1042 m = self.mode
1043 1043 data = [
1044 1044 (stat.S_IRUSR, "-r"),
1045 1045 (stat.S_IWUSR, "-w"),
1046 1046 (stat.S_IXUSR, "-x"),
1047 1047 (stat.S_IRGRP, "-r"),
1048 1048 (stat.S_IWGRP, "-w"),
1049 1049 (stat.S_IXGRP, "-x"),
1050 1050 (stat.S_IROTH, "-r"),
1051 1051 (stat.S_IWOTH, "-w"),
1052 1052 (stat.S_IXOTH, "-x"),
1053 1053 ]
1054 1054 return "".join([text[bool(m&bit)] for (bit, text) in data])
1055 1055
1056 1056 modestr = property(getmodestr, None, None, "Access mode as string")
1057 1057
1058 1058 def getblocks(self):
1059 1059 return self.stat().st_blocks
1060 1060 blocks = property(getblocks, None, None, "File size in blocks")
1061 1061
1062 1062 def getblksize(self):
1063 1063 return self.stat().st_blksize
1064 1064 blksize = property(getblksize, None, None, "Filesystem block size")
1065 1065
1066 1066 def getdev(self):
1067 1067 return self.stat().st_dev
1068 1068 dev = property(getdev)
1069 1069
1070 1070 def getnlink(self):
1071 1071 return self.stat().st_nlink
1072 1072 nlink = property(getnlink, None, None, "Number of links")
1073 1073
1074 1074 def getuid(self):
1075 1075 return self.stat().st_uid
1076 1076 uid = property(getuid, None, None, "User id of file owner")
1077 1077
1078 1078 def getgid(self):
1079 1079 return self.stat().st_gid
1080 1080 gid = property(getgid, None, None, "Group id of file owner")
1081 1081
1082 1082 def getowner(self):
1083 1083 stat = self.stat()
1084 1084 try:
1085 1085 return pwd.getpwuid(stat.st_uid).pw_name
1086 1086 except KeyError:
1087 1087 return stat.st_uid
1088 1088 owner = property(getowner, None, None, "Owner name (or id)")
1089 1089
1090 1090 def getgroup(self):
1091 1091 stat = self.stat()
1092 1092 try:
1093 1093 return grp.getgrgid(stat.st_gid).gr_name
1094 1094 except KeyError:
1095 1095 return stat.st_gid
1096 1096 group = property(getgroup, None, None, "Group name (or id)")
1097 1097
1098 1098 def getadate(self):
1099 1099 return datetime.datetime.utcfromtimestamp(self.atime)
1100 1100 adate = property(getadate, None, None, "Access date")
1101 1101
1102 1102 def getcdate(self):
1103 1103 return datetime.datetime.utcfromtimestamp(self.ctime)
1104 1104 cdate = property(getcdate, None, None, "Creation date")
1105 1105
1106 1106 def getmdate(self):
1107 1107 return datetime.datetime.utcfromtimestamp(self.mtime)
1108 1108 mdate = property(getmdate, None, None, "Modification date")
1109 1109
1110 1110 def mimetype(self):
1111 1111 """
1112 1112 Return MIME type guessed from the extension.
1113 1113 """
1114 1114 return mimetypes.guess_type(self.basename())[0]
1115 1115
1116 1116 def encoding(self):
1117 1117 """
1118 1118 Return guessed compression (like "compress" or "gzip").
1119 1119 """
1120 1120 return mimetypes.guess_type(self.basename())[1]
1121 1121
1122 1122 def __repr__(self):
1123 1123 return "ifile(%s)" % path._base.__repr__(self)
1124 1124
1125 1125 if sys.platform == "win32":
1126 1126 defaultattrs = (None, "type", "size", "modestr", "mdate")
1127 1127 else:
1128 1128 defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate")
1129 1129
1130 1130 def __xattrs__(self, mode="default"):
1131 1131 if mode == "detail":
1132 1132 return (
1133 1133 "name",
1134 1134 "basename()",
1135 1135 "abspath()",
1136 1136 "realpath()",
1137 1137 "type",
1138 1138 "mode",
1139 1139 "modestr",
1140 1140 "stat()",
1141 1141 "lstat()",
1142 1142 "uid",
1143 1143 "gid",
1144 1144 "owner",
1145 1145 "group",
1146 1146 "dev",
1147 1147 "nlink",
1148 1148 "ctime",
1149 1149 "mtime",
1150 1150 "atime",
1151 1151 "cdate",
1152 1152 "mdate",
1153 1153 "adate",
1154 1154 "size",
1155 1155 "blocks",
1156 1156 "blksize",
1157 1157 "isdir()",
1158 1158 "islink()",
1159 1159 "mimetype()",
1160 1160 "encoding()",
1161 1161 "-listdir()",
1162 1162 "-dirs()",
1163 1163 "-files()",
1164 1164 "-walk()",
1165 1165 "-walkdirs()",
1166 1166 "-walkfiles()",
1167 1167 )
1168 1168 else:
1169 1169 return self.defaultattrs
1170 1170
1171 1171
1172 1172 def xiter_ifile(self):
1173 1173 if self.isdir():
1174 1174 yield (self / os.pardir).abspath()
1175 1175 for child in sorted(self.listdir()):
1176 1176 yield child
1177 1177 else:
1178 1178 f = self.open("rb")
1179 1179 for line in f:
1180 1180 yield line
1181 1181 f.close()
1182 1182 xiter.when_type(ifile)(xiter_ifile)
1183 1183
1184 1184
1185 1185 # We need to implement ``xrepr`` for ``ifile`` as a generic function, because
1186 1186 # otherwise ``xrepr_str`` would kick in.
1187 1187 def xrepr_ifile(self, mode="default"):
1188 1188 try:
1189 1189 if self.isdir():
1190 1190 name = "idir"
1191 1191 style = astyle.style_dir
1192 1192 else:
1193 1193 name = "ifile"
1194 1194 style = astyle.style_file
1195 1195 except IOError:
1196 1196 name = "ifile"
1197 1197 style = astyle.style_default
1198 1198 if mode in ("cell", "header", "footer"):
1199 1199 abspath = repr(path._base(self.normpath()))
1200 1200 if abspath.startswith("u"):
1201 1201 abspath = abspath[2:-1]
1202 1202 else:
1203 1203 abspath = abspath[1:-1]
1204 1204 if mode == "cell":
1205 1205 yield (style, abspath)
1206 1206 else:
1207 1207 yield (style, "%s(%s)" % (name, abspath))
1208 1208 else:
1209 1209 yield (style, repr(self))
1210 1210 xrepr.when_type(ifile)(xrepr_ifile)
1211 1211
1212 1212
1213 1213 class ils(Table):
1214 1214 """
1215 1215 List the current (or a specified) directory.
1216 1216
1217 1217 Examples::
1218 1218
1219 1219 >>> ils
1220 1220 <class 'IPython.extensions.ipipe.ils'>
1221 1221 >>> ils("/usr/local/lib/python2.4")
1222 1222 IPython.extensions.ipipe.ils('/usr/local/lib/python2.4')
1223 1223 >>> ils("~")
1224 1224 IPython.extensions.ipipe.ils('/home/fperez')
1225 1225 # all-random
1226 1226 """
1227 1227 def __init__(self, base=os.curdir, dirs=True, files=True):
1228 1228 self.base = os.path.expanduser(base)
1229 1229 self.dirs = dirs
1230 1230 self.files = files
1231 1231
1232 1232 def __iter__(self):
1233 1233 base = ifile(self.base)
1234 1234 yield (base / os.pardir).abspath()
1235 1235 for child in sorted(base.listdir()):
1236 1236 if self.dirs:
1237 1237 if self.files:
1238 1238 yield child
1239 1239 else:
1240 1240 if child.isdir():
1241 1241 yield child
1242 1242 elif self.files:
1243 1243 if not child.isdir():
1244 1244 yield child
1245 1245
1246 1246 def __xrepr__(self, mode="default"):
1247 1247 return xrepr(ifile(self.base), mode)
1248 1248
1249 1249 def __repr__(self):
1250 1250 return "%s.%s(%r)" % \
1251 1251 (self.__class__.__module__, self.__class__.__name__, self.base)
1252 1252
1253 1253
1254 1254 class iglob(Table):
1255 1255 """
1256 1256 List all files and directories matching a specified pattern.
1257 1257 (See ``glob.glob()`` for more info.).
1258 1258
1259 1259 Examples::
1260 1260
1261 1261 >>> iglob("*.py")
1262 1262 IPython.extensions.ipipe.iglob('*.py')
1263 1263 """
1264 1264 def __init__(self, glob):
1265 1265 self.glob = glob
1266 1266
1267 1267 def __iter__(self):
1268 1268 for name in glob.glob(self.glob):
1269 1269 yield ifile(name)
1270 1270
1271 1271 def __xrepr__(self, mode="default"):
1272 1272 if mode == "header" or mode == "footer" or mode == "cell":
1273 1273 yield (astyle.style_default,
1274 1274 "%s(%r)" % (self.__class__.__name__, self.glob))
1275 1275 else:
1276 1276 yield (astyle.style_default, repr(self))
1277 1277
1278 1278 def __repr__(self):
1279 1279 return "%s.%s(%r)" % \
1280 1280 (self.__class__.__module__, self.__class__.__name__, self.glob)
1281 1281
1282 1282
1283 1283 class iwalk(Table):
1284 1284 """
1285 1285 List all files and directories in a directory and it's subdirectory::
1286 1286
1287 1287 >>> iwalk
1288 1288 <class 'IPython.extensions.ipipe.iwalk'>
1289 1289 >>> iwalk("/usr/lib")
1290 1290 IPython.extensions.ipipe.iwalk('/usr/lib')
1291 1291 >>> iwalk("~")
1292 1292 IPython.extensions.ipipe.iwalk('/home/fperez') # random
1293 1293
1294 1294 """
1295 1295 def __init__(self, base=os.curdir, dirs=True, files=True):
1296 1296 self.base = os.path.expanduser(base)
1297 1297 self.dirs = dirs
1298 1298 self.files = files
1299 1299
1300 1300 def __iter__(self):
1301 1301 for (dirpath, dirnames, filenames) in os.walk(self.base):
1302 1302 if self.dirs:
1303 1303 for name in sorted(dirnames):
1304 1304 yield ifile(os.path.join(dirpath, name))
1305 1305 if self.files:
1306 1306 for name in sorted(filenames):
1307 1307 yield ifile(os.path.join(dirpath, name))
1308 1308
1309 1309 def __xrepr__(self, mode="default"):
1310 1310 if mode == "header" or mode == "footer" or mode == "cell":
1311 1311 yield (astyle.style_default,
1312 1312 "%s(%r)" % (self.__class__.__name__, self.base))
1313 1313 else:
1314 1314 yield (astyle.style_default, repr(self))
1315 1315
1316 1316 def __repr__(self):
1317 1317 return "%s.%s(%r)" % \
1318 1318 (self.__class__.__module__, self.__class__.__name__, self.base)
1319 1319
1320 1320
1321 1321 class ipwdentry(object):
1322 1322 """
1323 1323 ``ipwdentry`` objects encapsulate entries in the Unix user account and
1324 1324 password database.
1325 1325 """
1326 1326 def __init__(self, id):
1327 1327 self._id = id
1328 1328 self._entry = None
1329 1329
1330 1330 def __eq__(self, other):
1331 1331 return self.__class__ is other.__class__ and self._id == other._id
1332 1332
1333 1333 def __ne__(self, other):
1334 1334 return self.__class__ is not other.__class__ or self._id != other._id
1335 1335
1336 1336 def _getentry(self):
1337 1337 if self._entry is None:
1338 1338 if isinstance(self._id, basestring):
1339 1339 self._entry = pwd.getpwnam(self._id)
1340 1340 else:
1341 1341 self._entry = pwd.getpwuid(self._id)
1342 1342 return self._entry
1343 1343
1344 1344 def getname(self):
1345 1345 if isinstance(self._id, basestring):
1346 1346 return self._id
1347 1347 else:
1348 1348 return self._getentry().pw_name
1349 1349 name = property(getname, None, None, "User name")
1350 1350
1351 1351 def getpasswd(self):
1352 1352 return self._getentry().pw_passwd
1353 1353 passwd = property(getpasswd, None, None, "Password")
1354 1354
1355 1355 def getuid(self):
1356 1356 if isinstance(self._id, basestring):
1357 1357 return self._getentry().pw_uid
1358 1358 else:
1359 1359 return self._id
1360 1360 uid = property(getuid, None, None, "User id")
1361 1361
1362 1362 def getgid(self):
1363 1363 return self._getentry().pw_gid
1364 1364 gid = property(getgid, None, None, "Primary group id")
1365 1365
1366 1366 def getgroup(self):
1367 1367 return igrpentry(self.gid)
1368 1368 group = property(getgroup, None, None, "Group")
1369 1369
1370 1370 def getgecos(self):
1371 1371 return self._getentry().pw_gecos
1372 1372 gecos = property(getgecos, None, None, "Information (e.g. full user name)")
1373 1373
1374 1374 def getdir(self):
1375 1375 return self._getentry().pw_dir
1376 1376 dir = property(getdir, None, None, "$HOME directory")
1377 1377
1378 1378 def getshell(self):
1379 1379 return self._getentry().pw_shell
1380 1380 shell = property(getshell, None, None, "Login shell")
1381 1381
1382 1382 def __xattrs__(self, mode="default"):
1383 1383 return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell")
1384 1384
1385 1385 def __repr__(self):
1386 1386 return "%s.%s(%r)" % \
1387 1387 (self.__class__.__module__, self.__class__.__name__, self._id)
1388 1388
1389 1389
1390 1390 class ipwd(Table):
1391 1391 """
1392 1392 List all entries in the Unix user account and password database.
1393 1393
1394 1394 Example::
1395 1395
1396 1396 >>> ipwd | isort("uid")
1397 1397 <IPython.extensions.ipipe.isort key='uid' reverse=False at 0x849efec>
1398 1398 # random
1399 1399 """
1400 1400 def __iter__(self):
1401 1401 for entry in pwd.getpwall():
1402 1402 yield ipwdentry(entry.pw_name)
1403 1403
1404 1404 def __xrepr__(self, mode="default"):
1405 1405 if mode == "header" or mode == "footer" or mode == "cell":
1406 1406 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1407 1407 else:
1408 1408 yield (astyle.style_default, repr(self))
1409 1409
1410 1410
1411 1411 class igrpentry(object):
1412 1412 """
1413 1413 ``igrpentry`` objects encapsulate entries in the Unix group database.
1414 1414 """
1415 1415 def __init__(self, id):
1416 1416 self._id = id
1417 1417 self._entry = None
1418 1418
1419 1419 def __eq__(self, other):
1420 1420 return self.__class__ is other.__class__ and self._id == other._id
1421 1421
1422 1422 def __ne__(self, other):
1423 1423 return self.__class__ is not other.__class__ or self._id != other._id
1424 1424
1425 1425 def _getentry(self):
1426 1426 if self._entry is None:
1427 1427 if isinstance(self._id, basestring):
1428 1428 self._entry = grp.getgrnam(self._id)
1429 1429 else:
1430 1430 self._entry = grp.getgrgid(self._id)
1431 1431 return self._entry
1432 1432
1433 1433 def getname(self):
1434 1434 if isinstance(self._id, basestring):
1435 1435 return self._id
1436 1436 else:
1437 1437 return self._getentry().gr_name
1438 1438 name = property(getname, None, None, "Group name")
1439 1439
1440 1440 def getpasswd(self):
1441 1441 return self._getentry().gr_passwd
1442 1442 passwd = property(getpasswd, None, None, "Password")
1443 1443
1444 1444 def getgid(self):
1445 1445 if isinstance(self._id, basestring):
1446 1446 return self._getentry().gr_gid
1447 1447 else:
1448 1448 return self._id
1449 1449 gid = property(getgid, None, None, "Group id")
1450 1450
1451 1451 def getmem(self):
1452 1452 return self._getentry().gr_mem
1453 1453 mem = property(getmem, None, None, "Members")
1454 1454
1455 1455 def __xattrs__(self, mode="default"):
1456 1456 return ("name", "passwd", "gid", "mem")
1457 1457
1458 1458 def __xrepr__(self, mode="default"):
1459 1459 if mode == "header" or mode == "footer" or mode == "cell":
1460 1460 yield (astyle.style_default, "group ")
1461 1461 try:
1462 1462 yield (astyle.style_default, self.name)
1463 1463 except KeyError:
1464 1464 if isinstance(self._id, basestring):
1465 1465 yield (astyle.style_default, self.name_id)
1466 1466 else:
1467 1467 yield (astyle.style_type_number, str(self._id))
1468 1468 else:
1469 1469 yield (astyle.style_default, repr(self))
1470 1470
1471 1471 def __iter__(self):
1472 1472 for member in self.mem:
1473 1473 yield ipwdentry(member)
1474 1474
1475 1475 def __repr__(self):
1476 1476 return "%s.%s(%r)" % \
1477 1477 (self.__class__.__module__, self.__class__.__name__, self._id)
1478 1478
1479 1479
1480 1480 class igrp(Table):
1481 1481 """
1482 1482 This ``Table`` lists all entries in the Unix group database.
1483 1483 """
1484 1484 def __iter__(self):
1485 1485 for entry in grp.getgrall():
1486 1486 yield igrpentry(entry.gr_name)
1487 1487
1488 1488 def __xrepr__(self, mode="default"):
1489 1489 if mode == "header" or mode == "footer":
1490 1490 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1491 1491 else:
1492 1492 yield (astyle.style_default, repr(self))
1493 1493
1494 1494
1495 1495 class Fields(object):
1496 1496 def __init__(self, fieldnames, **fields):
1497 1497 self.__fieldnames = [upgradexattr(fieldname) for fieldname in fieldnames]
1498 1498 for (key, value) in fields.iteritems():
1499 1499 setattr(self, key, value)
1500 1500
1501 1501 def __xattrs__(self, mode="default"):
1502 1502 return self.__fieldnames
1503 1503
1504 1504 def __xrepr__(self, mode="default"):
1505 1505 yield (-1, False)
1506 1506 if mode == "header" or mode == "cell":
1507 1507 yield (astyle.style_default, self.__class__.__name__)
1508 1508 yield (astyle.style_default, "(")
1509 1509 for (i, f) in enumerate(self.__fieldnames):
1510 1510 if i:
1511 1511 yield (astyle.style_default, ", ")
1512 1512 yield (astyle.style_default, f.name())
1513 1513 yield (astyle.style_default, "=")
1514 1514 for part in xrepr(getattr(self, f), "default"):
1515 1515 yield part
1516 1516 yield (astyle.style_default, ")")
1517 1517 elif mode == "footer":
1518 1518 yield (astyle.style_default, self.__class__.__name__)
1519 1519 yield (astyle.style_default, "(")
1520 1520 for (i, f) in enumerate(self.__fieldnames):
1521 1521 if i:
1522 1522 yield (astyle.style_default, ", ")
1523 1523 yield (astyle.style_default, f.name())
1524 1524 yield (astyle.style_default, ")")
1525 1525 else:
1526 1526 yield (astyle.style_default, repr(self))
1527 1527
1528 1528
1529 1529 class FieldTable(Table, list):
1530 1530 def __init__(self, *fields):
1531 1531 Table.__init__(self)
1532 1532 list.__init__(self)
1533 1533 self.fields = fields
1534 1534
1535 1535 def add(self, **fields):
1536 1536 self.append(Fields(self.fields, **fields))
1537 1537
1538 1538 def __xrepr__(self, mode="default"):
1539 1539 yield (-1, False)
1540 1540 if mode == "header" or mode == "footer":
1541 1541 yield (astyle.style_default, self.__class__.__name__)
1542 1542 yield (astyle.style_default, "(")
1543 1543 for (i, f) in enumerate(self.__fieldnames):
1544 1544 if i:
1545 1545 yield (astyle.style_default, ", ")
1546 1546 yield (astyle.style_default, f)
1547 1547 yield (astyle.style_default, ")")
1548 1548 else:
1549 1549 yield (astyle.style_default, repr(self))
1550 1550
1551 1551 def __repr__(self):
1552 1552 return "<%s.%s object with fields=%r at 0x%x>" % \
1553 1553 (self.__class__.__module__, self.__class__.__name__,
1554 1554 ", ".join(map(repr, self.fields)), id(self))
1555 1555
1556 1556
1557 1557 class List(list):
1558 1558 def __xattrs__(self, mode="default"):
1559 1559 return xrange(len(self))
1560 1560
1561 1561 def __xrepr__(self, mode="default"):
1562 1562 yield (-1, False)
1563 1563 if mode == "header" or mode == "cell" or mode == "footer" or mode == "default":
1564 1564 yield (astyle.style_default, self.__class__.__name__)
1565 1565 yield (astyle.style_default, "(")
1566 1566 for (i, item) in enumerate(self):
1567 1567 if i:
1568 1568 yield (astyle.style_default, ", ")
1569 1569 for part in xrepr(item, "default"):
1570 1570 yield part
1571 1571 yield (astyle.style_default, ")")
1572 1572 else:
1573 1573 yield (astyle.style_default, repr(self))
1574 1574
1575 1575
1576 1576 class ienv(Table):
1577 1577 """
1578 1578 List environment variables.
1579 1579
1580 1580 Example::
1581 1581
1582 1582 >>> ienv
1583 1583 <class 'IPython.extensions.ipipe.ienv'>
1584 1584 """
1585 1585
1586 1586 def __iter__(self):
1587 1587 fields = ("key", "value")
1588 1588 for (key, value) in os.environ.iteritems():
1589 1589 yield Fields(fields, key=key, value=value)
1590 1590
1591 1591 def __xrepr__(self, mode="default"):
1592 1592 if mode == "header" or mode == "cell":
1593 1593 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1594 1594 else:
1595 1595 yield (astyle.style_default, repr(self))
1596 1596
1597 1597
1598 1598 class ihist(Table):
1599 1599 """
1600 1600 IPython input history
1601 1601
1602 1602 Example::
1603 1603
1604 1604 >>> ihist
1605 1605 <class 'IPython.extensions.ipipe.ihist'>
1606 1606 >>> ihist(True) # raw mode
1607 1607 <IPython.extensions.ipipe.ihist object at 0x849602c> # random
1608 1608 """
1609 1609 def __init__(self, raw=True):
1610 1610 self.raw = raw
1611 1611
1612 1612 def __iter__(self):
1613 1613 api = ipapi.get()
1614 1614 if self.raw:
1615 1615 for line in api.input_hist_raw:
1616 1616 yield line.rstrip("\n")
1617 1617 else:
1618 1618 for line in api.input_hist:
1619 1619 yield line.rstrip("\n")
1620 1620
1621 1621
1622 1622 class Alias(object):
1623 1623 """
1624 1624 Entry in the alias table
1625 1625 """
1626 1626 def __init__(self, name, args, command):
1627 1627 self.name = name
1628 1628 self.args = args
1629 1629 self.command = command
1630 1630
1631 1631 def __xattrs__(self, mode="default"):
1632 1632 return ("name", "args", "command")
1633 1633
1634 1634
1635 1635 class ialias(Table):
1636 1636 """
1637 1637 IPython alias list
1638 1638
1639 1639 Example::
1640 1640
1641 1641 >>> ialias
1642 1642 <class 'IPython.extensions.ipipe.ialias'>
1643 1643 """
1644 1644 def __iter__(self):
1645 1645 api = ipapi.get()
1646 1646
1647 for (name, (args, command)) in api.alias_table.iteritems():
1647 for (name, (args, command)) in api.alias_manager.alias_table.iteritems():
1648 1648 yield Alias(name, args, command)
1649 1649
1650 1650
1651 1651 class icsv(Pipe):
1652 1652 """
1653 1653 This ``Pipe`` turns the input (with must be a pipe outputting lines
1654 1654 or an ``ifile``) into lines of CVS columns.
1655 1655 """
1656 1656 def __init__(self, **csvargs):
1657 1657 """
1658 1658 Create an ``icsv`` object. ``cvsargs`` will be passed through as
1659 1659 keyword arguments to ``cvs.reader()``.
1660 1660 """
1661 1661 self.csvargs = csvargs
1662 1662
1663 1663 def __iter__(self):
1664 1664 input = self.input
1665 1665 if isinstance(input, ifile):
1666 1666 input = input.open("rb")
1667 1667 reader = csv.reader(input, **self.csvargs)
1668 1668 for line in reader:
1669 1669 yield List(line)
1670 1670
1671 1671 def __xrepr__(self, mode="default"):
1672 1672 yield (-1, False)
1673 1673 if mode == "header" or mode == "footer":
1674 1674 input = getattr(self, "input", None)
1675 1675 if input is not None:
1676 1676 for part in xrepr(input, mode):
1677 1677 yield part
1678 1678 yield (astyle.style_default, " | ")
1679 1679 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1680 1680 for (i, (name, value)) in enumerate(self.csvargs.iteritems()):
1681 1681 if i:
1682 1682 yield (astyle.style_default, ", ")
1683 1683 yield (astyle.style_default, name)
1684 1684 yield (astyle.style_default, "=")
1685 1685 for part in xrepr(value, "default"):
1686 1686 yield part
1687 1687 yield (astyle.style_default, ")")
1688 1688 else:
1689 1689 yield (astyle.style_default, repr(self))
1690 1690
1691 1691 def __repr__(self):
1692 1692 args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()])
1693 1693 return "<%s.%s %s at 0x%x>" % \
1694 1694 (self.__class__.__module__, self.__class__.__name__, args, id(self))
1695 1695
1696 1696
1697 1697 class ix(Table):
1698 1698 """
1699 1699 Execute a system command and list its output as lines
1700 1700 (similar to ``os.popen()``).
1701 1701
1702 1702 Examples::
1703 1703
1704 1704 >>> ix("ps x")
1705 1705 IPython.extensions.ipipe.ix('ps x')
1706 1706
1707 1707 >>> ix("find .") | ifile
1708 1708 <IPython.extensions.ipipe.ieval expr=<class 'IPython.extensions.ipipe.ifile'> at 0x8509d2c>
1709 1709 # random
1710 1710 """
1711 1711 def __init__(self, cmd):
1712 1712 self.cmd = cmd
1713 1713 self._pipeout = None
1714 1714
1715 1715 def __iter__(self):
1716 1716 (_pipein, self._pipeout) = os.popen4(self.cmd)
1717 1717 _pipein.close()
1718 1718 for l in self._pipeout:
1719 1719 yield l.rstrip("\r\n")
1720 1720 self._pipeout.close()
1721 1721 self._pipeout = None
1722 1722
1723 1723 def __del__(self):
1724 1724 if self._pipeout is not None and not self._pipeout.closed:
1725 1725 self._pipeout.close()
1726 1726 self._pipeout = None
1727 1727
1728 1728 def __xrepr__(self, mode="default"):
1729 1729 if mode == "header" or mode == "footer":
1730 1730 yield (astyle.style_default,
1731 1731 "%s(%r)" % (self.__class__.__name__, self.cmd))
1732 1732 else:
1733 1733 yield (astyle.style_default, repr(self))
1734 1734
1735 1735 def __repr__(self):
1736 1736 return "%s.%s(%r)" % \
1737 1737 (self.__class__.__module__, self.__class__.__name__, self.cmd)
1738 1738
1739 1739
1740 1740 class ifilter(Pipe):
1741 1741 """
1742 1742 Filter an input pipe. Only objects where an expression evaluates to true
1743 1743 (and doesn't raise an exception) are listed.
1744 1744
1745 1745 Examples::
1746 1746
1747 1747 >>> ils | ifilter("_.isfile() and size>1000")
1748 1748 >>> igrp | ifilter("len(mem)")
1749 1749 >>> sys.modules | ifilter(lambda _:_.value is not None)
1750 1750 # all-random
1751 1751 """
1752 1752
1753 1753 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1754 1754 """
1755 1755 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1756 1756 containing an expression. ``globals`` will be used as the global
1757 1757 namespace for calling string expressions (defaulting to IPython's
1758 1758 user namespace). ``errors`` specifies how exception during evaluation
1759 1759 of ``expr`` are handled:
1760 1760
1761 1761 ``"drop"``
1762 1762 drop all items that have errors;
1763 1763
1764 1764 ``"keep"``
1765 1765 keep all items that have errors;
1766 1766
1767 1767 ``"keeperror"``
1768 1768 keep the exception of all items that have errors;
1769 1769
1770 1770 ``"raise"``
1771 1771 raise the exception;
1772 1772
1773 1773 ``"raiseifallfail"``
1774 1774 raise the first exception if all items have errors; otherwise drop
1775 1775 those with errors (this is the default).
1776 1776 """
1777 1777 self.expr = expr
1778 1778 self.globals = globals
1779 1779 self.errors = errors
1780 1780
1781 1781 def __iter__(self):
1782 1782 if callable(self.expr):
1783 1783 test = self.expr
1784 1784 else:
1785 1785 g = getglobals(self.globals)
1786 1786 expr = compile(self.expr, "ipipe-expression", "eval")
1787 1787 def test(item):
1788 1788 return eval(expr, g, AttrNamespace(item))
1789 1789
1790 1790 ok = 0
1791 1791 exc_info = None
1792 1792 for item in xiter(self.input):
1793 1793 try:
1794 1794 if test(item):
1795 1795 yield item
1796 1796 ok += 1
1797 1797 except (KeyboardInterrupt, SystemExit):
1798 1798 raise
1799 1799 except Exception, exc:
1800 1800 if self.errors == "drop":
1801 1801 pass # Ignore errors
1802 1802 elif self.errors == "keep":
1803 1803 yield item
1804 1804 elif self.errors == "keeperror":
1805 1805 yield exc
1806 1806 elif self.errors == "raise":
1807 1807 raise
1808 1808 elif self.errors == "raiseifallfail":
1809 1809 if exc_info is None:
1810 1810 exc_info = sys.exc_info()
1811 1811 if not ok and exc_info is not None:
1812 1812 raise exc_info[0], exc_info[1], exc_info[2]
1813 1813
1814 1814 def __xrepr__(self, mode="default"):
1815 1815 if mode == "header" or mode == "footer":
1816 1816 input = getattr(self, "input", None)
1817 1817 if input is not None:
1818 1818 for part in xrepr(input, mode):
1819 1819 yield part
1820 1820 yield (astyle.style_default, " | ")
1821 1821 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1822 1822 for part in xrepr(self.expr, "default"):
1823 1823 yield part
1824 1824 yield (astyle.style_default, ")")
1825 1825 else:
1826 1826 yield (astyle.style_default, repr(self))
1827 1827
1828 1828 def __repr__(self):
1829 1829 return "<%s.%s expr=%r at 0x%x>" % \
1830 1830 (self.__class__.__module__, self.__class__.__name__,
1831 1831 self.expr, id(self))
1832 1832
1833 1833
1834 1834 class ieval(Pipe):
1835 1835 """
1836 1836 Evaluate an expression for each object in the input pipe.
1837 1837
1838 1838 Examples::
1839 1839
1840 1840 >>> ils | ieval("_.abspath()")
1841 1841 # random
1842 1842 >>> sys.path | ieval(ifile)
1843 1843 # random
1844 1844 """
1845 1845
1846 1846 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1847 1847 """
1848 1848 Create an ``ieval`` object. ``expr`` can be a callable or a string
1849 1849 containing an expression. For the meaning of ``globals`` and
1850 1850 ``errors`` see ``ifilter``.
1851 1851 """
1852 1852 self.expr = expr
1853 1853 self.globals = globals
1854 1854 self.errors = errors
1855 1855
1856 1856 def __iter__(self):
1857 1857 if callable(self.expr):
1858 1858 do = self.expr
1859 1859 else:
1860 1860 g = getglobals(self.globals)
1861 1861 expr = compile(self.expr, "ipipe-expression", "eval")
1862 1862 def do(item):
1863 1863 return eval(expr, g, AttrNamespace(item))
1864 1864
1865 1865 ok = 0
1866 1866 exc_info = None
1867 1867 for item in xiter(self.input):
1868 1868 try:
1869 1869 yield do(item)
1870 1870 except (KeyboardInterrupt, SystemExit):
1871 1871 raise
1872 1872 except Exception, exc:
1873 1873 if self.errors == "drop":
1874 1874 pass # Ignore errors
1875 1875 elif self.errors == "keep":
1876 1876 yield item
1877 1877 elif self.errors == "keeperror":
1878 1878 yield exc
1879 1879 elif self.errors == "raise":
1880 1880 raise
1881 1881 elif self.errors == "raiseifallfail":
1882 1882 if exc_info is None:
1883 1883 exc_info = sys.exc_info()
1884 1884 if not ok and exc_info is not None:
1885 1885 raise exc_info[0], exc_info[1], exc_info[2]
1886 1886
1887 1887 def __xrepr__(self, mode="default"):
1888 1888 if mode == "header" or mode == "footer":
1889 1889 input = getattr(self, "input", None)
1890 1890 if input is not None:
1891 1891 for part in xrepr(input, mode):
1892 1892 yield part
1893 1893 yield (astyle.style_default, " | ")
1894 1894 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1895 1895 for part in xrepr(self.expr, "default"):
1896 1896 yield part
1897 1897 yield (astyle.style_default, ")")
1898 1898 else:
1899 1899 yield (astyle.style_default, repr(self))
1900 1900
1901 1901 def __repr__(self):
1902 1902 return "<%s.%s expr=%r at 0x%x>" % \
1903 1903 (self.__class__.__module__, self.__class__.__name__,
1904 1904 self.expr, id(self))
1905 1905
1906 1906
1907 1907 class ienum(Pipe):
1908 1908 """
1909 1909 Enumerate the input pipe (i.e. wrap each input object in an object
1910 1910 with ``index`` and ``object`` attributes).
1911 1911
1912 1912 Examples::
1913 1913
1914 1914 >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object")
1915 1915 """
1916 1916 skip_doctest = True
1917 1917
1918 1918 def __iter__(self):
1919 1919 fields = ("index", "object")
1920 1920 for (index, object) in enumerate(xiter(self.input)):
1921 1921 yield Fields(fields, index=index, object=object)
1922 1922
1923 1923
1924 1924 class isort(Pipe):
1925 1925 """
1926 1926 Sorts the input pipe.
1927 1927
1928 1928 Examples::
1929 1929
1930 1930 >>> ils | isort("size")
1931 1931 <IPython.extensions.ipipe.isort key='size' reverse=False at 0x849ec2c>
1932 1932 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1933 1933 <IPython.extensions.ipipe.isort key='_.isdir(), _.lower()' reverse=True at 0x849eacc>
1934 1934 # all-random
1935 1935 """
1936 1936
1937 1937 def __init__(self, key=None, globals=None, reverse=False):
1938 1938 """
1939 1939 Create an ``isort`` object. ``key`` can be a callable or a string
1940 1940 containing an expression (or ``None`` in which case the items
1941 1941 themselves will be sorted). If ``reverse`` is true the sort order
1942 1942 will be reversed. For the meaning of ``globals`` see ``ifilter``.
1943 1943 """
1944 1944 self.key = key
1945 1945 self.globals = globals
1946 1946 self.reverse = reverse
1947 1947
1948 1948 def __iter__(self):
1949 1949 if self.key is None:
1950 1950 items = sorted(xiter(self.input), reverse=self.reverse)
1951 1951 elif callable(self.key):
1952 1952 items = sorted(xiter(self.input), key=self.key, reverse=self.reverse)
1953 1953 else:
1954 1954 g = getglobals(self.globals)
1955 1955 key = compile(self.key, "ipipe-expression", "eval")
1956 1956 def realkey(item):
1957 1957 return eval(key, g, AttrNamespace(item))
1958 1958 items = sorted(xiter(self.input), key=realkey, reverse=self.reverse)
1959 1959 for item in items:
1960 1960 yield item
1961 1961
1962 1962 def __xrepr__(self, mode="default"):
1963 1963 if mode == "header" or mode == "footer":
1964 1964 input = getattr(self, "input", None)
1965 1965 if input is not None:
1966 1966 for part in xrepr(input, mode):
1967 1967 yield part
1968 1968 yield (astyle.style_default, " | ")
1969 1969 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1970 1970 for part in xrepr(self.key, "default"):
1971 1971 yield part
1972 1972 if self.reverse:
1973 1973 yield (astyle.style_default, ", ")
1974 1974 for part in xrepr(True, "default"):
1975 1975 yield part
1976 1976 yield (astyle.style_default, ")")
1977 1977 else:
1978 1978 yield (astyle.style_default, repr(self))
1979 1979
1980 1980 def __repr__(self):
1981 1981 return "<%s.%s key=%r reverse=%r at 0x%x>" % \
1982 1982 (self.__class__.__module__, self.__class__.__name__,
1983 1983 self.key, self.reverse, id(self))
1984 1984
1985 1985
1986 1986 tab = 3 # for expandtabs()
1987 1987
1988 1988 def _format(field):
1989 1989 if isinstance(field, str):
1990 1990 text = repr(field.expandtabs(tab))[1:-1]
1991 1991 elif isinstance(field, unicode):
1992 1992 text = repr(field.expandtabs(tab))[2:-1]
1993 1993 elif isinstance(field, datetime.datetime):
1994 1994 # Don't use strftime() here, as this requires year >= 1900
1995 1995 text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
1996 1996 (field.year, field.month, field.day,
1997 1997 field.hour, field.minute, field.second, field.microsecond)
1998 1998 elif isinstance(field, datetime.date):
1999 1999 text = "%04d-%02d-%02d" % (field.year, field.month, field.day)
2000 2000 else:
2001 2001 text = repr(field)
2002 2002 return text
2003 2003
2004 2004
2005 2005 class Display(object):
2006 2006 class __metaclass__(type):
2007 2007 def __ror__(self, input):
2008 2008 return input | self()
2009 2009
2010 2010 def __init__(self, input=None):
2011 2011 self.input = input
2012 2012
2013 2013 def __ror__(self, input):
2014 2014 self.input = input
2015 2015 return self
2016 2016
2017 2017 def display(self):
2018 2018 pass
2019 2019
2020 2020
2021 2021 class iless(Display):
2022 2022 cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS"
2023 2023
2024 2024 def display(self):
2025 2025 try:
2026 2026 pager = os.popen(self.cmd, "w")
2027 2027 try:
2028 2028 for item in xiter(self.input):
2029 2029 first = False
2030 2030 for attr in xattrs(item, "default"):
2031 2031 if first:
2032 2032 first = False
2033 2033 else:
2034 2034 pager.write(" ")
2035 2035 attr = upgradexattr(attr)
2036 2036 if not isinstance(attr, SelfDescriptor):
2037 2037 pager.write(attr.name())
2038 2038 pager.write("=")
2039 2039 pager.write(str(attr.value(item)))
2040 2040 pager.write("\n")
2041 2041 finally:
2042 2042 pager.close()
2043 2043 except Exception, exc:
2044 2044 print "%s: %s" % (exc.__class__.__name__, str(exc))
2045 2045
2046 2046
2047 2047 class _RedirectIO(object):
2048 2048 def __init__(self,*args,**kwargs):
2049 2049 """
2050 2050 Map the system output streams to self.
2051 2051 """
2052 2052 self.stream = StringIO.StringIO()
2053 2053 self.stdout = sys.stdout
2054 2054 sys.stdout = self
2055 2055 self.stderr = sys.stderr
2056 2056 sys.stderr = self
2057 2057
2058 2058 def write(self, text):
2059 2059 """
2060 2060 Write both to screen and to self.
2061 2061 """
2062 2062 self.stream.write(text)
2063 2063 self.stdout.write(text)
2064 2064 if "\n" in text:
2065 2065 self.stdout.flush()
2066 2066
2067 2067 def writelines(self, lines):
2068 2068 """
2069 2069 Write lines both to screen and to self.
2070 2070 """
2071 2071 self.stream.writelines(lines)
2072 2072 self.stdout.writelines(lines)
2073 2073 self.stdout.flush()
2074 2074
2075 2075 def restore(self):
2076 2076 """
2077 2077 Restore the default system streams.
2078 2078 """
2079 2079 self.stdout.flush()
2080 2080 self.stderr.flush()
2081 2081 sys.stdout = self.stdout
2082 2082 sys.stderr = self.stderr
2083 2083
2084 2084
2085 2085 class icap(Table):
2086 2086 """
2087 2087 Execute a python string and capture any output to stderr/stdout.
2088 2088
2089 2089 Examples::
2090 2090
2091 2091 >>> import time
2092 2092 >>> icap("for i in range(10): print i, time.sleep(0.1)")
2093 2093
2094 2094 """
2095 2095 skip_doctest = True
2096 2096
2097 2097 def __init__(self, expr, globals=None):
2098 2098 self.expr = expr
2099 2099 self.globals = globals
2100 2100 log = _RedirectIO()
2101 2101 try:
2102 2102 exec(expr, getglobals(globals))
2103 2103 finally:
2104 2104 log.restore()
2105 2105 self.stream = log.stream
2106 2106
2107 2107 def __iter__(self):
2108 2108 self.stream.seek(0)
2109 2109 for line in self.stream:
2110 2110 yield line.rstrip("\r\n")
2111 2111
2112 2112 def __xrepr__(self, mode="default"):
2113 2113 if mode == "header" or mode == "footer":
2114 2114 yield (astyle.style_default,
2115 2115 "%s(%r)" % (self.__class__.__name__, self.expr))
2116 2116 else:
2117 2117 yield (astyle.style_default, repr(self))
2118 2118
2119 2119 def __repr__(self):
2120 2120 return "%s.%s(%r)" % \
2121 2121 (self.__class__.__module__, self.__class__.__name__, self.expr)
2122 2122
2123 2123
2124 2124 def xformat(value, mode, maxlength):
2125 2125 align = None
2126 2126 full = True
2127 2127 width = 0
2128 2128 text = astyle.Text()
2129 2129 for (style, part) in xrepr(value, mode):
2130 2130 # only consider the first result
2131 2131 if align is None:
2132 2132 if isinstance(style, int):
2133 2133 # (style, text) really is (alignment, stop)
2134 2134 align = style
2135 2135 full = part
2136 2136 continue
2137 2137 else:
2138 2138 align = -1
2139 2139 full = True
2140 2140 if not isinstance(style, int):
2141 2141 text.append((style, part))
2142 2142 width += len(part)
2143 2143 if width >= maxlength and not full:
2144 2144 text.append((astyle.style_ellisis, "..."))
2145 2145 width += 3
2146 2146 break
2147 2147 if align is None: # default to left alignment
2148 2148 align = -1
2149 2149 return (align, width, text)
2150 2150
2151 2151
2152 2152
2153 2153 import astyle
2154 2154
2155 2155 class idump(Display):
2156 2156 # The approximate maximum length of a column entry
2157 2157 maxattrlength = 200
2158 2158
2159 2159 # Style for column names
2160 2160 style_header = astyle.Style.fromstr("white:black:bold")
2161 2161
2162 2162 def __init__(self, input=None, *attrs):
2163 2163 Display.__init__(self, input)
2164 2164 self.attrs = [upgradexattr(attr) for attr in attrs]
2165 2165 self.headerpadchar = " "
2166 2166 self.headersepchar = "|"
2167 2167 self.datapadchar = " "
2168 2168 self.datasepchar = "|"
2169 2169
2170 2170 def display(self):
2171 2171 stream = genutils.Term.cout
2172 2172 allattrs = []
2173 2173 attrset = set()
2174 2174 colwidths = {}
2175 2175 rows = []
2176 2176 for item in xiter(self.input):
2177 2177 row = {}
2178 2178 attrs = self.attrs
2179 2179 if not attrs:
2180 2180 attrs = xattrs(item, "default")
2181 2181 for attr in attrs:
2182 2182 if attr not in attrset:
2183 2183 allattrs.append(attr)
2184 2184 attrset.add(attr)
2185 2185 colwidths[attr] = len(attr.name())
2186 2186 try:
2187 2187 value = attr.value(item)
2188 2188 except (KeyboardInterrupt, SystemExit):
2189 2189 raise
2190 2190 except Exception, exc:
2191 2191 value = exc
2192 2192 (align, width, text) = xformat(value, "cell", self.maxattrlength)
2193 2193 colwidths[attr] = max(colwidths[attr], width)
2194 2194 # remember alignment, length and colored parts
2195 2195 row[attr] = (align, width, text)
2196 2196 rows.append(row)
2197 2197
2198 2198 stream.write("\n")
2199 2199 for (i, attr) in enumerate(allattrs):
2200 2200 attrname = attr.name()
2201 2201 self.style_header(attrname).write(stream)
2202 2202 spc = colwidths[attr] - len(attrname)
2203 2203 if i < len(colwidths)-1:
2204 2204 stream.write(self.headerpadchar*spc)
2205 2205 stream.write(self.headersepchar)
2206 2206 stream.write("\n")
2207 2207
2208 2208 for row in rows:
2209 2209 for (i, attr) in enumerate(allattrs):
2210 2210 (align, width, text) = row[attr]
2211 2211 spc = colwidths[attr] - width
2212 2212 if align == -1:
2213 2213 text.write(stream)
2214 2214 if i < len(colwidths)-1:
2215 2215 stream.write(self.datapadchar*spc)
2216 2216 elif align == 0:
2217 2217 spc = colwidths[attr] - width
2218 2218 spc1 = spc//2
2219 2219 spc2 = spc-spc1
2220 2220 stream.write(self.datapadchar*spc1)
2221 2221 text.write(stream)
2222 2222 if i < len(colwidths)-1:
2223 2223 stream.write(self.datapadchar*spc2)
2224 2224 else:
2225 2225 stream.write(self.datapadchar*spc)
2226 2226 text.write(stream)
2227 2227 if i < len(colwidths)-1:
2228 2228 stream.write(self.datasepchar)
2229 2229 stream.write("\n")
2230 2230
2231 2231
2232 2232 class AttributeDetail(Table):
2233 2233 """
2234 2234 ``AttributeDetail`` objects are use for displaying a detailed list of object
2235 2235 attributes.
2236 2236 """
2237 2237 def __init__(self, object, descriptor):
2238 2238 self.object = object
2239 2239 self.descriptor = descriptor
2240 2240
2241 2241 def __iter__(self):
2242 2242 return self.descriptor.iter(self.object)
2243 2243
2244 2244 def name(self):
2245 2245 return self.descriptor.name()
2246 2246
2247 2247 def attrtype(self):
2248 2248 return self.descriptor.attrtype(self.object)
2249 2249
2250 2250 def valuetype(self):
2251 2251 return self.descriptor.valuetype(self.object)
2252 2252
2253 2253 def doc(self):
2254 2254 return self.descriptor.doc(self.object)
2255 2255
2256 2256 def shortdoc(self):
2257 2257 return self.descriptor.shortdoc(self.object)
2258 2258
2259 2259 def value(self):
2260 2260 return self.descriptor.value(self.object)
2261 2261
2262 2262 def __xattrs__(self, mode="default"):
2263 2263 attrs = ("name()", "attrtype()", "valuetype()", "value()", "shortdoc()")
2264 2264 if mode == "detail":
2265 2265 attrs += ("doc()",)
2266 2266 return attrs
2267 2267
2268 2268 def __xrepr__(self, mode="default"):
2269 2269 yield (-1, True)
2270 2270 valuetype = self.valuetype()
2271 2271 if valuetype is not noitem:
2272 2272 for part in xrepr(valuetype):
2273 2273 yield part
2274 2274 yield (astyle.style_default, " ")
2275 2275 yield (astyle.style_default, self.attrtype())
2276 2276 yield (astyle.style_default, " ")
2277 2277 yield (astyle.style_default, self.name())
2278 2278 yield (astyle.style_default, " of ")
2279 2279 for part in xrepr(self.object):
2280 2280 yield part
2281 2281
2282 2282
2283 2283 try:
2284 2284 from ibrowse import ibrowse
2285 2285 except ImportError:
2286 2286 # No curses (probably Windows) => try igrid
2287 2287 try:
2288 2288 from igrid import igrid
2289 2289 except ImportError:
2290 2290 # no wx either => use ``idump`` as the default display.
2291 2291 defaultdisplay = idump
2292 2292 else:
2293 2293 defaultdisplay = igrid
2294 2294 __all__.append("igrid")
2295 2295 else:
2296 2296 defaultdisplay = ibrowse
2297 2297 __all__.append("ibrowse")
2298 2298
2299 2299
2300 2300 # If we're running under IPython, register our objects with IPython's
2301 2301 # generic function ``result_display``, else install a displayhook
2302 2302 # directly as sys.displayhook
2303 2303 if generics is not None:
2304 2304 def display_display(obj):
2305 2305 return obj.display()
2306 2306 generics.result_display.when_type(Display)(display_display)
2307 2307
2308 2308 def display_tableobject(obj):
2309 2309 return display_display(defaultdisplay(obj))
2310 2310 generics.result_display.when_type(Table)(display_tableobject)
2311 2311
2312 2312 def display_tableclass(obj):
2313 2313 return display_tableobject(obj())
2314 2314 generics.result_display.when_type(Table.__metaclass__)(display_tableclass)
2315 2315 else:
2316 2316 def installdisplayhook():
2317 2317 _originalhook = sys.displayhook
2318 2318 def displayhook(obj):
2319 2319 if isinstance(obj, type) and issubclass(obj, Table):
2320 2320 obj = obj()
2321 2321 if isinstance(obj, Table):
2322 2322 obj = defaultdisplay(obj)
2323 2323 if isinstance(obj, Display):
2324 2324 return obj.display()
2325 2325 else:
2326 2326 _originalhook(obj)
2327 2327 sys.displayhook = displayhook
2328 2328 installdisplayhook()
@@ -1,271 +1,271 b''
1 1 """Shell mode for IPython.
2 2
3 3 Start ipython in shell mode by invoking "ipython -p sh"
4 4
5 5 (the old version, "ipython -p pysh" still works but this is the more "modern"
6 6 shell mode and is recommended for users who don't care about pysh-mode
7 7 compatibility)
8 8 """
9 9
10 10 from IPython.core import ipapi
11 11 from IPython.core.error import TryNext
12 12 import os,re,textwrap
13 13
14 14 # The import below effectively obsoletes your old-style ipythonrc[.ini],
15 15 # so consider yourself warned!
16 16
17 17 import ipy_defaults
18 18
19 19 def main():
20 20 ip = ipapi.get()
21 21 o = ip.options
22 22 # autocall to "full" mode (smart mode is default, I like full mode)
23 23
24 24 o.autocall = 2
25 25
26 26 # Jason Orendorff's path class is handy to have in user namespace
27 27 # if you are doing shell-like stuff
28 28 try:
29 29 ip.ex("from IPython.external.path import path" )
30 30 except ImportError:
31 31 pass
32 32
33 33 # beefed up %env is handy in shell mode
34 34 import envpersist
35 35
36 36 # To see where mycmd resides (in path/aliases), do %which mycmd
37 37 import ipy_which
38 38
39 39 # tab completers for hg, svn, ...
40 40 import ipy_app_completers
41 41
42 42 # To make executables foo and bar in mybin usable without PATH change, do:
43 43 # %rehashdir c:/mybin
44 44 # %store foo
45 45 # %store bar
46 46 import ipy_rehashdir
47 47
48 48 # does not work without subprocess module!
49 49 #import ipy_signals
50 50
51 51 ip.ex('import os')
52 52 ip.ex("def up(): os.chdir('..')")
53 53 ip.user_ns['LA'] = LastArgFinder()
54 54
55 55 # You can assign to _prompt_title variable
56 56 # to provide some extra information for prompt
57 57 # (e.g. the current mode, host/username...)
58 58
59 59 ip.user_ns['_prompt_title'] = ''
60 60
61 61 # Nice prompt
62 62 o.prompt_in1= r'\C_Green${_prompt_title}\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> '
63 63 o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green> '
64 64 o.prompt_out= '<\#> '
65 65
66 66 from IPython.core import release
67 67
68 68 import sys
69 69 # Non-chatty banner
70 70 o.banner = "IPython %s [on Py %s]\n" % (release.version,sys.version.split(None,1)[0])
71 71
72 72
73 73 ip.default_option('cd','-q')
74 74 ip.default_option('macro', '-r')
75 75 # If you only rarely want to execute the things you %edit...
76 76 #ip.default_option('edit','-x')
77 77
78 78
79 79 o.prompts_pad_left="1"
80 80 # Remove all blank lines in between prompts, like a normal shell.
81 81 o.separate_in="0"
82 82 o.separate_out="0"
83 83 o.separate_out2="0"
84 84
85 85 # now alias all syscommands
86 86
87 87 db = ip.db
88 88
89 89 syscmds = db.get("syscmdlist",[] )
90 90 if not syscmds:
91 91 print textwrap.dedent("""
92 92 System command list not initialized, probably the first run...
93 93 running %rehashx to refresh the command list. Run %rehashx
94 94 again to refresh command list (after installing new software etc.)
95 95 """)
96 96 ip.magic('rehashx')
97 97 syscmds = db.get("syscmdlist")
98 98
99 99 # lowcase aliases on win32 only
100 100 if os.name == 'posix':
101 101 mapper = lambda s:s
102 102 else:
103 103 def mapper(s): return s.lower()
104 104
105 105 for cmd in syscmds:
106 106 # print "sys",cmd #dbg
107 107 noext, ext = os.path.splitext(cmd)
108 108 if ext.lower() == '.exe':
109 109 cmd = noext
110 110
111 111 key = mapper(cmd)
112 if key not in ip.alias_table:
112 if key not in ip.alias_manager.alias_table:
113 113 # Dots will be removed from alias names, since ipython
114 114 # assumes names with dots to be python code
115 115
116 116 ip.define_alias(key.replace('.',''), cmd)
117 117
118 118 # mglob combines 'find', recursion, exclusion... '%mglob?' to learn more
119 119 ip.load("IPython.external.mglob")
120 120
121 121 # win32 is crippled w/o cygwin, try to help it a little bit
122 122 if sys.platform == 'win32':
123 123 if 'cygwin' in os.environ['PATH'].lower():
124 124 # use the colors of cygwin ls (recommended)
125 125 ip.define_alias('d', 'ls -F --color=auto')
126 126 else:
127 127 # get icp, imv, imkdir, igrep, irm,...
128 128 ip.load('ipy_fsops')
129 129
130 130 # and the next best thing to real 'ls -F'
131 131 ip.define_alias('d','dir /w /og /on')
132 132
133 133 ip.set_hook('input_prefilter', slash_prefilter_f)
134 134 extend_shell_behavior(ip)
135 135
136 136 class LastArgFinder:
137 137 """ Allow $LA to work as "last argument of previous command", like $! in bash
138 138
139 139 To call this in normal IPython code, do LA()
140 140 """
141 141 def __call__(self, hist_idx = None):
142 142 ip = ipapi.get()
143 143 if hist_idx is None:
144 144 return str(self)
145 145 return ip.input_hist_raw[hist_idx].strip().split()[-1]
146 146 def __str__(self):
147 147 ip = ipapi.get()
148 148 for cmd in reversed(ip.input_hist_raw):
149 149 parts = cmd.strip().split()
150 150 if len(parts) < 2 or parts[-1] in ['$LA', 'LA()']:
151 151 continue
152 152 return parts[-1]
153 153 return ""
154 154
155 155 def slash_prefilter_f(self,line):
156 156 """ ./foo, ~/foo and /bin/foo now run foo as system command
157 157
158 158 Removes the need for doing !./foo, !~/foo or !/bin/foo
159 159 """
160 160 from IPython.utils import genutils
161 161 if re.match('(?:[.~]|/[a-zA-Z_0-9]+)/', line):
162 return "_ip.system(" + genutils.make_quoted_expr(line)+")"
162 return "get_ipython().system(" + genutils.make_quoted_expr(line)+")"
163 163 raise TryNext
164 164
165 165 # XXX You do not need to understand the next function!
166 166 # This should probably be moved out of profile
167 167
168 168 def extend_shell_behavior(ip):
169 169
170 170 # Instead of making signature a global variable tie it to IPSHELL.
171 171 # In future if it is required to distinguish between different
172 172 # shells we can assign a signature per shell basis
173 173 ip.__sig__ = 0xa005
174 174 # mark the IPSHELL with this signature
175 175 ip.user_ns['__builtins__'].__dict__['__sig__'] = ip.__sig__
176 176
177 177 from IPython.external.Itpl import ItplNS
178 178 from IPython.utils.genutils import shell
179 179 # utility to expand user variables via Itpl
180 180 # xxx do something sensible with depth?
181 181 ip.var_expand = lambda cmd, lvars=None, depth=2: \
182 182 str(ItplNS(cmd, ip.user_ns, get_locals()))
183 183
184 184 def get_locals():
185 185 """ Substituting a variable through Itpl deep inside the IPSHELL stack
186 186 requires the knowledge of all the variables in scope upto the last
187 187 IPSHELL frame. This routine simply merges all the local variables
188 188 on the IPSHELL stack without worrying about their scope rules
189 189 """
190 190 import sys
191 191 # note lambda expression constitues a function call
192 192 # hence fno should be incremented by one
193 193 getsig = lambda fno: sys._getframe(fno+1).f_globals \
194 194 ['__builtins__'].__dict__['__sig__']
195 195 getlvars = lambda fno: sys._getframe(fno+1).f_locals
196 196 # trackback until we enter the IPSHELL
197 197 frame_no = 1
198 198 sig = ip.__sig__
199 199 fsig = ~sig
200 200 while fsig != sig :
201 201 try:
202 202 fsig = getsig(frame_no)
203 203 except (AttributeError, KeyError):
204 204 frame_no += 1
205 205 except ValueError:
206 206 # stack is depleted
207 207 # call did not originate from IPSHELL
208 208 return {}
209 209 first_frame = frame_no
210 210 # walk further back until we exit from IPSHELL or deplete stack
211 211 try:
212 212 while(sig == getsig(frame_no+1)):
213 213 frame_no += 1
214 214 except (AttributeError, KeyError, ValueError):
215 215 pass
216 216 # merge the locals from top down hence overriding
217 217 # any re-definitions of variables, functions etc.
218 218 lvars = {}
219 219 for fno in range(frame_no, first_frame-1, -1):
220 220 lvars.update(getlvars(fno))
221 221 #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg
222 222 return lvars
223 223
224 224 def _runlines(lines):
225 225 """Run a string of one or more lines of source.
226 226
227 227 This method is capable of running a string containing multiple source
228 228 lines, as if they had been entered at the IPython prompt. Since it
229 229 exposes IPython's processing machinery, the given strings can contain
230 230 magic calls (%magic), special shell access (!cmd), etc."""
231 231
232 232 # We must start with a clean buffer, in case this is run from an
233 233 # interactive IPython session (via a magic, for example).
234 234 ip.resetbuffer()
235 235 lines = lines.split('\n')
236 236 more = 0
237 237 command = ''
238 238 for line in lines:
239 239 # skip blank lines so we don't mess up the prompt counter, but do
240 240 # NOT skip even a blank line if we are in a code block (more is
241 241 # true)
242 242 # if command is not empty trim the line
243 243 if command != '' :
244 244 line = line.strip()
245 245 # add the broken line to the command
246 246 if line and line[-1] == '\\' :
247 247 command += line[0:-1] + ' '
248 248 more = True
249 249 continue
250 250 else :
251 251 # add the last (current) line to the command
252 252 command += line
253 253 if command or more:
254 254 # push to raw history, so hist line numbers stay in sync
255 255 ip.input_hist_raw.append("# " + command + "\n")
256 256
257 257 more = ip.push_line(ip.prefilter(command,more))
258 258 command = ''
259 259 # IPython's runsource returns None if there was an error
260 260 # compiling the code. This allows us to stop processing right
261 261 # away, so the user gets the error message at the right place.
262 262 if more is None:
263 263 break
264 264 # final newline in case the input didn't have it, so that the code
265 265 # actually does get executed
266 266 if more:
267 267 ip.push_line('\n')
268 268
269 269 ip.runlines = _runlines
270 270
271 271 main()
@@ -1,140 +1,140 b''
1 1 # -*- coding: utf-8 -*-
2 2 """ IPython extension: add %rehashdir magic
3 3
4 4 Usage:
5 5
6 6 %rehashdir c:/bin c:/tools
7 7 - Add all executables under c:/bin and c:/tools to alias table, in
8 8 order to make them directly executable from any directory.
9 9
10 10 This also serves as an example on how to extend ipython
11 11 with new magic functions.
12 12
13 13 Unlike rest of ipython, this requires Python 2.4 (optional
14 14 extensions are allowed to do that).
15 15
16 16 """
17 17
18 18 from IPython.core import ipapi
19 19 ip = ipapi.get()
20 20
21 21
22 22 import os,re,fnmatch,sys
23 23
24 24 def selflaunch(ip,line):
25 25 """ Launch python script with 'this' interpreter
26 26
27 27 e.g. d:\foo\ipykit.exe a.py
28 28
29 29 """
30 30
31 31 tup = line.split(None,1)
32 32 if len(tup) == 1:
33 33 print "Launching nested ipython session"
34 34 os.system(sys.executable)
35 35 return
36 36
37 37 cmd = sys.executable + ' ' + tup[1]
38 38 print ">",cmd
39 39 os.system(cmd)
40 40
41 41 class PyLauncher:
42 42 """ Invoke selflanucher on the specified script
43 43
44 44 This is mostly useful for associating with scripts using::
45 45 _ip.define_alias('foo',PyLauncher('foo_script.py'))
46 46
47 47 """
48 48 def __init__(self,script):
49 49 self.script = os.path.abspath(script)
50 50 def __call__(self, ip, line):
51 51 if self.script.endswith('.ipy'):
52 52 ip.runlines(open(self.script).read())
53 53 else:
54 54 # first word is the script/alias name itself, strip it
55 55 tup = line.split(None,1)
56 56 if len(tup) == 2:
57 57 tail = ' ' + tup[1]
58 58 else:
59 59 tail = ''
60 60
61 61 selflaunch(ip,"py " + self.script + tail)
62 62 def __repr__(self):
63 63 return 'PyLauncher("%s")' % self.script
64 64
65 65 def rehashdir_f(self,arg):
66 66 """ Add executables in all specified dirs to alias table
67 67
68 68 Usage:
69 69
70 70 %rehashdir c:/bin;c:/tools
71 71 - Add all executables under c:/bin and c:/tools to alias table, in
72 72 order to make them directly executable from any directory.
73 73
74 74 Without arguments, add all executables in current directory.
75 75
76 76 """
77 77
78 78 # most of the code copied from Magic.magic_rehashx
79 79
80 80 def isjunk(fname):
81 81 junk = ['*~']
82 82 for j in junk:
83 83 if fnmatch.fnmatch(fname, j):
84 84 return True
85 85 return False
86 86
87 87 created = []
88 88 if not arg:
89 89 arg = '.'
90 90 path = map(os.path.abspath,arg.split(';'))
91 alias_table = self.shell.alias_table
91 alias_table = self.shell.alias_manager.alias_table
92 92
93 93 if os.name == 'posix':
94 94 isexec = lambda fname:os.path.isfile(fname) and \
95 95 os.access(fname,os.X_OK)
96 96 else:
97 97
98 98 try:
99 99 winext = os.environ['pathext'].replace(';','|').replace('.','')
100 100 except KeyError:
101 101 winext = 'exe|com|bat|py'
102 102 if 'py' not in winext:
103 103 winext += '|py'
104 104
105 105 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
106 106 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
107 107 savedir = os.getcwd()
108 108 try:
109 109 # write the whole loop for posix/Windows so we don't have an if in
110 110 # the innermost part
111 111 if os.name == 'posix':
112 112 for pdir in path:
113 113 os.chdir(pdir)
114 114 for ff in os.listdir(pdir):
115 115 if isexec(ff) and not isjunk(ff):
116 116 # each entry in the alias table must be (N,name),
117 117 # where N is the number of positional arguments of the
118 118 # alias.
119 119 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
120 120 created.append(src)
121 121 alias_table[src] = (0,tgt)
122 122 else:
123 123 for pdir in path:
124 124 os.chdir(pdir)
125 125 for ff in os.listdir(pdir):
126 126 if isexec(ff) and not isjunk(ff):
127 127 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
128 128 src = src.lower()
129 129 created.append(src)
130 130 alias_table[src] = (0,tgt)
131 131 # Make sure the alias table doesn't contain keywords or builtins
132 132 self.shell.alias_table_validate()
133 133 # Call again init_auto_alias() so we get 'rm -i' and other
134 134 # modified aliases since %rehashx will probably clobber them
135 135 # self.shell.init_auto_alias()
136 136 finally:
137 137 os.chdir(savedir)
138 138 return created
139 139
140 140 ip.define_magic("rehashdir",rehashdir_f)
@@ -1,243 +1,243 b''
1 1 """ Preliminary "job control" extensions for IPython
2 2
3 3 requires python 2.4 (or separate 'subprocess' module
4 4
5 5 This provides 2 features, launching background jobs and killing foreground jobs from another IPython instance.
6 6
7 7 Launching background jobs:
8 8
9 9 Usage:
10 10
11 11 [ipython]|2> import jobctrl
12 12 [ipython]|3> &ls
13 13 <3> <jobctrl.IpyPopen object at 0x00D87FD0>
14 14 [ipython]|4> _3.go
15 15 -----------> _3.go()
16 16 ChangeLog
17 17 IPython
18 18 MANIFEST.in
19 19 README
20 20 README_Windows.txt
21 21
22 22 ...
23 23
24 24 Killing foreground tasks:
25 25
26 26 Launch IPython instance, run a blocking command:
27 27
28 28 [Q:/ipython]|1> import jobctrl
29 29 [Q:/ipython]|2> cat
30 30
31 31 Now launch a new IPython prompt and kill the process:
32 32
33 33 IPython 0.8.3.svn.r2919 [on Py 2.5]
34 34 [Q:/ipython]|1> import jobctrl
35 35 [Q:/ipython]|2> %tasks
36 36 6020: 'cat ' (Q:\ipython)
37 37 [Q:/ipython]|3> %kill
38 38 SUCCESS: The process with PID 6020 has been terminated.
39 39 [Q:/ipython]|4>
40 40
41 41 (you don't need to specify PID for %kill if only one task is running)
42 42 """
43 43
44 44 from subprocess import *
45 45 import os,shlex,sys,time
46 46 import threading,Queue
47 47
48 48 from IPython.utils import genutils
49 49
50 50 from IPython.core import ipapi
51 51 from IPython.core.error import TryNext
52 52
53 53 if os.name == 'nt':
54 54 def kill_process(pid):
55 55 os.system('taskkill /F /PID %d' % pid)
56 56 else:
57 57 def kill_process(pid):
58 58 os.system('kill -9 %d' % pid)
59 59
60 60
61 61
62 62 class IpyPopen(Popen):
63 63 def go(self):
64 64 print self.communicate()[0]
65 65 def __repr__(self):
66 66 return '<IPython job "%s" PID=%d>' % (self.line, self.pid)
67 67
68 68 def kill(self):
69 69 kill_process(self.pid)
70 70
71 71 def startjob(job):
72 72 p = IpyPopen(shlex.split(job), stdout=PIPE, shell = False)
73 73 p.line = job
74 74 return p
75 75
76 76 class AsyncJobQ(threading.Thread):
77 77 def __init__(self):
78 78 threading.Thread.__init__(self)
79 79 self.q = Queue.Queue()
80 80 self.output = []
81 81 self.stop = False
82 82 def run(self):
83 83 while 1:
84 84 cmd,cwd = self.q.get()
85 85 if self.stop:
86 86 self.output.append("** Discarding: '%s' - %s" % (cmd,cwd))
87 87 continue
88 88 self.output.append("** Task started: '%s' - %s" % (cmd,cwd))
89 89
90 90 p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd = cwd)
91 91 out = p.stdout.read()
92 92 self.output.append("** Task complete: '%s'\n" % cmd)
93 93 self.output.append(out)
94 94
95 95 def add(self,cmd):
96 96 self.q.put_nowait((cmd, os.getcwd()))
97 97
98 98 def dumpoutput(self):
99 99 while self.output:
100 100 item = self.output.pop(0)
101 101 print item
102 102
103 103 _jobq = None
104 104
105 105 def jobqueue_f(self, line):
106 106
107 107 global _jobq
108 108 if not _jobq:
109 109 print "Starting jobqueue - do '&some_long_lasting_system_command' to enqueue"
110 110 _jobq = AsyncJobQ()
111 111 _jobq.setDaemon(True)
112 112 _jobq.start()
113 113 ip.jobq = _jobq.add
114 114 return
115 115 if line.strip() == 'stop':
116 116 print "Stopping and clearing jobqueue, %jobqueue start to start again"
117 117 _jobq.stop = True
118 118 return
119 119 if line.strip() == 'start':
120 120 _jobq.stop = False
121 121 return
122 122
123 123 def jobctrl_prefilter_f(self,line):
124 124 if line.startswith('&'):
125 125 pre,fn,rest = self.split_user_input(line[1:])
126 126
127 127 line = ip.expand_aliases(fn,rest)
128 128 if not _jobq:
129 return '_ip.startjob(%s)' % genutils.make_quoted_expr(line)
130 return '_ip.jobq(%s)' % genutils.make_quoted_expr(line)
129 return 'get_ipython().startjob(%s)' % genutils.make_quoted_expr(line)
130 return 'get_ipython().jobq(%s)' % genutils.make_quoted_expr(line)
131 131
132 132 raise TryNext
133 133
134 134 def jobq_output_hook(self):
135 135 if not _jobq:
136 136 return
137 137 _jobq.dumpoutput()
138 138
139 139
140 140
141 141 def job_list(ip):
142 142 keys = ip.db.keys('tasks/*')
143 143 ents = [ip.db[k] for k in keys]
144 144 return ents
145 145
146 146 def magic_tasks(self,line):
147 147 """ Show a list of tasks.
148 148
149 149 A 'task' is a process that has been started in IPython when 'jobctrl' extension is enabled.
150 150 Tasks can be killed with %kill.
151 151
152 152 '%tasks clear' clears the task list (from stale tasks)
153 153 """
154 154 ip = self.getapi()
155 155 if line.strip() == 'clear':
156 156 for k in ip.db.keys('tasks/*'):
157 157 print "Clearing",ip.db[k]
158 158 del ip.db[k]
159 159 return
160 160
161 161 ents = job_list(ip)
162 162 if not ents:
163 163 print "No tasks running"
164 164 for pid,cmd,cwd,t in ents:
165 165 dur = int(time.time()-t)
166 166 print "%d: '%s' (%s) %d:%02d" % (pid,cmd,cwd, dur / 60,dur%60)
167 167
168 168 def magic_kill(self,line):
169 169 """ Kill a task
170 170
171 171 Without args, either kill one task (if only one running) or show list (if many)
172 172 With arg, assume it's the process id.
173 173
174 174 %kill is typically (much) more powerful than trying to terminate a process with ctrl+C.
175 175 """
176 176 ip = self.getapi()
177 177 jobs = job_list(ip)
178 178
179 179 if not line.strip():
180 180 if len(jobs) == 1:
181 181 kill_process(jobs[0][0])
182 182 else:
183 183 magic_tasks(self,line)
184 184 return
185 185
186 186 try:
187 187 pid = int(line)
188 188 kill_process(pid)
189 189 except ValueError:
190 190 magic_tasks(self,line)
191 191
192 192 if sys.platform == 'win32':
193 193 shell_internal_commands = 'break chcp cls copy ctty date del erase dir md mkdir path prompt rd rmdir start time type ver vol'.split()
194 194 PopenExc = WindowsError
195 195 else:
196 196 # todo linux commands
197 197 shell_internal_commands = []
198 198 PopenExc = OSError
199 199
200 200
201 201 def jobctrl_shellcmd(ip,cmd):
202 202 """ os.system replacement that stores process info to db['tasks/t1234'] """
203 203 cmd = cmd.strip()
204 204 cmdname = cmd.split(None,1)[0]
205 205 if cmdname in shell_internal_commands or '|' in cmd or '>' in cmd or '<' in cmd:
206 206 use_shell = True
207 207 else:
208 208 use_shell = False
209 209
210 210 jobentry = None
211 211 try:
212 212 try:
213 213 p = Popen(cmd,shell = use_shell)
214 214 except PopenExc :
215 215 if use_shell:
216 216 # try with os.system
217 217 os.system(cmd)
218 218 return
219 219 else:
220 220 # have to go via shell, sucks
221 221 p = Popen(cmd,shell = True)
222 222
223 223 jobentry = 'tasks/t' + str(p.pid)
224 224 ip.db[jobentry] = (p.pid,cmd,os.getcwd(),time.time())
225 225 p.communicate()
226 226
227 227 finally:
228 228 if jobentry:
229 229 del ip.db[jobentry]
230 230
231 231
232 232 def install():
233 233 global ip
234 234 ip = ipapi.get()
235 235 # needed to make startjob visible as _ip.startjob('blah')
236 236 ip.startjob = startjob
237 237 ip.set_hook('input_prefilter', jobctrl_prefilter_f)
238 238 ip.set_hook('shell_hook', jobctrl_shellcmd)
239 239 ip.define_magic('kill',magic_kill)
240 240 ip.define_magic('tasks',magic_tasks)
241 241 ip.define_magic('jobqueue',jobqueue_f)
242 242 ip.set_hook('pre_prompt_hook', jobq_output_hook)
243 243 install()
@@ -1,182 +1,183 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 5 Stores variables, aliases etc. in PickleShare database.
6 6 """
7 7
8 8 from IPython.core import ipapi
9 9 from IPython.core.error import TryNext, UsageError
10 10 ip = ipapi.get()
11 11
12 12 import pickleshare
13 13
14 14 import inspect,pickle,os,sys,textwrap
15 15 from IPython.core.fakemodule import FakeModule
16 16
17 17 def restore_aliases(self):
18 18 ip = self.getapi()
19 19 staliases = ip.db.get('stored_aliases', {})
20 20 for k,v in staliases.items():
21 21 #print "restore alias",k,v # dbg
22 22 #self.alias_table[k] = v
23 23 ip.define_alias(k,v)
24 24
25 25
26 26 def refresh_variables(ip):
27 27 db = ip.db
28 28 for key in db.keys('autorestore/*'):
29 29 # strip autorestore
30 30 justkey = os.path.basename(key)
31 31 try:
32 32 obj = db[key]
33 33 except KeyError:
34 34 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
35 35 print "The error was:",sys.exc_info()[0]
36 36 else:
37 37 #print "restored",justkey,"=",obj #dbg
38 38 ip.user_ns[justkey] = obj
39 39
40 40
41 41 def restore_dhist(ip):
42 42 db = ip.db
43 43 ip.user_ns['_dh'] = db.get('dhist',[])
44 44
45 45 def restore_data(self):
46 46 ip = self.getapi()
47 47 refresh_variables(ip)
48 48 restore_aliases(self)
49 49 restore_dhist(self)
50 50 raise TryNext
51 51
52 52 ip.set_hook('late_startup_hook', restore_data)
53 53
54 54 def magic_store(self, parameter_s=''):
55 55 """Lightweight persistence for python variables.
56 56
57 57 Example:
58 58
59 59 ville@badger[~]|1> A = ['hello',10,'world']\\
60 60 ville@badger[~]|2> %store A\\
61 61 ville@badger[~]|3> Exit
62 62
63 63 (IPython session is closed and started again...)
64 64
65 65 ville@badger:~$ ipython -p pysh\\
66 66 ville@badger[~]|1> print A
67 67
68 68 ['hello', 10, 'world']
69 69
70 70 Usage:
71 71
72 72 %store - Show list of all variables and their current values\\
73 73 %store <var> - Store the *current* value of the variable to disk\\
74 74 %store -d <var> - Remove the variable and its value from storage\\
75 75 %store -z - Remove all variables from storage\\
76 76 %store -r - Refresh all variables from store (delete current vals)\\
77 77 %store foo >a.txt - Store value of foo to new file a.txt\\
78 78 %store foo >>a.txt - Append value of foo to file a.txt\\
79 79
80 80 It should be noted that if you change the value of a variable, you
81 81 need to %store it again if you want to persist the new value.
82 82
83 83 Note also that the variables will need to be pickleable; most basic
84 84 python types can be safely %stored.
85 85
86 86 Also aliases can be %store'd across sessions.
87 87 """
88 88
89 89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
90 90 args = argsl.split(None,1)
91 91 ip = self.getapi()
92 92 db = ip.db
93 93 # delete
94 94 if opts.has_key('d'):
95 95 try:
96 96 todel = args[0]
97 97 except IndexError:
98 98 raise UsageError('You must provide the variable to forget')
99 99 else:
100 100 try:
101 101 del db['autorestore/' + todel]
102 102 except:
103 103 raise UsageError("Can't delete variable '%s'" % todel)
104 104 # reset
105 105 elif opts.has_key('z'):
106 106 for k in db.keys('autorestore/*'):
107 107 del db[k]
108 108
109 109 elif opts.has_key('r'):
110 110 refresh_variables(ip)
111 111
112 112
113 113 # run without arguments -> list variables & values
114 114 elif not args:
115 115 vars = self.db.keys('autorestore/*')
116 116 vars.sort()
117 117 if vars:
118 118 size = max(map(len,vars))
119 119 else:
120 120 size = 0
121 121
122 122 print 'Stored variables and their in-db values:'
123 123 fmt = '%-'+str(size)+'s -> %s'
124 124 get = db.get
125 125 for var in vars:
126 126 justkey = os.path.basename(var)
127 127 # print 30 first characters from every var
128 128 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
129 129
130 130 # default action - store the variable
131 131 else:
132 132 # %store foo >file.txt or >>file.txt
133 133 if len(args) > 1 and args[1].startswith('>'):
134 134 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
135 135 if args[1].startswith('>>'):
136 136 fil = open(fnam,'a')
137 137 else:
138 138 fil = open(fnam,'w')
139 139 obj = ip.ev(args[0])
140 140 print "Writing '%s' (%s) to file '%s'." % (args[0],
141 141 obj.__class__.__name__, fnam)
142 142
143 143
144 144 if not isinstance (obj,basestring):
145 145 from pprint import pprint
146 146 pprint(obj,fil)
147 147 else:
148 148 fil.write(obj)
149 149 if not obj.endswith('\n'):
150 150 fil.write('\n')
151 151
152 152 fil.close()
153 153 return
154 154
155 155 # %store foo
156 156 try:
157 157 obj = ip.user_ns[args[0]]
158 158 except KeyError:
159 159 # it might be an alias
160 # This needs to be refactored to use the new AliasManager stuff.
160 161 if args[0] in self.alias_table:
161 162 staliases = db.get('stored_aliases',{})
162 163 staliases[ args[0] ] = self.alias_table[ args[0] ]
163 164 db['stored_aliases'] = staliases
164 165 print "Alias stored:", args[0], self.alias_table[ args[0] ]
165 166 return
166 167 else:
167 168 raise UsageError("Unknown variable '%s'" % args[0])
168 169
169 170 else:
170 171 if isinstance(inspect.getmodule(obj), FakeModule):
171 172 print textwrap.dedent("""\
172 173 Warning:%s is %s
173 174 Proper storage of interactively declared classes (or instances
174 175 of those classes) is not possible! Only instances
175 176 of classes in real modules on file system can be %%store'd.
176 177 """ % (args[0], obj) )
177 178 return
178 179 #pickled = pickle.dumps(obj)
179 180 self.db[ 'autorestore/' + args[0] ] = obj
180 181 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
181 182
182 183 ip.define_magic('store',magic_store)
@@ -1,171 +1,171 b''
1 1 # encoding: utf-8
2 2
3 3 """Magic command interface for interactive parallel work."""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 #-------------------------------------------------------------------------------
8 8 # Copyright (C) 2008 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-------------------------------------------------------------------------------
13 13
14 14 #-------------------------------------------------------------------------------
15 15 # Imports
16 16 #-------------------------------------------------------------------------------
17 17
18 18 import new
19 19
20 20 from IPython.core.iplib import InteractiveShell
21 21 from IPython.core.shell import MTInteractiveShell
22 22
23 23 from twisted.internet.defer import Deferred
24 24
25 25
26 26 #-------------------------------------------------------------------------------
27 27 # Definitions of magic functions for use with IPython
28 28 #-------------------------------------------------------------------------------
29 29
30 30 NO_ACTIVE_CONTROLLER = """
31 31 Error: No Controller is activated
32 32 Use activate() on a RemoteController object to activate it for magics.
33 33 """
34 34
35 35 def magic_result(self,parameter_s=''):
36 36 """Print the result of command i on all engines of the active controller.
37 37
38 38 To activate a controller in IPython, first create it and then call
39 39 the activate() method.
40 40
41 41 Then you can do the following:
42 42
43 43 >>> result # Print the latest result
44 44 Printing result...
45 45 [127.0.0.1:0] In [1]: b = 10
46 46 [127.0.0.1:1] In [1]: b = 10
47 47
48 48 >>> result 0 # Print result 0
49 49 In [14]: result 0
50 50 Printing result...
51 51 [127.0.0.1:0] In [0]: a = 5
52 52 [127.0.0.1:1] In [0]: a = 5
53 53 """
54 54 try:
55 55 activeController = __IPYTHON__.activeController
56 56 except AttributeError:
57 57 print NO_ACTIVE_CONTROLLER
58 58 else:
59 59 try:
60 60 index = int(parameter_s)
61 61 except:
62 62 index = None
63 63 result = activeController.get_result(index)
64 64 return result
65 65
66 66 def magic_px(self,parameter_s=''):
67 67 """Executes the given python command on the active IPython Controller.
68 68
69 69 To activate a Controller in IPython, first create it and then call
70 70 the activate() method.
71 71
72 72 Then you can do the following:
73 73
74 74 >>> %px a = 5 # Runs a = 5 on all nodes
75 75 """
76 76
77 77 try:
78 78 activeController = __IPYTHON__.activeController
79 79 except AttributeError:
80 80 print NO_ACTIVE_CONTROLLER
81 81 else:
82 82 print "Parallel execution on engines: %s" % activeController.targets
83 83 result = activeController.execute(parameter_s)
84 84 return result
85 85
86 86 def pxrunsource(self, source, filename="<input>", symbol="single"):
87 87
88 88 try:
89 89 code = self.compile(source, filename, symbol)
90 90 except (OverflowError, SyntaxError, ValueError):
91 91 # Case 1
92 92 self.showsyntaxerror(filename)
93 93 return None
94 94
95 95 if code is None:
96 96 # Case 2
97 97 return True
98 98
99 99 # Case 3
100 100 # Because autopx is enabled, we now call executeAll or disable autopx if
101 101 # %autopx or autopx has been called
102 if '_ip.magic("%autopx' in source or '_ip.magic("autopx' in source:
102 if 'get_ipython().magic("%autopx' in source or 'get_ipython().magic("autopx' in source:
103 103 _disable_autopx(self)
104 104 return False
105 105 else:
106 106 try:
107 107 result = self.activeController.execute(source)
108 108 except:
109 109 self.showtraceback()
110 110 else:
111 111 print result.__repr__()
112 112 return False
113 113
114 114 def magic_autopx(self, parameter_s=''):
115 115 """Toggles auto parallel mode for the active IPython Controller.
116 116
117 117 To activate a Controller in IPython, first create it and then call
118 118 the activate() method.
119 119
120 120 Then you can do the following:
121 121
122 122 >>> %autopx # Now all commands are executed in parallel
123 123 Auto Parallel Enabled
124 124 Type %autopx to disable
125 125 ...
126 126 >>> %autopx # Now all commands are locally executed
127 127 Auto Parallel Disabled
128 128 """
129 129
130 130 if hasattr(self, 'autopx'):
131 131 if self.autopx == True:
132 132 _disable_autopx(self)
133 133 else:
134 134 _enable_autopx(self)
135 135 else:
136 136 _enable_autopx(self)
137 137
138 138 def _enable_autopx(self):
139 139 """Enable %autopx mode by saving the original runsource and installing
140 140 pxrunsource.
141 141 """
142 142 try:
143 143 activeController = __IPYTHON__.activeController
144 144 except AttributeError:
145 145 print "No active RemoteController found, use RemoteController.activate()."
146 146 else:
147 147 self._original_runsource = self.runsource
148 148 self.runsource = new.instancemethod(pxrunsource, self, self.__class__)
149 149 self.autopx = True
150 150 print "Auto Parallel Enabled\nType %autopx to disable"
151 151
152 152 def _disable_autopx(self):
153 153 """Disable %autopx by restoring the original runsource."""
154 154 if hasattr(self, 'autopx'):
155 155 if self.autopx == True:
156 156 self.runsource = self._original_runsource
157 157 self.autopx = False
158 158 print "Auto Parallel Disabled"
159 159
160 160 # Add the new magic function to the class dict:
161 161
162 162 InteractiveShell.magic_result = magic_result
163 163 InteractiveShell.magic_px = magic_px
164 164 InteractiveShell.magic_autopx = magic_autopx
165 165
166 166 # And remove the global name to keep global namespace clean. Don't worry, the
167 167 # copy bound to IPython stays, we're just removing the global name.
168 168 del magic_result
169 169 del magic_px
170 170 del magic_autopx
171 171
@@ -1,687 +1,690 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 Tests for IPython.utils.traitlets.
5 5
6 6 Authors:
7 7
8 8 * Brian Granger
9 9 * Enthought, Inc. Some of the code in this file comes from enthought.traits
10 10 and is licensed under the BSD license. Also, many of the ideas also come
11 11 from enthought.traits even though our implementation is very different.
12 12 """
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2008-2009 The IPython Development Team
16 16 #
17 17 # Distributed under the terms of the BSD License. The full license is in
18 18 # the file COPYING, distributed as part of this software.
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Imports
23 23 #-----------------------------------------------------------------------------
24 24
25 25 import sys
26 26 import os
27 27
28 28
29 29 from unittest import TestCase
30 30
31 31 from IPython.utils.traitlets import (
32 32 HasTraitlets, MetaHasTraitlets, TraitletType, Any,
33 33 Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError,
34 34 Undefined, Type, This, Instance
35 35 )
36 36
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Helper classes for testing
40 40 #-----------------------------------------------------------------------------
41 41
42 42
43 43 class HasTraitletsStub(HasTraitlets):
44 44
45 45 def _notify_traitlet(self, name, old, new):
46 46 self._notify_name = name
47 47 self._notify_old = old
48 48 self._notify_new = new
49 49
50 50
51 51 #-----------------------------------------------------------------------------
52 52 # Test classes
53 53 #-----------------------------------------------------------------------------
54 54
55 55
56 56 class TestTraitletType(TestCase):
57 57
58 58 def test_get_undefined(self):
59 59 class A(HasTraitlets):
60 60 a = TraitletType
61 61 a = A()
62 62 self.assertEquals(a.a, Undefined)
63 63
64 64 def test_set(self):
65 65 class A(HasTraitletsStub):
66 66 a = TraitletType
67 67
68 68 a = A()
69 69 a.a = 10
70 70 self.assertEquals(a.a, 10)
71 71 self.assertEquals(a._notify_name, 'a')
72 72 self.assertEquals(a._notify_old, Undefined)
73 73 self.assertEquals(a._notify_new, 10)
74 74
75 75 def test_validate(self):
76 76 class MyTT(TraitletType):
77 77 def validate(self, inst, value):
78 78 return -1
79 79 class A(HasTraitletsStub):
80 80 tt = MyTT
81 81
82 82 a = A()
83 83 a.tt = 10
84 84 self.assertEquals(a.tt, -1)
85 85
86 86 def test_default_validate(self):
87 87 class MyIntTT(TraitletType):
88 88 def validate(self, obj, value):
89 89 if isinstance(value, int):
90 90 return value
91 91 self.error(obj, value)
92 92 class A(HasTraitlets):
93 93 tt = MyIntTT(10)
94 94 a = A()
95 95 self.assertEquals(a.tt, 10)
96 96
97 97 # Defaults are validated when the HasTraitlets is instantiated
98 98 class B(HasTraitlets):
99 99 tt = MyIntTT('bad default')
100 100 self.assertRaises(TraitletError, B)
101 101
102 102 def test_is_valid_for(self):
103 103 class MyTT(TraitletType):
104 104 def is_valid_for(self, value):
105 105 return True
106 106 class A(HasTraitlets):
107 107 tt = MyTT
108 108
109 109 a = A()
110 110 a.tt = 10
111 111 self.assertEquals(a.tt, 10)
112 112
113 113 def test_value_for(self):
114 114 class MyTT(TraitletType):
115 115 def value_for(self, value):
116 116 return 20
117 117 class A(HasTraitlets):
118 118 tt = MyTT
119 119
120 120 a = A()
121 121 a.tt = 10
122 122 self.assertEquals(a.tt, 20)
123 123
124 124 def test_info(self):
125 125 class A(HasTraitlets):
126 126 tt = TraitletType
127 127 a = A()
128 128 self.assertEquals(A.tt.info(), 'any value')
129 129
130 130 def test_error(self):
131 131 class A(HasTraitlets):
132 132 tt = TraitletType
133 133 a = A()
134 134 self.assertRaises(TraitletError, A.tt.error, a, 10)
135 135
136 136
137 137 class TestHasTraitletsMeta(TestCase):
138 138
139 139 def test_metaclass(self):
140 140 self.assertEquals(type(HasTraitlets), MetaHasTraitlets)
141 141
142 142 class A(HasTraitlets):
143 143 a = Int
144 144
145 145 a = A()
146 146 self.assertEquals(type(a.__class__), MetaHasTraitlets)
147 147 self.assertEquals(a.a,0)
148 148 a.a = 10
149 149 self.assertEquals(a.a,10)
150 150
151 151 class B(HasTraitlets):
152 152 b = Int()
153 153
154 154 b = B()
155 155 self.assertEquals(b.b,0)
156 156 b.b = 10
157 157 self.assertEquals(b.b,10)
158 158
159 159 class C(HasTraitlets):
160 160 c = Int(30)
161 161
162 162 c = C()
163 163 self.assertEquals(c.c,30)
164 164 c.c = 10
165 165 self.assertEquals(c.c,10)
166 166
167 167 def test_this_class(self):
168 168 class A(HasTraitlets):
169 169 t = This()
170 170 tt = This()
171 171 class B(A):
172 172 tt = This()
173 173 ttt = This()
174 174 self.assertEquals(A.t.this_class, A)
175 175 self.assertEquals(B.t.this_class, A)
176 176 self.assertEquals(B.tt.this_class, B)
177 177 self.assertEquals(B.ttt.this_class, B)
178 178
179 179 class TestHasTraitletsNotify(TestCase):
180 180
181 181 def setUp(self):
182 182 self._notify1 = []
183 183 self._notify2 = []
184 184
185 185 def notify1(self, name, old, new):
186 186 self._notify1.append((name, old, new))
187 187
188 188 def notify2(self, name, old, new):
189 189 self._notify2.append((name, old, new))
190 190
191 191 def test_notify_all(self):
192 192
193 193 class A(HasTraitlets):
194 194 a = Int
195 195 b = Float
196 196
197 197 a = A()
198 198 a.on_traitlet_change(self.notify1)
199 199 a.a = 0
200 200 self.assertEquals(len(self._notify1),0)
201 201 a.b = 0.0
202 202 self.assertEquals(len(self._notify1),0)
203 203 a.a = 10
204 204 self.assert_(('a',0,10) in self._notify1)
205 205 a.b = 10.0
206 206 self.assert_(('b',0.0,10.0) in self._notify1)
207 207 self.assertRaises(TraitletError,setattr,a,'a','bad string')
208 208 self.assertRaises(TraitletError,setattr,a,'b','bad string')
209 209 self._notify1 = []
210 210 a.on_traitlet_change(self.notify1,remove=True)
211 211 a.a = 20
212 212 a.b = 20.0
213 213 self.assertEquals(len(self._notify1),0)
214 214
215 215 def test_notify_one(self):
216 216
217 217 class A(HasTraitlets):
218 218 a = Int
219 219 b = Float
220 220
221 221 a = A()
222 222 a.on_traitlet_change(self.notify1, 'a')
223 223 a.a = 0
224 224 self.assertEquals(len(self._notify1),0)
225 225 a.a = 10
226 226 self.assert_(('a',0,10) in self._notify1)
227 227 self.assertRaises(TraitletError,setattr,a,'a','bad string')
228 228
229 229 def test_subclass(self):
230 230
231 231 class A(HasTraitlets):
232 232 a = Int
233 233
234 234 class B(A):
235 235 b = Float
236 236
237 237 b = B()
238 238 self.assertEquals(b.a,0)
239 239 self.assertEquals(b.b,0.0)
240 240 b.a = 100
241 241 b.b = 100.0
242 242 self.assertEquals(b.a,100)
243 243 self.assertEquals(b.b,100.0)
244 244
245 245 def test_notify_subclass(self):
246 246
247 247 class A(HasTraitlets):
248 248 a = Int
249 249
250 250 class B(A):
251 251 b = Float
252 252
253 253 b = B()
254 254 b.on_traitlet_change(self.notify1, 'a')
255 255 b.on_traitlet_change(self.notify2, 'b')
256 256 b.a = 0
257 257 b.b = 0.0
258 258 self.assertEquals(len(self._notify1),0)
259 259 self.assertEquals(len(self._notify2),0)
260 260 b.a = 10
261 261 b.b = 10.0
262 262 self.assert_(('a',0,10) in self._notify1)
263 263 self.assert_(('b',0.0,10.0) in self._notify2)
264 264
265 265 def test_static_notify(self):
266 266
267 267 class A(HasTraitlets):
268 268 a = Int
269 269 _notify1 = []
270 270 def _a_changed(self, name, old, new):
271 271 self._notify1.append((name, old, new))
272 272
273 273 a = A()
274 274 a.a = 0
275 275 # This is broken!!!
276 276 self.assertEquals(len(a._notify1),0)
277 277 a.a = 10
278 278 self.assert_(('a',0,10) in a._notify1)
279 279
280 280 class B(A):
281 281 b = Float
282 282 _notify2 = []
283 283 def _b_changed(self, name, old, new):
284 284 self._notify2.append((name, old, new))
285 285
286 286 b = B()
287 287 b.a = 10
288 288 b.b = 10.0
289 289 self.assert_(('a',0,10) in b._notify1)
290 290 self.assert_(('b',0.0,10.0) in b._notify2)
291 291
292 292 def test_notify_args(self):
293 293
294 294 def callback0():
295 295 self.cb = ()
296 296 def callback1(name):
297 297 self.cb = (name,)
298 298 def callback2(name, new):
299 299 self.cb = (name, new)
300 300 def callback3(name, old, new):
301 301 self.cb = (name, old, new)
302 302
303 303 class A(HasTraitlets):
304 304 a = Int
305 305
306 306 a = A()
307 307 a.on_traitlet_change(callback0, 'a')
308 308 a.a = 10
309 309 self.assertEquals(self.cb,())
310 310 a.on_traitlet_change(callback0, 'a', remove=True)
311 311
312 312 a.on_traitlet_change(callback1, 'a')
313 313 a.a = 100
314 314 self.assertEquals(self.cb,('a',))
315 315 a.on_traitlet_change(callback1, 'a', remove=True)
316 316
317 317 a.on_traitlet_change(callback2, 'a')
318 318 a.a = 1000
319 319 self.assertEquals(self.cb,('a',1000))
320 320 a.on_traitlet_change(callback2, 'a', remove=True)
321 321
322 322 a.on_traitlet_change(callback3, 'a')
323 323 a.a = 10000
324 324 self.assertEquals(self.cb,('a',1000,10000))
325 325 a.on_traitlet_change(callback3, 'a', remove=True)
326 326
327 327 self.assertEquals(len(a._traitlet_notifiers['a']),0)
328 328
329 329
330 330 class TestHasTraitlets(TestCase):
331 331
332 332 def test_traitlet_names(self):
333 333 class A(HasTraitlets):
334 334 i = Int
335 335 f = Float
336 336 a = A()
337 337 self.assertEquals(a.traitlet_names(),['i','f'])
338 338
339 339 def test_traitlet_metadata(self):
340 340 class A(HasTraitlets):
341 341 i = Int(config_key='MY_VALUE')
342 342 a = A()
343 343 self.assertEquals(a.traitlet_metadata('i','config_key'), 'MY_VALUE')
344 344
345 345 def test_traitlets(self):
346 346 class A(HasTraitlets):
347 347 i = Int
348 348 f = Float
349 349 a = A()
350 350 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f))
351 351
352 352 def test_traitlets_metadata(self):
353 353 class A(HasTraitlets):
354 354 i = Int(config_key='VALUE1', other_thing='VALUE2')
355 355 f = Float(config_key='VALUE3', other_thing='VALUE2')
356 j = Int(0)
356 357 a = A()
357 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f))
358 traitlets = a.traitlets(config_key=lambda v: True)
359 self.assertEquals(traitlets, dict(i=A.i, f=A.f))
358 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f, j=A.j))
360 359 traitlets = a.traitlets(config_key='VALUE1', other_thing='VALUE2')
361 360 self.assertEquals(traitlets, dict(i=A.i))
362 traitlets = a.traitlets('config_key')
363 self.assertEquals(traitlets, dict(i=A.i, f=A.f))
361
362 # This passes, but it shouldn't because I am replicating a bug in
363 # traits.
364 traitlets = a.traitlets(config_key=lambda v: True)
365 self.assertEquals(traitlets, dict(i=A.i, f=A.f, j=A.j))
366
364 367
365 368 #-----------------------------------------------------------------------------
366 369 # Tests for specific traitlet types
367 370 #-----------------------------------------------------------------------------
368 371
369 372
370 373 class TestType(TestCase):
371 374
372 375 def test_default(self):
373 376
374 377 class B(object): pass
375 378 class A(HasTraitlets):
376 379 klass = Type
377 380
378 381 a = A()
379 382 self.assertEquals(a.klass, None)
380 383
381 384 a.klass = B
382 385 self.assertEquals(a.klass, B)
383 386 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
384 387
385 388 def test_value(self):
386 389
387 390 class B(object): pass
388 391 class C(object): pass
389 392 class A(HasTraitlets):
390 393 klass = Type(B)
391 394
392 395 a = A()
393 396 self.assertEquals(a.klass, B)
394 397 self.assertRaises(TraitletError, setattr, a, 'klass', C)
395 398 self.assertRaises(TraitletError, setattr, a, 'klass', object)
396 399 a.klass = B
397 400
398 401 def test_allow_none(self):
399 402
400 403 class B(object): pass
401 404 class C(B): pass
402 405 class A(HasTraitlets):
403 406 klass = Type(B, allow_none=False)
404 407
405 408 a = A()
406 409 self.assertEquals(a.klass, B)
407 410 self.assertRaises(TraitletError, setattr, a, 'klass', None)
408 411 a.klass = C
409 412 self.assertEquals(a.klass, C)
410 413
411 414 def test_validate_klass(self):
412 415
413 416 class A(HasTraitlets):
414 417 klass = Type('no strings allowed')
415 418
416 419 self.assertRaises(ImportError, A)
417 420
418 421 class A(HasTraitlets):
419 422 klass = Type('rub.adub.Duck')
420 423
421 424 self.assertRaises(ImportError, A)
422 425
423 426 def test_validate_default(self):
424 427
425 428 class B(object): pass
426 429 class A(HasTraitlets):
427 430 klass = Type('bad default', B)
428 431
429 432 self.assertRaises(ImportError, A)
430 433
431 434 class C(HasTraitlets):
432 435 klass = Type(None, B, allow_none=False)
433 436
434 437 self.assertRaises(TraitletError, C)
435 438
436 439 def test_str_klass(self):
437 440
438 441 class A(HasTraitlets):
439 442 klass = Type('IPython.utils.ipstruct.Struct')
440 443
441 444 from IPython.utils.ipstruct import Struct
442 445 a = A()
443 446 a.klass = Struct
444 447 self.assertEquals(a.klass, Struct)
445 448
446 449 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
447 450
448 451 class TestInstance(TestCase):
449 452
450 453 def test_basic(self):
451 454 class Foo(object): pass
452 455 class Bar(Foo): pass
453 456 class Bah(object): pass
454 457
455 458 class A(HasTraitlets):
456 459 inst = Instance(Foo)
457 460
458 461 a = A()
459 462 self.assert_(a.inst is None)
460 463 a.inst = Foo()
461 464 self.assert_(isinstance(a.inst, Foo))
462 465 a.inst = Bar()
463 466 self.assert_(isinstance(a.inst, Foo))
464 467 self.assertRaises(TraitletError, setattr, a, 'inst', Foo)
465 468 self.assertRaises(TraitletError, setattr, a, 'inst', Bar)
466 469 self.assertRaises(TraitletError, setattr, a, 'inst', Bah())
467 470
468 471 def test_unique_default_value(self):
469 472 class Foo(object): pass
470 473 class A(HasTraitlets):
471 474 inst = Instance(Foo,(),{})
472 475
473 476 a = A()
474 477 b = A()
475 478 self.assert_(a.inst is not b.inst)
476 479
477 480 def test_args_kw(self):
478 481 class Foo(object):
479 482 def __init__(self, c): self.c = c
480 483 class Bar(object): pass
481 484 class Bah(object):
482 485 def __init__(self, c, d):
483 486 self.c = c; self.d = d
484 487
485 488 class A(HasTraitlets):
486 489 inst = Instance(Foo, (10,))
487 490 a = A()
488 491 self.assertEquals(a.inst.c, 10)
489 492
490 493 class B(HasTraitlets):
491 494 inst = Instance(Bah, args=(10,), kw=dict(d=20))
492 495 b = B()
493 496 self.assertEquals(b.inst.c, 10)
494 497 self.assertEquals(b.inst.d, 20)
495 498
496 499 class C(HasTraitlets):
497 500 inst = Instance(Foo)
498 501 c = C()
499 502 self.assert_(c.inst is None)
500 503
501 504 def test_bad_default(self):
502 505 class Foo(object): pass
503 506
504 507 class A(HasTraitlets):
505 508 inst = Instance(Foo, allow_none=False)
506 509
507 510 self.assertRaises(TraitletError, A)
508 511
509 512 def test_instance(self):
510 513 class Foo(object): pass
511 514
512 515 def inner():
513 516 class A(HasTraitlets):
514 517 inst = Instance(Foo())
515 518
516 519 self.assertRaises(TraitletError, inner)
517 520
518 521
519 522 class TestThis(TestCase):
520 523
521 524 def test_this_class(self):
522 525 class Foo(HasTraitlets):
523 526 this = This
524 527
525 528 f = Foo()
526 529 self.assertEquals(f.this, None)
527 530 g = Foo()
528 531 f.this = g
529 532 self.assertEquals(f.this, g)
530 533 self.assertRaises(TraitletError, setattr, f, 'this', 10)
531 534
532 535 def test_this_inst(self):
533 536 class Foo(HasTraitlets):
534 537 this = This()
535 538
536 539 f = Foo()
537 540 f.this = Foo()
538 541 self.assert_(isinstance(f.this, Foo))
539 542
540 543 def test_subclass(self):
541 544 class Foo(HasTraitlets):
542 545 t = This()
543 546 class Bar(Foo):
544 547 pass
545 548 f = Foo()
546 549 b = Bar()
547 550 f.t = b
548 551 b.t = f
549 552 self.assertEquals(f.t, b)
550 553 self.assertEquals(b.t, f)
551 554
552 555 def test_subclass_override(self):
553 556 class Foo(HasTraitlets):
554 557 t = This()
555 558 class Bar(Foo):
556 559 t = This()
557 560 f = Foo()
558 561 b = Bar()
559 562 f.t = b
560 563 self.assertEquals(f.t, b)
561 564 self.assertRaises(TraitletError, setattr, b, 't', f)
562 565
563 566 class TraitletTestBase(TestCase):
564 567 """A best testing class for basic traitlet types."""
565 568
566 569 def assign(self, value):
567 570 self.obj.value = value
568 571
569 572 def coerce(self, value):
570 573 return value
571 574
572 575 def test_good_values(self):
573 576 if hasattr(self, '_good_values'):
574 577 for value in self._good_values:
575 578 self.assign(value)
576 579 self.assertEquals(self.obj.value, self.coerce(value))
577 580
578 581 def test_bad_values(self):
579 582 if hasattr(self, '_bad_values'):
580 583 for value in self._bad_values:
581 584 self.assertRaises(TraitletError, self.assign, value)
582 585
583 586 def test_default_value(self):
584 587 if hasattr(self, '_default_value'):
585 588 self.assertEquals(self._default_value, self.obj.value)
586 589
587 590
588 591 class AnyTraitlet(HasTraitlets):
589 592
590 593 value = Any
591 594
592 595 class AnyTraitTest(TraitletTestBase):
593 596
594 597 obj = AnyTraitlet()
595 598
596 599 _default_value = None
597 600 _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j]
598 601 _bad_values = []
599 602
600 603
601 604 class IntTraitlet(HasTraitlets):
602 605
603 606 value = Int(99)
604 607
605 608 class TestInt(TraitletTestBase):
606 609
607 610 obj = IntTraitlet()
608 611 _default_value = 99
609 612 _good_values = [10, -10]
610 613 _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L,
611 614 -10L, 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L',
612 615 u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10']
613 616
614 617
615 618 class LongTraitlet(HasTraitlets):
616 619
617 620 value = Long(99L)
618 621
619 622 class TestLong(TraitletTestBase):
620 623
621 624 obj = LongTraitlet()
622 625
623 626 _default_value = 99L
624 627 _good_values = [10, -10, 10L, -10L]
625 628 _bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,),
626 629 None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1',
627 630 '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1',
628 631 u'-10.1']
629 632
630 633
631 634 class FloatTraitlet(HasTraitlets):
632 635
633 636 value = Float(99.0)
634 637
635 638 class TestFloat(TraitletTestBase):
636 639
637 640 obj = FloatTraitlet()
638 641
639 642 _default_value = 99.0
640 643 _good_values = [10, -10, 10.1, -10.1]
641 644 _bad_values = [10L, -10L, 'ten', u'ten', [10], {'ten': 10},(10,), None,
642 645 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10',
643 646 u'-10', u'10L', u'-10L', u'10.1', u'-10.1']
644 647
645 648
646 649 class ComplexTraitlet(HasTraitlets):
647 650
648 651 value = Complex(99.0-99.0j)
649 652
650 653 class TestComplex(TraitletTestBase):
651 654
652 655 obj = ComplexTraitlet()
653 656
654 657 _default_value = 99.0-99.0j
655 658 _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j,
656 659 10.1j, 10.1+10.1j, 10.1-10.1j]
657 660 _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None]
658 661
659 662
660 663 class StringTraitlet(HasTraitlets):
661 664
662 665 value = Str('string')
663 666
664 667 class TestString(TraitletTestBase):
665 668
666 669 obj = StringTraitlet()
667 670
668 671 _default_value = 'string'
669 672 _good_values = ['10', '-10', '10L',
670 673 '-10L', '10.1', '-10.1', 'string']
671 674 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10],
672 675 ['ten'],{'ten': 10},(10,), None, u'string']
673 676
674 677
675 678 class UnicodeTraitlet(HasTraitlets):
676 679
677 680 value = Unicode(u'unicode')
678 681
679 682 class TestUnicode(TraitletTestBase):
680 683
681 684 obj = UnicodeTraitlet()
682 685
683 686 _default_value = u'unicode'
684 687 _good_values = ['10', '-10', '10L', '-10L', '10.1',
685 688 '-10.1', '', u'', 'string', u'string', ]
686 689 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j,
687 690 [10], ['ten'], [u'ten'], {'ten': 10},(10,), None]
@@ -1,974 +1,977 b''
1 1 #!/usr/bin/env python
2 2 # encoding: utf-8
3 3 """
4 4 A lightweight Traits like module.
5 5
6 6 This is designed to provide a lightweight, simple, pure Python version of
7 7 many of the capabilities of enthought.traits. This includes:
8 8
9 9 * Validation
10 10 * Type specification with defaults
11 11 * Static and dynamic notification
12 12 * Basic predefined types
13 13 * An API that is similar to enthought.traits
14 14
15 15 We don't support:
16 16
17 17 * Delegation
18 18 * Automatic GUI generation
19 19 * A full set of trait types. Most importantly, we don't provide container
20 20 traitlets (list, dict, tuple) that can trigger notifications if their
21 21 contents change.
22 22 * API compatibility with enthought.traits
23 23
24 24 There are also some important difference in our design:
25 25
26 26 * enthought.traits does not validate default values. We do.
27 27
28 28 We choose to create this module because we need these capabilities, but
29 29 we need them to be pure Python so they work in all Python implementations,
30 30 including Jython and IronPython.
31 31
32 32 Authors:
33 33
34 34 * Brian Granger
35 35 * Enthought, Inc. Some of the code in this file comes from enthought.traits
36 36 and is licensed under the BSD license. Also, many of the ideas also come
37 37 from enthought.traits even though our implementation is very different.
38 38 """
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Copyright (C) 2008-2009 The IPython Development Team
42 42 #
43 43 # Distributed under the terms of the BSD License. The full license is in
44 44 # the file COPYING, distributed as part of this software.
45 45 #-----------------------------------------------------------------------------
46 46
47 47 #-----------------------------------------------------------------------------
48 48 # Imports
49 49 #-----------------------------------------------------------------------------
50 50
51 51
52 52 import inspect
53 53 import sys
54 54 import types
55 55 from types import (
56 56 InstanceType, ClassType, FunctionType,
57 57 ListType, TupleType
58 58 )
59 59
60 60 from IPython.utils.importstring import import_item
61 61
62 62 ClassTypes = (ClassType, type)
63 63
64 64 SequenceTypes = (ListType, TupleType)
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Basic classes
68 68 #-----------------------------------------------------------------------------
69 69
70 70
71 71 class NoDefaultSpecified ( object ): pass
72 72 NoDefaultSpecified = NoDefaultSpecified()
73 73
74 74
75 75 class Undefined ( object ): pass
76 76 Undefined = Undefined()
77 77
78 78
79 79 class TraitletError(Exception):
80 80 pass
81 81
82 82
83 83 #-----------------------------------------------------------------------------
84 84 # Utilities
85 85 #-----------------------------------------------------------------------------
86 86
87 87
88 88 def class_of ( object ):
89 89 """ Returns a string containing the class name of an object with the
90 90 correct indefinite article ('a' or 'an') preceding it (e.g., 'an Image',
91 91 'a PlotValue').
92 92 """
93 93 if isinstance( object, basestring ):
94 94 return add_article( object )
95 95
96 96 return add_article( object.__class__.__name__ )
97 97
98 98
99 99 def add_article ( name ):
100 100 """ Returns a string containing the correct indefinite article ('a' or 'an')
101 101 prefixed to the specified string.
102 102 """
103 103 if name[:1].lower() in 'aeiou':
104 104 return 'an ' + name
105 105
106 106 return 'a ' + name
107 107
108 108
109 109 def repr_type(obj):
110 110 """ Return a string representation of a value and its type for readable
111 111 error messages.
112 112 """
113 113 the_type = type(obj)
114 114 if the_type is InstanceType:
115 115 # Old-style class.
116 116 the_type = obj.__class__
117 117 msg = '%r %r' % (obj, the_type)
118 118 return msg
119 119
120 120
121 121 def parse_notifier_name(name):
122 122 """Convert the name argument to a list of names.
123 123
124 124 Examples
125 125 --------
126 126
127 127 >>> parse_notifier_name('a')
128 128 ['a']
129 129 >>> parse_notifier_name(['a','b'])
130 130 ['a', 'b']
131 131 >>> parse_notifier_name(None)
132 132 ['anytraitlet']
133 133 """
134 134 if isinstance(name, str):
135 135 return [name]
136 136 elif name is None:
137 137 return ['anytraitlet']
138 138 elif isinstance(name, (list, tuple)):
139 139 for n in name:
140 140 assert isinstance(n, str), "names must be strings"
141 141 return name
142 142
143 143
144 144 class _SimpleTest:
145 145 def __init__ ( self, value ): self.value = value
146 146 def __call__ ( self, test ):
147 147 return test == self.value
148 148 def __repr__(self):
149 149 return "<SimpleTest(%r)" % self.value
150 150 def __str__(self):
151 151 return self.__repr__()
152 152
153 153
154 154 #-----------------------------------------------------------------------------
155 155 # Base TraitletType for all traitlets
156 156 #-----------------------------------------------------------------------------
157 157
158 158
159 159 class TraitletType(object):
160 160 """A base class for all traitlet descriptors.
161 161
162 162 Notes
163 163 -----
164 164 Our implementation of traitlets is based on Python's descriptor
165 165 prototol. This class is the base class for all such descriptors. The
166 166 only magic we use is a custom metaclass for the main :class:`HasTraitlets`
167 167 class that does the following:
168 168
169 169 1. Sets the :attr:`name` attribute of every :class:`TraitletType`
170 170 instance in the class dict to the name of the attribute.
171 171 2. Sets the :attr:`this_class` attribute of every :class:`TraitletType`
172 172 instance in the class dict to the *class* that declared the traitlet.
173 173 This is used by the :class:`This` traitlet to allow subclasses to
174 174 accept superclasses for :class:`This` values.
175 175 """
176 176
177 177
178 178 metadata = {}
179 179 default_value = Undefined
180 180 info_text = 'any value'
181 181
182 182 def __init__(self, default_value=NoDefaultSpecified, **metadata):
183 183 """Create a TraitletType.
184 184 """
185 185 if default_value is not NoDefaultSpecified:
186 186 self.default_value = default_value
187 187
188 188 if len(metadata) > 0:
189 189 if len(self.metadata) > 0:
190 190 self._metadata = self.metadata.copy()
191 191 self._metadata.update(metadata)
192 192 else:
193 193 self._metadata = metadata
194 194 else:
195 195 self._metadata = self.metadata
196 196
197 197 self.init()
198 198
199 199 def init(self):
200 200 pass
201 201
202 202 def get_default_value(self):
203 203 """Create a new instance of the default value."""
204 204 dv = self.default_value
205 205 return dv
206 206
207 207 def instance_init(self, obj):
208 208 """This is called by :meth:`HasTraitlets.__new__` to finish init'ing.
209 209
210 210 Some stages of initialization must be delayed until the parent
211 211 :class:`HasTraitlets` instance has been created. This method is
212 212 called in :meth:`HasTraitlets.__new__` after the instance has been
213 213 created.
214 214
215 215 This method trigger the creation and validation of default values
216 216 and also things like the resolution of str given class names in
217 217 :class:`Type` and :class`Instance`.
218 218
219 219 Parameters
220 220 ----------
221 221 obj : :class:`HasTraitlets` instance
222 222 The parent :class:`HasTraitlets` instance that has just been
223 223 created.
224 224 """
225 225 self.set_default_value(obj)
226 226
227 227 def set_default_value(self, obj):
228 228 """Set the default value on a per instance basis.
229 229
230 230 This method is called by :meth:`instance_init` to create and
231 231 validate the default value. The creation and validation of
232 232 default values must be delayed until the parent :class:`HasTraitlets`
233 233 class has been instantiated.
234 234 """
235 235 dv = self.get_default_value()
236 236 newdv = self._validate(obj, dv)
237 237 obj._traitlet_values[self.name] = newdv
238 238
239 239 def __get__(self, obj, cls=None):
240 240 """Get the value of the traitlet by self.name for the instance.
241 241
242 242 Default values are instantiated when :meth:`HasTraitlets.__new__`
243 243 is called. Thus by the time this method gets called either the
244 244 default value or a user defined value (they called :meth:`__set__`)
245 245 is in the :class:`HasTraitlets` instance.
246 246 """
247 247 if obj is None:
248 248 return self
249 249 else:
250 250 try:
251 251 value = obj._traitlet_values[self.name]
252 252 except:
253 253 # HasTraitlets should call set_default_value to populate
254 254 # this. So this should never be reached.
255 255 raise TraitletError('Unexpected error in TraitletType: '
256 256 'default value not set properly')
257 257 else:
258 258 return value
259 259
260 260 def __set__(self, obj, value):
261 261 new_value = self._validate(obj, value)
262 262 old_value = self.__get__(obj)
263 263 if old_value != new_value:
264 264 obj._traitlet_values[self.name] = new_value
265 265 obj._notify_traitlet(self.name, old_value, new_value)
266 266
267 267 def _validate(self, obj, value):
268 268 if hasattr(self, 'validate'):
269 269 return self.validate(obj, value)
270 270 elif hasattr(self, 'is_valid_for'):
271 271 valid = self.is_valid_for(value)
272 272 if valid:
273 273 return value
274 274 else:
275 275 raise TraitletError('invalid value for type: %r' % value)
276 276 elif hasattr(self, 'value_for'):
277 277 return self.value_for(value)
278 278 else:
279 279 return value
280 280
281 281 def info(self):
282 282 return self.info_text
283 283
284 284 def error(self, obj, value):
285 285 if obj is not None:
286 286 e = "The '%s' traitlet of %s instance must be %s, but a value of %s was specified." \
287 287 % (self.name, class_of(obj),
288 288 self.info(), repr_type(value))
289 289 else:
290 290 e = "The '%s' traitlet must be %s, but a value of %r was specified." \
291 291 % (self.name, self.info(), repr_type(value))
292 292 raise TraitletError(e)
293 293
294 294 def get_metadata(self, key):
295 295 return getattr(self, '_metadata', {}).get(key, None)
296 296
297 297 def set_metadata(self, key, value):
298 298 getattr(self, '_metadata', {})[key] = value
299 299
300 300
301 301 #-----------------------------------------------------------------------------
302 302 # The HasTraitlets implementation
303 303 #-----------------------------------------------------------------------------
304 304
305 305
306 306 class MetaHasTraitlets(type):
307 307 """A metaclass for HasTraitlets.
308 308
309 309 This metaclass makes sure that any TraitletType class attributes are
310 310 instantiated and sets their name attribute.
311 311 """
312 312
313 313 def __new__(mcls, name, bases, classdict):
314 314 """Create the HasTraitlets class.
315 315
316 316 This instantiates all TraitletTypes in the class dict and sets their
317 317 :attr:`name` attribute.
318 318 """
319 319 for k,v in classdict.iteritems():
320 320 if isinstance(v, TraitletType):
321 321 v.name = k
322 322 elif inspect.isclass(v):
323 323 if issubclass(v, TraitletType):
324 324 vinst = v()
325 325 vinst.name = k
326 326 classdict[k] = vinst
327 327 return super(MetaHasTraitlets, mcls).__new__(mcls, name, bases, classdict)
328 328
329 329 def __init__(cls, name, bases, classdict):
330 330 """Finish initializing the HasTraitlets class.
331 331
332 332 This sets the :attr:`this_class` attribute of each TraitletType in the
333 333 class dict to the newly created class ``cls``.
334 334 """
335 335 for k, v in classdict.iteritems():
336 336 if isinstance(v, TraitletType):
337 337 v.this_class = cls
338 338 super(MetaHasTraitlets, cls).__init__(name, bases, classdict)
339 339
340 340 class HasTraitlets(object):
341 341
342 342 __metaclass__ = MetaHasTraitlets
343 343
344 344 def __new__(cls, *args, **kw):
345 345 inst = super(HasTraitlets, cls).__new__(cls, *args, **kw)
346 346 inst._traitlet_values = {}
347 347 inst._traitlet_notifiers = {}
348 348 # Here we tell all the TraitletType instances to set their default
349 349 # values on the instance.
350 350 for key in dir(cls):
351 351 value = getattr(cls, key)
352 352 if isinstance(value, TraitletType):
353 353 value.instance_init(inst)
354 354 return inst
355 355
356 356 # def __init__(self):
357 357 # self._traitlet_values = {}
358 358 # self._traitlet_notifiers = {}
359 359
360 360 def _notify_traitlet(self, name, old_value, new_value):
361 361
362 362 # First dynamic ones
363 363 callables = self._traitlet_notifiers.get(name,[])
364 364 more_callables = self._traitlet_notifiers.get('anytraitlet',[])
365 365 callables.extend(more_callables)
366 366
367 367 # Now static ones
368 368 try:
369 369 cb = getattr(self, '_%s_changed' % name)
370 370 except:
371 371 pass
372 372 else:
373 373 callables.append(cb)
374 374
375 375 # Call them all now
376 376 for c in callables:
377 377 # Traits catches and logs errors here. I allow them to raise
378 378 if callable(c):
379 379 argspec = inspect.getargspec(c)
380 380 nargs = len(argspec[0])
381 381 # Bound methods have an additional 'self' argument
382 382 # I don't know how to treat unbound methods, but they
383 383 # can't really be used for callbacks.
384 384 if isinstance(c, types.MethodType):
385 385 offset = -1
386 386 else:
387 387 offset = 0
388 388 if nargs + offset == 0:
389 389 c()
390 390 elif nargs + offset == 1:
391 391 c(name)
392 392 elif nargs + offset == 2:
393 393 c(name, new_value)
394 394 elif nargs + offset == 3:
395 395 c(name, old_value, new_value)
396 396 else:
397 397 raise TraitletError('a traitlet changed callback '
398 398 'must have 0-3 arguments.')
399 399 else:
400 400 raise TraitletError('a traitlet changed callback '
401 401 'must be callable.')
402 402
403 403
404 404 def _add_notifiers(self, handler, name):
405 405 if not self._traitlet_notifiers.has_key(name):
406 406 nlist = []
407 407 self._traitlet_notifiers[name] = nlist
408 408 else:
409 409 nlist = self._traitlet_notifiers[name]
410 410 if handler not in nlist:
411 411 nlist.append(handler)
412 412
413 413 def _remove_notifiers(self, handler, name):
414 414 if self._traitlet_notifiers.has_key(name):
415 415 nlist = self._traitlet_notifiers[name]
416 416 try:
417 417 index = nlist.index(handler)
418 418 except ValueError:
419 419 pass
420 420 else:
421 421 del nlist[index]
422 422
423 423 def on_traitlet_change(self, handler, name=None, remove=False):
424 424 """Setup a handler to be called when a traitlet changes.
425 425
426 426 This is used to setup dynamic notifications of traitlet changes.
427 427
428 428 Static handlers can be created by creating methods on a HasTraitlets
429 429 subclass with the naming convention '_[traitletname]_changed'. Thus,
430 430 to create static handler for the traitlet 'a', create the method
431 431 _a_changed(self, name, old, new) (fewer arguments can be used, see
432 432 below).
433 433
434 434 Parameters
435 435 ----------
436 436 handler : callable
437 437 A callable that is called when a traitlet changes. Its
438 438 signature can be handler(), handler(name), handler(name, new)
439 439 or handler(name, old, new).
440 440 name : list, str, None
441 441 If None, the handler will apply to all traitlets. If a list
442 442 of str, handler will apply to all names in the list. If a
443 443 str, the handler will apply just to that name.
444 444 remove : bool
445 445 If False (the default), then install the handler. If True
446 446 then unintall it.
447 447 """
448 448 if remove:
449 449 names = parse_notifier_name(name)
450 450 for n in names:
451 451 self._remove_notifiers(handler, n)
452 452 else:
453 453 names = parse_notifier_name(name)
454 454 for n in names:
455 455 self._add_notifiers(handler, n)
456 456
457 457 def traitlet_names(self, **metadata):
458 458 """Get a list of all the names of this classes traitlets."""
459 459 return self.traitlets(**metadata).keys()
460 460
461 def traitlets(self, *args, **metadata):
461 def traitlets(self, **metadata):
462 462 """Get a list of all the traitlets of this class.
463 463
464 464 The TraitletTypes returned don't know anything about the values
465 465 that the various HasTraitlet's instances are holding.
466
467 This follows the same algorithm as traits does and does not allow
468 for any simple way of specifying merely that a metadata name
469 exists, but has any value. This is because get_metadata returns
470 None if a metadata key doesn't exist.
466 471 """
467 472 traitlets = dict([memb for memb in inspect.getmembers(self.__class__) if \
468 473 isinstance(memb[1], TraitletType)])
469 if len(metadata) == 0 and len(args) == 0:
470 return traitlets
471 474
472 for meta_name in args:
473 metadata[meta_name] = lambda _: True
475 if len(metadata) == 0:
476 return traitlets
474 477
475 478 for meta_name, meta_eval in metadata.items():
476 479 if type(meta_eval) is not FunctionType:
477 480 metadata[meta_name] = _SimpleTest(meta_eval)
478 481
479 482 result = {}
480 483 for name, traitlet in traitlets.items():
481 484 for meta_name, meta_eval in metadata.items():
482 485 if not meta_eval(traitlet.get_metadata(meta_name)):
483 486 break
484 487 else:
485 488 result[name] = traitlet
486 489
487 490 return result
488 491
489 492 def traitlet_metadata(self, traitletname, key):
490 493 """Get metadata values for traitlet by key."""
491 494 try:
492 495 traitlet = getattr(self.__class__, traitletname)
493 496 except AttributeError:
494 497 raise TraitletError("Class %s does not have a traitlet named %s" %
495 498 (self.__class__.__name__, traitletname))
496 499 else:
497 500 return traitlet.get_metadata(key)
498 501
499 502 #-----------------------------------------------------------------------------
500 503 # Actual TraitletTypes implementations/subclasses
501 504 #-----------------------------------------------------------------------------
502 505
503 506 #-----------------------------------------------------------------------------
504 507 # TraitletTypes subclasses for handling classes and instances of classes
505 508 #-----------------------------------------------------------------------------
506 509
507 510
508 511 class ClassBasedTraitletType(TraitletType):
509 512 """A traitlet with error reporting for Type, Instance and This."""
510 513
511 514 def error(self, obj, value):
512 515 kind = type(value)
513 516 if kind is InstanceType:
514 517 msg = 'class %s' % value.__class__.__name__
515 518 else:
516 519 msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) )
517 520
518 521 super(ClassBasedTraitletType, self).error(obj, msg)
519 522
520 523
521 524 class Type(ClassBasedTraitletType):
522 525 """A traitlet whose value must be a subclass of a specified class."""
523 526
524 527 def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ):
525 528 """Construct a Type traitlet
526 529
527 530 A Type traitlet specifies that its values must be subclasses of
528 531 a particular class.
529 532
530 533 If only ``default_value`` is given, it is used for the ``klass`` as
531 534 well.
532 535
533 536 Parameters
534 537 ----------
535 538 default_value : class, str or None
536 539 The default value must be a subclass of klass. If an str,
537 540 the str must be a fully specified class name, like 'foo.bar.Bah'.
538 541 The string is resolved into real class, when the parent
539 542 :class:`HasTraitlets` class is instantiated.
540 543 klass : class, str, None
541 544 Values of this traitlet must be a subclass of klass. The klass
542 545 may be specified in a string like: 'foo.bar.MyClass'.
543 546 The string is resolved into real class, when the parent
544 547 :class:`HasTraitlets` class is instantiated.
545 548 allow_none : boolean
546 549 Indicates whether None is allowed as an assignable value. Even if
547 550 ``False``, the default value may be ``None``.
548 551 """
549 552 if default_value is None:
550 553 if klass is None:
551 554 klass = object
552 555 elif klass is None:
553 556 klass = default_value
554 557
555 558 if not (inspect.isclass(klass) or isinstance(klass, basestring)):
556 559 raise TraitletError("A Type traitlet must specify a class.")
557 560
558 561 self.klass = klass
559 562 self._allow_none = allow_none
560 563
561 564 super(Type, self).__init__(default_value, **metadata)
562 565
563 566 def validate(self, obj, value):
564 567 """Validates that the value is a valid object instance."""
565 568 try:
566 569 if issubclass(value, self.klass):
567 570 return value
568 571 except:
569 572 if (value is None) and (self._allow_none):
570 573 return value
571 574
572 575 self.error(obj, value)
573 576
574 577 def info(self):
575 578 """ Returns a description of the trait."""
576 579 if isinstance(self.klass, basestring):
577 580 klass = self.klass
578 581 else:
579 582 klass = self.klass.__name__
580 583 result = 'a subclass of ' + klass
581 584 if self._allow_none:
582 585 return result + ' or None'
583 586 return result
584 587
585 588 def instance_init(self, obj):
586 589 self._resolve_classes()
587 590 super(Type, self).instance_init(obj)
588 591
589 592 def _resolve_classes(self):
590 593 if isinstance(self.klass, basestring):
591 594 self.klass = import_item(self.klass)
592 595 if isinstance(self.default_value, basestring):
593 596 self.default_value = import_item(self.default_value)
594 597
595 598 def get_default_value(self):
596 599 return self.default_value
597 600
598 601
599 602 class DefaultValueGenerator(object):
600 603 """A class for generating new default value instances."""
601 604
602 605 def __init__(self, *args, **kw):
603 606 self.args = args
604 607 self.kw = kw
605 608
606 609 def generate(self, klass):
607 610 return klass(*self.args, **self.kw)
608 611
609 612
610 613 class Instance(ClassBasedTraitletType):
611 614 """A trait whose value must be an instance of a specified class.
612 615
613 616 The value can also be an instance of a subclass of the specified class.
614 617 """
615 618
616 619 def __init__(self, klass=None, args=None, kw=None,
617 620 allow_none=True, **metadata ):
618 621 """Construct an Instance traitlet.
619 622
620 623 This traitlet allows values that are instances of a particular
621 624 class or its sublclasses. Our implementation is quite different
622 625 from that of enthough.traits as we don't allow instances to be used
623 626 for klass and we handle the ``args`` and ``kw`` arguments differently.
624 627
625 628 Parameters
626 629 ----------
627 630 klass : class, str
628 631 The class that forms the basis for the traitlet. Class names
629 632 can also be specified as strings, like 'foo.bar.Bar'.
630 633 args : tuple
631 634 Positional arguments for generating the default value.
632 635 kw : dict
633 636 Keyword arguments for generating the default value.
634 637 allow_none : bool
635 638 Indicates whether None is allowed as a value.
636 639
637 640 Default Value
638 641 -------------
639 642 If both ``args`` and ``kw`` are None, then the default value is None.
640 643 If ``args`` is a tuple and ``kw`` is a dict, then the default is
641 644 created as ``klass(*args, **kw)``. If either ``args`` or ``kw`` is
642 645 not (but not both), None is replace by ``()`` or ``{}``.
643 646 """
644 647
645 648 self._allow_none = allow_none
646 649
647 650 if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, basestring))):
648 651 raise TraitletError('The klass argument must be a class'
649 652 ' you gave: %r' % klass)
650 653 self.klass = klass
651 654
652 655 # self.klass is a class, so handle default_value
653 656 if args is None and kw is None:
654 657 default_value = None
655 658 else:
656 659 if args is None:
657 660 # kw is not None
658 661 args = ()
659 662 elif kw is None:
660 663 # args is not None
661 664 kw = {}
662 665
663 666 if not isinstance(kw, dict):
664 667 raise TraitletError("The 'kw' argument must be a dict or None.")
665 668 if not isinstance(args, tuple):
666 669 raise TraitletError("The 'args' argument must be a tuple or None.")
667 670
668 671 default_value = DefaultValueGenerator(*args, **kw)
669 672
670 673 super(Instance, self).__init__(default_value, **metadata)
671 674
672 675 def validate(self, obj, value):
673 676 if value is None:
674 677 if self._allow_none:
675 678 return value
676 679 self.error(obj, value)
677 680
678 681 if isinstance(value, self.klass):
679 682 return value
680 683 else:
681 684 self.error(obj, value)
682 685
683 686 def info(self):
684 687 if isinstance(self.klass, basestring):
685 688 klass = self.klass
686 689 else:
687 690 klass = self.klass.__name__
688 691 result = class_of(klass)
689 692 if self._allow_none:
690 693 return result + ' or None'
691 694
692 695 return result
693 696
694 697 def instance_init(self, obj):
695 698 self._resolve_classes()
696 699 super(Instance, self).instance_init(obj)
697 700
698 701 def _resolve_classes(self):
699 702 if isinstance(self.klass, basestring):
700 703 self.klass = import_item(self.klass)
701 704
702 705 def get_default_value(self):
703 706 """Instantiate a default value instance.
704 707
705 708 This is called when the containing HasTraitlets classes'
706 709 :meth:`__new__` method is called to ensure that a unique instance
707 710 is created for each HasTraitlets instance.
708 711 """
709 712 dv = self.default_value
710 713 if isinstance(dv, DefaultValueGenerator):
711 714 return dv.generate(self.klass)
712 715 else:
713 716 return dv
714 717
715 718
716 719 class This(ClassBasedTraitletType):
717 720 """A traitlet for instances of the class containing this trait.
718 721
719 722 Because how how and when class bodies are executed, the ``This``
720 723 traitlet can only have a default value of None. This, and because we
721 724 always validate default values, ``allow_none`` is *always* true.
722 725 """
723 726
724 727 info_text = 'an instance of the same type as the receiver or None'
725 728
726 729 def __init__(self, **metadata):
727 730 super(This, self).__init__(None, **metadata)
728 731
729 732 def validate(self, obj, value):
730 733 # What if value is a superclass of obj.__class__? This is
731 734 # complicated if it was the superclass that defined the This
732 735 # traitlet.
733 736 if isinstance(value, self.this_class) or (value is None):
734 737 return value
735 738 else:
736 739 self.error(obj, value)
737 740
738 741
739 742 #-----------------------------------------------------------------------------
740 743 # Basic TraitletTypes implementations/subclasses
741 744 #-----------------------------------------------------------------------------
742 745
743 746
744 747 class Any(TraitletType):
745 748 default_value = None
746 749 info_text = 'any value'
747 750
748 751
749 752 class Int(TraitletType):
750 753 """A integer traitlet."""
751 754
752 755 evaluate = int
753 756 default_value = 0
754 757 info_text = 'an integer'
755 758
756 759 def validate(self, obj, value):
757 760 if isinstance(value, int):
758 761 return value
759 762 self.error(obj, value)
760 763
761 764 class CInt(Int):
762 765 """A casting version of the int traitlet."""
763 766
764 767 def validate(self, obj, value):
765 768 try:
766 769 return int(value)
767 770 except:
768 771 self.error(obj, value)
769 772
770 773
771 774 class Long(TraitletType):
772 775 """A long integer traitlet."""
773 776
774 777 evaluate = long
775 778 default_value = 0L
776 779 info_text = 'a long'
777 780
778 781 def validate(self, obj, value):
779 782 if isinstance(value, long):
780 783 return value
781 784 if isinstance(value, int):
782 785 return long(value)
783 786 self.error(obj, value)
784 787
785 788
786 789 class CLong(Long):
787 790 """A casting version of the long integer traitlet."""
788 791
789 792 def validate(self, obj, value):
790 793 try:
791 794 return long(value)
792 795 except:
793 796 self.error(obj, value)
794 797
795 798
796 799 class Float(TraitletType):
797 800 """A float traitlet."""
798 801
799 802 evaluate = float
800 803 default_value = 0.0
801 804 info_text = 'a float'
802 805
803 806 def validate(self, obj, value):
804 807 if isinstance(value, float):
805 808 return value
806 809 if isinstance(value, int):
807 810 return float(value)
808 811 self.error(obj, value)
809 812
810 813
811 814 class CFloat(Float):
812 815 """A casting version of the float traitlet."""
813 816
814 817 def validate(self, obj, value):
815 818 try:
816 819 return float(value)
817 820 except:
818 821 self.error(obj, value)
819 822
820 823 class Complex(TraitletType):
821 824 """A traitlet for complex numbers."""
822 825
823 826 evaluate = complex
824 827 default_value = 0.0 + 0.0j
825 828 info_text = 'a complex number'
826 829
827 830 def validate(self, obj, value):
828 831 if isinstance(value, complex):
829 832 return value
830 833 if isinstance(value, (float, int)):
831 834 return complex(value)
832 835 self.error(obj, value)
833 836
834 837
835 838 class CComplex(Complex):
836 839 """A casting version of the complex number traitlet."""
837 840
838 841 def validate (self, obj, value):
839 842 try:
840 843 return complex(value)
841 844 except:
842 845 self.error(obj, value)
843 846
844 847
845 848 class Str(TraitletType):
846 849 """A traitlet for strings."""
847 850
848 851 evaluate = lambda x: x
849 852 default_value = ''
850 853 info_text = 'a string'
851 854
852 855 def validate(self, obj, value):
853 856 if isinstance(value, str):
854 857 return value
855 858 self.error(obj, value)
856 859
857 860
858 861 class CStr(Str):
859 862 """A casting version of the string traitlet."""
860 863
861 864 def validate(self, obj, value):
862 865 try:
863 866 return str(value)
864 867 except:
865 868 try:
866 869 return unicode(value)
867 870 except:
868 871 self.error(obj, value)
869 872
870 873
871 874 class Unicode(TraitletType):
872 875 """A traitlet for unicode strings."""
873 876
874 877 evaluate = unicode
875 878 default_value = u''
876 879 info_text = 'a unicode string'
877 880
878 881 def validate(self, obj, value):
879 882 if isinstance(value, unicode):
880 883 return value
881 884 if isinstance(value, str):
882 885 return unicode(value)
883 886 self.error(obj, value)
884 887
885 888
886 889 class CUnicode(Unicode):
887 890 """A casting version of the unicode traitlet."""
888 891
889 892 def validate(self, obj, value):
890 893 try:
891 894 return unicode(value)
892 895 except:
893 896 self.error(obj, value)
894 897
895 898
896 899 class Bool(TraitletType):
897 900 """A boolean (True, False) traitlet."""
898 901 evaluate = bool
899 902 default_value = False
900 903 info_text = 'a boolean'
901 904
902 905 def validate(self, obj, value):
903 906 if isinstance(value, bool):
904 907 return value
905 908 self.error(obj, value)
906 909
907 910
908 911 class CBool(Bool):
909 912 """A casting version of the boolean traitlet."""
910 913
911 914 def validate(self, obj, value):
912 915 try:
913 916 return bool(value)
914 917 except:
915 918 self.error(obj, value)
916 919
917 920
918 921 class Enum(TraitletType):
919 922 """An enum that whose value must be in a given sequence."""
920 923
921 924 def __init__(self, values, default_value=None, allow_none=True, **metadata):
922 925 self.values = values
923 926 self._allow_none = allow_none
924 927 super(Enum, self).__init__(default_value, **metadata)
925 928
926 929 def validate(self, obj, value):
927 930 if value is None:
928 931 if self._allow_none:
929 932 return value
930 933
931 934 if value in self.values:
932 935 return value
933 936 self.error(obj, value)
934 937
935 938 def info(self):
936 939 """ Returns a description of the trait."""
937 940 result = 'any of ' + repr(self.values)
938 941 if self._allow_none:
939 942 return result + ' or None'
940 943 return result
941 944
942 945 class CaselessStrEnum(Enum):
943 946 """An enum of strings that are caseless in validate."""
944 947
945 948 def validate(self, obj, value):
946 949 if value is None:
947 950 if self._allow_none:
948 951 return value
949 952
950 953 if not isinstance(value, str):
951 954 self.error(obj, value)
952 955
953 956 for v in self.values:
954 957 if v.lower() == value.lower():
955 958 return v
956 959 self.error(obj, value)
957 960
958 961
959 962 class List(Instance):
960 963 """An instance of a Python list."""
961 964
962 965 def __init__(self, default_value=None, allow_none=True, **metadata):
963 966 """Create a list traitlet type from a list or tuple.
964 967
965 968 The default value is created by doing ``list(default_value)``,
966 969 which creates a copy of the ``default_value``.
967 970 """
968 971 if default_value is None:
969 972 args = ((),)
970 973 elif isinstance(default_value, SequenceTypes):
971 974 args = (default_value,)
972 975
973 976 super(List,self).__init__(klass=list, args=args,
974 977 allow_none=allow_none, **metadata)
General Comments 0
You need to be logged in to leave comments. Login now