##// 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'
22
23 InteractiveShell.banner2 = "This is for extra banner text"
28
24
29 EDITOR = 0
25 InteractiveShell.cache_size = 1000
30
26
31 LOG = True
27 InteractiveShell.colors = 'LightBG'
32
28
33 LOGFILE = ''
29 InteractiveShell.color_info = True
34
30
35 BANNER = True
31 InteractiveShell.confirm_exit = True
36
32
37 MESSAGES = True
33 InteractiveShell.deep_reload = False
38
34
39 PDB = False
35 InteractiveShell.display_banner = True
40
36
41 PPRINT = True
37 InteractiveShell.editor = 'nano'
42
38
43 PROMPT_IN1 = 'In [\#]: '
39 InteractiveShell.logstart = True
44
40
45 PROMPT_IN2 = ' .\D.: '
41 InteractiveShell.logfile = 'ipython_log.py'
46
42
47 PROMPT_OUT = 'Out[\#]: '
43 InteractiveShell.logplay = 'mylog.py'
48
44
49 PROMPTS_PAD_LEFT = True
45 InteractiveShell.object_info_string_level = 0
50
46
51 QUICK = False
47 InteractiveShell.pager = 'less'
52
48
53 SCREEN_LENGTH = 0
49 InteractiveShell.pdb = False
54
50
55 SEPARATE_IN = '\n'
51 InteractiveShell.pprint = True
56 SEPARATE_OUT = ''
57 SEPARATE_OUT2 = ''
58 NOSEP = False
59
52
60 WILDCARDS_CASE_SENSITIVE = True
53 InteractiveShell.prompt_in1 = 'In [\#]: '
54 InteractiveShell.prompt_in2 = ' .\D.: '
55 InteractiveShell.prompt_out = 'Out[\#]: '
56 InteractiveShell.prompts_pad_left = True
61
57
62 OBJECT_INFO_STRING_LEVEL = 0
58 InteractiveShell.quiet = False
63
59
64 XMODE = 'Context'
60 # Readline
61 InteractiveShell.readline_use = False
65
62
66 MULTI_LINE_SPECIALS = True
63 InteractiveShell.readline_parse_and_bind = [
67
68 SYSTEM_HEADER = "IPython system call: "
69
70 SYSTEM_VERBOSE = True
71
72 #-----------------------------------------------------------------------------
73 # Readline
74 #-----------------------------------------------------------------------------
75
76 READLINE = True
77
78 READLINE_PARSE_AND_BIND = [
79 'tab: complete',
64 'tab: complete',
80 '"\C-l": possible-completions',
65 '"\C-l": possible-completions',
81 'set show-all-if-ambiguous on',
66 'set show-all-if-ambiguous on',
82 '"\C-o": tab-insert',
67 '"\C-o": tab-insert',
83 '"\M-i": " "',
68 '"\M-i": " "',
84 '"\M-o": "\d\d\d\d"',
69 '"\M-o": "\d\d\d\d"',
85 '"\M-I": "\d\d\d\d"',
70 '"\M-I": "\d\d\d\d"',
86 '"\C-r": reverse-search-history',
71 '"\C-r": reverse-search-history',
87 '"\C-s": forward-search-history',
72 '"\C-s": forward-search-history',
88 '"\C-p": history-search-backward',
73 '"\C-p": history-search-backward',
89 '"\C-n": history-search-forward',
74 '"\C-n": history-search-forward',
90 '"\e[A": history-search-backward',
75 '"\e[A": history-search-backward',
91 '"\e[B": history-search-forward',
76 '"\e[B": history-search-forward',
92 '"\C-k": kill-line',
77 '"\C-k": kill-line',
93 '"\C-u": unix-line-discard',
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 = [
104 PrefilterManager.multi_line_specials = True
107 'import numpy as np',
108 'import sympy',
109 'a = 10'
110 ]
111
112 EXECFILE = []
113
105
114 #-----------------------------------------------------------------------------
106 #-----------------------------------------------------------------------------
115 # Alias
107 # AliasManager options
116 #-----------------------------------------------------------------------------
108 #-----------------------------------------------------------------------------
117
109
118 ALIAS = [
110 # Do this to enable all defaults
119 ('myls', 'ls -la')
111 # AliasManager.default_aliases = []
112
113 AliasManger.user_aliases = [
114 ('foo', 'echo Hi')
120 ] No newline at end of file
115 ]
@@ -1,203 +1,305 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
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 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2009 The IPython Development Team
8 #
12 #
9 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
12
16
13 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
14 # Imports
18 # Imports
15 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
16
20
21 import __builtin__
17 import os
22 import os
18 import sys
23 import sys
19
24
20 from IPython.external import argparse
25 from IPython.external import argparse
21 from IPython.utils.ipstruct import Struct
22 from IPython.utils.genutils import filefind
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):
30 pass
34 pass
31
35
32
36
37 class ConfigLoaderError(ConfigError):
38 pass
39
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 class ConfigLoader(object):
153 class ConfigLoader(object):
34 """A object for loading configurations from just about anywhere.
154 """A object for loading configurations from just about anywhere.
35
155
36 The resulting configuration is packaged as a :class:`Struct`.
156 The resulting configuration is packaged as a :class:`Struct`.
37
157
38 Notes
158 Notes
39 -----
159 -----
40 A :class:`ConfigLoader` does one thing: load a config from a source
160 A :class:`ConfigLoader` does one thing: load a config from a source
41 (file, command line arguments) and returns the data as a :class:`Struct`.
161 (file, command line arguments) and returns the data as a :class:`Struct`.
42 There are lots of things that :class:`ConfigLoader` does not do. It does
162 There are lots of things that :class:`ConfigLoader` does not do. It does
43 not implement complex logic for finding config files. It does not handle
163 not implement complex logic for finding config files. It does not handle
44 default values or merge multiple configs. These things need to be
164 default values or merge multiple configs. These things need to be
45 handled elsewhere.
165 handled elsewhere.
46 """
166 """
47
167
48 def __init__(self):
168 def __init__(self):
49 """A base class for config loaders.
169 """A base class for config loaders.
50
170
51 Examples
171 Examples
52 --------
172 --------
53
173
54 >>> cl = ConfigLoader()
174 >>> cl = ConfigLoader()
55 >>> config = cl.load_config()
175 >>> config = cl.load_config()
56 >>> config
176 >>> config
57 {}
177 {}
58 """
178 """
59 self.clear()
179 self.clear()
60
180
61 def clear(self):
181 def clear(self):
62 self.config = Struct()
182 self.config = Config()
63
183
64 def load_config(self):
184 def load_config(self):
65 """Load a config from somewhere, return a Struct.
185 """Load a config from somewhere, return a Struct.
66
186
67 Usually, this will cause self.config to be set and then returned.
187 Usually, this will cause self.config to be set and then returned.
68 """
188 """
69 return self.config
189 return self.config
70
190
71
191
72 class FileConfigLoader(ConfigLoader):
192 class FileConfigLoader(ConfigLoader):
73 """A base class for file based configurations.
193 """A base class for file based configurations.
74
194
75 As we add more file based config loaders, the common logic should go
195 As we add more file based config loaders, the common logic should go
76 here.
196 here.
77 """
197 """
78 pass
198 pass
79
199
80
200
81 class PyFileConfigLoader(FileConfigLoader):
201 class PyFileConfigLoader(FileConfigLoader):
82 """A config loader for pure python files.
202 """A config loader for pure python files.
83
203
84 This calls execfile on a plain python file and looks for attributes
204 This calls execfile on a plain python file and looks for attributes
85 that are all caps. These attribute are added to the config Struct.
205 that are all caps. These attribute are added to the config Struct.
86 """
206 """
87
207
88 def __init__(self, filename, path=None):
208 def __init__(self, filename, path=None):
89 """Build a config loader for a filename and path.
209 """Build a config loader for a filename and path.
90
210
91 Parameters
211 Parameters
92 ----------
212 ----------
93 filename : str
213 filename : str
94 The file name of the config file.
214 The file name of the config file.
95 path : str, list, tuple
215 path : str, list, tuple
96 The path to search for the config file on, or a sequence of
216 The path to search for the config file on, or a sequence of
97 paths to try in order.
217 paths to try in order.
98 """
218 """
99 super(PyFileConfigLoader, self).__init__()
219 super(PyFileConfigLoader, self).__init__()
100 self.filename = filename
220 self.filename = filename
101 self.path = path
221 self.path = path
102 self.full_filename = ''
222 self.full_filename = ''
103 self.data = None
223 self.data = None
104
224
105 def load_config(self):
225 def load_config(self):
106 """Load the config from a file and return it as a Struct."""
226 """Load the config from a file and return it as a Struct."""
107 self._find_file()
227 self._find_file()
108 self._read_file_as_dict()
228 self._read_file_as_dict()
109 self._convert_to_struct()
229 self._convert_to_config()
110 return self.config
230 return self.config
111
231
112 def _find_file(self):
232 def _find_file(self):
113 """Try to find the file by searching the paths."""
233 """Try to find the file by searching the paths."""
114 self.full_filename = filefind(self.filename, self.path)
234 self.full_filename = filefind(self.filename, self.path)
115
235
116 def _read_file_as_dict(self):
236 def _read_file_as_dict(self):
117 self.data = {}
237 execfile(self.full_filename, self.config)
118 execfile(self.full_filename, self.data)
119
238
120 def _convert_to_struct(self):
239 def _convert_to_config(self):
121 if self.data is None:
240 if self.data is None:
122 ConfigLoaderError('self.data does not exist')
241 ConfigLoaderError('self.data does not exist')
123 for k, v in self.data.iteritems():
242 del self.config['__builtins__']
124 if k == k.upper():
125 self.config[k] = v
126
243
127
244
128 class CommandLineConfigLoader(ConfigLoader):
245 class CommandLineConfigLoader(ConfigLoader):
129 """A config loader for command line arguments.
246 """A config loader for command line arguments.
130
247
131 As we add more command line based loaders, the common logic should go
248 As we add more command line based loaders, the common logic should go
132 here.
249 here.
133 """
250 """
134
251
135
252
136 class NoDefault(object): pass
253 class NoConfigDefault(object): pass
137 NoDefault = NoDefault()
254 NoConfigDefault = NoConfigDefault()
138
255
139 class ArgParseConfigLoader(CommandLineConfigLoader):
256 class ArgParseConfigLoader(CommandLineConfigLoader):
140
257
141 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
258 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
142 arguments = ()
259 arguments = ()
143
260
144 def __init__(self, *args, **kw):
261 def __init__(self, *args, **kw):
145 """Create a config loader for use with argparse.
262 """Create a config loader for use with argparse.
146
263
147 The args and kwargs arguments here are passed onto the constructor
264 The args and kwargs arguments here are passed onto the constructor
148 of :class:`argparse.ArgumentParser`.
265 of :class:`argparse.ArgumentParser`.
149 """
266 """
150 super(CommandLineConfigLoader, self).__init__()
267 super(CommandLineConfigLoader, self).__init__()
151 self.args = args
268 self.args = args
152 self.kw = kw
269 self.kw = kw
153
270
154 def load_config(self, args=None):
271 def load_config(self, args=None):
155 """Parse command line arguments and return as a Struct."""
272 """Parse command line arguments and return as a Struct."""
156 self._create_parser()
273 self._create_parser()
157 self._parse_args(args)
274 self._parse_args(args)
158 self._convert_to_struct()
275 self._convert_to_config()
159 return self.config
276 return self.config
160
277
161 def _create_parser(self):
278 def _create_parser(self):
162 self.parser = argparse.ArgumentParser(*self.args, **self.kw)
279 self.parser = argparse.ArgumentParser(*self.args, **self.kw)
163 self._add_arguments()
280 self._add_arguments()
164 self._add_other_arguments()
281 self._add_other_arguments()
165
282
166 def _add_other_arguments(self):
283 def _add_other_arguments(self):
167 pass
284 pass
168
285
169 def _add_arguments(self):
286 def _add_arguments(self):
170 for argument in self.arguments:
287 for argument in self.arguments:
171 if not argument[1].has_key('default'):
288 if not argument[1].has_key('default'):
172 argument[1]['default'] = NoDefault
289 argument[1]['default'] = NoConfigDefault
173 self.parser.add_argument(*argument[0],**argument[1])
290 self.parser.add_argument(*argument[0],**argument[1])
174
291
175 def _parse_args(self, args=None):
292 def _parse_args(self, args=None):
176 """self.parser->self.parsed_data"""
293 """self.parser->self.parsed_data"""
177 if args is None:
294 if args is None:
178 self.parsed_data = self.parser.parse_args()
295 self.parsed_data = self.parser.parse_args()
179 else:
296 else:
180 self.parsed_data = self.parser.parse_args(args)
297 self.parsed_data = self.parser.parse_args(args)
181
298
182 def _convert_to_struct(self):
299 def _convert_to_config(self):
183 """self.parsed_data->self.config"""
300 """self.parsed_data->self.config"""
184 self.config = Struct()
185 for k, v in vars(self.parsed_data).items():
301 for k, v in vars(self.parsed_data).items():
186 if v is not NoDefault:
302 if v is not NoConfigDefault:
187 setattr(self.config, k, v)
303 exec_str = 'self.config.' + k + '= v'
188
304 exec exec_str in locals(), globals()
189 class IPythonArgParseConfigLoader(ArgParseConfigLoader):
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 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Tests for IPython.config.loader
4 Tests for IPython.config.loader
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez (design help)
9 * Fernando Perez (design help)
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import os
23 import os
24 from tempfile import mkstemp
24 from tempfile import mkstemp
25 from unittest import TestCase
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 # Actual tests
35 # Actual tests
31 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
32
37
33
38
34 pyfile = """
39 pyfile = """
35 A = 10
40 a = 10
36 B = range(10)
41 b = 20
37 C = True
42 Foo.Bar.value = 10
38 D = 'hi there'
43 Foo.Bam.value = range(10)
44 D.C.value = 'hi there'
39 """
45 """
40
46
41 class TestPyFileCL(TestCase):
47 class TestPyFileCL(TestCase):
42
48
43 def test_basic(self):
49 def test_basic(self):
44 fd, fname = mkstemp()
50 fd, fname = mkstemp()
45 f = os.fdopen(fd, 'w')
51 f = os.fdopen(fd, 'w')
46 f.write(pyfile)
52 f.write(pyfile)
47 f.close()
53 f.close()
48 # Unlink the file
54 # Unlink the file
49 cl = PyFileConfigLoader(fname)
55 cl = PyFileConfigLoader(fname)
50 config = cl.load_config()
56 config = cl.load_config()
51 self.assertEquals(config.A, 10)
57 self.assertEquals(config.a, 10)
52 self.assertEquals(config.B, range(10))
58 self.assertEquals(config.b, 20)
53 self.assertEquals(config.C, True)
59 self.assertEquals(config.Foo.Bar.value, 10)
54 self.assertEquals(config.D, 'hi there')
60 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.D.C.value, 'hi there')
55
62
56
63
57 class TestArgParseCL(TestCase):
64 class TestArgParseCL(TestCase):
58
65
59 def test_basic(self):
66 def test_basic(self):
60
67
61 class MyLoader(ArgParseConfigLoader):
68 class MyLoader(ArgParseConfigLoader):
62 arguments = (
69 arguments = (
63 (('-f','--foo'), dict(dest='FOO', type=str)),
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
64 (('-b',), dict(dest='BAR', type=int)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),
65 (('-n',), dict(dest='N', action='store_true')),
72 (('-n',), dict(dest='n', action='store_true')),
66 (('BAM',), dict(type=str))
73 (('Global.bam',), dict(type=str))
67 )
74 )
68
75
69 cl = MyLoader()
76 cl = MyLoader()
70 config = cl.load_config('-f hi -b 10 -n wow'.split())
77 config = cl.load_config('-f hi -b 10 -n wow'.split())
71 self.assertEquals(config.FOO, 'hi')
78 self.assertEquals(config.Global.foo, 'hi')
72 self.assertEquals(config.BAR, 10)
79 self.assertEquals(config.MyClass.bar, 10)
73 self.assertEquals(config.N, True)
80 self.assertEquals(config.n, True)
74 self.assertEquals(config.BAM, 'wow')
81 self.assertEquals(config.Global.bam, 'wow')
75
82
76 def test_add_arguments(self):
83 def test_add_arguments(self):
77
84
78 class MyLoader(ArgParseConfigLoader):
85 class MyLoader(ArgParseConfigLoader):
79 def _add_arguments(self):
86 def _add_arguments(self):
80 subparsers = self.parser.add_subparsers(dest='subparser_name')
87 subparsers = self.parser.add_subparsers(dest='subparser_name')
81 subparser1 = subparsers.add_parser('1')
88 subparser1 = subparsers.add_parser('1')
82 subparser1.add_argument('-x')
89 subparser1.add_argument('-x',dest='Global.x')
83 subparser2 = subparsers.add_parser('2')
90 subparser2 = subparsers.add_parser('2')
84 subparser2.add_argument('y')
91 subparser2.add_argument('y')
85
92
86 cl = MyLoader()
93 cl = MyLoader()
87 config = cl.load_config('2 frobble'.split())
94 config = cl.load_config('2 frobble'.split())
88 self.assertEquals(config.subparser_name, '2')
95 self.assertEquals(config.subparser_name, '2')
89 self.assertEquals(config.y, 'frobble')
96 self.assertEquals(config.y, 'frobble')
90 config = cl.load_config('1 -x frobble'.split())
97 config = cl.load_config('1 -x frobble'.split())
91 self.assertEquals(config.subparser_name, '1')
98 self.assertEquals(config.subparser_name, '1')
92 self.assertEquals(config.x, 'frobble')
99 self.assertEquals(config.Global.x, 'frobble')
93
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 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 IPython's alias component
4 IPython's alias component
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import __builtin__
22 import __builtin__
23 import keyword
23 import keyword
24 import os
24 import os
25 import re
25 import re
26 import sys
26 import sys
27
27
28 from IPython.core.component import Component
28 from IPython.core.component import Component
29 from IPython.core.splitinput import split_user_input
29 from IPython.core.splitinput import split_user_input
30
30
31 from IPython.utils.traitlets import CBool, List, Instance
31 from IPython.utils.traitlets import CBool, List, Instance
32 from IPython.utils.genutils import error
32 from IPython.utils.genutils import error
33 from IPython.utils.autoattr import auto_attr
33 from IPython.utils.autoattr import auto_attr
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Utilities
36 # Utilities
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38
38
39 # This is used as the pattern for calls to split_user_input.
39 # This is used as the pattern for calls to split_user_input.
40 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
40 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41
41
42 def default_aliases():
42 def default_aliases():
43 # Make some aliases automatically
43 # Make some aliases automatically
44 # Prepare list of shell aliases to auto-define
44 # Prepare list of shell aliases to auto-define
45 if os.name == 'posix':
45 if os.name == 'posix':
46 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
46 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
47 'mv mv -i','rm rm -i','cp cp -i',
47 'mv mv -i','rm rm -i','cp cp -i',
48 'cat cat','less less','clear clear',
48 'cat cat','less less','clear clear',
49 # a better ls
49 # a better ls
50 'ls ls -F',
50 'ls ls -F',
51 # long ls
51 # long ls
52 'll ls -lF')
52 'll ls -lF')
53 # Extra ls aliases with color, which need special treatment on BSD
53 # Extra ls aliases with color, which need special treatment on BSD
54 # variants
54 # variants
55 ls_extra = ( # color ls
55 ls_extra = ( # color ls
56 'lc ls -F -o --color',
56 'lc ls -F -o --color',
57 # ls normal files only
57 # ls normal files only
58 'lf ls -F -o --color %l | grep ^-',
58 'lf ls -F -o --color %l | grep ^-',
59 # ls symbolic links
59 # ls symbolic links
60 'lk ls -F -o --color %l | grep ^l',
60 'lk ls -F -o --color %l | grep ^l',
61 # directories or links to directories,
61 # directories or links to directories,
62 'ldir ls -F -o --color %l | grep /$',
62 'ldir ls -F -o --color %l | grep /$',
63 # things which are executable
63 # things which are executable
64 'lx ls -F -o --color %l | grep ^-..x',
64 'lx ls -F -o --color %l | grep ^-..x',
65 )
65 )
66 # The BSDs don't ship GNU ls, so they don't understand the
66 # The BSDs don't ship GNU ls, so they don't understand the
67 # --color switch out of the box
67 # --color switch out of the box
68 if 'bsd' in sys.platform:
68 if 'bsd' in sys.platform:
69 ls_extra = ( # ls normal files only
69 ls_extra = ( # ls normal files only
70 'lf ls -lF | grep ^-',
70 'lf ls -lF | grep ^-',
71 # ls symbolic links
71 # ls symbolic links
72 'lk ls -lF | grep ^l',
72 'lk ls -lF | grep ^l',
73 # directories or links to directories,
73 # directories or links to directories,
74 'ldir ls -lF | grep /$',
74 'ldir ls -lF | grep /$',
75 # things which are executable
75 # things which are executable
76 'lx ls -lF | grep ^-..x',
76 'lx ls -lF | grep ^-..x',
77 )
77 )
78 auto_alias = auto_alias + ls_extra
78 default_aliases = default_aliases + ls_extra
79 elif os.name in ['nt','dos']:
79 elif os.name in ['nt','dos']:
80 auto_alias = ('ls dir /on',
80 default_aliases = ('ls dir /on',
81 'ddir dir /ad /on', 'ldir dir /ad /on',
81 'ddir dir /ad /on', 'ldir dir /ad /on',
82 'mkdir mkdir','rmdir rmdir','echo echo',
82 'mkdir mkdir','rmdir rmdir','echo echo',
83 'ren ren','cls cls','copy copy')
83 'ren ren','cls cls','copy copy')
84 else:
84 else:
85 auto_alias = ()
85 default_aliases = ()
86 return [s.split(None,1) for s in auto_alias]
86 return [s.split(None,1) for s in default_aliases]
87
87
88
88
89 class AliasError(Exception):
89 class AliasError(Exception):
90 pass
90 pass
91
91
92
92
93 class InvalidAliasError(AliasError):
93 class InvalidAliasError(AliasError):
94 pass
94 pass
95
95
96
96
97 #-----------------------------------------------------------------------------
97 #-----------------------------------------------------------------------------
98 # Main AliasManager class
98 # Main AliasManager class
99 #-----------------------------------------------------------------------------
99 #-----------------------------------------------------------------------------
100
100
101
101
102 class AliasManager(Component):
102 class AliasManager(Component):
103
103
104 auto_alias = List(default_aliases())
104 default_aliases = List(default_aliases(), config=True)
105 user_alias = List(default_value=[], config_key='USER_ALIAS')
105 user_aliases = List(default_value=[], config=True)
106
106
107 def __init__(self, parent, config=None):
107 def __init__(self, parent, config=None):
108 super(AliasManager, self).__init__(parent, config=config)
108 super(AliasManager, self).__init__(parent, config=config)
109 self.alias_table = {}
109 self.alias_table = {}
110 self.exclude_aliases()
110 self.exclude_aliases()
111 self.init_aliases()
111 self.init_aliases()
112
112
113 @auto_attr
113 @auto_attr
114 def shell(self):
114 def shell(self):
115 shell = Component.get_instances(
115 shell = Component.get_instances(
116 root=self.root,
116 root=self.root,
117 klass='IPython.core.iplib.InteractiveShell'
117 klass='IPython.core.iplib.InteractiveShell'
118 )[0]
118 )[0]
119 return shell
119 return shell
120
120
121 def __contains__(self, name):
121 def __contains__(self, name):
122 if name in self.alias_table:
122 if name in self.alias_table:
123 return True
123 return True
124 else:
124 else:
125 return False
125 return False
126
126
127 @property
127 @property
128 def aliases(self):
128 def aliases(self):
129 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
129 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
130
130
131 def exclude_aliases(self):
131 def exclude_aliases(self):
132 # set of things NOT to alias (keywords, builtins and some magics)
132 # set of things NOT to alias (keywords, builtins and some magics)
133 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
133 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
134 no_alias.update(set(keyword.kwlist))
134 no_alias.update(set(keyword.kwlist))
135 no_alias.update(set(__builtin__.__dict__.keys()))
135 no_alias.update(set(__builtin__.__dict__.keys()))
136 self.no_alias = no_alias
136 self.no_alias = no_alias
137
137
138 def init_aliases(self):
138 def init_aliases(self):
139 # Load default aliases
139 # Load default aliases
140 for name, cmd in self.auto_alias:
140 for name, cmd in self.default_aliases:
141 self.soft_define_alias(name, cmd)
141 self.soft_define_alias(name, cmd)
142
142
143 # Load user aliases
143 # Load user aliases
144 for name, cmd in self.user_alias:
144 for name, cmd in self.user_aliases:
145 self.soft_define_alias(name, cmd)
145 self.soft_define_alias(name, cmd)
146
146
147 def clear_aliases(self):
148 self.alias_table.clear()
149
147 def soft_define_alias(self, name, cmd):
150 def soft_define_alias(self, name, cmd):
148 """Define an alias, but don't raise on an AliasError."""
151 """Define an alias, but don't raise on an AliasError."""
149 try:
152 try:
150 self.define_alias(name, cmd)
153 self.define_alias(name, cmd)
151 except AliasError, e:
154 except AliasError, e:
152 error("Invalid alias: %s" % e)
155 error("Invalid alias: %s" % e)
153
156
154 def define_alias(self, name, cmd):
157 def define_alias(self, name, cmd):
155 """Define a new alias after validating it.
158 """Define a new alias after validating it.
156
159
157 This will raise an :exc:`AliasError` if there are validation
160 This will raise an :exc:`AliasError` if there are validation
158 problems.
161 problems.
159 """
162 """
160 nargs = self.validate_alias(name, cmd)
163 nargs = self.validate_alias(name, cmd)
161 self.alias_table[name] = (nargs, cmd)
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 def validate_alias(self, name, cmd):
170 def validate_alias(self, name, cmd):
164 """Validate an alias and return the its number of arguments."""
171 """Validate an alias and return the its number of arguments."""
165 if name in self.no_alias:
172 if name in self.no_alias:
166 raise InvalidAliasError("The name %s can't be aliased "
173 raise InvalidAliasError("The name %s can't be aliased "
167 "because it is a keyword or builtin." % name)
174 "because it is a keyword or builtin." % name)
168 if not (isinstance(cmd, basestring)):
175 if not (isinstance(cmd, basestring)):
169 raise InvalidAliasError("An alias command must be a string, "
176 raise InvalidAliasError("An alias command must be a string, "
170 "got: %r" % name)
177 "got: %r" % name)
171 nargs = cmd.count('%s')
178 nargs = cmd.count('%s')
172 if nargs>0 and cmd.find('%l')>=0:
179 if nargs>0 and cmd.find('%l')>=0:
173 raise InvalidAliasError('The %s and %l specifiers are mutually '
180 raise InvalidAliasError('The %s and %l specifiers are mutually '
174 'exclusive in alias definitions.')
181 'exclusive in alias definitions.')
175 return nargs
182 return nargs
176
183
177 def call_alias(self, alias, rest=''):
184 def call_alias(self, alias, rest=''):
178 """Call an alias given its name and the rest of the line."""
185 """Call an alias given its name and the rest of the line."""
179 cmd = self.transform_alias(alias, rest)
186 cmd = self.transform_alias(alias, rest)
180 try:
187 try:
181 self.shell.system(cmd)
188 self.shell.system(cmd)
182 except:
189 except:
183 self.shell.showtraceback()
190 self.shell.showtraceback()
184
191
185 def transform_alias(self, alias,rest=''):
192 def transform_alias(self, alias,rest=''):
186 """Transform alias to system command string."""
193 """Transform alias to system command string."""
187 nargs, cmd = self.alias_table[alias]
194 nargs, cmd = self.alias_table[alias]
188
195
189 if ' ' in cmd and os.path.isfile(cmd):
196 if ' ' in cmd and os.path.isfile(cmd):
190 cmd = '"%s"' % cmd
197 cmd = '"%s"' % cmd
191
198
192 # Expand the %l special to be the user's input line
199 # Expand the %l special to be the user's input line
193 if cmd.find('%l') >= 0:
200 if cmd.find('%l') >= 0:
194 cmd = cmd.replace('%l', rest)
201 cmd = cmd.replace('%l', rest)
195 rest = ''
202 rest = ''
196 if nargs==0:
203 if nargs==0:
197 # Simple, argument-less aliases
204 # Simple, argument-less aliases
198 cmd = '%s %s' % (cmd, rest)
205 cmd = '%s %s' % (cmd, rest)
199 else:
206 else:
200 # Handle aliases with positional arguments
207 # Handle aliases with positional arguments
201 args = rest.split(None, nargs)
208 args = rest.split(None, nargs)
202 if len(args) < nargs:
209 if len(args) < nargs:
203 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
210 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
204 (alias, nargs, len(args)))
211 (alias, nargs, len(args)))
205 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
212 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
206 return cmd
213 return cmd
207
214
208 def expand_alias(self, line):
215 def expand_alias(self, line):
209 """ Expand an alias in the command line
216 """ Expand an alias in the command line
210
217
211 Returns the provided command line, possibly with the first word
218 Returns the provided command line, possibly with the first word
212 (command) translated according to alias expansion rules.
219 (command) translated according to alias expansion rules.
213
220
214 [ipython]|16> _ip.expand_aliases("np myfile.txt")
221 [ipython]|16> _ip.expand_aliases("np myfile.txt")
215 <16> 'q:/opt/np/notepad++.exe myfile.txt'
222 <16> 'q:/opt/np/notepad++.exe myfile.txt'
216 """
223 """
217
224
218 pre,fn,rest = split_user_input(line)
225 pre,fn,rest = split_user_input(line)
219 res = pre + self.expand_aliases(fn, rest)
226 res = pre + self.expand_aliases(fn, rest)
220 return res
227 return res
221
228
222 def expand_aliases(self, fn, rest):
229 def expand_aliases(self, fn, rest):
223 """Expand multiple levels of aliases:
230 """Expand multiple levels of aliases:
224
231
225 if:
232 if:
226
233
227 alias foo bar /tmp
234 alias foo bar /tmp
228 alias baz foo
235 alias baz foo
229
236
230 then:
237 then:
231
238
232 baz huhhahhei -> bar /tmp huhhahhei
239 baz huhhahhei -> bar /tmp huhhahhei
233
240
234 """
241 """
235 line = fn + " " + rest
242 line = fn + " " + rest
236
243
237 done = set()
244 done = set()
238 while 1:
245 while 1:
239 pre,fn,rest = split_user_input(line, shell_line_split)
246 pre,fn,rest = split_user_input(line, shell_line_split)
240 if fn in self.alias_table:
247 if fn in self.alias_table:
241 if fn in done:
248 if fn in done:
242 warn("Cyclic alias definition, repeated '%s'" % fn)
249 warn("Cyclic alias definition, repeated '%s'" % fn)
243 return ""
250 return ""
244 done.add(fn)
251 done.add(fn)
245
252
246 l2 = self.transform_alias(fn, rest)
253 l2 = self.transform_alias(fn, rest)
247 if l2 == line:
254 if l2 == line:
248 break
255 break
249 # ls -> ls -F should not recurse forever
256 # ls -> ls -F should not recurse forever
250 if l2.split(None,1)[0] == line.split(None,1)[0]:
257 if l2.split(None,1)[0] == line.split(None,1)[0]:
251 line = l2
258 line = l2
252 break
259 break
253 line=l2
260 line=l2
254 else:
261 else:
255 break
262 break
256
263
257 return line
264 return line
@@ -1,242 +1,265 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 An application for IPython
4 An application for IPython
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10
10
11 Notes
11 Notes
12 -----
12 -----
13 """
13 """
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
17 #
18 # Distributed under the terms of the BSD License. The full license is in
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Imports
23 # Imports
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 import os
26 import os
27 import sys
27 import sys
28 import traceback
28 import traceback
29
30 from copy import deepcopy
29 from copy import deepcopy
31 from IPython.utils.ipstruct import Struct
30
32 from IPython.utils.genutils import get_ipython_dir, filefind
31 from IPython.utils.genutils import get_ipython_dir, filefind
33 from IPython.config.loader import (
32 from IPython.config.loader import (
34 IPythonArgParseConfigLoader,
33 PyFileConfigLoader,
35 PyFileConfigLoader
34 ArgParseConfigLoader,
35 Config,
36 NoConfigDefault
36 )
37 )
37
38
38 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
39 # Classes and functions
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 class ApplicationError(Exception):
65 class ApplicationError(Exception):
44 pass
66 pass
45
67
46
68
47 class Application(object):
69 class Application(object):
48 """Load a config, construct an app and run it.
70 """Load a config, construct an app and run it.
49 """
71 """
50
72
51 config_file_name = 'ipython_config.py'
73 config_file_name = 'ipython_config.py'
52 name = 'ipython'
74 name = 'ipython'
53 debug = False
75 debug = False
54
76
55 def __init__(self):
77 def __init__(self):
56 pass
78 pass
57
79
58 def start(self):
80 def start(self):
59 """Start the application."""
81 """Start the application."""
60 self.attempt(self.create_default_config)
82 self.attempt(self.create_default_config)
61 self.attempt(self.pre_load_command_line_config)
83 self.attempt(self.pre_load_command_line_config)
62 self.attempt(self.load_command_line_config, action='abort')
84 self.attempt(self.load_command_line_config, action='abort')
63 self.attempt(self.post_load_command_line_config)
85 self.attempt(self.post_load_command_line_config)
64 self.attempt(self.find_ipythondir)
86 self.attempt(self.find_ipythondir)
65 self.attempt(self.find_config_file_name)
87 self.attempt(self.find_config_file_name)
66 self.attempt(self.find_config_file_paths)
88 self.attempt(self.find_config_file_paths)
67 self.attempt(self.pre_load_file_config)
89 self.attempt(self.pre_load_file_config)
68 self.attempt(self.load_file_config)
90 self.attempt(self.load_file_config)
69 self.attempt(self.post_load_file_config)
91 self.attempt(self.post_load_file_config)
70 self.attempt(self.merge_configs)
92 self.attempt(self.merge_configs)
71 self.attempt(self.pre_construct)
93 self.attempt(self.pre_construct)
72 self.attempt(self.construct)
94 self.attempt(self.construct)
73 self.attempt(self.post_construct)
95 self.attempt(self.post_construct)
74 self.attempt(self.start_app)
96 self.attempt(self.start_app)
75
97
76 #-------------------------------------------------------------------------
98 #-------------------------------------------------------------------------
77 # Various stages of Application creation
99 # Various stages of Application creation
78 #-------------------------------------------------------------------------
100 #-------------------------------------------------------------------------
79
101
80 def create_default_config(self):
102 def create_default_config(self):
81 """Create defaults that can't be set elsewhere."""
103 """Create defaults that can't be set elsewhere."""
82 self.default_config = Struct()
104 self.default_config = Config()
83 self.default_config.IPYTHONDIR = get_ipython_dir()
105 self.default_config.Global.ipythondir = get_ipython_dir()
84
106
85 def create_command_line_config(self):
107 def create_command_line_config(self):
86 """Create and return a command line config loader."""
108 """Create and return a command line config loader."""
87 return IPythonArgParseConfigLoader(description=self.name)
109 return IPythonArgParseConfigLoader(description=self.name)
88
110
89 def pre_load_command_line_config(self):
111 def pre_load_command_line_config(self):
90 """Do actions just before loading the command line config."""
112 """Do actions just before loading the command line config."""
91 pass
113 pass
92
114
93 def load_command_line_config(self):
115 def load_command_line_config(self):
94 """Load the command line config.
116 """Load the command line config.
95
117
96 This method also sets ``self.debug``.
118 This method also sets ``self.debug``.
97 """
119 """
98
120
99 loader = self.create_command_line_config()
121 loader = self.create_command_line_config()
100 self.command_line_config = loader.load_config()
122 self.command_line_config = loader.load_config()
101 try:
123 try:
102 self.debug = self.command_line_config.DEBUG
124 self.debug = self.command_line_config.Global.debug
103 except AttributeError:
125 except AttributeError:
104 pass # use class default
126 pass # use class default
105 self.log("Default config loaded:", self.default_config)
127 self.log("Default config loaded:", self.default_config)
106 self.log("Command line config loaded:", self.command_line_config)
128 self.log("Command line config loaded:", self.command_line_config)
107
129
108 def post_load_command_line_config(self):
130 def post_load_command_line_config(self):
109 """Do actions just after loading the command line config."""
131 """Do actions just after loading the command line config."""
110 pass
132 pass
111
133
112 def find_ipythondir(self):
134 def find_ipythondir(self):
113 """Set the IPython directory.
135 """Set the IPython directory.
114
136
115 This sets ``self.ipythondir``, but the actual value that is passed
137 This sets ``self.ipythondir``, but the actual value that is passed
116 to the application is kept in either ``self.default_config`` or
138 to the application is kept in either ``self.default_config`` or
117 ``self.command_line_config``. This also added ``self.ipythondir`` to
139 ``self.command_line_config``. This also added ``self.ipythondir`` to
118 ``sys.path`` so config files there can be references by other config
140 ``sys.path`` so config files there can be references by other config
119 files.
141 files.
120 """
142 """
121
143
122 try:
144 try:
123 self.ipythondir = self.command_line_config.IPYTHONDIR
145 self.ipythondir = self.command_line_config.Global.ipythondir
124 except AttributeError:
146 except AttributeError:
125 self.ipythondir = self.default_config.IPYTHONDIR
147 self.ipythondir = self.default_config.Global.ipythondir
126 sys.path.append(os.path.abspath(self.ipythondir))
148 sys.path.append(os.path.abspath(self.ipythondir))
127 if not os.path.isdir(self.ipythondir):
149 if not os.path.isdir(self.ipythondir):
128 os.makedirs(self.ipythondir, mode = 0777)
150 os.makedirs(self.ipythondir, mode = 0777)
129 self.log("IPYTHONDIR set to: %s" % self.ipythondir)
151 self.log("IPYTHONDIR set to: %s" % self.ipythondir)
130
152
131 def find_config_file_name(self):
153 def find_config_file_name(self):
132 """Find the config file name for this application.
154 """Find the config file name for this application.
133
155
134 If a profile has been set at the command line, this will resolve
156 If a profile has been set at the command line, this will resolve
135 it. The search paths for the config file are set in
157 it. The search paths for the config file are set in
136 :meth:`find_config_file_paths` and then passed to the config file
158 :meth:`find_config_file_paths` and then passed to the config file
137 loader where they are resolved to an absolute path.
159 loader where they are resolved to an absolute path.
138 """
160 """
139
161
140 try:
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 except AttributeError:
164 except AttributeError:
143 pass
165 pass
144
166
145 try:
167 try:
146 self.profile_name = self.command_line_config.PROFILE
168 self.profile_name = self.command_line_config.Global.profile
147 name_parts = self.config_file_name.split('.')
169 name_parts = self.config_file_name.split('.')
148 name_parts.insert(1, '_' + self.profile_name + '.')
170 name_parts.insert(1, '_' + self.profile_name + '.')
149 self.config_file_name = ''.join(name_parts)
171 self.config_file_name = ''.join(name_parts)
150 except AttributeError:
172 except AttributeError:
151 pass
173 pass
152
174
153 def find_config_file_paths(self):
175 def find_config_file_paths(self):
154 """Set the search paths for resolving the config file."""
176 """Set the search paths for resolving the config file."""
155 self.config_file_paths = (os.getcwd(), self.ipythondir)
177 self.config_file_paths = (os.getcwd(), self.ipythondir)
156
178
157 def pre_load_file_config(self):
179 def pre_load_file_config(self):
158 """Do actions before the config file is loaded."""
180 """Do actions before the config file is loaded."""
159 pass
181 pass
160
182
161 def load_file_config(self):
183 def load_file_config(self):
162 """Load the config file.
184 """Load the config file.
163
185
164 This tries to load the config file from disk. If successful, the
186 This tries to load the config file from disk. If successful, the
165 ``CONFIG_FILE`` config variable is set to the resolved config file
187 ``CONFIG_FILE`` config variable is set to the resolved config file
166 location. If not successful, an empty config is used.
188 location. If not successful, an empty config is used.
167 """
189 """
168 loader = PyFileConfigLoader(self.config_file_name,
190 loader = PyFileConfigLoader(self.config_file_name,
169 self.config_file_paths)
191 path=self.config_file_paths)
170 try:
192 try:
171 self.file_config = loader.load_config()
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 except IOError:
195 except IOError:
174 self.log("Config file not found, skipping: %s" % \
196 self.log("Config file not found, skipping: %s" % \
175 self.config_file_name)
197 self.config_file_name)
176 self.file_config = Struct()
198 self.file_config = Config()
177 else:
199 else:
178 self.log("Config file loaded: %s" % loader.full_filename,
200 self.log("Config file loaded: %s" % loader.full_filename,
179 self.file_config)
201 self.file_config)
180
202
181 def post_load_file_config(self):
203 def post_load_file_config(self):
182 """Do actions after the config file is loaded."""
204 """Do actions after the config file is loaded."""
183 pass
205 pass
184
206
185 def merge_configs(self):
207 def merge_configs(self):
186 """Merge the default, command line and file config objects."""
208 """Merge the default, command line and file config objects."""
187 config = Struct()
209 config = Config()
188 config.update(self.default_config)
210 config._merge(self.default_config)
189 config.update(self.file_config)
211 config._merge(self.file_config)
190 config.update(self.command_line_config)
212 config._merge(self.command_line_config)
191 self.master_config = config
213 self.master_config = config
192 self.log("Master config created:", self.master_config)
214 self.log("Master config created:", self.master_config)
193
215
194 def pre_construct(self):
216 def pre_construct(self):
195 """Do actions after the config has been built, but before construct."""
217 """Do actions after the config has been built, but before construct."""
196 pass
218 pass
197
219
198 def construct(self):
220 def construct(self):
199 """Construct the main components that make up this app."""
221 """Construct the main components that make up this app."""
200 self.log("Constructing components for application...")
222 self.log("Constructing components for application...")
201
223
202 def post_construct(self):
224 def post_construct(self):
203 """Do actions after construct, but before starting the app."""
225 """Do actions after construct, but before starting the app."""
204 pass
226 pass
205
227
206 def start_app(self):
228 def start_app(self):
207 """Actually start the app."""
229 """Actually start the app."""
208 self.log("Starting application...")
230 self.log("Starting application...")
209
231
210 #-------------------------------------------------------------------------
232 #-------------------------------------------------------------------------
211 # Utility methods
233 # Utility methods
212 #-------------------------------------------------------------------------
234 #-------------------------------------------------------------------------
213
235
214 def abort(self):
236 def abort(self):
215 """Abort the starting of the application."""
237 """Abort the starting of the application."""
216 print "Aborting application: ", self.name
238 print "Aborting application: ", self.name
217 sys.exit(1)
239 sys.exit(1)
218
240
219 def exit(self):
241 def exit(self):
220 print "Exiting application: ", self.name
242 print "Exiting application: ", self.name
221 sys.exit(1)
243 sys.exit(1)
222
244
223 def attempt(self, func, action='abort'):
245 def attempt(self, func, action='abort'):
224 try:
246 try:
225 func()
247 func()
248 except SystemExit:
249 self.exit()
226 except:
250 except:
227 if action == 'abort':
251 if action == 'abort':
228 self.print_traceback()
252 self.print_traceback()
229 self.abort()
253 self.abort()
230 elif action == 'exit':
254 elif action == 'exit':
231 self.print_traceback()
232 self.exit()
255 self.exit()
233
256
234 def print_traceback(self):
257 def print_traceback(self):
235 print "Error in appliction startup: ", self.name
258 print "Error in appliction startup: ", self.name
236 print
259 print
237 traceback.print_exc()
260 traceback.print_exc()
238
261
239 def log(self, *args):
262 def log(self, *args):
240 if self.debug:
263 if self.debug:
241 for arg in args:
264 for arg in args:
242 print "[%s] %s" % (self.name, arg) No newline at end of file
265 print "[%s] %s" % (self.name, arg)
@@ -1,111 +1,118 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A context manager for managing things injected into :mod:`__builtin__`.
4 A context manager for managing things injected into :mod:`__builtin__`.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import __builtin__
22 import __builtin__
23
23
24 from IPython.core.component import Component
24 from IPython.core.component import Component
25 from IPython.core.quitter import Quitter
25 from IPython.core.quitter import Quitter
26
26
27 from IPython.utils.autoattr import auto_attr
27 from IPython.utils.autoattr import auto_attr
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes and functions
30 # Classes and functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33
33
34 class BuiltinUndefined(object): pass
34 class BuiltinUndefined(object): pass
35 BuiltinUndefined = BuiltinUndefined()
35 BuiltinUndefined = BuiltinUndefined()
36
36
37
37
38 class BuiltinTrap(Component):
38 class BuiltinTrap(Component):
39
39
40 def __init__(self, parent):
40 def __init__(self, parent):
41 super(BuiltinTrap, self).__init__(parent, None, None)
41 super(BuiltinTrap, self).__init__(parent, None, None)
42 self._orig_builtins = {}
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 @auto_attr
47 @auto_attr
45 def shell(self):
48 def shell(self):
46 shell = Component.get_instances(
49 shell = Component.get_instances(
47 root=self.root,
50 root=self.root,
48 klass='IPython.core.iplib.InteractiveShell'
51 klass='IPython.core.iplib.InteractiveShell'
49 )[0]
52 )[0]
50 return shell
53 return shell
51
54
52 def __enter__(self):
55 def __enter__(self):
53 self.set()
56 if self._nested_level == 0:
57 self.set()
58 self._nested_level += 1
54 # I return self, so callers can use add_builtin in a with clause.
59 # I return self, so callers can use add_builtin in a with clause.
55 return self
60 return self
56
61
57 def __exit__(self, type, value, traceback):
62 def __exit__(self, type, value, traceback):
58 self.unset()
63 if self._nested_level == 1:
64 self.unset()
65 self._nested_level -= 1
59 return True
66 return True
60
67
61 def add_builtin(self, key, value):
68 def add_builtin(self, key, value):
62 """Add a builtin and save the original."""
69 """Add a builtin and save the original."""
63 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
70 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
64 self._orig_builtins[key] = orig
71 self._orig_builtins[key] = orig
65 __builtin__.__dict__[key] = value
72 __builtin__.__dict__[key] = value
66
73
67 def remove_builtin(self, key):
74 def remove_builtin(self, key):
68 """Remove an added builtin and re-set the original."""
75 """Remove an added builtin and re-set the original."""
69 try:
76 try:
70 orig = self._orig_builtins.pop(key)
77 orig = self._orig_builtins.pop(key)
71 except KeyError:
78 except KeyError:
72 pass
79 pass
73 else:
80 else:
74 if orig is BuiltinUndefined:
81 if orig is BuiltinUndefined:
75 del __builtin__.__dict__[key]
82 del __builtin__.__dict__[key]
76 else:
83 else:
77 __builtin__.__dict__[key] = orig
84 __builtin__.__dict__[key] = orig
78
85
79 def set(self):
86 def set(self):
80 """Store ipython references in the __builtin__ namespace."""
87 """Store ipython references in the __builtin__ namespace."""
81 self.add_builtin('exit', Quitter(self.shell, 'exit'))
88 self.add_builtin('exit', Quitter(self.shell, 'exit'))
82 self.add_builtin('quit', Quitter(self.shell, 'quit'))
89 self.add_builtin('quit', Quitter(self.shell, 'quit'))
83
90
84 # Recursive reload function
91 # Recursive reload function
85 try:
92 try:
86 from IPython.lib import deepreload
93 from IPython.lib import deepreload
87 if self.shell.deep_reload:
94 if self.shell.deep_reload:
88 self.add_builtin('reload', deepreload.reload)
95 self.add_builtin('reload', deepreload.reload)
89 else:
96 else:
90 self.add_builtin('dreload', deepreload.reload)
97 self.add_builtin('dreload', deepreload.reload)
91 del deepreload
98 del deepreload
92 except ImportError:
99 except ImportError:
93 pass
100 pass
94
101
95 # Keep in the builtins a flag for when IPython is active. We set it
102 # Keep in the builtins a flag for when IPython is active. We set it
96 # with setdefault so that multiple nested IPythons don't clobber one
103 # with setdefault so that multiple nested IPythons don't clobber one
97 # another. Each will increase its value by one upon being activated,
104 # another. Each will increase its value by one upon being activated,
98 # which also gives us a way to determine the nesting level.
105 # which also gives us a way to determine the nesting level.
99 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
106 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
100
107
101 def unset(self):
108 def unset(self):
102 """Remove any builtins which might have been added by add_builtins, or
109 """Remove any builtins which might have been added by add_builtins, or
103 restore overwritten ones to their previous values."""
110 restore overwritten ones to their previous values."""
104 for key in self._orig_builtins.keys():
111 for key in self._orig_builtins.keys():
105 self.remove_builtin(key)
112 self.remove_builtin(key)
106 self._orig_builtins.clear()
113 self._orig_builtins.clear()
107 self._builtins_added = False
114 self._builtins_added = False
108 try:
115 try:
109 del __builtin__.__dict__['__IPYTHON__active']
116 del __builtin__.__dict__['__IPYTHON__active']
110 except KeyError:
117 except KeyError:
111 pass No newline at end of file
118 pass
@@ -1,304 +1,309 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A lightweight component system for IPython.
4 A lightweight component system for IPython.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 from copy import deepcopy
23 from copy import deepcopy
24 import datetime
24 import datetime
25 from weakref import WeakValueDictionary
25 from weakref import WeakValueDictionary
26
26
27 from IPython.utils.importstring import import_item
27 from IPython.utils.importstring import import_item
28 from IPython.utils.ipstruct import Struct
28 from IPython.config.loader import Config
29 from IPython.utils.traitlets import (
29 from IPython.utils.traitlets import (
30 HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This
30 HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This
31 )
31 )
32
32
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Helper classes for Components
35 # Helper classes for Components
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38
38
39 class ComponentError(Exception):
39 class ComponentError(Exception):
40 pass
40 pass
41
41
42 class MetaComponentTracker(type):
42 class MetaComponentTracker(type):
43 """A metaclass that tracks instances of Components and its subclasses."""
43 """A metaclass that tracks instances of Components and its subclasses."""
44
44
45 def __init__(cls, name, bases, d):
45 def __init__(cls, name, bases, d):
46 super(MetaComponentTracker, cls).__init__(name, bases, d)
46 super(MetaComponentTracker, cls).__init__(name, bases, d)
47 cls.__instance_refs = WeakValueDictionary()
47 cls.__instance_refs = WeakValueDictionary()
48 cls.__numcreated = 0
48 cls.__numcreated = 0
49
49
50 def __call__(cls, *args, **kw):
50 def __call__(cls, *args, **kw):
51 """Called when a class is called (instantiated)!!!
51 """Called when a class is called (instantiated)!!!
52
52
53 When a Component or subclass is instantiated, this is called and
53 When a Component or subclass is instantiated, this is called and
54 the instance is saved in a WeakValueDictionary for tracking.
54 the instance is saved in a WeakValueDictionary for tracking.
55 """
55 """
56 instance = cls.__new__(cls, *args, **kw)
56 instance = cls.__new__(cls, *args, **kw)
57
57
58 # Register the instance before __init__ is called so get_instances
58 # Register the instance before __init__ is called so get_instances
59 # works inside __init__ methods!
59 # works inside __init__ methods!
60 indices = cls.register_instance(instance)
60 indices = cls.register_instance(instance)
61
61
62 # This is in a try/except because of the __init__ method fails, the
62 # This is in a try/except because of the __init__ method fails, the
63 # instance is discarded and shouldn't be tracked.
63 # instance is discarded and shouldn't be tracked.
64 try:
64 try:
65 if isinstance(instance, cls):
65 if isinstance(instance, cls):
66 cls.__init__(instance, *args, **kw)
66 cls.__init__(instance, *args, **kw)
67 except:
67 except:
68 # Unregister the instance because __init__ failed!
68 # Unregister the instance because __init__ failed!
69 cls.unregister_instances(indices)
69 cls.unregister_instances(indices)
70 raise
70 raise
71 else:
71 else:
72 return instance
72 return instance
73
73
74 def register_instance(cls, instance):
74 def register_instance(cls, instance):
75 """Register instance with cls and its subclasses."""
75 """Register instance with cls and its subclasses."""
76 # indices is a list of the keys used to register the instance
76 # indices is a list of the keys used to register the instance
77 # with. This list is needed if the instance needs to be unregistered.
77 # with. This list is needed if the instance needs to be unregistered.
78 indices = []
78 indices = []
79 for c in cls.__mro__:
79 for c in cls.__mro__:
80 if issubclass(cls, c) and issubclass(c, Component):
80 if issubclass(cls, c) and issubclass(c, Component):
81 c.__numcreated += 1
81 c.__numcreated += 1
82 indices.append(c.__numcreated)
82 indices.append(c.__numcreated)
83 c.__instance_refs[c.__numcreated] = instance
83 c.__instance_refs[c.__numcreated] = instance
84 else:
84 else:
85 break
85 break
86 return indices
86 return indices
87
87
88 def unregister_instances(cls, indices):
88 def unregister_instances(cls, indices):
89 """Unregister instance with cls and its subclasses."""
89 """Unregister instance with cls and its subclasses."""
90 for c, index in zip(cls.__mro__, indices):
90 for c, index in zip(cls.__mro__, indices):
91 try:
91 try:
92 del c.__instance_refs[index]
92 del c.__instance_refs[index]
93 except KeyError:
93 except KeyError:
94 pass
94 pass
95
95
96 def clear_instances(cls):
96 def clear_instances(cls):
97 """Clear all instances tracked by cls."""
97 """Clear all instances tracked by cls."""
98 cls.__instance_refs.clear()
98 cls.__instance_refs.clear()
99 cls.__numcreated = 0
99 cls.__numcreated = 0
100
100
101 def get_instances(cls, name=None, root=None, klass=None):
101 def get_instances(cls, name=None, root=None, klass=None):
102 """Get all instances of cls and its subclasses.
102 """Get all instances of cls and its subclasses.
103
103
104 Parameters
104 Parameters
105 ----------
105 ----------
106 name : str
106 name : str
107 Limit to components with this name.
107 Limit to components with this name.
108 root : Component or subclass
108 root : Component or subclass
109 Limit to components having this root.
109 Limit to components having this root.
110 klass : class or str
110 klass : class or str
111 Limits to instances of the class or its subclasses. If a str
111 Limits to instances of the class or its subclasses. If a str
112 is given ut must be in the form 'foo.bar.MyClass'. The str
112 is given ut must be in the form 'foo.bar.MyClass'. The str
113 form of this argument is useful for forward declarations.
113 form of this argument is useful for forward declarations.
114 """
114 """
115 if klass is not None:
115 if klass is not None:
116 if isinstance(klass, basestring):
116 if isinstance(klass, basestring):
117 klass = import_item(klass)
117 klass = import_item(klass)
118 # Limit search to instances of klass for performance
118 # Limit search to instances of klass for performance
119 if issubclass(klass, Component):
119 if issubclass(klass, Component):
120 return klass.get_instances(name=name, root=root)
120 return klass.get_instances(name=name, root=root)
121 instances = cls.__instance_refs.values()
121 instances = cls.__instance_refs.values()
122 if name is not None:
122 if name is not None:
123 instances = [i for i in instances if i.name == name]
123 instances = [i for i in instances if i.name == name]
124 if klass is not None:
124 if klass is not None:
125 instances = [i for i in instances if isinstance(i, klass)]
125 instances = [i for i in instances if isinstance(i, klass)]
126 if root is not None:
126 if root is not None:
127 instances = [i for i in instances if i.root == root]
127 instances = [i for i in instances if i.root == root]
128 return instances
128 return instances
129
129
130 def get_instances_by_condition(cls, call, name=None, root=None,
130 def get_instances_by_condition(cls, call, name=None, root=None,
131 klass=None):
131 klass=None):
132 """Get all instances of cls, i such that call(i)==True.
132 """Get all instances of cls, i such that call(i)==True.
133
133
134 This also takes the ``name`` and ``root`` and ``classname``
134 This also takes the ``name`` and ``root`` and ``classname``
135 arguments of :meth:`get_instance`
135 arguments of :meth:`get_instance`
136 """
136 """
137 return [i for i in cls.get_instances(name, root, klass) if call(i)]
137 return [i for i in cls.get_instances(name, root, klass) if call(i)]
138
138
139
139
140 def masquerade_as(instance, cls):
140 def masquerade_as(instance, cls):
141 """Let instance masquerade as an instance of cls.
141 """Let instance masquerade as an instance of cls.
142
142
143 Sometimes, such as in testing code, it is useful to let a class
143 Sometimes, such as in testing code, it is useful to let a class
144 masquerade as another. Python, being duck typed, allows this by
144 masquerade as another. Python, being duck typed, allows this by
145 default. But, instances of components are tracked by their class type.
145 default. But, instances of components are tracked by their class type.
146
146
147 After calling this, cls.get_instances() will return ``instance``. This
147 After calling this, cls.get_instances() will return ``instance``. This
148 does not, however, cause isinstance(instance, cls) to return ``True``.
148 does not, however, cause isinstance(instance, cls) to return ``True``.
149
149
150 Parameters
150 Parameters
151 ----------
151 ----------
152 instance : an instance of a Component or Component subclass
152 instance : an instance of a Component or Component subclass
153 The instance that will pretend to be a cls.
153 The instance that will pretend to be a cls.
154 cls : subclass of Component
154 cls : subclass of Component
155 The Component subclass that instance will pretend to be.
155 The Component subclass that instance will pretend to be.
156 """
156 """
157 cls.register_instance(instance)
157 cls.register_instance(instance)
158
158
159
159
160 class ComponentNameGenerator(object):
160 class ComponentNameGenerator(object):
161 """A Singleton to generate unique component names."""
161 """A Singleton to generate unique component names."""
162
162
163 def __init__(self, prefix):
163 def __init__(self, prefix):
164 self.prefix = prefix
164 self.prefix = prefix
165 self.i = 0
165 self.i = 0
166
166
167 def __call__(self):
167 def __call__(self):
168 count = self.i
168 count = self.i
169 self.i += 1
169 self.i += 1
170 return "%s%s" % (self.prefix, count)
170 return "%s%s" % (self.prefix, count)
171
171
172
172
173 ComponentNameGenerator = ComponentNameGenerator('ipython.component')
173 ComponentNameGenerator = ComponentNameGenerator('ipython.component')
174
174
175
175
176 class MetaComponent(MetaHasTraitlets, MetaComponentTracker):
176 class MetaComponent(MetaHasTraitlets, MetaComponentTracker):
177 pass
177 pass
178
178
179
179
180 #-----------------------------------------------------------------------------
180 #-----------------------------------------------------------------------------
181 # Component implementation
181 # Component implementation
182 #-----------------------------------------------------------------------------
182 #-----------------------------------------------------------------------------
183
183
184
184
185 class Component(HasTraitlets):
185 class Component(HasTraitlets):
186
186
187 __metaclass__ = MetaComponent
187 __metaclass__ = MetaComponent
188
188
189 # Traitlets are fun!
189 # Traitlets are fun!
190 config = Instance(Struct,(),{})
190 config = Instance(Config,(),{})
191 parent = This()
191 parent = This()
192 root = This()
192 root = This()
193 created = None
193 created = None
194
194
195 def __init__(self, parent, name=None, config=None):
195 def __init__(self, parent, name=None, config=None):
196 """Create a component given a parent and possibly and name and config.
196 """Create a component given a parent and possibly and name and config.
197
197
198 Parameters
198 Parameters
199 ----------
199 ----------
200 parent : Component subclass
200 parent : Component subclass
201 The parent in the component graph. The parent is used
201 The parent in the component graph. The parent is used
202 to get the root of the component graph.
202 to get the root of the component graph.
203 name : str
203 name : str
204 The unique name of the component. If empty, then a unique
204 The unique name of the component. If empty, then a unique
205 one will be autogenerated.
205 one will be autogenerated.
206 config : Struct
206 config : Config
207 If this is empty, self.config = parent.config, otherwise
207 If this is empty, self.config = parent.config, otherwise
208 self.config = config and root.config is ignored. This argument
208 self.config = config and root.config is ignored. This argument
209 should only be used to *override* the automatic inheritance of
209 should only be used to *override* the automatic inheritance of
210 parent.config. If a caller wants to modify parent.config
210 parent.config. If a caller wants to modify parent.config
211 (not override), the caller should make a copy and change
211 (not override), the caller should make a copy and change
212 attributes and then pass the copy to this argument.
212 attributes and then pass the copy to this argument.
213
213
214 Notes
214 Notes
215 -----
215 -----
216 Subclasses of Component must call the :meth:`__init__` method of
216 Subclasses of Component must call the :meth:`__init__` method of
217 :class:`Component` *before* doing anything else and using
217 :class:`Component` *before* doing anything else and using
218 :func:`super`::
218 :func:`super`::
219
219
220 class MyComponent(Component):
220 class MyComponent(Component):
221 def __init__(self, parent, name=None, config=None):
221 def __init__(self, parent, name=None, config=None):
222 super(MyComponent, self).__init__(parent, name, config)
222 super(MyComponent, self).__init__(parent, name, config)
223 # Then any other code you need to finish initialization.
223 # Then any other code you need to finish initialization.
224
224
225 This ensures that the :attr:`parent`, :attr:`name` and :attr:`config`
225 This ensures that the :attr:`parent`, :attr:`name` and :attr:`config`
226 attributes are handled properly.
226 attributes are handled properly.
227 """
227 """
228 super(Component, self).__init__()
228 super(Component, self).__init__()
229 self._children = []
229 self._children = []
230 if name is None:
230 if name is None:
231 self.name = ComponentNameGenerator()
231 self.name = ComponentNameGenerator()
232 else:
232 else:
233 self.name = name
233 self.name = name
234 self.root = self # This is the default, it is set when parent is set
234 self.root = self # This is the default, it is set when parent is set
235 self.parent = parent
235 self.parent = parent
236 if config is not None:
236 if config is not None:
237 self.config = deepcopy(config)
237 self.config = deepcopy(config)
238 else:
238 else:
239 if self.parent is not None:
239 if self.parent is not None:
240 self.config = deepcopy(self.parent.config)
240 self.config = deepcopy(self.parent.config)
241
241
242 self.created = datetime.datetime.now()
242 self.created = datetime.datetime.now()
243
243
244 #-------------------------------------------------------------------------
244 #-------------------------------------------------------------------------
245 # Static traitlet notifiations
245 # Static traitlet notifiations
246 #-------------------------------------------------------------------------
246 #-------------------------------------------------------------------------
247
247
248 def _parent_changed(self, name, old, new):
248 def _parent_changed(self, name, old, new):
249 if old is not None:
249 if old is not None:
250 old._remove_child(self)
250 old._remove_child(self)
251 if new is not None:
251 if new is not None:
252 new._add_child(self)
252 new._add_child(self)
253
253
254 if new is None:
254 if new is None:
255 self.root = self
255 self.root = self
256 else:
256 else:
257 self.root = new.root
257 self.root = new.root
258
258
259 def _root_changed(self, name, old, new):
259 def _root_changed(self, name, old, new):
260 if self.parent is None:
260 if self.parent is None:
261 if not (new is self):
261 if not (new is self):
262 raise ComponentError("Root not self, but parent is None.")
262 raise ComponentError("Root not self, but parent is None.")
263 else:
263 else:
264 if not self.parent.root is new:
264 if not self.parent.root is new:
265 raise ComponentError("Error in setting the root attribute: "
265 raise ComponentError("Error in setting the root attribute: "
266 "root != parent.root")
266 "root != parent.root")
267
267
268 def _config_changed(self, name, old, new):
268 def _config_changed(self, name, old, new):
269 """Update all the class traits having a config_key with the config.
269 """Update all the class traits having a config_key with the config.
270
270
271 For any class traitlet with a ``config_key`` metadata attribute, we
271 For any class traitlet with a ``config_key`` metadata attribute, we
272 update the traitlet with the value of the corresponding config entry.
272 update the traitlet with the value of the corresponding config entry.
273
273
274 In the future, we might want to do a pop here so stale config info
274 In the future, we might want to do a pop here so stale config info
275 is not passed onto children.
275 is not passed onto children.
276 """
276 """
277 # Get all traitlets with a config_key metadata entry
277 # Get all traitlets with a config metadata entry that is True
278 traitlets = self.traitlets('config_key')
278 traitlets = self.traitlets(config=True)
279 for k, v in traitlets.items():
279
280 try:
280 # Don't do a blind getattr as that would cause the config to
281 config_value = new[v.get_metadata('config_key')]
281 # dynamically create the section with name self.__class__.__name__.
282 except KeyError:
282 if new._has_section(self.__class__.__name__):
283 pass
283 my_config = new[self.__class__.__name__]
284 else:
284 for k, v in traitlets.items():
285 setattr(self, k, config_value)
285 try:
286 config_value = my_config[k]
287 except KeyError:
288 pass
289 else:
290 setattr(self, k, config_value)
286
291
287 @property
292 @property
288 def children(self):
293 def children(self):
289 """A list of all my child components."""
294 """A list of all my child components."""
290 return self._children
295 return self._children
291
296
292 def _remove_child(self, child):
297 def _remove_child(self, child):
293 """A private method for removing children components."""
298 """A private method for removing children components."""
294 if child in self._children:
299 if child in self._children:
295 index = self._children.index(child)
300 index = self._children.index(child)
296 del self._children[index]
301 del self._children[index]
297
302
298 def _add_child(self, child):
303 def _add_child(self, child):
299 """A private method for adding children components."""
304 """A private method for adding children components."""
300 if child not in self._children:
305 if child not in self._children:
301 self._children.append(child)
306 self._children.append(child)
302
307
303 def __repr__(self):
308 def __repr__(self):
304 return "<%s('%s')>" % (self.__class__.__name__, self.name)
309 return "<%s('%s')>" % (self.__class__.__name__, self.name)
@@ -1,72 +1,78 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A context manager for handling sys.displayhook.
4 A context manager for handling sys.displayhook.
5
5
6 Authors:
6 Authors:
7
7
8 * Robert Kern
8 * Robert Kern
9 * Brian Granger
9 * Brian Granger
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import sys
23 import sys
24
24
25 from IPython.core.component import Component
25 from IPython.core.component import Component
26
26
27 from IPython.utils.autoattr import auto_attr
27 from IPython.utils.autoattr import auto_attr
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes and functions
30 # Classes and functions
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33
33
34 class DisplayTrap(Component):
34 class DisplayTrap(Component):
35 """Object to manage sys.displayhook.
35 """Object to manage sys.displayhook.
36
36
37 This came from IPython.core.kernel.display_hook, but is simplified
37 This came from IPython.core.kernel.display_hook, but is simplified
38 (no callbacks or formatters) until more of the core is refactored.
38 (no callbacks or formatters) until more of the core is refactored.
39 """
39 """
40
40
41 def __init__(self, parent, hook):
41 def __init__(self, parent, hook):
42 super(DisplayTrap, self).__init__(parent, None, None)
42 super(DisplayTrap, self).__init__(parent, None, None)
43
44 self.hook = hook
43 self.hook = hook
45 self.old_hook = None
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 @auto_attr
49 @auto_attr
48 def shell(self):
50 def shell(self):
49 shell = Component.get_instances(
51 shell = Component.get_instances(
50 root=self.root,
52 root=self.root,
51 klass='IPython.core.iplib.InteractiveShell'
53 klass='IPython.core.iplib.InteractiveShell'
52 )[0]
54 )[0]
53 return shell
55 return shell
54
56
55 def __enter__(self):
57 def __enter__(self):
56 self.set()
58 if self._nested_level == 0:
59 self.set()
60 self._nested_level += 1
57 return self
61 return self
58
62
59 def __exit__(self, type, value, traceback):
63 def __exit__(self, type, value, traceback):
60 self.unset()
64 if self._nested_level == 1:
65 self.unset()
66 self._nested_level -= 1
61 return True
67 return True
62
68
63 def set(self):
69 def set(self):
64 """Set the hook."""
70 """Set the hook."""
65 if sys.displayhook is not self.hook:
71 if sys.displayhook is not self.hook:
66 self.old_hook = sys.displayhook
72 self.old_hook = sys.displayhook
67 sys.displayhook = self.hook
73 sys.displayhook = self.hook
68
74
69 def unset(self):
75 def unset(self):
70 """Unset the hook."""
76 """Unset the hook."""
71 sys.displayhook = self.old_hook
77 sys.displayhook = self.old_hook
72
78
@@ -1,268 +1,272 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 An embedded IPython shell.
4 An embedded IPython shell.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10
10
11 Notes
11 Notes
12 -----
12 -----
13 """
13 """
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
17 #
18 # Distributed under the terms of the BSD License. The full license is in
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Imports
23 # Imports
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 from __future__ import with_statement
26 from __future__ import with_statement
27
27
28 import sys
28 import sys
29 from contextlib import nested
29 from contextlib import nested
30
30
31 from IPython.core import ultratb
31 from IPython.core import ultratb
32 from IPython.core.iplib import InteractiveShell
32 from IPython.core.iplib import InteractiveShell
33 from IPython.core.ipapp import load_default_config
33
34
34 from IPython.utils.traitlets import Bool, Str, CBool
35 from IPython.utils.traitlets import Bool, Str, CBool
35 from IPython.utils.genutils import ask_yes_no
36 from IPython.utils.genutils import ask_yes_no
36
37
38
37 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
38 # Classes and functions
40 # Classes and functions
39 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
40
42
41 # This is an additional magic that is exposed in embedded shells.
43 # This is an additional magic that is exposed in embedded shells.
42 def kill_embedded(self,parameter_s=''):
44 def kill_embedded(self,parameter_s=''):
43 """%kill_embedded : deactivate for good the current embedded IPython.
45 """%kill_embedded : deactivate for good the current embedded IPython.
44
46
45 This function (after asking for confirmation) sets an internal flag so that
47 This function (after asking for confirmation) sets an internal flag so that
46 an embedded IPython will never activate again. This is useful to
48 an embedded IPython will never activate again. This is useful to
47 permanently disable a shell that is being called inside a loop: once you've
49 permanently disable a shell that is being called inside a loop: once you've
48 figured out what you needed from it, you may then kill it and the program
50 figured out what you needed from it, you may then kill it and the program
49 will then continue to run without the interactive shell interfering again.
51 will then continue to run without the interactive shell interfering again.
50 """
52 """
51
53
52 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
54 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
53 "(y/n)? [y/N] ",'n')
55 "(y/n)? [y/N] ",'n')
54 if kill:
56 if kill:
55 self.embedded_active = False
57 self.embedded_active = False
56 print "This embedded IPython will not reactivate anymore once you exit."
58 print "This embedded IPython will not reactivate anymore once you exit."
57
59
58
60
59 class InteractiveShellEmbed(InteractiveShell):
61 class InteractiveShellEmbed(InteractiveShell):
60
62
61 dummy_mode = Bool(False)
63 dummy_mode = Bool(False)
62 exit_msg = Str('')
64 exit_msg = Str('')
63 embedded = CBool(True)
65 embedded = CBool(True)
64 embedded_active = CBool(True)
66 embedded_active = CBool(True)
65
67
66 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
68 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
67 user_ns=None, user_global_ns=None,
69 user_ns=None, user_global_ns=None,
68 banner1=None, banner2=None,
70 banner1=None, banner2=None,
69 custom_exceptions=((),None), exit_msg=''):
71 custom_exceptions=((),None), exit_msg=''):
70
72
71 self.save_sys_ipcompleter()
73 self.save_sys_ipcompleter()
72
74
73 super(InteractiveShellEmbed,self).__init__(
75 super(InteractiveShellEmbed,self).__init__(
74 parent=parent, config=config, ipythondir=ipythondir, usage=usage,
76 parent=parent, config=config, ipythondir=ipythondir, usage=usage,
75 user_ns=user_ns, user_global_ns=user_global_ns,
77 user_ns=user_ns, user_global_ns=user_global_ns,
76 banner1=banner1, banner2=banner2,
78 banner1=banner1, banner2=banner2,
77 custom_exceptions=custom_exceptions)
79 custom_exceptions=custom_exceptions)
78
80
79 self.exit_msg = exit_msg
81 self.exit_msg = exit_msg
80 self.define_magic("kill_embedded", kill_embedded)
82 self.define_magic("kill_embedded", kill_embedded)
81
83
82 # don't use the ipython crash handler so that user exceptions aren't
84 # don't use the ipython crash handler so that user exceptions aren't
83 # trapped
85 # trapped
84 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
86 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
85 mode=self.xmode,
87 mode=self.xmode,
86 call_pdb=self.pdb)
88 call_pdb=self.pdb)
87
89
88 self.restore_sys_ipcompleter()
90 self.restore_sys_ipcompleter()
89
91
90 def init_sys_modules(self):
92 def init_sys_modules(self):
91 pass
93 pass
92
94
93 def save_sys_ipcompleter(self):
95 def save_sys_ipcompleter(self):
94 """Save readline completer status."""
96 """Save readline completer status."""
95 try:
97 try:
96 #print 'Save completer',sys.ipcompleter # dbg
98 #print 'Save completer',sys.ipcompleter # dbg
97 self.sys_ipcompleter_orig = sys.ipcompleter
99 self.sys_ipcompleter_orig = sys.ipcompleter
98 except:
100 except:
99 pass # not nested with IPython
101 pass # not nested with IPython
100
102
101 def restore_sys_ipcompleter(self):
103 def restore_sys_ipcompleter(self):
102 """Restores the readline completer which was in place.
104 """Restores the readline completer which was in place.
103
105
104 This allows embedded IPython within IPython not to disrupt the
106 This allows embedded IPython within IPython not to disrupt the
105 parent's completion.
107 parent's completion.
106 """
108 """
107 try:
109 try:
108 self.readline.set_completer(self.sys_ipcompleter_orig)
110 self.readline.set_completer(self.sys_ipcompleter_orig)
109 sys.ipcompleter = self.sys_ipcompleter_orig
111 sys.ipcompleter = self.sys_ipcompleter_orig
110 except:
112 except:
111 pass
113 pass
112
114
113 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
115 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
114 stack_depth=1):
116 stack_depth=1):
115 """Activate the interactive interpreter.
117 """Activate the interactive interpreter.
116
118
117 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
119 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
118 the interpreter shell with the given local and global namespaces, and
120 the interpreter shell with the given local and global namespaces, and
119 optionally print a header string at startup.
121 optionally print a header string at startup.
120
122
121 The shell can be globally activated/deactivated using the
123 The shell can be globally activated/deactivated using the
122 set/get_dummy_mode methods. This allows you to turn off a shell used
124 set/get_dummy_mode methods. This allows you to turn off a shell used
123 for debugging globally.
125 for debugging globally.
124
126
125 However, *each* time you call the shell you can override the current
127 However, *each* time you call the shell you can override the current
126 state of dummy_mode with the optional keyword parameter 'dummy'. For
128 state of dummy_mode with the optional keyword parameter 'dummy'. For
127 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
129 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
128 can still have a specific call work by making it as IPShell(dummy=0).
130 can still have a specific call work by making it as IPShell(dummy=0).
129
131
130 The optional keyword parameter dummy controls whether the call
132 The optional keyword parameter dummy controls whether the call
131 actually does anything.
133 actually does anything.
132 """
134 """
133
135
134 # If the user has turned it off, go away
136 # If the user has turned it off, go away
135 if not self.embedded_active:
137 if not self.embedded_active:
136 return
138 return
137
139
138 # Normal exits from interactive mode set this flag, so the shell can't
140 # Normal exits from interactive mode set this flag, so the shell can't
139 # re-enter (it checks this variable at the start of interactive mode).
141 # re-enter (it checks this variable at the start of interactive mode).
140 self.exit_now = False
142 self.exit_now = False
141
143
142 # Allow the dummy parameter to override the global __dummy_mode
144 # Allow the dummy parameter to override the global __dummy_mode
143 if dummy or (dummy != 0 and self.dummy_mode):
145 if dummy or (dummy != 0 and self.dummy_mode):
144 return
146 return
145
147
146 if self.has_readline:
148 if self.has_readline:
147 self.set_completer()
149 self.set_completer()
148
150
149 if self.banner and header:
151 if self.banner and header:
150 format = '%s\n%s\n'
152 format = '%s\n%s\n'
151 else:
153 else:
152 format = '%s%s\n'
154 format = '%s%s\n'
153 banner = format % (self.banner,header)
155 banner = format % (self.banner,header)
154
156
155 # Call the embedding code with a stack depth of 1 so it can skip over
157 # Call the embedding code with a stack depth of 1 so it can skip over
156 # our call and get the original caller's namespaces.
158 # our call and get the original caller's namespaces.
157 self.mainloop(banner, local_ns, global_ns,
159 self.mainloop(banner, local_ns, global_ns,
158 stack_depth=stack_depth)
160 stack_depth=stack_depth)
159
161
160 if self.exit_msg is not None:
162 if self.exit_msg is not None:
161 print self.exit_msg
163 print self.exit_msg
162
164
163 self.restore_sys_ipcompleter()
165 self.restore_sys_ipcompleter()
164
166
165 def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
167 def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
166 """Embeds IPython into a running python program.
168 """Embeds IPython into a running python program.
167
169
168 Input:
170 Input:
169
171
170 - header: An optional header message can be specified.
172 - header: An optional header message can be specified.
171
173
172 - local_ns, global_ns: working namespaces. If given as None, the
174 - local_ns, global_ns: working namespaces. If given as None, the
173 IPython-initialized one is updated with __main__.__dict__, so that
175 IPython-initialized one is updated with __main__.__dict__, so that
174 program variables become visible but user-specific configuration
176 program variables become visible but user-specific configuration
175 remains possible.
177 remains possible.
176
178
177 - stack_depth: specifies how many levels in the stack to go to
179 - stack_depth: specifies how many levels in the stack to go to
178 looking for namespaces (when local_ns and global_ns are None). This
180 looking for namespaces (when local_ns and global_ns are None). This
179 allows an intermediate caller to make sure that this function gets
181 allows an intermediate caller to make sure that this function gets
180 the namespace from the intended level in the stack. By default (0)
182 the namespace from the intended level in the stack. By default (0)
181 it will get its locals and globals from the immediate caller.
183 it will get its locals and globals from the immediate caller.
182
184
183 Warning: it's possible to use this in a program which is being run by
185 Warning: it's possible to use this in a program which is being run by
184 IPython itself (via %run), but some funny things will happen (a few
186 IPython itself (via %run), but some funny things will happen (a few
185 globals get overwritten). In the future this will be cleaned up, as
187 globals get overwritten). In the future this will be cleaned up, as
186 there is no fundamental reason why it can't work perfectly."""
188 there is no fundamental reason why it can't work perfectly."""
187
189
188 # Get locals and globals from caller
190 # Get locals and globals from caller
189 if local_ns is None or global_ns is None:
191 if local_ns is None or global_ns is None:
190 call_frame = sys._getframe(stack_depth).f_back
192 call_frame = sys._getframe(stack_depth).f_back
191
193
192 if local_ns is None:
194 if local_ns is None:
193 local_ns = call_frame.f_locals
195 local_ns = call_frame.f_locals
194 if global_ns is None:
196 if global_ns is None:
195 global_ns = call_frame.f_globals
197 global_ns = call_frame.f_globals
196
198
197 # Update namespaces and fire up interpreter
199 # Update namespaces and fire up interpreter
198
200
199 # The global one is easy, we can just throw it in
201 # The global one is easy, we can just throw it in
200 self.user_global_ns = global_ns
202 self.user_global_ns = global_ns
201
203
202 # but the user/local one is tricky: ipython needs it to store internal
204 # but the user/local one is tricky: ipython needs it to store internal
203 # data, but we also need the locals. We'll copy locals in the user
205 # data, but we also need the locals. We'll copy locals in the user
204 # one, but will track what got copied so we can delete them at exit.
206 # one, but will track what got copied so we can delete them at exit.
205 # This is so that a later embedded call doesn't see locals from a
207 # This is so that a later embedded call doesn't see locals from a
206 # previous call (which most likely existed in a separate scope).
208 # previous call (which most likely existed in a separate scope).
207 local_varnames = local_ns.keys()
209 local_varnames = local_ns.keys()
208 self.user_ns.update(local_ns)
210 self.user_ns.update(local_ns)
209 #self.user_ns['local_ns'] = local_ns # dbg
211 #self.user_ns['local_ns'] = local_ns # dbg
210
212
211 # Patch for global embedding to make sure that things don't overwrite
213 # Patch for global embedding to make sure that things don't overwrite
212 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
214 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
213 # FIXME. Test this a bit more carefully (the if.. is new)
215 # FIXME. Test this a bit more carefully (the if.. is new)
214 if local_ns is None and global_ns is None:
216 if local_ns is None and global_ns is None:
215 self.user_global_ns.update(__main__.__dict__)
217 self.user_global_ns.update(__main__.__dict__)
216
218
217 # make sure the tab-completer has the correct frame information, so it
219 # make sure the tab-completer has the correct frame information, so it
218 # actually completes using the frame's locals/globals
220 # actually completes using the frame's locals/globals
219 self.set_completer_frame()
221 self.set_completer_frame()
220
222
221 with nested(self.builtin_trap, self.display_trap):
223 with nested(self.builtin_trap, self.display_trap):
222 self.interact(header)
224 self.interact(header)
223
225
224 # now, purge out the user namespace from anything we might have added
226 # now, purge out the user namespace from anything we might have added
225 # from the caller's local namespace
227 # from the caller's local namespace
226 delvar = self.user_ns.pop
228 delvar = self.user_ns.pop
227 for var in local_varnames:
229 for var in local_varnames:
228 delvar(var,None)
230 delvar(var,None)
229
231
230 def set_completer_frame(self, frame=None):
232 def set_completer_frame(self, frame=None):
231 if frame:
233 if frame:
232 self.Completer.namespace = frame.f_locals
234 self.Completer.namespace = frame.f_locals
233 self.Completer.global_namespace = frame.f_globals
235 self.Completer.global_namespace = frame.f_globals
234 else:
236 else:
235 self.Completer.namespace = self.user_ns
237 self.Completer.namespace = self.user_ns
236 self.Completer.global_namespace = self.user_global_ns
238 self.Completer.global_namespace = self.user_global_ns
237
239
238
240
239 _embedded_shell = None
241 _embedded_shell = None
240
242
241
243
242 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
244 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
243 exit_msg=''):
245 exit_msg=''):
244 """Call this to embed IPython at the current point in your program.
246 """Call this to embed IPython at the current point in your program.
245
247
246 The first invocation of this will create an :class:`InteractiveShellEmbed`
248 The first invocation of this will create an :class:`InteractiveShellEmbed`
247 instance and then call it. Consecutive calls just call the already
249 instance and then call it. Consecutive calls just call the already
248 created instance.
250 created instance.
249
251
250 Here is a simple example::
252 Here is a simple example::
251
253
252 from IPython import embed
254 from IPython import embed
253 a = 10
255 a = 10
254 b = 20
256 b = 20
255 embed('First time')
257 embed('First time')
256 c = 30
258 c = 30
257 d = 40
259 d = 40
258 embed
260 embed
259
261
260 Full customization can be done by passing a :class:`Struct` in as the
262 Full customization can be done by passing a :class:`Struct` in as the
261 config argument.
263 config argument.
262 """
264 """
265 if config is None:
266 config = load_default_config()
263 global _embedded_shell
267 global _embedded_shell
264 if _embedded_shell is None:
268 if _embedded_shell is None:
265 _embedded_shell = InteractiveShellEmbed(config=config,
269 _embedded_shell = InteractiveShellEmbed(config=config,
266 usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg)
270 usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg)
267 _embedded_shell(header=header, stack_depth=2)
271 _embedded_shell(header=header, stack_depth=2)
268
272
@@ -1,318 +1,349 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The main IPython application object
4 The main IPython application object
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10
10
11 Notes
11 Notes
12 -----
12 -----
13 """
13 """
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
17 #
18 # Distributed under the terms of the BSD License. The full license is in
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 # Imports
23 # Imports
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 import os
26 import os
27 import sys
27 import sys
28 import warnings
28 import warnings
29
29
30 from IPython.core.application import Application
30 from IPython.core.application import Application, IPythonArgParseConfigLoader
31 from IPython.core import release
31 from IPython.core import release
32 from IPython.core.iplib import InteractiveShell
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 from IPython.utils.ipstruct import Struct
39 from IPython.utils.ipstruct import Struct
36
40 from IPython.utils.genutils import get_ipython_dir
37
41
38 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
39 # Utilities and helpers
43 # Utilities and helpers
40 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
41
45
42
46
43 ipython_desc = """
47 ipython_desc = """
44 A Python shell with automatic history (input and output), dynamic object
48 A Python shell with automatic history (input and output), dynamic object
45 introspection, easier configuration, command completion, access to the system
49 introspection, easier configuration, command completion, access to the system
46 shell and more.
50 shell and more.
47 """
51 """
48
52
49 def threaded_shell_warning():
53 def threaded_shell_warning():
50 msg = """
54 msg = """
51
55
52 The IPython threaded shells and their associated command line
56 The IPython threaded shells and their associated command line
53 arguments (pylab/wthread/gthread/qthread/q4thread) have been
57 arguments (pylab/wthread/gthread/qthread/q4thread) have been
54 deprecated. See the %gui magic for information on the new interface.
58 deprecated. See the %gui magic for information on the new interface.
55 """
59 """
56 warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
60 warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
57
61
58
62
59 #-----------------------------------------------------------------------------
63 #-----------------------------------------------------------------------------
60 # Main classes and functions
64 # Main classes and functions
61 #-----------------------------------------------------------------------------
65 #-----------------------------------------------------------------------------
62
66
63 cl_args = (
67 cl_args = (
64 (('-autocall',), dict(
68 (('-autocall',), dict(
65 type=int, dest='AUTOCALL', default=NoDefault,
69 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
66 help='Set the autocall value (0,1,2).')
70 help='Set the autocall value (0,1,2).',
71 metavar='InteractiveShell.autocall')
67 ),
72 ),
68 (('-autoindent',), dict(
73 (('-autoindent',), dict(
69 action='store_true', dest='AUTOINDENT', default=NoDefault,
74 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
70 help='Turn on autoindenting.')
75 help='Turn on autoindenting.')
71 ),
76 ),
72 (('-noautoindent',), dict(
77 (('-noautoindent',), dict(
73 action='store_false', dest='AUTOINDENT', default=NoDefault,
78 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
74 help='Turn off autoindenting.')
79 help='Turn off autoindenting.')
75 ),
80 ),
76 (('-automagic',), dict(
81 (('-automagic',), dict(
77 action='store_true', dest='AUTOMAGIC', default=NoDefault,
82 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
78 help='Turn on the auto calling of magic commands.')
83 help='Turn on the auto calling of magic commands.')
79 ),
84 ),
80 (('-noautomagic',), dict(
85 (('-noautomagic',), dict(
81 action='store_false', dest='AUTOMAGIC', default=NoDefault,
86 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
82 help='Turn off the auto calling of magic commands.')
87 help='Turn off the auto calling of magic commands.')
83 ),
88 ),
84 (('-autoedit_syntax',), dict(
89 (('-autoedit_syntax',), dict(
85 action='store_true', dest='AUTOEDIT_SYNTAX', default=NoDefault,
90 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
86 help='Turn on auto editing of files with syntax errors.')
91 help='Turn on auto editing of files with syntax errors.')
87 ),
92 ),
88 (('-noautoedit_syntax',), dict(
93 (('-noautoedit_syntax',), dict(
89 action='store_false', dest='AUTOEDIT_SYNTAX', default=NoDefault,
94 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
90 help='Turn off auto editing of files with syntax errors.')
95 help='Turn off auto editing of files with syntax errors.')
91 ),
96 ),
92 (('-banner',), dict(
97 (('-banner',), dict(
93 action='store_true', dest='DISPLAY_BANNER', default=NoDefault,
98 action='store_true', dest='InteractiveShell.display_banner', default=NoConfigDefault,
94 help='Display a banner upon starting IPython.')
99 help='Display a banner upon starting IPython.')
95 ),
100 ),
96 (('-nobanner',), dict(
101 (('-nobanner',), dict(
97 action='store_false', dest='DISPLAY_BANNER', default=NoDefault,
102 action='store_false', dest='InteractiveShell.display_banner', default=NoConfigDefault,
98 help="Don't display a banner upon starting IPython.")
103 help="Don't display a banner upon starting IPython.")
99 ),
104 ),
100 (('-c',), dict(
105 (('-c',), dict(
101 type=str, dest='C', default=NoDefault,
106 type=str, dest='InteractiveShell.c', default=NoConfigDefault,
102 help="Execute the given command string.")
107 help="Execute the given command string.",
108 metavar='InteractiveShell.c')
103 ),
109 ),
104 (('-cache_size',), dict(
110 (('-cache_size',), dict(
105 type=int, dest='CACHE_SIZE', default=NoDefault,
111 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
106 help="Set the size of the output cache.")
112 help="Set the size of the output cache.",
113 metavar='InteractiveShell.cache_size')
107 ),
114 ),
108 (('-classic',), dict(
115 (('-classic',), dict(
109 action='store_true', dest='CLASSIC', default=NoDefault,
116 action='store_true', dest='Global.classic', default=NoConfigDefault,
110 help="Gives IPython a similar feel to the classic Python prompt.")
117 help="Gives IPython a similar feel to the classic Python prompt.")
111 ),
118 ),
112 (('-colors',), dict(
119 (('-colors',), dict(
113 type=str, dest='COLORS', default=NoDefault,
120 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
114 help="Set the color scheme (NoColor, Linux, and LightBG).")
121 help="Set the color scheme (NoColor, Linux, and LightBG).",
122 metavar='InteractiveShell.colors')
115 ),
123 ),
116 (('-color_info',), dict(
124 (('-color_info',), dict(
117 action='store_true', dest='COLOR_INFO', default=NoDefault,
125 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
118 help="Enable using colors for info related things.")
126 help="Enable using colors for info related things.")
119 ),
127 ),
120 (('-nocolor_info',), dict(
128 (('-nocolor_info',), dict(
121 action='store_false', dest='COLOR_INFO', default=NoDefault,
129 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
122 help="Disable using colors for info related things.")
130 help="Disable using colors for info related things.")
123 ),
131 ),
124 (('-confirm_exit',), dict(
132 (('-confirm_exit',), dict(
125 action='store_true', dest='CONFIRM_EXIT', default=NoDefault,
133 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
126 help="Prompt the user when existing.")
134 help="Prompt the user when existing.")
127 ),
135 ),
128 (('-noconfirm_exit',), dict(
136 (('-noconfirm_exit',), dict(
129 action='store_false', dest='CONFIRM_EXIT', default=NoDefault,
137 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
130 help="Don't prompt the user when existing.")
138 help="Don't prompt the user when existing.")
131 ),
139 ),
132 (('-deep_reload',), dict(
140 (('-deep_reload',), dict(
133 action='store_true', dest='DEEP_RELOAD', default=NoDefault,
141 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
134 help="Enable deep (recursive) reloading by default.")
142 help="Enable deep (recursive) reloading by default.")
135 ),
143 ),
136 (('-nodeep_reload',), dict(
144 (('-nodeep_reload',), dict(
137 action='store_false', dest='DEEP_RELOAD', default=NoDefault,
145 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
138 help="Disable deep (recursive) reloading by default.")
146 help="Disable deep (recursive) reloading by default.")
139 ),
147 ),
140 (('-editor',), dict(
148 (('-editor',), dict(
141 type=str, dest='EDITOR', default=NoDefault,
149 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
142 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).")
150 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
151 metavar='InteractiveShell.editor')
143 ),
152 ),
144 (('-log','-l'), dict(
153 (('-log','-l'), dict(
145 action='store_true', dest='LOGSTART', default=NoDefault,
154 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
146 help="Start logging to the default file (./ipython_log.py).")
155 help="Start logging to the default file (./ipython_log.py).")
147 ),
156 ),
148 (('-logfile','-lf'), dict(
157 (('-logfile','-lf'), dict(
149 type=str, dest='LOGFILE', default=NoDefault,
158 type=str, dest='InteractiveShell.logfile', default=NoConfigDefault,
150 help="Specify the name of your logfile.")
159 help="Specify the name of your logfile.",
160 metavar='InteractiveShell.logfile')
151 ),
161 ),
152 (('-logplay','-lp'), dict(
162 (('-logplay','-lp'), dict(
153 type=str, dest='LOGPLAY', default=NoDefault,
163 type=str, dest='InteractiveShell.logplay', default=NoConfigDefault,
154 help="Re-play a log file and then append to it.")
164 help="Re-play a log file and then append to it.",
165 metavar='InteractiveShell.logplay')
155 ),
166 ),
156 (('-pdb',), dict(
167 (('-pdb',), dict(
157 action='store_true', dest='PDB', default=NoDefault,
168 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
158 help="Enable auto calling the pdb debugger after every exception.")
169 help="Enable auto calling the pdb debugger after every exception.")
159 ),
170 ),
160 (('-nopdb',), dict(
171 (('-nopdb',), dict(
161 action='store_false', dest='PDB', default=NoDefault,
172 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
162 help="Disable auto calling the pdb debugger after every exception.")
173 help="Disable auto calling the pdb debugger after every exception.")
163 ),
174 ),
164 (('-pprint',), dict(
175 (('-pprint',), dict(
165 action='store_true', dest='PPRINT', default=NoDefault,
176 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
166 help="Enable auto pretty printing of results.")
177 help="Enable auto pretty printing of results.")
167 ),
178 ),
168 (('-nopprint',), dict(
179 (('-nopprint',), dict(
169 action='store_false', dest='PPRINT', default=NoDefault,
180 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
170 help="Disable auto auto pretty printing of results.")
181 help="Disable auto auto pretty printing of results.")
171 ),
182 ),
172 (('-prompt_in1','-pi1'), dict(
183 (('-prompt_in1','-pi1'), dict(
173 type=str, dest='PROMPT_IN1', default=NoDefault,
184 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
174 help="Set the main input prompt ('In [\#]: ')")
185 help="Set the main input prompt ('In [\#]: ')",
186 metavar='InteractiveShell.prompt_in1')
175 ),
187 ),
176 (('-prompt_in2','-pi2'), dict(
188 (('-prompt_in2','-pi2'), dict(
177 type=str, dest='PROMPT_IN2', default=NoDefault,
189 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
178 help="Set the secondary input prompt (' .\D.: ')")
190 help="Set the secondary input prompt (' .\D.: ')",
191 metavar='InteractiveShell.prompt_in2')
179 ),
192 ),
180 (('-prompt_out','-po'), dict(
193 (('-prompt_out','-po'), dict(
181 type=str, dest='PROMPT_OUT', default=NoDefault,
194 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
182 help="Set the output prompt ('Out[\#]:')")
195 help="Set the output prompt ('Out[\#]:')",
196 metavar='InteractiveShell.prompt_out')
183 ),
197 ),
184 (('-quick',), dict(
198 (('-quick',), dict(
185 action='store_true', dest='QUICK', default=NoDefault,
199 action='store_true', dest='Global.quick', default=NoConfigDefault,
186 help="Enable quick startup with no config files.")
200 help="Enable quick startup with no config files.")
187 ),
201 ),
188 (('-readline',), dict(
202 (('-readline',), dict(
189 action='store_true', dest='READLINE_USE', default=NoDefault,
203 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
190 help="Enable readline for command line usage.")
204 help="Enable readline for command line usage.")
191 ),
205 ),
192 (('-noreadline',), dict(
206 (('-noreadline',), dict(
193 action='store_false', dest='READLINE_USE', default=NoDefault,
207 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
194 help="Disable readline for command line usage.")
208 help="Disable readline for command line usage.")
195 ),
209 ),
196 (('-screen_length','-sl'), dict(
210 (('-screen_length','-sl'), dict(
197 type=int, dest='SCREEN_LENGTH', default=NoDefault,
211 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
198 help='Number of lines on screen, used to control printing of long strings.')
212 help='Number of lines on screen, used to control printing of long strings.',
213 metavar='InteractiveShell.screen_length')
199 ),
214 ),
200 (('-separate_in','-si'), dict(
215 (('-separate_in','-si'), dict(
201 type=str, dest='SEPARATE_IN', default=NoDefault,
216 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
202 help="Separator before input prompts. Default '\n'.")
217 help="Separator before input prompts. Default '\n'.",
218 metavar='InteractiveShell.separate_in')
203 ),
219 ),
204 (('-separate_out','-so'), dict(
220 (('-separate_out','-so'), dict(
205 type=str, dest='SEPARATE_OUT', default=NoDefault,
221 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
206 help="Separator before output prompts. Default 0 (nothing).")
222 help="Separator before output prompts. Default 0 (nothing).",
223 metavar='InteractiveShell.separate_out')
207 ),
224 ),
208 (('-separate_out2','-so2'), dict(
225 (('-separate_out2','-so2'), dict(
209 type=str, dest='SEPARATE_OUT2', default=NoDefault,
226 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
210 help="Separator after output prompts. Default 0 (nonight).")
227 help="Separator after output prompts. Default 0 (nonight).",
228 metavar='InteractiveShell.separate_out2')
211 ),
229 ),
212 (('-nosep',), dict(
230 (('-nosep',), dict(
213 action='store_true', dest='NOSEP', default=NoDefault,
231 action='store_true', dest='Global.nosep', default=NoConfigDefault,
214 help="Eliminate all spacing between prompts.")
232 help="Eliminate all spacing between prompts.")
215 ),
233 ),
216 (('-term_title',), dict(
234 (('-term_title',), dict(
217 action='store_true', dest='TERM_TITLE', default=NoDefault,
235 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
218 help="Enable auto setting the terminal title.")
236 help="Enable auto setting the terminal title.")
219 ),
237 ),
220 (('-noterm_title',), dict(
238 (('-noterm_title',), dict(
221 action='store_false', dest='TERM_TITLE', default=NoDefault,
239 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
222 help="Disable auto setting the terminal title.")
240 help="Disable auto setting the terminal title.")
223 ),
241 ),
224 (('-xmode',), dict(
242 (('-xmode',), dict(
225 type=str, dest='XMODE', default=NoDefault,
243 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
226 help="Exception mode ('Plain','Context','Verbose')")
244 help="Exception mode ('Plain','Context','Verbose')",
245 metavar='InteractiveShell.xmode')
227 ),
246 ),
228 # These are only here to get the proper deprecation warnings
247 # These are only here to get the proper deprecation warnings
229 (('-pylab','-wthread','-qthread','-q4thread','-gthread'), dict(
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 help="These command line flags are deprecated, see the 'gui' magic.")
250 help="These command line flags are deprecated, see the 'gui' magic.")
232 ),
251 ),
233 )
252 )
234
253
235
254
236 class IPythonAppCLConfigLoader(IPythonArgParseConfigLoader):
255 class IPythonAppCLConfigLoader(IPythonArgParseConfigLoader):
237
256
238 arguments = cl_args
257 arguments = cl_args
239
258
240
259
260 _default_config_file_name = 'ipython_config.py'
261
241 class IPythonApp(Application):
262 class IPythonApp(Application):
242 name = 'ipython'
263 name = 'ipython'
243 config_file_name = 'ipython_config.py'
264 config_file_name = _default_config_file_name
244
265
245 def create_command_line_config(self):
266 def create_command_line_config(self):
246 """Create and return a command line config loader."""
267 """Create and return a command line config loader."""
247 return IPythonAppCLConfigLoader(
268 return IPythonAppCLConfigLoader(
248 description=ipython_desc,
269 description=ipython_desc,
249 version=release.version)
270 version=release.version)
250
271
251 def post_load_command_line_config(self):
272 def post_load_command_line_config(self):
252 """Do actions after loading cl config."""
273 """Do actions after loading cl config."""
253 clc = self.command_line_config
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 # Display the deprecation warnings about threaded shells
276 # Display the deprecation warnings about threaded shells
260 if hasattr(clc, 'THREADED_SHELL'):
277 if hasattr(clc.Global, 'threaded_shell'):
261 threaded_shell_warning()
278 threaded_shell_warning()
262 del clc['THREADED_SHELL']
279 del clc.Global['threaded_shell']
263
280
264 def load_file_config(self):
281 def load_file_config(self):
265 if hasattr(self.command_line_config, 'QUICK'):
282 if hasattr(self.command_line_config.Global, 'quick'):
266 if self.command_line_config.QUICK:
283 if self.command_line_config.Global.quick:
267 self.file_config = Struct()
284 self.file_config = Config()
268 return
285 return
269 super(IPythonApp, self).load_file_config()
286 super(IPythonApp, self).load_file_config()
270
287
271 def post_load_file_config(self):
288 def post_load_file_config(self):
272 """Logic goes here."""
289 """Logic goes here."""
273
290
274 def pre_construct(self):
291 def pre_construct(self):
275 config = self.master_config
292 config = self.master_config
276
293
277 if hasattr(config, 'CLASSIC'):
294 if hasattr(config.Global, 'classic'):
278 if config.CLASSIC:
295 if config.Global.classic:
279 config.QUICK = 1
296 config.InteractiveShell.cache_size = 0
280 config.CACHE_SIZE = 0
297 config.InteractiveShell.pprint = 0
281 config.PPRINT = 0
298 config.InteractiveShell.prompt_in1 = '>>> '
282 config.PROMPT_IN1 = '>>> '
299 config.InteractiveShell.prompt_in2 = '... '
283 config.PROMPT_IN2 = '... '
300 config.InteractiveShell.prompt_out = ''
284 config.PROMPT_OUT = ''
301 config.InteractiveShell.separate_in = \
285 config.SEPARATE_IN = config.SEPARATE_OUT = config.SEPARATE_OUT2 = ''
302 config.InteractiveShell.separate_out = \
286 config.COLORS = 'NoColor'
303 config.InteractiveShell.separate_out2 = ''
287 config.XMODE = 'Plain'
304 config.InteractiveShell.colors = 'NoColor'
305 config.InteractiveShell.xmode = 'Plain'
288
306
289 # All this should be moved to traitlet handlers in InteractiveShell
307 # All this should be moved to traitlet handlers in InteractiveShell
290 # But, currently InteractiveShell doesn't have support for changing
308 # But, currently InteractiveShell doesn't have support for changing
291 # these values at runtime. Once we support that, this should
309 # these values at runtime. Once we support that, this should
292 # be moved there!!!
310 # be moved there!!!
293 if hasattr(config, 'NOSEP'):
311 if hasattr(config.Global, 'nosep'):
294 if config.NOSEP:
312 if config.Global.nosep:
295 config.SEPARATE_IN = config.SEPARATE_OUT = config.SEPARATE_OUT2 = '0'
313 config.InteractiveShell.separate_in = \
314 config.InteractiveShell.separate_out = \
315 config.InteractiveShell.separate_out2 = '0'
296
316
297 def construct(self):
317 def construct(self):
298 # I am a little hesitant to put these into InteractiveShell itself.
318 # I am a little hesitant to put these into InteractiveShell itself.
299 # But that might be the place for them
319 # But that might be the place for them
300 sys.path.insert(0, '')
320 sys.path.insert(0, '')
301 # add personal ipythondir to sys.path so that users can put things in
321 # add personal ipythondir to sys.path so that users can put things in
302 # there for customization
322 # there for customization
303 sys.path.append(os.path.abspath(self.ipythondir))
323 sys.path.append(os.path.abspath(self.ipythondir))
304
324
305 # Create an InteractiveShell instance
325 # Create an InteractiveShell instance
306 self.shell = InteractiveShell(
326 self.shell = InteractiveShell(
307 parent=None,
327 parent=None,
308 config=self.master_config
328 config=self.master_config
309 )
329 )
310 print self.shell
311
330
312 def start_app(self):
331 def start_app(self):
313 self.shell.mainloop()
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 if __name__ == '__main__':
347 if __name__ == '__main__':
317 app = IPythonApp()
348 app = IPythonApp()
318 app.start() No newline at end of file
349 app.start()
@@ -1,2506 +1,2503 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Main IPython Component
3 Main IPython Component
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from __future__ import with_statement
19 from __future__ import with_statement
20
20
21 import __main__
21 import __main__
22 import __builtin__
22 import __builtin__
23 import StringIO
23 import StringIO
24 import bdb
24 import bdb
25 import codeop
25 import codeop
26 import exceptions
26 import exceptions
27 import glob
27 import glob
28 import keyword
28 import keyword
29 import new
29 import new
30 import os
30 import os
31 import re
31 import re
32 import shutil
32 import shutil
33 import string
33 import string
34 import sys
34 import sys
35 import tempfile
35 import tempfile
36 from contextlib import nested
36 from contextlib import nested
37
37
38 from IPython.core import ultratb
38 from IPython.core import ultratb
39 from IPython.core import debugger, oinspect
39 from IPython.core import debugger, oinspect
40 from IPython.core import shadowns
40 from IPython.core import shadowns
41 from IPython.core import history as ipcorehist
41 from IPython.core import history as ipcorehist
42 from IPython.core import prefilter
42 from IPython.core import prefilter
43 from IPython.core.alias import AliasManager
43 from IPython.core.alias import AliasManager
44 from IPython.core.autocall import IPyAutocall
44 from IPython.core.autocall import IPyAutocall
45 from IPython.core.builtin_trap import BuiltinTrap
45 from IPython.core.builtin_trap import BuiltinTrap
46 from IPython.core.display_trap import DisplayTrap
46 from IPython.core.display_trap import DisplayTrap
47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
47 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
48 from IPython.core.logger import Logger
48 from IPython.core.logger import Logger
49 from IPython.core.magic import Magic
49 from IPython.core.magic import Magic
50 from IPython.core.prompts import CachedOutput
50 from IPython.core.prompts import CachedOutput
51 from IPython.core.page import page
51 from IPython.core.page import page
52 from IPython.core.prefilter import PrefilterManager
52 from IPython.core.prefilter import PrefilterManager
53 from IPython.core.component import Component
53 from IPython.core.component import Component
54 from IPython.core.oldusersetup import user_setup
54 from IPython.core.oldusersetup import user_setup
55 from IPython.core.usage import interactive_usage, default_banner
55 from IPython.core.usage import interactive_usage, default_banner
56 from IPython.core.error import TryNext, UsageError
56 from IPython.core.error import TryNext, UsageError
57 from IPython.core.splitinput import split_user_input
57 from IPython.core.splitinput import split_user_input
58
58
59 from IPython.extensions import pickleshare
59 from IPython.extensions import pickleshare
60 from IPython.external.Itpl import ItplNS
60 from IPython.external.Itpl import ItplNS
61 from IPython.lib.backgroundjobs import BackgroundJobManager
61 from IPython.lib.backgroundjobs import BackgroundJobManager
62 from IPython.utils.ipstruct import Struct
62 from IPython.utils.ipstruct import Struct
63 from IPython.utils import PyColorize
63 from IPython.utils import PyColorize
64 from IPython.utils.genutils import *
64 from IPython.utils.genutils import *
65 from IPython.utils.genutils import get_ipython_dir
65 from IPython.utils.strdispatch import StrDispatch
66 from IPython.utils.strdispatch import StrDispatch
66 from IPython.utils.platutils import toggle_set_term_title, set_term_title
67 from IPython.utils.platutils import toggle_set_term_title, set_term_title
67
68
68 # from IPython.utils import growl
69 # from IPython.utils import growl
69 # growl.start("IPython")
70 # growl.start("IPython")
70
71
71 from IPython.utils.traitlets import (
72 from IPython.utils.traitlets import (
72 Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode
73 Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode
73 )
74 )
74
75
75 #-----------------------------------------------------------------------------
76 #-----------------------------------------------------------------------------
76 # Globals
77 # Globals
77 #-----------------------------------------------------------------------------
78 #-----------------------------------------------------------------------------
78
79
79
80
80 # store the builtin raw_input globally, and use this always, in case user code
81 # store the builtin raw_input globally, and use this always, in case user code
81 # overwrites it (like wx.py.PyShell does)
82 # overwrites it (like wx.py.PyShell does)
82 raw_input_original = raw_input
83 raw_input_original = raw_input
83
84
84 # compiled regexps for autoindent management
85 # compiled regexps for autoindent management
85 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86
87
87
88
88 #-----------------------------------------------------------------------------
89 #-----------------------------------------------------------------------------
89 # Utilities
90 # Utilities
90 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
91
92
92
93
93 ini_spaces_re = re.compile(r'^(\s+)')
94 ini_spaces_re = re.compile(r'^(\s+)')
94
95
95
96
96 def num_ini_spaces(strng):
97 def num_ini_spaces(strng):
97 """Return the number of initial spaces in a string"""
98 """Return the number of initial spaces in a string"""
98
99
99 ini_spaces = ini_spaces_re.match(strng)
100 ini_spaces = ini_spaces_re.match(strng)
100 if ini_spaces:
101 if ini_spaces:
101 return ini_spaces.end()
102 return ini_spaces.end()
102 else:
103 else:
103 return 0
104 return 0
104
105
105
106
106 def softspace(file, newvalue):
107 def softspace(file, newvalue):
107 """Copied from code.py, to remove the dependency"""
108 """Copied from code.py, to remove the dependency"""
108
109
109 oldvalue = 0
110 oldvalue = 0
110 try:
111 try:
111 oldvalue = file.softspace
112 oldvalue = file.softspace
112 except AttributeError:
113 except AttributeError:
113 pass
114 pass
114 try:
115 try:
115 file.softspace = newvalue
116 file.softspace = newvalue
116 except (AttributeError, TypeError):
117 except (AttributeError, TypeError):
117 # "attribute-less object" or "read-only attributes"
118 # "attribute-less object" or "read-only attributes"
118 pass
119 pass
119 return oldvalue
120 return oldvalue
120
121
121
122
122 class SpaceInInput(exceptions.Exception): pass
123 class SpaceInInput(exceptions.Exception): pass
123
124
124 class Bunch: pass
125 class Bunch: pass
125
126
126 class InputList(list):
127 class InputList(list):
127 """Class to store user input.
128 """Class to store user input.
128
129
129 It's basically a list, but slices return a string instead of a list, thus
130 It's basically a list, but slices return a string instead of a list, thus
130 allowing things like (assuming 'In' is an instance):
131 allowing things like (assuming 'In' is an instance):
131
132
132 exec In[4:7]
133 exec In[4:7]
133
134
134 or
135 or
135
136
136 exec In[5:9] + In[14] + In[21:25]"""
137 exec In[5:9] + In[14] + In[21:25]"""
137
138
138 def __getslice__(self,i,j):
139 def __getslice__(self,i,j):
139 return ''.join(list.__getslice__(self,i,j))
140 return ''.join(list.__getslice__(self,i,j))
140
141
141
142
142 class SyntaxTB(ultratb.ListTB):
143 class SyntaxTB(ultratb.ListTB):
143 """Extension which holds some state: the last exception value"""
144 """Extension which holds some state: the last exception value"""
144
145
145 def __init__(self,color_scheme = 'NoColor'):
146 def __init__(self,color_scheme = 'NoColor'):
146 ultratb.ListTB.__init__(self,color_scheme)
147 ultratb.ListTB.__init__(self,color_scheme)
147 self.last_syntax_error = None
148 self.last_syntax_error = None
148
149
149 def __call__(self, etype, value, elist):
150 def __call__(self, etype, value, elist):
150 self.last_syntax_error = value
151 self.last_syntax_error = value
151 ultratb.ListTB.__call__(self,etype,value,elist)
152 ultratb.ListTB.__call__(self,etype,value,elist)
152
153
153 def clear_err_state(self):
154 def clear_err_state(self):
154 """Return the current error state and clear it"""
155 """Return the current error state and clear it"""
155 e = self.last_syntax_error
156 e = self.last_syntax_error
156 self.last_syntax_error = None
157 self.last_syntax_error = None
157 return e
158 return e
158
159
159
160
160 def get_default_editor():
161 def get_default_editor():
161 try:
162 try:
162 ed = os.environ['EDITOR']
163 ed = os.environ['EDITOR']
163 except KeyError:
164 except KeyError:
164 if os.name == 'posix':
165 if os.name == 'posix':
165 ed = 'vi' # the only one guaranteed to be there!
166 ed = 'vi' # the only one guaranteed to be there!
166 else:
167 else:
167 ed = 'notepad' # same in Windows!
168 ed = 'notepad' # same in Windows!
168 return ed
169 return ed
169
170
170
171
171 class SeparateStr(Str):
172 class SeparateStr(Str):
172 """A Str subclass to validate separate_in, separate_out, etc.
173 """A Str subclass to validate separate_in, separate_out, etc.
173
174
174 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
175 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
175 """
176 """
176
177
177 def validate(self, obj, value):
178 def validate(self, obj, value):
178 if value == '0': value = ''
179 if value == '0': value = ''
179 value = value.replace('\\n','\n')
180 value = value.replace('\\n','\n')
180 return super(SeparateStr, self).validate(obj, value)
181 return super(SeparateStr, self).validate(obj, value)
181
182
182
183
183 #-----------------------------------------------------------------------------
184 #-----------------------------------------------------------------------------
184 # Main IPython class
185 # Main IPython class
185 #-----------------------------------------------------------------------------
186 #-----------------------------------------------------------------------------
186
187
187
188
188 class InteractiveShell(Component, Magic):
189 class InteractiveShell(Component, Magic):
189 """An enhanced, interactive shell for Python."""
190 """An enhanced, interactive shell for Python."""
190
191
191 autocall = Enum((0,1,2), config_key='AUTOCALL')
192 autocall = Enum((0,1,2), config=True)
192 autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX')
193 autoedit_syntax = CBool(False, config=True)
193 autoindent = CBool(True, config_key='AUTOINDENT')
194 autoindent = CBool(True, config=True)
194 automagic = CBool(True, config_key='AUTOMAGIC')
195 automagic = CBool(True, config=True)
195 display_banner = CBool(True, config_key='DISPLAY_BANNER')
196 display_banner = CBool(True, config=True)
196 banner = Str('')
197 banner = Str('')
197 banner1 = Str(default_banner, config_key='BANNER1')
198 banner1 = Str(default_banner, config=True)
198 banner2 = Str('', config_key='BANNER2')
199 banner2 = Str('', config=True)
199 c = Str('', config_key='C')
200 c = Str('', config=True)
200 cache_size = Int(1000, config_key='CACHE_SIZE')
201 cache_size = Int(1000, config=True)
201 classic = CBool(False, config_key='CLASSIC')
202 color_info = CBool(True, config=True)
202 color_info = CBool(True, config_key='COLOR_INFO')
203 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
203 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
204 default_value='LightBG', config_key='COLORS')
204 default_value='LightBG', config=True)
205 confirm_exit = CBool(True, config_key='CONFIRM_EXIT')
205 confirm_exit = CBool(True, config=True)
206 debug = CBool(False, config_key='DEBUG')
206 debug = CBool(False, config=True)
207 deep_reload = CBool(False, config_key='DEEP_RELOAD')
207 deep_reload = CBool(False, config=True)
208 embedded = CBool(False)
208 embedded = CBool(False)
209 embedded_active = CBool(False)
209 embedded_active = CBool(False)
210 editor = Str(get_default_editor(), config_key='EDITOR')
210 editor = Str(get_default_editor(), config=True)
211 filename = Str("<ipython console>")
211 filename = Str("<ipython console>")
212 interactive = CBool(False, config_key='INTERACTIVE')
212 interactive = CBool(False, config=True)
213 ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__
213 ipythondir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
214 logstart = CBool(False, config_key='LOGSTART')
214 logstart = CBool(False, config=True)
215 logfile = Str('', config_key='LOGFILE')
215 logfile = Str('', config=True)
216 logplay = Str('', config_key='LOGPLAY')
216 logplay = Str('', config=True)
217 object_info_string_level = Enum((0,1,2), default_value=0,
217 object_info_string_level = Enum((0,1,2), default_value=0,
218 config_keys='OBJECT_INFO_STRING_LEVEL')
218 config=True)
219 pager = Str('less', config_key='PAGER')
219 pager = Str('less', config=True)
220 pdb = CBool(False, config_key='PDB')
220 pdb = CBool(False, config=True)
221 pprint = CBool(True, config_key='PPRINT')
221 pprint = CBool(True, config=True)
222 profile = Str('', config_key='PROFILE')
222 profile = Str('', config=True)
223 prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1')
223 prompt_in1 = Str('In [\\#]: ', config=True)
224 prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2')
224 prompt_in2 = Str(' .\\D.: ', config=True)
225 prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1')
225 prompt_out = Str('Out[\\#]: ', config=True)
226 prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT')
226 prompts_pad_left = CBool(True, config=True)
227 quiet = CBool(False, config_key='QUIET')
227 quiet = CBool(False, config=True)
228
228
229 readline_use = CBool(True, config_key='READLINE_USE')
229 readline_use = CBool(True, config=True)
230 readline_merge_completions = CBool(True,
230 readline_merge_completions = CBool(True, config=True)
231 config_key='READLINE_MERGE_COMPLETIONS')
231 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
232 readline_omit__names = Enum((0,1,2), default_value=0,
232 readline_remove_delims = Str('-/~', config=True)
233 config_key='READLINE_OMIT_NAMES')
234 readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS')
235 readline_parse_and_bind = List([
233 readline_parse_and_bind = List([
236 'tab: complete',
234 'tab: complete',
237 '"\C-l": possible-completions',
235 '"\C-l": possible-completions',
238 'set show-all-if-ambiguous on',
236 'set show-all-if-ambiguous on',
239 '"\C-o": tab-insert',
237 '"\C-o": tab-insert',
240 '"\M-i": " "',
238 '"\M-i": " "',
241 '"\M-o": "\d\d\d\d"',
239 '"\M-o": "\d\d\d\d"',
242 '"\M-I": "\d\d\d\d"',
240 '"\M-I": "\d\d\d\d"',
243 '"\C-r": reverse-search-history',
241 '"\C-r": reverse-search-history',
244 '"\C-s": forward-search-history',
242 '"\C-s": forward-search-history',
245 '"\C-p": history-search-backward',
243 '"\C-p": history-search-backward',
246 '"\C-n": history-search-forward',
244 '"\C-n": history-search-forward',
247 '"\e[A": history-search-backward',
245 '"\e[A": history-search-backward',
248 '"\e[B": history-search-forward',
246 '"\e[B": history-search-forward',
249 '"\C-k": kill-line',
247 '"\C-k": kill-line',
250 '"\C-u": unix-line-discard',
248 '"\C-u": unix-line-discard',
251 ], allow_none=False, config_key='READLINE_PARSE_AND_BIND'
249 ], allow_none=False, config=True)
252 )
253
250
254 screen_length = Int(0, config_key='SCREEN_LENGTH')
251 screen_length = Int(0, config=True)
255
252
256 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
253 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
257 separate_in = SeparateStr('\n', config_key='SEPARATE_IN')
254 separate_in = SeparateStr('\n', config=True)
258 separate_out = SeparateStr('', config_key='SEPARATE_OUT')
255 separate_out = SeparateStr('', config=True)
259 separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2')
256 separate_out2 = SeparateStr('', config=True)
260
257
261 system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER')
258 system_header = Str('IPython system call: ', config=True)
262 system_verbose = CBool(False, config_key='SYSTEM_VERBOSE')
259 system_verbose = CBool(False, config=True)
263 term_title = CBool(False, config_key='TERM_TITLE')
260 term_title = CBool(False, config=True)
264 wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE')
261 wildcards_case_sensitive = CBool(True, config=True)
265 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
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 autoexec = List(allow_none=False)
265 autoexec = List(allow_none=False)
270
266
271 # class attribute to indicate whether the class supports threads or not.
267 # class attribute to indicate whether the class supports threads or not.
272 # Subclasses with thread support should override this as needed.
268 # Subclasses with thread support should override this as needed.
273 isthreaded = False
269 isthreaded = False
274
270
275 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
271 def __init__(self, parent=None, config=None, ipythondir=None, usage=None,
276 user_ns=None, user_global_ns=None,
272 user_ns=None, user_global_ns=None,
277 banner1=None, banner2=None,
273 banner1=None, banner2=None,
278 custom_exceptions=((),None)):
274 custom_exceptions=((),None)):
279
275
280 # This is where traitlets with a config_key argument are updated
276 # This is where traitlets with a config_key argument are updated
281 # from the values on config.
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 # These are relatively independent and stateless
280 # These are relatively independent and stateless
285 self.init_ipythondir(ipythondir)
281 self.init_ipythondir(ipythondir)
286 self.init_instance_attrs()
282 self.init_instance_attrs()
287 self.init_term_title()
283 self.init_term_title()
288 self.init_usage(usage)
284 self.init_usage(usage)
289 self.init_banner(banner1, banner2)
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 self.init_create_namespaces(user_ns, user_global_ns)
288 self.init_create_namespaces(user_ns, user_global_ns)
293 # This has to be done after init_create_namespaces because it uses
289 # This has to be done after init_create_namespaces because it uses
294 # something in self.user_ns, but before init_sys_modules, which
290 # something in self.user_ns, but before init_sys_modules, which
295 # is the first thing to modify sys.
291 # is the first thing to modify sys.
296 self.save_sys_module_state()
292 self.save_sys_module_state()
297 self.init_sys_modules()
293 self.init_sys_modules()
298
294
299 self.init_history()
295 self.init_history()
300 self.init_encoding()
296 self.init_encoding()
301 self.init_prefilter()
297 self.init_prefilter()
302
298
303 Magic.__init__(self, self)
299 Magic.__init__(self, self)
304
300
305 self.init_syntax_highlighting()
301 self.init_syntax_highlighting()
306 self.init_hooks()
302 self.init_hooks()
307 self.init_pushd_popd_magic()
303 self.init_pushd_popd_magic()
308 self.init_traceback_handlers(custom_exceptions)
304 self.init_traceback_handlers(custom_exceptions)
309 self.init_user_ns()
305 self.init_user_ns()
310 self.init_logger()
306 self.init_logger()
311 self.init_alias()
307 self.init_alias()
312 self.init_builtins()
308 self.init_builtins()
313
309
314 # pre_config_initialization
310 # pre_config_initialization
315 self.init_shadow_hist()
311 self.init_shadow_hist()
316
312
317 # The next section should contain averything that was in ipmaker.
313 # The next section should contain averything that was in ipmaker.
318 self.init_logstart()
314 self.init_logstart()
319
315
320 # The following was in post_config_initialization
316 # The following was in post_config_initialization
321 self.init_inspector()
317 self.init_inspector()
322 self.init_readline()
318 self.init_readline()
323 self.init_prompts()
319 self.init_prompts()
324 self.init_displayhook()
320 self.init_displayhook()
325 self.init_reload_doctest()
321 self.init_reload_doctest()
326 self.init_magics()
322 self.init_magics()
327 self.init_pdb()
323 self.init_pdb()
328 self.hooks.late_startup_hook()
324 self.hooks.late_startup_hook()
329
325
326 def get_ipython(self):
327 return self
328
330 #-------------------------------------------------------------------------
329 #-------------------------------------------------------------------------
331 # Traitlet changed handlers
330 # Traitlet changed handlers
332 #-------------------------------------------------------------------------
331 #-------------------------------------------------------------------------
333
332
334 def _banner1_changed(self):
333 def _banner1_changed(self):
335 self.compute_banner()
334 self.compute_banner()
336
335
337 def _banner2_changed(self):
336 def _banner2_changed(self):
338 self.compute_banner()
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 @property
343 @property
341 def usable_screen_length(self):
344 def usable_screen_length(self):
342 if self.screen_length == 0:
345 if self.screen_length == 0:
343 return 0
346 return 0
344 else:
347 else:
345 num_lines_bot = self.separate_in.count('\n')+1
348 num_lines_bot = self.separate_in.count('\n')+1
346 return self.screen_length - num_lines_bot
349 return self.screen_length - num_lines_bot
347
350
348 def _term_title_changed(self, name, new_value):
351 def _term_title_changed(self, name, new_value):
349 self.init_term_title()
352 self.init_term_title()
350
353
351 def set_autoindent(self,value=None):
354 def set_autoindent(self,value=None):
352 """Set the autoindent flag, checking for readline support.
355 """Set the autoindent flag, checking for readline support.
353
356
354 If called with no arguments, it acts as a toggle."""
357 If called with no arguments, it acts as a toggle."""
355
358
356 if not self.has_readline:
359 if not self.has_readline:
357 if os.name == 'posix':
360 if os.name == 'posix':
358 warn("The auto-indent feature requires the readline library")
361 warn("The auto-indent feature requires the readline library")
359 self.autoindent = 0
362 self.autoindent = 0
360 return
363 return
361 if value is None:
364 if value is None:
362 self.autoindent = not self.autoindent
365 self.autoindent = not self.autoindent
363 else:
366 else:
364 self.autoindent = value
367 self.autoindent = value
365
368
366 #-------------------------------------------------------------------------
369 #-------------------------------------------------------------------------
367 # init_* methods called by __init__
370 # init_* methods called by __init__
368 #-------------------------------------------------------------------------
371 #-------------------------------------------------------------------------
369
372
370 def init_ipythondir(self, ipythondir):
373 def init_ipythondir(self, ipythondir):
371 if ipythondir is not None:
374 if ipythondir is not None:
372 self.ipythondir = ipythondir
375 self.ipythondir = ipythondir
373 self.config.IPYTHONDIR = self.ipythondir
376 self.config.Global.ipythondir = self.ipythondir
374 return
377 return
375
378
376 if hasattr(self.config, 'IPYTHONDIR'):
379 if hasattr(self.config.Global, 'ipythondir'):
377 self.ipythondir = self.config.IPYTHONDIR
380 self.ipythondir = self.config.Global.ipythondir
378 if not hasattr(self.config, 'IPYTHONDIR'):
381 else:
379 # cdw is always defined
382 self.ipythondir = get_ipython_dir()
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)
386
383
387 # All children can just read this
384 # All children can just read this
388 self.config.IPYTHONDIR = self.ipythondir
385 self.config.Global.ipythondir = self.ipythondir
389
386
390 def init_instance_attrs(self):
387 def init_instance_attrs(self):
391 self.jobs = BackgroundJobManager()
388 self.jobs = BackgroundJobManager()
392 self.more = False
389 self.more = False
393
390
394 # command compiler
391 # command compiler
395 self.compile = codeop.CommandCompiler()
392 self.compile = codeop.CommandCompiler()
396
393
397 # User input buffer
394 # User input buffer
398 self.buffer = []
395 self.buffer = []
399
396
400 # Make an empty namespace, which extension writers can rely on both
397 # Make an empty namespace, which extension writers can rely on both
401 # existing and NEVER being used by ipython itself. This gives them a
398 # existing and NEVER being used by ipython itself. This gives them a
402 # convenient location for storing additional information and state
399 # convenient location for storing additional information and state
403 # their extensions may require, without fear of collisions with other
400 # their extensions may require, without fear of collisions with other
404 # ipython names that may develop later.
401 # ipython names that may develop later.
405 self.meta = Struct()
402 self.meta = Struct()
406
403
407 # Object variable to store code object waiting execution. This is
404 # Object variable to store code object waiting execution. This is
408 # used mainly by the multithreaded shells, but it can come in handy in
405 # used mainly by the multithreaded shells, but it can come in handy in
409 # other situations. No need to use a Queue here, since it's a single
406 # other situations. No need to use a Queue here, since it's a single
410 # item which gets cleared once run.
407 # item which gets cleared once run.
411 self.code_to_run = None
408 self.code_to_run = None
412
409
413 # Flag to mark unconditional exit
410 # Flag to mark unconditional exit
414 self.exit_now = False
411 self.exit_now = False
415
412
416 # Temporary files used for various purposes. Deleted at exit.
413 # Temporary files used for various purposes. Deleted at exit.
417 self.tempfiles = []
414 self.tempfiles = []
418
415
419 # Keep track of readline usage (later set by init_readline)
416 # Keep track of readline usage (later set by init_readline)
420 self.has_readline = False
417 self.has_readline = False
421
418
422 # keep track of where we started running (mainly for crash post-mortem)
419 # keep track of where we started running (mainly for crash post-mortem)
423 # This is not being used anywhere currently.
420 # This is not being used anywhere currently.
424 self.starting_dir = os.getcwd()
421 self.starting_dir = os.getcwd()
425
422
426 # Indentation management
423 # Indentation management
427 self.indent_current_nsp = 0
424 self.indent_current_nsp = 0
428
425
429 def init_term_title(self):
426 def init_term_title(self):
430 # Enable or disable the terminal title.
427 # Enable or disable the terminal title.
431 if self.term_title:
428 if self.term_title:
432 toggle_set_term_title(True)
429 toggle_set_term_title(True)
433 set_term_title('IPython: ' + abbrev_cwd())
430 set_term_title('IPython: ' + abbrev_cwd())
434 else:
431 else:
435 toggle_set_term_title(False)
432 toggle_set_term_title(False)
436
433
437 def init_usage(self, usage=None):
434 def init_usage(self, usage=None):
438 if usage is None:
435 if usage is None:
439 self.usage = interactive_usage
436 self.usage = interactive_usage
440 else:
437 else:
441 self.usage = usage
438 self.usage = usage
442
439
443 def init_banner(self, banner1, banner2):
440 def init_banner(self, banner1, banner2):
444 if self.c: # regular python doesn't print the banner with -c
441 if self.c: # regular python doesn't print the banner with -c
445 self.display_banner = False
442 self.display_banner = False
446 if banner1 is not None:
443 if banner1 is not None:
447 self.banner1 = banner1
444 self.banner1 = banner1
448 if banner2 is not None:
445 if banner2 is not None:
449 self.banner2 = banner2
446 self.banner2 = banner2
450 self.compute_banner()
447 self.compute_banner()
451
448
452 def compute_banner(self):
449 def compute_banner(self):
453 self.banner = self.banner1 + '\n'
450 self.banner = self.banner1 + '\n'
454 if self.profile:
451 if self.profile:
455 self.banner += '\nIPython profile: %s\n' % self.profile
452 self.banner += '\nIPython profile: %s\n' % self.profile
456 if self.banner2:
453 if self.banner2:
457 self.banner += '\n' + self.banner2 + '\n'
454 self.banner += '\n' + self.banner2 + '\n'
458
455
459 def init_encoding(self):
456 def init_encoding(self):
460 # Get system encoding at startup time. Certain terminals (like Emacs
457 # Get system encoding at startup time. Certain terminals (like Emacs
461 # under Win32 have it set to None, and we need to have a known valid
458 # under Win32 have it set to None, and we need to have a known valid
462 # encoding to use in the raw_input() method
459 # encoding to use in the raw_input() method
463 try:
460 try:
464 self.stdin_encoding = sys.stdin.encoding or 'ascii'
461 self.stdin_encoding = sys.stdin.encoding or 'ascii'
465 except AttributeError:
462 except AttributeError:
466 self.stdin_encoding = 'ascii'
463 self.stdin_encoding = 'ascii'
467
464
468 def init_syntax_highlighting(self):
465 def init_syntax_highlighting(self):
469 # Python source parser/formatter for syntax highlighting
466 # Python source parser/formatter for syntax highlighting
470 pyformat = PyColorize.Parser().format
467 pyformat = PyColorize.Parser().format
471 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
468 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
472
469
473 def init_pushd_popd_magic(self):
470 def init_pushd_popd_magic(self):
474 # for pushd/popd management
471 # for pushd/popd management
475 try:
472 try:
476 self.home_dir = get_home_dir()
473 self.home_dir = get_home_dir()
477 except HomeDirError, msg:
474 except HomeDirError, msg:
478 fatal(msg)
475 fatal(msg)
479
476
480 self.dir_stack = []
477 self.dir_stack = []
481
478
482 def init_logger(self):
479 def init_logger(self):
483 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
480 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
484 # local shortcut, this is used a LOT
481 # local shortcut, this is used a LOT
485 self.log = self.logger.log
482 self.log = self.logger.log
486 # template for logfile headers. It gets resolved at runtime by the
483 # template for logfile headers. It gets resolved at runtime by the
487 # logstart method.
484 # logstart method.
488 self.loghead_tpl = \
485 self.loghead_tpl = \
489 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
486 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
490 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
487 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
491 #log# opts = %s
488 #log# opts = %s
492 #log# args = %s
489 #log# args = %s
493 #log# It is safe to make manual edits below here.
490 #log# It is safe to make manual edits below here.
494 #log#-----------------------------------------------------------------------
491 #log#-----------------------------------------------------------------------
495 """
492 """
496
493
497 def init_logstart(self):
494 def init_logstart(self):
498 if self.logplay:
495 if self.logplay:
499 self.magic_logstart(self.logplay + ' append')
496 self.magic_logstart(self.logplay + ' append')
500 elif self.logfile:
497 elif self.logfile:
501 self.magic_logstart(self.logfile)
498 self.magic_logstart(self.logfile)
502 elif self.logstart:
499 elif self.logstart:
503 self.magic_logstart()
500 self.magic_logstart()
504
501
505 def init_builtins(self):
502 def init_builtins(self):
506 self.builtin_trap = BuiltinTrap(self)
503 self.builtin_trap = BuiltinTrap(self)
507
504
508 def init_inspector(self):
505 def init_inspector(self):
509 # Object inspector
506 # Object inspector
510 self.inspector = oinspect.Inspector(oinspect.InspectColors,
507 self.inspector = oinspect.Inspector(oinspect.InspectColors,
511 PyColorize.ANSICodeColors,
508 PyColorize.ANSICodeColors,
512 'NoColor',
509 'NoColor',
513 self.object_info_string_level)
510 self.object_info_string_level)
514
511
515 def init_prompts(self):
512 def init_prompts(self):
516 # Initialize cache, set in/out prompts and printing system
513 # Initialize cache, set in/out prompts and printing system
517 self.outputcache = CachedOutput(self,
514 self.outputcache = CachedOutput(self,
518 self.cache_size,
515 self.cache_size,
519 self.pprint,
516 self.pprint,
520 input_sep = self.separate_in,
517 input_sep = self.separate_in,
521 output_sep = self.separate_out,
518 output_sep = self.separate_out,
522 output_sep2 = self.separate_out2,
519 output_sep2 = self.separate_out2,
523 ps1 = self.prompt_in1,
520 ps1 = self.prompt_in1,
524 ps2 = self.prompt_in2,
521 ps2 = self.prompt_in2,
525 ps_out = self.prompt_out,
522 ps_out = self.prompt_out,
526 pad_left = self.prompts_pad_left)
523 pad_left = self.prompts_pad_left)
527
524
528 # user may have over-ridden the default print hook:
525 # user may have over-ridden the default print hook:
529 try:
526 try:
530 self.outputcache.__class__.display = self.hooks.display
527 self.outputcache.__class__.display = self.hooks.display
531 except AttributeError:
528 except AttributeError:
532 pass
529 pass
533
530
534 def init_displayhook(self):
531 def init_displayhook(self):
535 self.display_trap = DisplayTrap(self, self.outputcache)
532 self.display_trap = DisplayTrap(self, self.outputcache)
536
533
537 def init_reload_doctest(self):
534 def init_reload_doctest(self):
538 # Do a proper resetting of doctest, including the necessary displayhook
535 # Do a proper resetting of doctest, including the necessary displayhook
539 # monkeypatching
536 # monkeypatching
540 try:
537 try:
541 doctest_reload()
538 doctest_reload()
542 except ImportError:
539 except ImportError:
543 warn("doctest module does not exist.")
540 warn("doctest module does not exist.")
544
541
545 #-------------------------------------------------------------------------
542 #-------------------------------------------------------------------------
546 # Things related to injections into the sys module
543 # Things related to injections into the sys module
547 #-------------------------------------------------------------------------
544 #-------------------------------------------------------------------------
548
545
549 def save_sys_module_state(self):
546 def save_sys_module_state(self):
550 """Save the state of hooks in the sys module.
547 """Save the state of hooks in the sys module.
551
548
552 This has to be called after self.user_ns is created.
549 This has to be called after self.user_ns is created.
553 """
550 """
554 self._orig_sys_module_state = {}
551 self._orig_sys_module_state = {}
555 self._orig_sys_module_state['stdin'] = sys.stdin
552 self._orig_sys_module_state['stdin'] = sys.stdin
556 self._orig_sys_module_state['stdout'] = sys.stdout
553 self._orig_sys_module_state['stdout'] = sys.stdout
557 self._orig_sys_module_state['stderr'] = sys.stderr
554 self._orig_sys_module_state['stderr'] = sys.stderr
558 self._orig_sys_module_state['excepthook'] = sys.excepthook
555 self._orig_sys_module_state['excepthook'] = sys.excepthook
559 try:
556 try:
560 self._orig_sys_modules_main_name = self.user_ns['__name__']
557 self._orig_sys_modules_main_name = self.user_ns['__name__']
561 except KeyError:
558 except KeyError:
562 pass
559 pass
563
560
564 def restore_sys_module_state(self):
561 def restore_sys_module_state(self):
565 """Restore the state of the sys module."""
562 """Restore the state of the sys module."""
566 try:
563 try:
567 for k, v in self._orig_sys_module_state.items():
564 for k, v in self._orig_sys_module_state.items():
568 setattr(sys, k, v)
565 setattr(sys, k, v)
569 except AttributeError:
566 except AttributeError:
570 pass
567 pass
571 try:
568 try:
572 delattr(sys, 'ipcompleter')
569 delattr(sys, 'ipcompleter')
573 except AttributeError:
570 except AttributeError:
574 pass
571 pass
575 # Reset what what done in self.init_sys_modules
572 # Reset what what done in self.init_sys_modules
576 try:
573 try:
577 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
574 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
578 except (AttributeError, KeyError):
575 except (AttributeError, KeyError):
579 pass
576 pass
580
577
581 #-------------------------------------------------------------------------
578 #-------------------------------------------------------------------------
582 # Things related to hooks
579 # Things related to hooks
583 #-------------------------------------------------------------------------
580 #-------------------------------------------------------------------------
584
581
585 def init_hooks(self):
582 def init_hooks(self):
586 # hooks holds pointers used for user-side customizations
583 # hooks holds pointers used for user-side customizations
587 self.hooks = Struct()
584 self.hooks = Struct()
588
585
589 self.strdispatchers = {}
586 self.strdispatchers = {}
590
587
591 # Set all default hooks, defined in the IPython.hooks module.
588 # Set all default hooks, defined in the IPython.hooks module.
592 import IPython.core.hooks
589 import IPython.core.hooks
593 hooks = IPython.core.hooks
590 hooks = IPython.core.hooks
594 for hook_name in hooks.__all__:
591 for hook_name in hooks.__all__:
595 # default hooks have priority 100, i.e. low; user hooks should have
592 # default hooks have priority 100, i.e. low; user hooks should have
596 # 0-100 priority
593 # 0-100 priority
597 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
594 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
598
595
599 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
596 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
600 """set_hook(name,hook) -> sets an internal IPython hook.
597 """set_hook(name,hook) -> sets an internal IPython hook.
601
598
602 IPython exposes some of its internal API as user-modifiable hooks. By
599 IPython exposes some of its internal API as user-modifiable hooks. By
603 adding your function to one of these hooks, you can modify IPython's
600 adding your function to one of these hooks, you can modify IPython's
604 behavior to call at runtime your own routines."""
601 behavior to call at runtime your own routines."""
605
602
606 # At some point in the future, this should validate the hook before it
603 # At some point in the future, this should validate the hook before it
607 # accepts it. Probably at least check that the hook takes the number
604 # accepts it. Probably at least check that the hook takes the number
608 # of args it's supposed to.
605 # of args it's supposed to.
609
606
610 f = new.instancemethod(hook,self,self.__class__)
607 f = new.instancemethod(hook,self,self.__class__)
611
608
612 # check if the hook is for strdispatcher first
609 # check if the hook is for strdispatcher first
613 if str_key is not None:
610 if str_key is not None:
614 sdp = self.strdispatchers.get(name, StrDispatch())
611 sdp = self.strdispatchers.get(name, StrDispatch())
615 sdp.add_s(str_key, f, priority )
612 sdp.add_s(str_key, f, priority )
616 self.strdispatchers[name] = sdp
613 self.strdispatchers[name] = sdp
617 return
614 return
618 if re_key is not None:
615 if re_key is not None:
619 sdp = self.strdispatchers.get(name, StrDispatch())
616 sdp = self.strdispatchers.get(name, StrDispatch())
620 sdp.add_re(re.compile(re_key), f, priority )
617 sdp.add_re(re.compile(re_key), f, priority )
621 self.strdispatchers[name] = sdp
618 self.strdispatchers[name] = sdp
622 return
619 return
623
620
624 dp = getattr(self.hooks, name, None)
621 dp = getattr(self.hooks, name, None)
625 if name not in IPython.core.hooks.__all__:
622 if name not in IPython.core.hooks.__all__:
626 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
623 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
627 if not dp:
624 if not dp:
628 dp = IPython.core.hooks.CommandChainDispatcher()
625 dp = IPython.core.hooks.CommandChainDispatcher()
629
626
630 try:
627 try:
631 dp.add(f,priority)
628 dp.add(f,priority)
632 except AttributeError:
629 except AttributeError:
633 # it was not commandchain, plain old func - replace
630 # it was not commandchain, plain old func - replace
634 dp = f
631 dp = f
635
632
636 setattr(self.hooks,name, dp)
633 setattr(self.hooks,name, dp)
637
634
638 #-------------------------------------------------------------------------
635 #-------------------------------------------------------------------------
639 # Things related to the "main" module
636 # Things related to the "main" module
640 #-------------------------------------------------------------------------
637 #-------------------------------------------------------------------------
641
638
642 def new_main_mod(self,ns=None):
639 def new_main_mod(self,ns=None):
643 """Return a new 'main' module object for user code execution.
640 """Return a new 'main' module object for user code execution.
644 """
641 """
645 main_mod = self._user_main_module
642 main_mod = self._user_main_module
646 init_fakemod_dict(main_mod,ns)
643 init_fakemod_dict(main_mod,ns)
647 return main_mod
644 return main_mod
648
645
649 def cache_main_mod(self,ns,fname):
646 def cache_main_mod(self,ns,fname):
650 """Cache a main module's namespace.
647 """Cache a main module's namespace.
651
648
652 When scripts are executed via %run, we must keep a reference to the
649 When scripts are executed via %run, we must keep a reference to the
653 namespace of their __main__ module (a FakeModule instance) around so
650 namespace of their __main__ module (a FakeModule instance) around so
654 that Python doesn't clear it, rendering objects defined therein
651 that Python doesn't clear it, rendering objects defined therein
655 useless.
652 useless.
656
653
657 This method keeps said reference in a private dict, keyed by the
654 This method keeps said reference in a private dict, keyed by the
658 absolute path of the module object (which corresponds to the script
655 absolute path of the module object (which corresponds to the script
659 path). This way, for multiple executions of the same script we only
656 path). This way, for multiple executions of the same script we only
660 keep one copy of the namespace (the last one), thus preventing memory
657 keep one copy of the namespace (the last one), thus preventing memory
661 leaks from old references while allowing the objects from the last
658 leaks from old references while allowing the objects from the last
662 execution to be accessible.
659 execution to be accessible.
663
660
664 Note: we can not allow the actual FakeModule instances to be deleted,
661 Note: we can not allow the actual FakeModule instances to be deleted,
665 because of how Python tears down modules (it hard-sets all their
662 because of how Python tears down modules (it hard-sets all their
666 references to None without regard for reference counts). This method
663 references to None without regard for reference counts). This method
667 must therefore make a *copy* of the given namespace, to allow the
664 must therefore make a *copy* of the given namespace, to allow the
668 original module's __dict__ to be cleared and reused.
665 original module's __dict__ to be cleared and reused.
669
666
670
667
671 Parameters
668 Parameters
672 ----------
669 ----------
673 ns : a namespace (a dict, typically)
670 ns : a namespace (a dict, typically)
674
671
675 fname : str
672 fname : str
676 Filename associated with the namespace.
673 Filename associated with the namespace.
677
674
678 Examples
675 Examples
679 --------
676 --------
680
677
681 In [10]: import IPython
678 In [10]: import IPython
682
679
683 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
680 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
684
681
685 In [12]: IPython.__file__ in _ip._main_ns_cache
682 In [12]: IPython.__file__ in _ip._main_ns_cache
686 Out[12]: True
683 Out[12]: True
687 """
684 """
688 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
685 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
689
686
690 def clear_main_mod_cache(self):
687 def clear_main_mod_cache(self):
691 """Clear the cache of main modules.
688 """Clear the cache of main modules.
692
689
693 Mainly for use by utilities like %reset.
690 Mainly for use by utilities like %reset.
694
691
695 Examples
692 Examples
696 --------
693 --------
697
694
698 In [15]: import IPython
695 In [15]: import IPython
699
696
700 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
697 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
701
698
702 In [17]: len(_ip._main_ns_cache) > 0
699 In [17]: len(_ip._main_ns_cache) > 0
703 Out[17]: True
700 Out[17]: True
704
701
705 In [18]: _ip.clear_main_mod_cache()
702 In [18]: _ip.clear_main_mod_cache()
706
703
707 In [19]: len(_ip._main_ns_cache) == 0
704 In [19]: len(_ip._main_ns_cache) == 0
708 Out[19]: True
705 Out[19]: True
709 """
706 """
710 self._main_ns_cache.clear()
707 self._main_ns_cache.clear()
711
708
712 #-------------------------------------------------------------------------
709 #-------------------------------------------------------------------------
713 # Things related to debugging
710 # Things related to debugging
714 #-------------------------------------------------------------------------
711 #-------------------------------------------------------------------------
715
712
716 def init_pdb(self):
713 def init_pdb(self):
717 # Set calling of pdb on exceptions
714 # Set calling of pdb on exceptions
718 # self.call_pdb is a property
715 # self.call_pdb is a property
719 self.call_pdb = self.pdb
716 self.call_pdb = self.pdb
720
717
721 def _get_call_pdb(self):
718 def _get_call_pdb(self):
722 return self._call_pdb
719 return self._call_pdb
723
720
724 def _set_call_pdb(self,val):
721 def _set_call_pdb(self,val):
725
722
726 if val not in (0,1,False,True):
723 if val not in (0,1,False,True):
727 raise ValueError,'new call_pdb value must be boolean'
724 raise ValueError,'new call_pdb value must be boolean'
728
725
729 # store value in instance
726 # store value in instance
730 self._call_pdb = val
727 self._call_pdb = val
731
728
732 # notify the actual exception handlers
729 # notify the actual exception handlers
733 self.InteractiveTB.call_pdb = val
730 self.InteractiveTB.call_pdb = val
734 if self.isthreaded:
731 if self.isthreaded:
735 try:
732 try:
736 self.sys_excepthook.call_pdb = val
733 self.sys_excepthook.call_pdb = val
737 except:
734 except:
738 warn('Failed to activate pdb for threaded exception handler')
735 warn('Failed to activate pdb for threaded exception handler')
739
736
740 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
737 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
741 'Control auto-activation of pdb at exceptions')
738 'Control auto-activation of pdb at exceptions')
742
739
743 def debugger(self,force=False):
740 def debugger(self,force=False):
744 """Call the pydb/pdb debugger.
741 """Call the pydb/pdb debugger.
745
742
746 Keywords:
743 Keywords:
747
744
748 - force(False): by default, this routine checks the instance call_pdb
745 - force(False): by default, this routine checks the instance call_pdb
749 flag and does not actually invoke the debugger if the flag is false.
746 flag and does not actually invoke the debugger if the flag is false.
750 The 'force' option forces the debugger to activate even if the flag
747 The 'force' option forces the debugger to activate even if the flag
751 is false.
748 is false.
752 """
749 """
753
750
754 if not (force or self.call_pdb):
751 if not (force or self.call_pdb):
755 return
752 return
756
753
757 if not hasattr(sys,'last_traceback'):
754 if not hasattr(sys,'last_traceback'):
758 error('No traceback has been produced, nothing to debug.')
755 error('No traceback has been produced, nothing to debug.')
759 return
756 return
760
757
761 # use pydb if available
758 # use pydb if available
762 if debugger.has_pydb:
759 if debugger.has_pydb:
763 from pydb import pm
760 from pydb import pm
764 else:
761 else:
765 # fallback to our internal debugger
762 # fallback to our internal debugger
766 pm = lambda : self.InteractiveTB.debugger(force=True)
763 pm = lambda : self.InteractiveTB.debugger(force=True)
767 self.history_saving_wrapper(pm)()
764 self.history_saving_wrapper(pm)()
768
765
769 #-------------------------------------------------------------------------
766 #-------------------------------------------------------------------------
770 # Things related to IPython's various namespaces
767 # Things related to IPython's various namespaces
771 #-------------------------------------------------------------------------
768 #-------------------------------------------------------------------------
772
769
773 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
770 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
774 # Create the namespace where the user will operate. user_ns is
771 # Create the namespace where the user will operate. user_ns is
775 # normally the only one used, and it is passed to the exec calls as
772 # normally the only one used, and it is passed to the exec calls as
776 # the locals argument. But we do carry a user_global_ns namespace
773 # the locals argument. But we do carry a user_global_ns namespace
777 # given as the exec 'globals' argument, This is useful in embedding
774 # given as the exec 'globals' argument, This is useful in embedding
778 # situations where the ipython shell opens in a context where the
775 # situations where the ipython shell opens in a context where the
779 # distinction between locals and globals is meaningful. For
776 # distinction between locals and globals is meaningful. For
780 # non-embedded contexts, it is just the same object as the user_ns dict.
777 # non-embedded contexts, it is just the same object as the user_ns dict.
781
778
782 # FIXME. For some strange reason, __builtins__ is showing up at user
779 # FIXME. For some strange reason, __builtins__ is showing up at user
783 # level as a dict instead of a module. This is a manual fix, but I
780 # level as a dict instead of a module. This is a manual fix, but I
784 # should really track down where the problem is coming from. Alex
781 # should really track down where the problem is coming from. Alex
785 # Schmolck reported this problem first.
782 # Schmolck reported this problem first.
786
783
787 # A useful post by Alex Martelli on this topic:
784 # A useful post by Alex Martelli on this topic:
788 # Re: inconsistent value from __builtins__
785 # Re: inconsistent value from __builtins__
789 # Von: Alex Martelli <aleaxit@yahoo.com>
786 # Von: Alex Martelli <aleaxit@yahoo.com>
790 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
787 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
791 # Gruppen: comp.lang.python
788 # Gruppen: comp.lang.python
792
789
793 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
790 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
794 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
791 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
795 # > <type 'dict'>
792 # > <type 'dict'>
796 # > >>> print type(__builtins__)
793 # > >>> print type(__builtins__)
797 # > <type 'module'>
794 # > <type 'module'>
798 # > Is this difference in return value intentional?
795 # > Is this difference in return value intentional?
799
796
800 # Well, it's documented that '__builtins__' can be either a dictionary
797 # Well, it's documented that '__builtins__' can be either a dictionary
801 # or a module, and it's been that way for a long time. Whether it's
798 # or a module, and it's been that way for a long time. Whether it's
802 # intentional (or sensible), I don't know. In any case, the idea is
799 # intentional (or sensible), I don't know. In any case, the idea is
803 # that if you need to access the built-in namespace directly, you
800 # that if you need to access the built-in namespace directly, you
804 # should start with "import __builtin__" (note, no 's') which will
801 # should start with "import __builtin__" (note, no 's') which will
805 # definitely give you a module. Yeah, it's somewhat confusing:-(.
802 # definitely give you a module. Yeah, it's somewhat confusing:-(.
806
803
807 # These routines return properly built dicts as needed by the rest of
804 # These routines return properly built dicts as needed by the rest of
808 # the code, and can also be used by extension writers to generate
805 # the code, and can also be used by extension writers to generate
809 # properly initialized namespaces.
806 # properly initialized namespaces.
810 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
807 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
811 user_global_ns)
808 user_global_ns)
812
809
813 # Assign namespaces
810 # Assign namespaces
814 # This is the namespace where all normal user variables live
811 # This is the namespace where all normal user variables live
815 self.user_ns = user_ns
812 self.user_ns = user_ns
816 self.user_global_ns = user_global_ns
813 self.user_global_ns = user_global_ns
817
814
818 # An auxiliary namespace that checks what parts of the user_ns were
815 # An auxiliary namespace that checks what parts of the user_ns were
819 # loaded at startup, so we can list later only variables defined in
816 # loaded at startup, so we can list later only variables defined in
820 # actual interactive use. Since it is always a subset of user_ns, it
817 # actual interactive use. Since it is always a subset of user_ns, it
821 # doesn't need to be seaparately tracked in the ns_table
818 # doesn't need to be seaparately tracked in the ns_table
822 self.user_config_ns = {}
819 self.user_config_ns = {}
823
820
824 # A namespace to keep track of internal data structures to prevent
821 # A namespace to keep track of internal data structures to prevent
825 # them from cluttering user-visible stuff. Will be updated later
822 # them from cluttering user-visible stuff. Will be updated later
826 self.internal_ns = {}
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 # Now that FakeModule produces a real module, we've run into a nasty
825 # Now that FakeModule produces a real module, we've run into a nasty
834 # problem: after script execution (via %run), the module where the user
826 # problem: after script execution (via %run), the module where the user
835 # code ran is deleted. Now that this object is a true module (needed
827 # code ran is deleted. Now that this object is a true module (needed
836 # so docetst and other tools work correctly), the Python module
828 # so docetst and other tools work correctly), the Python module
837 # teardown mechanism runs over it, and sets to None every variable
829 # teardown mechanism runs over it, and sets to None every variable
838 # present in that module. Top-level references to objects from the
830 # present in that module. Top-level references to objects from the
839 # script survive, because the user_ns is updated with them. However,
831 # script survive, because the user_ns is updated with them. However,
840 # calling functions defined in the script that use other things from
832 # calling functions defined in the script that use other things from
841 # the script will fail, because the function's closure had references
833 # the script will fail, because the function's closure had references
842 # to the original objects, which are now all None. So we must protect
834 # to the original objects, which are now all None. So we must protect
843 # these modules from deletion by keeping a cache.
835 # these modules from deletion by keeping a cache.
844 #
836 #
845 # To avoid keeping stale modules around (we only need the one from the
837 # To avoid keeping stale modules around (we only need the one from the
846 # last run), we use a dict keyed with the full path to the script, so
838 # last run), we use a dict keyed with the full path to the script, so
847 # only the last version of the module is held in the cache. Note,
839 # only the last version of the module is held in the cache. Note,
848 # however, that we must cache the module *namespace contents* (their
840 # however, that we must cache the module *namespace contents* (their
849 # __dict__). Because if we try to cache the actual modules, old ones
841 # __dict__). Because if we try to cache the actual modules, old ones
850 # (uncached) could be destroyed while still holding references (such as
842 # (uncached) could be destroyed while still holding references (such as
851 # those held by GUI objects that tend to be long-lived)>
843 # those held by GUI objects that tend to be long-lived)>
852 #
844 #
853 # The %reset command will flush this cache. See the cache_main_mod()
845 # The %reset command will flush this cache. See the cache_main_mod()
854 # and clear_main_mod_cache() methods for details on use.
846 # and clear_main_mod_cache() methods for details on use.
855
847
856 # This is the cache used for 'main' namespaces
848 # This is the cache used for 'main' namespaces
857 self._main_ns_cache = {}
849 self._main_ns_cache = {}
858 # And this is the single instance of FakeModule whose __dict__ we keep
850 # And this is the single instance of FakeModule whose __dict__ we keep
859 # copying and clearing for reuse on each %run
851 # copying and clearing for reuse on each %run
860 self._user_main_module = FakeModule()
852 self._user_main_module = FakeModule()
861
853
862 # A table holding all the namespaces IPython deals with, so that
854 # A table holding all the namespaces IPython deals with, so that
863 # introspection facilities can search easily.
855 # introspection facilities can search easily.
864 self.ns_table = {'user':user_ns,
856 self.ns_table = {'user':user_ns,
865 'user_global':user_global_ns,
857 'user_global':user_global_ns,
866 'alias':self.alias_table,
867 'internal':self.internal_ns,
858 'internal':self.internal_ns,
868 'builtin':__builtin__.__dict__
859 'builtin':__builtin__.__dict__
869 }
860 }
870
861
871 # Similarly, track all namespaces where references can be held and that
862 # Similarly, track all namespaces where references can be held and that
872 # we can safely clear (so it can NOT include builtin). This one can be
863 # we can safely clear (so it can NOT include builtin). This one can be
873 # a simple list.
864 # a simple list.
874 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
865 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
875 self.alias_table, self.internal_ns,
866 self.internal_ns, self._main_ns_cache ]
876 self._main_ns_cache ]
877
867
878 def init_sys_modules(self):
868 def init_sys_modules(self):
879 # We need to insert into sys.modules something that looks like a
869 # We need to insert into sys.modules something that looks like a
880 # module but which accesses the IPython namespace, for shelve and
870 # module but which accesses the IPython namespace, for shelve and
881 # pickle to work interactively. Normally they rely on getting
871 # pickle to work interactively. Normally they rely on getting
882 # everything out of __main__, but for embedding purposes each IPython
872 # everything out of __main__, but for embedding purposes each IPython
883 # instance has its own private namespace, so we can't go shoving
873 # instance has its own private namespace, so we can't go shoving
884 # everything into __main__.
874 # everything into __main__.
885
875
886 # note, however, that we should only do this for non-embedded
876 # note, however, that we should only do this for non-embedded
887 # ipythons, which really mimic the __main__.__dict__ with their own
877 # ipythons, which really mimic the __main__.__dict__ with their own
888 # namespace. Embedded instances, on the other hand, should not do
878 # namespace. Embedded instances, on the other hand, should not do
889 # this because they need to manage the user local/global namespaces
879 # this because they need to manage the user local/global namespaces
890 # only, but they live within a 'normal' __main__ (meaning, they
880 # only, but they live within a 'normal' __main__ (meaning, they
891 # shouldn't overtake the execution environment of the script they're
881 # shouldn't overtake the execution environment of the script they're
892 # embedded in).
882 # embedded in).
893
883
894 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
884 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
895
885
896 try:
886 try:
897 main_name = self.user_ns['__name__']
887 main_name = self.user_ns['__name__']
898 except KeyError:
888 except KeyError:
899 raise KeyError('user_ns dictionary MUST have a "__name__" key')
889 raise KeyError('user_ns dictionary MUST have a "__name__" key')
900 else:
890 else:
901 sys.modules[main_name] = FakeModule(self.user_ns)
891 sys.modules[main_name] = FakeModule(self.user_ns)
902
892
903 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
893 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
904 """Return a valid local and global user interactive namespaces.
894 """Return a valid local and global user interactive namespaces.
905
895
906 This builds a dict with the minimal information needed to operate as a
896 This builds a dict with the minimal information needed to operate as a
907 valid IPython user namespace, which you can pass to the various
897 valid IPython user namespace, which you can pass to the various
908 embedding classes in ipython. The default implementation returns the
898 embedding classes in ipython. The default implementation returns the
909 same dict for both the locals and the globals to allow functions to
899 same dict for both the locals and the globals to allow functions to
910 refer to variables in the namespace. Customized implementations can
900 refer to variables in the namespace. Customized implementations can
911 return different dicts. The locals dictionary can actually be anything
901 return different dicts. The locals dictionary can actually be anything
912 following the basic mapping protocol of a dict, but the globals dict
902 following the basic mapping protocol of a dict, but the globals dict
913 must be a true dict, not even a subclass. It is recommended that any
903 must be a true dict, not even a subclass. It is recommended that any
914 custom object for the locals namespace synchronize with the globals
904 custom object for the locals namespace synchronize with the globals
915 dict somehow.
905 dict somehow.
916
906
917 Raises TypeError if the provided globals namespace is not a true dict.
907 Raises TypeError if the provided globals namespace is not a true dict.
918
908
919 :Parameters:
909 :Parameters:
920 user_ns : dict-like, optional
910 user_ns : dict-like, optional
921 The current user namespace. The items in this namespace should
911 The current user namespace. The items in this namespace should
922 be included in the output. If None, an appropriate blank
912 be included in the output. If None, an appropriate blank
923 namespace should be created.
913 namespace should be created.
924 user_global_ns : dict, optional
914 user_global_ns : dict, optional
925 The current user global namespace. The items in this namespace
915 The current user global namespace. The items in this namespace
926 should be included in the output. If None, an appropriate
916 should be included in the output. If None, an appropriate
927 blank namespace should be created.
917 blank namespace should be created.
928
918
929 :Returns:
919 :Returns:
930 A tuple pair of dictionary-like object to be used as the local namespace
920 A tuple pair of dictionary-like object to be used as the local namespace
931 of the interpreter and a dict to be used as the global namespace.
921 of the interpreter and a dict to be used as the global namespace.
932 """
922 """
933
923
934 if user_ns is None:
924 if user_ns is None:
935 # Set __name__ to __main__ to better match the behavior of the
925 # Set __name__ to __main__ to better match the behavior of the
936 # normal interpreter.
926 # normal interpreter.
937 user_ns = {'__name__' :'__main__',
927 user_ns = {'__name__' :'__main__',
938 '__builtins__' : __builtin__,
928 '__builtins__' : __builtin__,
939 }
929 }
940 else:
930 else:
941 user_ns.setdefault('__name__','__main__')
931 user_ns.setdefault('__name__','__main__')
942 user_ns.setdefault('__builtins__',__builtin__)
932 user_ns.setdefault('__builtins__',__builtin__)
943
933
944 if user_global_ns is None:
934 if user_global_ns is None:
945 user_global_ns = user_ns
935 user_global_ns = user_ns
946 if type(user_global_ns) is not dict:
936 if type(user_global_ns) is not dict:
947 raise TypeError("user_global_ns must be a true dict; got %r"
937 raise TypeError("user_global_ns must be a true dict; got %r"
948 % type(user_global_ns))
938 % type(user_global_ns))
949
939
950 return user_ns, user_global_ns
940 return user_ns, user_global_ns
951
941
952 def init_user_ns(self):
942 def init_user_ns(self):
953 """Initialize all user-visible namespaces to their minimum defaults.
943 """Initialize all user-visible namespaces to their minimum defaults.
954
944
955 Certain history lists are also initialized here, as they effectively
945 Certain history lists are also initialized here, as they effectively
956 act as user namespaces.
946 act as user namespaces.
957
947
958 Notes
948 Notes
959 -----
949 -----
960 All data structures here are only filled in, they are NOT reset by this
950 All data structures here are only filled in, they are NOT reset by this
961 method. If they were not empty before, data will simply be added to
951 method. If they were not empty before, data will simply be added to
962 therm.
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 # Store myself as the public api!!!
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 # make global variables for user access to the histories
957 # make global variables for user access to the histories
971 self.user_ns['_ih'] = self.input_hist
958 self.user_ns['_ih'] = self.input_hist
972 self.user_ns['_oh'] = self.output_hist
959 self.user_ns['_oh'] = self.output_hist
973 self.user_ns['_dh'] = self.dir_hist
960 self.user_ns['_dh'] = self.dir_hist
974
961
975 # user aliases to input and output histories
962 # user aliases to input and output histories
976 self.user_ns['In'] = self.input_hist
963 self.user_ns['In'] = self.input_hist
977 self.user_ns['Out'] = self.output_hist
964 self.user_ns['Out'] = self.output_hist
978
965
979 self.user_ns['_sh'] = shadowns
966 self.user_ns['_sh'] = shadowns
980
967
981 # Put 'help' in the user namespace
968 # Put 'help' in the user namespace
982 try:
969 try:
983 from site import _Helper
970 from site import _Helper
984 self.user_ns['help'] = _Helper()
971 self.user_ns['help'] = _Helper()
985 except ImportError:
972 except ImportError:
986 warn('help() not available - check site.py')
973 warn('help() not available - check site.py')
987
974
988 def reset(self):
975 def reset(self):
989 """Clear all internal namespaces.
976 """Clear all internal namespaces.
990
977
991 Note that this is much more aggressive than %reset, since it clears
978 Note that this is much more aggressive than %reset, since it clears
992 fully all namespaces, as well as all input/output lists.
979 fully all namespaces, as well as all input/output lists.
993 """
980 """
994 for ns in self.ns_refs_table:
981 for ns in self.ns_refs_table:
995 ns.clear()
982 ns.clear()
996
983
984 self.alias_manager.clear_aliases()
985
997 # Clear input and output histories
986 # Clear input and output histories
998 self.input_hist[:] = []
987 self.input_hist[:] = []
999 self.input_hist_raw[:] = []
988 self.input_hist_raw[:] = []
1000 self.output_hist.clear()
989 self.output_hist.clear()
990
1001 # Restore the user namespaces to minimal usability
991 # Restore the user namespaces to minimal usability
1002 self.init_user_ns()
992 self.init_user_ns()
1003
993
994 # Restore the default and user aliases
995 self.alias_manager.init_aliases()
996
1004 def push(self, variables, interactive=True):
997 def push(self, variables, interactive=True):
1005 """Inject a group of variables into the IPython user namespace.
998 """Inject a group of variables into the IPython user namespace.
1006
999
1007 Parameters
1000 Parameters
1008 ----------
1001 ----------
1009 variables : dict, str or list/tuple of str
1002 variables : dict, str or list/tuple of str
1010 The variables to inject into the user's namespace. If a dict,
1003 The variables to inject into the user's namespace. If a dict,
1011 a simple update is done. If a str, the string is assumed to
1004 a simple update is done. If a str, the string is assumed to
1012 have variable names separated by spaces. A list/tuple of str
1005 have variable names separated by spaces. A list/tuple of str
1013 can also be used to give the variable names. If just the variable
1006 can also be used to give the variable names. If just the variable
1014 names are give (list/tuple/str) then the variable values looked
1007 names are give (list/tuple/str) then the variable values looked
1015 up in the callers frame.
1008 up in the callers frame.
1016 interactive : bool
1009 interactive : bool
1017 If True (default), the variables will be listed with the ``who``
1010 If True (default), the variables will be listed with the ``who``
1018 magic.
1011 magic.
1019 """
1012 """
1020 vdict = None
1013 vdict = None
1021
1014
1022 # We need a dict of name/value pairs to do namespace updates.
1015 # We need a dict of name/value pairs to do namespace updates.
1023 if isinstance(variables, dict):
1016 if isinstance(variables, dict):
1024 vdict = variables
1017 vdict = variables
1025 elif isinstance(variables, (basestring, list, tuple)):
1018 elif isinstance(variables, (basestring, list, tuple)):
1026 if isinstance(variables, basestring):
1019 if isinstance(variables, basestring):
1027 vlist = variables.split()
1020 vlist = variables.split()
1028 else:
1021 else:
1029 vlist = variables
1022 vlist = variables
1030 vdict = {}
1023 vdict = {}
1031 cf = sys._getframe(1)
1024 cf = sys._getframe(1)
1032 for name in vlist:
1025 for name in vlist:
1033 try:
1026 try:
1034 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1027 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1035 except:
1028 except:
1036 print ('Could not get variable %s from %s' %
1029 print ('Could not get variable %s from %s' %
1037 (name,cf.f_code.co_name))
1030 (name,cf.f_code.co_name))
1038 else:
1031 else:
1039 raise ValueError('variables must be a dict/str/list/tuple')
1032 raise ValueError('variables must be a dict/str/list/tuple')
1040
1033
1041 # Propagate variables to user namespace
1034 # Propagate variables to user namespace
1042 self.user_ns.update(vdict)
1035 self.user_ns.update(vdict)
1043
1036
1044 # And configure interactive visibility
1037 # And configure interactive visibility
1045 config_ns = self.user_config_ns
1038 config_ns = self.user_config_ns
1046 if interactive:
1039 if interactive:
1047 for name, val in vdict.iteritems():
1040 for name, val in vdict.iteritems():
1048 config_ns.pop(name, None)
1041 config_ns.pop(name, None)
1049 else:
1042 else:
1050 for name,val in vdict.iteritems():
1043 for name,val in vdict.iteritems():
1051 config_ns[name] = val
1044 config_ns[name] = val
1052
1045
1053 #-------------------------------------------------------------------------
1046 #-------------------------------------------------------------------------
1054 # Things related to history management
1047 # Things related to history management
1055 #-------------------------------------------------------------------------
1048 #-------------------------------------------------------------------------
1056
1049
1057 def init_history(self):
1050 def init_history(self):
1058 # List of input with multi-line handling.
1051 # List of input with multi-line handling.
1059 self.input_hist = InputList()
1052 self.input_hist = InputList()
1060 # This one will hold the 'raw' input history, without any
1053 # This one will hold the 'raw' input history, without any
1061 # pre-processing. This will allow users to retrieve the input just as
1054 # pre-processing. This will allow users to retrieve the input just as
1062 # it was exactly typed in by the user, with %hist -r.
1055 # it was exactly typed in by the user, with %hist -r.
1063 self.input_hist_raw = InputList()
1056 self.input_hist_raw = InputList()
1064
1057
1065 # list of visited directories
1058 # list of visited directories
1066 try:
1059 try:
1067 self.dir_hist = [os.getcwd()]
1060 self.dir_hist = [os.getcwd()]
1068 except OSError:
1061 except OSError:
1069 self.dir_hist = []
1062 self.dir_hist = []
1070
1063
1071 # dict of output history
1064 # dict of output history
1072 self.output_hist = {}
1065 self.output_hist = {}
1073
1066
1074 # Now the history file
1067 # Now the history file
1075 try:
1068 try:
1076 histfname = 'history-%s' % self.profile
1069 histfname = 'history-%s' % self.profile
1077 except AttributeError:
1070 except AttributeError:
1078 histfname = 'history'
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 # Fill the history zero entry, user counter starts at 1
1074 # Fill the history zero entry, user counter starts at 1
1082 self.input_hist.append('\n')
1075 self.input_hist.append('\n')
1083 self.input_hist_raw.append('\n')
1076 self.input_hist_raw.append('\n')
1084
1077
1085 def init_shadow_hist(self):
1078 def init_shadow_hist(self):
1086 try:
1079 try:
1087 self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db")
1080 self.db = pickleshare.PickleShareDB(self.ipythondir + "/db")
1088 except exceptions.UnicodeDecodeError:
1081 except exceptions.UnicodeDecodeError:
1089 print "Your ipythondir can't be decoded to unicode!"
1082 print "Your ipythondir can't be decoded to unicode!"
1090 print "Please set HOME environment variable to something that"
1083 print "Please set HOME environment variable to something that"
1091 print r"only has ASCII characters, e.g. c:\home"
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 sys.exit()
1086 sys.exit()
1094 self.shadowhist = ipcorehist.ShadowHist(self.db)
1087 self.shadowhist = ipcorehist.ShadowHist(self.db)
1095
1088
1096 def savehist(self):
1089 def savehist(self):
1097 """Save input history to a file (via readline library)."""
1090 """Save input history to a file (via readline library)."""
1098
1091
1099 if not self.has_readline:
1092 if not self.has_readline:
1100 return
1093 return
1101
1094
1102 try:
1095 try:
1103 self.readline.write_history_file(self.histfile)
1096 self.readline.write_history_file(self.histfile)
1104 except:
1097 except:
1105 print 'Unable to save IPython command history to file: ' + \
1098 print 'Unable to save IPython command history to file: ' + \
1106 `self.histfile`
1099 `self.histfile`
1107
1100
1108 def reloadhist(self):
1101 def reloadhist(self):
1109 """Reload the input history from disk file."""
1102 """Reload the input history from disk file."""
1110
1103
1111 if self.has_readline:
1104 if self.has_readline:
1112 try:
1105 try:
1113 self.readline.clear_history()
1106 self.readline.clear_history()
1114 self.readline.read_history_file(self.shell.histfile)
1107 self.readline.read_history_file(self.shell.histfile)
1115 except AttributeError:
1108 except AttributeError:
1116 pass
1109 pass
1117
1110
1118 def history_saving_wrapper(self, func):
1111 def history_saving_wrapper(self, func):
1119 """ Wrap func for readline history saving
1112 """ Wrap func for readline history saving
1120
1113
1121 Convert func into callable that saves & restores
1114 Convert func into callable that saves & restores
1122 history around the call """
1115 history around the call """
1123
1116
1124 if not self.has_readline:
1117 if not self.has_readline:
1125 return func
1118 return func
1126
1119
1127 def wrapper():
1120 def wrapper():
1128 self.savehist()
1121 self.savehist()
1129 try:
1122 try:
1130 func()
1123 func()
1131 finally:
1124 finally:
1132 readline.read_history_file(self.histfile)
1125 readline.read_history_file(self.histfile)
1133 return wrapper
1126 return wrapper
1134
1127
1135 #-------------------------------------------------------------------------
1128 #-------------------------------------------------------------------------
1136 # Things related to exception handling and tracebacks (not debugging)
1129 # Things related to exception handling and tracebacks (not debugging)
1137 #-------------------------------------------------------------------------
1130 #-------------------------------------------------------------------------
1138
1131
1139 def init_traceback_handlers(self, custom_exceptions):
1132 def init_traceback_handlers(self, custom_exceptions):
1140 # Syntax error handler.
1133 # Syntax error handler.
1141 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1134 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1142
1135
1143 # The interactive one is initialized with an offset, meaning we always
1136 # The interactive one is initialized with an offset, meaning we always
1144 # want to remove the topmost item in the traceback, which is our own
1137 # want to remove the topmost item in the traceback, which is our own
1145 # internal code. Valid modes: ['Plain','Context','Verbose']
1138 # internal code. Valid modes: ['Plain','Context','Verbose']
1146 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1139 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1147 color_scheme='NoColor',
1140 color_scheme='NoColor',
1148 tb_offset = 1)
1141 tb_offset = 1)
1149
1142
1150 # IPython itself shouldn't crash. This will produce a detailed
1143 # IPython itself shouldn't crash. This will produce a detailed
1151 # post-mortem if it does. But we only install the crash handler for
1144 # post-mortem if it does. But we only install the crash handler for
1152 # non-threaded shells, the threaded ones use a normal verbose reporter
1145 # non-threaded shells, the threaded ones use a normal verbose reporter
1153 # and lose the crash handler. This is because exceptions in the main
1146 # and lose the crash handler. This is because exceptions in the main
1154 # thread (such as in GUI code) propagate directly to sys.excepthook,
1147 # thread (such as in GUI code) propagate directly to sys.excepthook,
1155 # and there's no point in printing crash dumps for every user exception.
1148 # and there's no point in printing crash dumps for every user exception.
1156 if self.isthreaded:
1149 if self.isthreaded:
1157 ipCrashHandler = ultratb.FormattedTB()
1150 ipCrashHandler = ultratb.FormattedTB()
1158 else:
1151 else:
1159 from IPython.core import crashhandler
1152 from IPython.core import crashhandler
1160 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1153 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1161 self.set_crash_handler(ipCrashHandler)
1154 self.set_crash_handler(ipCrashHandler)
1162
1155
1163 # and add any custom exception handlers the user may have specified
1156 # and add any custom exception handlers the user may have specified
1164 self.set_custom_exc(*custom_exceptions)
1157 self.set_custom_exc(*custom_exceptions)
1165
1158
1166 def set_crash_handler(self, crashHandler):
1159 def set_crash_handler(self, crashHandler):
1167 """Set the IPython crash handler.
1160 """Set the IPython crash handler.
1168
1161
1169 This must be a callable with a signature suitable for use as
1162 This must be a callable with a signature suitable for use as
1170 sys.excepthook."""
1163 sys.excepthook."""
1171
1164
1172 # Install the given crash handler as the Python exception hook
1165 # Install the given crash handler as the Python exception hook
1173 sys.excepthook = crashHandler
1166 sys.excepthook = crashHandler
1174
1167
1175 # The instance will store a pointer to this, so that runtime code
1168 # The instance will store a pointer to this, so that runtime code
1176 # (such as magics) can access it. This is because during the
1169 # (such as magics) can access it. This is because during the
1177 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1170 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1178 # frameworks).
1171 # frameworks).
1179 self.sys_excepthook = sys.excepthook
1172 self.sys_excepthook = sys.excepthook
1180
1173
1181 def set_custom_exc(self,exc_tuple,handler):
1174 def set_custom_exc(self,exc_tuple,handler):
1182 """set_custom_exc(exc_tuple,handler)
1175 """set_custom_exc(exc_tuple,handler)
1183
1176
1184 Set a custom exception handler, which will be called if any of the
1177 Set a custom exception handler, which will be called if any of the
1185 exceptions in exc_tuple occur in the mainloop (specifically, in the
1178 exceptions in exc_tuple occur in the mainloop (specifically, in the
1186 runcode() method.
1179 runcode() method.
1187
1180
1188 Inputs:
1181 Inputs:
1189
1182
1190 - exc_tuple: a *tuple* of valid exceptions to call the defined
1183 - exc_tuple: a *tuple* of valid exceptions to call the defined
1191 handler for. It is very important that you use a tuple, and NOT A
1184 handler for. It is very important that you use a tuple, and NOT A
1192 LIST here, because of the way Python's except statement works. If
1185 LIST here, because of the way Python's except statement works. If
1193 you only want to trap a single exception, use a singleton tuple:
1186 you only want to trap a single exception, use a singleton tuple:
1194
1187
1195 exc_tuple == (MyCustomException,)
1188 exc_tuple == (MyCustomException,)
1196
1189
1197 - handler: this must be defined as a function with the following
1190 - handler: this must be defined as a function with the following
1198 basic interface: def my_handler(self,etype,value,tb).
1191 basic interface: def my_handler(self,etype,value,tb).
1199
1192
1200 This will be made into an instance method (via new.instancemethod)
1193 This will be made into an instance method (via new.instancemethod)
1201 of IPython itself, and it will be called if any of the exceptions
1194 of IPython itself, and it will be called if any of the exceptions
1202 listed in the exc_tuple are caught. If the handler is None, an
1195 listed in the exc_tuple are caught. If the handler is None, an
1203 internal basic one is used, which just prints basic info.
1196 internal basic one is used, which just prints basic info.
1204
1197
1205 WARNING: by putting in your own exception handler into IPython's main
1198 WARNING: by putting in your own exception handler into IPython's main
1206 execution loop, you run a very good chance of nasty crashes. This
1199 execution loop, you run a very good chance of nasty crashes. This
1207 facility should only be used if you really know what you are doing."""
1200 facility should only be used if you really know what you are doing."""
1208
1201
1209 assert type(exc_tuple)==type(()) , \
1202 assert type(exc_tuple)==type(()) , \
1210 "The custom exceptions must be given AS A TUPLE."
1203 "The custom exceptions must be given AS A TUPLE."
1211
1204
1212 def dummy_handler(self,etype,value,tb):
1205 def dummy_handler(self,etype,value,tb):
1213 print '*** Simple custom exception handler ***'
1206 print '*** Simple custom exception handler ***'
1214 print 'Exception type :',etype
1207 print 'Exception type :',etype
1215 print 'Exception value:',value
1208 print 'Exception value:',value
1216 print 'Traceback :',tb
1209 print 'Traceback :',tb
1217 print 'Source code :','\n'.join(self.buffer)
1210 print 'Source code :','\n'.join(self.buffer)
1218
1211
1219 if handler is None: handler = dummy_handler
1212 if handler is None: handler = dummy_handler
1220
1213
1221 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1214 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1222 self.custom_exceptions = exc_tuple
1215 self.custom_exceptions = exc_tuple
1223
1216
1224 def excepthook(self, etype, value, tb):
1217 def excepthook(self, etype, value, tb):
1225 """One more defense for GUI apps that call sys.excepthook.
1218 """One more defense for GUI apps that call sys.excepthook.
1226
1219
1227 GUI frameworks like wxPython trap exceptions and call
1220 GUI frameworks like wxPython trap exceptions and call
1228 sys.excepthook themselves. I guess this is a feature that
1221 sys.excepthook themselves. I guess this is a feature that
1229 enables them to keep running after exceptions that would
1222 enables them to keep running after exceptions that would
1230 otherwise kill their mainloop. This is a bother for IPython
1223 otherwise kill their mainloop. This is a bother for IPython
1231 which excepts to catch all of the program exceptions with a try:
1224 which excepts to catch all of the program exceptions with a try:
1232 except: statement.
1225 except: statement.
1233
1226
1234 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1227 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1235 any app directly invokes sys.excepthook, it will look to the user like
1228 any app directly invokes sys.excepthook, it will look to the user like
1236 IPython crashed. In order to work around this, we can disable the
1229 IPython crashed. In order to work around this, we can disable the
1237 CrashHandler and replace it with this excepthook instead, which prints a
1230 CrashHandler and replace it with this excepthook instead, which prints a
1238 regular traceback using our InteractiveTB. In this fashion, apps which
1231 regular traceback using our InteractiveTB. In this fashion, apps which
1239 call sys.excepthook will generate a regular-looking exception from
1232 call sys.excepthook will generate a regular-looking exception from
1240 IPython, and the CrashHandler will only be triggered by real IPython
1233 IPython, and the CrashHandler will only be triggered by real IPython
1241 crashes.
1234 crashes.
1242
1235
1243 This hook should be used sparingly, only in places which are not likely
1236 This hook should be used sparingly, only in places which are not likely
1244 to be true IPython errors.
1237 to be true IPython errors.
1245 """
1238 """
1246 self.showtraceback((etype,value,tb),tb_offset=0)
1239 self.showtraceback((etype,value,tb),tb_offset=0)
1247
1240
1248 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1241 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1249 """Display the exception that just occurred.
1242 """Display the exception that just occurred.
1250
1243
1251 If nothing is known about the exception, this is the method which
1244 If nothing is known about the exception, this is the method which
1252 should be used throughout the code for presenting user tracebacks,
1245 should be used throughout the code for presenting user tracebacks,
1253 rather than directly invoking the InteractiveTB object.
1246 rather than directly invoking the InteractiveTB object.
1254
1247
1255 A specific showsyntaxerror() also exists, but this method can take
1248 A specific showsyntaxerror() also exists, but this method can take
1256 care of calling it if needed, so unless you are explicitly catching a
1249 care of calling it if needed, so unless you are explicitly catching a
1257 SyntaxError exception, don't try to analyze the stack manually and
1250 SyntaxError exception, don't try to analyze the stack manually and
1258 simply call this method."""
1251 simply call this method."""
1259
1252
1260
1253
1261 # Though this won't be called by syntax errors in the input line,
1254 # Though this won't be called by syntax errors in the input line,
1262 # there may be SyntaxError cases whith imported code.
1255 # there may be SyntaxError cases whith imported code.
1263
1256
1264 try:
1257 try:
1265 if exc_tuple is None:
1258 if exc_tuple is None:
1266 etype, value, tb = sys.exc_info()
1259 etype, value, tb = sys.exc_info()
1267 else:
1260 else:
1268 etype, value, tb = exc_tuple
1261 etype, value, tb = exc_tuple
1269
1262
1270 if etype is SyntaxError:
1263 if etype is SyntaxError:
1271 self.showsyntaxerror(filename)
1264 self.showsyntaxerror(filename)
1272 elif etype is UsageError:
1265 elif etype is UsageError:
1273 print "UsageError:", value
1266 print "UsageError:", value
1274 else:
1267 else:
1275 # WARNING: these variables are somewhat deprecated and not
1268 # WARNING: these variables are somewhat deprecated and not
1276 # necessarily safe to use in a threaded environment, but tools
1269 # necessarily safe to use in a threaded environment, but tools
1277 # like pdb depend on their existence, so let's set them. If we
1270 # like pdb depend on their existence, so let's set them. If we
1278 # find problems in the field, we'll need to revisit their use.
1271 # find problems in the field, we'll need to revisit their use.
1279 sys.last_type = etype
1272 sys.last_type = etype
1280 sys.last_value = value
1273 sys.last_value = value
1281 sys.last_traceback = tb
1274 sys.last_traceback = tb
1282
1275
1283 if etype in self.custom_exceptions:
1276 if etype in self.custom_exceptions:
1284 self.CustomTB(etype,value,tb)
1277 self.CustomTB(etype,value,tb)
1285 else:
1278 else:
1286 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1279 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1287 if self.InteractiveTB.call_pdb and self.has_readline:
1280 if self.InteractiveTB.call_pdb and self.has_readline:
1288 # pdb mucks up readline, fix it back
1281 # pdb mucks up readline, fix it back
1289 self.set_completer()
1282 self.set_completer()
1290 except KeyboardInterrupt:
1283 except KeyboardInterrupt:
1291 self.write("\nKeyboardInterrupt\n")
1284 self.write("\nKeyboardInterrupt\n")
1292
1285
1293 def showsyntaxerror(self, filename=None):
1286 def showsyntaxerror(self, filename=None):
1294 """Display the syntax error that just occurred.
1287 """Display the syntax error that just occurred.
1295
1288
1296 This doesn't display a stack trace because there isn't one.
1289 This doesn't display a stack trace because there isn't one.
1297
1290
1298 If a filename is given, it is stuffed in the exception instead
1291 If a filename is given, it is stuffed in the exception instead
1299 of what was there before (because Python's parser always uses
1292 of what was there before (because Python's parser always uses
1300 "<string>" when reading from a string).
1293 "<string>" when reading from a string).
1301 """
1294 """
1302 etype, value, last_traceback = sys.exc_info()
1295 etype, value, last_traceback = sys.exc_info()
1303
1296
1304 # See note about these variables in showtraceback() below
1297 # See note about these variables in showtraceback() below
1305 sys.last_type = etype
1298 sys.last_type = etype
1306 sys.last_value = value
1299 sys.last_value = value
1307 sys.last_traceback = last_traceback
1300 sys.last_traceback = last_traceback
1308
1301
1309 if filename and etype is SyntaxError:
1302 if filename and etype is SyntaxError:
1310 # Work hard to stuff the correct filename in the exception
1303 # Work hard to stuff the correct filename in the exception
1311 try:
1304 try:
1312 msg, (dummy_filename, lineno, offset, line) = value
1305 msg, (dummy_filename, lineno, offset, line) = value
1313 except:
1306 except:
1314 # Not the format we expect; leave it alone
1307 # Not the format we expect; leave it alone
1315 pass
1308 pass
1316 else:
1309 else:
1317 # Stuff in the right filename
1310 # Stuff in the right filename
1318 try:
1311 try:
1319 # Assume SyntaxError is a class exception
1312 # Assume SyntaxError is a class exception
1320 value = SyntaxError(msg, (filename, lineno, offset, line))
1313 value = SyntaxError(msg, (filename, lineno, offset, line))
1321 except:
1314 except:
1322 # If that failed, assume SyntaxError is a string
1315 # If that failed, assume SyntaxError is a string
1323 value = msg, (filename, lineno, offset, line)
1316 value = msg, (filename, lineno, offset, line)
1324 self.SyntaxTB(etype,value,[])
1317 self.SyntaxTB(etype,value,[])
1325
1318
1326 def edit_syntax_error(self):
1319 def edit_syntax_error(self):
1327 """The bottom half of the syntax error handler called in the main loop.
1320 """The bottom half of the syntax error handler called in the main loop.
1328
1321
1329 Loop until syntax error is fixed or user cancels.
1322 Loop until syntax error is fixed or user cancels.
1330 """
1323 """
1331
1324
1332 while self.SyntaxTB.last_syntax_error:
1325 while self.SyntaxTB.last_syntax_error:
1333 # copy and clear last_syntax_error
1326 # copy and clear last_syntax_error
1334 err = self.SyntaxTB.clear_err_state()
1327 err = self.SyntaxTB.clear_err_state()
1335 if not self._should_recompile(err):
1328 if not self._should_recompile(err):
1336 return
1329 return
1337 try:
1330 try:
1338 # may set last_syntax_error again if a SyntaxError is raised
1331 # may set last_syntax_error again if a SyntaxError is raised
1339 self.safe_execfile(err.filename,self.user_ns)
1332 self.safe_execfile(err.filename,self.user_ns)
1340 except:
1333 except:
1341 self.showtraceback()
1334 self.showtraceback()
1342 else:
1335 else:
1343 try:
1336 try:
1344 f = file(err.filename)
1337 f = file(err.filename)
1345 try:
1338 try:
1346 # This should be inside a display_trap block and I
1339 # This should be inside a display_trap block and I
1347 # think it is.
1340 # think it is.
1348 sys.displayhook(f.read())
1341 sys.displayhook(f.read())
1349 finally:
1342 finally:
1350 f.close()
1343 f.close()
1351 except:
1344 except:
1352 self.showtraceback()
1345 self.showtraceback()
1353
1346
1354 def _should_recompile(self,e):
1347 def _should_recompile(self,e):
1355 """Utility routine for edit_syntax_error"""
1348 """Utility routine for edit_syntax_error"""
1356
1349
1357 if e.filename in ('<ipython console>','<input>','<string>',
1350 if e.filename in ('<ipython console>','<input>','<string>',
1358 '<console>','<BackgroundJob compilation>',
1351 '<console>','<BackgroundJob compilation>',
1359 None):
1352 None):
1360
1353
1361 return False
1354 return False
1362 try:
1355 try:
1363 if (self.autoedit_syntax and
1356 if (self.autoedit_syntax and
1364 not self.ask_yes_no('Return to editor to correct syntax error? '
1357 not self.ask_yes_no('Return to editor to correct syntax error? '
1365 '[Y/n] ','y')):
1358 '[Y/n] ','y')):
1366 return False
1359 return False
1367 except EOFError:
1360 except EOFError:
1368 return False
1361 return False
1369
1362
1370 def int0(x):
1363 def int0(x):
1371 try:
1364 try:
1372 return int(x)
1365 return int(x)
1373 except TypeError:
1366 except TypeError:
1374 return 0
1367 return 0
1375 # always pass integer line and offset values to editor hook
1368 # always pass integer line and offset values to editor hook
1376 try:
1369 try:
1377 self.hooks.fix_error_editor(e.filename,
1370 self.hooks.fix_error_editor(e.filename,
1378 int0(e.lineno),int0(e.offset),e.msg)
1371 int0(e.lineno),int0(e.offset),e.msg)
1379 except TryNext:
1372 except TryNext:
1380 warn('Could not open editor')
1373 warn('Could not open editor')
1381 return False
1374 return False
1382 return True
1375 return True
1383
1376
1384 #-------------------------------------------------------------------------
1377 #-------------------------------------------------------------------------
1385 # Things related to tab completion
1378 # Things related to tab completion
1386 #-------------------------------------------------------------------------
1379 #-------------------------------------------------------------------------
1387
1380
1388 def complete(self, text):
1381 def complete(self, text):
1389 """Return a sorted list of all possible completions on text.
1382 """Return a sorted list of all possible completions on text.
1390
1383
1391 Inputs:
1384 Inputs:
1392
1385
1393 - text: a string of text to be completed on.
1386 - text: a string of text to be completed on.
1394
1387
1395 This is a wrapper around the completion mechanism, similar to what
1388 This is a wrapper around the completion mechanism, similar to what
1396 readline does at the command line when the TAB key is hit. By
1389 readline does at the command line when the TAB key is hit. By
1397 exposing it as a method, it can be used by other non-readline
1390 exposing it as a method, it can be used by other non-readline
1398 environments (such as GUIs) for text completion.
1391 environments (such as GUIs) for text completion.
1399
1392
1400 Simple usage example:
1393 Simple usage example:
1401
1394
1402 In [7]: x = 'hello'
1395 In [7]: x = 'hello'
1403
1396
1404 In [8]: x
1397 In [8]: x
1405 Out[8]: 'hello'
1398 Out[8]: 'hello'
1406
1399
1407 In [9]: print x
1400 In [9]: print x
1408 hello
1401 hello
1409
1402
1410 In [10]: _ip.complete('x.l')
1403 In [10]: _ip.complete('x.l')
1411 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1404 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1412 """
1405 """
1413
1406
1414 # Inject names into __builtin__ so we can complete on the added names.
1407 # Inject names into __builtin__ so we can complete on the added names.
1415 with self.builtin_trap:
1408 with self.builtin_trap:
1416 complete = self.Completer.complete
1409 complete = self.Completer.complete
1417 state = 0
1410 state = 0
1418 # use a dict so we get unique keys, since ipyhton's multiple
1411 # use a dict so we get unique keys, since ipyhton's multiple
1419 # completers can return duplicates. When we make 2.4 a requirement,
1412 # completers can return duplicates. When we make 2.4 a requirement,
1420 # start using sets instead, which are faster.
1413 # start using sets instead, which are faster.
1421 comps = {}
1414 comps = {}
1422 while True:
1415 while True:
1423 newcomp = complete(text,state,line_buffer=text)
1416 newcomp = complete(text,state,line_buffer=text)
1424 if newcomp is None:
1417 if newcomp is None:
1425 break
1418 break
1426 comps[newcomp] = 1
1419 comps[newcomp] = 1
1427 state += 1
1420 state += 1
1428 outcomps = comps.keys()
1421 outcomps = comps.keys()
1429 outcomps.sort()
1422 outcomps.sort()
1430 #print "T:",text,"OC:",outcomps # dbg
1423 #print "T:",text,"OC:",outcomps # dbg
1431 #print "vars:",self.user_ns.keys()
1424 #print "vars:",self.user_ns.keys()
1432 return outcomps
1425 return outcomps
1433
1426
1434 def set_custom_completer(self,completer,pos=0):
1427 def set_custom_completer(self,completer,pos=0):
1435 """set_custom_completer(completer,pos=0)
1428 """set_custom_completer(completer,pos=0)
1436
1429
1437 Adds a new custom completer function.
1430 Adds a new custom completer function.
1438
1431
1439 The position argument (defaults to 0) is the index in the completers
1432 The position argument (defaults to 0) is the index in the completers
1440 list where you want the completer to be inserted."""
1433 list where you want the completer to be inserted."""
1441
1434
1442 newcomp = new.instancemethod(completer,self.Completer,
1435 newcomp = new.instancemethod(completer,self.Completer,
1443 self.Completer.__class__)
1436 self.Completer.__class__)
1444 self.Completer.matchers.insert(pos,newcomp)
1437 self.Completer.matchers.insert(pos,newcomp)
1445
1438
1446 def set_completer(self):
1439 def set_completer(self):
1447 """reset readline's completer to be our own."""
1440 """reset readline's completer to be our own."""
1448 self.readline.set_completer(self.Completer.complete)
1441 self.readline.set_completer(self.Completer.complete)
1449
1442
1450 #-------------------------------------------------------------------------
1443 #-------------------------------------------------------------------------
1451 # Things related to readline
1444 # Things related to readline
1452 #-------------------------------------------------------------------------
1445 #-------------------------------------------------------------------------
1453
1446
1454 def init_readline(self):
1447 def init_readline(self):
1455 """Command history completion/saving/reloading."""
1448 """Command history completion/saving/reloading."""
1456
1449
1457 self.rl_next_input = None
1450 self.rl_next_input = None
1458 self.rl_do_indent = False
1451 self.rl_do_indent = False
1459
1452
1460 if not self.readline_use:
1453 if not self.readline_use:
1461 return
1454 return
1462
1455
1463 import IPython.utils.rlineimpl as readline
1456 import IPython.utils.rlineimpl as readline
1464
1457
1465 if not readline.have_readline:
1458 if not readline.have_readline:
1466 self.has_readline = 0
1459 self.has_readline = 0
1467 self.readline = None
1460 self.readline = None
1468 # no point in bugging windows users with this every time:
1461 # no point in bugging windows users with this every time:
1469 warn('Readline services not available on this platform.')
1462 warn('Readline services not available on this platform.')
1470 else:
1463 else:
1471 sys.modules['readline'] = readline
1464 sys.modules['readline'] = readline
1472 import atexit
1465 import atexit
1473 from IPython.core.completer import IPCompleter
1466 from IPython.core.completer import IPCompleter
1474 self.Completer = IPCompleter(self,
1467 self.Completer = IPCompleter(self,
1475 self.user_ns,
1468 self.user_ns,
1476 self.user_global_ns,
1469 self.user_global_ns,
1477 self.readline_omit__names,
1470 self.readline_omit__names,
1478 self.alias_table)
1471 self.alias_manager.alias_table)
1479 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1472 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1480 self.strdispatchers['complete_command'] = sdisp
1473 self.strdispatchers['complete_command'] = sdisp
1481 self.Completer.custom_completers = sdisp
1474 self.Completer.custom_completers = sdisp
1482 # Platform-specific configuration
1475 # Platform-specific configuration
1483 if os.name == 'nt':
1476 if os.name == 'nt':
1484 self.readline_startup_hook = readline.set_pre_input_hook
1477 self.readline_startup_hook = readline.set_pre_input_hook
1485 else:
1478 else:
1486 self.readline_startup_hook = readline.set_startup_hook
1479 self.readline_startup_hook = readline.set_startup_hook
1487
1480
1488 # Load user's initrc file (readline config)
1481 # Load user's initrc file (readline config)
1489 # Or if libedit is used, load editrc.
1482 # Or if libedit is used, load editrc.
1490 inputrc_name = os.environ.get('INPUTRC')
1483 inputrc_name = os.environ.get('INPUTRC')
1491 if inputrc_name is None:
1484 if inputrc_name is None:
1492 home_dir = get_home_dir()
1485 home_dir = get_home_dir()
1493 if home_dir is not None:
1486 if home_dir is not None:
1494 inputrc_name = '.inputrc'
1487 inputrc_name = '.inputrc'
1495 if readline.uses_libedit:
1488 if readline.uses_libedit:
1496 inputrc_name = '.editrc'
1489 inputrc_name = '.editrc'
1497 inputrc_name = os.path.join(home_dir, inputrc_name)
1490 inputrc_name = os.path.join(home_dir, inputrc_name)
1498 if os.path.isfile(inputrc_name):
1491 if os.path.isfile(inputrc_name):
1499 try:
1492 try:
1500 readline.read_init_file(inputrc_name)
1493 readline.read_init_file(inputrc_name)
1501 except:
1494 except:
1502 warn('Problems reading readline initialization file <%s>'
1495 warn('Problems reading readline initialization file <%s>'
1503 % inputrc_name)
1496 % inputrc_name)
1504
1497
1505 self.has_readline = 1
1498 self.has_readline = 1
1506 self.readline = readline
1499 self.readline = readline
1507 # save this in sys so embedded copies can restore it properly
1500 # save this in sys so embedded copies can restore it properly
1508 sys.ipcompleter = self.Completer.complete
1501 sys.ipcompleter = self.Completer.complete
1509 self.set_completer()
1502 self.set_completer()
1510
1503
1511 # Configure readline according to user's prefs
1504 # Configure readline according to user's prefs
1512 # This is only done if GNU readline is being used. If libedit
1505 # This is only done if GNU readline is being used. If libedit
1513 # is being used (as on Leopard) the readline config is
1506 # is being used (as on Leopard) the readline config is
1514 # not run as the syntax for libedit is different.
1507 # not run as the syntax for libedit is different.
1515 if not readline.uses_libedit:
1508 if not readline.uses_libedit:
1516 for rlcommand in self.readline_parse_and_bind:
1509 for rlcommand in self.readline_parse_and_bind:
1517 #print "loading rl:",rlcommand # dbg
1510 #print "loading rl:",rlcommand # dbg
1518 readline.parse_and_bind(rlcommand)
1511 readline.parse_and_bind(rlcommand)
1519
1512
1520 # Remove some chars from the delimiters list. If we encounter
1513 # Remove some chars from the delimiters list. If we encounter
1521 # unicode chars, discard them.
1514 # unicode chars, discard them.
1522 delims = readline.get_completer_delims().encode("ascii", "ignore")
1515 delims = readline.get_completer_delims().encode("ascii", "ignore")
1523 delims = delims.translate(string._idmap,
1516 delims = delims.translate(string._idmap,
1524 self.readline_remove_delims)
1517 self.readline_remove_delims)
1525 readline.set_completer_delims(delims)
1518 readline.set_completer_delims(delims)
1526 # otherwise we end up with a monster history after a while:
1519 # otherwise we end up with a monster history after a while:
1527 readline.set_history_length(1000)
1520 readline.set_history_length(1000)
1528 try:
1521 try:
1529 #print '*** Reading readline history' # dbg
1522 #print '*** Reading readline history' # dbg
1530 readline.read_history_file(self.histfile)
1523 readline.read_history_file(self.histfile)
1531 except IOError:
1524 except IOError:
1532 pass # It doesn't exist yet.
1525 pass # It doesn't exist yet.
1533
1526
1534 atexit.register(self.atexit_operations)
1527 atexit.register(self.atexit_operations)
1535 del atexit
1528 del atexit
1536
1529
1537 # Configure auto-indent for all platforms
1530 # Configure auto-indent for all platforms
1538 self.set_autoindent(self.autoindent)
1531 self.set_autoindent(self.autoindent)
1539
1532
1540 def set_next_input(self, s):
1533 def set_next_input(self, s):
1541 """ Sets the 'default' input string for the next command line.
1534 """ Sets the 'default' input string for the next command line.
1542
1535
1543 Requires readline.
1536 Requires readline.
1544
1537
1545 Example:
1538 Example:
1546
1539
1547 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1540 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1548 [D:\ipython]|2> Hello Word_ # cursor is here
1541 [D:\ipython]|2> Hello Word_ # cursor is here
1549 """
1542 """
1550
1543
1551 self.rl_next_input = s
1544 self.rl_next_input = s
1552
1545
1553 def pre_readline(self):
1546 def pre_readline(self):
1554 """readline hook to be used at the start of each line.
1547 """readline hook to be used at the start of each line.
1555
1548
1556 Currently it handles auto-indent only."""
1549 Currently it handles auto-indent only."""
1557
1550
1558 #debugx('self.indent_current_nsp','pre_readline:')
1551 #debugx('self.indent_current_nsp','pre_readline:')
1559
1552
1560 if self.rl_do_indent:
1553 if self.rl_do_indent:
1561 self.readline.insert_text(self._indent_current_str())
1554 self.readline.insert_text(self._indent_current_str())
1562 if self.rl_next_input is not None:
1555 if self.rl_next_input is not None:
1563 self.readline.insert_text(self.rl_next_input)
1556 self.readline.insert_text(self.rl_next_input)
1564 self.rl_next_input = None
1557 self.rl_next_input = None
1565
1558
1566 def _indent_current_str(self):
1559 def _indent_current_str(self):
1567 """return the current level of indentation as a string"""
1560 """return the current level of indentation as a string"""
1568 return self.indent_current_nsp * ' '
1561 return self.indent_current_nsp * ' '
1569
1562
1570 #-------------------------------------------------------------------------
1563 #-------------------------------------------------------------------------
1571 # Things related to magics
1564 # Things related to magics
1572 #-------------------------------------------------------------------------
1565 #-------------------------------------------------------------------------
1573
1566
1574 def init_magics(self):
1567 def init_magics(self):
1575 # Set user colors (don't do it in the constructor above so that it
1568 # Set user colors (don't do it in the constructor above so that it
1576 # doesn't crash if colors option is invalid)
1569 # doesn't crash if colors option is invalid)
1577 self.magic_colors(self.colors)
1570 self.magic_colors(self.colors)
1578
1571
1579 def magic(self,arg_s):
1572 def magic(self,arg_s):
1580 """Call a magic function by name.
1573 """Call a magic function by name.
1581
1574
1582 Input: a string containing the name of the magic function to call and any
1575 Input: a string containing the name of the magic function to call and any
1583 additional arguments to be passed to the magic.
1576 additional arguments to be passed to the magic.
1584
1577
1585 magic('name -opt foo bar') is equivalent to typing at the ipython
1578 magic('name -opt foo bar') is equivalent to typing at the ipython
1586 prompt:
1579 prompt:
1587
1580
1588 In[1]: %name -opt foo bar
1581 In[1]: %name -opt foo bar
1589
1582
1590 To call a magic without arguments, simply use magic('name').
1583 To call a magic without arguments, simply use magic('name').
1591
1584
1592 This provides a proper Python function to call IPython's magics in any
1585 This provides a proper Python function to call IPython's magics in any
1593 valid Python code you can type at the interpreter, including loops and
1586 valid Python code you can type at the interpreter, including loops and
1594 compound statements.
1587 compound statements.
1595 """
1588 """
1596
1589
1597 args = arg_s.split(' ',1)
1590 args = arg_s.split(' ',1)
1598 magic_name = args[0]
1591 magic_name = args[0]
1599 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1592 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1600
1593
1601 try:
1594 try:
1602 magic_args = args[1]
1595 magic_args = args[1]
1603 except IndexError:
1596 except IndexError:
1604 magic_args = ''
1597 magic_args = ''
1605 fn = getattr(self,'magic_'+magic_name,None)
1598 fn = getattr(self,'magic_'+magic_name,None)
1606 if fn is None:
1599 if fn is None:
1607 error("Magic function `%s` not found." % magic_name)
1600 error("Magic function `%s` not found." % magic_name)
1608 else:
1601 else:
1609 magic_args = self.var_expand(magic_args,1)
1602 magic_args = self.var_expand(magic_args,1)
1610 with nested(self.builtin_trap, self.display_trap):
1603 with nested(self.builtin_trap,):
1611 return fn(magic_args)
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 def define_magic(self, magicname, func):
1609 def define_magic(self, magicname, func):
1614 """Expose own function as magic function for ipython
1610 """Expose own function as magic function for ipython
1615
1611
1616 def foo_impl(self,parameter_s=''):
1612 def foo_impl(self,parameter_s=''):
1617 'My very own magic!. (Use docstrings, IPython reads them).'
1613 'My very own magic!. (Use docstrings, IPython reads them).'
1618 print 'Magic function. Passed parameter is between < >:'
1614 print 'Magic function. Passed parameter is between < >:'
1619 print '<%s>' % parameter_s
1615 print '<%s>' % parameter_s
1620 print 'The self object is:',self
1616 print 'The self object is:',self
1621
1617
1622 self.define_magic('foo',foo_impl)
1618 self.define_magic('foo',foo_impl)
1623 """
1619 """
1624
1620
1625 import new
1621 import new
1626 im = new.instancemethod(func,self, self.__class__)
1622 im = new.instancemethod(func,self, self.__class__)
1627 old = getattr(self, "magic_" + magicname, None)
1623 old = getattr(self, "magic_" + magicname, None)
1628 setattr(self, "magic_" + magicname, im)
1624 setattr(self, "magic_" + magicname, im)
1629 return old
1625 return old
1630
1626
1631 #-------------------------------------------------------------------------
1627 #-------------------------------------------------------------------------
1632 # Things related to macros
1628 # Things related to macros
1633 #-------------------------------------------------------------------------
1629 #-------------------------------------------------------------------------
1634
1630
1635 def define_macro(self, name, themacro):
1631 def define_macro(self, name, themacro):
1636 """Define a new macro
1632 """Define a new macro
1637
1633
1638 Parameters
1634 Parameters
1639 ----------
1635 ----------
1640 name : str
1636 name : str
1641 The name of the macro.
1637 The name of the macro.
1642 themacro : str or Macro
1638 themacro : str or Macro
1643 The action to do upon invoking the macro. If a string, a new
1639 The action to do upon invoking the macro. If a string, a new
1644 Macro object is created by passing the string to it.
1640 Macro object is created by passing the string to it.
1645 """
1641 """
1646
1642
1647 from IPython.core import macro
1643 from IPython.core import macro
1648
1644
1649 if isinstance(themacro, basestring):
1645 if isinstance(themacro, basestring):
1650 themacro = macro.Macro(themacro)
1646 themacro = macro.Macro(themacro)
1651 if not isinstance(themacro, macro.Macro):
1647 if not isinstance(themacro, macro.Macro):
1652 raise ValueError('A macro must be a string or a Macro instance.')
1648 raise ValueError('A macro must be a string or a Macro instance.')
1653 self.user_ns[name] = themacro
1649 self.user_ns[name] = themacro
1654
1650
1655 #-------------------------------------------------------------------------
1651 #-------------------------------------------------------------------------
1656 # Things related to the running of system commands
1652 # Things related to the running of system commands
1657 #-------------------------------------------------------------------------
1653 #-------------------------------------------------------------------------
1658
1654
1659 def system(self, cmd):
1655 def system(self, cmd):
1660 """Make a system call, using IPython."""
1656 """Make a system call, using IPython."""
1661 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1657 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1662
1658
1663 #-------------------------------------------------------------------------
1659 #-------------------------------------------------------------------------
1664 # Things related to aliases
1660 # Things related to aliases
1665 #-------------------------------------------------------------------------
1661 #-------------------------------------------------------------------------
1666
1662
1667 def init_alias(self):
1663 def init_alias(self):
1668 self.alias_manager = AliasManager(self, config=self.config)
1664 self.alias_manager = AliasManager(self, config=self.config)
1665 self.ns_table['alias'] = self.alias_manager.alias_table,
1669
1666
1670 #-------------------------------------------------------------------------
1667 #-------------------------------------------------------------------------
1671 # Things related to the running of code
1668 # Things related to the running of code
1672 #-------------------------------------------------------------------------
1669 #-------------------------------------------------------------------------
1673
1670
1674 def ex(self, cmd):
1671 def ex(self, cmd):
1675 """Execute a normal python statement in user namespace."""
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 exec cmd in self.user_global_ns, self.user_ns
1674 exec cmd in self.user_global_ns, self.user_ns
1678
1675
1679 def ev(self, expr):
1676 def ev(self, expr):
1680 """Evaluate python expression expr in user namespace.
1677 """Evaluate python expression expr in user namespace.
1681
1678
1682 Returns the result of evaluation
1679 Returns the result of evaluation
1683 """
1680 """
1684 with nested(self.builtin_trap, self.display_trap):
1681 with nested(self.builtin_trap,):
1685 return eval(expr, self.user_global_ns, self.user_ns)
1682 return eval(expr, self.user_global_ns, self.user_ns)
1686
1683
1687 def mainloop(self, banner=None):
1684 def mainloop(self, banner=None):
1688 """Start the mainloop.
1685 """Start the mainloop.
1689
1686
1690 If an optional banner argument is given, it will override the
1687 If an optional banner argument is given, it will override the
1691 internally created default banner.
1688 internally created default banner.
1692 """
1689 """
1693
1690
1694 with nested(self.builtin_trap, self.display_trap):
1691 with nested(self.builtin_trap, self.display_trap):
1695 if self.c: # Emulate Python's -c option
1692 if self.c: # Emulate Python's -c option
1696 self.exec_init_cmd()
1693 self.exec_init_cmd()
1697
1694
1698 if self.display_banner:
1695 if self.display_banner:
1699 if banner is None:
1696 if banner is None:
1700 banner = self.banner
1697 banner = self.banner
1701
1698
1702 # if you run stuff with -c <cmd>, raw hist is not updated
1699 # if you run stuff with -c <cmd>, raw hist is not updated
1703 # ensure that it's in sync
1700 # ensure that it's in sync
1704 if len(self.input_hist) != len (self.input_hist_raw):
1701 if len(self.input_hist) != len (self.input_hist_raw):
1705 self.input_hist_raw = InputList(self.input_hist)
1702 self.input_hist_raw = InputList(self.input_hist)
1706
1703
1707 while 1:
1704 while 1:
1708 try:
1705 try:
1709 self.interact()
1706 self.interact()
1710 #self.interact_with_readline()
1707 #self.interact_with_readline()
1711 # XXX for testing of a readline-decoupled repl loop, call
1708 # XXX for testing of a readline-decoupled repl loop, call
1712 # interact_with_readline above
1709 # interact_with_readline above
1713 break
1710 break
1714 except KeyboardInterrupt:
1711 except KeyboardInterrupt:
1715 # this should not be necessary, but KeyboardInterrupt
1712 # this should not be necessary, but KeyboardInterrupt
1716 # handling seems rather unpredictable...
1713 # handling seems rather unpredictable...
1717 self.write("\nKeyboardInterrupt in interact()\n")
1714 self.write("\nKeyboardInterrupt in interact()\n")
1718
1715
1719 def exec_init_cmd(self):
1716 def exec_init_cmd(self):
1720 """Execute a command given at the command line.
1717 """Execute a command given at the command line.
1721
1718
1722 This emulates Python's -c option."""
1719 This emulates Python's -c option."""
1723
1720
1724 #sys.argv = ['-c']
1721 #sys.argv = ['-c']
1725 self.push_line(self.prefilter_manager.prefilter_lines(self.c, False))
1722 self.push_line(self.prefilter_manager.prefilter_lines(self.c, False))
1726 if not self.interactive:
1723 if not self.interactive:
1727 self.ask_exit()
1724 self.ask_exit()
1728
1725
1729 def interact_prompt(self):
1726 def interact_prompt(self):
1730 """ Print the prompt (in read-eval-print loop)
1727 """ Print the prompt (in read-eval-print loop)
1731
1728
1732 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1729 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1733 used in standard IPython flow.
1730 used in standard IPython flow.
1734 """
1731 """
1735 if self.more:
1732 if self.more:
1736 try:
1733 try:
1737 prompt = self.hooks.generate_prompt(True)
1734 prompt = self.hooks.generate_prompt(True)
1738 except:
1735 except:
1739 self.showtraceback()
1736 self.showtraceback()
1740 if self.autoindent:
1737 if self.autoindent:
1741 self.rl_do_indent = True
1738 self.rl_do_indent = True
1742
1739
1743 else:
1740 else:
1744 try:
1741 try:
1745 prompt = self.hooks.generate_prompt(False)
1742 prompt = self.hooks.generate_prompt(False)
1746 except:
1743 except:
1747 self.showtraceback()
1744 self.showtraceback()
1748 self.write(prompt)
1745 self.write(prompt)
1749
1746
1750 def interact_handle_input(self,line):
1747 def interact_handle_input(self,line):
1751 """ Handle the input line (in read-eval-print loop)
1748 """ Handle the input line (in read-eval-print loop)
1752
1749
1753 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1750 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1754 used in standard IPython flow.
1751 used in standard IPython flow.
1755 """
1752 """
1756 if line.lstrip() == line:
1753 if line.lstrip() == line:
1757 self.shadowhist.add(line.strip())
1754 self.shadowhist.add(line.strip())
1758 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1755 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1759
1756
1760 if line.strip():
1757 if line.strip():
1761 if self.more:
1758 if self.more:
1762 self.input_hist_raw[-1] += '%s\n' % line
1759 self.input_hist_raw[-1] += '%s\n' % line
1763 else:
1760 else:
1764 self.input_hist_raw.append('%s\n' % line)
1761 self.input_hist_raw.append('%s\n' % line)
1765
1762
1766
1763
1767 self.more = self.push_line(lineout)
1764 self.more = self.push_line(lineout)
1768 if (self.SyntaxTB.last_syntax_error and
1765 if (self.SyntaxTB.last_syntax_error and
1769 self.autoedit_syntax):
1766 self.autoedit_syntax):
1770 self.edit_syntax_error()
1767 self.edit_syntax_error()
1771
1768
1772 def interact_with_readline(self):
1769 def interact_with_readline(self):
1773 """ Demo of using interact_handle_input, interact_prompt
1770 """ Demo of using interact_handle_input, interact_prompt
1774
1771
1775 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1772 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1776 it should work like this.
1773 it should work like this.
1777 """
1774 """
1778 self.readline_startup_hook(self.pre_readline)
1775 self.readline_startup_hook(self.pre_readline)
1779 while not self.exit_now:
1776 while not self.exit_now:
1780 self.interact_prompt()
1777 self.interact_prompt()
1781 if self.more:
1778 if self.more:
1782 self.rl_do_indent = True
1779 self.rl_do_indent = True
1783 else:
1780 else:
1784 self.rl_do_indent = False
1781 self.rl_do_indent = False
1785 line = raw_input_original().decode(self.stdin_encoding)
1782 line = raw_input_original().decode(self.stdin_encoding)
1786 self.interact_handle_input(line)
1783 self.interact_handle_input(line)
1787
1784
1788 def interact(self, banner=None):
1785 def interact(self, banner=None):
1789 """Closely emulate the interactive Python console."""
1786 """Closely emulate the interactive Python console."""
1790
1787
1791 # batch run -> do not interact
1788 # batch run -> do not interact
1792 if self.exit_now:
1789 if self.exit_now:
1793 return
1790 return
1794
1791
1795 if self.display_banner:
1792 if self.display_banner:
1796 if banner is None:
1793 if banner is None:
1797 banner = self.banner
1794 banner = self.banner
1798 self.write(banner)
1795 self.write(banner)
1799
1796
1800 more = 0
1797 more = 0
1801
1798
1802 # Mark activity in the builtins
1799 # Mark activity in the builtins
1803 __builtin__.__dict__['__IPYTHON__active'] += 1
1800 __builtin__.__dict__['__IPYTHON__active'] += 1
1804
1801
1805 if self.has_readline:
1802 if self.has_readline:
1806 self.readline_startup_hook(self.pre_readline)
1803 self.readline_startup_hook(self.pre_readline)
1807 # exit_now is set by a call to %Exit or %Quit, through the
1804 # exit_now is set by a call to %Exit or %Quit, through the
1808 # ask_exit callback.
1805 # ask_exit callback.
1809
1806
1810 while not self.exit_now:
1807 while not self.exit_now:
1811 self.hooks.pre_prompt_hook()
1808 self.hooks.pre_prompt_hook()
1812 if more:
1809 if more:
1813 try:
1810 try:
1814 prompt = self.hooks.generate_prompt(True)
1811 prompt = self.hooks.generate_prompt(True)
1815 except:
1812 except:
1816 self.showtraceback()
1813 self.showtraceback()
1817 if self.autoindent:
1814 if self.autoindent:
1818 self.rl_do_indent = True
1815 self.rl_do_indent = True
1819
1816
1820 else:
1817 else:
1821 try:
1818 try:
1822 prompt = self.hooks.generate_prompt(False)
1819 prompt = self.hooks.generate_prompt(False)
1823 except:
1820 except:
1824 self.showtraceback()
1821 self.showtraceback()
1825 try:
1822 try:
1826 line = self.raw_input(prompt, more)
1823 line = self.raw_input(prompt, more)
1827 if self.exit_now:
1824 if self.exit_now:
1828 # quick exit on sys.std[in|out] close
1825 # quick exit on sys.std[in|out] close
1829 break
1826 break
1830 if self.autoindent:
1827 if self.autoindent:
1831 self.rl_do_indent = False
1828 self.rl_do_indent = False
1832
1829
1833 except KeyboardInterrupt:
1830 except KeyboardInterrupt:
1834 #double-guard against keyboardinterrupts during kbdint handling
1831 #double-guard against keyboardinterrupts during kbdint handling
1835 try:
1832 try:
1836 self.write('\nKeyboardInterrupt\n')
1833 self.write('\nKeyboardInterrupt\n')
1837 self.resetbuffer()
1834 self.resetbuffer()
1838 # keep cache in sync with the prompt counter:
1835 # keep cache in sync with the prompt counter:
1839 self.outputcache.prompt_count -= 1
1836 self.outputcache.prompt_count -= 1
1840
1837
1841 if self.autoindent:
1838 if self.autoindent:
1842 self.indent_current_nsp = 0
1839 self.indent_current_nsp = 0
1843 more = 0
1840 more = 0
1844 except KeyboardInterrupt:
1841 except KeyboardInterrupt:
1845 pass
1842 pass
1846 except EOFError:
1843 except EOFError:
1847 if self.autoindent:
1844 if self.autoindent:
1848 self.rl_do_indent = False
1845 self.rl_do_indent = False
1849 self.readline_startup_hook(None)
1846 self.readline_startup_hook(None)
1850 self.write('\n')
1847 self.write('\n')
1851 self.exit()
1848 self.exit()
1852 except bdb.BdbQuit:
1849 except bdb.BdbQuit:
1853 warn('The Python debugger has exited with a BdbQuit exception.\n'
1850 warn('The Python debugger has exited with a BdbQuit exception.\n'
1854 'Because of how pdb handles the stack, it is impossible\n'
1851 'Because of how pdb handles the stack, it is impossible\n'
1855 'for IPython to properly format this particular exception.\n'
1852 'for IPython to properly format this particular exception.\n'
1856 'IPython will resume normal operation.')
1853 'IPython will resume normal operation.')
1857 except:
1854 except:
1858 # exceptions here are VERY RARE, but they can be triggered
1855 # exceptions here are VERY RARE, but they can be triggered
1859 # asynchronously by signal handlers, for example.
1856 # asynchronously by signal handlers, for example.
1860 self.showtraceback()
1857 self.showtraceback()
1861 else:
1858 else:
1862 more = self.push_line(line)
1859 more = self.push_line(line)
1863 if (self.SyntaxTB.last_syntax_error and
1860 if (self.SyntaxTB.last_syntax_error and
1864 self.autoedit_syntax):
1861 self.autoedit_syntax):
1865 self.edit_syntax_error()
1862 self.edit_syntax_error()
1866
1863
1867 # We are off again...
1864 # We are off again...
1868 __builtin__.__dict__['__IPYTHON__active'] -= 1
1865 __builtin__.__dict__['__IPYTHON__active'] -= 1
1869
1866
1870 def safe_execfile(self,fname,*where,**kw):
1867 def safe_execfile(self,fname,*where,**kw):
1871 """A safe version of the builtin execfile().
1868 """A safe version of the builtin execfile().
1872
1869
1873 This version will never throw an exception, and knows how to handle
1870 This version will never throw an exception, and knows how to handle
1874 ipython logs as well.
1871 ipython logs as well.
1875
1872
1876 :Parameters:
1873 :Parameters:
1877 fname : string
1874 fname : string
1878 Name of the file to be executed.
1875 Name of the file to be executed.
1879
1876
1880 where : tuple
1877 where : tuple
1881 One or two namespaces, passed to execfile() as (globals,locals).
1878 One or two namespaces, passed to execfile() as (globals,locals).
1882 If only one is given, it is passed as both.
1879 If only one is given, it is passed as both.
1883
1880
1884 :Keywords:
1881 :Keywords:
1885 islog : boolean (False)
1882 islog : boolean (False)
1886
1883
1887 quiet : boolean (True)
1884 quiet : boolean (True)
1888
1885
1889 exit_ignore : boolean (False)
1886 exit_ignore : boolean (False)
1890 """
1887 """
1891
1888
1892 def syspath_cleanup():
1889 def syspath_cleanup():
1893 """Internal cleanup routine for sys.path."""
1890 """Internal cleanup routine for sys.path."""
1894 if add_dname:
1891 if add_dname:
1895 try:
1892 try:
1896 sys.path.remove(dname)
1893 sys.path.remove(dname)
1897 except ValueError:
1894 except ValueError:
1898 # For some reason the user has already removed it, ignore.
1895 # For some reason the user has already removed it, ignore.
1899 pass
1896 pass
1900
1897
1901 fname = os.path.expanduser(fname)
1898 fname = os.path.expanduser(fname)
1902
1899
1903 # Find things also in current directory. This is needed to mimic the
1900 # Find things also in current directory. This is needed to mimic the
1904 # behavior of running a script from the system command line, where
1901 # behavior of running a script from the system command line, where
1905 # Python inserts the script's directory into sys.path
1902 # Python inserts the script's directory into sys.path
1906 dname = os.path.dirname(os.path.abspath(fname))
1903 dname = os.path.dirname(os.path.abspath(fname))
1907 add_dname = False
1904 add_dname = False
1908 if dname not in sys.path:
1905 if dname not in sys.path:
1909 sys.path.insert(0,dname)
1906 sys.path.insert(0,dname)
1910 add_dname = True
1907 add_dname = True
1911
1908
1912 try:
1909 try:
1913 xfile = open(fname)
1910 xfile = open(fname)
1914 except:
1911 except:
1915 print >> Term.cerr, \
1912 print >> Term.cerr, \
1916 'Could not open file <%s> for safe execution.' % fname
1913 'Could not open file <%s> for safe execution.' % fname
1917 syspath_cleanup()
1914 syspath_cleanup()
1918 return None
1915 return None
1919
1916
1920 kw.setdefault('islog',0)
1917 kw.setdefault('islog',0)
1921 kw.setdefault('quiet',1)
1918 kw.setdefault('quiet',1)
1922 kw.setdefault('exit_ignore',0)
1919 kw.setdefault('exit_ignore',0)
1923
1920
1924 first = xfile.readline()
1921 first = xfile.readline()
1925 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
1922 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
1926 xfile.close()
1923 xfile.close()
1927 # line by line execution
1924 # line by line execution
1928 if first.startswith(loghead) or kw['islog']:
1925 if first.startswith(loghead) or kw['islog']:
1929 print 'Loading log file <%s> one line at a time...' % fname
1926 print 'Loading log file <%s> one line at a time...' % fname
1930 if kw['quiet']:
1927 if kw['quiet']:
1931 stdout_save = sys.stdout
1928 stdout_save = sys.stdout
1932 sys.stdout = StringIO.StringIO()
1929 sys.stdout = StringIO.StringIO()
1933 try:
1930 try:
1934 globs,locs = where[0:2]
1931 globs,locs = where[0:2]
1935 except:
1932 except:
1936 try:
1933 try:
1937 globs = locs = where[0]
1934 globs = locs = where[0]
1938 except:
1935 except:
1939 globs = locs = globals()
1936 globs = locs = globals()
1940 badblocks = []
1937 badblocks = []
1941
1938
1942 # we also need to identify indented blocks of code when replaying
1939 # we also need to identify indented blocks of code when replaying
1943 # logs and put them together before passing them to an exec
1940 # logs and put them together before passing them to an exec
1944 # statement. This takes a bit of regexp and look-ahead work in the
1941 # statement. This takes a bit of regexp and look-ahead work in the
1945 # file. It's easiest if we swallow the whole thing in memory
1942 # file. It's easiest if we swallow the whole thing in memory
1946 # first, and manually walk through the lines list moving the
1943 # first, and manually walk through the lines list moving the
1947 # counter ourselves.
1944 # counter ourselves.
1948 indent_re = re.compile('\s+\S')
1945 indent_re = re.compile('\s+\S')
1949 xfile = open(fname)
1946 xfile = open(fname)
1950 filelines = xfile.readlines()
1947 filelines = xfile.readlines()
1951 xfile.close()
1948 xfile.close()
1952 nlines = len(filelines)
1949 nlines = len(filelines)
1953 lnum = 0
1950 lnum = 0
1954 while lnum < nlines:
1951 while lnum < nlines:
1955 line = filelines[lnum]
1952 line = filelines[lnum]
1956 lnum += 1
1953 lnum += 1
1957 # don't re-insert logger status info into cache
1954 # don't re-insert logger status info into cache
1958 if line.startswith('#log#'):
1955 if line.startswith('#log#'):
1959 continue
1956 continue
1960 else:
1957 else:
1961 # build a block of code (maybe a single line) for execution
1958 # build a block of code (maybe a single line) for execution
1962 block = line
1959 block = line
1963 try:
1960 try:
1964 next = filelines[lnum] # lnum has already incremented
1961 next = filelines[lnum] # lnum has already incremented
1965 except:
1962 except:
1966 next = None
1963 next = None
1967 while next and indent_re.match(next):
1964 while next and indent_re.match(next):
1968 block += next
1965 block += next
1969 lnum += 1
1966 lnum += 1
1970 try:
1967 try:
1971 next = filelines[lnum]
1968 next = filelines[lnum]
1972 except:
1969 except:
1973 next = None
1970 next = None
1974 # now execute the block of one or more lines
1971 # now execute the block of one or more lines
1975 try:
1972 try:
1976 exec block in globs,locs
1973 exec block in globs,locs
1977 except SystemExit:
1974 except SystemExit:
1978 pass
1975 pass
1979 except:
1976 except:
1980 badblocks.append(block.rstrip())
1977 badblocks.append(block.rstrip())
1981 if kw['quiet']: # restore stdout
1978 if kw['quiet']: # restore stdout
1982 sys.stdout.close()
1979 sys.stdout.close()
1983 sys.stdout = stdout_save
1980 sys.stdout = stdout_save
1984 print 'Finished replaying log file <%s>' % fname
1981 print 'Finished replaying log file <%s>' % fname
1985 if badblocks:
1982 if badblocks:
1986 print >> sys.stderr, ('\nThe following lines/blocks in file '
1983 print >> sys.stderr, ('\nThe following lines/blocks in file '
1987 '<%s> reported errors:' % fname)
1984 '<%s> reported errors:' % fname)
1988
1985
1989 for badline in badblocks:
1986 for badline in badblocks:
1990 print >> sys.stderr, badline
1987 print >> sys.stderr, badline
1991 else: # regular file execution
1988 else: # regular file execution
1992 try:
1989 try:
1993 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1990 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1994 # Work around a bug in Python for Windows. The bug was
1991 # Work around a bug in Python for Windows. The bug was
1995 # fixed in in Python 2.5 r54159 and 54158, but that's still
1992 # fixed in in Python 2.5 r54159 and 54158, but that's still
1996 # SVN Python as of March/07. For details, see:
1993 # SVN Python as of March/07. For details, see:
1997 # http://projects.scipy.org/ipython/ipython/ticket/123
1994 # http://projects.scipy.org/ipython/ipython/ticket/123
1998 try:
1995 try:
1999 globs,locs = where[0:2]
1996 globs,locs = where[0:2]
2000 except:
1997 except:
2001 try:
1998 try:
2002 globs = locs = where[0]
1999 globs = locs = where[0]
2003 except:
2000 except:
2004 globs = locs = globals()
2001 globs = locs = globals()
2005 exec file(fname) in globs,locs
2002 exec file(fname) in globs,locs
2006 else:
2003 else:
2007 execfile(fname,*where)
2004 execfile(fname,*where)
2008 except SyntaxError:
2005 except SyntaxError:
2009 self.showsyntaxerror()
2006 self.showsyntaxerror()
2010 warn('Failure executing file: <%s>' % fname)
2007 warn('Failure executing file: <%s>' % fname)
2011 except SystemExit,status:
2008 except SystemExit,status:
2012 # Code that correctly sets the exit status flag to success (0)
2009 # Code that correctly sets the exit status flag to success (0)
2013 # shouldn't be bothered with a traceback. Note that a plain
2010 # shouldn't be bothered with a traceback. Note that a plain
2014 # sys.exit() does NOT set the message to 0 (it's empty) so that
2011 # sys.exit() does NOT set the message to 0 (it's empty) so that
2015 # will still get a traceback. Note that the structure of the
2012 # will still get a traceback. Note that the structure of the
2016 # SystemExit exception changed between Python 2.4 and 2.5, so
2013 # SystemExit exception changed between Python 2.4 and 2.5, so
2017 # the checks must be done in a version-dependent way.
2014 # the checks must be done in a version-dependent way.
2018 show = False
2015 show = False
2019
2016
2020 if sys.version_info[:2] > (2,5):
2017 if sys.version_info[:2] > (2,5):
2021 if status.message!=0 and not kw['exit_ignore']:
2018 if status.message!=0 and not kw['exit_ignore']:
2022 show = True
2019 show = True
2023 else:
2020 else:
2024 if status.code and not kw['exit_ignore']:
2021 if status.code and not kw['exit_ignore']:
2025 show = True
2022 show = True
2026 if show:
2023 if show:
2027 self.showtraceback()
2024 self.showtraceback()
2028 warn('Failure executing file: <%s>' % fname)
2025 warn('Failure executing file: <%s>' % fname)
2029 except:
2026 except:
2030 self.showtraceback()
2027 self.showtraceback()
2031 warn('Failure executing file: <%s>' % fname)
2028 warn('Failure executing file: <%s>' % fname)
2032
2029
2033 syspath_cleanup()
2030 syspath_cleanup()
2034
2031
2035 def cleanup_ipy_script(self, script):
2032 def cleanup_ipy_script(self, script):
2036 """Make a script safe for self.runlines()
2033 """Make a script safe for self.runlines()
2037
2034
2038 Notes
2035 Notes
2039 -----
2036 -----
2040 This was copied over from the old ipapi and probably can be done
2037 This was copied over from the old ipapi and probably can be done
2041 away with once we move to block based interpreter.
2038 away with once we move to block based interpreter.
2042
2039
2043 - Removes empty lines Suffixes all indented blocks that end with
2040 - Removes empty lines Suffixes all indented blocks that end with
2044 - unindented lines with empty lines
2041 - unindented lines with empty lines
2045 """
2042 """
2046
2043
2047 res = []
2044 res = []
2048 lines = script.splitlines()
2045 lines = script.splitlines()
2049
2046
2050 level = 0
2047 level = 0
2051 for l in lines:
2048 for l in lines:
2052 lstripped = l.lstrip()
2049 lstripped = l.lstrip()
2053 stripped = l.strip()
2050 stripped = l.strip()
2054 if not stripped:
2051 if not stripped:
2055 continue
2052 continue
2056 newlevel = len(l) - len(lstripped)
2053 newlevel = len(l) - len(lstripped)
2057 def is_secondary_block_start(s):
2054 def is_secondary_block_start(s):
2058 if not s.endswith(':'):
2055 if not s.endswith(':'):
2059 return False
2056 return False
2060 if (s.startswith('elif') or
2057 if (s.startswith('elif') or
2061 s.startswith('else') or
2058 s.startswith('else') or
2062 s.startswith('except') or
2059 s.startswith('except') or
2063 s.startswith('finally')):
2060 s.startswith('finally')):
2064 return True
2061 return True
2065
2062
2066 if level > 0 and newlevel == 0 and \
2063 if level > 0 and newlevel == 0 and \
2067 not is_secondary_block_start(stripped):
2064 not is_secondary_block_start(stripped):
2068 # add empty line
2065 # add empty line
2069 res.append('')
2066 res.append('')
2070
2067
2071 res.append(l)
2068 res.append(l)
2072 level = newlevel
2069 level = newlevel
2073 return '\n'.join(res) + '\n'
2070 return '\n'.join(res) + '\n'
2074
2071
2075 def runlines(self, lines, clean=False):
2072 def runlines(self, lines, clean=False):
2076 """Run a string of one or more lines of source.
2073 """Run a string of one or more lines of source.
2077
2074
2078 This method is capable of running a string containing multiple source
2075 This method is capable of running a string containing multiple source
2079 lines, as if they had been entered at the IPython prompt. Since it
2076 lines, as if they had been entered at the IPython prompt. Since it
2080 exposes IPython's processing machinery, the given strings can contain
2077 exposes IPython's processing machinery, the given strings can contain
2081 magic calls (%magic), special shell access (!cmd), etc.
2078 magic calls (%magic), special shell access (!cmd), etc.
2082 """
2079 """
2083
2080
2084 if isinstance(lines, (list, tuple)):
2081 if isinstance(lines, (list, tuple)):
2085 lines = '\n'.join(lines)
2082 lines = '\n'.join(lines)
2086
2083
2087 if clean:
2084 if clean:
2088 lines = self.cleanup_ipy_script(lines)
2085 lines = self.cleanup_ipy_script(lines)
2089
2086
2090 # We must start with a clean buffer, in case this is run from an
2087 # We must start with a clean buffer, in case this is run from an
2091 # interactive IPython session (via a magic, for example).
2088 # interactive IPython session (via a magic, for example).
2092 self.resetbuffer()
2089 self.resetbuffer()
2093 lines = lines.splitlines()
2090 lines = lines.splitlines()
2094 more = 0
2091 more = 0
2095
2092
2096 with nested(self.builtin_trap, self.display_trap):
2093 with nested(self.builtin_trap, self.display_trap):
2097 for line in lines:
2094 for line in lines:
2098 # skip blank lines so we don't mess up the prompt counter, but do
2095 # skip blank lines so we don't mess up the prompt counter, but do
2099 # NOT skip even a blank line if we are in a code block (more is
2096 # NOT skip even a blank line if we are in a code block (more is
2100 # true)
2097 # true)
2101
2098
2102 if line or more:
2099 if line or more:
2103 # push to raw history, so hist line numbers stay in sync
2100 # push to raw history, so hist line numbers stay in sync
2104 self.input_hist_raw.append("# " + line + "\n")
2101 self.input_hist_raw.append("# " + line + "\n")
2105 more = self.push_line(self.prefilter_manager.prefilter_lines(line,more))
2102 more = self.push_line(self.prefilter_manager.prefilter_lines(line,more))
2106 # IPython's runsource returns None if there was an error
2103 # IPython's runsource returns None if there was an error
2107 # compiling the code. This allows us to stop processing right
2104 # compiling the code. This allows us to stop processing right
2108 # away, so the user gets the error message at the right place.
2105 # away, so the user gets the error message at the right place.
2109 if more is None:
2106 if more is None:
2110 break
2107 break
2111 else:
2108 else:
2112 self.input_hist_raw.append("\n")
2109 self.input_hist_raw.append("\n")
2113 # final newline in case the input didn't have it, so that the code
2110 # final newline in case the input didn't have it, so that the code
2114 # actually does get executed
2111 # actually does get executed
2115 if more:
2112 if more:
2116 self.push_line('\n')
2113 self.push_line('\n')
2117
2114
2118 def runsource(self, source, filename='<input>', symbol='single'):
2115 def runsource(self, source, filename='<input>', symbol='single'):
2119 """Compile and run some source in the interpreter.
2116 """Compile and run some source in the interpreter.
2120
2117
2121 Arguments are as for compile_command().
2118 Arguments are as for compile_command().
2122
2119
2123 One several things can happen:
2120 One several things can happen:
2124
2121
2125 1) The input is incorrect; compile_command() raised an
2122 1) The input is incorrect; compile_command() raised an
2126 exception (SyntaxError or OverflowError). A syntax traceback
2123 exception (SyntaxError or OverflowError). A syntax traceback
2127 will be printed by calling the showsyntaxerror() method.
2124 will be printed by calling the showsyntaxerror() method.
2128
2125
2129 2) The input is incomplete, and more input is required;
2126 2) The input is incomplete, and more input is required;
2130 compile_command() returned None. Nothing happens.
2127 compile_command() returned None. Nothing happens.
2131
2128
2132 3) The input is complete; compile_command() returned a code
2129 3) The input is complete; compile_command() returned a code
2133 object. The code is executed by calling self.runcode() (which
2130 object. The code is executed by calling self.runcode() (which
2134 also handles run-time exceptions, except for SystemExit).
2131 also handles run-time exceptions, except for SystemExit).
2135
2132
2136 The return value is:
2133 The return value is:
2137
2134
2138 - True in case 2
2135 - True in case 2
2139
2136
2140 - False in the other cases, unless an exception is raised, where
2137 - False in the other cases, unless an exception is raised, where
2141 None is returned instead. This can be used by external callers to
2138 None is returned instead. This can be used by external callers to
2142 know whether to continue feeding input or not.
2139 know whether to continue feeding input or not.
2143
2140
2144 The return value can be used to decide whether to use sys.ps1 or
2141 The return value can be used to decide whether to use sys.ps1 or
2145 sys.ps2 to prompt the next line."""
2142 sys.ps2 to prompt the next line."""
2146
2143
2147 # if the source code has leading blanks, add 'if 1:\n' to it
2144 # if the source code has leading blanks, add 'if 1:\n' to it
2148 # this allows execution of indented pasted code. It is tempting
2145 # this allows execution of indented pasted code. It is tempting
2149 # to add '\n' at the end of source to run commands like ' a=1'
2146 # to add '\n' at the end of source to run commands like ' a=1'
2150 # directly, but this fails for more complicated scenarios
2147 # directly, but this fails for more complicated scenarios
2151 source=source.encode(self.stdin_encoding)
2148 source=source.encode(self.stdin_encoding)
2152 if source[:1] in [' ', '\t']:
2149 if source[:1] in [' ', '\t']:
2153 source = 'if 1:\n%s' % source
2150 source = 'if 1:\n%s' % source
2154
2151
2155 try:
2152 try:
2156 code = self.compile(source,filename,symbol)
2153 code = self.compile(source,filename,symbol)
2157 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2154 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2158 # Case 1
2155 # Case 1
2159 self.showsyntaxerror(filename)
2156 self.showsyntaxerror(filename)
2160 return None
2157 return None
2161
2158
2162 if code is None:
2159 if code is None:
2163 # Case 2
2160 # Case 2
2164 return True
2161 return True
2165
2162
2166 # Case 3
2163 # Case 3
2167 # We store the code object so that threaded shells and
2164 # We store the code object so that threaded shells and
2168 # custom exception handlers can access all this info if needed.
2165 # custom exception handlers can access all this info if needed.
2169 # The source corresponding to this can be obtained from the
2166 # The source corresponding to this can be obtained from the
2170 # buffer attribute as '\n'.join(self.buffer).
2167 # buffer attribute as '\n'.join(self.buffer).
2171 self.code_to_run = code
2168 self.code_to_run = code
2172 # now actually execute the code object
2169 # now actually execute the code object
2173 if self.runcode(code) == 0:
2170 if self.runcode(code) == 0:
2174 return False
2171 return False
2175 else:
2172 else:
2176 return None
2173 return None
2177
2174
2178 def runcode(self,code_obj):
2175 def runcode(self,code_obj):
2179 """Execute a code object.
2176 """Execute a code object.
2180
2177
2181 When an exception occurs, self.showtraceback() is called to display a
2178 When an exception occurs, self.showtraceback() is called to display a
2182 traceback.
2179 traceback.
2183
2180
2184 Return value: a flag indicating whether the code to be run completed
2181 Return value: a flag indicating whether the code to be run completed
2185 successfully:
2182 successfully:
2186
2183
2187 - 0: successful execution.
2184 - 0: successful execution.
2188 - 1: an error occurred.
2185 - 1: an error occurred.
2189 """
2186 """
2190
2187
2191 # Set our own excepthook in case the user code tries to call it
2188 # Set our own excepthook in case the user code tries to call it
2192 # directly, so that the IPython crash handler doesn't get triggered
2189 # directly, so that the IPython crash handler doesn't get triggered
2193 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2190 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2194
2191
2195 # we save the original sys.excepthook in the instance, in case config
2192 # we save the original sys.excepthook in the instance, in case config
2196 # code (such as magics) needs access to it.
2193 # code (such as magics) needs access to it.
2197 self.sys_excepthook = old_excepthook
2194 self.sys_excepthook = old_excepthook
2198 outflag = 1 # happens in more places, so it's easier as default
2195 outflag = 1 # happens in more places, so it's easier as default
2199 try:
2196 try:
2200 try:
2197 try:
2201 self.hooks.pre_runcode_hook()
2198 self.hooks.pre_runcode_hook()
2202 exec code_obj in self.user_global_ns, self.user_ns
2199 exec code_obj in self.user_global_ns, self.user_ns
2203 finally:
2200 finally:
2204 # Reset our crash handler in place
2201 # Reset our crash handler in place
2205 sys.excepthook = old_excepthook
2202 sys.excepthook = old_excepthook
2206 except SystemExit:
2203 except SystemExit:
2207 self.resetbuffer()
2204 self.resetbuffer()
2208 self.showtraceback()
2205 self.showtraceback()
2209 warn("Type %exit or %quit to exit IPython "
2206 warn("Type %exit or %quit to exit IPython "
2210 "(%Exit or %Quit do so unconditionally).",level=1)
2207 "(%Exit or %Quit do so unconditionally).",level=1)
2211 except self.custom_exceptions:
2208 except self.custom_exceptions:
2212 etype,value,tb = sys.exc_info()
2209 etype,value,tb = sys.exc_info()
2213 self.CustomTB(etype,value,tb)
2210 self.CustomTB(etype,value,tb)
2214 except:
2211 except:
2215 self.showtraceback()
2212 self.showtraceback()
2216 else:
2213 else:
2217 outflag = 0
2214 outflag = 0
2218 if softspace(sys.stdout, 0):
2215 if softspace(sys.stdout, 0):
2219 print
2216 print
2220 # Flush out code object which has been run (and source)
2217 # Flush out code object which has been run (and source)
2221 self.code_to_run = None
2218 self.code_to_run = None
2222 return outflag
2219 return outflag
2223
2220
2224 def push_line(self, line):
2221 def push_line(self, line):
2225 """Push a line to the interpreter.
2222 """Push a line to the interpreter.
2226
2223
2227 The line should not have a trailing newline; it may have
2224 The line should not have a trailing newline; it may have
2228 internal newlines. The line is appended to a buffer and the
2225 internal newlines. The line is appended to a buffer and the
2229 interpreter's runsource() method is called with the
2226 interpreter's runsource() method is called with the
2230 concatenated contents of the buffer as source. If this
2227 concatenated contents of the buffer as source. If this
2231 indicates that the command was executed or invalid, the buffer
2228 indicates that the command was executed or invalid, the buffer
2232 is reset; otherwise, the command is incomplete, and the buffer
2229 is reset; otherwise, the command is incomplete, and the buffer
2233 is left as it was after the line was appended. The return
2230 is left as it was after the line was appended. The return
2234 value is 1 if more input is required, 0 if the line was dealt
2231 value is 1 if more input is required, 0 if the line was dealt
2235 with in some way (this is the same as runsource()).
2232 with in some way (this is the same as runsource()).
2236 """
2233 """
2237
2234
2238 # autoindent management should be done here, and not in the
2235 # autoindent management should be done here, and not in the
2239 # interactive loop, since that one is only seen by keyboard input. We
2236 # interactive loop, since that one is only seen by keyboard input. We
2240 # need this done correctly even for code run via runlines (which uses
2237 # need this done correctly even for code run via runlines (which uses
2241 # push).
2238 # push).
2242
2239
2243 #print 'push line: <%s>' % line # dbg
2240 #print 'push line: <%s>' % line # dbg
2244 for subline in line.splitlines():
2241 for subline in line.splitlines():
2245 self._autoindent_update(subline)
2242 self._autoindent_update(subline)
2246 self.buffer.append(line)
2243 self.buffer.append(line)
2247 more = self.runsource('\n'.join(self.buffer), self.filename)
2244 more = self.runsource('\n'.join(self.buffer), self.filename)
2248 if not more:
2245 if not more:
2249 self.resetbuffer()
2246 self.resetbuffer()
2250 return more
2247 return more
2251
2248
2252 def _autoindent_update(self,line):
2249 def _autoindent_update(self,line):
2253 """Keep track of the indent level."""
2250 """Keep track of the indent level."""
2254
2251
2255 #debugx('line')
2252 #debugx('line')
2256 #debugx('self.indent_current_nsp')
2253 #debugx('self.indent_current_nsp')
2257 if self.autoindent:
2254 if self.autoindent:
2258 if line:
2255 if line:
2259 inisp = num_ini_spaces(line)
2256 inisp = num_ini_spaces(line)
2260 if inisp < self.indent_current_nsp:
2257 if inisp < self.indent_current_nsp:
2261 self.indent_current_nsp = inisp
2258 self.indent_current_nsp = inisp
2262
2259
2263 if line[-1] == ':':
2260 if line[-1] == ':':
2264 self.indent_current_nsp += 4
2261 self.indent_current_nsp += 4
2265 elif dedent_re.match(line):
2262 elif dedent_re.match(line):
2266 self.indent_current_nsp -= 4
2263 self.indent_current_nsp -= 4
2267 else:
2264 else:
2268 self.indent_current_nsp = 0
2265 self.indent_current_nsp = 0
2269
2266
2270 def resetbuffer(self):
2267 def resetbuffer(self):
2271 """Reset the input buffer."""
2268 """Reset the input buffer."""
2272 self.buffer[:] = []
2269 self.buffer[:] = []
2273
2270
2274 def raw_input(self,prompt='',continue_prompt=False):
2271 def raw_input(self,prompt='',continue_prompt=False):
2275 """Write a prompt and read a line.
2272 """Write a prompt and read a line.
2276
2273
2277 The returned line does not include the trailing newline.
2274 The returned line does not include the trailing newline.
2278 When the user enters the EOF key sequence, EOFError is raised.
2275 When the user enters the EOF key sequence, EOFError is raised.
2279
2276
2280 Optional inputs:
2277 Optional inputs:
2281
2278
2282 - prompt(''): a string to be printed to prompt the user.
2279 - prompt(''): a string to be printed to prompt the user.
2283
2280
2284 - continue_prompt(False): whether this line is the first one or a
2281 - continue_prompt(False): whether this line is the first one or a
2285 continuation in a sequence of inputs.
2282 continuation in a sequence of inputs.
2286 """
2283 """
2287 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2284 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2288
2285
2289 # Code run by the user may have modified the readline completer state.
2286 # Code run by the user may have modified the readline completer state.
2290 # We must ensure that our completer is back in place.
2287 # We must ensure that our completer is back in place.
2291
2288
2292 if self.has_readline:
2289 if self.has_readline:
2293 self.set_completer()
2290 self.set_completer()
2294
2291
2295 try:
2292 try:
2296 line = raw_input_original(prompt).decode(self.stdin_encoding)
2293 line = raw_input_original(prompt).decode(self.stdin_encoding)
2297 except ValueError:
2294 except ValueError:
2298 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2295 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2299 " or sys.stdout.close()!\nExiting IPython!")
2296 " or sys.stdout.close()!\nExiting IPython!")
2300 self.ask_exit()
2297 self.ask_exit()
2301 return ""
2298 return ""
2302
2299
2303 # Try to be reasonably smart about not re-indenting pasted input more
2300 # Try to be reasonably smart about not re-indenting pasted input more
2304 # than necessary. We do this by trimming out the auto-indent initial
2301 # than necessary. We do this by trimming out the auto-indent initial
2305 # spaces, if the user's actual input started itself with whitespace.
2302 # spaces, if the user's actual input started itself with whitespace.
2306 #debugx('self.buffer[-1]')
2303 #debugx('self.buffer[-1]')
2307
2304
2308 if self.autoindent:
2305 if self.autoindent:
2309 if num_ini_spaces(line) > self.indent_current_nsp:
2306 if num_ini_spaces(line) > self.indent_current_nsp:
2310 line = line[self.indent_current_nsp:]
2307 line = line[self.indent_current_nsp:]
2311 self.indent_current_nsp = 0
2308 self.indent_current_nsp = 0
2312
2309
2313 # store the unfiltered input before the user has any chance to modify
2310 # store the unfiltered input before the user has any chance to modify
2314 # it.
2311 # it.
2315 if line.strip():
2312 if line.strip():
2316 if continue_prompt:
2313 if continue_prompt:
2317 self.input_hist_raw[-1] += '%s\n' % line
2314 self.input_hist_raw[-1] += '%s\n' % line
2318 if self.has_readline: # and some config option is set?
2315 if self.has_readline: # and some config option is set?
2319 try:
2316 try:
2320 histlen = self.readline.get_current_history_length()
2317 histlen = self.readline.get_current_history_length()
2321 if histlen > 1:
2318 if histlen > 1:
2322 newhist = self.input_hist_raw[-1].rstrip()
2319 newhist = self.input_hist_raw[-1].rstrip()
2323 self.readline.remove_history_item(histlen-1)
2320 self.readline.remove_history_item(histlen-1)
2324 self.readline.replace_history_item(histlen-2,
2321 self.readline.replace_history_item(histlen-2,
2325 newhist.encode(self.stdin_encoding))
2322 newhist.encode(self.stdin_encoding))
2326 except AttributeError:
2323 except AttributeError:
2327 pass # re{move,place}_history_item are new in 2.4.
2324 pass # re{move,place}_history_item are new in 2.4.
2328 else:
2325 else:
2329 self.input_hist_raw.append('%s\n' % line)
2326 self.input_hist_raw.append('%s\n' % line)
2330 # only entries starting at first column go to shadow history
2327 # only entries starting at first column go to shadow history
2331 if line.lstrip() == line:
2328 if line.lstrip() == line:
2332 self.shadowhist.add(line.strip())
2329 self.shadowhist.add(line.strip())
2333 elif not continue_prompt:
2330 elif not continue_prompt:
2334 self.input_hist_raw.append('\n')
2331 self.input_hist_raw.append('\n')
2335 try:
2332 try:
2336 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2333 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2337 except:
2334 except:
2338 # blanket except, in case a user-defined prefilter crashes, so it
2335 # blanket except, in case a user-defined prefilter crashes, so it
2339 # can't take all of ipython with it.
2336 # can't take all of ipython with it.
2340 self.showtraceback()
2337 self.showtraceback()
2341 return ''
2338 return ''
2342 else:
2339 else:
2343 return lineout
2340 return lineout
2344
2341
2345 # def init_exec_commands(self):
2342 # def init_exec_commands(self):
2346 # for cmd in self.config.EXECUTE:
2343 # for cmd in self.config.EXECUTE:
2347 # print "execute:", cmd
2344 # print "execute:", cmd
2348 # self.api.runlines(cmd)
2345 # self.api.runlines(cmd)
2349 #
2346 #
2350 # batchrun = False
2347 # batchrun = False
2351 # if self.config.has_key('EXECFILE'):
2348 # if self.config.has_key('EXECFILE'):
2352 # for batchfile in [path(arg) for arg in self.config.EXECFILE
2349 # for batchfile in [path(arg) for arg in self.config.EXECFILE
2353 # if arg.lower().endswith('.ipy')]:
2350 # if arg.lower().endswith('.ipy')]:
2354 # if not batchfile.isfile():
2351 # if not batchfile.isfile():
2355 # print "No such batch file:", batchfile
2352 # print "No such batch file:", batchfile
2356 # continue
2353 # continue
2357 # self.api.runlines(batchfile.text())
2354 # self.api.runlines(batchfile.text())
2358 # batchrun = True
2355 # batchrun = True
2359 # # without -i option, exit after running the batch file
2356 # # without -i option, exit after running the batch file
2360 # if batchrun and not self.interactive:
2357 # if batchrun and not self.interactive:
2361 # self.ask_exit()
2358 # self.ask_exit()
2362
2359
2363 # def load(self, mod):
2360 # def load(self, mod):
2364 # """ Load an extension.
2361 # """ Load an extension.
2365 #
2362 #
2366 # Some modules should (or must) be 'load()':ed, rather than just imported.
2363 # Some modules should (or must) be 'load()':ed, rather than just imported.
2367 #
2364 #
2368 # Loading will do:
2365 # Loading will do:
2369 #
2366 #
2370 # - run init_ipython(ip)
2367 # - run init_ipython(ip)
2371 # - run ipython_firstrun(ip)
2368 # - run ipython_firstrun(ip)
2372 # """
2369 # """
2373 #
2370 #
2374 # if mod in self.extensions:
2371 # if mod in self.extensions:
2375 # # just to make sure we don't init it twice
2372 # # just to make sure we don't init it twice
2376 # # note that if you 'load' a module that has already been
2373 # # note that if you 'load' a module that has already been
2377 # # imported, init_ipython gets run anyway
2374 # # imported, init_ipython gets run anyway
2378 #
2375 #
2379 # return self.extensions[mod]
2376 # return self.extensions[mod]
2380 # __import__(mod)
2377 # __import__(mod)
2381 # m = sys.modules[mod]
2378 # m = sys.modules[mod]
2382 # if hasattr(m,'init_ipython'):
2379 # if hasattr(m,'init_ipython'):
2383 # m.init_ipython(self)
2380 # m.init_ipython(self)
2384 #
2381 #
2385 # if hasattr(m,'ipython_firstrun'):
2382 # if hasattr(m,'ipython_firstrun'):
2386 # already_loaded = self.db.get('firstrun_done', set())
2383 # already_loaded = self.db.get('firstrun_done', set())
2387 # if mod not in already_loaded:
2384 # if mod not in already_loaded:
2388 # m.ipython_firstrun(self)
2385 # m.ipython_firstrun(self)
2389 # already_loaded.add(mod)
2386 # already_loaded.add(mod)
2390 # self.db['firstrun_done'] = already_loaded
2387 # self.db['firstrun_done'] = already_loaded
2391 #
2388 #
2392 # self.extensions[mod] = m
2389 # self.extensions[mod] = m
2393 # return m
2390 # return m
2394
2391
2395 #-------------------------------------------------------------------------
2392 #-------------------------------------------------------------------------
2396 # Things related to the prefilter
2393 # Things related to the prefilter
2397 #-------------------------------------------------------------------------
2394 #-------------------------------------------------------------------------
2398
2395
2399 def init_prefilter(self):
2396 def init_prefilter(self):
2400 self.prefilter_manager = PrefilterManager(self, config=self.config)
2397 self.prefilter_manager = PrefilterManager(self, config=self.config)
2401
2398
2402 #-------------------------------------------------------------------------
2399 #-------------------------------------------------------------------------
2403 # Utilities
2400 # Utilities
2404 #-------------------------------------------------------------------------
2401 #-------------------------------------------------------------------------
2405
2402
2406 def getoutput(self, cmd):
2403 def getoutput(self, cmd):
2407 return getoutput(self.var_expand(cmd,depth=2),
2404 return getoutput(self.var_expand(cmd,depth=2),
2408 header=self.system_header,
2405 header=self.system_header,
2409 verbose=self.system_verbose)
2406 verbose=self.system_verbose)
2410
2407
2411 def getoutputerror(self, cmd):
2408 def getoutputerror(self, cmd):
2412 return getoutputerror(self.var_expand(cmd,depth=2),
2409 return getoutputerror(self.var_expand(cmd,depth=2),
2413 header=self.system_header,
2410 header=self.system_header,
2414 verbose=self.system_verbose)
2411 verbose=self.system_verbose)
2415
2412
2416 def var_expand(self,cmd,depth=0):
2413 def var_expand(self,cmd,depth=0):
2417 """Expand python variables in a string.
2414 """Expand python variables in a string.
2418
2415
2419 The depth argument indicates how many frames above the caller should
2416 The depth argument indicates how many frames above the caller should
2420 be walked to look for the local namespace where to expand variables.
2417 be walked to look for the local namespace where to expand variables.
2421
2418
2422 The global namespace for expansion is always the user's interactive
2419 The global namespace for expansion is always the user's interactive
2423 namespace.
2420 namespace.
2424 """
2421 """
2425
2422
2426 return str(ItplNS(cmd,
2423 return str(ItplNS(cmd,
2427 self.user_ns, # globals
2424 self.user_ns, # globals
2428 # Skip our own frame in searching for locals:
2425 # Skip our own frame in searching for locals:
2429 sys._getframe(depth+1).f_locals # locals
2426 sys._getframe(depth+1).f_locals # locals
2430 ))
2427 ))
2431
2428
2432 def mktempfile(self,data=None):
2429 def mktempfile(self,data=None):
2433 """Make a new tempfile and return its filename.
2430 """Make a new tempfile and return its filename.
2434
2431
2435 This makes a call to tempfile.mktemp, but it registers the created
2432 This makes a call to tempfile.mktemp, but it registers the created
2436 filename internally so ipython cleans it up at exit time.
2433 filename internally so ipython cleans it up at exit time.
2437
2434
2438 Optional inputs:
2435 Optional inputs:
2439
2436
2440 - data(None): if data is given, it gets written out to the temp file
2437 - data(None): if data is given, it gets written out to the temp file
2441 immediately, and the file is closed again."""
2438 immediately, and the file is closed again."""
2442
2439
2443 filename = tempfile.mktemp('.py','ipython_edit_')
2440 filename = tempfile.mktemp('.py','ipython_edit_')
2444 self.tempfiles.append(filename)
2441 self.tempfiles.append(filename)
2445
2442
2446 if data:
2443 if data:
2447 tmp_file = open(filename,'w')
2444 tmp_file = open(filename,'w')
2448 tmp_file.write(data)
2445 tmp_file.write(data)
2449 tmp_file.close()
2446 tmp_file.close()
2450 return filename
2447 return filename
2451
2448
2452 def write(self,data):
2449 def write(self,data):
2453 """Write a string to the default output"""
2450 """Write a string to the default output"""
2454 Term.cout.write(data)
2451 Term.cout.write(data)
2455
2452
2456 def write_err(self,data):
2453 def write_err(self,data):
2457 """Write a string to the default error output"""
2454 """Write a string to the default error output"""
2458 Term.cerr.write(data)
2455 Term.cerr.write(data)
2459
2456
2460 def ask_yes_no(self,prompt,default=True):
2457 def ask_yes_no(self,prompt,default=True):
2461 if self.quiet:
2458 if self.quiet:
2462 return True
2459 return True
2463 return ask_yes_no(prompt,default)
2460 return ask_yes_no(prompt,default)
2464
2461
2465 #-------------------------------------------------------------------------
2462 #-------------------------------------------------------------------------
2466 # Things related to IPython exiting
2463 # Things related to IPython exiting
2467 #-------------------------------------------------------------------------
2464 #-------------------------------------------------------------------------
2468
2465
2469 def ask_exit(self):
2466 def ask_exit(self):
2470 """ Call for exiting. Can be overiden and used as a callback. """
2467 """ Call for exiting. Can be overiden and used as a callback. """
2471 self.exit_now = True
2468 self.exit_now = True
2472
2469
2473 def exit(self):
2470 def exit(self):
2474 """Handle interactive exit.
2471 """Handle interactive exit.
2475
2472
2476 This method calls the ask_exit callback."""
2473 This method calls the ask_exit callback."""
2477 if self.confirm_exit:
2474 if self.confirm_exit:
2478 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2475 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2479 self.ask_exit()
2476 self.ask_exit()
2480 else:
2477 else:
2481 self.ask_exit()
2478 self.ask_exit()
2482
2479
2483 def atexit_operations(self):
2480 def atexit_operations(self):
2484 """This will be executed at the time of exit.
2481 """This will be executed at the time of exit.
2485
2482
2486 Saving of persistent data should be performed here.
2483 Saving of persistent data should be performed here.
2487 """
2484 """
2488 self.savehist()
2485 self.savehist()
2489
2486
2490 # Cleanup all tempfiles left around
2487 # Cleanup all tempfiles left around
2491 for tfile in self.tempfiles:
2488 for tfile in self.tempfiles:
2492 try:
2489 try:
2493 os.unlink(tfile)
2490 os.unlink(tfile)
2494 except OSError:
2491 except OSError:
2495 pass
2492 pass
2496
2493
2497 # Clear all user namespaces to release all references cleanly.
2494 # Clear all user namespaces to release all references cleanly.
2498 self.reset()
2495 self.reset()
2499
2496
2500 # Run user hooks
2497 # Run user hooks
2501 self.hooks.shutdown_hook()
2498 self.hooks.shutdown_hook()
2502
2499
2503 def cleanup(self):
2500 def cleanup(self):
2504 self.restore_sys_module_state()
2501 self.restore_sys_module_state()
2505
2502
2506
2503
@@ -1,3585 +1,3563 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #*****************************************************************************
5 #*****************************************************************************
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 #****************************************************************************
13 #****************************************************************************
14 # Modules and globals
14 # Modules and globals
15
15
16 # Python standard modules
16 # Python standard modules
17 import __builtin__
17 import __builtin__
18 import bdb
18 import bdb
19 import inspect
19 import inspect
20 import os
20 import os
21 import pdb
21 import pdb
22 import pydoc
22 import pydoc
23 import sys
23 import sys
24 import re
24 import re
25 import tempfile
25 import tempfile
26 import time
26 import time
27 import cPickle as pickle
27 import cPickle as pickle
28 import textwrap
28 import textwrap
29 from cStringIO import StringIO
29 from cStringIO import StringIO
30 from getopt import getopt,GetoptError
30 from getopt import getopt,GetoptError
31 from pprint import pprint, pformat
31 from pprint import pprint, pformat
32
32
33 # cProfile was added in Python2.5
33 # cProfile was added in Python2.5
34 try:
34 try:
35 import cProfile as profile
35 import cProfile as profile
36 import pstats
36 import pstats
37 except ImportError:
37 except ImportError:
38 # profile isn't bundled by default in Debian for license reasons
38 # profile isn't bundled by default in Debian for license reasons
39 try:
39 try:
40 import profile,pstats
40 import profile,pstats
41 except ImportError:
41 except ImportError:
42 profile = pstats = None
42 profile = pstats = None
43
43
44 # Homebrewed
44 # Homebrewed
45 import IPython
45 import IPython
46 from IPython.utils import wildcard
46 from IPython.utils import wildcard
47 from IPython.core import debugger, oinspect
47 from IPython.core import debugger, oinspect
48 from IPython.core.error import TryNext
48 from IPython.core.error import TryNext
49 from IPython.core.fakemodule import FakeModule
49 from IPython.core.fakemodule import FakeModule
50 from IPython.core.prefilter import ESC_MAGIC
50 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
51 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
51 from IPython.utils.PyColorize import Parser
52 from IPython.utils.PyColorize import Parser
52 from IPython.utils.ipstruct import Struct
53 from IPython.utils.ipstruct import Struct
53 from IPython.core.macro import Macro
54 from IPython.core.macro import Macro
54 from IPython.utils.genutils import *
55 from IPython.utils.genutils import *
55 from IPython.core.page import page
56 from IPython.core.page import page
56 from IPython.utils import platutils
57 from IPython.utils import platutils
57 import IPython.utils.generics
58 import IPython.utils.generics
58 from IPython.core.error import UsageError
59 from IPython.core.error import UsageError
59 from IPython.testing import decorators as testdec
60 from IPython.testing import decorators as testdec
60
61
61 #***************************************************************************
62 #***************************************************************************
62 # Utility functions
63 # Utility functions
63 def on_off(tag):
64 def on_off(tag):
64 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
65 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
65 return ['OFF','ON'][tag]
66 return ['OFF','ON'][tag]
66
67
67 class Bunch: pass
68 class Bunch: pass
68
69
69 def compress_dhist(dh):
70 def compress_dhist(dh):
70 head, tail = dh[:-10], dh[-10:]
71 head, tail = dh[:-10], dh[-10:]
71
72
72 newhead = []
73 newhead = []
73 done = set()
74 done = set()
74 for h in head:
75 for h in head:
75 if h in done:
76 if h in done:
76 continue
77 continue
77 newhead.append(h)
78 newhead.append(h)
78 done.add(h)
79 done.add(h)
79
80
80 return newhead + tail
81 return newhead + tail
81
82
82
83
83 #***************************************************************************
84 #***************************************************************************
84 # Main class implementing Magic functionality
85 # Main class implementing Magic functionality
85 class Magic:
86 class Magic:
86 """Magic functions for InteractiveShell.
87 """Magic functions for InteractiveShell.
87
88
88 Shell functions which can be reached as %function_name. All magic
89 Shell functions which can be reached as %function_name. All magic
89 functions should accept a string, which they can parse for their own
90 functions should accept a string, which they can parse for their own
90 needs. This can make some functions easier to type, eg `%cd ../`
91 needs. This can make some functions easier to type, eg `%cd ../`
91 vs. `%cd("../")`
92 vs. `%cd("../")`
92
93
93 ALL definitions MUST begin with the prefix magic_. The user won't need it
94 ALL definitions MUST begin with the prefix magic_. The user won't need it
94 at the command line, but it is is needed in the definition. """
95 at the command line, but it is is needed in the definition. """
95
96
96 # class globals
97 # class globals
97 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
98 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
98 'Automagic is ON, % prefix NOT needed for magic functions.']
99 'Automagic is ON, % prefix NOT needed for magic functions.']
99
100
100 #......................................................................
101 #......................................................................
101 # some utility functions
102 # some utility functions
102
103
103 def __init__(self,shell):
104 def __init__(self,shell):
104
105
105 self.options_table = {}
106 self.options_table = {}
106 if profile is None:
107 if profile is None:
107 self.magic_prun = self.profile_missing_notice
108 self.magic_prun = self.profile_missing_notice
108 self.shell = shell
109 self.shell = shell
109
110
110 # namespace for holding state we may need
111 # namespace for holding state we may need
111 self._magic_state = Bunch()
112 self._magic_state = Bunch()
112
113
113 def profile_missing_notice(self, *args, **kwargs):
114 def profile_missing_notice(self, *args, **kwargs):
114 error("""\
115 error("""\
115 The profile module could not be found. It has been removed from the standard
116 The profile module could not be found. It has been removed from the standard
116 python packages because of its non-free license. To use profiling, install the
117 python packages because of its non-free license. To use profiling, install the
117 python-profiler package from non-free.""")
118 python-profiler package from non-free.""")
118
119
119 def default_option(self,fn,optstr):
120 def default_option(self,fn,optstr):
120 """Make an entry in the options_table for fn, with value optstr"""
121 """Make an entry in the options_table for fn, with value optstr"""
121
122
122 if fn not in self.lsmagic():
123 if fn not in self.lsmagic():
123 error("%s is not a magic function" % fn)
124 error("%s is not a magic function" % fn)
124 self.options_table[fn] = optstr
125 self.options_table[fn] = optstr
125
126
126 def lsmagic(self):
127 def lsmagic(self):
127 """Return a list of currently available magic functions.
128 """Return a list of currently available magic functions.
128
129
129 Gives a list of the bare names after mangling (['ls','cd', ...], not
130 Gives a list of the bare names after mangling (['ls','cd', ...], not
130 ['magic_ls','magic_cd',...]"""
131 ['magic_ls','magic_cd',...]"""
131
132
132 # FIXME. This needs a cleanup, in the way the magics list is built.
133 # FIXME. This needs a cleanup, in the way the magics list is built.
133
134
134 # magics in class definition
135 # magics in class definition
135 class_magic = lambda fn: fn.startswith('magic_') and \
136 class_magic = lambda fn: fn.startswith('magic_') and \
136 callable(Magic.__dict__[fn])
137 callable(Magic.__dict__[fn])
137 # in instance namespace (run-time user additions)
138 # in instance namespace (run-time user additions)
138 inst_magic = lambda fn: fn.startswith('magic_') and \
139 inst_magic = lambda fn: fn.startswith('magic_') and \
139 callable(self.__dict__[fn])
140 callable(self.__dict__[fn])
140 # and bound magics by user (so they can access self):
141 # and bound magics by user (so they can access self):
141 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
142 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
142 callable(self.__class__.__dict__[fn])
143 callable(self.__class__.__dict__[fn])
143 magics = filter(class_magic,Magic.__dict__.keys()) + \
144 magics = filter(class_magic,Magic.__dict__.keys()) + \
144 filter(inst_magic,self.__dict__.keys()) + \
145 filter(inst_magic,self.__dict__.keys()) + \
145 filter(inst_bound_magic,self.__class__.__dict__.keys())
146 filter(inst_bound_magic,self.__class__.__dict__.keys())
146 out = []
147 out = []
147 for fn in set(magics):
148 for fn in set(magics):
148 out.append(fn.replace('magic_','',1))
149 out.append(fn.replace('magic_','',1))
149 out.sort()
150 out.sort()
150 return out
151 return out
151
152
152 def extract_input_slices(self,slices,raw=False):
153 def extract_input_slices(self,slices,raw=False):
153 """Return as a string a set of input history slices.
154 """Return as a string a set of input history slices.
154
155
155 Inputs:
156 Inputs:
156
157
157 - slices: the set of slices is given as a list of strings (like
158 - slices: the set of slices is given as a list of strings (like
158 ['1','4:8','9'], since this function is for use by magic functions
159 ['1','4:8','9'], since this function is for use by magic functions
159 which get their arguments as strings.
160 which get their arguments as strings.
160
161
161 Optional inputs:
162 Optional inputs:
162
163
163 - raw(False): by default, the processed input is used. If this is
164 - raw(False): by default, the processed input is used. If this is
164 true, the raw input history is used instead.
165 true, the raw input history is used instead.
165
166
166 Note that slices can be called with two notations:
167 Note that slices can be called with two notations:
167
168
168 N:M -> standard python form, means including items N...(M-1).
169 N:M -> standard python form, means including items N...(M-1).
169
170
170 N-M -> include items N..M (closed endpoint)."""
171 N-M -> include items N..M (closed endpoint)."""
171
172
172 if raw:
173 if raw:
173 hist = self.shell.input_hist_raw
174 hist = self.shell.input_hist_raw
174 else:
175 else:
175 hist = self.shell.input_hist
176 hist = self.shell.input_hist
176
177
177 cmds = []
178 cmds = []
178 for chunk in slices:
179 for chunk in slices:
179 if ':' in chunk:
180 if ':' in chunk:
180 ini,fin = map(int,chunk.split(':'))
181 ini,fin = map(int,chunk.split(':'))
181 elif '-' in chunk:
182 elif '-' in chunk:
182 ini,fin = map(int,chunk.split('-'))
183 ini,fin = map(int,chunk.split('-'))
183 fin += 1
184 fin += 1
184 else:
185 else:
185 ini = int(chunk)
186 ini = int(chunk)
186 fin = ini+1
187 fin = ini+1
187 cmds.append(hist[ini:fin])
188 cmds.append(hist[ini:fin])
188 return cmds
189 return cmds
189
190
190 def _ofind(self, oname, namespaces=None):
191 def _ofind(self, oname, namespaces=None):
191 """Find an object in the available namespaces.
192 """Find an object in the available namespaces.
192
193
193 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
194 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
194
195
195 Has special code to detect magic functions.
196 Has special code to detect magic functions.
196 """
197 """
197
198
198 oname = oname.strip()
199 oname = oname.strip()
199
200
200 alias_ns = None
201 alias_ns = None
201 if namespaces is None:
202 if namespaces is None:
202 # Namespaces to search in:
203 # Namespaces to search in:
203 # Put them in a list. The order is important so that we
204 # Put them in a list. The order is important so that we
204 # find things in the same order that Python finds them.
205 # find things in the same order that Python finds them.
205 namespaces = [ ('Interactive', self.shell.user_ns),
206 namespaces = [ ('Interactive', self.shell.user_ns),
206 ('IPython internal', self.shell.internal_ns),
207 ('IPython internal', self.shell.internal_ns),
207 ('Python builtin', __builtin__.__dict__),
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 # initialize results to 'null'
213 # initialize results to 'null'
213 found = 0; obj = None; ospace = None; ds = None;
214 found = 0; obj = None; ospace = None; ds = None;
214 ismagic = 0; isalias = 0; parent = None
215 ismagic = 0; isalias = 0; parent = None
215
216
216 # Look for the given name by splitting it in parts. If the head is
217 # Look for the given name by splitting it in parts. If the head is
217 # found, then we look for all the remaining parts as members, and only
218 # found, then we look for all the remaining parts as members, and only
218 # declare success if we can find them all.
219 # declare success if we can find them all.
219 oname_parts = oname.split('.')
220 oname_parts = oname.split('.')
220 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
221 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
221 for nsname,ns in namespaces:
222 for nsname,ns in namespaces:
222 try:
223 try:
223 obj = ns[oname_head]
224 obj = ns[oname_head]
224 except KeyError:
225 except KeyError:
225 continue
226 continue
226 else:
227 else:
227 #print 'oname_rest:', oname_rest # dbg
228 #print 'oname_rest:', oname_rest # dbg
228 for part in oname_rest:
229 for part in oname_rest:
229 try:
230 try:
230 parent = obj
231 parent = obj
231 obj = getattr(obj,part)
232 obj = getattr(obj,part)
232 except:
233 except:
233 # Blanket except b/c some badly implemented objects
234 # Blanket except b/c some badly implemented objects
234 # allow __getattr__ to raise exceptions other than
235 # allow __getattr__ to raise exceptions other than
235 # AttributeError, which then crashes IPython.
236 # AttributeError, which then crashes IPython.
236 break
237 break
237 else:
238 else:
238 # If we finish the for loop (no break), we got all members
239 # If we finish the for loop (no break), we got all members
239 found = 1
240 found = 1
240 ospace = nsname
241 ospace = nsname
241 if ns == alias_ns:
242 if ns == alias_ns:
242 isalias = 1
243 isalias = 1
243 break # namespace loop
244 break # namespace loop
244
245
245 # Try to see if it's magic
246 # Try to see if it's magic
246 if not found:
247 if not found:
247 if oname.startswith(self.shell.ESC_MAGIC):
248 if oname.startswith(ESC_MAGIC):
248 oname = oname[1:]
249 oname = oname[1:]
249 obj = getattr(self,'magic_'+oname,None)
250 obj = getattr(self,'magic_'+oname,None)
250 if obj is not None:
251 if obj is not None:
251 found = 1
252 found = 1
252 ospace = 'IPython internal'
253 ospace = 'IPython internal'
253 ismagic = 1
254 ismagic = 1
254
255
255 # Last try: special-case some literals like '', [], {}, etc:
256 # Last try: special-case some literals like '', [], {}, etc:
256 if not found and oname_head in ["''",'""','[]','{}','()']:
257 if not found and oname_head in ["''",'""','[]','{}','()']:
257 obj = eval(oname_head)
258 obj = eval(oname_head)
258 found = 1
259 found = 1
259 ospace = 'Interactive'
260 ospace = 'Interactive'
260
261
261 return {'found':found, 'obj':obj, 'namespace':ospace,
262 return {'found':found, 'obj':obj, 'namespace':ospace,
262 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
263 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
263
264
264 def arg_err(self,func):
265 def arg_err(self,func):
265 """Print docstring if incorrect arguments were passed"""
266 """Print docstring if incorrect arguments were passed"""
266 print 'Error in arguments:'
267 print 'Error in arguments:'
267 print OInspect.getdoc(func)
268 print OInspect.getdoc(func)
268
269
269 def format_latex(self,strng):
270 def format_latex(self,strng):
270 """Format a string for latex inclusion."""
271 """Format a string for latex inclusion."""
271
272
272 # Characters that need to be escaped for latex:
273 # Characters that need to be escaped for latex:
273 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
274 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
274 # Magic command names as headers:
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 re.MULTILINE)
277 re.MULTILINE)
277 # Magic commands
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 re.MULTILINE)
280 re.MULTILINE)
280 # Paragraph continue
281 # Paragraph continue
281 par_re = re.compile(r'\\$',re.MULTILINE)
282 par_re = re.compile(r'\\$',re.MULTILINE)
282
283
283 # The "\n" symbol
284 # The "\n" symbol
284 newline_re = re.compile(r'\\n')
285 newline_re = re.compile(r'\\n')
285
286
286 # Now build the string for output:
287 # Now build the string for output:
287 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
288 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
288 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
289 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
289 strng)
290 strng)
290 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
291 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
291 strng = par_re.sub(r'\\\\',strng)
292 strng = par_re.sub(r'\\\\',strng)
292 strng = escape_re.sub(r'\\\1',strng)
293 strng = escape_re.sub(r'\\\1',strng)
293 strng = newline_re.sub(r'\\textbackslash{}n',strng)
294 strng = newline_re.sub(r'\\textbackslash{}n',strng)
294 return strng
295 return strng
295
296
296 def format_screen(self,strng):
297 def format_screen(self,strng):
297 """Format a string for screen printing.
298 """Format a string for screen printing.
298
299
299 This removes some latex-type format codes."""
300 This removes some latex-type format codes."""
300 # Paragraph continue
301 # Paragraph continue
301 par_re = re.compile(r'\\$',re.MULTILINE)
302 par_re = re.compile(r'\\$',re.MULTILINE)
302 strng = par_re.sub('',strng)
303 strng = par_re.sub('',strng)
303 return strng
304 return strng
304
305
305 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
306 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
306 """Parse options passed to an argument string.
307 """Parse options passed to an argument string.
307
308
308 The interface is similar to that of getopt(), but it returns back a
309 The interface is similar to that of getopt(), but it returns back a
309 Struct with the options as keys and the stripped argument string still
310 Struct with the options as keys and the stripped argument string still
310 as a string.
311 as a string.
311
312
312 arg_str is quoted as a true sys.argv vector by using shlex.split.
313 arg_str is quoted as a true sys.argv vector by using shlex.split.
313 This allows us to easily expand variables, glob files, quote
314 This allows us to easily expand variables, glob files, quote
314 arguments, etc.
315 arguments, etc.
315
316
316 Options:
317 Options:
317 -mode: default 'string'. If given as 'list', the argument string is
318 -mode: default 'string'. If given as 'list', the argument string is
318 returned as a list (split on whitespace) instead of a string.
319 returned as a list (split on whitespace) instead of a string.
319
320
320 -list_all: put all option values in lists. Normally only options
321 -list_all: put all option values in lists. Normally only options
321 appearing more than once are put in a list.
322 appearing more than once are put in a list.
322
323
323 -posix (True): whether to split the input line in POSIX mode or not,
324 -posix (True): whether to split the input line in POSIX mode or not,
324 as per the conventions outlined in the shlex module from the
325 as per the conventions outlined in the shlex module from the
325 standard library."""
326 standard library."""
326
327
327 # inject default options at the beginning of the input line
328 # inject default options at the beginning of the input line
328 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
329 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
329 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
330 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
330
331
331 mode = kw.get('mode','string')
332 mode = kw.get('mode','string')
332 if mode not in ['string','list']:
333 if mode not in ['string','list']:
333 raise ValueError,'incorrect mode given: %s' % mode
334 raise ValueError,'incorrect mode given: %s' % mode
334 # Get options
335 # Get options
335 list_all = kw.get('list_all',0)
336 list_all = kw.get('list_all',0)
336 posix = kw.get('posix',True)
337 posix = kw.get('posix',True)
337
338
338 # Check if we have more than one argument to warrant extra processing:
339 # Check if we have more than one argument to warrant extra processing:
339 odict = {} # Dictionary with options
340 odict = {} # Dictionary with options
340 args = arg_str.split()
341 args = arg_str.split()
341 if len(args) >= 1:
342 if len(args) >= 1:
342 # If the list of inputs only has 0 or 1 thing in it, there's no
343 # If the list of inputs only has 0 or 1 thing in it, there's no
343 # need to look for options
344 # need to look for options
344 argv = arg_split(arg_str,posix)
345 argv = arg_split(arg_str,posix)
345 # Do regular option processing
346 # Do regular option processing
346 try:
347 try:
347 opts,args = getopt(argv,opt_str,*long_opts)
348 opts,args = getopt(argv,opt_str,*long_opts)
348 except GetoptError,e:
349 except GetoptError,e:
349 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
350 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
350 " ".join(long_opts)))
351 " ".join(long_opts)))
351 for o,a in opts:
352 for o,a in opts:
352 if o.startswith('--'):
353 if o.startswith('--'):
353 o = o[2:]
354 o = o[2:]
354 else:
355 else:
355 o = o[1:]
356 o = o[1:]
356 try:
357 try:
357 odict[o].append(a)
358 odict[o].append(a)
358 except AttributeError:
359 except AttributeError:
359 odict[o] = [odict[o],a]
360 odict[o] = [odict[o],a]
360 except KeyError:
361 except KeyError:
361 if list_all:
362 if list_all:
362 odict[o] = [a]
363 odict[o] = [a]
363 else:
364 else:
364 odict[o] = a
365 odict[o] = a
365
366
366 # Prepare opts,args for return
367 # Prepare opts,args for return
367 opts = Struct(odict)
368 opts = Struct(odict)
368 if mode == 'string':
369 if mode == 'string':
369 args = ' '.join(args)
370 args = ' '.join(args)
370
371
371 return opts,args
372 return opts,args
372
373
373 #......................................................................
374 #......................................................................
374 # And now the actual magic functions
375 # And now the actual magic functions
375
376
376 # Functions for IPython shell work (vars,funcs, config, etc)
377 # Functions for IPython shell work (vars,funcs, config, etc)
377 def magic_lsmagic(self, parameter_s = ''):
378 def magic_lsmagic(self, parameter_s = ''):
378 """List currently available magic functions."""
379 """List currently available magic functions."""
379 mesc = self.shell.ESC_MAGIC
380 mesc = ESC_MAGIC
380 print 'Available magic functions:\n'+mesc+\
381 print 'Available magic functions:\n'+mesc+\
381 (' '+mesc).join(self.lsmagic())
382 (' '+mesc).join(self.lsmagic())
382 print '\n' + Magic.auto_status[self.shell.automagic]
383 print '\n' + Magic.auto_status[self.shell.automagic]
383 return None
384 return None
384
385
385 def magic_magic(self, parameter_s = ''):
386 def magic_magic(self, parameter_s = ''):
386 """Print information about the magic function system.
387 """Print information about the magic function system.
387
388
388 Supported formats: -latex, -brief, -rest
389 Supported formats: -latex, -brief, -rest
389 """
390 """
390
391
391 mode = ''
392 mode = ''
392 try:
393 try:
393 if parameter_s.split()[0] == '-latex':
394 if parameter_s.split()[0] == '-latex':
394 mode = 'latex'
395 mode = 'latex'
395 if parameter_s.split()[0] == '-brief':
396 if parameter_s.split()[0] == '-brief':
396 mode = 'brief'
397 mode = 'brief'
397 if parameter_s.split()[0] == '-rest':
398 if parameter_s.split()[0] == '-rest':
398 mode = 'rest'
399 mode = 'rest'
399 rest_docs = []
400 rest_docs = []
400 except:
401 except:
401 pass
402 pass
402
403
403 magic_docs = []
404 magic_docs = []
404 for fname in self.lsmagic():
405 for fname in self.lsmagic():
405 mname = 'magic_' + fname
406 mname = 'magic_' + fname
406 for space in (Magic,self,self.__class__):
407 for space in (Magic,self,self.__class__):
407 try:
408 try:
408 fn = space.__dict__[mname]
409 fn = space.__dict__[mname]
409 except KeyError:
410 except KeyError:
410 pass
411 pass
411 else:
412 else:
412 break
413 break
413 if mode == 'brief':
414 if mode == 'brief':
414 # only first line
415 # only first line
415 if fn.__doc__:
416 if fn.__doc__:
416 fndoc = fn.__doc__.split('\n',1)[0]
417 fndoc = fn.__doc__.split('\n',1)[0]
417 else:
418 else:
418 fndoc = 'No documentation'
419 fndoc = 'No documentation'
419 else:
420 else:
420 if fn.__doc__:
421 if fn.__doc__:
421 fndoc = fn.__doc__.rstrip()
422 fndoc = fn.__doc__.rstrip()
422 else:
423 else:
423 fndoc = 'No documentation'
424 fndoc = 'No documentation'
424
425
425
426
426 if mode == 'rest':
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 fname,fndoc))
429 fname,fndoc))
429
430
430 else:
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 fname,fndoc))
433 fname,fndoc))
433
434
434 magic_docs = ''.join(magic_docs)
435 magic_docs = ''.join(magic_docs)
435
436
436 if mode == 'rest':
437 if mode == 'rest':
437 return "".join(rest_docs)
438 return "".join(rest_docs)
438
439
439 if mode == 'latex':
440 if mode == 'latex':
440 print self.format_latex(magic_docs)
441 print self.format_latex(magic_docs)
441 return
442 return
442 else:
443 else:
443 magic_docs = self.format_screen(magic_docs)
444 magic_docs = self.format_screen(magic_docs)
444 if mode == 'brief':
445 if mode == 'brief':
445 return magic_docs
446 return magic_docs
446
447
447 outmsg = """
448 outmsg = """
448 IPython's 'magic' functions
449 IPython's 'magic' functions
449 ===========================
450 ===========================
450
451
451 The magic function system provides a series of functions which allow you to
452 The magic function system provides a series of functions which allow you to
452 control the behavior of IPython itself, plus a lot of system-type
453 control the behavior of IPython itself, plus a lot of system-type
453 features. All these functions are prefixed with a % character, but parameters
454 features. All these functions are prefixed with a % character, but parameters
454 are given without parentheses or quotes.
455 are given without parentheses or quotes.
455
456
456 NOTE: If you have 'automagic' enabled (via the command line option or with the
457 NOTE: If you have 'automagic' enabled (via the command line option or with the
457 %automagic function), you don't need to type in the % explicitly. By default,
458 %automagic function), you don't need to type in the % explicitly. By default,
458 IPython ships with automagic on, so you should only rarely need the % escape.
459 IPython ships with automagic on, so you should only rarely need the % escape.
459
460
460 Example: typing '%cd mydir' (without the quotes) changes you working directory
461 Example: typing '%cd mydir' (without the quotes) changes you working directory
461 to 'mydir', if it exists.
462 to 'mydir', if it exists.
462
463
463 You can define your own magic functions to extend the system. See the supplied
464 You can define your own magic functions to extend the system. See the supplied
464 ipythonrc and example-magic.py files for details (in your ipython
465 ipythonrc and example-magic.py files for details (in your ipython
465 configuration directory, typically $HOME/.ipython/).
466 configuration directory, typically $HOME/.ipython/).
466
467
467 You can also define your own aliased names for magic functions. In your
468 You can also define your own aliased names for magic functions. In your
468 ipythonrc file, placing a line like:
469 ipythonrc file, placing a line like:
469
470
470 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
471 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
471
472
472 will define %pf as a new name for %profile.
473 will define %pf as a new name for %profile.
473
474
474 You can also call magics in code using the magic() function, which IPython
475 You can also call magics in code using the magic() function, which IPython
475 automatically adds to the builtin namespace. Type 'magic?' for details.
476 automatically adds to the builtin namespace. Type 'magic?' for details.
476
477
477 For a list of the available magic functions, use %lsmagic. For a description
478 For a list of the available magic functions, use %lsmagic. For a description
478 of any of them, type %magic_name?, e.g. '%cd?'.
479 of any of them, type %magic_name?, e.g. '%cd?'.
479
480
480 Currently the magic system has the following functions:\n"""
481 Currently the magic system has the following functions:\n"""
481
482
482 mesc = self.shell.ESC_MAGIC
483 mesc = ESC_MAGIC
483 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
484 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
484 "\n\n%s%s\n\n%s" % (outmsg,
485 "\n\n%s%s\n\n%s" % (outmsg,
485 magic_docs,mesc,mesc,
486 magic_docs,mesc,mesc,
486 (' '+mesc).join(self.lsmagic()),
487 (' '+mesc).join(self.lsmagic()),
487 Magic.auto_status[self.shell.automagic] ) )
488 Magic.auto_status[self.shell.automagic] ) )
488
489
489 page(outmsg,screen_lines=self.shell.usable_screen_length)
490 page(outmsg,screen_lines=self.shell.usable_screen_length)
490
491
491
492
492 def magic_autoindent(self, parameter_s = ''):
493 def magic_autoindent(self, parameter_s = ''):
493 """Toggle autoindent on/off (if available)."""
494 """Toggle autoindent on/off (if available)."""
494
495
495 self.shell.set_autoindent()
496 self.shell.set_autoindent()
496 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
497 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
497
498
498
499
499 def magic_automagic(self, parameter_s = ''):
500 def magic_automagic(self, parameter_s = ''):
500 """Make magic functions callable without having to type the initial %.
501 """Make magic functions callable without having to type the initial %.
501
502
502 Without argumentsl toggles on/off (when off, you must call it as
503 Without argumentsl toggles on/off (when off, you must call it as
503 %automagic, of course). With arguments it sets the value, and you can
504 %automagic, of course). With arguments it sets the value, and you can
504 use any of (case insensitive):
505 use any of (case insensitive):
505
506
506 - on,1,True: to activate
507 - on,1,True: to activate
507
508
508 - off,0,False: to deactivate.
509 - off,0,False: to deactivate.
509
510
510 Note that magic functions have lowest priority, so if there's a
511 Note that magic functions have lowest priority, so if there's a
511 variable whose name collides with that of a magic fn, automagic won't
512 variable whose name collides with that of a magic fn, automagic won't
512 work for that function (you get the variable instead). However, if you
513 work for that function (you get the variable instead). However, if you
513 delete the variable (del var), the previously shadowed magic function
514 delete the variable (del var), the previously shadowed magic function
514 becomes visible to automagic again."""
515 becomes visible to automagic again."""
515
516
516 arg = parameter_s.lower()
517 arg = parameter_s.lower()
517 if parameter_s in ('on','1','true'):
518 if parameter_s in ('on','1','true'):
518 self.shell.automagic = True
519 self.shell.automagic = True
519 elif parameter_s in ('off','0','false'):
520 elif parameter_s in ('off','0','false'):
520 self.shell.automagic = False
521 self.shell.automagic = False
521 else:
522 else:
522 self.shell.automagic = not self.shell.automagic
523 self.shell.automagic = not self.shell.automagic
523 print '\n' + Magic.auto_status[self.shell.automagic]
524 print '\n' + Magic.auto_status[self.shell.automagic]
524
525
525 @testdec.skip_doctest
526 @testdec.skip_doctest
526 def magic_autocall(self, parameter_s = ''):
527 def magic_autocall(self, parameter_s = ''):
527 """Make functions callable without having to type parentheses.
528 """Make functions callable without having to type parentheses.
528
529
529 Usage:
530 Usage:
530
531
531 %autocall [mode]
532 %autocall [mode]
532
533
533 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
534 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
534 value is toggled on and off (remembering the previous state).
535 value is toggled on and off (remembering the previous state).
535
536
536 In more detail, these values mean:
537 In more detail, these values mean:
537
538
538 0 -> fully disabled
539 0 -> fully disabled
539
540
540 1 -> active, but do not apply if there are no arguments on the line.
541 1 -> active, but do not apply if there are no arguments on the line.
541
542
542 In this mode, you get:
543 In this mode, you get:
543
544
544 In [1]: callable
545 In [1]: callable
545 Out[1]: <built-in function callable>
546 Out[1]: <built-in function callable>
546
547
547 In [2]: callable 'hello'
548 In [2]: callable 'hello'
548 ------> callable('hello')
549 ------> callable('hello')
549 Out[2]: False
550 Out[2]: False
550
551
551 2 -> Active always. Even if no arguments are present, the callable
552 2 -> Active always. Even if no arguments are present, the callable
552 object is called:
553 object is called:
553
554
554 In [2]: float
555 In [2]: float
555 ------> float()
556 ------> float()
556 Out[2]: 0.0
557 Out[2]: 0.0
557
558
558 Note that even with autocall off, you can still use '/' at the start of
559 Note that even with autocall off, you can still use '/' at the start of
559 a line to treat the first argument on the command line as a function
560 a line to treat the first argument on the command line as a function
560 and add parentheses to it:
561 and add parentheses to it:
561
562
562 In [8]: /str 43
563 In [8]: /str 43
563 ------> str(43)
564 ------> str(43)
564 Out[8]: '43'
565 Out[8]: '43'
565
566
566 # all-random (note for auto-testing)
567 # all-random (note for auto-testing)
567 """
568 """
568
569
569 if parameter_s:
570 if parameter_s:
570 arg = int(parameter_s)
571 arg = int(parameter_s)
571 else:
572 else:
572 arg = 'toggle'
573 arg = 'toggle'
573
574
574 if not arg in (0,1,2,'toggle'):
575 if not arg in (0,1,2,'toggle'):
575 error('Valid modes: (0->Off, 1->Smart, 2->Full')
576 error('Valid modes: (0->Off, 1->Smart, 2->Full')
576 return
577 return
577
578
578 if arg in (0,1,2):
579 if arg in (0,1,2):
579 self.shell.autocall = arg
580 self.shell.autocall = arg
580 else: # toggle
581 else: # toggle
581 if self.shell.autocall:
582 if self.shell.autocall:
582 self._magic_state.autocall_save = self.shell.autocall
583 self._magic_state.autocall_save = self.shell.autocall
583 self.shell.autocall = 0
584 self.shell.autocall = 0
584 else:
585 else:
585 try:
586 try:
586 self.shell.autocall = self._magic_state.autocall_save
587 self.shell.autocall = self._magic_state.autocall_save
587 except AttributeError:
588 except AttributeError:
588 self.shell.autocall = self._magic_state.autocall_save = 1
589 self.shell.autocall = self._magic_state.autocall_save = 1
589
590
590 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
591 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
591
592
592 def magic_system_verbose(self, parameter_s = ''):
593 def magic_system_verbose(self, parameter_s = ''):
593 """Set verbose printing of system calls.
594 """Set verbose printing of system calls.
594
595
595 If called without an argument, act as a toggle"""
596 If called without an argument, act as a toggle"""
596
597
597 if parameter_s:
598 if parameter_s:
598 val = bool(eval(parameter_s))
599 val = bool(eval(parameter_s))
599 else:
600 else:
600 val = None
601 val = None
601
602
602 if self.shell.system_verbose:
603 if self.shell.system_verbose:
603 self.shell.system_verbose = False
604 self.shell.system_verbose = False
604 else:
605 else:
605 self.shell.system_verbose = True
606 self.shell.system_verbose = True
606 print "System verbose printing is:",\
607 print "System verbose printing is:",\
607 ['OFF','ON'][self.shell.system_verbose]
608 ['OFF','ON'][self.shell.system_verbose]
608
609
609
610
610 def magic_page(self, parameter_s=''):
611 def magic_page(self, parameter_s=''):
611 """Pretty print the object and display it through a pager.
612 """Pretty print the object and display it through a pager.
612
613
613 %page [options] OBJECT
614 %page [options] OBJECT
614
615
615 If no object is given, use _ (last output).
616 If no object is given, use _ (last output).
616
617
617 Options:
618 Options:
618
619
619 -r: page str(object), don't pretty-print it."""
620 -r: page str(object), don't pretty-print it."""
620
621
621 # After a function contributed by Olivier Aubert, slightly modified.
622 # After a function contributed by Olivier Aubert, slightly modified.
622
623
623 # Process options/args
624 # Process options/args
624 opts,args = self.parse_options(parameter_s,'r')
625 opts,args = self.parse_options(parameter_s,'r')
625 raw = 'r' in opts
626 raw = 'r' in opts
626
627
627 oname = args and args or '_'
628 oname = args and args or '_'
628 info = self._ofind(oname)
629 info = self._ofind(oname)
629 if info['found']:
630 if info['found']:
630 txt = (raw and str or pformat)( info['obj'] )
631 txt = (raw and str or pformat)( info['obj'] )
631 page(txt)
632 page(txt)
632 else:
633 else:
633 print 'Object `%s` not found' % oname
634 print 'Object `%s` not found' % oname
634
635
635 def magic_profile(self, parameter_s=''):
636 def magic_profile(self, parameter_s=''):
636 """Print your currently active IPyhton profile."""
637 """Print your currently active IPyhton profile."""
637 if self.shell.profile:
638 if self.shell.profile:
638 printpl('Current IPython profile: $self.shell.profile.')
639 printpl('Current IPython profile: $self.shell.profile.')
639 else:
640 else:
640 print 'No profile active.'
641 print 'No profile active.'
641
642
642 def magic_pinfo(self, parameter_s='', namespaces=None):
643 def magic_pinfo(self, parameter_s='', namespaces=None):
643 """Provide detailed information about an object.
644 """Provide detailed information about an object.
644
645
645 '%pinfo object' is just a synonym for object? or ?object."""
646 '%pinfo object' is just a synonym for object? or ?object."""
646
647
647 #print 'pinfo par: <%s>' % parameter_s # dbg
648 #print 'pinfo par: <%s>' % parameter_s # dbg
648
649
649
650
650 # detail_level: 0 -> obj? , 1 -> obj??
651 # detail_level: 0 -> obj? , 1 -> obj??
651 detail_level = 0
652 detail_level = 0
652 # We need to detect if we got called as 'pinfo pinfo foo', which can
653 # We need to detect if we got called as 'pinfo pinfo foo', which can
653 # happen if the user types 'pinfo foo?' at the cmd line.
654 # happen if the user types 'pinfo foo?' at the cmd line.
654 pinfo,qmark1,oname,qmark2 = \
655 pinfo,qmark1,oname,qmark2 = \
655 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
656 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
656 if pinfo or qmark1 or qmark2:
657 if pinfo or qmark1 or qmark2:
657 detail_level = 1
658 detail_level = 1
658 if "*" in oname:
659 if "*" in oname:
659 self.magic_psearch(oname)
660 self.magic_psearch(oname)
660 else:
661 else:
661 self._inspect('pinfo', oname, detail_level=detail_level,
662 self._inspect('pinfo', oname, detail_level=detail_level,
662 namespaces=namespaces)
663 namespaces=namespaces)
663
664
664 def magic_pdef(self, parameter_s='', namespaces=None):
665 def magic_pdef(self, parameter_s='', namespaces=None):
665 """Print the definition header for any callable object.
666 """Print the definition header for any callable object.
666
667
667 If the object is a class, print the constructor information."""
668 If the object is a class, print the constructor information."""
668 self._inspect('pdef',parameter_s, namespaces)
669 self._inspect('pdef',parameter_s, namespaces)
669
670
670 def magic_pdoc(self, parameter_s='', namespaces=None):
671 def magic_pdoc(self, parameter_s='', namespaces=None):
671 """Print the docstring for an object.
672 """Print the docstring for an object.
672
673
673 If the given object is a class, it will print both the class and the
674 If the given object is a class, it will print both the class and the
674 constructor docstrings."""
675 constructor docstrings."""
675 self._inspect('pdoc',parameter_s, namespaces)
676 self._inspect('pdoc',parameter_s, namespaces)
676
677
677 def magic_psource(self, parameter_s='', namespaces=None):
678 def magic_psource(self, parameter_s='', namespaces=None):
678 """Print (or run through pager) the source code for an object."""
679 """Print (or run through pager) the source code for an object."""
679 self._inspect('psource',parameter_s, namespaces)
680 self._inspect('psource',parameter_s, namespaces)
680
681
681 def magic_pfile(self, parameter_s=''):
682 def magic_pfile(self, parameter_s=''):
682 """Print (or run through pager) the file where an object is defined.
683 """Print (or run through pager) the file where an object is defined.
683
684
684 The file opens at the line where the object definition begins. IPython
685 The file opens at the line where the object definition begins. IPython
685 will honor the environment variable PAGER if set, and otherwise will
686 will honor the environment variable PAGER if set, and otherwise will
686 do its best to print the file in a convenient form.
687 do its best to print the file in a convenient form.
687
688
688 If the given argument is not an object currently defined, IPython will
689 If the given argument is not an object currently defined, IPython will
689 try to interpret it as a filename (automatically adding a .py extension
690 try to interpret it as a filename (automatically adding a .py extension
690 if needed). You can thus use %pfile as a syntax highlighting code
691 if needed). You can thus use %pfile as a syntax highlighting code
691 viewer."""
692 viewer."""
692
693
693 # first interpret argument as an object name
694 # first interpret argument as an object name
694 out = self._inspect('pfile',parameter_s)
695 out = self._inspect('pfile',parameter_s)
695 # if not, try the input as a filename
696 # if not, try the input as a filename
696 if out == 'not found':
697 if out == 'not found':
697 try:
698 try:
698 filename = get_py_filename(parameter_s)
699 filename = get_py_filename(parameter_s)
699 except IOError,msg:
700 except IOError,msg:
700 print msg
701 print msg
701 return
702 return
702 page(self.shell.inspector.format(file(filename).read()))
703 page(self.shell.inspector.format(file(filename).read()))
703
704
704 def _inspect(self,meth,oname,namespaces=None,**kw):
705 def _inspect(self,meth,oname,namespaces=None,**kw):
705 """Generic interface to the inspector system.
706 """Generic interface to the inspector system.
706
707
707 This function is meant to be called by pdef, pdoc & friends."""
708 This function is meant to be called by pdef, pdoc & friends."""
708
709
709 #oname = oname.strip()
710 #oname = oname.strip()
710 #print '1- oname: <%r>' % oname # dbg
711 #print '1- oname: <%r>' % oname # dbg
711 try:
712 try:
712 oname = oname.strip().encode('ascii')
713 oname = oname.strip().encode('ascii')
713 #print '2- oname: <%r>' % oname # dbg
714 #print '2- oname: <%r>' % oname # dbg
714 except UnicodeEncodeError:
715 except UnicodeEncodeError:
715 print 'Python identifiers can only contain ascii characters.'
716 print 'Python identifiers can only contain ascii characters.'
716 return 'not found'
717 return 'not found'
717
718
718 info = Struct(self._ofind(oname, namespaces))
719 info = Struct(self._ofind(oname, namespaces))
719
720
720 if info.found:
721 if info.found:
721 try:
722 try:
722 IPython.utils.generics.inspect_object(info.obj)
723 IPython.utils.generics.inspect_object(info.obj)
723 return
724 return
724 except TryNext:
725 except TryNext:
725 pass
726 pass
726 # Get the docstring of the class property if it exists.
727 # Get the docstring of the class property if it exists.
727 path = oname.split('.')
728 path = oname.split('.')
728 root = '.'.join(path[:-1])
729 root = '.'.join(path[:-1])
729 if info.parent is not None:
730 if info.parent is not None:
730 try:
731 try:
731 target = getattr(info.parent, '__class__')
732 target = getattr(info.parent, '__class__')
732 # The object belongs to a class instance.
733 # The object belongs to a class instance.
733 try:
734 try:
734 target = getattr(target, path[-1])
735 target = getattr(target, path[-1])
735 # The class defines the object.
736 # The class defines the object.
736 if isinstance(target, property):
737 if isinstance(target, property):
737 oname = root + '.__class__.' + path[-1]
738 oname = root + '.__class__.' + path[-1]
738 info = Struct(self._ofind(oname))
739 info = Struct(self._ofind(oname))
739 except AttributeError: pass
740 except AttributeError: pass
740 except AttributeError: pass
741 except AttributeError: pass
741
742
742 pmethod = getattr(self.shell.inspector,meth)
743 pmethod = getattr(self.shell.inspector,meth)
743 formatter = info.ismagic and self.format_screen or None
744 formatter = info.ismagic and self.format_screen or None
744 if meth == 'pdoc':
745 if meth == 'pdoc':
745 pmethod(info.obj,oname,formatter)
746 pmethod(info.obj,oname,formatter)
746 elif meth == 'pinfo':
747 elif meth == 'pinfo':
747 pmethod(info.obj,oname,formatter,info,**kw)
748 pmethod(info.obj,oname,formatter,info,**kw)
748 else:
749 else:
749 pmethod(info.obj,oname)
750 pmethod(info.obj,oname)
750 else:
751 else:
751 print 'Object `%s` not found.' % oname
752 print 'Object `%s` not found.' % oname
752 return 'not found' # so callers can take other action
753 return 'not found' # so callers can take other action
753
754
754 def magic_psearch(self, parameter_s=''):
755 def magic_psearch(self, parameter_s=''):
755 """Search for object in namespaces by wildcard.
756 """Search for object in namespaces by wildcard.
756
757
757 %psearch [options] PATTERN [OBJECT TYPE]
758 %psearch [options] PATTERN [OBJECT TYPE]
758
759
759 Note: ? can be used as a synonym for %psearch, at the beginning or at
760 Note: ? can be used as a synonym for %psearch, at the beginning or at
760 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
761 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
761 rest of the command line must be unchanged (options come first), so
762 rest of the command line must be unchanged (options come first), so
762 for example the following forms are equivalent
763 for example the following forms are equivalent
763
764
764 %psearch -i a* function
765 %psearch -i a* function
765 -i a* function?
766 -i a* function?
766 ?-i a* function
767 ?-i a* function
767
768
768 Arguments:
769 Arguments:
769
770
770 PATTERN
771 PATTERN
771
772
772 where PATTERN is a string containing * as a wildcard similar to its
773 where PATTERN is a string containing * as a wildcard similar to its
773 use in a shell. The pattern is matched in all namespaces on the
774 use in a shell. The pattern is matched in all namespaces on the
774 search path. By default objects starting with a single _ are not
775 search path. By default objects starting with a single _ are not
775 matched, many IPython generated objects have a single
776 matched, many IPython generated objects have a single
776 underscore. The default is case insensitive matching. Matching is
777 underscore. The default is case insensitive matching. Matching is
777 also done on the attributes of objects and not only on the objects
778 also done on the attributes of objects and not only on the objects
778 in a module.
779 in a module.
779
780
780 [OBJECT TYPE]
781 [OBJECT TYPE]
781
782
782 Is the name of a python type from the types module. The name is
783 Is the name of a python type from the types module. The name is
783 given in lowercase without the ending type, ex. StringType is
784 given in lowercase without the ending type, ex. StringType is
784 written string. By adding a type here only objects matching the
785 written string. By adding a type here only objects matching the
785 given type are matched. Using all here makes the pattern match all
786 given type are matched. Using all here makes the pattern match all
786 types (this is the default).
787 types (this is the default).
787
788
788 Options:
789 Options:
789
790
790 -a: makes the pattern match even objects whose names start with a
791 -a: makes the pattern match even objects whose names start with a
791 single underscore. These names are normally ommitted from the
792 single underscore. These names are normally ommitted from the
792 search.
793 search.
793
794
794 -i/-c: make the pattern case insensitive/sensitive. If neither of
795 -i/-c: make the pattern case insensitive/sensitive. If neither of
795 these options is given, the default is read from your ipythonrc
796 these options is given, the default is read from your ipythonrc
796 file. The option name which sets this value is
797 file. The option name which sets this value is
797 'wildcards_case_sensitive'. If this option is not specified in your
798 'wildcards_case_sensitive'. If this option is not specified in your
798 ipythonrc file, IPython's internal default is to do a case sensitive
799 ipythonrc file, IPython's internal default is to do a case sensitive
799 search.
800 search.
800
801
801 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
802 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
802 specifiy can be searched in any of the following namespaces:
803 specifiy can be searched in any of the following namespaces:
803 'builtin', 'user', 'user_global','internal', 'alias', where
804 'builtin', 'user', 'user_global','internal', 'alias', where
804 'builtin' and 'user' are the search defaults. Note that you should
805 'builtin' and 'user' are the search defaults. Note that you should
805 not use quotes when specifying namespaces.
806 not use quotes when specifying namespaces.
806
807
807 'Builtin' contains the python module builtin, 'user' contains all
808 'Builtin' contains the python module builtin, 'user' contains all
808 user data, 'alias' only contain the shell aliases and no python
809 user data, 'alias' only contain the shell aliases and no python
809 objects, 'internal' contains objects used by IPython. The
810 objects, 'internal' contains objects used by IPython. The
810 'user_global' namespace is only used by embedded IPython instances,
811 'user_global' namespace is only used by embedded IPython instances,
811 and it contains module-level globals. You can add namespaces to the
812 and it contains module-level globals. You can add namespaces to the
812 search with -s or exclude them with -e (these options can be given
813 search with -s or exclude them with -e (these options can be given
813 more than once).
814 more than once).
814
815
815 Examples:
816 Examples:
816
817
817 %psearch a* -> objects beginning with an a
818 %psearch a* -> objects beginning with an a
818 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
819 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
819 %psearch a* function -> all functions beginning with an a
820 %psearch a* function -> all functions beginning with an a
820 %psearch re.e* -> objects beginning with an e in module re
821 %psearch re.e* -> objects beginning with an e in module re
821 %psearch r*.e* -> objects that start with e in modules starting in r
822 %psearch r*.e* -> objects that start with e in modules starting in r
822 %psearch r*.* string -> all strings in modules beginning with r
823 %psearch r*.* string -> all strings in modules beginning with r
823
824
824 Case sensitve search:
825 Case sensitve search:
825
826
826 %psearch -c a* list all object beginning with lower case a
827 %psearch -c a* list all object beginning with lower case a
827
828
828 Show objects beginning with a single _:
829 Show objects beginning with a single _:
829
830
830 %psearch -a _* list objects beginning with a single underscore"""
831 %psearch -a _* list objects beginning with a single underscore"""
831 try:
832 try:
832 parameter_s = parameter_s.encode('ascii')
833 parameter_s = parameter_s.encode('ascii')
833 except UnicodeEncodeError:
834 except UnicodeEncodeError:
834 print 'Python identifiers can only contain ascii characters.'
835 print 'Python identifiers can only contain ascii characters.'
835 return
836 return
836
837
837 # default namespaces to be searched
838 # default namespaces to be searched
838 def_search = ['user','builtin']
839 def_search = ['user','builtin']
839
840
840 # Process options/args
841 # Process options/args
841 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
842 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
842 opt = opts.get
843 opt = opts.get
843 shell = self.shell
844 shell = self.shell
844 psearch = shell.inspector.psearch
845 psearch = shell.inspector.psearch
845
846
846 # select case options
847 # select case options
847 if opts.has_key('i'):
848 if opts.has_key('i'):
848 ignore_case = True
849 ignore_case = True
849 elif opts.has_key('c'):
850 elif opts.has_key('c'):
850 ignore_case = False
851 ignore_case = False
851 else:
852 else:
852 ignore_case = not shell.wildcards_case_sensitive
853 ignore_case = not shell.wildcards_case_sensitive
853
854
854 # Build list of namespaces to search from user options
855 # Build list of namespaces to search from user options
855 def_search.extend(opt('s',[]))
856 def_search.extend(opt('s',[]))
856 ns_exclude = ns_exclude=opt('e',[])
857 ns_exclude = ns_exclude=opt('e',[])
857 ns_search = [nm for nm in def_search if nm not in ns_exclude]
858 ns_search = [nm for nm in def_search if nm not in ns_exclude]
858
859
859 # Call the actual search
860 # Call the actual search
860 try:
861 try:
861 psearch(args,shell.ns_table,ns_search,
862 psearch(args,shell.ns_table,ns_search,
862 show_all=opt('a'),ignore_case=ignore_case)
863 show_all=opt('a'),ignore_case=ignore_case)
863 except:
864 except:
864 shell.showtraceback()
865 shell.showtraceback()
865
866
866 def magic_who_ls(self, parameter_s=''):
867 def magic_who_ls(self, parameter_s=''):
867 """Return a sorted list of all interactive variables.
868 """Return a sorted list of all interactive variables.
868
869
869 If arguments are given, only variables of types matching these
870 If arguments are given, only variables of types matching these
870 arguments are returned."""
871 arguments are returned."""
871
872
872 user_ns = self.shell.user_ns
873 user_ns = self.shell.user_ns
873 internal_ns = self.shell.internal_ns
874 internal_ns = self.shell.internal_ns
874 user_config_ns = self.shell.user_config_ns
875 user_config_ns = self.shell.user_config_ns
875 out = []
876 out = []
876 typelist = parameter_s.split()
877 typelist = parameter_s.split()
877
878
878 for i in user_ns:
879 for i in user_ns:
879 if not (i.startswith('_') or i.startswith('_i')) \
880 if not (i.startswith('_') or i.startswith('_i')) \
880 and not (i in internal_ns or i in user_config_ns):
881 and not (i in internal_ns or i in user_config_ns):
881 if typelist:
882 if typelist:
882 if type(user_ns[i]).__name__ in typelist:
883 if type(user_ns[i]).__name__ in typelist:
883 out.append(i)
884 out.append(i)
884 else:
885 else:
885 out.append(i)
886 out.append(i)
886 out.sort()
887 out.sort()
887 return out
888 return out
888
889
889 def magic_who(self, parameter_s=''):
890 def magic_who(self, parameter_s=''):
890 """Print all interactive variables, with some minimal formatting.
891 """Print all interactive variables, with some minimal formatting.
891
892
892 If any arguments are given, only variables whose type matches one of
893 If any arguments are given, only variables whose type matches one of
893 these are printed. For example:
894 these are printed. For example:
894
895
895 %who function str
896 %who function str
896
897
897 will only list functions and strings, excluding all other types of
898 will only list functions and strings, excluding all other types of
898 variables. To find the proper type names, simply use type(var) at a
899 variables. To find the proper type names, simply use type(var) at a
899 command line to see how python prints type names. For example:
900 command line to see how python prints type names. For example:
900
901
901 In [1]: type('hello')\\
902 In [1]: type('hello')\\
902 Out[1]: <type 'str'>
903 Out[1]: <type 'str'>
903
904
904 indicates that the type name for strings is 'str'.
905 indicates that the type name for strings is 'str'.
905
906
906 %who always excludes executed names loaded through your configuration
907 %who always excludes executed names loaded through your configuration
907 file and things which are internal to IPython.
908 file and things which are internal to IPython.
908
909
909 This is deliberate, as typically you may load many modules and the
910 This is deliberate, as typically you may load many modules and the
910 purpose of %who is to show you only what you've manually defined."""
911 purpose of %who is to show you only what you've manually defined."""
911
912
912 varlist = self.magic_who_ls(parameter_s)
913 varlist = self.magic_who_ls(parameter_s)
913 if not varlist:
914 if not varlist:
914 if parameter_s:
915 if parameter_s:
915 print 'No variables match your requested type.'
916 print 'No variables match your requested type.'
916 else:
917 else:
917 print 'Interactive namespace is empty.'
918 print 'Interactive namespace is empty.'
918 return
919 return
919
920
920 # if we have variables, move on...
921 # if we have variables, move on...
921 count = 0
922 count = 0
922 for i in varlist:
923 for i in varlist:
923 print i+'\t',
924 print i+'\t',
924 count += 1
925 count += 1
925 if count > 8:
926 if count > 8:
926 count = 0
927 count = 0
927 print
928 print
928 print
929 print
929
930
930 def magic_whos(self, parameter_s=''):
931 def magic_whos(self, parameter_s=''):
931 """Like %who, but gives some extra information about each variable.
932 """Like %who, but gives some extra information about each variable.
932
933
933 The same type filtering of %who can be applied here.
934 The same type filtering of %who can be applied here.
934
935
935 For all variables, the type is printed. Additionally it prints:
936 For all variables, the type is printed. Additionally it prints:
936
937
937 - For {},[],(): their length.
938 - For {},[],(): their length.
938
939
939 - For numpy and Numeric arrays, a summary with shape, number of
940 - For numpy and Numeric arrays, a summary with shape, number of
940 elements, typecode and size in memory.
941 elements, typecode and size in memory.
941
942
942 - Everything else: a string representation, snipping their middle if
943 - Everything else: a string representation, snipping their middle if
943 too long."""
944 too long."""
944
945
945 varnames = self.magic_who_ls(parameter_s)
946 varnames = self.magic_who_ls(parameter_s)
946 if not varnames:
947 if not varnames:
947 if parameter_s:
948 if parameter_s:
948 print 'No variables match your requested type.'
949 print 'No variables match your requested type.'
949 else:
950 else:
950 print 'Interactive namespace is empty.'
951 print 'Interactive namespace is empty.'
951 return
952 return
952
953
953 # if we have variables, move on...
954 # if we have variables, move on...
954
955
955 # for these types, show len() instead of data:
956 # for these types, show len() instead of data:
956 seq_types = [types.DictType,types.ListType,types.TupleType]
957 seq_types = [types.DictType,types.ListType,types.TupleType]
957
958
958 # for numpy/Numeric arrays, display summary info
959 # for numpy/Numeric arrays, display summary info
959 try:
960 try:
960 import numpy
961 import numpy
961 except ImportError:
962 except ImportError:
962 ndarray_type = None
963 ndarray_type = None
963 else:
964 else:
964 ndarray_type = numpy.ndarray.__name__
965 ndarray_type = numpy.ndarray.__name__
965 try:
966 try:
966 import Numeric
967 import Numeric
967 except ImportError:
968 except ImportError:
968 array_type = None
969 array_type = None
969 else:
970 else:
970 array_type = Numeric.ArrayType.__name__
971 array_type = Numeric.ArrayType.__name__
971
972
972 # Find all variable names and types so we can figure out column sizes
973 # Find all variable names and types so we can figure out column sizes
973 def get_vars(i):
974 def get_vars(i):
974 return self.shell.user_ns[i]
975 return self.shell.user_ns[i]
975
976
976 # some types are well known and can be shorter
977 # some types are well known and can be shorter
977 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
978 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
978 def type_name(v):
979 def type_name(v):
979 tn = type(v).__name__
980 tn = type(v).__name__
980 return abbrevs.get(tn,tn)
981 return abbrevs.get(tn,tn)
981
982
982 varlist = map(get_vars,varnames)
983 varlist = map(get_vars,varnames)
983
984
984 typelist = []
985 typelist = []
985 for vv in varlist:
986 for vv in varlist:
986 tt = type_name(vv)
987 tt = type_name(vv)
987
988
988 if tt=='instance':
989 if tt=='instance':
989 typelist.append( abbrevs.get(str(vv.__class__),
990 typelist.append( abbrevs.get(str(vv.__class__),
990 str(vv.__class__)))
991 str(vv.__class__)))
991 else:
992 else:
992 typelist.append(tt)
993 typelist.append(tt)
993
994
994 # column labels and # of spaces as separator
995 # column labels and # of spaces as separator
995 varlabel = 'Variable'
996 varlabel = 'Variable'
996 typelabel = 'Type'
997 typelabel = 'Type'
997 datalabel = 'Data/Info'
998 datalabel = 'Data/Info'
998 colsep = 3
999 colsep = 3
999 # variable format strings
1000 # variable format strings
1000 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1001 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
1001 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1002 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
1002 aformat = "%s: %s elems, type `%s`, %s bytes"
1003 aformat = "%s: %s elems, type `%s`, %s bytes"
1003 # find the size of the columns to format the output nicely
1004 # find the size of the columns to format the output nicely
1004 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1005 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
1005 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1006 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
1006 # table header
1007 # table header
1007 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1008 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
1008 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1009 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
1009 # and the table itself
1010 # and the table itself
1010 kb = 1024
1011 kb = 1024
1011 Mb = 1048576 # kb**2
1012 Mb = 1048576 # kb**2
1012 for vname,var,vtype in zip(varnames,varlist,typelist):
1013 for vname,var,vtype in zip(varnames,varlist,typelist):
1013 print itpl(vformat),
1014 print itpl(vformat),
1014 if vtype in seq_types:
1015 if vtype in seq_types:
1015 print len(var)
1016 print len(var)
1016 elif vtype in [array_type,ndarray_type]:
1017 elif vtype in [array_type,ndarray_type]:
1017 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1018 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
1018 if vtype==ndarray_type:
1019 if vtype==ndarray_type:
1019 # numpy
1020 # numpy
1020 vsize = var.size
1021 vsize = var.size
1021 vbytes = vsize*var.itemsize
1022 vbytes = vsize*var.itemsize
1022 vdtype = var.dtype
1023 vdtype = var.dtype
1023 else:
1024 else:
1024 # Numeric
1025 # Numeric
1025 vsize = Numeric.size(var)
1026 vsize = Numeric.size(var)
1026 vbytes = vsize*var.itemsize()
1027 vbytes = vsize*var.itemsize()
1027 vdtype = var.typecode()
1028 vdtype = var.typecode()
1028
1029
1029 if vbytes < 100000:
1030 if vbytes < 100000:
1030 print aformat % (vshape,vsize,vdtype,vbytes)
1031 print aformat % (vshape,vsize,vdtype,vbytes)
1031 else:
1032 else:
1032 print aformat % (vshape,vsize,vdtype,vbytes),
1033 print aformat % (vshape,vsize,vdtype,vbytes),
1033 if vbytes < Mb:
1034 if vbytes < Mb:
1034 print '(%s kb)' % (vbytes/kb,)
1035 print '(%s kb)' % (vbytes/kb,)
1035 else:
1036 else:
1036 print '(%s Mb)' % (vbytes/Mb,)
1037 print '(%s Mb)' % (vbytes/Mb,)
1037 else:
1038 else:
1038 try:
1039 try:
1039 vstr = str(var)
1040 vstr = str(var)
1040 except UnicodeEncodeError:
1041 except UnicodeEncodeError:
1041 vstr = unicode(var).encode(sys.getdefaultencoding(),
1042 vstr = unicode(var).encode(sys.getdefaultencoding(),
1042 'backslashreplace')
1043 'backslashreplace')
1043 vstr = vstr.replace('\n','\\n')
1044 vstr = vstr.replace('\n','\\n')
1044 if len(vstr) < 50:
1045 if len(vstr) < 50:
1045 print vstr
1046 print vstr
1046 else:
1047 else:
1047 printpl(vfmt_short)
1048 printpl(vfmt_short)
1048
1049
1049 def magic_reset(self, parameter_s=''):
1050 def magic_reset(self, parameter_s=''):
1050 """Resets the namespace by removing all names defined by the user.
1051 """Resets the namespace by removing all names defined by the user.
1051
1052
1052 Input/Output history are left around in case you need them.
1053 Input/Output history are left around in case you need them.
1053
1054
1054 Parameters
1055 Parameters
1055 ----------
1056 ----------
1056 -y : force reset without asking for confirmation.
1057 -y : force reset without asking for confirmation.
1057
1058
1058 Examples
1059 Examples
1059 --------
1060 --------
1060 In [6]: a = 1
1061 In [6]: a = 1
1061
1062
1062 In [7]: a
1063 In [7]: a
1063 Out[7]: 1
1064 Out[7]: 1
1064
1065
1065 In [8]: 'a' in _ip.user_ns
1066 In [8]: 'a' in _ip.user_ns
1066 Out[8]: True
1067 Out[8]: True
1067
1068
1068 In [9]: %reset -f
1069 In [9]: %reset -f
1069
1070
1070 In [10]: 'a' in _ip.user_ns
1071 In [10]: 'a' in _ip.user_ns
1071 Out[10]: False
1072 Out[10]: False
1072 """
1073 """
1073
1074
1074 if parameter_s == '-f':
1075 if parameter_s == '-f':
1075 ans = True
1076 ans = True
1076 else:
1077 else:
1077 ans = self.shell.ask_yes_no(
1078 ans = self.shell.ask_yes_no(
1078 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1079 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1079 if not ans:
1080 if not ans:
1080 print 'Nothing done.'
1081 print 'Nothing done.'
1081 return
1082 return
1082 user_ns = self.shell.user_ns
1083 user_ns = self.shell.user_ns
1083 for i in self.magic_who_ls():
1084 for i in self.magic_who_ls():
1084 del(user_ns[i])
1085 del(user_ns[i])
1085
1086
1086 # Also flush the private list of module references kept for script
1087 # Also flush the private list of module references kept for script
1087 # execution protection
1088 # execution protection
1088 self.shell.clear_main_mod_cache()
1089 self.shell.clear_main_mod_cache()
1089
1090
1090 def magic_logstart(self,parameter_s=''):
1091 def magic_logstart(self,parameter_s=''):
1091 """Start logging anywhere in a session.
1092 """Start logging anywhere in a session.
1092
1093
1093 %logstart [-o|-r|-t] [log_name [log_mode]]
1094 %logstart [-o|-r|-t] [log_name [log_mode]]
1094
1095
1095 If no name is given, it defaults to a file named 'ipython_log.py' in your
1096 If no name is given, it defaults to a file named 'ipython_log.py' in your
1096 current directory, in 'rotate' mode (see below).
1097 current directory, in 'rotate' mode (see below).
1097
1098
1098 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1099 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1099 history up to that point and then continues logging.
1100 history up to that point and then continues logging.
1100
1101
1101 %logstart takes a second optional parameter: logging mode. This can be one
1102 %logstart takes a second optional parameter: logging mode. This can be one
1102 of (note that the modes are given unquoted):\\
1103 of (note that the modes are given unquoted):\\
1103 append: well, that says it.\\
1104 append: well, that says it.\\
1104 backup: rename (if exists) to name~ and start name.\\
1105 backup: rename (if exists) to name~ and start name.\\
1105 global: single logfile in your home dir, appended to.\\
1106 global: single logfile in your home dir, appended to.\\
1106 over : overwrite existing log.\\
1107 over : overwrite existing log.\\
1107 rotate: create rotating logs name.1~, name.2~, etc.
1108 rotate: create rotating logs name.1~, name.2~, etc.
1108
1109
1109 Options:
1110 Options:
1110
1111
1111 -o: log also IPython's output. In this mode, all commands which
1112 -o: log also IPython's output. In this mode, all commands which
1112 generate an Out[NN] prompt are recorded to the logfile, right after
1113 generate an Out[NN] prompt are recorded to the logfile, right after
1113 their corresponding input line. The output lines are always
1114 their corresponding input line. The output lines are always
1114 prepended with a '#[Out]# ' marker, so that the log remains valid
1115 prepended with a '#[Out]# ' marker, so that the log remains valid
1115 Python code.
1116 Python code.
1116
1117
1117 Since this marker is always the same, filtering only the output from
1118 Since this marker is always the same, filtering only the output from
1118 a log is very easy, using for example a simple awk call:
1119 a log is very easy, using for example a simple awk call:
1119
1120
1120 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1121 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1121
1122
1122 -r: log 'raw' input. Normally, IPython's logs contain the processed
1123 -r: log 'raw' input. Normally, IPython's logs contain the processed
1123 input, so that user lines are logged in their final form, converted
1124 input, so that user lines are logged in their final form, converted
1124 into valid Python. For example, %Exit is logged as
1125 into valid Python. For example, %Exit is logged as
1125 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1126 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1126 exactly as typed, with no transformations applied.
1127 exactly as typed, with no transformations applied.
1127
1128
1128 -t: put timestamps before each input line logged (these are put in
1129 -t: put timestamps before each input line logged (these are put in
1129 comments)."""
1130 comments)."""
1130
1131
1131 opts,par = self.parse_options(parameter_s,'ort')
1132 opts,par = self.parse_options(parameter_s,'ort')
1132 log_output = 'o' in opts
1133 log_output = 'o' in opts
1133 log_raw_input = 'r' in opts
1134 log_raw_input = 'r' in opts
1134 timestamp = 't' in opts
1135 timestamp = 't' in opts
1135
1136
1136 logger = self.shell.logger
1137 logger = self.shell.logger
1137
1138
1138 # if no args are given, the defaults set in the logger constructor by
1139 # if no args are given, the defaults set in the logger constructor by
1139 # ipytohn remain valid
1140 # ipytohn remain valid
1140 if par:
1141 if par:
1141 try:
1142 try:
1142 logfname,logmode = par.split()
1143 logfname,logmode = par.split()
1143 except:
1144 except:
1144 logfname = par
1145 logfname = par
1145 logmode = 'backup'
1146 logmode = 'backup'
1146 else:
1147 else:
1147 logfname = logger.logfname
1148 logfname = logger.logfname
1148 logmode = logger.logmode
1149 logmode = logger.logmode
1149 # put logfname into rc struct as if it had been called on the command
1150 # put logfname into rc struct as if it had been called on the command
1150 # line, so it ends up saved in the log header Save it in case we need
1151 # line, so it ends up saved in the log header Save it in case we need
1151 # to restore it...
1152 # to restore it...
1152 old_logfile = self.shell.logfile
1153 old_logfile = self.shell.logfile
1153 if logfname:
1154 if logfname:
1154 logfname = os.path.expanduser(logfname)
1155 logfname = os.path.expanduser(logfname)
1155 self.shell.logfile = logfname
1156 self.shell.logfile = logfname
1156 # TODO: we need to re-think how logs with args/opts are replayed
1157 # TODO: we need to re-think how logs with args/opts are replayed
1157 # and tracked.
1158 # and tracked.
1158 # loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1159 # loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1159 loghead = self.shell.loghead_tpl % ('','')
1160 loghead = self.shell.loghead_tpl % ('','')
1160 try:
1161 try:
1161 started = logger.logstart(logfname,loghead,logmode,
1162 started = logger.logstart(logfname,loghead,logmode,
1162 log_output,timestamp,log_raw_input)
1163 log_output,timestamp,log_raw_input)
1163 except:
1164 except:
1164 rc.opts.logfile = old_logfile
1165 rc.opts.logfile = old_logfile
1165 warn("Couldn't start log: %s" % sys.exc_info()[1])
1166 warn("Couldn't start log: %s" % sys.exc_info()[1])
1166 else:
1167 else:
1167 # log input history up to this point, optionally interleaving
1168 # log input history up to this point, optionally interleaving
1168 # output if requested
1169 # output if requested
1169
1170
1170 if timestamp:
1171 if timestamp:
1171 # disable timestamping for the previous history, since we've
1172 # disable timestamping for the previous history, since we've
1172 # lost those already (no time machine here).
1173 # lost those already (no time machine here).
1173 logger.timestamp = False
1174 logger.timestamp = False
1174
1175
1175 if log_raw_input:
1176 if log_raw_input:
1176 input_hist = self.shell.input_hist_raw
1177 input_hist = self.shell.input_hist_raw
1177 else:
1178 else:
1178 input_hist = self.shell.input_hist
1179 input_hist = self.shell.input_hist
1179
1180
1180 if log_output:
1181 if log_output:
1181 log_write = logger.log_write
1182 log_write = logger.log_write
1182 output_hist = self.shell.output_hist
1183 output_hist = self.shell.output_hist
1183 for n in range(1,len(input_hist)-1):
1184 for n in range(1,len(input_hist)-1):
1184 log_write(input_hist[n].rstrip())
1185 log_write(input_hist[n].rstrip())
1185 if n in output_hist:
1186 if n in output_hist:
1186 log_write(repr(output_hist[n]),'output')
1187 log_write(repr(output_hist[n]),'output')
1187 else:
1188 else:
1188 logger.log_write(input_hist[1:])
1189 logger.log_write(input_hist[1:])
1189 if timestamp:
1190 if timestamp:
1190 # re-enable timestamping
1191 # re-enable timestamping
1191 logger.timestamp = True
1192 logger.timestamp = True
1192
1193
1193 print ('Activating auto-logging. '
1194 print ('Activating auto-logging. '
1194 'Current session state plus future input saved.')
1195 'Current session state plus future input saved.')
1195 logger.logstate()
1196 logger.logstate()
1196
1197
1197 def magic_logstop(self,parameter_s=''):
1198 def magic_logstop(self,parameter_s=''):
1198 """Fully stop logging and close log file.
1199 """Fully stop logging and close log file.
1199
1200
1200 In order to start logging again, a new %logstart call needs to be made,
1201 In order to start logging again, a new %logstart call needs to be made,
1201 possibly (though not necessarily) with a new filename, mode and other
1202 possibly (though not necessarily) with a new filename, mode and other
1202 options."""
1203 options."""
1203 self.logger.logstop()
1204 self.logger.logstop()
1204
1205
1205 def magic_logoff(self,parameter_s=''):
1206 def magic_logoff(self,parameter_s=''):
1206 """Temporarily stop logging.
1207 """Temporarily stop logging.
1207
1208
1208 You must have previously started logging."""
1209 You must have previously started logging."""
1209 self.shell.logger.switch_log(0)
1210 self.shell.logger.switch_log(0)
1210
1211
1211 def magic_logon(self,parameter_s=''):
1212 def magic_logon(self,parameter_s=''):
1212 """Restart logging.
1213 """Restart logging.
1213
1214
1214 This function is for restarting logging which you've temporarily
1215 This function is for restarting logging which you've temporarily
1215 stopped with %logoff. For starting logging for the first time, you
1216 stopped with %logoff. For starting logging for the first time, you
1216 must use the %logstart function, which allows you to specify an
1217 must use the %logstart function, which allows you to specify an
1217 optional log filename."""
1218 optional log filename."""
1218
1219
1219 self.shell.logger.switch_log(1)
1220 self.shell.logger.switch_log(1)
1220
1221
1221 def magic_logstate(self,parameter_s=''):
1222 def magic_logstate(self,parameter_s=''):
1222 """Print the status of the logging system."""
1223 """Print the status of the logging system."""
1223
1224
1224 self.shell.logger.logstate()
1225 self.shell.logger.logstate()
1225
1226
1226 def magic_pdb(self, parameter_s=''):
1227 def magic_pdb(self, parameter_s=''):
1227 """Control the automatic calling of the pdb interactive debugger.
1228 """Control the automatic calling of the pdb interactive debugger.
1228
1229
1229 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1230 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1230 argument it works as a toggle.
1231 argument it works as a toggle.
1231
1232
1232 When an exception is triggered, IPython can optionally call the
1233 When an exception is triggered, IPython can optionally call the
1233 interactive pdb debugger after the traceback printout. %pdb toggles
1234 interactive pdb debugger after the traceback printout. %pdb toggles
1234 this feature on and off.
1235 this feature on and off.
1235
1236
1236 The initial state of this feature is set in your ipythonrc
1237 The initial state of this feature is set in your ipythonrc
1237 configuration file (the variable is called 'pdb').
1238 configuration file (the variable is called 'pdb').
1238
1239
1239 If you want to just activate the debugger AFTER an exception has fired,
1240 If you want to just activate the debugger AFTER an exception has fired,
1240 without having to type '%pdb on' and rerunning your code, you can use
1241 without having to type '%pdb on' and rerunning your code, you can use
1241 the %debug magic."""
1242 the %debug magic."""
1242
1243
1243 par = parameter_s.strip().lower()
1244 par = parameter_s.strip().lower()
1244
1245
1245 if par:
1246 if par:
1246 try:
1247 try:
1247 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1248 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1248 except KeyError:
1249 except KeyError:
1249 print ('Incorrect argument. Use on/1, off/0, '
1250 print ('Incorrect argument. Use on/1, off/0, '
1250 'or nothing for a toggle.')
1251 'or nothing for a toggle.')
1251 return
1252 return
1252 else:
1253 else:
1253 # toggle
1254 # toggle
1254 new_pdb = not self.shell.call_pdb
1255 new_pdb = not self.shell.call_pdb
1255
1256
1256 # set on the shell
1257 # set on the shell
1257 self.shell.call_pdb = new_pdb
1258 self.shell.call_pdb = new_pdb
1258 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1259 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1259
1260
1260 def magic_debug(self, parameter_s=''):
1261 def magic_debug(self, parameter_s=''):
1261 """Activate the interactive debugger in post-mortem mode.
1262 """Activate the interactive debugger in post-mortem mode.
1262
1263
1263 If an exception has just occurred, this lets you inspect its stack
1264 If an exception has just occurred, this lets you inspect its stack
1264 frames interactively. Note that this will always work only on the last
1265 frames interactively. Note that this will always work only on the last
1265 traceback that occurred, so you must call this quickly after an
1266 traceback that occurred, so you must call this quickly after an
1266 exception that you wish to inspect has fired, because if another one
1267 exception that you wish to inspect has fired, because if another one
1267 occurs, it clobbers the previous one.
1268 occurs, it clobbers the previous one.
1268
1269
1269 If you want IPython to automatically do this on every exception, see
1270 If you want IPython to automatically do this on every exception, see
1270 the %pdb magic for more details.
1271 the %pdb magic for more details.
1271 """
1272 """
1272
1273
1273 self.shell.debugger(force=True)
1274 self.shell.debugger(force=True)
1274
1275
1275 @testdec.skip_doctest
1276 @testdec.skip_doctest
1276 def magic_prun(self, parameter_s ='',user_mode=1,
1277 def magic_prun(self, parameter_s ='',user_mode=1,
1277 opts=None,arg_lst=None,prog_ns=None):
1278 opts=None,arg_lst=None,prog_ns=None):
1278
1279
1279 """Run a statement through the python code profiler.
1280 """Run a statement through the python code profiler.
1280
1281
1281 Usage:
1282 Usage:
1282 %prun [options] statement
1283 %prun [options] statement
1283
1284
1284 The given statement (which doesn't require quote marks) is run via the
1285 The given statement (which doesn't require quote marks) is run via the
1285 python profiler in a manner similar to the profile.run() function.
1286 python profiler in a manner similar to the profile.run() function.
1286 Namespaces are internally managed to work correctly; profile.run
1287 Namespaces are internally managed to work correctly; profile.run
1287 cannot be used in IPython because it makes certain assumptions about
1288 cannot be used in IPython because it makes certain assumptions about
1288 namespaces which do not hold under IPython.
1289 namespaces which do not hold under IPython.
1289
1290
1290 Options:
1291 Options:
1291
1292
1292 -l <limit>: you can place restrictions on what or how much of the
1293 -l <limit>: you can place restrictions on what or how much of the
1293 profile gets printed. The limit value can be:
1294 profile gets printed. The limit value can be:
1294
1295
1295 * A string: only information for function names containing this string
1296 * A string: only information for function names containing this string
1296 is printed.
1297 is printed.
1297
1298
1298 * An integer: only these many lines are printed.
1299 * An integer: only these many lines are printed.
1299
1300
1300 * A float (between 0 and 1): this fraction of the report is printed
1301 * A float (between 0 and 1): this fraction of the report is printed
1301 (for example, use a limit of 0.4 to see the topmost 40% only).
1302 (for example, use a limit of 0.4 to see the topmost 40% only).
1302
1303
1303 You can combine several limits with repeated use of the option. For
1304 You can combine several limits with repeated use of the option. For
1304 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1305 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1305 information about class constructors.
1306 information about class constructors.
1306
1307
1307 -r: return the pstats.Stats object generated by the profiling. This
1308 -r: return the pstats.Stats object generated by the profiling. This
1308 object has all the information about the profile in it, and you can
1309 object has all the information about the profile in it, and you can
1309 later use it for further analysis or in other functions.
1310 later use it for further analysis or in other functions.
1310
1311
1311 -s <key>: sort profile by given key. You can provide more than one key
1312 -s <key>: sort profile by given key. You can provide more than one key
1312 by using the option several times: '-s key1 -s key2 -s key3...'. The
1313 by using the option several times: '-s key1 -s key2 -s key3...'. The
1313 default sorting key is 'time'.
1314 default sorting key is 'time'.
1314
1315
1315 The following is copied verbatim from the profile documentation
1316 The following is copied verbatim from the profile documentation
1316 referenced below:
1317 referenced below:
1317
1318
1318 When more than one key is provided, additional keys are used as
1319 When more than one key is provided, additional keys are used as
1319 secondary criteria when the there is equality in all keys selected
1320 secondary criteria when the there is equality in all keys selected
1320 before them.
1321 before them.
1321
1322
1322 Abbreviations can be used for any key names, as long as the
1323 Abbreviations can be used for any key names, as long as the
1323 abbreviation is unambiguous. The following are the keys currently
1324 abbreviation is unambiguous. The following are the keys currently
1324 defined:
1325 defined:
1325
1326
1326 Valid Arg Meaning
1327 Valid Arg Meaning
1327 "calls" call count
1328 "calls" call count
1328 "cumulative" cumulative time
1329 "cumulative" cumulative time
1329 "file" file name
1330 "file" file name
1330 "module" file name
1331 "module" file name
1331 "pcalls" primitive call count
1332 "pcalls" primitive call count
1332 "line" line number
1333 "line" line number
1333 "name" function name
1334 "name" function name
1334 "nfl" name/file/line
1335 "nfl" name/file/line
1335 "stdname" standard name
1336 "stdname" standard name
1336 "time" internal time
1337 "time" internal time
1337
1338
1338 Note that all sorts on statistics are in descending order (placing
1339 Note that all sorts on statistics are in descending order (placing
1339 most time consuming items first), where as name, file, and line number
1340 most time consuming items first), where as name, file, and line number
1340 searches are in ascending order (i.e., alphabetical). The subtle
1341 searches are in ascending order (i.e., alphabetical). The subtle
1341 distinction between "nfl" and "stdname" is that the standard name is a
1342 distinction between "nfl" and "stdname" is that the standard name is a
1342 sort of the name as printed, which means that the embedded line
1343 sort of the name as printed, which means that the embedded line
1343 numbers get compared in an odd way. For example, lines 3, 20, and 40
1344 numbers get compared in an odd way. For example, lines 3, 20, and 40
1344 would (if the file names were the same) appear in the string order
1345 would (if the file names were the same) appear in the string order
1345 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1346 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1346 line numbers. In fact, sort_stats("nfl") is the same as
1347 line numbers. In fact, sort_stats("nfl") is the same as
1347 sort_stats("name", "file", "line").
1348 sort_stats("name", "file", "line").
1348
1349
1349 -T <filename>: save profile results as shown on screen to a text
1350 -T <filename>: save profile results as shown on screen to a text
1350 file. The profile is still shown on screen.
1351 file. The profile is still shown on screen.
1351
1352
1352 -D <filename>: save (via dump_stats) profile statistics to given
1353 -D <filename>: save (via dump_stats) profile statistics to given
1353 filename. This data is in a format understod by the pstats module, and
1354 filename. This data is in a format understod by the pstats module, and
1354 is generated by a call to the dump_stats() method of profile
1355 is generated by a call to the dump_stats() method of profile
1355 objects. The profile is still shown on screen.
1356 objects. The profile is still shown on screen.
1356
1357
1357 If you want to run complete programs under the profiler's control, use
1358 If you want to run complete programs under the profiler's control, use
1358 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1359 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1359 contains profiler specific options as described here.
1360 contains profiler specific options as described here.
1360
1361
1361 You can read the complete documentation for the profile module with::
1362 You can read the complete documentation for the profile module with::
1362
1363
1363 In [1]: import profile; profile.help()
1364 In [1]: import profile; profile.help()
1364 """
1365 """
1365
1366
1366 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1367 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1367 # protect user quote marks
1368 # protect user quote marks
1368 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1369 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1369
1370
1370 if user_mode: # regular user call
1371 if user_mode: # regular user call
1371 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1372 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1372 list_all=1)
1373 list_all=1)
1373 namespace = self.shell.user_ns
1374 namespace = self.shell.user_ns
1374 else: # called to run a program by %run -p
1375 else: # called to run a program by %run -p
1375 try:
1376 try:
1376 filename = get_py_filename(arg_lst[0])
1377 filename = get_py_filename(arg_lst[0])
1377 except IOError,msg:
1378 except IOError,msg:
1378 error(msg)
1379 error(msg)
1379 return
1380 return
1380
1381
1381 arg_str = 'execfile(filename,prog_ns)'
1382 arg_str = 'execfile(filename,prog_ns)'
1382 namespace = locals()
1383 namespace = locals()
1383
1384
1384 opts.merge(opts_def)
1385 opts.merge(opts_def)
1385
1386
1386 prof = profile.Profile()
1387 prof = profile.Profile()
1387 try:
1388 try:
1388 prof = prof.runctx(arg_str,namespace,namespace)
1389 prof = prof.runctx(arg_str,namespace,namespace)
1389 sys_exit = ''
1390 sys_exit = ''
1390 except SystemExit:
1391 except SystemExit:
1391 sys_exit = """*** SystemExit exception caught in code being profiled."""
1392 sys_exit = """*** SystemExit exception caught in code being profiled."""
1392
1393
1393 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1394 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1394
1395
1395 lims = opts.l
1396 lims = opts.l
1396 if lims:
1397 if lims:
1397 lims = [] # rebuild lims with ints/floats/strings
1398 lims = [] # rebuild lims with ints/floats/strings
1398 for lim in opts.l:
1399 for lim in opts.l:
1399 try:
1400 try:
1400 lims.append(int(lim))
1401 lims.append(int(lim))
1401 except ValueError:
1402 except ValueError:
1402 try:
1403 try:
1403 lims.append(float(lim))
1404 lims.append(float(lim))
1404 except ValueError:
1405 except ValueError:
1405 lims.append(lim)
1406 lims.append(lim)
1406
1407
1407 # Trap output.
1408 # Trap output.
1408 stdout_trap = StringIO()
1409 stdout_trap = StringIO()
1409
1410
1410 if hasattr(stats,'stream'):
1411 if hasattr(stats,'stream'):
1411 # In newer versions of python, the stats object has a 'stream'
1412 # In newer versions of python, the stats object has a 'stream'
1412 # attribute to write into.
1413 # attribute to write into.
1413 stats.stream = stdout_trap
1414 stats.stream = stdout_trap
1414 stats.print_stats(*lims)
1415 stats.print_stats(*lims)
1415 else:
1416 else:
1416 # For older versions, we manually redirect stdout during printing
1417 # For older versions, we manually redirect stdout during printing
1417 sys_stdout = sys.stdout
1418 sys_stdout = sys.stdout
1418 try:
1419 try:
1419 sys.stdout = stdout_trap
1420 sys.stdout = stdout_trap
1420 stats.print_stats(*lims)
1421 stats.print_stats(*lims)
1421 finally:
1422 finally:
1422 sys.stdout = sys_stdout
1423 sys.stdout = sys_stdout
1423
1424
1424 output = stdout_trap.getvalue()
1425 output = stdout_trap.getvalue()
1425 output = output.rstrip()
1426 output = output.rstrip()
1426
1427
1427 page(output,screen_lines=self.shell.usable_screen_length)
1428 page(output,screen_lines=self.shell.usable_screen_length)
1428 print sys_exit,
1429 print sys_exit,
1429
1430
1430 dump_file = opts.D[0]
1431 dump_file = opts.D[0]
1431 text_file = opts.T[0]
1432 text_file = opts.T[0]
1432 if dump_file:
1433 if dump_file:
1433 prof.dump_stats(dump_file)
1434 prof.dump_stats(dump_file)
1434 print '\n*** Profile stats marshalled to file',\
1435 print '\n*** Profile stats marshalled to file',\
1435 `dump_file`+'.',sys_exit
1436 `dump_file`+'.',sys_exit
1436 if text_file:
1437 if text_file:
1437 pfile = file(text_file,'w')
1438 pfile = file(text_file,'w')
1438 pfile.write(output)
1439 pfile.write(output)
1439 pfile.close()
1440 pfile.close()
1440 print '\n*** Profile printout saved to text file',\
1441 print '\n*** Profile printout saved to text file',\
1441 `text_file`+'.',sys_exit
1442 `text_file`+'.',sys_exit
1442
1443
1443 if opts.has_key('r'):
1444 if opts.has_key('r'):
1444 return stats
1445 return stats
1445 else:
1446 else:
1446 return None
1447 return None
1447
1448
1448 @testdec.skip_doctest
1449 @testdec.skip_doctest
1449 def magic_run(self, parameter_s ='',runner=None,
1450 def magic_run(self, parameter_s ='',runner=None,
1450 file_finder=get_py_filename):
1451 file_finder=get_py_filename):
1451 """Run the named file inside IPython as a program.
1452 """Run the named file inside IPython as a program.
1452
1453
1453 Usage:\\
1454 Usage:\\
1454 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1455 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1455
1456
1456 Parameters after the filename are passed as command-line arguments to
1457 Parameters after the filename are passed as command-line arguments to
1457 the program (put in sys.argv). Then, control returns to IPython's
1458 the program (put in sys.argv). Then, control returns to IPython's
1458 prompt.
1459 prompt.
1459
1460
1460 This is similar to running at a system prompt:\\
1461 This is similar to running at a system prompt:\\
1461 $ python file args\\
1462 $ python file args\\
1462 but with the advantage of giving you IPython's tracebacks, and of
1463 but with the advantage of giving you IPython's tracebacks, and of
1463 loading all variables into your interactive namespace for further use
1464 loading all variables into your interactive namespace for further use
1464 (unless -p is used, see below).
1465 (unless -p is used, see below).
1465
1466
1466 The file is executed in a namespace initially consisting only of
1467 The file is executed in a namespace initially consisting only of
1467 __name__=='__main__' and sys.argv constructed as indicated. It thus
1468 __name__=='__main__' and sys.argv constructed as indicated. It thus
1468 sees its environment as if it were being run as a stand-alone program
1469 sees its environment as if it were being run as a stand-alone program
1469 (except for sharing global objects such as previously imported
1470 (except for sharing global objects such as previously imported
1470 modules). But after execution, the IPython interactive namespace gets
1471 modules). But after execution, the IPython interactive namespace gets
1471 updated with all variables defined in the program (except for __name__
1472 updated with all variables defined in the program (except for __name__
1472 and sys.argv). This allows for very convenient loading of code for
1473 and sys.argv). This allows for very convenient loading of code for
1473 interactive work, while giving each program a 'clean sheet' to run in.
1474 interactive work, while giving each program a 'clean sheet' to run in.
1474
1475
1475 Options:
1476 Options:
1476
1477
1477 -n: __name__ is NOT set to '__main__', but to the running file's name
1478 -n: __name__ is NOT set to '__main__', but to the running file's name
1478 without extension (as python does under import). This allows running
1479 without extension (as python does under import). This allows running
1479 scripts and reloading the definitions in them without calling code
1480 scripts and reloading the definitions in them without calling code
1480 protected by an ' if __name__ == "__main__" ' clause.
1481 protected by an ' if __name__ == "__main__" ' clause.
1481
1482
1482 -i: run the file in IPython's namespace instead of an empty one. This
1483 -i: run the file in IPython's namespace instead of an empty one. This
1483 is useful if you are experimenting with code written in a text editor
1484 is useful if you are experimenting with code written in a text editor
1484 which depends on variables defined interactively.
1485 which depends on variables defined interactively.
1485
1486
1486 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1487 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1487 being run. This is particularly useful if IPython is being used to
1488 being run. This is particularly useful if IPython is being used to
1488 run unittests, which always exit with a sys.exit() call. In such
1489 run unittests, which always exit with a sys.exit() call. In such
1489 cases you are interested in the output of the test results, not in
1490 cases you are interested in the output of the test results, not in
1490 seeing a traceback of the unittest module.
1491 seeing a traceback of the unittest module.
1491
1492
1492 -t: print timing information at the end of the run. IPython will give
1493 -t: print timing information at the end of the run. IPython will give
1493 you an estimated CPU time consumption for your script, which under
1494 you an estimated CPU time consumption for your script, which under
1494 Unix uses the resource module to avoid the wraparound problems of
1495 Unix uses the resource module to avoid the wraparound problems of
1495 time.clock(). Under Unix, an estimate of time spent on system tasks
1496 time.clock(). Under Unix, an estimate of time spent on system tasks
1496 is also given (for Windows platforms this is reported as 0.0).
1497 is also given (for Windows platforms this is reported as 0.0).
1497
1498
1498 If -t is given, an additional -N<N> option can be given, where <N>
1499 If -t is given, an additional -N<N> option can be given, where <N>
1499 must be an integer indicating how many times you want the script to
1500 must be an integer indicating how many times you want the script to
1500 run. The final timing report will include total and per run results.
1501 run. The final timing report will include total and per run results.
1501
1502
1502 For example (testing the script uniq_stable.py):
1503 For example (testing the script uniq_stable.py):
1503
1504
1504 In [1]: run -t uniq_stable
1505 In [1]: run -t uniq_stable
1505
1506
1506 IPython CPU timings (estimated):\\
1507 IPython CPU timings (estimated):\\
1507 User : 0.19597 s.\\
1508 User : 0.19597 s.\\
1508 System: 0.0 s.\\
1509 System: 0.0 s.\\
1509
1510
1510 In [2]: run -t -N5 uniq_stable
1511 In [2]: run -t -N5 uniq_stable
1511
1512
1512 IPython CPU timings (estimated):\\
1513 IPython CPU timings (estimated):\\
1513 Total runs performed: 5\\
1514 Total runs performed: 5\\
1514 Times : Total Per run\\
1515 Times : Total Per run\\
1515 User : 0.910862 s, 0.1821724 s.\\
1516 User : 0.910862 s, 0.1821724 s.\\
1516 System: 0.0 s, 0.0 s.
1517 System: 0.0 s, 0.0 s.
1517
1518
1518 -d: run your program under the control of pdb, the Python debugger.
1519 -d: run your program under the control of pdb, the Python debugger.
1519 This allows you to execute your program step by step, watch variables,
1520 This allows you to execute your program step by step, watch variables,
1520 etc. Internally, what IPython does is similar to calling:
1521 etc. Internally, what IPython does is similar to calling:
1521
1522
1522 pdb.run('execfile("YOURFILENAME")')
1523 pdb.run('execfile("YOURFILENAME")')
1523
1524
1524 with a breakpoint set on line 1 of your file. You can change the line
1525 with a breakpoint set on line 1 of your file. You can change the line
1525 number for this automatic breakpoint to be <N> by using the -bN option
1526 number for this automatic breakpoint to be <N> by using the -bN option
1526 (where N must be an integer). For example:
1527 (where N must be an integer). For example:
1527
1528
1528 %run -d -b40 myscript
1529 %run -d -b40 myscript
1529
1530
1530 will set the first breakpoint at line 40 in myscript.py. Note that
1531 will set the first breakpoint at line 40 in myscript.py. Note that
1531 the first breakpoint must be set on a line which actually does
1532 the first breakpoint must be set on a line which actually does
1532 something (not a comment or docstring) for it to stop execution.
1533 something (not a comment or docstring) for it to stop execution.
1533
1534
1534 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1535 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1535 first enter 'c' (without qoutes) to start execution up to the first
1536 first enter 'c' (without qoutes) to start execution up to the first
1536 breakpoint.
1537 breakpoint.
1537
1538
1538 Entering 'help' gives information about the use of the debugger. You
1539 Entering 'help' gives information about the use of the debugger. You
1539 can easily see pdb's full documentation with "import pdb;pdb.help()"
1540 can easily see pdb's full documentation with "import pdb;pdb.help()"
1540 at a prompt.
1541 at a prompt.
1541
1542
1542 -p: run program under the control of the Python profiler module (which
1543 -p: run program under the control of the Python profiler module (which
1543 prints a detailed report of execution times, function calls, etc).
1544 prints a detailed report of execution times, function calls, etc).
1544
1545
1545 You can pass other options after -p which affect the behavior of the
1546 You can pass other options after -p which affect the behavior of the
1546 profiler itself. See the docs for %prun for details.
1547 profiler itself. See the docs for %prun for details.
1547
1548
1548 In this mode, the program's variables do NOT propagate back to the
1549 In this mode, the program's variables do NOT propagate back to the
1549 IPython interactive namespace (because they remain in the namespace
1550 IPython interactive namespace (because they remain in the namespace
1550 where the profiler executes them).
1551 where the profiler executes them).
1551
1552
1552 Internally this triggers a call to %prun, see its documentation for
1553 Internally this triggers a call to %prun, see its documentation for
1553 details on the options available specifically for profiling.
1554 details on the options available specifically for profiling.
1554
1555
1555 There is one special usage for which the text above doesn't apply:
1556 There is one special usage for which the text above doesn't apply:
1556 if the filename ends with .ipy, the file is run as ipython script,
1557 if the filename ends with .ipy, the file is run as ipython script,
1557 just as if the commands were written on IPython prompt.
1558 just as if the commands were written on IPython prompt.
1558 """
1559 """
1559
1560
1560 # get arguments and set sys.argv for program to be run.
1561 # get arguments and set sys.argv for program to be run.
1561 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1562 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1562 mode='list',list_all=1)
1563 mode='list',list_all=1)
1563
1564
1564 try:
1565 try:
1565 filename = file_finder(arg_lst[0])
1566 filename = file_finder(arg_lst[0])
1566 except IndexError:
1567 except IndexError:
1567 warn('you must provide at least a filename.')
1568 warn('you must provide at least a filename.')
1568 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1569 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1569 return
1570 return
1570 except IOError,msg:
1571 except IOError,msg:
1571 error(msg)
1572 error(msg)
1572 return
1573 return
1573
1574
1574 if filename.lower().endswith('.ipy'):
1575 if filename.lower().endswith('.ipy'):
1575 self.runlines(open(filename).read(), clean=True)
1576 self.runlines(open(filename).read(), clean=True)
1576 return
1577 return
1577
1578
1578 # Control the response to exit() calls made by the script being run
1579 # Control the response to exit() calls made by the script being run
1579 exit_ignore = opts.has_key('e')
1580 exit_ignore = opts.has_key('e')
1580
1581
1581 # Make sure that the running script gets a proper sys.argv as if it
1582 # Make sure that the running script gets a proper sys.argv as if it
1582 # were run from a system shell.
1583 # were run from a system shell.
1583 save_argv = sys.argv # save it for later restoring
1584 save_argv = sys.argv # save it for later restoring
1584 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1585 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1585
1586
1586 if opts.has_key('i'):
1587 if opts.has_key('i'):
1587 # Run in user's interactive namespace
1588 # Run in user's interactive namespace
1588 prog_ns = self.shell.user_ns
1589 prog_ns = self.shell.user_ns
1589 __name__save = self.shell.user_ns['__name__']
1590 __name__save = self.shell.user_ns['__name__']
1590 prog_ns['__name__'] = '__main__'
1591 prog_ns['__name__'] = '__main__'
1591 main_mod = self.shell.new_main_mod(prog_ns)
1592 main_mod = self.shell.new_main_mod(prog_ns)
1592 else:
1593 else:
1593 # Run in a fresh, empty namespace
1594 # Run in a fresh, empty namespace
1594 if opts.has_key('n'):
1595 if opts.has_key('n'):
1595 name = os.path.splitext(os.path.basename(filename))[0]
1596 name = os.path.splitext(os.path.basename(filename))[0]
1596 else:
1597 else:
1597 name = '__main__'
1598 name = '__main__'
1598
1599
1599 main_mod = self.shell.new_main_mod()
1600 main_mod = self.shell.new_main_mod()
1600 prog_ns = main_mod.__dict__
1601 prog_ns = main_mod.__dict__
1601 prog_ns['__name__'] = name
1602 prog_ns['__name__'] = name
1602
1603
1603 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1604 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1604 # set the __file__ global in the script's namespace
1605 # set the __file__ global in the script's namespace
1605 prog_ns['__file__'] = filename
1606 prog_ns['__file__'] = filename
1606
1607
1607 # pickle fix. See iplib for an explanation. But we need to make sure
1608 # pickle fix. See iplib for an explanation. But we need to make sure
1608 # that, if we overwrite __main__, we replace it at the end
1609 # that, if we overwrite __main__, we replace it at the end
1609 main_mod_name = prog_ns['__name__']
1610 main_mod_name = prog_ns['__name__']
1610
1611
1611 if main_mod_name == '__main__':
1612 if main_mod_name == '__main__':
1612 restore_main = sys.modules['__main__']
1613 restore_main = sys.modules['__main__']
1613 else:
1614 else:
1614 restore_main = False
1615 restore_main = False
1615
1616
1616 # This needs to be undone at the end to prevent holding references to
1617 # This needs to be undone at the end to prevent holding references to
1617 # every single object ever created.
1618 # every single object ever created.
1618 sys.modules[main_mod_name] = main_mod
1619 sys.modules[main_mod_name] = main_mod
1619
1620
1620 stats = None
1621 stats = None
1621 try:
1622 try:
1622 self.shell.savehist()
1623 self.shell.savehist()
1623
1624
1624 if opts.has_key('p'):
1625 if opts.has_key('p'):
1625 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1626 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1626 else:
1627 else:
1627 if opts.has_key('d'):
1628 if opts.has_key('d'):
1628 deb = debugger.Pdb(self.shell.colors)
1629 deb = debugger.Pdb(self.shell.colors)
1629 # reset Breakpoint state, which is moronically kept
1630 # reset Breakpoint state, which is moronically kept
1630 # in a class
1631 # in a class
1631 bdb.Breakpoint.next = 1
1632 bdb.Breakpoint.next = 1
1632 bdb.Breakpoint.bplist = {}
1633 bdb.Breakpoint.bplist = {}
1633 bdb.Breakpoint.bpbynumber = [None]
1634 bdb.Breakpoint.bpbynumber = [None]
1634 # Set an initial breakpoint to stop execution
1635 # Set an initial breakpoint to stop execution
1635 maxtries = 10
1636 maxtries = 10
1636 bp = int(opts.get('b',[1])[0])
1637 bp = int(opts.get('b',[1])[0])
1637 checkline = deb.checkline(filename,bp)
1638 checkline = deb.checkline(filename,bp)
1638 if not checkline:
1639 if not checkline:
1639 for bp in range(bp+1,bp+maxtries+1):
1640 for bp in range(bp+1,bp+maxtries+1):
1640 if deb.checkline(filename,bp):
1641 if deb.checkline(filename,bp):
1641 break
1642 break
1642 else:
1643 else:
1643 msg = ("\nI failed to find a valid line to set "
1644 msg = ("\nI failed to find a valid line to set "
1644 "a breakpoint\n"
1645 "a breakpoint\n"
1645 "after trying up to line: %s.\n"
1646 "after trying up to line: %s.\n"
1646 "Please set a valid breakpoint manually "
1647 "Please set a valid breakpoint manually "
1647 "with the -b option." % bp)
1648 "with the -b option." % bp)
1648 error(msg)
1649 error(msg)
1649 return
1650 return
1650 # if we find a good linenumber, set the breakpoint
1651 # if we find a good linenumber, set the breakpoint
1651 deb.do_break('%s:%s' % (filename,bp))
1652 deb.do_break('%s:%s' % (filename,bp))
1652 # Start file run
1653 # Start file run
1653 print "NOTE: Enter 'c' at the",
1654 print "NOTE: Enter 'c' at the",
1654 print "%s prompt to start your script." % deb.prompt
1655 print "%s prompt to start your script." % deb.prompt
1655 try:
1656 try:
1656 deb.run('execfile("%s")' % filename,prog_ns)
1657 deb.run('execfile("%s")' % filename,prog_ns)
1657
1658
1658 except:
1659 except:
1659 etype, value, tb = sys.exc_info()
1660 etype, value, tb = sys.exc_info()
1660 # Skip three frames in the traceback: the %run one,
1661 # Skip three frames in the traceback: the %run one,
1661 # one inside bdb.py, and the command-line typed by the
1662 # one inside bdb.py, and the command-line typed by the
1662 # user (run by exec in pdb itself).
1663 # user (run by exec in pdb itself).
1663 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1664 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1664 else:
1665 else:
1665 if runner is None:
1666 if runner is None:
1666 runner = self.shell.safe_execfile
1667 runner = self.shell.safe_execfile
1667 if opts.has_key('t'):
1668 if opts.has_key('t'):
1668 # timed execution
1669 # timed execution
1669 try:
1670 try:
1670 nruns = int(opts['N'][0])
1671 nruns = int(opts['N'][0])
1671 if nruns < 1:
1672 if nruns < 1:
1672 error('Number of runs must be >=1')
1673 error('Number of runs must be >=1')
1673 return
1674 return
1674 except (KeyError):
1675 except (KeyError):
1675 nruns = 1
1676 nruns = 1
1676 if nruns == 1:
1677 if nruns == 1:
1677 t0 = clock2()
1678 t0 = clock2()
1678 runner(filename,prog_ns,prog_ns,
1679 runner(filename,prog_ns,prog_ns,
1679 exit_ignore=exit_ignore)
1680 exit_ignore=exit_ignore)
1680 t1 = clock2()
1681 t1 = clock2()
1681 t_usr = t1[0]-t0[0]
1682 t_usr = t1[0]-t0[0]
1682 t_sys = t1[1]-t0[1]
1683 t_sys = t1[1]-t0[1]
1683 print "\nIPython CPU timings (estimated):"
1684 print "\nIPython CPU timings (estimated):"
1684 print " User : %10s s." % t_usr
1685 print " User : %10s s." % t_usr
1685 print " System: %10s s." % t_sys
1686 print " System: %10s s." % t_sys
1686 else:
1687 else:
1687 runs = range(nruns)
1688 runs = range(nruns)
1688 t0 = clock2()
1689 t0 = clock2()
1689 for nr in runs:
1690 for nr in runs:
1690 runner(filename,prog_ns,prog_ns,
1691 runner(filename,prog_ns,prog_ns,
1691 exit_ignore=exit_ignore)
1692 exit_ignore=exit_ignore)
1692 t1 = clock2()
1693 t1 = clock2()
1693 t_usr = t1[0]-t0[0]
1694 t_usr = t1[0]-t0[0]
1694 t_sys = t1[1]-t0[1]
1695 t_sys = t1[1]-t0[1]
1695 print "\nIPython CPU timings (estimated):"
1696 print "\nIPython CPU timings (estimated):"
1696 print "Total runs performed:",nruns
1697 print "Total runs performed:",nruns
1697 print " Times : %10s %10s" % ('Total','Per run')
1698 print " Times : %10s %10s" % ('Total','Per run')
1698 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1699 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1699 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1700 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1700
1701
1701 else:
1702 else:
1702 # regular execution
1703 # regular execution
1703 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1704 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1704
1705
1705 if opts.has_key('i'):
1706 if opts.has_key('i'):
1706 self.shell.user_ns['__name__'] = __name__save
1707 self.shell.user_ns['__name__'] = __name__save
1707 else:
1708 else:
1708 # The shell MUST hold a reference to prog_ns so after %run
1709 # The shell MUST hold a reference to prog_ns so after %run
1709 # exits, the python deletion mechanism doesn't zero it out
1710 # exits, the python deletion mechanism doesn't zero it out
1710 # (leaving dangling references).
1711 # (leaving dangling references).
1711 self.shell.cache_main_mod(prog_ns,filename)
1712 self.shell.cache_main_mod(prog_ns,filename)
1712 # update IPython interactive namespace
1713 # update IPython interactive namespace
1713
1714
1714 # Some forms of read errors on the file may mean the
1715 # Some forms of read errors on the file may mean the
1715 # __name__ key was never set; using pop we don't have to
1716 # __name__ key was never set; using pop we don't have to
1716 # worry about a possible KeyError.
1717 # worry about a possible KeyError.
1717 prog_ns.pop('__name__', None)
1718 prog_ns.pop('__name__', None)
1718
1719
1719 self.shell.user_ns.update(prog_ns)
1720 self.shell.user_ns.update(prog_ns)
1720 finally:
1721 finally:
1721 # It's a bit of a mystery why, but __builtins__ can change from
1722 # It's a bit of a mystery why, but __builtins__ can change from
1722 # being a module to becoming a dict missing some key data after
1723 # being a module to becoming a dict missing some key data after
1723 # %run. As best I can see, this is NOT something IPython is doing
1724 # %run. As best I can see, this is NOT something IPython is doing
1724 # at all, and similar problems have been reported before:
1725 # at all, and similar problems have been reported before:
1725 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1726 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1726 # Since this seems to be done by the interpreter itself, the best
1727 # Since this seems to be done by the interpreter itself, the best
1727 # we can do is to at least restore __builtins__ for the user on
1728 # we can do is to at least restore __builtins__ for the user on
1728 # exit.
1729 # exit.
1729 self.shell.user_ns['__builtins__'] = __builtin__
1730 self.shell.user_ns['__builtins__'] = __builtin__
1730
1731
1731 # Ensure key global structures are restored
1732 # Ensure key global structures are restored
1732 sys.argv = save_argv
1733 sys.argv = save_argv
1733 if restore_main:
1734 if restore_main:
1734 sys.modules['__main__'] = restore_main
1735 sys.modules['__main__'] = restore_main
1735 else:
1736 else:
1736 # Remove from sys.modules the reference to main_mod we'd
1737 # Remove from sys.modules the reference to main_mod we'd
1737 # added. Otherwise it will trap references to objects
1738 # added. Otherwise it will trap references to objects
1738 # contained therein.
1739 # contained therein.
1739 del sys.modules[main_mod_name]
1740 del sys.modules[main_mod_name]
1740
1741
1741 self.shell.reloadhist()
1742 self.shell.reloadhist()
1742
1743
1743 return stats
1744 return stats
1744
1745
1745 def magic_runlog(self, parameter_s =''):
1746 def magic_runlog(self, parameter_s =''):
1746 """Run files as logs.
1747 """Run files as logs.
1747
1748
1748 Usage:\\
1749 Usage:\\
1749 %runlog file1 file2 ...
1750 %runlog file1 file2 ...
1750
1751
1751 Run the named files (treating them as log files) in sequence inside
1752 Run the named files (treating them as log files) in sequence inside
1752 the interpreter, and return to the prompt. This is much slower than
1753 the interpreter, and return to the prompt. This is much slower than
1753 %run because each line is executed in a try/except block, but it
1754 %run because each line is executed in a try/except block, but it
1754 allows running files with syntax errors in them.
1755 allows running files with syntax errors in them.
1755
1756
1756 Normally IPython will guess when a file is one of its own logfiles, so
1757 Normally IPython will guess when a file is one of its own logfiles, so
1757 you can typically use %run even for logs. This shorthand allows you to
1758 you can typically use %run even for logs. This shorthand allows you to
1758 force any file to be treated as a log file."""
1759 force any file to be treated as a log file."""
1759
1760
1760 for f in parameter_s.split():
1761 for f in parameter_s.split():
1761 self.shell.safe_execfile(f,self.shell.user_ns,
1762 self.shell.safe_execfile(f,self.shell.user_ns,
1762 self.shell.user_ns,islog=1)
1763 self.shell.user_ns,islog=1)
1763
1764
1764 @testdec.skip_doctest
1765 @testdec.skip_doctest
1765 def magic_timeit(self, parameter_s =''):
1766 def magic_timeit(self, parameter_s =''):
1766 """Time execution of a Python statement or expression
1767 """Time execution of a Python statement or expression
1767
1768
1768 Usage:\\
1769 Usage:\\
1769 %timeit [-n<N> -r<R> [-t|-c]] statement
1770 %timeit [-n<N> -r<R> [-t|-c]] statement
1770
1771
1771 Time execution of a Python statement or expression using the timeit
1772 Time execution of a Python statement or expression using the timeit
1772 module.
1773 module.
1773
1774
1774 Options:
1775 Options:
1775 -n<N>: execute the given statement <N> times in a loop. If this value
1776 -n<N>: execute the given statement <N> times in a loop. If this value
1776 is not given, a fitting value is chosen.
1777 is not given, a fitting value is chosen.
1777
1778
1778 -r<R>: repeat the loop iteration <R> times and take the best result.
1779 -r<R>: repeat the loop iteration <R> times and take the best result.
1779 Default: 3
1780 Default: 3
1780
1781
1781 -t: use time.time to measure the time, which is the default on Unix.
1782 -t: use time.time to measure the time, which is the default on Unix.
1782 This function measures wall time.
1783 This function measures wall time.
1783
1784
1784 -c: use time.clock to measure the time, which is the default on
1785 -c: use time.clock to measure the time, which is the default on
1785 Windows and measures wall time. On Unix, resource.getrusage is used
1786 Windows and measures wall time. On Unix, resource.getrusage is used
1786 instead and returns the CPU user time.
1787 instead and returns the CPU user time.
1787
1788
1788 -p<P>: use a precision of <P> digits to display the timing result.
1789 -p<P>: use a precision of <P> digits to display the timing result.
1789 Default: 3
1790 Default: 3
1790
1791
1791
1792
1792 Examples:
1793 Examples:
1793
1794
1794 In [1]: %timeit pass
1795 In [1]: %timeit pass
1795 10000000 loops, best of 3: 53.3 ns per loop
1796 10000000 loops, best of 3: 53.3 ns per loop
1796
1797
1797 In [2]: u = None
1798 In [2]: u = None
1798
1799
1799 In [3]: %timeit u is None
1800 In [3]: %timeit u is None
1800 10000000 loops, best of 3: 184 ns per loop
1801 10000000 loops, best of 3: 184 ns per loop
1801
1802
1802 In [4]: %timeit -r 4 u == None
1803 In [4]: %timeit -r 4 u == None
1803 1000000 loops, best of 4: 242 ns per loop
1804 1000000 loops, best of 4: 242 ns per loop
1804
1805
1805 In [5]: import time
1806 In [5]: import time
1806
1807
1807 In [6]: %timeit -n1 time.sleep(2)
1808 In [6]: %timeit -n1 time.sleep(2)
1808 1 loops, best of 3: 2 s per loop
1809 1 loops, best of 3: 2 s per loop
1809
1810
1810
1811
1811 The times reported by %timeit will be slightly higher than those
1812 The times reported by %timeit will be slightly higher than those
1812 reported by the timeit.py script when variables are accessed. This is
1813 reported by the timeit.py script when variables are accessed. This is
1813 due to the fact that %timeit executes the statement in the namespace
1814 due to the fact that %timeit executes the statement in the namespace
1814 of the shell, compared with timeit.py, which uses a single setup
1815 of the shell, compared with timeit.py, which uses a single setup
1815 statement to import function or create variables. Generally, the bias
1816 statement to import function or create variables. Generally, the bias
1816 does not matter as long as results from timeit.py are not mixed with
1817 does not matter as long as results from timeit.py are not mixed with
1817 those from %timeit."""
1818 those from %timeit."""
1818
1819
1819 import timeit
1820 import timeit
1820 import math
1821 import math
1821
1822
1822 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1823 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1823 # certain terminals. Until we figure out a robust way of
1824 # certain terminals. Until we figure out a robust way of
1824 # auto-detecting if the terminal can deal with it, use plain 'us' for
1825 # auto-detecting if the terminal can deal with it, use plain 'us' for
1825 # microseconds. I am really NOT happy about disabling the proper
1826 # microseconds. I am really NOT happy about disabling the proper
1826 # 'micro' prefix, but crashing is worse... If anyone knows what the
1827 # 'micro' prefix, but crashing is worse... If anyone knows what the
1827 # right solution for this is, I'm all ears...
1828 # right solution for this is, I'm all ears...
1828 #
1829 #
1829 # Note: using
1830 # Note: using
1830 #
1831 #
1831 # s = u'\xb5'
1832 # s = u'\xb5'
1832 # s.encode(sys.getdefaultencoding())
1833 # s.encode(sys.getdefaultencoding())
1833 #
1834 #
1834 # is not sufficient, as I've seen terminals where that fails but
1835 # is not sufficient, as I've seen terminals where that fails but
1835 # print s
1836 # print s
1836 #
1837 #
1837 # succeeds
1838 # succeeds
1838 #
1839 #
1839 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1840 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1840
1841
1841 #units = [u"s", u"ms",u'\xb5',"ns"]
1842 #units = [u"s", u"ms",u'\xb5',"ns"]
1842 units = [u"s", u"ms",u'us',"ns"]
1843 units = [u"s", u"ms",u'us',"ns"]
1843
1844
1844 scaling = [1, 1e3, 1e6, 1e9]
1845 scaling = [1, 1e3, 1e6, 1e9]
1845
1846
1846 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1847 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1847 posix=False)
1848 posix=False)
1848 if stmt == "":
1849 if stmt == "":
1849 return
1850 return
1850 timefunc = timeit.default_timer
1851 timefunc = timeit.default_timer
1851 number = int(getattr(opts, "n", 0))
1852 number = int(getattr(opts, "n", 0))
1852 repeat = int(getattr(opts, "r", timeit.default_repeat))
1853 repeat = int(getattr(opts, "r", timeit.default_repeat))
1853 precision = int(getattr(opts, "p", 3))
1854 precision = int(getattr(opts, "p", 3))
1854 if hasattr(opts, "t"):
1855 if hasattr(opts, "t"):
1855 timefunc = time.time
1856 timefunc = time.time
1856 if hasattr(opts, "c"):
1857 if hasattr(opts, "c"):
1857 timefunc = clock
1858 timefunc = clock
1858
1859
1859 timer = timeit.Timer(timer=timefunc)
1860 timer = timeit.Timer(timer=timefunc)
1860 # this code has tight coupling to the inner workings of timeit.Timer,
1861 # this code has tight coupling to the inner workings of timeit.Timer,
1861 # but is there a better way to achieve that the code stmt has access
1862 # but is there a better way to achieve that the code stmt has access
1862 # to the shell namespace?
1863 # to the shell namespace?
1863
1864
1864 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1865 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1865 'setup': "pass"}
1866 'setup': "pass"}
1866 # Track compilation time so it can be reported if too long
1867 # Track compilation time so it can be reported if too long
1867 # Minimum time above which compilation time will be reported
1868 # Minimum time above which compilation time will be reported
1868 tc_min = 0.1
1869 tc_min = 0.1
1869
1870
1870 t0 = clock()
1871 t0 = clock()
1871 code = compile(src, "<magic-timeit>", "exec")
1872 code = compile(src, "<magic-timeit>", "exec")
1872 tc = clock()-t0
1873 tc = clock()-t0
1873
1874
1874 ns = {}
1875 ns = {}
1875 exec code in self.shell.user_ns, ns
1876 exec code in self.shell.user_ns, ns
1876 timer.inner = ns["inner"]
1877 timer.inner = ns["inner"]
1877
1878
1878 if number == 0:
1879 if number == 0:
1879 # determine number so that 0.2 <= total time < 2.0
1880 # determine number so that 0.2 <= total time < 2.0
1880 number = 1
1881 number = 1
1881 for i in range(1, 10):
1882 for i in range(1, 10):
1882 if timer.timeit(number) >= 0.2:
1883 if timer.timeit(number) >= 0.2:
1883 break
1884 break
1884 number *= 10
1885 number *= 10
1885
1886
1886 best = min(timer.repeat(repeat, number)) / number
1887 best = min(timer.repeat(repeat, number)) / number
1887
1888
1888 if best > 0.0:
1889 if best > 0.0:
1889 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1890 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1890 else:
1891 else:
1891 order = 3
1892 order = 3
1892 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1893 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1893 precision,
1894 precision,
1894 best * scaling[order],
1895 best * scaling[order],
1895 units[order])
1896 units[order])
1896 if tc > tc_min:
1897 if tc > tc_min:
1897 print "Compiler time: %.2f s" % tc
1898 print "Compiler time: %.2f s" % tc
1898
1899
1899 @testdec.skip_doctest
1900 @testdec.skip_doctest
1900 def magic_time(self,parameter_s = ''):
1901 def magic_time(self,parameter_s = ''):
1901 """Time execution of a Python statement or expression.
1902 """Time execution of a Python statement or expression.
1902
1903
1903 The CPU and wall clock times are printed, and the value of the
1904 The CPU and wall clock times are printed, and the value of the
1904 expression (if any) is returned. Note that under Win32, system time
1905 expression (if any) is returned. Note that under Win32, system time
1905 is always reported as 0, since it can not be measured.
1906 is always reported as 0, since it can not be measured.
1906
1907
1907 This function provides very basic timing functionality. In Python
1908 This function provides very basic timing functionality. In Python
1908 2.3, the timeit module offers more control and sophistication, so this
1909 2.3, the timeit module offers more control and sophistication, so this
1909 could be rewritten to use it (patches welcome).
1910 could be rewritten to use it (patches welcome).
1910
1911
1911 Some examples:
1912 Some examples:
1912
1913
1913 In [1]: time 2**128
1914 In [1]: time 2**128
1914 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1915 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1915 Wall time: 0.00
1916 Wall time: 0.00
1916 Out[1]: 340282366920938463463374607431768211456L
1917 Out[1]: 340282366920938463463374607431768211456L
1917
1918
1918 In [2]: n = 1000000
1919 In [2]: n = 1000000
1919
1920
1920 In [3]: time sum(range(n))
1921 In [3]: time sum(range(n))
1921 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1922 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1922 Wall time: 1.37
1923 Wall time: 1.37
1923 Out[3]: 499999500000L
1924 Out[3]: 499999500000L
1924
1925
1925 In [4]: time print 'hello world'
1926 In [4]: time print 'hello world'
1926 hello world
1927 hello world
1927 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1928 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1928 Wall time: 0.00
1929 Wall time: 0.00
1929
1930
1930 Note that the time needed by Python to compile the given expression
1931 Note that the time needed by Python to compile the given expression
1931 will be reported if it is more than 0.1s. In this example, the
1932 will be reported if it is more than 0.1s. In this example, the
1932 actual exponentiation is done by Python at compilation time, so while
1933 actual exponentiation is done by Python at compilation time, so while
1933 the expression can take a noticeable amount of time to compute, that
1934 the expression can take a noticeable amount of time to compute, that
1934 time is purely due to the compilation:
1935 time is purely due to the compilation:
1935
1936
1936 In [5]: time 3**9999;
1937 In [5]: time 3**9999;
1937 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1938 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1938 Wall time: 0.00 s
1939 Wall time: 0.00 s
1939
1940
1940 In [6]: time 3**999999;
1941 In [6]: time 3**999999;
1941 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1942 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1942 Wall time: 0.00 s
1943 Wall time: 0.00 s
1943 Compiler : 0.78 s
1944 Compiler : 0.78 s
1944 """
1945 """
1945
1946
1946 # fail immediately if the given expression can't be compiled
1947 # fail immediately if the given expression can't be compiled
1947
1948
1948 expr = self.shell.prefilter(parameter_s,False)
1949 expr = self.shell.prefilter(parameter_s,False)
1949
1950
1950 # Minimum time above which compilation time will be reported
1951 # Minimum time above which compilation time will be reported
1951 tc_min = 0.1
1952 tc_min = 0.1
1952
1953
1953 try:
1954 try:
1954 mode = 'eval'
1955 mode = 'eval'
1955 t0 = clock()
1956 t0 = clock()
1956 code = compile(expr,'<timed eval>',mode)
1957 code = compile(expr,'<timed eval>',mode)
1957 tc = clock()-t0
1958 tc = clock()-t0
1958 except SyntaxError:
1959 except SyntaxError:
1959 mode = 'exec'
1960 mode = 'exec'
1960 t0 = clock()
1961 t0 = clock()
1961 code = compile(expr,'<timed exec>',mode)
1962 code = compile(expr,'<timed exec>',mode)
1962 tc = clock()-t0
1963 tc = clock()-t0
1963 # skew measurement as little as possible
1964 # skew measurement as little as possible
1964 glob = self.shell.user_ns
1965 glob = self.shell.user_ns
1965 clk = clock2
1966 clk = clock2
1966 wtime = time.time
1967 wtime = time.time
1967 # time execution
1968 # time execution
1968 wall_st = wtime()
1969 wall_st = wtime()
1969 if mode=='eval':
1970 if mode=='eval':
1970 st = clk()
1971 st = clk()
1971 out = eval(code,glob)
1972 out = eval(code,glob)
1972 end = clk()
1973 end = clk()
1973 else:
1974 else:
1974 st = clk()
1975 st = clk()
1975 exec code in glob
1976 exec code in glob
1976 end = clk()
1977 end = clk()
1977 out = None
1978 out = None
1978 wall_end = wtime()
1979 wall_end = wtime()
1979 # Compute actual times and report
1980 # Compute actual times and report
1980 wall_time = wall_end-wall_st
1981 wall_time = wall_end-wall_st
1981 cpu_user = end[0]-st[0]
1982 cpu_user = end[0]-st[0]
1982 cpu_sys = end[1]-st[1]
1983 cpu_sys = end[1]-st[1]
1983 cpu_tot = cpu_user+cpu_sys
1984 cpu_tot = cpu_user+cpu_sys
1984 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1985 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1985 (cpu_user,cpu_sys,cpu_tot)
1986 (cpu_user,cpu_sys,cpu_tot)
1986 print "Wall time: %.2f s" % wall_time
1987 print "Wall time: %.2f s" % wall_time
1987 if tc > tc_min:
1988 if tc > tc_min:
1988 print "Compiler : %.2f s" % tc
1989 print "Compiler : %.2f s" % tc
1989 return out
1990 return out
1990
1991
1991 @testdec.skip_doctest
1992 @testdec.skip_doctest
1992 def magic_macro(self,parameter_s = ''):
1993 def magic_macro(self,parameter_s = ''):
1993 """Define a set of input lines as a macro for future re-execution.
1994 """Define a set of input lines as a macro for future re-execution.
1994
1995
1995 Usage:\\
1996 Usage:\\
1996 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1997 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1997
1998
1998 Options:
1999 Options:
1999
2000
2000 -r: use 'raw' input. By default, the 'processed' history is used,
2001 -r: use 'raw' input. By default, the 'processed' history is used,
2001 so that magics are loaded in their transformed version to valid
2002 so that magics are loaded in their transformed version to valid
2002 Python. If this option is given, the raw input as typed as the
2003 Python. If this option is given, the raw input as typed as the
2003 command line is used instead.
2004 command line is used instead.
2004
2005
2005 This will define a global variable called `name` which is a string
2006 This will define a global variable called `name` which is a string
2006 made of joining the slices and lines you specify (n1,n2,... numbers
2007 made of joining the slices and lines you specify (n1,n2,... numbers
2007 above) from your input history into a single string. This variable
2008 above) from your input history into a single string. This variable
2008 acts like an automatic function which re-executes those lines as if
2009 acts like an automatic function which re-executes those lines as if
2009 you had typed them. You just type 'name' at the prompt and the code
2010 you had typed them. You just type 'name' at the prompt and the code
2010 executes.
2011 executes.
2011
2012
2012 The notation for indicating number ranges is: n1-n2 means 'use line
2013 The notation for indicating number ranges is: n1-n2 means 'use line
2013 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2014 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
2014 using the lines numbered 5,6 and 7.
2015 using the lines numbered 5,6 and 7.
2015
2016
2016 Note: as a 'hidden' feature, you can also use traditional python slice
2017 Note: as a 'hidden' feature, you can also use traditional python slice
2017 notation, where N:M means numbers N through M-1.
2018 notation, where N:M means numbers N through M-1.
2018
2019
2019 For example, if your history contains (%hist prints it):
2020 For example, if your history contains (%hist prints it):
2020
2021
2021 44: x=1
2022 44: x=1
2022 45: y=3
2023 45: y=3
2023 46: z=x+y
2024 46: z=x+y
2024 47: print x
2025 47: print x
2025 48: a=5
2026 48: a=5
2026 49: print 'x',x,'y',y
2027 49: print 'x',x,'y',y
2027
2028
2028 you can create a macro with lines 44 through 47 (included) and line 49
2029 you can create a macro with lines 44 through 47 (included) and line 49
2029 called my_macro with:
2030 called my_macro with:
2030
2031
2031 In [55]: %macro my_macro 44-47 49
2032 In [55]: %macro my_macro 44-47 49
2032
2033
2033 Now, typing `my_macro` (without quotes) will re-execute all this code
2034 Now, typing `my_macro` (without quotes) will re-execute all this code
2034 in one pass.
2035 in one pass.
2035
2036
2036 You don't need to give the line-numbers in order, and any given line
2037 You don't need to give the line-numbers in order, and any given line
2037 number can appear multiple times. You can assemble macros with any
2038 number can appear multiple times. You can assemble macros with any
2038 lines from your input history in any order.
2039 lines from your input history in any order.
2039
2040
2040 The macro is a simple object which holds its value in an attribute,
2041 The macro is a simple object which holds its value in an attribute,
2041 but IPython's display system checks for macros and executes them as
2042 but IPython's display system checks for macros and executes them as
2042 code instead of printing them when you type their name.
2043 code instead of printing them when you type their name.
2043
2044
2044 You can view a macro's contents by explicitly printing it with:
2045 You can view a macro's contents by explicitly printing it with:
2045
2046
2046 'print macro_name'.
2047 'print macro_name'.
2047
2048
2048 For one-off cases which DON'T contain magic function calls in them you
2049 For one-off cases which DON'T contain magic function calls in them you
2049 can obtain similar results by explicitly executing slices from your
2050 can obtain similar results by explicitly executing slices from your
2050 input history with:
2051 input history with:
2051
2052
2052 In [60]: exec In[44:48]+In[49]"""
2053 In [60]: exec In[44:48]+In[49]"""
2053
2054
2054 opts,args = self.parse_options(parameter_s,'r',mode='list')
2055 opts,args = self.parse_options(parameter_s,'r',mode='list')
2055 if not args:
2056 if not args:
2056 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2057 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
2057 macs.sort()
2058 macs.sort()
2058 return macs
2059 return macs
2059 if len(args) == 1:
2060 if len(args) == 1:
2060 raise UsageError(
2061 raise UsageError(
2061 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2062 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
2062 name,ranges = args[0], args[1:]
2063 name,ranges = args[0], args[1:]
2063
2064
2064 #print 'rng',ranges # dbg
2065 #print 'rng',ranges # dbg
2065 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2066 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2066 macro = Macro(lines)
2067 macro = Macro(lines)
2067 self.shell.define_macro(name, macro)
2068 self.shell.define_macro(name, macro)
2068 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2069 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2069 print 'Macro contents:'
2070 print 'Macro contents:'
2070 print macro,
2071 print macro,
2071
2072
2072 def magic_save(self,parameter_s = ''):
2073 def magic_save(self,parameter_s = ''):
2073 """Save a set of lines to a given filename.
2074 """Save a set of lines to a given filename.
2074
2075
2075 Usage:\\
2076 Usage:\\
2076 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2077 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
2077
2078
2078 Options:
2079 Options:
2079
2080
2080 -r: use 'raw' input. By default, the 'processed' history is used,
2081 -r: use 'raw' input. By default, the 'processed' history is used,
2081 so that magics are loaded in their transformed version to valid
2082 so that magics are loaded in their transformed version to valid
2082 Python. If this option is given, the raw input as typed as the
2083 Python. If this option is given, the raw input as typed as the
2083 command line is used instead.
2084 command line is used instead.
2084
2085
2085 This function uses the same syntax as %macro for line extraction, but
2086 This function uses the same syntax as %macro for line extraction, but
2086 instead of creating a macro it saves the resulting string to the
2087 instead of creating a macro it saves the resulting string to the
2087 filename you specify.
2088 filename you specify.
2088
2089
2089 It adds a '.py' extension to the file if you don't do so yourself, and
2090 It adds a '.py' extension to the file if you don't do so yourself, and
2090 it asks for confirmation before overwriting existing files."""
2091 it asks for confirmation before overwriting existing files."""
2091
2092
2092 opts,args = self.parse_options(parameter_s,'r',mode='list')
2093 opts,args = self.parse_options(parameter_s,'r',mode='list')
2093 fname,ranges = args[0], args[1:]
2094 fname,ranges = args[0], args[1:]
2094 if not fname.endswith('.py'):
2095 if not fname.endswith('.py'):
2095 fname += '.py'
2096 fname += '.py'
2096 if os.path.isfile(fname):
2097 if os.path.isfile(fname):
2097 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2098 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2098 if ans.lower() not in ['y','yes']:
2099 if ans.lower() not in ['y','yes']:
2099 print 'Operation cancelled.'
2100 print 'Operation cancelled.'
2100 return
2101 return
2101 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2102 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2102 f = file(fname,'w')
2103 f = file(fname,'w')
2103 f.write(cmds)
2104 f.write(cmds)
2104 f.close()
2105 f.close()
2105 print 'The following commands were written to file `%s`:' % fname
2106 print 'The following commands were written to file `%s`:' % fname
2106 print cmds
2107 print cmds
2107
2108
2108 def _edit_macro(self,mname,macro):
2109 def _edit_macro(self,mname,macro):
2109 """open an editor with the macro data in a file"""
2110 """open an editor with the macro data in a file"""
2110 filename = self.shell.mktempfile(macro.value)
2111 filename = self.shell.mktempfile(macro.value)
2111 self.shell.hooks.editor(filename)
2112 self.shell.hooks.editor(filename)
2112
2113
2113 # and make a new macro object, to replace the old one
2114 # and make a new macro object, to replace the old one
2114 mfile = open(filename)
2115 mfile = open(filename)
2115 mvalue = mfile.read()
2116 mvalue = mfile.read()
2116 mfile.close()
2117 mfile.close()
2117 self.shell.user_ns[mname] = Macro(mvalue)
2118 self.shell.user_ns[mname] = Macro(mvalue)
2118
2119
2119 def magic_ed(self,parameter_s=''):
2120 def magic_ed(self,parameter_s=''):
2120 """Alias to %edit."""
2121 """Alias to %edit."""
2121 return self.magic_edit(parameter_s)
2122 return self.magic_edit(parameter_s)
2122
2123
2123 @testdec.skip_doctest
2124 @testdec.skip_doctest
2124 def magic_edit(self,parameter_s='',last_call=['','']):
2125 def magic_edit(self,parameter_s='',last_call=['','']):
2125 """Bring up an editor and execute the resulting code.
2126 """Bring up an editor and execute the resulting code.
2126
2127
2127 Usage:
2128 Usage:
2128 %edit [options] [args]
2129 %edit [options] [args]
2129
2130
2130 %edit runs IPython's editor hook. The default version of this hook is
2131 %edit runs IPython's editor hook. The default version of this hook is
2131 set to call the __IPYTHON__.rc.editor command. This is read from your
2132 set to call the __IPYTHON__.rc.editor command. This is read from your
2132 environment variable $EDITOR. If this isn't found, it will default to
2133 environment variable $EDITOR. If this isn't found, it will default to
2133 vi under Linux/Unix and to notepad under Windows. See the end of this
2134 vi under Linux/Unix and to notepad under Windows. See the end of this
2134 docstring for how to change the editor hook.
2135 docstring for how to change the editor hook.
2135
2136
2136 You can also set the value of this editor via the command line option
2137 You can also set the value of this editor via the command line option
2137 '-editor' or in your ipythonrc file. This is useful if you wish to use
2138 '-editor' or in your ipythonrc file. This is useful if you wish to use
2138 specifically for IPython an editor different from your typical default
2139 specifically for IPython an editor different from your typical default
2139 (and for Windows users who typically don't set environment variables).
2140 (and for Windows users who typically don't set environment variables).
2140
2141
2141 This command allows you to conveniently edit multi-line code right in
2142 This command allows you to conveniently edit multi-line code right in
2142 your IPython session.
2143 your IPython session.
2143
2144
2144 If called without arguments, %edit opens up an empty editor with a
2145 If called without arguments, %edit opens up an empty editor with a
2145 temporary file and will execute the contents of this file when you
2146 temporary file and will execute the contents of this file when you
2146 close it (don't forget to save it!).
2147 close it (don't forget to save it!).
2147
2148
2148
2149
2149 Options:
2150 Options:
2150
2151
2151 -n <number>: open the editor at a specified line number. By default,
2152 -n <number>: open the editor at a specified line number. By default,
2152 the IPython editor hook uses the unix syntax 'editor +N filename', but
2153 the IPython editor hook uses the unix syntax 'editor +N filename', but
2153 you can configure this by providing your own modified hook if your
2154 you can configure this by providing your own modified hook if your
2154 favorite editor supports line-number specifications with a different
2155 favorite editor supports line-number specifications with a different
2155 syntax.
2156 syntax.
2156
2157
2157 -p: this will call the editor with the same data as the previous time
2158 -p: this will call the editor with the same data as the previous time
2158 it was used, regardless of how long ago (in your current session) it
2159 it was used, regardless of how long ago (in your current session) it
2159 was.
2160 was.
2160
2161
2161 -r: use 'raw' input. This option only applies to input taken from the
2162 -r: use 'raw' input. This option only applies to input taken from the
2162 user's history. By default, the 'processed' history is used, so that
2163 user's history. By default, the 'processed' history is used, so that
2163 magics are loaded in their transformed version to valid Python. If
2164 magics are loaded in their transformed version to valid Python. If
2164 this option is given, the raw input as typed as the command line is
2165 this option is given, the raw input as typed as the command line is
2165 used instead. When you exit the editor, it will be executed by
2166 used instead. When you exit the editor, it will be executed by
2166 IPython's own processor.
2167 IPython's own processor.
2167
2168
2168 -x: do not execute the edited code immediately upon exit. This is
2169 -x: do not execute the edited code immediately upon exit. This is
2169 mainly useful if you are editing programs which need to be called with
2170 mainly useful if you are editing programs which need to be called with
2170 command line arguments, which you can then do using %run.
2171 command line arguments, which you can then do using %run.
2171
2172
2172
2173
2173 Arguments:
2174 Arguments:
2174
2175
2175 If arguments are given, the following possibilites exist:
2176 If arguments are given, the following possibilites exist:
2176
2177
2177 - The arguments are numbers or pairs of colon-separated numbers (like
2178 - The arguments are numbers or pairs of colon-separated numbers (like
2178 1 4:8 9). These are interpreted as lines of previous input to be
2179 1 4:8 9). These are interpreted as lines of previous input to be
2179 loaded into the editor. The syntax is the same of the %macro command.
2180 loaded into the editor. The syntax is the same of the %macro command.
2180
2181
2181 - If the argument doesn't start with a number, it is evaluated as a
2182 - If the argument doesn't start with a number, it is evaluated as a
2182 variable and its contents loaded into the editor. You can thus edit
2183 variable and its contents loaded into the editor. You can thus edit
2183 any string which contains python code (including the result of
2184 any string which contains python code (including the result of
2184 previous edits).
2185 previous edits).
2185
2186
2186 - If the argument is the name of an object (other than a string),
2187 - If the argument is the name of an object (other than a string),
2187 IPython will try to locate the file where it was defined and open the
2188 IPython will try to locate the file where it was defined and open the
2188 editor at the point where it is defined. You can use `%edit function`
2189 editor at the point where it is defined. You can use `%edit function`
2189 to load an editor exactly at the point where 'function' is defined,
2190 to load an editor exactly at the point where 'function' is defined,
2190 edit it and have the file be executed automatically.
2191 edit it and have the file be executed automatically.
2191
2192
2192 If the object is a macro (see %macro for details), this opens up your
2193 If the object is a macro (see %macro for details), this opens up your
2193 specified editor with a temporary file containing the macro's data.
2194 specified editor with a temporary file containing the macro's data.
2194 Upon exit, the macro is reloaded with the contents of the file.
2195 Upon exit, the macro is reloaded with the contents of the file.
2195
2196
2196 Note: opening at an exact line is only supported under Unix, and some
2197 Note: opening at an exact line is only supported under Unix, and some
2197 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2198 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2198 '+NUMBER' parameter necessary for this feature. Good editors like
2199 '+NUMBER' parameter necessary for this feature. Good editors like
2199 (X)Emacs, vi, jed, pico and joe all do.
2200 (X)Emacs, vi, jed, pico and joe all do.
2200
2201
2201 - If the argument is not found as a variable, IPython will look for a
2202 - If the argument is not found as a variable, IPython will look for a
2202 file with that name (adding .py if necessary) and load it into the
2203 file with that name (adding .py if necessary) and load it into the
2203 editor. It will execute its contents with execfile() when you exit,
2204 editor. It will execute its contents with execfile() when you exit,
2204 loading any code in the file into your interactive namespace.
2205 loading any code in the file into your interactive namespace.
2205
2206
2206 After executing your code, %edit will return as output the code you
2207 After executing your code, %edit will return as output the code you
2207 typed in the editor (except when it was an existing file). This way
2208 typed in the editor (except when it was an existing file). This way
2208 you can reload the code in further invocations of %edit as a variable,
2209 you can reload the code in further invocations of %edit as a variable,
2209 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2210 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2210 the output.
2211 the output.
2211
2212
2212 Note that %edit is also available through the alias %ed.
2213 Note that %edit is also available through the alias %ed.
2213
2214
2214 This is an example of creating a simple function inside the editor and
2215 This is an example of creating a simple function inside the editor and
2215 then modifying it. First, start up the editor:
2216 then modifying it. First, start up the editor:
2216
2217
2217 In [1]: ed
2218 In [1]: ed
2218 Editing... done. Executing edited code...
2219 Editing... done. Executing edited code...
2219 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2220 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2220
2221
2221 We can then call the function foo():
2222 We can then call the function foo():
2222
2223
2223 In [2]: foo()
2224 In [2]: foo()
2224 foo() was defined in an editing session
2225 foo() was defined in an editing session
2225
2226
2226 Now we edit foo. IPython automatically loads the editor with the
2227 Now we edit foo. IPython automatically loads the editor with the
2227 (temporary) file where foo() was previously defined:
2228 (temporary) file where foo() was previously defined:
2228
2229
2229 In [3]: ed foo
2230 In [3]: ed foo
2230 Editing... done. Executing edited code...
2231 Editing... done. Executing edited code...
2231
2232
2232 And if we call foo() again we get the modified version:
2233 And if we call foo() again we get the modified version:
2233
2234
2234 In [4]: foo()
2235 In [4]: foo()
2235 foo() has now been changed!
2236 foo() has now been changed!
2236
2237
2237 Here is an example of how to edit a code snippet successive
2238 Here is an example of how to edit a code snippet successive
2238 times. First we call the editor:
2239 times. First we call the editor:
2239
2240
2240 In [5]: ed
2241 In [5]: ed
2241 Editing... done. Executing edited code...
2242 Editing... done. Executing edited code...
2242 hello
2243 hello
2243 Out[5]: "print 'hello'n"
2244 Out[5]: "print 'hello'n"
2244
2245
2245 Now we call it again with the previous output (stored in _):
2246 Now we call it again with the previous output (stored in _):
2246
2247
2247 In [6]: ed _
2248 In [6]: ed _
2248 Editing... done. Executing edited code...
2249 Editing... done. Executing edited code...
2249 hello world
2250 hello world
2250 Out[6]: "print 'hello world'n"
2251 Out[6]: "print 'hello world'n"
2251
2252
2252 Now we call it with the output #8 (stored in _8, also as Out[8]):
2253 Now we call it with the output #8 (stored in _8, also as Out[8]):
2253
2254
2254 In [7]: ed _8
2255 In [7]: ed _8
2255 Editing... done. Executing edited code...
2256 Editing... done. Executing edited code...
2256 hello again
2257 hello again
2257 Out[7]: "print 'hello again'n"
2258 Out[7]: "print 'hello again'n"
2258
2259
2259
2260
2260 Changing the default editor hook:
2261 Changing the default editor hook:
2261
2262
2262 If you wish to write your own editor hook, you can put it in a
2263 If you wish to write your own editor hook, you can put it in a
2263 configuration file which you load at startup time. The default hook
2264 configuration file which you load at startup time. The default hook
2264 is defined in the IPython.core.hooks module, and you can use that as a
2265 is defined in the IPython.core.hooks module, and you can use that as a
2265 starting example for further modifications. That file also has
2266 starting example for further modifications. That file also has
2266 general instructions on how to set a new hook for use once you've
2267 general instructions on how to set a new hook for use once you've
2267 defined it."""
2268 defined it."""
2268
2269
2269 # FIXME: This function has become a convoluted mess. It needs a
2270 # FIXME: This function has become a convoluted mess. It needs a
2270 # ground-up rewrite with clean, simple logic.
2271 # ground-up rewrite with clean, simple logic.
2271
2272
2272 def make_filename(arg):
2273 def make_filename(arg):
2273 "Make a filename from the given args"
2274 "Make a filename from the given args"
2274 try:
2275 try:
2275 filename = get_py_filename(arg)
2276 filename = get_py_filename(arg)
2276 except IOError:
2277 except IOError:
2277 if args.endswith('.py'):
2278 if args.endswith('.py'):
2278 filename = arg
2279 filename = arg
2279 else:
2280 else:
2280 filename = None
2281 filename = None
2281 return filename
2282 return filename
2282
2283
2283 # custom exceptions
2284 # custom exceptions
2284 class DataIsObject(Exception): pass
2285 class DataIsObject(Exception): pass
2285
2286
2286 opts,args = self.parse_options(parameter_s,'prxn:')
2287 opts,args = self.parse_options(parameter_s,'prxn:')
2287 # Set a few locals from the options for convenience:
2288 # Set a few locals from the options for convenience:
2288 opts_p = opts.has_key('p')
2289 opts_p = opts.has_key('p')
2289 opts_r = opts.has_key('r')
2290 opts_r = opts.has_key('r')
2290
2291
2291 # Default line number value
2292 # Default line number value
2292 lineno = opts.get('n',None)
2293 lineno = opts.get('n',None)
2293
2294
2294 if opts_p:
2295 if opts_p:
2295 args = '_%s' % last_call[0]
2296 args = '_%s' % last_call[0]
2296 if not self.shell.user_ns.has_key(args):
2297 if not self.shell.user_ns.has_key(args):
2297 args = last_call[1]
2298 args = last_call[1]
2298
2299
2299 # use last_call to remember the state of the previous call, but don't
2300 # use last_call to remember the state of the previous call, but don't
2300 # let it be clobbered by successive '-p' calls.
2301 # let it be clobbered by successive '-p' calls.
2301 try:
2302 try:
2302 last_call[0] = self.shell.outputcache.prompt_count
2303 last_call[0] = self.shell.outputcache.prompt_count
2303 if not opts_p:
2304 if not opts_p:
2304 last_call[1] = parameter_s
2305 last_call[1] = parameter_s
2305 except:
2306 except:
2306 pass
2307 pass
2307
2308
2308 # by default this is done with temp files, except when the given
2309 # by default this is done with temp files, except when the given
2309 # arg is a filename
2310 # arg is a filename
2310 use_temp = 1
2311 use_temp = 1
2311
2312
2312 if re.match(r'\d',args):
2313 if re.match(r'\d',args):
2313 # Mode where user specifies ranges of lines, like in %macro.
2314 # Mode where user specifies ranges of lines, like in %macro.
2314 # This means that you can't edit files whose names begin with
2315 # This means that you can't edit files whose names begin with
2315 # numbers this way. Tough.
2316 # numbers this way. Tough.
2316 ranges = args.split()
2317 ranges = args.split()
2317 data = ''.join(self.extract_input_slices(ranges,opts_r))
2318 data = ''.join(self.extract_input_slices(ranges,opts_r))
2318 elif args.endswith('.py'):
2319 elif args.endswith('.py'):
2319 filename = make_filename(args)
2320 filename = make_filename(args)
2320 data = ''
2321 data = ''
2321 use_temp = 0
2322 use_temp = 0
2322 elif args:
2323 elif args:
2323 try:
2324 try:
2324 # Load the parameter given as a variable. If not a string,
2325 # Load the parameter given as a variable. If not a string,
2325 # process it as an object instead (below)
2326 # process it as an object instead (below)
2326
2327
2327 #print '*** args',args,'type',type(args) # dbg
2328 #print '*** args',args,'type',type(args) # dbg
2328 data = eval(args,self.shell.user_ns)
2329 data = eval(args,self.shell.user_ns)
2329 if not type(data) in StringTypes:
2330 if not type(data) in StringTypes:
2330 raise DataIsObject
2331 raise DataIsObject
2331
2332
2332 except (NameError,SyntaxError):
2333 except (NameError,SyntaxError):
2333 # given argument is not a variable, try as a filename
2334 # given argument is not a variable, try as a filename
2334 filename = make_filename(args)
2335 filename = make_filename(args)
2335 if filename is None:
2336 if filename is None:
2336 warn("Argument given (%s) can't be found as a variable "
2337 warn("Argument given (%s) can't be found as a variable "
2337 "or as a filename." % args)
2338 "or as a filename." % args)
2338 return
2339 return
2339
2340
2340 data = ''
2341 data = ''
2341 use_temp = 0
2342 use_temp = 0
2342 except DataIsObject:
2343 except DataIsObject:
2343
2344
2344 # macros have a special edit function
2345 # macros have a special edit function
2345 if isinstance(data,Macro):
2346 if isinstance(data,Macro):
2346 self._edit_macro(args,data)
2347 self._edit_macro(args,data)
2347 return
2348 return
2348
2349
2349 # For objects, try to edit the file where they are defined
2350 # For objects, try to edit the file where they are defined
2350 try:
2351 try:
2351 filename = inspect.getabsfile(data)
2352 filename = inspect.getabsfile(data)
2352 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2353 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2353 # class created by %edit? Try to find source
2354 # class created by %edit? Try to find source
2354 # by looking for method definitions instead, the
2355 # by looking for method definitions instead, the
2355 # __module__ in those classes is FakeModule.
2356 # __module__ in those classes is FakeModule.
2356 attrs = [getattr(data, aname) for aname in dir(data)]
2357 attrs = [getattr(data, aname) for aname in dir(data)]
2357 for attr in attrs:
2358 for attr in attrs:
2358 if not inspect.ismethod(attr):
2359 if not inspect.ismethod(attr):
2359 continue
2360 continue
2360 filename = inspect.getabsfile(attr)
2361 filename = inspect.getabsfile(attr)
2361 if filename and 'fakemodule' not in filename.lower():
2362 if filename and 'fakemodule' not in filename.lower():
2362 # change the attribute to be the edit target instead
2363 # change the attribute to be the edit target instead
2363 data = attr
2364 data = attr
2364 break
2365 break
2365
2366
2366 datafile = 1
2367 datafile = 1
2367 except TypeError:
2368 except TypeError:
2368 filename = make_filename(args)
2369 filename = make_filename(args)
2369 datafile = 1
2370 datafile = 1
2370 warn('Could not find file where `%s` is defined.\n'
2371 warn('Could not find file where `%s` is defined.\n'
2371 'Opening a file named `%s`' % (args,filename))
2372 'Opening a file named `%s`' % (args,filename))
2372 # Now, make sure we can actually read the source (if it was in
2373 # Now, make sure we can actually read the source (if it was in
2373 # a temp file it's gone by now).
2374 # a temp file it's gone by now).
2374 if datafile:
2375 if datafile:
2375 try:
2376 try:
2376 if lineno is None:
2377 if lineno is None:
2377 lineno = inspect.getsourcelines(data)[1]
2378 lineno = inspect.getsourcelines(data)[1]
2378 except IOError:
2379 except IOError:
2379 filename = make_filename(args)
2380 filename = make_filename(args)
2380 if filename is None:
2381 if filename is None:
2381 warn('The file `%s` where `%s` was defined cannot '
2382 warn('The file `%s` where `%s` was defined cannot '
2382 'be read.' % (filename,data))
2383 'be read.' % (filename,data))
2383 return
2384 return
2384 use_temp = 0
2385 use_temp = 0
2385 else:
2386 else:
2386 data = ''
2387 data = ''
2387
2388
2388 if use_temp:
2389 if use_temp:
2389 filename = self.shell.mktempfile(data)
2390 filename = self.shell.mktempfile(data)
2390 print 'IPython will make a temporary file named:',filename
2391 print 'IPython will make a temporary file named:',filename
2391
2392
2392 # do actual editing here
2393 # do actual editing here
2393 print 'Editing...',
2394 print 'Editing...',
2394 sys.stdout.flush()
2395 sys.stdout.flush()
2395 try:
2396 try:
2396 self.shell.hooks.editor(filename,lineno)
2397 self.shell.hooks.editor(filename,lineno)
2397 except TryNext:
2398 except TryNext:
2398 warn('Could not open editor')
2399 warn('Could not open editor')
2399 return
2400 return
2400
2401
2401 # XXX TODO: should this be generalized for all string vars?
2402 # XXX TODO: should this be generalized for all string vars?
2402 # For now, this is special-cased to blocks created by cpaste
2403 # For now, this is special-cased to blocks created by cpaste
2403 if args.strip() == 'pasted_block':
2404 if args.strip() == 'pasted_block':
2404 self.shell.user_ns['pasted_block'] = file_read(filename)
2405 self.shell.user_ns['pasted_block'] = file_read(filename)
2405
2406
2406 if opts.has_key('x'): # -x prevents actual execution
2407 if opts.has_key('x'): # -x prevents actual execution
2407 print
2408 print
2408 else:
2409 else:
2409 print 'done. Executing edited code...'
2410 print 'done. Executing edited code...'
2410 if opts_r:
2411 if opts_r:
2411 self.shell.runlines(file_read(filename))
2412 self.shell.runlines(file_read(filename))
2412 else:
2413 else:
2413 self.shell.safe_execfile(filename,self.shell.user_ns,
2414 self.shell.safe_execfile(filename,self.shell.user_ns,
2414 self.shell.user_ns)
2415 self.shell.user_ns)
2415
2416
2416
2417
2417 if use_temp:
2418 if use_temp:
2418 try:
2419 try:
2419 return open(filename).read()
2420 return open(filename).read()
2420 except IOError,msg:
2421 except IOError,msg:
2421 if msg.filename == filename:
2422 if msg.filename == filename:
2422 warn('File not found. Did you forget to save?')
2423 warn('File not found. Did you forget to save?')
2423 return
2424 return
2424 else:
2425 else:
2425 self.shell.showtraceback()
2426 self.shell.showtraceback()
2426
2427
2427 def magic_xmode(self,parameter_s = ''):
2428 def magic_xmode(self,parameter_s = ''):
2428 """Switch modes for the exception handlers.
2429 """Switch modes for the exception handlers.
2429
2430
2430 Valid modes: Plain, Context and Verbose.
2431 Valid modes: Plain, Context and Verbose.
2431
2432
2432 If called without arguments, acts as a toggle."""
2433 If called without arguments, acts as a toggle."""
2433
2434
2434 def xmode_switch_err(name):
2435 def xmode_switch_err(name):
2435 warn('Error changing %s exception modes.\n%s' %
2436 warn('Error changing %s exception modes.\n%s' %
2436 (name,sys.exc_info()[1]))
2437 (name,sys.exc_info()[1]))
2437
2438
2438 shell = self.shell
2439 shell = self.shell
2439 new_mode = parameter_s.strip().capitalize()
2440 new_mode = parameter_s.strip().capitalize()
2440 try:
2441 try:
2441 shell.InteractiveTB.set_mode(mode=new_mode)
2442 shell.InteractiveTB.set_mode(mode=new_mode)
2442 print 'Exception reporting mode:',shell.InteractiveTB.mode
2443 print 'Exception reporting mode:',shell.InteractiveTB.mode
2443 except:
2444 except:
2444 xmode_switch_err('user')
2445 xmode_switch_err('user')
2445
2446
2446 # threaded shells use a special handler in sys.excepthook
2447 # threaded shells use a special handler in sys.excepthook
2447 if shell.isthreaded:
2448 if shell.isthreaded:
2448 try:
2449 try:
2449 shell.sys_excepthook.set_mode(mode=new_mode)
2450 shell.sys_excepthook.set_mode(mode=new_mode)
2450 except:
2451 except:
2451 xmode_switch_err('threaded')
2452 xmode_switch_err('threaded')
2452
2453
2453 def magic_colors(self,parameter_s = ''):
2454 def magic_colors(self,parameter_s = ''):
2454 """Switch color scheme for prompts, info system and exception handlers.
2455 """Switch color scheme for prompts, info system and exception handlers.
2455
2456
2456 Currently implemented schemes: NoColor, Linux, LightBG.
2457 Currently implemented schemes: NoColor, Linux, LightBG.
2457
2458
2458 Color scheme names are not case-sensitive."""
2459 Color scheme names are not case-sensitive."""
2459
2460
2460 def color_switch_err(name):
2461 def color_switch_err(name):
2461 warn('Error changing %s color schemes.\n%s' %
2462 warn('Error changing %s color schemes.\n%s' %
2462 (name,sys.exc_info()[1]))
2463 (name,sys.exc_info()[1]))
2463
2464
2464
2465
2465 new_scheme = parameter_s.strip()
2466 new_scheme = parameter_s.strip()
2466 if not new_scheme:
2467 if not new_scheme:
2467 raise UsageError(
2468 raise UsageError(
2468 "%colors: you must specify a color scheme. See '%colors?'")
2469 "%colors: you must specify a color scheme. See '%colors?'")
2469 return
2470 return
2470 # local shortcut
2471 # local shortcut
2471 shell = self.shell
2472 shell = self.shell
2472
2473
2473 import IPython.utils.rlineimpl as readline
2474 import IPython.utils.rlineimpl as readline
2474
2475
2475 if not readline.have_readline and sys.platform == "win32":
2476 if not readline.have_readline and sys.platform == "win32":
2476 msg = """\
2477 msg = """\
2477 Proper color support under MS Windows requires the pyreadline library.
2478 Proper color support under MS Windows requires the pyreadline library.
2478 You can find it at:
2479 You can find it at:
2479 http://ipython.scipy.org/moin/PyReadline/Intro
2480 http://ipython.scipy.org/moin/PyReadline/Intro
2480 Gary's readline needs the ctypes module, from:
2481 Gary's readline needs the ctypes module, from:
2481 http://starship.python.net/crew/theller/ctypes
2482 http://starship.python.net/crew/theller/ctypes
2482 (Note that ctypes is already part of Python versions 2.5 and newer).
2483 (Note that ctypes is already part of Python versions 2.5 and newer).
2483
2484
2484 Defaulting color scheme to 'NoColor'"""
2485 Defaulting color scheme to 'NoColor'"""
2485 new_scheme = 'NoColor'
2486 new_scheme = 'NoColor'
2486 warn(msg)
2487 warn(msg)
2487
2488
2488 # readline option is 0
2489 # readline option is 0
2489 if not shell.has_readline:
2490 if not shell.has_readline:
2490 new_scheme = 'NoColor'
2491 new_scheme = 'NoColor'
2491
2492
2492 # Set prompt colors
2493 # Set prompt colors
2493 try:
2494 try:
2494 shell.outputcache.set_colors(new_scheme)
2495 shell.outputcache.set_colors(new_scheme)
2495 except:
2496 except:
2496 color_switch_err('prompt')
2497 color_switch_err('prompt')
2497 else:
2498 else:
2498 shell.colors = \
2499 shell.colors = \
2499 shell.outputcache.color_table.active_scheme_name
2500 shell.outputcache.color_table.active_scheme_name
2500 # Set exception colors
2501 # Set exception colors
2501 try:
2502 try:
2502 shell.InteractiveTB.set_colors(scheme = new_scheme)
2503 shell.InteractiveTB.set_colors(scheme = new_scheme)
2503 shell.SyntaxTB.set_colors(scheme = new_scheme)
2504 shell.SyntaxTB.set_colors(scheme = new_scheme)
2504 except:
2505 except:
2505 color_switch_err('exception')
2506 color_switch_err('exception')
2506
2507
2507 # threaded shells use a verbose traceback in sys.excepthook
2508 # threaded shells use a verbose traceback in sys.excepthook
2508 if shell.isthreaded:
2509 if shell.isthreaded:
2509 try:
2510 try:
2510 shell.sys_excepthook.set_colors(scheme=new_scheme)
2511 shell.sys_excepthook.set_colors(scheme=new_scheme)
2511 except:
2512 except:
2512 color_switch_err('system exception handler')
2513 color_switch_err('system exception handler')
2513
2514
2514 # Set info (for 'object?') colors
2515 # Set info (for 'object?') colors
2515 if shell.color_info:
2516 if shell.color_info:
2516 try:
2517 try:
2517 shell.inspector.set_active_scheme(new_scheme)
2518 shell.inspector.set_active_scheme(new_scheme)
2518 except:
2519 except:
2519 color_switch_err('object inspector')
2520 color_switch_err('object inspector')
2520 else:
2521 else:
2521 shell.inspector.set_active_scheme('NoColor')
2522 shell.inspector.set_active_scheme('NoColor')
2522
2523
2523 def magic_color_info(self,parameter_s = ''):
2524 def magic_color_info(self,parameter_s = ''):
2524 """Toggle color_info.
2525 """Toggle color_info.
2525
2526
2526 The color_info configuration parameter controls whether colors are
2527 The color_info configuration parameter controls whether colors are
2527 used for displaying object details (by things like %psource, %pfile or
2528 used for displaying object details (by things like %psource, %pfile or
2528 the '?' system). This function toggles this value with each call.
2529 the '?' system). This function toggles this value with each call.
2529
2530
2530 Note that unless you have a fairly recent pager (less works better
2531 Note that unless you have a fairly recent pager (less works better
2531 than more) in your system, using colored object information displays
2532 than more) in your system, using colored object information displays
2532 will not work properly. Test it and see."""
2533 will not work properly. Test it and see."""
2533
2534
2534 self.shell.color_info = not self.shell.color_info
2535 self.shell.color_info = not self.shell.color_info
2535 self.magic_colors(self.shell.colors)
2536 self.magic_colors(self.shell.colors)
2536 print 'Object introspection functions have now coloring:',
2537 print 'Object introspection functions have now coloring:',
2537 print ['OFF','ON'][int(self.shell.color_info)]
2538 print ['OFF','ON'][int(self.shell.color_info)]
2538
2539
2539 def magic_Pprint(self, parameter_s=''):
2540 def magic_Pprint(self, parameter_s=''):
2540 """Toggle pretty printing on/off."""
2541 """Toggle pretty printing on/off."""
2541
2542
2542 self.shell.pprint = 1 - self.shell.pprint
2543 self.shell.pprint = 1 - self.shell.pprint
2543 print 'Pretty printing has been turned', \
2544 print 'Pretty printing has been turned', \
2544 ['OFF','ON'][self.shell.pprint]
2545 ['OFF','ON'][self.shell.pprint]
2545
2546
2546 def magic_exit(self, parameter_s=''):
2547 def magic_exit(self, parameter_s=''):
2547 """Exit IPython, confirming if configured to do so.
2548 """Exit IPython, confirming if configured to do so.
2548
2549
2549 You can configure whether IPython asks for confirmation upon exit by
2550 You can configure whether IPython asks for confirmation upon exit by
2550 setting the confirm_exit flag in the ipythonrc file."""
2551 setting the confirm_exit flag in the ipythonrc file."""
2551
2552
2552 self.shell.exit()
2553 self.shell.exit()
2553
2554
2554 def magic_quit(self, parameter_s=''):
2555 def magic_quit(self, parameter_s=''):
2555 """Exit IPython, confirming if configured to do so (like %exit)"""
2556 """Exit IPython, confirming if configured to do so (like %exit)"""
2556
2557
2557 self.shell.exit()
2558 self.shell.exit()
2558
2559
2559 def magic_Exit(self, parameter_s=''):
2560 def magic_Exit(self, parameter_s=''):
2560 """Exit IPython without confirmation."""
2561 """Exit IPython without confirmation."""
2561
2562
2562 self.shell.ask_exit()
2563 self.shell.ask_exit()
2563
2564
2564 #......................................................................
2565 #......................................................................
2565 # Functions to implement unix shell-type things
2566 # Functions to implement unix shell-type things
2566
2567
2567 @testdec.skip_doctest
2568 @testdec.skip_doctest
2568 def magic_alias(self, parameter_s = ''):
2569 def magic_alias(self, parameter_s = ''):
2569 """Define an alias for a system command.
2570 """Define an alias for a system command.
2570
2571
2571 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2572 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2572
2573
2573 Then, typing 'alias_name params' will execute the system command 'cmd
2574 Then, typing 'alias_name params' will execute the system command 'cmd
2574 params' (from your underlying operating system).
2575 params' (from your underlying operating system).
2575
2576
2576 Aliases have lower precedence than magic functions and Python normal
2577 Aliases have lower precedence than magic functions and Python normal
2577 variables, so if 'foo' is both a Python variable and an alias, the
2578 variables, so if 'foo' is both a Python variable and an alias, the
2578 alias can not be executed until 'del foo' removes the Python variable.
2579 alias can not be executed until 'del foo' removes the Python variable.
2579
2580
2580 You can use the %l specifier in an alias definition to represent the
2581 You can use the %l specifier in an alias definition to represent the
2581 whole line when the alias is called. For example:
2582 whole line when the alias is called. For example:
2582
2583
2583 In [2]: alias all echo "Input in brackets: <%l>"
2584 In [2]: alias all echo "Input in brackets: <%l>"
2584 In [3]: all hello world
2585 In [3]: all hello world
2585 Input in brackets: <hello world>
2586 Input in brackets: <hello world>
2586
2587
2587 You can also define aliases with parameters using %s specifiers (one
2588 You can also define aliases with parameters using %s specifiers (one
2588 per parameter):
2589 per parameter):
2589
2590
2590 In [1]: alias parts echo first %s second %s
2591 In [1]: alias parts echo first %s second %s
2591 In [2]: %parts A B
2592 In [2]: %parts A B
2592 first A second B
2593 first A second B
2593 In [3]: %parts A
2594 In [3]: %parts A
2594 Incorrect number of arguments: 2 expected.
2595 Incorrect number of arguments: 2 expected.
2595 parts is an alias to: 'echo first %s second %s'
2596 parts is an alias to: 'echo first %s second %s'
2596
2597
2597 Note that %l and %s are mutually exclusive. You can only use one or
2598 Note that %l and %s are mutually exclusive. You can only use one or
2598 the other in your aliases.
2599 the other in your aliases.
2599
2600
2600 Aliases expand Python variables just like system calls using ! or !!
2601 Aliases expand Python variables just like system calls using ! or !!
2601 do: all expressions prefixed with '$' get expanded. For details of
2602 do: all expressions prefixed with '$' get expanded. For details of
2602 the semantic rules, see PEP-215:
2603 the semantic rules, see PEP-215:
2603 http://www.python.org/peps/pep-0215.html. This is the library used by
2604 http://www.python.org/peps/pep-0215.html. This is the library used by
2604 IPython for variable expansion. If you want to access a true shell
2605 IPython for variable expansion. If you want to access a true shell
2605 variable, an extra $ is necessary to prevent its expansion by IPython:
2606 variable, an extra $ is necessary to prevent its expansion by IPython:
2606
2607
2607 In [6]: alias show echo
2608 In [6]: alias show echo
2608 In [7]: PATH='A Python string'
2609 In [7]: PATH='A Python string'
2609 In [8]: show $PATH
2610 In [8]: show $PATH
2610 A Python string
2611 A Python string
2611 In [9]: show $$PATH
2612 In [9]: show $$PATH
2612 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2613 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2613
2614
2614 You can use the alias facility to acess all of $PATH. See the %rehash
2615 You can use the alias facility to acess all of $PATH. See the %rehash
2615 and %rehashx functions, which automatically create aliases for the
2616 and %rehashx functions, which automatically create aliases for the
2616 contents of your $PATH.
2617 contents of your $PATH.
2617
2618
2618 If called with no parameters, %alias prints the current alias table."""
2619 If called with no parameters, %alias prints the current alias table."""
2619
2620
2620 par = parameter_s.strip()
2621 par = parameter_s.strip()
2621 if not par:
2622 if not par:
2622 stored = self.db.get('stored_aliases', {} )
2623 stored = self.db.get('stored_aliases', {} )
2623 atab = self.shell.alias_table
2624 aliases = sorted(self.shell.alias_manager.aliases)
2624 aliases = atab.keys()
2625 # for k, v in stored:
2625 aliases.sort()
2626 # atab.append(k, v[0])
2626 res = []
2627
2627 showlast = []
2628 print "Total number of aliases:", len(aliases)
2628 for alias in aliases:
2629 return aliases
2629 special = False
2630
2630 try:
2631 # Now try to define a new one
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)
2647 print "Total number of aliases:",len(aliases)
2648 return res
2649 try:
2632 try:
2650 alias,cmd = par.split(None,1)
2633 alias,cmd = par.split(None, 1)
2651 except:
2634 except:
2652 print oinspect.getdoc(self.magic_alias)
2635 print oinspect.getdoc(self.magic_alias)
2653 else:
2636 else:
2654 nargs = cmd.count('%s')
2637 self.shell.alias_manager.soft_define_alias(alias, cmd)
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)
2661 # end magic_alias
2638 # end magic_alias
2662
2639
2663 def magic_unalias(self, parameter_s = ''):
2640 def magic_unalias(self, parameter_s = ''):
2664 """Remove an alias"""
2641 """Remove an alias"""
2665
2642
2666 aname = parameter_s.strip()
2643 aname = parameter_s.strip()
2667 if aname in self.shell.alias_table:
2644 self.shell.alias_manager.undefine_alias(aname)
2668 del self.shell.alias_table[aname]
2669 stored = self.db.get('stored_aliases', {} )
2645 stored = self.db.get('stored_aliases', {} )
2670 if aname in stored:
2646 if aname in stored:
2671 print "Removing %stored alias",aname
2647 print "Removing %stored alias",aname
2672 del stored[aname]
2648 del stored[aname]
2673 self.db['stored_aliases'] = stored
2649 self.db['stored_aliases'] = stored
2674
2650
2675
2651
2676 def magic_rehashx(self, parameter_s = ''):
2652 def magic_rehashx(self, parameter_s = ''):
2677 """Update the alias table with all executable files in $PATH.
2653 """Update the alias table with all executable files in $PATH.
2678
2654
2679 This version explicitly checks that every entry in $PATH is a file
2655 This version explicitly checks that every entry in $PATH is a file
2680 with execute access (os.X_OK), so it is much slower than %rehash.
2656 with execute access (os.X_OK), so it is much slower than %rehash.
2681
2657
2682 Under Windows, it checks executability as a match agains a
2658 Under Windows, it checks executability as a match agains a
2683 '|'-separated string of extensions, stored in the IPython config
2659 '|'-separated string of extensions, stored in the IPython config
2684 variable win_exec_ext. This defaults to 'exe|com|bat'.
2660 variable win_exec_ext. This defaults to 'exe|com|bat'.
2685
2661
2686 This function also resets the root module cache of module completer,
2662 This function also resets the root module cache of module completer,
2687 used on slow filesystems.
2663 used on slow filesystems.
2688 """
2664 """
2665 from IPython.core.alias import InvalidAliasError
2689
2666
2690 # for the benefit of module completer in ipy_completers.py
2667 # for the benefit of module completer in ipy_completers.py
2691 del self.db['rootmodules']
2668 del self.db['rootmodules']
2692
2669
2693 path = [os.path.abspath(os.path.expanduser(p)) for p in
2670 path = [os.path.abspath(os.path.expanduser(p)) for p in
2694 os.environ.get('PATH','').split(os.pathsep)]
2671 os.environ.get('PATH','').split(os.pathsep)]
2695 path = filter(os.path.isdir,path)
2672 path = filter(os.path.isdir,path)
2696
2673
2697 alias_table = self.shell.alias_table
2698 syscmdlist = []
2674 syscmdlist = []
2675 # Now define isexec in a cross platform manner.
2699 if os.name == 'posix':
2676 if os.name == 'posix':
2700 isexec = lambda fname:os.path.isfile(fname) and \
2677 isexec = lambda fname:os.path.isfile(fname) and \
2701 os.access(fname,os.X_OK)
2678 os.access(fname,os.X_OK)
2702 else:
2679 else:
2703
2704 try:
2680 try:
2705 winext = os.environ['pathext'].replace(';','|').replace('.','')
2681 winext = os.environ['pathext'].replace(';','|').replace('.','')
2706 except KeyError:
2682 except KeyError:
2707 winext = 'exe|com|bat|py'
2683 winext = 'exe|com|bat|py'
2708 if 'py' not in winext:
2684 if 'py' not in winext:
2709 winext += '|py'
2685 winext += '|py'
2710 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2686 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2711 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2687 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2712 savedir = os.getcwd()
2688 savedir = os.getcwd()
2689
2690 # Now walk the paths looking for executables to alias.
2713 try:
2691 try:
2714 # write the whole loop for posix/Windows so we don't have an if in
2692 # write the whole loop for posix/Windows so we don't have an if in
2715 # the innermost part
2693 # the innermost part
2716 if os.name == 'posix':
2694 if os.name == 'posix':
2717 for pdir in path:
2695 for pdir in path:
2718 os.chdir(pdir)
2696 os.chdir(pdir)
2719 for ff in os.listdir(pdir):
2697 for ff in os.listdir(pdir):
2720 if isexec(ff) and ff not in self.shell.no_alias:
2698 if isexec(ff):
2721 # each entry in the alias table must be (N,name),
2699 try:
2722 # where N is the number of positional arguments of the
2700 # Removes dots from the name since ipython
2723 # alias.
2701 # will assume names with dots to be python.
2724 # Dots will be removed from alias names, since ipython
2702 self.shell.alias_manager.define_alias(
2725 # assumes names with dots to be python code
2703 ff.replace('.',''), ff)
2726 alias_table[ff.replace('.','')] = (0,ff)
2704 except InvalidAliasError:
2727 syscmdlist.append(ff)
2705 pass
2706 else:
2707 syscmdlist.append(ff)
2728 else:
2708 else:
2729 for pdir in path:
2709 for pdir in path:
2730 os.chdir(pdir)
2710 os.chdir(pdir)
2731 for ff in os.listdir(pdir):
2711 for ff in os.listdir(pdir):
2732 base, ext = os.path.splitext(ff)
2712 base, ext = os.path.splitext(ff)
2733 if isexec(ff) and base.lower() not in self.shell.no_alias:
2713 if isexec(ff) and base.lower() not in self.shell.no_alias:
2734 if ext.lower() == '.exe':
2714 if ext.lower() == '.exe':
2735 ff = base
2715 ff = base
2736 alias_table[base.lower().replace('.','')] = (0,ff)
2716 try:
2737 syscmdlist.append(ff)
2717 # Removes dots from the name since ipython
2738 # Make sure the alias table doesn't contain keywords or builtins
2718 # will assume names with dots to be python.
2739 self.shell.alias_table_validate()
2719 self.shell.alias_manager.define_alias(
2740 # Call again init_auto_alias() so we get 'rm -i' and other
2720 base.lower().replace('.',''), ff)
2741 # modified aliases since %rehashx will probably clobber them
2721 except InvalidAliasError:
2742
2722 pass
2743 # no, we don't want them. if %rehashx clobbers them, good,
2723 syscmdlist.append(ff)
2744 # we'll probably get better versions
2745 # self.shell.init_auto_alias()
2746 db = self.db
2724 db = self.db
2747 db['syscmdlist'] = syscmdlist
2725 db['syscmdlist'] = syscmdlist
2748 finally:
2726 finally:
2749 os.chdir(savedir)
2727 os.chdir(savedir)
2750
2728
2751 def magic_pwd(self, parameter_s = ''):
2729 def magic_pwd(self, parameter_s = ''):
2752 """Return the current working directory path."""
2730 """Return the current working directory path."""
2753 return os.getcwd()
2731 return os.getcwd()
2754
2732
2755 def magic_cd(self, parameter_s=''):
2733 def magic_cd(self, parameter_s=''):
2756 """Change the current working directory.
2734 """Change the current working directory.
2757
2735
2758 This command automatically maintains an internal list of directories
2736 This command automatically maintains an internal list of directories
2759 you visit during your IPython session, in the variable _dh. The
2737 you visit during your IPython session, in the variable _dh. The
2760 command %dhist shows this history nicely formatted. You can also
2738 command %dhist shows this history nicely formatted. You can also
2761 do 'cd -<tab>' to see directory history conveniently.
2739 do 'cd -<tab>' to see directory history conveniently.
2762
2740
2763 Usage:
2741 Usage:
2764
2742
2765 cd 'dir': changes to directory 'dir'.
2743 cd 'dir': changes to directory 'dir'.
2766
2744
2767 cd -: changes to the last visited directory.
2745 cd -: changes to the last visited directory.
2768
2746
2769 cd -<n>: changes to the n-th directory in the directory history.
2747 cd -<n>: changes to the n-th directory in the directory history.
2770
2748
2771 cd --foo: change to directory that matches 'foo' in history
2749 cd --foo: change to directory that matches 'foo' in history
2772
2750
2773 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2751 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2774 (note: cd <bookmark_name> is enough if there is no
2752 (note: cd <bookmark_name> is enough if there is no
2775 directory <bookmark_name>, but a bookmark with the name exists.)
2753 directory <bookmark_name>, but a bookmark with the name exists.)
2776 'cd -b <tab>' allows you to tab-complete bookmark names.
2754 'cd -b <tab>' allows you to tab-complete bookmark names.
2777
2755
2778 Options:
2756 Options:
2779
2757
2780 -q: quiet. Do not print the working directory after the cd command is
2758 -q: quiet. Do not print the working directory after the cd command is
2781 executed. By default IPython's cd command does print this directory,
2759 executed. By default IPython's cd command does print this directory,
2782 since the default prompts do not display path information.
2760 since the default prompts do not display path information.
2783
2761
2784 Note that !cd doesn't work for this purpose because the shell where
2762 Note that !cd doesn't work for this purpose because the shell where
2785 !command runs is immediately discarded after executing 'command'."""
2763 !command runs is immediately discarded after executing 'command'."""
2786
2764
2787 parameter_s = parameter_s.strip()
2765 parameter_s = parameter_s.strip()
2788 #bkms = self.shell.persist.get("bookmarks",{})
2766 #bkms = self.shell.persist.get("bookmarks",{})
2789
2767
2790 oldcwd = os.getcwd()
2768 oldcwd = os.getcwd()
2791 numcd = re.match(r'(-)(\d+)$',parameter_s)
2769 numcd = re.match(r'(-)(\d+)$',parameter_s)
2792 # jump in directory history by number
2770 # jump in directory history by number
2793 if numcd:
2771 if numcd:
2794 nn = int(numcd.group(2))
2772 nn = int(numcd.group(2))
2795 try:
2773 try:
2796 ps = self.shell.user_ns['_dh'][nn]
2774 ps = self.shell.user_ns['_dh'][nn]
2797 except IndexError:
2775 except IndexError:
2798 print 'The requested directory does not exist in history.'
2776 print 'The requested directory does not exist in history.'
2799 return
2777 return
2800 else:
2778 else:
2801 opts = {}
2779 opts = {}
2802 elif parameter_s.startswith('--'):
2780 elif parameter_s.startswith('--'):
2803 ps = None
2781 ps = None
2804 fallback = None
2782 fallback = None
2805 pat = parameter_s[2:]
2783 pat = parameter_s[2:]
2806 dh = self.shell.user_ns['_dh']
2784 dh = self.shell.user_ns['_dh']
2807 # first search only by basename (last component)
2785 # first search only by basename (last component)
2808 for ent in reversed(dh):
2786 for ent in reversed(dh):
2809 if pat in os.path.basename(ent) and os.path.isdir(ent):
2787 if pat in os.path.basename(ent) and os.path.isdir(ent):
2810 ps = ent
2788 ps = ent
2811 break
2789 break
2812
2790
2813 if fallback is None and pat in ent and os.path.isdir(ent):
2791 if fallback is None and pat in ent and os.path.isdir(ent):
2814 fallback = ent
2792 fallback = ent
2815
2793
2816 # if we have no last part match, pick the first full path match
2794 # if we have no last part match, pick the first full path match
2817 if ps is None:
2795 if ps is None:
2818 ps = fallback
2796 ps = fallback
2819
2797
2820 if ps is None:
2798 if ps is None:
2821 print "No matching entry in directory history"
2799 print "No matching entry in directory history"
2822 return
2800 return
2823 else:
2801 else:
2824 opts = {}
2802 opts = {}
2825
2803
2826
2804
2827 else:
2805 else:
2828 #turn all non-space-escaping backslashes to slashes,
2806 #turn all non-space-escaping backslashes to slashes,
2829 # for c:\windows\directory\names\
2807 # for c:\windows\directory\names\
2830 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2808 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2831 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2809 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2832 # jump to previous
2810 # jump to previous
2833 if ps == '-':
2811 if ps == '-':
2834 try:
2812 try:
2835 ps = self.shell.user_ns['_dh'][-2]
2813 ps = self.shell.user_ns['_dh'][-2]
2836 except IndexError:
2814 except IndexError:
2837 raise UsageError('%cd -: No previous directory to change to.')
2815 raise UsageError('%cd -: No previous directory to change to.')
2838 # jump to bookmark if needed
2816 # jump to bookmark if needed
2839 else:
2817 else:
2840 if not os.path.isdir(ps) or opts.has_key('b'):
2818 if not os.path.isdir(ps) or opts.has_key('b'):
2841 bkms = self.db.get('bookmarks', {})
2819 bkms = self.db.get('bookmarks', {})
2842
2820
2843 if bkms.has_key(ps):
2821 if bkms.has_key(ps):
2844 target = bkms[ps]
2822 target = bkms[ps]
2845 print '(bookmark:%s) -> %s' % (ps,target)
2823 print '(bookmark:%s) -> %s' % (ps,target)
2846 ps = target
2824 ps = target
2847 else:
2825 else:
2848 if opts.has_key('b'):
2826 if opts.has_key('b'):
2849 raise UsageError("Bookmark '%s' not found. "
2827 raise UsageError("Bookmark '%s' not found. "
2850 "Use '%%bookmark -l' to see your bookmarks." % ps)
2828 "Use '%%bookmark -l' to see your bookmarks." % ps)
2851
2829
2852 # at this point ps should point to the target dir
2830 # at this point ps should point to the target dir
2853 if ps:
2831 if ps:
2854 try:
2832 try:
2855 os.chdir(os.path.expanduser(ps))
2833 os.chdir(os.path.expanduser(ps))
2856 if self.shell.term_title:
2834 if self.shell.term_title:
2857 platutils.set_term_title('IPython: ' + abbrev_cwd())
2835 platutils.set_term_title('IPython: ' + abbrev_cwd())
2858 except OSError:
2836 except OSError:
2859 print sys.exc_info()[1]
2837 print sys.exc_info()[1]
2860 else:
2838 else:
2861 cwd = os.getcwd()
2839 cwd = os.getcwd()
2862 dhist = self.shell.user_ns['_dh']
2840 dhist = self.shell.user_ns['_dh']
2863 if oldcwd != cwd:
2841 if oldcwd != cwd:
2864 dhist.append(cwd)
2842 dhist.append(cwd)
2865 self.db['dhist'] = compress_dhist(dhist)[-100:]
2843 self.db['dhist'] = compress_dhist(dhist)[-100:]
2866
2844
2867 else:
2845 else:
2868 os.chdir(self.shell.home_dir)
2846 os.chdir(self.shell.home_dir)
2869 if self.shell.term_title:
2847 if self.shell.term_title:
2870 platutils.set_term_title('IPython: ' + '~')
2848 platutils.set_term_title('IPython: ' + '~')
2871 cwd = os.getcwd()
2849 cwd = os.getcwd()
2872 dhist = self.shell.user_ns['_dh']
2850 dhist = self.shell.user_ns['_dh']
2873
2851
2874 if oldcwd != cwd:
2852 if oldcwd != cwd:
2875 dhist.append(cwd)
2853 dhist.append(cwd)
2876 self.db['dhist'] = compress_dhist(dhist)[-100:]
2854 self.db['dhist'] = compress_dhist(dhist)[-100:]
2877 if not 'q' in opts and self.shell.user_ns['_dh']:
2855 if not 'q' in opts and self.shell.user_ns['_dh']:
2878 print self.shell.user_ns['_dh'][-1]
2856 print self.shell.user_ns['_dh'][-1]
2879
2857
2880
2858
2881 def magic_env(self, parameter_s=''):
2859 def magic_env(self, parameter_s=''):
2882 """List environment variables."""
2860 """List environment variables."""
2883
2861
2884 return os.environ.data
2862 return os.environ.data
2885
2863
2886 def magic_pushd(self, parameter_s=''):
2864 def magic_pushd(self, parameter_s=''):
2887 """Place the current dir on stack and change directory.
2865 """Place the current dir on stack and change directory.
2888
2866
2889 Usage:\\
2867 Usage:\\
2890 %pushd ['dirname']
2868 %pushd ['dirname']
2891 """
2869 """
2892
2870
2893 dir_s = self.shell.dir_stack
2871 dir_s = self.shell.dir_stack
2894 tgt = os.path.expanduser(parameter_s)
2872 tgt = os.path.expanduser(parameter_s)
2895 cwd = os.getcwd().replace(self.home_dir,'~')
2873 cwd = os.getcwd().replace(self.home_dir,'~')
2896 if tgt:
2874 if tgt:
2897 self.magic_cd(parameter_s)
2875 self.magic_cd(parameter_s)
2898 dir_s.insert(0,cwd)
2876 dir_s.insert(0,cwd)
2899 return self.magic_dirs()
2877 return self.magic_dirs()
2900
2878
2901 def magic_popd(self, parameter_s=''):
2879 def magic_popd(self, parameter_s=''):
2902 """Change to directory popped off the top of the stack.
2880 """Change to directory popped off the top of the stack.
2903 """
2881 """
2904 if not self.shell.dir_stack:
2882 if not self.shell.dir_stack:
2905 raise UsageError("%popd on empty stack")
2883 raise UsageError("%popd on empty stack")
2906 top = self.shell.dir_stack.pop(0)
2884 top = self.shell.dir_stack.pop(0)
2907 self.magic_cd(top)
2885 self.magic_cd(top)
2908 print "popd ->",top
2886 print "popd ->",top
2909
2887
2910 def magic_dirs(self, parameter_s=''):
2888 def magic_dirs(self, parameter_s=''):
2911 """Return the current directory stack."""
2889 """Return the current directory stack."""
2912
2890
2913 return self.shell.dir_stack
2891 return self.shell.dir_stack
2914
2892
2915 def magic_dhist(self, parameter_s=''):
2893 def magic_dhist(self, parameter_s=''):
2916 """Print your history of visited directories.
2894 """Print your history of visited directories.
2917
2895
2918 %dhist -> print full history\\
2896 %dhist -> print full history\\
2919 %dhist n -> print last n entries only\\
2897 %dhist n -> print last n entries only\\
2920 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2898 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2921
2899
2922 This history is automatically maintained by the %cd command, and
2900 This history is automatically maintained by the %cd command, and
2923 always available as the global list variable _dh. You can use %cd -<n>
2901 always available as the global list variable _dh. You can use %cd -<n>
2924 to go to directory number <n>.
2902 to go to directory number <n>.
2925
2903
2926 Note that most of time, you should view directory history by entering
2904 Note that most of time, you should view directory history by entering
2927 cd -<TAB>.
2905 cd -<TAB>.
2928
2906
2929 """
2907 """
2930
2908
2931 dh = self.shell.user_ns['_dh']
2909 dh = self.shell.user_ns['_dh']
2932 if parameter_s:
2910 if parameter_s:
2933 try:
2911 try:
2934 args = map(int,parameter_s.split())
2912 args = map(int,parameter_s.split())
2935 except:
2913 except:
2936 self.arg_err(Magic.magic_dhist)
2914 self.arg_err(Magic.magic_dhist)
2937 return
2915 return
2938 if len(args) == 1:
2916 if len(args) == 1:
2939 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2917 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2940 elif len(args) == 2:
2918 elif len(args) == 2:
2941 ini,fin = args
2919 ini,fin = args
2942 else:
2920 else:
2943 self.arg_err(Magic.magic_dhist)
2921 self.arg_err(Magic.magic_dhist)
2944 return
2922 return
2945 else:
2923 else:
2946 ini,fin = 0,len(dh)
2924 ini,fin = 0,len(dh)
2947 nlprint(dh,
2925 nlprint(dh,
2948 header = 'Directory history (kept in _dh)',
2926 header = 'Directory history (kept in _dh)',
2949 start=ini,stop=fin)
2927 start=ini,stop=fin)
2950
2928
2951 @testdec.skip_doctest
2929 @testdec.skip_doctest
2952 def magic_sc(self, parameter_s=''):
2930 def magic_sc(self, parameter_s=''):
2953 """Shell capture - execute a shell command and capture its output.
2931 """Shell capture - execute a shell command and capture its output.
2954
2932
2955 DEPRECATED. Suboptimal, retained for backwards compatibility.
2933 DEPRECATED. Suboptimal, retained for backwards compatibility.
2956
2934
2957 You should use the form 'var = !command' instead. Example:
2935 You should use the form 'var = !command' instead. Example:
2958
2936
2959 "%sc -l myfiles = ls ~" should now be written as
2937 "%sc -l myfiles = ls ~" should now be written as
2960
2938
2961 "myfiles = !ls ~"
2939 "myfiles = !ls ~"
2962
2940
2963 myfiles.s, myfiles.l and myfiles.n still apply as documented
2941 myfiles.s, myfiles.l and myfiles.n still apply as documented
2964 below.
2942 below.
2965
2943
2966 --
2944 --
2967 %sc [options] varname=command
2945 %sc [options] varname=command
2968
2946
2969 IPython will run the given command using commands.getoutput(), and
2947 IPython will run the given command using commands.getoutput(), and
2970 will then update the user's interactive namespace with a variable
2948 will then update the user's interactive namespace with a variable
2971 called varname, containing the value of the call. Your command can
2949 called varname, containing the value of the call. Your command can
2972 contain shell wildcards, pipes, etc.
2950 contain shell wildcards, pipes, etc.
2973
2951
2974 The '=' sign in the syntax is mandatory, and the variable name you
2952 The '=' sign in the syntax is mandatory, and the variable name you
2975 supply must follow Python's standard conventions for valid names.
2953 supply must follow Python's standard conventions for valid names.
2976
2954
2977 (A special format without variable name exists for internal use)
2955 (A special format without variable name exists for internal use)
2978
2956
2979 Options:
2957 Options:
2980
2958
2981 -l: list output. Split the output on newlines into a list before
2959 -l: list output. Split the output on newlines into a list before
2982 assigning it to the given variable. By default the output is stored
2960 assigning it to the given variable. By default the output is stored
2983 as a single string.
2961 as a single string.
2984
2962
2985 -v: verbose. Print the contents of the variable.
2963 -v: verbose. Print the contents of the variable.
2986
2964
2987 In most cases you should not need to split as a list, because the
2965 In most cases you should not need to split as a list, because the
2988 returned value is a special type of string which can automatically
2966 returned value is a special type of string which can automatically
2989 provide its contents either as a list (split on newlines) or as a
2967 provide its contents either as a list (split on newlines) or as a
2990 space-separated string. These are convenient, respectively, either
2968 space-separated string. These are convenient, respectively, either
2991 for sequential processing or to be passed to a shell command.
2969 for sequential processing or to be passed to a shell command.
2992
2970
2993 For example:
2971 For example:
2994
2972
2995 # all-random
2973 # all-random
2996
2974
2997 # Capture into variable a
2975 # Capture into variable a
2998 In [1]: sc a=ls *py
2976 In [1]: sc a=ls *py
2999
2977
3000 # a is a string with embedded newlines
2978 # a is a string with embedded newlines
3001 In [2]: a
2979 In [2]: a
3002 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2980 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
3003
2981
3004 # which can be seen as a list:
2982 # which can be seen as a list:
3005 In [3]: a.l
2983 In [3]: a.l
3006 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2984 Out[3]: ['setup.py', 'win32_manual_post_install.py']
3007
2985
3008 # or as a whitespace-separated string:
2986 # or as a whitespace-separated string:
3009 In [4]: a.s
2987 In [4]: a.s
3010 Out[4]: 'setup.py win32_manual_post_install.py'
2988 Out[4]: 'setup.py win32_manual_post_install.py'
3011
2989
3012 # a.s is useful to pass as a single command line:
2990 # a.s is useful to pass as a single command line:
3013 In [5]: !wc -l $a.s
2991 In [5]: !wc -l $a.s
3014 146 setup.py
2992 146 setup.py
3015 130 win32_manual_post_install.py
2993 130 win32_manual_post_install.py
3016 276 total
2994 276 total
3017
2995
3018 # while the list form is useful to loop over:
2996 # while the list form is useful to loop over:
3019 In [6]: for f in a.l:
2997 In [6]: for f in a.l:
3020 ...: !wc -l $f
2998 ...: !wc -l $f
3021 ...:
2999 ...:
3022 146 setup.py
3000 146 setup.py
3023 130 win32_manual_post_install.py
3001 130 win32_manual_post_install.py
3024
3002
3025 Similiarly, the lists returned by the -l option are also special, in
3003 Similiarly, the lists returned by the -l option are also special, in
3026 the sense that you can equally invoke the .s attribute on them to
3004 the sense that you can equally invoke the .s attribute on them to
3027 automatically get a whitespace-separated string from their contents:
3005 automatically get a whitespace-separated string from their contents:
3028
3006
3029 In [7]: sc -l b=ls *py
3007 In [7]: sc -l b=ls *py
3030
3008
3031 In [8]: b
3009 In [8]: b
3032 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3010 Out[8]: ['setup.py', 'win32_manual_post_install.py']
3033
3011
3034 In [9]: b.s
3012 In [9]: b.s
3035 Out[9]: 'setup.py win32_manual_post_install.py'
3013 Out[9]: 'setup.py win32_manual_post_install.py'
3036
3014
3037 In summary, both the lists and strings used for ouptut capture have
3015 In summary, both the lists and strings used for ouptut capture have
3038 the following special attributes:
3016 the following special attributes:
3039
3017
3040 .l (or .list) : value as list.
3018 .l (or .list) : value as list.
3041 .n (or .nlstr): value as newline-separated string.
3019 .n (or .nlstr): value as newline-separated string.
3042 .s (or .spstr): value as space-separated string.
3020 .s (or .spstr): value as space-separated string.
3043 """
3021 """
3044
3022
3045 opts,args = self.parse_options(parameter_s,'lv')
3023 opts,args = self.parse_options(parameter_s,'lv')
3046 # Try to get a variable name and command to run
3024 # Try to get a variable name and command to run
3047 try:
3025 try:
3048 # the variable name must be obtained from the parse_options
3026 # the variable name must be obtained from the parse_options
3049 # output, which uses shlex.split to strip options out.
3027 # output, which uses shlex.split to strip options out.
3050 var,_ = args.split('=',1)
3028 var,_ = args.split('=',1)
3051 var = var.strip()
3029 var = var.strip()
3052 # But the the command has to be extracted from the original input
3030 # But the the command has to be extracted from the original input
3053 # parameter_s, not on what parse_options returns, to avoid the
3031 # parameter_s, not on what parse_options returns, to avoid the
3054 # quote stripping which shlex.split performs on it.
3032 # quote stripping which shlex.split performs on it.
3055 _,cmd = parameter_s.split('=',1)
3033 _,cmd = parameter_s.split('=',1)
3056 except ValueError:
3034 except ValueError:
3057 var,cmd = '',''
3035 var,cmd = '',''
3058 # If all looks ok, proceed
3036 # If all looks ok, proceed
3059 out,err = self.shell.getoutputerror(cmd)
3037 out,err = self.shell.getoutputerror(cmd)
3060 if err:
3038 if err:
3061 print >> Term.cerr,err
3039 print >> Term.cerr,err
3062 if opts.has_key('l'):
3040 if opts.has_key('l'):
3063 out = SList(out.split('\n'))
3041 out = SList(out.split('\n'))
3064 else:
3042 else:
3065 out = LSString(out)
3043 out = LSString(out)
3066 if opts.has_key('v'):
3044 if opts.has_key('v'):
3067 print '%s ==\n%s' % (var,pformat(out))
3045 print '%s ==\n%s' % (var,pformat(out))
3068 if var:
3046 if var:
3069 self.shell.user_ns.update({var:out})
3047 self.shell.user_ns.update({var:out})
3070 else:
3048 else:
3071 return out
3049 return out
3072
3050
3073 def magic_sx(self, parameter_s=''):
3051 def magic_sx(self, parameter_s=''):
3074 """Shell execute - run a shell command and capture its output.
3052 """Shell execute - run a shell command and capture its output.
3075
3053
3076 %sx command
3054 %sx command
3077
3055
3078 IPython will run the given command using commands.getoutput(), and
3056 IPython will run the given command using commands.getoutput(), and
3079 return the result formatted as a list (split on '\\n'). Since the
3057 return the result formatted as a list (split on '\\n'). Since the
3080 output is _returned_, it will be stored in ipython's regular output
3058 output is _returned_, it will be stored in ipython's regular output
3081 cache Out[N] and in the '_N' automatic variables.
3059 cache Out[N] and in the '_N' automatic variables.
3082
3060
3083 Notes:
3061 Notes:
3084
3062
3085 1) If an input line begins with '!!', then %sx is automatically
3063 1) If an input line begins with '!!', then %sx is automatically
3086 invoked. That is, while:
3064 invoked. That is, while:
3087 !ls
3065 !ls
3088 causes ipython to simply issue system('ls'), typing
3066 causes ipython to simply issue system('ls'), typing
3089 !!ls
3067 !!ls
3090 is a shorthand equivalent to:
3068 is a shorthand equivalent to:
3091 %sx ls
3069 %sx ls
3092
3070
3093 2) %sx differs from %sc in that %sx automatically splits into a list,
3071 2) %sx differs from %sc in that %sx automatically splits into a list,
3094 like '%sc -l'. The reason for this is to make it as easy as possible
3072 like '%sc -l'. The reason for this is to make it as easy as possible
3095 to process line-oriented shell output via further python commands.
3073 to process line-oriented shell output via further python commands.
3096 %sc is meant to provide much finer control, but requires more
3074 %sc is meant to provide much finer control, but requires more
3097 typing.
3075 typing.
3098
3076
3099 3) Just like %sc -l, this is a list with special attributes:
3077 3) Just like %sc -l, this is a list with special attributes:
3100
3078
3101 .l (or .list) : value as list.
3079 .l (or .list) : value as list.
3102 .n (or .nlstr): value as newline-separated string.
3080 .n (or .nlstr): value as newline-separated string.
3103 .s (or .spstr): value as whitespace-separated string.
3081 .s (or .spstr): value as whitespace-separated string.
3104
3082
3105 This is very useful when trying to use such lists as arguments to
3083 This is very useful when trying to use such lists as arguments to
3106 system commands."""
3084 system commands."""
3107
3085
3108 if parameter_s:
3086 if parameter_s:
3109 out,err = self.shell.getoutputerror(parameter_s)
3087 out,err = self.shell.getoutputerror(parameter_s)
3110 if err:
3088 if err:
3111 print >> Term.cerr,err
3089 print >> Term.cerr,err
3112 return SList(out.split('\n'))
3090 return SList(out.split('\n'))
3113
3091
3114 def magic_bg(self, parameter_s=''):
3092 def magic_bg(self, parameter_s=''):
3115 """Run a job in the background, in a separate thread.
3093 """Run a job in the background, in a separate thread.
3116
3094
3117 For example,
3095 For example,
3118
3096
3119 %bg myfunc(x,y,z=1)
3097 %bg myfunc(x,y,z=1)
3120
3098
3121 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3099 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
3122 execution starts, a message will be printed indicating the job
3100 execution starts, a message will be printed indicating the job
3123 number. If your job number is 5, you can use
3101 number. If your job number is 5, you can use
3124
3102
3125 myvar = jobs.result(5) or myvar = jobs[5].result
3103 myvar = jobs.result(5) or myvar = jobs[5].result
3126
3104
3127 to assign this result to variable 'myvar'.
3105 to assign this result to variable 'myvar'.
3128
3106
3129 IPython has a job manager, accessible via the 'jobs' object. You can
3107 IPython has a job manager, accessible via the 'jobs' object. You can
3130 type jobs? to get more information about it, and use jobs.<TAB> to see
3108 type jobs? to get more information about it, and use jobs.<TAB> to see
3131 its attributes. All attributes not starting with an underscore are
3109 its attributes. All attributes not starting with an underscore are
3132 meant for public use.
3110 meant for public use.
3133
3111
3134 In particular, look at the jobs.new() method, which is used to create
3112 In particular, look at the jobs.new() method, which is used to create
3135 new jobs. This magic %bg function is just a convenience wrapper
3113 new jobs. This magic %bg function is just a convenience wrapper
3136 around jobs.new(), for expression-based jobs. If you want to create a
3114 around jobs.new(), for expression-based jobs. If you want to create a
3137 new job with an explicit function object and arguments, you must call
3115 new job with an explicit function object and arguments, you must call
3138 jobs.new() directly.
3116 jobs.new() directly.
3139
3117
3140 The jobs.new docstring also describes in detail several important
3118 The jobs.new docstring also describes in detail several important
3141 caveats associated with a thread-based model for background job
3119 caveats associated with a thread-based model for background job
3142 execution. Type jobs.new? for details.
3120 execution. Type jobs.new? for details.
3143
3121
3144 You can check the status of all jobs with jobs.status().
3122 You can check the status of all jobs with jobs.status().
3145
3123
3146 The jobs variable is set by IPython into the Python builtin namespace.
3124 The jobs variable is set by IPython into the Python builtin namespace.
3147 If you ever declare a variable named 'jobs', you will shadow this
3125 If you ever declare a variable named 'jobs', you will shadow this
3148 name. You can either delete your global jobs variable to regain
3126 name. You can either delete your global jobs variable to regain
3149 access to the job manager, or make a new name and assign it manually
3127 access to the job manager, or make a new name and assign it manually
3150 to the manager (stored in IPython's namespace). For example, to
3128 to the manager (stored in IPython's namespace). For example, to
3151 assign the job manager to the Jobs name, use:
3129 assign the job manager to the Jobs name, use:
3152
3130
3153 Jobs = __builtins__.jobs"""
3131 Jobs = __builtins__.jobs"""
3154
3132
3155 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3133 self.shell.jobs.new(parameter_s,self.shell.user_ns)
3156
3134
3157 def magic_r(self, parameter_s=''):
3135 def magic_r(self, parameter_s=''):
3158 """Repeat previous input.
3136 """Repeat previous input.
3159
3137
3160 Note: Consider using the more powerfull %rep instead!
3138 Note: Consider using the more powerfull %rep instead!
3161
3139
3162 If given an argument, repeats the previous command which starts with
3140 If given an argument, repeats the previous command which starts with
3163 the same string, otherwise it just repeats the previous input.
3141 the same string, otherwise it just repeats the previous input.
3164
3142
3165 Shell escaped commands (with ! as first character) are not recognized
3143 Shell escaped commands (with ! as first character) are not recognized
3166 by this system, only pure python code and magic commands.
3144 by this system, only pure python code and magic commands.
3167 """
3145 """
3168
3146
3169 start = parameter_s.strip()
3147 start = parameter_s.strip()
3170 esc_magic = self.shell.ESC_MAGIC
3148 esc_magic = ESC_MAGIC
3171 # Identify magic commands even if automagic is on (which means
3149 # Identify magic commands even if automagic is on (which means
3172 # the in-memory version is different from that typed by the user).
3150 # the in-memory version is different from that typed by the user).
3173 if self.shell.automagic:
3151 if self.shell.automagic:
3174 start_magic = esc_magic+start
3152 start_magic = esc_magic+start
3175 else:
3153 else:
3176 start_magic = start
3154 start_magic = start
3177 # Look through the input history in reverse
3155 # Look through the input history in reverse
3178 for n in range(len(self.shell.input_hist)-2,0,-1):
3156 for n in range(len(self.shell.input_hist)-2,0,-1):
3179 input = self.shell.input_hist[n]
3157 input = self.shell.input_hist[n]
3180 # skip plain 'r' lines so we don't recurse to infinity
3158 # skip plain 'r' lines so we don't recurse to infinity
3181 if input != '_ip.magic("r")\n' and \
3159 if input != '_ip.magic("r")\n' and \
3182 (input.startswith(start) or input.startswith(start_magic)):
3160 (input.startswith(start) or input.startswith(start_magic)):
3183 #print 'match',`input` # dbg
3161 #print 'match',`input` # dbg
3184 print 'Executing:',input,
3162 print 'Executing:',input,
3185 self.shell.runlines(input)
3163 self.shell.runlines(input)
3186 return
3164 return
3187 print 'No previous input matching `%s` found.' % start
3165 print 'No previous input matching `%s` found.' % start
3188
3166
3189
3167
3190 def magic_bookmark(self, parameter_s=''):
3168 def magic_bookmark(self, parameter_s=''):
3191 """Manage IPython's bookmark system.
3169 """Manage IPython's bookmark system.
3192
3170
3193 %bookmark <name> - set bookmark to current dir
3171 %bookmark <name> - set bookmark to current dir
3194 %bookmark <name> <dir> - set bookmark to <dir>
3172 %bookmark <name> <dir> - set bookmark to <dir>
3195 %bookmark -l - list all bookmarks
3173 %bookmark -l - list all bookmarks
3196 %bookmark -d <name> - remove bookmark
3174 %bookmark -d <name> - remove bookmark
3197 %bookmark -r - remove all bookmarks
3175 %bookmark -r - remove all bookmarks
3198
3176
3199 You can later on access a bookmarked folder with:
3177 You can later on access a bookmarked folder with:
3200 %cd -b <name>
3178 %cd -b <name>
3201 or simply '%cd <name>' if there is no directory called <name> AND
3179 or simply '%cd <name>' if there is no directory called <name> AND
3202 there is such a bookmark defined.
3180 there is such a bookmark defined.
3203
3181
3204 Your bookmarks persist through IPython sessions, but they are
3182 Your bookmarks persist through IPython sessions, but they are
3205 associated with each profile."""
3183 associated with each profile."""
3206
3184
3207 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3185 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3208 if len(args) > 2:
3186 if len(args) > 2:
3209 raise UsageError("%bookmark: too many arguments")
3187 raise UsageError("%bookmark: too many arguments")
3210
3188
3211 bkms = self.db.get('bookmarks',{})
3189 bkms = self.db.get('bookmarks',{})
3212
3190
3213 if opts.has_key('d'):
3191 if opts.has_key('d'):
3214 try:
3192 try:
3215 todel = args[0]
3193 todel = args[0]
3216 except IndexError:
3194 except IndexError:
3217 raise UsageError(
3195 raise UsageError(
3218 "%bookmark -d: must provide a bookmark to delete")
3196 "%bookmark -d: must provide a bookmark to delete")
3219 else:
3197 else:
3220 try:
3198 try:
3221 del bkms[todel]
3199 del bkms[todel]
3222 except KeyError:
3200 except KeyError:
3223 raise UsageError(
3201 raise UsageError(
3224 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3202 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3225
3203
3226 elif opts.has_key('r'):
3204 elif opts.has_key('r'):
3227 bkms = {}
3205 bkms = {}
3228 elif opts.has_key('l'):
3206 elif opts.has_key('l'):
3229 bks = bkms.keys()
3207 bks = bkms.keys()
3230 bks.sort()
3208 bks.sort()
3231 if bks:
3209 if bks:
3232 size = max(map(len,bks))
3210 size = max(map(len,bks))
3233 else:
3211 else:
3234 size = 0
3212 size = 0
3235 fmt = '%-'+str(size)+'s -> %s'
3213 fmt = '%-'+str(size)+'s -> %s'
3236 print 'Current bookmarks:'
3214 print 'Current bookmarks:'
3237 for bk in bks:
3215 for bk in bks:
3238 print fmt % (bk,bkms[bk])
3216 print fmt % (bk,bkms[bk])
3239 else:
3217 else:
3240 if not args:
3218 if not args:
3241 raise UsageError("%bookmark: You must specify the bookmark name")
3219 raise UsageError("%bookmark: You must specify the bookmark name")
3242 elif len(args)==1:
3220 elif len(args)==1:
3243 bkms[args[0]] = os.getcwd()
3221 bkms[args[0]] = os.getcwd()
3244 elif len(args)==2:
3222 elif len(args)==2:
3245 bkms[args[0]] = args[1]
3223 bkms[args[0]] = args[1]
3246 self.db['bookmarks'] = bkms
3224 self.db['bookmarks'] = bkms
3247
3225
3248 def magic_pycat(self, parameter_s=''):
3226 def magic_pycat(self, parameter_s=''):
3249 """Show a syntax-highlighted file through a pager.
3227 """Show a syntax-highlighted file through a pager.
3250
3228
3251 This magic is similar to the cat utility, but it will assume the file
3229 This magic is similar to the cat utility, but it will assume the file
3252 to be Python source and will show it with syntax highlighting. """
3230 to be Python source and will show it with syntax highlighting. """
3253
3231
3254 try:
3232 try:
3255 filename = get_py_filename(parameter_s)
3233 filename = get_py_filename(parameter_s)
3256 cont = file_read(filename)
3234 cont = file_read(filename)
3257 except IOError:
3235 except IOError:
3258 try:
3236 try:
3259 cont = eval(parameter_s,self.user_ns)
3237 cont = eval(parameter_s,self.user_ns)
3260 except NameError:
3238 except NameError:
3261 cont = None
3239 cont = None
3262 if cont is None:
3240 if cont is None:
3263 print "Error: no such file or variable"
3241 print "Error: no such file or variable"
3264 return
3242 return
3265
3243
3266 page(self.shell.pycolorize(cont),
3244 page(self.shell.pycolorize(cont),
3267 screen_lines=self.shell.usable_screen_length)
3245 screen_lines=self.shell.usable_screen_length)
3268
3246
3269 def _rerun_pasted(self):
3247 def _rerun_pasted(self):
3270 """ Rerun a previously pasted command.
3248 """ Rerun a previously pasted command.
3271 """
3249 """
3272 b = self.user_ns.get('pasted_block', None)
3250 b = self.user_ns.get('pasted_block', None)
3273 if b is None:
3251 if b is None:
3274 raise UsageError('No previous pasted block available')
3252 raise UsageError('No previous pasted block available')
3275 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3253 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3276 exec b in self.user_ns
3254 exec b in self.user_ns
3277
3255
3278 def _get_pasted_lines(self, sentinel):
3256 def _get_pasted_lines(self, sentinel):
3279 """ Yield pasted lines until the user enters the given sentinel value.
3257 """ Yield pasted lines until the user enters the given sentinel value.
3280 """
3258 """
3281 from IPython.core import iplib
3259 from IPython.core import iplib
3282 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3260 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3283 while True:
3261 while True:
3284 l = iplib.raw_input_original(':')
3262 l = iplib.raw_input_original(':')
3285 if l == sentinel:
3263 if l == sentinel:
3286 return
3264 return
3287 else:
3265 else:
3288 yield l
3266 yield l
3289
3267
3290 def _strip_pasted_lines_for_code(self, raw_lines):
3268 def _strip_pasted_lines_for_code(self, raw_lines):
3291 """ Strip non-code parts of a sequence of lines to return a block of
3269 """ Strip non-code parts of a sequence of lines to return a block of
3292 code.
3270 code.
3293 """
3271 """
3294 # Regular expressions that declare text we strip from the input:
3272 # Regular expressions that declare text we strip from the input:
3295 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3273 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3296 r'^\s*(\s?>)+', # Python input prompt
3274 r'^\s*(\s?>)+', # Python input prompt
3297 r'^\s*\.{3,}', # Continuation prompts
3275 r'^\s*\.{3,}', # Continuation prompts
3298 r'^\++',
3276 r'^\++',
3299 ]
3277 ]
3300
3278
3301 strip_from_start = map(re.compile,strip_re)
3279 strip_from_start = map(re.compile,strip_re)
3302
3280
3303 lines = []
3281 lines = []
3304 for l in raw_lines:
3282 for l in raw_lines:
3305 for pat in strip_from_start:
3283 for pat in strip_from_start:
3306 l = pat.sub('',l)
3284 l = pat.sub('',l)
3307 lines.append(l)
3285 lines.append(l)
3308
3286
3309 block = "\n".join(lines) + '\n'
3287 block = "\n".join(lines) + '\n'
3310 #print "block:\n",block
3288 #print "block:\n",block
3311 return block
3289 return block
3312
3290
3313 def _execute_block(self, block, par):
3291 def _execute_block(self, block, par):
3314 """ Execute a block, or store it in a variable, per the user's request.
3292 """ Execute a block, or store it in a variable, per the user's request.
3315 """
3293 """
3316 if not par:
3294 if not par:
3317 b = textwrap.dedent(block)
3295 b = textwrap.dedent(block)
3318 self.user_ns['pasted_block'] = b
3296 self.user_ns['pasted_block'] = b
3319 exec b in self.user_ns
3297 exec b in self.user_ns
3320 else:
3298 else:
3321 self.user_ns[par] = SList(block.splitlines())
3299 self.user_ns[par] = SList(block.splitlines())
3322 print "Block assigned to '%s'" % par
3300 print "Block assigned to '%s'" % par
3323
3301
3324 def magic_cpaste(self, parameter_s=''):
3302 def magic_cpaste(self, parameter_s=''):
3325 """Allows you to paste & execute a pre-formatted code block from clipboard.
3303 """Allows you to paste & execute a pre-formatted code block from clipboard.
3326
3304
3327 You must terminate the block with '--' (two minus-signs) alone on the
3305 You must terminate the block with '--' (two minus-signs) alone on the
3328 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3306 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
3329 is the new sentinel for this operation)
3307 is the new sentinel for this operation)
3330
3308
3331 The block is dedented prior to execution to enable execution of method
3309 The block is dedented prior to execution to enable execution of method
3332 definitions. '>' and '+' characters at the beginning of a line are
3310 definitions. '>' and '+' characters at the beginning of a line are
3333 ignored, to allow pasting directly from e-mails, diff files and
3311 ignored, to allow pasting directly from e-mails, diff files and
3334 doctests (the '...' continuation prompt is also stripped). The
3312 doctests (the '...' continuation prompt is also stripped). The
3335 executed block is also assigned to variable named 'pasted_block' for
3313 executed block is also assigned to variable named 'pasted_block' for
3336 later editing with '%edit pasted_block'.
3314 later editing with '%edit pasted_block'.
3337
3315
3338 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3316 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
3339 This assigns the pasted block to variable 'foo' as string, without
3317 This assigns the pasted block to variable 'foo' as string, without
3340 dedenting or executing it (preceding >>> and + is still stripped)
3318 dedenting or executing it (preceding >>> and + is still stripped)
3341
3319
3342 '%cpaste -r' re-executes the block previously entered by cpaste.
3320 '%cpaste -r' re-executes the block previously entered by cpaste.
3343
3321
3344 Do not be alarmed by garbled output on Windows (it's a readline bug).
3322 Do not be alarmed by garbled output on Windows (it's a readline bug).
3345 Just press enter and type -- (and press enter again) and the block
3323 Just press enter and type -- (and press enter again) and the block
3346 will be what was just pasted.
3324 will be what was just pasted.
3347
3325
3348 IPython statements (magics, shell escapes) are not supported (yet).
3326 IPython statements (magics, shell escapes) are not supported (yet).
3349
3327
3350 See also
3328 See also
3351 --------
3329 --------
3352 paste: automatically pull code from clipboard.
3330 paste: automatically pull code from clipboard.
3353 """
3331 """
3354
3332
3355 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3333 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
3356 par = args.strip()
3334 par = args.strip()
3357 if opts.has_key('r'):
3335 if opts.has_key('r'):
3358 self._rerun_pasted()
3336 self._rerun_pasted()
3359 return
3337 return
3360
3338
3361 sentinel = opts.get('s','--')
3339 sentinel = opts.get('s','--')
3362
3340
3363 block = self._strip_pasted_lines_for_code(
3341 block = self._strip_pasted_lines_for_code(
3364 self._get_pasted_lines(sentinel))
3342 self._get_pasted_lines(sentinel))
3365
3343
3366 self._execute_block(block, par)
3344 self._execute_block(block, par)
3367
3345
3368 def magic_paste(self, parameter_s=''):
3346 def magic_paste(self, parameter_s=''):
3369 """Allows you to paste & execute a pre-formatted code block from clipboard.
3347 """Allows you to paste & execute a pre-formatted code block from clipboard.
3370
3348
3371 The text is pulled directly from the clipboard without user
3349 The text is pulled directly from the clipboard without user
3372 intervention and printed back on the screen before execution (unless
3350 intervention and printed back on the screen before execution (unless
3373 the -q flag is given to force quiet mode).
3351 the -q flag is given to force quiet mode).
3374
3352
3375 The block is dedented prior to execution to enable execution of method
3353 The block is dedented prior to execution to enable execution of method
3376 definitions. '>' and '+' characters at the beginning of a line are
3354 definitions. '>' and '+' characters at the beginning of a line are
3377 ignored, to allow pasting directly from e-mails, diff files and
3355 ignored, to allow pasting directly from e-mails, diff files and
3378 doctests (the '...' continuation prompt is also stripped). The
3356 doctests (the '...' continuation prompt is also stripped). The
3379 executed block is also assigned to variable named 'pasted_block' for
3357 executed block is also assigned to variable named 'pasted_block' for
3380 later editing with '%edit pasted_block'.
3358 later editing with '%edit pasted_block'.
3381
3359
3382 You can also pass a variable name as an argument, e.g. '%paste foo'.
3360 You can also pass a variable name as an argument, e.g. '%paste foo'.
3383 This assigns the pasted block to variable 'foo' as string, without
3361 This assigns the pasted block to variable 'foo' as string, without
3384 dedenting or executing it (preceding >>> and + is still stripped)
3362 dedenting or executing it (preceding >>> and + is still stripped)
3385
3363
3386 Options
3364 Options
3387 -------
3365 -------
3388
3366
3389 -r: re-executes the block previously entered by cpaste.
3367 -r: re-executes the block previously entered by cpaste.
3390
3368
3391 -q: quiet mode: do not echo the pasted text back to the terminal.
3369 -q: quiet mode: do not echo the pasted text back to the terminal.
3392
3370
3393 IPython statements (magics, shell escapes) are not supported (yet).
3371 IPython statements (magics, shell escapes) are not supported (yet).
3394
3372
3395 See also
3373 See also
3396 --------
3374 --------
3397 cpaste: manually paste code into terminal until you mark its end.
3375 cpaste: manually paste code into terminal until you mark its end.
3398 """
3376 """
3399 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3377 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3400 par = args.strip()
3378 par = args.strip()
3401 if opts.has_key('r'):
3379 if opts.has_key('r'):
3402 self._rerun_pasted()
3380 self._rerun_pasted()
3403 return
3381 return
3404
3382
3405 text = self.shell.hooks.clipboard_get()
3383 text = self.shell.hooks.clipboard_get()
3406 block = self._strip_pasted_lines_for_code(text.splitlines())
3384 block = self._strip_pasted_lines_for_code(text.splitlines())
3407
3385
3408 # By default, echo back to terminal unless quiet mode is requested
3386 # By default, echo back to terminal unless quiet mode is requested
3409 if not opts.has_key('q'):
3387 if not opts.has_key('q'):
3410 write = self.shell.write
3388 write = self.shell.write
3411 write(block)
3389 write(block)
3412 if not block.endswith('\n'):
3390 if not block.endswith('\n'):
3413 write('\n')
3391 write('\n')
3414 write("## -- End pasted text --\n")
3392 write("## -- End pasted text --\n")
3415
3393
3416 self._execute_block(block, par)
3394 self._execute_block(block, par)
3417
3395
3418 def magic_quickref(self,arg):
3396 def magic_quickref(self,arg):
3419 """ Show a quick reference sheet """
3397 """ Show a quick reference sheet """
3420 import IPython.core.usage
3398 import IPython.core.usage
3421 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3399 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3422
3400
3423 page(qr)
3401 page(qr)
3424
3402
3425 def magic_upgrade(self,arg):
3403 def magic_upgrade(self,arg):
3426 """ Upgrade your IPython installation
3404 """ Upgrade your IPython installation
3427
3405
3428 This will copy the config files that don't yet exist in your
3406 This will copy the config files that don't yet exist in your
3429 ipython dir from the system config dir. Use this after upgrading
3407 ipython dir from the system config dir. Use this after upgrading
3430 IPython if you don't wish to delete your .ipython dir.
3408 IPython if you don't wish to delete your .ipython dir.
3431
3409
3432 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3410 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3433 new users)
3411 new users)
3434
3412
3435 """
3413 """
3436 ip = self.getapi()
3414 ip = self.getapi()
3437 ipinstallation = path(IPython.__file__).dirname()
3415 ipinstallation = path(IPython.__file__).dirname()
3438 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'utils' / 'upgradedir.py')
3416 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'utils' / 'upgradedir.py')
3439 src_config = ipinstallation / 'config' / 'userconfig'
3417 src_config = ipinstallation / 'config' / 'userconfig'
3440 userdir = path(ip.config.IPYTHONDIR)
3418 userdir = path(ip.config.IPYTHONDIR)
3441 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3419 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3442 print ">",cmd
3420 print ">",cmd
3443 shell(cmd)
3421 shell(cmd)
3444 if arg == '-nolegacy':
3422 if arg == '-nolegacy':
3445 legacy = userdir.files('ipythonrc*')
3423 legacy = userdir.files('ipythonrc*')
3446 print "Nuking legacy files:",legacy
3424 print "Nuking legacy files:",legacy
3447
3425
3448 [p.remove() for p in legacy]
3426 [p.remove() for p in legacy]
3449 suffix = (sys.platform == 'win32' and '.ini' or '')
3427 suffix = (sys.platform == 'win32' and '.ini' or '')
3450 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3428 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3451
3429
3452
3430
3453 def magic_doctest_mode(self,parameter_s=''):
3431 def magic_doctest_mode(self,parameter_s=''):
3454 """Toggle doctest mode on and off.
3432 """Toggle doctest mode on and off.
3455
3433
3456 This mode allows you to toggle the prompt behavior between normal
3434 This mode allows you to toggle the prompt behavior between normal
3457 IPython prompts and ones that are as similar to the default IPython
3435 IPython prompts and ones that are as similar to the default IPython
3458 interpreter as possible.
3436 interpreter as possible.
3459
3437
3460 It also supports the pasting of code snippets that have leading '>>>'
3438 It also supports the pasting of code snippets that have leading '>>>'
3461 and '...' prompts in them. This means that you can paste doctests from
3439 and '...' prompts in them. This means that you can paste doctests from
3462 files or docstrings (even if they have leading whitespace), and the
3440 files or docstrings (even if they have leading whitespace), and the
3463 code will execute correctly. You can then use '%history -tn' to see
3441 code will execute correctly. You can then use '%history -tn' to see
3464 the translated history without line numbers; this will give you the
3442 the translated history without line numbers; this will give you the
3465 input after removal of all the leading prompts and whitespace, which
3443 input after removal of all the leading prompts and whitespace, which
3466 can be pasted back into an editor.
3444 can be pasted back into an editor.
3467
3445
3468 With these features, you can switch into this mode easily whenever you
3446 With these features, you can switch into this mode easily whenever you
3469 need to do testing and changes to doctests, without having to leave
3447 need to do testing and changes to doctests, without having to leave
3470 your existing IPython session.
3448 your existing IPython session.
3471 """
3449 """
3472
3450
3473 # XXX - Fix this to have cleaner activate/deactivate calls.
3451 # XXX - Fix this to have cleaner activate/deactivate calls.
3474 from IPython.extensions import InterpreterPasteInput as ipaste
3452 from IPython.extensions import InterpreterPasteInput as ipaste
3475 from IPython.utils.ipstruct import Struct
3453 from IPython.utils.ipstruct import Struct
3476
3454
3477 # Shorthands
3455 # Shorthands
3478 shell = self.shell
3456 shell = self.shell
3479 oc = shell.outputcache
3457 oc = shell.outputcache
3480 meta = shell.meta
3458 meta = shell.meta
3481 # dstore is a data store kept in the instance metadata bag to track any
3459 # dstore is a data store kept in the instance metadata bag to track any
3482 # changes we make, so we can undo them later.
3460 # changes we make, so we can undo them later.
3483 dstore = meta.setdefault('doctest_mode',Struct())
3461 dstore = meta.setdefault('doctest_mode',Struct())
3484 save_dstore = dstore.setdefault
3462 save_dstore = dstore.setdefault
3485
3463
3486 # save a few values we'll need to recover later
3464 # save a few values we'll need to recover later
3487 mode = save_dstore('mode',False)
3465 mode = save_dstore('mode',False)
3488 save_dstore('rc_pprint',shell.pprint)
3466 save_dstore('rc_pprint',shell.pprint)
3489 save_dstore('xmode',shell.InteractiveTB.mode)
3467 save_dstore('xmode',shell.InteractiveTB.mode)
3490 save_dstore('rc_separate_out',shell.separate_out)
3468 save_dstore('rc_separate_out',shell.separate_out)
3491 save_dstore('rc_separate_out2',shell.separate_out2)
3469 save_dstore('rc_separate_out2',shell.separate_out2)
3492 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3470 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3493 save_dstore('rc_separate_in',shell.separate_in)
3471 save_dstore('rc_separate_in',shell.separate_in)
3494
3472
3495 if mode == False:
3473 if mode == False:
3496 # turn on
3474 # turn on
3497 ipaste.activate_prefilter()
3475 ipaste.activate_prefilter()
3498
3476
3499 oc.prompt1.p_template = '>>> '
3477 oc.prompt1.p_template = '>>> '
3500 oc.prompt2.p_template = '... '
3478 oc.prompt2.p_template = '... '
3501 oc.prompt_out.p_template = ''
3479 oc.prompt_out.p_template = ''
3502
3480
3503 # Prompt separators like plain python
3481 # Prompt separators like plain python
3504 oc.input_sep = oc.prompt1.sep = ''
3482 oc.input_sep = oc.prompt1.sep = ''
3505 oc.output_sep = ''
3483 oc.output_sep = ''
3506 oc.output_sep2 = ''
3484 oc.output_sep2 = ''
3507
3485
3508 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3486 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3509 oc.prompt_out.pad_left = False
3487 oc.prompt_out.pad_left = False
3510
3488
3511 shell.pprint = False
3489 shell.pprint = False
3512
3490
3513 shell.magic_xmode('Plain')
3491 shell.magic_xmode('Plain')
3514
3492
3515 else:
3493 else:
3516 # turn off
3494 # turn off
3517 ipaste.deactivate_prefilter()
3495 ipaste.deactivate_prefilter()
3518
3496
3519 oc.prompt1.p_template = shell.prompt_in1
3497 oc.prompt1.p_template = shell.prompt_in1
3520 oc.prompt2.p_template = shell.prompt_in2
3498 oc.prompt2.p_template = shell.prompt_in2
3521 oc.prompt_out.p_template = shell.prompt_out
3499 oc.prompt_out.p_template = shell.prompt_out
3522
3500
3523 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3501 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3524
3502
3525 oc.output_sep = dstore.rc_separate_out
3503 oc.output_sep = dstore.rc_separate_out
3526 oc.output_sep2 = dstore.rc_separate_out2
3504 oc.output_sep2 = dstore.rc_separate_out2
3527
3505
3528 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3506 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3529 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3507 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3530
3508
3531 rc.pprint = dstore.rc_pprint
3509 rc.pprint = dstore.rc_pprint
3532
3510
3533 shell.magic_xmode(dstore.xmode)
3511 shell.magic_xmode(dstore.xmode)
3534
3512
3535 # Store new mode and inform
3513 # Store new mode and inform
3536 dstore.mode = bool(1-int(mode))
3514 dstore.mode = bool(1-int(mode))
3537 print 'Doctest mode is:',
3515 print 'Doctest mode is:',
3538 print ['OFF','ON'][dstore.mode]
3516 print ['OFF','ON'][dstore.mode]
3539
3517
3540 def magic_gui(self, parameter_s=''):
3518 def magic_gui(self, parameter_s=''):
3541 """Enable or disable IPython GUI event loop integration.
3519 """Enable or disable IPython GUI event loop integration.
3542
3520
3543 %gui [-a] [GUINAME]
3521 %gui [-a] [GUINAME]
3544
3522
3545 This magic replaces IPython's threaded shells that were activated
3523 This magic replaces IPython's threaded shells that were activated
3546 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3524 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3547 can now be enabled, disabled and swtiched at runtime and keyboard
3525 can now be enabled, disabled and swtiched at runtime and keyboard
3548 interrupts should work without any problems. The following toolkits
3526 interrupts should work without any problems. The following toolkits
3549 are supports: wxPython, PyQt4, PyGTK, and Tk::
3527 are supports: wxPython, PyQt4, PyGTK, and Tk::
3550
3528
3551 %gui wx # enable wxPython event loop integration
3529 %gui wx # enable wxPython event loop integration
3552 %gui qt4|qt # enable PyQt4 event loop integration
3530 %gui qt4|qt # enable PyQt4 event loop integration
3553 %gui gtk # enable PyGTK event loop integration
3531 %gui gtk # enable PyGTK event loop integration
3554 %gui tk # enable Tk event loop integration
3532 %gui tk # enable Tk event loop integration
3555 %gui # disable all event loop integration
3533 %gui # disable all event loop integration
3556
3534
3557 WARNING: after any of these has been called you can simply create
3535 WARNING: after any of these has been called you can simply create
3558 an application object, but DO NOT start the event loop yourself, as
3536 an application object, but DO NOT start the event loop yourself, as
3559 we have already handled that.
3537 we have already handled that.
3560
3538
3561 If you want us to create an appropriate application object add the
3539 If you want us to create an appropriate application object add the
3562 "-a" flag to your command::
3540 "-a" flag to your command::
3563
3541
3564 %gui -a wx
3542 %gui -a wx
3565
3543
3566 This is highly recommended for most users.
3544 This is highly recommended for most users.
3567 """
3545 """
3568 from IPython.lib import inputhook
3546 from IPython.lib import inputhook
3569 if "-a" in parameter_s:
3547 if "-a" in parameter_s:
3570 app = True
3548 app = True
3571 else:
3549 else:
3572 app = False
3550 app = False
3573 if not parameter_s:
3551 if not parameter_s:
3574 inputhook.clear_inputhook()
3552 inputhook.clear_inputhook()
3575 elif 'wx' in parameter_s:
3553 elif 'wx' in parameter_s:
3576 return inputhook.enable_wx(app)
3554 return inputhook.enable_wx(app)
3577 elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3555 elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3578 return inputhook.enable_qt4(app)
3556 return inputhook.enable_qt4(app)
3579 elif 'gtk' in parameter_s:
3557 elif 'gtk' in parameter_s:
3580 return inputhook.enable_gtk(app)
3558 return inputhook.enable_gtk(app)
3581 elif 'tk' in parameter_s:
3559 elif 'tk' in parameter_s:
3582 return inputhook.enable_tk(app)
3560 return inputhook.enable_tk(app)
3583
3561
3584
3562
3585 # end Magic
3563 # end Magic
@@ -1,777 +1,777 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Prefiltering components.
4 Prefiltering components.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez
9 * Fernando Perez
10 * Dan Milstein
10 * Dan Milstein
11 """
11 """
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Copyright (C) 2008-2009 The IPython Development Team
14 # Copyright (C) 2008-2009 The IPython Development Team
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Imports
21 # Imports
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 import __builtin__
24 import __builtin__
25 import codeop
25 import codeop
26 import keyword
26 import keyword
27 import os
27 import os
28 import re
28 import re
29 import sys
29 import sys
30
30
31 from IPython.core.alias import AliasManager
31 from IPython.core.alias import AliasManager
32 from IPython.core.autocall import IPyAutocall
32 from IPython.core.autocall import IPyAutocall
33 from IPython.core.component import Component
33 from IPython.core.component import Component
34 from IPython.core.splitinput import split_user_input
34 from IPython.core.splitinput import split_user_input
35
35
36 from IPython.utils.traitlets import List, Int, Any, Str, CBool
36 from IPython.utils.traitlets import List, Int, Any, Str, CBool
37 from IPython.utils.genutils import make_quoted_expr
37 from IPython.utils.genutils import make_quoted_expr
38 from IPython.utils.autoattr import auto_attr
38 from IPython.utils.autoattr import auto_attr
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Global utilities, errors and constants
41 # Global utilities, errors and constants
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44
44
45 ESC_SHELL = '!'
45 ESC_SHELL = '!'
46 ESC_SH_CAP = '!!'
46 ESC_SH_CAP = '!!'
47 ESC_HELP = '?'
47 ESC_HELP = '?'
48 ESC_MAGIC = '%'
48 ESC_MAGIC = '%'
49 ESC_QUOTE = ','
49 ESC_QUOTE = ','
50 ESC_QUOTE2 = ';'
50 ESC_QUOTE2 = ';'
51 ESC_PAREN = '/'
51 ESC_PAREN = '/'
52
52
53
53
54 class PrefilterError(Exception):
54 class PrefilterError(Exception):
55 pass
55 pass
56
56
57
57
58 # RegExp to identify potential function names
58 # RegExp to identify potential function names
59 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
59 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
60
60
61 # RegExp to exclude strings with this start from autocalling. In
61 # RegExp to exclude strings with this start from autocalling. In
62 # particular, all binary operators should be excluded, so that if foo is
62 # particular, all binary operators should be excluded, so that if foo is
63 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
63 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
64 # characters '!=()' don't need to be checked for, as the checkPythonChars
64 # characters '!=()' don't need to be checked for, as the checkPythonChars
65 # routine explicitely does so, to catch direct calls and rebindings of
65 # routine explicitely does so, to catch direct calls and rebindings of
66 # existing names.
66 # existing names.
67
67
68 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
68 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
69 # it affects the rest of the group in square brackets.
69 # it affects the rest of the group in square brackets.
70 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
70 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
71 r'|^is |^not |^in |^and |^or ')
71 r'|^is |^not |^in |^and |^or ')
72
72
73 # try to catch also methods for stuff in lists/tuples/dicts: off
73 # try to catch also methods for stuff in lists/tuples/dicts: off
74 # (experimental). For this to work, the line_split regexp would need
74 # (experimental). For this to work, the line_split regexp would need
75 # to be modified so it wouldn't break things at '['. That line is
75 # to be modified so it wouldn't break things at '['. That line is
76 # nasty enough that I shouldn't change it until I can test it _well_.
76 # nasty enough that I shouldn't change it until I can test it _well_.
77 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
77 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
78
78
79
79
80 # Handler Check Utilities
80 # Handler Check Utilities
81 def is_shadowed(identifier, ip):
81 def is_shadowed(identifier, ip):
82 """Is the given identifier defined in one of the namespaces which shadow
82 """Is the given identifier defined in one of the namespaces which shadow
83 the alias and magic namespaces? Note that an identifier is different
83 the alias and magic namespaces? Note that an identifier is different
84 than ifun, because it can not contain a '.' character."""
84 than ifun, because it can not contain a '.' character."""
85 # This is much safer than calling ofind, which can change state
85 # This is much safer than calling ofind, which can change state
86 return (identifier in ip.user_ns \
86 return (identifier in ip.user_ns \
87 or identifier in ip.internal_ns \
87 or identifier in ip.internal_ns \
88 or identifier in ip.ns_table['builtin'])
88 or identifier in ip.ns_table['builtin'])
89
89
90
90
91 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
92 # The LineInfo class used throughout
92 # The LineInfo class used throughout
93 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
94
94
95
95
96 class LineInfo(object):
96 class LineInfo(object):
97 """A single line of input and associated info.
97 """A single line of input and associated info.
98
98
99 Includes the following as properties:
99 Includes the following as properties:
100
100
101 line
101 line
102 The original, raw line
102 The original, raw line
103
103
104 continue_prompt
104 continue_prompt
105 Is this line a continuation in a sequence of multiline input?
105 Is this line a continuation in a sequence of multiline input?
106
106
107 pre
107 pre
108 The initial esc character or whitespace.
108 The initial esc character or whitespace.
109
109
110 pre_char
110 pre_char
111 The escape character(s) in pre or the empty string if there isn't one.
111 The escape character(s) in pre or the empty string if there isn't one.
112 Note that '!!' is a possible value for pre_char. Otherwise it will
112 Note that '!!' is a possible value for pre_char. Otherwise it will
113 always be a single character.
113 always be a single character.
114
114
115 pre_whitespace
115 pre_whitespace
116 The leading whitespace from pre if it exists. If there is a pre_char,
116 The leading whitespace from pre if it exists. If there is a pre_char,
117 this is just ''.
117 this is just ''.
118
118
119 ifun
119 ifun
120 The 'function part', which is basically the maximal initial sequence
120 The 'function part', which is basically the maximal initial sequence
121 of valid python identifiers and the '.' character. This is what is
121 of valid python identifiers and the '.' character. This is what is
122 checked for alias and magic transformations, used for auto-calling,
122 checked for alias and magic transformations, used for auto-calling,
123 etc.
123 etc.
124
124
125 the_rest
125 the_rest
126 Everything else on the line.
126 Everything else on the line.
127 """
127 """
128 def __init__(self, line, continue_prompt):
128 def __init__(self, line, continue_prompt):
129 self.line = line
129 self.line = line
130 self.continue_prompt = continue_prompt
130 self.continue_prompt = continue_prompt
131 self.pre, self.ifun, self.the_rest = split_user_input(line)
131 self.pre, self.ifun, self.the_rest = split_user_input(line)
132
132
133 self.pre_char = self.pre.strip()
133 self.pre_char = self.pre.strip()
134 if self.pre_char:
134 if self.pre_char:
135 self.pre_whitespace = '' # No whitespace allowd before esc chars
135 self.pre_whitespace = '' # No whitespace allowd before esc chars
136 else:
136 else:
137 self.pre_whitespace = self.pre
137 self.pre_whitespace = self.pre
138
138
139 self._oinfo = None
139 self._oinfo = None
140
140
141 def ofind(self, ip):
141 def ofind(self, ip):
142 """Do a full, attribute-walking lookup of the ifun in the various
142 """Do a full, attribute-walking lookup of the ifun in the various
143 namespaces for the given IPython InteractiveShell instance.
143 namespaces for the given IPython InteractiveShell instance.
144
144
145 Return a dict with keys: found,obj,ospace,ismagic
145 Return a dict with keys: found,obj,ospace,ismagic
146
146
147 Note: can cause state changes because of calling getattr, but should
147 Note: can cause state changes because of calling getattr, but should
148 only be run if autocall is on and if the line hasn't matched any
148 only be run if autocall is on and if the line hasn't matched any
149 other, less dangerous handlers.
149 other, less dangerous handlers.
150
150
151 Does cache the results of the call, so can be called multiple times
151 Does cache the results of the call, so can be called multiple times
152 without worrying about *further* damaging state.
152 without worrying about *further* damaging state.
153 """
153 """
154 if not self._oinfo:
154 if not self._oinfo:
155 self._oinfo = ip._ofind(self.ifun)
155 self._oinfo = ip._ofind(self.ifun)
156 return self._oinfo
156 return self._oinfo
157
157
158 def __str__(self):
158 def __str__(self):
159 return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest)
159 return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest)
160
160
161
161
162 #-----------------------------------------------------------------------------
162 #-----------------------------------------------------------------------------
163 # Main Prefilter manager
163 # Main Prefilter manager
164 #-----------------------------------------------------------------------------
164 #-----------------------------------------------------------------------------
165
165
166
166
167 class PrefilterManager(Component):
167 class PrefilterManager(Component):
168 """Main prefilter component.
168 """Main prefilter component.
169
169
170 The IPython prefilter is run on all user input before it is run. The
170 The IPython prefilter is run on all user input before it is run. The
171 prefilter consumes lines of input and produces transformed lines of
171 prefilter consumes lines of input and produces transformed lines of
172 input. The implementation consists of checkers and handlers. The
172 input. The implementation consists of checkers and handlers. The
173 checkers inspect the input line and select which handler will be used
173 checkers inspect the input line and select which handler will be used
174 to transform the input line.
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 def __init__(self, parent, config=None):
179 def __init__(self, parent, config=None):
180 super(PrefilterManager, self).__init__(parent, config=config)
180 super(PrefilterManager, self).__init__(parent, config=config)
181 self.init_handlers()
181 self.init_handlers()
182 self.init_checkers()
182 self.init_checkers()
183
183
184 @auto_attr
184 @auto_attr
185 def shell(self):
185 def shell(self):
186 shell = Component.get_instances(
186 shell = Component.get_instances(
187 root=self.root,
187 root=self.root,
188 klass='IPython.core.iplib.InteractiveShell'
188 klass='IPython.core.iplib.InteractiveShell'
189 )[0]
189 )[0]
190 return shell
190 return shell
191
191
192 def init_checkers(self):
192 def init_checkers(self):
193 self._checkers = []
193 self._checkers = []
194 for checker in _default_checkers:
194 for checker in _default_checkers:
195 self._checkers.append(checker(self, config=self.config))
195 self._checkers.append(checker(self, config=self.config))
196
196
197 def init_handlers(self):
197 def init_handlers(self):
198 self._handlers = {}
198 self._handlers = {}
199 self._esc_handlers = {}
199 self._esc_handlers = {}
200 for handler in _default_handlers:
200 for handler in _default_handlers:
201 handler(self, config=self.config)
201 handler(self, config=self.config)
202
202
203 @property
203 @property
204 def sorted_checkers(self):
204 def sorted_checkers(self):
205 """Return a list of checkers, sorted by priority."""
205 """Return a list of checkers, sorted by priority."""
206 return sorted(self._checkers, cmp=lambda x,y: x.priority-y.priority)
206 return sorted(self._checkers, cmp=lambda x,y: x.priority-y.priority)
207
207
208 def register_handler(self, name, handler, esc_strings):
208 def register_handler(self, name, handler, esc_strings):
209 """Register a handler instance by name with esc_strings."""
209 """Register a handler instance by name with esc_strings."""
210 self._handlers[name] = handler
210 self._handlers[name] = handler
211 for esc_str in esc_strings:
211 for esc_str in esc_strings:
212 self._esc_handlers[esc_str] = handler
212 self._esc_handlers[esc_str] = handler
213
213
214 def unregister_handler(self, name, handler, esc_strings):
214 def unregister_handler(self, name, handler, esc_strings):
215 """Unregister a handler instance by name with esc_strings."""
215 """Unregister a handler instance by name with esc_strings."""
216 try:
216 try:
217 del self._handlers[name]
217 del self._handlers[name]
218 except KeyError:
218 except KeyError:
219 pass
219 pass
220 for esc_str in esc_strings:
220 for esc_str in esc_strings:
221 h = self._esc_handlers.get(esc_str)
221 h = self._esc_handlers.get(esc_str)
222 if h is handler:
222 if h is handler:
223 del self._esc_handlers[esc_str]
223 del self._esc_handlers[esc_str]
224
224
225 def get_handler_by_name(self, name):
225 def get_handler_by_name(self, name):
226 """Get a handler by its name."""
226 """Get a handler by its name."""
227 return self._handlers.get(name)
227 return self._handlers.get(name)
228
228
229 def get_handler_by_esc(self, esc_str):
229 def get_handler_by_esc(self, esc_str):
230 """Get a handler by its escape string."""
230 """Get a handler by its escape string."""
231 return self._esc_handlers.get(esc_str)
231 return self._esc_handlers.get(esc_str)
232
232
233 def prefilter_line_info(self, line_info):
233 def prefilter_line_info(self, line_info):
234 """Prefilter a line that has been converted to a LineInfo object."""
234 """Prefilter a line that has been converted to a LineInfo object."""
235 handler = self.find_handler(line_info)
235 handler = self.find_handler(line_info)
236 return handler.handle(line_info)
236 return handler.handle(line_info)
237
237
238 def find_handler(self, line_info):
238 def find_handler(self, line_info):
239 """Find a handler for the line_info by trying checkers."""
239 """Find a handler for the line_info by trying checkers."""
240 for checker in self.sorted_checkers:
240 for checker in self.sorted_checkers:
241 handler = checker.check(line_info)
241 handler = checker.check(line_info)
242 if handler:
242 if handler:
243 return handler
243 return handler
244 return self.get_handler_by_name('normal')
244 return self.get_handler_by_name('normal')
245
245
246 def prefilter_line(self, line, continue_prompt):
246 def prefilter_line(self, line, continue_prompt):
247 """Prefilter a single input line as text."""
247 """Prefilter a single input line as text."""
248
248
249 # All handlers *must* return a value, even if it's blank ('').
249 # All handlers *must* return a value, even if it's blank ('').
250
250
251 # Lines are NOT logged here. Handlers should process the line as
251 # Lines are NOT logged here. Handlers should process the line as
252 # needed, update the cache AND log it (so that the input cache array
252 # needed, update the cache AND log it (so that the input cache array
253 # stays synced).
253 # stays synced).
254
254
255 # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt))
255 # growl.notify("_prefilter: ", "line = %s\ncontinue_prompt = %s" % (line, continue_prompt))
256
256
257 # save the line away in case we crash, so the post-mortem handler can
257 # save the line away in case we crash, so the post-mortem handler can
258 # record it
258 # record it
259 self.shell._last_input_line = line
259 self.shell._last_input_line = line
260
260
261 if not line:
261 if not line:
262 # Return immediately on purely empty lines, so that if the user
262 # Return immediately on purely empty lines, so that if the user
263 # previously typed some whitespace that started a continuation
263 # previously typed some whitespace that started a continuation
264 # prompt, he can break out of that loop with just an empty line.
264 # prompt, he can break out of that loop with just an empty line.
265 # This is how the default python prompt works.
265 # This is how the default python prompt works.
266
266
267 # Only return if the accumulated input buffer was just whitespace!
267 # Only return if the accumulated input buffer was just whitespace!
268 if ''.join(self.shell.buffer).isspace():
268 if ''.join(self.shell.buffer).isspace():
269 self.shell.buffer[:] = []
269 self.shell.buffer[:] = []
270 return ''
270 return ''
271
271
272 line_info = LineInfo(line, continue_prompt)
272 line_info = LineInfo(line, continue_prompt)
273
273
274 # the input history needs to track even empty lines
274 # the input history needs to track even empty lines
275 stripped = line.strip()
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 if not stripped:
278 if not stripped:
279 if not continue_prompt:
279 if not continue_prompt:
280 self.shell.outputcache.prompt_count -= 1
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 # special handlers are only allowed for single line statements
284 # special handlers are only allowed for single line statements
285 if continue_prompt and not self.multi_line_specials:
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 return self.prefilter_line_info(line_info)
288 return self.prefilter_line_info(line_info)
289
289
290 def prefilter_lines(self, lines, continue_prompt):
290 def prefilter_lines(self, lines, continue_prompt):
291 """Prefilter multiple input lines of text.
291 """Prefilter multiple input lines of text.
292
292
293 Covers cases where there are multiple lines in the user entry,
293 Covers cases where there are multiple lines in the user entry,
294 which is the case when the user goes back to a multiline history
294 which is the case when the user goes back to a multiline history
295 entry and presses enter.
295 entry and presses enter.
296 """
296 """
297 # growl.notify("multiline_prefilter: ", "%s\n%s" % (line, continue_prompt))
297 # growl.notify("multiline_prefilter: ", "%s\n%s" % (line, continue_prompt))
298 out = []
298 out = []
299 for line in lines.rstrip('\n').split('\n'):
299 for line in lines.rstrip('\n').split('\n'):
300 out.append(self.prefilter_line(line, continue_prompt))
300 out.append(self.prefilter_line(line, continue_prompt))
301 # growl.notify("multiline_prefilter return: ", '\n'.join(out))
301 # growl.notify("multiline_prefilter return: ", '\n'.join(out))
302 return '\n'.join(out)
302 return '\n'.join(out)
303
303
304
304
305 #-----------------------------------------------------------------------------
305 #-----------------------------------------------------------------------------
306 # Prefilter checkers
306 # Prefilter checkers
307 #-----------------------------------------------------------------------------
307 #-----------------------------------------------------------------------------
308
308
309
309
310 class PrefilterChecker(Component):
310 class PrefilterChecker(Component):
311 """Inspect an input line and return a handler for that line."""
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 shell = Any
314 shell = Any
315 prefilter_manager = Any
315 prefilter_manager = Any
316
316
317 def __init__(self, parent, config=None):
317 def __init__(self, parent, config=None):
318 super(PrefilterChecker, self).__init__(parent, config=config)
318 super(PrefilterChecker, self).__init__(parent, config=config)
319
319
320 @auto_attr
320 @auto_attr
321 def shell(self):
321 def shell(self):
322 shell = Component.get_instances(
322 shell = Component.get_instances(
323 root=self.root,
323 root=self.root,
324 klass='IPython.core.iplib.InteractiveShell'
324 klass='IPython.core.iplib.InteractiveShell'
325 )[0]
325 )[0]
326 return shell
326 return shell
327
327
328 @auto_attr
328 @auto_attr
329 def prefilter_manager(self):
329 def prefilter_manager(self):
330 return PrefilterManager.get_instances(root=self.root)[0]
330 return PrefilterManager.get_instances(root=self.root)[0]
331
331
332 def check(self, line_info):
332 def check(self, line_info):
333 """Inspect line_info and return a handler or None."""
333 """Inspect line_info and return a handler or None."""
334 return None
334 return None
335
335
336
336
337 class EmacsChecker(PrefilterChecker):
337 class EmacsChecker(PrefilterChecker):
338
338
339 priority = Int(100)
339 priority = Int(100, config=True)
340
340
341 def check(self, line_info):
341 def check(self, line_info):
342 "Emacs ipython-mode tags certain input lines."
342 "Emacs ipython-mode tags certain input lines."
343 if line_info.line.endswith('# PYTHON-MODE'):
343 if line_info.line.endswith('# PYTHON-MODE'):
344 return self.prefilter_manager.get_handler_by_name('emacs')
344 return self.prefilter_manager.get_handler_by_name('emacs')
345 else:
345 else:
346 return None
346 return None
347
347
348
348
349 class ShellEscapeChecker(PrefilterChecker):
349 class ShellEscapeChecker(PrefilterChecker):
350
350
351 priority = Int(200)
351 priority = Int(200, config=True)
352
352
353 def check(self, line_info):
353 def check(self, line_info):
354 if line_info.line.lstrip().startswith(ESC_SHELL):
354 if line_info.line.lstrip().startswith(ESC_SHELL):
355 return self.prefilter_manager.get_handler_by_name('shell')
355 return self.prefilter_manager.get_handler_by_name('shell')
356
356
357
357
358 class IPyAutocallChecker(PrefilterChecker):
358 class IPyAutocallChecker(PrefilterChecker):
359
359
360 priority = Int(300)
360 priority = Int(300, config=True)
361
361
362 def check(self, line_info):
362 def check(self, line_info):
363 "Instances of IPyAutocall in user_ns get autocalled immediately"
363 "Instances of IPyAutocall in user_ns get autocalled immediately"
364 obj = self.shell.user_ns.get(line_info.ifun, None)
364 obj = self.shell.user_ns.get(line_info.ifun, None)
365 if isinstance(obj, IPyAutocall):
365 if isinstance(obj, IPyAutocall):
366 obj.set_ip(self.shell)
366 obj.set_ip(self.shell)
367 return self.prefilter_manager.get_handler_by_name('auto')
367 return self.prefilter_manager.get_handler_by_name('auto')
368 else:
368 else:
369 return None
369 return None
370
370
371
371
372 class MultiLineMagicChecker(PrefilterChecker):
372 class MultiLineMagicChecker(PrefilterChecker):
373
373
374 priority = Int(400)
374 priority = Int(400, config=True)
375
375
376 def check(self, line_info):
376 def check(self, line_info):
377 "Allow ! and !! in multi-line statements if multi_line_specials is on"
377 "Allow ! and !! in multi-line statements if multi_line_specials is on"
378 # Note that this one of the only places we check the first character of
378 # Note that this one of the only places we check the first character of
379 # ifun and *not* the pre_char. Also note that the below test matches
379 # ifun and *not* the pre_char. Also note that the below test matches
380 # both ! and !!.
380 # both ! and !!.
381 if line_info.continue_prompt \
381 if line_info.continue_prompt \
382 and self.prefilter_manager.multi_line_specials:
382 and self.prefilter_manager.multi_line_specials:
383 if line_info.ifun.startswith(ESC_MAGIC):
383 if line_info.ifun.startswith(ESC_MAGIC):
384 return self.prefilter_manager.get_handler_by_name('magic')
384 return self.prefilter_manager.get_handler_by_name('magic')
385 else:
385 else:
386 return None
386 return None
387
387
388
388
389 class EscCharsChecker(PrefilterChecker):
389 class EscCharsChecker(PrefilterChecker):
390
390
391 priority = Int(500)
391 priority = Int(500, config=True)
392
392
393 def check(self, line_info):
393 def check(self, line_info):
394 """Check for escape character and return either a handler to handle it,
394 """Check for escape character and return either a handler to handle it,
395 or None if there is no escape char."""
395 or None if there is no escape char."""
396 if line_info.line[-1] == ESC_HELP \
396 if line_info.line[-1] == ESC_HELP \
397 and line_info.pre_char != ESC_SHELL \
397 and line_info.pre_char != ESC_SHELL \
398 and line_info.pre_char != ESC_SH_CAP:
398 and line_info.pre_char != ESC_SH_CAP:
399 # the ? can be at the end, but *not* for either kind of shell escape,
399 # the ? can be at the end, but *not* for either kind of shell escape,
400 # because a ? can be a vaild final char in a shell cmd
400 # because a ? can be a vaild final char in a shell cmd
401 return self.prefilter_manager.get_handler_by_name('help')
401 return self.prefilter_manager.get_handler_by_name('help')
402 else:
402 else:
403 # This returns None like it should if no handler exists
403 # This returns None like it should if no handler exists
404 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
404 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
405
405
406
406
407 class AssignmentChecker(PrefilterChecker):
407 class AssignmentChecker(PrefilterChecker):
408
408
409 priority = Int(600)
409 priority = Int(600, config=True)
410
410
411 def check(self, line_info):
411 def check(self, line_info):
412 """Check to see if user is assigning to a var for the first time, in
412 """Check to see if user is assigning to a var for the first time, in
413 which case we want to avoid any sort of automagic / autocall games.
413 which case we want to avoid any sort of automagic / autocall games.
414
414
415 This allows users to assign to either alias or magic names true python
415 This allows users to assign to either alias or magic names true python
416 variables (the magic/alias systems always take second seat to true
416 variables (the magic/alias systems always take second seat to true
417 python code). E.g. ls='hi', or ls,that=1,2"""
417 python code). E.g. ls='hi', or ls,that=1,2"""
418 if line_info.the_rest and line_info.the_rest[0] in '=,':
418 if line_info.the_rest and line_info.the_rest[0] in '=,':
419 return self.prefilter_manager.get_handler_by_name('normal')
419 return self.prefilter_manager.get_handler_by_name('normal')
420 else:
420 else:
421 return None
421 return None
422
422
423
423
424 class AutoMagicChecker(PrefilterChecker):
424 class AutoMagicChecker(PrefilterChecker):
425
425
426 priority = Int(700)
426 priority = Int(700, config=True)
427
427
428 def check(self, line_info):
428 def check(self, line_info):
429 """If the ifun is magic, and automagic is on, run it. Note: normal,
429 """If the ifun is magic, and automagic is on, run it. Note: normal,
430 non-auto magic would already have been triggered via '%' in
430 non-auto magic would already have been triggered via '%' in
431 check_esc_chars. This just checks for automagic. Also, before
431 check_esc_chars. This just checks for automagic. Also, before
432 triggering the magic handler, make sure that there is nothing in the
432 triggering the magic handler, make sure that there is nothing in the
433 user namespace which could shadow it."""
433 user namespace which could shadow it."""
434 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
434 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
435 return None
435 return None
436
436
437 # We have a likely magic method. Make sure we should actually call it.
437 # We have a likely magic method. Make sure we should actually call it.
438 if line_info.continue_prompt and not self.shell.multi_line_specials:
438 if line_info.continue_prompt and not self.shell.multi_line_specials:
439 return None
439 return None
440
440
441 head = line_info.ifun.split('.',1)[0]
441 head = line_info.ifun.split('.',1)[0]
442 if is_shadowed(head, self.shell):
442 if is_shadowed(head, self.shell):
443 return None
443 return None
444
444
445 return self.prefilter_manager.get_handler_by_name('magic')
445 return self.prefilter_manager.get_handler_by_name('magic')
446
446
447
447
448 class AliasChecker(PrefilterChecker):
448 class AliasChecker(PrefilterChecker):
449
449
450 priority = Int(800)
450 priority = Int(800, config=True)
451
451
452 @auto_attr
452 @auto_attr
453 def alias_manager(self):
453 def alias_manager(self):
454 return AliasManager.get_instances(root=self.root)[0]
454 return AliasManager.get_instances(root=self.root)[0]
455
455
456 def check(self, line_info):
456 def check(self, line_info):
457 "Check if the initital identifier on the line is an alias."
457 "Check if the initital identifier on the line is an alias."
458 # Note: aliases can not contain '.'
458 # Note: aliases can not contain '.'
459 head = line_info.ifun.split('.',1)[0]
459 head = line_info.ifun.split('.',1)[0]
460 if line_info.ifun not in self.alias_manager \
460 if line_info.ifun not in self.alias_manager \
461 or head not in self.alias_manager \
461 or head not in self.alias_manager \
462 or is_shadowed(head, self.shell):
462 or is_shadowed(head, self.shell):
463 return None
463 return None
464
464
465 return self.prefilter_manager.get_handler_by_name('alias')
465 return self.prefilter_manager.get_handler_by_name('alias')
466
466
467
467
468 class PythonOpsChecker(PrefilterChecker):
468 class PythonOpsChecker(PrefilterChecker):
469
469
470 priority = Int(900)
470 priority = Int(900, config=True)
471
471
472 def check(self, line_info):
472 def check(self, line_info):
473 """If the 'rest' of the line begins with a function call or pretty much
473 """If the 'rest' of the line begins with a function call or pretty much
474 any python operator, we should simply execute the line (regardless of
474 any python operator, we should simply execute the line (regardless of
475 whether or not there's a possible autocall expansion). This avoids
475 whether or not there's a possible autocall expansion). This avoids
476 spurious (and very confusing) geattr() accesses."""
476 spurious (and very confusing) geattr() accesses."""
477 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
477 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
478 return self.prefilter_manager.get_handler_by_name('normal')
478 return self.prefilter_manager.get_handler_by_name('normal')
479 else:
479 else:
480 return None
480 return None
481
481
482
482
483 class AutocallChecker(PrefilterChecker):
483 class AutocallChecker(PrefilterChecker):
484
484
485 priority = Int(1000)
485 priority = Int(1000, config=True)
486
486
487 def check(self, line_info):
487 def check(self, line_info):
488 "Check if the initial word/function is callable and autocall is on."
488 "Check if the initial word/function is callable and autocall is on."
489 if not self.shell.autocall:
489 if not self.shell.autocall:
490 return None
490 return None
491
491
492 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
492 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
493 if not oinfo['found']:
493 if not oinfo['found']:
494 return None
494 return None
495
495
496 if callable(oinfo['obj']) \
496 if callable(oinfo['obj']) \
497 and (not re_exclude_auto.match(line_info.the_rest)) \
497 and (not re_exclude_auto.match(line_info.the_rest)) \
498 and re_fun_name.match(line_info.ifun):
498 and re_fun_name.match(line_info.ifun):
499 return self.prefilter_manager.get_handler_by_name('auto')
499 return self.prefilter_manager.get_handler_by_name('auto')
500 else:
500 else:
501 return None
501 return None
502
502
503
503
504 #-----------------------------------------------------------------------------
504 #-----------------------------------------------------------------------------
505 # Prefilter handlers
505 # Prefilter handlers
506 #-----------------------------------------------------------------------------
506 #-----------------------------------------------------------------------------
507
507
508
508
509 class PrefilterHandler(Component):
509 class PrefilterHandler(Component):
510
510
511 handler_name = Str('normal')
511 handler_name = Str('normal')
512 esc_strings = List([])
512 esc_strings = List([])
513 shell = Any
513 shell = Any
514 prefilter_manager = Any
514 prefilter_manager = Any
515
515
516 def __init__(self, parent, config=None):
516 def __init__(self, parent, config=None):
517 super(PrefilterHandler, self).__init__(parent, config=config)
517 super(PrefilterHandler, self).__init__(parent, config=config)
518 self.prefilter_manager.register_handler(
518 self.prefilter_manager.register_handler(
519 self.handler_name,
519 self.handler_name,
520 self,
520 self,
521 self.esc_strings
521 self.esc_strings
522 )
522 )
523
523
524 @auto_attr
524 @auto_attr
525 def shell(self):
525 def shell(self):
526 shell = Component.get_instances(
526 shell = Component.get_instances(
527 root=self.root,
527 root=self.root,
528 klass='IPython.core.iplib.InteractiveShell'
528 klass='IPython.core.iplib.InteractiveShell'
529 )[0]
529 )[0]
530 return shell
530 return shell
531
531
532 @auto_attr
532 @auto_attr
533 def prefilter_manager(self):
533 def prefilter_manager(self):
534 return PrefilterManager.get_instances(root=self.root)[0]
534 return PrefilterManager.get_instances(root=self.root)[0]
535
535
536 def handle(self, line_info):
536 def handle(self, line_info):
537 """Handle normal input lines. Use as a template for handlers."""
537 """Handle normal input lines. Use as a template for handlers."""
538
538
539 # With autoindent on, we need some way to exit the input loop, and I
539 # With autoindent on, we need some way to exit the input loop, and I
540 # don't want to force the user to have to backspace all the way to
540 # don't want to force the user to have to backspace all the way to
541 # clear the line. The rule will be in this case, that either two
541 # clear the line. The rule will be in this case, that either two
542 # lines of pure whitespace in a row, or a line of pure whitespace but
542 # lines of pure whitespace in a row, or a line of pure whitespace but
543 # of a size different to the indent level, will exit the input loop.
543 # of a size different to the indent level, will exit the input loop.
544 line = line_info.line
544 line = line_info.line
545 continue_prompt = line_info.continue_prompt
545 continue_prompt = line_info.continue_prompt
546
546
547 if (continue_prompt and self.shell.autoindent and line.isspace() and
547 if (continue_prompt and self.shell.autoindent and line.isspace() and
548 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or
548 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or
549 (self.shell.buffer[-1]).isspace() )):
549 (self.shell.buffer[-1]).isspace() )):
550 line = ''
550 line = ''
551
551
552 self.shell.log(line, line, continue_prompt)
552 self.shell.log(line, line, continue_prompt)
553 return line
553 return line
554
554
555
555
556 class AliasHandler(PrefilterHandler):
556 class AliasHandler(PrefilterHandler):
557
557
558 handler_name = Str('alias')
558 handler_name = Str('alias')
559 esc_strings = List([])
559 esc_strings = List([])
560
560
561 @auto_attr
561 @auto_attr
562 def alias_manager(self):
562 def alias_manager(self):
563 return AliasManager.get_instances(root=self.root)[0]
563 return AliasManager.get_instances(root=self.root)[0]
564
564
565 def handle(self, line_info):
565 def handle(self, line_info):
566 """Handle alias input lines. """
566 """Handle alias input lines. """
567 transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
567 transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
568 # pre is needed, because it carries the leading whitespace. Otherwise
568 # pre is needed, because it carries the leading whitespace. Otherwise
569 # aliases won't work in indented sections.
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 make_quoted_expr(transformed))
571 make_quoted_expr(transformed))
572
572
573 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
573 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
574 return line_out
574 return line_out
575
575
576
576
577 class ShellEscapeHandler(PrefilterHandler):
577 class ShellEscapeHandler(PrefilterHandler):
578
578
579 handler_name = Str('shell')
579 handler_name = Str('shell')
580 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
580 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
581
581
582 def handle(self, line_info):
582 def handle(self, line_info):
583 """Execute the line in a shell, empty return value"""
583 """Execute the line in a shell, empty return value"""
584 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
584 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
585
585
586 line = line_info.line
586 line = line_info.line
587 if line.lstrip().startswith(ESC_SH_CAP):
587 if line.lstrip().startswith(ESC_SH_CAP):
588 # rewrite LineInfo's line, ifun and the_rest to properly hold the
588 # rewrite LineInfo's line, ifun and the_rest to properly hold the
589 # call to %sx and the actual command to be executed, so
589 # call to %sx and the actual command to be executed, so
590 # handle_magic can work correctly. Note that this works even if
590 # handle_magic can work correctly. Note that this works even if
591 # the line is indented, so it handles multi_line_specials
591 # the line is indented, so it handles multi_line_specials
592 # properly.
592 # properly.
593 new_rest = line.lstrip()[2:]
593 new_rest = line.lstrip()[2:]
594 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
594 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
595 line_info.ifun = 'sx'
595 line_info.ifun = 'sx'
596 line_info.the_rest = new_rest
596 line_info.the_rest = new_rest
597 return magic_handler.handle(line_info)
597 return magic_handler.handle(line_info)
598 else:
598 else:
599 cmd = line.lstrip().lstrip(ESC_SHELL)
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 make_quoted_expr(cmd))
601 make_quoted_expr(cmd))
602 # update cache/log and return
602 # update cache/log and return
603 self.shell.log(line, line_out, line_info.continue_prompt)
603 self.shell.log(line, line_out, line_info.continue_prompt)
604 return line_out
604 return line_out
605
605
606
606
607 class MagicHandler(PrefilterHandler):
607 class MagicHandler(PrefilterHandler):
608
608
609 handler_name = Str('magic')
609 handler_name = Str('magic')
610 esc_strings = List(['%'])
610 esc_strings = List([ESC_MAGIC])
611
611
612 def handle(self, line_info):
612 def handle(self, line_info):
613 """Execute magic functions."""
613 """Execute magic functions."""
614 ifun = line_info.ifun
614 ifun = line_info.ifun
615 the_rest = line_info.the_rest
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 make_quoted_expr(ifun + " " + the_rest))
617 make_quoted_expr(ifun + " " + the_rest))
618 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
618 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
619 return cmd
619 return cmd
620
620
621
621
622 class AutoHandler(PrefilterHandler):
622 class AutoHandler(PrefilterHandler):
623
623
624 handler_name = Str('auto')
624 handler_name = Str('auto')
625 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
625 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
626
626
627 def handle(self, line_info):
627 def handle(self, line_info):
628 """Hande lines which can be auto-executed, quoting if requested."""
628 """Hande lines which can be auto-executed, quoting if requested."""
629 line = line_info.line
629 line = line_info.line
630 ifun = line_info.ifun
630 ifun = line_info.ifun
631 the_rest = line_info.the_rest
631 the_rest = line_info.the_rest
632 pre = line_info.pre
632 pre = line_info.pre
633 continue_prompt = line_info.continue_prompt
633 continue_prompt = line_info.continue_prompt
634 obj = line_info.ofind(self)['obj']
634 obj = line_info.ofind(self)['obj']
635
635
636 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
636 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
637
637
638 # This should only be active for single-line input!
638 # This should only be active for single-line input!
639 if continue_prompt:
639 if continue_prompt:
640 self.log(line,line,continue_prompt)
640 self.log(line,line,continue_prompt)
641 return line
641 return line
642
642
643 force_auto = isinstance(obj, IPyAutocall)
643 force_auto = isinstance(obj, IPyAutocall)
644 auto_rewrite = True
644 auto_rewrite = True
645
645
646 if pre == ESC_QUOTE:
646 if pre == ESC_QUOTE:
647 # Auto-quote splitting on whitespace
647 # Auto-quote splitting on whitespace
648 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
648 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
649 elif pre == ESC_QUOTE2:
649 elif pre == ESC_QUOTE2:
650 # Auto-quote whole string
650 # Auto-quote whole string
651 newcmd = '%s("%s")' % (ifun,the_rest)
651 newcmd = '%s("%s")' % (ifun,the_rest)
652 elif pre == ESC_PAREN:
652 elif pre == ESC_PAREN:
653 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
653 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
654 else:
654 else:
655 # Auto-paren.
655 # Auto-paren.
656 # We only apply it to argument-less calls if the autocall
656 # We only apply it to argument-less calls if the autocall
657 # parameter is set to 2. We only need to check that autocall is <
657 # parameter is set to 2. We only need to check that autocall is <
658 # 2, since this function isn't called unless it's at least 1.
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 newcmd = '%s %s' % (ifun,the_rest)
660 newcmd = '%s %s' % (ifun,the_rest)
661 auto_rewrite = False
661 auto_rewrite = False
662 else:
662 else:
663 if not force_auto and the_rest.startswith('['):
663 if not force_auto and the_rest.startswith('['):
664 if hasattr(obj,'__getitem__'):
664 if hasattr(obj,'__getitem__'):
665 # Don't autocall in this case: item access for an object
665 # Don't autocall in this case: item access for an object
666 # which is BOTH callable and implements __getitem__.
666 # which is BOTH callable and implements __getitem__.
667 newcmd = '%s %s' % (ifun,the_rest)
667 newcmd = '%s %s' % (ifun,the_rest)
668 auto_rewrite = False
668 auto_rewrite = False
669 else:
669 else:
670 # if the object doesn't support [] access, go ahead and
670 # if the object doesn't support [] access, go ahead and
671 # autocall
671 # autocall
672 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
672 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
673 elif the_rest.endswith(';'):
673 elif the_rest.endswith(';'):
674 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
674 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
675 else:
675 else:
676 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
676 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
677
677
678 if auto_rewrite:
678 if auto_rewrite:
679 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
679 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
680
680
681 try:
681 try:
682 # plain ascii works better w/ pyreadline, on some machines, so
682 # plain ascii works better w/ pyreadline, on some machines, so
683 # we use it and only print uncolored rewrite if we have unicode
683 # we use it and only print uncolored rewrite if we have unicode
684 rw = str(rw)
684 rw = str(rw)
685 print >>Term.cout, rw
685 print >>Term.cout, rw
686 except UnicodeEncodeError:
686 except UnicodeEncodeError:
687 print "-------------->" + newcmd
687 print "-------------->" + newcmd
688
688
689 # log what is now valid Python, not the actual user input (without the
689 # log what is now valid Python, not the actual user input (without the
690 # final newline)
690 # final newline)
691 self.shell.log(line,newcmd,continue_prompt)
691 self.shell.log(line,newcmd,continue_prompt)
692 return newcmd
692 return newcmd
693
693
694
694
695 class HelpHandler(PrefilterHandler):
695 class HelpHandler(PrefilterHandler):
696
696
697 handler_name = Str('help')
697 handler_name = Str('help')
698 esc_strings = List([ESC_HELP])
698 esc_strings = List([ESC_HELP])
699
699
700 def handle(self, line_info):
700 def handle(self, line_info):
701 """Try to get some help for the object.
701 """Try to get some help for the object.
702
702
703 obj? or ?obj -> basic information.
703 obj? or ?obj -> basic information.
704 obj?? or ??obj -> more details.
704 obj?? or ??obj -> more details.
705 """
705 """
706 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
706 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
707 line = line_info.line
707 line = line_info.line
708 # We need to make sure that we don't process lines which would be
708 # We need to make sure that we don't process lines which would be
709 # otherwise valid python, such as "x=1 # what?"
709 # otherwise valid python, such as "x=1 # what?"
710 try:
710 try:
711 codeop.compile_command(line)
711 codeop.compile_command(line)
712 except SyntaxError:
712 except SyntaxError:
713 # We should only handle as help stuff which is NOT valid syntax
713 # We should only handle as help stuff which is NOT valid syntax
714 if line[0]==ESC_HELP:
714 if line[0]==ESC_HELP:
715 line = line[1:]
715 line = line[1:]
716 elif line[-1]==ESC_HELP:
716 elif line[-1]==ESC_HELP:
717 line = line[:-1]
717 line = line[:-1]
718 self.shell.log(line, '#?'+line, line_info.continue_prompt)
718 self.shell.log(line, '#?'+line, line_info.continue_prompt)
719 if line:
719 if line:
720 #print 'line:<%r>' % line # dbg
720 #print 'line:<%r>' % line # dbg
721 self.shell.magic_pinfo(line)
721 self.shell.magic_pinfo(line)
722 else:
722 else:
723 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
723 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
724 return '' # Empty string is needed here!
724 return '' # Empty string is needed here!
725 except:
725 except:
726 raise
726 raise
727 # Pass any other exceptions through to the normal handler
727 # Pass any other exceptions through to the normal handler
728 return normal_handler.handle(line_info)
728 return normal_handler.handle(line_info)
729 else:
729 else:
730 raise
730 raise
731 # If the code compiles ok, we should handle it normally
731 # If the code compiles ok, we should handle it normally
732 return normal_handler.handle(line_info)
732 return normal_handler.handle(line_info)
733
733
734
734
735 class EmacsHandler(PrefilterHandler):
735 class EmacsHandler(PrefilterHandler):
736
736
737 handler_name = Str('emacs')
737 handler_name = Str('emacs')
738 esc_strings = List([])
738 esc_strings = List([])
739
739
740 def handle(self, line_info):
740 def handle(self, line_info):
741 """Handle input lines marked by python-mode."""
741 """Handle input lines marked by python-mode."""
742
742
743 # Currently, nothing is done. Later more functionality can be added
743 # Currently, nothing is done. Later more functionality can be added
744 # here if needed.
744 # here if needed.
745
745
746 # The input cache shouldn't be updated
746 # The input cache shouldn't be updated
747 return line_info.line
747 return line_info.line
748
748
749
749
750 #-----------------------------------------------------------------------------
750 #-----------------------------------------------------------------------------
751 # Defaults
751 # Defaults
752 #-----------------------------------------------------------------------------
752 #-----------------------------------------------------------------------------
753
753
754
754
755 _default_checkers = [
755 _default_checkers = [
756 EmacsChecker,
756 EmacsChecker,
757 ShellEscapeChecker,
757 ShellEscapeChecker,
758 IPyAutocallChecker,
758 IPyAutocallChecker,
759 MultiLineMagicChecker,
759 MultiLineMagicChecker,
760 EscCharsChecker,
760 EscCharsChecker,
761 AssignmentChecker,
761 AssignmentChecker,
762 AutoMagicChecker,
762 AutoMagicChecker,
763 AliasChecker,
763 AliasChecker,
764 PythonOpsChecker,
764 PythonOpsChecker,
765 AutocallChecker
765 AutocallChecker
766 ]
766 ]
767
767
768 _default_handlers = [
768 _default_handlers = [
769 PrefilterHandler,
769 PrefilterHandler,
770 AliasHandler,
770 AliasHandler,
771 ShellEscapeHandler,
771 ShellEscapeHandler,
772 MagicHandler,
772 MagicHandler,
773 AutoHandler,
773 AutoHandler,
774 HelpHandler,
774 HelpHandler,
775 EmacsHandler
775 EmacsHandler
776 ]
776 ]
777
777
@@ -1,627 +1,630 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4 """
4 """
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2008-2009 The IPython Development Team
7 # Copyright (C) 2008-2009 The IPython Development Team
8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Required modules
15 # Required modules
16 import __builtin__
16 import __builtin__
17 import os
17 import os
18 import socket
18 import socket
19 import sys
19 import sys
20 import time
20 import time
21
21
22 # IPython's own
22 # IPython's own
23 from IPython.utils import coloransi
23 from IPython.utils import coloransi
24 from IPython.core import release
24 from IPython.core import release
25 from IPython.external.Itpl import ItplNS
25 from IPython.external.Itpl import ItplNS
26 from IPython.core.error import TryNext
26 from IPython.core.error import TryNext
27 from IPython.utils.ipstruct import Struct
27 from IPython.utils.ipstruct import Struct
28 from IPython.core.macro import Macro
28 from IPython.core.macro import Macro
29 import IPython.utils.generics
29 import IPython.utils.generics
30
30
31 from IPython.utils.genutils import *
31 from IPython.utils.genutils import *
32
32
33 #****************************************************************************
33 #****************************************************************************
34 #Color schemes for Prompts.
34 #Color schemes for Prompts.
35
35
36 PromptColors = coloransi.ColorSchemeTable()
36 PromptColors = coloransi.ColorSchemeTable()
37 InputColors = coloransi.InputTermColors # just a shorthand
37 InputColors = coloransi.InputTermColors # just a shorthand
38 Colors = coloransi.TermColors # just a shorthand
38 Colors = coloransi.TermColors # just a shorthand
39
39
40 PromptColors.add_scheme(coloransi.ColorScheme(
40 PromptColors.add_scheme(coloransi.ColorScheme(
41 'NoColor',
41 'NoColor',
42 in_prompt = InputColors.NoColor, # Input prompt
42 in_prompt = InputColors.NoColor, # Input prompt
43 in_number = InputColors.NoColor, # Input prompt number
43 in_number = InputColors.NoColor, # Input prompt number
44 in_prompt2 = InputColors.NoColor, # Continuation prompt
44 in_prompt2 = InputColors.NoColor, # Continuation prompt
45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
46
46
47 out_prompt = Colors.NoColor, # Output prompt
47 out_prompt = Colors.NoColor, # Output prompt
48 out_number = Colors.NoColor, # Output prompt number
48 out_number = Colors.NoColor, # Output prompt number
49
49
50 normal = Colors.NoColor # color off (usu. Colors.Normal)
50 normal = Colors.NoColor # color off (usu. Colors.Normal)
51 ))
51 ))
52
52
53 # make some schemes as instances so we can copy them for modification easily:
53 # make some schemes as instances so we can copy them for modification easily:
54 __PColLinux = coloransi.ColorScheme(
54 __PColLinux = coloransi.ColorScheme(
55 'Linux',
55 'Linux',
56 in_prompt = InputColors.Green,
56 in_prompt = InputColors.Green,
57 in_number = InputColors.LightGreen,
57 in_number = InputColors.LightGreen,
58 in_prompt2 = InputColors.Green,
58 in_prompt2 = InputColors.Green,
59 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
59 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
60
60
61 out_prompt = Colors.Red,
61 out_prompt = Colors.Red,
62 out_number = Colors.LightRed,
62 out_number = Colors.LightRed,
63
63
64 normal = Colors.Normal
64 normal = Colors.Normal
65 )
65 )
66 # Don't forget to enter it into the table!
66 # Don't forget to enter it into the table!
67 PromptColors.add_scheme(__PColLinux)
67 PromptColors.add_scheme(__PColLinux)
68
68
69 # Slightly modified Linux for light backgrounds
69 # Slightly modified Linux for light backgrounds
70 __PColLightBG = __PColLinux.copy('LightBG')
70 __PColLightBG = __PColLinux.copy('LightBG')
71
71
72 __PColLightBG.colors.update(
72 __PColLightBG.colors.update(
73 in_prompt = InputColors.Blue,
73 in_prompt = InputColors.Blue,
74 in_number = InputColors.LightBlue,
74 in_number = InputColors.LightBlue,
75 in_prompt2 = InputColors.Blue
75 in_prompt2 = InputColors.Blue
76 )
76 )
77 PromptColors.add_scheme(__PColLightBG)
77 PromptColors.add_scheme(__PColLightBG)
78
78
79 del Colors,InputColors
79 del Colors,InputColors
80
80
81 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
82 def multiple_replace(dict, text):
82 def multiple_replace(dict, text):
83 """ Replace in 'text' all occurences of any key in the given
83 """ Replace in 'text' all occurences of any key in the given
84 dictionary by its corresponding value. Returns the new string."""
84 dictionary by its corresponding value. Returns the new string."""
85
85
86 # Function by Xavier Defrang, originally found at:
86 # Function by Xavier Defrang, originally found at:
87 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
87 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
88
88
89 # Create a regular expression from the dictionary keys
89 # Create a regular expression from the dictionary keys
90 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
90 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
91 # For each match, look-up corresponding value in dictionary
91 # For each match, look-up corresponding value in dictionary
92 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
92 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
93
93
94 #-----------------------------------------------------------------------------
94 #-----------------------------------------------------------------------------
95 # Special characters that can be used in prompt templates, mainly bash-like
95 # Special characters that can be used in prompt templates, mainly bash-like
96
96
97 # If $HOME isn't defined (Windows), make it an absurd string so that it can
97 # If $HOME isn't defined (Windows), make it an absurd string so that it can
98 # never be expanded out into '~'. Basically anything which can never be a
98 # never be expanded out into '~'. Basically anything which can never be a
99 # reasonable directory name will do, we just want the $HOME -> '~' operation
99 # reasonable directory name will do, we just want the $HOME -> '~' operation
100 # to become a no-op. We pre-compute $HOME here so it's not done on every
100 # to become a no-op. We pre-compute $HOME here so it's not done on every
101 # prompt call.
101 # prompt call.
102
102
103 # FIXME:
103 # FIXME:
104
104
105 # - This should be turned into a class which does proper namespace management,
105 # - This should be turned into a class which does proper namespace management,
106 # since the prompt specials need to be evaluated in a certain namespace.
106 # since the prompt specials need to be evaluated in a certain namespace.
107 # Currently it's just globals, which need to be managed manually by code
107 # Currently it's just globals, which need to be managed manually by code
108 # below.
108 # below.
109
109
110 # - I also need to split up the color schemes from the prompt specials
110 # - I also need to split up the color schemes from the prompt specials
111 # somehow. I don't have a clean design for that quite yet.
111 # somehow. I don't have a clean design for that quite yet.
112
112
113 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
113 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
114
114
115 # We precompute a few more strings here for the prompt_specials, which are
115 # We precompute a few more strings here for the prompt_specials, which are
116 # fixed once ipython starts. This reduces the runtime overhead of computing
116 # fixed once ipython starts. This reduces the runtime overhead of computing
117 # prompt strings.
117 # prompt strings.
118 USER = os.environ.get("USER")
118 USER = os.environ.get("USER")
119 HOSTNAME = socket.gethostname()
119 HOSTNAME = socket.gethostname()
120 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
120 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
121 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
121 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
122
122
123 prompt_specials_color = {
123 prompt_specials_color = {
124 # Prompt/history count
124 # Prompt/history count
125 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
126 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
126 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
127 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
127 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
128 # can get numbers displayed in whatever color they want.
128 # can get numbers displayed in whatever color they want.
129 r'\N': '${self.cache.prompt_count}',
129 r'\N': '${self.cache.prompt_count}',
130
130
131 # Prompt/history count, with the actual digits replaced by dots. Used
131 # Prompt/history count, with the actual digits replaced by dots. Used
132 # mainly in continuation prompts (prompt_in2)
132 # mainly in continuation prompts (prompt_in2)
133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
134 # More robust form of the above expression, that uses __builtins__
134 # More robust form of the above expression, that uses __builtins__
135 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
135 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
136
136
137 # Current working directory
137 # Current working directory
138 r'\w': '${os.getcwd()}',
138 r'\w': '${os.getcwd()}',
139 # Current time
139 # Current time
140 r'\t' : '${time.strftime("%H:%M:%S")}',
140 r'\t' : '${time.strftime("%H:%M:%S")}',
141 # Basename of current working directory.
141 # Basename of current working directory.
142 # (use os.sep to make this portable across OSes)
142 # (use os.sep to make this portable across OSes)
143 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
143 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
144 # These X<N> are an extension to the normal bash prompts. They return
144 # These X<N> are an extension to the normal bash prompts. They return
145 # N terms of the path, after replacing $HOME with '~'
145 # N terms of the path, after replacing $HOME with '~'
146 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
146 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
147 r'\X1': '${self.cwd_filt(1)}',
147 r'\X1': '${self.cwd_filt(1)}',
148 r'\X2': '${self.cwd_filt(2)}',
148 r'\X2': '${self.cwd_filt(2)}',
149 r'\X3': '${self.cwd_filt(3)}',
149 r'\X3': '${self.cwd_filt(3)}',
150 r'\X4': '${self.cwd_filt(4)}',
150 r'\X4': '${self.cwd_filt(4)}',
151 r'\X5': '${self.cwd_filt(5)}',
151 r'\X5': '${self.cwd_filt(5)}',
152 # Y<N> are similar to X<N>, but they show '~' if it's the directory
152 # Y<N> are similar to X<N>, but they show '~' if it's the directory
153 # N+1 in the list. Somewhat like %cN in tcsh.
153 # N+1 in the list. Somewhat like %cN in tcsh.
154 r'\Y0': '${self.cwd_filt2(0)}',
154 r'\Y0': '${self.cwd_filt2(0)}',
155 r'\Y1': '${self.cwd_filt2(1)}',
155 r'\Y1': '${self.cwd_filt2(1)}',
156 r'\Y2': '${self.cwd_filt2(2)}',
156 r'\Y2': '${self.cwd_filt2(2)}',
157 r'\Y3': '${self.cwd_filt2(3)}',
157 r'\Y3': '${self.cwd_filt2(3)}',
158 r'\Y4': '${self.cwd_filt2(4)}',
158 r'\Y4': '${self.cwd_filt2(4)}',
159 r'\Y5': '${self.cwd_filt2(5)}',
159 r'\Y5': '${self.cwd_filt2(5)}',
160 # Hostname up to first .
160 # Hostname up to first .
161 r'\h': HOSTNAME_SHORT,
161 r'\h': HOSTNAME_SHORT,
162 # Full hostname
162 # Full hostname
163 r'\H': HOSTNAME,
163 r'\H': HOSTNAME,
164 # Username of current user
164 # Username of current user
165 r'\u': USER,
165 r'\u': USER,
166 # Escaped '\'
166 # Escaped '\'
167 '\\\\': '\\',
167 '\\\\': '\\',
168 # Newline
168 # Newline
169 r'\n': '\n',
169 r'\n': '\n',
170 # Carriage return
170 # Carriage return
171 r'\r': '\r',
171 r'\r': '\r',
172 # Release version
172 # Release version
173 r'\v': release.version,
173 r'\v': release.version,
174 # Root symbol ($ or #)
174 # Root symbol ($ or #)
175 r'\$': ROOT_SYMBOL,
175 r'\$': ROOT_SYMBOL,
176 }
176 }
177
177
178 # A copy of the prompt_specials dictionary but with all color escapes removed,
178 # A copy of the prompt_specials dictionary but with all color escapes removed,
179 # so we can correctly compute the prompt length for the auto_rewrite method.
179 # so we can correctly compute the prompt length for the auto_rewrite method.
180 prompt_specials_nocolor = prompt_specials_color.copy()
180 prompt_specials_nocolor = prompt_specials_color.copy()
181 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
181 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
182 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
182 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
183
183
184 # Add in all the InputTermColors color escapes as valid prompt characters.
184 # Add in all the InputTermColors color escapes as valid prompt characters.
185 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
185 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
186 # with a color name which may begin with a letter used by any other of the
186 # with a color name which may begin with a letter used by any other of the
187 # allowed specials. This of course means that \\C will never be allowed for
187 # allowed specials. This of course means that \\C will never be allowed for
188 # anything else.
188 # anything else.
189 input_colors = coloransi.InputTermColors
189 input_colors = coloransi.InputTermColors
190 for _color in dir(input_colors):
190 for _color in dir(input_colors):
191 if _color[0] != '_':
191 if _color[0] != '_':
192 c_name = r'\C_'+_color
192 c_name = r'\C_'+_color
193 prompt_specials_color[c_name] = getattr(input_colors,_color)
193 prompt_specials_color[c_name] = getattr(input_colors,_color)
194 prompt_specials_nocolor[c_name] = ''
194 prompt_specials_nocolor[c_name] = ''
195
195
196 # we default to no color for safety. Note that prompt_specials is a global
196 # we default to no color for safety. Note that prompt_specials is a global
197 # variable used by all prompt objects.
197 # variable used by all prompt objects.
198 prompt_specials = prompt_specials_nocolor
198 prompt_specials = prompt_specials_nocolor
199
199
200 #-----------------------------------------------------------------------------
200 #-----------------------------------------------------------------------------
201 def str_safe(arg):
201 def str_safe(arg):
202 """Convert to a string, without ever raising an exception.
202 """Convert to a string, without ever raising an exception.
203
203
204 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
204 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
205 error message."""
205 error message."""
206
206
207 try:
207 try:
208 out = str(arg)
208 out = str(arg)
209 except UnicodeError:
209 except UnicodeError:
210 try:
210 try:
211 out = arg.encode('utf_8','replace')
211 out = arg.encode('utf_8','replace')
212 except Exception,msg:
212 except Exception,msg:
213 # let's keep this little duplication here, so that the most common
213 # let's keep this little duplication here, so that the most common
214 # case doesn't suffer from a double try wrapping.
214 # case doesn't suffer from a double try wrapping.
215 out = '<ERROR: %s>' % msg
215 out = '<ERROR: %s>' % msg
216 except Exception,msg:
216 except Exception,msg:
217 out = '<ERROR: %s>' % msg
217 out = '<ERROR: %s>' % msg
218 return out
218 return out
219
219
220 class BasePrompt(object):
220 class BasePrompt(object):
221 """Interactive prompt similar to Mathematica's."""
221 """Interactive prompt similar to Mathematica's."""
222
222
223 def _get_p_template(self):
223 def _get_p_template(self):
224 return self._p_template
224 return self._p_template
225
225
226 def _set_p_template(self,val):
226 def _set_p_template(self,val):
227 self._p_template = val
227 self._p_template = val
228 self.set_p_str()
228 self.set_p_str()
229
229
230 p_template = property(_get_p_template,_set_p_template,
230 p_template = property(_get_p_template,_set_p_template,
231 doc='Template for prompt string creation')
231 doc='Template for prompt string creation')
232
232
233 def __init__(self,cache,sep,prompt,pad_left=False):
233 def __init__(self,cache,sep,prompt,pad_left=False):
234
234
235 # Hack: we access information about the primary prompt through the
235 # Hack: we access information about the primary prompt through the
236 # cache argument. We need this, because we want the secondary prompt
236 # cache argument. We need this, because we want the secondary prompt
237 # to be aligned with the primary one. Color table info is also shared
237 # to be aligned with the primary one. Color table info is also shared
238 # by all prompt classes through the cache. Nice OO spaghetti code!
238 # by all prompt classes through the cache. Nice OO spaghetti code!
239 self.cache = cache
239 self.cache = cache
240 self.sep = sep
240 self.sep = sep
241
241
242 # regexp to count the number of spaces at the end of a prompt
242 # regexp to count the number of spaces at the end of a prompt
243 # expression, useful for prompt auto-rewriting
243 # expression, useful for prompt auto-rewriting
244 self.rspace = re.compile(r'(\s*)$')
244 self.rspace = re.compile(r'(\s*)$')
245 # Flag to left-pad prompt strings to match the length of the primary
245 # Flag to left-pad prompt strings to match the length of the primary
246 # prompt
246 # prompt
247 self.pad_left = pad_left
247 self.pad_left = pad_left
248
248
249 # Set template to create each actual prompt (where numbers change).
249 # Set template to create each actual prompt (where numbers change).
250 # Use a property
250 # Use a property
251 self.p_template = prompt
251 self.p_template = prompt
252 self.set_p_str()
252 self.set_p_str()
253
253
254 def set_p_str(self):
254 def set_p_str(self):
255 """ Set the interpolating prompt strings.
255 """ Set the interpolating prompt strings.
256
256
257 This must be called every time the color settings change, because the
257 This must be called every time the color settings change, because the
258 prompt_specials global may have changed."""
258 prompt_specials global may have changed."""
259
259
260 import os,time # needed in locals for prompt string handling
260 import os,time # needed in locals for prompt string handling
261 loc = locals()
261 loc = locals()
262 try:
262 try:
263 self.p_str = ItplNS('%s%s%s' %
263 self.p_str = ItplNS('%s%s%s' %
264 ('${self.sep}${self.col_p}',
264 ('${self.sep}${self.col_p}',
265 multiple_replace(prompt_specials, self.p_template),
265 multiple_replace(prompt_specials, self.p_template),
266 '${self.col_norm}'),self.cache.user_ns,loc)
266 '${self.col_norm}'),self.cache.user_ns,loc)
267
267
268 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
268 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
269 self.p_template),
269 self.p_template),
270 self.cache.user_ns,loc)
270 self.cache.user_ns,loc)
271 except:
271 except:
272 print "Illegal prompt template (check $ usage!):",self.p_template
272 print "Illegal prompt template (check $ usage!):",self.p_template
273 self.p_str = self.p_template
273 self.p_str = self.p_template
274 self.p_str_nocolor = self.p_template
274 self.p_str_nocolor = self.p_template
275
275
276 def write(self,msg): # dbg
276 def write(self,msg): # dbg
277 sys.stdout.write(msg)
277 sys.stdout.write(msg)
278 return ''
278 return ''
279
279
280 def __str__(self):
280 def __str__(self):
281 """Return a string form of the prompt.
281 """Return a string form of the prompt.
282
282
283 This for is useful for continuation and output prompts, since it is
283 This for is useful for continuation and output prompts, since it is
284 left-padded to match lengths with the primary one (if the
284 left-padded to match lengths with the primary one (if the
285 self.pad_left attribute is set)."""
285 self.pad_left attribute is set)."""
286
286
287 out_str = str_safe(self.p_str)
287 out_str = str_safe(self.p_str)
288 if self.pad_left:
288 if self.pad_left:
289 # We must find the amount of padding required to match lengths,
289 # We must find the amount of padding required to match lengths,
290 # taking the color escapes (which are invisible on-screen) into
290 # taking the color escapes (which are invisible on-screen) into
291 # account.
291 # account.
292 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
292 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
293 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
293 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
294 return format % out_str
294 return format % out_str
295 else:
295 else:
296 return out_str
296 return out_str
297
297
298 # these path filters are put in as methods so that we can control the
298 # these path filters are put in as methods so that we can control the
299 # namespace where the prompt strings get evaluated
299 # namespace where the prompt strings get evaluated
300 def cwd_filt(self,depth):
300 def cwd_filt(self,depth):
301 """Return the last depth elements of the current working directory.
301 """Return the last depth elements of the current working directory.
302
302
303 $HOME is always replaced with '~'.
303 $HOME is always replaced with '~'.
304 If depth==0, the full path is returned."""
304 If depth==0, the full path is returned."""
305
305
306 cwd = os.getcwd().replace(HOME,"~")
306 cwd = os.getcwd().replace(HOME,"~")
307 out = os.sep.join(cwd.split(os.sep)[-depth:])
307 out = os.sep.join(cwd.split(os.sep)[-depth:])
308 if out:
308 if out:
309 return out
309 return out
310 else:
310 else:
311 return os.sep
311 return os.sep
312
312
313 def cwd_filt2(self,depth):
313 def cwd_filt2(self,depth):
314 """Return the last depth elements of the current working directory.
314 """Return the last depth elements of the current working directory.
315
315
316 $HOME is always replaced with '~'.
316 $HOME is always replaced with '~'.
317 If depth==0, the full path is returned."""
317 If depth==0, the full path is returned."""
318
318
319 full_cwd = os.getcwd()
319 full_cwd = os.getcwd()
320 cwd = full_cwd.replace(HOME,"~").split(os.sep)
320 cwd = full_cwd.replace(HOME,"~").split(os.sep)
321 if '~' in cwd and len(cwd) == depth+1:
321 if '~' in cwd and len(cwd) == depth+1:
322 depth += 1
322 depth += 1
323 drivepart = ''
323 drivepart = ''
324 if sys.platform == 'win32' and len(cwd) > depth:
324 if sys.platform == 'win32' and len(cwd) > depth:
325 drivepart = os.path.splitdrive(full_cwd)[0]
325 drivepart = os.path.splitdrive(full_cwd)[0]
326 out = drivepart + '/'.join(cwd[-depth:])
326 out = drivepart + '/'.join(cwd[-depth:])
327
327
328 if out:
328 if out:
329 return out
329 return out
330 else:
330 else:
331 return os.sep
331 return os.sep
332
332
333 def __nonzero__(self):
333 def __nonzero__(self):
334 """Implement boolean behavior.
334 """Implement boolean behavior.
335
335
336 Checks whether the p_str attribute is non-empty"""
336 Checks whether the p_str attribute is non-empty"""
337
337
338 return bool(self.p_template)
338 return bool(self.p_template)
339
339
340 class Prompt1(BasePrompt):
340 class Prompt1(BasePrompt):
341 """Input interactive prompt similar to Mathematica's."""
341 """Input interactive prompt similar to Mathematica's."""
342
342
343 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
343 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
344 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
344 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
345
345
346 def set_colors(self):
346 def set_colors(self):
347 self.set_p_str()
347 self.set_p_str()
348 Colors = self.cache.color_table.active_colors # shorthand
348 Colors = self.cache.color_table.active_colors # shorthand
349 self.col_p = Colors.in_prompt
349 self.col_p = Colors.in_prompt
350 self.col_num = Colors.in_number
350 self.col_num = Colors.in_number
351 self.col_norm = Colors.in_normal
351 self.col_norm = Colors.in_normal
352 # We need a non-input version of these escapes for the '--->'
352 # We need a non-input version of these escapes for the '--->'
353 # auto-call prompts used in the auto_rewrite() method.
353 # auto-call prompts used in the auto_rewrite() method.
354 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
354 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
355 self.col_norm_ni = Colors.normal
355 self.col_norm_ni = Colors.normal
356
356
357 def __str__(self):
357 def __str__(self):
358 self.cache.prompt_count += 1
358 self.cache.prompt_count += 1
359 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
359 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
360 return str_safe(self.p_str)
360 return str_safe(self.p_str)
361
361
362 def auto_rewrite(self):
362 def auto_rewrite(self):
363 """Print a string of the form '--->' which lines up with the previous
363 """Print a string of the form '--->' which lines up with the previous
364 input string. Useful for systems which re-write the user input when
364 input string. Useful for systems which re-write the user input when
365 handling automatically special syntaxes."""
365 handling automatically special syntaxes."""
366
366
367 curr = str(self.cache.last_prompt)
367 curr = str(self.cache.last_prompt)
368 nrspaces = len(self.rspace.search(curr).group())
368 nrspaces = len(self.rspace.search(curr).group())
369 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
369 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
370 ' '*nrspaces,self.col_norm_ni)
370 ' '*nrspaces,self.col_norm_ni)
371
371
372 class PromptOut(BasePrompt):
372 class PromptOut(BasePrompt):
373 """Output interactive prompt similar to Mathematica's."""
373 """Output interactive prompt similar to Mathematica's."""
374
374
375 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
375 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
376 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
376 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
377 if not self.p_template:
377 if not self.p_template:
378 self.__str__ = lambda: ''
378 self.__str__ = lambda: ''
379
379
380 def set_colors(self):
380 def set_colors(self):
381 self.set_p_str()
381 self.set_p_str()
382 Colors = self.cache.color_table.active_colors # shorthand
382 Colors = self.cache.color_table.active_colors # shorthand
383 self.col_p = Colors.out_prompt
383 self.col_p = Colors.out_prompt
384 self.col_num = Colors.out_number
384 self.col_num = Colors.out_number
385 self.col_norm = Colors.normal
385 self.col_norm = Colors.normal
386
386
387 class Prompt2(BasePrompt):
387 class Prompt2(BasePrompt):
388 """Interactive continuation prompt."""
388 """Interactive continuation prompt."""
389
389
390 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
390 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
391 self.cache = cache
391 self.cache = cache
392 self.p_template = prompt
392 self.p_template = prompt
393 self.pad_left = pad_left
393 self.pad_left = pad_left
394 self.set_p_str()
394 self.set_p_str()
395
395
396 def set_p_str(self):
396 def set_p_str(self):
397 import os,time # needed in locals for prompt string handling
397 import os,time # needed in locals for prompt string handling
398 loc = locals()
398 loc = locals()
399 self.p_str = ItplNS('%s%s%s' %
399 self.p_str = ItplNS('%s%s%s' %
400 ('${self.col_p2}',
400 ('${self.col_p2}',
401 multiple_replace(prompt_specials, self.p_template),
401 multiple_replace(prompt_specials, self.p_template),
402 '$self.col_norm'),
402 '$self.col_norm'),
403 self.cache.user_ns,loc)
403 self.cache.user_ns,loc)
404 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
404 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
405 self.p_template),
405 self.p_template),
406 self.cache.user_ns,loc)
406 self.cache.user_ns,loc)
407
407
408 def set_colors(self):
408 def set_colors(self):
409 self.set_p_str()
409 self.set_p_str()
410 Colors = self.cache.color_table.active_colors
410 Colors = self.cache.color_table.active_colors
411 self.col_p2 = Colors.in_prompt2
411 self.col_p2 = Colors.in_prompt2
412 self.col_norm = Colors.in_normal
412 self.col_norm = Colors.in_normal
413 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
413 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
414 # updated their prompt_in2 definitions. Remove eventually.
414 # updated their prompt_in2 definitions. Remove eventually.
415 self.col_p = Colors.out_prompt
415 self.col_p = Colors.out_prompt
416 self.col_num = Colors.out_number
416 self.col_num = Colors.out_number
417
417
418
418
419 #-----------------------------------------------------------------------------
419 #-----------------------------------------------------------------------------
420 class CachedOutput:
420 class CachedOutput:
421 """Class for printing output from calculations while keeping a cache of
421 """Class for printing output from calculations while keeping a cache of
422 reults. It dynamically creates global variables prefixed with _ which
422 reults. It dynamically creates global variables prefixed with _ which
423 contain these results.
423 contain these results.
424
424
425 Meant to be used as a sys.displayhook replacement, providing numbered
425 Meant to be used as a sys.displayhook replacement, providing numbered
426 prompts and cache services.
426 prompts and cache services.
427
427
428 Initialize with initial and final values for cache counter (this defines
428 Initialize with initial and final values for cache counter (this defines
429 the maximum size of the cache."""
429 the maximum size of the cache."""
430
430
431 def __init__(self,shell,cache_size,Pprint,
431 def __init__(self,shell,cache_size,Pprint,
432 colors='NoColor',input_sep='\n',
432 colors='NoColor',input_sep='\n',
433 output_sep='\n',output_sep2='',
433 output_sep='\n',output_sep2='',
434 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
434 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
435
435
436 cache_size_min = 3
436 cache_size_min = 3
437 if cache_size <= 0:
437 if cache_size <= 0:
438 self.do_full_cache = 0
438 self.do_full_cache = 0
439 cache_size = 0
439 cache_size = 0
440 elif cache_size < cache_size_min:
440 elif cache_size < cache_size_min:
441 self.do_full_cache = 0
441 self.do_full_cache = 0
442 cache_size = 0
442 cache_size = 0
443 warn('caching was disabled (min value for cache size is %s).' %
443 warn('caching was disabled (min value for cache size is %s).' %
444 cache_size_min,level=3)
444 cache_size_min,level=3)
445 else:
445 else:
446 self.do_full_cache = 1
446 self.do_full_cache = 1
447
447
448 self.cache_size = cache_size
448 self.cache_size = cache_size
449 self.input_sep = input_sep
449 self.input_sep = input_sep
450
450
451 # we need a reference to the user-level namespace
451 # we need a reference to the user-level namespace
452 self.shell = shell
452 self.shell = shell
453 self.user_ns = shell.user_ns
453 self.user_ns = shell.user_ns
454 # and to the user's input
454 # and to the user's input
455 self.input_hist = shell.input_hist
455 self.input_hist = shell.input_hist
456 # and to the user's logger, for logging output
456 # and to the user's logger, for logging output
457 self.logger = shell.logger
457 self.logger = shell.logger
458
458
459 # Set input prompt strings and colors
459 # Set input prompt strings and colors
460 if cache_size == 0:
460 if cache_size == 0:
461 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
461 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
462 or ps1.find(r'\N') > -1:
462 or ps1.find(r'\N') > -1:
463 ps1 = '>>> '
463 ps1 = '>>> '
464 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
464 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
465 or ps2.find(r'\N') > -1:
465 or ps2.find(r'\N') > -1:
466 ps2 = '... '
466 ps2 = '... '
467 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
467 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
468 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
468 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
469 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
469 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
470
470
471 self.color_table = PromptColors
471 self.color_table = PromptColors
472 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
472 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
473 pad_left=pad_left)
473 pad_left=pad_left)
474 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
474 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
475 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
475 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
476 pad_left=pad_left)
476 pad_left=pad_left)
477 self.set_colors(colors)
477 self.set_colors(colors)
478
478
479 # other more normal stuff
479 # other more normal stuff
480 # b/c each call to the In[] prompt raises it by 1, even the first.
480 # b/c each call to the In[] prompt raises it by 1, even the first.
481 self.prompt_count = 0
481 self.prompt_count = 0
482 # Store the last prompt string each time, we need it for aligning
482 # Store the last prompt string each time, we need it for aligning
483 # continuation and auto-rewrite prompts
483 # continuation and auto-rewrite prompts
484 self.last_prompt = ''
484 self.last_prompt = ''
485 self.Pprint = Pprint
485 self.Pprint = Pprint
486 self.output_sep = output_sep
486 self.output_sep = output_sep
487 self.output_sep2 = output_sep2
487 self.output_sep2 = output_sep2
488 self._,self.__,self.___ = '','',''
488 self._,self.__,self.___ = '','',''
489 self.pprint_types = map(type,[(),[],{}])
489 self.pprint_types = map(type,[(),[],{}])
490
490
491 # these are deliberately global:
491 # these are deliberately global:
492 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
492 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
493 self.user_ns.update(to_user_ns)
493 self.user_ns.update(to_user_ns)
494
494
495 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
495 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
496 if p_str is None:
496 if p_str is None:
497 if self.do_full_cache:
497 if self.do_full_cache:
498 return cache_def
498 return cache_def
499 else:
499 else:
500 return no_cache_def
500 return no_cache_def
501 else:
501 else:
502 return p_str
502 return p_str
503
503
504 def set_colors(self,colors):
504 def set_colors(self,colors):
505 """Set the active color scheme and configure colors for the three
505 """Set the active color scheme and configure colors for the three
506 prompt subsystems."""
506 prompt subsystems."""
507
507
508 # FIXME: the prompt_specials global should be gobbled inside this
508 # FIXME: the prompt_specials global should be gobbled inside this
509 # class instead. Do it when cleaning up the whole 3-prompt system.
509 # class instead. Do it when cleaning up the whole 3-prompt system.
510 global prompt_specials
510 global prompt_specials
511 if colors.lower()=='nocolor':
511 if colors.lower()=='nocolor':
512 prompt_specials = prompt_specials_nocolor
512 prompt_specials = prompt_specials_nocolor
513 else:
513 else:
514 prompt_specials = prompt_specials_color
514 prompt_specials = prompt_specials_color
515
515
516 self.color_table.set_active_scheme(colors)
516 self.color_table.set_active_scheme(colors)
517 self.prompt1.set_colors()
517 self.prompt1.set_colors()
518 self.prompt2.set_colors()
518 self.prompt2.set_colors()
519 self.prompt_out.set_colors()
519 self.prompt_out.set_colors()
520
520
521 def __call__(self,arg=None):
521 def __call__(self,arg=None):
522 """Printing with history cache management.
522 """Printing with history cache management.
523
523
524 This is invoked everytime the interpreter needs to print, and is
524 This is invoked everytime the interpreter needs to print, and is
525 activated by setting the variable sys.displayhook to it."""
525 activated by setting the variable sys.displayhook to it."""
526
526
527 # If something injected a '_' variable in __builtin__, delete
527 # If something injected a '_' variable in __builtin__, delete
528 # ipython's automatic one so we don't clobber that. gettext() in
528 # ipython's automatic one so we don't clobber that. gettext() in
529 # particular uses _, so we need to stay away from it.
529 # particular uses _, so we need to stay away from it.
530 if '_' in __builtin__.__dict__:
530 if '_' in __builtin__.__dict__:
531 try:
531 try:
532 del self.user_ns['_']
532 del self.user_ns['_']
533 except KeyError:
533 except KeyError:
534 pass
534 pass
535 if arg is not None:
535 if arg is not None:
536 cout_write = Term.cout.write # fast lookup
536 cout_write = Term.cout.write # fast lookup
537 # first handle the cache and counters
537 # first handle the cache and counters
538
538
539 # do not print output if input ends in ';'
539 # do not print output if input ends in ';'
540 try:
540 try:
541 if self.input_hist[self.prompt_count].endswith(';\n'):
541 if self.input_hist[self.prompt_count].endswith(';\n'):
542 return
542 return
543 except IndexError:
543 except IndexError:
544 # some uses of ipshellembed may fail here
544 # some uses of ipshellembed may fail here
545 pass
545 pass
546 # don't use print, puts an extra space
546 # don't use print, puts an extra space
547 cout_write(self.output_sep)
547 cout_write(self.output_sep)
548 outprompt = self.shell.hooks.generate_output_prompt()
548 outprompt = self.shell.hooks.generate_output_prompt()
549 # print "Got prompt: ", outprompt
549 if self.do_full_cache:
550 if self.do_full_cache:
550 cout_write(outprompt)
551 cout_write(outprompt)
552 else:
553 print "self.do_full_cache = False"
551
554
552 # and now call a possibly user-defined print mechanism
555 # and now call a possibly user-defined print mechanism
553 manipulated_val = self.display(arg)
556 manipulated_val = self.display(arg)
554
557
555 # user display hooks can change the variable to be stored in
558 # user display hooks can change the variable to be stored in
556 # output history
559 # output history
557
560
558 if manipulated_val is not None:
561 if manipulated_val is not None:
559 arg = manipulated_val
562 arg = manipulated_val
560
563
561 # avoid recursive reference when displaying _oh/Out
564 # avoid recursive reference when displaying _oh/Out
562 if arg is not self.user_ns['_oh']:
565 if arg is not self.user_ns['_oh']:
563 self.update(arg)
566 self.update(arg)
564
567
565 if self.logger.log_output:
568 if self.logger.log_output:
566 self.logger.log_write(repr(arg),'output')
569 self.logger.log_write(repr(arg),'output')
567 cout_write(self.output_sep2)
570 cout_write(self.output_sep2)
568 Term.cout.flush()
571 Term.cout.flush()
569
572
570 def _display(self,arg):
573 def _display(self,arg):
571 """Default printer method, uses pprint.
574 """Default printer method, uses pprint.
572
575
573 Do ip.set_hook("result_display", my_displayhook) for custom result
576 Do ip.set_hook("result_display", my_displayhook) for custom result
574 display, e.g. when your own objects need special formatting.
577 display, e.g. when your own objects need special formatting.
575 """
578 """
576 try:
579 try:
577 return IPython.utils.generics.result_display(arg)
580 return IPython.utils.generics.result_display(arg)
578 except TryNext:
581 except TryNext:
579 return self.shell.hooks.result_display(arg)
582 return self.shell.hooks.result_display(arg)
580
583
581 # Assign the default display method:
584 # Assign the default display method:
582 display = _display
585 display = _display
583
586
584 def update(self,arg):
587 def update(self,arg):
585 #print '***cache_count', self.cache_count # dbg
588 #print '***cache_count', self.cache_count # dbg
586 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
589 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
587 warn('Output cache limit (currently '+
590 warn('Output cache limit (currently '+
588 `self.cache_size`+' entries) hit.\n'
591 `self.cache_size`+' entries) hit.\n'
589 'Flushing cache and resetting history counter...\n'
592 'Flushing cache and resetting history counter...\n'
590 'The only history variables available will be _,__,___ and _1\n'
593 'The only history variables available will be _,__,___ and _1\n'
591 'with the current result.')
594 'with the current result.')
592
595
593 self.flush()
596 self.flush()
594 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
597 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
595 # we cause buggy behavior for things like gettext).
598 # we cause buggy behavior for things like gettext).
596 if '_' not in __builtin__.__dict__:
599 if '_' not in __builtin__.__dict__:
597 self.___ = self.__
600 self.___ = self.__
598 self.__ = self._
601 self.__ = self._
599 self._ = arg
602 self._ = arg
600 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
603 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
601
604
602 # hackish access to top-level namespace to create _1,_2... dynamically
605 # hackish access to top-level namespace to create _1,_2... dynamically
603 to_main = {}
606 to_main = {}
604 if self.do_full_cache:
607 if self.do_full_cache:
605 new_result = '_'+`self.prompt_count`
608 new_result = '_'+`self.prompt_count`
606 to_main[new_result] = arg
609 to_main[new_result] = arg
607 self.user_ns.update(to_main)
610 self.user_ns.update(to_main)
608 self.user_ns['_oh'][self.prompt_count] = arg
611 self.user_ns['_oh'][self.prompt_count] = arg
609
612
610 def flush(self):
613 def flush(self):
611 if not self.do_full_cache:
614 if not self.do_full_cache:
612 raise ValueError,"You shouldn't have reached the cache flush "\
615 raise ValueError,"You shouldn't have reached the cache flush "\
613 "if full caching is not enabled!"
616 "if full caching is not enabled!"
614 # delete auto-generated vars from global namespace
617 # delete auto-generated vars from global namespace
615
618
616 for n in range(1,self.prompt_count + 1):
619 for n in range(1,self.prompt_count + 1):
617 key = '_'+`n`
620 key = '_'+`n`
618 try:
621 try:
619 del self.user_ns[key]
622 del self.user_ns[key]
620 except: pass
623 except: pass
621 self.user_ns['_oh'].clear()
624 self.user_ns['_oh'].clear()
622
625
623 if '_' not in __builtin__.__dict__:
626 if '_' not in __builtin__.__dict__:
624 self.user_ns.update({'_':None,'__':None, '___':None})
627 self.user_ns.update({'_':None,'__':None, '___':None})
625 import gc
628 import gc
626 gc.collect() # xxx needed?
629 gc.collect() # xxx needed?
627
630
@@ -1,34 +1,35 b''
1 """Test code for https://bugs.launchpad.net/ipython/+bug/239054
1 """Test code for https://bugs.launchpad.net/ipython/+bug/239054
2
2
3 WARNING: this script exits IPython! It MUST be run in a subprocess.
3 WARNING: this script exits IPython! It MUST be run in a subprocess.
4
4
5 When you run the following script from CPython it prints:
5 When you run the following script from CPython it prints:
6 __init__ is here
6 __init__ is here
7 __del__ is here
7 __del__ is here
8
8
9 and creates the __del__.txt file
9 and creates the __del__.txt file
10
10
11 When you run it from IPython it prints:
11 When you run it from IPython it prints:
12 __init__ is here
12 __init__ is here
13
13
14 When you exit() or Exit from IPython neothing is printed and no file is created
14 When you exit() or Exit from IPython neothing is printed and no file is created
15 (the file thing is to make sure __del__ is really never called and not that
15 (the file thing is to make sure __del__ is really never called and not that
16 just the output is eaten).
16 just the output is eaten).
17
17
18 Note that if you call %reset in IPython then everything is Ok.
18 Note that if you call %reset in IPython then everything is Ok.
19
19
20 IPython should do the equivalent of %reset and release all the references it
20 IPython should do the equivalent of %reset and release all the references it
21 holds before exit. This behavior is important when working with binding objects
21 holds before exit. This behavior is important when working with binding objects
22 that rely on __del__. If the current behavior has some use case then I suggest
22 that rely on __del__. If the current behavior has some use case then I suggest
23 to add a configuration option to IPython to control it.
23 to add a configuration option to IPython to control it.
24 """
24 """
25 import sys
25 import sys
26
26
27 class A(object):
27 class A(object):
28 def __del__(self):
28 def __del__(self):
29 print 'obj_del.py: object A deleted'
29 print 'obj_del.py: object A deleted'
30
30
31 a = A()
31 a = A()
32
32
33 # Now, we force an exit, the caller will check that the del printout was given
33 # Now, we force an exit, the caller will check that the del printout was given
34 _ip = get_ipython()
34 _ip.ask_exit()
35 _ip.ask_exit()
@@ -1,194 +1,194 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Tests for IPython.core.component
4 Tests for IPython.core.component
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez (design help)
9 * Fernando Perez (design help)
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 from unittest import TestCase
23 from unittest import TestCase
24
24
25 from IPython.core.component import Component, ComponentError
25 from IPython.core.component import Component, ComponentError
26 from IPython.utils.traitlets import (
26 from IPython.utils.traitlets import (
27 TraitletError, Int, Float, Str
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 # Test cases
33 # Test cases
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36
36
37 class TestComponentMeta(TestCase):
37 class TestComponentMeta(TestCase):
38
38
39 def test_get_instances(self):
39 def test_get_instances(self):
40 class BaseComponent(Component):
40 class BaseComponent(Component):
41 pass
41 pass
42 c1 = BaseComponent(None)
42 c1 = BaseComponent(None)
43 c2 = BaseComponent(c1)
43 c2 = BaseComponent(c1)
44 self.assertEquals(BaseComponent.get_instances(),[c1,c2])
44 self.assertEquals(BaseComponent.get_instances(),[c1,c2])
45
45
46 def test_get_instances_subclass(self):
46 def test_get_instances_subclass(self):
47 class MyComponent(Component):
47 class MyComponent(Component):
48 pass
48 pass
49 class MyOtherComponent(MyComponent):
49 class MyOtherComponent(MyComponent):
50 pass
50 pass
51 c1 = MyComponent(None)
51 c1 = MyComponent(None)
52 c2 = MyOtherComponent(c1)
52 c2 = MyOtherComponent(c1)
53 c3 = MyOtherComponent(c2)
53 c3 = MyOtherComponent(c2)
54 self.assertEquals(MyComponent.get_instances(), [c1, c2, c3])
54 self.assertEquals(MyComponent.get_instances(), [c1, c2, c3])
55 self.assertEquals(MyOtherComponent.get_instances(), [c2, c3])
55 self.assertEquals(MyOtherComponent.get_instances(), [c2, c3])
56
56
57 def test_get_instances_root(self):
57 def test_get_instances_root(self):
58 class MyComponent(Component):
58 class MyComponent(Component):
59 pass
59 pass
60 class MyOtherComponent(MyComponent):
60 class MyOtherComponent(MyComponent):
61 pass
61 pass
62 c1 = MyComponent(None)
62 c1 = MyComponent(None)
63 c2 = MyOtherComponent(c1)
63 c2 = MyOtherComponent(c1)
64 c3 = MyOtherComponent(c2)
64 c3 = MyOtherComponent(c2)
65 c4 = MyComponent(None)
65 c4 = MyComponent(None)
66 c5 = MyComponent(c4)
66 c5 = MyComponent(c4)
67 self.assertEquals(MyComponent.get_instances(root=c1), [c1, c2, c3])
67 self.assertEquals(MyComponent.get_instances(root=c1), [c1, c2, c3])
68 self.assertEquals(MyComponent.get_instances(root=c4), [c4, c5])
68 self.assertEquals(MyComponent.get_instances(root=c4), [c4, c5])
69
69
70
70
71 class TestComponent(TestCase):
71 class TestComponent(TestCase):
72
72
73 def test_parent_child(self):
73 def test_parent_child(self):
74 c1 = Component(None)
74 c1 = Component(None)
75 c2 = Component(c1)
75 c2 = Component(c1)
76 c3 = Component(c1)
76 c3 = Component(c1)
77 c4 = Component(c3)
77 c4 = Component(c3)
78 self.assertEquals(c1.parent, None)
78 self.assertEquals(c1.parent, None)
79 self.assertEquals(c2.parent, c1)
79 self.assertEquals(c2.parent, c1)
80 self.assertEquals(c3.parent, c1)
80 self.assertEquals(c3.parent, c1)
81 self.assertEquals(c4.parent, c3)
81 self.assertEquals(c4.parent, c3)
82 self.assertEquals(c1.children, [c2, c3])
82 self.assertEquals(c1.children, [c2, c3])
83 self.assertEquals(c2.children, [])
83 self.assertEquals(c2.children, [])
84 self.assertEquals(c3.children, [c4])
84 self.assertEquals(c3.children, [c4])
85 self.assertEquals(c4.children, [])
85 self.assertEquals(c4.children, [])
86
86
87 def test_root(self):
87 def test_root(self):
88 c1 = Component(None)
88 c1 = Component(None)
89 c2 = Component(c1)
89 c2 = Component(c1)
90 c3 = Component(c1)
90 c3 = Component(c1)
91 c4 = Component(c3)
91 c4 = Component(c3)
92 self.assertEquals(c1.root, c1.root)
92 self.assertEquals(c1.root, c1.root)
93 self.assertEquals(c2.root, c1)
93 self.assertEquals(c2.root, c1)
94 self.assertEquals(c3.root, c1)
94 self.assertEquals(c3.root, c1)
95 self.assertEquals(c4.root, c1)
95 self.assertEquals(c4.root, c1)
96
96
97 def test_change_parent(self):
97 def test_change_parent(self):
98 c1 = Component(None)
98 c1 = Component(None)
99 c2 = Component(None)
99 c2 = Component(None)
100 c3 = Component(c1)
100 c3 = Component(c1)
101 self.assertEquals(c3.root, c1)
101 self.assertEquals(c3.root, c1)
102 self.assertEquals(c3.parent, c1)
102 self.assertEquals(c3.parent, c1)
103 self.assertEquals(c1.children,[c3])
103 self.assertEquals(c1.children,[c3])
104 c3.parent = c2
104 c3.parent = c2
105 self.assertEquals(c3.root, c2)
105 self.assertEquals(c3.root, c2)
106 self.assertEquals(c3.parent, c2)
106 self.assertEquals(c3.parent, c2)
107 self.assertEquals(c2.children,[c3])
107 self.assertEquals(c2.children,[c3])
108 self.assertEquals(c1.children,[])
108 self.assertEquals(c1.children,[])
109
109
110 def test_subclass_parent(self):
110 def test_subclass_parent(self):
111 c1 = Component(None)
111 c1 = Component(None)
112 self.assertRaises(TraitletError, setattr, c1, 'parent', 10)
112 self.assertRaises(TraitletError, setattr, c1, 'parent', 10)
113
113
114 class MyComponent(Component):
114 class MyComponent(Component):
115 pass
115 pass
116 c1 = Component(None)
116 c1 = Component(None)
117 c2 = MyComponent(c1)
117 c2 = MyComponent(c1)
118 self.assertEquals(MyComponent.parent.this_class, Component)
118 self.assertEquals(MyComponent.parent.this_class, Component)
119 self.assertEquals(c2.parent, c1)
119 self.assertEquals(c2.parent, c1)
120
120
121 def test_bad_root(self):
121 def test_bad_root(self):
122 c1 = Component(None)
122 c1 = Component(None)
123 c2 = Component(None)
123 c2 = Component(None)
124 c3 = Component(None)
124 c3 = Component(None)
125 self.assertRaises(ComponentError, setattr, c1, 'root', c2)
125 self.assertRaises(ComponentError, setattr, c1, 'root', c2)
126 c1.parent = c2
126 c1.parent = c2
127 self.assertEquals(c1.root, c2)
127 self.assertEquals(c1.root, c2)
128 self.assertRaises(ComponentError, setattr, c1, 'root', c3)
128 self.assertRaises(ComponentError, setattr, c1, 'root', c3)
129
129
130
130
131 class TestComponentConfig(TestCase):
131 class TestComponentConfig(TestCase):
132
132
133 def test_default(self):
133 def test_default(self):
134 c1 = Component(None)
134 c1 = Component(None)
135 c2 = Component(c1)
135 c2 = Component(c1)
136 c3 = Component(c2)
136 c3 = Component(c2)
137 self.assertEquals(c1.config, c2.config)
137 self.assertEquals(c1.config, c2.config)
138 self.assertEquals(c2.config, c3.config)
138 self.assertEquals(c2.config, c3.config)
139
139
140 def test_custom(self):
140 def test_custom(self):
141 config = Struct()
141 config = Config()
142 config.FOO = 'foo'
142 config.foo = 'foo'
143 config.BAR = 'bar'
143 config.bar = 'bar'
144 c1 = Component(None, config=config)
144 c1 = Component(None, config=config)
145 c2 = Component(c1)
145 c2 = Component(c1)
146 c3 = Component(c2)
146 c3 = Component(c2)
147 self.assertEquals(c1.config, config)
147 self.assertEquals(c1.config, config)
148 self.assertEquals(c2.config, config)
148 self.assertEquals(c2.config, config)
149 self.assertEquals(c3.config, config)
149 self.assertEquals(c3.config, config)
150 # Test that we always make copies
150 # Test that we always make copies
151 self.assert_(c1.config is not config)
151 self.assert_(c1.config is not config)
152 self.assert_(c2.config is not config)
152 self.assert_(c2.config is not config)
153 self.assert_(c3.config is not config)
153 self.assert_(c3.config is not config)
154 self.assert_(c1.config is not c2.config)
154 self.assert_(c1.config is not c2.config)
155 self.assert_(c2.config is not c3.config)
155 self.assert_(c2.config is not c3.config)
156
156
157 def test_inheritance(self):
157 def test_inheritance(self):
158 class MyComponent(Component):
158 class MyComponent(Component):
159 a = Int(1, config_key='A')
159 a = Int(1, config=True)
160 b = Float(1.0, config_key='B')
160 b = Float(1.0, config=True)
161 c = Str('no config')
161 c = Str('no config')
162 config = Struct()
162 config = Config()
163 config.A = 2
163 config.MyComponent.a = 2
164 config.B = 2.0
164 config.MyComponent.b = 2.0
165 c1 = MyComponent(None, config=config)
165 c1 = MyComponent(None, config=config)
166 c2 = MyComponent(c1)
166 c2 = MyComponent(c1)
167 self.assertEquals(c1.a, config.A)
167 self.assertEquals(c1.a, config.MyComponent.a)
168 self.assertEquals(c1.b, config.B)
168 self.assertEquals(c1.b, config.MyComponent.b)
169 self.assertEquals(c2.a, config.A)
169 self.assertEquals(c2.a, config.MyComponent.a)
170 self.assertEquals(c2.b, config.B)
170 self.assertEquals(c2.b, config.MyComponent.b)
171 c4 = MyComponent(c2, config=Struct())
171 c4 = MyComponent(c2, config=Config())
172 self.assertEquals(c4.a, 1)
172 self.assertEquals(c4.a, 1)
173 self.assertEquals(c4.b, 1.0)
173 self.assertEquals(c4.b, 1.0)
174
174
175 class TestComponentName(TestCase):
175 class TestComponentName(TestCase):
176
176
177 def test_default(self):
177 def test_default(self):
178 class MyComponent(Component):
178 class MyComponent(Component):
179 pass
179 pass
180 c1 = Component(None)
180 c1 = Component(None)
181 c2 = MyComponent(None)
181 c2 = MyComponent(None)
182 c3 = Component(c2)
182 c3 = Component(c2)
183 self.assertNotEquals(c1.name, c2.name)
183 self.assertNotEquals(c1.name, c2.name)
184 self.assertNotEquals(c1.name, c3.name)
184 self.assertNotEquals(c1.name, c3.name)
185
185
186 def test_manual(self):
186 def test_manual(self):
187 class MyComponent(Component):
187 class MyComponent(Component):
188 pass
188 pass
189 c1 = Component(None, name='foo')
189 c1 = Component(None, name='foo')
190 c2 = MyComponent(None, name='bar')
190 c2 = MyComponent(None, name='bar')
191 c3 = Component(c2, name='bah')
191 c3 = Component(c2, name='bah')
192 self.assertEquals(c1.name, 'foo')
192 self.assertEquals(c1.name, 'foo')
193 self.assertEquals(c2.name, 'bar')
193 self.assertEquals(c2.name, 'bar')
194 self.assertEquals(c3.name, 'bah')
194 self.assertEquals(c3.name, 'bah')
@@ -1,326 +1,331 b''
1 """Tests for various magic functions.
1 """Tests for various magic functions.
2
2
3 Needs to be run by nose (to make ipython session available).
3 Needs to be run by nose (to make ipython session available).
4 """
4 """
5
5
6 import os
6 import os
7 import sys
7 import sys
8 import tempfile
8 import tempfile
9 import types
9 import types
10 from cStringIO import StringIO
10 from cStringIO import StringIO
11
11
12 import nose.tools as nt
12 import nose.tools as nt
13
13
14 from IPython.utils.platutils import find_cmd, get_long_path_name
14 from IPython.utils.platutils import find_cmd, get_long_path_name
15 from IPython.testing import decorators as dec
15 from IPython.testing import decorators as dec
16 from IPython.testing import tools as tt
16 from IPython.testing import tools as tt
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Test functions begin
19 # Test functions begin
20
20
21 def test_rehashx():
21 def test_rehashx():
22 # clear up everything
22 # clear up everything
23 _ip.alias_table.clear()
23 _ip = get_ipython()
24 _ip.alias_manager.alias_table.clear()
24 del _ip.db['syscmdlist']
25 del _ip.db['syscmdlist']
25
26
26 _ip.magic('rehashx')
27 _ip.magic('rehashx')
27 # Practically ALL ipython development systems will have more than 10 aliases
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 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
30 for key, val in _ip.alias_table.items():
31 for key, val in _ip.alias_manager.alias_table.items():
31 # we must strip dots from alias names
32 # we must strip dots from alias names
32 nt.assert_true('.' not in key)
33 nt.assert_true('.' not in key)
33
34
34 # rehashx must fill up syscmdlist
35 # rehashx must fill up syscmdlist
35 scoms = _ip.db['syscmdlist']
36 scoms = _ip.db['syscmdlist']
36 yield (nt.assert_true, len(scoms) > 10)
37 yield (nt.assert_true, len(scoms) > 10)
37
38
38
39
39 def doctest_hist_f():
40 def doctest_hist_f():
40 """Test %hist -f with temporary filename.
41 """Test %hist -f with temporary filename.
41
42
42 In [9]: import tempfile
43 In [9]: import tempfile
43
44
44 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
45 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
45
46
46 In [11]: %hist -n -f $tfile 3
47 In [11]: %hist -n -f $tfile 3
47 """
48 """
48
49
49
50
50 def doctest_hist_r():
51 def doctest_hist_r():
51 """Test %hist -r
52 """Test %hist -r
52
53
53 XXX - This test is not recording the output correctly. Not sure why...
54 XXX - This test is not recording the output correctly. Not sure why...
54
55
55 In [20]: 'hist' in _ip.lsmagic()
56 In [20]: 'hist' in _ip.lsmagic()
56 Out[20]: True
57 Out[20]: True
57
58
58 In [6]: x=1
59 In [6]: x=1
59
60
60 In [7]: %hist -n -r 2
61 In [7]: %hist -n -r 2
61 x=1 # random
62 x=1 # random
62 hist -n -r 2 # random
63 hist -n -r 2 # random
63 """
64 """
64
65
65 # This test is known to fail on win32.
66 # This test is known to fail on win32.
66 # See ticket https://bugs.launchpad.net/bugs/366334
67 # See ticket https://bugs.launchpad.net/bugs/366334
67 def test_obj_del():
68 def test_obj_del():
69 _ip = get_ipython()
68 """Test that object's __del__ methods are called on exit."""
70 """Test that object's __del__ methods are called on exit."""
69 test_dir = os.path.dirname(__file__)
71 test_dir = os.path.dirname(__file__)
70 del_file = os.path.join(test_dir,'obj_del.py')
72 del_file = os.path.join(test_dir,'obj_del.py')
71 ipython_cmd = find_cmd('ipython')
73 ipython_cmd = find_cmd('ipython')
72 out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))
74 out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))
73 nt.assert_equals(out,'obj_del.py: object A deleted')
75 nt.assert_equals(out,'obj_del.py: object A deleted')
74
76
75
77
76 def test_shist():
78 def test_shist():
77 # Simple tests of ShadowHist class - test generator.
79 # Simple tests of ShadowHist class - test generator.
78 import os, shutil, tempfile
80 import os, shutil, tempfile
79
81
80 from IPython.extensions import pickleshare
82 from IPython.extensions import pickleshare
81 from IPython.core.history import ShadowHist
83 from IPython.core.history import ShadowHist
82
84
83 tfile = tempfile.mktemp('','tmp-ipython-')
85 tfile = tempfile.mktemp('','tmp-ipython-')
84
86
85 db = pickleshare.PickleShareDB(tfile)
87 db = pickleshare.PickleShareDB(tfile)
86 s = ShadowHist(db)
88 s = ShadowHist(db)
87 s.add('hello')
89 s.add('hello')
88 s.add('world')
90 s.add('world')
89 s.add('hello')
91 s.add('hello')
90 s.add('hello')
92 s.add('hello')
91 s.add('karhu')
93 s.add('karhu')
92
94
93 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
95 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
94
96
95 yield nt.assert_equal,s.get(2),'world'
97 yield nt.assert_equal,s.get(2),'world'
96
98
97 shutil.rmtree(tfile)
99 shutil.rmtree(tfile)
98
100
99 @dec.skipif_not_numpy
101 @dec.skipif_not_numpy
100 def test_numpy_clear_array_undec():
102 def test_numpy_clear_array_undec():
101 from IPython.extensions import clearcmd
103 from IPython.extensions import clearcmd
102
104
103 _ip.ex('import numpy as np')
105 _ip.ex('import numpy as np')
104 _ip.ex('a = np.empty(2)')
106 _ip.ex('a = np.empty(2)')
105 yield (nt.assert_true, 'a' in _ip.user_ns)
107 yield (nt.assert_true, 'a' in _ip.user_ns)
106 _ip.magic('clear array')
108 _ip.magic('clear array')
107 yield (nt.assert_false, 'a' in _ip.user_ns)
109 yield (nt.assert_false, 'a' in _ip.user_ns)
108
110
109
111
110 @dec.skip()
112 @dec.skip()
111 def test_fail_dec(*a,**k):
113 def test_fail_dec(*a,**k):
112 yield nt.assert_true, False
114 yield nt.assert_true, False
113
115
114 @dec.skip('This one shouldn not run')
116 @dec.skip('This one shouldn not run')
115 def test_fail_dec2(*a,**k):
117 def test_fail_dec2(*a,**k):
116 yield nt.assert_true, False
118 yield nt.assert_true, False
117
119
118 @dec.skipknownfailure
120 @dec.skipknownfailure
119 def test_fail_dec3(*a,**k):
121 def test_fail_dec3(*a,**k):
120 yield nt.assert_true, False
122 yield nt.assert_true, False
121
123
122
124
123 def doctest_refbug():
125 def doctest_refbug():
124 """Very nasty problem with references held by multiple runs of a script.
126 """Very nasty problem with references held by multiple runs of a script.
125 See: https://bugs.launchpad.net/ipython/+bug/269966
127 See: https://bugs.launchpad.net/ipython/+bug/269966
126
128
127 In [1]: _ip.clear_main_mod_cache()
129 In [1]: _ip.clear_main_mod_cache()
128
130
129 In [2]: run refbug
131 In [2]: run refbug
130
132
131 In [3]: call_f()
133 In [3]: call_f()
132 lowercased: hello
134 lowercased: hello
133
135
134 In [4]: run refbug
136 In [4]: run refbug
135
137
136 In [5]: call_f()
138 In [5]: call_f()
137 lowercased: hello
139 lowercased: hello
138 lowercased: hello
140 lowercased: hello
139 """
141 """
140
142
141 #-----------------------------------------------------------------------------
143 #-----------------------------------------------------------------------------
142 # Tests for %run
144 # Tests for %run
143 #-----------------------------------------------------------------------------
145 #-----------------------------------------------------------------------------
144
146
145 # %run is critical enough that it's a good idea to have a solid collection of
147 # %run is critical enough that it's a good idea to have a solid collection of
146 # tests for it, some as doctests and some as normal tests.
148 # tests for it, some as doctests and some as normal tests.
147
149
148 def doctest_run_ns():
150 def doctest_run_ns():
149 """Classes declared %run scripts must be instantiable afterwards.
151 """Classes declared %run scripts must be instantiable afterwards.
150
152
151 In [11]: run tclass foo
153 In [11]: run tclass foo
152
154
153 In [12]: isinstance(f(),foo)
155 In [12]: isinstance(f(),foo)
154 Out[12]: True
156 Out[12]: True
155 """
157 """
156
158
157
159
158 def doctest_run_ns2():
160 def doctest_run_ns2():
159 """Classes declared %run scripts must be instantiable afterwards.
161 """Classes declared %run scripts must be instantiable afterwards.
160
162
161 In [4]: run tclass C-first_pass
163 In [4]: run tclass C-first_pass
162
164
163 In [5]: run tclass C-second_pass
165 In [5]: run tclass C-second_pass
164 tclass.py: deleting object: C-first_pass
166 tclass.py: deleting object: C-first_pass
165 """
167 """
166
168
167 def doctest_run_builtins():
169 def doctest_run_builtins():
168 """Check that %run doesn't damage __builtins__ via a doctest.
170 """Check that %run doesn't damage __builtins__ via a doctest.
169
171
170 This is similar to the test_run_builtins, but I want *both* forms of the
172 This is similar to the test_run_builtins, but I want *both* forms of the
171 test to catch any possible glitches in our testing machinery, since that
173 test to catch any possible glitches in our testing machinery, since that
172 modifies %run somewhat. So for this, we have both a normal test (below)
174 modifies %run somewhat. So for this, we have both a normal test (below)
173 and a doctest (this one).
175 and a doctest (this one).
174
176
175 In [1]: import tempfile
177 In [1]: import tempfile
176
178
177 In [2]: bid1 = id(__builtins__)
179 In [2]: bid1 = id(__builtins__)
178
180
179 In [3]: fname = tempfile.mkstemp()[1]
181 In [3]: fname = tempfile.mkstemp()[1]
180
182
181 In [3]: f = open(fname,'w')
183 In [3]: f = open(fname,'w')
182
184
183 In [4]: f.write('pass\\n')
185 In [4]: f.write('pass\\n')
184
186
185 In [5]: f.flush()
187 In [5]: f.flush()
186
188
187 In [6]: print type(__builtins__)
189 In [6]: print type(__builtins__)
188 <type 'module'>
190 <type 'module'>
189
191
190 In [7]: %run "$fname"
192 In [7]: %run "$fname"
191
193
192 In [7]: f.close()
194 In [7]: f.close()
193
195
194 In [8]: bid2 = id(__builtins__)
196 In [8]: bid2 = id(__builtins__)
195
197
196 In [9]: print type(__builtins__)
198 In [9]: print type(__builtins__)
197 <type 'module'>
199 <type 'module'>
198
200
199 In [10]: bid1 == bid2
201 In [10]: bid1 == bid2
200 Out[10]: True
202 Out[10]: True
201
203
202 In [12]: try:
204 In [12]: try:
203 ....: os.unlink(fname)
205 ....: os.unlink(fname)
204 ....: except:
206 ....: except:
205 ....: pass
207 ....: pass
206 ....:
208 ....:
207 """
209 """
208
210
209 # For some tests, it will be handy to organize them in a class with a common
211 # For some tests, it will be handy to organize them in a class with a common
210 # setup that makes a temp file
212 # setup that makes a temp file
211
213
212 class TestMagicRun(object):
214 class TestMagicRun(object):
213
215
214 def setup(self):
216 def setup(self):
215 """Make a valid python temp file."""
217 """Make a valid python temp file."""
216 fname = tempfile.mkstemp()[1]
218 fname = tempfile.mkstemp()[1]
217 f = open(fname,'w')
219 f = open(fname,'w')
218 f.write('pass\n')
220 f.write('pass\n')
219 f.flush()
221 f.flush()
220 self.tmpfile = f
222 self.tmpfile = f
221 self.fname = fname
223 self.fname = fname
222
224
223 def run_tmpfile(self):
225 def run_tmpfile(self):
226 _ip = get_ipython()
224 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
227 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
225 # See below and ticket https://bugs.launchpad.net/bugs/366353
228 # See below and ticket https://bugs.launchpad.net/bugs/366353
226 _ip.magic('run "%s"' % self.fname)
229 _ip.magic('run "%s"' % self.fname)
227
230
228 def test_builtins_id(self):
231 def test_builtins_id(self):
229 """Check that %run doesn't damage __builtins__ """
232 """Check that %run doesn't damage __builtins__ """
230
233 _ip = get_ipython()
231 # Test that the id of __builtins__ is not modified by %run
234 # Test that the id of __builtins__ is not modified by %run
232 bid1 = id(_ip.user_ns['__builtins__'])
235 bid1 = id(_ip.user_ns['__builtins__'])
233 self.run_tmpfile()
236 self.run_tmpfile()
234 bid2 = id(_ip.user_ns['__builtins__'])
237 bid2 = id(_ip.user_ns['__builtins__'])
235 tt.assert_equals(bid1, bid2)
238 tt.assert_equals(bid1, bid2)
236
239
237 def test_builtins_type(self):
240 def test_builtins_type(self):
238 """Check that the type of __builtins__ doesn't change with %run.
241 """Check that the type of __builtins__ doesn't change with %run.
239
242
240 However, the above could pass if __builtins__ was already modified to
243 However, the above could pass if __builtins__ was already modified to
241 be a dict (it should be a module) by a previous use of %run. So we
244 be a dict (it should be a module) by a previous use of %run. So we
242 also check explicitly that it really is a module:
245 also check explicitly that it really is a module:
243 """
246 """
247 _ip = get_ipython()
244 self.run_tmpfile()
248 self.run_tmpfile()
245 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
249 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
246
250
247 def test_prompts(self):
251 def test_prompts(self):
248 """Test that prompts correctly generate after %run"""
252 """Test that prompts correctly generate after %run"""
249 self.run_tmpfile()
253 self.run_tmpfile()
254 _ip = get_ipython()
250 p2 = str(_ip.outputcache.prompt2).strip()
255 p2 = str(_ip.outputcache.prompt2).strip()
251 nt.assert_equals(p2[:3], '...')
256 nt.assert_equals(p2[:3], '...')
252
257
253 def teardown(self):
258 def teardown(self):
254 self.tmpfile.close()
259 self.tmpfile.close()
255 try:
260 try:
256 os.unlink(self.fname)
261 os.unlink(self.fname)
257 except:
262 except:
258 # On Windows, even though we close the file, we still can't delete
263 # On Windows, even though we close the file, we still can't delete
259 # it. I have no clue why
264 # it. I have no clue why
260 pass
265 pass
261
266
262 # Multiple tests for clipboard pasting
267 # Multiple tests for clipboard pasting
263 def test_paste():
268 def test_paste():
264
269 _ip = get_ipython()
265 def paste(txt, flags='-q'):
270 def paste(txt, flags='-q'):
266 """Paste input text, by default in quiet mode"""
271 """Paste input text, by default in quiet mode"""
267 hooks.clipboard_get = lambda : txt
272 hooks.clipboard_get = lambda : txt
268 _ip.magic('paste '+flags)
273 _ip.magic('paste '+flags)
269
274
270 # Inject fake clipboard hook but save original so we can restore it later
275 # Inject fake clipboard hook but save original so we can restore it later
271 hooks = _ip.hooks
276 hooks = _ip.hooks
272 user_ns = _ip.user_ns
277 user_ns = _ip.user_ns
273 original_clip = hooks.clipboard_get
278 original_clip = hooks.clipboard_get
274
279
275 try:
280 try:
276 # This try/except with an emtpy except clause is here only because
281 # This try/except with an emtpy except clause is here only because
277 # try/yield/finally is invalid syntax in Python 2.4. This will be
282 # try/yield/finally is invalid syntax in Python 2.4. This will be
278 # removed when we drop 2.4-compatibility, and the emtpy except below
283 # removed when we drop 2.4-compatibility, and the emtpy except below
279 # will be changed to a finally.
284 # will be changed to a finally.
280
285
281 # Run tests with fake clipboard function
286 # Run tests with fake clipboard function
282 user_ns.pop('x', None)
287 user_ns.pop('x', None)
283 paste('x=1')
288 paste('x=1')
284 yield (nt.assert_equal, user_ns['x'], 1)
289 yield (nt.assert_equal, user_ns['x'], 1)
285
290
286 user_ns.pop('x', None)
291 user_ns.pop('x', None)
287 paste('>>> x=2')
292 paste('>>> x=2')
288 yield (nt.assert_equal, user_ns['x'], 2)
293 yield (nt.assert_equal, user_ns['x'], 2)
289
294
290 paste("""
295 paste("""
291 >>> x = [1,2,3]
296 >>> x = [1,2,3]
292 >>> y = []
297 >>> y = []
293 >>> for i in x:
298 >>> for i in x:
294 ... y.append(i**2)
299 ... y.append(i**2)
295 ...
300 ...
296 """)
301 """)
297 yield (nt.assert_equal, user_ns['x'], [1,2,3])
302 yield (nt.assert_equal, user_ns['x'], [1,2,3])
298 yield (nt.assert_equal, user_ns['y'], [1,4,9])
303 yield (nt.assert_equal, user_ns['y'], [1,4,9])
299
304
300 # Now, test that paste -r works
305 # Now, test that paste -r works
301 user_ns.pop('x', None)
306 user_ns.pop('x', None)
302 yield (nt.assert_false, 'x' in user_ns)
307 yield (nt.assert_false, 'x' in user_ns)
303 _ip.magic('paste -r')
308 _ip.magic('paste -r')
304 yield (nt.assert_equal, user_ns['x'], [1,2,3])
309 yield (nt.assert_equal, user_ns['x'], [1,2,3])
305
310
306 # Also test paste echoing, by temporarily faking the writer
311 # Also test paste echoing, by temporarily faking the writer
307 w = StringIO()
312 w = StringIO()
308 writer = _ip.write
313 writer = _ip.write
309 _ip.write = w.write
314 _ip.write = w.write
310 code = """
315 code = """
311 a = 100
316 a = 100
312 b = 200"""
317 b = 200"""
313 try:
318 try:
314 paste(code,'')
319 paste(code,'')
315 out = w.getvalue()
320 out = w.getvalue()
316 finally:
321 finally:
317 _ip.write = writer
322 _ip.write = writer
318 yield (nt.assert_equal, user_ns['a'], 100)
323 yield (nt.assert_equal, user_ns['a'], 100)
319 yield (nt.assert_equal, user_ns['b'], 200)
324 yield (nt.assert_equal, user_ns['b'], 200)
320 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
325 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
321
326
322 finally:
327 finally:
323 # This should be in a finally clause, instead of the bare except above.
328 # This should be in a finally clause, instead of the bare except above.
324 # Restore original hook
329 # Restore original hook
325 hooks.clipboard_get = original_clip
330 hooks.clipboard_get = original_clip
326
331
@@ -1,58 +1,58 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: new prefilters for output grabbing
2 """ IPython extension: new prefilters for output grabbing
3
3
4 Provides
4 Provides
5
5
6 var = %magic blah blah
6 var = %magic blah blah
7
7
8 var = !ls
8 var = !ls
9 """
9 """
10
10
11 from IPython.core import ipapi
11 from IPython.core import ipapi
12 from IPython.core.error import TryNext
12 from IPython.core.error import TryNext
13 from IPython.utils.genutils import *
13 from IPython.utils.genutils import *
14
14
15 ip = ipapi.get()
15 ip = ipapi.get()
16
16
17 import re
17 import re
18
18
19 def hnd_magic(line,mo):
19 def hnd_magic(line,mo):
20 """ Handle a = %mymagic blah blah """
20 """ Handle a = %mymagic blah blah """
21 var = mo.group('varname')
21 var = mo.group('varname')
22 cmd = mo.group('cmd')
22 cmd = mo.group('cmd')
23 expr = make_quoted_expr(cmd)
23 expr = make_quoted_expr(cmd)
24 return itpl('$var = _ip.magic($expr)')
24 return itpl('$var = get_ipython().magic($expr)')
25
25
26 def hnd_syscmd(line,mo):
26 def hnd_syscmd(line,mo):
27 """ Handle a = !ls """
27 """ Handle a = !ls """
28 var = mo.group('varname')
28 var = mo.group('varname')
29 cmd = mo.group('cmd')
29 cmd = mo.group('cmd')
30 expr = make_quoted_expr(itpl("sc -l =$cmd"))
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 def install_re_handler(pat, hnd):
33 def install_re_handler(pat, hnd):
34 ip.meta.re_prefilters.append((re.compile(pat), hnd))
34 ip.meta.re_prefilters.append((re.compile(pat), hnd))
35
35
36 def init_handlers():
36 def init_handlers():
37
37
38 ip.meta.re_prefilters = []
38 ip.meta.re_prefilters = []
39
39
40 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
40 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
41 hnd_magic
41 hnd_magic
42 )
42 )
43
43
44 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
44 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
45 hnd_syscmd
45 hnd_syscmd
46 )
46 )
47
47
48 init_handlers()
48 init_handlers()
49
49
50 def regex_prefilter_f(self,line):
50 def regex_prefilter_f(self,line):
51 for pat, handler in ip.meta.re_prefilters:
51 for pat, handler in ip.meta.re_prefilters:
52 mo = pat.match(line)
52 mo = pat.match(line)
53 if mo:
53 if mo:
54 return handler(line,mo)
54 return handler(line,mo)
55
55
56 raise TryNext
56 raise TryNext
57
57
58 ip.set_hook('input_prefilter', regex_prefilter_f)
58 ip.set_hook('input_prefilter', regex_prefilter_f)
@@ -1,2328 +1,2328 b''
1 # -*- coding: iso-8859-1 -*-
1 # -*- coding: iso-8859-1 -*-
2
2
3 """
3 """
4 ``ipipe`` provides classes to be used in an interactive Python session. Doing a
4 ``ipipe`` provides classes to be used in an interactive Python session. Doing a
5 ``from ipipe import *`` is the preferred way to do this. The name of all
5 ``from ipipe import *`` is the preferred way to do this. The name of all
6 objects imported this way starts with ``i`` to minimize collisions.
6 objects imported this way starts with ``i`` to minimize collisions.
7
7
8 ``ipipe`` supports "pipeline expressions", which is something resembling Unix
8 ``ipipe`` supports "pipeline expressions", which is something resembling Unix
9 pipes. An example is::
9 pipes. An example is::
10
10
11 >>> ienv | isort("key.lower()")
11 >>> ienv | isort("key.lower()")
12
12
13 This gives a listing of all environment variables sorted by name.
13 This gives a listing of all environment variables sorted by name.
14
14
15
15
16 There are three types of objects in a pipeline expression:
16 There are three types of objects in a pipeline expression:
17
17
18 * ``Table``s: These objects produce items. Examples are ``ils`` (listing the
18 * ``Table``s: These objects produce items. Examples are ``ils`` (listing the
19 current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing
19 current directory, ``ienv`` (listing environment variables), ``ipwd`` (listing
20 user accounts) and ``igrp`` (listing user groups). A ``Table`` must be the
20 user accounts) and ``igrp`` (listing user groups). A ``Table`` must be the
21 first object in a pipe expression.
21 first object in a pipe expression.
22
22
23 * ``Pipe``s: These objects sit in the middle of a pipe expression. They
23 * ``Pipe``s: These objects sit in the middle of a pipe expression. They
24 transform the input in some way (e.g. filtering or sorting it). Examples are:
24 transform the input in some way (e.g. filtering or sorting it). Examples are:
25 ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input
25 ``ifilter`` (which filters the input pipe), ``isort`` (which sorts the input
26 pipe) and ``ieval`` (which evaluates a function or expression for each object
26 pipe) and ``ieval`` (which evaluates a function or expression for each object
27 in the input pipe).
27 in the input pipe).
28
28
29 * ``Display``s: These objects can be put as the last object in a pipeline
29 * ``Display``s: These objects can be put as the last object in a pipeline
30 expression. There are responsible for displaying the result of the pipeline
30 expression. There are responsible for displaying the result of the pipeline
31 expression. If a pipeline expression doesn't end in a display object a default
31 expression. If a pipeline expression doesn't end in a display object a default
32 display objects will be used. One example is ``ibrowse`` which is a ``curses``
32 display objects will be used. One example is ``ibrowse`` which is a ``curses``
33 based browser.
33 based browser.
34
34
35
35
36 Adding support for pipeline expressions to your own objects can be done through
36 Adding support for pipeline expressions to your own objects can be done through
37 three extensions points (all of them optional):
37 three extensions points (all of them optional):
38
38
39 * An object that will be displayed as a row by a ``Display`` object should
39 * An object that will be displayed as a row by a ``Display`` object should
40 implement the method ``__xattrs__(self, mode)`` method or register an
40 implement the method ``__xattrs__(self, mode)`` method or register an
41 implementation of the generic function ``xattrs``. For more info see ``xattrs``.
41 implementation of the generic function ``xattrs``. For more info see ``xattrs``.
42
42
43 * When an object ``foo`` is displayed by a ``Display`` object, the generic
43 * When an object ``foo`` is displayed by a ``Display`` object, the generic
44 function ``xrepr`` is used.
44 function ``xrepr`` is used.
45
45
46 * Objects that can be iterated by ``Pipe``s must iterable. For special cases,
46 * Objects that can be iterated by ``Pipe``s must iterable. For special cases,
47 where iteration for display is different than the normal iteration a special
47 where iteration for display is different than the normal iteration a special
48 implementation can be registered with the generic function ``xiter``. This
48 implementation can be registered with the generic function ``xiter``. This
49 makes it possible to use dictionaries and modules in pipeline expressions,
49 makes it possible to use dictionaries and modules in pipeline expressions,
50 for example::
50 for example::
51
51
52 >>> import sys
52 >>> import sys
53 >>> sys | ifilter("isinstance(value, int)") | idump
53 >>> sys | ifilter("isinstance(value, int)") | idump
54 key |value
54 key |value
55 api_version| 1012
55 api_version| 1012
56 dllhandle | 503316480
56 dllhandle | 503316480
57 hexversion | 33817328
57 hexversion | 33817328
58 maxint |2147483647
58 maxint |2147483647
59 maxunicode | 65535
59 maxunicode | 65535
60 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
60 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
61 ...
61 ...
62
62
63 Note: The expression strings passed to ``ifilter()`` and ``isort()`` can
63 Note: The expression strings passed to ``ifilter()`` and ``isort()`` can
64 refer to the object to be filtered or sorted via the variable ``_`` and to any
64 refer to the object to be filtered or sorted via the variable ``_`` and to any
65 of the attributes of the object, i.e.::
65 of the attributes of the object, i.e.::
66
66
67 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
67 >>> sys.modules | ifilter("_.value is not None") | isort("_.key.lower()")
68
68
69 does the same as::
69 does the same as::
70
70
71 >>> sys.modules | ifilter("value is not None") | isort("key.lower()")
71 >>> sys.modules | ifilter("value is not None") | isort("key.lower()")
72
72
73 In addition to expression strings, it's possible to pass callables (taking
73 In addition to expression strings, it's possible to pass callables (taking
74 the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``::
74 the object as an argument) to ``ifilter()``, ``isort()`` and ``ieval()``::
75
75
76 >>> sys | ifilter(lambda _:isinstance(_.value, int)) \
76 >>> sys | ifilter(lambda _:isinstance(_.value, int)) \
77 ... | ieval(lambda _: (_.key, hex(_.value))) | idump
77 ... | ieval(lambda _: (_.key, hex(_.value))) | idump
78 0 |1
78 0 |1
79 api_version|0x3f4
79 api_version|0x3f4
80 dllhandle |0x1e000000
80 dllhandle |0x1e000000
81 hexversion |0x20402f0
81 hexversion |0x20402f0
82 maxint |0x7fffffff
82 maxint |0x7fffffff
83 maxunicode |0xffff
83 maxunicode |0xffff
84 """
84 """
85
85
86 skip_doctest = True # ignore top-level docstring as a doctest.
86 skip_doctest = True # ignore top-level docstring as a doctest.
87
87
88 import sys, os, os.path, stat, glob, new, csv, datetime, types
88 import sys, os, os.path, stat, glob, new, csv, datetime, types
89 import itertools, mimetypes, StringIO
89 import itertools, mimetypes, StringIO
90
90
91 try: # Python 2.3 compatibility
91 try: # Python 2.3 compatibility
92 import collections
92 import collections
93 except ImportError:
93 except ImportError:
94 deque = list
94 deque = list
95 else:
95 else:
96 deque = collections.deque
96 deque = collections.deque
97
97
98 try: # Python 2.3 compatibility
98 try: # Python 2.3 compatibility
99 set
99 set
100 except NameError:
100 except NameError:
101 import sets
101 import sets
102 set = sets.Set
102 set = sets.Set
103
103
104 try: # Python 2.3 compatibility
104 try: # Python 2.3 compatibility
105 sorted
105 sorted
106 except NameError:
106 except NameError:
107 def sorted(iterator, key=None, reverse=False):
107 def sorted(iterator, key=None, reverse=False):
108 items = list(iterator)
108 items = list(iterator)
109 if key is not None:
109 if key is not None:
110 items.sort(lambda i1, i2: cmp(key(i1), key(i2)))
110 items.sort(lambda i1, i2: cmp(key(i1), key(i2)))
111 else:
111 else:
112 items.sort()
112 items.sort()
113 if reverse:
113 if reverse:
114 items.reverse()
114 items.reverse()
115 return items
115 return items
116
116
117 try: # Python 2.4 compatibility
117 try: # Python 2.4 compatibility
118 GeneratorExit
118 GeneratorExit
119 except NameError:
119 except NameError:
120 GeneratorExit = SystemExit
120 GeneratorExit = SystemExit
121
121
122 try:
122 try:
123 import pwd
123 import pwd
124 except ImportError:
124 except ImportError:
125 pwd = None
125 pwd = None
126
126
127 try:
127 try:
128 import grp
128 import grp
129 except ImportError:
129 except ImportError:
130 grp = None
130 grp = None
131
131
132 from IPython.external import simplegeneric
132 from IPython.external import simplegeneric
133 from IPython.external import path
133 from IPython.external import path
134
134
135 try:
135 try:
136 from IPython.utils import genutils
136 from IPython.utils import genutils
137 from IPython.utils import generics
137 from IPython.utils import generics
138 except ImportError:
138 except ImportError:
139 genutils = None
139 genutils = None
140 generics = None
140 generics = None
141
141
142 from IPython.core import ipapi
142 from IPython.core import ipapi
143
143
144
144
145 __all__ = [
145 __all__ = [
146 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
146 "ifile", "ils", "iglob", "iwalk", "ipwdentry", "ipwd", "igrpentry", "igrp",
147 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum",
147 "icsv", "ix", "ichain", "isort", "ifilter", "ieval", "ienum",
148 "ienv", "ihist", "ialias", "icap", "idump", "iless"
148 "ienv", "ihist", "ialias", "icap", "idump", "iless"
149 ]
149 ]
150
150
151
151
152 os.stat_float_times(True) # enable microseconds
152 os.stat_float_times(True) # enable microseconds
153
153
154
154
155 class AttrNamespace(object):
155 class AttrNamespace(object):
156 """
156 """
157 Helper class that is used for providing a namespace for evaluating
157 Helper class that is used for providing a namespace for evaluating
158 expressions containing attribute names of an object.
158 expressions containing attribute names of an object.
159 """
159 """
160 def __init__(self, wrapped):
160 def __init__(self, wrapped):
161 self.wrapped = wrapped
161 self.wrapped = wrapped
162
162
163 def __getitem__(self, name):
163 def __getitem__(self, name):
164 if name == "_":
164 if name == "_":
165 return self.wrapped
165 return self.wrapped
166 try:
166 try:
167 return getattr(self.wrapped, name)
167 return getattr(self.wrapped, name)
168 except AttributeError:
168 except AttributeError:
169 raise KeyError(name)
169 raise KeyError(name)
170
170
171 # Python 2.3 compatibility
171 # Python 2.3 compatibility
172 # use eval workaround to find out which names are used in the
172 # use eval workaround to find out which names are used in the
173 # eval string and put them into the locals. This works for most
173 # eval string and put them into the locals. This works for most
174 # normal uses case, bizarre ones like accessing the locals()
174 # normal uses case, bizarre ones like accessing the locals()
175 # will fail
175 # will fail
176 try:
176 try:
177 eval("_", None, AttrNamespace(None))
177 eval("_", None, AttrNamespace(None))
178 except TypeError:
178 except TypeError:
179 real_eval = eval
179 real_eval = eval
180 def eval(codestring, _globals, _locals):
180 def eval(codestring, _globals, _locals):
181 """
181 """
182 eval(source[, globals[, locals]]) -> value
182 eval(source[, globals[, locals]]) -> value
183
183
184 Evaluate the source in the context of globals and locals.
184 Evaluate the source in the context of globals and locals.
185 The source may be a string representing a Python expression
185 The source may be a string representing a Python expression
186 or a code object as returned by compile().
186 or a code object as returned by compile().
187 The globals must be a dictionary and locals can be any mappping.
187 The globals must be a dictionary and locals can be any mappping.
188
188
189 This function is a workaround for the shortcomings of
189 This function is a workaround for the shortcomings of
190 Python 2.3's eval.
190 Python 2.3's eval.
191 """
191 """
192
192
193 if isinstance(codestring, basestring):
193 if isinstance(codestring, basestring):
194 code = compile(codestring, "_eval", "eval")
194 code = compile(codestring, "_eval", "eval")
195 else:
195 else:
196 code = codestring
196 code = codestring
197 newlocals = {}
197 newlocals = {}
198 for name in code.co_names:
198 for name in code.co_names:
199 try:
199 try:
200 newlocals[name] = _locals[name]
200 newlocals[name] = _locals[name]
201 except KeyError:
201 except KeyError:
202 pass
202 pass
203 return real_eval(code, _globals, newlocals)
203 return real_eval(code, _globals, newlocals)
204
204
205
205
206 noitem = object()
206 noitem = object()
207
207
208
208
209 def item(iterator, index, default=noitem):
209 def item(iterator, index, default=noitem):
210 """
210 """
211 Return the ``index``th item from the iterator ``iterator``.
211 Return the ``index``th item from the iterator ``iterator``.
212 ``index`` must be an integer (negative integers are relative to the
212 ``index`` must be an integer (negative integers are relative to the
213 end (i.e. the last items produced by the iterator)).
213 end (i.e. the last items produced by the iterator)).
214
214
215 If ``default`` is given, this will be the default value when
215 If ``default`` is given, this will be the default value when
216 the iterator doesn't contain an item at this position. Otherwise an
216 the iterator doesn't contain an item at this position. Otherwise an
217 ``IndexError`` will be raised.
217 ``IndexError`` will be raised.
218
218
219 Note that using this function will partially or totally exhaust the
219 Note that using this function will partially or totally exhaust the
220 iterator.
220 iterator.
221 """
221 """
222 i = index
222 i = index
223 if i>=0:
223 if i>=0:
224 for item in iterator:
224 for item in iterator:
225 if not i:
225 if not i:
226 return item
226 return item
227 i -= 1
227 i -= 1
228 else:
228 else:
229 i = -index
229 i = -index
230 cache = deque()
230 cache = deque()
231 for item in iterator:
231 for item in iterator:
232 cache.append(item)
232 cache.append(item)
233 if len(cache)>i:
233 if len(cache)>i:
234 cache.popleft()
234 cache.popleft()
235 if len(cache)==i:
235 if len(cache)==i:
236 return cache.popleft()
236 return cache.popleft()
237 if default is noitem:
237 if default is noitem:
238 raise IndexError(index)
238 raise IndexError(index)
239 else:
239 else:
240 return default
240 return default
241
241
242
242
243 def getglobals(g):
243 def getglobals(g):
244 """
244 """
245 Return the global namespace that is used for expression strings in
245 Return the global namespace that is used for expression strings in
246 ``ifilter`` and others. This is ``g`` or (if ``g`` is ``None``) IPython's
246 ``ifilter`` and others. This is ``g`` or (if ``g`` is ``None``) IPython's
247 user namespace.
247 user namespace.
248 """
248 """
249 if g is None:
249 if g is None:
250 if ipapi is not None:
250 if ipapi is not None:
251 api = ipapi.get()
251 api = ipapi.get()
252 if api is not None:
252 if api is not None:
253 return api.user_ns
253 return api.user_ns
254 return globals()
254 return globals()
255 return g
255 return g
256
256
257
257
258 class Descriptor(object):
258 class Descriptor(object):
259 """
259 """
260 A ``Descriptor`` object is used for describing the attributes of objects.
260 A ``Descriptor`` object is used for describing the attributes of objects.
261 """
261 """
262 def __hash__(self):
262 def __hash__(self):
263 return hash(self.__class__) ^ hash(self.key())
263 return hash(self.__class__) ^ hash(self.key())
264
264
265 def __eq__(self, other):
265 def __eq__(self, other):
266 return self.__class__ is other.__class__ and self.key() == other.key()
266 return self.__class__ is other.__class__ and self.key() == other.key()
267
267
268 def __ne__(self, other):
268 def __ne__(self, other):
269 return self.__class__ is not other.__class__ or self.key() != other.key()
269 return self.__class__ is not other.__class__ or self.key() != other.key()
270
270
271 def key(self):
271 def key(self):
272 pass
272 pass
273
273
274 def name(self):
274 def name(self):
275 """
275 """
276 Return the name of this attribute for display by a ``Display`` object
276 Return the name of this attribute for display by a ``Display`` object
277 (e.g. as a column title).
277 (e.g. as a column title).
278 """
278 """
279 key = self.key()
279 key = self.key()
280 if key is None:
280 if key is None:
281 return "_"
281 return "_"
282 return str(key)
282 return str(key)
283
283
284 def attrtype(self, obj):
284 def attrtype(self, obj):
285 """
285 """
286 Return the type of this attribute (i.e. something like "attribute" or
286 Return the type of this attribute (i.e. something like "attribute" or
287 "method").
287 "method").
288 """
288 """
289
289
290 def valuetype(self, obj):
290 def valuetype(self, obj):
291 """
291 """
292 Return the type of this attribute value of the object ``obj``.
292 Return the type of this attribute value of the object ``obj``.
293 """
293 """
294
294
295 def value(self, obj):
295 def value(self, obj):
296 """
296 """
297 Return the value of this attribute of the object ``obj``.
297 Return the value of this attribute of the object ``obj``.
298 """
298 """
299
299
300 def doc(self, obj):
300 def doc(self, obj):
301 """
301 """
302 Return the documentation for this attribute.
302 Return the documentation for this attribute.
303 """
303 """
304
304
305 def shortdoc(self, obj):
305 def shortdoc(self, obj):
306 """
306 """
307 Return a short documentation for this attribute (defaulting to the
307 Return a short documentation for this attribute (defaulting to the
308 first line).
308 first line).
309 """
309 """
310 doc = self.doc(obj)
310 doc = self.doc(obj)
311 if doc is not None:
311 if doc is not None:
312 doc = doc.strip().splitlines()[0].strip()
312 doc = doc.strip().splitlines()[0].strip()
313 return doc
313 return doc
314
314
315 def iter(self, obj):
315 def iter(self, obj):
316 """
316 """
317 Return an iterator for this attribute of the object ``obj``.
317 Return an iterator for this attribute of the object ``obj``.
318 """
318 """
319 return xiter(self.value(obj))
319 return xiter(self.value(obj))
320
320
321
321
322 class SelfDescriptor(Descriptor):
322 class SelfDescriptor(Descriptor):
323 """
323 """
324 A ``SelfDescriptor`` describes the object itself.
324 A ``SelfDescriptor`` describes the object itself.
325 """
325 """
326 def key(self):
326 def key(self):
327 return None
327 return None
328
328
329 def attrtype(self, obj):
329 def attrtype(self, obj):
330 return "self"
330 return "self"
331
331
332 def valuetype(self, obj):
332 def valuetype(self, obj):
333 return type(obj)
333 return type(obj)
334
334
335 def value(self, obj):
335 def value(self, obj):
336 return obj
336 return obj
337
337
338 def __repr__(self):
338 def __repr__(self):
339 return "Self"
339 return "Self"
340
340
341 selfdescriptor = SelfDescriptor() # there's no need for more than one
341 selfdescriptor = SelfDescriptor() # there's no need for more than one
342
342
343
343
344 class AttributeDescriptor(Descriptor):
344 class AttributeDescriptor(Descriptor):
345 """
345 """
346 An ``AttributeDescriptor`` describes a simple attribute of an object.
346 An ``AttributeDescriptor`` describes a simple attribute of an object.
347 """
347 """
348 __slots__ = ("_name", "_doc")
348 __slots__ = ("_name", "_doc")
349
349
350 def __init__(self, name, doc=None):
350 def __init__(self, name, doc=None):
351 self._name = name
351 self._name = name
352 self._doc = doc
352 self._doc = doc
353
353
354 def key(self):
354 def key(self):
355 return self._name
355 return self._name
356
356
357 def doc(self, obj):
357 def doc(self, obj):
358 return self._doc
358 return self._doc
359
359
360 def attrtype(self, obj):
360 def attrtype(self, obj):
361 return "attr"
361 return "attr"
362
362
363 def valuetype(self, obj):
363 def valuetype(self, obj):
364 return type(getattr(obj, self._name))
364 return type(getattr(obj, self._name))
365
365
366 def value(self, obj):
366 def value(self, obj):
367 return getattr(obj, self._name)
367 return getattr(obj, self._name)
368
368
369 def __repr__(self):
369 def __repr__(self):
370 if self._doc is None:
370 if self._doc is None:
371 return "Attribute(%r)" % self._name
371 return "Attribute(%r)" % self._name
372 else:
372 else:
373 return "Attribute(%r, %r)" % (self._name, self._doc)
373 return "Attribute(%r, %r)" % (self._name, self._doc)
374
374
375
375
376 class IndexDescriptor(Descriptor):
376 class IndexDescriptor(Descriptor):
377 """
377 """
378 An ``IndexDescriptor`` describes an "attribute" of an object that is fetched
378 An ``IndexDescriptor`` describes an "attribute" of an object that is fetched
379 via ``__getitem__``.
379 via ``__getitem__``.
380 """
380 """
381 __slots__ = ("_index",)
381 __slots__ = ("_index",)
382
382
383 def __init__(self, index):
383 def __init__(self, index):
384 self._index = index
384 self._index = index
385
385
386 def key(self):
386 def key(self):
387 return self._index
387 return self._index
388
388
389 def attrtype(self, obj):
389 def attrtype(self, obj):
390 return "item"
390 return "item"
391
391
392 def valuetype(self, obj):
392 def valuetype(self, obj):
393 return type(obj[self._index])
393 return type(obj[self._index])
394
394
395 def value(self, obj):
395 def value(self, obj):
396 return obj[self._index]
396 return obj[self._index]
397
397
398 def __repr__(self):
398 def __repr__(self):
399 return "Index(%r)" % self._index
399 return "Index(%r)" % self._index
400
400
401
401
402 class MethodDescriptor(Descriptor):
402 class MethodDescriptor(Descriptor):
403 """
403 """
404 A ``MethodDescriptor`` describes a method of an object that can be called
404 A ``MethodDescriptor`` describes a method of an object that can be called
405 without argument. Note that this method shouldn't change the object.
405 without argument. Note that this method shouldn't change the object.
406 """
406 """
407 __slots__ = ("_name", "_doc")
407 __slots__ = ("_name", "_doc")
408
408
409 def __init__(self, name, doc=None):
409 def __init__(self, name, doc=None):
410 self._name = name
410 self._name = name
411 self._doc = doc
411 self._doc = doc
412
412
413 def key(self):
413 def key(self):
414 return self._name
414 return self._name
415
415
416 def doc(self, obj):
416 def doc(self, obj):
417 if self._doc is None:
417 if self._doc is None:
418 return getattr(obj, self._name).__doc__
418 return getattr(obj, self._name).__doc__
419 return self._doc
419 return self._doc
420
420
421 def attrtype(self, obj):
421 def attrtype(self, obj):
422 return "method"
422 return "method"
423
423
424 def valuetype(self, obj):
424 def valuetype(self, obj):
425 return type(self.value(obj))
425 return type(self.value(obj))
426
426
427 def value(self, obj):
427 def value(self, obj):
428 return getattr(obj, self._name)()
428 return getattr(obj, self._name)()
429
429
430 def __repr__(self):
430 def __repr__(self):
431 if self._doc is None:
431 if self._doc is None:
432 return "Method(%r)" % self._name
432 return "Method(%r)" % self._name
433 else:
433 else:
434 return "Method(%r, %r)" % (self._name, self._doc)
434 return "Method(%r, %r)" % (self._name, self._doc)
435
435
436
436
437 class IterAttributeDescriptor(Descriptor):
437 class IterAttributeDescriptor(Descriptor):
438 """
438 """
439 An ``IterAttributeDescriptor`` works like an ``AttributeDescriptor`` but
439 An ``IterAttributeDescriptor`` works like an ``AttributeDescriptor`` but
440 doesn't return an attribute values (because this value might be e.g. a large
440 doesn't return an attribute values (because this value might be e.g. a large
441 list).
441 list).
442 """
442 """
443 __slots__ = ("_name", "_doc")
443 __slots__ = ("_name", "_doc")
444
444
445 def __init__(self, name, doc=None):
445 def __init__(self, name, doc=None):
446 self._name = name
446 self._name = name
447 self._doc = doc
447 self._doc = doc
448
448
449 def key(self):
449 def key(self):
450 return self._name
450 return self._name
451
451
452 def doc(self, obj):
452 def doc(self, obj):
453 return self._doc
453 return self._doc
454
454
455 def attrtype(self, obj):
455 def attrtype(self, obj):
456 return "iter"
456 return "iter"
457
457
458 def valuetype(self, obj):
458 def valuetype(self, obj):
459 return noitem
459 return noitem
460
460
461 def value(self, obj):
461 def value(self, obj):
462 return noitem
462 return noitem
463
463
464 def iter(self, obj):
464 def iter(self, obj):
465 return xiter(getattr(obj, self._name))
465 return xiter(getattr(obj, self._name))
466
466
467 def __repr__(self):
467 def __repr__(self):
468 if self._doc is None:
468 if self._doc is None:
469 return "IterAttribute(%r)" % self._name
469 return "IterAttribute(%r)" % self._name
470 else:
470 else:
471 return "IterAttribute(%r, %r)" % (self._name, self._doc)
471 return "IterAttribute(%r, %r)" % (self._name, self._doc)
472
472
473
473
474 class IterMethodDescriptor(Descriptor):
474 class IterMethodDescriptor(Descriptor):
475 """
475 """
476 An ``IterMethodDescriptor`` works like an ``MethodDescriptor`` but doesn't
476 An ``IterMethodDescriptor`` works like an ``MethodDescriptor`` but doesn't
477 return an attribute values (because this value might be e.g. a large list).
477 return an attribute values (because this value might be e.g. a large list).
478 """
478 """
479 __slots__ = ("_name", "_doc")
479 __slots__ = ("_name", "_doc")
480
480
481 def __init__(self, name, doc=None):
481 def __init__(self, name, doc=None):
482 self._name = name
482 self._name = name
483 self._doc = doc
483 self._doc = doc
484
484
485 def key(self):
485 def key(self):
486 return self._name
486 return self._name
487
487
488 def doc(self, obj):
488 def doc(self, obj):
489 if self._doc is None:
489 if self._doc is None:
490 return getattr(obj, self._name).__doc__
490 return getattr(obj, self._name).__doc__
491 return self._doc
491 return self._doc
492
492
493 def attrtype(self, obj):
493 def attrtype(self, obj):
494 return "itermethod"
494 return "itermethod"
495
495
496 def valuetype(self, obj):
496 def valuetype(self, obj):
497 return noitem
497 return noitem
498
498
499 def value(self, obj):
499 def value(self, obj):
500 return noitem
500 return noitem
501
501
502 def iter(self, obj):
502 def iter(self, obj):
503 return xiter(getattr(obj, self._name)())
503 return xiter(getattr(obj, self._name)())
504
504
505 def __repr__(self):
505 def __repr__(self):
506 if self._doc is None:
506 if self._doc is None:
507 return "IterMethod(%r)" % self._name
507 return "IterMethod(%r)" % self._name
508 else:
508 else:
509 return "IterMethod(%r, %r)" % (self._name, self._doc)
509 return "IterMethod(%r, %r)" % (self._name, self._doc)
510
510
511
511
512 class FunctionDescriptor(Descriptor):
512 class FunctionDescriptor(Descriptor):
513 """
513 """
514 A ``FunctionDescriptor`` turns a function into a descriptor. The function
514 A ``FunctionDescriptor`` turns a function into a descriptor. The function
515 will be called with the object to get the type and value of the attribute.
515 will be called with the object to get the type and value of the attribute.
516 """
516 """
517 __slots__ = ("_function", "_name", "_doc")
517 __slots__ = ("_function", "_name", "_doc")
518
518
519 def __init__(self, function, name=None, doc=None):
519 def __init__(self, function, name=None, doc=None):
520 self._function = function
520 self._function = function
521 self._name = name
521 self._name = name
522 self._doc = doc
522 self._doc = doc
523
523
524 def key(self):
524 def key(self):
525 return self._function
525 return self._function
526
526
527 def name(self):
527 def name(self):
528 if self._name is not None:
528 if self._name is not None:
529 return self._name
529 return self._name
530 return getattr(self._function, "__xname__", self._function.__name__)
530 return getattr(self._function, "__xname__", self._function.__name__)
531
531
532 def doc(self, obj):
532 def doc(self, obj):
533 if self._doc is None:
533 if self._doc is None:
534 return self._function.__doc__
534 return self._function.__doc__
535 return self._doc
535 return self._doc
536
536
537 def attrtype(self, obj):
537 def attrtype(self, obj):
538 return "function"
538 return "function"
539
539
540 def valuetype(self, obj):
540 def valuetype(self, obj):
541 return type(self._function(obj))
541 return type(self._function(obj))
542
542
543 def value(self, obj):
543 def value(self, obj):
544 return self._function(obj)
544 return self._function(obj)
545
545
546 def __repr__(self):
546 def __repr__(self):
547 if self._doc is None:
547 if self._doc is None:
548 return "Function(%r)" % self._name
548 return "Function(%r)" % self._name
549 else:
549 else:
550 return "Function(%r, %r)" % (self._name, self._doc)
550 return "Function(%r, %r)" % (self._name, self._doc)
551
551
552
552
553 class Table(object):
553 class Table(object):
554 """
554 """
555 A ``Table`` is an object that produces items (just like a normal Python
555 A ``Table`` is an object that produces items (just like a normal Python
556 iterator/generator does) and can be used as the first object in a pipeline
556 iterator/generator does) and can be used as the first object in a pipeline
557 expression. The displayhook will open the default browser for such an object
557 expression. The displayhook will open the default browser for such an object
558 (instead of simply printing the ``repr()`` result).
558 (instead of simply printing the ``repr()`` result).
559 """
559 """
560
560
561 # We want to support ``foo`` and ``foo()`` in pipeline expression:
561 # We want to support ``foo`` and ``foo()`` in pipeline expression:
562 # So we implement the required operators (``|`` and ``+``) in the metaclass,
562 # So we implement the required operators (``|`` and ``+``) in the metaclass,
563 # instantiate the class and forward the operator to the instance
563 # instantiate the class and forward the operator to the instance
564 class __metaclass__(type):
564 class __metaclass__(type):
565 def __iter__(self):
565 def __iter__(self):
566 return iter(self())
566 return iter(self())
567
567
568 def __or__(self, other):
568 def __or__(self, other):
569 return self() | other
569 return self() | other
570
570
571 def __add__(self, other):
571 def __add__(self, other):
572 return self() + other
572 return self() + other
573
573
574 def __radd__(self, other):
574 def __radd__(self, other):
575 return other + self()
575 return other + self()
576
576
577 def __getitem__(self, index):
577 def __getitem__(self, index):
578 return self()[index]
578 return self()[index]
579
579
580 def __getitem__(self, index):
580 def __getitem__(self, index):
581 return item(self, index)
581 return item(self, index)
582
582
583 def __contains__(self, item):
583 def __contains__(self, item):
584 for haveitem in self:
584 for haveitem in self:
585 if item == haveitem:
585 if item == haveitem:
586 return True
586 return True
587 return False
587 return False
588
588
589 def __or__(self, other):
589 def __or__(self, other):
590 # autoinstantiate right hand side
590 # autoinstantiate right hand side
591 if isinstance(other, type) and issubclass(other, (Table, Display)):
591 if isinstance(other, type) and issubclass(other, (Table, Display)):
592 other = other()
592 other = other()
593 # treat simple strings and functions as ``ieval`` instances
593 # treat simple strings and functions as ``ieval`` instances
594 elif not isinstance(other, Display) and not isinstance(other, Table):
594 elif not isinstance(other, Display) and not isinstance(other, Table):
595 other = ieval(other)
595 other = ieval(other)
596 # forward operations to the right hand side
596 # forward operations to the right hand side
597 return other.__ror__(self)
597 return other.__ror__(self)
598
598
599 def __add__(self, other):
599 def __add__(self, other):
600 # autoinstantiate right hand side
600 # autoinstantiate right hand side
601 if isinstance(other, type) and issubclass(other, Table):
601 if isinstance(other, type) and issubclass(other, Table):
602 other = other()
602 other = other()
603 return ichain(self, other)
603 return ichain(self, other)
604
604
605 def __radd__(self, other):
605 def __radd__(self, other):
606 # autoinstantiate left hand side
606 # autoinstantiate left hand side
607 if isinstance(other, type) and issubclass(other, Table):
607 if isinstance(other, type) and issubclass(other, Table):
608 other = other()
608 other = other()
609 return ichain(other, self)
609 return ichain(other, self)
610
610
611
611
612 class Pipe(Table):
612 class Pipe(Table):
613 """
613 """
614 A ``Pipe`` is an object that can be used in a pipeline expression. It
614 A ``Pipe`` is an object that can be used in a pipeline expression. It
615 processes the objects it gets from its input ``Table``/``Pipe``. Note that
615 processes the objects it gets from its input ``Table``/``Pipe``. Note that
616 a ``Pipe`` object can't be used as the first object in a pipeline
616 a ``Pipe`` object can't be used as the first object in a pipeline
617 expression, as it doesn't produces items itself.
617 expression, as it doesn't produces items itself.
618 """
618 """
619 class __metaclass__(Table.__metaclass__):
619 class __metaclass__(Table.__metaclass__):
620 def __ror__(self, input):
620 def __ror__(self, input):
621 return input | self()
621 return input | self()
622
622
623 def __ror__(self, input):
623 def __ror__(self, input):
624 # autoinstantiate left hand side
624 # autoinstantiate left hand side
625 if isinstance(input, type) and issubclass(input, Table):
625 if isinstance(input, type) and issubclass(input, Table):
626 input = input()
626 input = input()
627 self.input = input
627 self.input = input
628 return self
628 return self
629
629
630
630
631 def xrepr(item, mode="default"):
631 def xrepr(item, mode="default"):
632 """
632 """
633 Generic function that adds color output and different display modes to ``repr``.
633 Generic function that adds color output and different display modes to ``repr``.
634
634
635 The result of an ``xrepr`` call is iterable and consists of ``(style, string)``
635 The result of an ``xrepr`` call is iterable and consists of ``(style, string)``
636 tuples. The ``style`` in this tuple must be a ``Style`` object from the
636 tuples. The ``style`` in this tuple must be a ``Style`` object from the
637 ``astring`` module. To reconfigure the output the first yielded tuple can be
637 ``astring`` module. To reconfigure the output the first yielded tuple can be
638 a ``(aligment, full)`` tuple instead of a ``(style, string)`` tuple.
638 a ``(aligment, full)`` tuple instead of a ``(style, string)`` tuple.
639 ``alignment`` can be -1 for left aligned, 0 for centered and 1 for right
639 ``alignment`` can be -1 for left aligned, 0 for centered and 1 for right
640 aligned (the default is left alignment). ``full`` is a boolean that specifies
640 aligned (the default is left alignment). ``full`` is a boolean that specifies
641 whether the complete output must be displayed or the ``Display`` object is
641 whether the complete output must be displayed or the ``Display`` object is
642 allowed to stop output after enough text has been produced (e.g. a syntax
642 allowed to stop output after enough text has been produced (e.g. a syntax
643 highlighted text line would use ``True``, but for a large data structure
643 highlighted text line would use ``True``, but for a large data structure
644 (i.e. a nested list, tuple or dictionary) ``False`` would be used).
644 (i.e. a nested list, tuple or dictionary) ``False`` would be used).
645 The default is full output.
645 The default is full output.
646
646
647 There are four different possible values for ``mode`` depending on where
647 There are four different possible values for ``mode`` depending on where
648 the ``Display`` object will display ``item``:
648 the ``Display`` object will display ``item``:
649
649
650 ``"header"``
650 ``"header"``
651 ``item`` will be displayed in a header line (this is used by ``ibrowse``).
651 ``item`` will be displayed in a header line (this is used by ``ibrowse``).
652
652
653 ``"footer"``
653 ``"footer"``
654 ``item`` will be displayed in a footer line (this is used by ``ibrowse``).
654 ``item`` will be displayed in a footer line (this is used by ``ibrowse``).
655
655
656 ``"cell"``
656 ``"cell"``
657 ``item`` will be displayed in a table cell/list.
657 ``item`` will be displayed in a table cell/list.
658
658
659 ``"default"``
659 ``"default"``
660 default mode. If an ``xrepr`` implementation recursively outputs objects,
660 default mode. If an ``xrepr`` implementation recursively outputs objects,
661 ``"default"`` must be passed in the recursive calls to ``xrepr``.
661 ``"default"`` must be passed in the recursive calls to ``xrepr``.
662
662
663 If no implementation is registered for ``item``, ``xrepr`` will try the
663 If no implementation is registered for ``item``, ``xrepr`` will try the
664 ``__xrepr__`` method on ``item``. If ``item`` doesn't have an ``__xrepr__``
664 ``__xrepr__`` method on ``item``. If ``item`` doesn't have an ``__xrepr__``
665 method it falls back to ``repr``/``__repr__`` for all modes.
665 method it falls back to ``repr``/``__repr__`` for all modes.
666 """
666 """
667 try:
667 try:
668 func = item.__xrepr__
668 func = item.__xrepr__
669 except AttributeError:
669 except AttributeError:
670 yield (astyle.style_default, repr(item))
670 yield (astyle.style_default, repr(item))
671 else:
671 else:
672 try:
672 try:
673 for x in func(mode):
673 for x in func(mode):
674 yield x
674 yield x
675 except (KeyboardInterrupt, SystemExit, GeneratorExit):
675 except (KeyboardInterrupt, SystemExit, GeneratorExit):
676 raise
676 raise
677 except Exception:
677 except Exception:
678 yield (astyle.style_default, repr(item))
678 yield (astyle.style_default, repr(item))
679 xrepr = simplegeneric.generic(xrepr)
679 xrepr = simplegeneric.generic(xrepr)
680
680
681
681
682 def xrepr_none(self, mode="default"):
682 def xrepr_none(self, mode="default"):
683 yield (astyle.style_type_none, repr(self))
683 yield (astyle.style_type_none, repr(self))
684 xrepr.when_object(None)(xrepr_none)
684 xrepr.when_object(None)(xrepr_none)
685
685
686
686
687 def xrepr_noitem(self, mode="default"):
687 def xrepr_noitem(self, mode="default"):
688 yield (2, True)
688 yield (2, True)
689 yield (astyle.style_nodata, "<?>")
689 yield (astyle.style_nodata, "<?>")
690 xrepr.when_object(noitem)(xrepr_noitem)
690 xrepr.when_object(noitem)(xrepr_noitem)
691
691
692
692
693 def xrepr_bool(self, mode="default"):
693 def xrepr_bool(self, mode="default"):
694 yield (astyle.style_type_bool, repr(self))
694 yield (astyle.style_type_bool, repr(self))
695 xrepr.when_type(bool)(xrepr_bool)
695 xrepr.when_type(bool)(xrepr_bool)
696
696
697
697
698 def xrepr_str(self, mode="default"):
698 def xrepr_str(self, mode="default"):
699 if mode == "cell":
699 if mode == "cell":
700 yield (astyle.style_default, repr(self.expandtabs(tab))[1:-1])
700 yield (astyle.style_default, repr(self.expandtabs(tab))[1:-1])
701 else:
701 else:
702 yield (astyle.style_default, repr(self))
702 yield (astyle.style_default, repr(self))
703 xrepr.when_type(str)(xrepr_str)
703 xrepr.when_type(str)(xrepr_str)
704
704
705
705
706 def xrepr_unicode(self, mode="default"):
706 def xrepr_unicode(self, mode="default"):
707 if mode == "cell":
707 if mode == "cell":
708 yield (astyle.style_default, repr(self.expandtabs(tab))[2:-1])
708 yield (astyle.style_default, repr(self.expandtabs(tab))[2:-1])
709 else:
709 else:
710 yield (astyle.style_default, repr(self))
710 yield (astyle.style_default, repr(self))
711 xrepr.when_type(unicode)(xrepr_unicode)
711 xrepr.when_type(unicode)(xrepr_unicode)
712
712
713
713
714 def xrepr_number(self, mode="default"):
714 def xrepr_number(self, mode="default"):
715 yield (1, True)
715 yield (1, True)
716 yield (astyle.style_type_number, repr(self))
716 yield (astyle.style_type_number, repr(self))
717 xrepr.when_type(int)(xrepr_number)
717 xrepr.when_type(int)(xrepr_number)
718 xrepr.when_type(long)(xrepr_number)
718 xrepr.when_type(long)(xrepr_number)
719 xrepr.when_type(float)(xrepr_number)
719 xrepr.when_type(float)(xrepr_number)
720
720
721
721
722 def xrepr_complex(self, mode="default"):
722 def xrepr_complex(self, mode="default"):
723 yield (astyle.style_type_number, repr(self))
723 yield (astyle.style_type_number, repr(self))
724 xrepr.when_type(complex)(xrepr_number)
724 xrepr.when_type(complex)(xrepr_number)
725
725
726
726
727 def xrepr_datetime(self, mode="default"):
727 def xrepr_datetime(self, mode="default"):
728 if mode == "cell":
728 if mode == "cell":
729 # Don't use strftime() here, as this requires year >= 1900
729 # Don't use strftime() here, as this requires year >= 1900
730 yield (astyle.style_type_datetime,
730 yield (astyle.style_type_datetime,
731 "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
731 "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
732 (self.year, self.month, self.day,
732 (self.year, self.month, self.day,
733 self.hour, self.minute, self.second,
733 self.hour, self.minute, self.second,
734 self.microsecond),
734 self.microsecond),
735 )
735 )
736 else:
736 else:
737 yield (astyle.style_type_datetime, repr(self))
737 yield (astyle.style_type_datetime, repr(self))
738 xrepr.when_type(datetime.datetime)(xrepr_datetime)
738 xrepr.when_type(datetime.datetime)(xrepr_datetime)
739
739
740
740
741 def xrepr_date(self, mode="default"):
741 def xrepr_date(self, mode="default"):
742 if mode == "cell":
742 if mode == "cell":
743 yield (astyle.style_type_datetime,
743 yield (astyle.style_type_datetime,
744 "%04d-%02d-%02d" % (self.year, self.month, self.day))
744 "%04d-%02d-%02d" % (self.year, self.month, self.day))
745 else:
745 else:
746 yield (astyle.style_type_datetime, repr(self))
746 yield (astyle.style_type_datetime, repr(self))
747 xrepr.when_type(datetime.date)(xrepr_date)
747 xrepr.when_type(datetime.date)(xrepr_date)
748
748
749
749
750 def xrepr_time(self, mode="default"):
750 def xrepr_time(self, mode="default"):
751 if mode == "cell":
751 if mode == "cell":
752 yield (astyle.style_type_datetime,
752 yield (astyle.style_type_datetime,
753 "%02d:%02d:%02d.%06d" % \
753 "%02d:%02d:%02d.%06d" % \
754 (self.hour, self.minute, self.second, self.microsecond))
754 (self.hour, self.minute, self.second, self.microsecond))
755 else:
755 else:
756 yield (astyle.style_type_datetime, repr(self))
756 yield (astyle.style_type_datetime, repr(self))
757 xrepr.when_type(datetime.time)(xrepr_time)
757 xrepr.when_type(datetime.time)(xrepr_time)
758
758
759
759
760 def xrepr_timedelta(self, mode="default"):
760 def xrepr_timedelta(self, mode="default"):
761 yield (astyle.style_type_datetime, repr(self))
761 yield (astyle.style_type_datetime, repr(self))
762 xrepr.when_type(datetime.timedelta)(xrepr_timedelta)
762 xrepr.when_type(datetime.timedelta)(xrepr_timedelta)
763
763
764
764
765 def xrepr_type(self, mode="default"):
765 def xrepr_type(self, mode="default"):
766 if self.__module__ == "__builtin__":
766 if self.__module__ == "__builtin__":
767 yield (astyle.style_type_type, self.__name__)
767 yield (astyle.style_type_type, self.__name__)
768 else:
768 else:
769 yield (astyle.style_type_type, "%s.%s" % (self.__module__, self.__name__))
769 yield (astyle.style_type_type, "%s.%s" % (self.__module__, self.__name__))
770 xrepr.when_type(type)(xrepr_type)
770 xrepr.when_type(type)(xrepr_type)
771
771
772
772
773 def xrepr_exception(self, mode="default"):
773 def xrepr_exception(self, mode="default"):
774 if self.__class__.__module__ == "exceptions":
774 if self.__class__.__module__ == "exceptions":
775 classname = self.__class__.__name__
775 classname = self.__class__.__name__
776 else:
776 else:
777 classname = "%s.%s" % \
777 classname = "%s.%s" % \
778 (self.__class__.__module__, self.__class__.__name__)
778 (self.__class__.__module__, self.__class__.__name__)
779 if mode == "header" or mode == "footer":
779 if mode == "header" or mode == "footer":
780 yield (astyle.style_error, "%s: %s" % (classname, self))
780 yield (astyle.style_error, "%s: %s" % (classname, self))
781 else:
781 else:
782 yield (astyle.style_error, classname)
782 yield (astyle.style_error, classname)
783 xrepr.when_type(Exception)(xrepr_exception)
783 xrepr.when_type(Exception)(xrepr_exception)
784
784
785
785
786 def xrepr_listtuple(self, mode="default"):
786 def xrepr_listtuple(self, mode="default"):
787 if mode == "header" or mode == "footer":
787 if mode == "header" or mode == "footer":
788 if self.__class__.__module__ == "__builtin__":
788 if self.__class__.__module__ == "__builtin__":
789 classname = self.__class__.__name__
789 classname = self.__class__.__name__
790 else:
790 else:
791 classname = "%s.%s" % \
791 classname = "%s.%s" % \
792 (self.__class__.__module__,self.__class__.__name__)
792 (self.__class__.__module__,self.__class__.__name__)
793 yield (astyle.style_default,
793 yield (astyle.style_default,
794 "<%s object with %d items at 0x%x>" % \
794 "<%s object with %d items at 0x%x>" % \
795 (classname, len(self), id(self)))
795 (classname, len(self), id(self)))
796 else:
796 else:
797 yield (-1, False)
797 yield (-1, False)
798 if isinstance(self, list):
798 if isinstance(self, list):
799 yield (astyle.style_default, "[")
799 yield (astyle.style_default, "[")
800 end = "]"
800 end = "]"
801 else:
801 else:
802 yield (astyle.style_default, "(")
802 yield (astyle.style_default, "(")
803 end = ")"
803 end = ")"
804 for (i, subself) in enumerate(self):
804 for (i, subself) in enumerate(self):
805 if i:
805 if i:
806 yield (astyle.style_default, ", ")
806 yield (astyle.style_default, ", ")
807 for part in xrepr(subself, "default"):
807 for part in xrepr(subself, "default"):
808 yield part
808 yield part
809 yield (astyle.style_default, end)
809 yield (astyle.style_default, end)
810 xrepr.when_type(list)(xrepr_listtuple)
810 xrepr.when_type(list)(xrepr_listtuple)
811 xrepr.when_type(tuple)(xrepr_listtuple)
811 xrepr.when_type(tuple)(xrepr_listtuple)
812
812
813
813
814 def xrepr_dict(self, mode="default"):
814 def xrepr_dict(self, mode="default"):
815 if mode == "header" or mode == "footer":
815 if mode == "header" or mode == "footer":
816 if self.__class__.__module__ == "__builtin__":
816 if self.__class__.__module__ == "__builtin__":
817 classname = self.__class__.__name__
817 classname = self.__class__.__name__
818 else:
818 else:
819 classname = "%s.%s" % \
819 classname = "%s.%s" % \
820 (self.__class__.__module__,self.__class__.__name__)
820 (self.__class__.__module__,self.__class__.__name__)
821 yield (astyle.style_default,
821 yield (astyle.style_default,
822 "<%s object with %d items at 0x%x>" % \
822 "<%s object with %d items at 0x%x>" % \
823 (classname, len(self), id(self)))
823 (classname, len(self), id(self)))
824 else:
824 else:
825 yield (-1, False)
825 yield (-1, False)
826 if isinstance(self, dict):
826 if isinstance(self, dict):
827 yield (astyle.style_default, "{")
827 yield (astyle.style_default, "{")
828 end = "}"
828 end = "}"
829 else:
829 else:
830 yield (astyle.style_default, "dictproxy((")
830 yield (astyle.style_default, "dictproxy((")
831 end = "})"
831 end = "})"
832 for (i, (key, value)) in enumerate(self.iteritems()):
832 for (i, (key, value)) in enumerate(self.iteritems()):
833 if i:
833 if i:
834 yield (astyle.style_default, ", ")
834 yield (astyle.style_default, ", ")
835 for part in xrepr(key, "default"):
835 for part in xrepr(key, "default"):
836 yield part
836 yield part
837 yield (astyle.style_default, ": ")
837 yield (astyle.style_default, ": ")
838 for part in xrepr(value, "default"):
838 for part in xrepr(value, "default"):
839 yield part
839 yield part
840 yield (astyle.style_default, end)
840 yield (astyle.style_default, end)
841 xrepr.when_type(dict)(xrepr_dict)
841 xrepr.when_type(dict)(xrepr_dict)
842 xrepr.when_type(types.DictProxyType)(xrepr_dict)
842 xrepr.when_type(types.DictProxyType)(xrepr_dict)
843
843
844
844
845 def upgradexattr(attr):
845 def upgradexattr(attr):
846 """
846 """
847 Convert an attribute descriptor string to a real descriptor object.
847 Convert an attribute descriptor string to a real descriptor object.
848
848
849 If attr already is a descriptor object return it unmodified. A
849 If attr already is a descriptor object return it unmodified. A
850 ``SelfDescriptor`` will be returned if ``attr`` is ``None``. ``"foo"``
850 ``SelfDescriptor`` will be returned if ``attr`` is ``None``. ``"foo"``
851 returns an ``AttributeDescriptor`` for the attribute named ``"foo"``.
851 returns an ``AttributeDescriptor`` for the attribute named ``"foo"``.
852 ``"foo()"`` returns a ``MethodDescriptor`` for the method named ``"foo"``.
852 ``"foo()"`` returns a ``MethodDescriptor`` for the method named ``"foo"``.
853 ``"-foo"`` will return an ``IterAttributeDescriptor`` for the attribute
853 ``"-foo"`` will return an ``IterAttributeDescriptor`` for the attribute
854 named ``"foo"`` and ``"-foo()"`` will return an ``IterMethodDescriptor``
854 named ``"foo"`` and ``"-foo()"`` will return an ``IterMethodDescriptor``
855 for the method named ``"foo"``. Furthermore integers will return the appropriate
855 for the method named ``"foo"``. Furthermore integers will return the appropriate
856 ``IndexDescriptor`` and callables will return a ``FunctionDescriptor``.
856 ``IndexDescriptor`` and callables will return a ``FunctionDescriptor``.
857 """
857 """
858 if attr is None:
858 if attr is None:
859 return selfdescriptor
859 return selfdescriptor
860 elif isinstance(attr, Descriptor):
860 elif isinstance(attr, Descriptor):
861 return attr
861 return attr
862 elif isinstance(attr, basestring):
862 elif isinstance(attr, basestring):
863 if attr.endswith("()"):
863 if attr.endswith("()"):
864 if attr.startswith("-"):
864 if attr.startswith("-"):
865 return IterMethodDescriptor(attr[1:-2])
865 return IterMethodDescriptor(attr[1:-2])
866 else:
866 else:
867 return MethodDescriptor(attr[:-2])
867 return MethodDescriptor(attr[:-2])
868 else:
868 else:
869 if attr.startswith("-"):
869 if attr.startswith("-"):
870 return IterAttributeDescriptor(attr[1:])
870 return IterAttributeDescriptor(attr[1:])
871 else:
871 else:
872 return AttributeDescriptor(attr)
872 return AttributeDescriptor(attr)
873 elif isinstance(attr, (int, long)):
873 elif isinstance(attr, (int, long)):
874 return IndexDescriptor(attr)
874 return IndexDescriptor(attr)
875 elif callable(attr):
875 elif callable(attr):
876 return FunctionDescriptor(attr)
876 return FunctionDescriptor(attr)
877 else:
877 else:
878 raise TypeError("can't handle descriptor %r" % attr)
878 raise TypeError("can't handle descriptor %r" % attr)
879
879
880
880
881 def xattrs(item, mode="default"):
881 def xattrs(item, mode="default"):
882 """
882 """
883 Generic function that returns an iterable of attribute descriptors
883 Generic function that returns an iterable of attribute descriptors
884 to be used for displaying the attributes ob the object ``item`` in display
884 to be used for displaying the attributes ob the object ``item`` in display
885 mode ``mode``.
885 mode ``mode``.
886
886
887 There are two possible modes:
887 There are two possible modes:
888
888
889 ``"detail"``
889 ``"detail"``
890 The ``Display`` object wants to display a detailed list of the object
890 The ``Display`` object wants to display a detailed list of the object
891 attributes.
891 attributes.
892
892
893 ``"default"``
893 ``"default"``
894 The ``Display`` object wants to display the object in a list view.
894 The ``Display`` object wants to display the object in a list view.
895
895
896 If no implementation is registered for the object ``item`` ``xattrs`` falls
896 If no implementation is registered for the object ``item`` ``xattrs`` falls
897 back to trying the ``__xattrs__`` method of the object. If this doesn't
897 back to trying the ``__xattrs__`` method of the object. If this doesn't
898 exist either, ``dir(item)`` is used for ``"detail"`` mode and ``(None,)``
898 exist either, ``dir(item)`` is used for ``"detail"`` mode and ``(None,)``
899 for ``"default"`` mode.
899 for ``"default"`` mode.
900
900
901 The implementation must yield attribute descriptors (see the class
901 The implementation must yield attribute descriptors (see the class
902 ``Descriptor`` for more info). The ``__xattrs__`` method may also return
902 ``Descriptor`` for more info). The ``__xattrs__`` method may also return
903 attribute descriptor strings (and ``None``) which will be converted to real
903 attribute descriptor strings (and ``None``) which will be converted to real
904 descriptors by ``upgradexattr()``.
904 descriptors by ``upgradexattr()``.
905 """
905 """
906 try:
906 try:
907 func = item.__xattrs__
907 func = item.__xattrs__
908 except AttributeError:
908 except AttributeError:
909 if mode == "detail":
909 if mode == "detail":
910 for attrname in dir(item):
910 for attrname in dir(item):
911 yield AttributeDescriptor(attrname)
911 yield AttributeDescriptor(attrname)
912 else:
912 else:
913 yield selfdescriptor
913 yield selfdescriptor
914 else:
914 else:
915 for attr in func(mode):
915 for attr in func(mode):
916 yield upgradexattr(attr)
916 yield upgradexattr(attr)
917 xattrs = simplegeneric.generic(xattrs)
917 xattrs = simplegeneric.generic(xattrs)
918
918
919
919
920 def xattrs_complex(self, mode="default"):
920 def xattrs_complex(self, mode="default"):
921 if mode == "detail":
921 if mode == "detail":
922 return (AttributeDescriptor("real"), AttributeDescriptor("imag"))
922 return (AttributeDescriptor("real"), AttributeDescriptor("imag"))
923 return (selfdescriptor,)
923 return (selfdescriptor,)
924 xattrs.when_type(complex)(xattrs_complex)
924 xattrs.when_type(complex)(xattrs_complex)
925
925
926
926
927 def _isdict(item):
927 def _isdict(item):
928 try:
928 try:
929 itermeth = item.__class__.__iter__
929 itermeth = item.__class__.__iter__
930 except (AttributeError, TypeError):
930 except (AttributeError, TypeError):
931 return False
931 return False
932 return itermeth is dict.__iter__ or itermeth is types.DictProxyType.__iter__
932 return itermeth is dict.__iter__ or itermeth is types.DictProxyType.__iter__
933
933
934
934
935 def _isstr(item):
935 def _isstr(item):
936 if not isinstance(item, basestring):
936 if not isinstance(item, basestring):
937 return False
937 return False
938 try:
938 try:
939 itermeth = item.__class__.__iter__
939 itermeth = item.__class__.__iter__
940 except AttributeError:
940 except AttributeError:
941 return True
941 return True
942 return False # ``__iter__`` has been redefined
942 return False # ``__iter__`` has been redefined
943
943
944
944
945 def xiter(item):
945 def xiter(item):
946 """
946 """
947 Generic function that implements iteration for pipeline expression. If no
947 Generic function that implements iteration for pipeline expression. If no
948 implementation is registered for ``item`` ``xiter`` falls back to ``iter``.
948 implementation is registered for ``item`` ``xiter`` falls back to ``iter``.
949 """
949 """
950 try:
950 try:
951 func = item.__xiter__
951 func = item.__xiter__
952 except AttributeError:
952 except AttributeError:
953 if _isdict(item):
953 if _isdict(item):
954 def items(item):
954 def items(item):
955 fields = ("key", "value")
955 fields = ("key", "value")
956 for (key, value) in item.iteritems():
956 for (key, value) in item.iteritems():
957 yield Fields(fields, key=key, value=value)
957 yield Fields(fields, key=key, value=value)
958 return items(item)
958 return items(item)
959 elif isinstance(item, new.module):
959 elif isinstance(item, new.module):
960 def items(item):
960 def items(item):
961 fields = ("key", "value")
961 fields = ("key", "value")
962 for key in sorted(item.__dict__):
962 for key in sorted(item.__dict__):
963 yield Fields(fields, key=key, value=getattr(item, key))
963 yield Fields(fields, key=key, value=getattr(item, key))
964 return items(item)
964 return items(item)
965 elif _isstr(item):
965 elif _isstr(item):
966 if not item:
966 if not item:
967 raise ValueError("can't enter empty string")
967 raise ValueError("can't enter empty string")
968 lines = item.splitlines()
968 lines = item.splitlines()
969 if len(lines) == 1:
969 if len(lines) == 1:
970 def iterone(item):
970 def iterone(item):
971 yield item
971 yield item
972 return iterone(item)
972 return iterone(item)
973 else:
973 else:
974 return iter(lines)
974 return iter(lines)
975 return iter(item)
975 return iter(item)
976 else:
976 else:
977 return iter(func()) # iter() just to be safe
977 return iter(func()) # iter() just to be safe
978 xiter = simplegeneric.generic(xiter)
978 xiter = simplegeneric.generic(xiter)
979
979
980
980
981 class ichain(Pipe):
981 class ichain(Pipe):
982 """
982 """
983 Chains multiple ``Table``s into one.
983 Chains multiple ``Table``s into one.
984 """
984 """
985
985
986 def __init__(self, *iters):
986 def __init__(self, *iters):
987 self.iters = iters
987 self.iters = iters
988
988
989 def __iter__(self):
989 def __iter__(self):
990 return itertools.chain(*self.iters)
990 return itertools.chain(*self.iters)
991
991
992 def __xrepr__(self, mode="default"):
992 def __xrepr__(self, mode="default"):
993 if mode == "header" or mode == "footer":
993 if mode == "header" or mode == "footer":
994 for (i, item) in enumerate(self.iters):
994 for (i, item) in enumerate(self.iters):
995 if i:
995 if i:
996 yield (astyle.style_default, "+")
996 yield (astyle.style_default, "+")
997 if isinstance(item, Pipe):
997 if isinstance(item, Pipe):
998 yield (astyle.style_default, "(")
998 yield (astyle.style_default, "(")
999 for part in xrepr(item, mode):
999 for part in xrepr(item, mode):
1000 yield part
1000 yield part
1001 if isinstance(item, Pipe):
1001 if isinstance(item, Pipe):
1002 yield (astyle.style_default, ")")
1002 yield (astyle.style_default, ")")
1003 else:
1003 else:
1004 yield (astyle.style_default, repr(self))
1004 yield (astyle.style_default, repr(self))
1005
1005
1006 def __repr__(self):
1006 def __repr__(self):
1007 args = ", ".join([repr(it) for it in self.iters])
1007 args = ", ".join([repr(it) for it in self.iters])
1008 return "%s.%s(%s)" % \
1008 return "%s.%s(%s)" % \
1009 (self.__class__.__module__, self.__class__.__name__, args)
1009 (self.__class__.__module__, self.__class__.__name__, args)
1010
1010
1011
1011
1012 class ifile(path.path):
1012 class ifile(path.path):
1013 """
1013 """
1014 file (or directory) object.
1014 file (or directory) object.
1015 """
1015 """
1016
1016
1017 def getmode(self):
1017 def getmode(self):
1018 return self.stat().st_mode
1018 return self.stat().st_mode
1019 mode = property(getmode, None, None, "Access mode")
1019 mode = property(getmode, None, None, "Access mode")
1020
1020
1021 def gettype(self):
1021 def gettype(self):
1022 data = [
1022 data = [
1023 (stat.S_ISREG, "file"),
1023 (stat.S_ISREG, "file"),
1024 (stat.S_ISDIR, "dir"),
1024 (stat.S_ISDIR, "dir"),
1025 (stat.S_ISCHR, "chardev"),
1025 (stat.S_ISCHR, "chardev"),
1026 (stat.S_ISBLK, "blockdev"),
1026 (stat.S_ISBLK, "blockdev"),
1027 (stat.S_ISFIFO, "fifo"),
1027 (stat.S_ISFIFO, "fifo"),
1028 (stat.S_ISLNK, "symlink"),
1028 (stat.S_ISLNK, "symlink"),
1029 (stat.S_ISSOCK,"socket"),
1029 (stat.S_ISSOCK,"socket"),
1030 ]
1030 ]
1031 lstat = self.lstat()
1031 lstat = self.lstat()
1032 if lstat is not None:
1032 if lstat is not None:
1033 types = set([text for (func, text) in data if func(lstat.st_mode)])
1033 types = set([text for (func, text) in data if func(lstat.st_mode)])
1034 else:
1034 else:
1035 types = set()
1035 types = set()
1036 m = self.mode
1036 m = self.mode
1037 types.update([text for (func, text) in data if func(m)])
1037 types.update([text for (func, text) in data if func(m)])
1038 return ", ".join(types)
1038 return ", ".join(types)
1039 type = property(gettype, None, None, "file type (file, directory, link, etc.)")
1039 type = property(gettype, None, None, "file type (file, directory, link, etc.)")
1040
1040
1041 def getmodestr(self):
1041 def getmodestr(self):
1042 m = self.mode
1042 m = self.mode
1043 data = [
1043 data = [
1044 (stat.S_IRUSR, "-r"),
1044 (stat.S_IRUSR, "-r"),
1045 (stat.S_IWUSR, "-w"),
1045 (stat.S_IWUSR, "-w"),
1046 (stat.S_IXUSR, "-x"),
1046 (stat.S_IXUSR, "-x"),
1047 (stat.S_IRGRP, "-r"),
1047 (stat.S_IRGRP, "-r"),
1048 (stat.S_IWGRP, "-w"),
1048 (stat.S_IWGRP, "-w"),
1049 (stat.S_IXGRP, "-x"),
1049 (stat.S_IXGRP, "-x"),
1050 (stat.S_IROTH, "-r"),
1050 (stat.S_IROTH, "-r"),
1051 (stat.S_IWOTH, "-w"),
1051 (stat.S_IWOTH, "-w"),
1052 (stat.S_IXOTH, "-x"),
1052 (stat.S_IXOTH, "-x"),
1053 ]
1053 ]
1054 return "".join([text[bool(m&bit)] for (bit, text) in data])
1054 return "".join([text[bool(m&bit)] for (bit, text) in data])
1055
1055
1056 modestr = property(getmodestr, None, None, "Access mode as string")
1056 modestr = property(getmodestr, None, None, "Access mode as string")
1057
1057
1058 def getblocks(self):
1058 def getblocks(self):
1059 return self.stat().st_blocks
1059 return self.stat().st_blocks
1060 blocks = property(getblocks, None, None, "File size in blocks")
1060 blocks = property(getblocks, None, None, "File size in blocks")
1061
1061
1062 def getblksize(self):
1062 def getblksize(self):
1063 return self.stat().st_blksize
1063 return self.stat().st_blksize
1064 blksize = property(getblksize, None, None, "Filesystem block size")
1064 blksize = property(getblksize, None, None, "Filesystem block size")
1065
1065
1066 def getdev(self):
1066 def getdev(self):
1067 return self.stat().st_dev
1067 return self.stat().st_dev
1068 dev = property(getdev)
1068 dev = property(getdev)
1069
1069
1070 def getnlink(self):
1070 def getnlink(self):
1071 return self.stat().st_nlink
1071 return self.stat().st_nlink
1072 nlink = property(getnlink, None, None, "Number of links")
1072 nlink = property(getnlink, None, None, "Number of links")
1073
1073
1074 def getuid(self):
1074 def getuid(self):
1075 return self.stat().st_uid
1075 return self.stat().st_uid
1076 uid = property(getuid, None, None, "User id of file owner")
1076 uid = property(getuid, None, None, "User id of file owner")
1077
1077
1078 def getgid(self):
1078 def getgid(self):
1079 return self.stat().st_gid
1079 return self.stat().st_gid
1080 gid = property(getgid, None, None, "Group id of file owner")
1080 gid = property(getgid, None, None, "Group id of file owner")
1081
1081
1082 def getowner(self):
1082 def getowner(self):
1083 stat = self.stat()
1083 stat = self.stat()
1084 try:
1084 try:
1085 return pwd.getpwuid(stat.st_uid).pw_name
1085 return pwd.getpwuid(stat.st_uid).pw_name
1086 except KeyError:
1086 except KeyError:
1087 return stat.st_uid
1087 return stat.st_uid
1088 owner = property(getowner, None, None, "Owner name (or id)")
1088 owner = property(getowner, None, None, "Owner name (or id)")
1089
1089
1090 def getgroup(self):
1090 def getgroup(self):
1091 stat = self.stat()
1091 stat = self.stat()
1092 try:
1092 try:
1093 return grp.getgrgid(stat.st_gid).gr_name
1093 return grp.getgrgid(stat.st_gid).gr_name
1094 except KeyError:
1094 except KeyError:
1095 return stat.st_gid
1095 return stat.st_gid
1096 group = property(getgroup, None, None, "Group name (or id)")
1096 group = property(getgroup, None, None, "Group name (or id)")
1097
1097
1098 def getadate(self):
1098 def getadate(self):
1099 return datetime.datetime.utcfromtimestamp(self.atime)
1099 return datetime.datetime.utcfromtimestamp(self.atime)
1100 adate = property(getadate, None, None, "Access date")
1100 adate = property(getadate, None, None, "Access date")
1101
1101
1102 def getcdate(self):
1102 def getcdate(self):
1103 return datetime.datetime.utcfromtimestamp(self.ctime)
1103 return datetime.datetime.utcfromtimestamp(self.ctime)
1104 cdate = property(getcdate, None, None, "Creation date")
1104 cdate = property(getcdate, None, None, "Creation date")
1105
1105
1106 def getmdate(self):
1106 def getmdate(self):
1107 return datetime.datetime.utcfromtimestamp(self.mtime)
1107 return datetime.datetime.utcfromtimestamp(self.mtime)
1108 mdate = property(getmdate, None, None, "Modification date")
1108 mdate = property(getmdate, None, None, "Modification date")
1109
1109
1110 def mimetype(self):
1110 def mimetype(self):
1111 """
1111 """
1112 Return MIME type guessed from the extension.
1112 Return MIME type guessed from the extension.
1113 """
1113 """
1114 return mimetypes.guess_type(self.basename())[0]
1114 return mimetypes.guess_type(self.basename())[0]
1115
1115
1116 def encoding(self):
1116 def encoding(self):
1117 """
1117 """
1118 Return guessed compression (like "compress" or "gzip").
1118 Return guessed compression (like "compress" or "gzip").
1119 """
1119 """
1120 return mimetypes.guess_type(self.basename())[1]
1120 return mimetypes.guess_type(self.basename())[1]
1121
1121
1122 def __repr__(self):
1122 def __repr__(self):
1123 return "ifile(%s)" % path._base.__repr__(self)
1123 return "ifile(%s)" % path._base.__repr__(self)
1124
1124
1125 if sys.platform == "win32":
1125 if sys.platform == "win32":
1126 defaultattrs = (None, "type", "size", "modestr", "mdate")
1126 defaultattrs = (None, "type", "size", "modestr", "mdate")
1127 else:
1127 else:
1128 defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate")
1128 defaultattrs = (None, "type", "size", "modestr", "owner", "group", "mdate")
1129
1129
1130 def __xattrs__(self, mode="default"):
1130 def __xattrs__(self, mode="default"):
1131 if mode == "detail":
1131 if mode == "detail":
1132 return (
1132 return (
1133 "name",
1133 "name",
1134 "basename()",
1134 "basename()",
1135 "abspath()",
1135 "abspath()",
1136 "realpath()",
1136 "realpath()",
1137 "type",
1137 "type",
1138 "mode",
1138 "mode",
1139 "modestr",
1139 "modestr",
1140 "stat()",
1140 "stat()",
1141 "lstat()",
1141 "lstat()",
1142 "uid",
1142 "uid",
1143 "gid",
1143 "gid",
1144 "owner",
1144 "owner",
1145 "group",
1145 "group",
1146 "dev",
1146 "dev",
1147 "nlink",
1147 "nlink",
1148 "ctime",
1148 "ctime",
1149 "mtime",
1149 "mtime",
1150 "atime",
1150 "atime",
1151 "cdate",
1151 "cdate",
1152 "mdate",
1152 "mdate",
1153 "adate",
1153 "adate",
1154 "size",
1154 "size",
1155 "blocks",
1155 "blocks",
1156 "blksize",
1156 "blksize",
1157 "isdir()",
1157 "isdir()",
1158 "islink()",
1158 "islink()",
1159 "mimetype()",
1159 "mimetype()",
1160 "encoding()",
1160 "encoding()",
1161 "-listdir()",
1161 "-listdir()",
1162 "-dirs()",
1162 "-dirs()",
1163 "-files()",
1163 "-files()",
1164 "-walk()",
1164 "-walk()",
1165 "-walkdirs()",
1165 "-walkdirs()",
1166 "-walkfiles()",
1166 "-walkfiles()",
1167 )
1167 )
1168 else:
1168 else:
1169 return self.defaultattrs
1169 return self.defaultattrs
1170
1170
1171
1171
1172 def xiter_ifile(self):
1172 def xiter_ifile(self):
1173 if self.isdir():
1173 if self.isdir():
1174 yield (self / os.pardir).abspath()
1174 yield (self / os.pardir).abspath()
1175 for child in sorted(self.listdir()):
1175 for child in sorted(self.listdir()):
1176 yield child
1176 yield child
1177 else:
1177 else:
1178 f = self.open("rb")
1178 f = self.open("rb")
1179 for line in f:
1179 for line in f:
1180 yield line
1180 yield line
1181 f.close()
1181 f.close()
1182 xiter.when_type(ifile)(xiter_ifile)
1182 xiter.when_type(ifile)(xiter_ifile)
1183
1183
1184
1184
1185 # We need to implement ``xrepr`` for ``ifile`` as a generic function, because
1185 # We need to implement ``xrepr`` for ``ifile`` as a generic function, because
1186 # otherwise ``xrepr_str`` would kick in.
1186 # otherwise ``xrepr_str`` would kick in.
1187 def xrepr_ifile(self, mode="default"):
1187 def xrepr_ifile(self, mode="default"):
1188 try:
1188 try:
1189 if self.isdir():
1189 if self.isdir():
1190 name = "idir"
1190 name = "idir"
1191 style = astyle.style_dir
1191 style = astyle.style_dir
1192 else:
1192 else:
1193 name = "ifile"
1193 name = "ifile"
1194 style = astyle.style_file
1194 style = astyle.style_file
1195 except IOError:
1195 except IOError:
1196 name = "ifile"
1196 name = "ifile"
1197 style = astyle.style_default
1197 style = astyle.style_default
1198 if mode in ("cell", "header", "footer"):
1198 if mode in ("cell", "header", "footer"):
1199 abspath = repr(path._base(self.normpath()))
1199 abspath = repr(path._base(self.normpath()))
1200 if abspath.startswith("u"):
1200 if abspath.startswith("u"):
1201 abspath = abspath[2:-1]
1201 abspath = abspath[2:-1]
1202 else:
1202 else:
1203 abspath = abspath[1:-1]
1203 abspath = abspath[1:-1]
1204 if mode == "cell":
1204 if mode == "cell":
1205 yield (style, abspath)
1205 yield (style, abspath)
1206 else:
1206 else:
1207 yield (style, "%s(%s)" % (name, abspath))
1207 yield (style, "%s(%s)" % (name, abspath))
1208 else:
1208 else:
1209 yield (style, repr(self))
1209 yield (style, repr(self))
1210 xrepr.when_type(ifile)(xrepr_ifile)
1210 xrepr.when_type(ifile)(xrepr_ifile)
1211
1211
1212
1212
1213 class ils(Table):
1213 class ils(Table):
1214 """
1214 """
1215 List the current (or a specified) directory.
1215 List the current (or a specified) directory.
1216
1216
1217 Examples::
1217 Examples::
1218
1218
1219 >>> ils
1219 >>> ils
1220 <class 'IPython.extensions.ipipe.ils'>
1220 <class 'IPython.extensions.ipipe.ils'>
1221 >>> ils("/usr/local/lib/python2.4")
1221 >>> ils("/usr/local/lib/python2.4")
1222 IPython.extensions.ipipe.ils('/usr/local/lib/python2.4')
1222 IPython.extensions.ipipe.ils('/usr/local/lib/python2.4')
1223 >>> ils("~")
1223 >>> ils("~")
1224 IPython.extensions.ipipe.ils('/home/fperez')
1224 IPython.extensions.ipipe.ils('/home/fperez')
1225 # all-random
1225 # all-random
1226 """
1226 """
1227 def __init__(self, base=os.curdir, dirs=True, files=True):
1227 def __init__(self, base=os.curdir, dirs=True, files=True):
1228 self.base = os.path.expanduser(base)
1228 self.base = os.path.expanduser(base)
1229 self.dirs = dirs
1229 self.dirs = dirs
1230 self.files = files
1230 self.files = files
1231
1231
1232 def __iter__(self):
1232 def __iter__(self):
1233 base = ifile(self.base)
1233 base = ifile(self.base)
1234 yield (base / os.pardir).abspath()
1234 yield (base / os.pardir).abspath()
1235 for child in sorted(base.listdir()):
1235 for child in sorted(base.listdir()):
1236 if self.dirs:
1236 if self.dirs:
1237 if self.files:
1237 if self.files:
1238 yield child
1238 yield child
1239 else:
1239 else:
1240 if child.isdir():
1240 if child.isdir():
1241 yield child
1241 yield child
1242 elif self.files:
1242 elif self.files:
1243 if not child.isdir():
1243 if not child.isdir():
1244 yield child
1244 yield child
1245
1245
1246 def __xrepr__(self, mode="default"):
1246 def __xrepr__(self, mode="default"):
1247 return xrepr(ifile(self.base), mode)
1247 return xrepr(ifile(self.base), mode)
1248
1248
1249 def __repr__(self):
1249 def __repr__(self):
1250 return "%s.%s(%r)" % \
1250 return "%s.%s(%r)" % \
1251 (self.__class__.__module__, self.__class__.__name__, self.base)
1251 (self.__class__.__module__, self.__class__.__name__, self.base)
1252
1252
1253
1253
1254 class iglob(Table):
1254 class iglob(Table):
1255 """
1255 """
1256 List all files and directories matching a specified pattern.
1256 List all files and directories matching a specified pattern.
1257 (See ``glob.glob()`` for more info.).
1257 (See ``glob.glob()`` for more info.).
1258
1258
1259 Examples::
1259 Examples::
1260
1260
1261 >>> iglob("*.py")
1261 >>> iglob("*.py")
1262 IPython.extensions.ipipe.iglob('*.py')
1262 IPython.extensions.ipipe.iglob('*.py')
1263 """
1263 """
1264 def __init__(self, glob):
1264 def __init__(self, glob):
1265 self.glob = glob
1265 self.glob = glob
1266
1266
1267 def __iter__(self):
1267 def __iter__(self):
1268 for name in glob.glob(self.glob):
1268 for name in glob.glob(self.glob):
1269 yield ifile(name)
1269 yield ifile(name)
1270
1270
1271 def __xrepr__(self, mode="default"):
1271 def __xrepr__(self, mode="default"):
1272 if mode == "header" or mode == "footer" or mode == "cell":
1272 if mode == "header" or mode == "footer" or mode == "cell":
1273 yield (astyle.style_default,
1273 yield (astyle.style_default,
1274 "%s(%r)" % (self.__class__.__name__, self.glob))
1274 "%s(%r)" % (self.__class__.__name__, self.glob))
1275 else:
1275 else:
1276 yield (astyle.style_default, repr(self))
1276 yield (astyle.style_default, repr(self))
1277
1277
1278 def __repr__(self):
1278 def __repr__(self):
1279 return "%s.%s(%r)" % \
1279 return "%s.%s(%r)" % \
1280 (self.__class__.__module__, self.__class__.__name__, self.glob)
1280 (self.__class__.__module__, self.__class__.__name__, self.glob)
1281
1281
1282
1282
1283 class iwalk(Table):
1283 class iwalk(Table):
1284 """
1284 """
1285 List all files and directories in a directory and it's subdirectory::
1285 List all files and directories in a directory and it's subdirectory::
1286
1286
1287 >>> iwalk
1287 >>> iwalk
1288 <class 'IPython.extensions.ipipe.iwalk'>
1288 <class 'IPython.extensions.ipipe.iwalk'>
1289 >>> iwalk("/usr/lib")
1289 >>> iwalk("/usr/lib")
1290 IPython.extensions.ipipe.iwalk('/usr/lib')
1290 IPython.extensions.ipipe.iwalk('/usr/lib')
1291 >>> iwalk("~")
1291 >>> iwalk("~")
1292 IPython.extensions.ipipe.iwalk('/home/fperez') # random
1292 IPython.extensions.ipipe.iwalk('/home/fperez') # random
1293
1293
1294 """
1294 """
1295 def __init__(self, base=os.curdir, dirs=True, files=True):
1295 def __init__(self, base=os.curdir, dirs=True, files=True):
1296 self.base = os.path.expanduser(base)
1296 self.base = os.path.expanduser(base)
1297 self.dirs = dirs
1297 self.dirs = dirs
1298 self.files = files
1298 self.files = files
1299
1299
1300 def __iter__(self):
1300 def __iter__(self):
1301 for (dirpath, dirnames, filenames) in os.walk(self.base):
1301 for (dirpath, dirnames, filenames) in os.walk(self.base):
1302 if self.dirs:
1302 if self.dirs:
1303 for name in sorted(dirnames):
1303 for name in sorted(dirnames):
1304 yield ifile(os.path.join(dirpath, name))
1304 yield ifile(os.path.join(dirpath, name))
1305 if self.files:
1305 if self.files:
1306 for name in sorted(filenames):
1306 for name in sorted(filenames):
1307 yield ifile(os.path.join(dirpath, name))
1307 yield ifile(os.path.join(dirpath, name))
1308
1308
1309 def __xrepr__(self, mode="default"):
1309 def __xrepr__(self, mode="default"):
1310 if mode == "header" or mode == "footer" or mode == "cell":
1310 if mode == "header" or mode == "footer" or mode == "cell":
1311 yield (astyle.style_default,
1311 yield (astyle.style_default,
1312 "%s(%r)" % (self.__class__.__name__, self.base))
1312 "%s(%r)" % (self.__class__.__name__, self.base))
1313 else:
1313 else:
1314 yield (astyle.style_default, repr(self))
1314 yield (astyle.style_default, repr(self))
1315
1315
1316 def __repr__(self):
1316 def __repr__(self):
1317 return "%s.%s(%r)" % \
1317 return "%s.%s(%r)" % \
1318 (self.__class__.__module__, self.__class__.__name__, self.base)
1318 (self.__class__.__module__, self.__class__.__name__, self.base)
1319
1319
1320
1320
1321 class ipwdentry(object):
1321 class ipwdentry(object):
1322 """
1322 """
1323 ``ipwdentry`` objects encapsulate entries in the Unix user account and
1323 ``ipwdentry`` objects encapsulate entries in the Unix user account and
1324 password database.
1324 password database.
1325 """
1325 """
1326 def __init__(self, id):
1326 def __init__(self, id):
1327 self._id = id
1327 self._id = id
1328 self._entry = None
1328 self._entry = None
1329
1329
1330 def __eq__(self, other):
1330 def __eq__(self, other):
1331 return self.__class__ is other.__class__ and self._id == other._id
1331 return self.__class__ is other.__class__ and self._id == other._id
1332
1332
1333 def __ne__(self, other):
1333 def __ne__(self, other):
1334 return self.__class__ is not other.__class__ or self._id != other._id
1334 return self.__class__ is not other.__class__ or self._id != other._id
1335
1335
1336 def _getentry(self):
1336 def _getentry(self):
1337 if self._entry is None:
1337 if self._entry is None:
1338 if isinstance(self._id, basestring):
1338 if isinstance(self._id, basestring):
1339 self._entry = pwd.getpwnam(self._id)
1339 self._entry = pwd.getpwnam(self._id)
1340 else:
1340 else:
1341 self._entry = pwd.getpwuid(self._id)
1341 self._entry = pwd.getpwuid(self._id)
1342 return self._entry
1342 return self._entry
1343
1343
1344 def getname(self):
1344 def getname(self):
1345 if isinstance(self._id, basestring):
1345 if isinstance(self._id, basestring):
1346 return self._id
1346 return self._id
1347 else:
1347 else:
1348 return self._getentry().pw_name
1348 return self._getentry().pw_name
1349 name = property(getname, None, None, "User name")
1349 name = property(getname, None, None, "User name")
1350
1350
1351 def getpasswd(self):
1351 def getpasswd(self):
1352 return self._getentry().pw_passwd
1352 return self._getentry().pw_passwd
1353 passwd = property(getpasswd, None, None, "Password")
1353 passwd = property(getpasswd, None, None, "Password")
1354
1354
1355 def getuid(self):
1355 def getuid(self):
1356 if isinstance(self._id, basestring):
1356 if isinstance(self._id, basestring):
1357 return self._getentry().pw_uid
1357 return self._getentry().pw_uid
1358 else:
1358 else:
1359 return self._id
1359 return self._id
1360 uid = property(getuid, None, None, "User id")
1360 uid = property(getuid, None, None, "User id")
1361
1361
1362 def getgid(self):
1362 def getgid(self):
1363 return self._getentry().pw_gid
1363 return self._getentry().pw_gid
1364 gid = property(getgid, None, None, "Primary group id")
1364 gid = property(getgid, None, None, "Primary group id")
1365
1365
1366 def getgroup(self):
1366 def getgroup(self):
1367 return igrpentry(self.gid)
1367 return igrpentry(self.gid)
1368 group = property(getgroup, None, None, "Group")
1368 group = property(getgroup, None, None, "Group")
1369
1369
1370 def getgecos(self):
1370 def getgecos(self):
1371 return self._getentry().pw_gecos
1371 return self._getentry().pw_gecos
1372 gecos = property(getgecos, None, None, "Information (e.g. full user name)")
1372 gecos = property(getgecos, None, None, "Information (e.g. full user name)")
1373
1373
1374 def getdir(self):
1374 def getdir(self):
1375 return self._getentry().pw_dir
1375 return self._getentry().pw_dir
1376 dir = property(getdir, None, None, "$HOME directory")
1376 dir = property(getdir, None, None, "$HOME directory")
1377
1377
1378 def getshell(self):
1378 def getshell(self):
1379 return self._getentry().pw_shell
1379 return self._getentry().pw_shell
1380 shell = property(getshell, None, None, "Login shell")
1380 shell = property(getshell, None, None, "Login shell")
1381
1381
1382 def __xattrs__(self, mode="default"):
1382 def __xattrs__(self, mode="default"):
1383 return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell")
1383 return ("name", "passwd", "uid", "gid", "gecos", "dir", "shell")
1384
1384
1385 def __repr__(self):
1385 def __repr__(self):
1386 return "%s.%s(%r)" % \
1386 return "%s.%s(%r)" % \
1387 (self.__class__.__module__, self.__class__.__name__, self._id)
1387 (self.__class__.__module__, self.__class__.__name__, self._id)
1388
1388
1389
1389
1390 class ipwd(Table):
1390 class ipwd(Table):
1391 """
1391 """
1392 List all entries in the Unix user account and password database.
1392 List all entries in the Unix user account and password database.
1393
1393
1394 Example::
1394 Example::
1395
1395
1396 >>> ipwd | isort("uid")
1396 >>> ipwd | isort("uid")
1397 <IPython.extensions.ipipe.isort key='uid' reverse=False at 0x849efec>
1397 <IPython.extensions.ipipe.isort key='uid' reverse=False at 0x849efec>
1398 # random
1398 # random
1399 """
1399 """
1400 def __iter__(self):
1400 def __iter__(self):
1401 for entry in pwd.getpwall():
1401 for entry in pwd.getpwall():
1402 yield ipwdentry(entry.pw_name)
1402 yield ipwdentry(entry.pw_name)
1403
1403
1404 def __xrepr__(self, mode="default"):
1404 def __xrepr__(self, mode="default"):
1405 if mode == "header" or mode == "footer" or mode == "cell":
1405 if mode == "header" or mode == "footer" or mode == "cell":
1406 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1406 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1407 else:
1407 else:
1408 yield (astyle.style_default, repr(self))
1408 yield (astyle.style_default, repr(self))
1409
1409
1410
1410
1411 class igrpentry(object):
1411 class igrpentry(object):
1412 """
1412 """
1413 ``igrpentry`` objects encapsulate entries in the Unix group database.
1413 ``igrpentry`` objects encapsulate entries in the Unix group database.
1414 """
1414 """
1415 def __init__(self, id):
1415 def __init__(self, id):
1416 self._id = id
1416 self._id = id
1417 self._entry = None
1417 self._entry = None
1418
1418
1419 def __eq__(self, other):
1419 def __eq__(self, other):
1420 return self.__class__ is other.__class__ and self._id == other._id
1420 return self.__class__ is other.__class__ and self._id == other._id
1421
1421
1422 def __ne__(self, other):
1422 def __ne__(self, other):
1423 return self.__class__ is not other.__class__ or self._id != other._id
1423 return self.__class__ is not other.__class__ or self._id != other._id
1424
1424
1425 def _getentry(self):
1425 def _getentry(self):
1426 if self._entry is None:
1426 if self._entry is None:
1427 if isinstance(self._id, basestring):
1427 if isinstance(self._id, basestring):
1428 self._entry = grp.getgrnam(self._id)
1428 self._entry = grp.getgrnam(self._id)
1429 else:
1429 else:
1430 self._entry = grp.getgrgid(self._id)
1430 self._entry = grp.getgrgid(self._id)
1431 return self._entry
1431 return self._entry
1432
1432
1433 def getname(self):
1433 def getname(self):
1434 if isinstance(self._id, basestring):
1434 if isinstance(self._id, basestring):
1435 return self._id
1435 return self._id
1436 else:
1436 else:
1437 return self._getentry().gr_name
1437 return self._getentry().gr_name
1438 name = property(getname, None, None, "Group name")
1438 name = property(getname, None, None, "Group name")
1439
1439
1440 def getpasswd(self):
1440 def getpasswd(self):
1441 return self._getentry().gr_passwd
1441 return self._getentry().gr_passwd
1442 passwd = property(getpasswd, None, None, "Password")
1442 passwd = property(getpasswd, None, None, "Password")
1443
1443
1444 def getgid(self):
1444 def getgid(self):
1445 if isinstance(self._id, basestring):
1445 if isinstance(self._id, basestring):
1446 return self._getentry().gr_gid
1446 return self._getentry().gr_gid
1447 else:
1447 else:
1448 return self._id
1448 return self._id
1449 gid = property(getgid, None, None, "Group id")
1449 gid = property(getgid, None, None, "Group id")
1450
1450
1451 def getmem(self):
1451 def getmem(self):
1452 return self._getentry().gr_mem
1452 return self._getentry().gr_mem
1453 mem = property(getmem, None, None, "Members")
1453 mem = property(getmem, None, None, "Members")
1454
1454
1455 def __xattrs__(self, mode="default"):
1455 def __xattrs__(self, mode="default"):
1456 return ("name", "passwd", "gid", "mem")
1456 return ("name", "passwd", "gid", "mem")
1457
1457
1458 def __xrepr__(self, mode="default"):
1458 def __xrepr__(self, mode="default"):
1459 if mode == "header" or mode == "footer" or mode == "cell":
1459 if mode == "header" or mode == "footer" or mode == "cell":
1460 yield (astyle.style_default, "group ")
1460 yield (astyle.style_default, "group ")
1461 try:
1461 try:
1462 yield (astyle.style_default, self.name)
1462 yield (astyle.style_default, self.name)
1463 except KeyError:
1463 except KeyError:
1464 if isinstance(self._id, basestring):
1464 if isinstance(self._id, basestring):
1465 yield (astyle.style_default, self.name_id)
1465 yield (astyle.style_default, self.name_id)
1466 else:
1466 else:
1467 yield (astyle.style_type_number, str(self._id))
1467 yield (astyle.style_type_number, str(self._id))
1468 else:
1468 else:
1469 yield (astyle.style_default, repr(self))
1469 yield (astyle.style_default, repr(self))
1470
1470
1471 def __iter__(self):
1471 def __iter__(self):
1472 for member in self.mem:
1472 for member in self.mem:
1473 yield ipwdentry(member)
1473 yield ipwdentry(member)
1474
1474
1475 def __repr__(self):
1475 def __repr__(self):
1476 return "%s.%s(%r)" % \
1476 return "%s.%s(%r)" % \
1477 (self.__class__.__module__, self.__class__.__name__, self._id)
1477 (self.__class__.__module__, self.__class__.__name__, self._id)
1478
1478
1479
1479
1480 class igrp(Table):
1480 class igrp(Table):
1481 """
1481 """
1482 This ``Table`` lists all entries in the Unix group database.
1482 This ``Table`` lists all entries in the Unix group database.
1483 """
1483 """
1484 def __iter__(self):
1484 def __iter__(self):
1485 for entry in grp.getgrall():
1485 for entry in grp.getgrall():
1486 yield igrpentry(entry.gr_name)
1486 yield igrpentry(entry.gr_name)
1487
1487
1488 def __xrepr__(self, mode="default"):
1488 def __xrepr__(self, mode="default"):
1489 if mode == "header" or mode == "footer":
1489 if mode == "header" or mode == "footer":
1490 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1490 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1491 else:
1491 else:
1492 yield (astyle.style_default, repr(self))
1492 yield (astyle.style_default, repr(self))
1493
1493
1494
1494
1495 class Fields(object):
1495 class Fields(object):
1496 def __init__(self, fieldnames, **fields):
1496 def __init__(self, fieldnames, **fields):
1497 self.__fieldnames = [upgradexattr(fieldname) for fieldname in fieldnames]
1497 self.__fieldnames = [upgradexattr(fieldname) for fieldname in fieldnames]
1498 for (key, value) in fields.iteritems():
1498 for (key, value) in fields.iteritems():
1499 setattr(self, key, value)
1499 setattr(self, key, value)
1500
1500
1501 def __xattrs__(self, mode="default"):
1501 def __xattrs__(self, mode="default"):
1502 return self.__fieldnames
1502 return self.__fieldnames
1503
1503
1504 def __xrepr__(self, mode="default"):
1504 def __xrepr__(self, mode="default"):
1505 yield (-1, False)
1505 yield (-1, False)
1506 if mode == "header" or mode == "cell":
1506 if mode == "header" or mode == "cell":
1507 yield (astyle.style_default, self.__class__.__name__)
1507 yield (astyle.style_default, self.__class__.__name__)
1508 yield (astyle.style_default, "(")
1508 yield (astyle.style_default, "(")
1509 for (i, f) in enumerate(self.__fieldnames):
1509 for (i, f) in enumerate(self.__fieldnames):
1510 if i:
1510 if i:
1511 yield (astyle.style_default, ", ")
1511 yield (astyle.style_default, ", ")
1512 yield (astyle.style_default, f.name())
1512 yield (astyle.style_default, f.name())
1513 yield (astyle.style_default, "=")
1513 yield (astyle.style_default, "=")
1514 for part in xrepr(getattr(self, f), "default"):
1514 for part in xrepr(getattr(self, f), "default"):
1515 yield part
1515 yield part
1516 yield (astyle.style_default, ")")
1516 yield (astyle.style_default, ")")
1517 elif mode == "footer":
1517 elif mode == "footer":
1518 yield (astyle.style_default, self.__class__.__name__)
1518 yield (astyle.style_default, self.__class__.__name__)
1519 yield (astyle.style_default, "(")
1519 yield (astyle.style_default, "(")
1520 for (i, f) in enumerate(self.__fieldnames):
1520 for (i, f) in enumerate(self.__fieldnames):
1521 if i:
1521 if i:
1522 yield (astyle.style_default, ", ")
1522 yield (astyle.style_default, ", ")
1523 yield (astyle.style_default, f.name())
1523 yield (astyle.style_default, f.name())
1524 yield (astyle.style_default, ")")
1524 yield (astyle.style_default, ")")
1525 else:
1525 else:
1526 yield (astyle.style_default, repr(self))
1526 yield (astyle.style_default, repr(self))
1527
1527
1528
1528
1529 class FieldTable(Table, list):
1529 class FieldTable(Table, list):
1530 def __init__(self, *fields):
1530 def __init__(self, *fields):
1531 Table.__init__(self)
1531 Table.__init__(self)
1532 list.__init__(self)
1532 list.__init__(self)
1533 self.fields = fields
1533 self.fields = fields
1534
1534
1535 def add(self, **fields):
1535 def add(self, **fields):
1536 self.append(Fields(self.fields, **fields))
1536 self.append(Fields(self.fields, **fields))
1537
1537
1538 def __xrepr__(self, mode="default"):
1538 def __xrepr__(self, mode="default"):
1539 yield (-1, False)
1539 yield (-1, False)
1540 if mode == "header" or mode == "footer":
1540 if mode == "header" or mode == "footer":
1541 yield (astyle.style_default, self.__class__.__name__)
1541 yield (astyle.style_default, self.__class__.__name__)
1542 yield (astyle.style_default, "(")
1542 yield (astyle.style_default, "(")
1543 for (i, f) in enumerate(self.__fieldnames):
1543 for (i, f) in enumerate(self.__fieldnames):
1544 if i:
1544 if i:
1545 yield (astyle.style_default, ", ")
1545 yield (astyle.style_default, ", ")
1546 yield (astyle.style_default, f)
1546 yield (astyle.style_default, f)
1547 yield (astyle.style_default, ")")
1547 yield (astyle.style_default, ")")
1548 else:
1548 else:
1549 yield (astyle.style_default, repr(self))
1549 yield (astyle.style_default, repr(self))
1550
1550
1551 def __repr__(self):
1551 def __repr__(self):
1552 return "<%s.%s object with fields=%r at 0x%x>" % \
1552 return "<%s.%s object with fields=%r at 0x%x>" % \
1553 (self.__class__.__module__, self.__class__.__name__,
1553 (self.__class__.__module__, self.__class__.__name__,
1554 ", ".join(map(repr, self.fields)), id(self))
1554 ", ".join(map(repr, self.fields)), id(self))
1555
1555
1556
1556
1557 class List(list):
1557 class List(list):
1558 def __xattrs__(self, mode="default"):
1558 def __xattrs__(self, mode="default"):
1559 return xrange(len(self))
1559 return xrange(len(self))
1560
1560
1561 def __xrepr__(self, mode="default"):
1561 def __xrepr__(self, mode="default"):
1562 yield (-1, False)
1562 yield (-1, False)
1563 if mode == "header" or mode == "cell" or mode == "footer" or mode == "default":
1563 if mode == "header" or mode == "cell" or mode == "footer" or mode == "default":
1564 yield (astyle.style_default, self.__class__.__name__)
1564 yield (astyle.style_default, self.__class__.__name__)
1565 yield (astyle.style_default, "(")
1565 yield (astyle.style_default, "(")
1566 for (i, item) in enumerate(self):
1566 for (i, item) in enumerate(self):
1567 if i:
1567 if i:
1568 yield (astyle.style_default, ", ")
1568 yield (astyle.style_default, ", ")
1569 for part in xrepr(item, "default"):
1569 for part in xrepr(item, "default"):
1570 yield part
1570 yield part
1571 yield (astyle.style_default, ")")
1571 yield (astyle.style_default, ")")
1572 else:
1572 else:
1573 yield (astyle.style_default, repr(self))
1573 yield (astyle.style_default, repr(self))
1574
1574
1575
1575
1576 class ienv(Table):
1576 class ienv(Table):
1577 """
1577 """
1578 List environment variables.
1578 List environment variables.
1579
1579
1580 Example::
1580 Example::
1581
1581
1582 >>> ienv
1582 >>> ienv
1583 <class 'IPython.extensions.ipipe.ienv'>
1583 <class 'IPython.extensions.ipipe.ienv'>
1584 """
1584 """
1585
1585
1586 def __iter__(self):
1586 def __iter__(self):
1587 fields = ("key", "value")
1587 fields = ("key", "value")
1588 for (key, value) in os.environ.iteritems():
1588 for (key, value) in os.environ.iteritems():
1589 yield Fields(fields, key=key, value=value)
1589 yield Fields(fields, key=key, value=value)
1590
1590
1591 def __xrepr__(self, mode="default"):
1591 def __xrepr__(self, mode="default"):
1592 if mode == "header" or mode == "cell":
1592 if mode == "header" or mode == "cell":
1593 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1593 yield (astyle.style_default, "%s()" % self.__class__.__name__)
1594 else:
1594 else:
1595 yield (astyle.style_default, repr(self))
1595 yield (astyle.style_default, repr(self))
1596
1596
1597
1597
1598 class ihist(Table):
1598 class ihist(Table):
1599 """
1599 """
1600 IPython input history
1600 IPython input history
1601
1601
1602 Example::
1602 Example::
1603
1603
1604 >>> ihist
1604 >>> ihist
1605 <class 'IPython.extensions.ipipe.ihist'>
1605 <class 'IPython.extensions.ipipe.ihist'>
1606 >>> ihist(True) # raw mode
1606 >>> ihist(True) # raw mode
1607 <IPython.extensions.ipipe.ihist object at 0x849602c> # random
1607 <IPython.extensions.ipipe.ihist object at 0x849602c> # random
1608 """
1608 """
1609 def __init__(self, raw=True):
1609 def __init__(self, raw=True):
1610 self.raw = raw
1610 self.raw = raw
1611
1611
1612 def __iter__(self):
1612 def __iter__(self):
1613 api = ipapi.get()
1613 api = ipapi.get()
1614 if self.raw:
1614 if self.raw:
1615 for line in api.input_hist_raw:
1615 for line in api.input_hist_raw:
1616 yield line.rstrip("\n")
1616 yield line.rstrip("\n")
1617 else:
1617 else:
1618 for line in api.input_hist:
1618 for line in api.input_hist:
1619 yield line.rstrip("\n")
1619 yield line.rstrip("\n")
1620
1620
1621
1621
1622 class Alias(object):
1622 class Alias(object):
1623 """
1623 """
1624 Entry in the alias table
1624 Entry in the alias table
1625 """
1625 """
1626 def __init__(self, name, args, command):
1626 def __init__(self, name, args, command):
1627 self.name = name
1627 self.name = name
1628 self.args = args
1628 self.args = args
1629 self.command = command
1629 self.command = command
1630
1630
1631 def __xattrs__(self, mode="default"):
1631 def __xattrs__(self, mode="default"):
1632 return ("name", "args", "command")
1632 return ("name", "args", "command")
1633
1633
1634
1634
1635 class ialias(Table):
1635 class ialias(Table):
1636 """
1636 """
1637 IPython alias list
1637 IPython alias list
1638
1638
1639 Example::
1639 Example::
1640
1640
1641 >>> ialias
1641 >>> ialias
1642 <class 'IPython.extensions.ipipe.ialias'>
1642 <class 'IPython.extensions.ipipe.ialias'>
1643 """
1643 """
1644 def __iter__(self):
1644 def __iter__(self):
1645 api = ipapi.get()
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 yield Alias(name, args, command)
1648 yield Alias(name, args, command)
1649
1649
1650
1650
1651 class icsv(Pipe):
1651 class icsv(Pipe):
1652 """
1652 """
1653 This ``Pipe`` turns the input (with must be a pipe outputting lines
1653 This ``Pipe`` turns the input (with must be a pipe outputting lines
1654 or an ``ifile``) into lines of CVS columns.
1654 or an ``ifile``) into lines of CVS columns.
1655 """
1655 """
1656 def __init__(self, **csvargs):
1656 def __init__(self, **csvargs):
1657 """
1657 """
1658 Create an ``icsv`` object. ``cvsargs`` will be passed through as
1658 Create an ``icsv`` object. ``cvsargs`` will be passed through as
1659 keyword arguments to ``cvs.reader()``.
1659 keyword arguments to ``cvs.reader()``.
1660 """
1660 """
1661 self.csvargs = csvargs
1661 self.csvargs = csvargs
1662
1662
1663 def __iter__(self):
1663 def __iter__(self):
1664 input = self.input
1664 input = self.input
1665 if isinstance(input, ifile):
1665 if isinstance(input, ifile):
1666 input = input.open("rb")
1666 input = input.open("rb")
1667 reader = csv.reader(input, **self.csvargs)
1667 reader = csv.reader(input, **self.csvargs)
1668 for line in reader:
1668 for line in reader:
1669 yield List(line)
1669 yield List(line)
1670
1670
1671 def __xrepr__(self, mode="default"):
1671 def __xrepr__(self, mode="default"):
1672 yield (-1, False)
1672 yield (-1, False)
1673 if mode == "header" or mode == "footer":
1673 if mode == "header" or mode == "footer":
1674 input = getattr(self, "input", None)
1674 input = getattr(self, "input", None)
1675 if input is not None:
1675 if input is not None:
1676 for part in xrepr(input, mode):
1676 for part in xrepr(input, mode):
1677 yield part
1677 yield part
1678 yield (astyle.style_default, " | ")
1678 yield (astyle.style_default, " | ")
1679 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1679 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1680 for (i, (name, value)) in enumerate(self.csvargs.iteritems()):
1680 for (i, (name, value)) in enumerate(self.csvargs.iteritems()):
1681 if i:
1681 if i:
1682 yield (astyle.style_default, ", ")
1682 yield (astyle.style_default, ", ")
1683 yield (astyle.style_default, name)
1683 yield (astyle.style_default, name)
1684 yield (astyle.style_default, "=")
1684 yield (astyle.style_default, "=")
1685 for part in xrepr(value, "default"):
1685 for part in xrepr(value, "default"):
1686 yield part
1686 yield part
1687 yield (astyle.style_default, ")")
1687 yield (astyle.style_default, ")")
1688 else:
1688 else:
1689 yield (astyle.style_default, repr(self))
1689 yield (astyle.style_default, repr(self))
1690
1690
1691 def __repr__(self):
1691 def __repr__(self):
1692 args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()])
1692 args = ", ".join(["%s=%r" % item for item in self.csvargs.iteritems()])
1693 return "<%s.%s %s at 0x%x>" % \
1693 return "<%s.%s %s at 0x%x>" % \
1694 (self.__class__.__module__, self.__class__.__name__, args, id(self))
1694 (self.__class__.__module__, self.__class__.__name__, args, id(self))
1695
1695
1696
1696
1697 class ix(Table):
1697 class ix(Table):
1698 """
1698 """
1699 Execute a system command and list its output as lines
1699 Execute a system command and list its output as lines
1700 (similar to ``os.popen()``).
1700 (similar to ``os.popen()``).
1701
1701
1702 Examples::
1702 Examples::
1703
1703
1704 >>> ix("ps x")
1704 >>> ix("ps x")
1705 IPython.extensions.ipipe.ix('ps x')
1705 IPython.extensions.ipipe.ix('ps x')
1706
1706
1707 >>> ix("find .") | ifile
1707 >>> ix("find .") | ifile
1708 <IPython.extensions.ipipe.ieval expr=<class 'IPython.extensions.ipipe.ifile'> at 0x8509d2c>
1708 <IPython.extensions.ipipe.ieval expr=<class 'IPython.extensions.ipipe.ifile'> at 0x8509d2c>
1709 # random
1709 # random
1710 """
1710 """
1711 def __init__(self, cmd):
1711 def __init__(self, cmd):
1712 self.cmd = cmd
1712 self.cmd = cmd
1713 self._pipeout = None
1713 self._pipeout = None
1714
1714
1715 def __iter__(self):
1715 def __iter__(self):
1716 (_pipein, self._pipeout) = os.popen4(self.cmd)
1716 (_pipein, self._pipeout) = os.popen4(self.cmd)
1717 _pipein.close()
1717 _pipein.close()
1718 for l in self._pipeout:
1718 for l in self._pipeout:
1719 yield l.rstrip("\r\n")
1719 yield l.rstrip("\r\n")
1720 self._pipeout.close()
1720 self._pipeout.close()
1721 self._pipeout = None
1721 self._pipeout = None
1722
1722
1723 def __del__(self):
1723 def __del__(self):
1724 if self._pipeout is not None and not self._pipeout.closed:
1724 if self._pipeout is not None and not self._pipeout.closed:
1725 self._pipeout.close()
1725 self._pipeout.close()
1726 self._pipeout = None
1726 self._pipeout = None
1727
1727
1728 def __xrepr__(self, mode="default"):
1728 def __xrepr__(self, mode="default"):
1729 if mode == "header" or mode == "footer":
1729 if mode == "header" or mode == "footer":
1730 yield (astyle.style_default,
1730 yield (astyle.style_default,
1731 "%s(%r)" % (self.__class__.__name__, self.cmd))
1731 "%s(%r)" % (self.__class__.__name__, self.cmd))
1732 else:
1732 else:
1733 yield (astyle.style_default, repr(self))
1733 yield (astyle.style_default, repr(self))
1734
1734
1735 def __repr__(self):
1735 def __repr__(self):
1736 return "%s.%s(%r)" % \
1736 return "%s.%s(%r)" % \
1737 (self.__class__.__module__, self.__class__.__name__, self.cmd)
1737 (self.__class__.__module__, self.__class__.__name__, self.cmd)
1738
1738
1739
1739
1740 class ifilter(Pipe):
1740 class ifilter(Pipe):
1741 """
1741 """
1742 Filter an input pipe. Only objects where an expression evaluates to true
1742 Filter an input pipe. Only objects where an expression evaluates to true
1743 (and doesn't raise an exception) are listed.
1743 (and doesn't raise an exception) are listed.
1744
1744
1745 Examples::
1745 Examples::
1746
1746
1747 >>> ils | ifilter("_.isfile() and size>1000")
1747 >>> ils | ifilter("_.isfile() and size>1000")
1748 >>> igrp | ifilter("len(mem)")
1748 >>> igrp | ifilter("len(mem)")
1749 >>> sys.modules | ifilter(lambda _:_.value is not None)
1749 >>> sys.modules | ifilter(lambda _:_.value is not None)
1750 # all-random
1750 # all-random
1751 """
1751 """
1752
1752
1753 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1753 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1754 """
1754 """
1755 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1755 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1756 containing an expression. ``globals`` will be used as the global
1756 containing an expression. ``globals`` will be used as the global
1757 namespace for calling string expressions (defaulting to IPython's
1757 namespace for calling string expressions (defaulting to IPython's
1758 user namespace). ``errors`` specifies how exception during evaluation
1758 user namespace). ``errors`` specifies how exception during evaluation
1759 of ``expr`` are handled:
1759 of ``expr`` are handled:
1760
1760
1761 ``"drop"``
1761 ``"drop"``
1762 drop all items that have errors;
1762 drop all items that have errors;
1763
1763
1764 ``"keep"``
1764 ``"keep"``
1765 keep all items that have errors;
1765 keep all items that have errors;
1766
1766
1767 ``"keeperror"``
1767 ``"keeperror"``
1768 keep the exception of all items that have errors;
1768 keep the exception of all items that have errors;
1769
1769
1770 ``"raise"``
1770 ``"raise"``
1771 raise the exception;
1771 raise the exception;
1772
1772
1773 ``"raiseifallfail"``
1773 ``"raiseifallfail"``
1774 raise the first exception if all items have errors; otherwise drop
1774 raise the first exception if all items have errors; otherwise drop
1775 those with errors (this is the default).
1775 those with errors (this is the default).
1776 """
1776 """
1777 self.expr = expr
1777 self.expr = expr
1778 self.globals = globals
1778 self.globals = globals
1779 self.errors = errors
1779 self.errors = errors
1780
1780
1781 def __iter__(self):
1781 def __iter__(self):
1782 if callable(self.expr):
1782 if callable(self.expr):
1783 test = self.expr
1783 test = self.expr
1784 else:
1784 else:
1785 g = getglobals(self.globals)
1785 g = getglobals(self.globals)
1786 expr = compile(self.expr, "ipipe-expression", "eval")
1786 expr = compile(self.expr, "ipipe-expression", "eval")
1787 def test(item):
1787 def test(item):
1788 return eval(expr, g, AttrNamespace(item))
1788 return eval(expr, g, AttrNamespace(item))
1789
1789
1790 ok = 0
1790 ok = 0
1791 exc_info = None
1791 exc_info = None
1792 for item in xiter(self.input):
1792 for item in xiter(self.input):
1793 try:
1793 try:
1794 if test(item):
1794 if test(item):
1795 yield item
1795 yield item
1796 ok += 1
1796 ok += 1
1797 except (KeyboardInterrupt, SystemExit):
1797 except (KeyboardInterrupt, SystemExit):
1798 raise
1798 raise
1799 except Exception, exc:
1799 except Exception, exc:
1800 if self.errors == "drop":
1800 if self.errors == "drop":
1801 pass # Ignore errors
1801 pass # Ignore errors
1802 elif self.errors == "keep":
1802 elif self.errors == "keep":
1803 yield item
1803 yield item
1804 elif self.errors == "keeperror":
1804 elif self.errors == "keeperror":
1805 yield exc
1805 yield exc
1806 elif self.errors == "raise":
1806 elif self.errors == "raise":
1807 raise
1807 raise
1808 elif self.errors == "raiseifallfail":
1808 elif self.errors == "raiseifallfail":
1809 if exc_info is None:
1809 if exc_info is None:
1810 exc_info = sys.exc_info()
1810 exc_info = sys.exc_info()
1811 if not ok and exc_info is not None:
1811 if not ok and exc_info is not None:
1812 raise exc_info[0], exc_info[1], exc_info[2]
1812 raise exc_info[0], exc_info[1], exc_info[2]
1813
1813
1814 def __xrepr__(self, mode="default"):
1814 def __xrepr__(self, mode="default"):
1815 if mode == "header" or mode == "footer":
1815 if mode == "header" or mode == "footer":
1816 input = getattr(self, "input", None)
1816 input = getattr(self, "input", None)
1817 if input is not None:
1817 if input is not None:
1818 for part in xrepr(input, mode):
1818 for part in xrepr(input, mode):
1819 yield part
1819 yield part
1820 yield (astyle.style_default, " | ")
1820 yield (astyle.style_default, " | ")
1821 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1821 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1822 for part in xrepr(self.expr, "default"):
1822 for part in xrepr(self.expr, "default"):
1823 yield part
1823 yield part
1824 yield (astyle.style_default, ")")
1824 yield (astyle.style_default, ")")
1825 else:
1825 else:
1826 yield (astyle.style_default, repr(self))
1826 yield (astyle.style_default, repr(self))
1827
1827
1828 def __repr__(self):
1828 def __repr__(self):
1829 return "<%s.%s expr=%r at 0x%x>" % \
1829 return "<%s.%s expr=%r at 0x%x>" % \
1830 (self.__class__.__module__, self.__class__.__name__,
1830 (self.__class__.__module__, self.__class__.__name__,
1831 self.expr, id(self))
1831 self.expr, id(self))
1832
1832
1833
1833
1834 class ieval(Pipe):
1834 class ieval(Pipe):
1835 """
1835 """
1836 Evaluate an expression for each object in the input pipe.
1836 Evaluate an expression for each object in the input pipe.
1837
1837
1838 Examples::
1838 Examples::
1839
1839
1840 >>> ils | ieval("_.abspath()")
1840 >>> ils | ieval("_.abspath()")
1841 # random
1841 # random
1842 >>> sys.path | ieval(ifile)
1842 >>> sys.path | ieval(ifile)
1843 # random
1843 # random
1844 """
1844 """
1845
1845
1846 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1846 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1847 """
1847 """
1848 Create an ``ieval`` object. ``expr`` can be a callable or a string
1848 Create an ``ieval`` object. ``expr`` can be a callable or a string
1849 containing an expression. For the meaning of ``globals`` and
1849 containing an expression. For the meaning of ``globals`` and
1850 ``errors`` see ``ifilter``.
1850 ``errors`` see ``ifilter``.
1851 """
1851 """
1852 self.expr = expr
1852 self.expr = expr
1853 self.globals = globals
1853 self.globals = globals
1854 self.errors = errors
1854 self.errors = errors
1855
1855
1856 def __iter__(self):
1856 def __iter__(self):
1857 if callable(self.expr):
1857 if callable(self.expr):
1858 do = self.expr
1858 do = self.expr
1859 else:
1859 else:
1860 g = getglobals(self.globals)
1860 g = getglobals(self.globals)
1861 expr = compile(self.expr, "ipipe-expression", "eval")
1861 expr = compile(self.expr, "ipipe-expression", "eval")
1862 def do(item):
1862 def do(item):
1863 return eval(expr, g, AttrNamespace(item))
1863 return eval(expr, g, AttrNamespace(item))
1864
1864
1865 ok = 0
1865 ok = 0
1866 exc_info = None
1866 exc_info = None
1867 for item in xiter(self.input):
1867 for item in xiter(self.input):
1868 try:
1868 try:
1869 yield do(item)
1869 yield do(item)
1870 except (KeyboardInterrupt, SystemExit):
1870 except (KeyboardInterrupt, SystemExit):
1871 raise
1871 raise
1872 except Exception, exc:
1872 except Exception, exc:
1873 if self.errors == "drop":
1873 if self.errors == "drop":
1874 pass # Ignore errors
1874 pass # Ignore errors
1875 elif self.errors == "keep":
1875 elif self.errors == "keep":
1876 yield item
1876 yield item
1877 elif self.errors == "keeperror":
1877 elif self.errors == "keeperror":
1878 yield exc
1878 yield exc
1879 elif self.errors == "raise":
1879 elif self.errors == "raise":
1880 raise
1880 raise
1881 elif self.errors == "raiseifallfail":
1881 elif self.errors == "raiseifallfail":
1882 if exc_info is None:
1882 if exc_info is None:
1883 exc_info = sys.exc_info()
1883 exc_info = sys.exc_info()
1884 if not ok and exc_info is not None:
1884 if not ok and exc_info is not None:
1885 raise exc_info[0], exc_info[1], exc_info[2]
1885 raise exc_info[0], exc_info[1], exc_info[2]
1886
1886
1887 def __xrepr__(self, mode="default"):
1887 def __xrepr__(self, mode="default"):
1888 if mode == "header" or mode == "footer":
1888 if mode == "header" or mode == "footer":
1889 input = getattr(self, "input", None)
1889 input = getattr(self, "input", None)
1890 if input is not None:
1890 if input is not None:
1891 for part in xrepr(input, mode):
1891 for part in xrepr(input, mode):
1892 yield part
1892 yield part
1893 yield (astyle.style_default, " | ")
1893 yield (astyle.style_default, " | ")
1894 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1894 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1895 for part in xrepr(self.expr, "default"):
1895 for part in xrepr(self.expr, "default"):
1896 yield part
1896 yield part
1897 yield (astyle.style_default, ")")
1897 yield (astyle.style_default, ")")
1898 else:
1898 else:
1899 yield (astyle.style_default, repr(self))
1899 yield (astyle.style_default, repr(self))
1900
1900
1901 def __repr__(self):
1901 def __repr__(self):
1902 return "<%s.%s expr=%r at 0x%x>" % \
1902 return "<%s.%s expr=%r at 0x%x>" % \
1903 (self.__class__.__module__, self.__class__.__name__,
1903 (self.__class__.__module__, self.__class__.__name__,
1904 self.expr, id(self))
1904 self.expr, id(self))
1905
1905
1906
1906
1907 class ienum(Pipe):
1907 class ienum(Pipe):
1908 """
1908 """
1909 Enumerate the input pipe (i.e. wrap each input object in an object
1909 Enumerate the input pipe (i.e. wrap each input object in an object
1910 with ``index`` and ``object`` attributes).
1910 with ``index`` and ``object`` attributes).
1911
1911
1912 Examples::
1912 Examples::
1913
1913
1914 >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object")
1914 >>> xrange(20) | ieval("_,_*_") | ienum | ifilter("index % 2 == 0") | ieval("object")
1915 """
1915 """
1916 skip_doctest = True
1916 skip_doctest = True
1917
1917
1918 def __iter__(self):
1918 def __iter__(self):
1919 fields = ("index", "object")
1919 fields = ("index", "object")
1920 for (index, object) in enumerate(xiter(self.input)):
1920 for (index, object) in enumerate(xiter(self.input)):
1921 yield Fields(fields, index=index, object=object)
1921 yield Fields(fields, index=index, object=object)
1922
1922
1923
1923
1924 class isort(Pipe):
1924 class isort(Pipe):
1925 """
1925 """
1926 Sorts the input pipe.
1926 Sorts the input pipe.
1927
1927
1928 Examples::
1928 Examples::
1929
1929
1930 >>> ils | isort("size")
1930 >>> ils | isort("size")
1931 <IPython.extensions.ipipe.isort key='size' reverse=False at 0x849ec2c>
1931 <IPython.extensions.ipipe.isort key='size' reverse=False at 0x849ec2c>
1932 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1932 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1933 <IPython.extensions.ipipe.isort key='_.isdir(), _.lower()' reverse=True at 0x849eacc>
1933 <IPython.extensions.ipipe.isort key='_.isdir(), _.lower()' reverse=True at 0x849eacc>
1934 # all-random
1934 # all-random
1935 """
1935 """
1936
1936
1937 def __init__(self, key=None, globals=None, reverse=False):
1937 def __init__(self, key=None, globals=None, reverse=False):
1938 """
1938 """
1939 Create an ``isort`` object. ``key`` can be a callable or a string
1939 Create an ``isort`` object. ``key`` can be a callable or a string
1940 containing an expression (or ``None`` in which case the items
1940 containing an expression (or ``None`` in which case the items
1941 themselves will be sorted). If ``reverse`` is true the sort order
1941 themselves will be sorted). If ``reverse`` is true the sort order
1942 will be reversed. For the meaning of ``globals`` see ``ifilter``.
1942 will be reversed. For the meaning of ``globals`` see ``ifilter``.
1943 """
1943 """
1944 self.key = key
1944 self.key = key
1945 self.globals = globals
1945 self.globals = globals
1946 self.reverse = reverse
1946 self.reverse = reverse
1947
1947
1948 def __iter__(self):
1948 def __iter__(self):
1949 if self.key is None:
1949 if self.key is None:
1950 items = sorted(xiter(self.input), reverse=self.reverse)
1950 items = sorted(xiter(self.input), reverse=self.reverse)
1951 elif callable(self.key):
1951 elif callable(self.key):
1952 items = sorted(xiter(self.input), key=self.key, reverse=self.reverse)
1952 items = sorted(xiter(self.input), key=self.key, reverse=self.reverse)
1953 else:
1953 else:
1954 g = getglobals(self.globals)
1954 g = getglobals(self.globals)
1955 key = compile(self.key, "ipipe-expression", "eval")
1955 key = compile(self.key, "ipipe-expression", "eval")
1956 def realkey(item):
1956 def realkey(item):
1957 return eval(key, g, AttrNamespace(item))
1957 return eval(key, g, AttrNamespace(item))
1958 items = sorted(xiter(self.input), key=realkey, reverse=self.reverse)
1958 items = sorted(xiter(self.input), key=realkey, reverse=self.reverse)
1959 for item in items:
1959 for item in items:
1960 yield item
1960 yield item
1961
1961
1962 def __xrepr__(self, mode="default"):
1962 def __xrepr__(self, mode="default"):
1963 if mode == "header" or mode == "footer":
1963 if mode == "header" or mode == "footer":
1964 input = getattr(self, "input", None)
1964 input = getattr(self, "input", None)
1965 if input is not None:
1965 if input is not None:
1966 for part in xrepr(input, mode):
1966 for part in xrepr(input, mode):
1967 yield part
1967 yield part
1968 yield (astyle.style_default, " | ")
1968 yield (astyle.style_default, " | ")
1969 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1969 yield (astyle.style_default, "%s(" % self.__class__.__name__)
1970 for part in xrepr(self.key, "default"):
1970 for part in xrepr(self.key, "default"):
1971 yield part
1971 yield part
1972 if self.reverse:
1972 if self.reverse:
1973 yield (astyle.style_default, ", ")
1973 yield (astyle.style_default, ", ")
1974 for part in xrepr(True, "default"):
1974 for part in xrepr(True, "default"):
1975 yield part
1975 yield part
1976 yield (astyle.style_default, ")")
1976 yield (astyle.style_default, ")")
1977 else:
1977 else:
1978 yield (astyle.style_default, repr(self))
1978 yield (astyle.style_default, repr(self))
1979
1979
1980 def __repr__(self):
1980 def __repr__(self):
1981 return "<%s.%s key=%r reverse=%r at 0x%x>" % \
1981 return "<%s.%s key=%r reverse=%r at 0x%x>" % \
1982 (self.__class__.__module__, self.__class__.__name__,
1982 (self.__class__.__module__, self.__class__.__name__,
1983 self.key, self.reverse, id(self))
1983 self.key, self.reverse, id(self))
1984
1984
1985
1985
1986 tab = 3 # for expandtabs()
1986 tab = 3 # for expandtabs()
1987
1987
1988 def _format(field):
1988 def _format(field):
1989 if isinstance(field, str):
1989 if isinstance(field, str):
1990 text = repr(field.expandtabs(tab))[1:-1]
1990 text = repr(field.expandtabs(tab))[1:-1]
1991 elif isinstance(field, unicode):
1991 elif isinstance(field, unicode):
1992 text = repr(field.expandtabs(tab))[2:-1]
1992 text = repr(field.expandtabs(tab))[2:-1]
1993 elif isinstance(field, datetime.datetime):
1993 elif isinstance(field, datetime.datetime):
1994 # Don't use strftime() here, as this requires year >= 1900
1994 # Don't use strftime() here, as this requires year >= 1900
1995 text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
1995 text = "%04d-%02d-%02d %02d:%02d:%02d.%06d" % \
1996 (field.year, field.month, field.day,
1996 (field.year, field.month, field.day,
1997 field.hour, field.minute, field.second, field.microsecond)
1997 field.hour, field.minute, field.second, field.microsecond)
1998 elif isinstance(field, datetime.date):
1998 elif isinstance(field, datetime.date):
1999 text = "%04d-%02d-%02d" % (field.year, field.month, field.day)
1999 text = "%04d-%02d-%02d" % (field.year, field.month, field.day)
2000 else:
2000 else:
2001 text = repr(field)
2001 text = repr(field)
2002 return text
2002 return text
2003
2003
2004
2004
2005 class Display(object):
2005 class Display(object):
2006 class __metaclass__(type):
2006 class __metaclass__(type):
2007 def __ror__(self, input):
2007 def __ror__(self, input):
2008 return input | self()
2008 return input | self()
2009
2009
2010 def __init__(self, input=None):
2010 def __init__(self, input=None):
2011 self.input = input
2011 self.input = input
2012
2012
2013 def __ror__(self, input):
2013 def __ror__(self, input):
2014 self.input = input
2014 self.input = input
2015 return self
2015 return self
2016
2016
2017 def display(self):
2017 def display(self):
2018 pass
2018 pass
2019
2019
2020
2020
2021 class iless(Display):
2021 class iless(Display):
2022 cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS"
2022 cmd = "less --quit-if-one-screen --LONG-PROMPT --LINE-NUMBERS --chop-long-lines --shift=8 --RAW-CONTROL-CHARS"
2023
2023
2024 def display(self):
2024 def display(self):
2025 try:
2025 try:
2026 pager = os.popen(self.cmd, "w")
2026 pager = os.popen(self.cmd, "w")
2027 try:
2027 try:
2028 for item in xiter(self.input):
2028 for item in xiter(self.input):
2029 first = False
2029 first = False
2030 for attr in xattrs(item, "default"):
2030 for attr in xattrs(item, "default"):
2031 if first:
2031 if first:
2032 first = False
2032 first = False
2033 else:
2033 else:
2034 pager.write(" ")
2034 pager.write(" ")
2035 attr = upgradexattr(attr)
2035 attr = upgradexattr(attr)
2036 if not isinstance(attr, SelfDescriptor):
2036 if not isinstance(attr, SelfDescriptor):
2037 pager.write(attr.name())
2037 pager.write(attr.name())
2038 pager.write("=")
2038 pager.write("=")
2039 pager.write(str(attr.value(item)))
2039 pager.write(str(attr.value(item)))
2040 pager.write("\n")
2040 pager.write("\n")
2041 finally:
2041 finally:
2042 pager.close()
2042 pager.close()
2043 except Exception, exc:
2043 except Exception, exc:
2044 print "%s: %s" % (exc.__class__.__name__, str(exc))
2044 print "%s: %s" % (exc.__class__.__name__, str(exc))
2045
2045
2046
2046
2047 class _RedirectIO(object):
2047 class _RedirectIO(object):
2048 def __init__(self,*args,**kwargs):
2048 def __init__(self,*args,**kwargs):
2049 """
2049 """
2050 Map the system output streams to self.
2050 Map the system output streams to self.
2051 """
2051 """
2052 self.stream = StringIO.StringIO()
2052 self.stream = StringIO.StringIO()
2053 self.stdout = sys.stdout
2053 self.stdout = sys.stdout
2054 sys.stdout = self
2054 sys.stdout = self
2055 self.stderr = sys.stderr
2055 self.stderr = sys.stderr
2056 sys.stderr = self
2056 sys.stderr = self
2057
2057
2058 def write(self, text):
2058 def write(self, text):
2059 """
2059 """
2060 Write both to screen and to self.
2060 Write both to screen and to self.
2061 """
2061 """
2062 self.stream.write(text)
2062 self.stream.write(text)
2063 self.stdout.write(text)
2063 self.stdout.write(text)
2064 if "\n" in text:
2064 if "\n" in text:
2065 self.stdout.flush()
2065 self.stdout.flush()
2066
2066
2067 def writelines(self, lines):
2067 def writelines(self, lines):
2068 """
2068 """
2069 Write lines both to screen and to self.
2069 Write lines both to screen and to self.
2070 """
2070 """
2071 self.stream.writelines(lines)
2071 self.stream.writelines(lines)
2072 self.stdout.writelines(lines)
2072 self.stdout.writelines(lines)
2073 self.stdout.flush()
2073 self.stdout.flush()
2074
2074
2075 def restore(self):
2075 def restore(self):
2076 """
2076 """
2077 Restore the default system streams.
2077 Restore the default system streams.
2078 """
2078 """
2079 self.stdout.flush()
2079 self.stdout.flush()
2080 self.stderr.flush()
2080 self.stderr.flush()
2081 sys.stdout = self.stdout
2081 sys.stdout = self.stdout
2082 sys.stderr = self.stderr
2082 sys.stderr = self.stderr
2083
2083
2084
2084
2085 class icap(Table):
2085 class icap(Table):
2086 """
2086 """
2087 Execute a python string and capture any output to stderr/stdout.
2087 Execute a python string and capture any output to stderr/stdout.
2088
2088
2089 Examples::
2089 Examples::
2090
2090
2091 >>> import time
2091 >>> import time
2092 >>> icap("for i in range(10): print i, time.sleep(0.1)")
2092 >>> icap("for i in range(10): print i, time.sleep(0.1)")
2093
2093
2094 """
2094 """
2095 skip_doctest = True
2095 skip_doctest = True
2096
2096
2097 def __init__(self, expr, globals=None):
2097 def __init__(self, expr, globals=None):
2098 self.expr = expr
2098 self.expr = expr
2099 self.globals = globals
2099 self.globals = globals
2100 log = _RedirectIO()
2100 log = _RedirectIO()
2101 try:
2101 try:
2102 exec(expr, getglobals(globals))
2102 exec(expr, getglobals(globals))
2103 finally:
2103 finally:
2104 log.restore()
2104 log.restore()
2105 self.stream = log.stream
2105 self.stream = log.stream
2106
2106
2107 def __iter__(self):
2107 def __iter__(self):
2108 self.stream.seek(0)
2108 self.stream.seek(0)
2109 for line in self.stream:
2109 for line in self.stream:
2110 yield line.rstrip("\r\n")
2110 yield line.rstrip("\r\n")
2111
2111
2112 def __xrepr__(self, mode="default"):
2112 def __xrepr__(self, mode="default"):
2113 if mode == "header" or mode == "footer":
2113 if mode == "header" or mode == "footer":
2114 yield (astyle.style_default,
2114 yield (astyle.style_default,
2115 "%s(%r)" % (self.__class__.__name__, self.expr))
2115 "%s(%r)" % (self.__class__.__name__, self.expr))
2116 else:
2116 else:
2117 yield (astyle.style_default, repr(self))
2117 yield (astyle.style_default, repr(self))
2118
2118
2119 def __repr__(self):
2119 def __repr__(self):
2120 return "%s.%s(%r)" % \
2120 return "%s.%s(%r)" % \
2121 (self.__class__.__module__, self.__class__.__name__, self.expr)
2121 (self.__class__.__module__, self.__class__.__name__, self.expr)
2122
2122
2123
2123
2124 def xformat(value, mode, maxlength):
2124 def xformat(value, mode, maxlength):
2125 align = None
2125 align = None
2126 full = True
2126 full = True
2127 width = 0
2127 width = 0
2128 text = astyle.Text()
2128 text = astyle.Text()
2129 for (style, part) in xrepr(value, mode):
2129 for (style, part) in xrepr(value, mode):
2130 # only consider the first result
2130 # only consider the first result
2131 if align is None:
2131 if align is None:
2132 if isinstance(style, int):
2132 if isinstance(style, int):
2133 # (style, text) really is (alignment, stop)
2133 # (style, text) really is (alignment, stop)
2134 align = style
2134 align = style
2135 full = part
2135 full = part
2136 continue
2136 continue
2137 else:
2137 else:
2138 align = -1
2138 align = -1
2139 full = True
2139 full = True
2140 if not isinstance(style, int):
2140 if not isinstance(style, int):
2141 text.append((style, part))
2141 text.append((style, part))
2142 width += len(part)
2142 width += len(part)
2143 if width >= maxlength and not full:
2143 if width >= maxlength and not full:
2144 text.append((astyle.style_ellisis, "..."))
2144 text.append((astyle.style_ellisis, "..."))
2145 width += 3
2145 width += 3
2146 break
2146 break
2147 if align is None: # default to left alignment
2147 if align is None: # default to left alignment
2148 align = -1
2148 align = -1
2149 return (align, width, text)
2149 return (align, width, text)
2150
2150
2151
2151
2152
2152
2153 import astyle
2153 import astyle
2154
2154
2155 class idump(Display):
2155 class idump(Display):
2156 # The approximate maximum length of a column entry
2156 # The approximate maximum length of a column entry
2157 maxattrlength = 200
2157 maxattrlength = 200
2158
2158
2159 # Style for column names
2159 # Style for column names
2160 style_header = astyle.Style.fromstr("white:black:bold")
2160 style_header = astyle.Style.fromstr("white:black:bold")
2161
2161
2162 def __init__(self, input=None, *attrs):
2162 def __init__(self, input=None, *attrs):
2163 Display.__init__(self, input)
2163 Display.__init__(self, input)
2164 self.attrs = [upgradexattr(attr) for attr in attrs]
2164 self.attrs = [upgradexattr(attr) for attr in attrs]
2165 self.headerpadchar = " "
2165 self.headerpadchar = " "
2166 self.headersepchar = "|"
2166 self.headersepchar = "|"
2167 self.datapadchar = " "
2167 self.datapadchar = " "
2168 self.datasepchar = "|"
2168 self.datasepchar = "|"
2169
2169
2170 def display(self):
2170 def display(self):
2171 stream = genutils.Term.cout
2171 stream = genutils.Term.cout
2172 allattrs = []
2172 allattrs = []
2173 attrset = set()
2173 attrset = set()
2174 colwidths = {}
2174 colwidths = {}
2175 rows = []
2175 rows = []
2176 for item in xiter(self.input):
2176 for item in xiter(self.input):
2177 row = {}
2177 row = {}
2178 attrs = self.attrs
2178 attrs = self.attrs
2179 if not attrs:
2179 if not attrs:
2180 attrs = xattrs(item, "default")
2180 attrs = xattrs(item, "default")
2181 for attr in attrs:
2181 for attr in attrs:
2182 if attr not in attrset:
2182 if attr not in attrset:
2183 allattrs.append(attr)
2183 allattrs.append(attr)
2184 attrset.add(attr)
2184 attrset.add(attr)
2185 colwidths[attr] = len(attr.name())
2185 colwidths[attr] = len(attr.name())
2186 try:
2186 try:
2187 value = attr.value(item)
2187 value = attr.value(item)
2188 except (KeyboardInterrupt, SystemExit):
2188 except (KeyboardInterrupt, SystemExit):
2189 raise
2189 raise
2190 except Exception, exc:
2190 except Exception, exc:
2191 value = exc
2191 value = exc
2192 (align, width, text) = xformat(value, "cell", self.maxattrlength)
2192 (align, width, text) = xformat(value, "cell", self.maxattrlength)
2193 colwidths[attr] = max(colwidths[attr], width)
2193 colwidths[attr] = max(colwidths[attr], width)
2194 # remember alignment, length and colored parts
2194 # remember alignment, length and colored parts
2195 row[attr] = (align, width, text)
2195 row[attr] = (align, width, text)
2196 rows.append(row)
2196 rows.append(row)
2197
2197
2198 stream.write("\n")
2198 stream.write("\n")
2199 for (i, attr) in enumerate(allattrs):
2199 for (i, attr) in enumerate(allattrs):
2200 attrname = attr.name()
2200 attrname = attr.name()
2201 self.style_header(attrname).write(stream)
2201 self.style_header(attrname).write(stream)
2202 spc = colwidths[attr] - len(attrname)
2202 spc = colwidths[attr] - len(attrname)
2203 if i < len(colwidths)-1:
2203 if i < len(colwidths)-1:
2204 stream.write(self.headerpadchar*spc)
2204 stream.write(self.headerpadchar*spc)
2205 stream.write(self.headersepchar)
2205 stream.write(self.headersepchar)
2206 stream.write("\n")
2206 stream.write("\n")
2207
2207
2208 for row in rows:
2208 for row in rows:
2209 for (i, attr) in enumerate(allattrs):
2209 for (i, attr) in enumerate(allattrs):
2210 (align, width, text) = row[attr]
2210 (align, width, text) = row[attr]
2211 spc = colwidths[attr] - width
2211 spc = colwidths[attr] - width
2212 if align == -1:
2212 if align == -1:
2213 text.write(stream)
2213 text.write(stream)
2214 if i < len(colwidths)-1:
2214 if i < len(colwidths)-1:
2215 stream.write(self.datapadchar*spc)
2215 stream.write(self.datapadchar*spc)
2216 elif align == 0:
2216 elif align == 0:
2217 spc = colwidths[attr] - width
2217 spc = colwidths[attr] - width
2218 spc1 = spc//2
2218 spc1 = spc//2
2219 spc2 = spc-spc1
2219 spc2 = spc-spc1
2220 stream.write(self.datapadchar*spc1)
2220 stream.write(self.datapadchar*spc1)
2221 text.write(stream)
2221 text.write(stream)
2222 if i < len(colwidths)-1:
2222 if i < len(colwidths)-1:
2223 stream.write(self.datapadchar*spc2)
2223 stream.write(self.datapadchar*spc2)
2224 else:
2224 else:
2225 stream.write(self.datapadchar*spc)
2225 stream.write(self.datapadchar*spc)
2226 text.write(stream)
2226 text.write(stream)
2227 if i < len(colwidths)-1:
2227 if i < len(colwidths)-1:
2228 stream.write(self.datasepchar)
2228 stream.write(self.datasepchar)
2229 stream.write("\n")
2229 stream.write("\n")
2230
2230
2231
2231
2232 class AttributeDetail(Table):
2232 class AttributeDetail(Table):
2233 """
2233 """
2234 ``AttributeDetail`` objects are use for displaying a detailed list of object
2234 ``AttributeDetail`` objects are use for displaying a detailed list of object
2235 attributes.
2235 attributes.
2236 """
2236 """
2237 def __init__(self, object, descriptor):
2237 def __init__(self, object, descriptor):
2238 self.object = object
2238 self.object = object
2239 self.descriptor = descriptor
2239 self.descriptor = descriptor
2240
2240
2241 def __iter__(self):
2241 def __iter__(self):
2242 return self.descriptor.iter(self.object)
2242 return self.descriptor.iter(self.object)
2243
2243
2244 def name(self):
2244 def name(self):
2245 return self.descriptor.name()
2245 return self.descriptor.name()
2246
2246
2247 def attrtype(self):
2247 def attrtype(self):
2248 return self.descriptor.attrtype(self.object)
2248 return self.descriptor.attrtype(self.object)
2249
2249
2250 def valuetype(self):
2250 def valuetype(self):
2251 return self.descriptor.valuetype(self.object)
2251 return self.descriptor.valuetype(self.object)
2252
2252
2253 def doc(self):
2253 def doc(self):
2254 return self.descriptor.doc(self.object)
2254 return self.descriptor.doc(self.object)
2255
2255
2256 def shortdoc(self):
2256 def shortdoc(self):
2257 return self.descriptor.shortdoc(self.object)
2257 return self.descriptor.shortdoc(self.object)
2258
2258
2259 def value(self):
2259 def value(self):
2260 return self.descriptor.value(self.object)
2260 return self.descriptor.value(self.object)
2261
2261
2262 def __xattrs__(self, mode="default"):
2262 def __xattrs__(self, mode="default"):
2263 attrs = ("name()", "attrtype()", "valuetype()", "value()", "shortdoc()")
2263 attrs = ("name()", "attrtype()", "valuetype()", "value()", "shortdoc()")
2264 if mode == "detail":
2264 if mode == "detail":
2265 attrs += ("doc()",)
2265 attrs += ("doc()",)
2266 return attrs
2266 return attrs
2267
2267
2268 def __xrepr__(self, mode="default"):
2268 def __xrepr__(self, mode="default"):
2269 yield (-1, True)
2269 yield (-1, True)
2270 valuetype = self.valuetype()
2270 valuetype = self.valuetype()
2271 if valuetype is not noitem:
2271 if valuetype is not noitem:
2272 for part in xrepr(valuetype):
2272 for part in xrepr(valuetype):
2273 yield part
2273 yield part
2274 yield (astyle.style_default, " ")
2274 yield (astyle.style_default, " ")
2275 yield (astyle.style_default, self.attrtype())
2275 yield (astyle.style_default, self.attrtype())
2276 yield (astyle.style_default, " ")
2276 yield (astyle.style_default, " ")
2277 yield (astyle.style_default, self.name())
2277 yield (astyle.style_default, self.name())
2278 yield (astyle.style_default, " of ")
2278 yield (astyle.style_default, " of ")
2279 for part in xrepr(self.object):
2279 for part in xrepr(self.object):
2280 yield part
2280 yield part
2281
2281
2282
2282
2283 try:
2283 try:
2284 from ibrowse import ibrowse
2284 from ibrowse import ibrowse
2285 except ImportError:
2285 except ImportError:
2286 # No curses (probably Windows) => try igrid
2286 # No curses (probably Windows) => try igrid
2287 try:
2287 try:
2288 from igrid import igrid
2288 from igrid import igrid
2289 except ImportError:
2289 except ImportError:
2290 # no wx either => use ``idump`` as the default display.
2290 # no wx either => use ``idump`` as the default display.
2291 defaultdisplay = idump
2291 defaultdisplay = idump
2292 else:
2292 else:
2293 defaultdisplay = igrid
2293 defaultdisplay = igrid
2294 __all__.append("igrid")
2294 __all__.append("igrid")
2295 else:
2295 else:
2296 defaultdisplay = ibrowse
2296 defaultdisplay = ibrowse
2297 __all__.append("ibrowse")
2297 __all__.append("ibrowse")
2298
2298
2299
2299
2300 # If we're running under IPython, register our objects with IPython's
2300 # If we're running under IPython, register our objects with IPython's
2301 # generic function ``result_display``, else install a displayhook
2301 # generic function ``result_display``, else install a displayhook
2302 # directly as sys.displayhook
2302 # directly as sys.displayhook
2303 if generics is not None:
2303 if generics is not None:
2304 def display_display(obj):
2304 def display_display(obj):
2305 return obj.display()
2305 return obj.display()
2306 generics.result_display.when_type(Display)(display_display)
2306 generics.result_display.when_type(Display)(display_display)
2307
2307
2308 def display_tableobject(obj):
2308 def display_tableobject(obj):
2309 return display_display(defaultdisplay(obj))
2309 return display_display(defaultdisplay(obj))
2310 generics.result_display.when_type(Table)(display_tableobject)
2310 generics.result_display.when_type(Table)(display_tableobject)
2311
2311
2312 def display_tableclass(obj):
2312 def display_tableclass(obj):
2313 return display_tableobject(obj())
2313 return display_tableobject(obj())
2314 generics.result_display.when_type(Table.__metaclass__)(display_tableclass)
2314 generics.result_display.when_type(Table.__metaclass__)(display_tableclass)
2315 else:
2315 else:
2316 def installdisplayhook():
2316 def installdisplayhook():
2317 _originalhook = sys.displayhook
2317 _originalhook = sys.displayhook
2318 def displayhook(obj):
2318 def displayhook(obj):
2319 if isinstance(obj, type) and issubclass(obj, Table):
2319 if isinstance(obj, type) and issubclass(obj, Table):
2320 obj = obj()
2320 obj = obj()
2321 if isinstance(obj, Table):
2321 if isinstance(obj, Table):
2322 obj = defaultdisplay(obj)
2322 obj = defaultdisplay(obj)
2323 if isinstance(obj, Display):
2323 if isinstance(obj, Display):
2324 return obj.display()
2324 return obj.display()
2325 else:
2325 else:
2326 _originalhook(obj)
2326 _originalhook(obj)
2327 sys.displayhook = displayhook
2327 sys.displayhook = displayhook
2328 installdisplayhook()
2328 installdisplayhook()
@@ -1,271 +1,271 b''
1 """Shell mode for IPython.
1 """Shell mode for IPython.
2
2
3 Start ipython in shell mode by invoking "ipython -p sh"
3 Start ipython in shell mode by invoking "ipython -p sh"
4
4
5 (the old version, "ipython -p pysh" still works but this is the more "modern"
5 (the old version, "ipython -p pysh" still works but this is the more "modern"
6 shell mode and is recommended for users who don't care about pysh-mode
6 shell mode and is recommended for users who don't care about pysh-mode
7 compatibility)
7 compatibility)
8 """
8 """
9
9
10 from IPython.core import ipapi
10 from IPython.core import ipapi
11 from IPython.core.error import TryNext
11 from IPython.core.error import TryNext
12 import os,re,textwrap
12 import os,re,textwrap
13
13
14 # The import below effectively obsoletes your old-style ipythonrc[.ini],
14 # The import below effectively obsoletes your old-style ipythonrc[.ini],
15 # so consider yourself warned!
15 # so consider yourself warned!
16
16
17 import ipy_defaults
17 import ipy_defaults
18
18
19 def main():
19 def main():
20 ip = ipapi.get()
20 ip = ipapi.get()
21 o = ip.options
21 o = ip.options
22 # autocall to "full" mode (smart mode is default, I like full mode)
22 # autocall to "full" mode (smart mode is default, I like full mode)
23
23
24 o.autocall = 2
24 o.autocall = 2
25
25
26 # Jason Orendorff's path class is handy to have in user namespace
26 # Jason Orendorff's path class is handy to have in user namespace
27 # if you are doing shell-like stuff
27 # if you are doing shell-like stuff
28 try:
28 try:
29 ip.ex("from IPython.external.path import path" )
29 ip.ex("from IPython.external.path import path" )
30 except ImportError:
30 except ImportError:
31 pass
31 pass
32
32
33 # beefed up %env is handy in shell mode
33 # beefed up %env is handy in shell mode
34 import envpersist
34 import envpersist
35
35
36 # To see where mycmd resides (in path/aliases), do %which mycmd
36 # To see where mycmd resides (in path/aliases), do %which mycmd
37 import ipy_which
37 import ipy_which
38
38
39 # tab completers for hg, svn, ...
39 # tab completers for hg, svn, ...
40 import ipy_app_completers
40 import ipy_app_completers
41
41
42 # To make executables foo and bar in mybin usable without PATH change, do:
42 # To make executables foo and bar in mybin usable without PATH change, do:
43 # %rehashdir c:/mybin
43 # %rehashdir c:/mybin
44 # %store foo
44 # %store foo
45 # %store bar
45 # %store bar
46 import ipy_rehashdir
46 import ipy_rehashdir
47
47
48 # does not work without subprocess module!
48 # does not work without subprocess module!
49 #import ipy_signals
49 #import ipy_signals
50
50
51 ip.ex('import os')
51 ip.ex('import os')
52 ip.ex("def up(): os.chdir('..')")
52 ip.ex("def up(): os.chdir('..')")
53 ip.user_ns['LA'] = LastArgFinder()
53 ip.user_ns['LA'] = LastArgFinder()
54
54
55 # You can assign to _prompt_title variable
55 # You can assign to _prompt_title variable
56 # to provide some extra information for prompt
56 # to provide some extra information for prompt
57 # (e.g. the current mode, host/username...)
57 # (e.g. the current mode, host/username...)
58
58
59 ip.user_ns['_prompt_title'] = ''
59 ip.user_ns['_prompt_title'] = ''
60
60
61 # Nice prompt
61 # Nice prompt
62 o.prompt_in1= r'\C_Green${_prompt_title}\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> '
62 o.prompt_in1= r'\C_Green${_prompt_title}\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> '
63 o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green> '
63 o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green> '
64 o.prompt_out= '<\#> '
64 o.prompt_out= '<\#> '
65
65
66 from IPython.core import release
66 from IPython.core import release
67
67
68 import sys
68 import sys
69 # Non-chatty banner
69 # Non-chatty banner
70 o.banner = "IPython %s [on Py %s]\n" % (release.version,sys.version.split(None,1)[0])
70 o.banner = "IPython %s [on Py %s]\n" % (release.version,sys.version.split(None,1)[0])
71
71
72
72
73 ip.default_option('cd','-q')
73 ip.default_option('cd','-q')
74 ip.default_option('macro', '-r')
74 ip.default_option('macro', '-r')
75 # If you only rarely want to execute the things you %edit...
75 # If you only rarely want to execute the things you %edit...
76 #ip.default_option('edit','-x')
76 #ip.default_option('edit','-x')
77
77
78
78
79 o.prompts_pad_left="1"
79 o.prompts_pad_left="1"
80 # Remove all blank lines in between prompts, like a normal shell.
80 # Remove all blank lines in between prompts, like a normal shell.
81 o.separate_in="0"
81 o.separate_in="0"
82 o.separate_out="0"
82 o.separate_out="0"
83 o.separate_out2="0"
83 o.separate_out2="0"
84
84
85 # now alias all syscommands
85 # now alias all syscommands
86
86
87 db = ip.db
87 db = ip.db
88
88
89 syscmds = db.get("syscmdlist",[] )
89 syscmds = db.get("syscmdlist",[] )
90 if not syscmds:
90 if not syscmds:
91 print textwrap.dedent("""
91 print textwrap.dedent("""
92 System command list not initialized, probably the first run...
92 System command list not initialized, probably the first run...
93 running %rehashx to refresh the command list. Run %rehashx
93 running %rehashx to refresh the command list. Run %rehashx
94 again to refresh command list (after installing new software etc.)
94 again to refresh command list (after installing new software etc.)
95 """)
95 """)
96 ip.magic('rehashx')
96 ip.magic('rehashx')
97 syscmds = db.get("syscmdlist")
97 syscmds = db.get("syscmdlist")
98
98
99 # lowcase aliases on win32 only
99 # lowcase aliases on win32 only
100 if os.name == 'posix':
100 if os.name == 'posix':
101 mapper = lambda s:s
101 mapper = lambda s:s
102 else:
102 else:
103 def mapper(s): return s.lower()
103 def mapper(s): return s.lower()
104
104
105 for cmd in syscmds:
105 for cmd in syscmds:
106 # print "sys",cmd #dbg
106 # print "sys",cmd #dbg
107 noext, ext = os.path.splitext(cmd)
107 noext, ext = os.path.splitext(cmd)
108 if ext.lower() == '.exe':
108 if ext.lower() == '.exe':
109 cmd = noext
109 cmd = noext
110
110
111 key = mapper(cmd)
111 key = mapper(cmd)
112 if key not in ip.alias_table:
112 if key not in ip.alias_manager.alias_table:
113 # Dots will be removed from alias names, since ipython
113 # Dots will be removed from alias names, since ipython
114 # assumes names with dots to be python code
114 # assumes names with dots to be python code
115
115
116 ip.define_alias(key.replace('.',''), cmd)
116 ip.define_alias(key.replace('.',''), cmd)
117
117
118 # mglob combines 'find', recursion, exclusion... '%mglob?' to learn more
118 # mglob combines 'find', recursion, exclusion... '%mglob?' to learn more
119 ip.load("IPython.external.mglob")
119 ip.load("IPython.external.mglob")
120
120
121 # win32 is crippled w/o cygwin, try to help it a little bit
121 # win32 is crippled w/o cygwin, try to help it a little bit
122 if sys.platform == 'win32':
122 if sys.platform == 'win32':
123 if 'cygwin' in os.environ['PATH'].lower():
123 if 'cygwin' in os.environ['PATH'].lower():
124 # use the colors of cygwin ls (recommended)
124 # use the colors of cygwin ls (recommended)
125 ip.define_alias('d', 'ls -F --color=auto')
125 ip.define_alias('d', 'ls -F --color=auto')
126 else:
126 else:
127 # get icp, imv, imkdir, igrep, irm,...
127 # get icp, imv, imkdir, igrep, irm,...
128 ip.load('ipy_fsops')
128 ip.load('ipy_fsops')
129
129
130 # and the next best thing to real 'ls -F'
130 # and the next best thing to real 'ls -F'
131 ip.define_alias('d','dir /w /og /on')
131 ip.define_alias('d','dir /w /og /on')
132
132
133 ip.set_hook('input_prefilter', slash_prefilter_f)
133 ip.set_hook('input_prefilter', slash_prefilter_f)
134 extend_shell_behavior(ip)
134 extend_shell_behavior(ip)
135
135
136 class LastArgFinder:
136 class LastArgFinder:
137 """ Allow $LA to work as "last argument of previous command", like $! in bash
137 """ Allow $LA to work as "last argument of previous command", like $! in bash
138
138
139 To call this in normal IPython code, do LA()
139 To call this in normal IPython code, do LA()
140 """
140 """
141 def __call__(self, hist_idx = None):
141 def __call__(self, hist_idx = None):
142 ip = ipapi.get()
142 ip = ipapi.get()
143 if hist_idx is None:
143 if hist_idx is None:
144 return str(self)
144 return str(self)
145 return ip.input_hist_raw[hist_idx].strip().split()[-1]
145 return ip.input_hist_raw[hist_idx].strip().split()[-1]
146 def __str__(self):
146 def __str__(self):
147 ip = ipapi.get()
147 ip = ipapi.get()
148 for cmd in reversed(ip.input_hist_raw):
148 for cmd in reversed(ip.input_hist_raw):
149 parts = cmd.strip().split()
149 parts = cmd.strip().split()
150 if len(parts) < 2 or parts[-1] in ['$LA', 'LA()']:
150 if len(parts) < 2 or parts[-1] in ['$LA', 'LA()']:
151 continue
151 continue
152 return parts[-1]
152 return parts[-1]
153 return ""
153 return ""
154
154
155 def slash_prefilter_f(self,line):
155 def slash_prefilter_f(self,line):
156 """ ./foo, ~/foo and /bin/foo now run foo as system command
156 """ ./foo, ~/foo and /bin/foo now run foo as system command
157
157
158 Removes the need for doing !./foo, !~/foo or !/bin/foo
158 Removes the need for doing !./foo, !~/foo or !/bin/foo
159 """
159 """
160 from IPython.utils import genutils
160 from IPython.utils import genutils
161 if re.match('(?:[.~]|/[a-zA-Z_0-9]+)/', line):
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 raise TryNext
163 raise TryNext
164
164
165 # XXX You do not need to understand the next function!
165 # XXX You do not need to understand the next function!
166 # This should probably be moved out of profile
166 # This should probably be moved out of profile
167
167
168 def extend_shell_behavior(ip):
168 def extend_shell_behavior(ip):
169
169
170 # Instead of making signature a global variable tie it to IPSHELL.
170 # Instead of making signature a global variable tie it to IPSHELL.
171 # In future if it is required to distinguish between different
171 # In future if it is required to distinguish between different
172 # shells we can assign a signature per shell basis
172 # shells we can assign a signature per shell basis
173 ip.__sig__ = 0xa005
173 ip.__sig__ = 0xa005
174 # mark the IPSHELL with this signature
174 # mark the IPSHELL with this signature
175 ip.user_ns['__builtins__'].__dict__['__sig__'] = ip.__sig__
175 ip.user_ns['__builtins__'].__dict__['__sig__'] = ip.__sig__
176
176
177 from IPython.external.Itpl import ItplNS
177 from IPython.external.Itpl import ItplNS
178 from IPython.utils.genutils import shell
178 from IPython.utils.genutils import shell
179 # utility to expand user variables via Itpl
179 # utility to expand user variables via Itpl
180 # xxx do something sensible with depth?
180 # xxx do something sensible with depth?
181 ip.var_expand = lambda cmd, lvars=None, depth=2: \
181 ip.var_expand = lambda cmd, lvars=None, depth=2: \
182 str(ItplNS(cmd, ip.user_ns, get_locals()))
182 str(ItplNS(cmd, ip.user_ns, get_locals()))
183
183
184 def get_locals():
184 def get_locals():
185 """ Substituting a variable through Itpl deep inside the IPSHELL stack
185 """ Substituting a variable through Itpl deep inside the IPSHELL stack
186 requires the knowledge of all the variables in scope upto the last
186 requires the knowledge of all the variables in scope upto the last
187 IPSHELL frame. This routine simply merges all the local variables
187 IPSHELL frame. This routine simply merges all the local variables
188 on the IPSHELL stack without worrying about their scope rules
188 on the IPSHELL stack without worrying about their scope rules
189 """
189 """
190 import sys
190 import sys
191 # note lambda expression constitues a function call
191 # note lambda expression constitues a function call
192 # hence fno should be incremented by one
192 # hence fno should be incremented by one
193 getsig = lambda fno: sys._getframe(fno+1).f_globals \
193 getsig = lambda fno: sys._getframe(fno+1).f_globals \
194 ['__builtins__'].__dict__['__sig__']
194 ['__builtins__'].__dict__['__sig__']
195 getlvars = lambda fno: sys._getframe(fno+1).f_locals
195 getlvars = lambda fno: sys._getframe(fno+1).f_locals
196 # trackback until we enter the IPSHELL
196 # trackback until we enter the IPSHELL
197 frame_no = 1
197 frame_no = 1
198 sig = ip.__sig__
198 sig = ip.__sig__
199 fsig = ~sig
199 fsig = ~sig
200 while fsig != sig :
200 while fsig != sig :
201 try:
201 try:
202 fsig = getsig(frame_no)
202 fsig = getsig(frame_no)
203 except (AttributeError, KeyError):
203 except (AttributeError, KeyError):
204 frame_no += 1
204 frame_no += 1
205 except ValueError:
205 except ValueError:
206 # stack is depleted
206 # stack is depleted
207 # call did not originate from IPSHELL
207 # call did not originate from IPSHELL
208 return {}
208 return {}
209 first_frame = frame_no
209 first_frame = frame_no
210 # walk further back until we exit from IPSHELL or deplete stack
210 # walk further back until we exit from IPSHELL or deplete stack
211 try:
211 try:
212 while(sig == getsig(frame_no+1)):
212 while(sig == getsig(frame_no+1)):
213 frame_no += 1
213 frame_no += 1
214 except (AttributeError, KeyError, ValueError):
214 except (AttributeError, KeyError, ValueError):
215 pass
215 pass
216 # merge the locals from top down hence overriding
216 # merge the locals from top down hence overriding
217 # any re-definitions of variables, functions etc.
217 # any re-definitions of variables, functions etc.
218 lvars = {}
218 lvars = {}
219 for fno in range(frame_no, first_frame-1, -1):
219 for fno in range(frame_no, first_frame-1, -1):
220 lvars.update(getlvars(fno))
220 lvars.update(getlvars(fno))
221 #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg
221 #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg
222 return lvars
222 return lvars
223
223
224 def _runlines(lines):
224 def _runlines(lines):
225 """Run a string of one or more lines of source.
225 """Run a string of one or more lines of source.
226
226
227 This method is capable of running a string containing multiple source
227 This method is capable of running a string containing multiple source
228 lines, as if they had been entered at the IPython prompt. Since it
228 lines, as if they had been entered at the IPython prompt. Since it
229 exposes IPython's processing machinery, the given strings can contain
229 exposes IPython's processing machinery, the given strings can contain
230 magic calls (%magic), special shell access (!cmd), etc."""
230 magic calls (%magic), special shell access (!cmd), etc."""
231
231
232 # We must start with a clean buffer, in case this is run from an
232 # We must start with a clean buffer, in case this is run from an
233 # interactive IPython session (via a magic, for example).
233 # interactive IPython session (via a magic, for example).
234 ip.resetbuffer()
234 ip.resetbuffer()
235 lines = lines.split('\n')
235 lines = lines.split('\n')
236 more = 0
236 more = 0
237 command = ''
237 command = ''
238 for line in lines:
238 for line in lines:
239 # skip blank lines so we don't mess up the prompt counter, but do
239 # skip blank lines so we don't mess up the prompt counter, but do
240 # NOT skip even a blank line if we are in a code block (more is
240 # NOT skip even a blank line if we are in a code block (more is
241 # true)
241 # true)
242 # if command is not empty trim the line
242 # if command is not empty trim the line
243 if command != '' :
243 if command != '' :
244 line = line.strip()
244 line = line.strip()
245 # add the broken line to the command
245 # add the broken line to the command
246 if line and line[-1] == '\\' :
246 if line and line[-1] == '\\' :
247 command += line[0:-1] + ' '
247 command += line[0:-1] + ' '
248 more = True
248 more = True
249 continue
249 continue
250 else :
250 else :
251 # add the last (current) line to the command
251 # add the last (current) line to the command
252 command += line
252 command += line
253 if command or more:
253 if command or more:
254 # push to raw history, so hist line numbers stay in sync
254 # push to raw history, so hist line numbers stay in sync
255 ip.input_hist_raw.append("# " + command + "\n")
255 ip.input_hist_raw.append("# " + command + "\n")
256
256
257 more = ip.push_line(ip.prefilter(command,more))
257 more = ip.push_line(ip.prefilter(command,more))
258 command = ''
258 command = ''
259 # IPython's runsource returns None if there was an error
259 # IPython's runsource returns None if there was an error
260 # compiling the code. This allows us to stop processing right
260 # compiling the code. This allows us to stop processing right
261 # away, so the user gets the error message at the right place.
261 # away, so the user gets the error message at the right place.
262 if more is None:
262 if more is None:
263 break
263 break
264 # final newline in case the input didn't have it, so that the code
264 # final newline in case the input didn't have it, so that the code
265 # actually does get executed
265 # actually does get executed
266 if more:
266 if more:
267 ip.push_line('\n')
267 ip.push_line('\n')
268
268
269 ip.runlines = _runlines
269 ip.runlines = _runlines
270
270
271 main()
271 main()
@@ -1,140 +1,140 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %rehashdir magic
2 """ IPython extension: add %rehashdir magic
3
3
4 Usage:
4 Usage:
5
5
6 %rehashdir c:/bin c:/tools
6 %rehashdir c:/bin c:/tools
7 - Add all executables under c:/bin and c:/tools to alias table, in
7 - Add all executables under c:/bin and c:/tools to alias table, in
8 order to make them directly executable from any directory.
8 order to make them directly executable from any directory.
9
9
10 This also serves as an example on how to extend ipython
10 This also serves as an example on how to extend ipython
11 with new magic functions.
11 with new magic functions.
12
12
13 Unlike rest of ipython, this requires Python 2.4 (optional
13 Unlike rest of ipython, this requires Python 2.4 (optional
14 extensions are allowed to do that).
14 extensions are allowed to do that).
15
15
16 """
16 """
17
17
18 from IPython.core import ipapi
18 from IPython.core import ipapi
19 ip = ipapi.get()
19 ip = ipapi.get()
20
20
21
21
22 import os,re,fnmatch,sys
22 import os,re,fnmatch,sys
23
23
24 def selflaunch(ip,line):
24 def selflaunch(ip,line):
25 """ Launch python script with 'this' interpreter
25 """ Launch python script with 'this' interpreter
26
26
27 e.g. d:\foo\ipykit.exe a.py
27 e.g. d:\foo\ipykit.exe a.py
28
28
29 """
29 """
30
30
31 tup = line.split(None,1)
31 tup = line.split(None,1)
32 if len(tup) == 1:
32 if len(tup) == 1:
33 print "Launching nested ipython session"
33 print "Launching nested ipython session"
34 os.system(sys.executable)
34 os.system(sys.executable)
35 return
35 return
36
36
37 cmd = sys.executable + ' ' + tup[1]
37 cmd = sys.executable + ' ' + tup[1]
38 print ">",cmd
38 print ">",cmd
39 os.system(cmd)
39 os.system(cmd)
40
40
41 class PyLauncher:
41 class PyLauncher:
42 """ Invoke selflanucher on the specified script
42 """ Invoke selflanucher on the specified script
43
43
44 This is mostly useful for associating with scripts using::
44 This is mostly useful for associating with scripts using::
45 _ip.define_alias('foo',PyLauncher('foo_script.py'))
45 _ip.define_alias('foo',PyLauncher('foo_script.py'))
46
46
47 """
47 """
48 def __init__(self,script):
48 def __init__(self,script):
49 self.script = os.path.abspath(script)
49 self.script = os.path.abspath(script)
50 def __call__(self, ip, line):
50 def __call__(self, ip, line):
51 if self.script.endswith('.ipy'):
51 if self.script.endswith('.ipy'):
52 ip.runlines(open(self.script).read())
52 ip.runlines(open(self.script).read())
53 else:
53 else:
54 # first word is the script/alias name itself, strip it
54 # first word is the script/alias name itself, strip it
55 tup = line.split(None,1)
55 tup = line.split(None,1)
56 if len(tup) == 2:
56 if len(tup) == 2:
57 tail = ' ' + tup[1]
57 tail = ' ' + tup[1]
58 else:
58 else:
59 tail = ''
59 tail = ''
60
60
61 selflaunch(ip,"py " + self.script + tail)
61 selflaunch(ip,"py " + self.script + tail)
62 def __repr__(self):
62 def __repr__(self):
63 return 'PyLauncher("%s")' % self.script
63 return 'PyLauncher("%s")' % self.script
64
64
65 def rehashdir_f(self,arg):
65 def rehashdir_f(self,arg):
66 """ Add executables in all specified dirs to alias table
66 """ Add executables in all specified dirs to alias table
67
67
68 Usage:
68 Usage:
69
69
70 %rehashdir c:/bin;c:/tools
70 %rehashdir c:/bin;c:/tools
71 - Add all executables under c:/bin and c:/tools to alias table, in
71 - Add all executables under c:/bin and c:/tools to alias table, in
72 order to make them directly executable from any directory.
72 order to make them directly executable from any directory.
73
73
74 Without arguments, add all executables in current directory.
74 Without arguments, add all executables in current directory.
75
75
76 """
76 """
77
77
78 # most of the code copied from Magic.magic_rehashx
78 # most of the code copied from Magic.magic_rehashx
79
79
80 def isjunk(fname):
80 def isjunk(fname):
81 junk = ['*~']
81 junk = ['*~']
82 for j in junk:
82 for j in junk:
83 if fnmatch.fnmatch(fname, j):
83 if fnmatch.fnmatch(fname, j):
84 return True
84 return True
85 return False
85 return False
86
86
87 created = []
87 created = []
88 if not arg:
88 if not arg:
89 arg = '.'
89 arg = '.'
90 path = map(os.path.abspath,arg.split(';'))
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 if os.name == 'posix':
93 if os.name == 'posix':
94 isexec = lambda fname:os.path.isfile(fname) and \
94 isexec = lambda fname:os.path.isfile(fname) and \
95 os.access(fname,os.X_OK)
95 os.access(fname,os.X_OK)
96 else:
96 else:
97
97
98 try:
98 try:
99 winext = os.environ['pathext'].replace(';','|').replace('.','')
99 winext = os.environ['pathext'].replace(';','|').replace('.','')
100 except KeyError:
100 except KeyError:
101 winext = 'exe|com|bat|py'
101 winext = 'exe|com|bat|py'
102 if 'py' not in winext:
102 if 'py' not in winext:
103 winext += '|py'
103 winext += '|py'
104
104
105 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
105 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
106 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
106 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
107 savedir = os.getcwd()
107 savedir = os.getcwd()
108 try:
108 try:
109 # write the whole loop for posix/Windows so we don't have an if in
109 # write the whole loop for posix/Windows so we don't have an if in
110 # the innermost part
110 # the innermost part
111 if os.name == 'posix':
111 if os.name == 'posix':
112 for pdir in path:
112 for pdir in path:
113 os.chdir(pdir)
113 os.chdir(pdir)
114 for ff in os.listdir(pdir):
114 for ff in os.listdir(pdir):
115 if isexec(ff) and not isjunk(ff):
115 if isexec(ff) and not isjunk(ff):
116 # each entry in the alias table must be (N,name),
116 # each entry in the alias table must be (N,name),
117 # where N is the number of positional arguments of the
117 # where N is the number of positional arguments of the
118 # alias.
118 # alias.
119 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
119 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
120 created.append(src)
120 created.append(src)
121 alias_table[src] = (0,tgt)
121 alias_table[src] = (0,tgt)
122 else:
122 else:
123 for pdir in path:
123 for pdir in path:
124 os.chdir(pdir)
124 os.chdir(pdir)
125 for ff in os.listdir(pdir):
125 for ff in os.listdir(pdir):
126 if isexec(ff) and not isjunk(ff):
126 if isexec(ff) and not isjunk(ff):
127 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
127 src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
128 src = src.lower()
128 src = src.lower()
129 created.append(src)
129 created.append(src)
130 alias_table[src] = (0,tgt)
130 alias_table[src] = (0,tgt)
131 # Make sure the alias table doesn't contain keywords or builtins
131 # Make sure the alias table doesn't contain keywords or builtins
132 self.shell.alias_table_validate()
132 self.shell.alias_table_validate()
133 # Call again init_auto_alias() so we get 'rm -i' and other
133 # Call again init_auto_alias() so we get 'rm -i' and other
134 # modified aliases since %rehashx will probably clobber them
134 # modified aliases since %rehashx will probably clobber them
135 # self.shell.init_auto_alias()
135 # self.shell.init_auto_alias()
136 finally:
136 finally:
137 os.chdir(savedir)
137 os.chdir(savedir)
138 return created
138 return created
139
139
140 ip.define_magic("rehashdir",rehashdir_f)
140 ip.define_magic("rehashdir",rehashdir_f)
@@ -1,243 +1,243 b''
1 """ Preliminary "job control" extensions for IPython
1 """ Preliminary "job control" extensions for IPython
2
2
3 requires python 2.4 (or separate 'subprocess' module
3 requires python 2.4 (or separate 'subprocess' module
4
4
5 This provides 2 features, launching background jobs and killing foreground jobs from another IPython instance.
5 This provides 2 features, launching background jobs and killing foreground jobs from another IPython instance.
6
6
7 Launching background jobs:
7 Launching background jobs:
8
8
9 Usage:
9 Usage:
10
10
11 [ipython]|2> import jobctrl
11 [ipython]|2> import jobctrl
12 [ipython]|3> &ls
12 [ipython]|3> &ls
13 <3> <jobctrl.IpyPopen object at 0x00D87FD0>
13 <3> <jobctrl.IpyPopen object at 0x00D87FD0>
14 [ipython]|4> _3.go
14 [ipython]|4> _3.go
15 -----------> _3.go()
15 -----------> _3.go()
16 ChangeLog
16 ChangeLog
17 IPython
17 IPython
18 MANIFEST.in
18 MANIFEST.in
19 README
19 README
20 README_Windows.txt
20 README_Windows.txt
21
21
22 ...
22 ...
23
23
24 Killing foreground tasks:
24 Killing foreground tasks:
25
25
26 Launch IPython instance, run a blocking command:
26 Launch IPython instance, run a blocking command:
27
27
28 [Q:/ipython]|1> import jobctrl
28 [Q:/ipython]|1> import jobctrl
29 [Q:/ipython]|2> cat
29 [Q:/ipython]|2> cat
30
30
31 Now launch a new IPython prompt and kill the process:
31 Now launch a new IPython prompt and kill the process:
32
32
33 IPython 0.8.3.svn.r2919 [on Py 2.5]
33 IPython 0.8.3.svn.r2919 [on Py 2.5]
34 [Q:/ipython]|1> import jobctrl
34 [Q:/ipython]|1> import jobctrl
35 [Q:/ipython]|2> %tasks
35 [Q:/ipython]|2> %tasks
36 6020: 'cat ' (Q:\ipython)
36 6020: 'cat ' (Q:\ipython)
37 [Q:/ipython]|3> %kill
37 [Q:/ipython]|3> %kill
38 SUCCESS: The process with PID 6020 has been terminated.
38 SUCCESS: The process with PID 6020 has been terminated.
39 [Q:/ipython]|4>
39 [Q:/ipython]|4>
40
40
41 (you don't need to specify PID for %kill if only one task is running)
41 (you don't need to specify PID for %kill if only one task is running)
42 """
42 """
43
43
44 from subprocess import *
44 from subprocess import *
45 import os,shlex,sys,time
45 import os,shlex,sys,time
46 import threading,Queue
46 import threading,Queue
47
47
48 from IPython.utils import genutils
48 from IPython.utils import genutils
49
49
50 from IPython.core import ipapi
50 from IPython.core import ipapi
51 from IPython.core.error import TryNext
51 from IPython.core.error import TryNext
52
52
53 if os.name == 'nt':
53 if os.name == 'nt':
54 def kill_process(pid):
54 def kill_process(pid):
55 os.system('taskkill /F /PID %d' % pid)
55 os.system('taskkill /F /PID %d' % pid)
56 else:
56 else:
57 def kill_process(pid):
57 def kill_process(pid):
58 os.system('kill -9 %d' % pid)
58 os.system('kill -9 %d' % pid)
59
59
60
60
61
61
62 class IpyPopen(Popen):
62 class IpyPopen(Popen):
63 def go(self):
63 def go(self):
64 print self.communicate()[0]
64 print self.communicate()[0]
65 def __repr__(self):
65 def __repr__(self):
66 return '<IPython job "%s" PID=%d>' % (self.line, self.pid)
66 return '<IPython job "%s" PID=%d>' % (self.line, self.pid)
67
67
68 def kill(self):
68 def kill(self):
69 kill_process(self.pid)
69 kill_process(self.pid)
70
70
71 def startjob(job):
71 def startjob(job):
72 p = IpyPopen(shlex.split(job), stdout=PIPE, shell = False)
72 p = IpyPopen(shlex.split(job), stdout=PIPE, shell = False)
73 p.line = job
73 p.line = job
74 return p
74 return p
75
75
76 class AsyncJobQ(threading.Thread):
76 class AsyncJobQ(threading.Thread):
77 def __init__(self):
77 def __init__(self):
78 threading.Thread.__init__(self)
78 threading.Thread.__init__(self)
79 self.q = Queue.Queue()
79 self.q = Queue.Queue()
80 self.output = []
80 self.output = []
81 self.stop = False
81 self.stop = False
82 def run(self):
82 def run(self):
83 while 1:
83 while 1:
84 cmd,cwd = self.q.get()
84 cmd,cwd = self.q.get()
85 if self.stop:
85 if self.stop:
86 self.output.append("** Discarding: '%s' - %s" % (cmd,cwd))
86 self.output.append("** Discarding: '%s' - %s" % (cmd,cwd))
87 continue
87 continue
88 self.output.append("** Task started: '%s' - %s" % (cmd,cwd))
88 self.output.append("** Task started: '%s' - %s" % (cmd,cwd))
89
89
90 p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd = cwd)
90 p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd = cwd)
91 out = p.stdout.read()
91 out = p.stdout.read()
92 self.output.append("** Task complete: '%s'\n" % cmd)
92 self.output.append("** Task complete: '%s'\n" % cmd)
93 self.output.append(out)
93 self.output.append(out)
94
94
95 def add(self,cmd):
95 def add(self,cmd):
96 self.q.put_nowait((cmd, os.getcwd()))
96 self.q.put_nowait((cmd, os.getcwd()))
97
97
98 def dumpoutput(self):
98 def dumpoutput(self):
99 while self.output:
99 while self.output:
100 item = self.output.pop(0)
100 item = self.output.pop(0)
101 print item
101 print item
102
102
103 _jobq = None
103 _jobq = None
104
104
105 def jobqueue_f(self, line):
105 def jobqueue_f(self, line):
106
106
107 global _jobq
107 global _jobq
108 if not _jobq:
108 if not _jobq:
109 print "Starting jobqueue - do '&some_long_lasting_system_command' to enqueue"
109 print "Starting jobqueue - do '&some_long_lasting_system_command' to enqueue"
110 _jobq = AsyncJobQ()
110 _jobq = AsyncJobQ()
111 _jobq.setDaemon(True)
111 _jobq.setDaemon(True)
112 _jobq.start()
112 _jobq.start()
113 ip.jobq = _jobq.add
113 ip.jobq = _jobq.add
114 return
114 return
115 if line.strip() == 'stop':
115 if line.strip() == 'stop':
116 print "Stopping and clearing jobqueue, %jobqueue start to start again"
116 print "Stopping and clearing jobqueue, %jobqueue start to start again"
117 _jobq.stop = True
117 _jobq.stop = True
118 return
118 return
119 if line.strip() == 'start':
119 if line.strip() == 'start':
120 _jobq.stop = False
120 _jobq.stop = False
121 return
121 return
122
122
123 def jobctrl_prefilter_f(self,line):
123 def jobctrl_prefilter_f(self,line):
124 if line.startswith('&'):
124 if line.startswith('&'):
125 pre,fn,rest = self.split_user_input(line[1:])
125 pre,fn,rest = self.split_user_input(line[1:])
126
126
127 line = ip.expand_aliases(fn,rest)
127 line = ip.expand_aliases(fn,rest)
128 if not _jobq:
128 if not _jobq:
129 return '_ip.startjob(%s)' % genutils.make_quoted_expr(line)
129 return 'get_ipython().startjob(%s)' % genutils.make_quoted_expr(line)
130 return '_ip.jobq(%s)' % genutils.make_quoted_expr(line)
130 return 'get_ipython().jobq(%s)' % genutils.make_quoted_expr(line)
131
131
132 raise TryNext
132 raise TryNext
133
133
134 def jobq_output_hook(self):
134 def jobq_output_hook(self):
135 if not _jobq:
135 if not _jobq:
136 return
136 return
137 _jobq.dumpoutput()
137 _jobq.dumpoutput()
138
138
139
139
140
140
141 def job_list(ip):
141 def job_list(ip):
142 keys = ip.db.keys('tasks/*')
142 keys = ip.db.keys('tasks/*')
143 ents = [ip.db[k] for k in keys]
143 ents = [ip.db[k] for k in keys]
144 return ents
144 return ents
145
145
146 def magic_tasks(self,line):
146 def magic_tasks(self,line):
147 """ Show a list of tasks.
147 """ Show a list of tasks.
148
148
149 A 'task' is a process that has been started in IPython when 'jobctrl' extension is enabled.
149 A 'task' is a process that has been started in IPython when 'jobctrl' extension is enabled.
150 Tasks can be killed with %kill.
150 Tasks can be killed with %kill.
151
151
152 '%tasks clear' clears the task list (from stale tasks)
152 '%tasks clear' clears the task list (from stale tasks)
153 """
153 """
154 ip = self.getapi()
154 ip = self.getapi()
155 if line.strip() == 'clear':
155 if line.strip() == 'clear':
156 for k in ip.db.keys('tasks/*'):
156 for k in ip.db.keys('tasks/*'):
157 print "Clearing",ip.db[k]
157 print "Clearing",ip.db[k]
158 del ip.db[k]
158 del ip.db[k]
159 return
159 return
160
160
161 ents = job_list(ip)
161 ents = job_list(ip)
162 if not ents:
162 if not ents:
163 print "No tasks running"
163 print "No tasks running"
164 for pid,cmd,cwd,t in ents:
164 for pid,cmd,cwd,t in ents:
165 dur = int(time.time()-t)
165 dur = int(time.time()-t)
166 print "%d: '%s' (%s) %d:%02d" % (pid,cmd,cwd, dur / 60,dur%60)
166 print "%d: '%s' (%s) %d:%02d" % (pid,cmd,cwd, dur / 60,dur%60)
167
167
168 def magic_kill(self,line):
168 def magic_kill(self,line):
169 """ Kill a task
169 """ Kill a task
170
170
171 Without args, either kill one task (if only one running) or show list (if many)
171 Without args, either kill one task (if only one running) or show list (if many)
172 With arg, assume it's the process id.
172 With arg, assume it's the process id.
173
173
174 %kill is typically (much) more powerful than trying to terminate a process with ctrl+C.
174 %kill is typically (much) more powerful than trying to terminate a process with ctrl+C.
175 """
175 """
176 ip = self.getapi()
176 ip = self.getapi()
177 jobs = job_list(ip)
177 jobs = job_list(ip)
178
178
179 if not line.strip():
179 if not line.strip():
180 if len(jobs) == 1:
180 if len(jobs) == 1:
181 kill_process(jobs[0][0])
181 kill_process(jobs[0][0])
182 else:
182 else:
183 magic_tasks(self,line)
183 magic_tasks(self,line)
184 return
184 return
185
185
186 try:
186 try:
187 pid = int(line)
187 pid = int(line)
188 kill_process(pid)
188 kill_process(pid)
189 except ValueError:
189 except ValueError:
190 magic_tasks(self,line)
190 magic_tasks(self,line)
191
191
192 if sys.platform == 'win32':
192 if sys.platform == 'win32':
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()
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 PopenExc = WindowsError
194 PopenExc = WindowsError
195 else:
195 else:
196 # todo linux commands
196 # todo linux commands
197 shell_internal_commands = []
197 shell_internal_commands = []
198 PopenExc = OSError
198 PopenExc = OSError
199
199
200
200
201 def jobctrl_shellcmd(ip,cmd):
201 def jobctrl_shellcmd(ip,cmd):
202 """ os.system replacement that stores process info to db['tasks/t1234'] """
202 """ os.system replacement that stores process info to db['tasks/t1234'] """
203 cmd = cmd.strip()
203 cmd = cmd.strip()
204 cmdname = cmd.split(None,1)[0]
204 cmdname = cmd.split(None,1)[0]
205 if cmdname in shell_internal_commands or '|' in cmd or '>' in cmd or '<' in cmd:
205 if cmdname in shell_internal_commands or '|' in cmd or '>' in cmd or '<' in cmd:
206 use_shell = True
206 use_shell = True
207 else:
207 else:
208 use_shell = False
208 use_shell = False
209
209
210 jobentry = None
210 jobentry = None
211 try:
211 try:
212 try:
212 try:
213 p = Popen(cmd,shell = use_shell)
213 p = Popen(cmd,shell = use_shell)
214 except PopenExc :
214 except PopenExc :
215 if use_shell:
215 if use_shell:
216 # try with os.system
216 # try with os.system
217 os.system(cmd)
217 os.system(cmd)
218 return
218 return
219 else:
219 else:
220 # have to go via shell, sucks
220 # have to go via shell, sucks
221 p = Popen(cmd,shell = True)
221 p = Popen(cmd,shell = True)
222
222
223 jobentry = 'tasks/t' + str(p.pid)
223 jobentry = 'tasks/t' + str(p.pid)
224 ip.db[jobentry] = (p.pid,cmd,os.getcwd(),time.time())
224 ip.db[jobentry] = (p.pid,cmd,os.getcwd(),time.time())
225 p.communicate()
225 p.communicate()
226
226
227 finally:
227 finally:
228 if jobentry:
228 if jobentry:
229 del ip.db[jobentry]
229 del ip.db[jobentry]
230
230
231
231
232 def install():
232 def install():
233 global ip
233 global ip
234 ip = ipapi.get()
234 ip = ipapi.get()
235 # needed to make startjob visible as _ip.startjob('blah')
235 # needed to make startjob visible as _ip.startjob('blah')
236 ip.startjob = startjob
236 ip.startjob = startjob
237 ip.set_hook('input_prefilter', jobctrl_prefilter_f)
237 ip.set_hook('input_prefilter', jobctrl_prefilter_f)
238 ip.set_hook('shell_hook', jobctrl_shellcmd)
238 ip.set_hook('shell_hook', jobctrl_shellcmd)
239 ip.define_magic('kill',magic_kill)
239 ip.define_magic('kill',magic_kill)
240 ip.define_magic('tasks',magic_tasks)
240 ip.define_magic('tasks',magic_tasks)
241 ip.define_magic('jobqueue',jobqueue_f)
241 ip.define_magic('jobqueue',jobqueue_f)
242 ip.set_hook('pre_prompt_hook', jobq_output_hook)
242 ip.set_hook('pre_prompt_hook', jobq_output_hook)
243 install()
243 install()
@@ -1,182 +1,183 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 %store magic for lightweight persistence.
3 %store magic for lightweight persistence.
4
4
5 Stores variables, aliases etc. in PickleShare database.
5 Stores variables, aliases etc. in PickleShare database.
6 """
6 """
7
7
8 from IPython.core import ipapi
8 from IPython.core import ipapi
9 from IPython.core.error import TryNext, UsageError
9 from IPython.core.error import TryNext, UsageError
10 ip = ipapi.get()
10 ip = ipapi.get()
11
11
12 import pickleshare
12 import pickleshare
13
13
14 import inspect,pickle,os,sys,textwrap
14 import inspect,pickle,os,sys,textwrap
15 from IPython.core.fakemodule import FakeModule
15 from IPython.core.fakemodule import FakeModule
16
16
17 def restore_aliases(self):
17 def restore_aliases(self):
18 ip = self.getapi()
18 ip = self.getapi()
19 staliases = ip.db.get('stored_aliases', {})
19 staliases = ip.db.get('stored_aliases', {})
20 for k,v in staliases.items():
20 for k,v in staliases.items():
21 #print "restore alias",k,v # dbg
21 #print "restore alias",k,v # dbg
22 #self.alias_table[k] = v
22 #self.alias_table[k] = v
23 ip.define_alias(k,v)
23 ip.define_alias(k,v)
24
24
25
25
26 def refresh_variables(ip):
26 def refresh_variables(ip):
27 db = ip.db
27 db = ip.db
28 for key in db.keys('autorestore/*'):
28 for key in db.keys('autorestore/*'):
29 # strip autorestore
29 # strip autorestore
30 justkey = os.path.basename(key)
30 justkey = os.path.basename(key)
31 try:
31 try:
32 obj = db[key]
32 obj = db[key]
33 except KeyError:
33 except KeyError:
34 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
34 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
35 print "The error was:",sys.exc_info()[0]
35 print "The error was:",sys.exc_info()[0]
36 else:
36 else:
37 #print "restored",justkey,"=",obj #dbg
37 #print "restored",justkey,"=",obj #dbg
38 ip.user_ns[justkey] = obj
38 ip.user_ns[justkey] = obj
39
39
40
40
41 def restore_dhist(ip):
41 def restore_dhist(ip):
42 db = ip.db
42 db = ip.db
43 ip.user_ns['_dh'] = db.get('dhist',[])
43 ip.user_ns['_dh'] = db.get('dhist',[])
44
44
45 def restore_data(self):
45 def restore_data(self):
46 ip = self.getapi()
46 ip = self.getapi()
47 refresh_variables(ip)
47 refresh_variables(ip)
48 restore_aliases(self)
48 restore_aliases(self)
49 restore_dhist(self)
49 restore_dhist(self)
50 raise TryNext
50 raise TryNext
51
51
52 ip.set_hook('late_startup_hook', restore_data)
52 ip.set_hook('late_startup_hook', restore_data)
53
53
54 def magic_store(self, parameter_s=''):
54 def magic_store(self, parameter_s=''):
55 """Lightweight persistence for python variables.
55 """Lightweight persistence for python variables.
56
56
57 Example:
57 Example:
58
58
59 ville@badger[~]|1> A = ['hello',10,'world']\\
59 ville@badger[~]|1> A = ['hello',10,'world']\\
60 ville@badger[~]|2> %store A\\
60 ville@badger[~]|2> %store A\\
61 ville@badger[~]|3> Exit
61 ville@badger[~]|3> Exit
62
62
63 (IPython session is closed and started again...)
63 (IPython session is closed and started again...)
64
64
65 ville@badger:~$ ipython -p pysh\\
65 ville@badger:~$ ipython -p pysh\\
66 ville@badger[~]|1> print A
66 ville@badger[~]|1> print A
67
67
68 ['hello', 10, 'world']
68 ['hello', 10, 'world']
69
69
70 Usage:
70 Usage:
71
71
72 %store - Show list of all variables and their current values\\
72 %store - Show list of all variables and their current values\\
73 %store <var> - Store the *current* value of the variable to disk\\
73 %store <var> - Store the *current* value of the variable to disk\\
74 %store -d <var> - Remove the variable and its value from storage\\
74 %store -d <var> - Remove the variable and its value from storage\\
75 %store -z - Remove all variables from storage\\
75 %store -z - Remove all variables from storage\\
76 %store -r - Refresh all variables from store (delete current vals)\\
76 %store -r - Refresh all variables from store (delete current vals)\\
77 %store foo >a.txt - Store value of foo to new file a.txt\\
77 %store foo >a.txt - Store value of foo to new file a.txt\\
78 %store foo >>a.txt - Append value of foo to file a.txt\\
78 %store foo >>a.txt - Append value of foo to file a.txt\\
79
79
80 It should be noted that if you change the value of a variable, you
80 It should be noted that if you change the value of a variable, you
81 need to %store it again if you want to persist the new value.
81 need to %store it again if you want to persist the new value.
82
82
83 Note also that the variables will need to be pickleable; most basic
83 Note also that the variables will need to be pickleable; most basic
84 python types can be safely %stored.
84 python types can be safely %stored.
85
85
86 Also aliases can be %store'd across sessions.
86 Also aliases can be %store'd across sessions.
87 """
87 """
88
88
89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
89 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
90 args = argsl.split(None,1)
90 args = argsl.split(None,1)
91 ip = self.getapi()
91 ip = self.getapi()
92 db = ip.db
92 db = ip.db
93 # delete
93 # delete
94 if opts.has_key('d'):
94 if opts.has_key('d'):
95 try:
95 try:
96 todel = args[0]
96 todel = args[0]
97 except IndexError:
97 except IndexError:
98 raise UsageError('You must provide the variable to forget')
98 raise UsageError('You must provide the variable to forget')
99 else:
99 else:
100 try:
100 try:
101 del db['autorestore/' + todel]
101 del db['autorestore/' + todel]
102 except:
102 except:
103 raise UsageError("Can't delete variable '%s'" % todel)
103 raise UsageError("Can't delete variable '%s'" % todel)
104 # reset
104 # reset
105 elif opts.has_key('z'):
105 elif opts.has_key('z'):
106 for k in db.keys('autorestore/*'):
106 for k in db.keys('autorestore/*'):
107 del db[k]
107 del db[k]
108
108
109 elif opts.has_key('r'):
109 elif opts.has_key('r'):
110 refresh_variables(ip)
110 refresh_variables(ip)
111
111
112
112
113 # run without arguments -> list variables & values
113 # run without arguments -> list variables & values
114 elif not args:
114 elif not args:
115 vars = self.db.keys('autorestore/*')
115 vars = self.db.keys('autorestore/*')
116 vars.sort()
116 vars.sort()
117 if vars:
117 if vars:
118 size = max(map(len,vars))
118 size = max(map(len,vars))
119 else:
119 else:
120 size = 0
120 size = 0
121
121
122 print 'Stored variables and their in-db values:'
122 print 'Stored variables and their in-db values:'
123 fmt = '%-'+str(size)+'s -> %s'
123 fmt = '%-'+str(size)+'s -> %s'
124 get = db.get
124 get = db.get
125 for var in vars:
125 for var in vars:
126 justkey = os.path.basename(var)
126 justkey = os.path.basename(var)
127 # print 30 first characters from every var
127 # print 30 first characters from every var
128 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
128 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
129
129
130 # default action - store the variable
130 # default action - store the variable
131 else:
131 else:
132 # %store foo >file.txt or >>file.txt
132 # %store foo >file.txt or >>file.txt
133 if len(args) > 1 and args[1].startswith('>'):
133 if len(args) > 1 and args[1].startswith('>'):
134 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
134 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
135 if args[1].startswith('>>'):
135 if args[1].startswith('>>'):
136 fil = open(fnam,'a')
136 fil = open(fnam,'a')
137 else:
137 else:
138 fil = open(fnam,'w')
138 fil = open(fnam,'w')
139 obj = ip.ev(args[0])
139 obj = ip.ev(args[0])
140 print "Writing '%s' (%s) to file '%s'." % (args[0],
140 print "Writing '%s' (%s) to file '%s'." % (args[0],
141 obj.__class__.__name__, fnam)
141 obj.__class__.__name__, fnam)
142
142
143
143
144 if not isinstance (obj,basestring):
144 if not isinstance (obj,basestring):
145 from pprint import pprint
145 from pprint import pprint
146 pprint(obj,fil)
146 pprint(obj,fil)
147 else:
147 else:
148 fil.write(obj)
148 fil.write(obj)
149 if not obj.endswith('\n'):
149 if not obj.endswith('\n'):
150 fil.write('\n')
150 fil.write('\n')
151
151
152 fil.close()
152 fil.close()
153 return
153 return
154
154
155 # %store foo
155 # %store foo
156 try:
156 try:
157 obj = ip.user_ns[args[0]]
157 obj = ip.user_ns[args[0]]
158 except KeyError:
158 except KeyError:
159 # it might be an alias
159 # it might be an alias
160 # This needs to be refactored to use the new AliasManager stuff.
160 if args[0] in self.alias_table:
161 if args[0] in self.alias_table:
161 staliases = db.get('stored_aliases',{})
162 staliases = db.get('stored_aliases',{})
162 staliases[ args[0] ] = self.alias_table[ args[0] ]
163 staliases[ args[0] ] = self.alias_table[ args[0] ]
163 db['stored_aliases'] = staliases
164 db['stored_aliases'] = staliases
164 print "Alias stored:", args[0], self.alias_table[ args[0] ]
165 print "Alias stored:", args[0], self.alias_table[ args[0] ]
165 return
166 return
166 else:
167 else:
167 raise UsageError("Unknown variable '%s'" % args[0])
168 raise UsageError("Unknown variable '%s'" % args[0])
168
169
169 else:
170 else:
170 if isinstance(inspect.getmodule(obj), FakeModule):
171 if isinstance(inspect.getmodule(obj), FakeModule):
171 print textwrap.dedent("""\
172 print textwrap.dedent("""\
172 Warning:%s is %s
173 Warning:%s is %s
173 Proper storage of interactively declared classes (or instances
174 Proper storage of interactively declared classes (or instances
174 of those classes) is not possible! Only instances
175 of those classes) is not possible! Only instances
175 of classes in real modules on file system can be %%store'd.
176 of classes in real modules on file system can be %%store'd.
176 """ % (args[0], obj) )
177 """ % (args[0], obj) )
177 return
178 return
178 #pickled = pickle.dumps(obj)
179 #pickled = pickle.dumps(obj)
179 self.db[ 'autorestore/' + args[0] ] = obj
180 self.db[ 'autorestore/' + args[0] ] = obj
180 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
181 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
181
182
182 ip.define_magic('store',magic_store)
183 ip.define_magic('store',magic_store)
@@ -1,171 +1,171 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
2
3 """Magic command interface for interactive parallel work."""
3 """Magic command interface for interactive parallel work."""
4
4
5 __docformat__ = "restructuredtext en"
5 __docformat__ = "restructuredtext en"
6
6
7 #-------------------------------------------------------------------------------
7 #-------------------------------------------------------------------------------
8 # Copyright (C) 2008 The IPython Development Team
8 # Copyright (C) 2008 The IPython Development Team
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-------------------------------------------------------------------------------
12 #-------------------------------------------------------------------------------
13
13
14 #-------------------------------------------------------------------------------
14 #-------------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-------------------------------------------------------------------------------
16 #-------------------------------------------------------------------------------
17
17
18 import new
18 import new
19
19
20 from IPython.core.iplib import InteractiveShell
20 from IPython.core.iplib import InteractiveShell
21 from IPython.core.shell import MTInteractiveShell
21 from IPython.core.shell import MTInteractiveShell
22
22
23 from twisted.internet.defer import Deferred
23 from twisted.internet.defer import Deferred
24
24
25
25
26 #-------------------------------------------------------------------------------
26 #-------------------------------------------------------------------------------
27 # Definitions of magic functions for use with IPython
27 # Definitions of magic functions for use with IPython
28 #-------------------------------------------------------------------------------
28 #-------------------------------------------------------------------------------
29
29
30 NO_ACTIVE_CONTROLLER = """
30 NO_ACTIVE_CONTROLLER = """
31 Error: No Controller is activated
31 Error: No Controller is activated
32 Use activate() on a RemoteController object to activate it for magics.
32 Use activate() on a RemoteController object to activate it for magics.
33 """
33 """
34
34
35 def magic_result(self,parameter_s=''):
35 def magic_result(self,parameter_s=''):
36 """Print the result of command i on all engines of the active controller.
36 """Print the result of command i on all engines of the active controller.
37
37
38 To activate a controller in IPython, first create it and then call
38 To activate a controller in IPython, first create it and then call
39 the activate() method.
39 the activate() method.
40
40
41 Then you can do the following:
41 Then you can do the following:
42
42
43 >>> result # Print the latest result
43 >>> result # Print the latest result
44 Printing result...
44 Printing result...
45 [127.0.0.1:0] In [1]: b = 10
45 [127.0.0.1:0] In [1]: b = 10
46 [127.0.0.1:1] In [1]: b = 10
46 [127.0.0.1:1] In [1]: b = 10
47
47
48 >>> result 0 # Print result 0
48 >>> result 0 # Print result 0
49 In [14]: result 0
49 In [14]: result 0
50 Printing result...
50 Printing result...
51 [127.0.0.1:0] In [0]: a = 5
51 [127.0.0.1:0] In [0]: a = 5
52 [127.0.0.1:1] In [0]: a = 5
52 [127.0.0.1:1] In [0]: a = 5
53 """
53 """
54 try:
54 try:
55 activeController = __IPYTHON__.activeController
55 activeController = __IPYTHON__.activeController
56 except AttributeError:
56 except AttributeError:
57 print NO_ACTIVE_CONTROLLER
57 print NO_ACTIVE_CONTROLLER
58 else:
58 else:
59 try:
59 try:
60 index = int(parameter_s)
60 index = int(parameter_s)
61 except:
61 except:
62 index = None
62 index = None
63 result = activeController.get_result(index)
63 result = activeController.get_result(index)
64 return result
64 return result
65
65
66 def magic_px(self,parameter_s=''):
66 def magic_px(self,parameter_s=''):
67 """Executes the given python command on the active IPython Controller.
67 """Executes the given python command on the active IPython Controller.
68
68
69 To activate a Controller in IPython, first create it and then call
69 To activate a Controller in IPython, first create it and then call
70 the activate() method.
70 the activate() method.
71
71
72 Then you can do the following:
72 Then you can do the following:
73
73
74 >>> %px a = 5 # Runs a = 5 on all nodes
74 >>> %px a = 5 # Runs a = 5 on all nodes
75 """
75 """
76
76
77 try:
77 try:
78 activeController = __IPYTHON__.activeController
78 activeController = __IPYTHON__.activeController
79 except AttributeError:
79 except AttributeError:
80 print NO_ACTIVE_CONTROLLER
80 print NO_ACTIVE_CONTROLLER
81 else:
81 else:
82 print "Parallel execution on engines: %s" % activeController.targets
82 print "Parallel execution on engines: %s" % activeController.targets
83 result = activeController.execute(parameter_s)
83 result = activeController.execute(parameter_s)
84 return result
84 return result
85
85
86 def pxrunsource(self, source, filename="<input>", symbol="single"):
86 def pxrunsource(self, source, filename="<input>", symbol="single"):
87
87
88 try:
88 try:
89 code = self.compile(source, filename, symbol)
89 code = self.compile(source, filename, symbol)
90 except (OverflowError, SyntaxError, ValueError):
90 except (OverflowError, SyntaxError, ValueError):
91 # Case 1
91 # Case 1
92 self.showsyntaxerror(filename)
92 self.showsyntaxerror(filename)
93 return None
93 return None
94
94
95 if code is None:
95 if code is None:
96 # Case 2
96 # Case 2
97 return True
97 return True
98
98
99 # Case 3
99 # Case 3
100 # Because autopx is enabled, we now call executeAll or disable autopx if
100 # Because autopx is enabled, we now call executeAll or disable autopx if
101 # %autopx or autopx has been called
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 _disable_autopx(self)
103 _disable_autopx(self)
104 return False
104 return False
105 else:
105 else:
106 try:
106 try:
107 result = self.activeController.execute(source)
107 result = self.activeController.execute(source)
108 except:
108 except:
109 self.showtraceback()
109 self.showtraceback()
110 else:
110 else:
111 print result.__repr__()
111 print result.__repr__()
112 return False
112 return False
113
113
114 def magic_autopx(self, parameter_s=''):
114 def magic_autopx(self, parameter_s=''):
115 """Toggles auto parallel mode for the active IPython Controller.
115 """Toggles auto parallel mode for the active IPython Controller.
116
116
117 To activate a Controller in IPython, first create it and then call
117 To activate a Controller in IPython, first create it and then call
118 the activate() method.
118 the activate() method.
119
119
120 Then you can do the following:
120 Then you can do the following:
121
121
122 >>> %autopx # Now all commands are executed in parallel
122 >>> %autopx # Now all commands are executed in parallel
123 Auto Parallel Enabled
123 Auto Parallel Enabled
124 Type %autopx to disable
124 Type %autopx to disable
125 ...
125 ...
126 >>> %autopx # Now all commands are locally executed
126 >>> %autopx # Now all commands are locally executed
127 Auto Parallel Disabled
127 Auto Parallel Disabled
128 """
128 """
129
129
130 if hasattr(self, 'autopx'):
130 if hasattr(self, 'autopx'):
131 if self.autopx == True:
131 if self.autopx == True:
132 _disable_autopx(self)
132 _disable_autopx(self)
133 else:
133 else:
134 _enable_autopx(self)
134 _enable_autopx(self)
135 else:
135 else:
136 _enable_autopx(self)
136 _enable_autopx(self)
137
137
138 def _enable_autopx(self):
138 def _enable_autopx(self):
139 """Enable %autopx mode by saving the original runsource and installing
139 """Enable %autopx mode by saving the original runsource and installing
140 pxrunsource.
140 pxrunsource.
141 """
141 """
142 try:
142 try:
143 activeController = __IPYTHON__.activeController
143 activeController = __IPYTHON__.activeController
144 except AttributeError:
144 except AttributeError:
145 print "No active RemoteController found, use RemoteController.activate()."
145 print "No active RemoteController found, use RemoteController.activate()."
146 else:
146 else:
147 self._original_runsource = self.runsource
147 self._original_runsource = self.runsource
148 self.runsource = new.instancemethod(pxrunsource, self, self.__class__)
148 self.runsource = new.instancemethod(pxrunsource, self, self.__class__)
149 self.autopx = True
149 self.autopx = True
150 print "Auto Parallel Enabled\nType %autopx to disable"
150 print "Auto Parallel Enabled\nType %autopx to disable"
151
151
152 def _disable_autopx(self):
152 def _disable_autopx(self):
153 """Disable %autopx by restoring the original runsource."""
153 """Disable %autopx by restoring the original runsource."""
154 if hasattr(self, 'autopx'):
154 if hasattr(self, 'autopx'):
155 if self.autopx == True:
155 if self.autopx == True:
156 self.runsource = self._original_runsource
156 self.runsource = self._original_runsource
157 self.autopx = False
157 self.autopx = False
158 print "Auto Parallel Disabled"
158 print "Auto Parallel Disabled"
159
159
160 # Add the new magic function to the class dict:
160 # Add the new magic function to the class dict:
161
161
162 InteractiveShell.magic_result = magic_result
162 InteractiveShell.magic_result = magic_result
163 InteractiveShell.magic_px = magic_px
163 InteractiveShell.magic_px = magic_px
164 InteractiveShell.magic_autopx = magic_autopx
164 InteractiveShell.magic_autopx = magic_autopx
165
165
166 # And remove the global name to keep global namespace clean. Don't worry, the
166 # And remove the global name to keep global namespace clean. Don't worry, the
167 # copy bound to IPython stays, we're just removing the global name.
167 # copy bound to IPython stays, we're just removing the global name.
168 del magic_result
168 del magic_result
169 del magic_px
169 del magic_px
170 del magic_autopx
170 del magic_autopx
171
171
@@ -1,687 +1,690 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Tests for IPython.utils.traitlets.
4 Tests for IPython.utils.traitlets.
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Enthought, Inc. Some of the code in this file comes from enthought.traits
9 * Enthought, Inc. Some of the code in this file comes from enthought.traits
10 and is licensed under the BSD license. Also, many of the ideas also come
10 and is licensed under the BSD license. Also, many of the ideas also come
11 from enthought.traits even though our implementation is very different.
11 from enthought.traits even though our implementation is very different.
12 """
12 """
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Copyright (C) 2008-2009 The IPython Development Team
15 # Copyright (C) 2008-2009 The IPython Development Team
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 import sys
25 import sys
26 import os
26 import os
27
27
28
28
29 from unittest import TestCase
29 from unittest import TestCase
30
30
31 from IPython.utils.traitlets import (
31 from IPython.utils.traitlets import (
32 HasTraitlets, MetaHasTraitlets, TraitletType, Any,
32 HasTraitlets, MetaHasTraitlets, TraitletType, Any,
33 Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError,
33 Int, Long, Float, Complex, Str, Unicode, Bool, TraitletError,
34 Undefined, Type, This, Instance
34 Undefined, Type, This, Instance
35 )
35 )
36
36
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Helper classes for testing
39 # Helper classes for testing
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41
41
42
42
43 class HasTraitletsStub(HasTraitlets):
43 class HasTraitletsStub(HasTraitlets):
44
44
45 def _notify_traitlet(self, name, old, new):
45 def _notify_traitlet(self, name, old, new):
46 self._notify_name = name
46 self._notify_name = name
47 self._notify_old = old
47 self._notify_old = old
48 self._notify_new = new
48 self._notify_new = new
49
49
50
50
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52 # Test classes
52 # Test classes
53 #-----------------------------------------------------------------------------
53 #-----------------------------------------------------------------------------
54
54
55
55
56 class TestTraitletType(TestCase):
56 class TestTraitletType(TestCase):
57
57
58 def test_get_undefined(self):
58 def test_get_undefined(self):
59 class A(HasTraitlets):
59 class A(HasTraitlets):
60 a = TraitletType
60 a = TraitletType
61 a = A()
61 a = A()
62 self.assertEquals(a.a, Undefined)
62 self.assertEquals(a.a, Undefined)
63
63
64 def test_set(self):
64 def test_set(self):
65 class A(HasTraitletsStub):
65 class A(HasTraitletsStub):
66 a = TraitletType
66 a = TraitletType
67
67
68 a = A()
68 a = A()
69 a.a = 10
69 a.a = 10
70 self.assertEquals(a.a, 10)
70 self.assertEquals(a.a, 10)
71 self.assertEquals(a._notify_name, 'a')
71 self.assertEquals(a._notify_name, 'a')
72 self.assertEquals(a._notify_old, Undefined)
72 self.assertEquals(a._notify_old, Undefined)
73 self.assertEquals(a._notify_new, 10)
73 self.assertEquals(a._notify_new, 10)
74
74
75 def test_validate(self):
75 def test_validate(self):
76 class MyTT(TraitletType):
76 class MyTT(TraitletType):
77 def validate(self, inst, value):
77 def validate(self, inst, value):
78 return -1
78 return -1
79 class A(HasTraitletsStub):
79 class A(HasTraitletsStub):
80 tt = MyTT
80 tt = MyTT
81
81
82 a = A()
82 a = A()
83 a.tt = 10
83 a.tt = 10
84 self.assertEquals(a.tt, -1)
84 self.assertEquals(a.tt, -1)
85
85
86 def test_default_validate(self):
86 def test_default_validate(self):
87 class MyIntTT(TraitletType):
87 class MyIntTT(TraitletType):
88 def validate(self, obj, value):
88 def validate(self, obj, value):
89 if isinstance(value, int):
89 if isinstance(value, int):
90 return value
90 return value
91 self.error(obj, value)
91 self.error(obj, value)
92 class A(HasTraitlets):
92 class A(HasTraitlets):
93 tt = MyIntTT(10)
93 tt = MyIntTT(10)
94 a = A()
94 a = A()
95 self.assertEquals(a.tt, 10)
95 self.assertEquals(a.tt, 10)
96
96
97 # Defaults are validated when the HasTraitlets is instantiated
97 # Defaults are validated when the HasTraitlets is instantiated
98 class B(HasTraitlets):
98 class B(HasTraitlets):
99 tt = MyIntTT('bad default')
99 tt = MyIntTT('bad default')
100 self.assertRaises(TraitletError, B)
100 self.assertRaises(TraitletError, B)
101
101
102 def test_is_valid_for(self):
102 def test_is_valid_for(self):
103 class MyTT(TraitletType):
103 class MyTT(TraitletType):
104 def is_valid_for(self, value):
104 def is_valid_for(self, value):
105 return True
105 return True
106 class A(HasTraitlets):
106 class A(HasTraitlets):
107 tt = MyTT
107 tt = MyTT
108
108
109 a = A()
109 a = A()
110 a.tt = 10
110 a.tt = 10
111 self.assertEquals(a.tt, 10)
111 self.assertEquals(a.tt, 10)
112
112
113 def test_value_for(self):
113 def test_value_for(self):
114 class MyTT(TraitletType):
114 class MyTT(TraitletType):
115 def value_for(self, value):
115 def value_for(self, value):
116 return 20
116 return 20
117 class A(HasTraitlets):
117 class A(HasTraitlets):
118 tt = MyTT
118 tt = MyTT
119
119
120 a = A()
120 a = A()
121 a.tt = 10
121 a.tt = 10
122 self.assertEquals(a.tt, 20)
122 self.assertEquals(a.tt, 20)
123
123
124 def test_info(self):
124 def test_info(self):
125 class A(HasTraitlets):
125 class A(HasTraitlets):
126 tt = TraitletType
126 tt = TraitletType
127 a = A()
127 a = A()
128 self.assertEquals(A.tt.info(), 'any value')
128 self.assertEquals(A.tt.info(), 'any value')
129
129
130 def test_error(self):
130 def test_error(self):
131 class A(HasTraitlets):
131 class A(HasTraitlets):
132 tt = TraitletType
132 tt = TraitletType
133 a = A()
133 a = A()
134 self.assertRaises(TraitletError, A.tt.error, a, 10)
134 self.assertRaises(TraitletError, A.tt.error, a, 10)
135
135
136
136
137 class TestHasTraitletsMeta(TestCase):
137 class TestHasTraitletsMeta(TestCase):
138
138
139 def test_metaclass(self):
139 def test_metaclass(self):
140 self.assertEquals(type(HasTraitlets), MetaHasTraitlets)
140 self.assertEquals(type(HasTraitlets), MetaHasTraitlets)
141
141
142 class A(HasTraitlets):
142 class A(HasTraitlets):
143 a = Int
143 a = Int
144
144
145 a = A()
145 a = A()
146 self.assertEquals(type(a.__class__), MetaHasTraitlets)
146 self.assertEquals(type(a.__class__), MetaHasTraitlets)
147 self.assertEquals(a.a,0)
147 self.assertEquals(a.a,0)
148 a.a = 10
148 a.a = 10
149 self.assertEquals(a.a,10)
149 self.assertEquals(a.a,10)
150
150
151 class B(HasTraitlets):
151 class B(HasTraitlets):
152 b = Int()
152 b = Int()
153
153
154 b = B()
154 b = B()
155 self.assertEquals(b.b,0)
155 self.assertEquals(b.b,0)
156 b.b = 10
156 b.b = 10
157 self.assertEquals(b.b,10)
157 self.assertEquals(b.b,10)
158
158
159 class C(HasTraitlets):
159 class C(HasTraitlets):
160 c = Int(30)
160 c = Int(30)
161
161
162 c = C()
162 c = C()
163 self.assertEquals(c.c,30)
163 self.assertEquals(c.c,30)
164 c.c = 10
164 c.c = 10
165 self.assertEquals(c.c,10)
165 self.assertEquals(c.c,10)
166
166
167 def test_this_class(self):
167 def test_this_class(self):
168 class A(HasTraitlets):
168 class A(HasTraitlets):
169 t = This()
169 t = This()
170 tt = This()
170 tt = This()
171 class B(A):
171 class B(A):
172 tt = This()
172 tt = This()
173 ttt = This()
173 ttt = This()
174 self.assertEquals(A.t.this_class, A)
174 self.assertEquals(A.t.this_class, A)
175 self.assertEquals(B.t.this_class, A)
175 self.assertEquals(B.t.this_class, A)
176 self.assertEquals(B.tt.this_class, B)
176 self.assertEquals(B.tt.this_class, B)
177 self.assertEquals(B.ttt.this_class, B)
177 self.assertEquals(B.ttt.this_class, B)
178
178
179 class TestHasTraitletsNotify(TestCase):
179 class TestHasTraitletsNotify(TestCase):
180
180
181 def setUp(self):
181 def setUp(self):
182 self._notify1 = []
182 self._notify1 = []
183 self._notify2 = []
183 self._notify2 = []
184
184
185 def notify1(self, name, old, new):
185 def notify1(self, name, old, new):
186 self._notify1.append((name, old, new))
186 self._notify1.append((name, old, new))
187
187
188 def notify2(self, name, old, new):
188 def notify2(self, name, old, new):
189 self._notify2.append((name, old, new))
189 self._notify2.append((name, old, new))
190
190
191 def test_notify_all(self):
191 def test_notify_all(self):
192
192
193 class A(HasTraitlets):
193 class A(HasTraitlets):
194 a = Int
194 a = Int
195 b = Float
195 b = Float
196
196
197 a = A()
197 a = A()
198 a.on_traitlet_change(self.notify1)
198 a.on_traitlet_change(self.notify1)
199 a.a = 0
199 a.a = 0
200 self.assertEquals(len(self._notify1),0)
200 self.assertEquals(len(self._notify1),0)
201 a.b = 0.0
201 a.b = 0.0
202 self.assertEquals(len(self._notify1),0)
202 self.assertEquals(len(self._notify1),0)
203 a.a = 10
203 a.a = 10
204 self.assert_(('a',0,10) in self._notify1)
204 self.assert_(('a',0,10) in self._notify1)
205 a.b = 10.0
205 a.b = 10.0
206 self.assert_(('b',0.0,10.0) in self._notify1)
206 self.assert_(('b',0.0,10.0) in self._notify1)
207 self.assertRaises(TraitletError,setattr,a,'a','bad string')
207 self.assertRaises(TraitletError,setattr,a,'a','bad string')
208 self.assertRaises(TraitletError,setattr,a,'b','bad string')
208 self.assertRaises(TraitletError,setattr,a,'b','bad string')
209 self._notify1 = []
209 self._notify1 = []
210 a.on_traitlet_change(self.notify1,remove=True)
210 a.on_traitlet_change(self.notify1,remove=True)
211 a.a = 20
211 a.a = 20
212 a.b = 20.0
212 a.b = 20.0
213 self.assertEquals(len(self._notify1),0)
213 self.assertEquals(len(self._notify1),0)
214
214
215 def test_notify_one(self):
215 def test_notify_one(self):
216
216
217 class A(HasTraitlets):
217 class A(HasTraitlets):
218 a = Int
218 a = Int
219 b = Float
219 b = Float
220
220
221 a = A()
221 a = A()
222 a.on_traitlet_change(self.notify1, 'a')
222 a.on_traitlet_change(self.notify1, 'a')
223 a.a = 0
223 a.a = 0
224 self.assertEquals(len(self._notify1),0)
224 self.assertEquals(len(self._notify1),0)
225 a.a = 10
225 a.a = 10
226 self.assert_(('a',0,10) in self._notify1)
226 self.assert_(('a',0,10) in self._notify1)
227 self.assertRaises(TraitletError,setattr,a,'a','bad string')
227 self.assertRaises(TraitletError,setattr,a,'a','bad string')
228
228
229 def test_subclass(self):
229 def test_subclass(self):
230
230
231 class A(HasTraitlets):
231 class A(HasTraitlets):
232 a = Int
232 a = Int
233
233
234 class B(A):
234 class B(A):
235 b = Float
235 b = Float
236
236
237 b = B()
237 b = B()
238 self.assertEquals(b.a,0)
238 self.assertEquals(b.a,0)
239 self.assertEquals(b.b,0.0)
239 self.assertEquals(b.b,0.0)
240 b.a = 100
240 b.a = 100
241 b.b = 100.0
241 b.b = 100.0
242 self.assertEquals(b.a,100)
242 self.assertEquals(b.a,100)
243 self.assertEquals(b.b,100.0)
243 self.assertEquals(b.b,100.0)
244
244
245 def test_notify_subclass(self):
245 def test_notify_subclass(self):
246
246
247 class A(HasTraitlets):
247 class A(HasTraitlets):
248 a = Int
248 a = Int
249
249
250 class B(A):
250 class B(A):
251 b = Float
251 b = Float
252
252
253 b = B()
253 b = B()
254 b.on_traitlet_change(self.notify1, 'a')
254 b.on_traitlet_change(self.notify1, 'a')
255 b.on_traitlet_change(self.notify2, 'b')
255 b.on_traitlet_change(self.notify2, 'b')
256 b.a = 0
256 b.a = 0
257 b.b = 0.0
257 b.b = 0.0
258 self.assertEquals(len(self._notify1),0)
258 self.assertEquals(len(self._notify1),0)
259 self.assertEquals(len(self._notify2),0)
259 self.assertEquals(len(self._notify2),0)
260 b.a = 10
260 b.a = 10
261 b.b = 10.0
261 b.b = 10.0
262 self.assert_(('a',0,10) in self._notify1)
262 self.assert_(('a',0,10) in self._notify1)
263 self.assert_(('b',0.0,10.0) in self._notify2)
263 self.assert_(('b',0.0,10.0) in self._notify2)
264
264
265 def test_static_notify(self):
265 def test_static_notify(self):
266
266
267 class A(HasTraitlets):
267 class A(HasTraitlets):
268 a = Int
268 a = Int
269 _notify1 = []
269 _notify1 = []
270 def _a_changed(self, name, old, new):
270 def _a_changed(self, name, old, new):
271 self._notify1.append((name, old, new))
271 self._notify1.append((name, old, new))
272
272
273 a = A()
273 a = A()
274 a.a = 0
274 a.a = 0
275 # This is broken!!!
275 # This is broken!!!
276 self.assertEquals(len(a._notify1),0)
276 self.assertEquals(len(a._notify1),0)
277 a.a = 10
277 a.a = 10
278 self.assert_(('a',0,10) in a._notify1)
278 self.assert_(('a',0,10) in a._notify1)
279
279
280 class B(A):
280 class B(A):
281 b = Float
281 b = Float
282 _notify2 = []
282 _notify2 = []
283 def _b_changed(self, name, old, new):
283 def _b_changed(self, name, old, new):
284 self._notify2.append((name, old, new))
284 self._notify2.append((name, old, new))
285
285
286 b = B()
286 b = B()
287 b.a = 10
287 b.a = 10
288 b.b = 10.0
288 b.b = 10.0
289 self.assert_(('a',0,10) in b._notify1)
289 self.assert_(('a',0,10) in b._notify1)
290 self.assert_(('b',0.0,10.0) in b._notify2)
290 self.assert_(('b',0.0,10.0) in b._notify2)
291
291
292 def test_notify_args(self):
292 def test_notify_args(self):
293
293
294 def callback0():
294 def callback0():
295 self.cb = ()
295 self.cb = ()
296 def callback1(name):
296 def callback1(name):
297 self.cb = (name,)
297 self.cb = (name,)
298 def callback2(name, new):
298 def callback2(name, new):
299 self.cb = (name, new)
299 self.cb = (name, new)
300 def callback3(name, old, new):
300 def callback3(name, old, new):
301 self.cb = (name, old, new)
301 self.cb = (name, old, new)
302
302
303 class A(HasTraitlets):
303 class A(HasTraitlets):
304 a = Int
304 a = Int
305
305
306 a = A()
306 a = A()
307 a.on_traitlet_change(callback0, 'a')
307 a.on_traitlet_change(callback0, 'a')
308 a.a = 10
308 a.a = 10
309 self.assertEquals(self.cb,())
309 self.assertEquals(self.cb,())
310 a.on_traitlet_change(callback0, 'a', remove=True)
310 a.on_traitlet_change(callback0, 'a', remove=True)
311
311
312 a.on_traitlet_change(callback1, 'a')
312 a.on_traitlet_change(callback1, 'a')
313 a.a = 100
313 a.a = 100
314 self.assertEquals(self.cb,('a',))
314 self.assertEquals(self.cb,('a',))
315 a.on_traitlet_change(callback1, 'a', remove=True)
315 a.on_traitlet_change(callback1, 'a', remove=True)
316
316
317 a.on_traitlet_change(callback2, 'a')
317 a.on_traitlet_change(callback2, 'a')
318 a.a = 1000
318 a.a = 1000
319 self.assertEquals(self.cb,('a',1000))
319 self.assertEquals(self.cb,('a',1000))
320 a.on_traitlet_change(callback2, 'a', remove=True)
320 a.on_traitlet_change(callback2, 'a', remove=True)
321
321
322 a.on_traitlet_change(callback3, 'a')
322 a.on_traitlet_change(callback3, 'a')
323 a.a = 10000
323 a.a = 10000
324 self.assertEquals(self.cb,('a',1000,10000))
324 self.assertEquals(self.cb,('a',1000,10000))
325 a.on_traitlet_change(callback3, 'a', remove=True)
325 a.on_traitlet_change(callback3, 'a', remove=True)
326
326
327 self.assertEquals(len(a._traitlet_notifiers['a']),0)
327 self.assertEquals(len(a._traitlet_notifiers['a']),0)
328
328
329
329
330 class TestHasTraitlets(TestCase):
330 class TestHasTraitlets(TestCase):
331
331
332 def test_traitlet_names(self):
332 def test_traitlet_names(self):
333 class A(HasTraitlets):
333 class A(HasTraitlets):
334 i = Int
334 i = Int
335 f = Float
335 f = Float
336 a = A()
336 a = A()
337 self.assertEquals(a.traitlet_names(),['i','f'])
337 self.assertEquals(a.traitlet_names(),['i','f'])
338
338
339 def test_traitlet_metadata(self):
339 def test_traitlet_metadata(self):
340 class A(HasTraitlets):
340 class A(HasTraitlets):
341 i = Int(config_key='MY_VALUE')
341 i = Int(config_key='MY_VALUE')
342 a = A()
342 a = A()
343 self.assertEquals(a.traitlet_metadata('i','config_key'), 'MY_VALUE')
343 self.assertEquals(a.traitlet_metadata('i','config_key'), 'MY_VALUE')
344
344
345 def test_traitlets(self):
345 def test_traitlets(self):
346 class A(HasTraitlets):
346 class A(HasTraitlets):
347 i = Int
347 i = Int
348 f = Float
348 f = Float
349 a = A()
349 a = A()
350 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f))
350 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f))
351
351
352 def test_traitlets_metadata(self):
352 def test_traitlets_metadata(self):
353 class A(HasTraitlets):
353 class A(HasTraitlets):
354 i = Int(config_key='VALUE1', other_thing='VALUE2')
354 i = Int(config_key='VALUE1', other_thing='VALUE2')
355 f = Float(config_key='VALUE3', other_thing='VALUE2')
355 f = Float(config_key='VALUE3', other_thing='VALUE2')
356 j = Int(0)
356 a = A()
357 a = A()
357 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f))
358 self.assertEquals(a.traitlets(), dict(i=A.i, f=A.f, j=A.j))
358 traitlets = a.traitlets(config_key=lambda v: True)
359 self.assertEquals(traitlets, dict(i=A.i, f=A.f))
360 traitlets = a.traitlets(config_key='VALUE1', other_thing='VALUE2')
359 traitlets = a.traitlets(config_key='VALUE1', other_thing='VALUE2')
361 self.assertEquals(traitlets, dict(i=A.i))
360 self.assertEquals(traitlets, dict(i=A.i))
362 traitlets = a.traitlets('config_key')
361
363 self.assertEquals(traitlets, dict(i=A.i, f=A.f))
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 # Tests for specific traitlet types
369 # Tests for specific traitlet types
367 #-----------------------------------------------------------------------------
370 #-----------------------------------------------------------------------------
368
371
369
372
370 class TestType(TestCase):
373 class TestType(TestCase):
371
374
372 def test_default(self):
375 def test_default(self):
373
376
374 class B(object): pass
377 class B(object): pass
375 class A(HasTraitlets):
378 class A(HasTraitlets):
376 klass = Type
379 klass = Type
377
380
378 a = A()
381 a = A()
379 self.assertEquals(a.klass, None)
382 self.assertEquals(a.klass, None)
380
383
381 a.klass = B
384 a.klass = B
382 self.assertEquals(a.klass, B)
385 self.assertEquals(a.klass, B)
383 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
386 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
384
387
385 def test_value(self):
388 def test_value(self):
386
389
387 class B(object): pass
390 class B(object): pass
388 class C(object): pass
391 class C(object): pass
389 class A(HasTraitlets):
392 class A(HasTraitlets):
390 klass = Type(B)
393 klass = Type(B)
391
394
392 a = A()
395 a = A()
393 self.assertEquals(a.klass, B)
396 self.assertEquals(a.klass, B)
394 self.assertRaises(TraitletError, setattr, a, 'klass', C)
397 self.assertRaises(TraitletError, setattr, a, 'klass', C)
395 self.assertRaises(TraitletError, setattr, a, 'klass', object)
398 self.assertRaises(TraitletError, setattr, a, 'klass', object)
396 a.klass = B
399 a.klass = B
397
400
398 def test_allow_none(self):
401 def test_allow_none(self):
399
402
400 class B(object): pass
403 class B(object): pass
401 class C(B): pass
404 class C(B): pass
402 class A(HasTraitlets):
405 class A(HasTraitlets):
403 klass = Type(B, allow_none=False)
406 klass = Type(B, allow_none=False)
404
407
405 a = A()
408 a = A()
406 self.assertEquals(a.klass, B)
409 self.assertEquals(a.klass, B)
407 self.assertRaises(TraitletError, setattr, a, 'klass', None)
410 self.assertRaises(TraitletError, setattr, a, 'klass', None)
408 a.klass = C
411 a.klass = C
409 self.assertEquals(a.klass, C)
412 self.assertEquals(a.klass, C)
410
413
411 def test_validate_klass(self):
414 def test_validate_klass(self):
412
415
413 class A(HasTraitlets):
416 class A(HasTraitlets):
414 klass = Type('no strings allowed')
417 klass = Type('no strings allowed')
415
418
416 self.assertRaises(ImportError, A)
419 self.assertRaises(ImportError, A)
417
420
418 class A(HasTraitlets):
421 class A(HasTraitlets):
419 klass = Type('rub.adub.Duck')
422 klass = Type('rub.adub.Duck')
420
423
421 self.assertRaises(ImportError, A)
424 self.assertRaises(ImportError, A)
422
425
423 def test_validate_default(self):
426 def test_validate_default(self):
424
427
425 class B(object): pass
428 class B(object): pass
426 class A(HasTraitlets):
429 class A(HasTraitlets):
427 klass = Type('bad default', B)
430 klass = Type('bad default', B)
428
431
429 self.assertRaises(ImportError, A)
432 self.assertRaises(ImportError, A)
430
433
431 class C(HasTraitlets):
434 class C(HasTraitlets):
432 klass = Type(None, B, allow_none=False)
435 klass = Type(None, B, allow_none=False)
433
436
434 self.assertRaises(TraitletError, C)
437 self.assertRaises(TraitletError, C)
435
438
436 def test_str_klass(self):
439 def test_str_klass(self):
437
440
438 class A(HasTraitlets):
441 class A(HasTraitlets):
439 klass = Type('IPython.utils.ipstruct.Struct')
442 klass = Type('IPython.utils.ipstruct.Struct')
440
443
441 from IPython.utils.ipstruct import Struct
444 from IPython.utils.ipstruct import Struct
442 a = A()
445 a = A()
443 a.klass = Struct
446 a.klass = Struct
444 self.assertEquals(a.klass, Struct)
447 self.assertEquals(a.klass, Struct)
445
448
446 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
449 self.assertRaises(TraitletError, setattr, a, 'klass', 10)
447
450
448 class TestInstance(TestCase):
451 class TestInstance(TestCase):
449
452
450 def test_basic(self):
453 def test_basic(self):
451 class Foo(object): pass
454 class Foo(object): pass
452 class Bar(Foo): pass
455 class Bar(Foo): pass
453 class Bah(object): pass
456 class Bah(object): pass
454
457
455 class A(HasTraitlets):
458 class A(HasTraitlets):
456 inst = Instance(Foo)
459 inst = Instance(Foo)
457
460
458 a = A()
461 a = A()
459 self.assert_(a.inst is None)
462 self.assert_(a.inst is None)
460 a.inst = Foo()
463 a.inst = Foo()
461 self.assert_(isinstance(a.inst, Foo))
464 self.assert_(isinstance(a.inst, Foo))
462 a.inst = Bar()
465 a.inst = Bar()
463 self.assert_(isinstance(a.inst, Foo))
466 self.assert_(isinstance(a.inst, Foo))
464 self.assertRaises(TraitletError, setattr, a, 'inst', Foo)
467 self.assertRaises(TraitletError, setattr, a, 'inst', Foo)
465 self.assertRaises(TraitletError, setattr, a, 'inst', Bar)
468 self.assertRaises(TraitletError, setattr, a, 'inst', Bar)
466 self.assertRaises(TraitletError, setattr, a, 'inst', Bah())
469 self.assertRaises(TraitletError, setattr, a, 'inst', Bah())
467
470
468 def test_unique_default_value(self):
471 def test_unique_default_value(self):
469 class Foo(object): pass
472 class Foo(object): pass
470 class A(HasTraitlets):
473 class A(HasTraitlets):
471 inst = Instance(Foo,(),{})
474 inst = Instance(Foo,(),{})
472
475
473 a = A()
476 a = A()
474 b = A()
477 b = A()
475 self.assert_(a.inst is not b.inst)
478 self.assert_(a.inst is not b.inst)
476
479
477 def test_args_kw(self):
480 def test_args_kw(self):
478 class Foo(object):
481 class Foo(object):
479 def __init__(self, c): self.c = c
482 def __init__(self, c): self.c = c
480 class Bar(object): pass
483 class Bar(object): pass
481 class Bah(object):
484 class Bah(object):
482 def __init__(self, c, d):
485 def __init__(self, c, d):
483 self.c = c; self.d = d
486 self.c = c; self.d = d
484
487
485 class A(HasTraitlets):
488 class A(HasTraitlets):
486 inst = Instance(Foo, (10,))
489 inst = Instance(Foo, (10,))
487 a = A()
490 a = A()
488 self.assertEquals(a.inst.c, 10)
491 self.assertEquals(a.inst.c, 10)
489
492
490 class B(HasTraitlets):
493 class B(HasTraitlets):
491 inst = Instance(Bah, args=(10,), kw=dict(d=20))
494 inst = Instance(Bah, args=(10,), kw=dict(d=20))
492 b = B()
495 b = B()
493 self.assertEquals(b.inst.c, 10)
496 self.assertEquals(b.inst.c, 10)
494 self.assertEquals(b.inst.d, 20)
497 self.assertEquals(b.inst.d, 20)
495
498
496 class C(HasTraitlets):
499 class C(HasTraitlets):
497 inst = Instance(Foo)
500 inst = Instance(Foo)
498 c = C()
501 c = C()
499 self.assert_(c.inst is None)
502 self.assert_(c.inst is None)
500
503
501 def test_bad_default(self):
504 def test_bad_default(self):
502 class Foo(object): pass
505 class Foo(object): pass
503
506
504 class A(HasTraitlets):
507 class A(HasTraitlets):
505 inst = Instance(Foo, allow_none=False)
508 inst = Instance(Foo, allow_none=False)
506
509
507 self.assertRaises(TraitletError, A)
510 self.assertRaises(TraitletError, A)
508
511
509 def test_instance(self):
512 def test_instance(self):
510 class Foo(object): pass
513 class Foo(object): pass
511
514
512 def inner():
515 def inner():
513 class A(HasTraitlets):
516 class A(HasTraitlets):
514 inst = Instance(Foo())
517 inst = Instance(Foo())
515
518
516 self.assertRaises(TraitletError, inner)
519 self.assertRaises(TraitletError, inner)
517
520
518
521
519 class TestThis(TestCase):
522 class TestThis(TestCase):
520
523
521 def test_this_class(self):
524 def test_this_class(self):
522 class Foo(HasTraitlets):
525 class Foo(HasTraitlets):
523 this = This
526 this = This
524
527
525 f = Foo()
528 f = Foo()
526 self.assertEquals(f.this, None)
529 self.assertEquals(f.this, None)
527 g = Foo()
530 g = Foo()
528 f.this = g
531 f.this = g
529 self.assertEquals(f.this, g)
532 self.assertEquals(f.this, g)
530 self.assertRaises(TraitletError, setattr, f, 'this', 10)
533 self.assertRaises(TraitletError, setattr, f, 'this', 10)
531
534
532 def test_this_inst(self):
535 def test_this_inst(self):
533 class Foo(HasTraitlets):
536 class Foo(HasTraitlets):
534 this = This()
537 this = This()
535
538
536 f = Foo()
539 f = Foo()
537 f.this = Foo()
540 f.this = Foo()
538 self.assert_(isinstance(f.this, Foo))
541 self.assert_(isinstance(f.this, Foo))
539
542
540 def test_subclass(self):
543 def test_subclass(self):
541 class Foo(HasTraitlets):
544 class Foo(HasTraitlets):
542 t = This()
545 t = This()
543 class Bar(Foo):
546 class Bar(Foo):
544 pass
547 pass
545 f = Foo()
548 f = Foo()
546 b = Bar()
549 b = Bar()
547 f.t = b
550 f.t = b
548 b.t = f
551 b.t = f
549 self.assertEquals(f.t, b)
552 self.assertEquals(f.t, b)
550 self.assertEquals(b.t, f)
553 self.assertEquals(b.t, f)
551
554
552 def test_subclass_override(self):
555 def test_subclass_override(self):
553 class Foo(HasTraitlets):
556 class Foo(HasTraitlets):
554 t = This()
557 t = This()
555 class Bar(Foo):
558 class Bar(Foo):
556 t = This()
559 t = This()
557 f = Foo()
560 f = Foo()
558 b = Bar()
561 b = Bar()
559 f.t = b
562 f.t = b
560 self.assertEquals(f.t, b)
563 self.assertEquals(f.t, b)
561 self.assertRaises(TraitletError, setattr, b, 't', f)
564 self.assertRaises(TraitletError, setattr, b, 't', f)
562
565
563 class TraitletTestBase(TestCase):
566 class TraitletTestBase(TestCase):
564 """A best testing class for basic traitlet types."""
567 """A best testing class for basic traitlet types."""
565
568
566 def assign(self, value):
569 def assign(self, value):
567 self.obj.value = value
570 self.obj.value = value
568
571
569 def coerce(self, value):
572 def coerce(self, value):
570 return value
573 return value
571
574
572 def test_good_values(self):
575 def test_good_values(self):
573 if hasattr(self, '_good_values'):
576 if hasattr(self, '_good_values'):
574 for value in self._good_values:
577 for value in self._good_values:
575 self.assign(value)
578 self.assign(value)
576 self.assertEquals(self.obj.value, self.coerce(value))
579 self.assertEquals(self.obj.value, self.coerce(value))
577
580
578 def test_bad_values(self):
581 def test_bad_values(self):
579 if hasattr(self, '_bad_values'):
582 if hasattr(self, '_bad_values'):
580 for value in self._bad_values:
583 for value in self._bad_values:
581 self.assertRaises(TraitletError, self.assign, value)
584 self.assertRaises(TraitletError, self.assign, value)
582
585
583 def test_default_value(self):
586 def test_default_value(self):
584 if hasattr(self, '_default_value'):
587 if hasattr(self, '_default_value'):
585 self.assertEquals(self._default_value, self.obj.value)
588 self.assertEquals(self._default_value, self.obj.value)
586
589
587
590
588 class AnyTraitlet(HasTraitlets):
591 class AnyTraitlet(HasTraitlets):
589
592
590 value = Any
593 value = Any
591
594
592 class AnyTraitTest(TraitletTestBase):
595 class AnyTraitTest(TraitletTestBase):
593
596
594 obj = AnyTraitlet()
597 obj = AnyTraitlet()
595
598
596 _default_value = None
599 _default_value = None
597 _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j]
600 _good_values = [10.0, 'ten', u'ten', [10], {'ten': 10},(10,), None, 1j]
598 _bad_values = []
601 _bad_values = []
599
602
600
603
601 class IntTraitlet(HasTraitlets):
604 class IntTraitlet(HasTraitlets):
602
605
603 value = Int(99)
606 value = Int(99)
604
607
605 class TestInt(TraitletTestBase):
608 class TestInt(TraitletTestBase):
606
609
607 obj = IntTraitlet()
610 obj = IntTraitlet()
608 _default_value = 99
611 _default_value = 99
609 _good_values = [10, -10]
612 _good_values = [10, -10]
610 _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L,
613 _bad_values = ['ten', u'ten', [10], {'ten': 10},(10,), None, 1j, 10L,
611 -10L, 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L',
614 -10L, 10.1, -10.1, '10L', '-10L', '10.1', '-10.1', u'10L',
612 u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10']
615 u'-10L', u'10.1', u'-10.1', '10', '-10', u'10', u'-10']
613
616
614
617
615 class LongTraitlet(HasTraitlets):
618 class LongTraitlet(HasTraitlets):
616
619
617 value = Long(99L)
620 value = Long(99L)
618
621
619 class TestLong(TraitletTestBase):
622 class TestLong(TraitletTestBase):
620
623
621 obj = LongTraitlet()
624 obj = LongTraitlet()
622
625
623 _default_value = 99L
626 _default_value = 99L
624 _good_values = [10, -10, 10L, -10L]
627 _good_values = [10, -10, 10L, -10L]
625 _bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,),
628 _bad_values = ['ten', u'ten', [10], [10l], {'ten': 10},(10,),(10L,),
626 None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1',
629 None, 1j, 10.1, -10.1, '10', '-10', '10L', '-10L', '10.1',
627 '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1',
630 '-10.1', u'10', u'-10', u'10L', u'-10L', u'10.1',
628 u'-10.1']
631 u'-10.1']
629
632
630
633
631 class FloatTraitlet(HasTraitlets):
634 class FloatTraitlet(HasTraitlets):
632
635
633 value = Float(99.0)
636 value = Float(99.0)
634
637
635 class TestFloat(TraitletTestBase):
638 class TestFloat(TraitletTestBase):
636
639
637 obj = FloatTraitlet()
640 obj = FloatTraitlet()
638
641
639 _default_value = 99.0
642 _default_value = 99.0
640 _good_values = [10, -10, 10.1, -10.1]
643 _good_values = [10, -10, 10.1, -10.1]
641 _bad_values = [10L, -10L, 'ten', u'ten', [10], {'ten': 10},(10,), None,
644 _bad_values = [10L, -10L, 'ten', u'ten', [10], {'ten': 10},(10,), None,
642 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10',
645 1j, '10', '-10', '10L', '-10L', '10.1', '-10.1', u'10',
643 u'-10', u'10L', u'-10L', u'10.1', u'-10.1']
646 u'-10', u'10L', u'-10L', u'10.1', u'-10.1']
644
647
645
648
646 class ComplexTraitlet(HasTraitlets):
649 class ComplexTraitlet(HasTraitlets):
647
650
648 value = Complex(99.0-99.0j)
651 value = Complex(99.0-99.0j)
649
652
650 class TestComplex(TraitletTestBase):
653 class TestComplex(TraitletTestBase):
651
654
652 obj = ComplexTraitlet()
655 obj = ComplexTraitlet()
653
656
654 _default_value = 99.0-99.0j
657 _default_value = 99.0-99.0j
655 _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j,
658 _good_values = [10, -10, 10.1, -10.1, 10j, 10+10j, 10-10j,
656 10.1j, 10.1+10.1j, 10.1-10.1j]
659 10.1j, 10.1+10.1j, 10.1-10.1j]
657 _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None]
660 _bad_values = [10L, -10L, u'10L', u'-10L', 'ten', [10], {'ten': 10},(10,), None]
658
661
659
662
660 class StringTraitlet(HasTraitlets):
663 class StringTraitlet(HasTraitlets):
661
664
662 value = Str('string')
665 value = Str('string')
663
666
664 class TestString(TraitletTestBase):
667 class TestString(TraitletTestBase):
665
668
666 obj = StringTraitlet()
669 obj = StringTraitlet()
667
670
668 _default_value = 'string'
671 _default_value = 'string'
669 _good_values = ['10', '-10', '10L',
672 _good_values = ['10', '-10', '10L',
670 '-10L', '10.1', '-10.1', 'string']
673 '-10L', '10.1', '-10.1', 'string']
671 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10],
674 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j, [10],
672 ['ten'],{'ten': 10},(10,), None, u'string']
675 ['ten'],{'ten': 10},(10,), None, u'string']
673
676
674
677
675 class UnicodeTraitlet(HasTraitlets):
678 class UnicodeTraitlet(HasTraitlets):
676
679
677 value = Unicode(u'unicode')
680 value = Unicode(u'unicode')
678
681
679 class TestUnicode(TraitletTestBase):
682 class TestUnicode(TraitletTestBase):
680
683
681 obj = UnicodeTraitlet()
684 obj = UnicodeTraitlet()
682
685
683 _default_value = u'unicode'
686 _default_value = u'unicode'
684 _good_values = ['10', '-10', '10L', '-10L', '10.1',
687 _good_values = ['10', '-10', '10L', '-10L', '10.1',
685 '-10.1', '', u'', 'string', u'string', ]
688 '-10.1', '', u'', 'string', u'string', ]
686 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j,
689 _bad_values = [10, -10, 10L, -10L, 10.1, -10.1, 1j,
687 [10], ['ten'], [u'ten'], {'ten': 10},(10,), None]
690 [10], ['ten'], [u'ten'], {'ten': 10},(10,), None]
@@ -1,974 +1,977 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 A lightweight Traits like module.
4 A lightweight Traits like module.
5
5
6 This is designed to provide a lightweight, simple, pure Python version of
6 This is designed to provide a lightweight, simple, pure Python version of
7 many of the capabilities of enthought.traits. This includes:
7 many of the capabilities of enthought.traits. This includes:
8
8
9 * Validation
9 * Validation
10 * Type specification with defaults
10 * Type specification with defaults
11 * Static and dynamic notification
11 * Static and dynamic notification
12 * Basic predefined types
12 * Basic predefined types
13 * An API that is similar to enthought.traits
13 * An API that is similar to enthought.traits
14
14
15 We don't support:
15 We don't support:
16
16
17 * Delegation
17 * Delegation
18 * Automatic GUI generation
18 * Automatic GUI generation
19 * A full set of trait types. Most importantly, we don't provide container
19 * A full set of trait types. Most importantly, we don't provide container
20 traitlets (list, dict, tuple) that can trigger notifications if their
20 traitlets (list, dict, tuple) that can trigger notifications if their
21 contents change.
21 contents change.
22 * API compatibility with enthought.traits
22 * API compatibility with enthought.traits
23
23
24 There are also some important difference in our design:
24 There are also some important difference in our design:
25
25
26 * enthought.traits does not validate default values. We do.
26 * enthought.traits does not validate default values. We do.
27
27
28 We choose to create this module because we need these capabilities, but
28 We choose to create this module because we need these capabilities, but
29 we need them to be pure Python so they work in all Python implementations,
29 we need them to be pure Python so they work in all Python implementations,
30 including Jython and IronPython.
30 including Jython and IronPython.
31
31
32 Authors:
32 Authors:
33
33
34 * Brian Granger
34 * Brian Granger
35 * Enthought, Inc. Some of the code in this file comes from enthought.traits
35 * Enthought, Inc. Some of the code in this file comes from enthought.traits
36 and is licensed under the BSD license. Also, many of the ideas also come
36 and is licensed under the BSD license. Also, many of the ideas also come
37 from enthought.traits even though our implementation is very different.
37 from enthought.traits even though our implementation is very different.
38 """
38 """
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Copyright (C) 2008-2009 The IPython Development Team
41 # Copyright (C) 2008-2009 The IPython Development Team
42 #
42 #
43 # Distributed under the terms of the BSD License. The full license is in
43 # Distributed under the terms of the BSD License. The full license is in
44 # the file COPYING, distributed as part of this software.
44 # the file COPYING, distributed as part of this software.
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48 # Imports
48 # Imports
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50
50
51
51
52 import inspect
52 import inspect
53 import sys
53 import sys
54 import types
54 import types
55 from types import (
55 from types import (
56 InstanceType, ClassType, FunctionType,
56 InstanceType, ClassType, FunctionType,
57 ListType, TupleType
57 ListType, TupleType
58 )
58 )
59
59
60 from IPython.utils.importstring import import_item
60 from IPython.utils.importstring import import_item
61
61
62 ClassTypes = (ClassType, type)
62 ClassTypes = (ClassType, type)
63
63
64 SequenceTypes = (ListType, TupleType)
64 SequenceTypes = (ListType, TupleType)
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Basic classes
67 # Basic classes
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70
70
71 class NoDefaultSpecified ( object ): pass
71 class NoDefaultSpecified ( object ): pass
72 NoDefaultSpecified = NoDefaultSpecified()
72 NoDefaultSpecified = NoDefaultSpecified()
73
73
74
74
75 class Undefined ( object ): pass
75 class Undefined ( object ): pass
76 Undefined = Undefined()
76 Undefined = Undefined()
77
77
78
78
79 class TraitletError(Exception):
79 class TraitletError(Exception):
80 pass
80 pass
81
81
82
82
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84 # Utilities
84 # Utilities
85 #-----------------------------------------------------------------------------
85 #-----------------------------------------------------------------------------
86
86
87
87
88 def class_of ( object ):
88 def class_of ( object ):
89 """ Returns a string containing the class name of an object with the
89 """ Returns a string containing the class name of an object with the
90 correct indefinite article ('a' or 'an') preceding it (e.g., 'an Image',
90 correct indefinite article ('a' or 'an') preceding it (e.g., 'an Image',
91 'a PlotValue').
91 'a PlotValue').
92 """
92 """
93 if isinstance( object, basestring ):
93 if isinstance( object, basestring ):
94 return add_article( object )
94 return add_article( object )
95
95
96 return add_article( object.__class__.__name__ )
96 return add_article( object.__class__.__name__ )
97
97
98
98
99 def add_article ( name ):
99 def add_article ( name ):
100 """ Returns a string containing the correct indefinite article ('a' or 'an')
100 """ Returns a string containing the correct indefinite article ('a' or 'an')
101 prefixed to the specified string.
101 prefixed to the specified string.
102 """
102 """
103 if name[:1].lower() in 'aeiou':
103 if name[:1].lower() in 'aeiou':
104 return 'an ' + name
104 return 'an ' + name
105
105
106 return 'a ' + name
106 return 'a ' + name
107
107
108
108
109 def repr_type(obj):
109 def repr_type(obj):
110 """ Return a string representation of a value and its type for readable
110 """ Return a string representation of a value and its type for readable
111 error messages.
111 error messages.
112 """
112 """
113 the_type = type(obj)
113 the_type = type(obj)
114 if the_type is InstanceType:
114 if the_type is InstanceType:
115 # Old-style class.
115 # Old-style class.
116 the_type = obj.__class__
116 the_type = obj.__class__
117 msg = '%r %r' % (obj, the_type)
117 msg = '%r %r' % (obj, the_type)
118 return msg
118 return msg
119
119
120
120
121 def parse_notifier_name(name):
121 def parse_notifier_name(name):
122 """Convert the name argument to a list of names.
122 """Convert the name argument to a list of names.
123
123
124 Examples
124 Examples
125 --------
125 --------
126
126
127 >>> parse_notifier_name('a')
127 >>> parse_notifier_name('a')
128 ['a']
128 ['a']
129 >>> parse_notifier_name(['a','b'])
129 >>> parse_notifier_name(['a','b'])
130 ['a', 'b']
130 ['a', 'b']
131 >>> parse_notifier_name(None)
131 >>> parse_notifier_name(None)
132 ['anytraitlet']
132 ['anytraitlet']
133 """
133 """
134 if isinstance(name, str):
134 if isinstance(name, str):
135 return [name]
135 return [name]
136 elif name is None:
136 elif name is None:
137 return ['anytraitlet']
137 return ['anytraitlet']
138 elif isinstance(name, (list, tuple)):
138 elif isinstance(name, (list, tuple)):
139 for n in name:
139 for n in name:
140 assert isinstance(n, str), "names must be strings"
140 assert isinstance(n, str), "names must be strings"
141 return name
141 return name
142
142
143
143
144 class _SimpleTest:
144 class _SimpleTest:
145 def __init__ ( self, value ): self.value = value
145 def __init__ ( self, value ): self.value = value
146 def __call__ ( self, test ):
146 def __call__ ( self, test ):
147 return test == self.value
147 return test == self.value
148 def __repr__(self):
148 def __repr__(self):
149 return "<SimpleTest(%r)" % self.value
149 return "<SimpleTest(%r)" % self.value
150 def __str__(self):
150 def __str__(self):
151 return self.__repr__()
151 return self.__repr__()
152
152
153
153
154 #-----------------------------------------------------------------------------
154 #-----------------------------------------------------------------------------
155 # Base TraitletType for all traitlets
155 # Base TraitletType for all traitlets
156 #-----------------------------------------------------------------------------
156 #-----------------------------------------------------------------------------
157
157
158
158
159 class TraitletType(object):
159 class TraitletType(object):
160 """A base class for all traitlet descriptors.
160 """A base class for all traitlet descriptors.
161
161
162 Notes
162 Notes
163 -----
163 -----
164 Our implementation of traitlets is based on Python's descriptor
164 Our implementation of traitlets is based on Python's descriptor
165 prototol. This class is the base class for all such descriptors. The
165 prototol. This class is the base class for all such descriptors. The
166 only magic we use is a custom metaclass for the main :class:`HasTraitlets`
166 only magic we use is a custom metaclass for the main :class:`HasTraitlets`
167 class that does the following:
167 class that does the following:
168
168
169 1. Sets the :attr:`name` attribute of every :class:`TraitletType`
169 1. Sets the :attr:`name` attribute of every :class:`TraitletType`
170 instance in the class dict to the name of the attribute.
170 instance in the class dict to the name of the attribute.
171 2. Sets the :attr:`this_class` attribute of every :class:`TraitletType`
171 2. Sets the :attr:`this_class` attribute of every :class:`TraitletType`
172 instance in the class dict to the *class* that declared the traitlet.
172 instance in the class dict to the *class* that declared the traitlet.
173 This is used by the :class:`This` traitlet to allow subclasses to
173 This is used by the :class:`This` traitlet to allow subclasses to
174 accept superclasses for :class:`This` values.
174 accept superclasses for :class:`This` values.
175 """
175 """
176
176
177
177
178 metadata = {}
178 metadata = {}
179 default_value = Undefined
179 default_value = Undefined
180 info_text = 'any value'
180 info_text = 'any value'
181
181
182 def __init__(self, default_value=NoDefaultSpecified, **metadata):
182 def __init__(self, default_value=NoDefaultSpecified, **metadata):
183 """Create a TraitletType.
183 """Create a TraitletType.
184 """
184 """
185 if default_value is not NoDefaultSpecified:
185 if default_value is not NoDefaultSpecified:
186 self.default_value = default_value
186 self.default_value = default_value
187
187
188 if len(metadata) > 0:
188 if len(metadata) > 0:
189 if len(self.metadata) > 0:
189 if len(self.metadata) > 0:
190 self._metadata = self.metadata.copy()
190 self._metadata = self.metadata.copy()
191 self._metadata.update(metadata)
191 self._metadata.update(metadata)
192 else:
192 else:
193 self._metadata = metadata
193 self._metadata = metadata
194 else:
194 else:
195 self._metadata = self.metadata
195 self._metadata = self.metadata
196
196
197 self.init()
197 self.init()
198
198
199 def init(self):
199 def init(self):
200 pass
200 pass
201
201
202 def get_default_value(self):
202 def get_default_value(self):
203 """Create a new instance of the default value."""
203 """Create a new instance of the default value."""
204 dv = self.default_value
204 dv = self.default_value
205 return dv
205 return dv
206
206
207 def instance_init(self, obj):
207 def instance_init(self, obj):
208 """This is called by :meth:`HasTraitlets.__new__` to finish init'ing.
208 """This is called by :meth:`HasTraitlets.__new__` to finish init'ing.
209
209
210 Some stages of initialization must be delayed until the parent
210 Some stages of initialization must be delayed until the parent
211 :class:`HasTraitlets` instance has been created. This method is
211 :class:`HasTraitlets` instance has been created. This method is
212 called in :meth:`HasTraitlets.__new__` after the instance has been
212 called in :meth:`HasTraitlets.__new__` after the instance has been
213 created.
213 created.
214
214
215 This method trigger the creation and validation of default values
215 This method trigger the creation and validation of default values
216 and also things like the resolution of str given class names in
216 and also things like the resolution of str given class names in
217 :class:`Type` and :class`Instance`.
217 :class:`Type` and :class`Instance`.
218
218
219 Parameters
219 Parameters
220 ----------
220 ----------
221 obj : :class:`HasTraitlets` instance
221 obj : :class:`HasTraitlets` instance
222 The parent :class:`HasTraitlets` instance that has just been
222 The parent :class:`HasTraitlets` instance that has just been
223 created.
223 created.
224 """
224 """
225 self.set_default_value(obj)
225 self.set_default_value(obj)
226
226
227 def set_default_value(self, obj):
227 def set_default_value(self, obj):
228 """Set the default value on a per instance basis.
228 """Set the default value on a per instance basis.
229
229
230 This method is called by :meth:`instance_init` to create and
230 This method is called by :meth:`instance_init` to create and
231 validate the default value. The creation and validation of
231 validate the default value. The creation and validation of
232 default values must be delayed until the parent :class:`HasTraitlets`
232 default values must be delayed until the parent :class:`HasTraitlets`
233 class has been instantiated.
233 class has been instantiated.
234 """
234 """
235 dv = self.get_default_value()
235 dv = self.get_default_value()
236 newdv = self._validate(obj, dv)
236 newdv = self._validate(obj, dv)
237 obj._traitlet_values[self.name] = newdv
237 obj._traitlet_values[self.name] = newdv
238
238
239 def __get__(self, obj, cls=None):
239 def __get__(self, obj, cls=None):
240 """Get the value of the traitlet by self.name for the instance.
240 """Get the value of the traitlet by self.name for the instance.
241
241
242 Default values are instantiated when :meth:`HasTraitlets.__new__`
242 Default values are instantiated when :meth:`HasTraitlets.__new__`
243 is called. Thus by the time this method gets called either the
243 is called. Thus by the time this method gets called either the
244 default value or a user defined value (they called :meth:`__set__`)
244 default value or a user defined value (they called :meth:`__set__`)
245 is in the :class:`HasTraitlets` instance.
245 is in the :class:`HasTraitlets` instance.
246 """
246 """
247 if obj is None:
247 if obj is None:
248 return self
248 return self
249 else:
249 else:
250 try:
250 try:
251 value = obj._traitlet_values[self.name]
251 value = obj._traitlet_values[self.name]
252 except:
252 except:
253 # HasTraitlets should call set_default_value to populate
253 # HasTraitlets should call set_default_value to populate
254 # this. So this should never be reached.
254 # this. So this should never be reached.
255 raise TraitletError('Unexpected error in TraitletType: '
255 raise TraitletError('Unexpected error in TraitletType: '
256 'default value not set properly')
256 'default value not set properly')
257 else:
257 else:
258 return value
258 return value
259
259
260 def __set__(self, obj, value):
260 def __set__(self, obj, value):
261 new_value = self._validate(obj, value)
261 new_value = self._validate(obj, value)
262 old_value = self.__get__(obj)
262 old_value = self.__get__(obj)
263 if old_value != new_value:
263 if old_value != new_value:
264 obj._traitlet_values[self.name] = new_value
264 obj._traitlet_values[self.name] = new_value
265 obj._notify_traitlet(self.name, old_value, new_value)
265 obj._notify_traitlet(self.name, old_value, new_value)
266
266
267 def _validate(self, obj, value):
267 def _validate(self, obj, value):
268 if hasattr(self, 'validate'):
268 if hasattr(self, 'validate'):
269 return self.validate(obj, value)
269 return self.validate(obj, value)
270 elif hasattr(self, 'is_valid_for'):
270 elif hasattr(self, 'is_valid_for'):
271 valid = self.is_valid_for(value)
271 valid = self.is_valid_for(value)
272 if valid:
272 if valid:
273 return value
273 return value
274 else:
274 else:
275 raise TraitletError('invalid value for type: %r' % value)
275 raise TraitletError('invalid value for type: %r' % value)
276 elif hasattr(self, 'value_for'):
276 elif hasattr(self, 'value_for'):
277 return self.value_for(value)
277 return self.value_for(value)
278 else:
278 else:
279 return value
279 return value
280
280
281 def info(self):
281 def info(self):
282 return self.info_text
282 return self.info_text
283
283
284 def error(self, obj, value):
284 def error(self, obj, value):
285 if obj is not None:
285 if obj is not None:
286 e = "The '%s' traitlet of %s instance must be %s, but a value of %s was specified." \
286 e = "The '%s' traitlet of %s instance must be %s, but a value of %s was specified." \
287 % (self.name, class_of(obj),
287 % (self.name, class_of(obj),
288 self.info(), repr_type(value))
288 self.info(), repr_type(value))
289 else:
289 else:
290 e = "The '%s' traitlet must be %s, but a value of %r was specified." \
290 e = "The '%s' traitlet must be %s, but a value of %r was specified." \
291 % (self.name, self.info(), repr_type(value))
291 % (self.name, self.info(), repr_type(value))
292 raise TraitletError(e)
292 raise TraitletError(e)
293
293
294 def get_metadata(self, key):
294 def get_metadata(self, key):
295 return getattr(self, '_metadata', {}).get(key, None)
295 return getattr(self, '_metadata', {}).get(key, None)
296
296
297 def set_metadata(self, key, value):
297 def set_metadata(self, key, value):
298 getattr(self, '_metadata', {})[key] = value
298 getattr(self, '_metadata', {})[key] = value
299
299
300
300
301 #-----------------------------------------------------------------------------
301 #-----------------------------------------------------------------------------
302 # The HasTraitlets implementation
302 # The HasTraitlets implementation
303 #-----------------------------------------------------------------------------
303 #-----------------------------------------------------------------------------
304
304
305
305
306 class MetaHasTraitlets(type):
306 class MetaHasTraitlets(type):
307 """A metaclass for HasTraitlets.
307 """A metaclass for HasTraitlets.
308
308
309 This metaclass makes sure that any TraitletType class attributes are
309 This metaclass makes sure that any TraitletType class attributes are
310 instantiated and sets their name attribute.
310 instantiated and sets their name attribute.
311 """
311 """
312
312
313 def __new__(mcls, name, bases, classdict):
313 def __new__(mcls, name, bases, classdict):
314 """Create the HasTraitlets class.
314 """Create the HasTraitlets class.
315
315
316 This instantiates all TraitletTypes in the class dict and sets their
316 This instantiates all TraitletTypes in the class dict and sets their
317 :attr:`name` attribute.
317 :attr:`name` attribute.
318 """
318 """
319 for k,v in classdict.iteritems():
319 for k,v in classdict.iteritems():
320 if isinstance(v, TraitletType):
320 if isinstance(v, TraitletType):
321 v.name = k
321 v.name = k
322 elif inspect.isclass(v):
322 elif inspect.isclass(v):
323 if issubclass(v, TraitletType):
323 if issubclass(v, TraitletType):
324 vinst = v()
324 vinst = v()
325 vinst.name = k
325 vinst.name = k
326 classdict[k] = vinst
326 classdict[k] = vinst
327 return super(MetaHasTraitlets, mcls).__new__(mcls, name, bases, classdict)
327 return super(MetaHasTraitlets, mcls).__new__(mcls, name, bases, classdict)
328
328
329 def __init__(cls, name, bases, classdict):
329 def __init__(cls, name, bases, classdict):
330 """Finish initializing the HasTraitlets class.
330 """Finish initializing the HasTraitlets class.
331
331
332 This sets the :attr:`this_class` attribute of each TraitletType in the
332 This sets the :attr:`this_class` attribute of each TraitletType in the
333 class dict to the newly created class ``cls``.
333 class dict to the newly created class ``cls``.
334 """
334 """
335 for k, v in classdict.iteritems():
335 for k, v in classdict.iteritems():
336 if isinstance(v, TraitletType):
336 if isinstance(v, TraitletType):
337 v.this_class = cls
337 v.this_class = cls
338 super(MetaHasTraitlets, cls).__init__(name, bases, classdict)
338 super(MetaHasTraitlets, cls).__init__(name, bases, classdict)
339
339
340 class HasTraitlets(object):
340 class HasTraitlets(object):
341
341
342 __metaclass__ = MetaHasTraitlets
342 __metaclass__ = MetaHasTraitlets
343
343
344 def __new__(cls, *args, **kw):
344 def __new__(cls, *args, **kw):
345 inst = super(HasTraitlets, cls).__new__(cls, *args, **kw)
345 inst = super(HasTraitlets, cls).__new__(cls, *args, **kw)
346 inst._traitlet_values = {}
346 inst._traitlet_values = {}
347 inst._traitlet_notifiers = {}
347 inst._traitlet_notifiers = {}
348 # Here we tell all the TraitletType instances to set their default
348 # Here we tell all the TraitletType instances to set their default
349 # values on the instance.
349 # values on the instance.
350 for key in dir(cls):
350 for key in dir(cls):
351 value = getattr(cls, key)
351 value = getattr(cls, key)
352 if isinstance(value, TraitletType):
352 if isinstance(value, TraitletType):
353 value.instance_init(inst)
353 value.instance_init(inst)
354 return inst
354 return inst
355
355
356 # def __init__(self):
356 # def __init__(self):
357 # self._traitlet_values = {}
357 # self._traitlet_values = {}
358 # self._traitlet_notifiers = {}
358 # self._traitlet_notifiers = {}
359
359
360 def _notify_traitlet(self, name, old_value, new_value):
360 def _notify_traitlet(self, name, old_value, new_value):
361
361
362 # First dynamic ones
362 # First dynamic ones
363 callables = self._traitlet_notifiers.get(name,[])
363 callables = self._traitlet_notifiers.get(name,[])
364 more_callables = self._traitlet_notifiers.get('anytraitlet',[])
364 more_callables = self._traitlet_notifiers.get('anytraitlet',[])
365 callables.extend(more_callables)
365 callables.extend(more_callables)
366
366
367 # Now static ones
367 # Now static ones
368 try:
368 try:
369 cb = getattr(self, '_%s_changed' % name)
369 cb = getattr(self, '_%s_changed' % name)
370 except:
370 except:
371 pass
371 pass
372 else:
372 else:
373 callables.append(cb)
373 callables.append(cb)
374
374
375 # Call them all now
375 # Call them all now
376 for c in callables:
376 for c in callables:
377 # Traits catches and logs errors here. I allow them to raise
377 # Traits catches and logs errors here. I allow them to raise
378 if callable(c):
378 if callable(c):
379 argspec = inspect.getargspec(c)
379 argspec = inspect.getargspec(c)
380 nargs = len(argspec[0])
380 nargs = len(argspec[0])
381 # Bound methods have an additional 'self' argument
381 # Bound methods have an additional 'self' argument
382 # I don't know how to treat unbound methods, but they
382 # I don't know how to treat unbound methods, but they
383 # can't really be used for callbacks.
383 # can't really be used for callbacks.
384 if isinstance(c, types.MethodType):
384 if isinstance(c, types.MethodType):
385 offset = -1
385 offset = -1
386 else:
386 else:
387 offset = 0
387 offset = 0
388 if nargs + offset == 0:
388 if nargs + offset == 0:
389 c()
389 c()
390 elif nargs + offset == 1:
390 elif nargs + offset == 1:
391 c(name)
391 c(name)
392 elif nargs + offset == 2:
392 elif nargs + offset == 2:
393 c(name, new_value)
393 c(name, new_value)
394 elif nargs + offset == 3:
394 elif nargs + offset == 3:
395 c(name, old_value, new_value)
395 c(name, old_value, new_value)
396 else:
396 else:
397 raise TraitletError('a traitlet changed callback '
397 raise TraitletError('a traitlet changed callback '
398 'must have 0-3 arguments.')
398 'must have 0-3 arguments.')
399 else:
399 else:
400 raise TraitletError('a traitlet changed callback '
400 raise TraitletError('a traitlet changed callback '
401 'must be callable.')
401 'must be callable.')
402
402
403
403
404 def _add_notifiers(self, handler, name):
404 def _add_notifiers(self, handler, name):
405 if not self._traitlet_notifiers.has_key(name):
405 if not self._traitlet_notifiers.has_key(name):
406 nlist = []
406 nlist = []
407 self._traitlet_notifiers[name] = nlist
407 self._traitlet_notifiers[name] = nlist
408 else:
408 else:
409 nlist = self._traitlet_notifiers[name]
409 nlist = self._traitlet_notifiers[name]
410 if handler not in nlist:
410 if handler not in nlist:
411 nlist.append(handler)
411 nlist.append(handler)
412
412
413 def _remove_notifiers(self, handler, name):
413 def _remove_notifiers(self, handler, name):
414 if self._traitlet_notifiers.has_key(name):
414 if self._traitlet_notifiers.has_key(name):
415 nlist = self._traitlet_notifiers[name]
415 nlist = self._traitlet_notifiers[name]
416 try:
416 try:
417 index = nlist.index(handler)
417 index = nlist.index(handler)
418 except ValueError:
418 except ValueError:
419 pass
419 pass
420 else:
420 else:
421 del nlist[index]
421 del nlist[index]
422
422
423 def on_traitlet_change(self, handler, name=None, remove=False):
423 def on_traitlet_change(self, handler, name=None, remove=False):
424 """Setup a handler to be called when a traitlet changes.
424 """Setup a handler to be called when a traitlet changes.
425
425
426 This is used to setup dynamic notifications of traitlet changes.
426 This is used to setup dynamic notifications of traitlet changes.
427
427
428 Static handlers can be created by creating methods on a HasTraitlets
428 Static handlers can be created by creating methods on a HasTraitlets
429 subclass with the naming convention '_[traitletname]_changed'. Thus,
429 subclass with the naming convention '_[traitletname]_changed'. Thus,
430 to create static handler for the traitlet 'a', create the method
430 to create static handler for the traitlet 'a', create the method
431 _a_changed(self, name, old, new) (fewer arguments can be used, see
431 _a_changed(self, name, old, new) (fewer arguments can be used, see
432 below).
432 below).
433
433
434 Parameters
434 Parameters
435 ----------
435 ----------
436 handler : callable
436 handler : callable
437 A callable that is called when a traitlet changes. Its
437 A callable that is called when a traitlet changes. Its
438 signature can be handler(), handler(name), handler(name, new)
438 signature can be handler(), handler(name), handler(name, new)
439 or handler(name, old, new).
439 or handler(name, old, new).
440 name : list, str, None
440 name : list, str, None
441 If None, the handler will apply to all traitlets. If a list
441 If None, the handler will apply to all traitlets. If a list
442 of str, handler will apply to all names in the list. If a
442 of str, handler will apply to all names in the list. If a
443 str, the handler will apply just to that name.
443 str, the handler will apply just to that name.
444 remove : bool
444 remove : bool
445 If False (the default), then install the handler. If True
445 If False (the default), then install the handler. If True
446 then unintall it.
446 then unintall it.
447 """
447 """
448 if remove:
448 if remove:
449 names = parse_notifier_name(name)
449 names = parse_notifier_name(name)
450 for n in names:
450 for n in names:
451 self._remove_notifiers(handler, n)
451 self._remove_notifiers(handler, n)
452 else:
452 else:
453 names = parse_notifier_name(name)
453 names = parse_notifier_name(name)
454 for n in names:
454 for n in names:
455 self._add_notifiers(handler, n)
455 self._add_notifiers(handler, n)
456
456
457 def traitlet_names(self, **metadata):
457 def traitlet_names(self, **metadata):
458 """Get a list of all the names of this classes traitlets."""
458 """Get a list of all the names of this classes traitlets."""
459 return self.traitlets(**metadata).keys()
459 return self.traitlets(**metadata).keys()
460
460
461 def traitlets(self, *args, **metadata):
461 def traitlets(self, **metadata):
462 """Get a list of all the traitlets of this class.
462 """Get a list of all the traitlets of this class.
463
463
464 The TraitletTypes returned don't know anything about the values
464 The TraitletTypes returned don't know anything about the values
465 that the various HasTraitlet's instances are holding.
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 traitlets = dict([memb for memb in inspect.getmembers(self.__class__) if \
472 traitlets = dict([memb for memb in inspect.getmembers(self.__class__) if \
468 isinstance(memb[1], TraitletType)])
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:
475 if len(metadata) == 0:
473 metadata[meta_name] = lambda _: True
476 return traitlets
474
477
475 for meta_name, meta_eval in metadata.items():
478 for meta_name, meta_eval in metadata.items():
476 if type(meta_eval) is not FunctionType:
479 if type(meta_eval) is not FunctionType:
477 metadata[meta_name] = _SimpleTest(meta_eval)
480 metadata[meta_name] = _SimpleTest(meta_eval)
478
481
479 result = {}
482 result = {}
480 for name, traitlet in traitlets.items():
483 for name, traitlet in traitlets.items():
481 for meta_name, meta_eval in metadata.items():
484 for meta_name, meta_eval in metadata.items():
482 if not meta_eval(traitlet.get_metadata(meta_name)):
485 if not meta_eval(traitlet.get_metadata(meta_name)):
483 break
486 break
484 else:
487 else:
485 result[name] = traitlet
488 result[name] = traitlet
486
489
487 return result
490 return result
488
491
489 def traitlet_metadata(self, traitletname, key):
492 def traitlet_metadata(self, traitletname, key):
490 """Get metadata values for traitlet by key."""
493 """Get metadata values for traitlet by key."""
491 try:
494 try:
492 traitlet = getattr(self.__class__, traitletname)
495 traitlet = getattr(self.__class__, traitletname)
493 except AttributeError:
496 except AttributeError:
494 raise TraitletError("Class %s does not have a traitlet named %s" %
497 raise TraitletError("Class %s does not have a traitlet named %s" %
495 (self.__class__.__name__, traitletname))
498 (self.__class__.__name__, traitletname))
496 else:
499 else:
497 return traitlet.get_metadata(key)
500 return traitlet.get_metadata(key)
498
501
499 #-----------------------------------------------------------------------------
502 #-----------------------------------------------------------------------------
500 # Actual TraitletTypes implementations/subclasses
503 # Actual TraitletTypes implementations/subclasses
501 #-----------------------------------------------------------------------------
504 #-----------------------------------------------------------------------------
502
505
503 #-----------------------------------------------------------------------------
506 #-----------------------------------------------------------------------------
504 # TraitletTypes subclasses for handling classes and instances of classes
507 # TraitletTypes subclasses for handling classes and instances of classes
505 #-----------------------------------------------------------------------------
508 #-----------------------------------------------------------------------------
506
509
507
510
508 class ClassBasedTraitletType(TraitletType):
511 class ClassBasedTraitletType(TraitletType):
509 """A traitlet with error reporting for Type, Instance and This."""
512 """A traitlet with error reporting for Type, Instance and This."""
510
513
511 def error(self, obj, value):
514 def error(self, obj, value):
512 kind = type(value)
515 kind = type(value)
513 if kind is InstanceType:
516 if kind is InstanceType:
514 msg = 'class %s' % value.__class__.__name__
517 msg = 'class %s' % value.__class__.__name__
515 else:
518 else:
516 msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) )
519 msg = '%s (i.e. %s)' % ( str( kind )[1:-1], repr( value ) )
517
520
518 super(ClassBasedTraitletType, self).error(obj, msg)
521 super(ClassBasedTraitletType, self).error(obj, msg)
519
522
520
523
521 class Type(ClassBasedTraitletType):
524 class Type(ClassBasedTraitletType):
522 """A traitlet whose value must be a subclass of a specified class."""
525 """A traitlet whose value must be a subclass of a specified class."""
523
526
524 def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ):
527 def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ):
525 """Construct a Type traitlet
528 """Construct a Type traitlet
526
529
527 A Type traitlet specifies that its values must be subclasses of
530 A Type traitlet specifies that its values must be subclasses of
528 a particular class.
531 a particular class.
529
532
530 If only ``default_value`` is given, it is used for the ``klass`` as
533 If only ``default_value`` is given, it is used for the ``klass`` as
531 well.
534 well.
532
535
533 Parameters
536 Parameters
534 ----------
537 ----------
535 default_value : class, str or None
538 default_value : class, str or None
536 The default value must be a subclass of klass. If an str,
539 The default value must be a subclass of klass. If an str,
537 the str must be a fully specified class name, like 'foo.bar.Bah'.
540 the str must be a fully specified class name, like 'foo.bar.Bah'.
538 The string is resolved into real class, when the parent
541 The string is resolved into real class, when the parent
539 :class:`HasTraitlets` class is instantiated.
542 :class:`HasTraitlets` class is instantiated.
540 klass : class, str, None
543 klass : class, str, None
541 Values of this traitlet must be a subclass of klass. The klass
544 Values of this traitlet must be a subclass of klass. The klass
542 may be specified in a string like: 'foo.bar.MyClass'.
545 may be specified in a string like: 'foo.bar.MyClass'.
543 The string is resolved into real class, when the parent
546 The string is resolved into real class, when the parent
544 :class:`HasTraitlets` class is instantiated.
547 :class:`HasTraitlets` class is instantiated.
545 allow_none : boolean
548 allow_none : boolean
546 Indicates whether None is allowed as an assignable value. Even if
549 Indicates whether None is allowed as an assignable value. Even if
547 ``False``, the default value may be ``None``.
550 ``False``, the default value may be ``None``.
548 """
551 """
549 if default_value is None:
552 if default_value is None:
550 if klass is None:
553 if klass is None:
551 klass = object
554 klass = object
552 elif klass is None:
555 elif klass is None:
553 klass = default_value
556 klass = default_value
554
557
555 if not (inspect.isclass(klass) or isinstance(klass, basestring)):
558 if not (inspect.isclass(klass) or isinstance(klass, basestring)):
556 raise TraitletError("A Type traitlet must specify a class.")
559 raise TraitletError("A Type traitlet must specify a class.")
557
560
558 self.klass = klass
561 self.klass = klass
559 self._allow_none = allow_none
562 self._allow_none = allow_none
560
563
561 super(Type, self).__init__(default_value, **metadata)
564 super(Type, self).__init__(default_value, **metadata)
562
565
563 def validate(self, obj, value):
566 def validate(self, obj, value):
564 """Validates that the value is a valid object instance."""
567 """Validates that the value is a valid object instance."""
565 try:
568 try:
566 if issubclass(value, self.klass):
569 if issubclass(value, self.klass):
567 return value
570 return value
568 except:
571 except:
569 if (value is None) and (self._allow_none):
572 if (value is None) and (self._allow_none):
570 return value
573 return value
571
574
572 self.error(obj, value)
575 self.error(obj, value)
573
576
574 def info(self):
577 def info(self):
575 """ Returns a description of the trait."""
578 """ Returns a description of the trait."""
576 if isinstance(self.klass, basestring):
579 if isinstance(self.klass, basestring):
577 klass = self.klass
580 klass = self.klass
578 else:
581 else:
579 klass = self.klass.__name__
582 klass = self.klass.__name__
580 result = 'a subclass of ' + klass
583 result = 'a subclass of ' + klass
581 if self._allow_none:
584 if self._allow_none:
582 return result + ' or None'
585 return result + ' or None'
583 return result
586 return result
584
587
585 def instance_init(self, obj):
588 def instance_init(self, obj):
586 self._resolve_classes()
589 self._resolve_classes()
587 super(Type, self).instance_init(obj)
590 super(Type, self).instance_init(obj)
588
591
589 def _resolve_classes(self):
592 def _resolve_classes(self):
590 if isinstance(self.klass, basestring):
593 if isinstance(self.klass, basestring):
591 self.klass = import_item(self.klass)
594 self.klass = import_item(self.klass)
592 if isinstance(self.default_value, basestring):
595 if isinstance(self.default_value, basestring):
593 self.default_value = import_item(self.default_value)
596 self.default_value = import_item(self.default_value)
594
597
595 def get_default_value(self):
598 def get_default_value(self):
596 return self.default_value
599 return self.default_value
597
600
598
601
599 class DefaultValueGenerator(object):
602 class DefaultValueGenerator(object):
600 """A class for generating new default value instances."""
603 """A class for generating new default value instances."""
601
604
602 def __init__(self, *args, **kw):
605 def __init__(self, *args, **kw):
603 self.args = args
606 self.args = args
604 self.kw = kw
607 self.kw = kw
605
608
606 def generate(self, klass):
609 def generate(self, klass):
607 return klass(*self.args, **self.kw)
610 return klass(*self.args, **self.kw)
608
611
609
612
610 class Instance(ClassBasedTraitletType):
613 class Instance(ClassBasedTraitletType):
611 """A trait whose value must be an instance of a specified class.
614 """A trait whose value must be an instance of a specified class.
612
615
613 The value can also be an instance of a subclass of the specified class.
616 The value can also be an instance of a subclass of the specified class.
614 """
617 """
615
618
616 def __init__(self, klass=None, args=None, kw=None,
619 def __init__(self, klass=None, args=None, kw=None,
617 allow_none=True, **metadata ):
620 allow_none=True, **metadata ):
618 """Construct an Instance traitlet.
621 """Construct an Instance traitlet.
619
622
620 This traitlet allows values that are instances of a particular
623 This traitlet allows values that are instances of a particular
621 class or its sublclasses. Our implementation is quite different
624 class or its sublclasses. Our implementation is quite different
622 from that of enthough.traits as we don't allow instances to be used
625 from that of enthough.traits as we don't allow instances to be used
623 for klass and we handle the ``args`` and ``kw`` arguments differently.
626 for klass and we handle the ``args`` and ``kw`` arguments differently.
624
627
625 Parameters
628 Parameters
626 ----------
629 ----------
627 klass : class, str
630 klass : class, str
628 The class that forms the basis for the traitlet. Class names
631 The class that forms the basis for the traitlet. Class names
629 can also be specified as strings, like 'foo.bar.Bar'.
632 can also be specified as strings, like 'foo.bar.Bar'.
630 args : tuple
633 args : tuple
631 Positional arguments for generating the default value.
634 Positional arguments for generating the default value.
632 kw : dict
635 kw : dict
633 Keyword arguments for generating the default value.
636 Keyword arguments for generating the default value.
634 allow_none : bool
637 allow_none : bool
635 Indicates whether None is allowed as a value.
638 Indicates whether None is allowed as a value.
636
639
637 Default Value
640 Default Value
638 -------------
641 -------------
639 If both ``args`` and ``kw`` are None, then the default value is None.
642 If both ``args`` and ``kw`` are None, then the default value is None.
640 If ``args`` is a tuple and ``kw`` is a dict, then the default is
643 If ``args`` is a tuple and ``kw`` is a dict, then the default is
641 created as ``klass(*args, **kw)``. If either ``args`` or ``kw`` is
644 created as ``klass(*args, **kw)``. If either ``args`` or ``kw`` is
642 not (but not both), None is replace by ``()`` or ``{}``.
645 not (but not both), None is replace by ``()`` or ``{}``.
643 """
646 """
644
647
645 self._allow_none = allow_none
648 self._allow_none = allow_none
646
649
647 if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, basestring))):
650 if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, basestring))):
648 raise TraitletError('The klass argument must be a class'
651 raise TraitletError('The klass argument must be a class'
649 ' you gave: %r' % klass)
652 ' you gave: %r' % klass)
650 self.klass = klass
653 self.klass = klass
651
654
652 # self.klass is a class, so handle default_value
655 # self.klass is a class, so handle default_value
653 if args is None and kw is None:
656 if args is None and kw is None:
654 default_value = None
657 default_value = None
655 else:
658 else:
656 if args is None:
659 if args is None:
657 # kw is not None
660 # kw is not None
658 args = ()
661 args = ()
659 elif kw is None:
662 elif kw is None:
660 # args is not None
663 # args is not None
661 kw = {}
664 kw = {}
662
665
663 if not isinstance(kw, dict):
666 if not isinstance(kw, dict):
664 raise TraitletError("The 'kw' argument must be a dict or None.")
667 raise TraitletError("The 'kw' argument must be a dict or None.")
665 if not isinstance(args, tuple):
668 if not isinstance(args, tuple):
666 raise TraitletError("The 'args' argument must be a tuple or None.")
669 raise TraitletError("The 'args' argument must be a tuple or None.")
667
670
668 default_value = DefaultValueGenerator(*args, **kw)
671 default_value = DefaultValueGenerator(*args, **kw)
669
672
670 super(Instance, self).__init__(default_value, **metadata)
673 super(Instance, self).__init__(default_value, **metadata)
671
674
672 def validate(self, obj, value):
675 def validate(self, obj, value):
673 if value is None:
676 if value is None:
674 if self._allow_none:
677 if self._allow_none:
675 return value
678 return value
676 self.error(obj, value)
679 self.error(obj, value)
677
680
678 if isinstance(value, self.klass):
681 if isinstance(value, self.klass):
679 return value
682 return value
680 else:
683 else:
681 self.error(obj, value)
684 self.error(obj, value)
682
685
683 def info(self):
686 def info(self):
684 if isinstance(self.klass, basestring):
687 if isinstance(self.klass, basestring):
685 klass = self.klass
688 klass = self.klass
686 else:
689 else:
687 klass = self.klass.__name__
690 klass = self.klass.__name__
688 result = class_of(klass)
691 result = class_of(klass)
689 if self._allow_none:
692 if self._allow_none:
690 return result + ' or None'
693 return result + ' or None'
691
694
692 return result
695 return result
693
696
694 def instance_init(self, obj):
697 def instance_init(self, obj):
695 self._resolve_classes()
698 self._resolve_classes()
696 super(Instance, self).instance_init(obj)
699 super(Instance, self).instance_init(obj)
697
700
698 def _resolve_classes(self):
701 def _resolve_classes(self):
699 if isinstance(self.klass, basestring):
702 if isinstance(self.klass, basestring):
700 self.klass = import_item(self.klass)
703 self.klass = import_item(self.klass)
701
704
702 def get_default_value(self):
705 def get_default_value(self):
703 """Instantiate a default value instance.
706 """Instantiate a default value instance.
704
707
705 This is called when the containing HasTraitlets classes'
708 This is called when the containing HasTraitlets classes'
706 :meth:`__new__` method is called to ensure that a unique instance
709 :meth:`__new__` method is called to ensure that a unique instance
707 is created for each HasTraitlets instance.
710 is created for each HasTraitlets instance.
708 """
711 """
709 dv = self.default_value
712 dv = self.default_value
710 if isinstance(dv, DefaultValueGenerator):
713 if isinstance(dv, DefaultValueGenerator):
711 return dv.generate(self.klass)
714 return dv.generate(self.klass)
712 else:
715 else:
713 return dv
716 return dv
714
717
715
718
716 class This(ClassBasedTraitletType):
719 class This(ClassBasedTraitletType):
717 """A traitlet for instances of the class containing this trait.
720 """A traitlet for instances of the class containing this trait.
718
721
719 Because how how and when class bodies are executed, the ``This``
722 Because how how and when class bodies are executed, the ``This``
720 traitlet can only have a default value of None. This, and because we
723 traitlet can only have a default value of None. This, and because we
721 always validate default values, ``allow_none`` is *always* true.
724 always validate default values, ``allow_none`` is *always* true.
722 """
725 """
723
726
724 info_text = 'an instance of the same type as the receiver or None'
727 info_text = 'an instance of the same type as the receiver or None'
725
728
726 def __init__(self, **metadata):
729 def __init__(self, **metadata):
727 super(This, self).__init__(None, **metadata)
730 super(This, self).__init__(None, **metadata)
728
731
729 def validate(self, obj, value):
732 def validate(self, obj, value):
730 # What if value is a superclass of obj.__class__? This is
733 # What if value is a superclass of obj.__class__? This is
731 # complicated if it was the superclass that defined the This
734 # complicated if it was the superclass that defined the This
732 # traitlet.
735 # traitlet.
733 if isinstance(value, self.this_class) or (value is None):
736 if isinstance(value, self.this_class) or (value is None):
734 return value
737 return value
735 else:
738 else:
736 self.error(obj, value)
739 self.error(obj, value)
737
740
738
741
739 #-----------------------------------------------------------------------------
742 #-----------------------------------------------------------------------------
740 # Basic TraitletTypes implementations/subclasses
743 # Basic TraitletTypes implementations/subclasses
741 #-----------------------------------------------------------------------------
744 #-----------------------------------------------------------------------------
742
745
743
746
744 class Any(TraitletType):
747 class Any(TraitletType):
745 default_value = None
748 default_value = None
746 info_text = 'any value'
749 info_text = 'any value'
747
750
748
751
749 class Int(TraitletType):
752 class Int(TraitletType):
750 """A integer traitlet."""
753 """A integer traitlet."""
751
754
752 evaluate = int
755 evaluate = int
753 default_value = 0
756 default_value = 0
754 info_text = 'an integer'
757 info_text = 'an integer'
755
758
756 def validate(self, obj, value):
759 def validate(self, obj, value):
757 if isinstance(value, int):
760 if isinstance(value, int):
758 return value
761 return value
759 self.error(obj, value)
762 self.error(obj, value)
760
763
761 class CInt(Int):
764 class CInt(Int):
762 """A casting version of the int traitlet."""
765 """A casting version of the int traitlet."""
763
766
764 def validate(self, obj, value):
767 def validate(self, obj, value):
765 try:
768 try:
766 return int(value)
769 return int(value)
767 except:
770 except:
768 self.error(obj, value)
771 self.error(obj, value)
769
772
770
773
771 class Long(TraitletType):
774 class Long(TraitletType):
772 """A long integer traitlet."""
775 """A long integer traitlet."""
773
776
774 evaluate = long
777 evaluate = long
775 default_value = 0L
778 default_value = 0L
776 info_text = 'a long'
779 info_text = 'a long'
777
780
778 def validate(self, obj, value):
781 def validate(self, obj, value):
779 if isinstance(value, long):
782 if isinstance(value, long):
780 return value
783 return value
781 if isinstance(value, int):
784 if isinstance(value, int):
782 return long(value)
785 return long(value)
783 self.error(obj, value)
786 self.error(obj, value)
784
787
785
788
786 class CLong(Long):
789 class CLong(Long):
787 """A casting version of the long integer traitlet."""
790 """A casting version of the long integer traitlet."""
788
791
789 def validate(self, obj, value):
792 def validate(self, obj, value):
790 try:
793 try:
791 return long(value)
794 return long(value)
792 except:
795 except:
793 self.error(obj, value)
796 self.error(obj, value)
794
797
795
798
796 class Float(TraitletType):
799 class Float(TraitletType):
797 """A float traitlet."""
800 """A float traitlet."""
798
801
799 evaluate = float
802 evaluate = float
800 default_value = 0.0
803 default_value = 0.0
801 info_text = 'a float'
804 info_text = 'a float'
802
805
803 def validate(self, obj, value):
806 def validate(self, obj, value):
804 if isinstance(value, float):
807 if isinstance(value, float):
805 return value
808 return value
806 if isinstance(value, int):
809 if isinstance(value, int):
807 return float(value)
810 return float(value)
808 self.error(obj, value)
811 self.error(obj, value)
809
812
810
813
811 class CFloat(Float):
814 class CFloat(Float):
812 """A casting version of the float traitlet."""
815 """A casting version of the float traitlet."""
813
816
814 def validate(self, obj, value):
817 def validate(self, obj, value):
815 try:
818 try:
816 return float(value)
819 return float(value)
817 except:
820 except:
818 self.error(obj, value)
821 self.error(obj, value)
819
822
820 class Complex(TraitletType):
823 class Complex(TraitletType):
821 """A traitlet for complex numbers."""
824 """A traitlet for complex numbers."""
822
825
823 evaluate = complex
826 evaluate = complex
824 default_value = 0.0 + 0.0j
827 default_value = 0.0 + 0.0j
825 info_text = 'a complex number'
828 info_text = 'a complex number'
826
829
827 def validate(self, obj, value):
830 def validate(self, obj, value):
828 if isinstance(value, complex):
831 if isinstance(value, complex):
829 return value
832 return value
830 if isinstance(value, (float, int)):
833 if isinstance(value, (float, int)):
831 return complex(value)
834 return complex(value)
832 self.error(obj, value)
835 self.error(obj, value)
833
836
834
837
835 class CComplex(Complex):
838 class CComplex(Complex):
836 """A casting version of the complex number traitlet."""
839 """A casting version of the complex number traitlet."""
837
840
838 def validate (self, obj, value):
841 def validate (self, obj, value):
839 try:
842 try:
840 return complex(value)
843 return complex(value)
841 except:
844 except:
842 self.error(obj, value)
845 self.error(obj, value)
843
846
844
847
845 class Str(TraitletType):
848 class Str(TraitletType):
846 """A traitlet for strings."""
849 """A traitlet for strings."""
847
850
848 evaluate = lambda x: x
851 evaluate = lambda x: x
849 default_value = ''
852 default_value = ''
850 info_text = 'a string'
853 info_text = 'a string'
851
854
852 def validate(self, obj, value):
855 def validate(self, obj, value):
853 if isinstance(value, str):
856 if isinstance(value, str):
854 return value
857 return value
855 self.error(obj, value)
858 self.error(obj, value)
856
859
857
860
858 class CStr(Str):
861 class CStr(Str):
859 """A casting version of the string traitlet."""
862 """A casting version of the string traitlet."""
860
863
861 def validate(self, obj, value):
864 def validate(self, obj, value):
862 try:
865 try:
863 return str(value)
866 return str(value)
864 except:
867 except:
865 try:
868 try:
866 return unicode(value)
869 return unicode(value)
867 except:
870 except:
868 self.error(obj, value)
871 self.error(obj, value)
869
872
870
873
871 class Unicode(TraitletType):
874 class Unicode(TraitletType):
872 """A traitlet for unicode strings."""
875 """A traitlet for unicode strings."""
873
876
874 evaluate = unicode
877 evaluate = unicode
875 default_value = u''
878 default_value = u''
876 info_text = 'a unicode string'
879 info_text = 'a unicode string'
877
880
878 def validate(self, obj, value):
881 def validate(self, obj, value):
879 if isinstance(value, unicode):
882 if isinstance(value, unicode):
880 return value
883 return value
881 if isinstance(value, str):
884 if isinstance(value, str):
882 return unicode(value)
885 return unicode(value)
883 self.error(obj, value)
886 self.error(obj, value)
884
887
885
888
886 class CUnicode(Unicode):
889 class CUnicode(Unicode):
887 """A casting version of the unicode traitlet."""
890 """A casting version of the unicode traitlet."""
888
891
889 def validate(self, obj, value):
892 def validate(self, obj, value):
890 try:
893 try:
891 return unicode(value)
894 return unicode(value)
892 except:
895 except:
893 self.error(obj, value)
896 self.error(obj, value)
894
897
895
898
896 class Bool(TraitletType):
899 class Bool(TraitletType):
897 """A boolean (True, False) traitlet."""
900 """A boolean (True, False) traitlet."""
898 evaluate = bool
901 evaluate = bool
899 default_value = False
902 default_value = False
900 info_text = 'a boolean'
903 info_text = 'a boolean'
901
904
902 def validate(self, obj, value):
905 def validate(self, obj, value):
903 if isinstance(value, bool):
906 if isinstance(value, bool):
904 return value
907 return value
905 self.error(obj, value)
908 self.error(obj, value)
906
909
907
910
908 class CBool(Bool):
911 class CBool(Bool):
909 """A casting version of the boolean traitlet."""
912 """A casting version of the boolean traitlet."""
910
913
911 def validate(self, obj, value):
914 def validate(self, obj, value):
912 try:
915 try:
913 return bool(value)
916 return bool(value)
914 except:
917 except:
915 self.error(obj, value)
918 self.error(obj, value)
916
919
917
920
918 class Enum(TraitletType):
921 class Enum(TraitletType):
919 """An enum that whose value must be in a given sequence."""
922 """An enum that whose value must be in a given sequence."""
920
923
921 def __init__(self, values, default_value=None, allow_none=True, **metadata):
924 def __init__(self, values, default_value=None, allow_none=True, **metadata):
922 self.values = values
925 self.values = values
923 self._allow_none = allow_none
926 self._allow_none = allow_none
924 super(Enum, self).__init__(default_value, **metadata)
927 super(Enum, self).__init__(default_value, **metadata)
925
928
926 def validate(self, obj, value):
929 def validate(self, obj, value):
927 if value is None:
930 if value is None:
928 if self._allow_none:
931 if self._allow_none:
929 return value
932 return value
930
933
931 if value in self.values:
934 if value in self.values:
932 return value
935 return value
933 self.error(obj, value)
936 self.error(obj, value)
934
937
935 def info(self):
938 def info(self):
936 """ Returns a description of the trait."""
939 """ Returns a description of the trait."""
937 result = 'any of ' + repr(self.values)
940 result = 'any of ' + repr(self.values)
938 if self._allow_none:
941 if self._allow_none:
939 return result + ' or None'
942 return result + ' or None'
940 return result
943 return result
941
944
942 class CaselessStrEnum(Enum):
945 class CaselessStrEnum(Enum):
943 """An enum of strings that are caseless in validate."""
946 """An enum of strings that are caseless in validate."""
944
947
945 def validate(self, obj, value):
948 def validate(self, obj, value):
946 if value is None:
949 if value is None:
947 if self._allow_none:
950 if self._allow_none:
948 return value
951 return value
949
952
950 if not isinstance(value, str):
953 if not isinstance(value, str):
951 self.error(obj, value)
954 self.error(obj, value)
952
955
953 for v in self.values:
956 for v in self.values:
954 if v.lower() == value.lower():
957 if v.lower() == value.lower():
955 return v
958 return v
956 self.error(obj, value)
959 self.error(obj, value)
957
960
958
961
959 class List(Instance):
962 class List(Instance):
960 """An instance of a Python list."""
963 """An instance of a Python list."""
961
964
962 def __init__(self, default_value=None, allow_none=True, **metadata):
965 def __init__(self, default_value=None, allow_none=True, **metadata):
963 """Create a list traitlet type from a list or tuple.
966 """Create a list traitlet type from a list or tuple.
964
967
965 The default value is created by doing ``list(default_value)``,
968 The default value is created by doing ``list(default_value)``,
966 which creates a copy of the ``default_value``.
969 which creates a copy of the ``default_value``.
967 """
970 """
968 if default_value is None:
971 if default_value is None:
969 args = ((),)
972 args = ((),)
970 elif isinstance(default_value, SequenceTypes):
973 elif isinstance(default_value, SequenceTypes):
971 args = (default_value,)
974 args = (default_value,)
972
975
973 super(List,self).__init__(klass=list, args=args,
976 super(List,self).__init__(klass=list, args=args,
974 allow_none=allow_none, **metadata)
977 allow_none=allow_none, **metadata)
General Comments 0
You need to be logged in to leave comments. Login now