##// END OF EJS Templates
Unicode fixes (utf-8 used by default if ascii is not enough). This should fix some reported crashes....
fperez -
r5:c83aafa3
parent child Browse files
Show More

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

@@ -1,270 +1,272 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Modified input prompt for executing files.
3 3
4 4 We define a special input line filter to allow typing lines which begin with
5 5 '~', '/' or '.'. If one of those strings is encountered, it is automatically
6 6 executed.
7 7
8 $Id: InterpreterExec.py 573 2005-04-08 08:38:09Z fperez $"""
8 $Id: InterpreterExec.py 638 2005-07-18 03:01:41Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2004 W.J. van der Laan <gnufnork@hetdigitalegat.nl>
12 12 # Copyright (C) 2004 Fernando Perez <fperez@colorado.edu>
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #*****************************************************************************
17 17
18 18 from IPython import Release
19 19 __author__ = 'W.J. van der Laan <gnufnork@hetdigitalegat.nl>, '\
20 20 '%s <%s>' % Release.authors['Fernando']
21 21 __license__ = Release.license
22 22
23 23 def prefilter_shell(self,line,continuation):
24 24 """Alternate prefilter, modified for shell-like functionality.
25 25
26 26 - Execute all lines beginning with '~', '/' or '.'
27 27 - $var=cmd <=> %sc var=cmd
28 28 - $$var=cmd <=> %sc -l var=cmd
29 29 """
30 30
31 31 if line:
32 32 l0 = line[0]
33 33 if l0 in '~/.':
34 34 return self._prefilter("!%s"%line,continuation)
35 35 elif l0=='$':
36 36 lrest = line[1:]
37 37 if lrest.startswith('$'):
38 38 # $$var=cmd <=> %sc -l var=cmd
39 39 return self._prefilter("%ssc -l %s" % (self.ESC_MAGIC,lrest[1:]),
40 40 continuation)
41 41 else:
42 42 # $var=cmd <=> %sc var=cmd
43 43 return self._prefilter("%ssc %s" % (self.ESC_MAGIC,lrest),
44 44 continuation)
45 45 else:
46 46 return self._prefilter(line,continuation)
47 47 else:
48 48 return self._prefilter(line,continuation)
49 49
50 50 # Rebind this to be the new IPython prefilter:
51 51 from IPython.iplib import InteractiveShell
52 52 InteractiveShell.prefilter = prefilter_shell
53 53 # Clean up the namespace.
54 54 del InteractiveShell,prefilter_shell
55 55
56 56 # Provide pysh and further shell-oriented services
57 57 import os,sys,shutil
58 58 from IPython.genutils import system,shell,getoutput,getoutputerror
59 59
60 60 # Short aliases for getting shell output as a string and a list
61 61 sout = getoutput
62 62 lout = lambda cmd: getoutput(cmd,split=1)
63 63
64 64 # Empty function, meant as a docstring holder so help(pysh) works.
65 65 def pysh():
66 66 """Pysh is a set of modules and extensions to IPython which make shell-like
67 67 usage with Python syntax more convenient. Keep in mind that pysh is NOT a
68 68 full-blown shell, so don't try to make it your /etc/passwd entry!
69 69
70 70 In particular, it has no job control, so if you type Ctrl-Z (under Unix),
71 71 you'll suspend pysh itself, not the process you just started.
72 72
73 73 Since pysh is really nothing but a customized IPython, you should
74 74 familiarize yourself with IPython's features. This brief help mainly
75 75 documents areas in which pysh differs from the normal IPython.
76 76
77 77 ALIASES
78 78 -------
79 79 All of your $PATH has been loaded as IPython aliases, so you should be
80 80 able to type any normal system command and have it executed. See %alias?
81 81 and %unalias? for details on the alias facilities.
82 82
83 83 SPECIAL SYNTAX
84 84 --------------
85 85 Any lines which begin with '~', '/' and '.' will be executed as shell
86 86 commands instead of as Python code. The special escapes below are also
87 87 recognized. !cmd is valid in single or multi-line input, all others are
88 88 only valid in single-line input:
89 89
90 90 !cmd - pass 'cmd' directly to the shell
91 91 !!cmd - execute 'cmd' and return output as a list (split on '\\n')
92 92 $var=cmd - capture output of cmd into var, as a string
93 93 $$var=cmd - capture output of cmd into var, as a list (split on '\\n')
94 94
95 95 The $/$$ syntaxes make Python variables from system output, which you can
96 96 later use for further scripting. The converse is also possible: when
97 97 executing an alias or calling to the system via !/!!, you can expand any
98 98 python variable or expression by prepending it with $. Full details of
99 99 the allowed syntax can be found in Python's PEP 215.
100 100
101 101 A few brief examples will illustrate these:
102 102
103 103 fperez[~/test]|3> !ls *s.py
104 104 scopes.py strings.py
105 105
106 106 ls is an internal alias, so there's no need to use !:
107 107 fperez[~/test]|4> ls *s.py
108 108 scopes.py* strings.py
109 109
110 110 !!ls will return the output into a Python variable:
111 111 fperez[~/test]|5> !!ls *s.py
112 112 <5> ['scopes.py', 'strings.py']
113 113 fperez[~/test]|6> print _5
114 114 ['scopes.py', 'strings.py']
115 115
116 116 $ and $$ allow direct capture to named variables:
117 117 fperez[~/test]|7> $astr = ls *s.py
118 118 fperez[~/test]|8> astr
119 119 <8> 'scopes.py\\nstrings.py'
120 120
121 121 fperez[~/test]|9> $$alist = ls *s.py
122 122 fperez[~/test]|10> alist
123 123 <10> ['scopes.py', 'strings.py']
124 124
125 125 alist is now a normal python list you can loop over. Using $ will expand
126 126 back the python values when alias calls are made:
127 127 fperez[~/test]|11> for f in alist:
128 128 |..> print 'file',f,
129 129 |..> wc -l $f
130 130 |..>
131 131 file scopes.py 13 scopes.py
132 132 file strings.py 4 strings.py
133 133
134 134 Note that you may need to protect your variables with braces if you want
135 135 to append strings to their names. To copy all files in alist to .bak
136 136 extensions, you must use:
137 137 fperez[~/test]|12> for f in alist:
138 138 |..> cp $f ${f}.bak
139 139
140 140 If you try using $f.bak, you'll get an AttributeError exception saying
141 141 that your string object doesn't have a .bak attribute. This is because
142 142 the $ expansion mechanism allows you to expand full Python expressions:
143 143 fperez[~/test]|13> echo "sys.platform is: $sys.platform"
144 144 sys.platform is: linux2
145 145
146 146 IPython's input history handling is still active, which allows you to
147 147 rerun a single block of multi-line input by simply using exec:
148 148 fperez[~/test]|14> $$alist = ls *.eps
149 149 fperez[~/test]|15> exec _i11
150 150 file image2.eps 921 image2.eps
151 151 file image.eps 921 image.eps
152 152
153 153 While these are new special-case syntaxes, they are designed to allow very
154 154 efficient use of the shell with minimal typing. At an interactive shell
155 155 prompt, conciseness of expression wins over readability.
156 156
157 157 USEFUL FUNCTIONS AND MODULES
158 158 ----------------------------
159 159 The os, sys and shutil modules from the Python standard library are
160 160 automatically loaded. Some additional functions, useful for shell usage,
161 161 are listed below. You can request more help about them with '?'.
162 162
163 163 shell - execute a command in the underlying system shell
164 164 system - like shell(), but return the exit status of the command
165 165 sout - capture the output of a command as a string
166 166 lout - capture the output of a command as a list (split on '\\n')
167 167 getoutputerror - capture (output,error) of a shell command
168 168
169 169 sout/lout are the functional equivalents of $/$$. They are provided to
170 170 allow you to capture system output in the middle of true python code,
171 171 function definitions, etc (where $ and $$ are invalid).
172 172
173 173 DIRECTORY MANAGEMENT
174 174 --------------------
175 175 Since each command passed by pysh to the underlying system is executed in
176 176 a subshell which exits immediately, you can NOT use !cd to navigate the
177 177 filesystem.
178 178
179 179 Pysh provides its own builtin '%cd' magic command to move in the
180 180 filesystem (the % is not required with automagic on). It also maintains a
181 181 list of visited directories (use %dhist to see it) and allows direct
182 182 switching to any of them. Type 'cd?' for more details.
183 183
184 184 %pushd, %popd and %dirs are provided for directory stack handling.
185 185
186 186 PROMPT CUSTOMIZATION
187 187 --------------------
188 188
189 189 The supplied ipythonrc-pysh profile comes with an example of a very
190 190 colored and detailed prompt, mainly to serve as an illustration. The
191 191 valid escape sequences, besides color names, are:
192 192
193 193 \\# - Prompt number.
194 194 \\D - Dots, as many as there are digits in \\# (so they align).
195 195 \\w - Current working directory (cwd).
196 196 \\W - Basename of current working directory.
197 197 \\XN - Where N=0..5. N terms of the cwd, with $HOME written as ~.
198 198 \\YN - Where N=0..5. Like XN, but if ~ is term N+1 it's also shown.
199 199 \\u - Username.
200 200 \\H - Full hostname.
201 201 \\h - Hostname up to first '.'
202 202 \\$ - Root symbol ($ or #).
203 203 \\t - Current time, in H:M:S format.
204 204 \\v - IPython release version.
205 205 \\n - Newline.
206 206 \\r - Carriage return.
207 207 \\\\ - An explicitly escaped '\\'.
208 208
209 209 You can configure your prompt colors using any ANSI color escape. Each
210 210 color escape sets the color for any subsequent text, until another escape
211 211 comes in and changes things. The valid color escapes are:
212 212
213 213 \\C_Black
214 214 \\C_Blue
215 215 \\C_Brown
216 216 \\C_Cyan
217 217 \\C_DarkGray
218 218 \\C_Green
219 219 \\C_LightBlue
220 220 \\C_LightCyan
221 221 \\C_LightGray
222 222 \\C_LightGreen
223 223 \\C_LightPurple
224 224 \\C_LightRed
225 225 \\C_Purple
226 226 \\C_Red
227 227 \\C_White
228 228 \\C_Yellow
229 229 \\C_Normal - Stop coloring, defaults to your terminal settings.
230 230 """
231 231 pass
232 232
233 233 # Configure a few things. Much of this is fairly hackish, since IPython
234 234 # doesn't really expose a clean API for it. Be careful if you start making
235 235 # many modifications here.
236 236
237 237 print """\
238 238 Welcome to pysh, a set of extensions to IPython for shell usage.
239 239 help(pysh) -> help on the installed shell extensions and syntax.
240 240 """
241 241
242 242 # Set the 'cd' command to quiet mode, a more shell-like behavior
243 243 __IPYTHON__.default_option('cd','-q')
244 244
245 245 # Load all of $PATH as aliases
246 246 if os.name == 'posix':
247 247 # %rehash is very fast, but it doesn't check for executability, it simply
248 248 # dumps everything in $PATH as an alias. Use rehashx if you want more
249 249 # checks.
250 250 __IPYTHON__.magic_rehash()
251 251 else:
252 252 # Windows users: the list of extensions considered executable is read from
253 253 # the environment variable 'pathext'. If this is undefined, IPython
254 254 # defaults to EXE, COM and BAT.
255 255 # %rehashx is the one which does extension analysis, at the cost of
256 256 # being much slower than %rehash.
257 257 __IPYTHON__.magic_rehashx()
258 258
259 259 # Remove %sc,%sx if present as aliases
260 260 __IPYTHON__.magic_unalias('sc')
261 261 __IPYTHON__.magic_unalias('sx')
262 262
263 263 # We need different criteria for line-splitting, so that aliases such as
264 264 # 'gnome-terminal' are interpreted as a single alias instead of variable
265 265 # 'gnome' minus variable 'terminal'.
266 266 import re
267 __IPYTHON__.line_split = re.compile(r'^(\s*)([\?\w\.\-\+]+\w*\s*)(\(?.*$)')
267 __IPYTHON__.line_split = re.compile(r'^([\s*,;/])'
268 r'([\?\w\.\-\+]+\w*\s*)'
269 r'(\(?.*$)')
268 270
269 271 # Namespace cleanup
270 272 del re
@@ -1,199 +1,192 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Word completion for GNU readline 2.0.
3 3
4 4 ---------------------------------------------------------------------------
5 5 NOTE: This version is a re-implementation of rlcompleter with selectable
6 6 namespace.
7 7
8 8 The problem with rlcompleter is that it's hardwired to work with
9 9 __main__.__dict__, and in some cases one may have 'sandboxed' namespaces. So
10 10 this class is a ripoff of rlcompleter, with the namespace to work in as an
11 11 optional parameter.
12 12
13 13 This class can be used just like rlcompleter, but the Completer class now has
14 14 a constructor with the optional 'namespace' parameter.
15 15
16 16 A patch has been submitted to Python@sourceforge for these changes to go in
17 17 the standard Python distribution.
18 18
19 19 The patch went in for Python 2.3. Once IPython drops support for Python 2.2,
20 20 this file can be significantly reduced.
21 21 ---------------------------------------------------------------------------
22 22
23 23 Original rlcompleter documentation:
24 24
25 25 This requires the latest extension to the readline module (the
26 26 completes keywords, built-ins and globals in __main__; when completing
27 27 NAME.NAME..., it evaluates (!) the expression up to the last dot and
28 28 completes its attributes.
29 29
30 30 It's very cool to do "import string" type "string.", hit the
31 31 completion key (twice), and see the list of names defined by the
32 32 string module!
33 33
34 34 Tip: to use the tab key as the completion key, call
35 35
36 36 readline.parse_and_bind("tab: complete")
37 37
38 38 Notes:
39 39
40 40 - Exceptions raised by the completer function are *ignored* (and
41 41 generally cause the completion to fail). This is a feature -- since
42 42 readline sets the tty device in raw (or cbreak) mode, printing a
43 43 traceback wouldn't work well without some complicated hoopla to save,
44 44 reset and restore the tty state.
45 45
46 46 - The evaluation of the NAME.NAME... form may cause arbitrary
47 47 application defined code to be executed if an object with a
48 48 __getattr__ hook is found. Since it is the responsibility of the
49 49 application (or the user) to enable this feature, I consider this an
50 50 acceptable risk. More complicated expressions (e.g. function calls or
51 51 indexing operations) are *not* evaluated.
52 52
53 53 - GNU readline is also used by the built-in functions input() and
54 54 raw_input(), and thus these also benefit/suffer from the completer
55 55 features. Clearly an interactive application can benefit by
56 56 specifying its own completer function and using raw_input() for all
57 57 its input.
58 58
59 59 - When the original stdin is not a tty device, GNU readline is never
60 60 used, and this module (and the readline module) are silently inactive.
61 61
62 62 """
63 63
64 64 #*****************************************************************************
65 65 #
66 66 # Since this file is essentially a minimally modified copy of the rlcompleter
67 67 # module which is part of the standard Python distribution, I assume that the
68 68 # proper procedure is to maintain its copyright as belonging to the Python
69 69 # Software Foundation:
70 70 #
71 71 # Copyright (C) 2001 Python Software Foundation, www.python.org
72 72 #
73 73 # Distributed under the terms of the Python Software Foundation license.
74 74 #
75 75 # Full text available at:
76 76 #
77 77 # http://www.python.org/2.1/license.html
78 78 #
79 79 #*****************************************************************************
80 80
81 81 import readline
82 82 import __builtin__
83 83 import __main__
84 84
85 85 __all__ = ["Completer"]
86 86
87 # declares Python 2.2 compatibility symbols:
88 try:
89 basestring
90 except NameError:
91 import types
92 basestring = (types.StringType, types.UnicodeType)
93
94 87 class Completer:
95 88 def __init__(self, namespace = None):
96 89 """Create a new completer for the command line.
97 90
98 91 Completer([namespace]) -> completer instance.
99 92
100 93 If unspecified, the default namespace where completions are performed
101 94 is __main__ (technically, __main__.__dict__). Namespaces should be
102 95 given as dictionaries.
103 96
104 97 Completer instances should be used as the completion mechanism of
105 98 readline via the set_completer() call:
106 99
107 100 readline.set_completer(Completer(my_namespace).complete)
108 101 """
109 102
110 103 if namespace and type(namespace) != type({}):
111 104 raise TypeError,'namespace must be a dictionary'
112 105
113 106 # Don't bind to namespace quite yet, but flag whether the user wants a
114 107 # specific namespace or to use __main__.__dict__. This will allow us
115 108 # to bind to __main__.__dict__ at completion time, not now.
116 109 if namespace is None:
117 110 self.use_main_ns = 1
118 111 else:
119 112 self.use_main_ns = 0
120 113 self.namespace = namespace
121 114
122 115 def complete(self, text, state):
123 116 """Return the next possible completion for 'text'.
124 117
125 118 This is called successively with state == 0, 1, 2, ... until it
126 119 returns None. The completion should begin with 'text'.
127 120
128 121 """
129 122 if self.use_main_ns:
130 123 self.namespace = __main__.__dict__
131 124
132 125 if state == 0:
133 126 if "." in text:
134 127 self.matches = self.attr_matches(text)
135 128 else:
136 129 self.matches = self.global_matches(text)
137 130 try:
138 131 return self.matches[state]
139 132 except IndexError:
140 133 return None
141 134
142 135 def global_matches(self, text):
143 136 """Compute matches when text is a simple name.
144 137
145 138 Return a list of all keywords, built-in functions and names currently
146 139 defined in self.namespace that match.
147 140
148 141 """
149 142 import keyword
150 143 matches = []
151 144 n = len(text)
152 145 for list in [keyword.kwlist,
153 146 __builtin__.__dict__.keys(),
154 147 self.namespace.keys()]:
155 148 for word in list:
156 149 if word[:n] == text and word != "__builtins__":
157 150 matches.append(word)
158 151 return matches
159 152
160 153 def attr_matches(self, text):
161 154 """Compute matches when text contains a dot.
162 155
163 156 Assuming the text is of the form NAME.NAME....[NAME], and is
164 157 evaluatable in self.namespace, it will be evaluated and its attributes
165 158 (as revealed by dir()) are used as possible completions. (For class
166 159 instances, class members are are also considered.)
167 160
168 161 WARNING: this can still invoke arbitrary C code, if an object
169 162 with a __getattr__ hook is evaluated.
170 163
171 164 """
172 165 import re
173 166
174 167 # Another option, seems to work great. Catches things like ''.<tab>
175 168 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
176 169
177 170 if not m:
178 171 return []
179 172 expr, attr = m.group(1, 3)
180 173 object = eval(expr, self.namespace)
181 174 words = [w for w in dir(object) if isinstance(w, basestring)]
182 175 if hasattr(object,'__class__'):
183 176 words.append('__class__')
184 177 words.extend(get_class_members(object.__class__))
185 178 n = len(attr)
186 179 matches = []
187 180 for word in words:
188 181 if word[:n] == attr and word != "__builtins__":
189 182 matches.append("%s.%s" % (expr, word))
190 183 return matches
191 184
192 185 def get_class_members(klass):
193 186 ret = dir(klass)
194 187 if hasattr(klass,'__bases__'):
195 188 for base in klass.__bases__:
196 189 ret.extend(get_class_members(base))
197 190 return ret
198 191
199 192 readline.set_completer(Completer().complete)
@@ -1,253 +1,277 b''
1 1 # -*- coding: utf-8 -*-
2 2 """String interpolation for Python (by Ka-Ping Yee, 14 Feb 2000).
3 3
4 4 This module lets you quickly and conveniently interpolate values into
5 5 strings (in the flavour of Perl or Tcl, but with less extraneous
6 6 punctuation). You get a bit more power than in the other languages,
7 7 because this module allows subscripting, slicing, function calls,
8 8 attribute lookup, or arbitrary expressions. Variables and expressions
9 9 are evaluated in the namespace of the caller.
10 10
11 11 The itpl() function returns the result of interpolating a string, and
12 12 printpl() prints out an interpolated string. Here are some examples:
13 13
14 14 from Itpl import printpl
15 15 printpl("Here is a $string.")
16 16 printpl("Here is a $module.member.")
17 17 printpl("Here is an $object.member.")
18 18 printpl("Here is a $functioncall(with, arguments).")
19 19 printpl("Here is an ${arbitrary + expression}.")
20 20 printpl("Here is an $array[3] member.")
21 21 printpl("Here is a $dictionary['member'].")
22 22
23 23 The filter() function filters a file object so that output through it
24 24 is interpolated. This lets you produce the illusion that Python knows
25 25 how to do interpolation:
26 26
27 27 import Itpl
28 28 sys.stdout = Itpl.filter()
29 29 f = "fancy"
30 30 print "Isn't this $f?"
31 31 print "Standard output has been replaced with a $sys.stdout object."
32 32 sys.stdout = Itpl.unfilter()
33 33 print "Okay, back $to $normal."
34 34
35 35 Under the hood, the Itpl class represents a string that knows how to
36 36 interpolate values. An instance of the class parses the string once
37 37 upon initialization; the evaluation and substitution can then be done
38 38 each time the instance is evaluated with str(instance). For example:
39 39
40 40 from Itpl import Itpl
41 41 s = Itpl("Here is $foo.")
42 42 foo = 5
43 43 print str(s)
44 44 foo = "bar"
45 45 print str(s)
46 46
47 $Id: Itpl.py 542 2005-03-18 09:16:04Z fperez $
47 $Id: Itpl.py 638 2005-07-18 03:01:41Z fperez $
48 48 """ # ' -> close an open quote for stupid emacs
49 49
50 50 #*****************************************************************************
51 51 #
52 52 # Copyright (c) 2001 Ka-Ping Yee <ping@lfw.org>
53 53 #
54 54 #
55 55 # Published under the terms of the MIT license, hereby reproduced:
56 56 #
57 57 # Permission is hereby granted, free of charge, to any person obtaining a copy
58 58 # of this software and associated documentation files (the "Software"), to
59 59 # deal in the Software without restriction, including without limitation the
60 60 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
61 61 # sell copies of the Software, and to permit persons to whom the Software is
62 62 # furnished to do so, subject to the following conditions:
63 63 #
64 64 # The above copyright notice and this permission notice shall be included in
65 65 # all copies or substantial portions of the Software.
66 66 #
67 67 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68 68 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69 69 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70 70 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71 71 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
72 72 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
73 73 # IN THE SOFTWARE.
74 74 #
75 75 #*****************************************************************************
76 76
77 77 __author__ = 'Ka-Ping Yee <ping@lfw.org>'
78 78 __license__ = 'MIT'
79 79
80 80 import sys, string
81 81 from types import StringType
82 82 from tokenize import tokenprog
83 83
84 84 class ItplError(ValueError):
85 85 def __init__(self, text, pos):
86 86 self.text = text
87 87 self.pos = pos
88 88 def __str__(self):
89 89 return "unfinished expression in %s at char %d" % (
90 90 repr(self.text), self.pos)
91 91
92 92 def matchorfail(text, pos):
93 93 match = tokenprog.match(text, pos)
94 94 if match is None:
95 95 raise ItplError(text, pos)
96 96 return match, match.end()
97 97
98 98 class Itpl:
99 99 """Class representing a string with interpolation abilities.
100 100
101 101 Upon creation, an instance works out what parts of the format
102 102 string are literal and what parts need to be evaluated. The
103 103 evaluation and substitution happens in the namespace of the
104 104 caller when str(instance) is called."""
105 105
106 def __init__(self, format):
107 """The single argument to this constructor is a format string.
106 def __init__(self, format,codec='utf_8',encoding_errors='backslashreplace'):
107 """The single mandatory argument to this constructor is a format
108 string.
108 109
109 110 The format string is parsed according to the following rules:
110 111
111 112 1. A dollar sign and a name, possibly followed by any of:
112 113 - an open-paren, and anything up to the matching paren
113 114 - an open-bracket, and anything up to the matching bracket
114 115 - a period and a name
115 116 any number of times, is evaluated as a Python expression.
116 117
117 118 2. A dollar sign immediately followed by an open-brace, and
118 119 anything up to the matching close-brace, is evaluated as
119 120 a Python expression.
120 121
121 122 3. Outside of the expressions described in the above two rules,
122 two dollar signs in a row give you one literal dollar sign."""
123 two dollar signs in a row give you one literal dollar sign.
123 124
124 if type(format) != StringType:
125 Optional arguments:
126
127 - codec('utf_8'): a string containing the name of a valid Python
128 codec.
129
130 - encoding_errors('backslashreplace'): a string with a valid error handling
131 policy. See the codecs module documentation for details.
132
133 These are used to encode the format string if a call to str() fails on
134 the expanded result."""
135
136 if not isinstance(format,basestring):
125 137 raise TypeError, "needs string initializer"
126 138 self.format = format
127
139 self.codec = codec
140 self.encoding_errors = encoding_errors
141
128 142 namechars = "abcdefghijklmnopqrstuvwxyz" \
129 143 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
130 144 chunks = []
131 145 pos = 0
132 146
133 147 while 1:
134 148 dollar = string.find(format, "$", pos)
135 149 if dollar < 0: break
136 150 nextchar = format[dollar+1]
137 151
138 152 if nextchar == "{":
139 153 chunks.append((0, format[pos:dollar]))
140 154 pos, level = dollar+2, 1
141 155 while level:
142 156 match, pos = matchorfail(format, pos)
143 157 tstart, tend = match.regs[3]
144 158 token = format[tstart:tend]
145 159 if token == "{": level = level+1
146 160 elif token == "}": level = level-1
147 161 chunks.append((1, format[dollar+2:pos-1]))
148 162
149 163 elif nextchar in namechars:
150 164 chunks.append((0, format[pos:dollar]))
151 165 match, pos = matchorfail(format, dollar+1)
152 166 while pos < len(format):
153 167 if format[pos] == "." and \
154 168 pos+1 < len(format) and format[pos+1] in namechars:
155 169 match, pos = matchorfail(format, pos+1)
156 170 elif format[pos] in "([":
157 171 pos, level = pos+1, 1
158 172 while level:
159 173 match, pos = matchorfail(format, pos)
160 174 tstart, tend = match.regs[3]
161 175 token = format[tstart:tend]
162 176 if token[0] in "([": level = level+1
163 177 elif token[0] in ")]": level = level-1
164 178 else: break
165 179 chunks.append((1, format[dollar+1:pos]))
166 180
167 181 else:
168 182 chunks.append((0, format[pos:dollar+1]))
169 183 pos = dollar + 1 + (nextchar == "$")
170 184
171 185 if pos < len(format): chunks.append((0, format[pos:]))
172 186 self.chunks = chunks
173 187
174 188 def __repr__(self):
175 189 return "<Itpl %s >" % repr(self.format)
176 190
191 def _str(self,glob,loc):
192 """Evaluate to a string in the given globals/locals.
193
194 The final output is built by calling str(), but if this fails, the
195 result is encoded with the instance's codec and error handling policy,
196 via a call to out.encode(self.codec,self.encoding_errors)"""
197 result = []
198 app = result.append
199 for live, chunk in self.chunks:
200 if live: app(str(eval(chunk,glob,loc)))
201 else: app(chunk)
202 out = ''.join(result)
203 try:
204 return str(out)
205 except UnicodeError:
206 return out.encode(self.codec,self.encoding_errors)
207
177 208 def __str__(self):
178 209 """Evaluate and substitute the appropriate parts of the string."""
179 210
180 211 # We need to skip enough frames to get to the actual caller outside of
181 212 # Itpl.
182 213 frame = sys._getframe(1)
183 214 while frame.f_globals["__name__"] == __name__: frame = frame.f_back
184 215 loc, glob = frame.f_locals, frame.f_globals
185 216
186 result = []
187 for live, chunk in self.chunks:
188 if live: result.append(str(eval(chunk,glob,loc)))
189 else: result.append(chunk)
190
191 return ''.join(result)
192
217 return self._str(glob,loc)
218
193 219 class ItplNS(Itpl):
194 220 """Class representing a string with interpolation abilities.
195 221
196 222 This inherits from Itpl, but at creation time a namespace is provided
197 223 where the evaluation will occur. The interpolation becomes a bit more
198 224 efficient, as no traceback needs to be extracte. It also allows the
199 225 caller to supply a different namespace for the interpolation to occur than
200 226 its own."""
201 227
202 def __init__(self, format,globals,locals=None):
228 def __init__(self, format,globals,locals=None,
229 codec='utf_8',encoding_errors='backslashreplace'):
203 230 """ItplNS(format,globals[,locals]) -> interpolating string instance.
204 231
205 232 This constructor, besides a format string, takes a globals dictionary
206 233 and optionally a locals (which defaults to globals if not provided).
207 234
208 235 For further details, see the Itpl constructor."""
209 236
210 237 if locals is None:
211 238 locals = globals
212 239 self.globals = globals
213 240 self.locals = locals
214 Itpl.__init__(self,format)
241 Itpl.__init__(self,format,codec,encoding_errors)
215 242
216 243 def __str__(self):
217 244 """Evaluate and substitute the appropriate parts of the string."""
218 glob = self.globals
219 loc = self.locals
220 result = []
221 for live, chunk in self.chunks:
222 if live: result.append(str(eval(chunk,glob,loc)))
223 else: result.append(chunk)
224 return ''.join(result)
245 return self._str(self.globals,self.locals)
246
247 def __repr__(self):
248 return "<ItplNS %s >" % repr(self.format)
225 249
226 250 # utilities for fast printing
227 251 def itpl(text): return str(Itpl(text))
228 252 def printpl(text): print itpl(text)
229 253 # versions with namespace
230 254 def itplns(text,globals,locals=None): return str(ItplNS(text,globals,locals))
231 255 def printplns(text,globals,locals=None): print itplns(text,globals,locals)
232 256
233 257 class ItplFile:
234 258 """A file object that filters each write() through an interpolator."""
235 259 def __init__(self, file): self.file = file
236 260 def __repr__(self): return "<interpolated " + repr(self.file) + ">"
237 261 def __getattr__(self, attr): return getattr(self.file, attr)
238 262 def write(self, text): self.file.write(str(Itpl(text)))
239 263
240 264 def filter(file=sys.stdout):
241 265 """Return an ItplFile that filters writes to the given file object.
242 266
243 267 'file = filter(file)' replaces 'file' with a filtered object that
244 268 has a write() method. When called with no argument, this creates
245 269 a filter to sys.stdout."""
246 270 return ItplFile(file)
247 271
248 272 def unfilter(ifile=None):
249 273 """Return the original file that corresponds to the given ItplFile.
250 274
251 275 'file = unfilter(file)' undoes the effect of 'file = filter(file)'.
252 276 'sys.stdout = unfilter()' undoes the effect of 'sys.stdout = filter()'."""
253 277 return ifile and ifile.file or sys.stdout.file
@@ -1,574 +1,572 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Classes for handling input/output prompts.
4 4
5 $Id: Prompts.py 564 2005-03-26 22:43:58Z fperez $"""
5 $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $"""
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #*****************************************************************************
13 13
14 14 from IPython import Release
15 15 __author__ = '%s <%s>' % Release.authors['Fernando']
16 16 __license__ = Release.license
17 17 __version__ = Release.version
18 18
19 19 #****************************************************************************
20 20 # Required modules
21 21 import __builtin__
22 22 import os,sys,socket
23 23 import time
24 24 from pprint import pprint,pformat
25 25
26 26 # IPython's own
27 27 from IPython.genutils import *
28 28 from IPython.Struct import Struct
29 29 from IPython.Magic import Macro
30 30 from IPython.Itpl import ItplNS
31 31 from IPython import ColorANSI
32 32
33 33 #****************************************************************************
34 34 #Color schemes for Prompts.
35 35
36 36 PromptColors = ColorANSI.ColorSchemeTable()
37 37 InputColors = ColorANSI.InputTermColors # just a shorthand
38 38 Colors = ColorANSI.TermColors # just a shorthand
39 39
40 40 PromptColors.add_scheme(ColorANSI.ColorScheme(
41 41 'NoColor',
42 42 in_prompt = InputColors.NoColor, # Input prompt
43 43 in_number = InputColors.NoColor, # Input prompt number
44 44 in_prompt2 = InputColors.NoColor, # Continuation prompt
45 45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
46 46
47 47 out_prompt = Colors.NoColor, # Output prompt
48 48 out_number = Colors.NoColor, # Output prompt number
49 49
50 50 normal = Colors.NoColor # color off (usu. Colors.Normal)
51 51 ))
52 52 # make some schemes as instances so we can copy them for modification easily:
53 53 __PColLinux = ColorANSI.ColorScheme(
54 54 'Linux',
55 55 in_prompt = InputColors.Green,
56 56 in_number = InputColors.LightGreen,
57 57 in_prompt2 = InputColors.Green,
58 58 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
59 59
60 60 out_prompt = Colors.Red,
61 61 out_number = Colors.LightRed,
62 62
63 63 normal = Colors.Normal
64 64 )
65 65 # Don't forget to enter it into the table!
66 66 PromptColors.add_scheme(__PColLinux)
67 67 # Slightly modified Linux for light backgrounds
68 68 __PColLightBG = ColorANSI.ColorScheme('LightBG',**__PColLinux.colors.dict().copy())
69 69
70 70 __PColLightBG.colors.update(
71 71 in_prompt = InputColors.Blue,
72 72 in_number = InputColors.LightBlue,
73 73 in_prompt2 = InputColors.Blue
74 74 )
75 75 PromptColors.add_scheme(__PColLightBG)
76 76
77 77 del Colors,InputColors
78 78
79 79 #-----------------------------------------------------------------------------
80 80 def multiple_replace(dict, text):
81 81 """ Replace in 'text' all occurences of any key in the given
82 82 dictionary by its corresponding value. Returns the new string."""
83 83
84 84 # Function by Xavier Defrang, originally found at:
85 85 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
86 86
87 87 # Create a regular expression from the dictionary keys
88 88 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
89 89 # For each match, look-up corresponding value in dictionary
90 90 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
91 91
92 92 #-----------------------------------------------------------------------------
93 93 # Special characters that can be used in prompt templates, mainly bash-like
94 94
95 95 # If $HOME isn't defined (Windows), make it an absurd string so that it can
96 96 # never be expanded out into '~'. Basically anything which can never be a
97 97 # reasonable directory name will do, we just want the $HOME -> '~' operation
98 98 # to become a no-op. We pre-compute $HOME here so it's not done on every
99 99 # prompt call.
100 100
101 101 # FIXME:
102 102
103 103 # - This should be turned into a class which does proper namespace management,
104 104 # since the prompt specials need to be evaluated in a certain namespace.
105 105 # Currently it's just globals, which need to be managed manually by code
106 106 # below.
107 107
108 108 # - I also need to split up the color schemes from the prompt specials
109 109 # somehow. I don't have a clean design for that quite yet.
110 110
111 111 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
112 112
113 113 # We precompute a few more strings here for the prompt_specials, which are
114 114 # fixed once ipython starts. This reduces the runtime overhead of computing
115 115 # prompt strings.
116 116 USER = os.environ.get("USER")
117 117 HOSTNAME = socket.gethostname()
118 118 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
119 119 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
120 120
121 121 prompt_specials_color = {
122 122 # Prompt/history count
123 123 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
124 124 '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 125 # Prompt/history count, with the actual digits replaced by dots. Used
126 126 # mainly in continuation prompts (prompt_in2)
127 127 '\\D': '${"."*len(str(self.cache.prompt_count))}',
128 128 # Current working directory
129 129 '\\w': '${os.getcwd()}',
130 130 # Current time
131 131 '\\t' : '${time.strftime("%H:%M:%S")}',
132 132 # Basename of current working directory.
133 133 # (use os.sep to make this portable across OSes)
134 134 '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
135 135 # These X<N> are an extension to the normal bash prompts. They return
136 136 # N terms of the path, after replacing $HOME with '~'
137 137 '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,
138 138 '\\X1': '${self.cwd_filt(1)}',
139 139 '\\X2': '${self.cwd_filt(2)}',
140 140 '\\X3': '${self.cwd_filt(3)}',
141 141 '\\X4': '${self.cwd_filt(4)}',
142 142 '\\X5': '${self.cwd_filt(5)}',
143 143 # Y<N> are similar to X<N>, but they show '~' if it's the directory
144 144 # N+1 in the list. Somewhat like %cN in tcsh.
145 145 '\\Y0': '${self.cwd_filt2(0)}',
146 146 '\\Y1': '${self.cwd_filt2(1)}',
147 147 '\\Y2': '${self.cwd_filt2(2)}',
148 148 '\\Y3': '${self.cwd_filt2(3)}',
149 149 '\\Y4': '${self.cwd_filt2(4)}',
150 150 '\\Y5': '${self.cwd_filt2(5)}',
151 151 # Hostname up to first .
152 152 '\\h': HOSTNAME_SHORT,
153 153 # Full hostname
154 154 '\\H': HOSTNAME,
155 155 # Username of current user
156 156 '\\u': USER,
157 157 # Escaped '\'
158 158 '\\\\': '\\',
159 159 # Newline
160 160 '\\n': '\n',
161 161 # Carriage return
162 162 '\\r': '\r',
163 163 # Release version
164 164 '\\v': __version__,
165 165 # Root symbol ($ or #)
166 166 '\\$': ROOT_SYMBOL,
167 167 }
168 168
169 169 # A copy of the prompt_specials dictionary but with all color escapes removed,
170 170 # so we can correctly compute the prompt length for the auto_rewrite method.
171 171 prompt_specials_nocolor = prompt_specials_color.copy()
172 172 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
173 173 prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}'
174 174
175 175 # Add in all the InputTermColors color escapes as valid prompt characters.
176 176 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
177 177 # with a color name which may begin with a letter used by any other of the
178 178 # allowed specials. This of course means that \\C will never be allowed for
179 179 # anything else.
180 180 input_colors = ColorANSI.InputTermColors
181 181 for _color in dir(input_colors):
182 182 if _color[0] != '_':
183 183 c_name = '\\C_'+_color
184 184 prompt_specials_color[c_name] = getattr(input_colors,_color)
185 185 prompt_specials_nocolor[c_name] = ''
186 186
187 187 # we default to no color for safety. Note that prompt_specials is a global
188 188 # variable used by all prompt objects.
189 189 prompt_specials = prompt_specials_nocolor
190 190
191 191 #-----------------------------------------------------------------------------
192 192 def str_safe(arg):
193 193 """Convert to a string, without ever raising an exception.
194 194
195 195 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
196 196 error message."""
197
197
198 198 try:
199 return str(arg)
199 out = str(arg)
200 except UnicodeError:
201 try:
202 out = arg.encode('utf_8','replace')
203 except Exception,msg:
204 # let's keep this little duplication here, so that the most common
205 # case doesn't suffer from a double try wrapping.
206 out = '<ERROR: %s>' % msg
200 207 except Exception,msg:
201 return '<ERROR: %s>' % msg
208 out = '<ERROR: %s>' % msg
209 return out
202 210
203 211 class BasePrompt:
204 212 """Interactive prompt similar to Mathematica's."""
205 213 def __init__(self,cache,sep,prompt,pad_left=False):
206 214
207 215 # Hack: we access information about the primary prompt through the
208 216 # cache argument. We need this, because we want the secondary prompt
209 217 # to be aligned with the primary one. Color table info is also shared
210 218 # by all prompt classes through the cache. Nice OO spaghetti code!
211 219 self.cache = cache
212 220 self.sep = sep
213 221
214 222 # regexp to count the number of spaces at the end of a prompt
215 223 # expression, useful for prompt auto-rewriting
216 224 self.rspace = re.compile(r'(\s*)$')
217 225 # Flag to left-pad prompt strings to match the length of the primary
218 226 # prompt
219 227 self.pad_left = pad_left
220 228 # Set template to create each actual prompt (where numbers change)
221 229 self.p_template = prompt
222 230 self.set_p_str()
223 231
224 232 def set_p_str(self):
225 233 """ Set the interpolating prompt strings.
226 234
227 235 This must be called every time the color settings change, because the
228 236 prompt_specials global may have changed."""
229 237
230 238 import os,time # needed in locals for prompt string handling
231 239 loc = locals()
232 240 self.p_str = ItplNS('%s%s%s' %
233 241 ('${self.sep}${self.col_p}',
234 242 multiple_replace(prompt_specials, self.p_template),
235 243 '${self.col_norm}'),self.cache.user_ns,loc)
236 244
237 245 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
238 246 self.p_template),
239 247 self.cache.user_ns,loc)
240 248
241 249 def write(self,msg): # dbg
242 250 sys.stdout.write(msg)
243 251 return ''
244 252
245 253 def __str__(self):
246 254 """Return a string form of the prompt.
247 255
248 256 This for is useful for continuation and output prompts, since it is
249 257 left-padded to match lengths with the primary one (if the
250 258 self.pad_left attribute is set)."""
251 259
252 260 out_str = str_safe(self.p_str)
253 261 if self.pad_left:
254 262 # We must find the amount of padding required to match lengths,
255 263 # taking the color escapes (which are invisible on-screen) into
256 264 # account.
257 265 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
258 266 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
259 267 return format % out_str
260 268 else:
261 269 return out_str
262 270
263 271 # these path filters are put in as methods so that we can control the
264 272 # namespace where the prompt strings get evaluated
265 273 def cwd_filt(self,depth):
266 274 """Return the last depth elements of the current working directory.
267 275
268 276 $HOME is always replaced with '~'.
269 277 If depth==0, the full path is returned."""
270 278
271 279 cwd = os.getcwd().replace(HOME,"~")
272 280 out = os.sep.join(cwd.split(os.sep)[-depth:])
273 281 if out:
274 282 return out
275 283 else:
276 284 return os.sep
277 285
278 286 def cwd_filt2(self,depth):
279 287 """Return the last depth elements of the current working directory.
280 288
281 289 $HOME is always replaced with '~'.
282 290 If depth==0, the full path is returned."""
283 291
284 292 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
285 293 if '~' in cwd and len(cwd) == depth+1:
286 294 depth += 1
287 295 out = os.sep.join(cwd[-depth:])
288 296 if out:
289 297 return out
290 298 else:
291 299 return os.sep
292 300
293 301 class Prompt1(BasePrompt):
294 302 """Input interactive prompt similar to Mathematica's."""
295 303
296 304 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
297 305 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
298 306
299 307 def set_colors(self):
300 308 self.set_p_str()
301 309 Colors = self.cache.color_table.active_colors # shorthand
302 310 self.col_p = Colors.in_prompt
303 311 self.col_num = Colors.in_number
304 312 self.col_norm = Colors.in_normal
305 313 # We need a non-input version of these escapes for the '--->'
306 314 # auto-call prompts used in the auto_rewrite() method.
307 315 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
308 316 self.col_norm_ni = Colors.normal
309 317
310 318 def __str__(self):
311 319 self.cache.prompt_count += 1
312 320 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
313 321 return str_safe(self.p_str)
314 322
315 323 def auto_rewrite(self):
316 324 """Print a string of the form '--->' which lines up with the previous
317 325 input string. Useful for systems which re-write the user input when
318 326 handling automatically special syntaxes."""
319 327
320 328 curr = str(self.cache.last_prompt)
321 329 nrspaces = len(self.rspace.search(curr).group())
322 330 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
323 331 ' '*nrspaces,self.col_norm_ni)
324 332
325 333 class PromptOut(BasePrompt):
326 334 """Output interactive prompt similar to Mathematica's."""
327 335
328 336 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
329 337 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
330 338 if not self.p_template:
331 339 self.__str__ = lambda: ''
332 340
333 341 def set_colors(self):
334 342 self.set_p_str()
335 343 Colors = self.cache.color_table.active_colors # shorthand
336 344 self.col_p = Colors.out_prompt
337 345 self.col_num = Colors.out_number
338 346 self.col_norm = Colors.normal
339 347
340 348 class Prompt2(BasePrompt):
341 349 """Interactive continuation prompt."""
342 350
343 351 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
344 352 self.cache = cache
345 353 self.p_template = prompt
346 354 self.pad_left = pad_left
347 355 self.set_p_str()
348 356
349 357 def set_p_str(self):
350 358 import os,time # needed in locals for prompt string handling
351 359 loc = locals()
352 360 self.p_str = ItplNS('%s%s%s' %
353 361 ('${self.col_p2}',
354 362 multiple_replace(prompt_specials, self.p_template),
355 363 '$self.col_norm'),
356 364 self.cache.user_ns,loc)
357 365 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
358 366 self.p_template),
359 367 self.cache.user_ns,loc)
360 368
361 369 def set_colors(self):
362 370 self.set_p_str()
363 371 Colors = self.cache.color_table.active_colors
364 372 self.col_p2 = Colors.in_prompt2
365 373 self.col_norm = Colors.in_normal
366 374 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
367 375 # updated their prompt_in2 definitions. Remove eventually.
368 376 self.col_p = Colors.out_prompt
369 377 self.col_num = Colors.out_number
370 378
371 379 #-----------------------------------------------------------------------------
372 380 class CachedOutput:
373 381 """Class for printing output from calculations while keeping a cache of
374 382 reults. It dynamically creates global variables prefixed with _ which
375 383 contain these results.
376 384
377 385 Meant to be used as a sys.displayhook replacement, providing numbered
378 386 prompts and cache services.
379 387
380 388 Initialize with initial and final values for cache counter (this defines
381 389 the maximum size of the cache."""
382 390
383 391 def __init__(self,cache_size,Pprint,colors='NoColor',input_sep='\n',
384 392 output_sep='\n',output_sep2='',user_ns={},
385 393 ps1 = None, ps2 = None,ps_out = None,
386 394 input_hist = None,pad_left=True):
387 395
388 396 cache_size_min = 20
389 397 if cache_size <= 0:
390 398 self.do_full_cache = 0
391 399 cache_size = 0
392 400 elif cache_size < cache_size_min:
393 401 self.do_full_cache = 0
394 402 cache_size = 0
395 403 warn('caching was disabled (min value for cache size is %s).' %
396 404 cache_size_min,level=3)
397 405 else:
398 406 self.do_full_cache = 1
399 407
400 408 self.cache_size = cache_size
401 409 self.input_sep = input_sep
402 410
403 411 # we need a reference to the user-level namespace
404 412 self.user_ns = user_ns
405 413 # and to the user's input
406 414 self.input_hist = input_hist
407 415
408 416 # Set input prompt strings and colors
409 417 if cache_size == 0:
410 418 if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> '
411 419 if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... '
412 420 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
413 421 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
414 422 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
415 423
424 self.color_table = PromptColors
416 425 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
417 426 pad_left=pad_left)
418 427 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
419 428 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
420 429 pad_left=pad_left)
421 self.color_table = PromptColors
422 430 self.set_colors(colors)
423 431
424 432 # other more normal stuff
425 433 # b/c each call to the In[] prompt raises it by 1, even the first.
426 434 self.prompt_count = 0
427 435 self.cache_count = 1
428 436 # Store the last prompt string each time, we need it for aligning
429 437 # continuation and auto-rewrite prompts
430 438 self.last_prompt = ''
431 439 self.entries = [None] # output counter starts at 1 for the user
432 440 self.Pprint = Pprint
433 441 self.output_sep = output_sep
434 442 self.output_sep2 = output_sep2
435 443 self._,self.__,self.___ = '','',''
436 444 self.pprint_types = map(type,[(),[],{}])
437 445
438 446 # these are deliberately global:
439 447 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
440 448 self.user_ns.update(to_user_ns)
441 449
442 450 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
443 451 if p_str is None:
444 452 if self.do_full_cache:
445 453 return cache_def
446 454 else:
447 455 return no_cache_def
448 456 else:
449 457 return p_str
450 458
451 459 def set_colors(self,colors):
452 460 """Set the active color scheme and configure colors for the three
453 461 prompt subsystems."""
454 462
455 463 # FIXME: the prompt_specials global should be gobbled inside this
456 464 # class instead. Do it when cleaning up the whole 3-prompt system.
457 465 global prompt_specials
458 466 if colors.lower()=='nocolor':
459 467 prompt_specials = prompt_specials_nocolor
460 468 else:
461 469 prompt_specials = prompt_specials_color
462 470
463 471 self.color_table.set_active_scheme(colors)
464 472 self.prompt1.set_colors()
465 473 self.prompt2.set_colors()
466 474 self.prompt_out.set_colors()
467 475
468 476 def __call__(self,arg=None):
469 477 """Printing with history cache management.
470 478
471 479 This is invoked everytime the interpreter needs to print, and is
472 480 activated by setting the variable sys.displayhook to it."""
473 481
474 482 # If something injected a '_' variable in __builtin__, delete
475 483 # ipython's automatic one so we don't clobber that. gettext() in
476 484 # particular uses _, so we need to stay away from it.
477 485 if '_' in __builtin__.__dict__:
478 486 try:
479 487 del self.user_ns['_']
480 488 except KeyError:
481 489 pass
482 490 if arg is not None:
491 cout_write = Term.cout.write # fast lookup
483 492 # first handle the cache and counters
484 493 self.update(arg)
485 494 # do not print output if input ends in ';'
486 495 if self.input_hist[self.prompt_count].endswith(';\n'):
487 496 return
488 497 # don't use print, puts an extra space
489 Term.cout.write(self.output_sep)
498 cout_write(self.output_sep)
490 499 if self.do_full_cache:
491 Term.cout.write(str(self.prompt_out))
500 cout_write(str(self.prompt_out))
492 501
493 502 if isinstance(arg,Macro):
494 503 print 'Executing Macro...'
495 504 # in case the macro takes a long time to execute
496 505 Term.cout.flush()
497 506 exec arg.value in self.user_ns
498 507 return None
499 508
500 509 # and now call a possibly user-defined print mechanism
501 510 self.display(arg)
502 Term.cout.write(self.output_sep2)
511 cout_write(self.output_sep2)
503 512 Term.cout.flush()
504 513
505 514 def _display(self,arg):
506 515 """Default printer method, uses pprint.
507 516
508 517 This can be over-ridden by the users to implement special formatting
509 518 of certain types of output."""
510 519
511 520 if self.Pprint:
512 # The following is an UGLY kludge, b/c python fails to properly
513 # identify instances of classes imported in the user namespace
514 # (they have different memory locations, I guess). Structs are
515 # essentially dicts but pprint doesn't know what to do with them.
516 try:
517 if arg.__class__.__module__ == 'Struct' and \
518 arg.__class__.__name__ == 'Struct':
519 out = 'Struct:\n%s' % pformat(arg.dict())
520 else:
521 out = pformat(arg)
522 except:
523 out = pformat(arg)
521 out = pformat(arg)
524 522 if '\n' in out:
525 523 # So that multi-line strings line up with the left column of
526 524 # the screen, instead of having the output prompt mess up
527 525 # their first line.
528 526 Term.cout.write('\n')
529 527 print >>Term.cout, out
530 528 else:
531 529 print >>Term.cout, arg
532 530
533 531 # Assign the default display method:
534 532 display = _display
535 533
536 534 def update(self,arg):
537 535 #print '***cache_count', self.cache_count # dbg
538 536 if self.cache_count >= self.cache_size and self.do_full_cache:
539 537 self.flush()
540 538 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
541 539 # we cause buggy behavior for things like gettext).
542 540 if '_' not in __builtin__.__dict__:
543 541 self.___ = self.__
544 542 self.__ = self._
545 543 self._ = arg
546 544 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
547 545
548 546 # hackish access to top-level namespace to create _1,_2... dynamically
549 547 to_main = {}
550 548 if self.do_full_cache:
551 549 self.cache_count += 1
552 550 self.entries.append(arg)
553 551 new_result = '_'+`self.prompt_count`
554 552 to_main[new_result] = self.entries[-1]
555 553 self.user_ns.update(to_main)
556 554 self.user_ns['_oh'][self.prompt_count] = arg
557 555
558 556 def flush(self):
559 557 if not self.do_full_cache:
560 558 raise ValueError,"You shouldn't have reached the cache flush "\
561 559 "if full caching is not enabled!"
562 560 warn('Output cache limit (currently '+\
563 561 `self.cache_count`+' entries) hit.\n'
564 562 'Flushing cache and resetting history counter...\n'
565 563 'The only history variables available will be _,__,___ and _1\n'
566 564 'with the current result.')
567 565 # delete auto-generated vars from global namespace
568 566 for n in range(1,self.prompt_count + 1):
569 567 key = '_'+`n`
570 568 try:
571 569 del self.user_ns[key]
572 570 except: pass
573 571 self.prompt_count = 1
574 572 self.cache_count = 1
@@ -1,376 +1,375 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Mimic C structs with lots of extra functionality.
3 3
4 $Id: Struct.py 410 2004-11-04 07:58:17Z fperez $"""
4 $Id: Struct.py 638 2005-07-18 03:01:41Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #*****************************************************************************
12 12
13 13 from IPython import Release
14 14 __author__ = '%s <%s>' % Release.authors['Fernando']
15 15 __license__ = Release.license
16 16
17 17 __all__ = ['Struct']
18 18
19 19 import types
20 20 from IPython.genutils import list2dict2
21 21
22 22 class Struct:
23 23 """Class to mimic C structs but also provide convenient dictionary-like
24 24 functionality.
25 25
26 26 Instances can be initialized with a dictionary, a list of key=value pairs
27 27 or both. If both are present, the dictionary must come first.
28 28
29 29 Because Python classes provide direct assignment to their members, it's
30 30 easy to overwrite normal methods (S.copy = 1 would destroy access to
31 31 S.copy()). For this reason, all builtin method names are protected and
32 32 can't be assigned to. An attempt to do s.copy=1 or s['copy']=1 will raise
33 33 a KeyError exception. If you really want to, you can bypass this
34 34 protection by directly assigning to __dict__: s.__dict__['copy']=1 will
35 35 still work. Doing this will break functionality, though. As in most of
36 36 Python, namespace protection is weakly enforced, so feel free to shoot
37 37 yourself if you really want to.
38 38
39 39 Note that this class uses more memory and is *much* slower than a regular
40 40 dictionary, so be careful in situations where memory or performance are
41 41 critical. But for day to day use it should behave fine. It is particularly
42 42 convenient for storing configuration data in programs.
43 43
44 44 +,+=,- and -= are implemented. +/+= do merges (non-destructive updates),
45 45 -/-= remove keys from the original. See the method descripitions.
46 46
47 47 This class allows a quick access syntax: both s.key and s['key'] are
48 48 valid. This syntax has a limitation: each 'key' has to be explicitly
49 49 accessed by its original name. The normal s.key syntax doesn't provide
50 50 access to the keys via variables whose values evaluate to the desired
51 51 keys. An example should clarify this:
52 52
53 53 Define a dictionary and initialize both with dict and k=v pairs:
54 54 >>> d={'a':1,'b':2}
55 55 >>> s=Struct(d,hi=10,ho=20)
56 56 The return of __repr__ can be used to create a new instance:
57 57 >>> s
58 58 Struct({'ho': 20, 'b': 2, 'hi': 10, 'a': 1})
59 59 __str__ (called by print) shows it's not quite a regular dictionary:
60 60 >>> print s
61 61 Struct {a: 1, b: 2, hi: 10, ho: 20}
62 62 Access by explicitly named key with dot notation:
63 63 >>> s.a
64 64 1
65 65 Or like a dictionary:
66 66 >>> s['a']
67 67 1
68 68 If you want a variable to hold the key value, only dictionary access works:
69 69 >>> key='hi'
70 70 >>> s.key
71 71 Traceback (most recent call last):
72 72 File "<stdin>", line 1, in ?
73 73 AttributeError: Struct instance has no attribute 'key'
74 74 >>> s[key]
75 75 10
76 76
77 77 Another limitation of the s.key syntax (and Struct(key=val)
78 78 initialization): keys can't be numbers. But numeric keys can be used and
79 79 accessed using the dictionary syntax. Again, an example:
80 80
81 81 This doesn't work:
82 82 >>> s=Struct(4='hi')
83 83 SyntaxError: keyword can't be an expression
84 84 But this does:
85 85 >>> s=Struct()
86 86 >>> s[4]='hi'
87 87 >>> s
88 88 Struct({4: 'hi'})
89 89 >>> s[4]
90 90 'hi'
91 91 """
92 92
93 93 # Attributes to which __setitem__ and __setattr__ will block access.
94 94 # Note: much of this will be moot in Python 2.2 and will be done in a much
95 95 # cleaner way.
96 96 __protected = ('copy dict dictcopy get has_attr has_key items keys '
97 97 'merge popitem setdefault update values '
98 98 '__make_dict __dict_invert ').split()
99 99
100 100 def __init__(self,dict=None,**kw):
101 101 """Initialize with a dictionary, another Struct, or by giving
102 102 explicitly the list of attributes.
103 103
104 104 Both can be used, but the dictionary must come first:
105 105 Struct(dict), Struct(k1=v1,k2=v2) or Struct(dict,k1=v1,k2=v2).
106 106 """
107 107 if dict is None:
108 108 dict = {}
109 109 if isinstance(dict,Struct):
110 110 dict = dict.dict()
111 111 elif dict and type(dict) is not types.DictType:
112 112 raise TypeError,\
113 113 'Initialize with a dictionary or key=val pairs.'
114 114 dict.update(kw)
115 115 # do the updating by hand to guarantee that we go through the
116 116 # safety-checked __setitem__
117 117 for k,v in dict.items():
118 118 self[k] = v
119 119
120 120 def __setitem__(self,key,value):
121 121 """Used when struct[key] = val calls are made."""
122 122 if key in Struct.__protected:
123 123 raise KeyError,'Key '+`key`+' is a protected key of class Struct.'
124 124 self.__dict__[key] = value
125 125
126 126 def __setattr__(self, key, value):
127 127 """Used when struct.key = val calls are made."""
128 128 self.__setitem__(key,value)
129 129
130 130 def __str__(self):
131 131 """Gets called by print."""
132 132
133 133 return 'Struct('+str(self.__dict__)+')'
134 134
135 135 def __repr__(self):
136 136 """Gets called by repr.
137 137
138 138 A Struct can be recreated with S_new=eval(repr(S_old))."""
139 139 return 'Struct('+str(self.__dict__)+')'
140 140
141 141 def __getitem__(self,key):
142 142 """Allows struct[key] access."""
143 143 return self.__dict__[key]
144 144
145 145 def __contains__(self,key):
146 146 """Allows use of the 'in' operator."""
147 147 return self.__dict__.has_key(key)
148 148
149 149 def __iadd__(self,other):
150 150 """S += S2 is a shorthand for S.merge(S2)."""
151 151 self.merge(other)
152 152 return self
153 153
154 154 def __add__(self,other):
155 155 """S + S2 -> New Struct made form S and S.merge(S2)"""
156 156 Sout = self.copy()
157 157 Sout.merge(other)
158 158 return Sout
159 159
160 160 def __sub__(self,other):
161 161 """Return S1-S2, where all keys in S2 have been deleted (if present)
162 162 from S1."""
163 163 Sout = self.copy()
164 164 Sout -= other
165 165 return Sout
166 166
167 167 def __isub__(self,other):
168 168 """Do in place S = S - S2, meaning all keys in S2 have been deleted
169 169 (if present) from S1."""
170 170
171 171 for k in other.keys():
172 172 if self.has_key(k):
173 173 del self.__dict__[k]
174 174
175 175 def __make_dict(self,__loc_data__,**kw):
176 176 "Helper function for update and merge. Return a dict from data."
177 177
178 178 if __loc_data__ == None:
179 179 dict = {}
180 180 elif type(__loc_data__) is types.DictType:
181 181 dict = __loc_data__
182 182 elif isinstance(__loc_data__,Struct):
183 183 dict = __loc_data__.__dict__
184 184 else:
185 185 raise TypeError, 'Update with a dict, a Struct or key=val pairs.'
186 186 if kw:
187 187 dict.update(kw)
188 188 return dict
189 189
190 190 def __dict_invert(self,dict):
191 191 """Helper function for merge. Takes a dictionary whose values are
192 192 lists and returns a dict. with the elements of each list as keys and
193 193 the original keys as values."""
194 194
195 195 outdict = {}
196 196 for k,lst in dict.items():
197 197 if type(lst) is types.StringType:
198 198 lst = lst.split()
199 199 for entry in lst:
200 200 outdict[entry] = k
201 201 return outdict
202 202
203 203 def clear(self):
204 204 """Clear all attributes."""
205 205 self.__dict__.clear()
206 206
207 207 def copy(self):
208 208 """Return a (shallow) copy of a Struct."""
209 209 return Struct(self.__dict__.copy())
210 210
211 211 def dict(self):
212 212 """Return the Struct's dictionary."""
213 213 return self.__dict__
214 214
215 215 def dictcopy(self):
216 216 """Return a (shallow) copy of the Struct's dictionary."""
217 217 return self.__dict__.copy()
218 218
219 219 def popitem(self):
220 220 """S.popitem() -> (k, v), remove and return some (key, value) pair as
221 221 a 2-tuple; but raise KeyError if S is empty."""
222 222 return self.__dict__.popitem()
223 223
224 224 def update(self,__loc_data__=None,**kw):
225 225 """Update (merge) with data from another Struct or from a dictionary.
226 226 Optionally, one or more key=value pairs can be given at the end for
227 227 direct update."""
228 228
229 229 # The funny name __loc_data__ is to prevent a common variable name which
230 230 # could be a fieled of a Struct to collide with this parameter. The problem
231 231 # would arise if the function is called with a keyword with this same name
232 232 # that a user means to add as a Struct field.
233 233 newdict = Struct.__make_dict(self,__loc_data__,**kw)
234 234 for k,v in newdict.items():
235 235 self[k] = v
236 236
237 237 def merge(self,__loc_data__=None,__conflict_solve=None,**kw):
238 238 """S.merge(data,conflict,k=v1,k=v2,...) -> merge data and k=v into S.
239 239
240 240 This is similar to update(), but much more flexible. First, a dict is
241 241 made from data+key=value pairs. When merging this dict with the Struct
242 242 S, the optional dictionary 'conflict' is used to decide what to do.
243 243
244 244 If conflict is not given, the default behavior is to preserve any keys
245 245 with their current value (the opposite of the update method's
246 246 behavior).
247 247
248 248 conflict is a dictionary of binary functions which will be used to
249 249 solve key conflicts. It must have the following structure:
250 250
251 251 conflict == { fn1 : [Skey1,Skey2,...], fn2 : [Skey3], etc }
252 252
253 253 Values must be lists or whitespace separated strings which are
254 254 automatically converted to lists of strings by calling string.split().
255 255
256 256 Each key of conflict is a function which defines a policy for
257 257 resolving conflicts when merging with the input data. Each fn must be
258 258 a binary function which returns the desired outcome for a key
259 259 conflict. These functions will be called as fn(old,new).
260 260
261 261 An example is probably in order. Suppose you are merging the struct S
262 262 with a dict D and the following conflict policy dict:
263 263
264 264 S.merge(D,{fn1:['a','b',4], fn2:'key_c key_d'})
265 265
266 266 If the key 'a' is found in both S and D, the merge method will call:
267 267
268 268 S['a'] = fn1(S['a'],D['a'])
269 269
270 270 As a convenience, merge() provides five (the most commonly needed)
271 271 pre-defined policies: preserve, update, add, add_flip and add_s. The
272 272 easiest explanation is their implementation:
273 273
274 274 preserve = lambda old,new: old
275 275 update = lambda old,new: new
276 276 add = lambda old,new: old + new
277 277 add_flip = lambda old,new: new + old # note change of order!
278 278 add_s = lambda old,new: old + ' ' + new # only works for strings!
279 279
280 280 You can use those four words (as strings) as keys in conflict instead
281 281 of defining them as functions, and the merge method will substitute
282 282 the appropriate functions for you. That is, the call
283 283
284 284 S.merge(D,{'preserve':'a b c','add':[4,5,'d'],my_function:[6]})
285 285
286 286 will automatically substitute the functions preserve and add for the
287 287 names 'preserve' and 'add' before making any function calls.
288 288
289 289 For more complicated conflict resolution policies, you still need to
290 290 construct your own functions. """
291 291
292 292 data_dict = Struct.__make_dict(self,__loc_data__,**kw)
293 293
294 294 # policies for conflict resolution: two argument functions which return
295 295 # the value that will go in the new struct
296 296 preserve = lambda old,new: old
297 297 update = lambda old,new: new
298 298 add = lambda old,new: old + new
299 299 add_flip = lambda old,new: new + old # note change of order!
300 300 add_s = lambda old,new: old + ' ' + new
301 301
302 302 # default policy is to keep current keys when there's a conflict
303 303 conflict_solve = list2dict2(self.keys(),default = preserve)
304 304
305 305 # the conflict_solve dictionary is given by the user 'inverted': we
306 306 # need a name-function mapping, it comes as a function -> names
307 307 # dict. Make a local copy (b/c we'll make changes), replace user
308 308 # strings for the three builtin policies and invert it.
309 309 if __conflict_solve:
310 310 inv_conflict_solve_user = __conflict_solve.copy()
311 311 for name, func in [('preserve',preserve), ('update',update),
312 312 ('add',add), ('add_flip',add_flip), ('add_s',add_s)]:
313 313 if name in inv_conflict_solve_user.keys():
314 314 inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
315 315 del inv_conflict_solve_user[name]
316 316 conflict_solve.update(Struct.__dict_invert(self,inv_conflict_solve_user))
317 317 #print 'merge. conflict_solve: '; pprint(conflict_solve) # dbg
318 # after Python 2.2, use iterators: for key in data_dict will then work
319 318 #print '*'*50,'in merger. conflict_solver:'; pprint(conflict_solve)
320 for key in data_dict.keys():
319 for key in data_dict:
321 320 if key not in self:
322 321 self[key] = data_dict[key]
323 322 else:
324 323 self[key] = conflict_solve[key](self[key],data_dict[key])
325 324
326 325 def has_key(self,key):
327 326 """Like has_key() dictionary method."""
328 327 return self.__dict__.has_key(key)
329 328
330 329 def hasattr(self,key):
331 330 """hasattr function available as a method.
332 331
333 332 Implemented like has_key, to make sure that all available keys in the
334 333 internal dictionary of the Struct appear also as attributes (even
335 334 numeric keys)."""
336 335 return self.__dict__.has_key(key)
337 336
338 337 def items(self):
339 338 """Return the items in the Struct's dictionary, in the same format
340 339 as a call to {}.items()."""
341 340 return self.__dict__.items()
342 341
343 342 def keys(self):
344 343 """Return the keys in the Struct's dictionary, in the same format
345 344 as a call to {}.keys()."""
346 345 return self.__dict__.keys()
347 346
348 347 def values(self,keys=None):
349 348 """Return the values in the Struct's dictionary, in the same format
350 349 as a call to {}.values().
351 350
352 351 Can be called with an optional argument keys, which must be a list or
353 352 tuple of keys. In this case it returns only the values corresponding
354 353 to those keys (allowing a form of 'slicing' for Structs)."""
355 354 if not keys:
356 355 return self.__dict__.values()
357 356 else:
358 357 ret=[]
359 358 for k in keys:
360 359 ret.append(self[k])
361 360 return ret
362 361
363 362 def get(self,attr,val=None):
364 363 """S.get(k[,d]) -> S[k] if S.has_key(k), else d. d defaults to None."""
365 364 try:
366 365 return self[attr]
367 366 except KeyError:
368 367 return val
369 368
370 369 def setdefault(self,attr,val=None):
371 370 """S.setdefault(k[,d]) -> S.get(k,d), also set S[k]=d if not S.has_key(k)"""
372 371 if not self.has_key(attr):
373 372 self[attr] = val
374 373 return self.get(attr,val)
375 374 # end class Struct
376 375
@@ -1,504 +1,495 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Manage background (threaded) jobs conveniently from an interactive shell.
3 3
4 4 This module provides a BackgroundJobManager class. This is the main class
5 5 meant for public usage, it implements an object which can create and manage
6 6 new background jobs.
7 7
8 8 It also provides the actual job classes managed by these BackgroundJobManager
9 9 objects, see their docstrings below.
10 10
11 11
12 12 This system was inspired by discussions with B. Granger and the
13 13 BackgroundCommand class described in the book Python Scripting for
14 14 Computational Science, by H. P. Langtangen:
15 15
16 16 http://folk.uio.no/hpl/scripting
17 17
18 18 (although ultimately no code from this text was used, as IPython's system is a
19 19 separate implementation).
20 20
21 $Id: background_jobs.py 515 2005-02-15 07:41:41Z fperez $
21 $Id: background_jobs.py 638 2005-07-18 03:01:41Z fperez $
22 22 """
23 23
24 24 #*****************************************************************************
25 25 # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu>
26 26 #
27 27 # Distributed under the terms of the BSD License. The full license is in
28 28 # the file COPYING, distributed as part of this software.
29 29 #*****************************************************************************
30 30
31 31 from IPython import Release
32 32 __author__ = '%s <%s>' % Release.authors['Fernando']
33 33 __license__ = Release.license
34 34
35 35 # Code begins
36 36 import threading,sys
37 37
38 38 from IPython.ultraTB import AutoFormattedTB
39 39 from IPython.genutils import warn,error
40 40
41 # declares Python 2.2 compatibility symbols:
42 try:
43 basestring
44 except NameError:
45 import types
46 basestring = (types.StringType, types.UnicodeType)
47 True = 1==1
48 False = 1==0
49
50 41 class BackgroundJobManager:
51 42 """Class to manage a pool of backgrounded threaded jobs.
52 43
53 44 Below, we assume that 'jobs' is a BackgroundJobManager instance.
54 45
55 46 Usage summary (see the method docstrings for details):
56 47
57 48 jobs.new(...) -> start a new job
58 49
59 50 jobs() or jobs.status() -> print status summary of all jobs
60 51
61 52 jobs[N] -> returns job number N.
62 53
63 54 foo = jobs[N].result -> assign to variable foo the result of job N
64 55
65 56 jobs[N].traceback() -> print the traceback of dead job N
66 57
67 58 jobs.remove(N) -> remove (finished) job N
68 59
69 60 jobs.flush_finished() -> remove all finished jobs
70 61
71 62 As a convenience feature, BackgroundJobManager instances provide the
72 63 utility result and traceback methods which retrieve the corresponding
73 64 information from the jobs list:
74 65
75 66 jobs.result(N) <--> jobs[N].result
76 67 jobs.traceback(N) <--> jobs[N].traceback()
77 68
78 69 While this appears minor, it allows you to use tab completion
79 70 interactively on the job manager instance.
80 71
81 72 In interactive mode, IPython provides the magic fuction %bg for quick
82 73 creation of backgrounded expression-based jobs. Type bg? for details."""
83 74
84 75 def __init__(self):
85 76 # Lists for job management
86 77 self.jobs_run = []
87 78 self.jobs_comp = []
88 79 self.jobs_dead = []
89 80 # A dict of all jobs, so users can easily access any of them
90 81 self.jobs_all = {}
91 82 # For reporting
92 83 self._comp_report = []
93 84 self._dead_report = []
94 85 # Store status codes locally for fast lookups
95 86 self._s_created = BackgroundJobBase.stat_created_c
96 87 self._s_running = BackgroundJobBase.stat_running_c
97 88 self._s_completed = BackgroundJobBase.stat_completed_c
98 89 self._s_dead = BackgroundJobBase.stat_dead_c
99 90
100 91 def new(self,func_or_exp,*args,**kwargs):
101 92 """Add a new background job and start it in a separate thread.
102 93
103 94 There are two types of jobs which can be created:
104 95
105 96 1. Jobs based on expressions which can be passed to an eval() call.
106 97 The expression must be given as a string. For example:
107 98
108 99 job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]])
109 100
110 101 The given expression is passed to eval(), along with the optional
111 102 global/local dicts provided. If no dicts are given, they are
112 103 extracted automatically from the caller's frame.
113 104
114 105 A Python statement is NOT a valid eval() expression. Basically, you
115 106 can only use as an eval() argument something which can go on the right
116 107 of an '=' sign and be assigned to a variable.
117 108
118 109 For example,"print 'hello'" is not valid, but '2+3' is.
119 110
120 111 2. Jobs given a function object, optionally passing additional
121 112 positional arguments:
122 113
123 114 job_manager.new(myfunc,x,y)
124 115
125 116 The function is called with the given arguments.
126 117
127 118 If you need to pass keyword arguments to your function, you must
128 119 supply them as a dict named kw:
129 120
130 121 job_manager.new(myfunc,x,y,kw=dict(z=1))
131 122
132 123 The reason for this assymmetry is that the new() method needs to
133 124 maintain access to its own keywords, and this prevents name collisions
134 125 between arguments to new() and arguments to your own functions.
135 126
136 127 In both cases, the result is stored in the job.result field of the
137 128 background job object.
138 129
139 130
140 131 Notes and caveats:
141 132
142 133 1. All threads running share the same standard output. Thus, if your
143 134 background jobs generate output, it will come out on top of whatever
144 135 you are currently writing. For this reason, background jobs are best
145 136 used with silent functions which simply return their output.
146 137
147 138 2. Threads also all work within the same global namespace, and this
148 139 system does not lock interactive variables. So if you send job to the
149 140 background which operates on a mutable object for a long time, and
150 141 start modifying that same mutable object interactively (or in another
151 142 backgrounded job), all sorts of bizarre behaviour will occur.
152 143
153 144 3. If a background job is spending a lot of time inside a C extension
154 145 module which does not release the Python Global Interpreter Lock
155 146 (GIL), this will block the IPython prompt. This is simply because the
156 147 Python interpreter can only switch between threads at Python
157 148 bytecodes. While the execution is inside C code, the interpreter must
158 149 simply wait unless the extension module releases the GIL.
159 150
160 151 4. There is no way, due to limitations in the Python threads library,
161 152 to kill a thread once it has started."""
162 153
163 154 if callable(func_or_exp):
164 155 kw = kwargs.get('kw',{})
165 156 job = BackgroundJobFunc(func_or_exp,*args,**kw)
166 157 elif isinstance(func_or_exp,basestring):
167 158 if not args:
168 159 frame = sys._getframe(1)
169 160 glob, loc = frame.f_globals, frame.f_locals
170 161 elif len(args)==1:
171 162 glob = loc = args[0]
172 163 elif len(args)==2:
173 164 glob,loc = args
174 165 else:
175 166 raise ValueError,\
176 167 'Expression jobs take at most 2 args (globals,locals)'
177 168 job = BackgroundJobExpr(func_or_exp,glob,loc)
178 169 else:
179 170 raise
180 171 jkeys = self.jobs_all.keys()
181 172 if jkeys:
182 173 job.num = max(jkeys)+1
183 174 else:
184 175 job.num = 0
185 176 self.jobs_run.append(job)
186 177 self.jobs_all[job.num] = job
187 178 print 'Starting job # %s in a separate thread.' % job.num
188 179 job.start()
189 180 return job
190 181
191 182 def __getitem__(self,key):
192 183 return self.jobs_all[key]
193 184
194 185 def __call__(self):
195 186 """An alias to self.status(),
196 187
197 188 This allows you to simply call a job manager instance much like the
198 189 Unix jobs shell command."""
199 190
200 191 return self.status()
201 192
202 193 def _update_status(self):
203 194 """Update the status of the job lists.
204 195
205 196 This method moves finished jobs to one of two lists:
206 197 - self.jobs_comp: jobs which completed successfully
207 198 - self.jobs_dead: jobs which finished but died.
208 199
209 200 It also copies those jobs to corresponding _report lists. These lists
210 201 are used to report jobs completed/dead since the last update, and are
211 202 then cleared by the reporting function after each call."""
212 203
213 204 run,comp,dead = self._s_running,self._s_completed,self._s_dead
214 205 jobs_run = self.jobs_run
215 206 for num in range(len(jobs_run)):
216 207 job = jobs_run[num]
217 208 stat = job.stat_code
218 209 if stat == run:
219 210 continue
220 211 elif stat == comp:
221 212 self.jobs_comp.append(job)
222 213 self._comp_report.append(job)
223 214 jobs_run[num] = False
224 215 elif stat == dead:
225 216 self.jobs_dead.append(job)
226 217 self._dead_report.append(job)
227 218 jobs_run[num] = False
228 219 self.jobs_run = filter(None,self.jobs_run)
229 220
230 221 def _group_report(self,group,name):
231 222 """Report summary for a given job group.
232 223
233 224 Return True if the group had any elements."""
234 225
235 226 if group:
236 227 print '%s jobs:' % name
237 228 for job in group:
238 229 print '%s : %s' % (job.num,job)
239 230 print
240 231 return True
241 232
242 233 def _group_flush(self,group,name):
243 234 """Flush a given job group
244 235
245 236 Return True if the group had any elements."""
246 237
247 238 njobs = len(group)
248 239 if njobs:
249 240 plural = {1:''}.setdefault(njobs,'s')
250 241 print 'Flushing %s %s job%s.' % (njobs,name,plural)
251 242 group[:] = []
252 243 return True
253 244
254 245 def _status_new(self):
255 246 """Print the status of newly finished jobs.
256 247
257 248 Return True if any new jobs are reported.
258 249
259 250 This call resets its own state every time, so it only reports jobs
260 251 which have finished since the last time it was called."""
261 252
262 253 self._update_status()
263 254 new_comp = self._group_report(self._comp_report,'Completed')
264 255 new_dead = self._group_report(self._dead_report,
265 256 'Dead, call job.traceback() for details')
266 257 self._comp_report[:] = []
267 258 self._dead_report[:] = []
268 259 return new_comp or new_dead
269 260
270 261 def status(self,verbose=0):
271 262 """Print a status of all jobs currently being managed."""
272 263
273 264 self._update_status()
274 265 self._group_report(self.jobs_run,'Running')
275 266 self._group_report(self.jobs_comp,'Completed')
276 267 self._group_report(self.jobs_dead,'Dead')
277 268 # Also flush the report queues
278 269 self._comp_report[:] = []
279 270 self._dead_report[:] = []
280 271
281 272 def remove(self,num):
282 273 """Remove a finished (completed or dead) job."""
283 274
284 275 try:
285 276 job = self.jobs_all[num]
286 277 except KeyError:
287 278 error('Job #%s not found' % num)
288 279 else:
289 280 stat_code = job.stat_code
290 281 if stat_code == self._s_running:
291 282 error('Job #%s is still running, it can not be removed.' % num)
292 283 return
293 284 elif stat_code == self._s_completed:
294 285 self.jobs_comp.remove(job)
295 286 elif stat_code == self._s_dead:
296 287 self.jobs_dead.remove(job)
297 288
298 289 def flush_finished(self):
299 290 """Flush all jobs finished (completed and dead) from lists.
300 291
301 292 Running jobs are never flushed.
302 293
303 294 It first calls _status_new(), to update info. If any jobs have
304 295 completed since the last _status_new() call, the flush operation
305 296 aborts."""
306 297
307 298 if self._status_new():
308 299 error('New jobs completed since last '\
309 300 '_status_new(), aborting flush.')
310 301 return
311 302
312 303 # Remove the finished jobs from the master dict
313 304 jobs_all = self.jobs_all
314 305 for job in self.jobs_comp+self.jobs_dead:
315 306 del(jobs_all[job.num])
316 307
317 308 # Now flush these lists completely
318 309 fl_comp = self._group_flush(self.jobs_comp,'Completed')
319 310 fl_dead = self._group_flush(self.jobs_dead,'Dead')
320 311 if not (fl_comp or fl_dead):
321 312 print 'No jobs to flush.'
322 313
323 314 def result(self,num):
324 315 """result(N) -> return the result of job N."""
325 316 try:
326 317 return self.jobs_all[num].result
327 318 except KeyError:
328 319 error('Job #%s not found' % num)
329 320
330 321 def traceback(self,num):
331 322 try:
332 323 self.jobs_all[num].traceback()
333 324 except KeyError:
334 325 error('Job #%s not found' % num)
335 326
336 327
337 328 class BackgroundJobBase(threading.Thread):
338 329 """Base class to build BackgroundJob classes.
339 330
340 331 The derived classes must implement:
341 332
342 333 - Their own __init__, since the one here raises NotImplementedError. The
343 334 derived constructor must call self._init() at the end, to provide common
344 335 initialization.
345 336
346 337 - A strform attribute used in calls to __str__.
347 338
348 339 - A call() method, which will make the actual execution call and must
349 340 return a value to be held in the 'result' field of the job object."""
350 341
351 342 # Class constants for status, in string and as numerical codes (when
352 343 # updating jobs lists, we don't want to do string comparisons). This will
353 344 # be done at every user prompt, so it has to be as fast as possible
354 345 stat_created = 'Created'; stat_created_c = 0
355 346 stat_running = 'Running'; stat_running_c = 1
356 347 stat_completed = 'Completed'; stat_completed_c = 2
357 348 stat_dead = 'Dead (Exception), call job.traceback() for details'
358 349 stat_dead_c = -1
359 350
360 351 def __init__(self):
361 352 raise NotImplementedError, \
362 353 "This class can not be instantiated directly."
363 354
364 355 def _init(self):
365 356 """Common initialization for all BackgroundJob objects"""
366 357
367 358 for attr in ['call','strform']:
368 359 assert hasattr(self,attr), "Missing attribute <%s>" % attr
369 360
370 361 # The num tag can be set by an external job manager
371 362 self.num = None
372 363
373 364 self.status = BackgroundJobBase.stat_created
374 365 self.stat_code = BackgroundJobBase.stat_created_c
375 366 self.finished = False
376 367 self.result = '<BackgroundJob has not completed>'
377 368 # reuse the ipython traceback handler if we can get to it, otherwise
378 369 # make a new one
379 370 try:
380 371 self._make_tb = __IPYTHON__.InteractiveTB.text
381 372 except:
382 373 self._make_tb = AutoFormattedTB(mode = 'Context',
383 374 color_scheme='NoColor',
384 375 tb_offset = 1).text
385 376 # Hold a formatted traceback if one is generated.
386 377 self._tb = None
387 378
388 379 threading.Thread.__init__(self)
389 380
390 381 def __str__(self):
391 382 return self.strform
392 383
393 384 def __repr__(self):
394 385 return '<BackgroundJob: %s>' % self.strform
395 386
396 387 def traceback(self):
397 388 print self._tb
398 389
399 390 def run(self):
400 391 try:
401 392 self.status = BackgroundJobBase.stat_running
402 393 self.stat_code = BackgroundJobBase.stat_running_c
403 394 self.result = self.call()
404 395 except:
405 396 self.status = BackgroundJobBase.stat_dead
406 397 self.stat_code = BackgroundJobBase.stat_dead_c
407 398 self.finished = None
408 399 self.result = ('<BackgroundJob died, call job.traceback() for details>')
409 400 self._tb = self._make_tb()
410 401 else:
411 402 self.status = BackgroundJobBase.stat_completed
412 403 self.stat_code = BackgroundJobBase.stat_completed_c
413 404 self.finished = True
414 405
415 406 class BackgroundJobExpr(BackgroundJobBase):
416 407 """Evaluate an expression as a background job (uses a separate thread)."""
417 408
418 409 def __init__(self,expression,glob=None,loc=None):
419 410 """Create a new job from a string which can be fed to eval().
420 411
421 412 global/locals dicts can be provided, which will be passed to the eval
422 413 call."""
423 414
424 415 # fail immediately if the given expression can't be compiled
425 416 self.code = compile(expression,'<BackgroundJob compilation>','eval')
426 417
427 418 if glob is None:
428 419 glob = {}
429 420 if loc is None:
430 421 loc = {}
431 422
432 423 self.expression = self.strform = expression
433 424 self.glob = glob
434 425 self.loc = loc
435 426 self._init()
436 427
437 428 def call(self):
438 429 return eval(self.code,self.glob,self.loc)
439 430
440 431 class BackgroundJobFunc(BackgroundJobBase):
441 432 """Run a function call as a background job (uses a separate thread)."""
442 433
443 434 def __init__(self,func,*args,**kwargs):
444 435 """Create a new job from a callable object.
445 436
446 437 Any positional arguments and keyword args given to this constructor
447 438 after the initial callable are passed directly to it."""
448 439
449 440 assert callable(func),'first argument must be callable'
450 441
451 442 if args is None:
452 443 args = []
453 444 if kwargs is None:
454 445 kwargs = {}
455 446
456 447 self.func = func
457 448 self.args = args
458 449 self.kwargs = kwargs
459 450 # The string form will only include the function passed, because
460 451 # generating string representations of the arguments is a potentially
461 452 # _very_ expensive operation (e.g. with large arrays).
462 453 self.strform = str(func)
463 454 self._init()
464 455
465 456 def call(self):
466 457 return self.func(*self.args,**self.kwargs)
467 458
468 459
469 460 if __name__=='__main__':
470 461
471 462 import time
472 463
473 464 def sleepfunc(interval=2,*a,**kw):
474 465 args = dict(interval=interval,
475 466 args=a,
476 467 kwargs=kw)
477 468 time.sleep(interval)
478 469 return args
479 470
480 471 def diefunc(interval=2,*a,**kw):
481 472 time.sleep(interval)
482 473 die
483 474
484 475 def printfunc(interval=1,reps=5):
485 476 for n in range(reps):
486 477 time.sleep(interval)
487 478 print 'In the background...'
488 479
489 480 jobs = BackgroundJobManager()
490 481 # first job will have # 0
491 482 jobs.new(sleepfunc,4)
492 483 jobs.new(sleepfunc,kw={'reps':2})
493 484 # This makes a job which will die
494 485 jobs.new(diefunc,1)
495 486 jobs.new('printfunc(1,3)')
496 487
497 488 # after a while, you can get the traceback of a dead job. Run the line
498 489 # below again interactively until it prints a traceback (check the status
499 490 # of the job):
500 491 print jobs[1].status
501 492 jobs[1].traceback()
502 493
503 494 # Run this line again until the printed result changes
504 495 print "The result of job #0 is:",jobs[0].result
@@ -1,1519 +1,1508 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 General purpose utilities.
4 4
5 5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 6 these things are also convenient when working at the command line.
7 7
8 $Id: genutils.py 633 2005-07-17 01:03:15Z tzanko $"""
8 $Id: genutils.py 638 2005-07-18 03:01:41Z fperez $"""
9 9
10 10 #*****************************************************************************
11 11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
12 12 #
13 13 # Distributed under the terms of the BSD License. The full license is in
14 14 # the file COPYING, distributed as part of this software.
15 15 #*****************************************************************************
16 16
17 17 from IPython import Release
18 18 __author__ = '%s <%s>' % Release.authors['Fernando']
19 19 __license__ = Release.license
20 20
21 21 #****************************************************************************
22 22 # required modules
23 23 import __main__
24 24 import types,commands,time,sys,os,re,shutil
25 25 import tempfile
26 import codecs
26 27 from IPython.Itpl import Itpl,itpl,printpl
27 28 from IPython import DPyGetOpt
28 29
30 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
31 # 2.2-friendly
32 try:
33 basestring
34 except NameError:
35 import types
36 basestring = (types.StringType, types.UnicodeType)
37 True = 1==1
38 False = 1==0
39
40 def enumerate(obj):
41 i = -1
42 for item in obj:
43 i += 1
44 yield i, item
45
46 # add these to the builtin namespace, so that all modules find them
47 import __builtin__
48 __builtin__.basestring = basestring
49 __builtin__.True = True
50 __builtin__.False = False
51 __builtin__.enumerate = enumerate
52
29 53 #****************************************************************************
30 54 # Exceptions
31 55 class Error(Exception):
32 56 """Base class for exceptions in this module."""
33 57 pass
34 58
35 59 #----------------------------------------------------------------------------
36 class Stream:
37 """Simple class to hold the various I/O streams in Term"""
38
39 def __init__(self,stream,name):
60 class IOStream:
61 def __init__(self,stream,fallback):
62 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
63 stream = fallback
40 64 self.stream = stream
41 self.name = name
42 try:
43 self.fileno = stream.fileno()
44 except AttributeError:
45 msg = ("Stream <%s> looks suspicious: it lacks a 'fileno' attribute."
46 % name)
47 print >> sys.stderr, 'WARNING:',msg
48 try:
49 self.mode = stream.mode
50 except AttributeError:
51 msg = ("Stream <%s> looks suspicious: it lacks a 'mode' attribute."
52 % name)
53 print >> sys.stderr, 'WARNING:',msg
65 self._swrite = stream.write
66 self.flush = stream.flush
54 67
55 class Term:
68 def write(self,data):
69 try:
70 self._swrite(data)
71 except:
72 try:
73 # print handles some unicode issues which may trip a plain
74 # write() call. Attempt to emulate write() by using a
75 # trailing comma
76 print >> self.stream, data,
77 except:
78 # if we get here, something is seriously broken.
79 print >> sys.stderr, \
80 'ERROR - failed to write data to stream:', stream
81
82 class IOTerm:
56 83 """ Term holds the file or file-like objects for handling I/O operations.
57 84
58 85 These are normally just sys.stdin, sys.stdout and sys.stderr but for
59 86 Windows they can can replaced to allow editing the strings before they are
60 87 displayed."""
61 88
62 89 # In the future, having IPython channel all its I/O operations through
63 90 # this class will make it easier to embed it into other environments which
64 91 # are not a normal terminal (such as a GUI-based shell)
65 in_s = Stream(sys.stdin,'cin')
66 out_s = Stream(sys.stdout,'cout')
67 err_s = Stream(sys.stderr,'cerr')
68
69 # Store the three streams in (err,out,in) order so that if we need to reopen
70 # them, the error channel is reopened first to provide info.
71 streams = [err_s,out_s,in_s]
72
73 # The class globals should be the actual 'bare' streams for normal I/O to work
74 cin = streams[2].stream
75 cout = streams[1].stream
76 cerr = streams[0].stream
77
78 def reopen_all(cls):
79 """Reopen all streams if necessary.
80
81 This should only be called if it is suspected that someting closed
82 accidentally one of the I/O streams."""
83
84 any_closed = 0
85
86 for sn in range(len(cls.streams)):
87 st = cls.streams[sn]
88 if st.stream.closed:
89 any_closed = 1
90 new_stream = os.fdopen(os.dup(st.fileno), st.mode,0)
91 cls.streams[sn] = Stream(new_stream,st.name)
92 print >> cls.streams[0].stream, \
93 '\nWARNING:\nStream Term.%s had to be reopened!' % st.name
94
95 # Rebuild the class globals
96 cls.cin = cls.streams[2].stream
97 cls.cout = cls.streams[1].stream
98 cls.cerr = cls.streams[0].stream
99
100 reopen_all = classmethod(reopen_all)
101
102 def set_stdout(cls,stream):
103 """Set the stream """
104 cls.cout = stream
105 set_stdout = classmethod(set_stdout)
106
107 def set_stderr(cls,stream):
108 cls.cerr = stream
109 set_stderr = classmethod(set_stderr)
92 def __init__(self,cin=None,cout=None,cerr=None):
93 self.cin = IOStream(cin,sys.stdin)
94 self.cout = IOStream(cout,sys.stdout)
95 self.cerr = IOStream(cerr,sys.stderr)
96
97 # Global variable to be used for all I/O
98 Term = IOTerm()
110 99
111 100 # Windows-specific code to load Gary Bishop's readline and configure it
112 101 # automatically for the users
113 102 # Note: os.name on cygwin returns posix, so this should only pick up 'native'
114 103 # windows. Cygwin returns 'cygwin' for sys.platform.
115 104 if os.name == 'nt':
116 105 try:
117 106 import readline
118 107 except ImportError:
119 108 pass
120 109 else:
121 110 try:
122 111 _out = readline.GetOutputFile()
123 112 except AttributeError:
124 113 pass
125 114 else:
126 Term.set_stdout(_out)
127 Term.set_stderr(_out)
115 # Remake Term to use the readline i/o facilities
116 Term = IOTerm(cout=_out,cerr=_out)
128 117 del _out
129 118
130 119 #****************************************************************************
131 120 # Generic warning/error printer, used by everything else
132 121 def warn(msg,level=2,exit_val=1):
133 122 """Standard warning printer. Gives formatting consistency.
134 123
135 124 Output is sent to Term.cerr (sys.stderr by default).
136 125
137 126 Options:
138 127
139 128 -level(2): allows finer control:
140 129 0 -> Do nothing, dummy function.
141 130 1 -> Print message.
142 131 2 -> Print 'WARNING:' + message. (Default level).
143 132 3 -> Print 'ERROR:' + message.
144 133 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
145 134
146 135 -exit_val (1): exit value returned by sys.exit() for a level 4
147 136 warning. Ignored for all other levels."""
148 137
149 138 if level>0:
150 139 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
151 140 print >> Term.cerr, '%s%s' % (header[level],msg)
152 141 if level == 4:
153 142 print >> Term.cerr,'Exiting.\n'
154 143 sys.exit(exit_val)
155 144
156 145 def info(msg):
157 146 """Equivalent to warn(msg,level=1)."""
158 147
159 148 warn(msg,level=1)
160 149
161 150 def error(msg):
162 151 """Equivalent to warn(msg,level=3)."""
163 152
164 153 warn(msg,level=3)
165 154
166 155 def fatal(msg,exit_val=1):
167 156 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
168 157
169 158 warn(msg,exit_val=exit_val,level=4)
170 159
171 160 #----------------------------------------------------------------------------
172 161 StringTypes = types.StringTypes
173 162
174 163 # Basic timing functionality
175 164
176 165 # If possible (Unix), use the resource module instead of time.clock()
177 166 try:
178 167 import resource
179 168 def clock():
180 169 """clock() -> floating point number
181 170
182 171 Return the CPU time in seconds (user time only, system time is
183 172 ignored) since the start of the process. This is done via a call to
184 173 resource.getrusage, so it avoids the wraparound problems in
185 174 time.clock()."""
186 175
187 176 return resource.getrusage(resource.RUSAGE_SELF)[0]
188 177
189 178 def clock2():
190 179 """clock2() -> (t_user,t_system)
191 180
192 181 Similar to clock(), but return a tuple of user/system times."""
193 182 return resource.getrusage(resource.RUSAGE_SELF)[:2]
194 183
195 184 except ImportError:
196 185 clock = time.clock
197 186 def clock2():
198 187 """Under windows, system CPU time can't be measured.
199 188
200 189 This just returns clock() and zero."""
201 190 return time.clock(),0.0
202 191
203 192 def timings_out(reps,func,*args,**kw):
204 193 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
205 194
206 195 Execute a function reps times, return a tuple with the elapsed total
207 196 CPU time in seconds, the time per call and the function's output.
208 197
209 198 Under Unix, the return value is the sum of user+system time consumed by
210 199 the process, computed via the resource module. This prevents problems
211 200 related to the wraparound effect which the time.clock() function has.
212 201
213 202 Under Windows the return value is in wall clock seconds. See the
214 203 documentation for the time module for more details."""
215 204
216 205 reps = int(reps)
217 206 assert reps >=1, 'reps must be >= 1'
218 207 if reps==1:
219 208 start = clock()
220 209 out = func(*args,**kw)
221 210 tot_time = clock()-start
222 211 else:
223 212 rng = xrange(reps-1) # the last time is executed separately to store output
224 213 start = clock()
225 214 for dummy in rng: func(*args,**kw)
226 215 out = func(*args,**kw) # one last time
227 216 tot_time = clock()-start
228 217 av_time = tot_time / reps
229 218 return tot_time,av_time,out
230 219
231 220 def timings(reps,func,*args,**kw):
232 221 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
233 222
234 223 Execute a function reps times, return a tuple with the elapsed total CPU
235 224 time in seconds and the time per call. These are just the first two values
236 225 in timings_out()."""
237 226
238 227 return timings_out(reps,func,*args,**kw)[0:2]
239 228
240 229 def timing(func,*args,**kw):
241 230 """timing(func,*args,**kw) -> t_total
242 231
243 232 Execute a function once, return the elapsed total CPU time in
244 233 seconds. This is just the first value in timings_out()."""
245 234
246 235 return timings_out(1,func,*args,**kw)[0]
247 236
248 237 #****************************************************************************
249 238 # file and system
250 239
251 240 def system(cmd,verbose=0,debug=0,header=''):
252 241 """Execute a system command, return its exit status.
253 242
254 243 Options:
255 244
256 245 - verbose (0): print the command to be executed.
257 246
258 247 - debug (0): only print, do not actually execute.
259 248
260 249 - header (''): Header to print on screen prior to the executed command (it
261 250 is only prepended to the command, no newlines are added).
262 251
263 252 Note: a stateful version of this function is available through the
264 253 SystemExec class."""
265 254
266 255 stat = 0
267 256 if verbose or debug: print header+cmd
268 257 sys.stdout.flush()
269 258 if not debug: stat = os.system(cmd)
270 259 return stat
271 260
272 261 def shell(cmd,verbose=0,debug=0,header=''):
273 262 """Execute a command in the system shell, always return None.
274 263
275 264 Options:
276 265
277 266 - verbose (0): print the command to be executed.
278 267
279 268 - debug (0): only print, do not actually execute.
280 269
281 270 - header (''): Header to print on screen prior to the executed command (it
282 271 is only prepended to the command, no newlines are added).
283 272
284 273 Note: this is similar to genutils.system(), but it returns None so it can
285 274 be conveniently used in interactive loops without getting the return value
286 275 (typically 0) printed many times."""
287 276
288 277 stat = 0
289 278 if verbose or debug: print header+cmd
290 279 # flush stdout so we don't mangle python's buffering
291 280 sys.stdout.flush()
292 281 if not debug:
293 282 os.system(cmd)
294 283
295 284 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
296 285 """Dummy substitute for perl's backquotes.
297 286
298 287 Executes a command and returns the output.
299 288
300 289 Accepts the same arguments as system(), plus:
301 290
302 291 - split(0): if true, the output is returned as a list split on newlines.
303 292
304 293 Note: a stateful version of this function is available through the
305 294 SystemExec class."""
306 295
307 296 if verbose or debug: print header+cmd
308 297 if not debug:
309 298 output = commands.getoutput(cmd)
310 299 if split:
311 300 return output.split('\n')
312 301 else:
313 302 return output
314 303
315 304 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
316 305 """Return (standard output,standard error) of executing cmd in a shell.
317 306
318 307 Accepts the same arguments as system(), plus:
319 308
320 309 - split(0): if true, each of stdout/err is returned as a list split on
321 310 newlines.
322 311
323 312 Note: a stateful version of this function is available through the
324 313 SystemExec class."""
325 314
326 315 if verbose or debug: print header+cmd
327 316 if not cmd:
328 317 if split:
329 318 return [],[]
330 319 else:
331 320 return '',''
332 321 if not debug:
333 322 pin,pout,perr = os.popen3(cmd)
334 323 tout = pout.read().rstrip()
335 324 terr = perr.read().rstrip()
336 325 pin.close()
337 326 pout.close()
338 327 perr.close()
339 328 if split:
340 329 return tout.split('\n'),terr.split('\n')
341 330 else:
342 331 return tout,terr
343 332
344 333 # for compatibility with older naming conventions
345 334 xsys = system
346 335 bq = getoutput
347 336
348 337 class SystemExec:
349 338 """Access the system and getoutput functions through a stateful interface.
350 339
351 340 Note: here we refer to the system and getoutput functions from this
352 341 library, not the ones from the standard python library.
353 342
354 343 This class offers the system and getoutput functions as methods, but the
355 344 verbose, debug and header parameters can be set for the instance (at
356 345 creation time or later) so that they don't need to be specified on each
357 346 call.
358 347
359 348 For efficiency reasons, there's no way to override the parameters on a
360 349 per-call basis other than by setting instance attributes. If you need
361 350 local overrides, it's best to directly call system() or getoutput().
362 351
363 352 The following names are provided as alternate options:
364 353 - xsys: alias to system
365 354 - bq: alias to getoutput
366 355
367 356 An instance can then be created as:
368 357 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
369 358
370 359 And used as:
371 360 >>> sysexec.xsys('pwd')
372 361 >>> dirlist = sysexec.bq('ls -l')
373 362 """
374 363
375 364 def __init__(self,verbose=0,debug=0,header='',split=0):
376 365 """Specify the instance's values for verbose, debug and header."""
377 366 setattr_list(self,'verbose debug header split')
378 367
379 368 def system(self,cmd):
380 369 """Stateful interface to system(), with the same keyword parameters."""
381 370
382 371 system(cmd,self.verbose,self.debug,self.header)
383 372
384 373 def shell(self,cmd):
385 374 """Stateful interface to shell(), with the same keyword parameters."""
386 375
387 376 shell(cmd,self.verbose,self.debug,self.header)
388 377
389 378 xsys = system # alias
390 379
391 380 def getoutput(self,cmd):
392 381 """Stateful interface to getoutput()."""
393 382
394 383 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
395 384
396 385 def getoutputerror(self,cmd):
397 386 """Stateful interface to getoutputerror()."""
398 387
399 388 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
400 389
401 390 bq = getoutput # alias
402 391
403 392 #-----------------------------------------------------------------------------
404 393 def mutex_opts(dict,ex_op):
405 394 """Check for presence of mutually exclusive keys in a dict.
406 395
407 396 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
408 397 for op1,op2 in ex_op:
409 398 if op1 in dict and op2 in dict:
410 399 raise ValueError,'\n*** ERROR in Arguments *** '\
411 400 'Options '+op1+' and '+op2+' are mutually exclusive.'
412 401
413 402 #-----------------------------------------------------------------------------
414 403 def filefind(fname,alt_dirs = None):
415 404 """Return the given filename either in the current directory, if it
416 405 exists, or in a specified list of directories.
417 406
418 407 ~ expansion is done on all file and directory names.
419 408
420 409 Upon an unsuccessful search, raise an IOError exception."""
421 410
422 411 if alt_dirs is None:
423 412 try:
424 413 alt_dirs = get_home_dir()
425 414 except HomeDirError:
426 415 alt_dirs = os.getcwd()
427 416 search = [fname] + list_strings(alt_dirs)
428 417 search = map(os.path.expanduser,search)
429 418 #print 'search list for',fname,'list:',search # dbg
430 419 fname = search[0]
431 420 if os.path.isfile(fname):
432 421 return fname
433 422 for direc in search[1:]:
434 423 testname = os.path.join(direc,fname)
435 424 #print 'testname',testname # dbg
436 425 if os.path.isfile(testname):
437 426 return testname
438 427 raise IOError,'File' + `fname` + \
439 428 ' not found in current or supplied directories:' + `alt_dirs`
440 429
441 430 #----------------------------------------------------------------------------
442 431 def target_outdated(target,deps):
443 432 """Determine whether a target is out of date.
444 433
445 434 target_outdated(target,deps) -> 1/0
446 435
447 436 deps: list of filenames which MUST exist.
448 437 target: single filename which may or may not exist.
449 438
450 439 If target doesn't exist or is older than any file listed in deps, return
451 440 true, otherwise return false.
452 441 """
453 442 try:
454 443 target_time = os.path.getmtime(target)
455 444 except os.error:
456 445 return 1
457 446 for dep in deps:
458 447 dep_time = os.path.getmtime(dep)
459 448 if dep_time > target_time:
460 449 #print "For target",target,"Dep failed:",dep # dbg
461 450 #print "times (dep,tar):",dep_time,target_time # dbg
462 451 return 1
463 452 return 0
464 453
465 454 #-----------------------------------------------------------------------------
466 455 def target_update(target,deps,cmd):
467 456 """Update a target with a given command given a list of dependencies.
468 457
469 458 target_update(target,deps,cmd) -> runs cmd if target is outdated.
470 459
471 460 This is just a wrapper around target_outdated() which calls the given
472 461 command if target is outdated."""
473 462
474 463 if target_outdated(target,deps):
475 464 xsys(cmd)
476 465
477 466 #----------------------------------------------------------------------------
478 467 def unquote_ends(istr):
479 468 """Remove a single pair of quotes from the endpoints of a string."""
480 469
481 470 if not istr:
482 471 return istr
483 472 if (istr[0]=="'" and istr[-1]=="'") or \
484 473 (istr[0]=='"' and istr[-1]=='"'):
485 474 return istr[1:-1]
486 475 else:
487 476 return istr
488 477
489 478 #----------------------------------------------------------------------------
490 479 def process_cmdline(argv,names=[],defaults={},usage=''):
491 480 """ Process command-line options and arguments.
492 481
493 482 Arguments:
494 483
495 484 - argv: list of arguments, typically sys.argv.
496 485
497 486 - names: list of option names. See DPyGetOpt docs for details on options
498 487 syntax.
499 488
500 489 - defaults: dict of default values.
501 490
502 491 - usage: optional usage notice to print if a wrong argument is passed.
503 492
504 493 Return a dict of options and a list of free arguments."""
505 494
506 495 getopt = DPyGetOpt.DPyGetOpt()
507 496 getopt.setIgnoreCase(0)
508 497 getopt.parseConfiguration(names)
509 498
510 499 try:
511 500 getopt.processArguments(argv)
512 501 except:
513 502 print usage
514 503 warn(`sys.exc_value`,level=4)
515 504
516 505 defaults.update(getopt.optionValues)
517 506 args = getopt.freeValues
518 507
519 508 return defaults,args
520 509
521 510 #----------------------------------------------------------------------------
522 511 def optstr2types(ostr):
523 512 """Convert a string of option names to a dict of type mappings.
524 513
525 514 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
526 515
527 516 This is used to get the types of all the options in a string formatted
528 517 with the conventions of DPyGetOpt. The 'type' None is used for options
529 518 which are strings (they need no further conversion). This function's main
530 519 use is to get a typemap for use with read_dict().
531 520 """
532 521
533 522 typeconv = {None:'',int:'',float:''}
534 523 typemap = {'s':None,'i':int,'f':float}
535 524 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
536 525
537 526 for w in ostr.split():
538 527 oname,alias,otype = opt_re.match(w).groups()
539 528 if otype == '' or alias == '!': # simple switches are integers too
540 529 otype = 'i'
541 530 typeconv[typemap[otype]] += oname + ' '
542 531 return typeconv
543 532
544 533 #----------------------------------------------------------------------------
545 534 def read_dict(filename,type_conv=None,**opt):
546 535
547 536 """Read a dictionary of key=value pairs from an input file, optionally
548 537 performing conversions on the resulting values.
549 538
550 539 read_dict(filename,type_conv,**opt) -> dict
551 540
552 541 Only one value per line is accepted, the format should be
553 542 # optional comments are ignored
554 543 key value\n
555 544
556 545 Args:
557 546
558 547 - type_conv: A dictionary specifying which keys need to be converted to
559 548 which types. By default all keys are read as strings. This dictionary
560 549 should have as its keys valid conversion functions for strings
561 550 (int,long,float,complex, or your own). The value for each key
562 551 (converter) should be a whitespace separated string containing the names
563 552 of all the entries in the file to be converted using that function. For
564 553 keys to be left alone, use None as the conversion function (only needed
565 554 with purge=1, see below).
566 555
567 556 - opt: dictionary with extra options as below (default in parens)
568 557
569 558 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
570 559 of the dictionary to be returned. If purge is going to be used, the
571 560 set of keys to be left as strings also has to be explicitly specified
572 561 using the (non-existent) conversion function None.
573 562
574 563 fs(None): field separator. This is the key/value separator to be used
575 564 when parsing the file. The None default means any whitespace [behavior
576 565 of string.split()].
577 566
578 567 strip(0): if 1, strip string values of leading/trailinig whitespace.
579 568
580 569 warn(1): warning level if requested keys are not found in file.
581 570 - 0: silently ignore.
582 571 - 1: inform but proceed.
583 572 - 2: raise KeyError exception.
584 573
585 574 no_empty(0): if 1, remove keys with whitespace strings as a value.
586 575
587 576 unique([]): list of keys (or space separated string) which can't be
588 577 repeated. If one such key is found in the file, each new instance
589 578 overwrites the previous one. For keys not listed here, the behavior is
590 579 to make a list of all appearances.
591 580
592 581 Example:
593 582 If the input file test.ini has:
594 583 i 3
595 584 x 4.5
596 585 y 5.5
597 586 s hi ho
598 587 Then:
599 588
600 589 >>> type_conv={int:'i',float:'x',None:'s'}
601 590 >>> read_dict('test.ini')
602 591 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
603 592 >>> read_dict('test.ini',type_conv)
604 593 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
605 594 >>> read_dict('test.ini',type_conv,purge=1)
606 595 {'i': 3, 's': 'hi ho', 'x': 4.5}
607 596 """
608 597
609 598 # starting config
610 599 opt.setdefault('purge',0)
611 600 opt.setdefault('fs',None) # field sep defaults to any whitespace
612 601 opt.setdefault('strip',0)
613 602 opt.setdefault('warn',1)
614 603 opt.setdefault('no_empty',0)
615 604 opt.setdefault('unique','')
616 605 if type(opt['unique']) in StringTypes:
617 606 unique_keys = qw(opt['unique'])
618 607 elif type(opt['unique']) in (types.TupleType,types.ListType):
619 608 unique_keys = opt['unique']
620 609 else:
621 610 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
622 611
623 612 dict = {}
624 613 # first read in table of values as strings
625 614 file = open(filename,'r')
626 615 for line in file.readlines():
627 616 line = line.strip()
628 617 if len(line) and line[0]=='#': continue
629 618 if len(line)>0:
630 619 lsplit = line.split(opt['fs'],1)
631 620 try:
632 621 key,val = lsplit
633 622 except ValueError:
634 623 key,val = lsplit[0],''
635 624 key = key.strip()
636 625 if opt['strip']: val = val.strip()
637 626 if val == "''" or val == '""': val = ''
638 627 if opt['no_empty'] and (val=='' or val.isspace()):
639 628 continue
640 629 # if a key is found more than once in the file, build a list
641 630 # unless it's in the 'unique' list. In that case, last found in file
642 631 # takes precedence. User beware.
643 632 try:
644 633 if dict[key] and key in unique_keys:
645 634 dict[key] = val
646 635 elif type(dict[key]) is types.ListType:
647 636 dict[key].append(val)
648 637 else:
649 638 dict[key] = [dict[key],val]
650 639 except KeyError:
651 640 dict[key] = val
652 641 # purge if requested
653 642 if opt['purge']:
654 643 accepted_keys = qwflat(type_conv.values())
655 644 for key in dict.keys():
656 645 if key in accepted_keys: continue
657 646 del(dict[key])
658 647 # now convert if requested
659 648 if type_conv==None: return dict
660 649 conversions = type_conv.keys()
661 650 try: conversions.remove(None)
662 651 except: pass
663 652 for convert in conversions:
664 653 for val in qw(type_conv[convert]):
665 654 try:
666 655 dict[val] = convert(dict[val])
667 656 except KeyError,e:
668 657 if opt['warn'] == 0:
669 658 pass
670 659 elif opt['warn'] == 1:
671 660 print >>sys.stderr, 'Warning: key',val,\
672 661 'not found in file',filename
673 662 elif opt['warn'] == 2:
674 663 raise KeyError,e
675 664 else:
676 665 raise ValueError,'Warning level must be 0,1 or 2'
677 666
678 667 return dict
679 668
680 669 #----------------------------------------------------------------------------
681 670 def flag_calls(func):
682 671 """Wrap a function to detect and flag when it gets called.
683 672
684 673 This is a decorator which takes a function and wraps it in a function with
685 674 a 'called' attribute. wrapper.called is initialized to False.
686 675
687 676 The wrapper.called attribute is set to False right before each call to the
688 677 wrapped function, so if the call fails it remains False. After the call
689 678 completes, wrapper.called is set to True and the output is returned.
690 679
691 680 Testing for truth in wrapper.called allows you to determine if a call to
692 681 func() was attempted and succeeded."""
693 682
694 683 def wrapper(*args,**kw):
695 684 wrapper.called = False
696 685 out = func(*args,**kw)
697 686 wrapper.called = True
698 687 return out
699 688
700 689 wrapper.called = False
701 690 wrapper.__doc__ = func.__doc__
702 691 return wrapper
703 692
704 693 #----------------------------------------------------------------------------
705 694 class HomeDirError(Error):
706 695 pass
707 696
708 697 def get_home_dir():
709 698 """Return the closest possible equivalent to a 'home' directory.
710 699
711 700 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
712 701
713 702 Currently only Posix and NT are implemented, a HomeDirError exception is
714 703 raised for all other OSes. """
715 704
716 705 try:
717 706 return os.environ['HOME']
718 707 except KeyError:
719 708 if os.name == 'posix':
720 709 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
721 710 elif os.name == 'nt':
722 711 # For some strange reason, win9x returns 'nt' for os.name.
723 712 try:
724 713 return os.path.join(os.environ['HOMEDRIVE'],os.environ['HOMEPATH'])
725 714 except:
726 715 try:
727 716 # Use the registry to get the 'My Documents' folder.
728 717 import _winreg as wreg
729 718 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
730 719 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
731 720 homedir = wreg.QueryValueEx(key,'Personal')[0]
732 721 key.Close()
733 722 return homedir
734 723 except:
735 724 return 'C:\\'
736 725 elif os.name == 'dos':
737 726 # Desperate, may do absurd things in classic MacOS. May work under DOS.
738 727 return 'C:\\'
739 728 else:
740 729 raise HomeDirError,'support for your operating system not implemented.'
741 730
742 731 #****************************************************************************
743 732 # strings and text
744 733
745 734 class LSString(str):
746 735 """String derivative with a special access attributes.
747 736
748 737 These are normal strings, but with the special attributes:
749 738
750 739 .l (or .list) : value as list (split on newlines).
751 740 .n (or .nlstr): original value (the string itself).
752 741 .s (or .spstr): value as whitespace-separated string.
753 742
754 743 Any values which require transformations are computed only once and
755 744 cached.
756 745
757 746 Such strings are very useful to efficiently interact with the shell, which
758 747 typically only understands whitespace-separated options for commands."""
759 748
760 749 def get_list(self):
761 750 try:
762 751 return self.__list
763 752 except AttributeError:
764 753 self.__list = self.split('\n')
765 754 return self.__list
766 755
767 756 l = list = property(get_list)
768 757
769 758 def get_spstr(self):
770 759 try:
771 760 return self.__spstr
772 761 except AttributeError:
773 762 self.__spstr = self.replace('\n',' ')
774 763 return self.__spstr
775 764
776 765 s = spstr = property(get_spstr)
777 766
778 767 def get_nlstr(self):
779 768 return self
780 769
781 770 n = nlstr = property(get_nlstr)
782 771
783 772 class SList(list):
784 773 """List derivative with a special access attributes.
785 774
786 775 These are normal lists, but with the special attributes:
787 776
788 777 .l (or .list) : value as list (the list itself).
789 778 .n (or .nlstr): value as a string, joined on newlines.
790 779 .s (or .spstr): value as a string, joined on spaces.
791 780
792 781 Any values which require transformations are computed only once and
793 782 cached."""
794 783
795 784 def get_list(self):
796 785 return self
797 786
798 787 l = list = property(get_list)
799 788
800 789 def get_spstr(self):
801 790 try:
802 791 return self.__spstr
803 792 except AttributeError:
804 793 self.__spstr = ' '.join(self)
805 794 return self.__spstr
806 795
807 796 s = spstr = property(get_spstr)
808 797
809 798 def get_nlstr(self):
810 799 try:
811 800 return self.__nlstr
812 801 except AttributeError:
813 802 self.__nlstr = '\n'.join(self)
814 803 return self.__nlstr
815 804
816 805 n = nlstr = property(get_nlstr)
817 806
818 807 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
819 808 """Take multiple lines of input.
820 809
821 810 A list with each line of input as a separate element is returned when a
822 811 termination string is entered (defaults to a single '.'). Input can also
823 812 terminate via EOF (^D in Unix, ^Z-RET in Windows).
824 813
825 814 Lines of input which end in \\ are joined into single entries (and a
826 815 secondary continuation prompt is issued as long as the user terminates
827 816 lines with \\). This allows entering very long strings which are still
828 817 meant to be treated as single entities.
829 818 """
830 819
831 820 try:
832 821 if header:
833 822 header += '\n'
834 823 lines = [raw_input(header + ps1)]
835 824 except EOFError:
836 825 return []
837 826 terminate = [terminate_str]
838 827 try:
839 828 while lines[-1:] != terminate:
840 829 new_line = raw_input(ps1)
841 830 while new_line.endswith('\\'):
842 831 new_line = new_line[:-1] + raw_input(ps2)
843 832 lines.append(new_line)
844 833
845 834 return lines[:-1] # don't return the termination command
846 835 except EOFError:
847 836 print
848 837 return lines
849 838
850 839 #----------------------------------------------------------------------------
851 840 def raw_input_ext(prompt='', ps2='... '):
852 841 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
853 842
854 843 line = raw_input(prompt)
855 844 while line.endswith('\\'):
856 845 line = line[:-1] + raw_input(ps2)
857 846 return line
858 847
859 848 #----------------------------------------------------------------------------
860 849 def ask_yes_no(prompt,default=None):
861 850 """Asks a question and returns an integer 1/0 (y/n) answer.
862 851
863 852 If default is given (one of 'y','n'), it is used if the user input is
864 853 empty. Otherwise the question is repeated until an answer is given.
865 854 If EOF occurs 20 times consecutively, the default answer is assumed,
866 855 or if there is no default, an exception is raised to prevent infinite
867 856 loops.
868 857
869 858 Valid answers are: y/yes/n/no (match is not case sensitive)."""
870 859
871 860 answers = {'y':1,'n':0,'yes':1,'no':0}
872 861 ans = None
873 862 eofs, max_eofs = 0, 20
874 863 while ans not in answers.keys():
875 864 try:
876 865 ans = raw_input(prompt+' ').lower()
877 866 if not ans: # response was an empty string
878 867 ans = default
879 868 eofs = 0
880 869 except (EOFError,KeyboardInterrupt):
881 870 eofs = eofs + 1
882 871 if eofs >= max_eofs:
883 872 if default in answers.keys():
884 873 ans = default
885 874 else:
886 875 raise
887 876
888 877 return answers[ans]
889 878
890 879 #----------------------------------------------------------------------------
891 880 class EvalDict:
892 881 """
893 882 Emulate a dict which evaluates its contents in the caller's frame.
894 883
895 884 Usage:
896 885 >>>number = 19
897 886 >>>text = "python"
898 887 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
899 888 """
900 889
901 890 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
902 891 # modified (shorter) version of:
903 892 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
904 893 # Skip Montanaro (skip@pobox.com).
905 894
906 895 def __getitem__(self, name):
907 896 frame = sys._getframe(1)
908 897 return eval(name, frame.f_globals, frame.f_locals)
909 898
910 899 EvalString = EvalDict # for backwards compatibility
911 900 #----------------------------------------------------------------------------
912 901 def qw(words,flat=0,sep=None,maxsplit=-1):
913 902 """Similar to Perl's qw() operator, but with some more options.
914 903
915 904 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
916 905
917 906 words can also be a list itself, and with flat=1, the output will be
918 907 recursively flattened. Examples:
919 908
920 909 >>> qw('1 2')
921 910 ['1', '2']
922 911 >>> qw(['a b','1 2',['m n','p q']])
923 912 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
924 913 >>> qw(['a b','1 2',['m n','p q']],flat=1)
925 914 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
926 915
927 916 if type(words) in StringTypes:
928 917 return [word.strip() for word in words.split(sep,maxsplit)
929 918 if word and not word.isspace() ]
930 919 if flat:
931 920 return flatten(map(qw,words,[1]*len(words)))
932 921 return map(qw,words)
933 922
934 923 #----------------------------------------------------------------------------
935 924 def qwflat(words,sep=None,maxsplit=-1):
936 925 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
937 926 return qw(words,1,sep,maxsplit)
938 927
939 928 #-----------------------------------------------------------------------------
940 929 def list_strings(arg):
941 930 """Always return a list of strings, given a string or list of strings
942 931 as input."""
943 932
944 933 if type(arg) in StringTypes: return [arg]
945 934 else: return arg
946 935
947 936 #----------------------------------------------------------------------------
948 937 def grep(pat,list,case=1):
949 938 """Simple minded grep-like function.
950 939 grep(pat,list) returns occurrences of pat in list, None on failure.
951 940
952 941 It only does simple string matching, with no support for regexps. Use the
953 942 option case=0 for case-insensitive matching."""
954 943
955 944 # This is pretty crude. At least it should implement copying only references
956 945 # to the original data in case it's big. Now it copies the data for output.
957 946 out=[]
958 947 if case:
959 948 for term in list:
960 949 if term.find(pat)>-1: out.append(term)
961 950 else:
962 951 lpat=pat.lower()
963 952 for term in list:
964 953 if term.lower().find(lpat)>-1: out.append(term)
965 954
966 955 if len(out): return out
967 956 else: return None
968 957
969 958 #----------------------------------------------------------------------------
970 959 def dgrep(pat,*opts):
971 960 """Return grep() on dir()+dir(__builtins__).
972 961
973 962 A very common use of grep() when working interactively."""
974 963
975 964 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
976 965
977 966 #----------------------------------------------------------------------------
978 967 def idgrep(pat):
979 968 """Case-insensitive dgrep()"""
980 969
981 970 return dgrep(pat,0)
982 971
983 972 #----------------------------------------------------------------------------
984 973 def igrep(pat,list):
985 974 """Synonym for case-insensitive grep."""
986 975
987 976 return grep(pat,list,case=0)
988 977
989 978 #----------------------------------------------------------------------------
990 979 def indent(str,nspaces=4,ntabs=0):
991 980 """Indent a string a given number of spaces or tabstops.
992 981
993 982 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
994 983 """
995 984 if str is None:
996 985 return
997 986 ind = '\t'*ntabs+' '*nspaces
998 987 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
999 988 if outstr.endswith(os.linesep+ind):
1000 989 return outstr[:-len(ind)]
1001 990 else:
1002 991 return outstr
1003 992
1004 993 #-----------------------------------------------------------------------------
1005 994 def native_line_ends(filename,backup=1):
1006 995 """Convert (in-place) a file to line-ends native to the current OS.
1007 996
1008 997 If the optional backup argument is given as false, no backup of the
1009 998 original file is left. """
1010 999
1011 1000 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1012 1001
1013 1002 bak_filename = filename + backup_suffixes[os.name]
1014 1003
1015 1004 original = open(filename).read()
1016 1005 shutil.copy2(filename,bak_filename)
1017 1006 try:
1018 1007 new = open(filename,'wb')
1019 1008 new.write(os.linesep.join(original.splitlines()))
1020 1009 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1021 1010 new.close()
1022 1011 except:
1023 1012 os.rename(bak_filename,filename)
1024 1013 if not backup:
1025 1014 try:
1026 1015 os.remove(bak_filename)
1027 1016 except:
1028 1017 pass
1029 1018
1030 1019 #----------------------------------------------------------------------------
1031 1020 def get_pager_cmd(pager_cmd = None):
1032 1021 """Return a pager command.
1033 1022
1034 1023 Makes some attempts at finding an OS-correct one."""
1035 1024
1036 1025 if os.name == 'posix':
1037 1026 default_pager_cmd = 'less -r' # -r for color control sequences
1038 1027 elif os.name in ['nt','dos']:
1039 1028 default_pager_cmd = 'type'
1040 1029
1041 1030 if pager_cmd is None:
1042 1031 try:
1043 1032 pager_cmd = os.environ['PAGER']
1044 1033 except:
1045 1034 pager_cmd = default_pager_cmd
1046 1035 return pager_cmd
1047 1036
1048 1037 #-----------------------------------------------------------------------------
1049 1038 def get_pager_start(pager,start):
1050 1039 """Return the string for paging files with an offset.
1051 1040
1052 1041 This is the '+N' argument which less and more (under Unix) accept.
1053 1042 """
1054 1043
1055 1044 if pager in ['less','more']:
1056 1045 if start:
1057 1046 start_string = '+' + str(start)
1058 1047 else:
1059 1048 start_string = ''
1060 1049 else:
1061 1050 start_string = ''
1062 1051 return start_string
1063 1052
1064 1053 #----------------------------------------------------------------------------
1065 1054 def page_dumb(strng,start=0,screen_lines=25):
1066 1055 """Very dumb 'pager' in Python, for when nothing else works.
1067 1056
1068 1057 Only moves forward, same interface as page(), except for pager_cmd and
1069 1058 mode."""
1070 1059
1071 1060 out_ln = strng.splitlines()[start:]
1072 1061 screens = chop(out_ln,screen_lines-1)
1073 1062 if len(screens) == 1:
1074 1063 print >>Term.cout, os.linesep.join(screens[0])
1075 1064 else:
1076 1065 for scr in screens[0:-1]:
1077 1066 print >>Term.cout, os.linesep.join(scr)
1078 1067 ans = raw_input('---Return to continue, q to quit--- ')
1079 1068 if ans.lower().startswith('q'):
1080 1069 return
1081 1070 print >>Term.cout, os.linesep.join(screens[-1])
1082 1071
1083 1072 #----------------------------------------------------------------------------
1084 1073 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1085 1074 """Print a string, piping through a pager after a certain length.
1086 1075
1087 1076 The screen_lines parameter specifies the number of *usable* lines of your
1088 1077 terminal screen (total lines minus lines you need to reserve to show other
1089 1078 information).
1090 1079
1091 1080 If you set screen_lines to a number <=0, page() will try to auto-determine
1092 1081 your screen size and will only use up to (screen_size+screen_lines) for
1093 1082 printing, paging after that. That is, if you want auto-detection but need
1094 1083 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1095 1084 auto-detection without any lines reserved simply use screen_lines = 0.
1096 1085
1097 1086 If a string won't fit in the allowed lines, it is sent through the
1098 1087 specified pager command. If none given, look for PAGER in the environment,
1099 1088 and ultimately default to less.
1100 1089
1101 1090 If no system pager works, the string is sent through a 'dumb pager'
1102 1091 written in python, very simplistic.
1103 1092 """
1104 1093
1105 1094 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1106 1095 TERM = os.environ.get('TERM','dumb')
1107 1096 if TERM in ['dumb','emacs'] and os.name != 'nt':
1108 1097 print strng
1109 1098 return
1110 1099 # chop off the topmost part of the string we don't want to see
1111 1100 str_lines = strng.split(os.linesep)[start:]
1112 1101 str_toprint = os.linesep.join(str_lines)
1113 1102 num_newlines = len(str_lines)
1114 1103 len_str = len(str_toprint)
1115 1104
1116 1105 # Dumb heuristics to guesstimate number of on-screen lines the string
1117 1106 # takes. Very basic, but good enough for docstrings in reasonable
1118 1107 # terminals. If someone later feels like refining it, it's not hard.
1119 1108 numlines = max(num_newlines,int(len_str/80)+1)
1120 1109
1121 1110 screen_lines_def = 25 # default value if we can't auto-determine
1122 1111
1123 1112 # auto-determine screen size
1124 1113 if screen_lines <= 0:
1125 1114 if TERM=='xterm':
1126 1115 try:
1127 1116 import curses
1128 1117 if hasattr(curses,'initscr'):
1129 1118 use_curses = 1
1130 1119 else:
1131 1120 use_curses = 0
1132 1121 except ImportError:
1133 1122 use_curses = 0
1134 1123 else:
1135 1124 # curses causes problems on many terminals other than xterm.
1136 1125 use_curses = 0
1137 1126 if use_curses:
1138 1127 scr = curses.initscr()
1139 1128 screen_lines_real,screen_cols = scr.getmaxyx()
1140 1129 curses.endwin()
1141 1130 screen_lines += screen_lines_real
1142 1131 #print '***Screen size:',screen_lines_real,'lines x',\
1143 1132 #screen_cols,'columns.' # dbg
1144 1133 else:
1145 1134 screen_lines += screen_lines_def
1146 1135
1147 1136 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1148 1137 if numlines <= screen_lines :
1149 1138 #print '*** normal print' # dbg
1150 1139 print >>Term.cout, str_toprint
1151 1140 else:
1152 1141 # Try to open pager and default to internal one if that fails.
1153 1142 # All failure modes are tagged as 'retval=1', to match the return
1154 1143 # value of a failed system command. If any intermediate attempt
1155 1144 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1156 1145 pager_cmd = get_pager_cmd(pager_cmd)
1157 1146 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1158 1147 if os.name == 'nt':
1159 1148 if pager_cmd.startswith('type'):
1160 1149 # The default WinXP 'type' command is failing on complex strings.
1161 1150 retval = 1
1162 1151 else:
1163 1152 tmpname = tempfile.mktemp('.txt')
1164 1153 tmpfile = file(tmpname,'wt')
1165 1154 tmpfile.write(strng)
1166 1155 tmpfile.close()
1167 1156 cmd = "%s < %s" % (pager_cmd,tmpname)
1168 1157 if os.system(cmd):
1169 1158 retval = 1
1170 1159 else:
1171 1160 retval = None
1172 1161 os.remove(tmpname)
1173 1162 else:
1174 1163 try:
1175 1164 retval = None
1176 1165 # if I use popen4, things hang. No idea why.
1177 1166 #pager,shell_out = os.popen4(pager_cmd)
1178 1167 pager = os.popen(pager_cmd,'w')
1179 1168 pager.write(strng)
1180 1169 pager.close()
1181 1170 retval = pager.close() # success returns None
1182 1171 except IOError,msg: # broken pipe when user quits
1183 1172 if msg.args == (32,'Broken pipe'):
1184 1173 retval = None
1185 1174 else:
1186 1175 retval = 1
1187 1176 except OSError:
1188 1177 # Other strange problems, sometimes seen in Win2k/cygwin
1189 1178 retval = 1
1190 1179 if retval is not None:
1191 1180 page_dumb(strng,screen_lines=screen_lines)
1192 1181
1193 1182 #----------------------------------------------------------------------------
1194 1183 def page_file(fname,start = 0, pager_cmd = None):
1195 1184 """Page a file, using an optional pager command and starting line.
1196 1185 """
1197 1186
1198 1187 pager_cmd = get_pager_cmd(pager_cmd)
1199 1188 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1200 1189
1201 1190 try:
1202 1191 if os.environ['TERM'] in ['emacs','dumb']:
1203 1192 raise EnvironmentError
1204 1193 xsys(pager_cmd + ' ' + fname)
1205 1194 except:
1206 1195 try:
1207 1196 if start > 0:
1208 1197 start -= 1
1209 1198 page(open(fname).read(),start)
1210 1199 except:
1211 1200 print 'Unable to show file',`fname`
1212 1201
1213 1202 #----------------------------------------------------------------------------
1214 1203 def snip_print(str,width = 75,print_full = 0,header = ''):
1215 1204 """Print a string snipping the midsection to fit in width.
1216 1205
1217 1206 print_full: mode control:
1218 1207 - 0: only snip long strings
1219 1208 - 1: send to page() directly.
1220 1209 - 2: snip long strings and ask for full length viewing with page()
1221 1210 Return 1 if snipping was necessary, 0 otherwise."""
1222 1211
1223 1212 if print_full == 1:
1224 1213 page(header+str)
1225 1214 return 0
1226 1215
1227 1216 print header,
1228 1217 if len(str) < width:
1229 1218 print str
1230 1219 snip = 0
1231 1220 else:
1232 1221 whalf = int((width -5)/2)
1233 1222 print str[:whalf] + ' <...> ' + str[-whalf:]
1234 1223 snip = 1
1235 1224 if snip and print_full == 2:
1236 1225 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1237 1226 page(str)
1238 1227 return snip
1239 1228
1240 1229 #****************************************************************************
1241 1230 # lists, dicts and structures
1242 1231
1243 1232 def belong(candidates,checklist):
1244 1233 """Check whether a list of items appear in a given list of options.
1245 1234
1246 1235 Returns a list of 1 and 0, one for each candidate given."""
1247 1236
1248 1237 return [x in checklist for x in candidates]
1249 1238
1250 1239 #----------------------------------------------------------------------------
1251 1240 def uniq_stable(elems):
1252 1241 """uniq_stable(elems) -> list
1253 1242
1254 1243 Return from an iterable, a list of all the unique elements in the input,
1255 1244 but maintaining the order in which they first appear.
1256 1245
1257 1246 A naive solution to this problem which just makes a dictionary with the
1258 1247 elements as keys fails to respect the stability condition, since
1259 1248 dictionaries are unsorted by nature.
1260 1249
1261 1250 Note: All elements in the input must be valid dictionary keys for this
1262 1251 routine to work, as it internally uses a dictionary for efficiency
1263 1252 reasons."""
1264 1253
1265 1254 unique = []
1266 1255 unique_dict = {}
1267 1256 for nn in elems:
1268 1257 if nn not in unique_dict:
1269 1258 unique.append(nn)
1270 1259 unique_dict[nn] = None
1271 1260 return unique
1272 1261
1273 1262 #----------------------------------------------------------------------------
1274 1263 class NLprinter:
1275 1264 """Print an arbitrarily nested list, indicating index numbers.
1276 1265
1277 1266 An instance of this class called nlprint is available and callable as a
1278 1267 function.
1279 1268
1280 1269 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1281 1270 and using 'sep' to separate the index from the value. """
1282 1271
1283 1272 def __init__(self):
1284 1273 self.depth = 0
1285 1274
1286 1275 def __call__(self,lst,pos='',**kw):
1287 1276 """Prints the nested list numbering levels."""
1288 1277 kw.setdefault('indent',' ')
1289 1278 kw.setdefault('sep',': ')
1290 1279 kw.setdefault('start',0)
1291 1280 kw.setdefault('stop',len(lst))
1292 1281 # we need to remove start and stop from kw so they don't propagate
1293 1282 # into a recursive call for a nested list.
1294 1283 start = kw['start']; del kw['start']
1295 1284 stop = kw['stop']; del kw['stop']
1296 1285 if self.depth == 0 and 'header' in kw.keys():
1297 1286 print kw['header']
1298 1287
1299 1288 for idx in range(start,stop):
1300 1289 elem = lst[idx]
1301 1290 if type(elem)==type([]):
1302 1291 self.depth += 1
1303 1292 self.__call__(elem,itpl('$pos$idx,'),**kw)
1304 1293 self.depth -= 1
1305 1294 else:
1306 1295 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1307 1296
1308 1297 nlprint = NLprinter()
1309 1298 #----------------------------------------------------------------------------
1310 1299 def all_belong(candidates,checklist):
1311 1300 """Check whether a list of items ALL appear in a given list of options.
1312 1301
1313 1302 Returns a single 1 or 0 value."""
1314 1303
1315 1304 return 1-(0 in [x in checklist for x in candidates])
1316 1305
1317 1306 #----------------------------------------------------------------------------
1318 1307 def sort_compare(lst1,lst2,inplace = 1):
1319 1308 """Sort and compare two lists.
1320 1309
1321 1310 By default it does it in place, thus modifying the lists. Use inplace = 0
1322 1311 to avoid that (at the cost of temporary copy creation)."""
1323 1312 if not inplace:
1324 1313 lst1 = lst1[:]
1325 1314 lst2 = lst2[:]
1326 1315 lst1.sort(); lst2.sort()
1327 1316 return lst1 == lst2
1328 1317
1329 1318 #----------------------------------------------------------------------------
1330 1319 def mkdict(**kwargs):
1331 1320 """Return a dict from a keyword list.
1332 1321
1333 1322 It's just syntactic sugar for making ditcionary creation more convenient:
1334 1323 # the standard way
1335 1324 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1336 1325 # a cleaner way
1337 1326 >>>data = dict(red=1, green=2, blue=3)
1338 1327
1339 1328 If you need more than this, look at the Struct() class."""
1340 1329
1341 1330 return kwargs
1342 1331
1343 1332 #----------------------------------------------------------------------------
1344 1333 def list2dict(lst):
1345 1334 """Takes a list of (key,value) pairs and turns it into a dict."""
1346 1335
1347 1336 dic = {}
1348 1337 for k,v in lst: dic[k] = v
1349 1338 return dic
1350 1339
1351 1340 #----------------------------------------------------------------------------
1352 1341 def list2dict2(lst,default=''):
1353 1342 """Takes a list and turns it into a dict.
1354 1343 Much slower than list2dict, but more versatile. This version can take
1355 1344 lists with sublists of arbitrary length (including sclars)."""
1356 1345
1357 1346 dic = {}
1358 1347 for elem in lst:
1359 1348 if type(elem) in (types.ListType,types.TupleType):
1360 1349 size = len(elem)
1361 1350 if size == 0:
1362 1351 pass
1363 1352 elif size == 1:
1364 1353 dic[elem] = default
1365 1354 else:
1366 1355 k,v = elem[0], elem[1:]
1367 1356 if len(v) == 1: v = v[0]
1368 1357 dic[k] = v
1369 1358 else:
1370 1359 dic[elem] = default
1371 1360 return dic
1372 1361
1373 1362 #----------------------------------------------------------------------------
1374 1363 def flatten(seq):
1375 1364 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1376 1365
1377 1366 # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in).
1378 1367
1379 1368 # if the x=0 isn't made, a *global* variable x is left over after calling
1380 1369 # this function, with the value of the last element in the return
1381 1370 # list. This does seem like a bug big time to me.
1382 1371
1383 1372 # the problem is fixed with the x=0, which seems to force the creation of
1384 1373 # a local name
1385 1374
1386 1375 x = 0
1387 1376 return [x for subseq in seq for x in subseq]
1388 1377
1389 1378 #----------------------------------------------------------------------------
1390 1379 def get_slice(seq,start=0,stop=None,step=1):
1391 1380 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1392 1381 if stop == None:
1393 1382 stop = len(seq)
1394 1383 item = lambda i: seq[i]
1395 1384 return map(item,xrange(start,stop,step))
1396 1385
1397 1386 #----------------------------------------------------------------------------
1398 1387 def chop(seq,size):
1399 1388 """Chop a sequence into chunks of the given size."""
1400 1389 chunk = lambda i: seq[i:i+size]
1401 1390 return map(chunk,xrange(0,len(seq),size))
1402 1391
1403 1392 #----------------------------------------------------------------------------
1404 1393 def with(object, **args):
1405 1394 """Set multiple attributes for an object, similar to Pascal's with.
1406 1395
1407 1396 Example:
1408 1397 with(jim,
1409 1398 born = 1960,
1410 1399 haircolour = 'Brown',
1411 1400 eyecolour = 'Green')
1412 1401
1413 1402 Credit: Greg Ewing, in
1414 1403 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1415 1404
1416 1405 object.__dict__.update(args)
1417 1406
1418 1407 #----------------------------------------------------------------------------
1419 1408 def setattr_list(obj,alist,nspace = None):
1420 1409 """Set a list of attributes for an object taken from a namespace.
1421 1410
1422 1411 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1423 1412 alist with their values taken from nspace, which must be a dict (something
1424 1413 like locals() will often do) If nspace isn't given, locals() of the
1425 1414 *caller* is used, so in most cases you can omit it.
1426 1415
1427 1416 Note that alist can be given as a string, which will be automatically
1428 1417 split into a list on whitespace. If given as a list, it must be a list of
1429 1418 *strings* (the variable names themselves), not of variables."""
1430 1419
1431 1420 # this grabs the local variables from the *previous* call frame -- that is
1432 1421 # the locals from the function that called setattr_list().
1433 1422 # - snipped from weave.inline()
1434 1423 if nspace is None:
1435 1424 call_frame = sys._getframe().f_back
1436 1425 nspace = call_frame.f_locals
1437 1426
1438 1427 if type(alist) in StringTypes:
1439 1428 alist = alist.split()
1440 1429 for attr in alist:
1441 1430 val = eval(attr,nspace)
1442 1431 setattr(obj,attr,val)
1443 1432
1444 1433 #----------------------------------------------------------------------------
1445 1434 def getattr_list(obj,alist,*args):
1446 1435 """getattr_list(obj,alist[, default]) -> attribute list.
1447 1436
1448 1437 Get a list of named attributes for an object. When a default argument is
1449 1438 given, it is returned when the attribute doesn't exist; without it, an
1450 1439 exception is raised in that case.
1451 1440
1452 1441 Note that alist can be given as a string, which will be automatically
1453 1442 split into a list on whitespace. If given as a list, it must be a list of
1454 1443 *strings* (the variable names themselves), not of variables."""
1455 1444
1456 1445 if type(alist) in StringTypes:
1457 1446 alist = alist.split()
1458 1447 if args:
1459 1448 if len(args)==1:
1460 1449 default = args[0]
1461 1450 return map(lambda attr: getattr(obj,attr,default),alist)
1462 1451 else:
1463 1452 raise ValueError,'getattr_list() takes only one optional argument'
1464 1453 else:
1465 1454 return map(lambda attr: getattr(obj,attr),alist)
1466 1455
1467 1456 #----------------------------------------------------------------------------
1468 1457 def map_method(method,object_list,*argseq,**kw):
1469 1458 """map_method(method,object_list,*args,**kw) -> list
1470 1459
1471 1460 Return a list of the results of applying the methods to the items of the
1472 1461 argument sequence(s). If more than one sequence is given, the method is
1473 1462 called with an argument list consisting of the corresponding item of each
1474 1463 sequence. All sequences must be of the same length.
1475 1464
1476 1465 Keyword arguments are passed verbatim to all objects called.
1477 1466
1478 1467 This is Python code, so it's not nearly as fast as the builtin map()."""
1479 1468
1480 1469 out_list = []
1481 1470 idx = 0
1482 1471 for object in object_list:
1483 1472 try:
1484 1473 handler = getattr(object, method)
1485 1474 except AttributeError:
1486 1475 out_list.append(None)
1487 1476 else:
1488 1477 if argseq:
1489 1478 args = map(lambda lst:lst[idx],argseq)
1490 1479 #print 'ob',object,'hand',handler,'ar',args # dbg
1491 1480 out_list.append(handler(args,**kw))
1492 1481 else:
1493 1482 out_list.append(handler(**kw))
1494 1483 idx += 1
1495 1484 return out_list
1496 1485
1497 1486 #----------------------------------------------------------------------------
1498 1487 # Proposed popitem() extension, written as a method
1499 1488
1500 1489 class NotGiven: pass
1501 1490
1502 1491 def popkey(dct,key,default=NotGiven):
1503 1492 """Return dct[key] and delete dct[key].
1504 1493
1505 1494 If default is given, return it if dct[key] doesn't exist, otherwise raise
1506 1495 KeyError. """
1507 1496
1508 1497 try:
1509 1498 val = dct[key]
1510 1499 except KeyError:
1511 1500 if default is NotGiven:
1512 1501 raise
1513 1502 else:
1514 1503 return default
1515 1504 else:
1516 1505 del dct[key]
1517 1506 return val
1518 1507 #*************************** end of file <genutils.py> **********************
1519 1508
@@ -1,2084 +1,1999 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.1 or newer.
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 602 2005-06-09 03:02:30Z fperez $
9 $Id: iplib.py 638 2005-07-18 03:01:41Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
13 13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 14 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #
19 19 # Note: this code originally subclassed code.InteractiveConsole from the
20 20 # Python standard library. Over time, much of that class has been copied
21 21 # verbatim here for modifications which could not be accomplished by
22 22 # subclassing. The Python License (sec. 2) allows for this, but it's always
23 23 # nice to acknowledge credit where credit is due.
24 24 #*****************************************************************************
25 25
26 26 #****************************************************************************
27 27 # Modules and globals
28 28
29 29 from __future__ import generators # for 2.2 backwards-compatibility
30 30
31 31 from IPython import Release
32 32 __author__ = '%s <%s>\n%s <%s>' % \
33 33 ( Release.authors['Janko'] + Release.authors['Fernando'] )
34 34 __license__ = Release.license
35 35 __version__ = Release.version
36 36
37 37 # Python standard modules
38 38 import __main__
39 39 import __builtin__
40 40 import exceptions
41 41 import keyword
42 42 import new
43 43 import os, sys, shutil
44 44 import code, glob, types, re
45 45 import string, StringIO
46 46 import inspect, pydoc
47 47 import bdb, pdb
48 48 import UserList # don't subclass list so this works with Python2.1
49 49 from pprint import pprint, pformat
50 50 import cPickle as pickle
51 51 import traceback
52 52
53 53 # IPython's own modules
54 54 import IPython
55 55 from IPython import OInspect,PyColorize,ultraTB
56 56 from IPython.ultraTB import ColorScheme,ColorSchemeTable # too long names
57 57 from IPython.Logger import Logger
58 58 from IPython.Magic import Magic,magic2python,shlex_split
59 59 from IPython.usage import cmd_line_usage,interactive_usage
60 60 from IPython.Struct import Struct
61 61 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
62 62 from IPython.FakeModule import FakeModule
63 63 from IPython.background_jobs import BackgroundJobManager
64 64 from IPython.genutils import *
65 65
66 66 # Global pointer to the running
67 67
68 68 # store the builtin raw_input globally, and use this always, in case user code
69 69 # overwrites it (like wx.py.PyShell does)
70 70 raw_input_original = raw_input
71 71
72 # declares Python 2.2 compatibility symbols:
73 try:
74 enumerate
75 except NameError:
76 def enumerate(obj):
77 i = -1
78 for item in obj:
79 i += 1
80 yield i, item
81 72 #****************************************************************************
82 73 # Some utility function definitions
83 74
84 75 class Bunch: pass
85 76
86 77 def esc_quotes(strng):
87 78 """Return the input string with single and double quotes escaped out"""
88 79
89 80 return strng.replace('"','\\"').replace("'","\\'")
90 81
91 82 def import_fail_info(mod_name,fns=None):
92 83 """Inform load failure for a module."""
93 84
94 85 if fns == None:
95 86 warn("Loading of %s failed.\n" % (mod_name,))
96 87 else:
97 88 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
98 89
99 90 def qw_lol(indata):
100 91 """qw_lol('a b') -> [['a','b']],
101 92 otherwise it's just a call to qw().
102 93
103 94 We need this to make sure the modules_some keys *always* end up as a
104 95 list of lists."""
105 96
106 97 if type(indata) in StringTypes:
107 98 return [qw(indata)]
108 99 else:
109 100 return qw(indata)
110 101
111 102 def ipmagic(arg_s):
112 103 """Call a magic function by name.
113 104
114 105 Input: a string containing the name of the magic function to call and any
115 106 additional arguments to be passed to the magic.
116 107
117 108 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
118 109 prompt:
119 110
120 111 In[1]: %name -opt foo bar
121 112
122 113 To call a magic without arguments, simply use ipmagic('name').
123 114
124 115 This provides a proper Python function to call IPython's magics in any
125 116 valid Python code you can type at the interpreter, including loops and
126 117 compound statements. It is added by IPython to the Python builtin
127 118 namespace upon initialization."""
128 119
129 120 args = arg_s.split(' ',1)
130 121 magic_name = args[0]
131 122 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
132 123 magic_name = magic_name[1:]
133 124 try:
134 125 magic_args = args[1]
135 126 except IndexError:
136 127 magic_args = ''
137 128 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
138 129 if fn is None:
139 130 error("Magic function `%s` not found." % magic_name)
140 131 else:
141 132 magic_args = __IPYTHON__.var_expand(magic_args)
142 133 return fn(magic_args)
143 134
144 135 def ipalias(arg_s):
145 136 """Call an alias by name.
146 137
147 138 Input: a string containing the name of the alias to call and any
148 139 additional arguments to be passed to the magic.
149 140
150 141 ipalias('name -opt foo bar') is equivalent to typing at the ipython
151 142 prompt:
152 143
153 144 In[1]: name -opt foo bar
154 145
155 146 To call an alias without arguments, simply use ipalias('name').
156 147
157 148 This provides a proper Python function to call IPython's aliases in any
158 149 valid Python code you can type at the interpreter, including loops and
159 150 compound statements. It is added by IPython to the Python builtin
160 151 namespace upon initialization."""
161 152
162 153 args = arg_s.split(' ',1)
163 154 alias_name = args[0]
164 155 try:
165 156 alias_args = args[1]
166 157 except IndexError:
167 158 alias_args = ''
168 159 if alias_name in __IPYTHON__.alias_table:
169 160 __IPYTHON__.call_alias(alias_name,alias_args)
170 161 else:
171 162 error("Alias `%s` not found." % alias_name)
172 163
173 164 #-----------------------------------------------------------------------------
174 165 # Local use classes
175 166 try:
176 167 from IPython import FlexCompleter
177 168
178 169 class MagicCompleter(FlexCompleter.Completer):
179 170 """Extension of the completer class to work on %-prefixed lines."""
180 171
181 172 def __init__(self,shell,namespace=None,omit__names=0,alias_table=None):
182 173 """MagicCompleter() -> completer
183 174
184 175 Return a completer object suitable for use by the readline library
185 176 via readline.set_completer().
186 177
187 178 Inputs:
188 179
189 180 - shell: a pointer to the ipython shell itself. This is needed
190 181 because this completer knows about magic functions, and those can
191 182 only be accessed via the ipython instance.
192 183
193 184 - namespace: an optional dict where completions are performed.
194 185
195 186 - The optional omit__names parameter sets the completer to omit the
196 187 'magic' names (__magicname__) for python objects unless the text
197 188 to be completed explicitly starts with one or more underscores.
198 189
199 190 - If alias_table is supplied, it should be a dictionary of aliases
200 191 to complete. """
201 192
202 193 FlexCompleter.Completer.__init__(self,namespace)
203 194 self.magic_prefix = shell.name+'.magic_'
204 195 self.magic_escape = shell.ESC_MAGIC
205 196 self.readline = FlexCompleter.readline
206 197 delims = self.readline.get_completer_delims()
207 198 delims = delims.replace(self.magic_escape,'')
208 199 self.readline.set_completer_delims(delims)
209 200 self.get_line_buffer = self.readline.get_line_buffer
210 201 self.omit__names = omit__names
211 202 self.merge_completions = shell.rc.readline_merge_completions
212 203
213 204 if alias_table is None:
214 205 alias_table = {}
215 206 self.alias_table = alias_table
216 207 # Regexp to split filenames with spaces in them
217 208 self.space_name_re = re.compile(r'([^\\] )')
218 209 # Hold a local ref. to glob.glob for speed
219 210 self.glob = glob.glob
220 211 # Special handling of backslashes needed in win32 platforms
221 212 if sys.platform == "win32":
222 213 self.clean_glob = self._clean_glob_win32
223 214 else:
224 215 self.clean_glob = self._clean_glob
225 216 self.matchers = [self.python_matches,
226 217 self.file_matches,
227 218 self.alias_matches,
228 219 self.python_func_kw_matches]
229 220
230 221 # Code contributed by Alex Schmolck, for ipython/emacs integration
231 222 def all_completions(self, text):
232 223 """Return all possible completions for the benefit of emacs."""
233 224
234 225 completions = []
235 226 try:
236 227 for i in xrange(sys.maxint):
237 228 res = self.complete(text, i)
238 229
239 230 if not res: break
240 231
241 232 completions.append(res)
242 233 #XXX workaround for ``notDefined.<tab>``
243 234 except NameError:
244 235 pass
245 236 return completions
246 237 # /end Alex Schmolck code.
247 238
248 239 def _clean_glob(self,text):
249 240 return self.glob("%s*" % text)
250 241
251 242 def _clean_glob_win32(self,text):
252 243 return [f.replace("\\","/")
253 244 for f in self.glob("%s*" % text)]
254 245
255 246 def file_matches(self, text):
256 247 """Match filneames, expanding ~USER type strings.
257 248
258 249 Most of the seemingly convoluted logic in this completer is an
259 250 attempt to handle filenames with spaces in them. And yet it's not
260 251 quite perfect, because Python's readline doesn't expose all of the
261 252 GNU readline details needed for this to be done correctly.
262 253
263 254 For a filename with a space in it, the printed completions will be
264 255 only the parts after what's already been typed (instead of the
265 256 full completions, as is normally done). I don't think with the
266 257 current (as of Python 2.3) Python readline it's possible to do
267 258 better."""
268 259
269 260 #print 'Completer->file_matches: <%s>' % text # dbg
270 261
271 262 # chars that require escaping with backslash - i.e. chars
272 263 # that readline treats incorrectly as delimiters, but we
273 264 # don't want to treat as delimiters in filename matching
274 265 # when escaped with backslash
275 266
276 267 protectables = ' ()[]{}'
277 268
278 269 def protect_filename(s):
279 270 return "".join([(ch in protectables and '\\' + ch or ch)
280 271 for ch in s])
281 272
282 273 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
283 274 open_quotes = 0 # track strings with open quotes
284 275 try:
285 276 lsplit = shlex_split(lbuf)[-1]
286 277 except ValueError:
287 278 # typically an unmatched ", or backslash without escaped char.
288 279 if lbuf.count('"')==1:
289 280 open_quotes = 1
290 281 lsplit = lbuf.split('"')[-1]
291 282 elif lbuf.count("'")==1:
292 283 open_quotes = 1
293 284 lsplit = lbuf.split("'")[-1]
294 285 else:
295 286 return None
296 287 except IndexError:
297 288 # tab pressed on empty line
298 289 lsplit = ""
299 290
300 291 if lsplit != protect_filename(lsplit):
301 292 # if protectables are found, do matching on the whole escaped
302 293 # name
303 294 has_protectables = 1
304 295 text0,text = text,lsplit
305 296 else:
306 297 has_protectables = 0
307 298 text = os.path.expanduser(text)
308 299
309 300 if text == "":
310 301 return [protect_filename(f) for f in self.glob("*")]
311 302
312 303 m0 = self.clean_glob(text.replace('\\',''))
313 304 if has_protectables:
314 305 # If we had protectables, we need to revert our changes to the
315 306 # beginning of filename so that we don't double-write the part
316 307 # of the filename we have so far
317 308 len_lsplit = len(lsplit)
318 309 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
319 310 else:
320 311 if open_quotes:
321 312 # if we have a string with an open quote, we don't need to
322 313 # protect the names at all (and we _shouldn't_, as it
323 314 # would cause bugs when the filesystem call is made).
324 315 matches = m0
325 316 else:
326 317 matches = [protect_filename(f) for f in m0]
327 318 if len(matches) == 1 and os.path.isdir(matches[0]):
328 319 # Takes care of links to directories also. Use '/'
329 320 # explicitly, even under Windows, so that name completions
330 321 # don't end up escaped.
331 322 matches[0] += '/'
332 323 return matches
333 324
334 325 def alias_matches(self, text):
335 326 """Match internal system aliases"""
336 327 #print 'Completer->alias_matches:',text # dbg
337 328 text = os.path.expanduser(text)
338 329 aliases = self.alias_table.keys()
339 330 if text == "":
340 331 return aliases
341 332 else:
342 333 return [alias for alias in aliases if alias.startswith(text)]
343 334
344 335 def python_matches(self,text):
345 336 """Match attributes or global python names"""
346 337 #print 'Completer->python_matches' # dbg
347 338 if "." in text:
348 339 try:
349 340 matches = self.attr_matches(text)
350 341 if text.endswith('.') and self.omit__names:
351 342 if self.omit__names == 1:
352 343 # true if txt is _not_ a __ name, false otherwise:
353 344 no__name = (lambda txt:
354 345 re.match(r'.*\.__.*?__',txt) is None)
355 346 else:
356 347 # true if txt is _not_ a _ name, false otherwise:
357 348 no__name = (lambda txt:
358 349 re.match(r'.*\._.*?',txt) is None)
359 350 matches = filter(no__name, matches)
360 351 except NameError:
361 352 # catches <undefined attributes>.<tab>
362 353 matches = []
363 354 else:
364 355 matches = self.global_matches(text)
365 356 # this is so completion finds magics when automagic is on:
366 357 if matches == [] and not text.startswith(os.sep):
367 358 matches = self.attr_matches(self.magic_prefix+text)
368 359 return matches
369 360
370 361 def _default_arguments(self, obj):
371 362 """Return the list of default arguments of obj if it is callable,
372 363 or empty list otherwise."""
373 364
374 365 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
375 366 # for classes, check for __init__,__new__
376 367 if inspect.isclass(obj):
377 368 obj = (getattr(obj,'__init__',None) or
378 369 getattr(obj,'__new__',None))
379 370 # for all others, check if they are __call__able
380 371 elif hasattr(obj, '__call__'):
381 372 obj = obj.__call__
382 373 # XXX: is there a way to handle the builtins ?
383 374 try:
384 375 args,_,_1,defaults = inspect.getargspec(obj)
385 376 if defaults:
386 377 return args[-len(defaults):]
387 378 except TypeError: pass
388 379 return []
389 380
390 381 def python_func_kw_matches(self,text):
391 382 """Match named parameters (kwargs) of the last open function"""
392 383
393 384 if "." in text: # a parameter cannot be dotted
394 385 return []
395 386 try: regexp = self.__funcParamsRegex
396 387 except AttributeError:
397 388 regexp = self.__funcParamsRegex = re.compile(r'''
398 389 '.*?' | # single quoted strings or
399 390 ".*?" | # double quoted strings or
400 391 \w+ | # identifier
401 392 \S # other characters
402 393 ''', re.VERBOSE | re.DOTALL)
403 394 # 1. find the nearest identifier that comes before an unclosed
404 395 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
405 396 tokens = regexp.findall(self.get_line_buffer())
406 397 tokens.reverse()
407 398 iterTokens = iter(tokens); openPar = 0
408 399 for token in iterTokens:
409 400 if token == ')':
410 401 openPar -= 1
411 402 elif token == '(':
412 403 openPar += 1
413 404 if openPar > 0:
414 405 # found the last unclosed parenthesis
415 406 break
416 407 else:
417 408 return []
418 409 # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" )
419 410 ids = []
420 411 isId = re.compile(r'\w+$').match
421 412 while True:
422 413 try:
423 414 ids.append(iterTokens.next())
424 415 if not isId(ids[-1]):
425 416 ids.pop(); break
426 417 if not iterTokens.next() == '.':
427 418 break
428 419 except StopIteration:
429 420 break
430 421 # lookup the candidate callable matches either using global_matches
431 422 # or attr_matches for dotted names
432 423 if len(ids) == 1:
433 424 callableMatches = self.global_matches(ids[0])
434 425 else:
435 426 callableMatches = self.attr_matches('.'.join(ids[::-1]))
436 427 argMatches = []
437 428 for callableMatch in callableMatches:
438 429 try: namedArgs = self._default_arguments(eval(callableMatch,
439 430 self.namespace))
440 431 except: continue
441 432 for namedArg in namedArgs:
442 433 if namedArg.startswith(text):
443 434 argMatches.append("%s=" %namedArg)
444 435 return argMatches
445 436
446 437 def complete(self, text, state):
447 438 """Return the next possible completion for 'text'.
448 439
449 440 This is called successively with state == 0, 1, 2, ... until it
450 441 returns None. The completion should begin with 'text'. """
451 442
452 443 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
453 444 magic_escape = self.magic_escape
454 445 magic_prefix = self.magic_prefix
455 446
456 447 try:
457 448 if text.startswith(magic_escape):
458 449 text = text.replace(magic_escape,magic_prefix)
459 450 elif text.startswith('~'):
460 451 text = os.path.expanduser(text)
461 452 if state == 0:
462 453 # Extend the list of completions with the results of each
463 454 # matcher, so we return results to the user from all
464 455 # namespaces.
465 456 if self.merge_completions:
466 457 self.matches = []
467 458 for matcher in self.matchers:
468 459 self.matches.extend(matcher(text))
469 460 else:
470 461 for matcher in self.matchers:
471 462 self.matches = matcher(text)
472 463 if self.matches:
473 464 break
474 465
475 466 try:
476 467 return self.matches[state].replace(magic_prefix,magic_escape)
477 468 except IndexError:
478 469 return None
479 470 except:
480 471 # If completion fails, don't annoy the user.
481 472 pass
482 473
483 474 except ImportError:
484 475 pass # no readline support
485 476
486 477 except KeyError:
487 478 pass # Windows doesn't set TERM, it doesn't matter
488 479
489 480
490 481 class InputList(UserList.UserList):
491 482 """Class to store user input.
492 483
493 484 It's basically a list, but slices return a string instead of a list, thus
494 485 allowing things like (assuming 'In' is an instance):
495 486
496 487 exec In[4:7]
497 488
498 489 or
499 490
500 491 exec In[5:9] + In[14] + In[21:25]"""
501 492
502 493 def __getslice__(self,i,j):
503 494 return ''.join(UserList.UserList.__getslice__(self,i,j))
504 495
505 496 #****************************************************************************
506 497 # Local use exceptions
507 498 class SpaceInInput(exceptions.Exception):
508 499 pass
509 500
510 501 #****************************************************************************
511 502 # Main IPython class
512 503
513 504 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
514 505 """An enhanced console for Python."""
515 506
516 507 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
517 508 user_ns = None,banner2='',
518 509 custom_exceptions=((),None)):
519 510
520 511 # Put a reference to self in builtins so that any form of embedded or
521 512 # imported code can test for being inside IPython.
522 513 __builtin__.__IPYTHON__ = self
523 514
524 515 # And load into builtins ipmagic/ipalias as well
525 516 __builtin__.ipmagic = ipmagic
526 517 __builtin__.ipalias = ipalias
527 518
528 519 # Add to __builtin__ other parts of IPython's public API
529 520 __builtin__.ip_set_hook = self.set_hook
530 521
531 522 # Keep in the builtins a flag for when IPython is active. We set it
532 523 # with setdefault so that multiple nested IPythons don't clobber one
533 524 # another. Each will increase its value by one upon being activated,
534 525 # which also gives us a way to determine the nesting level.
535 526 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
536 527
537 528 # Inform the user of ipython's fast exit magics.
538 529 _exit = ' Use %Exit or %Quit to exit without confirmation.'
539 530 __builtin__.exit += _exit
540 531 __builtin__.quit += _exit
541 532
542 533 # Create the namespace where the user will operate:
543 534
544 535 # FIXME. For some strange reason, __builtins__ is showing up at user
545 536 # level as a dict instead of a module. This is a manual fix, but I
546 537 # should really track down where the problem is coming from. Alex
547 538 # Schmolck reported this problem first.
548 539
549 540 # A useful post by Alex Martelli on this topic:
550 541 # Re: inconsistent value from __builtins__
551 542 # Von: Alex Martelli <aleaxit@yahoo.com>
552 543 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
553 544 # Gruppen: comp.lang.python
554 545 # Referenzen: 1
555 546
556 547 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
557 548 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
558 549 # > <type 'dict'>
559 550 # > >>> print type(__builtins__)
560 551 # > <type 'module'>
561 552 # > Is this difference in return value intentional?
562 553
563 554 # Well, it's documented that '__builtins__' can be either a dictionary
564 555 # or a module, and it's been that way for a long time. Whether it's
565 556 # intentional (or sensible), I don't know. In any case, the idea is that
566 557 # if you need to access the built-in namespace directly, you should start
567 558 # with "import __builtin__" (note, no 's') which will definitely give you
568 559 # a module. Yeah, it's somewhat confusing:-(.
569 560
570 561 if user_ns is None:
571 562 # Set __name__ to __main__ to better match the behavior of the
572 563 # normal interpreter.
573 564 self.user_ns = {'__name__' :'__main__',
574 565 '__builtins__' : __builtin__,
575 566 }
576 567 else:
577 568 self.user_ns = user_ns
578 569
579 570 # The user namespace MUST have a pointer to the shell itself.
580 571 self.user_ns[name] = self
581 572
582 573 # We need to insert into sys.modules something that looks like a
583 574 # module but which accesses the IPython namespace, for shelve and
584 575 # pickle to work interactively. Normally they rely on getting
585 576 # everything out of __main__, but for embedding purposes each IPython
586 577 # instance has its own private namespace, so we can't go shoving
587 578 # everything into __main__.
588 579
589 580 try:
590 581 main_name = self.user_ns['__name__']
591 582 except KeyError:
592 583 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
593 584 else:
594 585 #print "pickle hack in place" # dbg
595 586 sys.modules[main_name] = FakeModule(self.user_ns)
596 587
597 588 # List of input with multi-line handling.
598 589 # Fill its zero entry, user counter starts at 1
599 590 self.input_hist = InputList(['\n'])
600 591
601 592 # list of visited directories
602 593 self.dir_hist = [os.getcwd()]
603 594
604 595 # dict of output history
605 596 self.output_hist = {}
606 597
607 598 # dict of names to be treated as system aliases. Each entry in the
608 599 # alias table must be a 2-tuple of the form (N,name), where N is the
609 600 # number of positional arguments of the alias.
610 601 self.alias_table = {}
611 602
612 603 # dict of things NOT to alias (keywords and builtins)
613 604 self.no_alias = {}
614 605 for key in keyword.kwlist:
615 606 self.no_alias[key] = 1
616 607 self.no_alias.update(__builtin__.__dict__)
617 608
618 609 # make global variables for user access to these
619 610 self.user_ns['_ih'] = self.input_hist
620 611 self.user_ns['_oh'] = self.output_hist
621 612 self.user_ns['_dh'] = self.dir_hist
622 613
623 614 # user aliases to input and output histories
624 615 self.user_ns['In'] = self.input_hist
625 616 self.user_ns['Out'] = self.output_hist
626 617
627 618 # Store the actual shell's name
628 619 self.name = name
629 620
630 621 # Object variable to store code object waiting execution. This is
631 622 # used mainly by the multithreaded shells, but it can come in handy in
632 623 # other situations. No need to use a Queue here, since it's a single
633 624 # item which gets cleared once run.
634 625 self.code_to_run = None
635 626 self.code_to_run_src = '' # corresponding source
636 627
637 628 # Job manager (for jobs run as background threads)
638 629 self.jobs = BackgroundJobManager()
639 630 # Put the job manager into builtins so it's always there.
640 631 __builtin__.jobs = self.jobs
641 632
642 633 # escapes for automatic behavior on the command line
643 634 self.ESC_SHELL = '!'
644 635 self.ESC_HELP = '?'
645 636 self.ESC_MAGIC = '%'
646 637 self.ESC_QUOTE = ','
647 638 self.ESC_QUOTE2 = ';'
648 639 self.ESC_PAREN = '/'
649 640
650 641 # And their associated handlers
651 642 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
652 643 self.ESC_QUOTE:self.handle_auto,
653 644 self.ESC_QUOTE2:self.handle_auto,
654 645 self.ESC_MAGIC:self.handle_magic,
655 646 self.ESC_HELP:self.handle_help,
656 647 self.ESC_SHELL:self.handle_shell_escape,
657 648 }
658 649
659 650 # class initializations
660 651 code.InteractiveConsole.__init__(self,locals = self.user_ns)
661 652 Logger.__init__(self,log_ns = self.user_ns)
662 653 Magic.__init__(self,self)
663 654
664 655 # an ugly hack to get a pointer to the shell, so I can start writing
665 656 # magic code via this pointer instead of the current mixin salad.
666 657 Magic.set_shell(self,self)
667 658
668 659 # hooks holds pointers used for user-side customizations
669 660 self.hooks = Struct()
670 661
671 662 # Set all default hooks, defined in the IPython.hooks module.
672 663 hooks = IPython.hooks
673 664 for hook_name in hooks.__all__:
674 665 self.set_hook(hook_name,getattr(hooks,hook_name))
675 666
676 667 # Flag to mark unconditional exit
677 668 self.exit_now = False
678 669
679 670 self.usage_min = """\
680 671 An enhanced console for Python.
681 672 Some of its features are:
682 673 - Readline support if the readline library is present.
683 674 - Tab completion in the local namespace.
684 675 - Logging of input, see command-line options.
685 676 - System shell escape via ! , eg !ls.
686 677 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
687 678 - Keeps track of locally defined variables via %who, %whos.
688 679 - Show object information with a ? eg ?x or x? (use ?? for more info).
689 680 """
690 681 if usage: self.usage = usage
691 682 else: self.usage = self.usage_min
692 683
693 684 # Storage
694 685 self.rc = rc # This will hold all configuration information
695 686 self.inputcache = []
696 687 self._boundcache = []
697 688 self.pager = 'less'
698 689 # temporary files used for various purposes. Deleted at exit.
699 690 self.tempfiles = []
700 691
701 692 # for pushd/popd management
702 693 try:
703 694 self.home_dir = get_home_dir()
704 695 except HomeDirError,msg:
705 696 fatal(msg)
706 697
707 698 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
708 699
709 700 # Functions to call the underlying shell.
710 701
711 702 # utility to expand user variables via Itpl
712 703 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
713 704 self.user_ns))
714 705 # The first is similar to os.system, but it doesn't return a value,
715 706 # and it allows interpolation of variables in the user's namespace.
716 707 self.system = lambda cmd: shell(self.var_expand(cmd),
717 708 header='IPython system call: ',
718 709 verbose=self.rc.system_verbose)
719 710 # These are for getoutput and getoutputerror:
720 711 self.getoutput = lambda cmd: \
721 712 getoutput(self.var_expand(cmd),
722 713 header='IPython system call: ',
723 714 verbose=self.rc.system_verbose)
724 715 self.getoutputerror = lambda cmd: \
725 716 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
726 717 self.user_ns)),
727 718 header='IPython system call: ',
728 719 verbose=self.rc.system_verbose)
729 720
730 721 # RegExp for splitting line contents into pre-char//first
731 722 # word-method//rest. For clarity, each group in on one line.
732 723
733 724 # WARNING: update the regexp if the above escapes are changed, as they
734 725 # are hardwired in.
735 726
736 727 # Don't get carried away with trying to make the autocalling catch too
737 728 # much: it's better to be conservative rather than to trigger hidden
738 729 # evals() somewhere and end up causing side effects.
739 730
740 731 self.line_split = re.compile(r'^([\s*,;/])'
741 732 r'([\?\w\.]+\w*\s*)'
742 733 r'(\(?.*$)')
743 734
744 735 # Original re, keep around for a while in case changes break something
745 736 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
746 737 # r'(\s*[\?\w\.]+\w*\s*)'
747 738 # r'(\(?.*$)')
748 739
749 740 # RegExp to identify potential function names
750 741 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
751 742 # RegExp to exclude strings with this start from autocalling
752 743 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
753 744 # try to catch also methods for stuff in lists/tuples/dicts: off
754 745 # (experimental). For this to work, the line_split regexp would need
755 746 # to be modified so it wouldn't break things at '['. That line is
756 747 # nasty enough that I shouldn't change it until I can test it _well_.
757 748 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
758 749
759 750 # keep track of where we started running (mainly for crash post-mortem)
760 751 self.starting_dir = os.getcwd()
761 752
762 753 # Attributes for Logger mixin class, make defaults here
763 754 self._dolog = 0
764 755 self.LOG = ''
765 756 self.LOGDEF = '.InteractiveShell.log'
766 757 self.LOGMODE = 'over'
767 758 self.LOGHEAD = Itpl(
768 759 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
769 760 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
770 761 #log# opts = $self.rc.opts
771 762 #log# args = $self.rc.args
772 763 #log# It is safe to make manual edits below here.
773 764 #log#-----------------------------------------------------------------------
774 765 """)
775 766 # Various switches which can be set
776 767 self.CACHELENGTH = 5000 # this is cheap, it's just text
777 768 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
778 769 self.banner2 = banner2
779 770
780 771 # TraceBack handlers:
781 772 # Need two, one for syntax errors and one for other exceptions.
782 773 self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor')
783 774 # This one is initialized with an offset, meaning we always want to
784 775 # remove the topmost item in the traceback, which is our own internal
785 776 # code. Valid modes: ['Plain','Context','Verbose']
786 777 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
787 778 color_scheme='NoColor',
788 779 tb_offset = 1)
789 780 # and add any custom exception handlers the user may have specified
790 781 self.set_custom_exc(*custom_exceptions)
791 782
792 783 # Object inspector
793 784 ins_colors = OInspect.InspectColors
794 785 code_colors = PyColorize.ANSICodeColors
795 786 self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor')
796 787 self.autoindent = 0
797 788
798 789 # Make some aliases automatically
799 790 # Prepare list of shell aliases to auto-define
800 791 if os.name == 'posix':
801 792 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
802 793 'mv mv -i','rm rm -i','cp cp -i',
803 794 'cat cat','less less','clear clear',
804 795 # a better ls
805 796 'ls ls -F',
806 797 # long ls
807 798 'll ls -lF',
808 799 # color ls
809 800 'lc ls -F -o --color',
810 801 # ls normal files only
811 802 'lf ls -F -o --color %l | grep ^-',
812 803 # ls symbolic links
813 804 'lk ls -F -o --color %l | grep ^l',
814 805 # directories or links to directories,
815 806 'ldir ls -F -o --color %l | grep /$',
816 807 # things which are executable
817 808 'lx ls -F -o --color %l | grep ^-..x',
818 809 )
819 810 elif os.name in ['nt','dos']:
820 811 auto_alias = ('dir dir /on', 'ls dir /on',
821 812 'ddir dir /ad /on', 'ldir dir /ad /on',
822 813 'mkdir mkdir','rmdir rmdir','echo echo',
823 814 'ren ren','cls cls','copy copy')
824 815 else:
825 816 auto_alias = ()
826 817 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
827 818 # Call the actual (public) initializer
828 819 self.init_auto_alias()
829 820 # end __init__
830 821
831 822 def set_hook(self,name,hook):
832 823 """set_hook(name,hook) -> sets an internal IPython hook.
833 824
834 825 IPython exposes some of its internal API as user-modifiable hooks. By
835 826 resetting one of these hooks, you can modify IPython's behavior to
836 827 call at runtime your own routines."""
837 828
838 829 # At some point in the future, this should validate the hook before it
839 830 # accepts it. Probably at least check that the hook takes the number
840 831 # of args it's supposed to.
841 832 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
842 833
843 834 def set_custom_exc(self,exc_tuple,handler):
844 835 """set_custom_exc(exc_tuple,handler)
845 836
846 837 Set a custom exception handler, which will be called if any of the
847 838 exceptions in exc_tuple occur in the mainloop (specifically, in the
848 839 runcode() method.
849 840
850 841 Inputs:
851 842
852 843 - exc_tuple: a *tuple* of valid exceptions to call the defined
853 844 handler for. It is very important that you use a tuple, and NOT A
854 845 LIST here, because of the way Python's except statement works. If
855 846 you only want to trap a single exception, use a singleton tuple:
856 847
857 848 exc_tuple == (MyCustomException,)
858 849
859 850 - handler: this must be defined as a function with the following
860 851 basic interface: def my_handler(self,etype,value,tb).
861 852
862 853 This will be made into an instance method (via new.instancemethod)
863 854 of IPython itself, and it will be called if any of the exceptions
864 855 listed in the exc_tuple are caught. If the handler is None, an
865 856 internal basic one is used, which just prints basic info.
866 857
867 858 WARNING: by putting in your own exception handler into IPython's main
868 859 execution loop, you run a very good chance of nasty crashes. This
869 860 facility should only be used if you really know what you are doing."""
870 861
871 862 assert type(exc_tuple)==type(()) , \
872 863 "The custom exceptions must be given AS A TUPLE."
873 864
874 865 def dummy_handler(self,etype,value,tb):
875 866 print '*** Simple custom exception handler ***'
876 867 print 'Exception type :',etype
877 868 print 'Exception value:',value
878 869 print 'Traceback :',tb
879 870 print 'Source code :',self.code_to_run_src
880 871
881 872 if handler is None: handler = dummy_handler
882 873
883 874 self.CustomTB = new.instancemethod(handler,self,self.__class__)
884 875 self.custom_exceptions = exc_tuple
885 876
886 877 def set_custom_completer(self,completer,pos=0):
887 878 """set_custom_completer(completer,pos=0)
888 879
889 880 Adds a new custom completer function.
890 881
891 882 The position argument (defaults to 0) is the index in the completers
892 883 list where you want the completer to be inserted."""
893 884
894 885 newcomp = new.instancemethod(completer,self.Completer,
895 886 self.Completer.__class__)
896 887 self.Completer.matchers.insert(pos,newcomp)
897 888
898 889 def post_config_initialization(self):
899 890 """Post configuration init method
900 891
901 892 This is called after the configuration files have been processed to
902 893 'finalize' the initialization."""
903 894
904 895 # dynamic data that survives through sessions
905 896 # XXX make the filename a config option?
906 897 persist_base = 'persist'
907 898 if self.rc.profile:
908 899 persist_base += '_%s' % self.rc.profile
909 900 self.persist_fname = os.path.join(self.rc.ipythondir,persist_base)
910 901
911 902 try:
912 903 self.persist = pickle.load(file(self.persist_fname))
913 904 except:
914 905 self.persist = {}
915 906
916 907 def init_auto_alias(self):
917 908 """Define some aliases automatically.
918 909
919 910 These are ALL parameter-less aliases"""
920 911 for alias,cmd in self.auto_alias:
921 912 self.alias_table[alias] = (0,cmd)
922 913
923 914 def alias_table_validate(self,verbose=0):
924 915 """Update information about the alias table.
925 916
926 917 In particular, make sure no Python keywords/builtins are in it."""
927 918
928 919 no_alias = self.no_alias
929 920 for k in self.alias_table.keys():
930 921 if k in no_alias:
931 922 del self.alias_table[k]
932 923 if verbose:
933 924 print ("Deleting alias <%s>, it's a Python "
934 925 "keyword or builtin." % k)
935 926
936 927 def set_autoindent(self,value=None):
937 928 """Set the autoindent flag, checking for readline support.
938 929
939 930 If called with no arguments, it acts as a toggle."""
940 931
941 932 if not self.has_readline:
942 933 if os.name == 'posix':
943 934 warn("The auto-indent feature requires the readline library")
944 935 self.autoindent = 0
945 936 return
946 937 if value is None:
947 938 self.autoindent = not self.autoindent
948 939 else:
949 940 self.autoindent = value
950 941
951 942 def rc_set_toggle(self,rc_field,value=None):
952 943 """Set or toggle a field in IPython's rc config. structure.
953 944
954 945 If called with no arguments, it acts as a toggle.
955 946
956 947 If called with a non-existent field, the resulting AttributeError
957 948 exception will propagate out."""
958 949
959 950 rc_val = getattr(self.rc,rc_field)
960 951 if value is None:
961 952 value = not rc_val
962 953 setattr(self.rc,rc_field,value)
963 954
964 955 def user_setup(self,ipythondir,rc_suffix,mode='install'):
965 956 """Install the user configuration directory.
966 957
967 958 Can be called when running for the first time or to upgrade the user's
968 959 .ipython/ directory with the mode parameter. Valid modes are 'install'
969 960 and 'upgrade'."""
970 961
971 962 def wait():
972 963 try:
973 964 raw_input("Please press <RETURN> to start IPython.")
974 965 except EOFError:
975 966 print >> Term.cout
976 967 print '*'*70
977 968
978 969 cwd = os.getcwd() # remember where we started
979 970 glb = glob.glob
980 971 print '*'*70
981 972 if mode == 'install':
982 973 print \
983 974 """Welcome to IPython. I will try to create a personal configuration directory
984 975 where you can customize many aspects of IPython's functionality in:\n"""
985 976 else:
986 977 print 'I am going to upgrade your configuration in:'
987 978
988 979 print ipythondir
989 980
990 981 rcdirend = os.path.join('IPython','UserConfig')
991 982 cfg = lambda d: os.path.join(d,rcdirend)
992 983 try:
993 984 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
994 985 except IOError:
995 986 warning = """
996 987 Installation error. IPython's directory was not found.
997 988
998 989 Check the following:
999 990
1000 991 The ipython/IPython directory should be in a directory belonging to your
1001 992 PYTHONPATH environment variable (that is, it should be in a directory
1002 993 belonging to sys.path). You can copy it explicitly there or just link to it.
1003 994
1004 995 IPython will proceed with builtin defaults.
1005 996 """
1006 997 warn(warning)
1007 998 wait()
1008 999 return
1009 1000
1010 1001 if mode == 'install':
1011 1002 try:
1012 1003 shutil.copytree(rcdir,ipythondir)
1013 1004 os.chdir(ipythondir)
1014 1005 rc_files = glb("ipythonrc*")
1015 1006 for rc_file in rc_files:
1016 1007 os.rename(rc_file,rc_file+rc_suffix)
1017 1008 except:
1018 1009 warning = """
1019 1010
1020 1011 There was a problem with the installation:
1021 1012 %s
1022 1013 Try to correct it or contact the developers if you think it's a bug.
1023 1014 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1024 1015 warn(warning)
1025 1016 wait()
1026 1017 return
1027 1018
1028 1019 elif mode == 'upgrade':
1029 1020 try:
1030 1021 os.chdir(ipythondir)
1031 1022 except:
1032 1023 print """
1033 1024 Can not upgrade: changing to directory %s failed. Details:
1034 1025 %s
1035 1026 """ % (ipythondir,sys.exc_info()[1])
1036 1027 wait()
1037 1028 return
1038 1029 else:
1039 1030 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1040 1031 for new_full_path in sources:
1041 1032 new_filename = os.path.basename(new_full_path)
1042 1033 if new_filename.startswith('ipythonrc'):
1043 1034 new_filename = new_filename + rc_suffix
1044 1035 # The config directory should only contain files, skip any
1045 1036 # directories which may be there (like CVS)
1046 1037 if os.path.isdir(new_full_path):
1047 1038 continue
1048 1039 if os.path.exists(new_filename):
1049 1040 old_file = new_filename+'.old'
1050 1041 if os.path.exists(old_file):
1051 1042 os.remove(old_file)
1052 1043 os.rename(new_filename,old_file)
1053 1044 shutil.copy(new_full_path,new_filename)
1054 1045 else:
1055 1046 raise ValueError,'unrecognized mode for install:',`mode`
1056 1047
1057 1048 # Fix line-endings to those native to each platform in the config
1058 1049 # directory.
1059 1050 try:
1060 1051 os.chdir(ipythondir)
1061 1052 except:
1062 1053 print """
1063 1054 Problem: changing to directory %s failed.
1064 1055 Details:
1065 1056 %s
1066 1057
1067 1058 Some configuration files may have incorrect line endings. This should not
1068 1059 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1069 1060 wait()
1070 1061 else:
1071 1062 for fname in glb('ipythonrc*'):
1072 1063 try:
1073 1064 native_line_ends(fname,backup=0)
1074 1065 except IOError:
1075 1066 pass
1076 1067
1077 1068 if mode == 'install':
1078 1069 print """
1079 1070 Successful installation!
1080 1071
1081 1072 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1082 1073 IPython manual (there are both HTML and PDF versions supplied with the
1083 1074 distribution) to make sure that your system environment is properly configured
1084 1075 to take advantage of IPython's features."""
1085 1076 else:
1086 1077 print """
1087 1078 Successful upgrade!
1088 1079
1089 1080 All files in your directory:
1090 1081 %(ipythondir)s
1091 1082 which would have been overwritten by the upgrade were backed up with a .old
1092 1083 extension. If you had made particular customizations in those files you may
1093 1084 want to merge them back into the new files.""" % locals()
1094 1085 wait()
1095 1086 os.chdir(cwd)
1096 1087 # end user_setup()
1097 1088
1098 1089 def atexit_operations(self):
1099 1090 """This will be executed at the time of exit.
1100 1091
1101 1092 Saving of persistent data should be performed here. """
1102 1093
1103 1094 # input history
1104 1095 self.savehist()
1105 1096
1106 1097 # Cleanup all tempfiles left around
1107 1098 for tfile in self.tempfiles:
1108 1099 try:
1109 1100 os.unlink(tfile)
1110 1101 except OSError:
1111 1102 pass
1112 1103
1113 1104 # save the "persistent data" catch-all dictionary
1114 1105 try:
1115 1106 pickle.dump(self.persist, open(self.persist_fname,"w"))
1116 1107 except:
1117 1108 print "*** ERROR *** persistent data saving failed."
1118 1109
1119 1110 def savehist(self):
1120 1111 """Save input history to a file (via readline library)."""
1121 1112 try:
1122 1113 self.readline.write_history_file(self.histfile)
1123 1114 except:
1124 1115 print 'Unable to save IPython command history to file: ' + \
1125 1116 `self.histfile`
1126 1117
1127 1118 def pre_readline(self):
1128 1119 """readline hook to be used at the start of each line.
1129 1120
1130 1121 Currently it handles auto-indent only."""
1131 1122
1132 1123 self.readline.insert_text(' '* self.readline_indent)
1133 1124
1134 1125 def init_readline(self):
1135 1126 """Command history completion/saving/reloading."""
1136 1127 try:
1137 1128 import readline
1138 1129 self.Completer = MagicCompleter(self,
1139 1130 self.user_ns,
1140 1131 self.rc.readline_omit__names,
1141 1132 self.alias_table)
1142 1133 except ImportError,NameError:
1143 1134 # If FlexCompleter failed to import, MagicCompleter won't be
1144 1135 # defined. This can happen because of a problem with readline
1145 1136 self.has_readline = 0
1146 1137 # no point in bugging windows users with this every time:
1147 1138 if os.name == 'posix':
1148 1139 warn('Readline services not available on this platform.')
1149 1140 else:
1150 1141 import atexit
1151 1142
1152 1143 # Platform-specific configuration
1153 1144 if os.name == 'nt':
1154 1145 # readline under Windows modifies the default exit behavior
1155 1146 # from being Ctrl-Z/Return to the Unix Ctrl-D one.
1156 1147 __builtin__.exit = __builtin__.quit = \
1157 1148 ('Use Ctrl-D (i.e. EOF) to exit. '
1158 1149 'Use %Exit or %Quit to exit without confirmation.')
1159 1150 self.readline_startup_hook = readline.set_pre_input_hook
1160 1151 else:
1161 1152 self.readline_startup_hook = readline.set_startup_hook
1162 1153
1163 1154 # Load user's initrc file (readline config)
1164 1155 inputrc_name = os.environ.get('INPUTRC')
1165 1156 if inputrc_name is None:
1166 1157 home_dir = get_home_dir()
1167 1158 if home_dir is not None:
1168 1159 inputrc_name = os.path.join(home_dir,'.inputrc')
1169 1160 if os.path.isfile(inputrc_name):
1170 1161 try:
1171 1162 readline.read_init_file(inputrc_name)
1172 1163 except:
1173 1164 warn('Problems reading readline initialization file <%s>'
1174 1165 % inputrc_name)
1175 1166
1176 1167 self.has_readline = 1
1177 1168 self.readline = readline
1178 1169 self.readline_indent = 0 # for auto-indenting via readline
1179 1170 # save this in sys so embedded copies can restore it properly
1180 1171 sys.ipcompleter = self.Completer.complete
1181 1172 readline.set_completer(self.Completer.complete)
1182 1173
1183 1174 # Configure readline according to user's prefs
1184 1175 for rlcommand in self.rc.readline_parse_and_bind:
1185 1176 readline.parse_and_bind(rlcommand)
1186 1177
1187 1178 # remove some chars from the delimiters list
1188 1179 delims = readline.get_completer_delims()
1189 1180 delims = delims.translate(string._idmap,
1190 1181 self.rc.readline_remove_delims)
1191 1182 readline.set_completer_delims(delims)
1192 1183 # otherwise we end up with a monster history after a while:
1193 1184 readline.set_history_length(1000)
1194 1185 try:
1195 1186 #print '*** Reading readline history' # dbg
1196 1187 readline.read_history_file(self.histfile)
1197 1188 except IOError:
1198 1189 pass # It doesn't exist yet.
1199 1190
1200 1191 atexit.register(self.atexit_operations)
1201 1192 del atexit
1202 1193
1203 1194 # Configure auto-indent for all platforms
1204 1195 self.set_autoindent(self.rc.autoindent)
1205 1196
1206 1197 def showsyntaxerror(self, filename=None):
1207 1198 """Display the syntax error that just occurred.
1208 1199
1209 1200 This doesn't display a stack trace because there isn't one.
1210 1201
1211 1202 If a filename is given, it is stuffed in the exception instead
1212 1203 of what was there before (because Python's parser always uses
1213 1204 "<string>" when reading from a string).
1214 1205 """
1215 1206 type, value, sys.last_traceback = sys.exc_info()
1216 1207 sys.last_type = type
1217 1208 sys.last_value = value
1218 1209 if filename and type is SyntaxError:
1219 1210 # Work hard to stuff the correct filename in the exception
1220 1211 try:
1221 1212 msg, (dummy_filename, lineno, offset, line) = value
1222 1213 except:
1223 1214 # Not the format we expect; leave it alone
1224 1215 pass
1225 1216 else:
1226 1217 # Stuff in the right filename
1227 1218 try:
1228 1219 # Assume SyntaxError is a class exception
1229 1220 value = SyntaxError(msg, (filename, lineno, offset, line))
1230 1221 except:
1231 1222 # If that failed, assume SyntaxError is a string
1232 1223 value = msg, (filename, lineno, offset, line)
1233 1224 self.SyntaxTB(type,value,[])
1234 1225
1235 1226 def debugger(self):
1236 1227 """Call the pdb debugger."""
1237 1228
1238 1229 if not self.rc.pdb:
1239 1230 return
1240 1231 pdb.pm()
1241 1232
1242 1233 def showtraceback(self,exc_tuple = None):
1243 1234 """Display the exception that just occurred."""
1244 1235
1245 1236 # Though this won't be called by syntax errors in the input line,
1246 1237 # there may be SyntaxError cases whith imported code.
1247 1238 if exc_tuple is None:
1248 1239 type, value, tb = sys.exc_info()
1249 1240 else:
1250 1241 type, value, tb = exc_tuple
1251 1242 if type is SyntaxError:
1252 1243 self.showsyntaxerror()
1253 1244 else:
1254 1245 sys.last_type = type
1255 1246 sys.last_value = value
1256 1247 sys.last_traceback = tb
1257 1248 self.InteractiveTB()
1258 1249 if self.InteractiveTB.call_pdb and self.has_readline:
1259 1250 # pdb mucks up readline, fix it back
1260 1251 self.readline.set_completer(self.Completer.complete)
1261 1252
1262 1253 def update_cache(self, line):
1263 1254 """puts line into cache"""
1264 1255 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1265 1256 if len(self.inputcache) >= self.CACHELENGTH:
1266 1257 self.inputcache.pop() # This not :-)
1267 1258
1268 1259 def name_space_init(self):
1269 1260 """Create local namespace."""
1270 1261 # We want this to be a method to facilitate embedded initialization.
1271 1262 code.InteractiveConsole.__init__(self,self.user_ns)
1272 1263
1273 1264 def mainloop(self,banner=None):
1274 1265 """Creates the local namespace and starts the mainloop.
1275 1266
1276 1267 If an optional banner argument is given, it will override the
1277 1268 internally created default banner."""
1278 1269
1279 1270 self.name_space_init()
1280 1271 if self.rc.c: # Emulate Python's -c option
1281 1272 self.exec_init_cmd()
1282 1273 if banner is None:
1283 1274 if self.rc.banner:
1284 1275 banner = self.BANNER+self.banner2
1285 1276 else:
1286 1277 banner = ''
1287 1278 self.interact(banner)
1288 1279
1289 1280 def exec_init_cmd(self):
1290 1281 """Execute a command given at the command line.
1291 1282
1292 1283 This emulates Python's -c option."""
1293 1284
1294 1285 sys.argv = ['-c']
1295 1286 self.push(self.rc.c)
1296 1287
1297 1288 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1298 1289 """Embeds IPython into a running python program.
1299 1290
1300 1291 Input:
1301 1292
1302 1293 - header: An optional header message can be specified.
1303 1294
1304 1295 - local_ns, global_ns: working namespaces. If given as None, the
1305 1296 IPython-initialized one is updated with __main__.__dict__, so that
1306 1297 program variables become visible but user-specific configuration
1307 1298 remains possible.
1308 1299
1309 1300 - stack_depth: specifies how many levels in the stack to go to
1310 1301 looking for namespaces (when local_ns and global_ns are None). This
1311 1302 allows an intermediate caller to make sure that this function gets
1312 1303 the namespace from the intended level in the stack. By default (0)
1313 1304 it will get its locals and globals from the immediate caller.
1314 1305
1315 1306 Warning: it's possible to use this in a program which is being run by
1316 1307 IPython itself (via %run), but some funny things will happen (a few
1317 1308 globals get overwritten). In the future this will be cleaned up, as
1318 1309 there is no fundamental reason why it can't work perfectly."""
1319 1310
1320 1311 # Patch for global embedding to make sure that things don't overwrite
1321 1312 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1322 1313 # FIXME. Test this a bit more carefully (the if.. is new)
1323 1314 if local_ns is None and global_ns is None:
1324 1315 self.user_ns.update(__main__.__dict__)
1325 1316
1326 1317 # Get locals and globals from caller
1327 1318 if local_ns is None or global_ns is None:
1328 1319 call_frame = sys._getframe(stack_depth).f_back
1329 1320
1330 1321 if local_ns is None:
1331 1322 local_ns = call_frame.f_locals
1332 1323 if global_ns is None:
1333 1324 global_ns = call_frame.f_globals
1334 1325
1335 1326 # Update namespaces and fire up interpreter
1336 1327 self.user_ns.update(local_ns)
1337 1328 self.interact(header)
1338 1329
1339 1330 # Remove locals from namespace
1340 for k in local_ns.keys():
1331 for k in local_ns:
1341 1332 del self.user_ns[k]
1342 1333
1343 1334 def interact(self, banner=None):
1344 1335 """Closely emulate the interactive Python console.
1345 1336
1346 1337 The optional banner argument specify the banner to print
1347 1338 before the first interaction; by default it prints a banner
1348 1339 similar to the one printed by the real Python interpreter,
1349 1340 followed by the current class name in parentheses (so as not
1350 1341 to confuse this with the real interpreter -- since it's so
1351 1342 close!).
1352 1343
1353 1344 """
1354 1345 cprt = 'Type "copyright", "credits" or "license" for more information.'
1355 1346 if banner is None:
1356 1347 self.write("Python %s on %s\n%s\n(%s)\n" %
1357 1348 (sys.version, sys.platform, cprt,
1358 1349 self.__class__.__name__))
1359 1350 else:
1360 1351 self.write(banner)
1361 1352
1362 1353 more = 0
1363 1354
1364 1355 # Mark activity in the builtins
1365 1356 __builtin__.__dict__['__IPYTHON__active'] += 1
1366 while 1:
1367 # This is set by a call to %Exit or %Quit
1368 if self.exit_now:
1369 break
1357
1358 # exit_now is set by a call to %Exit or %Quit
1359 while not self.exit_now:
1370 1360 try:
1371 1361 if more:
1372 1362 prompt = self.outputcache.prompt2
1373 1363 if self.autoindent:
1374 1364 self.readline_startup_hook(self.pre_readline)
1375 1365 else:
1376 1366 prompt = self.outputcache.prompt1
1377 1367 try:
1378 1368 line = self.raw_input(prompt)
1379 1369 if self.autoindent:
1380 1370 self.readline_startup_hook(None)
1381 1371 except EOFError:
1382 1372 if self.autoindent:
1383 1373 self.readline_startup_hook(None)
1384 1374 self.write("\n")
1385 1375 if self.rc.confirm_exit:
1386 1376 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1387 1377 break
1388 1378 else:
1389 1379 break
1390 1380 else:
1391 1381 more = self.push(line)
1392 1382 # Auto-indent management
1393 1383 if self.autoindent:
1394 1384 if line:
1395 1385 ini_spaces = re.match('^(\s+)',line)
1396 1386 if ini_spaces:
1397 1387 nspaces = ini_spaces.end()
1398 1388 else:
1399 1389 nspaces = 0
1400 1390 self.readline_indent = nspaces
1401 1391
1402 1392 if line[-1] == ':':
1403 1393 self.readline_indent += 4
1404 1394 elif re.match(r'^\s+raise|^\s+return',line):
1405 1395 self.readline_indent -= 4
1406 1396 else:
1407 1397 self.readline_indent = 0
1408 1398
1409 1399 except KeyboardInterrupt:
1410 1400 self.write("\nKeyboardInterrupt\n")
1411 1401 self.resetbuffer()
1412 1402 more = 0
1413 1403 # keep cache in sync with the prompt counter:
1414 1404 self.outputcache.prompt_count -= 1
1415 1405
1416 1406 if self.autoindent:
1417 1407 self.readline_indent = 0
1418 1408
1419 1409 except bdb.BdbQuit:
1420 1410 warn("The Python debugger has exited with a BdbQuit exception.\n"
1421 1411 "Because of how pdb handles the stack, it is impossible\n"
1422 1412 "for IPython to properly format this particular exception.\n"
1423 1413 "IPython will resume normal operation.")
1424 except:
1425 # We should never get here except in fairly bizarre situations
1426 # (or b/c of an IPython bug). One reasonable exception is if
1427 # the user sets stdin/out/err to a broken object (or closes
1428 # any of them!)
1429
1430 fixed_in_out_err = 0
1431
1432 # Call the Term I/O class and have it reopen any stream which
1433 # the user might have closed.
1434 Term.reopen_all()
1435
1436 # Do the same manually for sys.stderr/out/in
1437
1438 # err first, so we can print at least warnings
1439 if sys.__stderr__.closed:
1440 sys.__stderr__ = os.fdopen(os.dup(2),'w',0)
1441 fixed_err_err = 1
1442 print >> sys.__stderr__,"""
1443 WARNING:
1444 sys.__stderr__ was closed!
1445 I've tried to reopen it, but bear in mind that things may not work normally
1446 from now. In particular, readline support may have broken.
1447 """
1448 # Next, check stdin/out
1449 if sys.__stdin__.closed:
1450 sys.__stdin__ = os.fdopen(os.dup(0),'r',0)
1451 fixed_in_out_err = 1
1452 print >> sys.__stderr__,"""
1453 WARNING:
1454 sys.__stdin__ was closed!
1455 I've tried to reopen it, but bear in mind that things may not work normally
1456 from now. In particular, readline support may have broken.
1457 """
1458 if sys.__stdout__.closed:
1459 sys.__stdout__ = os.fdopen(os.dup(1),'w',0)
1460 fixed_in_out_err = 1
1461 print >> sys.__stderr__,"""
1462 WARNING:
1463 sys.__stdout__ was closed!
1464 I've tried to reopen it, but bear in mind that things may not work normally
1465 from now. In particular, readline support may have broken.
1466 """
1467
1468 # Now, check mismatch of objects
1469 if sys.stdin is not sys.__stdin__:
1470 sys.stdin = sys.__stdin__
1471 fixed_in_out_err = 1
1472 print >> sys.__stderr__,"""
1473 WARNING:
1474 sys.stdin has been reset to sys.__stdin__.
1475 There seemed to be a problem with your sys.stdin.
1476 """
1477 if sys.stdout is not sys.__stdout__:
1478 sys.stdout = sys.__stdout__
1479 fixed_in_out_err = 1
1480 print >> sys.__stderr__,"""
1481 WARNING:
1482 sys.stdout has been reset to sys.__stdout__.
1483 There seemed to be a problem with your sys.stdout.
1484 """
1485
1486 if sys.stderr is not sys.__stderr__:
1487 sys.stderr = sys.__stderr__
1488 fixed_in_out_err = 1
1489 print >> sys.__stderr__,"""
1490 WARNING:
1491 sys.stderr has been reset to sys.__stderr__.
1492 There seemed to be a problem with your sys.stderr.
1493 """
1494 # If the problem wasn't a broken out/err, it's an IPython bug
1495 # I wish we could ask the user whether to crash or not, but
1496 # calling any function at this point messes up the stack.
1497 if not fixed_in_out_err:
1498 raise
1499
1414
1500 1415 # We are off again...
1501 1416 __builtin__.__dict__['__IPYTHON__active'] -= 1
1502 1417
1503 1418 def excepthook(self, type, value, tb):
1504 1419 """One more defense for GUI apps that call sys.excepthook.
1505 1420
1506 1421 GUI frameworks like wxPython trap exceptions and call
1507 1422 sys.excepthook themselves. I guess this is a feature that
1508 1423 enables them to keep running after exceptions that would
1509 1424 otherwise kill their mainloop. This is a bother for IPython
1510 1425 which excepts to catch all of the program exceptions with a try:
1511 1426 except: statement.
1512 1427
1513 1428 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1514 1429 any app directly invokes sys.excepthook, it will look to the user like
1515 1430 IPython crashed. In order to work around this, we can disable the
1516 1431 CrashHandler and replace it with this excepthook instead, which prints a
1517 1432 regular traceback using our InteractiveTB. In this fashion, apps which
1518 1433 call sys.excepthook will generate a regular-looking exception from
1519 1434 IPython, and the CrashHandler will only be triggered by real IPython
1520 1435 crashes.
1521 1436
1522 1437 This hook should be used sparingly, only in places which are not likely
1523 1438 to be true IPython errors.
1524 1439 """
1525 1440
1526 1441 self.InteractiveTB(type, value, tb, tb_offset=0)
1527 1442 if self.InteractiveTB.call_pdb and self.has_readline:
1528 1443 self.readline.set_completer(self.Completer.complete)
1529 1444
1530 1445 def call_alias(self,alias,rest=''):
1531 1446 """Call an alias given its name and the rest of the line.
1532 1447
1533 1448 This function MUST be given a proper alias, because it doesn't make
1534 1449 any checks when looking up into the alias table. The caller is
1535 1450 responsible for invoking it only with a valid alias."""
1536 1451
1537 1452 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1538 1453 nargs,cmd = self.alias_table[alias]
1539 1454 # Expand the %l special to be the user's input line
1540 1455 if cmd.find('%l') >= 0:
1541 1456 cmd = cmd.replace('%l',rest)
1542 1457 rest = ''
1543 1458 if nargs==0:
1544 1459 # Simple, argument-less aliases
1545 1460 cmd = '%s %s' % (cmd,rest)
1546 1461 else:
1547 1462 # Handle aliases with positional arguments
1548 1463 args = rest.split(None,nargs)
1549 1464 if len(args)< nargs:
1550 1465 error('Alias <%s> requires %s arguments, %s given.' %
1551 1466 (alias,nargs,len(args)))
1552 1467 return
1553 1468 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1554 1469 # Now call the macro, evaluating in the user's namespace
1555 1470 try:
1556 1471 self.system(cmd)
1557 1472 except:
1558 1473 self.showtraceback()
1559 1474
1560 1475 def runlines(self,lines):
1561 1476 """Run a string of one or more lines of source.
1562 1477
1563 1478 This method is capable of running a string containing multiple source
1564 1479 lines, as if they had been entered at the IPython prompt. Since it
1565 1480 exposes IPython's processing machinery, the given strings can contain
1566 1481 magic calls (%magic), special shell access (!cmd), etc."""
1567 1482
1568 1483 # We must start with a clean buffer, in case this is run from an
1569 1484 # interactive IPython session (via a magic, for example).
1570 1485 self.resetbuffer()
1571 1486 lines = lines.split('\n')
1572 1487 more = 0
1573 1488 for line in lines:
1574 1489 # skip blank lines so we don't mess up the prompt counter, but do
1575 1490 # NOT skip even a blank line if we are in a code block (more is
1576 1491 # true)
1577 1492 if line or more:
1578 1493 more = self.push((self.prefilter(line,more)))
1579 1494 # IPython's runsource returns None if there was an error
1580 1495 # compiling the code. This allows us to stop processing right
1581 1496 # away, so the user gets the error message at the right place.
1582 1497 if more is None:
1583 1498 break
1584 1499 # final newline in case the input didn't have it, so that the code
1585 1500 # actually does get executed
1586 1501 if more:
1587 1502 self.push('\n')
1588 1503
1589 1504 def runsource(self, source, filename="<input>", symbol="single"):
1590 1505 """Compile and run some source in the interpreter.
1591 1506
1592 1507 Arguments are as for compile_command().
1593 1508
1594 1509 One several things can happen:
1595 1510
1596 1511 1) The input is incorrect; compile_command() raised an
1597 1512 exception (SyntaxError or OverflowError). A syntax traceback
1598 1513 will be printed by calling the showsyntaxerror() method.
1599 1514
1600 1515 2) The input is incomplete, and more input is required;
1601 1516 compile_command() returned None. Nothing happens.
1602 1517
1603 1518 3) The input is complete; compile_command() returned a code
1604 1519 object. The code is executed by calling self.runcode() (which
1605 1520 also handles run-time exceptions, except for SystemExit).
1606 1521
1607 1522 The return value is:
1608 1523
1609 1524 - True in case 2
1610 1525
1611 1526 - False in the other cases, unless an exception is raised, where
1612 1527 None is returned instead. This can be used by external callers to
1613 1528 know whether to continue feeding input or not.
1614 1529
1615 1530 The return value can be used to decide whether to use sys.ps1 or
1616 1531 sys.ps2 to prompt the next line."""
1617 1532 try:
1618 1533 code = self.compile(source, filename, symbol)
1619 1534 except (OverflowError, SyntaxError, ValueError):
1620 1535 # Case 1
1621 1536 self.showsyntaxerror(filename)
1622 1537 return None
1623 1538
1624 1539 if code is None:
1625 1540 # Case 2
1626 1541 return True
1627 1542
1628 1543 # Case 3
1629 1544 # We store the code source and object so that threaded shells and
1630 1545 # custom exception handlers can access all this info if needed.
1631 1546 self.code_to_run_src = source
1632 1547 self.code_to_run = code
1633 1548 # now actually execute the code object
1634 1549 if self.runcode(code) == 0:
1635 1550 return False
1636 1551 else:
1637 1552 return None
1638 1553
1639 1554 def runcode(self,code_obj):
1640 1555 """Execute a code object.
1641 1556
1642 1557 When an exception occurs, self.showtraceback() is called to display a
1643 1558 traceback.
1644 1559
1645 1560 Return value: a flag indicating whether the code to be run completed
1646 1561 successfully:
1647 1562
1648 1563 - 0: successful execution.
1649 1564 - 1: an error occurred.
1650 1565 """
1651 1566
1652 1567 # Set our own excepthook in case the user code tries to call it
1653 1568 # directly, so that the IPython crash handler doesn't get triggered
1654 1569 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1655 1570 outflag = 1 # happens in more places, so it's easier as default
1656 1571 try:
1657 1572 try:
1658 1573 exec code_obj in self.locals
1659 1574 finally:
1660 1575 # Reset our crash handler in place
1661 1576 sys.excepthook = old_excepthook
1662 1577 except SystemExit:
1663 1578 self.resetbuffer()
1664 1579 self.showtraceback()
1665 1580 warn( __builtin__.exit,level=1)
1666 1581 except self.custom_exceptions:
1667 1582 etype,value,tb = sys.exc_info()
1668 1583 self.CustomTB(etype,value,tb)
1669 1584 except:
1670 1585 self.showtraceback()
1671 1586 else:
1672 1587 outflag = 0
1673 1588 if code.softspace(sys.stdout, 0):
1674 1589 print
1675 1590 # Flush out code object which has been run (and source)
1676 1591 self.code_to_run = None
1677 1592 self.code_to_run_src = ''
1678 1593 return outflag
1679 1594
1680 1595 def raw_input(self, prompt=""):
1681 1596 """Write a prompt and read a line.
1682 1597
1683 1598 The returned line does not include the trailing newline.
1684 1599 When the user enters the EOF key sequence, EOFError is raised.
1685 1600
1686 1601 The base implementation uses the built-in function
1687 1602 raw_input(); a subclass may replace this with a different
1688 1603 implementation.
1689 1604 """
1690 1605 return self.prefilter(raw_input_original(prompt),
1691 1606 prompt==self.outputcache.prompt2)
1692 1607
1693 1608 def split_user_input(self,line):
1694 1609 """Split user input into pre-char, function part and rest."""
1695 1610
1696 1611 lsplit = self.line_split.match(line)
1697 1612 if lsplit is None: # no regexp match returns None
1698 1613 try:
1699 1614 iFun,theRest = line.split(None,1)
1700 1615 except ValueError:
1701 1616 iFun,theRest = line,''
1702 1617 pre = re.match('^(\s*)(.*)',line).groups()[0]
1703 1618 else:
1704 1619 pre,iFun,theRest = lsplit.groups()
1705 1620
1706 1621 #print 'line:<%s>' % line # dbg
1707 1622 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1708 1623 return pre,iFun.strip(),theRest
1709 1624
1710 1625 def _prefilter(self, line, continue_prompt):
1711 1626 """Calls different preprocessors, depending on the form of line."""
1712 1627
1713 1628 # All handlers *must* return a value, even if it's blank ('').
1714 1629
1715 1630 # Lines are NOT logged here. Handlers should process the line as
1716 1631 # needed, update the cache AND log it (so that the input cache array
1717 1632 # stays synced).
1718 1633
1719 1634 # This function is _very_ delicate, and since it's also the one which
1720 1635 # determines IPython's response to user input, it must be as efficient
1721 1636 # as possible. For this reason it has _many_ returns in it, trying
1722 1637 # always to exit as quickly as it can figure out what it needs to do.
1723 1638
1724 1639 # This function is the main responsible for maintaining IPython's
1725 1640 # behavior respectful of Python's semantics. So be _very_ careful if
1726 1641 # making changes to anything here.
1727 1642
1728 1643 #.....................................................................
1729 1644 # Code begins
1730 1645
1731 1646 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1732 1647
1733 1648 # save the line away in case we crash, so the post-mortem handler can
1734 1649 # record it
1735 1650 self._last_input_line = line
1736 1651
1737 1652 #print '***line: <%s>' % line # dbg
1738 1653
1739 1654 # the input history needs to track even empty lines
1740 1655 if not line.strip():
1741 1656 if not continue_prompt:
1742 1657 self.outputcache.prompt_count -= 1
1743 1658 return self.handle_normal('',continue_prompt)
1744 1659
1745 1660 # print '***cont',continue_prompt # dbg
1746 1661 # special handlers are only allowed for single line statements
1747 1662 if continue_prompt and not self.rc.multi_line_specials:
1748 1663 return self.handle_normal(line,continue_prompt)
1749 1664
1750 1665 # For the rest, we need the structure of the input
1751 1666 pre,iFun,theRest = self.split_user_input(line)
1752 1667 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1753 1668
1754 1669 # First check for explicit escapes in the last/first character
1755 1670 handler = None
1756 1671 if line[-1] == self.ESC_HELP:
1757 1672 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1758 1673 if handler is None:
1759 1674 # look at the first character of iFun, NOT of line, so we skip
1760 1675 # leading whitespace in multiline input
1761 1676 handler = self.esc_handlers.get(iFun[0:1])
1762 1677 if handler is not None:
1763 1678 return handler(line,continue_prompt,pre,iFun,theRest)
1764 1679 # Emacs ipython-mode tags certain input lines
1765 1680 if line.endswith('# PYTHON-MODE'):
1766 1681 return self.handle_emacs(line,continue_prompt)
1767 1682
1768 1683 # Next, check if we can automatically execute this thing
1769 1684
1770 1685 # Allow ! in multi-line statements if multi_line_specials is on:
1771 1686 if continue_prompt and self.rc.multi_line_specials and \
1772 1687 iFun.startswith(self.ESC_SHELL):
1773 1688 return self.handle_shell_escape(line,continue_prompt,
1774 1689 pre=pre,iFun=iFun,
1775 1690 theRest=theRest)
1776 1691
1777 1692 # Let's try to find if the input line is a magic fn
1778 1693 oinfo = None
1779 1694 if hasattr(self,'magic_'+iFun):
1780 1695 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1781 1696 if oinfo['ismagic']:
1782 1697 # Be careful not to call magics when a variable assignment is
1783 1698 # being made (ls='hi', for example)
1784 1699 if self.rc.automagic and \
1785 1700 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1786 1701 (self.rc.multi_line_specials or not continue_prompt):
1787 1702 return self.handle_magic(line,continue_prompt,
1788 1703 pre,iFun,theRest)
1789 1704 else:
1790 1705 return self.handle_normal(line,continue_prompt)
1791 1706
1792 1707 # If the rest of the line begins with an (in)equality, assginment or
1793 1708 # function call, we should not call _ofind but simply execute it.
1794 1709 # This avoids spurious geattr() accesses on objects upon assignment.
1795 1710 #
1796 1711 # It also allows users to assign to either alias or magic names true
1797 1712 # python variables (the magic/alias systems always take second seat to
1798 1713 # true python code).
1799 1714 if theRest and theRest[0] in '!=()':
1800 1715 return self.handle_normal(line,continue_prompt)
1801 1716
1802 1717 if oinfo is None:
1803 1718 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1804 1719
1805 1720 if not oinfo['found']:
1806 1721 return self.handle_normal(line,continue_prompt)
1807 1722 else:
1808 1723 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1809 1724 if oinfo['isalias']:
1810 1725 return self.handle_alias(line,continue_prompt,
1811 1726 pre,iFun,theRest)
1812 1727
1813 1728 if self.rc.autocall and \
1814 1729 not self.re_exclude_auto.match(theRest) and \
1815 1730 self.re_fun_name.match(iFun) and \
1816 1731 callable(oinfo['obj']) :
1817 1732 #print 'going auto' # dbg
1818 1733 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1819 1734 else:
1820 1735 #print 'was callable?', callable(oinfo['obj']) # dbg
1821 1736 return self.handle_normal(line,continue_prompt)
1822 1737
1823 1738 # If we get here, we have a normal Python line. Log and return.
1824 1739 return self.handle_normal(line,continue_prompt)
1825 1740
1826 1741 def _prefilter_dumb(self, line, continue_prompt):
1827 1742 """simple prefilter function, for debugging"""
1828 1743 return self.handle_normal(line,continue_prompt)
1829 1744
1830 1745 # Set the default prefilter() function (this can be user-overridden)
1831 1746 prefilter = _prefilter
1832 1747
1833 1748 def handle_normal(self,line,continue_prompt=None,
1834 1749 pre=None,iFun=None,theRest=None):
1835 1750 """Handle normal input lines. Use as a template for handlers."""
1836 1751
1837 1752 self.log(line,continue_prompt)
1838 1753 self.update_cache(line)
1839 1754 return line
1840 1755
1841 1756 def handle_alias(self,line,continue_prompt=None,
1842 1757 pre=None,iFun=None,theRest=None):
1843 1758 """Handle alias input lines. """
1844 1759
1845 1760 theRest = esc_quotes(theRest)
1846 1761 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1847 1762 self.log(line_out,continue_prompt)
1848 1763 self.update_cache(line_out)
1849 1764 return line_out
1850 1765
1851 1766 def handle_shell_escape(self, line, continue_prompt=None,
1852 1767 pre=None,iFun=None,theRest=None):
1853 1768 """Execute the line in a shell, empty return value"""
1854 1769
1855 1770 # Example of a special handler. Others follow a similar pattern.
1856 1771 if continue_prompt: # multi-line statements
1857 1772 if iFun.startswith('!!'):
1858 1773 print 'SyntaxError: !! is not allowed in multiline statements'
1859 1774 return pre
1860 1775 else:
1861 1776 cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"')
1862 1777 line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1863 1778 else: # single-line input
1864 1779 if line.startswith('!!'):
1865 1780 # rewrite iFun/theRest to properly hold the call to %sx and
1866 1781 # the actual command to be executed, so handle_magic can work
1867 1782 # correctly
1868 1783 theRest = '%s %s' % (iFun[2:],theRest)
1869 1784 iFun = 'sx'
1870 1785 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1871 1786 continue_prompt,pre,iFun,theRest)
1872 1787 else:
1873 1788 cmd = esc_quotes(line[1:])
1874 1789 line_out = '%s.system("%s")' % (self.name,cmd)
1875 1790 # update cache/log and return
1876 1791 self.log(line_out,continue_prompt)
1877 1792 self.update_cache(line_out) # readline cache gets normal line
1878 1793 return line_out
1879 1794
1880 1795 def handle_magic(self, line, continue_prompt=None,
1881 1796 pre=None,iFun=None,theRest=None):
1882 1797 """Execute magic functions.
1883 1798
1884 1799 Also log them with a prepended # so the log is clean Python."""
1885 1800
1886 1801 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1887 1802 self.log(cmd,continue_prompt)
1888 1803 self.update_cache(line)
1889 1804 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1890 1805 return cmd
1891 1806
1892 1807 def handle_auto(self, line, continue_prompt=None,
1893 1808 pre=None,iFun=None,theRest=None):
1894 1809 """Hande lines which can be auto-executed, quoting if requested."""
1895 1810
1896 1811 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1897 1812
1898 1813 # This should only be active for single-line input!
1899 1814 if continue_prompt:
1900 1815 return line
1901 1816
1902 1817 if pre == self.ESC_QUOTE:
1903 1818 # Auto-quote splitting on whitespace
1904 1819 newcmd = '%s("%s")\n' % (iFun,'", "'.join(theRest.split()) )
1905 1820 elif pre == self.ESC_QUOTE2:
1906 1821 # Auto-quote whole string
1907 1822 newcmd = '%s("%s")\n' % (iFun,theRest)
1908 1823 else:
1909 1824 # Auto-paren
1910 1825 if theRest[0:1] in ('=','['):
1911 1826 # Don't autocall in these cases. They can be either
1912 1827 # rebindings of an existing callable's name, or item access
1913 1828 # for an object which is BOTH callable and implements
1914 1829 # __getitem__.
1915 1830 return '%s %s\n' % (iFun,theRest)
1916 1831 if theRest.endswith(';'):
1917 1832 newcmd = '%s(%s);\n' % (iFun.rstrip(),theRest[:-1])
1918 1833 else:
1919 1834 newcmd = '%s(%s)\n' % (iFun.rstrip(),theRest)
1920 1835
1921 1836 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd,
1922 1837 # log what is now valid Python, not the actual user input (without the
1923 1838 # final newline)
1924 1839 self.log(newcmd.strip(),continue_prompt)
1925 1840 return newcmd
1926 1841
1927 1842 def handle_help(self, line, continue_prompt=None,
1928 1843 pre=None,iFun=None,theRest=None):
1929 1844 """Try to get some help for the object.
1930 1845
1931 1846 obj? or ?obj -> basic information.
1932 1847 obj?? or ??obj -> more details.
1933 1848 """
1934 1849
1935 1850 # We need to make sure that we don't process lines which would be
1936 1851 # otherwise valid python, such as "x=1 # what?"
1937 1852 try:
1938 1853 code.compile_command(line)
1939 1854 except SyntaxError:
1940 1855 # We should only handle as help stuff which is NOT valid syntax
1941 1856 if line[0]==self.ESC_HELP:
1942 1857 line = line[1:]
1943 1858 elif line[-1]==self.ESC_HELP:
1944 1859 line = line[:-1]
1945 1860 self.log('#?'+line)
1946 1861 self.update_cache(line)
1947 1862 if line:
1948 1863 self.magic_pinfo(line)
1949 1864 else:
1950 1865 page(self.usage,screen_lines=self.rc.screen_length)
1951 1866 return '' # Empty string is needed here!
1952 1867 except:
1953 1868 # Pass any other exceptions through to the normal handler
1954 1869 return self.handle_normal(line,continue_prompt)
1955 1870 else:
1956 1871 # If the code compiles ok, we should handle it normally
1957 1872 return self.handle_normal(line,continue_prompt)
1958 1873
1959 1874 def handle_emacs(self,line,continue_prompt=None,
1960 1875 pre=None,iFun=None,theRest=None):
1961 1876 """Handle input lines marked by python-mode."""
1962 1877
1963 1878 # Currently, nothing is done. Later more functionality can be added
1964 1879 # here if needed.
1965 1880
1966 1881 # The input cache shouldn't be updated
1967 1882
1968 1883 return line
1969 1884
1970 1885 def write(self,data):
1971 1886 """Write a string to the default output"""
1972 1887 Term.cout.write(data)
1973 1888
1974 1889 def write_err(self,data):
1975 1890 """Write a string to the default error output"""
1976 1891 Term.cerr.write(data)
1977 1892
1978 1893 def safe_execfile(self,fname,*where,**kw):
1979 1894 fname = os.path.expanduser(fname)
1980 1895
1981 1896 # find things also in current directory
1982 1897 dname = os.path.dirname(fname)
1983 1898 if not sys.path.count(dname):
1984 1899 sys.path.append(dname)
1985 1900
1986 1901 try:
1987 1902 xfile = open(fname)
1988 1903 except:
1989 1904 print >> Term.cerr, \
1990 1905 'Could not open file <%s> for safe execution.' % fname
1991 1906 return None
1992 1907
1993 1908 kw.setdefault('islog',0)
1994 1909 kw.setdefault('quiet',1)
1995 1910 kw.setdefault('exit_ignore',0)
1996 1911 first = xfile.readline()
1997 1912 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
1998 1913 xfile.close()
1999 1914 # line by line execution
2000 1915 if first.startswith(_LOGHEAD) or kw['islog']:
2001 1916 print 'Loading log file <%s> one line at a time...' % fname
2002 1917 if kw['quiet']:
2003 1918 stdout_save = sys.stdout
2004 1919 sys.stdout = StringIO.StringIO()
2005 1920 try:
2006 1921 globs,locs = where[0:2]
2007 1922 except:
2008 1923 try:
2009 1924 globs = locs = where[0]
2010 1925 except:
2011 1926 globs = locs = globals()
2012 1927 badblocks = []
2013 1928
2014 1929 # we also need to identify indented blocks of code when replaying
2015 1930 # logs and put them together before passing them to an exec
2016 1931 # statement. This takes a bit of regexp and look-ahead work in the
2017 1932 # file. It's easiest if we swallow the whole thing in memory
2018 1933 # first, and manually walk through the lines list moving the
2019 1934 # counter ourselves.
2020 1935 indent_re = re.compile('\s+\S')
2021 1936 xfile = open(fname)
2022 1937 filelines = xfile.readlines()
2023 1938 xfile.close()
2024 1939 nlines = len(filelines)
2025 1940 lnum = 0
2026 1941 while lnum < nlines:
2027 1942 line = filelines[lnum]
2028 1943 lnum += 1
2029 1944 # don't re-insert logger status info into cache
2030 1945 if line.startswith('#log#'):
2031 1946 continue
2032 1947 elif line.startswith('#%s'% self.ESC_MAGIC):
2033 1948 self.update_cache(line[1:])
2034 1949 line = magic2python(line)
2035 1950 elif line.startswith('#!'):
2036 1951 self.update_cache(line[1:])
2037 1952 else:
2038 1953 # build a block of code (maybe a single line) for execution
2039 1954 block = line
2040 1955 try:
2041 1956 next = filelines[lnum] # lnum has already incremented
2042 1957 except:
2043 1958 next = None
2044 1959 while next and indent_re.match(next):
2045 1960 block += next
2046 1961 lnum += 1
2047 1962 try:
2048 1963 next = filelines[lnum]
2049 1964 except:
2050 1965 next = None
2051 1966 # now execute the block of one or more lines
2052 1967 try:
2053 1968 exec block in globs,locs
2054 1969 self.update_cache(block.rstrip())
2055 1970 except SystemExit:
2056 1971 pass
2057 1972 except:
2058 1973 badblocks.append(block.rstrip())
2059 1974 if kw['quiet']: # restore stdout
2060 1975 sys.stdout.close()
2061 1976 sys.stdout = stdout_save
2062 1977 print 'Finished replaying log file <%s>' % fname
2063 1978 if badblocks:
2064 1979 print >> sys.stderr, \
2065 1980 '\nThe following lines/blocks in file <%s> reported errors:' \
2066 1981 % fname
2067 1982 for badline in badblocks:
2068 1983 print >> sys.stderr, badline
2069 1984 else: # regular file execution
2070 1985 try:
2071 1986 execfile(fname,*where)
2072 1987 except SyntaxError:
2073 1988 etype, evalue = sys.exc_info()[0:2]
2074 1989 self.SyntaxTB(etype,evalue,[])
2075 1990 warn('Failure executing file: <%s>' % fname)
2076 1991 except SystemExit,status:
2077 1992 if not kw['exit_ignore']:
2078 1993 self.InteractiveTB()
2079 1994 warn('Failure executing file: <%s>' % fname)
2080 1995 except:
2081 1996 self.InteractiveTB()
2082 1997 warn('Failure executing file: <%s>' % fname)
2083 1998
2084 1999 #************************* end of file <iplib.py> *****************************
@@ -1,736 +1,725 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 IPython -- An enhanced Interactive Python
4 4
5 5 Requires Python 2.1 or better.
6 6
7 7 This file contains the main make_IPython() starter function.
8 8
9 $Id: ipmaker.py 582 2005-05-13 21:20:00Z fperez $"""
9 $Id: ipmaker.py 638 2005-07-18 03:01:41Z fperez $"""
10 10
11 11 #*****************************************************************************
12 12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
13 13 #
14 14 # Distributed under the terms of the BSD License. The full license is in
15 15 # the file COPYING, distributed as part of this software.
16 16 #*****************************************************************************
17 17
18 18 from IPython import Release
19 19 __author__ = '%s <%s>' % Release.authors['Fernando']
20 20 __license__ = Release.license
21 21 __version__ = Release.version
22 22
23 23 credits._Printer__data = """
24 24 Python: %s
25 25
26 26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
27 27 See http://ipython.scipy.org for more information.""" \
28 28 % credits._Printer__data
29 29
30 30 copyright._Printer__data += """
31 31
32 32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
33 33 All Rights Reserved."""
34 34
35 35 #****************************************************************************
36 36 # Required modules
37 37
38 38 # From the standard library
39 39 import __main__, __builtin__
40 40 import os,sys,types,re
41 41 from pprint import pprint,pformat
42 42
43 43 # Our own
44 44 from IPython import DPyGetOpt
45 45 from IPython.Struct import Struct
46 46 from IPython.OutputTrap import OutputTrap
47 47 from IPython.ConfigLoader import ConfigLoader
48 48 from IPython.iplib import InteractiveShell,qw_lol,import_fail_info
49 49 from IPython.usage import cmd_line_usage,interactive_usage
50 50 from IPython.Prompts import CachedOutput
51 51 from IPython.genutils import *
52 52
53 53 #-----------------------------------------------------------------------------
54 54 def make_IPython(argv=None,user_ns=None,debug=1,rc_override=None,
55 55 shell_class=InteractiveShell,embedded=False,**kw):
56 56 """This is a dump of IPython into a single function.
57 57
58 58 Later it will have to be broken up in a sensible manner.
59 59
60 60 Arguments:
61 61
62 62 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
63 63 script name, b/c DPyGetOpt strips the first argument only for the real
64 64 sys.argv.
65 65
66 66 - user_ns: a dict to be used as the user's namespace."""
67 67
68 68 #----------------------------------------------------------------------
69 69 # Defaults and initialization
70 70
71 71 # For developer debugging, deactivates crash handler and uses pdb.
72 72 DEVDEBUG = False
73 73
74 74 if argv is None:
75 75 argv = sys.argv
76 76
77 77 # __IP is the main global that lives throughout and represents the whole
78 78 # application. If the user redefines it, all bets are off as to what
79 79 # happens.
80 80
81 81 # __IP is the name of he global which the caller will have accessible as
82 82 # __IP.name. We set its name via the first parameter passed to
83 83 # InteractiveShell:
84 84
85 85 IP = shell_class('__IP',user_ns=user_ns,**kw)
86 86
87 87 # Put 'help' in the user namespace
88 try:
89 from site import _Helper
90 except ImportError:
91 # Use the _Helper class from Python 2.2 for older Python versions
92 class _Helper:
93 def __repr__(self):
94 return "Type help() for interactive help, " \
95 "or help(object) for help about object."
96 def __call__(self, *args, **kwds):
97 import pydoc
98 return pydoc.help(*args, **kwds)
99 else:
100 IP.user_ns['help'] = _Helper()
88 from site import _Helper
89 IP.user_ns['help'] = _Helper()
101 90
102 91 if DEVDEBUG:
103 92 # For developer debugging only (global flag)
104 93 from IPython import ultraTB
105 94 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
106 95 else:
107 96 # IPython itself shouldn't crash. This will produce a detailed
108 97 # post-mortem if it does
109 98 from IPython import CrashHandler
110 99 sys.excepthook = CrashHandler.CrashHandler(IP)
111 100
112 101 IP.BANNER_PARTS = ['Python %s\n'
113 102 'Type "copyright", "credits" or "license" '
114 103 'for more information.\n'
115 104 % (sys.version.split('\n')[0],),
116 105 "IPython %s -- An enhanced Interactive Python."
117 106 % (__version__,),
118 107 """? -> Introduction to IPython's features.
119 108 %magic -> Information about IPython's 'magic' % functions.
120 109 help -> Python's own help system.
121 110 object? -> Details about 'object'. ?object also works, ?? prints more.
122 111 """ ]
123 112
124 113 IP.usage = interactive_usage
125 114
126 115 # Platform-dependent suffix and directory names
127 116 if os.name == 'posix':
128 117 rc_suffix = ''
129 118 ipdir_def = '.ipython'
130 119 else:
131 120 rc_suffix = '.ini'
132 121 ipdir_def = '_ipython'
133 122
134 123 # default directory for configuration
135 124 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
136 125 os.path.join(IP.home_dir,ipdir_def)))
137 126
138 127 # we need the directory where IPython itself is installed
139 128 import IPython
140 129 IPython_dir = os.path.dirname(IPython.__file__)
141 130 del IPython
142 131
143 132 #-------------------------------------------------------------------------
144 133 # Command line handling
145 134
146 135 # Valid command line options (uses DPyGetOpt syntax, like Perl's
147 136 # GetOpt::Long)
148 137
149 138 # Any key not listed here gets deleted even if in the file (like session
150 139 # or profile). That's deliberate, to maintain the rc namespace clean.
151 140
152 141 # Each set of options appears twice: under _conv only the names are
153 142 # listed, indicating which type they must be converted to when reading the
154 143 # ipythonrc file. And under DPyGetOpt they are listed with the regular
155 144 # DPyGetOpt syntax (=s,=i,:f,etc).
156 145
157 146 # Make sure there's a space before each end of line (they get auto-joined!)
158 147 cmdline_opts = ('autocall! autoindent! automagic! banner! cache_size|cs=i '
159 148 'c=s classic|cl color_info! colors=s confirm_exit! '
160 149 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
161 150 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
162 151 'quick screen_length|sl=i prompts_pad_left=i '
163 152 'logfile|lf=s logplay|lp=s profile|p=s '
164 153 'readline! readline_merge_completions! '
165 154 'readline_omit__names! '
166 155 'rcfile=s separate_in|si=s separate_out|so=s '
167 156 'separate_out2|so2=s xmode=s '
168 157 'magic_docstrings system_verbose! '
169 158 'multi_line_specials!')
170 159
171 160 # Options that can *only* appear at the cmd line (not in rcfiles).
172 161
173 162 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
174 163 # the 'C-c !' command in emacs automatically appends a -i option at the end.
175 164 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
176 165 'gthread! qthread! wthread! pylab! tk!')
177 166
178 167 # Build the actual name list to be used by DPyGetOpt
179 168 opts_names = qw(cmdline_opts) + qw(cmdline_only)
180 169
181 170 # Set sensible command line defaults.
182 171 # This should have everything from cmdline_opts and cmdline_only
183 172 opts_def = Struct(autocall = 1,
184 173 autoindent=0,
185 174 automagic = 1,
186 175 banner = 1,
187 176 cache_size = 1000,
188 177 c = '',
189 178 classic = 0,
190 179 colors = 'NoColor',
191 180 color_info = 0,
192 181 confirm_exit = 1,
193 182 debug = 0,
194 183 deep_reload = 0,
195 184 editor = '0',
196 185 help = 0,
197 186 ignore = 0,
198 187 ipythondir = ipythondir,
199 188 log = 0,
200 189 logfile = '',
201 190 logplay = '',
202 191 multi_line_specials = 1,
203 192 messages = 1,
204 193 nosep = 0,
205 194 pdb = 0,
206 195 pprint = 0,
207 196 profile = '',
208 197 prompt_in1 = 'In [\\#]:',
209 198 prompt_in2 = ' .\\D.:',
210 199 prompt_out = 'Out[\\#]:',
211 200 prompts_pad_left = 1,
212 201 quick = 0,
213 202 readline = 1,
214 203 readline_merge_completions = 1,
215 204 readline_omit__names = 0,
216 205 rcfile = 'ipythonrc' + rc_suffix,
217 206 screen_length = 0,
218 207 separate_in = '\n',
219 208 separate_out = '\n',
220 209 separate_out2 = '',
221 210 system_verbose = 0,
222 211 gthread = 0,
223 212 qthread = 0,
224 213 wthread = 0,
225 214 pylab = 0,
226 215 tk = 0,
227 216 upgrade = 0,
228 217 Version = 0,
229 218 xmode = 'Verbose',
230 219 magic_docstrings = 0, # undocumented, for doc generation
231 220 )
232 221
233 222 # Things that will *only* appear in rcfiles (not at the command line).
234 223 # Make sure there's a space before each end of line (they get auto-joined!)
235 224 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
236 225 qw_lol: 'import_some ',
237 226 # for things with embedded whitespace:
238 227 list_strings:'execute alias readline_parse_and_bind ',
239 228 # Regular strings need no conversion:
240 229 None:'readline_remove_delims ',
241 230 }
242 231 # Default values for these
243 232 rc_def = Struct(include = [],
244 233 import_mod = [],
245 234 import_all = [],
246 235 import_some = [[]],
247 236 execute = [],
248 237 execfile = [],
249 238 alias = [],
250 239 readline_parse_and_bind = [],
251 240 readline_remove_delims = '',
252 241 )
253 242
254 243 # Build the type conversion dictionary from the above tables:
255 244 typeconv = rcfile_opts.copy()
256 245 typeconv.update(optstr2types(cmdline_opts))
257 246
258 247 # FIXME: the None key appears in both, put that back together by hand. Ugly!
259 248 typeconv[None] += ' ' + rcfile_opts[None]
260 249
261 250 # Remove quotes at ends of all strings (used to protect spaces)
262 251 typeconv[unquote_ends] = typeconv[None]
263 252 del typeconv[None]
264 253
265 254 # Build the list we'll use to make all config decisions with defaults:
266 255 opts_all = opts_def.copy()
267 256 opts_all.update(rc_def)
268 257
269 258 # Build conflict resolver for recursive loading of config files:
270 259 # - preserve means the outermost file maintains the value, it is not
271 260 # overwritten if an included file has the same key.
272 261 # - add_flip applies + to the two values, so it better make sense to add
273 262 # those types of keys. But it flips them first so that things loaded
274 263 # deeper in the inclusion chain have lower precedence.
275 264 conflict = {'preserve': ' '.join([ typeconv[int],
276 265 typeconv[unquote_ends] ]),
277 266 'add_flip': ' '.join([ typeconv[qwflat],
278 267 typeconv[qw_lol],
279 268 typeconv[list_strings] ])
280 269 }
281 270
282 271 # Now actually process the command line
283 272 getopt = DPyGetOpt.DPyGetOpt()
284 273 getopt.setIgnoreCase(0)
285 274
286 275 getopt.parseConfiguration(opts_names)
287 276
288 277 try:
289 278 getopt.processArguments(argv)
290 279 except:
291 280 print cmd_line_usage
292 281 warn('\nError in Arguments: ' + `sys.exc_value`)
293 282 sys.exit()
294 283
295 284 # convert the options dict to a struct for much lighter syntax later
296 285 opts = Struct(getopt.optionValues)
297 286 args = getopt.freeValues
298 287
299 288 # this is the struct (which has default values at this point) with which
300 289 # we make all decisions:
301 290 opts_all.update(opts)
302 291
303 292 # Options that force an immediate exit
304 293 if opts_all.help:
305 294 page(cmd_line_usage)
306 295 sys.exit()
307 296
308 297 if opts_all.Version:
309 298 print __version__
310 299 sys.exit()
311 300
312 301 if opts_all.magic_docstrings:
313 302 IP.magic_magic('-latex')
314 303 sys.exit()
315 304
316 305 # Create user config directory if it doesn't exist. This must be done
317 306 # *after* getting the cmd line options.
318 307 if not os.path.isdir(opts_all.ipythondir):
319 308 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
320 309
321 310 # upgrade user config files while preserving a copy of the originals
322 311 if opts_all.upgrade:
323 312 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
324 313
325 314 # check mutually exclusive options in the *original* command line
326 315 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
327 316 qw('classic profile'),qw('classic rcfile')])
328 317
329 318 # default logfilename used when -log is called.
330 319 IP.LOGDEF = 'ipython.log'
331 320
332 321 #---------------------------------------------------------------------------
333 322 # Log replay
334 323
335 324 # if -logplay, we need to 'become' the other session. That basically means
336 325 # replacing the current command line environment with that of the old
337 326 # session and moving on.
338 327
339 328 # this is needed so that later we know we're in session reload mode, as
340 329 # opts_all will get overwritten:
341 330 load_logplay = 0
342 331
343 332 if opts_all.logplay:
344 333 load_logplay = opts_all.logplay
345 334 opts_debug_save = opts_all.debug
346 335 try:
347 336 logplay = open(opts_all.logplay)
348 337 except IOError:
349 338 if opts_all.debug: IP.InteractiveTB()
350 339 warn('Could not open logplay file '+`opts_all.logplay`)
351 340 # restore state as if nothing had happened and move on, but make
352 341 # sure that later we don't try to actually load the session file
353 342 logplay = None
354 343 load_logplay = 0
355 344 del opts_all.logplay
356 345 else:
357 346 try:
358 347 logplay.readline()
359 348 logplay.readline();
360 349 # this reloads that session's command line
361 350 cmd = logplay.readline()[6:]
362 351 exec cmd
363 352 # restore the true debug flag given so that the process of
364 353 # session loading itself can be monitored.
365 354 opts.debug = opts_debug_save
366 355 # save the logplay flag so later we don't overwrite the log
367 356 opts.logplay = load_logplay
368 357 # now we must update our own structure with defaults
369 358 opts_all.update(opts)
370 359 # now load args
371 360 cmd = logplay.readline()[6:]
372 361 exec cmd
373 362 logplay.close()
374 363 except:
375 364 logplay.close()
376 365 if opts_all.debug: IP.InteractiveTB()
377 366 warn("Logplay file lacking full configuration information.\n"
378 367 "I'll try to read it, but some things may not work.")
379 368
380 369 #-------------------------------------------------------------------------
381 370 # set up output traps: catch all output from files, being run, modules
382 371 # loaded, etc. Then give it to the user in a clean form at the end.
383 372
384 373 msg_out = 'Output messages. '
385 374 msg_err = 'Error messages. '
386 375 msg_sep = '\n'
387 376 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
388 377 msg_err,msg_sep,debug,
389 378 quiet_out=1),
390 379 user_exec = OutputTrap('User File Execution',msg_out,
391 380 msg_err,msg_sep,debug),
392 381 logplay = OutputTrap('Log Loader',msg_out,
393 382 msg_err,msg_sep,debug),
394 383 summary = ''
395 384 )
396 385
397 386 #-------------------------------------------------------------------------
398 387 # Process user ipythonrc-type configuration files
399 388
400 389 # turn on output trapping and log to msg.config
401 390 # remember that with debug on, trapping is actually disabled
402 391 msg.config.trap_all()
403 392
404 393 # look for rcfile in current or default directory
405 394 try:
406 395 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
407 396 except IOError:
408 397 if opts_all.debug: IP.InteractiveTB()
409 398 warn('Configuration file %s not found. Ignoring request.'
410 399 % (opts_all.rcfile) )
411 400
412 401 # 'profiles' are a shorthand notation for config filenames
413 402 if opts_all.profile:
414 403 try:
415 404 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
416 405 + rc_suffix,
417 406 opts_all.ipythondir)
418 407 except IOError:
419 408 if opts_all.debug: IP.InteractiveTB()
420 409 opts.profile = '' # remove profile from options if invalid
421 410 warn('Profile configuration file %s not found. Ignoring request.'
422 411 % (opts_all.profile) )
423 412
424 413 # load the config file
425 414 rcfiledata = None
426 415 if opts_all.quick:
427 416 print 'Launching IPython in quick mode. No config file read.'
428 417 elif opts_all.classic:
429 418 print 'Launching IPython in classic mode. No config file read.'
430 419 elif opts_all.rcfile:
431 420 try:
432 421 cfg_loader = ConfigLoader(conflict)
433 422 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
434 423 'include',opts_all.ipythondir,
435 424 purge = 1,
436 425 unique = conflict['preserve'])
437 426 except:
438 427 IP.InteractiveTB()
439 428 warn('Problems loading configuration file '+
440 429 `opts_all.rcfile`+
441 430 '\nStarting with default -bare bones- configuration.')
442 431 else:
443 432 warn('No valid configuration file found in either currrent directory\n'+
444 433 'or in the IPython config. directory: '+`opts_all.ipythondir`+
445 434 '\nProceeding with internal defaults.')
446 435
447 436 #------------------------------------------------------------------------
448 437 # Set exception handlers in mode requested by user.
449 438 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
450 439 IP.magic_xmode(opts_all.xmode)
451 440 otrap.release_out()
452 441
453 442 #------------------------------------------------------------------------
454 443 # Execute user config
455 444
456 445 # first, create a valid config structure with the right precedence order:
457 446 # defaults < rcfile < command line
458 447 IP.rc = rc_def.copy()
459 448 IP.rc.update(opts_def)
460 449 if rcfiledata:
461 450 # now we can update
462 451 IP.rc.update(rcfiledata)
463 452 IP.rc.update(opts)
464 453 IP.rc.update(rc_override)
465 454
466 455 # Store the original cmd line for reference:
467 456 IP.rc.opts = opts
468 457 IP.rc.args = args
469 458
470 459 # create a *runtime* Struct like rc for holding parameters which may be
471 460 # created and/or modified by runtime user extensions.
472 461 IP.runtime_rc = Struct()
473 462
474 463 # from this point on, all config should be handled through IP.rc,
475 464 # opts* shouldn't be used anymore.
476 465
477 466 # add personal .ipython dir to sys.path so that users can put things in
478 467 # there for customization
479 468 sys.path.append(IP.rc.ipythondir)
480 469 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
481 470
482 471 # update IP.rc with some special things that need manual
483 472 # tweaks. Basically options which affect other options. I guess this
484 473 # should just be written so that options are fully orthogonal and we
485 474 # wouldn't worry about this stuff!
486 475
487 476 if IP.rc.classic:
488 477 IP.rc.quick = 1
489 478 IP.rc.cache_size = 0
490 479 IP.rc.pprint = 0
491 480 IP.rc.prompt_in1 = '>>> '
492 481 IP.rc.prompt_in2 = '... '
493 482 IP.rc.prompt_out = ''
494 483 IP.rc.separate_in = IP.rc.separate_out = IP.rc.separate_out2 = '0'
495 484 IP.rc.colors = 'NoColor'
496 485 IP.rc.xmode = 'Plain'
497 486
498 487 # configure readline
499 488 # Define the history file for saving commands in between sessions
500 489 if IP.rc.profile:
501 490 histfname = 'history-%s' % IP.rc.profile
502 491 else:
503 492 histfname = 'history'
504 493 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
505 494 # Load readline proper
506 495 if IP.rc.readline:
507 496 IP.init_readline()
508 497
509 498 # update exception handlers with rc file status
510 499 otrap.trap_out() # I don't want these messages ever.
511 500 IP.magic_xmode(IP.rc.xmode)
512 501 otrap.release_out()
513 502
514 503 # activate logging if requested and not reloading a log
515 504 if IP.rc.logplay:
516 505 IP.magic_logstart(IP.rc.logplay + ' append')
517 506 elif IP.rc.logfile:
518 507 IP.magic_logstart(IP.rc.logfile)
519 508 elif IP.rc.log:
520 509 IP.magic_logstart()
521 510
522 511 # find user editor so that it we don't have to look it up constantly
523 512 if IP.rc.editor.strip()=='0':
524 513 try:
525 514 ed = os.environ['EDITOR']
526 515 except KeyError:
527 516 if os.name == 'posix':
528 517 ed = 'vi' # the only one guaranteed to be there!
529 518 else:
530 519 ed = 'notepad' # same in Windows!
531 520 IP.rc.editor = ed
532 521
533 522 # Recursive reload
534 523 try:
535 524 from IPython import deep_reload
536 525 if IP.rc.deep_reload:
537 526 __builtin__.reload = deep_reload.reload
538 527 else:
539 528 __builtin__.dreload = deep_reload.reload
540 529 del deep_reload
541 530 except ImportError:
542 531 pass
543 532
544 533 # Save the current state of our namespace so that the interactive shell
545 534 # can later know which variables have been created by us from config files
546 535 # and loading. This way, loading a file (in any way) is treated just like
547 536 # defining things on the command line, and %who works as expected.
548 537
549 538 # DON'T do anything that affects the namespace beyond this point!
550 539 IP.internal_ns = __main__.__dict__.copy()
551 540
552 541 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
553 542
554 543 # Now run through the different sections of the users's config
555 544 if IP.rc.debug:
556 545 print 'Trying to execute the following configuration structure:'
557 546 print '(Things listed first are deeper in the inclusion tree and get'
558 547 print 'loaded first).\n'
559 548 pprint(IP.rc.__dict__)
560 549
561 550 for mod in IP.rc.import_mod:
562 551 try:
563 552 exec 'import '+mod in IP.user_ns
564 553 except :
565 554 IP.InteractiveTB()
566 555 import_fail_info(mod)
567 556
568 557 for mod_fn in IP.rc.import_some:
569 558 if mod_fn == []: break
570 559 mod,fn = mod_fn[0],','.join(mod_fn[1:])
571 560 try:
572 561 exec 'from '+mod+' import '+fn in IP.user_ns
573 562 except :
574 563 IP.InteractiveTB()
575 564 import_fail_info(mod,fn)
576 565
577 566 for mod in IP.rc.import_all:
578 567 try:
579 568 exec 'from '+mod+' import *' in IP.user_ns
580 569 except :
581 570 IP.InteractiveTB()
582 571 import_fail_info(mod)
583 572
584 573 for code in IP.rc.execute:
585 574 try:
586 575 exec code in IP.user_ns
587 576 except:
588 577 IP.InteractiveTB()
589 578 warn('Failure executing code: ' + `code`)
590 579
591 580 # Execute the files the user wants in ipythonrc
592 581 for file in IP.rc.execfile:
593 582 try:
594 583 file = filefind(file,sys.path+[IPython_dir])
595 584 except IOError:
596 585 warn(itpl('File $file not found. Skipping it.'))
597 586 else:
598 587 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
599 588
600 589 # Load user aliases
601 590 for alias in IP.rc.alias:
602 591 IP.magic_alias(alias)
603 592
604 593 # release stdout and stderr and save config log into a global summary
605 594 msg.config.release_all()
606 595 if IP.rc.messages:
607 596 msg.summary += msg.config.summary_all()
608 597
609 598 #------------------------------------------------------------------------
610 599 # Setup interactive session
611 600
612 601 # Now we should be fully configured. We can then execute files or load
613 602 # things only needed for interactive use. Then we'll open the shell.
614 603
615 604 # Take a snapshot of the user namespace before opening the shell. That way
616 605 # we'll be able to identify which things were interactively defined and
617 606 # which were defined through config files.
618 607 IP.user_config_ns = IP.user_ns.copy()
619 608
620 609 # Force reading a file as if it were a session log. Slower but safer.
621 610 if load_logplay:
622 611 print 'Replaying log...'
623 612 try:
624 613 if IP.rc.debug:
625 614 logplay_quiet = 0
626 615 else:
627 616 logplay_quiet = 1
628 617
629 618 msg.logplay.trap_all()
630 619 IP.safe_execfile(load_logplay,IP.user_ns,
631 620 islog = 1, quiet = logplay_quiet)
632 621 msg.logplay.release_all()
633 622 if IP.rc.messages:
634 623 msg.summary += msg.logplay.summary_all()
635 624 except:
636 625 warn('Problems replaying logfile %s.' % load_logplay)
637 626 IP.InteractiveTB()
638 627
639 628 # Load remaining files in command line
640 629 msg.user_exec.trap_all()
641 630
642 631 # Do NOT execute files named in the command line as scripts to be loaded
643 632 # by embedded instances. Doing so has the potential for an infinite
644 633 # recursion if there are exceptions thrown in the process.
645 634
646 635 # XXX FIXME: the execution of user files should be moved out to after
647 636 # ipython is fully initialized, just as if they were run via %run at the
648 637 # ipython prompt. This would also give them the benefit of ipython's
649 638 # nice tracebacks.
650 639
651 640 if not embedded and IP.rc.args:
652 641 name_save = IP.user_ns['__name__']
653 642 IP.user_ns['__name__'] = '__main__'
654 643 try:
655 644 # Set our own excepthook in case the user code tries to call it
656 645 # directly. This prevents triggering the IPython crash handler.
657 646 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
658 647 for run in args:
659 648 IP.safe_execfile(run,IP.user_ns)
660 649 finally:
661 650 # Reset our crash handler in place
662 651 sys.excepthook = old_excepthook
663 652
664 653 IP.user_ns['__name__'] = name_save
665 654
666 655 msg.user_exec.release_all()
667 656 if IP.rc.messages:
668 657 msg.summary += msg.user_exec.summary_all()
669 658
670 659 # since we can't specify a null string on the cmd line, 0 is the equivalent:
671 660 if IP.rc.nosep:
672 661 IP.rc.separate_in = IP.rc.separate_out = IP.rc.separate_out2 = '0'
673 662 if IP.rc.separate_in == '0': IP.rc.separate_in = ''
674 663 if IP.rc.separate_out == '0': IP.rc.separate_out = ''
675 664 if IP.rc.separate_out2 == '0': IP.rc.separate_out2 = ''
676 665 IP.rc.separate_in = IP.rc.separate_in.replace('\\n','\n')
677 666 IP.rc.separate_out = IP.rc.separate_out.replace('\\n','\n')
678 667 IP.rc.separate_out2 = IP.rc.separate_out2.replace('\\n','\n')
679 668
680 669 # Determine how many lines at the bottom of the screen are needed for
681 670 # showing prompts, so we can know wheter long strings are to be printed or
682 671 # paged:
683 672 num_lines_bot = IP.rc.separate_in.count('\n')+1
684 673 IP.rc.screen_length = IP.rc.screen_length - num_lines_bot
685 674 # Initialize cache, set in/out prompts and printing system
686 675 IP.outputcache = CachedOutput(IP.rc.cache_size,
687 676 IP.rc.pprint,
688 677 input_sep = IP.rc.separate_in,
689 678 output_sep = IP.rc.separate_out,
690 679 output_sep2 = IP.rc.separate_out2,
691 680 ps1 = IP.rc.prompt_in1,
692 681 ps2 = IP.rc.prompt_in2,
693 682 ps_out = IP.rc.prompt_out,
694 683 user_ns = IP.user_ns,
695 684 input_hist = IP.input_hist,
696 685 pad_left = IP.rc.prompts_pad_left)
697 686
698 687 # Set user colors (don't do it in the constructor above so that it doesn't
699 688 # crash if colors option is invalid)
700 689 IP.magic_colors(IP.rc.colors)
701 690
702 691 # user may have over-ridden the default print hook:
703 692 try:
704 693 IP.outputcache.__class__.display = IP.hooks.display
705 694 except AttributeError:
706 695 pass
707 696
708 697 # Set calling of pdb on exceptions
709 698 IP.InteractiveTB.call_pdb = IP.rc.pdb
710 699
711 700 # I don't like assigning globally to sys, because it means when embedding
712 701 # instances, each embedded instance overrides the previous choice. But
713 702 # sys.displayhook seems to be called internally by exec, so I don't see a
714 703 # way around it.
715 704 sys.displayhook = IP.outputcache
716 705
717 706 # we need to know globally if we're caching i/o or not
718 707 IP.do_full_cache = IP.outputcache.do_full_cache
719 708
720 709 # configure startup banner
721 710 if IP.rc.c: # regular python doesn't print the banner with -c
722 711 IP.rc.banner = 0
723 712 if IP.rc.banner:
724 713 IP.BANNER = '\n'.join(IP.BANNER_PARTS)
725 714 else:
726 715 IP.BANNER = ''
727 716
728 717 if IP.rc.profile: IP.BANNER += '\nIPython profile: '+IP.rc.profile+'\n'
729 718
730 719 # add message log (possibly empty)
731 720 IP.BANNER += msg.summary
732 721
733 722 IP.post_config_initialization()
734 723
735 724 return IP
736 725 #************************ end of file <ipmaker.py> **************************
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now