##// END OF EJS Templates
Merged 1071-1076 from banches/0.7.1
vivainio -
Show More

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

@@ -1,233 +1,234 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Logger class for IPython's logging facilities.
3 Logger class for IPython's logging facilities.
4
4
5 $Id: Logger.py 994 2006-01-08 08:29:44Z fperez $
5 $Id: Logger.py 1077 2006-01-24 18:15:27Z vivainio $
6 """
6 """
7
7
8 #*****************************************************************************
8 #*****************************************************************************
9 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
9 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 #****************************************************************************
16 #****************************************************************************
17 # Modules and globals
17 # Modules and globals
18
18
19 from IPython import Release
19 from IPython import Release
20 __author__ = '%s <%s>\n%s <%s>' % \
20 __author__ = '%s <%s>\n%s <%s>' % \
21 ( Release.authors['Janko'] + Release.authors['Fernando'] )
21 ( Release.authors['Janko'] + Release.authors['Fernando'] )
22 __license__ = Release.license
22 __license__ = Release.license
23
23
24 # Python standard modules
24 # Python standard modules
25 import glob
25 import glob
26 import os
26 import os
27 import time
27 import time
28
28
29 #****************************************************************************
29 #****************************************************************************
30 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
30 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
31 # ipython and does input cache management. Finish cleanup later...
31 # ipython and does input cache management. Finish cleanup later...
32
32
33 class Logger(object):
33 class Logger(object):
34 """A Logfile class with different policies for file creation"""
34 """A Logfile class with different policies for file creation"""
35
35
36 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
36 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
37
37
38 self._i00,self._i,self._ii,self._iii = '','','',''
38 self._i00,self._i,self._ii,self._iii = '','','',''
39
39
40 # this is the full ipython instance, we need some attributes from it
40 # this is the full ipython instance, we need some attributes from it
41 # which won't exist until later. What a mess, clean up later...
41 # which won't exist until later. What a mess, clean up later...
42 self.shell = shell
42 self.shell = shell
43
43
44 self.logfname = logfname
44 self.logfname = logfname
45 self.loghead = loghead
45 self.loghead = loghead
46 self.logmode = logmode
46 self.logmode = logmode
47 self.logfile = None
47 self.logfile = None
48
48
49 # whether to also log output
49 # whether to also log output
50 self.log_output = False
50 self.log_output = False
51
51
52 # whether to put timestamps before each log entry
52 # whether to put timestamps before each log entry
53 self.timestamp = False
53 self.timestamp = False
54
54
55 # activity control flags
55 # activity control flags
56 self.log_active = False
56 self.log_active = False
57
57
58 # logmode is a validated property
58 # logmode is a validated property
59 def _set_mode(self,mode):
59 def _set_mode(self,mode):
60 if mode not in ['append','backup','global','over','rotate']:
60 if mode not in ['append','backup','global','over','rotate']:
61 raise ValueError,'invalid log mode %s given' % mode
61 raise ValueError,'invalid log mode %s given' % mode
62 self._logmode = mode
62 self._logmode = mode
63
63
64 def _get_mode(self):
64 def _get_mode(self):
65 return self._logmode
65 return self._logmode
66
66
67 logmode = property(_get_mode,_set_mode)
67 logmode = property(_get_mode,_set_mode)
68
68
69 def logstart(self,logfname=None,loghead=None,logmode=None,
69 def logstart(self,logfname=None,loghead=None,logmode=None,
70 log_output=False,timestamp=False):
70 log_output=False,timestamp=False):
71 """Generate a new log-file with a default header.
71 """Generate a new log-file with a default header.
72
72
73 Raises RuntimeError if the log has already been started"""
73 Raises RuntimeError if the log has already been started"""
74
74
75 if self.logfile is not None:
75 if self.logfile is not None:
76 raise RuntimeError('Log file is already active: %s' %
76 raise RuntimeError('Log file is already active: %s' %
77 self.logfname)
77 self.logfname)
78
78
79 self.log_active = True
79 self.log_active = True
80
80
81 # The three parameters can override constructor defaults
81 # The three parameters can override constructor defaults
82 if logfname: self.logfname = logfname
82 if logfname: self.logfname = logfname
83 if loghead: self.loghead = loghead
83 if loghead: self.loghead = loghead
84 if logmode: self.logmode = logmode
84 if logmode: self.logmode = logmode
85 self.timestamp = timestamp
85 self.timestamp = timestamp
86 self.log_output = log_output
86 self.log_output = log_output
87
87
88 # init depending on the log mode requested
88 # init depending on the log mode requested
89 isfile = os.path.isfile
89 isfile = os.path.isfile
90 logmode = self.logmode
90 logmode = self.logmode
91
91
92 if logmode == 'append':
92 if logmode == 'append':
93 self.logfile = open(self.logfname,'a')
93 self.logfile = open(self.logfname,'a')
94
94
95 elif logmode == 'backup':
95 elif logmode == 'backup':
96 if isfile(self.logfname):
96 if isfile(self.logfname):
97 backup_logname = self.logfname+'~'
97 backup_logname = self.logfname+'~'
98 # Manually remove any old backup, since os.rename may fail
98 # Manually remove any old backup, since os.rename may fail
99 # under Windows.
99 # under Windows.
100 if isfile(backup_logname):
100 if isfile(backup_logname):
101 os.remove(backup_logname)
101 os.remove(backup_logname)
102 os.rename(self.logfname,backup_logname)
102 os.rename(self.logfname,backup_logname)
103 self.logfile = open(self.logfname,'w')
103 self.logfile = open(self.logfname,'w')
104
104
105 elif logmode == 'global':
105 elif logmode == 'global':
106 self.logfname = os.path.join(self.shell.home_dir,self.logfname)
106 self.logfname = os.path.join(self.shell.home_dir,self.logfname)
107 self.logfile = open(self.logfname, 'a')
107 self.logfile = open(self.logfname, 'a')
108
108
109 elif logmode == 'over':
109 elif logmode == 'over':
110 if isfile(self.logfname):
110 if isfile(self.logfname):
111 os.remove(self.logfname)
111 os.remove(self.logfname)
112 self.logfile = open(self.logfname,'w')
112 self.logfile = open(self.logfname,'w')
113
113
114 elif logmode == 'rotate':
114 elif logmode == 'rotate':
115 if isfile(self.logfname):
115 if isfile(self.logfname):
116 if isfile(self.logfname+'.001~'):
116 if isfile(self.logfname+'.001~'):
117 old = glob.glob(self.logfname+'.*~')
117 old = glob.glob(self.logfname+'.*~')
118 old.sort()
118 old.sort()
119 old.reverse()
119 old.reverse()
120 for f in old:
120 for f in old:
121 root, ext = os.path.splitext(f)
121 root, ext = os.path.splitext(f)
122 num = int(ext[1:-1])+1
122 num = int(ext[1:-1])+1
123 os.rename(f, root+'.'+`num`.zfill(3)+'~')
123 os.rename(f, root+'.'+`num`.zfill(3)+'~')
124 os.rename(self.logfname, self.logfname+'.001~')
124 os.rename(self.logfname, self.logfname+'.001~')
125 self.logfile = open(self.logfname,'w')
125 self.logfile = open(self.logfname,'w')
126
126
127 if logmode != 'append':
127 if logmode != 'append':
128 self.logfile.write(self.loghead)
128 self.logfile.write(self.loghead)
129
129
130 self.logfile.flush()
130 self.logfile.flush()
131
131
132 def switch_log(self,val):
132 def switch_log(self,val):
133 """Switch logging on/off. val should be ONLY a boolean."""
133 """Switch logging on/off. val should be ONLY a boolean."""
134
134
135 if val not in [False,True,0,1]:
135 if val not in [False,True,0,1]:
136 raise ValueError, \
136 raise ValueError, \
137 'Call switch_log ONLY with a boolean argument, not with:',val
137 'Call switch_log ONLY with a boolean argument, not with:',val
138
138
139 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
139 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
140
140
141 if self.logfile is None:
141 if self.logfile is None:
142 print """
142 print """
143 Logging hasn't been started yet (use logstart for that).
143 Logging hasn't been started yet (use logstart for that).
144
144
145 %logon/%logoff are for temporarily starting and stopping logging for a logfile
145 %logon/%logoff are for temporarily starting and stopping logging for a logfile
146 which already exists. But you must first start the logging process with
146 which already exists. But you must first start the logging process with
147 %logstart (optionally giving a logfile name)."""
147 %logstart (optionally giving a logfile name)."""
148
148
149 else:
149 else:
150 if self.log_active == val:
150 if self.log_active == val:
151 print 'Logging is already',label[val]
151 print 'Logging is already',label[val]
152 else:
152 else:
153 print 'Switching logging',label[val]
153 print 'Switching logging',label[val]
154 self.log_active = not self.log_active
154 self.log_active = not self.log_active
155 self.log_active_out = self.log_active
155 self.log_active_out = self.log_active
156
156
157 def logstate(self):
157 def logstate(self):
158 """Print a status message about the logger."""
158 """Print a status message about the logger."""
159 if self.logfile is None:
159 if self.logfile is None:
160 print 'Logging has not been activated.'
160 print 'Logging has not been activated.'
161 else:
161 else:
162 state = self.log_active and 'active' or 'temporarily suspended'
162 state = self.log_active and 'active' or 'temporarily suspended'
163 print 'Filename :',self.logfname
163 print 'Filename :',self.logfname
164 print 'Mode :',self.logmode
164 print 'Mode :',self.logmode
165 print 'Output logging :',self.log_output
165 print 'Output logging :',self.log_output
166 print 'Timestamping :',self.timestamp
166 print 'Timestamping :',self.timestamp
167 print 'State :',state
167 print 'State :',state
168
168
169 def log(self, line,continuation=None):
169 def log(self, line,continuation=None):
170 """Write the line to a log and create input cache variables _i*."""
170 """Write the line to a log and create input cache variables _i*."""
171
171
172 # update the auto _i tables
172 # update the auto _i tables
173 #print '***logging line',line # dbg
173 #print '***logging line',line # dbg
174 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
174 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
175 try:
175 try:
176 input_hist = self.shell.user_ns['_ih']
176 input_hist = self.shell.user_ns['_ih']
177 except:
177 except:
178 print 'userns:',self.shell.user_ns.keys()
178 print 'userns:',self.shell.user_ns.keys()
179 return
179 return
180
180
181 if not continuation and line:
181 if not continuation and line:
182 self._iii = self._ii
182 self._iii = self._ii
183 self._ii = self._i
183 self._ii = self._i
184 self._i = self._i00
184 self._i = self._i00
185 # put back the final \n of every input line
185 # put back the final \n of every input line
186 self._i00 = line+'\n'
186 self._i00 = line+'\n'
187 #print 'Logging input:<%s>' % line # dbg
187 #print 'Logging input:<%s>' % line # dbg
188 input_hist.append(self._i00)
188 input_hist.append(self._i00)
189 #print '---[%s]' % (len(input_hist)-1,) # dbg
189 #print '---[%s]' % (len(input_hist)-1,) # dbg
190
190
191 # hackish access to top-level namespace to create _i1,_i2... dynamically
191 # hackish access to top-level namespace to create _i1,_i2... dynamically
192 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
192 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
193 if self.shell.outputcache.do_full_cache:
193 if self.shell.outputcache.do_full_cache:
194 in_num = self.shell.outputcache.prompt_count
194 in_num = self.shell.outputcache.prompt_count
195 # add blank lines if the input cache fell out of sync. This can
195 # add blank lines if the input cache fell out of sync. This can
196 # happen for embedded instances which get killed via C-D and then
196 # happen for embedded instances which get killed via C-D and then
197 # get resumed.
197 # get resumed.
198 while in_num >= len(input_hist):
198 while in_num >= len(input_hist):
199 input_hist.append('\n')
199 input_hist.append('\n')
200 # but if the opposite is true (a macro can produce multiple inputs
200 # but if the opposite is true (a macro can produce multiple inputs
201 # with no output display called), then bring the output counter in
201 # with no output display called), then bring the output counter in
202 # sync:
202 # sync:
203 last_num = len(input_hist)-1
203 last_num = len(input_hist)-1
204 if in_num != last_num:
204 if in_num != last_num:
205 in_num = self.shell.outputcache.prompt_count = last_num
205 in_num = self.shell.outputcache.prompt_count = last_num
206 new_i = '_i%s' % in_num
206 new_i = '_i%s' % in_num
207 if continuation:
207 if continuation:
208 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line)
208 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line)
209 input_hist[in_num] = self._i00
209 input_hist[in_num] = self._i00
210 to_main[new_i] = self._i00
210 to_main[new_i] = self._i00
211 self.shell.user_ns.update(to_main)
211 self.shell.user_ns.update(to_main)
212 self.log_write(line)
212 self.log_write(line)
213
213
214 def log_write(self,data,kind='input'):
214 def log_write(self,data,kind='input'):
215 """Write data to the log file, if active"""
215 """Write data to the log file, if active"""
216
216
217 #print 'data: %r' % data # dbg
217 if self.log_active and data:
218 if self.log_active and data:
218 write = self.logfile.write
219 write = self.logfile.write
219 if kind=='input':
220 if kind=='input':
220 if self.timestamp:
221 if self.timestamp:
221 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
222 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
222 time.localtime()))
223 time.localtime()))
223 write('%s\n' % data)
224 write('%s\n' % data)
224 elif kind=='output' and self.log_output:
225 elif kind=='output' and self.log_output:
225 odata = '\n'.join(['#[Out]# %s' % s
226 odata = '\n'.join(['#[Out]# %s' % s
226 for s in data.split('\n')])
227 for s in data.split('\n')])
227 write('%s\n' % odata)
228 write('%s\n' % odata)
228 self.logfile.flush()
229 self.logfile.flush()
229
230
230 def close_log(self):
231 def close_log(self):
231 self.logfile.close()
232 self.logfile.close()
232 self.logfile = None
233 self.logfile = None
233 self.logfname = ''
234 self.logfname = ''
@@ -1,2809 +1,2824 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 1076 2006-01-24 17:27:05Z vivainio $"""
4 $Id: Magic.py 1077 2006-01-24 18:15:27Z vivainio $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Modules and globals
15 # Modules and globals
16
16
17 from IPython import Release
17 from IPython import Release
18 __author__ = '%s <%s>\n%s <%s>' % \
18 __author__ = '%s <%s>\n%s <%s>' % \
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 __license__ = Release.license
20 __license__ = Release.license
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
23 import __builtin__
24 import bdb
24 import bdb
25 import inspect
25 import inspect
26 import os
26 import os
27 import pdb
27 import pdb
28 import pydoc
28 import pydoc
29 import sys
29 import sys
30 import re
30 import re
31 import tempfile
31 import tempfile
32 import time
32 import time
33 import cPickle as pickle
33 import cPickle as pickle
34 import textwrap
34 import textwrap
35 from cStringIO import StringIO
35 from cStringIO import StringIO
36 from getopt import getopt
36 from getopt import getopt
37 from pprint import pprint, pformat
37 from pprint import pprint, pformat
38
38
39 # profile isn't bundled by default in Debian for license reasons
39 # profile isn't bundled by default in Debian for license reasons
40 try:
40 try:
41 import profile,pstats
41 import profile,pstats
42 except ImportError:
42 except ImportError:
43 profile = pstats = None
43 profile = pstats = None
44
44
45 # Homebrewed
45 # Homebrewed
46 from IPython import Debugger, OInspect, wildcard
46 from IPython import Debugger, OInspect, wildcard
47 from IPython.FakeModule import FakeModule
47 from IPython.FakeModule import FakeModule
48 from IPython.Itpl import Itpl, itpl, printpl,itplns
48 from IPython.Itpl import Itpl, itpl, printpl,itplns
49 from IPython.PyColorize import Parser
49 from IPython.PyColorize import Parser
50 from IPython.ipstruct import Struct
50 from IPython.ipstruct import Struct
51 from IPython.macro import Macro
51 from IPython.macro import Macro
52 from IPython.genutils import *
52 from IPython.genutils import *
53 from IPython import platutils
53 from IPython import platutils
54
54
55 #***************************************************************************
55 #***************************************************************************
56 # Utility functions
56 # Utility functions
57 def on_off(tag):
57 def on_off(tag):
58 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
58 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
59 return ['OFF','ON'][tag]
59 return ['OFF','ON'][tag]
60
60
61 class Bunch: pass
61 class Bunch: pass
62
62
63 #***************************************************************************
63 #***************************************************************************
64 # Main class implementing Magic functionality
64 # Main class implementing Magic functionality
65 class Magic:
65 class Magic:
66 """Magic functions for InteractiveShell.
66 """Magic functions for InteractiveShell.
67
67
68 Shell functions which can be reached as %function_name. All magic
68 Shell functions which can be reached as %function_name. All magic
69 functions should accept a string, which they can parse for their own
69 functions should accept a string, which they can parse for their own
70 needs. This can make some functions easier to type, eg `%cd ../`
70 needs. This can make some functions easier to type, eg `%cd ../`
71 vs. `%cd("../")`
71 vs. `%cd("../")`
72
72
73 ALL definitions MUST begin with the prefix magic_. The user won't need it
73 ALL definitions MUST begin with the prefix magic_. The user won't need it
74 at the command line, but it is is needed in the definition. """
74 at the command line, but it is is needed in the definition. """
75
75
76 # class globals
76 # class globals
77 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
77 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
78 'Automagic is ON, % prefix NOT needed for magic functions.']
78 'Automagic is ON, % prefix NOT needed for magic functions.']
79
79
80 #......................................................................
80 #......................................................................
81 # some utility functions
81 # some utility functions
82
82
83 def __init__(self,shell):
83 def __init__(self,shell):
84
84
85 self.options_table = {}
85 self.options_table = {}
86 if profile is None:
86 if profile is None:
87 self.magic_prun = self.profile_missing_notice
87 self.magic_prun = self.profile_missing_notice
88 self.shell = shell
88 self.shell = shell
89
89
90 # namespace for holding state we may need
90 # namespace for holding state we may need
91 self._magic_state = Bunch()
91 self._magic_state = Bunch()
92
92
93 def profile_missing_notice(self, *args, **kwargs):
93 def profile_missing_notice(self, *args, **kwargs):
94 error("""\
94 error("""\
95 The profile module could not be found. If you are a Debian user,
95 The profile module could not be found. If you are a Debian user,
96 it has been removed from the standard Debian package because of its non-free
96 it has been removed from the standard Debian package because of its non-free
97 license. To use profiling, please install"python2.3-profiler" from non-free.""")
97 license. To use profiling, please install"python2.3-profiler" from non-free.""")
98
98
99 def default_option(self,fn,optstr):
99 def default_option(self,fn,optstr):
100 """Make an entry in the options_table for fn, with value optstr"""
100 """Make an entry in the options_table for fn, with value optstr"""
101
101
102 if fn not in self.lsmagic():
102 if fn not in self.lsmagic():
103 error("%s is not a magic function" % fn)
103 error("%s is not a magic function" % fn)
104 self.options_table[fn] = optstr
104 self.options_table[fn] = optstr
105
105
106 def lsmagic(self):
106 def lsmagic(self):
107 """Return a list of currently available magic functions.
107 """Return a list of currently available magic functions.
108
108
109 Gives a list of the bare names after mangling (['ls','cd', ...], not
109 Gives a list of the bare names after mangling (['ls','cd', ...], not
110 ['magic_ls','magic_cd',...]"""
110 ['magic_ls','magic_cd',...]"""
111
111
112 # FIXME. This needs a cleanup, in the way the magics list is built.
112 # FIXME. This needs a cleanup, in the way the magics list is built.
113
113
114 # magics in class definition
114 # magics in class definition
115 class_magic = lambda fn: fn.startswith('magic_') and \
115 class_magic = lambda fn: fn.startswith('magic_') and \
116 callable(Magic.__dict__[fn])
116 callable(Magic.__dict__[fn])
117 # in instance namespace (run-time user additions)
117 # in instance namespace (run-time user additions)
118 inst_magic = lambda fn: fn.startswith('magic_') and \
118 inst_magic = lambda fn: fn.startswith('magic_') and \
119 callable(self.__dict__[fn])
119 callable(self.__dict__[fn])
120 # and bound magics by user (so they can access self):
120 # and bound magics by user (so they can access self):
121 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
121 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
122 callable(self.__class__.__dict__[fn])
122 callable(self.__class__.__dict__[fn])
123 magics = filter(class_magic,Magic.__dict__.keys()) + \
123 magics = filter(class_magic,Magic.__dict__.keys()) + \
124 filter(inst_magic,self.__dict__.keys()) + \
124 filter(inst_magic,self.__dict__.keys()) + \
125 filter(inst_bound_magic,self.__class__.__dict__.keys())
125 filter(inst_bound_magic,self.__class__.__dict__.keys())
126 out = []
126 out = []
127 for fn in magics:
127 for fn in magics:
128 out.append(fn.replace('magic_','',1))
128 out.append(fn.replace('magic_','',1))
129 out.sort()
129 out.sort()
130 return out
130 return out
131
131
132 def extract_input_slices(self,slices):
132 def extract_input_slices(self,slices):
133 """Return as a string a set of input history slices.
133 """Return as a string a set of input history slices.
134
134
135 The set of slices is given as a list of strings (like ['1','4:8','9'],
135 The set of slices is given as a list of strings (like ['1','4:8','9'],
136 since this function is for use by magic functions which get their
136 since this function is for use by magic functions which get their
137 arguments as strings.
137 arguments as strings.
138
138
139 Note that slices can be called with two notations:
139 Note that slices can be called with two notations:
140
140
141 N:M -> standard python form, means including items N...(M-1).
141 N:M -> standard python form, means including items N...(M-1).
142
142
143 N-M -> include items N..M (closed endpoint)."""
143 N-M -> include items N..M (closed endpoint)."""
144
144
145 cmds = []
145 cmds = []
146 for chunk in slices:
146 for chunk in slices:
147 if ':' in chunk:
147 if ':' in chunk:
148 ini,fin = map(int,chunk.split(':'))
148 ini,fin = map(int,chunk.split(':'))
149 elif '-' in chunk:
149 elif '-' in chunk:
150 ini,fin = map(int,chunk.split('-'))
150 ini,fin = map(int,chunk.split('-'))
151 fin += 1
151 fin += 1
152 else:
152 else:
153 ini = int(chunk)
153 ini = int(chunk)
154 fin = ini+1
154 fin = ini+1
155 cmds.append(self.shell.input_hist[ini:fin])
155 cmds.append(self.shell.input_hist[ini:fin])
156 return cmds
156 return cmds
157
157
158 def _ofind(self,oname):
158 def _ofind(self,oname):
159 """Find an object in the available namespaces.
159 """Find an object in the available namespaces.
160
160
161 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
161 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
162
162
163 Has special code to detect magic functions.
163 Has special code to detect magic functions.
164 """
164 """
165
165
166 oname = oname.strip()
166 oname = oname.strip()
167
167
168 # Namespaces to search in:
168 # Namespaces to search in:
169 user_ns = self.shell.user_ns
169 user_ns = self.shell.user_ns
170 internal_ns = self.shell.internal_ns
170 internal_ns = self.shell.internal_ns
171 builtin_ns = __builtin__.__dict__
171 builtin_ns = __builtin__.__dict__
172 alias_ns = self.shell.alias_table
172 alias_ns = self.shell.alias_table
173
173
174 # Put them in a list. The order is important so that we find things in
174 # Put them in a list. The order is important so that we find things in
175 # the same order that Python finds them.
175 # the same order that Python finds them.
176 namespaces = [ ('Interactive',user_ns),
176 namespaces = [ ('Interactive',user_ns),
177 ('IPython internal',internal_ns),
177 ('IPython internal',internal_ns),
178 ('Python builtin',builtin_ns),
178 ('Python builtin',builtin_ns),
179 ('Alias',alias_ns),
179 ('Alias',alias_ns),
180 ]
180 ]
181
181
182 # initialize results to 'null'
182 # initialize results to 'null'
183 found = 0; obj = None; ospace = None; ds = None;
183 found = 0; obj = None; ospace = None; ds = None;
184 ismagic = 0; isalias = 0
184 ismagic = 0; isalias = 0
185
185
186 # Look for the given name by splitting it in parts. If the head is
186 # Look for the given name by splitting it in parts. If the head is
187 # found, then we look for all the remaining parts as members, and only
187 # found, then we look for all the remaining parts as members, and only
188 # declare success if we can find them all.
188 # declare success if we can find them all.
189 oname_parts = oname.split('.')
189 oname_parts = oname.split('.')
190 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
190 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
191 for nsname,ns in namespaces:
191 for nsname,ns in namespaces:
192 try:
192 try:
193 obj = ns[oname_head]
193 obj = ns[oname_head]
194 except KeyError:
194 except KeyError:
195 continue
195 continue
196 else:
196 else:
197 for part in oname_rest:
197 for part in oname_rest:
198 try:
198 try:
199 obj = getattr(obj,part)
199 obj = getattr(obj,part)
200 except:
200 except:
201 # Blanket except b/c some badly implemented objects
201 # Blanket except b/c some badly implemented objects
202 # allow __getattr__ to raise exceptions other than
202 # allow __getattr__ to raise exceptions other than
203 # AttributeError, which then crashes IPython.
203 # AttributeError, which then crashes IPython.
204 break
204 break
205 else:
205 else:
206 # If we finish the for loop (no break), we got all members
206 # If we finish the for loop (no break), we got all members
207 found = 1
207 found = 1
208 ospace = nsname
208 ospace = nsname
209 if ns == alias_ns:
209 if ns == alias_ns:
210 isalias = 1
210 isalias = 1
211 break # namespace loop
211 break # namespace loop
212
212
213 # Try to see if it's magic
213 # Try to see if it's magic
214 if not found:
214 if not found:
215 if oname.startswith(self.shell.ESC_MAGIC):
215 if oname.startswith(self.shell.ESC_MAGIC):
216 oname = oname[1:]
216 oname = oname[1:]
217 obj = getattr(self,'magic_'+oname,None)
217 obj = getattr(self,'magic_'+oname,None)
218 if obj is not None:
218 if obj is not None:
219 found = 1
219 found = 1
220 ospace = 'IPython internal'
220 ospace = 'IPython internal'
221 ismagic = 1
221 ismagic = 1
222
222
223 # Last try: special-case some literals like '', [], {}, etc:
223 # Last try: special-case some literals like '', [], {}, etc:
224 if not found and oname_head in ["''",'""','[]','{}','()']:
224 if not found and oname_head in ["''",'""','[]','{}','()']:
225 obj = eval(oname_head)
225 obj = eval(oname_head)
226 found = 1
226 found = 1
227 ospace = 'Interactive'
227 ospace = 'Interactive'
228
228
229 return {'found':found, 'obj':obj, 'namespace':ospace,
229 return {'found':found, 'obj':obj, 'namespace':ospace,
230 'ismagic':ismagic, 'isalias':isalias}
230 'ismagic':ismagic, 'isalias':isalias}
231
231
232 def arg_err(self,func):
232 def arg_err(self,func):
233 """Print docstring if incorrect arguments were passed"""
233 """Print docstring if incorrect arguments were passed"""
234 print 'Error in arguments:'
234 print 'Error in arguments:'
235 print OInspect.getdoc(func)
235 print OInspect.getdoc(func)
236
236
237 def format_latex(self,strng):
237 def format_latex(self,strng):
238 """Format a string for latex inclusion."""
238 """Format a string for latex inclusion."""
239
239
240 # Characters that need to be escaped for latex:
240 # Characters that need to be escaped for latex:
241 escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE)
241 escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE)
242 # Magic command names as headers:
242 # Magic command names as headers:
243 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
243 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
244 re.MULTILINE)
244 re.MULTILINE)
245 # Magic commands
245 # Magic commands
246 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
246 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
247 re.MULTILINE)
247 re.MULTILINE)
248 # Paragraph continue
248 # Paragraph continue
249 par_re = re.compile(r'\\$',re.MULTILINE)
249 par_re = re.compile(r'\\$',re.MULTILINE)
250
250
251 # The "\n" symbol
251 # The "\n" symbol
252 newline_re = re.compile(r'\\n')
252 newline_re = re.compile(r'\\n')
253
253
254 # Now build the string for output:
254 # Now build the string for output:
255 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
255 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
256 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
256 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
257 strng)
257 strng)
258 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
258 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
259 strng = par_re.sub(r'\\\\',strng)
259 strng = par_re.sub(r'\\\\',strng)
260 strng = escape_re.sub(r'\\\1',strng)
260 strng = escape_re.sub(r'\\\1',strng)
261 strng = newline_re.sub(r'\\textbackslash{}n',strng)
261 strng = newline_re.sub(r'\\textbackslash{}n',strng)
262 return strng
262 return strng
263
263
264 def format_screen(self,strng):
264 def format_screen(self,strng):
265 """Format a string for screen printing.
265 """Format a string for screen printing.
266
266
267 This removes some latex-type format codes."""
267 This removes some latex-type format codes."""
268 # Paragraph continue
268 # Paragraph continue
269 par_re = re.compile(r'\\$',re.MULTILINE)
269 par_re = re.compile(r'\\$',re.MULTILINE)
270 strng = par_re.sub('',strng)
270 strng = par_re.sub('',strng)
271 return strng
271 return strng
272
272
273 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
273 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
274 """Parse options passed to an argument string.
274 """Parse options passed to an argument string.
275
275
276 The interface is similar to that of getopt(), but it returns back a
276 The interface is similar to that of getopt(), but it returns back a
277 Struct with the options as keys and the stripped argument string still
277 Struct with the options as keys and the stripped argument string still
278 as a string.
278 as a string.
279
279
280 arg_str is quoted as a true sys.argv vector by using shlex.split.
280 arg_str is quoted as a true sys.argv vector by using shlex.split.
281 This allows us to easily expand variables, glob files, quote
281 This allows us to easily expand variables, glob files, quote
282 arguments, etc.
282 arguments, etc.
283
283
284 Options:
284 Options:
285 -mode: default 'string'. If given as 'list', the argument string is
285 -mode: default 'string'. If given as 'list', the argument string is
286 returned as a list (split on whitespace) instead of a string.
286 returned as a list (split on whitespace) instead of a string.
287
287
288 -list_all: put all option values in lists. Normally only options
288 -list_all: put all option values in lists. Normally only options
289 appearing more than once are put in a list."""
289 appearing more than once are put in a list."""
290
290
291 # inject default options at the beginning of the input line
291 # inject default options at the beginning of the input line
292 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
292 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
293 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
293 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
294
294
295 mode = kw.get('mode','string')
295 mode = kw.get('mode','string')
296 if mode not in ['string','list']:
296 if mode not in ['string','list']:
297 raise ValueError,'incorrect mode given: %s' % mode
297 raise ValueError,'incorrect mode given: %s' % mode
298 # Get options
298 # Get options
299 list_all = kw.get('list_all',0)
299 list_all = kw.get('list_all',0)
300
300
301 # Check if we have more than one argument to warrant extra processing:
301 # Check if we have more than one argument to warrant extra processing:
302 odict = {} # Dictionary with options
302 odict = {} # Dictionary with options
303 args = arg_str.split()
303 args = arg_str.split()
304 if len(args) >= 1:
304 if len(args) >= 1:
305 # If the list of inputs only has 0 or 1 thing in it, there's no
305 # If the list of inputs only has 0 or 1 thing in it, there's no
306 # need to look for options
306 # need to look for options
307 argv = shlex_split(arg_str)
307 argv = shlex_split(arg_str)
308 # Do regular option processing
308 # Do regular option processing
309 opts,args = getopt(argv,opt_str,*long_opts)
309 opts,args = getopt(argv,opt_str,*long_opts)
310 for o,a in opts:
310 for o,a in opts:
311 if o.startswith('--'):
311 if o.startswith('--'):
312 o = o[2:]
312 o = o[2:]
313 else:
313 else:
314 o = o[1:]
314 o = o[1:]
315 try:
315 try:
316 odict[o].append(a)
316 odict[o].append(a)
317 except AttributeError:
317 except AttributeError:
318 odict[o] = [odict[o],a]
318 odict[o] = [odict[o],a]
319 except KeyError:
319 except KeyError:
320 if list_all:
320 if list_all:
321 odict[o] = [a]
321 odict[o] = [a]
322 else:
322 else:
323 odict[o] = a
323 odict[o] = a
324
324
325 # Prepare opts,args for return
325 # Prepare opts,args for return
326 opts = Struct(odict)
326 opts = Struct(odict)
327 if mode == 'string':
327 if mode == 'string':
328 args = ' '.join(args)
328 args = ' '.join(args)
329
329
330 return opts,args
330 return opts,args
331
331
332 #......................................................................
332 #......................................................................
333 # And now the actual magic functions
333 # And now the actual magic functions
334
334
335 # Functions for IPython shell work (vars,funcs, config, etc)
335 # Functions for IPython shell work (vars,funcs, config, etc)
336 def magic_lsmagic(self, parameter_s = ''):
336 def magic_lsmagic(self, parameter_s = ''):
337 """List currently available magic functions."""
337 """List currently available magic functions."""
338 mesc = self.shell.ESC_MAGIC
338 mesc = self.shell.ESC_MAGIC
339 print 'Available magic functions:\n'+mesc+\
339 print 'Available magic functions:\n'+mesc+\
340 (' '+mesc).join(self.lsmagic())
340 (' '+mesc).join(self.lsmagic())
341 print '\n' + Magic.auto_status[self.shell.rc.automagic]
341 print '\n' + Magic.auto_status[self.shell.rc.automagic]
342 return None
342 return None
343
343
344 def magic_magic(self, parameter_s = ''):
344 def magic_magic(self, parameter_s = ''):
345 """Print information about the magic function system."""
345 """Print information about the magic function system."""
346
346
347 mode = ''
347 mode = ''
348 try:
348 try:
349 if parameter_s.split()[0] == '-latex':
349 if parameter_s.split()[0] == '-latex':
350 mode = 'latex'
350 mode = 'latex'
351 except:
351 except:
352 pass
352 pass
353
353
354 magic_docs = []
354 magic_docs = []
355 for fname in self.lsmagic():
355 for fname in self.lsmagic():
356 mname = 'magic_' + fname
356 mname = 'magic_' + fname
357 for space in (Magic,self,self.__class__):
357 for space in (Magic,self,self.__class__):
358 try:
358 try:
359 fn = space.__dict__[mname]
359 fn = space.__dict__[mname]
360 except KeyError:
360 except KeyError:
361 pass
361 pass
362 else:
362 else:
363 break
363 break
364 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
364 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
365 fname,fn.__doc__))
365 fname,fn.__doc__))
366 magic_docs = ''.join(magic_docs)
366 magic_docs = ''.join(magic_docs)
367
367
368 if mode == 'latex':
368 if mode == 'latex':
369 print self.format_latex(magic_docs)
369 print self.format_latex(magic_docs)
370 return
370 return
371 else:
371 else:
372 magic_docs = self.format_screen(magic_docs)
372 magic_docs = self.format_screen(magic_docs)
373
373
374 outmsg = """
374 outmsg = """
375 IPython's 'magic' functions
375 IPython's 'magic' functions
376 ===========================
376 ===========================
377
377
378 The magic function system provides a series of functions which allow you to
378 The magic function system provides a series of functions which allow you to
379 control the behavior of IPython itself, plus a lot of system-type
379 control the behavior of IPython itself, plus a lot of system-type
380 features. All these functions are prefixed with a % character, but parameters
380 features. All these functions are prefixed with a % character, but parameters
381 are given without parentheses or quotes.
381 are given without parentheses or quotes.
382
382
383 NOTE: If you have 'automagic' enabled (via the command line option or with the
383 NOTE: If you have 'automagic' enabled (via the command line option or with the
384 %automagic function), you don't need to type in the % explicitly. By default,
384 %automagic function), you don't need to type in the % explicitly. By default,
385 IPython ships with automagic on, so you should only rarely need the % escape.
385 IPython ships with automagic on, so you should only rarely need the % escape.
386
386
387 Example: typing '%cd mydir' (without the quotes) changes you working directory
387 Example: typing '%cd mydir' (without the quotes) changes you working directory
388 to 'mydir', if it exists.
388 to 'mydir', if it exists.
389
389
390 You can define your own magic functions to extend the system. See the supplied
390 You can define your own magic functions to extend the system. See the supplied
391 ipythonrc and example-magic.py files for details (in your ipython
391 ipythonrc and example-magic.py files for details (in your ipython
392 configuration directory, typically $HOME/.ipython/).
392 configuration directory, typically $HOME/.ipython/).
393
393
394 You can also define your own aliased names for magic functions. In your
394 You can also define your own aliased names for magic functions. In your
395 ipythonrc file, placing a line like:
395 ipythonrc file, placing a line like:
396
396
397 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
397 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
398
398
399 will define %pf as a new name for %profile.
399 will define %pf as a new name for %profile.
400
400
401 You can also call magics in code using the ipmagic() function, which IPython
401 You can also call magics in code using the ipmagic() function, which IPython
402 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
402 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
403
403
404 For a list of the available magic functions, use %lsmagic. For a description
404 For a list of the available magic functions, use %lsmagic. For a description
405 of any of them, type %magic_name?, e.g. '%cd?'.
405 of any of them, type %magic_name?, e.g. '%cd?'.
406
406
407 Currently the magic system has the following functions:\n"""
407 Currently the magic system has the following functions:\n"""
408
408
409 mesc = self.shell.ESC_MAGIC
409 mesc = self.shell.ESC_MAGIC
410 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
410 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
411 "\n\n%s%s\n\n%s" % (outmsg,
411 "\n\n%s%s\n\n%s" % (outmsg,
412 magic_docs,mesc,mesc,
412 magic_docs,mesc,mesc,
413 (' '+mesc).join(self.lsmagic()),
413 (' '+mesc).join(self.lsmagic()),
414 Magic.auto_status[self.shell.rc.automagic] ) )
414 Magic.auto_status[self.shell.rc.automagic] ) )
415
415
416 page(outmsg,screen_lines=self.shell.rc.screen_length)
416 page(outmsg,screen_lines=self.shell.rc.screen_length)
417
417
418 def magic_automagic(self, parameter_s = ''):
418 def magic_automagic(self, parameter_s = ''):
419 """Make magic functions callable without having to type the initial %.
419 """Make magic functions callable without having to type the initial %.
420
420
421 Toggles on/off (when off, you must call it as %automagic, of
421 Toggles on/off (when off, you must call it as %automagic, of
422 course). Note that magic functions have lowest priority, so if there's
422 course). Note that magic functions have lowest priority, so if there's
423 a variable whose name collides with that of a magic fn, automagic
423 a variable whose name collides with that of a magic fn, automagic
424 won't work for that function (you get the variable instead). However,
424 won't work for that function (you get the variable instead). However,
425 if you delete the variable (del var), the previously shadowed magic
425 if you delete the variable (del var), the previously shadowed magic
426 function becomes visible to automagic again."""
426 function becomes visible to automagic again."""
427
427
428 rc = self.shell.rc
428 rc = self.shell.rc
429 rc.automagic = not rc.automagic
429 rc.automagic = not rc.automagic
430 print '\n' + Magic.auto_status[rc.automagic]
430 print '\n' + Magic.auto_status[rc.automagic]
431
431
432 def magic_autocall(self, parameter_s = ''):
432 def magic_autocall(self, parameter_s = ''):
433 """Make functions callable without having to type parentheses.
433 """Make functions callable without having to type parentheses.
434
434
435 Usage:
435 Usage:
436
436
437 %autocall [mode]
437 %autocall [mode]
438
438
439 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
439 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
440 value is toggled on and off (remembering the previous state)."""
440 value is toggled on and off (remembering the previous state)."""
441
441
442 rc = self.shell.rc
442 rc = self.shell.rc
443
443
444 if parameter_s:
444 if parameter_s:
445 arg = int(parameter_s)
445 arg = int(parameter_s)
446 else:
446 else:
447 arg = 'toggle'
447 arg = 'toggle'
448
448
449 if not arg in (0,1,2,'toggle'):
449 if not arg in (0,1,2,'toggle'):
450 error('Valid modes: (0->Off, 1->Smart, 2->Full')
450 error('Valid modes: (0->Off, 1->Smart, 2->Full')
451 return
451 return
452
452
453 if arg in (0,1,2):
453 if arg in (0,1,2):
454 rc.autocall = arg
454 rc.autocall = arg
455 else: # toggle
455 else: # toggle
456 if rc.autocall:
456 if rc.autocall:
457 self._magic_state.autocall_save = rc.autocall
457 self._magic_state.autocall_save = rc.autocall
458 rc.autocall = 0
458 rc.autocall = 0
459 else:
459 else:
460 try:
460 try:
461 rc.autocall = self._magic_state.autocall_save
461 rc.autocall = self._magic_state.autocall_save
462 except AttributeError:
462 except AttributeError:
463 rc.autocall = self._magic_state.autocall_save = 1
463 rc.autocall = self._magic_state.autocall_save = 1
464
464
465 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
465 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
466
466
467 def magic_autoindent(self, parameter_s = ''):
467 def magic_autoindent(self, parameter_s = ''):
468 """Toggle autoindent on/off (if available)."""
468 """Toggle autoindent on/off (if available)."""
469
469
470 self.shell.set_autoindent()
470 self.shell.set_autoindent()
471 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
471 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
472
472
473 def magic_system_verbose(self, parameter_s = ''):
473 def magic_system_verbose(self, parameter_s = ''):
474 """Toggle verbose printing of system calls on/off."""
474 """Toggle verbose printing of system calls on/off."""
475
475
476 self.shell.rc_set_toggle('system_verbose')
476 self.shell.rc_set_toggle('system_verbose')
477 print "System verbose printing is:",\
477 print "System verbose printing is:",\
478 ['OFF','ON'][self.shell.rc.system_verbose]
478 ['OFF','ON'][self.shell.rc.system_verbose]
479
479
480 def magic_history(self, parameter_s = ''):
480 def magic_history(self, parameter_s = ''):
481 """Print input history (_i<n> variables), with most recent last.
481 """Print input history (_i<n> variables), with most recent last.
482
482
483 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
483 %history -> print at most 40 inputs (some may be multi-line)\\
484 %history [-n] n -> print at most n inputs\\
484 %history n -> print at most n inputs\\
485 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
485 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
486
486
487 Each input's number <n> is shown, and is accessible as the
487 Each input's number <n> is shown, and is accessible as the
488 automatically generated variable _i<n>. Multi-line statements are
488 automatically generated variable _i<n>. Multi-line statements are
489 printed starting at a new line for easy copy/paste.
489 printed starting at a new line for easy copy/paste.
490
491
492 Options:
490
493
491 If option -n is used, input numbers are not printed. This is useful if
494 -n: do NOT print line numbers. This is useful if you want to get a
492 you want to get a printout of many lines which can be directly pasted
495 printout of many lines which can be directly pasted into a text
493 into a text editor.
496 editor.
494
497
495 This feature is only available if numbered prompts are in use."""
498 This feature is only available if numbered prompts are in use.
499
500 -r: print the 'raw' history. IPython filters your input and
501 converts it all into valid Python source before executing it (things
502 like magics or aliases are turned into function calls, for
503 example). With this option, you'll see the unfiltered history
504 instead of the filtered version: '%cd /' will be seen as '%cd /'
505 instead of 'ipmagic("%cd /")'.
506 """
496
507
497 shell = self.shell
508 shell = self.shell
498 if not shell.outputcache.do_full_cache:
509 if not shell.outputcache.do_full_cache:
499 print 'This feature is only available if numbered prompts are in use.'
510 print 'This feature is only available if numbered prompts are in use.'
500 return
511 return
501 opts,args = self.parse_options(parameter_s,'n',mode='list')
512 opts,args = self.parse_options(parameter_s,'nr',mode='list')
513
514 if opts.has_key('r'):
515 input_hist = shell.input_hist_raw
516 else:
517 input_hist = shell.input_hist
502
518
503 input_hist = shell.input_hist
504 default_length = 40
519 default_length = 40
505 if len(args) == 0:
520 if len(args) == 0:
506 final = len(input_hist)
521 final = len(input_hist)
507 init = max(1,final-default_length)
522 init = max(1,final-default_length)
508 elif len(args) == 1:
523 elif len(args) == 1:
509 final = len(input_hist)
524 final = len(input_hist)
510 init = max(1,final-int(args[0]))
525 init = max(1,final-int(args[0]))
511 elif len(args) == 2:
526 elif len(args) == 2:
512 init,final = map(int,args)
527 init,final = map(int,args)
513 else:
528 else:
514 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
529 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
515 print self.magic_hist.__doc__
530 print self.magic_hist.__doc__
516 return
531 return
517 width = len(str(final))
532 width = len(str(final))
518 line_sep = ['','\n']
533 line_sep = ['','\n']
519 print_nums = not opts.has_key('n')
534 print_nums = not opts.has_key('n')
520 for in_num in range(init,final):
535 for in_num in range(init,final):
521 inline = input_hist[in_num]
536 inline = input_hist[in_num]
522 multiline = int(inline.count('\n') > 1)
537 multiline = int(inline.count('\n') > 1)
523 if print_nums:
538 if print_nums:
524 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
539 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
525 print inline,
540 print inline,
526
541
527 def magic_hist(self, parameter_s=''):
542 def magic_hist(self, parameter_s=''):
528 """Alternate name for %history."""
543 """Alternate name for %history."""
529 return self.magic_history(parameter_s)
544 return self.magic_history(parameter_s)
530
545
531 def magic_p(self, parameter_s=''):
546 def magic_p(self, parameter_s=''):
532 """Just a short alias for Python's 'print'."""
547 """Just a short alias for Python's 'print'."""
533 exec 'print ' + parameter_s in self.shell.user_ns
548 exec 'print ' + parameter_s in self.shell.user_ns
534
549
535 def magic_r(self, parameter_s=''):
550 def magic_r(self, parameter_s=''):
536 """Repeat previous input.
551 """Repeat previous input.
537
552
538 If given an argument, repeats the previous command which starts with
553 If given an argument, repeats the previous command which starts with
539 the same string, otherwise it just repeats the previous input.
554 the same string, otherwise it just repeats the previous input.
540
555
541 Shell escaped commands (with ! as first character) are not recognized
556 Shell escaped commands (with ! as first character) are not recognized
542 by this system, only pure python code and magic commands.
557 by this system, only pure python code and magic commands.
543 """
558 """
544
559
545 start = parameter_s.strip()
560 start = parameter_s.strip()
546 esc_magic = self.shell.ESC_MAGIC
561 esc_magic = self.shell.ESC_MAGIC
547 # Identify magic commands even if automagic is on (which means
562 # Identify magic commands even if automagic is on (which means
548 # the in-memory version is different from that typed by the user).
563 # the in-memory version is different from that typed by the user).
549 if self.shell.rc.automagic:
564 if self.shell.rc.automagic:
550 start_magic = esc_magic+start
565 start_magic = esc_magic+start
551 else:
566 else:
552 start_magic = start
567 start_magic = start
553 # Look through the input history in reverse
568 # Look through the input history in reverse
554 for n in range(len(self.shell.input_hist)-2,0,-1):
569 for n in range(len(self.shell.input_hist)-2,0,-1):
555 input = self.shell.input_hist[n]
570 input = self.shell.input_hist[n]
556 # skip plain 'r' lines so we don't recurse to infinity
571 # skip plain 'r' lines so we don't recurse to infinity
557 if input != 'ipmagic("r")\n' and \
572 if input != 'ipmagic("r")\n' and \
558 (input.startswith(start) or input.startswith(start_magic)):
573 (input.startswith(start) or input.startswith(start_magic)):
559 #print 'match',`input` # dbg
574 #print 'match',`input` # dbg
560 print 'Executing:',input,
575 print 'Executing:',input,
561 self.shell.runlines(input)
576 self.shell.runlines(input)
562 return
577 return
563 print 'No previous input matching `%s` found.' % start
578 print 'No previous input matching `%s` found.' % start
564
579
565 def magic_page(self, parameter_s=''):
580 def magic_page(self, parameter_s=''):
566 """Pretty print the object and display it through a pager.
581 """Pretty print the object and display it through a pager.
567
582
568 If no parameter is given, use _ (last output)."""
583 If no parameter is given, use _ (last output)."""
569 # After a function contributed by Olivier Aubert, slightly modified.
584 # After a function contributed by Olivier Aubert, slightly modified.
570
585
571 oname = parameter_s and parameter_s or '_'
586 oname = parameter_s and parameter_s or '_'
572 info = self._ofind(oname)
587 info = self._ofind(oname)
573 if info['found']:
588 if info['found']:
574 page(pformat(info['obj']))
589 page(pformat(info['obj']))
575 else:
590 else:
576 print 'Object `%s` not found' % oname
591 print 'Object `%s` not found' % oname
577
592
578 def magic_profile(self, parameter_s=''):
593 def magic_profile(self, parameter_s=''):
579 """Print your currently active IPyhton profile."""
594 """Print your currently active IPyhton profile."""
580 if self.shell.rc.profile:
595 if self.shell.rc.profile:
581 printpl('Current IPython profile: $self.shell.rc.profile.')
596 printpl('Current IPython profile: $self.shell.rc.profile.')
582 else:
597 else:
583 print 'No profile active.'
598 print 'No profile active.'
584
599
585 def _inspect(self,meth,oname,**kw):
600 def _inspect(self,meth,oname,**kw):
586 """Generic interface to the inspector system.
601 """Generic interface to the inspector system.
587
602
588 This function is meant to be called by pdef, pdoc & friends."""
603 This function is meant to be called by pdef, pdoc & friends."""
589
604
590 oname = oname.strip()
605 oname = oname.strip()
591 info = Struct(self._ofind(oname))
606 info = Struct(self._ofind(oname))
592 if info.found:
607 if info.found:
593 pmethod = getattr(self.shell.inspector,meth)
608 pmethod = getattr(self.shell.inspector,meth)
594 formatter = info.ismagic and self.format_screen or None
609 formatter = info.ismagic and self.format_screen or None
595 if meth == 'pdoc':
610 if meth == 'pdoc':
596 pmethod(info.obj,oname,formatter)
611 pmethod(info.obj,oname,formatter)
597 elif meth == 'pinfo':
612 elif meth == 'pinfo':
598 pmethod(info.obj,oname,formatter,info,**kw)
613 pmethod(info.obj,oname,formatter,info,**kw)
599 else:
614 else:
600 pmethod(info.obj,oname)
615 pmethod(info.obj,oname)
601 else:
616 else:
602 print 'Object `%s` not found.' % oname
617 print 'Object `%s` not found.' % oname
603 return 'not found' # so callers can take other action
618 return 'not found' # so callers can take other action
604
619
605 def magic_pdef(self, parameter_s=''):
620 def magic_pdef(self, parameter_s=''):
606 """Print the definition header for any callable object.
621 """Print the definition header for any callable object.
607
622
608 If the object is a class, print the constructor information."""
623 If the object is a class, print the constructor information."""
609 self._inspect('pdef',parameter_s)
624 self._inspect('pdef',parameter_s)
610
625
611 def magic_pdoc(self, parameter_s=''):
626 def magic_pdoc(self, parameter_s=''):
612 """Print the docstring for an object.
627 """Print the docstring for an object.
613
628
614 If the given object is a class, it will print both the class and the
629 If the given object is a class, it will print both the class and the
615 constructor docstrings."""
630 constructor docstrings."""
616 self._inspect('pdoc',parameter_s)
631 self._inspect('pdoc',parameter_s)
617
632
618 def magic_psource(self, parameter_s=''):
633 def magic_psource(self, parameter_s=''):
619 """Print (or run through pager) the source code for an object."""
634 """Print (or run through pager) the source code for an object."""
620 self._inspect('psource',parameter_s)
635 self._inspect('psource',parameter_s)
621
636
622 def magic_pfile(self, parameter_s=''):
637 def magic_pfile(self, parameter_s=''):
623 """Print (or run through pager) the file where an object is defined.
638 """Print (or run through pager) the file where an object is defined.
624
639
625 The file opens at the line where the object definition begins. IPython
640 The file opens at the line where the object definition begins. IPython
626 will honor the environment variable PAGER if set, and otherwise will
641 will honor the environment variable PAGER if set, and otherwise will
627 do its best to print the file in a convenient form.
642 do its best to print the file in a convenient form.
628
643
629 If the given argument is not an object currently defined, IPython will
644 If the given argument is not an object currently defined, IPython will
630 try to interpret it as a filename (automatically adding a .py extension
645 try to interpret it as a filename (automatically adding a .py extension
631 if needed). You can thus use %pfile as a syntax highlighting code
646 if needed). You can thus use %pfile as a syntax highlighting code
632 viewer."""
647 viewer."""
633
648
634 # first interpret argument as an object name
649 # first interpret argument as an object name
635 out = self._inspect('pfile',parameter_s)
650 out = self._inspect('pfile',parameter_s)
636 # if not, try the input as a filename
651 # if not, try the input as a filename
637 if out == 'not found':
652 if out == 'not found':
638 try:
653 try:
639 filename = get_py_filename(parameter_s)
654 filename = get_py_filename(parameter_s)
640 except IOError,msg:
655 except IOError,msg:
641 print msg
656 print msg
642 return
657 return
643 page(self.shell.inspector.format(file(filename).read()))
658 page(self.shell.inspector.format(file(filename).read()))
644
659
645 def magic_pinfo(self, parameter_s=''):
660 def magic_pinfo(self, parameter_s=''):
646 """Provide detailed information about an object.
661 """Provide detailed information about an object.
647
662
648 '%pinfo object' is just a synonym for object? or ?object."""
663 '%pinfo object' is just a synonym for object? or ?object."""
649
664
650 #print 'pinfo par: <%s>' % parameter_s # dbg
665 #print 'pinfo par: <%s>' % parameter_s # dbg
651
666
652 # detail_level: 0 -> obj? , 1 -> obj??
667 # detail_level: 0 -> obj? , 1 -> obj??
653 detail_level = 0
668 detail_level = 0
654 # We need to detect if we got called as 'pinfo pinfo foo', which can
669 # We need to detect if we got called as 'pinfo pinfo foo', which can
655 # happen if the user types 'pinfo foo?' at the cmd line.
670 # happen if the user types 'pinfo foo?' at the cmd line.
656 pinfo,qmark1,oname,qmark2 = \
671 pinfo,qmark1,oname,qmark2 = \
657 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
672 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
658 if pinfo or qmark1 or qmark2:
673 if pinfo or qmark1 or qmark2:
659 detail_level = 1
674 detail_level = 1
660 if "*" in oname:
675 if "*" in oname:
661 self.magic_psearch(oname)
676 self.magic_psearch(oname)
662 else:
677 else:
663 self._inspect('pinfo',oname,detail_level=detail_level)
678 self._inspect('pinfo',oname,detail_level=detail_level)
664
679
665 def magic_psearch(self, parameter_s=''):
680 def magic_psearch(self, parameter_s=''):
666 """Search for object in namespaces by wildcard.
681 """Search for object in namespaces by wildcard.
667
682
668 %psearch [options] PATTERN [OBJECT TYPE]
683 %psearch [options] PATTERN [OBJECT TYPE]
669
684
670 Note: ? can be used as a synonym for %psearch, at the beginning or at
685 Note: ? can be used as a synonym for %psearch, at the beginning or at
671 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
686 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
672 rest of the command line must be unchanged (options come first), so
687 rest of the command line must be unchanged (options come first), so
673 for example the following forms are equivalent
688 for example the following forms are equivalent
674
689
675 %psearch -i a* function
690 %psearch -i a* function
676 -i a* function?
691 -i a* function?
677 ?-i a* function
692 ?-i a* function
678
693
679 Arguments:
694 Arguments:
680
695
681 PATTERN
696 PATTERN
682
697
683 where PATTERN is a string containing * as a wildcard similar to its
698 where PATTERN is a string containing * as a wildcard similar to its
684 use in a shell. The pattern is matched in all namespaces on the
699 use in a shell. The pattern is matched in all namespaces on the
685 search path. By default objects starting with a single _ are not
700 search path. By default objects starting with a single _ are not
686 matched, many IPython generated objects have a single
701 matched, many IPython generated objects have a single
687 underscore. The default is case insensitive matching. Matching is
702 underscore. The default is case insensitive matching. Matching is
688 also done on the attributes of objects and not only on the objects
703 also done on the attributes of objects and not only on the objects
689 in a module.
704 in a module.
690
705
691 [OBJECT TYPE]
706 [OBJECT TYPE]
692
707
693 Is the name of a python type from the types module. The name is
708 Is the name of a python type from the types module. The name is
694 given in lowercase without the ending type, ex. StringType is
709 given in lowercase without the ending type, ex. StringType is
695 written string. By adding a type here only objects matching the
710 written string. By adding a type here only objects matching the
696 given type are matched. Using all here makes the pattern match all
711 given type are matched. Using all here makes the pattern match all
697 types (this is the default).
712 types (this is the default).
698
713
699 Options:
714 Options:
700
715
701 -a: makes the pattern match even objects whose names start with a
716 -a: makes the pattern match even objects whose names start with a
702 single underscore. These names are normally ommitted from the
717 single underscore. These names are normally ommitted from the
703 search.
718 search.
704
719
705 -i/-c: make the pattern case insensitive/sensitive. If neither of
720 -i/-c: make the pattern case insensitive/sensitive. If neither of
706 these options is given, the default is read from your ipythonrc
721 these options is given, the default is read from your ipythonrc
707 file. The option name which sets this value is
722 file. The option name which sets this value is
708 'wildcards_case_sensitive'. If this option is not specified in your
723 'wildcards_case_sensitive'. If this option is not specified in your
709 ipythonrc file, IPython's internal default is to do a case sensitive
724 ipythonrc file, IPython's internal default is to do a case sensitive
710 search.
725 search.
711
726
712 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
727 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
713 specifiy can be searched in any of the following namespaces:
728 specifiy can be searched in any of the following namespaces:
714 'builtin', 'user', 'user_global','internal', 'alias', where
729 'builtin', 'user', 'user_global','internal', 'alias', where
715 'builtin' and 'user' are the search defaults. Note that you should
730 'builtin' and 'user' are the search defaults. Note that you should
716 not use quotes when specifying namespaces.
731 not use quotes when specifying namespaces.
717
732
718 'Builtin' contains the python module builtin, 'user' contains all
733 'Builtin' contains the python module builtin, 'user' contains all
719 user data, 'alias' only contain the shell aliases and no python
734 user data, 'alias' only contain the shell aliases and no python
720 objects, 'internal' contains objects used by IPython. The
735 objects, 'internal' contains objects used by IPython. The
721 'user_global' namespace is only used by embedded IPython instances,
736 'user_global' namespace is only used by embedded IPython instances,
722 and it contains module-level globals. You can add namespaces to the
737 and it contains module-level globals. You can add namespaces to the
723 search with -s or exclude them with -e (these options can be given
738 search with -s or exclude them with -e (these options can be given
724 more than once).
739 more than once).
725
740
726 Examples:
741 Examples:
727
742
728 %psearch a* -> objects beginning with an a
743 %psearch a* -> objects beginning with an a
729 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
744 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
730 %psearch a* function -> all functions beginning with an a
745 %psearch a* function -> all functions beginning with an a
731 %psearch re.e* -> objects beginning with an e in module re
746 %psearch re.e* -> objects beginning with an e in module re
732 %psearch r*.e* -> objects that start with e in modules starting in r
747 %psearch r*.e* -> objects that start with e in modules starting in r
733 %psearch r*.* string -> all strings in modules beginning with r
748 %psearch r*.* string -> all strings in modules beginning with r
734
749
735 Case sensitve search:
750 Case sensitve search:
736
751
737 %psearch -c a* list all object beginning with lower case a
752 %psearch -c a* list all object beginning with lower case a
738
753
739 Show objects beginning with a single _:
754 Show objects beginning with a single _:
740
755
741 %psearch -a _* list objects beginning with a single underscore"""
756 %psearch -a _* list objects beginning with a single underscore"""
742
757
743 # default namespaces to be searched
758 # default namespaces to be searched
744 def_search = ['user','builtin']
759 def_search = ['user','builtin']
745
760
746 # Process options/args
761 # Process options/args
747 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
762 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
748 opt = opts.get
763 opt = opts.get
749 shell = self.shell
764 shell = self.shell
750 psearch = shell.inspector.psearch
765 psearch = shell.inspector.psearch
751
766
752 # select case options
767 # select case options
753 if opts.has_key('i'):
768 if opts.has_key('i'):
754 ignore_case = True
769 ignore_case = True
755 elif opts.has_key('c'):
770 elif opts.has_key('c'):
756 ignore_case = False
771 ignore_case = False
757 else:
772 else:
758 ignore_case = not shell.rc.wildcards_case_sensitive
773 ignore_case = not shell.rc.wildcards_case_sensitive
759
774
760 # Build list of namespaces to search from user options
775 # Build list of namespaces to search from user options
761 def_search.extend(opt('s',[]))
776 def_search.extend(opt('s',[]))
762 ns_exclude = ns_exclude=opt('e',[])
777 ns_exclude = ns_exclude=opt('e',[])
763 ns_search = [nm for nm in def_search if nm not in ns_exclude]
778 ns_search = [nm for nm in def_search if nm not in ns_exclude]
764
779
765 # Call the actual search
780 # Call the actual search
766 try:
781 try:
767 psearch(args,shell.ns_table,ns_search,
782 psearch(args,shell.ns_table,ns_search,
768 show_all=opt('a'),ignore_case=ignore_case)
783 show_all=opt('a'),ignore_case=ignore_case)
769 except:
784 except:
770 shell.showtraceback()
785 shell.showtraceback()
771
786
772 def magic_who_ls(self, parameter_s=''):
787 def magic_who_ls(self, parameter_s=''):
773 """Return a sorted list of all interactive variables.
788 """Return a sorted list of all interactive variables.
774
789
775 If arguments are given, only variables of types matching these
790 If arguments are given, only variables of types matching these
776 arguments are returned."""
791 arguments are returned."""
777
792
778 user_ns = self.shell.user_ns
793 user_ns = self.shell.user_ns
779 internal_ns = self.shell.internal_ns
794 internal_ns = self.shell.internal_ns
780 user_config_ns = self.shell.user_config_ns
795 user_config_ns = self.shell.user_config_ns
781 out = []
796 out = []
782 typelist = parameter_s.split()
797 typelist = parameter_s.split()
783
798
784 for i in user_ns:
799 for i in user_ns:
785 if not (i.startswith('_') or i.startswith('_i')) \
800 if not (i.startswith('_') or i.startswith('_i')) \
786 and not (i in internal_ns or i in user_config_ns):
801 and not (i in internal_ns or i in user_config_ns):
787 if typelist:
802 if typelist:
788 if type(user_ns[i]).__name__ in typelist:
803 if type(user_ns[i]).__name__ in typelist:
789 out.append(i)
804 out.append(i)
790 else:
805 else:
791 out.append(i)
806 out.append(i)
792 out.sort()
807 out.sort()
793 return out
808 return out
794
809
795 def magic_who(self, parameter_s=''):
810 def magic_who(self, parameter_s=''):
796 """Print all interactive variables, with some minimal formatting.
811 """Print all interactive variables, with some minimal formatting.
797
812
798 If any arguments are given, only variables whose type matches one of
813 If any arguments are given, only variables whose type matches one of
799 these are printed. For example:
814 these are printed. For example:
800
815
801 %who function str
816 %who function str
802
817
803 will only list functions and strings, excluding all other types of
818 will only list functions and strings, excluding all other types of
804 variables. To find the proper type names, simply use type(var) at a
819 variables. To find the proper type names, simply use type(var) at a
805 command line to see how python prints type names. For example:
820 command line to see how python prints type names. For example:
806
821
807 In [1]: type('hello')\\
822 In [1]: type('hello')\\
808 Out[1]: <type 'str'>
823 Out[1]: <type 'str'>
809
824
810 indicates that the type name for strings is 'str'.
825 indicates that the type name for strings is 'str'.
811
826
812 %who always excludes executed names loaded through your configuration
827 %who always excludes executed names loaded through your configuration
813 file and things which are internal to IPython.
828 file and things which are internal to IPython.
814
829
815 This is deliberate, as typically you may load many modules and the
830 This is deliberate, as typically you may load many modules and the
816 purpose of %who is to show you only what you've manually defined."""
831 purpose of %who is to show you only what you've manually defined."""
817
832
818 varlist = self.magic_who_ls(parameter_s)
833 varlist = self.magic_who_ls(parameter_s)
819 if not varlist:
834 if not varlist:
820 print 'Interactive namespace is empty.'
835 print 'Interactive namespace is empty.'
821 return
836 return
822
837
823 # if we have variables, move on...
838 # if we have variables, move on...
824
839
825 # stupid flushing problem: when prompts have no separators, stdout is
840 # stupid flushing problem: when prompts have no separators, stdout is
826 # getting lost. I'm starting to think this is a python bug. I'm having
841 # getting lost. I'm starting to think this is a python bug. I'm having
827 # to force a flush with a print because even a sys.stdout.flush
842 # to force a flush with a print because even a sys.stdout.flush
828 # doesn't seem to do anything!
843 # doesn't seem to do anything!
829
844
830 count = 0
845 count = 0
831 for i in varlist:
846 for i in varlist:
832 print i+'\t',
847 print i+'\t',
833 count += 1
848 count += 1
834 if count > 8:
849 if count > 8:
835 count = 0
850 count = 0
836 print
851 print
837 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
852 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
838
853
839 print # well, this does force a flush at the expense of an extra \n
854 print # well, this does force a flush at the expense of an extra \n
840
855
841 def magic_whos(self, parameter_s=''):
856 def magic_whos(self, parameter_s=''):
842 """Like %who, but gives some extra information about each variable.
857 """Like %who, but gives some extra information about each variable.
843
858
844 The same type filtering of %who can be applied here.
859 The same type filtering of %who can be applied here.
845
860
846 For all variables, the type is printed. Additionally it prints:
861 For all variables, the type is printed. Additionally it prints:
847
862
848 - For {},[],(): their length.
863 - For {},[],(): their length.
849
864
850 - For Numeric arrays, a summary with shape, number of elements,
865 - For Numeric arrays, a summary with shape, number of elements,
851 typecode and size in memory.
866 typecode and size in memory.
852
867
853 - Everything else: a string representation, snipping their middle if
868 - Everything else: a string representation, snipping their middle if
854 too long."""
869 too long."""
855
870
856 varnames = self.magic_who_ls(parameter_s)
871 varnames = self.magic_who_ls(parameter_s)
857 if not varnames:
872 if not varnames:
858 print 'Interactive namespace is empty.'
873 print 'Interactive namespace is empty.'
859 return
874 return
860
875
861 # if we have variables, move on...
876 # if we have variables, move on...
862
877
863 # for these types, show len() instead of data:
878 # for these types, show len() instead of data:
864 seq_types = [types.DictType,types.ListType,types.TupleType]
879 seq_types = [types.DictType,types.ListType,types.TupleType]
865
880
866 # for Numeric arrays, display summary info
881 # for Numeric arrays, display summary info
867 try:
882 try:
868 import Numeric
883 import Numeric
869 except ImportError:
884 except ImportError:
870 array_type = None
885 array_type = None
871 else:
886 else:
872 array_type = Numeric.ArrayType.__name__
887 array_type = Numeric.ArrayType.__name__
873
888
874 # Find all variable names and types so we can figure out column sizes
889 # Find all variable names and types so we can figure out column sizes
875 get_vars = lambda i: self.shell.user_ns[i]
890 get_vars = lambda i: self.shell.user_ns[i]
876 type_name = lambda v: type(v).__name__
891 type_name = lambda v: type(v).__name__
877 varlist = map(get_vars,varnames)
892 varlist = map(get_vars,varnames)
878
893
879 typelist = []
894 typelist = []
880 for vv in varlist:
895 for vv in varlist:
881 tt = type_name(vv)
896 tt = type_name(vv)
882 if tt=='instance':
897 if tt=='instance':
883 typelist.append(str(vv.__class__))
898 typelist.append(str(vv.__class__))
884 else:
899 else:
885 typelist.append(tt)
900 typelist.append(tt)
886
901
887 # column labels and # of spaces as separator
902 # column labels and # of spaces as separator
888 varlabel = 'Variable'
903 varlabel = 'Variable'
889 typelabel = 'Type'
904 typelabel = 'Type'
890 datalabel = 'Data/Info'
905 datalabel = 'Data/Info'
891 colsep = 3
906 colsep = 3
892 # variable format strings
907 # variable format strings
893 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
908 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
894 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
909 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
895 aformat = "%s: %s elems, type `%s`, %s bytes"
910 aformat = "%s: %s elems, type `%s`, %s bytes"
896 # find the size of the columns to format the output nicely
911 # find the size of the columns to format the output nicely
897 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
912 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
898 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
913 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
899 # table header
914 # table header
900 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
915 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
901 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
916 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
902 # and the table itself
917 # and the table itself
903 kb = 1024
918 kb = 1024
904 Mb = 1048576 # kb**2
919 Mb = 1048576 # kb**2
905 for vname,var,vtype in zip(varnames,varlist,typelist):
920 for vname,var,vtype in zip(varnames,varlist,typelist):
906 print itpl(vformat),
921 print itpl(vformat),
907 if vtype in seq_types:
922 if vtype in seq_types:
908 print len(var)
923 print len(var)
909 elif vtype==array_type:
924 elif vtype==array_type:
910 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
925 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
911 vsize = Numeric.size(var)
926 vsize = Numeric.size(var)
912 vbytes = vsize*var.itemsize()
927 vbytes = vsize*var.itemsize()
913 if vbytes < 100000:
928 if vbytes < 100000:
914 print aformat % (vshape,vsize,var.typecode(),vbytes)
929 print aformat % (vshape,vsize,var.typecode(),vbytes)
915 else:
930 else:
916 print aformat % (vshape,vsize,var.typecode(),vbytes),
931 print aformat % (vshape,vsize,var.typecode(),vbytes),
917 if vbytes < Mb:
932 if vbytes < Mb:
918 print '(%s kb)' % (vbytes/kb,)
933 print '(%s kb)' % (vbytes/kb,)
919 else:
934 else:
920 print '(%s Mb)' % (vbytes/Mb,)
935 print '(%s Mb)' % (vbytes/Mb,)
921 else:
936 else:
922 vstr = str(var).replace('\n','\\n')
937 vstr = str(var).replace('\n','\\n')
923 if len(vstr) < 50:
938 if len(vstr) < 50:
924 print vstr
939 print vstr
925 else:
940 else:
926 printpl(vfmt_short)
941 printpl(vfmt_short)
927
942
928 def magic_reset(self, parameter_s=''):
943 def magic_reset(self, parameter_s=''):
929 """Resets the namespace by removing all names defined by the user.
944 """Resets the namespace by removing all names defined by the user.
930
945
931 Input/Output history are left around in case you need them."""
946 Input/Output history are left around in case you need them."""
932
947
933 ans = raw_input(
948 ans = raw_input(
934 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
949 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
935 if not ans.lower() == 'y':
950 if not ans.lower() == 'y':
936 print 'Nothing done.'
951 print 'Nothing done.'
937 return
952 return
938 user_ns = self.shell.user_ns
953 user_ns = self.shell.user_ns
939 for i in self.magic_who_ls():
954 for i in self.magic_who_ls():
940 del(user_ns[i])
955 del(user_ns[i])
941
956
942 def magic_config(self,parameter_s=''):
957 def magic_config(self,parameter_s=''):
943 """Show IPython's internal configuration."""
958 """Show IPython's internal configuration."""
944
959
945 page('Current configuration structure:\n'+
960 page('Current configuration structure:\n'+
946 pformat(self.shell.rc.dict()))
961 pformat(self.shell.rc.dict()))
947
962
948 def magic_logstart(self,parameter_s=''):
963 def magic_logstart(self,parameter_s=''):
949 """Start logging anywhere in a session.
964 """Start logging anywhere in a session.
950
965
951 %logstart [-o|-t] [log_name [log_mode]]
966 %logstart [-o|-t] [log_name [log_mode]]
952
967
953 If no name is given, it defaults to a file named 'ipython_log.py' in your
968 If no name is given, it defaults to a file named 'ipython_log.py' in your
954 current directory, in 'rotate' mode (see below).
969 current directory, in 'rotate' mode (see below).
955
970
956 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
971 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
957 history up to that point and then continues logging.
972 history up to that point and then continues logging.
958
973
959 %logstart takes a second optional parameter: logging mode. This can be one
974 %logstart takes a second optional parameter: logging mode. This can be one
960 of (note that the modes are given unquoted):\\
975 of (note that the modes are given unquoted):\\
961 append: well, that says it.\\
976 append: well, that says it.\\
962 backup: rename (if exists) to name~ and start name.\\
977 backup: rename (if exists) to name~ and start name.\\
963 global: single logfile in your home dir, appended to.\\
978 global: single logfile in your home dir, appended to.\\
964 over : overwrite existing log.\\
979 over : overwrite existing log.\\
965 rotate: create rotating logs name.1~, name.2~, etc.
980 rotate: create rotating logs name.1~, name.2~, etc.
966
981
967 Options:
982 Options:
968
983
969 -o: log also IPython's output. In this mode, all commands which
984 -o: log also IPython's output. In this mode, all commands which
970 generate an Out[NN] prompt are recorded to the logfile, right after
985 generate an Out[NN] prompt are recorded to the logfile, right after
971 their corresponding input line. The output lines are always
986 their corresponding input line. The output lines are always
972 prepended with a '#[Out]# ' marker, so that the log remains valid
987 prepended with a '#[Out]# ' marker, so that the log remains valid
973 Python code.
988 Python code.
974
989
975 Since this marker is always the same, filtering only the output from
990 Since this marker is always the same, filtering only the output from
976 a log is very easy, using for example a simple awk call:
991 a log is very easy, using for example a simple awk call:
977
992
978 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
993 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
979
994
980 -t: put timestamps before each input line logged (these are put in
995 -t: put timestamps before each input line logged (these are put in
981 comments)."""
996 comments)."""
982
997
983 opts,par = self.parse_options(parameter_s,'ot')
998 opts,par = self.parse_options(parameter_s,'ot')
984 log_output = 'o' in opts
999 log_output = 'o' in opts
985 timestamp = 't' in opts
1000 timestamp = 't' in opts
986
1001
987 rc = self.shell.rc
1002 rc = self.shell.rc
988 logger = self.shell.logger
1003 logger = self.shell.logger
989
1004
990 # if no args are given, the defaults set in the logger constructor by
1005 # if no args are given, the defaults set in the logger constructor by
991 # ipytohn remain valid
1006 # ipytohn remain valid
992 if par:
1007 if par:
993 try:
1008 try:
994 logfname,logmode = par.split()
1009 logfname,logmode = par.split()
995 except:
1010 except:
996 logfname = par
1011 logfname = par
997 logmode = 'backup'
1012 logmode = 'backup'
998 else:
1013 else:
999 logfname = logger.logfname
1014 logfname = logger.logfname
1000 logmode = logger.logmode
1015 logmode = logger.logmode
1001 # put logfname into rc struct as if it had been called on the command
1016 # put logfname into rc struct as if it had been called on the command
1002 # line, so it ends up saved in the log header Save it in case we need
1017 # line, so it ends up saved in the log header Save it in case we need
1003 # to restore it...
1018 # to restore it...
1004 old_logfile = rc.opts.get('logfile','')
1019 old_logfile = rc.opts.get('logfile','')
1005 if logfname:
1020 if logfname:
1006 logfname = os.path.expanduser(logfname)
1021 logfname = os.path.expanduser(logfname)
1007 rc.opts.logfile = logfname
1022 rc.opts.logfile = logfname
1008 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1023 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1009 try:
1024 try:
1010 started = logger.logstart(logfname,loghead,logmode,
1025 started = logger.logstart(logfname,loghead,logmode,
1011 log_output,timestamp)
1026 log_output,timestamp)
1012 except:
1027 except:
1013 rc.opts.logfile = old_logfile
1028 rc.opts.logfile = old_logfile
1014 warn("Couldn't start log: %s" % sys.exc_info()[1])
1029 warn("Couldn't start log: %s" % sys.exc_info()[1])
1015 else:
1030 else:
1016 # log input history up to this point, optionally interleaving
1031 # log input history up to this point, optionally interleaving
1017 # output if requested
1032 # output if requested
1018
1033
1019 if timestamp:
1034 if timestamp:
1020 # disable timestamping for the previous history, since we've
1035 # disable timestamping for the previous history, since we've
1021 # lost those already (no time machine here).
1036 # lost those already (no time machine here).
1022 logger.timestamp = False
1037 logger.timestamp = False
1023 if log_output:
1038 if log_output:
1024 log_write = logger.log_write
1039 log_write = logger.log_write
1025 input_hist = self.shell.input_hist
1040 input_hist = self.shell.input_hist
1026 output_hist = self.shell.output_hist
1041 output_hist = self.shell.output_hist
1027 for n in range(1,len(input_hist)-1):
1042 for n in range(1,len(input_hist)-1):
1028 log_write(input_hist[n].rstrip())
1043 log_write(input_hist[n].rstrip())
1029 if n in output_hist:
1044 if n in output_hist:
1030 log_write(repr(output_hist[n]),'output')
1045 log_write(repr(output_hist[n]),'output')
1031 else:
1046 else:
1032 logger.log_write(self.shell.input_hist[1:])
1047 logger.log_write(self.shell.input_hist[1:])
1033 if timestamp:
1048 if timestamp:
1034 # re-enable timestamping
1049 # re-enable timestamping
1035 logger.timestamp = True
1050 logger.timestamp = True
1036
1051
1037 print ('Activating auto-logging. '
1052 print ('Activating auto-logging. '
1038 'Current session state plus future input saved.')
1053 'Current session state plus future input saved.')
1039 logger.logstate()
1054 logger.logstate()
1040
1055
1041 def magic_logoff(self,parameter_s=''):
1056 def magic_logoff(self,parameter_s=''):
1042 """Temporarily stop logging.
1057 """Temporarily stop logging.
1043
1058
1044 You must have previously started logging."""
1059 You must have previously started logging."""
1045 self.shell.logger.switch_log(0)
1060 self.shell.logger.switch_log(0)
1046
1061
1047 def magic_logon(self,parameter_s=''):
1062 def magic_logon(self,parameter_s=''):
1048 """Restart logging.
1063 """Restart logging.
1049
1064
1050 This function is for restarting logging which you've temporarily
1065 This function is for restarting logging which you've temporarily
1051 stopped with %logoff. For starting logging for the first time, you
1066 stopped with %logoff. For starting logging for the first time, you
1052 must use the %logstart function, which allows you to specify an
1067 must use the %logstart function, which allows you to specify an
1053 optional log filename."""
1068 optional log filename."""
1054
1069
1055 self.shell.logger.switch_log(1)
1070 self.shell.logger.switch_log(1)
1056
1071
1057 def magic_logstate(self,parameter_s=''):
1072 def magic_logstate(self,parameter_s=''):
1058 """Print the status of the logging system."""
1073 """Print the status of the logging system."""
1059
1074
1060 self.shell.logger.logstate()
1075 self.shell.logger.logstate()
1061
1076
1062 def magic_pdb(self, parameter_s=''):
1077 def magic_pdb(self, parameter_s=''):
1063 """Control the calling of the pdb interactive debugger.
1078 """Control the calling of the pdb interactive debugger.
1064
1079
1065 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1080 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1066 argument it works as a toggle.
1081 argument it works as a toggle.
1067
1082
1068 When an exception is triggered, IPython can optionally call the
1083 When an exception is triggered, IPython can optionally call the
1069 interactive pdb debugger after the traceback printout. %pdb toggles
1084 interactive pdb debugger after the traceback printout. %pdb toggles
1070 this feature on and off."""
1085 this feature on and off."""
1071
1086
1072 par = parameter_s.strip().lower()
1087 par = parameter_s.strip().lower()
1073
1088
1074 if par:
1089 if par:
1075 try:
1090 try:
1076 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1091 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1077 except KeyError:
1092 except KeyError:
1078 print ('Incorrect argument. Use on/1, off/0, '
1093 print ('Incorrect argument. Use on/1, off/0, '
1079 'or nothing for a toggle.')
1094 'or nothing for a toggle.')
1080 return
1095 return
1081 else:
1096 else:
1082 # toggle
1097 # toggle
1083 new_pdb = not self.shell.InteractiveTB.call_pdb
1098 new_pdb = not self.shell.InteractiveTB.call_pdb
1084
1099
1085 # set on the shell
1100 # set on the shell
1086 self.shell.call_pdb = new_pdb
1101 self.shell.call_pdb = new_pdb
1087 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1102 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1088
1103
1089 def magic_prun(self, parameter_s ='',user_mode=1,
1104 def magic_prun(self, parameter_s ='',user_mode=1,
1090 opts=None,arg_lst=None,prog_ns=None):
1105 opts=None,arg_lst=None,prog_ns=None):
1091
1106
1092 """Run a statement through the python code profiler.
1107 """Run a statement through the python code profiler.
1093
1108
1094 Usage:\\
1109 Usage:\\
1095 %prun [options] statement
1110 %prun [options] statement
1096
1111
1097 The given statement (which doesn't require quote marks) is run via the
1112 The given statement (which doesn't require quote marks) is run via the
1098 python profiler in a manner similar to the profile.run() function.
1113 python profiler in a manner similar to the profile.run() function.
1099 Namespaces are internally managed to work correctly; profile.run
1114 Namespaces are internally managed to work correctly; profile.run
1100 cannot be used in IPython because it makes certain assumptions about
1115 cannot be used in IPython because it makes certain assumptions about
1101 namespaces which do not hold under IPython.
1116 namespaces which do not hold under IPython.
1102
1117
1103 Options:
1118 Options:
1104
1119
1105 -l <limit>: you can place restrictions on what or how much of the
1120 -l <limit>: you can place restrictions on what or how much of the
1106 profile gets printed. The limit value can be:
1121 profile gets printed. The limit value can be:
1107
1122
1108 * A string: only information for function names containing this string
1123 * A string: only information for function names containing this string
1109 is printed.
1124 is printed.
1110
1125
1111 * An integer: only these many lines are printed.
1126 * An integer: only these many lines are printed.
1112
1127
1113 * A float (between 0 and 1): this fraction of the report is printed
1128 * A float (between 0 and 1): this fraction of the report is printed
1114 (for example, use a limit of 0.4 to see the topmost 40% only).
1129 (for example, use a limit of 0.4 to see the topmost 40% only).
1115
1130
1116 You can combine several limits with repeated use of the option. For
1131 You can combine several limits with repeated use of the option. For
1117 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1132 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1118 information about class constructors.
1133 information about class constructors.
1119
1134
1120 -r: return the pstats.Stats object generated by the profiling. This
1135 -r: return the pstats.Stats object generated by the profiling. This
1121 object has all the information about the profile in it, and you can
1136 object has all the information about the profile in it, and you can
1122 later use it for further analysis or in other functions.
1137 later use it for further analysis or in other functions.
1123
1138
1124 Since magic functions have a particular form of calling which prevents
1139 Since magic functions have a particular form of calling which prevents
1125 you from writing something like:\\
1140 you from writing something like:\\
1126 In [1]: p = %prun -r print 4 # invalid!\\
1141 In [1]: p = %prun -r print 4 # invalid!\\
1127 you must instead use IPython's automatic variables to assign this:\\
1142 you must instead use IPython's automatic variables to assign this:\\
1128 In [1]: %prun -r print 4 \\
1143 In [1]: %prun -r print 4 \\
1129 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1144 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1130 In [2]: stats = _
1145 In [2]: stats = _
1131
1146
1132 If you really need to assign this value via an explicit function call,
1147 If you really need to assign this value via an explicit function call,
1133 you can always tap directly into the true name of the magic function
1148 you can always tap directly into the true name of the magic function
1134 by using the ipmagic function (which IPython automatically adds to the
1149 by using the ipmagic function (which IPython automatically adds to the
1135 builtins):\\
1150 builtins):\\
1136 In [3]: stats = ipmagic('prun','-r print 4')
1151 In [3]: stats = ipmagic('prun','-r print 4')
1137
1152
1138 You can type ipmagic? for more details on ipmagic.
1153 You can type ipmagic? for more details on ipmagic.
1139
1154
1140 -s <key>: sort profile by given key. You can provide more than one key
1155 -s <key>: sort profile by given key. You can provide more than one key
1141 by using the option several times: '-s key1 -s key2 -s key3...'. The
1156 by using the option several times: '-s key1 -s key2 -s key3...'. The
1142 default sorting key is 'time'.
1157 default sorting key is 'time'.
1143
1158
1144 The following is copied verbatim from the profile documentation
1159 The following is copied verbatim from the profile documentation
1145 referenced below:
1160 referenced below:
1146
1161
1147 When more than one key is provided, additional keys are used as
1162 When more than one key is provided, additional keys are used as
1148 secondary criteria when the there is equality in all keys selected
1163 secondary criteria when the there is equality in all keys selected
1149 before them.
1164 before them.
1150
1165
1151 Abbreviations can be used for any key names, as long as the
1166 Abbreviations can be used for any key names, as long as the
1152 abbreviation is unambiguous. The following are the keys currently
1167 abbreviation is unambiguous. The following are the keys currently
1153 defined:
1168 defined:
1154
1169
1155 Valid Arg Meaning\\
1170 Valid Arg Meaning\\
1156 "calls" call count\\
1171 "calls" call count\\
1157 "cumulative" cumulative time\\
1172 "cumulative" cumulative time\\
1158 "file" file name\\
1173 "file" file name\\
1159 "module" file name\\
1174 "module" file name\\
1160 "pcalls" primitive call count\\
1175 "pcalls" primitive call count\\
1161 "line" line number\\
1176 "line" line number\\
1162 "name" function name\\
1177 "name" function name\\
1163 "nfl" name/file/line\\
1178 "nfl" name/file/line\\
1164 "stdname" standard name\\
1179 "stdname" standard name\\
1165 "time" internal time
1180 "time" internal time
1166
1181
1167 Note that all sorts on statistics are in descending order (placing
1182 Note that all sorts on statistics are in descending order (placing
1168 most time consuming items first), where as name, file, and line number
1183 most time consuming items first), where as name, file, and line number
1169 searches are in ascending order (i.e., alphabetical). The subtle
1184 searches are in ascending order (i.e., alphabetical). The subtle
1170 distinction between "nfl" and "stdname" is that the standard name is a
1185 distinction between "nfl" and "stdname" is that the standard name is a
1171 sort of the name as printed, which means that the embedded line
1186 sort of the name as printed, which means that the embedded line
1172 numbers get compared in an odd way. For example, lines 3, 20, and 40
1187 numbers get compared in an odd way. For example, lines 3, 20, and 40
1173 would (if the file names were the same) appear in the string order
1188 would (if the file names were the same) appear in the string order
1174 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1189 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1175 line numbers. In fact, sort_stats("nfl") is the same as
1190 line numbers. In fact, sort_stats("nfl") is the same as
1176 sort_stats("name", "file", "line").
1191 sort_stats("name", "file", "line").
1177
1192
1178 -T <filename>: save profile results as shown on screen to a text
1193 -T <filename>: save profile results as shown on screen to a text
1179 file. The profile is still shown on screen.
1194 file. The profile is still shown on screen.
1180
1195
1181 -D <filename>: save (via dump_stats) profile statistics to given
1196 -D <filename>: save (via dump_stats) profile statistics to given
1182 filename. This data is in a format understod by the pstats module, and
1197 filename. This data is in a format understod by the pstats module, and
1183 is generated by a call to the dump_stats() method of profile
1198 is generated by a call to the dump_stats() method of profile
1184 objects. The profile is still shown on screen.
1199 objects. The profile is still shown on screen.
1185
1200
1186 If you want to run complete programs under the profiler's control, use
1201 If you want to run complete programs under the profiler's control, use
1187 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1202 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1188 contains profiler specific options as described here.
1203 contains profiler specific options as described here.
1189
1204
1190 You can read the complete documentation for the profile module with:\\
1205 You can read the complete documentation for the profile module with:\\
1191 In [1]: import profile; profile.help() """
1206 In [1]: import profile; profile.help() """
1192
1207
1193 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1208 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1194 # protect user quote marks
1209 # protect user quote marks
1195 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1210 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1196
1211
1197 if user_mode: # regular user call
1212 if user_mode: # regular user call
1198 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1213 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1199 list_all=1)
1214 list_all=1)
1200 namespace = self.shell.user_ns
1215 namespace = self.shell.user_ns
1201 else: # called to run a program by %run -p
1216 else: # called to run a program by %run -p
1202 try:
1217 try:
1203 filename = get_py_filename(arg_lst[0])
1218 filename = get_py_filename(arg_lst[0])
1204 except IOError,msg:
1219 except IOError,msg:
1205 error(msg)
1220 error(msg)
1206 return
1221 return
1207
1222
1208 arg_str = 'execfile(filename,prog_ns)'
1223 arg_str = 'execfile(filename,prog_ns)'
1209 namespace = locals()
1224 namespace = locals()
1210
1225
1211 opts.merge(opts_def)
1226 opts.merge(opts_def)
1212
1227
1213 prof = profile.Profile()
1228 prof = profile.Profile()
1214 try:
1229 try:
1215 prof = prof.runctx(arg_str,namespace,namespace)
1230 prof = prof.runctx(arg_str,namespace,namespace)
1216 sys_exit = ''
1231 sys_exit = ''
1217 except SystemExit:
1232 except SystemExit:
1218 sys_exit = """*** SystemExit exception caught in code being profiled."""
1233 sys_exit = """*** SystemExit exception caught in code being profiled."""
1219
1234
1220 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1235 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1221
1236
1222 lims = opts.l
1237 lims = opts.l
1223 if lims:
1238 if lims:
1224 lims = [] # rebuild lims with ints/floats/strings
1239 lims = [] # rebuild lims with ints/floats/strings
1225 for lim in opts.l:
1240 for lim in opts.l:
1226 try:
1241 try:
1227 lims.append(int(lim))
1242 lims.append(int(lim))
1228 except ValueError:
1243 except ValueError:
1229 try:
1244 try:
1230 lims.append(float(lim))
1245 lims.append(float(lim))
1231 except ValueError:
1246 except ValueError:
1232 lims.append(lim)
1247 lims.append(lim)
1233
1248
1234 # trap output
1249 # trap output
1235 sys_stdout = sys.stdout
1250 sys_stdout = sys.stdout
1236 stdout_trap = StringIO()
1251 stdout_trap = StringIO()
1237 try:
1252 try:
1238 sys.stdout = stdout_trap
1253 sys.stdout = stdout_trap
1239 stats.print_stats(*lims)
1254 stats.print_stats(*lims)
1240 finally:
1255 finally:
1241 sys.stdout = sys_stdout
1256 sys.stdout = sys_stdout
1242 output = stdout_trap.getvalue()
1257 output = stdout_trap.getvalue()
1243 output = output.rstrip()
1258 output = output.rstrip()
1244
1259
1245 page(output,screen_lines=self.shell.rc.screen_length)
1260 page(output,screen_lines=self.shell.rc.screen_length)
1246 print sys_exit,
1261 print sys_exit,
1247
1262
1248 dump_file = opts.D[0]
1263 dump_file = opts.D[0]
1249 text_file = opts.T[0]
1264 text_file = opts.T[0]
1250 if dump_file:
1265 if dump_file:
1251 prof.dump_stats(dump_file)
1266 prof.dump_stats(dump_file)
1252 print '\n*** Profile stats marshalled to file',\
1267 print '\n*** Profile stats marshalled to file',\
1253 `dump_file`+'.',sys_exit
1268 `dump_file`+'.',sys_exit
1254 if text_file:
1269 if text_file:
1255 file(text_file,'w').write(output)
1270 file(text_file,'w').write(output)
1256 print '\n*** Profile printout saved to text file',\
1271 print '\n*** Profile printout saved to text file',\
1257 `text_file`+'.',sys_exit
1272 `text_file`+'.',sys_exit
1258
1273
1259 if opts.has_key('r'):
1274 if opts.has_key('r'):
1260 return stats
1275 return stats
1261 else:
1276 else:
1262 return None
1277 return None
1263
1278
1264 def magic_run(self, parameter_s ='',runner=None):
1279 def magic_run(self, parameter_s ='',runner=None):
1265 """Run the named file inside IPython as a program.
1280 """Run the named file inside IPython as a program.
1266
1281
1267 Usage:\\
1282 Usage:\\
1268 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1283 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1269
1284
1270 Parameters after the filename are passed as command-line arguments to
1285 Parameters after the filename are passed as command-line arguments to
1271 the program (put in sys.argv). Then, control returns to IPython's
1286 the program (put in sys.argv). Then, control returns to IPython's
1272 prompt.
1287 prompt.
1273
1288
1274 This is similar to running at a system prompt:\\
1289 This is similar to running at a system prompt:\\
1275 $ python file args\\
1290 $ python file args\\
1276 but with the advantage of giving you IPython's tracebacks, and of
1291 but with the advantage of giving you IPython's tracebacks, and of
1277 loading all variables into your interactive namespace for further use
1292 loading all variables into your interactive namespace for further use
1278 (unless -p is used, see below).
1293 (unless -p is used, see below).
1279
1294
1280 The file is executed in a namespace initially consisting only of
1295 The file is executed in a namespace initially consisting only of
1281 __name__=='__main__' and sys.argv constructed as indicated. It thus
1296 __name__=='__main__' and sys.argv constructed as indicated. It thus
1282 sees its environment as if it were being run as a stand-alone
1297 sees its environment as if it were being run as a stand-alone
1283 program. But after execution, the IPython interactive namespace gets
1298 program. But after execution, the IPython interactive namespace gets
1284 updated with all variables defined in the program (except for __name__
1299 updated with all variables defined in the program (except for __name__
1285 and sys.argv). This allows for very convenient loading of code for
1300 and sys.argv). This allows for very convenient loading of code for
1286 interactive work, while giving each program a 'clean sheet' to run in.
1301 interactive work, while giving each program a 'clean sheet' to run in.
1287
1302
1288 Options:
1303 Options:
1289
1304
1290 -n: __name__ is NOT set to '__main__', but to the running file's name
1305 -n: __name__ is NOT set to '__main__', but to the running file's name
1291 without extension (as python does under import). This allows running
1306 without extension (as python does under import). This allows running
1292 scripts and reloading the definitions in them without calling code
1307 scripts and reloading the definitions in them without calling code
1293 protected by an ' if __name__ == "__main__" ' clause.
1308 protected by an ' if __name__ == "__main__" ' clause.
1294
1309
1295 -i: run the file in IPython's namespace instead of an empty one. This
1310 -i: run the file in IPython's namespace instead of an empty one. This
1296 is useful if you are experimenting with code written in a text editor
1311 is useful if you are experimenting with code written in a text editor
1297 which depends on variables defined interactively.
1312 which depends on variables defined interactively.
1298
1313
1299 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1314 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1300 being run. This is particularly useful if IPython is being used to
1315 being run. This is particularly useful if IPython is being used to
1301 run unittests, which always exit with a sys.exit() call. In such
1316 run unittests, which always exit with a sys.exit() call. In such
1302 cases you are interested in the output of the test results, not in
1317 cases you are interested in the output of the test results, not in
1303 seeing a traceback of the unittest module.
1318 seeing a traceback of the unittest module.
1304
1319
1305 -t: print timing information at the end of the run. IPython will give
1320 -t: print timing information at the end of the run. IPython will give
1306 you an estimated CPU time consumption for your script, which under
1321 you an estimated CPU time consumption for your script, which under
1307 Unix uses the resource module to avoid the wraparound problems of
1322 Unix uses the resource module to avoid the wraparound problems of
1308 time.clock(). Under Unix, an estimate of time spent on system tasks
1323 time.clock(). Under Unix, an estimate of time spent on system tasks
1309 is also given (for Windows platforms this is reported as 0.0).
1324 is also given (for Windows platforms this is reported as 0.0).
1310
1325
1311 If -t is given, an additional -N<N> option can be given, where <N>
1326 If -t is given, an additional -N<N> option can be given, where <N>
1312 must be an integer indicating how many times you want the script to
1327 must be an integer indicating how many times you want the script to
1313 run. The final timing report will include total and per run results.
1328 run. The final timing report will include total and per run results.
1314
1329
1315 For example (testing the script uniq_stable.py):
1330 For example (testing the script uniq_stable.py):
1316
1331
1317 In [1]: run -t uniq_stable
1332 In [1]: run -t uniq_stable
1318
1333
1319 IPython CPU timings (estimated):\\
1334 IPython CPU timings (estimated):\\
1320 User : 0.19597 s.\\
1335 User : 0.19597 s.\\
1321 System: 0.0 s.\\
1336 System: 0.0 s.\\
1322
1337
1323 In [2]: run -t -N5 uniq_stable
1338 In [2]: run -t -N5 uniq_stable
1324
1339
1325 IPython CPU timings (estimated):\\
1340 IPython CPU timings (estimated):\\
1326 Total runs performed: 5\\
1341 Total runs performed: 5\\
1327 Times : Total Per run\\
1342 Times : Total Per run\\
1328 User : 0.910862 s, 0.1821724 s.\\
1343 User : 0.910862 s, 0.1821724 s.\\
1329 System: 0.0 s, 0.0 s.
1344 System: 0.0 s, 0.0 s.
1330
1345
1331 -d: run your program under the control of pdb, the Python debugger.
1346 -d: run your program under the control of pdb, the Python debugger.
1332 This allows you to execute your program step by step, watch variables,
1347 This allows you to execute your program step by step, watch variables,
1333 etc. Internally, what IPython does is similar to calling:
1348 etc. Internally, what IPython does is similar to calling:
1334
1349
1335 pdb.run('execfile("YOURFILENAME")')
1350 pdb.run('execfile("YOURFILENAME")')
1336
1351
1337 with a breakpoint set on line 1 of your file. You can change the line
1352 with a breakpoint set on line 1 of your file. You can change the line
1338 number for this automatic breakpoint to be <N> by using the -bN option
1353 number for this automatic breakpoint to be <N> by using the -bN option
1339 (where N must be an integer). For example:
1354 (where N must be an integer). For example:
1340
1355
1341 %run -d -b40 myscript
1356 %run -d -b40 myscript
1342
1357
1343 will set the first breakpoint at line 40 in myscript.py. Note that
1358 will set the first breakpoint at line 40 in myscript.py. Note that
1344 the first breakpoint must be set on a line which actually does
1359 the first breakpoint must be set on a line which actually does
1345 something (not a comment or docstring) for it to stop execution.
1360 something (not a comment or docstring) for it to stop execution.
1346
1361
1347 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1362 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1348 first enter 'c' (without qoutes) to start execution up to the first
1363 first enter 'c' (without qoutes) to start execution up to the first
1349 breakpoint.
1364 breakpoint.
1350
1365
1351 Entering 'help' gives information about the use of the debugger. You
1366 Entering 'help' gives information about the use of the debugger. You
1352 can easily see pdb's full documentation with "import pdb;pdb.help()"
1367 can easily see pdb's full documentation with "import pdb;pdb.help()"
1353 at a prompt.
1368 at a prompt.
1354
1369
1355 -p: run program under the control of the Python profiler module (which
1370 -p: run program under the control of the Python profiler module (which
1356 prints a detailed report of execution times, function calls, etc).
1371 prints a detailed report of execution times, function calls, etc).
1357
1372
1358 You can pass other options after -p which affect the behavior of the
1373 You can pass other options after -p which affect the behavior of the
1359 profiler itself. See the docs for %prun for details.
1374 profiler itself. See the docs for %prun for details.
1360
1375
1361 In this mode, the program's variables do NOT propagate back to the
1376 In this mode, the program's variables do NOT propagate back to the
1362 IPython interactive namespace (because they remain in the namespace
1377 IPython interactive namespace (because they remain in the namespace
1363 where the profiler executes them).
1378 where the profiler executes them).
1364
1379
1365 Internally this triggers a call to %prun, see its documentation for
1380 Internally this triggers a call to %prun, see its documentation for
1366 details on the options available specifically for profiling."""
1381 details on the options available specifically for profiling."""
1367
1382
1368 # get arguments and set sys.argv for program to be run.
1383 # get arguments and set sys.argv for program to be run.
1369 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1384 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1370 mode='list',list_all=1)
1385 mode='list',list_all=1)
1371
1386
1372 try:
1387 try:
1373 filename = get_py_filename(arg_lst[0])
1388 filename = get_py_filename(arg_lst[0])
1374 except IndexError:
1389 except IndexError:
1375 warn('you must provide at least a filename.')
1390 warn('you must provide at least a filename.')
1376 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1391 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1377 return
1392 return
1378 except IOError,msg:
1393 except IOError,msg:
1379 error(msg)
1394 error(msg)
1380 return
1395 return
1381
1396
1382 # Control the response to exit() calls made by the script being run
1397 # Control the response to exit() calls made by the script being run
1383 exit_ignore = opts.has_key('e')
1398 exit_ignore = opts.has_key('e')
1384
1399
1385 # Make sure that the running script gets a proper sys.argv as if it
1400 # Make sure that the running script gets a proper sys.argv as if it
1386 # were run from a system shell.
1401 # were run from a system shell.
1387 save_argv = sys.argv # save it for later restoring
1402 save_argv = sys.argv # save it for later restoring
1388 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1403 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1389
1404
1390 if opts.has_key('i'):
1405 if opts.has_key('i'):
1391 prog_ns = self.shell.user_ns
1406 prog_ns = self.shell.user_ns
1392 __name__save = self.shell.user_ns['__name__']
1407 __name__save = self.shell.user_ns['__name__']
1393 prog_ns['__name__'] = '__main__'
1408 prog_ns['__name__'] = '__main__'
1394 else:
1409 else:
1395 if opts.has_key('n'):
1410 if opts.has_key('n'):
1396 name = os.path.splitext(os.path.basename(filename))[0]
1411 name = os.path.splitext(os.path.basename(filename))[0]
1397 else:
1412 else:
1398 name = '__main__'
1413 name = '__main__'
1399 prog_ns = {'__name__':name}
1414 prog_ns = {'__name__':name}
1400
1415
1401 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1416 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1402 # set the __file__ global in the script's namespace
1417 # set the __file__ global in the script's namespace
1403 prog_ns['__file__'] = filename
1418 prog_ns['__file__'] = filename
1404
1419
1405 # pickle fix. See iplib for an explanation. But we need to make sure
1420 # pickle fix. See iplib for an explanation. But we need to make sure
1406 # that, if we overwrite __main__, we replace it at the end
1421 # that, if we overwrite __main__, we replace it at the end
1407 if prog_ns['__name__'] == '__main__':
1422 if prog_ns['__name__'] == '__main__':
1408 restore_main = sys.modules['__main__']
1423 restore_main = sys.modules['__main__']
1409 else:
1424 else:
1410 restore_main = False
1425 restore_main = False
1411
1426
1412 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1427 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1413
1428
1414 stats = None
1429 stats = None
1415 try:
1430 try:
1416 if opts.has_key('p'):
1431 if opts.has_key('p'):
1417 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1432 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1418 else:
1433 else:
1419 if opts.has_key('d'):
1434 if opts.has_key('d'):
1420 deb = Debugger.Pdb(self.shell.rc.colors)
1435 deb = Debugger.Pdb(self.shell.rc.colors)
1421 # reset Breakpoint state, which is moronically kept
1436 # reset Breakpoint state, which is moronically kept
1422 # in a class
1437 # in a class
1423 bdb.Breakpoint.next = 1
1438 bdb.Breakpoint.next = 1
1424 bdb.Breakpoint.bplist = {}
1439 bdb.Breakpoint.bplist = {}
1425 bdb.Breakpoint.bpbynumber = [None]
1440 bdb.Breakpoint.bpbynumber = [None]
1426 # Set an initial breakpoint to stop execution
1441 # Set an initial breakpoint to stop execution
1427 maxtries = 10
1442 maxtries = 10
1428 bp = int(opts.get('b',[1])[0])
1443 bp = int(opts.get('b',[1])[0])
1429 checkline = deb.checkline(filename,bp)
1444 checkline = deb.checkline(filename,bp)
1430 if not checkline:
1445 if not checkline:
1431 for bp in range(bp+1,bp+maxtries+1):
1446 for bp in range(bp+1,bp+maxtries+1):
1432 if deb.checkline(filename,bp):
1447 if deb.checkline(filename,bp):
1433 break
1448 break
1434 else:
1449 else:
1435 msg = ("\nI failed to find a valid line to set "
1450 msg = ("\nI failed to find a valid line to set "
1436 "a breakpoint\n"
1451 "a breakpoint\n"
1437 "after trying up to line: %s.\n"
1452 "after trying up to line: %s.\n"
1438 "Please set a valid breakpoint manually "
1453 "Please set a valid breakpoint manually "
1439 "with the -b option." % bp)
1454 "with the -b option." % bp)
1440 error(msg)
1455 error(msg)
1441 return
1456 return
1442 # if we find a good linenumber, set the breakpoint
1457 # if we find a good linenumber, set the breakpoint
1443 deb.do_break('%s:%s' % (filename,bp))
1458 deb.do_break('%s:%s' % (filename,bp))
1444 # Start file run
1459 # Start file run
1445 print "NOTE: Enter 'c' at the",
1460 print "NOTE: Enter 'c' at the",
1446 print "ipdb> prompt to start your script."
1461 print "ipdb> prompt to start your script."
1447 try:
1462 try:
1448 deb.run('execfile("%s")' % filename,prog_ns)
1463 deb.run('execfile("%s")' % filename,prog_ns)
1449 except:
1464 except:
1450 etype, value, tb = sys.exc_info()
1465 etype, value, tb = sys.exc_info()
1451 # Skip three frames in the traceback: the %run one,
1466 # Skip three frames in the traceback: the %run one,
1452 # one inside bdb.py, and the command-line typed by the
1467 # one inside bdb.py, and the command-line typed by the
1453 # user (run by exec in pdb itself).
1468 # user (run by exec in pdb itself).
1454 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1469 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1455 else:
1470 else:
1456 if runner is None:
1471 if runner is None:
1457 runner = self.shell.safe_execfile
1472 runner = self.shell.safe_execfile
1458 if opts.has_key('t'):
1473 if opts.has_key('t'):
1459 try:
1474 try:
1460 nruns = int(opts['N'][0])
1475 nruns = int(opts['N'][0])
1461 if nruns < 1:
1476 if nruns < 1:
1462 error('Number of runs must be >=1')
1477 error('Number of runs must be >=1')
1463 return
1478 return
1464 except (KeyError):
1479 except (KeyError):
1465 nruns = 1
1480 nruns = 1
1466 if nruns == 1:
1481 if nruns == 1:
1467 t0 = clock2()
1482 t0 = clock2()
1468 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1483 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1469 t1 = clock2()
1484 t1 = clock2()
1470 t_usr = t1[0]-t0[0]
1485 t_usr = t1[0]-t0[0]
1471 t_sys = t1[1]-t1[1]
1486 t_sys = t1[1]-t1[1]
1472 print "\nIPython CPU timings (estimated):"
1487 print "\nIPython CPU timings (estimated):"
1473 print " User : %10s s." % t_usr
1488 print " User : %10s s." % t_usr
1474 print " System: %10s s." % t_sys
1489 print " System: %10s s." % t_sys
1475 else:
1490 else:
1476 runs = range(nruns)
1491 runs = range(nruns)
1477 t0 = clock2()
1492 t0 = clock2()
1478 for nr in runs:
1493 for nr in runs:
1479 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1494 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1480 t1 = clock2()
1495 t1 = clock2()
1481 t_usr = t1[0]-t0[0]
1496 t_usr = t1[0]-t0[0]
1482 t_sys = t1[1]-t1[1]
1497 t_sys = t1[1]-t1[1]
1483 print "\nIPython CPU timings (estimated):"
1498 print "\nIPython CPU timings (estimated):"
1484 print "Total runs performed:",nruns
1499 print "Total runs performed:",nruns
1485 print " Times : %10s %10s" % ('Total','Per run')
1500 print " Times : %10s %10s" % ('Total','Per run')
1486 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1501 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1487 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1502 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1488
1503
1489 else:
1504 else:
1490 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1505 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1491 if opts.has_key('i'):
1506 if opts.has_key('i'):
1492 self.shell.user_ns['__name__'] = __name__save
1507 self.shell.user_ns['__name__'] = __name__save
1493 else:
1508 else:
1494 # update IPython interactive namespace
1509 # update IPython interactive namespace
1495 del prog_ns['__name__']
1510 del prog_ns['__name__']
1496 self.shell.user_ns.update(prog_ns)
1511 self.shell.user_ns.update(prog_ns)
1497 finally:
1512 finally:
1498 sys.argv = save_argv
1513 sys.argv = save_argv
1499 if restore_main:
1514 if restore_main:
1500 sys.modules['__main__'] = restore_main
1515 sys.modules['__main__'] = restore_main
1501 return stats
1516 return stats
1502
1517
1503 def magic_runlog(self, parameter_s =''):
1518 def magic_runlog(self, parameter_s =''):
1504 """Run files as logs.
1519 """Run files as logs.
1505
1520
1506 Usage:\\
1521 Usage:\\
1507 %runlog file1 file2 ...
1522 %runlog file1 file2 ...
1508
1523
1509 Run the named files (treating them as log files) in sequence inside
1524 Run the named files (treating them as log files) in sequence inside
1510 the interpreter, and return to the prompt. This is much slower than
1525 the interpreter, and return to the prompt. This is much slower than
1511 %run because each line is executed in a try/except block, but it
1526 %run because each line is executed in a try/except block, but it
1512 allows running files with syntax errors in them.
1527 allows running files with syntax errors in them.
1513
1528
1514 Normally IPython will guess when a file is one of its own logfiles, so
1529 Normally IPython will guess when a file is one of its own logfiles, so
1515 you can typically use %run even for logs. This shorthand allows you to
1530 you can typically use %run even for logs. This shorthand allows you to
1516 force any file to be treated as a log file."""
1531 force any file to be treated as a log file."""
1517
1532
1518 for f in parameter_s.split():
1533 for f in parameter_s.split():
1519 self.shell.safe_execfile(f,self.shell.user_ns,
1534 self.shell.safe_execfile(f,self.shell.user_ns,
1520 self.shell.user_ns,islog=1)
1535 self.shell.user_ns,islog=1)
1521
1536
1522 def magic_time(self,parameter_s = ''):
1537 def magic_time(self,parameter_s = ''):
1523 """Time execution of a Python statement or expression.
1538 """Time execution of a Python statement or expression.
1524
1539
1525 The CPU and wall clock times are printed, and the value of the
1540 The CPU and wall clock times are printed, and the value of the
1526 expression (if any) is returned. Note that under Win32, system time
1541 expression (if any) is returned. Note that under Win32, system time
1527 is always reported as 0, since it can not be measured.
1542 is always reported as 0, since it can not be measured.
1528
1543
1529 This function provides very basic timing functionality. In Python
1544 This function provides very basic timing functionality. In Python
1530 2.3, the timeit module offers more control and sophistication, but for
1545 2.3, the timeit module offers more control and sophistication, but for
1531 now IPython supports Python 2.2, so we can not rely on timeit being
1546 now IPython supports Python 2.2, so we can not rely on timeit being
1532 present.
1547 present.
1533
1548
1534 Some examples:
1549 Some examples:
1535
1550
1536 In [1]: time 2**128
1551 In [1]: time 2**128
1537 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1552 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1538 Wall time: 0.00
1553 Wall time: 0.00
1539 Out[1]: 340282366920938463463374607431768211456L
1554 Out[1]: 340282366920938463463374607431768211456L
1540
1555
1541 In [2]: n = 1000000
1556 In [2]: n = 1000000
1542
1557
1543 In [3]: time sum(range(n))
1558 In [3]: time sum(range(n))
1544 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1559 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1545 Wall time: 1.37
1560 Wall time: 1.37
1546 Out[3]: 499999500000L
1561 Out[3]: 499999500000L
1547
1562
1548 In [4]: time print 'hello world'
1563 In [4]: time print 'hello world'
1549 hello world
1564 hello world
1550 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1565 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1551 Wall time: 0.00
1566 Wall time: 0.00
1552 """
1567 """
1553
1568
1554 # fail immediately if the given expression can't be compiled
1569 # fail immediately if the given expression can't be compiled
1555 try:
1570 try:
1556 mode = 'eval'
1571 mode = 'eval'
1557 code = compile(parameter_s,'<timed eval>',mode)
1572 code = compile(parameter_s,'<timed eval>',mode)
1558 except SyntaxError:
1573 except SyntaxError:
1559 mode = 'exec'
1574 mode = 'exec'
1560 code = compile(parameter_s,'<timed exec>',mode)
1575 code = compile(parameter_s,'<timed exec>',mode)
1561 # skew measurement as little as possible
1576 # skew measurement as little as possible
1562 glob = self.shell.user_ns
1577 glob = self.shell.user_ns
1563 clk = clock2
1578 clk = clock2
1564 wtime = time.time
1579 wtime = time.time
1565 # time execution
1580 # time execution
1566 wall_st = wtime()
1581 wall_st = wtime()
1567 if mode=='eval':
1582 if mode=='eval':
1568 st = clk()
1583 st = clk()
1569 out = eval(code,glob)
1584 out = eval(code,glob)
1570 end = clk()
1585 end = clk()
1571 else:
1586 else:
1572 st = clk()
1587 st = clk()
1573 exec code in glob
1588 exec code in glob
1574 end = clk()
1589 end = clk()
1575 out = None
1590 out = None
1576 wall_end = wtime()
1591 wall_end = wtime()
1577 # Compute actual times and report
1592 # Compute actual times and report
1578 wall_time = wall_end-wall_st
1593 wall_time = wall_end-wall_st
1579 cpu_user = end[0]-st[0]
1594 cpu_user = end[0]-st[0]
1580 cpu_sys = end[1]-st[1]
1595 cpu_sys = end[1]-st[1]
1581 cpu_tot = cpu_user+cpu_sys
1596 cpu_tot = cpu_user+cpu_sys
1582 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1597 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1583 (cpu_user,cpu_sys,cpu_tot)
1598 (cpu_user,cpu_sys,cpu_tot)
1584 print "Wall time: %.2f" % wall_time
1599 print "Wall time: %.2f" % wall_time
1585 return out
1600 return out
1586
1601
1587 def magic_macro(self,parameter_s = ''):
1602 def magic_macro(self,parameter_s = ''):
1588 """Define a set of input lines as a macro for future re-execution.
1603 """Define a set of input lines as a macro for future re-execution.
1589
1604
1590 Usage:\\
1605 Usage:\\
1591 %macro name n1-n2 n3-n4 ... n5 .. n6 ...
1606 %macro name n1-n2 n3-n4 ... n5 .. n6 ...
1592
1607
1593 This will define a global variable called `name` which is a string
1608 This will define a global variable called `name` which is a string
1594 made of joining the slices and lines you specify (n1,n2,... numbers
1609 made of joining the slices and lines you specify (n1,n2,... numbers
1595 above) from your input history into a single string. This variable
1610 above) from your input history into a single string. This variable
1596 acts like an automatic function which re-executes those lines as if
1611 acts like an automatic function which re-executes those lines as if
1597 you had typed them. You just type 'name' at the prompt and the code
1612 you had typed them. You just type 'name' at the prompt and the code
1598 executes.
1613 executes.
1599
1614
1600 The notation for indicating number ranges is: n1-n2 means 'use line
1615 The notation for indicating number ranges is: n1-n2 means 'use line
1601 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1616 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1602 using the lines numbered 5,6 and 7.
1617 using the lines numbered 5,6 and 7.
1603
1618
1604 Note: as a 'hidden' feature, you can also use traditional python slice
1619 Note: as a 'hidden' feature, you can also use traditional python slice
1605 notation, where N:M means numbers N through M-1.
1620 notation, where N:M means numbers N through M-1.
1606
1621
1607 For example, if your history contains (%hist prints it):
1622 For example, if your history contains (%hist prints it):
1608
1623
1609 44: x=1\\
1624 44: x=1\\
1610 45: y=3\\
1625 45: y=3\\
1611 46: z=x+y\\
1626 46: z=x+y\\
1612 47: print x\\
1627 47: print x\\
1613 48: a=5\\
1628 48: a=5\\
1614 49: print 'x',x,'y',y\\
1629 49: print 'x',x,'y',y\\
1615
1630
1616 you can create a macro with lines 44 through 47 (included) and line 49
1631 you can create a macro with lines 44 through 47 (included) and line 49
1617 called my_macro with:
1632 called my_macro with:
1618
1633
1619 In [51]: %macro my_macro 44-47 49
1634 In [51]: %macro my_macro 44-47 49
1620
1635
1621 Now, typing `my_macro` (without quotes) will re-execute all this code
1636 Now, typing `my_macro` (without quotes) will re-execute all this code
1622 in one pass.
1637 in one pass.
1623
1638
1624 You don't need to give the line-numbers in order, and any given line
1639 You don't need to give the line-numbers in order, and any given line
1625 number can appear multiple times. You can assemble macros with any
1640 number can appear multiple times. You can assemble macros with any
1626 lines from your input history in any order.
1641 lines from your input history in any order.
1627
1642
1628 The macro is a simple object which holds its value in an attribute,
1643 The macro is a simple object which holds its value in an attribute,
1629 but IPython's display system checks for macros and executes them as
1644 but IPython's display system checks for macros and executes them as
1630 code instead of printing them when you type their name.
1645 code instead of printing them when you type their name.
1631
1646
1632 You can view a macro's contents by explicitly printing it with:
1647 You can view a macro's contents by explicitly printing it with:
1633
1648
1634 'print macro_name'.
1649 'print macro_name'.
1635
1650
1636 For one-off cases which DON'T contain magic function calls in them you
1651 For one-off cases which DON'T contain magic function calls in them you
1637 can obtain similar results by explicitly executing slices from your
1652 can obtain similar results by explicitly executing slices from your
1638 input history with:
1653 input history with:
1639
1654
1640 In [60]: exec In[44:48]+In[49]"""
1655 In [60]: exec In[44:48]+In[49]"""
1641
1656
1642 args = parameter_s.split()
1657 args = parameter_s.split()
1643 name,ranges = args[0], args[1:]
1658 name,ranges = args[0], args[1:]
1644 #print 'rng',ranges # dbg
1659 #print 'rng',ranges # dbg
1645 lines = self.extract_input_slices(ranges)
1660 lines = self.extract_input_slices(ranges)
1646 macro = Macro(lines)
1661 macro = Macro(lines)
1647 self.shell.user_ns.update({name:macro})
1662 self.shell.user_ns.update({name:macro})
1648 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1663 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1649 print 'Macro contents:'
1664 print 'Macro contents:'
1650 print macro,
1665 print macro,
1651
1666
1652 def magic_save(self,parameter_s = ''):
1667 def magic_save(self,parameter_s = ''):
1653 """Save a set of lines to a given filename.
1668 """Save a set of lines to a given filename.
1654
1669
1655 Usage:\\
1670 Usage:\\
1656 %save filename n1-n2 n3-n4 ... n5 .. n6 ...
1671 %save filename n1-n2 n3-n4 ... n5 .. n6 ...
1657
1672
1658 This function uses the same syntax as %macro for line extraction, but
1673 This function uses the same syntax as %macro for line extraction, but
1659 instead of creating a macro it saves the resulting string to the
1674 instead of creating a macro it saves the resulting string to the
1660 filename you specify.
1675 filename you specify.
1661
1676
1662 It adds a '.py' extension to the file if you don't do so yourself, and
1677 It adds a '.py' extension to the file if you don't do so yourself, and
1663 it asks for confirmation before overwriting existing files."""
1678 it asks for confirmation before overwriting existing files."""
1664
1679
1665 args = parameter_s.split()
1680 args = parameter_s.split()
1666 fname,ranges = args[0], args[1:]
1681 fname,ranges = args[0], args[1:]
1667 if not fname.endswith('.py'):
1682 if not fname.endswith('.py'):
1668 fname += '.py'
1683 fname += '.py'
1669 if os.path.isfile(fname):
1684 if os.path.isfile(fname):
1670 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1685 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1671 if ans.lower() not in ['y','yes']:
1686 if ans.lower() not in ['y','yes']:
1672 print 'Operation cancelled.'
1687 print 'Operation cancelled.'
1673 return
1688 return
1674 cmds = ''.join(self.extract_input_slices(ranges))
1689 cmds = ''.join(self.extract_input_slices(ranges))
1675 f = file(fname,'w')
1690 f = file(fname,'w')
1676 f.write(cmds)
1691 f.write(cmds)
1677 f.close()
1692 f.close()
1678 print 'The following commands were written to file `%s`:' % fname
1693 print 'The following commands were written to file `%s`:' % fname
1679 print cmds
1694 print cmds
1680
1695
1681 def _edit_macro(self,mname,macro):
1696 def _edit_macro(self,mname,macro):
1682 """open an editor with the macro data in a file"""
1697 """open an editor with the macro data in a file"""
1683 filename = self.shell.mktempfile(macro.value)
1698 filename = self.shell.mktempfile(macro.value)
1684 self.shell.hooks.editor(filename)
1699 self.shell.hooks.editor(filename)
1685
1700
1686 # and make a new macro object, to replace the old one
1701 # and make a new macro object, to replace the old one
1687 mfile = open(filename)
1702 mfile = open(filename)
1688 mvalue = mfile.read()
1703 mvalue = mfile.read()
1689 mfile.close()
1704 mfile.close()
1690 self.shell.user_ns[mname] = Macro(mvalue)
1705 self.shell.user_ns[mname] = Macro(mvalue)
1691
1706
1692 def magic_ed(self,parameter_s=''):
1707 def magic_ed(self,parameter_s=''):
1693 """Alias to %edit."""
1708 """Alias to %edit."""
1694 return self.magic_edit(parameter_s)
1709 return self.magic_edit(parameter_s)
1695
1710
1696 def magic_edit(self,parameter_s='',last_call=['','']):
1711 def magic_edit(self,parameter_s='',last_call=['','']):
1697 """Bring up an editor and execute the resulting code.
1712 """Bring up an editor and execute the resulting code.
1698
1713
1699 Usage:
1714 Usage:
1700 %edit [options] [args]
1715 %edit [options] [args]
1701
1716
1702 %edit runs IPython's editor hook. The default version of this hook is
1717 %edit runs IPython's editor hook. The default version of this hook is
1703 set to call the __IPYTHON__.rc.editor command. This is read from your
1718 set to call the __IPYTHON__.rc.editor command. This is read from your
1704 environment variable $EDITOR. If this isn't found, it will default to
1719 environment variable $EDITOR. If this isn't found, it will default to
1705 vi under Linux/Unix and to notepad under Windows. See the end of this
1720 vi under Linux/Unix and to notepad under Windows. See the end of this
1706 docstring for how to change the editor hook.
1721 docstring for how to change the editor hook.
1707
1722
1708 You can also set the value of this editor via the command line option
1723 You can also set the value of this editor via the command line option
1709 '-editor' or in your ipythonrc file. This is useful if you wish to use
1724 '-editor' or in your ipythonrc file. This is useful if you wish to use
1710 specifically for IPython an editor different from your typical default
1725 specifically for IPython an editor different from your typical default
1711 (and for Windows users who typically don't set environment variables).
1726 (and for Windows users who typically don't set environment variables).
1712
1727
1713 This command allows you to conveniently edit multi-line code right in
1728 This command allows you to conveniently edit multi-line code right in
1714 your IPython session.
1729 your IPython session.
1715
1730
1716 If called without arguments, %edit opens up an empty editor with a
1731 If called without arguments, %edit opens up an empty editor with a
1717 temporary file and will execute the contents of this file when you
1732 temporary file and will execute the contents of this file when you
1718 close it (don't forget to save it!).
1733 close it (don't forget to save it!).
1719
1734
1720
1735
1721 Options:
1736 Options:
1722
1737
1723 -p: this will call the editor with the same data as the previous time
1738 -p: this will call the editor with the same data as the previous time
1724 it was used, regardless of how long ago (in your current session) it
1739 it was used, regardless of how long ago (in your current session) it
1725 was.
1740 was.
1726
1741
1727 -x: do not execute the edited code immediately upon exit. This is
1742 -x: do not execute the edited code immediately upon exit. This is
1728 mainly useful if you are editing programs which need to be called with
1743 mainly useful if you are editing programs which need to be called with
1729 command line arguments, which you can then do using %run.
1744 command line arguments, which you can then do using %run.
1730
1745
1731
1746
1732 Arguments:
1747 Arguments:
1733
1748
1734 If arguments are given, the following possibilites exist:
1749 If arguments are given, the following possibilites exist:
1735
1750
1736 - The arguments are numbers or pairs of colon-separated numbers (like
1751 - The arguments are numbers or pairs of colon-separated numbers (like
1737 1 4:8 9). These are interpreted as lines of previous input to be
1752 1 4:8 9). These are interpreted as lines of previous input to be
1738 loaded into the editor. The syntax is the same of the %macro command.
1753 loaded into the editor. The syntax is the same of the %macro command.
1739
1754
1740 - If the argument doesn't start with a number, it is evaluated as a
1755 - If the argument doesn't start with a number, it is evaluated as a
1741 variable and its contents loaded into the editor. You can thus edit
1756 variable and its contents loaded into the editor. You can thus edit
1742 any string which contains python code (including the result of
1757 any string which contains python code (including the result of
1743 previous edits).
1758 previous edits).
1744
1759
1745 - If the argument is the name of an object (other than a string),
1760 - If the argument is the name of an object (other than a string),
1746 IPython will try to locate the file where it was defined and open the
1761 IPython will try to locate the file where it was defined and open the
1747 editor at the point where it is defined. You can use `%edit function`
1762 editor at the point where it is defined. You can use `%edit function`
1748 to load an editor exactly at the point where 'function' is defined,
1763 to load an editor exactly at the point where 'function' is defined,
1749 edit it and have the file be executed automatically.
1764 edit it and have the file be executed automatically.
1750
1765
1751 If the object is a macro (see %macro for details), this opens up your
1766 If the object is a macro (see %macro for details), this opens up your
1752 specified editor with a temporary file containing the macro's data.
1767 specified editor with a temporary file containing the macro's data.
1753 Upon exit, the macro is reloaded with the contents of the file.
1768 Upon exit, the macro is reloaded with the contents of the file.
1754
1769
1755 Note: opening at an exact line is only supported under Unix, and some
1770 Note: opening at an exact line is only supported under Unix, and some
1756 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1771 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1757 '+NUMBER' parameter necessary for this feature. Good editors like
1772 '+NUMBER' parameter necessary for this feature. Good editors like
1758 (X)Emacs, vi, jed, pico and joe all do.
1773 (X)Emacs, vi, jed, pico and joe all do.
1759
1774
1760 - If the argument is not found as a variable, IPython will look for a
1775 - If the argument is not found as a variable, IPython will look for a
1761 file with that name (adding .py if necessary) and load it into the
1776 file with that name (adding .py if necessary) and load it into the
1762 editor. It will execute its contents with execfile() when you exit,
1777 editor. It will execute its contents with execfile() when you exit,
1763 loading any code in the file into your interactive namespace.
1778 loading any code in the file into your interactive namespace.
1764
1779
1765 After executing your code, %edit will return as output the code you
1780 After executing your code, %edit will return as output the code you
1766 typed in the editor (except when it was an existing file). This way
1781 typed in the editor (except when it was an existing file). This way
1767 you can reload the code in further invocations of %edit as a variable,
1782 you can reload the code in further invocations of %edit as a variable,
1768 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1783 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1769 the output.
1784 the output.
1770
1785
1771 Note that %edit is also available through the alias %ed.
1786 Note that %edit is also available through the alias %ed.
1772
1787
1773 This is an example of creating a simple function inside the editor and
1788 This is an example of creating a simple function inside the editor and
1774 then modifying it. First, start up the editor:
1789 then modifying it. First, start up the editor:
1775
1790
1776 In [1]: ed\\
1791 In [1]: ed\\
1777 Editing... done. Executing edited code...\\
1792 Editing... done. Executing edited code...\\
1778 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1793 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1779
1794
1780 We can then call the function foo():
1795 We can then call the function foo():
1781
1796
1782 In [2]: foo()\\
1797 In [2]: foo()\\
1783 foo() was defined in an editing session
1798 foo() was defined in an editing session
1784
1799
1785 Now we edit foo. IPython automatically loads the editor with the
1800 Now we edit foo. IPython automatically loads the editor with the
1786 (temporary) file where foo() was previously defined:
1801 (temporary) file where foo() was previously defined:
1787
1802
1788 In [3]: ed foo\\
1803 In [3]: ed foo\\
1789 Editing... done. Executing edited code...
1804 Editing... done. Executing edited code...
1790
1805
1791 And if we call foo() again we get the modified version:
1806 And if we call foo() again we get the modified version:
1792
1807
1793 In [4]: foo()\\
1808 In [4]: foo()\\
1794 foo() has now been changed!
1809 foo() has now been changed!
1795
1810
1796 Here is an example of how to edit a code snippet successive
1811 Here is an example of how to edit a code snippet successive
1797 times. First we call the editor:
1812 times. First we call the editor:
1798
1813
1799 In [8]: ed\\
1814 In [8]: ed\\
1800 Editing... done. Executing edited code...\\
1815 Editing... done. Executing edited code...\\
1801 hello\\
1816 hello\\
1802 Out[8]: "print 'hello'\\n"
1817 Out[8]: "print 'hello'\\n"
1803
1818
1804 Now we call it again with the previous output (stored in _):
1819 Now we call it again with the previous output (stored in _):
1805
1820
1806 In [9]: ed _\\
1821 In [9]: ed _\\
1807 Editing... done. Executing edited code...\\
1822 Editing... done. Executing edited code...\\
1808 hello world\\
1823 hello world\\
1809 Out[9]: "print 'hello world'\\n"
1824 Out[9]: "print 'hello world'\\n"
1810
1825
1811 Now we call it with the output #8 (stored in _8, also as Out[8]):
1826 Now we call it with the output #8 (stored in _8, also as Out[8]):
1812
1827
1813 In [10]: ed _8\\
1828 In [10]: ed _8\\
1814 Editing... done. Executing edited code...\\
1829 Editing... done. Executing edited code...\\
1815 hello again\\
1830 hello again\\
1816 Out[10]: "print 'hello again'\\n"
1831 Out[10]: "print 'hello again'\\n"
1817
1832
1818
1833
1819 Changing the default editor hook:
1834 Changing the default editor hook:
1820
1835
1821 If you wish to write your own editor hook, you can put it in a
1836 If you wish to write your own editor hook, you can put it in a
1822 configuration file which you load at startup time. The default hook
1837 configuration file which you load at startup time. The default hook
1823 is defined in the IPython.hooks module, and you can use that as a
1838 is defined in the IPython.hooks module, and you can use that as a
1824 starting example for further modifications. That file also has
1839 starting example for further modifications. That file also has
1825 general instructions on how to set a new hook for use once you've
1840 general instructions on how to set a new hook for use once you've
1826 defined it."""
1841 defined it."""
1827
1842
1828 # FIXME: This function has become a convoluted mess. It needs a
1843 # FIXME: This function has become a convoluted mess. It needs a
1829 # ground-up rewrite with clean, simple logic.
1844 # ground-up rewrite with clean, simple logic.
1830
1845
1831 def make_filename(arg):
1846 def make_filename(arg):
1832 "Make a filename from the given args"
1847 "Make a filename from the given args"
1833 try:
1848 try:
1834 filename = get_py_filename(arg)
1849 filename = get_py_filename(arg)
1835 except IOError:
1850 except IOError:
1836 if args.endswith('.py'):
1851 if args.endswith('.py'):
1837 filename = arg
1852 filename = arg
1838 else:
1853 else:
1839 filename = None
1854 filename = None
1840 return filename
1855 return filename
1841
1856
1842 # custom exceptions
1857 # custom exceptions
1843 class DataIsObject(Exception): pass
1858 class DataIsObject(Exception): pass
1844
1859
1845 opts,args = self.parse_options(parameter_s,'px')
1860 opts,args = self.parse_options(parameter_s,'px')
1846
1861
1847 # Default line number value
1862 # Default line number value
1848 lineno = None
1863 lineno = None
1849 if opts.has_key('p'):
1864 if opts.has_key('p'):
1850 args = '_%s' % last_call[0]
1865 args = '_%s' % last_call[0]
1851 if not self.shell.user_ns.has_key(args):
1866 if not self.shell.user_ns.has_key(args):
1852 args = last_call[1]
1867 args = last_call[1]
1853
1868
1854 # use last_call to remember the state of the previous call, but don't
1869 # use last_call to remember the state of the previous call, but don't
1855 # let it be clobbered by successive '-p' calls.
1870 # let it be clobbered by successive '-p' calls.
1856 try:
1871 try:
1857 last_call[0] = self.shell.outputcache.prompt_count
1872 last_call[0] = self.shell.outputcache.prompt_count
1858 if not opts.has_key('p'):
1873 if not opts.has_key('p'):
1859 last_call[1] = parameter_s
1874 last_call[1] = parameter_s
1860 except:
1875 except:
1861 pass
1876 pass
1862
1877
1863 # by default this is done with temp files, except when the given
1878 # by default this is done with temp files, except when the given
1864 # arg is a filename
1879 # arg is a filename
1865 use_temp = 1
1880 use_temp = 1
1866
1881
1867 if re.match(r'\d',args):
1882 if re.match(r'\d',args):
1868 # Mode where user specifies ranges of lines, like in %macro.
1883 # Mode where user specifies ranges of lines, like in %macro.
1869 # This means that you can't edit files whose names begin with
1884 # This means that you can't edit files whose names begin with
1870 # numbers this way. Tough.
1885 # numbers this way. Tough.
1871 ranges = args.split()
1886 ranges = args.split()
1872 data = ''.join(self.extract_input_slices(ranges))
1887 data = ''.join(self.extract_input_slices(ranges))
1873 elif args.endswith('.py'):
1888 elif args.endswith('.py'):
1874 filename = make_filename(args)
1889 filename = make_filename(args)
1875 data = ''
1890 data = ''
1876 use_temp = 0
1891 use_temp = 0
1877 elif args:
1892 elif args:
1878 try:
1893 try:
1879 # Load the parameter given as a variable. If not a string,
1894 # Load the parameter given as a variable. If not a string,
1880 # process it as an object instead (below)
1895 # process it as an object instead (below)
1881
1896
1882 #print '*** args',args,'type',type(args) # dbg
1897 #print '*** args',args,'type',type(args) # dbg
1883 data = eval(args,self.shell.user_ns)
1898 data = eval(args,self.shell.user_ns)
1884 if not type(data) in StringTypes:
1899 if not type(data) in StringTypes:
1885 raise DataIsObject
1900 raise DataIsObject
1886
1901
1887 except (NameError,SyntaxError):
1902 except (NameError,SyntaxError):
1888 # given argument is not a variable, try as a filename
1903 # given argument is not a variable, try as a filename
1889 filename = make_filename(args)
1904 filename = make_filename(args)
1890 if filename is None:
1905 if filename is None:
1891 warn("Argument given (%s) can't be found as a variable "
1906 warn("Argument given (%s) can't be found as a variable "
1892 "or as a filename." % args)
1907 "or as a filename." % args)
1893 return
1908 return
1894
1909
1895 data = ''
1910 data = ''
1896 use_temp = 0
1911 use_temp = 0
1897 except DataIsObject:
1912 except DataIsObject:
1898
1913
1899 # macros have a special edit function
1914 # macros have a special edit function
1900 if isinstance(data,Macro):
1915 if isinstance(data,Macro):
1901 self._edit_macro(args,data)
1916 self._edit_macro(args,data)
1902 return
1917 return
1903
1918
1904 # For objects, try to edit the file where they are defined
1919 # For objects, try to edit the file where they are defined
1905 try:
1920 try:
1906 filename = inspect.getabsfile(data)
1921 filename = inspect.getabsfile(data)
1907 datafile = 1
1922 datafile = 1
1908 except TypeError:
1923 except TypeError:
1909 filename = make_filename(args)
1924 filename = make_filename(args)
1910 datafile = 1
1925 datafile = 1
1911 warn('Could not find file where `%s` is defined.\n'
1926 warn('Could not find file where `%s` is defined.\n'
1912 'Opening a file named `%s`' % (args,filename))
1927 'Opening a file named `%s`' % (args,filename))
1913 # Now, make sure we can actually read the source (if it was in
1928 # Now, make sure we can actually read the source (if it was in
1914 # a temp file it's gone by now).
1929 # a temp file it's gone by now).
1915 if datafile:
1930 if datafile:
1916 try:
1931 try:
1917 lineno = inspect.getsourcelines(data)[1]
1932 lineno = inspect.getsourcelines(data)[1]
1918 except IOError:
1933 except IOError:
1919 filename = make_filename(args)
1934 filename = make_filename(args)
1920 if filename is None:
1935 if filename is None:
1921 warn('The file `%s` where `%s` was defined cannot '
1936 warn('The file `%s` where `%s` was defined cannot '
1922 'be read.' % (filename,data))
1937 'be read.' % (filename,data))
1923 return
1938 return
1924 use_temp = 0
1939 use_temp = 0
1925 else:
1940 else:
1926 data = ''
1941 data = ''
1927
1942
1928 if use_temp:
1943 if use_temp:
1929 filename = self.shell.mktempfile(data)
1944 filename = self.shell.mktempfile(data)
1930 print 'IPython will make a temporary file named:',filename
1945 print 'IPython will make a temporary file named:',filename
1931
1946
1932 # do actual editing here
1947 # do actual editing here
1933 print 'Editing...',
1948 print 'Editing...',
1934 sys.stdout.flush()
1949 sys.stdout.flush()
1935 self.shell.hooks.editor(filename,lineno)
1950 self.shell.hooks.editor(filename,lineno)
1936 if opts.has_key('x'): # -x prevents actual execution
1951 if opts.has_key('x'): # -x prevents actual execution
1937 print
1952 print
1938 else:
1953 else:
1939 print 'done. Executing edited code...'
1954 print 'done. Executing edited code...'
1940 self.shell.safe_execfile(filename,self.shell.user_ns)
1955 self.shell.safe_execfile(filename,self.shell.user_ns)
1941 if use_temp:
1956 if use_temp:
1942 try:
1957 try:
1943 return open(filename).read()
1958 return open(filename).read()
1944 except IOError,msg:
1959 except IOError,msg:
1945 if msg.filename == filename:
1960 if msg.filename == filename:
1946 warn('File not found. Did you forget to save?')
1961 warn('File not found. Did you forget to save?')
1947 return
1962 return
1948 else:
1963 else:
1949 self.shell.showtraceback()
1964 self.shell.showtraceback()
1950
1965
1951 def magic_xmode(self,parameter_s = ''):
1966 def magic_xmode(self,parameter_s = ''):
1952 """Switch modes for the exception handlers.
1967 """Switch modes for the exception handlers.
1953
1968
1954 Valid modes: Plain, Context and Verbose.
1969 Valid modes: Plain, Context and Verbose.
1955
1970
1956 If called without arguments, acts as a toggle."""
1971 If called without arguments, acts as a toggle."""
1957
1972
1958 def xmode_switch_err(name):
1973 def xmode_switch_err(name):
1959 warn('Error changing %s exception modes.\n%s' %
1974 warn('Error changing %s exception modes.\n%s' %
1960 (name,sys.exc_info()[1]))
1975 (name,sys.exc_info()[1]))
1961
1976
1962 shell = self.shell
1977 shell = self.shell
1963 new_mode = parameter_s.strip().capitalize()
1978 new_mode = parameter_s.strip().capitalize()
1964 try:
1979 try:
1965 shell.InteractiveTB.set_mode(mode=new_mode)
1980 shell.InteractiveTB.set_mode(mode=new_mode)
1966 print 'Exception reporting mode:',shell.InteractiveTB.mode
1981 print 'Exception reporting mode:',shell.InteractiveTB.mode
1967 except:
1982 except:
1968 xmode_switch_err('user')
1983 xmode_switch_err('user')
1969
1984
1970 # threaded shells use a special handler in sys.excepthook
1985 # threaded shells use a special handler in sys.excepthook
1971 if shell.isthreaded:
1986 if shell.isthreaded:
1972 try:
1987 try:
1973 shell.sys_excepthook.set_mode(mode=new_mode)
1988 shell.sys_excepthook.set_mode(mode=new_mode)
1974 except:
1989 except:
1975 xmode_switch_err('threaded')
1990 xmode_switch_err('threaded')
1976
1991
1977 def magic_colors(self,parameter_s = ''):
1992 def magic_colors(self,parameter_s = ''):
1978 """Switch color scheme for prompts, info system and exception handlers.
1993 """Switch color scheme for prompts, info system and exception handlers.
1979
1994
1980 Currently implemented schemes: NoColor, Linux, LightBG.
1995 Currently implemented schemes: NoColor, Linux, LightBG.
1981
1996
1982 Color scheme names are not case-sensitive."""
1997 Color scheme names are not case-sensitive."""
1983
1998
1984 def color_switch_err(name):
1999 def color_switch_err(name):
1985 warn('Error changing %s color schemes.\n%s' %
2000 warn('Error changing %s color schemes.\n%s' %
1986 (name,sys.exc_info()[1]))
2001 (name,sys.exc_info()[1]))
1987
2002
1988
2003
1989 new_scheme = parameter_s.strip()
2004 new_scheme = parameter_s.strip()
1990 if not new_scheme:
2005 if not new_scheme:
1991 print 'You must specify a color scheme.'
2006 print 'You must specify a color scheme.'
1992 return
2007 return
1993 # Under Windows, check for Gary Bishop's readline, which is necessary
2008 # Under Windows, check for Gary Bishop's readline, which is necessary
1994 # for ANSI coloring
2009 # for ANSI coloring
1995 if os.name in ['nt','dos']:
2010 if os.name in ['nt','dos']:
1996 try:
2011 try:
1997 import readline
2012 import readline
1998 except ImportError:
2013 except ImportError:
1999 has_readline = 0
2014 has_readline = 0
2000 else:
2015 else:
2001 try:
2016 try:
2002 readline.GetOutputFile()
2017 readline.GetOutputFile()
2003 except AttributeError:
2018 except AttributeError:
2004 has_readline = 0
2019 has_readline = 0
2005 else:
2020 else:
2006 has_readline = 1
2021 has_readline = 1
2007 if not has_readline:
2022 if not has_readline:
2008 msg = """\
2023 msg = """\
2009 Proper color support under MS Windows requires Gary Bishop's readline library.
2024 Proper color support under MS Windows requires Gary Bishop's readline library.
2010 You can find it at:
2025 You can find it at:
2011 http://sourceforge.net/projects/uncpythontools
2026 http://sourceforge.net/projects/uncpythontools
2012 Gary's readline needs the ctypes module, from:
2027 Gary's readline needs the ctypes module, from:
2013 http://starship.python.net/crew/theller/ctypes
2028 http://starship.python.net/crew/theller/ctypes
2014
2029
2015 Defaulting color scheme to 'NoColor'"""
2030 Defaulting color scheme to 'NoColor'"""
2016 new_scheme = 'NoColor'
2031 new_scheme = 'NoColor'
2017 warn(msg)
2032 warn(msg)
2018 # local shortcut
2033 # local shortcut
2019 shell = self.shell
2034 shell = self.shell
2020
2035
2021 # Set prompt colors
2036 # Set prompt colors
2022 try:
2037 try:
2023 shell.outputcache.set_colors(new_scheme)
2038 shell.outputcache.set_colors(new_scheme)
2024 except:
2039 except:
2025 color_switch_err('prompt')
2040 color_switch_err('prompt')
2026 else:
2041 else:
2027 shell.rc.colors = \
2042 shell.rc.colors = \
2028 shell.outputcache.color_table.active_scheme_name
2043 shell.outputcache.color_table.active_scheme_name
2029 # Set exception colors
2044 # Set exception colors
2030 try:
2045 try:
2031 shell.InteractiveTB.set_colors(scheme = new_scheme)
2046 shell.InteractiveTB.set_colors(scheme = new_scheme)
2032 shell.SyntaxTB.set_colors(scheme = new_scheme)
2047 shell.SyntaxTB.set_colors(scheme = new_scheme)
2033 except:
2048 except:
2034 color_switch_err('exception')
2049 color_switch_err('exception')
2035
2050
2036 # threaded shells use a verbose traceback in sys.excepthook
2051 # threaded shells use a verbose traceback in sys.excepthook
2037 if shell.isthreaded:
2052 if shell.isthreaded:
2038 try:
2053 try:
2039 shell.sys_excepthook.set_colors(scheme=new_scheme)
2054 shell.sys_excepthook.set_colors(scheme=new_scheme)
2040 except:
2055 except:
2041 color_switch_err('system exception handler')
2056 color_switch_err('system exception handler')
2042
2057
2043 # Set info (for 'object?') colors
2058 # Set info (for 'object?') colors
2044 if shell.rc.color_info:
2059 if shell.rc.color_info:
2045 try:
2060 try:
2046 shell.inspector.set_active_scheme(new_scheme)
2061 shell.inspector.set_active_scheme(new_scheme)
2047 except:
2062 except:
2048 color_switch_err('object inspector')
2063 color_switch_err('object inspector')
2049 else:
2064 else:
2050 shell.inspector.set_active_scheme('NoColor')
2065 shell.inspector.set_active_scheme('NoColor')
2051
2066
2052 def magic_color_info(self,parameter_s = ''):
2067 def magic_color_info(self,parameter_s = ''):
2053 """Toggle color_info.
2068 """Toggle color_info.
2054
2069
2055 The color_info configuration parameter controls whether colors are
2070 The color_info configuration parameter controls whether colors are
2056 used for displaying object details (by things like %psource, %pfile or
2071 used for displaying object details (by things like %psource, %pfile or
2057 the '?' system). This function toggles this value with each call.
2072 the '?' system). This function toggles this value with each call.
2058
2073
2059 Note that unless you have a fairly recent pager (less works better
2074 Note that unless you have a fairly recent pager (less works better
2060 than more) in your system, using colored object information displays
2075 than more) in your system, using colored object information displays
2061 will not work properly. Test it and see."""
2076 will not work properly. Test it and see."""
2062
2077
2063 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2078 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2064 self.magic_colors(self.shell.rc.colors)
2079 self.magic_colors(self.shell.rc.colors)
2065 print 'Object introspection functions have now coloring:',
2080 print 'Object introspection functions have now coloring:',
2066 print ['OFF','ON'][self.shell.rc.color_info]
2081 print ['OFF','ON'][self.shell.rc.color_info]
2067
2082
2068 def magic_Pprint(self, parameter_s=''):
2083 def magic_Pprint(self, parameter_s=''):
2069 """Toggle pretty printing on/off."""
2084 """Toggle pretty printing on/off."""
2070
2085
2071 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
2086 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
2072 print 'Pretty printing has been turned', \
2087 print 'Pretty printing has been turned', \
2073 ['OFF','ON'][self.shell.outputcache.Pprint]
2088 ['OFF','ON'][self.shell.outputcache.Pprint]
2074
2089
2075 def magic_exit(self, parameter_s=''):
2090 def magic_exit(self, parameter_s=''):
2076 """Exit IPython, confirming if configured to do so.
2091 """Exit IPython, confirming if configured to do so.
2077
2092
2078 You can configure whether IPython asks for confirmation upon exit by
2093 You can configure whether IPython asks for confirmation upon exit by
2079 setting the confirm_exit flag in the ipythonrc file."""
2094 setting the confirm_exit flag in the ipythonrc file."""
2080
2095
2081 self.shell.exit()
2096 self.shell.exit()
2082
2097
2083 def magic_quit(self, parameter_s=''):
2098 def magic_quit(self, parameter_s=''):
2084 """Exit IPython, confirming if configured to do so (like %exit)"""
2099 """Exit IPython, confirming if configured to do so (like %exit)"""
2085
2100
2086 self.shell.exit()
2101 self.shell.exit()
2087
2102
2088 def magic_Exit(self, parameter_s=''):
2103 def magic_Exit(self, parameter_s=''):
2089 """Exit IPython without confirmation."""
2104 """Exit IPython without confirmation."""
2090
2105
2091 self.shell.exit_now = True
2106 self.shell.exit_now = True
2092
2107
2093 def magic_Quit(self, parameter_s=''):
2108 def magic_Quit(self, parameter_s=''):
2094 """Exit IPython without confirmation (like %Exit)."""
2109 """Exit IPython without confirmation (like %Exit)."""
2095
2110
2096 self.shell.exit_now = True
2111 self.shell.exit_now = True
2097
2112
2098 #......................................................................
2113 #......................................................................
2099 # Functions to implement unix shell-type things
2114 # Functions to implement unix shell-type things
2100
2115
2101 def magic_alias(self, parameter_s = ''):
2116 def magic_alias(self, parameter_s = ''):
2102 """Define an alias for a system command.
2117 """Define an alias for a system command.
2103
2118
2104 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2119 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2105
2120
2106 Then, typing 'alias_name params' will execute the system command 'cmd
2121 Then, typing 'alias_name params' will execute the system command 'cmd
2107 params' (from your underlying operating system).
2122 params' (from your underlying operating system).
2108
2123
2109 Aliases have lower precedence than magic functions and Python normal
2124 Aliases have lower precedence than magic functions and Python normal
2110 variables, so if 'foo' is both a Python variable and an alias, the
2125 variables, so if 'foo' is both a Python variable and an alias, the
2111 alias can not be executed until 'del foo' removes the Python variable.
2126 alias can not be executed until 'del foo' removes the Python variable.
2112
2127
2113 You can use the %l specifier in an alias definition to represent the
2128 You can use the %l specifier in an alias definition to represent the
2114 whole line when the alias is called. For example:
2129 whole line when the alias is called. For example:
2115
2130
2116 In [2]: alias all echo "Input in brackets: <%l>"\\
2131 In [2]: alias all echo "Input in brackets: <%l>"\\
2117 In [3]: all hello world\\
2132 In [3]: all hello world\\
2118 Input in brackets: <hello world>
2133 Input in brackets: <hello world>
2119
2134
2120 You can also define aliases with parameters using %s specifiers (one
2135 You can also define aliases with parameters using %s specifiers (one
2121 per parameter):
2136 per parameter):
2122
2137
2123 In [1]: alias parts echo first %s second %s\\
2138 In [1]: alias parts echo first %s second %s\\
2124 In [2]: %parts A B\\
2139 In [2]: %parts A B\\
2125 first A second B\\
2140 first A second B\\
2126 In [3]: %parts A\\
2141 In [3]: %parts A\\
2127 Incorrect number of arguments: 2 expected.\\
2142 Incorrect number of arguments: 2 expected.\\
2128 parts is an alias to: 'echo first %s second %s'
2143 parts is an alias to: 'echo first %s second %s'
2129
2144
2130 Note that %l and %s are mutually exclusive. You can only use one or
2145 Note that %l and %s are mutually exclusive. You can only use one or
2131 the other in your aliases.
2146 the other in your aliases.
2132
2147
2133 Aliases expand Python variables just like system calls using ! or !!
2148 Aliases expand Python variables just like system calls using ! or !!
2134 do: all expressions prefixed with '$' get expanded. For details of
2149 do: all expressions prefixed with '$' get expanded. For details of
2135 the semantic rules, see PEP-215:
2150 the semantic rules, see PEP-215:
2136 http://www.python.org/peps/pep-0215.html. This is the library used by
2151 http://www.python.org/peps/pep-0215.html. This is the library used by
2137 IPython for variable expansion. If you want to access a true shell
2152 IPython for variable expansion. If you want to access a true shell
2138 variable, an extra $ is necessary to prevent its expansion by IPython:
2153 variable, an extra $ is necessary to prevent its expansion by IPython:
2139
2154
2140 In [6]: alias show echo\\
2155 In [6]: alias show echo\\
2141 In [7]: PATH='A Python string'\\
2156 In [7]: PATH='A Python string'\\
2142 In [8]: show $PATH\\
2157 In [8]: show $PATH\\
2143 A Python string\\
2158 A Python string\\
2144 In [9]: show $$PATH\\
2159 In [9]: show $$PATH\\
2145 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2160 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2146
2161
2147 You can use the alias facility to acess all of $PATH. See the %rehash
2162 You can use the alias facility to acess all of $PATH. See the %rehash
2148 and %rehashx functions, which automatically create aliases for the
2163 and %rehashx functions, which automatically create aliases for the
2149 contents of your $PATH.
2164 contents of your $PATH.
2150
2165
2151 If called with no parameters, %alias prints the current alias table."""
2166 If called with no parameters, %alias prints the current alias table."""
2152
2167
2153 par = parameter_s.strip()
2168 par = parameter_s.strip()
2154 if not par:
2169 if not par:
2155 if self.shell.rc.automagic:
2170 if self.shell.rc.automagic:
2156 prechar = ''
2171 prechar = ''
2157 else:
2172 else:
2158 prechar = self.shell.ESC_MAGIC
2173 prechar = self.shell.ESC_MAGIC
2159 print 'Alias\t\tSystem Command\n'+'-'*30
2174 print 'Alias\t\tSystem Command\n'+'-'*30
2160 atab = self.shell.alias_table
2175 atab = self.shell.alias_table
2161 aliases = atab.keys()
2176 aliases = atab.keys()
2162 aliases.sort()
2177 aliases.sort()
2163 for alias in aliases:
2178 for alias in aliases:
2164 print prechar+alias+'\t\t'+atab[alias][1]
2179 print prechar+alias+'\t\t'+atab[alias][1]
2165 print '-'*30+'\nTotal number of aliases:',len(aliases)
2180 print '-'*30+'\nTotal number of aliases:',len(aliases)
2166 return
2181 return
2167 try:
2182 try:
2168 alias,cmd = par.split(None,1)
2183 alias,cmd = par.split(None,1)
2169 except:
2184 except:
2170 print OInspect.getdoc(self.magic_alias)
2185 print OInspect.getdoc(self.magic_alias)
2171 else:
2186 else:
2172 nargs = cmd.count('%s')
2187 nargs = cmd.count('%s')
2173 if nargs>0 and cmd.find('%l')>=0:
2188 if nargs>0 and cmd.find('%l')>=0:
2174 error('The %s and %l specifiers are mutually exclusive '
2189 error('The %s and %l specifiers are mutually exclusive '
2175 'in alias definitions.')
2190 'in alias definitions.')
2176 else: # all looks OK
2191 else: # all looks OK
2177 self.shell.alias_table[alias] = (nargs,cmd)
2192 self.shell.alias_table[alias] = (nargs,cmd)
2178 self.shell.alias_table_validate(verbose=1)
2193 self.shell.alias_table_validate(verbose=1)
2179 # end magic_alias
2194 # end magic_alias
2180
2195
2181 def magic_unalias(self, parameter_s = ''):
2196 def magic_unalias(self, parameter_s = ''):
2182 """Remove an alias"""
2197 """Remove an alias"""
2183
2198
2184 aname = parameter_s.strip()
2199 aname = parameter_s.strip()
2185 if aname in self.shell.alias_table:
2200 if aname in self.shell.alias_table:
2186 del self.shell.alias_table[aname]
2201 del self.shell.alias_table[aname]
2187
2202
2188 def magic_rehash(self, parameter_s = ''):
2203 def magic_rehash(self, parameter_s = ''):
2189 """Update the alias table with all entries in $PATH.
2204 """Update the alias table with all entries in $PATH.
2190
2205
2191 This version does no checks on execute permissions or whether the
2206 This version does no checks on execute permissions or whether the
2192 contents of $PATH are truly files (instead of directories or something
2207 contents of $PATH are truly files (instead of directories or something
2193 else). For such a safer (but slower) version, use %rehashx."""
2208 else). For such a safer (but slower) version, use %rehashx."""
2194
2209
2195 # This function (and rehashx) manipulate the alias_table directly
2210 # This function (and rehashx) manipulate the alias_table directly
2196 # rather than calling magic_alias, for speed reasons. A rehash on a
2211 # rather than calling magic_alias, for speed reasons. A rehash on a
2197 # typical Linux box involves several thousand entries, so efficiency
2212 # typical Linux box involves several thousand entries, so efficiency
2198 # here is a top concern.
2213 # here is a top concern.
2199
2214
2200 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2215 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2201 alias_table = self.shell.alias_table
2216 alias_table = self.shell.alias_table
2202 for pdir in path:
2217 for pdir in path:
2203 for ff in os.listdir(pdir):
2218 for ff in os.listdir(pdir):
2204 # each entry in the alias table must be (N,name), where
2219 # each entry in the alias table must be (N,name), where
2205 # N is the number of positional arguments of the alias.
2220 # N is the number of positional arguments of the alias.
2206 alias_table[ff] = (0,ff)
2221 alias_table[ff] = (0,ff)
2207 # Make sure the alias table doesn't contain keywords or builtins
2222 # Make sure the alias table doesn't contain keywords or builtins
2208 self.shell.alias_table_validate()
2223 self.shell.alias_table_validate()
2209 # Call again init_auto_alias() so we get 'rm -i' and other modified
2224 # Call again init_auto_alias() so we get 'rm -i' and other modified
2210 # aliases since %rehash will probably clobber them
2225 # aliases since %rehash will probably clobber them
2211 self.shell.init_auto_alias()
2226 self.shell.init_auto_alias()
2212
2227
2213 def magic_rehashx(self, parameter_s = ''):
2228 def magic_rehashx(self, parameter_s = ''):
2214 """Update the alias table with all executable files in $PATH.
2229 """Update the alias table with all executable files in $PATH.
2215
2230
2216 This version explicitly checks that every entry in $PATH is a file
2231 This version explicitly checks that every entry in $PATH is a file
2217 with execute access (os.X_OK), so it is much slower than %rehash.
2232 with execute access (os.X_OK), so it is much slower than %rehash.
2218
2233
2219 Under Windows, it checks executability as a match agains a
2234 Under Windows, it checks executability as a match agains a
2220 '|'-separated string of extensions, stored in the IPython config
2235 '|'-separated string of extensions, stored in the IPython config
2221 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2236 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2222
2237
2223 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2238 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2224 alias_table = self.shell.alias_table
2239 alias_table = self.shell.alias_table
2225
2240
2226 if os.name == 'posix':
2241 if os.name == 'posix':
2227 isexec = lambda fname:os.path.isfile(fname) and \
2242 isexec = lambda fname:os.path.isfile(fname) and \
2228 os.access(fname,os.X_OK)
2243 os.access(fname,os.X_OK)
2229 else:
2244 else:
2230
2245
2231 try:
2246 try:
2232 winext = os.environ['pathext'].replace(';','|').replace('.','')
2247 winext = os.environ['pathext'].replace(';','|').replace('.','')
2233 except KeyError:
2248 except KeyError:
2234 winext = 'exe|com|bat'
2249 winext = 'exe|com|bat'
2235
2250
2236 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2251 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2237 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2252 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2238 savedir = os.getcwd()
2253 savedir = os.getcwd()
2239 try:
2254 try:
2240 # write the whole loop for posix/Windows so we don't have an if in
2255 # write the whole loop for posix/Windows so we don't have an if in
2241 # the innermost part
2256 # the innermost part
2242 if os.name == 'posix':
2257 if os.name == 'posix':
2243 for pdir in path:
2258 for pdir in path:
2244 os.chdir(pdir)
2259 os.chdir(pdir)
2245 for ff in os.listdir(pdir):
2260 for ff in os.listdir(pdir):
2246 if isexec(ff):
2261 if isexec(ff):
2247 # each entry in the alias table must be (N,name),
2262 # each entry in the alias table must be (N,name),
2248 # where N is the number of positional arguments of the
2263 # where N is the number of positional arguments of the
2249 # alias.
2264 # alias.
2250 alias_table[ff] = (0,ff)
2265 alias_table[ff] = (0,ff)
2251 else:
2266 else:
2252 for pdir in path:
2267 for pdir in path:
2253 os.chdir(pdir)
2268 os.chdir(pdir)
2254 for ff in os.listdir(pdir):
2269 for ff in os.listdir(pdir):
2255 if isexec(ff):
2270 if isexec(ff):
2256 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2271 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2257 # Make sure the alias table doesn't contain keywords or builtins
2272 # Make sure the alias table doesn't contain keywords or builtins
2258 self.shell.alias_table_validate()
2273 self.shell.alias_table_validate()
2259 # Call again init_auto_alias() so we get 'rm -i' and other
2274 # Call again init_auto_alias() so we get 'rm -i' and other
2260 # modified aliases since %rehashx will probably clobber them
2275 # modified aliases since %rehashx will probably clobber them
2261 self.shell.init_auto_alias()
2276 self.shell.init_auto_alias()
2262 finally:
2277 finally:
2263 os.chdir(savedir)
2278 os.chdir(savedir)
2264
2279
2265 def magic_pwd(self, parameter_s = ''):
2280 def magic_pwd(self, parameter_s = ''):
2266 """Return the current working directory path."""
2281 """Return the current working directory path."""
2267 return os.getcwd()
2282 return os.getcwd()
2268
2283
2269 def magic_cd(self, parameter_s=''):
2284 def magic_cd(self, parameter_s=''):
2270 """Change the current working directory.
2285 """Change the current working directory.
2271
2286
2272 This command automatically maintains an internal list of directories
2287 This command automatically maintains an internal list of directories
2273 you visit during your IPython session, in the variable _dh. The
2288 you visit during your IPython session, in the variable _dh. The
2274 command %dhist shows this history nicely formatted.
2289 command %dhist shows this history nicely formatted.
2275
2290
2276 Usage:
2291 Usage:
2277
2292
2278 cd 'dir': changes to directory 'dir'.
2293 cd 'dir': changes to directory 'dir'.
2279
2294
2280 cd -: changes to the last visited directory.
2295 cd -: changes to the last visited directory.
2281
2296
2282 cd -<n>: changes to the n-th directory in the directory history.
2297 cd -<n>: changes to the n-th directory in the directory history.
2283
2298
2284 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2299 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2285 (note: cd <bookmark_name> is enough if there is no
2300 (note: cd <bookmark_name> is enough if there is no
2286 directory <bookmark_name>, but a bookmark with the name exists.)
2301 directory <bookmark_name>, but a bookmark with the name exists.)
2287
2302
2288 Options:
2303 Options:
2289
2304
2290 -q: quiet. Do not print the working directory after the cd command is
2305 -q: quiet. Do not print the working directory after the cd command is
2291 executed. By default IPython's cd command does print this directory,
2306 executed. By default IPython's cd command does print this directory,
2292 since the default prompts do not display path information.
2307 since the default prompts do not display path information.
2293
2308
2294 Note that !cd doesn't work for this purpose because the shell where
2309 Note that !cd doesn't work for this purpose because the shell where
2295 !command runs is immediately discarded after executing 'command'."""
2310 !command runs is immediately discarded after executing 'command'."""
2296
2311
2297 parameter_s = parameter_s.strip()
2312 parameter_s = parameter_s.strip()
2298 bkms = self.shell.persist.get("bookmarks",{})
2313 bkms = self.shell.persist.get("bookmarks",{})
2299
2314
2300 numcd = re.match(r'(-)(\d+)$',parameter_s)
2315 numcd = re.match(r'(-)(\d+)$',parameter_s)
2301 # jump in directory history by number
2316 # jump in directory history by number
2302 if numcd:
2317 if numcd:
2303 nn = int(numcd.group(2))
2318 nn = int(numcd.group(2))
2304 try:
2319 try:
2305 ps = self.shell.user_ns['_dh'][nn]
2320 ps = self.shell.user_ns['_dh'][nn]
2306 except IndexError:
2321 except IndexError:
2307 print 'The requested directory does not exist in history.'
2322 print 'The requested directory does not exist in history.'
2308 return
2323 return
2309 else:
2324 else:
2310 opts = {}
2325 opts = {}
2311 else:
2326 else:
2312 #turn all non-space-escaping backslashes to slashes,
2327 #turn all non-space-escaping backslashes to slashes,
2313 # for c:\windows\directory\names\
2328 # for c:\windows\directory\names\
2314 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2329 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2315 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2330 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2316 # jump to previous
2331 # jump to previous
2317 if ps == '-':
2332 if ps == '-':
2318 try:
2333 try:
2319 ps = self.shell.user_ns['_dh'][-2]
2334 ps = self.shell.user_ns['_dh'][-2]
2320 except IndexError:
2335 except IndexError:
2321 print 'No previous directory to change to.'
2336 print 'No previous directory to change to.'
2322 return
2337 return
2323 # jump to bookmark
2338 # jump to bookmark
2324 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2339 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2325 if bkms.has_key(ps):
2340 if bkms.has_key(ps):
2326 target = bkms[ps]
2341 target = bkms[ps]
2327 print '(bookmark:%s) -> %s' % (ps,target)
2342 print '(bookmark:%s) -> %s' % (ps,target)
2328 ps = target
2343 ps = target
2329 else:
2344 else:
2330 if bkms:
2345 if bkms:
2331 error("Bookmark '%s' not found. "
2346 error("Bookmark '%s' not found. "
2332 "Use '%%bookmark -l' to see your bookmarks." % ps)
2347 "Use '%%bookmark -l' to see your bookmarks." % ps)
2333 else:
2348 else:
2334 print "Bookmarks not set - use %bookmark <bookmarkname>"
2349 print "Bookmarks not set - use %bookmark <bookmarkname>"
2335 return
2350 return
2336
2351
2337 # at this point ps should point to the target dir
2352 # at this point ps should point to the target dir
2338 if ps:
2353 if ps:
2339 try:
2354 try:
2340 os.chdir(os.path.expanduser(ps))
2355 os.chdir(os.path.expanduser(ps))
2341 ttitle = ("IPy:" + (
2356 ttitle = ("IPy:" + (
2342 os.getcwd() == '/' and '/' or os.path.basename(os.getcwd())))
2357 os.getcwd() == '/' and '/' or os.path.basename(os.getcwd())))
2343 platutils.set_term_title(ttitle)
2358 platutils.set_term_title(ttitle)
2344 except OSError:
2359 except OSError:
2345 print sys.exc_info()[1]
2360 print sys.exc_info()[1]
2346 else:
2361 else:
2347 self.shell.user_ns['_dh'].append(os.getcwd())
2362 self.shell.user_ns['_dh'].append(os.getcwd())
2348 else:
2363 else:
2349 os.chdir(self.shell.home_dir)
2364 os.chdir(self.shell.home_dir)
2350 platutils.set_term_title("IPy:~")
2365 platutils.set_term_title("IPy:~")
2351 self.shell.user_ns['_dh'].append(os.getcwd())
2366 self.shell.user_ns['_dh'].append(os.getcwd())
2352 if not 'q' in opts:
2367 if not 'q' in opts:
2353 print self.shell.user_ns['_dh'][-1]
2368 print self.shell.user_ns['_dh'][-1]
2354
2369
2355 def magic_dhist(self, parameter_s=''):
2370 def magic_dhist(self, parameter_s=''):
2356 """Print your history of visited directories.
2371 """Print your history of visited directories.
2357
2372
2358 %dhist -> print full history\\
2373 %dhist -> print full history\\
2359 %dhist n -> print last n entries only\\
2374 %dhist n -> print last n entries only\\
2360 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2375 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2361
2376
2362 This history is automatically maintained by the %cd command, and
2377 This history is automatically maintained by the %cd command, and
2363 always available as the global list variable _dh. You can use %cd -<n>
2378 always available as the global list variable _dh. You can use %cd -<n>
2364 to go to directory number <n>."""
2379 to go to directory number <n>."""
2365
2380
2366 dh = self.shell.user_ns['_dh']
2381 dh = self.shell.user_ns['_dh']
2367 if parameter_s:
2382 if parameter_s:
2368 try:
2383 try:
2369 args = map(int,parameter_s.split())
2384 args = map(int,parameter_s.split())
2370 except:
2385 except:
2371 self.arg_err(Magic.magic_dhist)
2386 self.arg_err(Magic.magic_dhist)
2372 return
2387 return
2373 if len(args) == 1:
2388 if len(args) == 1:
2374 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2389 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2375 elif len(args) == 2:
2390 elif len(args) == 2:
2376 ini,fin = args
2391 ini,fin = args
2377 else:
2392 else:
2378 self.arg_err(Magic.magic_dhist)
2393 self.arg_err(Magic.magic_dhist)
2379 return
2394 return
2380 else:
2395 else:
2381 ini,fin = 0,len(dh)
2396 ini,fin = 0,len(dh)
2382 nlprint(dh,
2397 nlprint(dh,
2383 header = 'Directory history (kept in _dh)',
2398 header = 'Directory history (kept in _dh)',
2384 start=ini,stop=fin)
2399 start=ini,stop=fin)
2385
2400
2386 def magic_env(self, parameter_s=''):
2401 def magic_env(self, parameter_s=''):
2387 """List environment variables."""
2402 """List environment variables."""
2388
2403
2389 return os.environ.data
2404 return os.environ.data
2390
2405
2391 def magic_pushd(self, parameter_s=''):
2406 def magic_pushd(self, parameter_s=''):
2392 """Place the current dir on stack and change directory.
2407 """Place the current dir on stack and change directory.
2393
2408
2394 Usage:\\
2409 Usage:\\
2395 %pushd ['dirname']
2410 %pushd ['dirname']
2396
2411
2397 %pushd with no arguments does a %pushd to your home directory.
2412 %pushd with no arguments does a %pushd to your home directory.
2398 """
2413 """
2399 if parameter_s == '': parameter_s = '~'
2414 if parameter_s == '': parameter_s = '~'
2400 dir_s = self.shell.dir_stack
2415 dir_s = self.shell.dir_stack
2401 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2416 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2402 os.path.expanduser(self.shell.dir_stack[0]):
2417 os.path.expanduser(self.shell.dir_stack[0]):
2403 try:
2418 try:
2404 self.magic_cd(parameter_s)
2419 self.magic_cd(parameter_s)
2405 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2420 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2406 self.magic_dirs()
2421 self.magic_dirs()
2407 except:
2422 except:
2408 print 'Invalid directory'
2423 print 'Invalid directory'
2409 else:
2424 else:
2410 print 'You are already there!'
2425 print 'You are already there!'
2411
2426
2412 def magic_popd(self, parameter_s=''):
2427 def magic_popd(self, parameter_s=''):
2413 """Change to directory popped off the top of the stack.
2428 """Change to directory popped off the top of the stack.
2414 """
2429 """
2415 if len (self.shell.dir_stack) > 1:
2430 if len (self.shell.dir_stack) > 1:
2416 self.shell.dir_stack.pop(0)
2431 self.shell.dir_stack.pop(0)
2417 self.magic_cd(self.shell.dir_stack[0])
2432 self.magic_cd(self.shell.dir_stack[0])
2418 print self.shell.dir_stack[0]
2433 print self.shell.dir_stack[0]
2419 else:
2434 else:
2420 print "You can't remove the starting directory from the stack:",\
2435 print "You can't remove the starting directory from the stack:",\
2421 self.shell.dir_stack
2436 self.shell.dir_stack
2422
2437
2423 def magic_dirs(self, parameter_s=''):
2438 def magic_dirs(self, parameter_s=''):
2424 """Return the current directory stack."""
2439 """Return the current directory stack."""
2425
2440
2426 return self.shell.dir_stack[:]
2441 return self.shell.dir_stack[:]
2427
2442
2428 def magic_sc(self, parameter_s=''):
2443 def magic_sc(self, parameter_s=''):
2429 """Shell capture - execute a shell command and capture its output.
2444 """Shell capture - execute a shell command and capture its output.
2430
2445
2431 %sc [options] varname=command
2446 %sc [options] varname=command
2432
2447
2433 IPython will run the given command using commands.getoutput(), and
2448 IPython will run the given command using commands.getoutput(), and
2434 will then update the user's interactive namespace with a variable
2449 will then update the user's interactive namespace with a variable
2435 called varname, containing the value of the call. Your command can
2450 called varname, containing the value of the call. Your command can
2436 contain shell wildcards, pipes, etc.
2451 contain shell wildcards, pipes, etc.
2437
2452
2438 The '=' sign in the syntax is mandatory, and the variable name you
2453 The '=' sign in the syntax is mandatory, and the variable name you
2439 supply must follow Python's standard conventions for valid names.
2454 supply must follow Python's standard conventions for valid names.
2440
2455
2441 Options:
2456 Options:
2442
2457
2443 -l: list output. Split the output on newlines into a list before
2458 -l: list output. Split the output on newlines into a list before
2444 assigning it to the given variable. By default the output is stored
2459 assigning it to the given variable. By default the output is stored
2445 as a single string.
2460 as a single string.
2446
2461
2447 -v: verbose. Print the contents of the variable.
2462 -v: verbose. Print the contents of the variable.
2448
2463
2449 In most cases you should not need to split as a list, because the
2464 In most cases you should not need to split as a list, because the
2450 returned value is a special type of string which can automatically
2465 returned value is a special type of string which can automatically
2451 provide its contents either as a list (split on newlines) or as a
2466 provide its contents either as a list (split on newlines) or as a
2452 space-separated string. These are convenient, respectively, either
2467 space-separated string. These are convenient, respectively, either
2453 for sequential processing or to be passed to a shell command.
2468 for sequential processing or to be passed to a shell command.
2454
2469
2455 For example:
2470 For example:
2456
2471
2457 # Capture into variable a
2472 # Capture into variable a
2458 In [9]: sc a=ls *py
2473 In [9]: sc a=ls *py
2459
2474
2460 # a is a string with embedded newlines
2475 # a is a string with embedded newlines
2461 In [10]: a
2476 In [10]: a
2462 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2477 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2463
2478
2464 # which can be seen as a list:
2479 # which can be seen as a list:
2465 In [11]: a.l
2480 In [11]: a.l
2466 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2481 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2467
2482
2468 # or as a whitespace-separated string:
2483 # or as a whitespace-separated string:
2469 In [12]: a.s
2484 In [12]: a.s
2470 Out[12]: 'setup.py win32_manual_post_install.py'
2485 Out[12]: 'setup.py win32_manual_post_install.py'
2471
2486
2472 # a.s is useful to pass as a single command line:
2487 # a.s is useful to pass as a single command line:
2473 In [13]: !wc -l $a.s
2488 In [13]: !wc -l $a.s
2474 146 setup.py
2489 146 setup.py
2475 130 win32_manual_post_install.py
2490 130 win32_manual_post_install.py
2476 276 total
2491 276 total
2477
2492
2478 # while the list form is useful to loop over:
2493 # while the list form is useful to loop over:
2479 In [14]: for f in a.l:
2494 In [14]: for f in a.l:
2480 ....: !wc -l $f
2495 ....: !wc -l $f
2481 ....:
2496 ....:
2482 146 setup.py
2497 146 setup.py
2483 130 win32_manual_post_install.py
2498 130 win32_manual_post_install.py
2484
2499
2485 Similiarly, the lists returned by the -l option are also special, in
2500 Similiarly, the lists returned by the -l option are also special, in
2486 the sense that you can equally invoke the .s attribute on them to
2501 the sense that you can equally invoke the .s attribute on them to
2487 automatically get a whitespace-separated string from their contents:
2502 automatically get a whitespace-separated string from their contents:
2488
2503
2489 In [1]: sc -l b=ls *py
2504 In [1]: sc -l b=ls *py
2490
2505
2491 In [2]: b
2506 In [2]: b
2492 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2507 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2493
2508
2494 In [3]: b.s
2509 In [3]: b.s
2495 Out[3]: 'setup.py win32_manual_post_install.py'
2510 Out[3]: 'setup.py win32_manual_post_install.py'
2496
2511
2497 In summary, both the lists and strings used for ouptut capture have
2512 In summary, both the lists and strings used for ouptut capture have
2498 the following special attributes:
2513 the following special attributes:
2499
2514
2500 .l (or .list) : value as list.
2515 .l (or .list) : value as list.
2501 .n (or .nlstr): value as newline-separated string.
2516 .n (or .nlstr): value as newline-separated string.
2502 .s (or .spstr): value as space-separated string.
2517 .s (or .spstr): value as space-separated string.
2503 """
2518 """
2504
2519
2505 opts,args = self.parse_options(parameter_s,'lv')
2520 opts,args = self.parse_options(parameter_s,'lv')
2506 # Try to get a variable name and command to run
2521 # Try to get a variable name and command to run
2507 try:
2522 try:
2508 # the variable name must be obtained from the parse_options
2523 # the variable name must be obtained from the parse_options
2509 # output, which uses shlex.split to strip options out.
2524 # output, which uses shlex.split to strip options out.
2510 var,_ = args.split('=',1)
2525 var,_ = args.split('=',1)
2511 var = var.strip()
2526 var = var.strip()
2512 # But the the command has to be extracted from the original input
2527 # But the the command has to be extracted from the original input
2513 # parameter_s, not on what parse_options returns, to avoid the
2528 # parameter_s, not on what parse_options returns, to avoid the
2514 # quote stripping which shlex.split performs on it.
2529 # quote stripping which shlex.split performs on it.
2515 _,cmd = parameter_s.split('=',1)
2530 _,cmd = parameter_s.split('=',1)
2516 except ValueError:
2531 except ValueError:
2517 var,cmd = '',''
2532 var,cmd = '',''
2518 if not var:
2533 if not var:
2519 error('you must specify a variable to assign the command to.')
2534 error('you must specify a variable to assign the command to.')
2520 return
2535 return
2521 # If all looks ok, proceed
2536 # If all looks ok, proceed
2522 out,err = self.shell.getoutputerror(cmd)
2537 out,err = self.shell.getoutputerror(cmd)
2523 if err:
2538 if err:
2524 print >> Term.cerr,err
2539 print >> Term.cerr,err
2525 if opts.has_key('l'):
2540 if opts.has_key('l'):
2526 out = SList(out.split('\n'))
2541 out = SList(out.split('\n'))
2527 else:
2542 else:
2528 out = LSString(out)
2543 out = LSString(out)
2529 if opts.has_key('v'):
2544 if opts.has_key('v'):
2530 print '%s ==\n%s' % (var,pformat(out))
2545 print '%s ==\n%s' % (var,pformat(out))
2531 self.shell.user_ns.update({var:out})
2546 self.shell.user_ns.update({var:out})
2532
2547
2533 def magic_sx(self, parameter_s=''):
2548 def magic_sx(self, parameter_s=''):
2534 """Shell execute - run a shell command and capture its output.
2549 """Shell execute - run a shell command and capture its output.
2535
2550
2536 %sx command
2551 %sx command
2537
2552
2538 IPython will run the given command using commands.getoutput(), and
2553 IPython will run the given command using commands.getoutput(), and
2539 return the result formatted as a list (split on '\\n'). Since the
2554 return the result formatted as a list (split on '\\n'). Since the
2540 output is _returned_, it will be stored in ipython's regular output
2555 output is _returned_, it will be stored in ipython's regular output
2541 cache Out[N] and in the '_N' automatic variables.
2556 cache Out[N] and in the '_N' automatic variables.
2542
2557
2543 Notes:
2558 Notes:
2544
2559
2545 1) If an input line begins with '!!', then %sx is automatically
2560 1) If an input line begins with '!!', then %sx is automatically
2546 invoked. That is, while:
2561 invoked. That is, while:
2547 !ls
2562 !ls
2548 causes ipython to simply issue system('ls'), typing
2563 causes ipython to simply issue system('ls'), typing
2549 !!ls
2564 !!ls
2550 is a shorthand equivalent to:
2565 is a shorthand equivalent to:
2551 %sx ls
2566 %sx ls
2552
2567
2553 2) %sx differs from %sc in that %sx automatically splits into a list,
2568 2) %sx differs from %sc in that %sx automatically splits into a list,
2554 like '%sc -l'. The reason for this is to make it as easy as possible
2569 like '%sc -l'. The reason for this is to make it as easy as possible
2555 to process line-oriented shell output via further python commands.
2570 to process line-oriented shell output via further python commands.
2556 %sc is meant to provide much finer control, but requires more
2571 %sc is meant to provide much finer control, but requires more
2557 typing.
2572 typing.
2558
2573
2559 3) Just like %sc -l, this is a list with special attributes:
2574 3) Just like %sc -l, this is a list with special attributes:
2560
2575
2561 .l (or .list) : value as list.
2576 .l (or .list) : value as list.
2562 .n (or .nlstr): value as newline-separated string.
2577 .n (or .nlstr): value as newline-separated string.
2563 .s (or .spstr): value as whitespace-separated string.
2578 .s (or .spstr): value as whitespace-separated string.
2564
2579
2565 This is very useful when trying to use such lists as arguments to
2580 This is very useful when trying to use such lists as arguments to
2566 system commands."""
2581 system commands."""
2567
2582
2568 if parameter_s:
2583 if parameter_s:
2569 out,err = self.shell.getoutputerror(parameter_s)
2584 out,err = self.shell.getoutputerror(parameter_s)
2570 if err:
2585 if err:
2571 print >> Term.cerr,err
2586 print >> Term.cerr,err
2572 return SList(out.split('\n'))
2587 return SList(out.split('\n'))
2573
2588
2574 def magic_bg(self, parameter_s=''):
2589 def magic_bg(self, parameter_s=''):
2575 """Run a job in the background, in a separate thread.
2590 """Run a job in the background, in a separate thread.
2576
2591
2577 For example,
2592 For example,
2578
2593
2579 %bg myfunc(x,y,z=1)
2594 %bg myfunc(x,y,z=1)
2580
2595
2581 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2596 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2582 execution starts, a message will be printed indicating the job
2597 execution starts, a message will be printed indicating the job
2583 number. If your job number is 5, you can use
2598 number. If your job number is 5, you can use
2584
2599
2585 myvar = jobs.result(5) or myvar = jobs[5].result
2600 myvar = jobs.result(5) or myvar = jobs[5].result
2586
2601
2587 to assign this result to variable 'myvar'.
2602 to assign this result to variable 'myvar'.
2588
2603
2589 IPython has a job manager, accessible via the 'jobs' object. You can
2604 IPython has a job manager, accessible via the 'jobs' object. You can
2590 type jobs? to get more information about it, and use jobs.<TAB> to see
2605 type jobs? to get more information about it, and use jobs.<TAB> to see
2591 its attributes. All attributes not starting with an underscore are
2606 its attributes. All attributes not starting with an underscore are
2592 meant for public use.
2607 meant for public use.
2593
2608
2594 In particular, look at the jobs.new() method, which is used to create
2609 In particular, look at the jobs.new() method, which is used to create
2595 new jobs. This magic %bg function is just a convenience wrapper
2610 new jobs. This magic %bg function is just a convenience wrapper
2596 around jobs.new(), for expression-based jobs. If you want to create a
2611 around jobs.new(), for expression-based jobs. If you want to create a
2597 new job with an explicit function object and arguments, you must call
2612 new job with an explicit function object and arguments, you must call
2598 jobs.new() directly.
2613 jobs.new() directly.
2599
2614
2600 The jobs.new docstring also describes in detail several important
2615 The jobs.new docstring also describes in detail several important
2601 caveats associated with a thread-based model for background job
2616 caveats associated with a thread-based model for background job
2602 execution. Type jobs.new? for details.
2617 execution. Type jobs.new? for details.
2603
2618
2604 You can check the status of all jobs with jobs.status().
2619 You can check the status of all jobs with jobs.status().
2605
2620
2606 The jobs variable is set by IPython into the Python builtin namespace.
2621 The jobs variable is set by IPython into the Python builtin namespace.
2607 If you ever declare a variable named 'jobs', you will shadow this
2622 If you ever declare a variable named 'jobs', you will shadow this
2608 name. You can either delete your global jobs variable to regain
2623 name. You can either delete your global jobs variable to regain
2609 access to the job manager, or make a new name and assign it manually
2624 access to the job manager, or make a new name and assign it manually
2610 to the manager (stored in IPython's namespace). For example, to
2625 to the manager (stored in IPython's namespace). For example, to
2611 assign the job manager to the Jobs name, use:
2626 assign the job manager to the Jobs name, use:
2612
2627
2613 Jobs = __builtins__.jobs"""
2628 Jobs = __builtins__.jobs"""
2614
2629
2615 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2630 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2616
2631
2617 def magic_store(self, parameter_s=''):
2632 def magic_store(self, parameter_s=''):
2618 """Lightweight persistence for python variables.
2633 """Lightweight persistence for python variables.
2619
2634
2620 Example:
2635 Example:
2621
2636
2622 ville@badger[~]|1> A = ['hello',10,'world']\\
2637 ville@badger[~]|1> A = ['hello',10,'world']\\
2623 ville@badger[~]|2> %store A\\
2638 ville@badger[~]|2> %store A\\
2624 ville@badger[~]|3> Exit
2639 ville@badger[~]|3> Exit
2625
2640
2626 (IPython session is closed and started again...)
2641 (IPython session is closed and started again...)
2627
2642
2628 ville@badger:~$ ipython -p pysh\\
2643 ville@badger:~$ ipython -p pysh\\
2629 ville@badger[~]|1> print A
2644 ville@badger[~]|1> print A
2630
2645
2631 ['hello', 10, 'world']
2646 ['hello', 10, 'world']
2632
2647
2633 Usage:
2648 Usage:
2634
2649
2635 %store - Show list of all variables and their current values\\
2650 %store - Show list of all variables and their current values\\
2636 %store <var> - Store the *current* value of the variable to disk\\
2651 %store <var> - Store the *current* value of the variable to disk\\
2637 %store -d <var> - Remove the variable and its value from storage\\
2652 %store -d <var> - Remove the variable and its value from storage\\
2638 %store -r - Remove all variables from storage
2653 %store -r - Remove all variables from storage
2639
2654
2640 It should be noted that if you change the value of a variable, you
2655 It should be noted that if you change the value of a variable, you
2641 need to %store it again if you want to persist the new value.
2656 need to %store it again if you want to persist the new value.
2642
2657
2643 Note also that the variables will need to be pickleable; most basic
2658 Note also that the variables will need to be pickleable; most basic
2644 python types can be safely %stored.
2659 python types can be safely %stored.
2645 """
2660 """
2646
2661
2647 opts,args = self.parse_options(parameter_s,'dr',mode='list')
2662 opts,args = self.parse_options(parameter_s,'dr',mode='list')
2648 # delete
2663 # delete
2649 if opts.has_key('d'):
2664 if opts.has_key('d'):
2650 try:
2665 try:
2651 todel = args[0]
2666 todel = args[0]
2652 except IndexError:
2667 except IndexError:
2653 error('You must provide the variable to forget')
2668 error('You must provide the variable to forget')
2654 else:
2669 else:
2655 try:
2670 try:
2656 del self.shell.persist['S:' + todel]
2671 del self.shell.persist['S:' + todel]
2657 except:
2672 except:
2658 error("Can't delete variable '%s'" % todel)
2673 error("Can't delete variable '%s'" % todel)
2659 # reset
2674 # reset
2660 elif opts.has_key('r'):
2675 elif opts.has_key('r'):
2661 for k in self.shell.persist.keys():
2676 for k in self.shell.persist.keys():
2662 if k.startswith('S:'):
2677 if k.startswith('S:'):
2663 del self.shell.persist[k]
2678 del self.shell.persist[k]
2664
2679
2665 # run without arguments -> list variables & values
2680 # run without arguments -> list variables & values
2666 elif not args:
2681 elif not args:
2667 vars = [v[2:] for v in self.shell.persist.keys()
2682 vars = [v[2:] for v in self.shell.persist.keys()
2668 if v.startswith('S:')]
2683 if v.startswith('S:')]
2669 vars.sort()
2684 vars.sort()
2670 if vars:
2685 if vars:
2671 size = max(map(len,vars))
2686 size = max(map(len,vars))
2672 else:
2687 else:
2673 size = 0
2688 size = 0
2674
2689
2675 print 'Stored variables and their in-memory values:'
2690 print 'Stored variables and their in-memory values:'
2676 fmt = '%-'+str(size)+'s -> %s'
2691 fmt = '%-'+str(size)+'s -> %s'
2677 get = self.shell.user_ns.get
2692 get = self.shell.user_ns.get
2678 for var in vars:
2693 for var in vars:
2679 # print 30 first characters from every var
2694 # print 30 first characters from every var
2680 print fmt % (var,repr(get(var,'<unavailable>'))[:50])
2695 print fmt % (var,repr(get(var,'<unavailable>'))[:50])
2681
2696
2682 # default action - store the variable
2697 # default action - store the variable
2683 else:
2698 else:
2684 obj = self.shell.user_ns[args[0] ]
2699 obj = self.shell.user_ns[args[0] ]
2685 if isinstance(inspect.getmodule(obj), FakeModule):
2700 if isinstance(inspect.getmodule(obj), FakeModule):
2686 print textwrap.dedent("""\
2701 print textwrap.dedent("""\
2687 Warning:%s is %s
2702 Warning:%s is %s
2688 Proper storage of interactively declared classes (or instances
2703 Proper storage of interactively declared classes (or instances
2689 of those classes) is not possible! Only instances
2704 of those classes) is not possible! Only instances
2690 of classes in real modules on file system can be %%store'd.
2705 of classes in real modules on file system can be %%store'd.
2691 """ % (args[0], obj) )
2706 """ % (args[0], obj) )
2692 return
2707 return
2693 pickled = pickle.dumps(obj)
2708 pickled = pickle.dumps(obj)
2694 self.shell.persist[ 'S:' + args[0] ] = pickled
2709 self.shell.persist[ 'S:' + args[0] ] = pickled
2695 print "Stored '%s' (%d bytes)" % (args[0], len(pickled))
2710 print "Stored '%s' (%d bytes)" % (args[0], len(pickled))
2696
2711
2697 def magic_bookmark(self, parameter_s=''):
2712 def magic_bookmark(self, parameter_s=''):
2698 """Manage IPython's bookmark system.
2713 """Manage IPython's bookmark system.
2699
2714
2700 %bookmark <name> - set bookmark to current dir
2715 %bookmark <name> - set bookmark to current dir
2701 %bookmark <name> <dir> - set bookmark to <dir>
2716 %bookmark <name> <dir> - set bookmark to <dir>
2702 %bookmark -l - list all bookmarks
2717 %bookmark -l - list all bookmarks
2703 %bookmark -d <name> - remove bookmark
2718 %bookmark -d <name> - remove bookmark
2704 %bookmark -r - remove all bookmarks
2719 %bookmark -r - remove all bookmarks
2705
2720
2706 You can later on access a bookmarked folder with:
2721 You can later on access a bookmarked folder with:
2707 %cd -b <name>
2722 %cd -b <name>
2708 or simply '%cd <name>' if there is no directory called <name> AND
2723 or simply '%cd <name>' if there is no directory called <name> AND
2709 there is such a bookmark defined.
2724 there is such a bookmark defined.
2710
2725
2711 Your bookmarks persist through IPython sessions, but they are
2726 Your bookmarks persist through IPython sessions, but they are
2712 associated with each profile."""
2727 associated with each profile."""
2713
2728
2714 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2729 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2715 if len(args) > 2:
2730 if len(args) > 2:
2716 error('You can only give at most two arguments')
2731 error('You can only give at most two arguments')
2717 return
2732 return
2718
2733
2719 bkms = self.shell.persist.get('bookmarks',{})
2734 bkms = self.shell.persist.get('bookmarks',{})
2720
2735
2721 if opts.has_key('d'):
2736 if opts.has_key('d'):
2722 try:
2737 try:
2723 todel = args[0]
2738 todel = args[0]
2724 except IndexError:
2739 except IndexError:
2725 error('You must provide a bookmark to delete')
2740 error('You must provide a bookmark to delete')
2726 else:
2741 else:
2727 try:
2742 try:
2728 del bkms[todel]
2743 del bkms[todel]
2729 except:
2744 except:
2730 error("Can't delete bookmark '%s'" % todel)
2745 error("Can't delete bookmark '%s'" % todel)
2731 elif opts.has_key('r'):
2746 elif opts.has_key('r'):
2732 bkms = {}
2747 bkms = {}
2733 elif opts.has_key('l'):
2748 elif opts.has_key('l'):
2734 bks = bkms.keys()
2749 bks = bkms.keys()
2735 bks.sort()
2750 bks.sort()
2736 if bks:
2751 if bks:
2737 size = max(map(len,bks))
2752 size = max(map(len,bks))
2738 else:
2753 else:
2739 size = 0
2754 size = 0
2740 fmt = '%-'+str(size)+'s -> %s'
2755 fmt = '%-'+str(size)+'s -> %s'
2741 print 'Current bookmarks:'
2756 print 'Current bookmarks:'
2742 for bk in bks:
2757 for bk in bks:
2743 print fmt % (bk,bkms[bk])
2758 print fmt % (bk,bkms[bk])
2744 else:
2759 else:
2745 if not args:
2760 if not args:
2746 error("You must specify the bookmark name")
2761 error("You must specify the bookmark name")
2747 elif len(args)==1:
2762 elif len(args)==1:
2748 bkms[args[0]] = os.getcwd()
2763 bkms[args[0]] = os.getcwd()
2749 elif len(args)==2:
2764 elif len(args)==2:
2750 bkms[args[0]] = args[1]
2765 bkms[args[0]] = args[1]
2751 self.shell.persist['bookmarks'] = bkms
2766 self.shell.persist['bookmarks'] = bkms
2752
2767
2753 def magic_pycat(self, parameter_s=''):
2768 def magic_pycat(self, parameter_s=''):
2754 """Show a syntax-highlighted file through a pager.
2769 """Show a syntax-highlighted file through a pager.
2755
2770
2756 This magic is similar to the cat utility, but it will assume the file
2771 This magic is similar to the cat utility, but it will assume the file
2757 to be Python source and will show it with syntax highlighting. """
2772 to be Python source and will show it with syntax highlighting. """
2758
2773
2759 filename = get_py_filename(parameter_s)
2774 filename = get_py_filename(parameter_s)
2760 page(self.shell.pycolorize(file_read(filename)),
2775 page(self.shell.pycolorize(file_read(filename)),
2761 screen_lines=self.shell.rc.screen_length)
2776 screen_lines=self.shell.rc.screen_length)
2762
2777
2763 def magic_cpaste(self, parameter_s=''):
2778 def magic_cpaste(self, parameter_s=''):
2764 """Allows you to paste & execute a pre-formatted code block from
2779 """Allows you to paste & execute a pre-formatted code block from
2765 clipboard.
2780 clipboard.
2766
2781
2767 You must terminate the block with '--' (two minus-signs) alone on the
2782 You must terminate the block with '--' (two minus-signs) alone on the
2768 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2783 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2769 is the new sentinel for this operation)
2784 is the new sentinel for this operation)
2770
2785
2771 The block is dedented prior to execution to enable execution of
2786 The block is dedented prior to execution to enable execution of
2772 method definitions. The executed block is also assigned to variable
2787 method definitions. The executed block is also assigned to variable
2773 named 'pasted_block' for later editing with '%edit pasted_block'.
2788 named 'pasted_block' for later editing with '%edit pasted_block'.
2774
2789
2775 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2790 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2776 This assigns the pasted block to variable 'foo' as string, without
2791 This assigns the pasted block to variable 'foo' as string, without
2777 dedenting or executing it.
2792 dedenting or executing it.
2778
2793
2779 Do not be alarmed by garbled output on Windows (it's a readline bug).
2794 Do not be alarmed by garbled output on Windows (it's a readline bug).
2780 Just press enter and type -- (and press enter again) and the block
2795 Just press enter and type -- (and press enter again) and the block
2781 will be what was just pasted.
2796 will be what was just pasted.
2782
2797
2783 IPython statements (magics, shell escapes) are not supported (yet).
2798 IPython statements (magics, shell escapes) are not supported (yet).
2784 """
2799 """
2785 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2800 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2786 par = args.strip()
2801 par = args.strip()
2787 sentinel = opts.get('s','--')
2802 sentinel = opts.get('s','--')
2788
2803
2789 from IPython import iplib
2804 from IPython import iplib
2790 lines = []
2805 lines = []
2791 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2806 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2792 while 1:
2807 while 1:
2793 l = iplib.raw_input_original(':')
2808 l = iplib.raw_input_original(':')
2794 if l ==sentinel:
2809 if l ==sentinel:
2795 break
2810 break
2796 lines.append(l)
2811 lines.append(l)
2797 block = "\n".join(lines)
2812 block = "\n".join(lines)
2798 #print "block:\n",block
2813 #print "block:\n",block
2799 if not par:
2814 if not par:
2800 b = textwrap.dedent(block)
2815 b = textwrap.dedent(block)
2801 exec b in self.user_ns
2816 exec b in self.user_ns
2802 self.user_ns['pasted_block'] = b
2817 self.user_ns['pasted_block'] = b
2803 else:
2818 else:
2804 self.user_ns[par] = block
2819 self.user_ns[par] = block
2805 print "Block assigned to '%s'" % par
2820 print "Block assigned to '%s'" % par
2806
2821
2807
2822
2808
2823
2809 # end Magic
2824 # end Magic
@@ -1,77 +1,78 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Release data for the IPython project.
2 """Release data for the IPython project.
3
3
4 $Id: Release.py 1058 2006-01-22 14:30:01Z vivainio $"""
4 $Id: Release.py 1077 2006-01-24 18:15:27Z vivainio $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 #
8 #
9 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
9 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
10 # <n8gray@caltech.edu>
10 # <n8gray@caltech.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 # Name of the package for release purposes. This is the name which labels
16 # Name of the package for release purposes. This is the name which labels
17 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
17 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
18 name = 'ipython'
18 name = 'ipython'
19
19
20 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
20 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
21 # the new substring. We have to avoid using either dashes or underscores,
21 # the new substring. We have to avoid using either dashes or underscores,
22 # because bdist_rpm does not accept dashes (an RPM) convention, and
22 # because bdist_rpm does not accept dashes (an RPM) convention, and
23 # bdist_deb does not accept underscores (a Debian convention).
23 # bdist_deb does not accept underscores (a Debian convention).
24
24
25
25 version = '0.7.2.svn'
26 version = '0.7.2.svn'
26
27
27 revision = '$Revision: 1058 $'
28 revision = '$Revision: 1077 $'
28
29
29 description = "An enhanced interactive Python shell."
30 description = "An enhanced interactive Python shell."
30
31
31 long_description = \
32 long_description = \
32 """
33 """
33 IPython provides a replacement for the interactive Python interpreter with
34 IPython provides a replacement for the interactive Python interpreter with
34 extra functionality.
35 extra functionality.
35
36
36 Main features:
37 Main features:
37
38
38 * Comprehensive object introspection.
39 * Comprehensive object introspection.
39
40
40 * Input history, persistent across sessions.
41 * Input history, persistent across sessions.
41
42
42 * Caching of output results during a session with automatically generated
43 * Caching of output results during a session with automatically generated
43 references.
44 references.
44
45
45 * Readline based name completion.
46 * Readline based name completion.
46
47
47 * Extensible system of 'magic' commands for controlling the environment and
48 * Extensible system of 'magic' commands for controlling the environment and
48 performing many tasks related either to IPython or the operating system.
49 performing many tasks related either to IPython or the operating system.
49
50
50 * Configuration system with easy switching between different setups (simpler
51 * Configuration system with easy switching between different setups (simpler
51 than changing $PYTHONSTARTUP environment variables every time).
52 than changing $PYTHONSTARTUP environment variables every time).
52
53
53 * Session logging and reloading.
54 * Session logging and reloading.
54
55
55 * Extensible syntax processing for special purpose situations.
56 * Extensible syntax processing for special purpose situations.
56
57
57 * Access to the system shell with user-extensible alias system.
58 * Access to the system shell with user-extensible alias system.
58
59
59 * Easily embeddable in other Python programs.
60 * Easily embeddable in other Python programs.
60
61
61 * Integrated access to the pdb debugger and the Python profiler. """
62 * Integrated access to the pdb debugger and the Python profiler. """
62
63
63 license = 'BSD'
64 license = 'BSD'
64
65
65 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
66 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
66 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
67 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
67 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
68 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
68 'Ville' : ('Ville Vainio','vivainio@gmail.com')
69 'Ville' : ('Ville Vainio','vivainio@gmail.com')
69 }
70 }
70
71
71 url = 'http://ipython.scipy.org'
72 url = 'http://ipython.scipy.org'
72
73
73 download_url = 'http://ipython.scipy.org/dist'
74 download_url = 'http://ipython.scipy.org/dist'
74
75
75 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
76 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
76
77
77 keywords = ['Interactive','Interpreter','Shell']
78 keywords = ['Interactive','Interpreter','Shell']
@@ -1,562 +1,562 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 ---------------------------------------------------------------------------
9 ---------------------------------------------------------------------------
10 Original rlcompleter documentation:
10 Original rlcompleter documentation:
11
11
12 This requires the latest extension to the readline module (the
12 This requires the latest extension to the readline module (the
13 completes keywords, built-ins and globals in __main__; when completing
13 completes keywords, built-ins and globals in __main__; when completing
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
15 completes its attributes.
15 completes its attributes.
16
16
17 It's very cool to do "import string" type "string.", hit the
17 It's very cool to do "import string" type "string.", hit the
18 completion key (twice), and see the list of names defined by the
18 completion key (twice), and see the list of names defined by the
19 string module!
19 string module!
20
20
21 Tip: to use the tab key as the completion key, call
21 Tip: to use the tab key as the completion key, call
22
22
23 readline.parse_and_bind("tab: complete")
23 readline.parse_and_bind("tab: complete")
24
24
25 Notes:
25 Notes:
26
26
27 - Exceptions raised by the completer function are *ignored* (and
27 - Exceptions raised by the completer function are *ignored* (and
28 generally cause the completion to fail). This is a feature -- since
28 generally cause the completion to fail). This is a feature -- since
29 readline sets the tty device in raw (or cbreak) mode, printing a
29 readline sets the tty device in raw (or cbreak) mode, printing a
30 traceback wouldn't work well without some complicated hoopla to save,
30 traceback wouldn't work well without some complicated hoopla to save,
31 reset and restore the tty state.
31 reset and restore the tty state.
32
32
33 - The evaluation of the NAME.NAME... form may cause arbitrary
33 - The evaluation of the NAME.NAME... form may cause arbitrary
34 application defined code to be executed if an object with a
34 application defined code to be executed if an object with a
35 __getattr__ hook is found. Since it is the responsibility of the
35 __getattr__ hook is found. Since it is the responsibility of the
36 application (or the user) to enable this feature, I consider this an
36 application (or the user) to enable this feature, I consider this an
37 acceptable risk. More complicated expressions (e.g. function calls or
37 acceptable risk. More complicated expressions (e.g. function calls or
38 indexing operations) are *not* evaluated.
38 indexing operations) are *not* evaluated.
39
39
40 - GNU readline is also used by the built-in functions input() and
40 - GNU readline is also used by the built-in functions input() and
41 raw_input(), and thus these also benefit/suffer from the completer
41 raw_input(), and thus these also benefit/suffer from the completer
42 features. Clearly an interactive application can benefit by
42 features. Clearly an interactive application can benefit by
43 specifying its own completer function and using raw_input() for all
43 specifying its own completer function and using raw_input() for all
44 its input.
44 its input.
45
45
46 - When the original stdin is not a tty device, GNU readline is never
46 - When the original stdin is not a tty device, GNU readline is never
47 used, and this module (and the readline module) are silently inactive.
47 used, and this module (and the readline module) are silently inactive.
48
48
49 """
49 """
50
50
51 #*****************************************************************************
51 #*****************************************************************************
52 #
52 #
53 # Since this file is essentially a minimally modified copy of the rlcompleter
53 # Since this file is essentially a minimally modified copy of the rlcompleter
54 # module which is part of the standard Python distribution, I assume that the
54 # module which is part of the standard Python distribution, I assume that the
55 # proper procedure is to maintain its copyright as belonging to the Python
55 # proper procedure is to maintain its copyright as belonging to the Python
56 # Software Foundation (in addition to my own, for all new code).
56 # Software Foundation (in addition to my own, for all new code).
57 #
57 #
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
59 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
60 #
60 #
61 # Distributed under the terms of the BSD License. The full license is in
61 # Distributed under the terms of the BSD License. The full license is in
62 # the file COPYING, distributed as part of this software.
62 # the file COPYING, distributed as part of this software.
63 #
63 #
64 #*****************************************************************************
64 #*****************************************************************************
65
65
66 import __builtin__
66 import __builtin__
67 import __main__
67 import __main__
68 import glob
68 import glob
69 import keyword
69 import keyword
70 import os
70 import os
71 import re
71 import re
72 import readline
72 import readline
73 import sys
73 import sys
74 import types
74 import types
75
75
76 # Python 2.4 offers sets as a builtin
76 # Python 2.4 offers sets as a builtin
77 try:
77 try:
78 set([1,2])
78 set([1,2])
79 except NameError:
79 except NameError:
80 from sets import Set as set
80 from sets import Set as set
81
81
82
82
83 from IPython.genutils import shlex_split,debugp
83 from IPython.genutils import shlex_split,debugx
84
84
85 __all__ = ['Completer','IPCompleter']
85 __all__ = ['Completer','IPCompleter']
86
86
87 def get_class_members(cls):
87 def get_class_members(cls):
88 ret = dir(cls)
88 ret = dir(cls)
89 if hasattr(cls,'__bases__'):
89 if hasattr(cls,'__bases__'):
90 for base in cls.__bases__:
90 for base in cls.__bases__:
91 ret.extend(get_class_members(base))
91 ret.extend(get_class_members(base))
92 return ret
92 return ret
93
93
94 class Completer:
94 class Completer:
95 def __init__(self,namespace=None,global_namespace=None):
95 def __init__(self,namespace=None,global_namespace=None):
96 """Create a new completer for the command line.
96 """Create a new completer for the command line.
97
97
98 Completer([namespace,global_namespace]) -> completer instance.
98 Completer([namespace,global_namespace]) -> completer instance.
99
99
100 If unspecified, the default namespace where completions are performed
100 If unspecified, the default namespace where completions are performed
101 is __main__ (technically, __main__.__dict__). Namespaces should be
101 is __main__ (technically, __main__.__dict__). Namespaces should be
102 given as dictionaries.
102 given as dictionaries.
103
103
104 An optional second namespace can be given. This allows the completer
104 An optional second namespace can be given. This allows the completer
105 to handle cases where both the local and global scopes need to be
105 to handle cases where both the local and global scopes need to be
106 distinguished.
106 distinguished.
107
107
108 Completer instances should be used as the completion mechanism of
108 Completer instances should be used as the completion mechanism of
109 readline via the set_completer() call:
109 readline via the set_completer() call:
110
110
111 readline.set_completer(Completer(my_namespace).complete)
111 readline.set_completer(Completer(my_namespace).complete)
112 """
112 """
113
113
114 # some minimal strict typechecks. For some core data structures, I
114 # some minimal strict typechecks. For some core data structures, I
115 # want actual basic python types, not just anything that looks like
115 # want actual basic python types, not just anything that looks like
116 # one. This is especially true for namespaces.
116 # one. This is especially true for namespaces.
117 for ns in (namespace,global_namespace):
117 for ns in (namespace,global_namespace):
118 if ns is not None and type(ns) != types.DictType:
118 if ns is not None and type(ns) != types.DictType:
119 raise TypeError,'namespace must be a dictionary'
119 raise TypeError,'namespace must be a dictionary'
120
120
121 # Don't bind to namespace quite yet, but flag whether the user wants a
121 # Don't bind to namespace quite yet, but flag whether the user wants a
122 # specific namespace or to use __main__.__dict__. This will allow us
122 # specific namespace or to use __main__.__dict__. This will allow us
123 # to bind to __main__.__dict__ at completion time, not now.
123 # to bind to __main__.__dict__ at completion time, not now.
124 if namespace is None:
124 if namespace is None:
125 self.use_main_ns = 1
125 self.use_main_ns = 1
126 else:
126 else:
127 self.use_main_ns = 0
127 self.use_main_ns = 0
128 self.namespace = namespace
128 self.namespace = namespace
129
129
130 # The global namespace, if given, can be bound directly
130 # The global namespace, if given, can be bound directly
131 if global_namespace is None:
131 if global_namespace is None:
132 self.global_namespace = {}
132 self.global_namespace = {}
133 else:
133 else:
134 self.global_namespace = global_namespace
134 self.global_namespace = global_namespace
135
135
136 def complete(self, text, state):
136 def complete(self, text, state):
137 """Return the next possible completion for 'text'.
137 """Return the next possible completion for 'text'.
138
138
139 This is called successively with state == 0, 1, 2, ... until it
139 This is called successively with state == 0, 1, 2, ... until it
140 returns None. The completion should begin with 'text'.
140 returns None. The completion should begin with 'text'.
141
141
142 """
142 """
143 if self.use_main_ns:
143 if self.use_main_ns:
144 self.namespace = __main__.__dict__
144 self.namespace = __main__.__dict__
145
145
146 if state == 0:
146 if state == 0:
147 if "." in text:
147 if "." in text:
148 self.matches = self.attr_matches(text)
148 self.matches = self.attr_matches(text)
149 else:
149 else:
150 self.matches = self.global_matches(text)
150 self.matches = self.global_matches(text)
151 try:
151 try:
152 return self.matches[state]
152 return self.matches[state]
153 except IndexError:
153 except IndexError:
154 return None
154 return None
155
155
156 def global_matches(self, text):
156 def global_matches(self, text):
157 """Compute matches when text is a simple name.
157 """Compute matches when text is a simple name.
158
158
159 Return a list of all keywords, built-in functions and names currently
159 Return a list of all keywords, built-in functions and names currently
160 defined in self.namespace or self.global_namespace that match.
160 defined in self.namespace or self.global_namespace that match.
161
161
162 """
162 """
163 matches = []
163 matches = []
164 match_append = matches.append
164 match_append = matches.append
165 n = len(text)
165 n = len(text)
166 for lst in [keyword.kwlist,
166 for lst in [keyword.kwlist,
167 __builtin__.__dict__.keys(),
167 __builtin__.__dict__.keys(),
168 self.namespace.keys(),
168 self.namespace.keys(),
169 self.global_namespace.keys()]:
169 self.global_namespace.keys()]:
170 for word in lst:
170 for word in lst:
171 if word[:n] == text and word != "__builtins__":
171 if word[:n] == text and word != "__builtins__":
172 match_append(word)
172 match_append(word)
173 return matches
173 return matches
174
174
175 def attr_matches(self, text):
175 def attr_matches(self, text):
176 """Compute matches when text contains a dot.
176 """Compute matches when text contains a dot.
177
177
178 Assuming the text is of the form NAME.NAME....[NAME], and is
178 Assuming the text is of the form NAME.NAME....[NAME], and is
179 evaluatable in self.namespace or self.global_namespace, it will be
179 evaluatable in self.namespace or self.global_namespace, it will be
180 evaluated and its attributes (as revealed by dir()) are used as
180 evaluated and its attributes (as revealed by dir()) are used as
181 possible completions. (For class instances, class members are are
181 possible completions. (For class instances, class members are are
182 also considered.)
182 also considered.)
183
183
184 WARNING: this can still invoke arbitrary C code, if an object
184 WARNING: this can still invoke arbitrary C code, if an object
185 with a __getattr__ hook is evaluated.
185 with a __getattr__ hook is evaluated.
186
186
187 """
187 """
188 import re
188 import re
189
189
190 # Another option, seems to work great. Catches things like ''.<tab>
190 # Another option, seems to work great. Catches things like ''.<tab>
191 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
191 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
192
192
193 if not m:
193 if not m:
194 return []
194 return []
195
195
196 expr, attr = m.group(1, 3)
196 expr, attr = m.group(1, 3)
197 try:
197 try:
198 object = eval(expr, self.namespace)
198 object = eval(expr, self.namespace)
199 except:
199 except:
200 object = eval(expr, self.global_namespace)
200 object = eval(expr, self.global_namespace)
201
201
202 # Start building the attribute list via dir(), and then complete it
202 # Start building the attribute list via dir(), and then complete it
203 # with a few extra special-purpose calls.
203 # with a few extra special-purpose calls.
204 words = dir(object)
204 words = dir(object)
205
205
206 if hasattr(object,'__class__'):
206 if hasattr(object,'__class__'):
207 words.append('__class__')
207 words.append('__class__')
208 words.extend(get_class_members(object.__class__))
208 words.extend(get_class_members(object.__class__))
209
209
210 # this is the 'dir' function for objects with Enthought's traits
210 # this is the 'dir' function for objects with Enthought's traits
211 if hasattr(object, 'trait_names'):
211 if hasattr(object, 'trait_names'):
212 try:
212 try:
213 words.extend(object.trait_names())
213 words.extend(object.trait_names())
214 # eliminate possible duplicates, as some traits may also
214 # eliminate possible duplicates, as some traits may also
215 # appear as normal attributes in the dir() call.
215 # appear as normal attributes in the dir() call.
216 words = set(words)
216 words = set(words)
217 except TypeError:
217 except TypeError:
218 # This will happen if `object` is a class and not an instance.
218 # This will happen if `object` is a class and not an instance.
219 pass
219 pass
220
220
221 # filter out non-string attributes which may be stuffed by dir() calls
221 # filter out non-string attributes which may be stuffed by dir() calls
222 # and poor coding in third-party modules
222 # and poor coding in third-party modules
223 words = [w for w in words
223 words = [w for w in words
224 if isinstance(w, basestring) and w != "__builtins__"]
224 if isinstance(w, basestring) and w != "__builtins__"]
225 # Build match list to return
225 # Build match list to return
226 n = len(attr)
226 n = len(attr)
227 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
227 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
228
228
229 class IPCompleter(Completer):
229 class IPCompleter(Completer):
230 """Extension of the completer class with IPython-specific features"""
230 """Extension of the completer class with IPython-specific features"""
231
231
232 def __init__(self,shell,namespace=None,global_namespace=None,
232 def __init__(self,shell,namespace=None,global_namespace=None,
233 omit__names=0,alias_table=None):
233 omit__names=0,alias_table=None):
234 """IPCompleter() -> completer
234 """IPCompleter() -> completer
235
235
236 Return a completer object suitable for use by the readline library
236 Return a completer object suitable for use by the readline library
237 via readline.set_completer().
237 via readline.set_completer().
238
238
239 Inputs:
239 Inputs:
240
240
241 - shell: a pointer to the ipython shell itself. This is needed
241 - shell: a pointer to the ipython shell itself. This is needed
242 because this completer knows about magic functions, and those can
242 because this completer knows about magic functions, and those can
243 only be accessed via the ipython instance.
243 only be accessed via the ipython instance.
244
244
245 - namespace: an optional dict where completions are performed.
245 - namespace: an optional dict where completions are performed.
246
246
247 - global_namespace: secondary optional dict for completions, to
247 - global_namespace: secondary optional dict for completions, to
248 handle cases (such as IPython embedded inside functions) where
248 handle cases (such as IPython embedded inside functions) where
249 both Python scopes are visible.
249 both Python scopes are visible.
250
250
251 - The optional omit__names parameter sets the completer to omit the
251 - The optional omit__names parameter sets the completer to omit the
252 'magic' names (__magicname__) for python objects unless the text
252 'magic' names (__magicname__) for python objects unless the text
253 to be completed explicitly starts with one or more underscores.
253 to be completed explicitly starts with one or more underscores.
254
254
255 - If alias_table is supplied, it should be a dictionary of aliases
255 - If alias_table is supplied, it should be a dictionary of aliases
256 to complete. """
256 to complete. """
257
257
258 Completer.__init__(self,namespace,global_namespace)
258 Completer.__init__(self,namespace,global_namespace)
259 self.magic_prefix = shell.name+'.magic_'
259 self.magic_prefix = shell.name+'.magic_'
260 self.magic_escape = shell.ESC_MAGIC
260 self.magic_escape = shell.ESC_MAGIC
261 self.readline = readline
261 self.readline = readline
262 delims = self.readline.get_completer_delims()
262 delims = self.readline.get_completer_delims()
263 delims = delims.replace(self.magic_escape,'')
263 delims = delims.replace(self.magic_escape,'')
264 self.readline.set_completer_delims(delims)
264 self.readline.set_completer_delims(delims)
265 self.get_line_buffer = self.readline.get_line_buffer
265 self.get_line_buffer = self.readline.get_line_buffer
266 self.omit__names = omit__names
266 self.omit__names = omit__names
267 self.merge_completions = shell.rc.readline_merge_completions
267 self.merge_completions = shell.rc.readline_merge_completions
268
268
269 if alias_table is None:
269 if alias_table is None:
270 alias_table = {}
270 alias_table = {}
271 self.alias_table = alias_table
271 self.alias_table = alias_table
272 # Regexp to split filenames with spaces in them
272 # Regexp to split filenames with spaces in them
273 self.space_name_re = re.compile(r'([^\\] )')
273 self.space_name_re = re.compile(r'([^\\] )')
274 # Hold a local ref. to glob.glob for speed
274 # Hold a local ref. to glob.glob for speed
275 self.glob = glob.glob
275 self.glob = glob.glob
276
276
277 # Determine if we are running on 'dumb' terminals, like (X)Emacs
277 # Determine if we are running on 'dumb' terminals, like (X)Emacs
278 # buffers, to avoid completion problems.
278 # buffers, to avoid completion problems.
279 term = os.environ.get('TERM','xterm')
279 term = os.environ.get('TERM','xterm')
280 self.dumb_terminal = term in ['dumb','emacs']
280 self.dumb_terminal = term in ['dumb','emacs']
281
281
282 # Special handling of backslashes needed in win32 platforms
282 # Special handling of backslashes needed in win32 platforms
283 if sys.platform == "win32":
283 if sys.platform == "win32":
284 self.clean_glob = self._clean_glob_win32
284 self.clean_glob = self._clean_glob_win32
285 else:
285 else:
286 self.clean_glob = self._clean_glob
286 self.clean_glob = self._clean_glob
287 self.matchers = [self.python_matches,
287 self.matchers = [self.python_matches,
288 self.file_matches,
288 self.file_matches,
289 self.alias_matches,
289 self.alias_matches,
290 self.python_func_kw_matches]
290 self.python_func_kw_matches]
291
291
292 # Code contributed by Alex Schmolck, for ipython/emacs integration
292 # Code contributed by Alex Schmolck, for ipython/emacs integration
293 def all_completions(self, text):
293 def all_completions(self, text):
294 """Return all possible completions for the benefit of emacs."""
294 """Return all possible completions for the benefit of emacs."""
295
295
296 completions = []
296 completions = []
297 comp_append = completions.append
297 comp_append = completions.append
298 try:
298 try:
299 for i in xrange(sys.maxint):
299 for i in xrange(sys.maxint):
300 res = self.complete(text, i)
300 res = self.complete(text, i)
301
301
302 if not res: break
302 if not res: break
303
303
304 comp_append(res)
304 comp_append(res)
305 #XXX workaround for ``notDefined.<tab>``
305 #XXX workaround for ``notDefined.<tab>``
306 except NameError:
306 except NameError:
307 pass
307 pass
308 return completions
308 return completions
309 # /end Alex Schmolck code.
309 # /end Alex Schmolck code.
310
310
311 def _clean_glob(self,text):
311 def _clean_glob(self,text):
312 return self.glob("%s*" % text)
312 return self.glob("%s*" % text)
313
313
314 def _clean_glob_win32(self,text):
314 def _clean_glob_win32(self,text):
315 return [f.replace("\\","/")
315 return [f.replace("\\","/")
316 for f in self.glob("%s*" % text)]
316 for f in self.glob("%s*" % text)]
317
317
318 def file_matches(self, text):
318 def file_matches(self, text):
319 """Match filneames, expanding ~USER type strings.
319 """Match filneames, expanding ~USER type strings.
320
320
321 Most of the seemingly convoluted logic in this completer is an
321 Most of the seemingly convoluted logic in this completer is an
322 attempt to handle filenames with spaces in them. And yet it's not
322 attempt to handle filenames with spaces in them. And yet it's not
323 quite perfect, because Python's readline doesn't expose all of the
323 quite perfect, because Python's readline doesn't expose all of the
324 GNU readline details needed for this to be done correctly.
324 GNU readline details needed for this to be done correctly.
325
325
326 For a filename with a space in it, the printed completions will be
326 For a filename with a space in it, the printed completions will be
327 only the parts after what's already been typed (instead of the
327 only the parts after what's already been typed (instead of the
328 full completions, as is normally done). I don't think with the
328 full completions, as is normally done). I don't think with the
329 current (as of Python 2.3) Python readline it's possible to do
329 current (as of Python 2.3) Python readline it's possible to do
330 better."""
330 better."""
331
331
332 #print 'Completer->file_matches: <%s>' % text # dbg
332 #print 'Completer->file_matches: <%s>' % text # dbg
333
333
334 # chars that require escaping with backslash - i.e. chars
334 # chars that require escaping with backslash - i.e. chars
335 # that readline treats incorrectly as delimiters, but we
335 # that readline treats incorrectly as delimiters, but we
336 # don't want to treat as delimiters in filename matching
336 # don't want to treat as delimiters in filename matching
337 # when escaped with backslash
337 # when escaped with backslash
338
338
339 protectables = ' ()[]{}'
339 protectables = ' ()[]{}'
340
340
341 def protect_filename(s):
341 def protect_filename(s):
342 return "".join([(ch in protectables and '\\' + ch or ch)
342 return "".join([(ch in protectables and '\\' + ch or ch)
343 for ch in s])
343 for ch in s])
344
344
345 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
345 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
346 open_quotes = 0 # track strings with open quotes
346 open_quotes = 0 # track strings with open quotes
347 try:
347 try:
348 lsplit = shlex_split(lbuf)[-1]
348 lsplit = shlex_split(lbuf)[-1]
349 except ValueError:
349 except ValueError:
350 # typically an unmatched ", or backslash without escaped char.
350 # typically an unmatched ", or backslash without escaped char.
351 if lbuf.count('"')==1:
351 if lbuf.count('"')==1:
352 open_quotes = 1
352 open_quotes = 1
353 lsplit = lbuf.split('"')[-1]
353 lsplit = lbuf.split('"')[-1]
354 elif lbuf.count("'")==1:
354 elif lbuf.count("'")==1:
355 open_quotes = 1
355 open_quotes = 1
356 lsplit = lbuf.split("'")[-1]
356 lsplit = lbuf.split("'")[-1]
357 else:
357 else:
358 return None
358 return None
359 except IndexError:
359 except IndexError:
360 # tab pressed on empty line
360 # tab pressed on empty line
361 lsplit = ""
361 lsplit = ""
362
362
363 if lsplit != protect_filename(lsplit):
363 if lsplit != protect_filename(lsplit):
364 # if protectables are found, do matching on the whole escaped
364 # if protectables are found, do matching on the whole escaped
365 # name
365 # name
366 has_protectables = 1
366 has_protectables = 1
367 text0,text = text,lsplit
367 text0,text = text,lsplit
368 else:
368 else:
369 has_protectables = 0
369 has_protectables = 0
370 text = os.path.expanduser(text)
370 text = os.path.expanduser(text)
371
371
372 if text == "":
372 if text == "":
373 return [protect_filename(f) for f in self.glob("*")]
373 return [protect_filename(f) for f in self.glob("*")]
374
374
375 m0 = self.clean_glob(text.replace('\\',''))
375 m0 = self.clean_glob(text.replace('\\',''))
376 if has_protectables:
376 if has_protectables:
377 # If we had protectables, we need to revert our changes to the
377 # If we had protectables, we need to revert our changes to the
378 # beginning of filename so that we don't double-write the part
378 # beginning of filename so that we don't double-write the part
379 # of the filename we have so far
379 # of the filename we have so far
380 len_lsplit = len(lsplit)
380 len_lsplit = len(lsplit)
381 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
381 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
382 else:
382 else:
383 if open_quotes:
383 if open_quotes:
384 # if we have a string with an open quote, we don't need to
384 # if we have a string with an open quote, we don't need to
385 # protect the names at all (and we _shouldn't_, as it
385 # protect the names at all (and we _shouldn't_, as it
386 # would cause bugs when the filesystem call is made).
386 # would cause bugs when the filesystem call is made).
387 matches = m0
387 matches = m0
388 else:
388 else:
389 matches = [protect_filename(f) for f in m0]
389 matches = [protect_filename(f) for f in m0]
390 if len(matches) == 1 and os.path.isdir(matches[0]):
390 if len(matches) == 1 and os.path.isdir(matches[0]):
391 # Takes care of links to directories also. Use '/'
391 # Takes care of links to directories also. Use '/'
392 # explicitly, even under Windows, so that name completions
392 # explicitly, even under Windows, so that name completions
393 # don't end up escaped.
393 # don't end up escaped.
394 matches[0] += '/'
394 matches[0] += '/'
395 return matches
395 return matches
396
396
397 def alias_matches(self, text):
397 def alias_matches(self, text):
398 """Match internal system aliases"""
398 """Match internal system aliases"""
399
399
400 #print 'Completer->alias_matches:',text # dbg
400 #print 'Completer->alias_matches:',text # dbg
401 text = os.path.expanduser(text)
401 text = os.path.expanduser(text)
402 aliases = self.alias_table.keys()
402 aliases = self.alias_table.keys()
403 if text == "":
403 if text == "":
404 return aliases
404 return aliases
405 else:
405 else:
406 return [alias for alias in aliases if alias.startswith(text)]
406 return [alias for alias in aliases if alias.startswith(text)]
407
407
408 def python_matches(self,text):
408 def python_matches(self,text):
409 """Match attributes or global python names"""
409 """Match attributes or global python names"""
410
410
411 #print 'Completer->python_matches, txt=<%s>' % text # dbg
411 #print 'Completer->python_matches, txt=<%s>' % text # dbg
412 if "." in text:
412 if "." in text:
413 try:
413 try:
414 matches = self.attr_matches(text)
414 matches = self.attr_matches(text)
415 if text.endswith('.') and self.omit__names:
415 if text.endswith('.') and self.omit__names:
416 if self.omit__names == 1:
416 if self.omit__names == 1:
417 # true if txt is _not_ a __ name, false otherwise:
417 # true if txt is _not_ a __ name, false otherwise:
418 no__name = (lambda txt:
418 no__name = (lambda txt:
419 re.match(r'.*\.__.*?__',txt) is None)
419 re.match(r'.*\.__.*?__',txt) is None)
420 else:
420 else:
421 # true if txt is _not_ a _ name, false otherwise:
421 # true if txt is _not_ a _ name, false otherwise:
422 no__name = (lambda txt:
422 no__name = (lambda txt:
423 re.match(r'.*\._.*?',txt) is None)
423 re.match(r'.*\._.*?',txt) is None)
424 matches = filter(no__name, matches)
424 matches = filter(no__name, matches)
425 except NameError:
425 except NameError:
426 # catches <undefined attributes>.<tab>
426 # catches <undefined attributes>.<tab>
427 matches = []
427 matches = []
428 else:
428 else:
429 matches = self.global_matches(text)
429 matches = self.global_matches(text)
430 # this is so completion finds magics when automagic is on:
430 # this is so completion finds magics when automagic is on:
431 if matches == [] and not text.startswith(os.sep):
431 if matches == [] and not text.startswith(os.sep):
432 matches = self.attr_matches(self.magic_prefix+text)
432 matches = self.attr_matches(self.magic_prefix+text)
433 return matches
433 return matches
434
434
435 def _default_arguments(self, obj):
435 def _default_arguments(self, obj):
436 """Return the list of default arguments of obj if it is callable,
436 """Return the list of default arguments of obj if it is callable,
437 or empty list otherwise."""
437 or empty list otherwise."""
438
438
439 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
439 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
440 # for classes, check for __init__,__new__
440 # for classes, check for __init__,__new__
441 if inspect.isclass(obj):
441 if inspect.isclass(obj):
442 obj = (getattr(obj,'__init__',None) or
442 obj = (getattr(obj,'__init__',None) or
443 getattr(obj,'__new__',None))
443 getattr(obj,'__new__',None))
444 # for all others, check if they are __call__able
444 # for all others, check if they are __call__able
445 elif hasattr(obj, '__call__'):
445 elif hasattr(obj, '__call__'):
446 obj = obj.__call__
446 obj = obj.__call__
447 # XXX: is there a way to handle the builtins ?
447 # XXX: is there a way to handle the builtins ?
448 try:
448 try:
449 args,_,_1,defaults = inspect.getargspec(obj)
449 args,_,_1,defaults = inspect.getargspec(obj)
450 if defaults:
450 if defaults:
451 return args[-len(defaults):]
451 return args[-len(defaults):]
452 except TypeError: pass
452 except TypeError: pass
453 return []
453 return []
454
454
455 def python_func_kw_matches(self,text):
455 def python_func_kw_matches(self,text):
456 """Match named parameters (kwargs) of the last open function"""
456 """Match named parameters (kwargs) of the last open function"""
457
457
458 if "." in text: # a parameter cannot be dotted
458 if "." in text: # a parameter cannot be dotted
459 return []
459 return []
460 try: regexp = self.__funcParamsRegex
460 try: regexp = self.__funcParamsRegex
461 except AttributeError:
461 except AttributeError:
462 regexp = self.__funcParamsRegex = re.compile(r'''
462 regexp = self.__funcParamsRegex = re.compile(r'''
463 '.*?' | # single quoted strings or
463 '.*?' | # single quoted strings or
464 ".*?" | # double quoted strings or
464 ".*?" | # double quoted strings or
465 \w+ | # identifier
465 \w+ | # identifier
466 \S # other characters
466 \S # other characters
467 ''', re.VERBOSE | re.DOTALL)
467 ''', re.VERBOSE | re.DOTALL)
468 # 1. find the nearest identifier that comes before an unclosed
468 # 1. find the nearest identifier that comes before an unclosed
469 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
469 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
470 tokens = regexp.findall(self.get_line_buffer())
470 tokens = regexp.findall(self.get_line_buffer())
471 tokens.reverse()
471 tokens.reverse()
472 iterTokens = iter(tokens); openPar = 0
472 iterTokens = iter(tokens); openPar = 0
473 for token in iterTokens:
473 for token in iterTokens:
474 if token == ')':
474 if token == ')':
475 openPar -= 1
475 openPar -= 1
476 elif token == '(':
476 elif token == '(':
477 openPar += 1
477 openPar += 1
478 if openPar > 0:
478 if openPar > 0:
479 # found the last unclosed parenthesis
479 # found the last unclosed parenthesis
480 break
480 break
481 else:
481 else:
482 return []
482 return []
483 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
483 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
484 ids = []
484 ids = []
485 isId = re.compile(r'\w+$').match
485 isId = re.compile(r'\w+$').match
486 while True:
486 while True:
487 try:
487 try:
488 ids.append(iterTokens.next())
488 ids.append(iterTokens.next())
489 if not isId(ids[-1]):
489 if not isId(ids[-1]):
490 ids.pop(); break
490 ids.pop(); break
491 if not iterTokens.next() == '.':
491 if not iterTokens.next() == '.':
492 break
492 break
493 except StopIteration:
493 except StopIteration:
494 break
494 break
495 # lookup the candidate callable matches either using global_matches
495 # lookup the candidate callable matches either using global_matches
496 # or attr_matches for dotted names
496 # or attr_matches for dotted names
497 if len(ids) == 1:
497 if len(ids) == 1:
498 callableMatches = self.global_matches(ids[0])
498 callableMatches = self.global_matches(ids[0])
499 else:
499 else:
500 callableMatches = self.attr_matches('.'.join(ids[::-1]))
500 callableMatches = self.attr_matches('.'.join(ids[::-1]))
501 argMatches = []
501 argMatches = []
502 for callableMatch in callableMatches:
502 for callableMatch in callableMatches:
503 try: namedArgs = self._default_arguments(eval(callableMatch,
503 try: namedArgs = self._default_arguments(eval(callableMatch,
504 self.namespace))
504 self.namespace))
505 except: continue
505 except: continue
506 for namedArg in namedArgs:
506 for namedArg in namedArgs:
507 if namedArg.startswith(text):
507 if namedArg.startswith(text):
508 argMatches.append("%s=" %namedArg)
508 argMatches.append("%s=" %namedArg)
509 return argMatches
509 return argMatches
510
510
511 def complete(self, text, state):
511 def complete(self, text, state):
512 """Return the next possible completion for 'text'.
512 """Return the next possible completion for 'text'.
513
513
514 This is called successively with state == 0, 1, 2, ... until it
514 This is called successively with state == 0, 1, 2, ... until it
515 returns None. The completion should begin with 'text'. """
515 returns None. The completion should begin with 'text'. """
516
516
517 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
517 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
518
518
519 # if there is only a tab on a line with only whitespace, instead
519 # if there is only a tab on a line with only whitespace, instead
520 # of the mostly useless 'do you want to see all million
520 # of the mostly useless 'do you want to see all million
521 # completions' message, just do the right thing and give the user
521 # completions' message, just do the right thing and give the user
522 # his tab! Incidentally, this enables pasting of tabbed text from
522 # his tab! Incidentally, this enables pasting of tabbed text from
523 # an editor (as long as autoindent is off).
523 # an editor (as long as autoindent is off).
524
524
525 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
525 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
526 # don't interfere with their own tab-completion mechanism.
526 # don't interfere with their own tab-completion mechanism.
527 if not (self.dumb_terminal or self.get_line_buffer().strip()):
527 if not (self.dumb_terminal or self.get_line_buffer().strip()):
528 self.readline.insert_text('\t')
528 self.readline.insert_text('\t')
529 return None
529 return None
530
530
531 magic_escape = self.magic_escape
531 magic_escape = self.magic_escape
532 magic_prefix = self.magic_prefix
532 magic_prefix = self.magic_prefix
533
533
534 try:
534 try:
535 if text.startswith(magic_escape):
535 if text.startswith(magic_escape):
536 text = text.replace(magic_escape,magic_prefix)
536 text = text.replace(magic_escape,magic_prefix)
537 elif text.startswith('~'):
537 elif text.startswith('~'):
538 text = os.path.expanduser(text)
538 text = os.path.expanduser(text)
539 if state == 0:
539 if state == 0:
540 # Extend the list of completions with the results of each
540 # Extend the list of completions with the results of each
541 # matcher, so we return results to the user from all
541 # matcher, so we return results to the user from all
542 # namespaces.
542 # namespaces.
543 if self.merge_completions:
543 if self.merge_completions:
544 self.matches = []
544 self.matches = []
545 for matcher in self.matchers:
545 for matcher in self.matchers:
546 self.matches.extend(matcher(text))
546 self.matches.extend(matcher(text))
547 else:
547 else:
548 for matcher in self.matchers:
548 for matcher in self.matchers:
549 self.matches = matcher(text)
549 self.matches = matcher(text)
550 if self.matches:
550 if self.matches:
551 break
551 break
552
552
553 try:
553 try:
554 return self.matches[state].replace(magic_prefix,magic_escape)
554 return self.matches[state].replace(magic_prefix,magic_escape)
555 except IndexError:
555 except IndexError:
556 return None
556 return None
557 except:
557 except:
558 #from IPython.ultraTB import AutoFormattedTB; # dbg
558 #from IPython.ultraTB import AutoFormattedTB; # dbg
559 #tb=AutoFormattedTB('Verbose');tb() #dbg
559 #tb=AutoFormattedTB('Verbose');tb() #dbg
560
560
561 # If completion fails, don't annoy the user.
561 # If completion fails, don't annoy the user.
562 return None
562 return None
@@ -1,1772 +1,1773 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 General purpose utilities.
3 General purpose utilities.
4
4
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 1058 2006-01-22 14:30:01Z vivainio $"""
8 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #*****************************************************************************
15 #*****************************************************************************
16
16
17 from __future__ import generators # 2.2 compatibility
17 from __future__ import generators # 2.2 compatibility
18
18
19 from IPython import Release
19 from IPython import Release
20 __author__ = '%s <%s>' % Release.authors['Fernando']
20 __author__ = '%s <%s>' % Release.authors['Fernando']
21 __license__ = Release.license
21 __license__ = Release.license
22
22
23 #****************************************************************************
23 #****************************************************************************
24 # required modules from the Python standard library
24 # required modules from the Python standard library
25 import __main__
25 import __main__
26 import commands
26 import commands
27 import os
27 import os
28 import re
28 import re
29 import shlex
29 import shlex
30 import shutil
30 import shutil
31 import sys
31 import sys
32 import tempfile
32 import tempfile
33 import time
33 import time
34 import types
34 import types
35
35
36 # Other IPython utilities
36 # Other IPython utilities
37 from IPython.Itpl import Itpl,itpl,printpl
37 from IPython.Itpl import Itpl,itpl,printpl
38 from IPython import DPyGetOpt
38 from IPython import DPyGetOpt
39 from IPython.path import path
39 from IPython.path import path
40 if os.name == "nt":
40 if os.name == "nt":
41 from IPython.winconsole import get_console_size
41 from IPython.winconsole import get_console_size
42
42
43 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
43 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
44 # 2.2-friendly
44 # 2.2-friendly
45 try:
45 try:
46 basestring
46 basestring
47 except NameError:
47 except NameError:
48 import types
48 import types
49 basestring = (types.StringType, types.UnicodeType)
49 basestring = (types.StringType, types.UnicodeType)
50 True = 1==1
50 True = 1==1
51 False = 1==0
51 False = 1==0
52
52
53 def enumerate(obj):
53 def enumerate(obj):
54 i = -1
54 i = -1
55 for item in obj:
55 for item in obj:
56 i += 1
56 i += 1
57 yield i, item
57 yield i, item
58
58
59 # add these to the builtin namespace, so that all modules find them
59 # add these to the builtin namespace, so that all modules find them
60 import __builtin__
60 import __builtin__
61 __builtin__.basestring = basestring
61 __builtin__.basestring = basestring
62 __builtin__.True = True
62 __builtin__.True = True
63 __builtin__.False = False
63 __builtin__.False = False
64 __builtin__.enumerate = enumerate
64 __builtin__.enumerate = enumerate
65
65
66 # Try to use shlex.split for converting an input string into a sys.argv-type
66 # Try to use shlex.split for converting an input string into a sys.argv-type
67 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
67 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
68 try:
68 try:
69 shlex_split = shlex.split
69 shlex_split = shlex.split
70 except AttributeError:
70 except AttributeError:
71 _quotesre = re.compile(r'[\'"](.*)[\'"]')
71 _quotesre = re.compile(r'[\'"](.*)[\'"]')
72 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
72 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
73 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
73 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
74 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
74 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
75 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
75 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
76 % os.sep)
76 % os.sep)
77
77
78 def shlex_split(s):
78 def shlex_split(s):
79 """Simplified backport to Python 2.2 of shlex.split().
79 """Simplified backport to Python 2.2 of shlex.split().
80
80
81 This is a quick and dirty hack, since the shlex module under 2.2 lacks
81 This is a quick and dirty hack, since the shlex module under 2.2 lacks
82 several of the features needed to really match the functionality of
82 several of the features needed to really match the functionality of
83 shlex.split() in 2.3."""
83 shlex.split() in 2.3."""
84
84
85 lex = shlex.shlex(StringIO(s))
85 lex = shlex.shlex(StringIO(s))
86 # Try to get options, extensions and path separators as characters
86 # Try to get options, extensions and path separators as characters
87 lex.wordchars = _wordchars
87 lex.wordchars = _wordchars
88 lex.commenters = ''
88 lex.commenters = ''
89 # Make a list out of the lexer by hand, since in 2.2 it's not an
89 # Make a list out of the lexer by hand, since in 2.2 it's not an
90 # iterator.
90 # iterator.
91 lout = []
91 lout = []
92 while 1:
92 while 1:
93 token = lex.get_token()
93 token = lex.get_token()
94 if token == '':
94 if token == '':
95 break
95 break
96 # Try to handle quoted tokens correctly
96 # Try to handle quoted tokens correctly
97 quotes = _quotesre.match(token)
97 quotes = _quotesre.match(token)
98 if quotes:
98 if quotes:
99 token = quotes.group(1)
99 token = quotes.group(1)
100 lout.append(token)
100 lout.append(token)
101 return lout
101 return lout
102
102
103 #****************************************************************************
103 #****************************************************************************
104 # Exceptions
104 # Exceptions
105 class Error(Exception):
105 class Error(Exception):
106 """Base class for exceptions in this module."""
106 """Base class for exceptions in this module."""
107 pass
107 pass
108
108
109 #----------------------------------------------------------------------------
109 #----------------------------------------------------------------------------
110 class IOStream:
110 class IOStream:
111 def __init__(self,stream,fallback):
111 def __init__(self,stream,fallback):
112 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
112 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
113 stream = fallback
113 stream = fallback
114 self.stream = stream
114 self.stream = stream
115 self._swrite = stream.write
115 self._swrite = stream.write
116 self.flush = stream.flush
116 self.flush = stream.flush
117
117
118 def write(self,data):
118 def write(self,data):
119 try:
119 try:
120 self._swrite(data)
120 self._swrite(data)
121 except:
121 except:
122 try:
122 try:
123 # print handles some unicode issues which may trip a plain
123 # print handles some unicode issues which may trip a plain
124 # write() call. Attempt to emulate write() by using a
124 # write() call. Attempt to emulate write() by using a
125 # trailing comma
125 # trailing comma
126 print >> self.stream, data,
126 print >> self.stream, data,
127 except:
127 except:
128 # if we get here, something is seriously broken.
128 # if we get here, something is seriously broken.
129 print >> sys.stderr, \
129 print >> sys.stderr, \
130 'ERROR - failed to write data to stream:', stream
130 'ERROR - failed to write data to stream:', stream
131
131
132 class IOTerm:
132 class IOTerm:
133 """ Term holds the file or file-like objects for handling I/O operations.
133 """ Term holds the file or file-like objects for handling I/O operations.
134
134
135 These are normally just sys.stdin, sys.stdout and sys.stderr but for
135 These are normally just sys.stdin, sys.stdout and sys.stderr but for
136 Windows they can can replaced to allow editing the strings before they are
136 Windows they can can replaced to allow editing the strings before they are
137 displayed."""
137 displayed."""
138
138
139 # In the future, having IPython channel all its I/O operations through
139 # In the future, having IPython channel all its I/O operations through
140 # this class will make it easier to embed it into other environments which
140 # this class will make it easier to embed it into other environments which
141 # are not a normal terminal (such as a GUI-based shell)
141 # are not a normal terminal (such as a GUI-based shell)
142 def __init__(self,cin=None,cout=None,cerr=None):
142 def __init__(self,cin=None,cout=None,cerr=None):
143 self.cin = IOStream(cin,sys.stdin)
143 self.cin = IOStream(cin,sys.stdin)
144 self.cout = IOStream(cout,sys.stdout)
144 self.cout = IOStream(cout,sys.stdout)
145 self.cerr = IOStream(cerr,sys.stderr)
145 self.cerr = IOStream(cerr,sys.stderr)
146
146
147 # Global variable to be used for all I/O
147 # Global variable to be used for all I/O
148 Term = IOTerm()
148 Term = IOTerm()
149
149
150 # Windows-specific code to load Gary Bishop's readline and configure it
150 # Windows-specific code to load Gary Bishop's readline and configure it
151 # automatically for the users
151 # automatically for the users
152 # Note: os.name on cygwin returns posix, so this should only pick up 'native'
152 # Note: os.name on cygwin returns posix, so this should only pick up 'native'
153 # windows. Cygwin returns 'cygwin' for sys.platform.
153 # windows. Cygwin returns 'cygwin' for sys.platform.
154 if os.name == 'nt':
154 if os.name == 'nt':
155 try:
155 try:
156 import readline
156 import readline
157 except ImportError:
157 except ImportError:
158 pass
158 pass
159 else:
159 else:
160 try:
160 try:
161 _out = readline.GetOutputFile()
161 _out = readline.GetOutputFile()
162 except AttributeError:
162 except AttributeError:
163 pass
163 pass
164 else:
164 else:
165 # Remake Term to use the readline i/o facilities
165 # Remake Term to use the readline i/o facilities
166 Term = IOTerm(cout=_out,cerr=_out)
166 Term = IOTerm(cout=_out,cerr=_out)
167 del _out
167 del _out
168
168
169 #****************************************************************************
169 #****************************************************************************
170 # Generic warning/error printer, used by everything else
170 # Generic warning/error printer, used by everything else
171 def warn(msg,level=2,exit_val=1):
171 def warn(msg,level=2,exit_val=1):
172 """Standard warning printer. Gives formatting consistency.
172 """Standard warning printer. Gives formatting consistency.
173
173
174 Output is sent to Term.cerr (sys.stderr by default).
174 Output is sent to Term.cerr (sys.stderr by default).
175
175
176 Options:
176 Options:
177
177
178 -level(2): allows finer control:
178 -level(2): allows finer control:
179 0 -> Do nothing, dummy function.
179 0 -> Do nothing, dummy function.
180 1 -> Print message.
180 1 -> Print message.
181 2 -> Print 'WARNING:' + message. (Default level).
181 2 -> Print 'WARNING:' + message. (Default level).
182 3 -> Print 'ERROR:' + message.
182 3 -> Print 'ERROR:' + message.
183 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
183 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
184
184
185 -exit_val (1): exit value returned by sys.exit() for a level 4
185 -exit_val (1): exit value returned by sys.exit() for a level 4
186 warning. Ignored for all other levels."""
186 warning. Ignored for all other levels."""
187
187
188 if level>0:
188 if level>0:
189 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
189 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
190 print >> Term.cerr, '%s%s' % (header[level],msg)
190 print >> Term.cerr, '%s%s' % (header[level],msg)
191 if level == 4:
191 if level == 4:
192 print >> Term.cerr,'Exiting.\n'
192 print >> Term.cerr,'Exiting.\n'
193 sys.exit(exit_val)
193 sys.exit(exit_val)
194
194
195 def info(msg):
195 def info(msg):
196 """Equivalent to warn(msg,level=1)."""
196 """Equivalent to warn(msg,level=1)."""
197
197
198 warn(msg,level=1)
198 warn(msg,level=1)
199
199
200 def error(msg):
200 def error(msg):
201 """Equivalent to warn(msg,level=3)."""
201 """Equivalent to warn(msg,level=3)."""
202
202
203 warn(msg,level=3)
203 warn(msg,level=3)
204
204
205 def fatal(msg,exit_val=1):
205 def fatal(msg,exit_val=1):
206 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
206 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
207
207
208 warn(msg,exit_val=exit_val,level=4)
208 warn(msg,exit_val=exit_val,level=4)
209
209
210
210 #---------------------------------------------------------------------------
211 # useful for debugging
211 # Debugging routines
212 def debugp(expr,pre_msg=''):
212 #
213 def debugx(expr,pre_msg=''):
213 """Print the value of an expression from the caller's frame.
214 """Print the value of an expression from the caller's frame.
214
215
215 Takes an expression, evaluates it in the caller's frame and prints both
216 Takes an expression, evaluates it in the caller's frame and prints both
216 the given expression and the resulting value (as well as a debug mark
217 the given expression and the resulting value (as well as a debug mark
217 indicating the name of the calling function. The input must be of a form
218 indicating the name of the calling function. The input must be of a form
218 suitable for eval().
219 suitable for eval().
219
220
220 An optional message can be passed, which will be prepended to the printed
221 An optional message can be passed, which will be prepended to the printed
221 expr->value pair."""
222 expr->value pair."""
222
223
223 cf = sys._getframe(1)
224 cf = sys._getframe(1)
224 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
225 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
225 eval(expr,cf.f_globals,cf.f_locals))
226 eval(expr,cf.f_globals,cf.f_locals))
226
227
227 # deactivate it by uncommenting the following line, which makes it a no-op
228 # deactivate it by uncommenting the following line, which makes it a no-op
228 def debugp(expr,pre_msg=''): pass
229 #def debugx(expr,pre_msg=''): pass
229
230
230 #----------------------------------------------------------------------------
231 #----------------------------------------------------------------------------
231 StringTypes = types.StringTypes
232 StringTypes = types.StringTypes
232
233
233 # Basic timing functionality
234 # Basic timing functionality
234
235
235 # If possible (Unix), use the resource module instead of time.clock()
236 # If possible (Unix), use the resource module instead of time.clock()
236 try:
237 try:
237 import resource
238 import resource
238 def clock():
239 def clock():
239 """clock() -> floating point number
240 """clock() -> floating point number
240
241
241 Return the CPU time in seconds (user time only, system time is
242 Return the CPU time in seconds (user time only, system time is
242 ignored) since the start of the process. This is done via a call to
243 ignored) since the start of the process. This is done via a call to
243 resource.getrusage, so it avoids the wraparound problems in
244 resource.getrusage, so it avoids the wraparound problems in
244 time.clock()."""
245 time.clock()."""
245
246
246 return resource.getrusage(resource.RUSAGE_SELF)[0]
247 return resource.getrusage(resource.RUSAGE_SELF)[0]
247
248
248 def clock2():
249 def clock2():
249 """clock2() -> (t_user,t_system)
250 """clock2() -> (t_user,t_system)
250
251
251 Similar to clock(), but return a tuple of user/system times."""
252 Similar to clock(), but return a tuple of user/system times."""
252 return resource.getrusage(resource.RUSAGE_SELF)[:2]
253 return resource.getrusage(resource.RUSAGE_SELF)[:2]
253
254
254 except ImportError:
255 except ImportError:
255 clock = time.clock
256 clock = time.clock
256 def clock2():
257 def clock2():
257 """Under windows, system CPU time can't be measured.
258 """Under windows, system CPU time can't be measured.
258
259
259 This just returns clock() and zero."""
260 This just returns clock() and zero."""
260 return time.clock(),0.0
261 return time.clock(),0.0
261
262
262 def timings_out(reps,func,*args,**kw):
263 def timings_out(reps,func,*args,**kw):
263 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
264 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
264
265
265 Execute a function reps times, return a tuple with the elapsed total
266 Execute a function reps times, return a tuple with the elapsed total
266 CPU time in seconds, the time per call and the function's output.
267 CPU time in seconds, the time per call and the function's output.
267
268
268 Under Unix, the return value is the sum of user+system time consumed by
269 Under Unix, the return value is the sum of user+system time consumed by
269 the process, computed via the resource module. This prevents problems
270 the process, computed via the resource module. This prevents problems
270 related to the wraparound effect which the time.clock() function has.
271 related to the wraparound effect which the time.clock() function has.
271
272
272 Under Windows the return value is in wall clock seconds. See the
273 Under Windows the return value is in wall clock seconds. See the
273 documentation for the time module for more details."""
274 documentation for the time module for more details."""
274
275
275 reps = int(reps)
276 reps = int(reps)
276 assert reps >=1, 'reps must be >= 1'
277 assert reps >=1, 'reps must be >= 1'
277 if reps==1:
278 if reps==1:
278 start = clock()
279 start = clock()
279 out = func(*args,**kw)
280 out = func(*args,**kw)
280 tot_time = clock()-start
281 tot_time = clock()-start
281 else:
282 else:
282 rng = xrange(reps-1) # the last time is executed separately to store output
283 rng = xrange(reps-1) # the last time is executed separately to store output
283 start = clock()
284 start = clock()
284 for dummy in rng: func(*args,**kw)
285 for dummy in rng: func(*args,**kw)
285 out = func(*args,**kw) # one last time
286 out = func(*args,**kw) # one last time
286 tot_time = clock()-start
287 tot_time = clock()-start
287 av_time = tot_time / reps
288 av_time = tot_time / reps
288 return tot_time,av_time,out
289 return tot_time,av_time,out
289
290
290 def timings(reps,func,*args,**kw):
291 def timings(reps,func,*args,**kw):
291 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
292 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
292
293
293 Execute a function reps times, return a tuple with the elapsed total CPU
294 Execute a function reps times, return a tuple with the elapsed total CPU
294 time in seconds and the time per call. These are just the first two values
295 time in seconds and the time per call. These are just the first two values
295 in timings_out()."""
296 in timings_out()."""
296
297
297 return timings_out(reps,func,*args,**kw)[0:2]
298 return timings_out(reps,func,*args,**kw)[0:2]
298
299
299 def timing(func,*args,**kw):
300 def timing(func,*args,**kw):
300 """timing(func,*args,**kw) -> t_total
301 """timing(func,*args,**kw) -> t_total
301
302
302 Execute a function once, return the elapsed total CPU time in
303 Execute a function once, return the elapsed total CPU time in
303 seconds. This is just the first value in timings_out()."""
304 seconds. This is just the first value in timings_out()."""
304
305
305 return timings_out(1,func,*args,**kw)[0]
306 return timings_out(1,func,*args,**kw)[0]
306
307
307 #****************************************************************************
308 #****************************************************************************
308 # file and system
309 # file and system
309
310
310 def system(cmd,verbose=0,debug=0,header=''):
311 def system(cmd,verbose=0,debug=0,header=''):
311 """Execute a system command, return its exit status.
312 """Execute a system command, return its exit status.
312
313
313 Options:
314 Options:
314
315
315 - verbose (0): print the command to be executed.
316 - verbose (0): print the command to be executed.
316
317
317 - debug (0): only print, do not actually execute.
318 - debug (0): only print, do not actually execute.
318
319
319 - header (''): Header to print on screen prior to the executed command (it
320 - header (''): Header to print on screen prior to the executed command (it
320 is only prepended to the command, no newlines are added).
321 is only prepended to the command, no newlines are added).
321
322
322 Note: a stateful version of this function is available through the
323 Note: a stateful version of this function is available through the
323 SystemExec class."""
324 SystemExec class."""
324
325
325 stat = 0
326 stat = 0
326 if verbose or debug: print header+cmd
327 if verbose or debug: print header+cmd
327 sys.stdout.flush()
328 sys.stdout.flush()
328 if not debug: stat = os.system(cmd)
329 if not debug: stat = os.system(cmd)
329 return stat
330 return stat
330
331
331 # This function is used by ipython in a lot of places to make system calls.
332 # This function is used by ipython in a lot of places to make system calls.
332 # We need it to be slightly different under win32, due to the vagaries of
333 # We need it to be slightly different under win32, due to the vagaries of
333 # 'network shares'. A win32 override is below.
334 # 'network shares'. A win32 override is below.
334
335
335 def shell(cmd,verbose=0,debug=0,header=''):
336 def shell(cmd,verbose=0,debug=0,header=''):
336 """Execute a command in the system shell, always return None.
337 """Execute a command in the system shell, always return None.
337
338
338 Options:
339 Options:
339
340
340 - verbose (0): print the command to be executed.
341 - verbose (0): print the command to be executed.
341
342
342 - debug (0): only print, do not actually execute.
343 - debug (0): only print, do not actually execute.
343
344
344 - header (''): Header to print on screen prior to the executed command (it
345 - header (''): Header to print on screen prior to the executed command (it
345 is only prepended to the command, no newlines are added).
346 is only prepended to the command, no newlines are added).
346
347
347 Note: this is similar to genutils.system(), but it returns None so it can
348 Note: this is similar to genutils.system(), but it returns None so it can
348 be conveniently used in interactive loops without getting the return value
349 be conveniently used in interactive loops without getting the return value
349 (typically 0) printed many times."""
350 (typically 0) printed many times."""
350
351
351 stat = 0
352 stat = 0
352 if verbose or debug: print header+cmd
353 if verbose or debug: print header+cmd
353 # flush stdout so we don't mangle python's buffering
354 # flush stdout so we don't mangle python's buffering
354 sys.stdout.flush()
355 sys.stdout.flush()
355 if not debug:
356 if not debug:
356 os.system(cmd)
357 os.system(cmd)
357
358
358 # override shell() for win32 to deal with network shares
359 # override shell() for win32 to deal with network shares
359 if os.name in ('nt','dos'):
360 if os.name in ('nt','dos'):
360
361
361 shell_ori = shell
362 shell_ori = shell
362
363
363 def shell(cmd,verbose=0,debug=0,header=''):
364 def shell(cmd,verbose=0,debug=0,header=''):
364 if os.getcwd().startswith(r"\\"):
365 if os.getcwd().startswith(r"\\"):
365 path = os.getcwd()
366 path = os.getcwd()
366 # change to c drive (cannot be on UNC-share when issuing os.system,
367 # change to c drive (cannot be on UNC-share when issuing os.system,
367 # as cmd.exe cannot handle UNC addresses)
368 # as cmd.exe cannot handle UNC addresses)
368 os.chdir("c:")
369 os.chdir("c:")
369 # issue pushd to the UNC-share and then run the command
370 # issue pushd to the UNC-share and then run the command
370 try:
371 try:
371 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
372 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
372 finally:
373 finally:
373 os.chdir(path)
374 os.chdir(path)
374 else:
375 else:
375 shell_ori(cmd,verbose,debug,header)
376 shell_ori(cmd,verbose,debug,header)
376
377
377 shell.__doc__ = shell_ori.__doc__
378 shell.__doc__ = shell_ori.__doc__
378
379
379 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
380 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
380 """Dummy substitute for perl's backquotes.
381 """Dummy substitute for perl's backquotes.
381
382
382 Executes a command and returns the output.
383 Executes a command and returns the output.
383
384
384 Accepts the same arguments as system(), plus:
385 Accepts the same arguments as system(), plus:
385
386
386 - split(0): if true, the output is returned as a list split on newlines.
387 - split(0): if true, the output is returned as a list split on newlines.
387
388
388 Note: a stateful version of this function is available through the
389 Note: a stateful version of this function is available through the
389 SystemExec class."""
390 SystemExec class."""
390
391
391 if verbose or debug: print header+cmd
392 if verbose or debug: print header+cmd
392 if not debug:
393 if not debug:
393 output = commands.getoutput(cmd)
394 output = commands.getoutput(cmd)
394 if split:
395 if split:
395 return output.split('\n')
396 return output.split('\n')
396 else:
397 else:
397 return output
398 return output
398
399
399 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
400 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
400 """Return (standard output,standard error) of executing cmd in a shell.
401 """Return (standard output,standard error) of executing cmd in a shell.
401
402
402 Accepts the same arguments as system(), plus:
403 Accepts the same arguments as system(), plus:
403
404
404 - split(0): if true, each of stdout/err is returned as a list split on
405 - split(0): if true, each of stdout/err is returned as a list split on
405 newlines.
406 newlines.
406
407
407 Note: a stateful version of this function is available through the
408 Note: a stateful version of this function is available through the
408 SystemExec class."""
409 SystemExec class."""
409
410
410 if verbose or debug: print header+cmd
411 if verbose or debug: print header+cmd
411 if not cmd:
412 if not cmd:
412 if split:
413 if split:
413 return [],[]
414 return [],[]
414 else:
415 else:
415 return '',''
416 return '',''
416 if not debug:
417 if not debug:
417 pin,pout,perr = os.popen3(cmd)
418 pin,pout,perr = os.popen3(cmd)
418 tout = pout.read().rstrip()
419 tout = pout.read().rstrip()
419 terr = perr.read().rstrip()
420 terr = perr.read().rstrip()
420 pin.close()
421 pin.close()
421 pout.close()
422 pout.close()
422 perr.close()
423 perr.close()
423 if split:
424 if split:
424 return tout.split('\n'),terr.split('\n')
425 return tout.split('\n'),terr.split('\n')
425 else:
426 else:
426 return tout,terr
427 return tout,terr
427
428
428 # for compatibility with older naming conventions
429 # for compatibility with older naming conventions
429 xsys = system
430 xsys = system
430 bq = getoutput
431 bq = getoutput
431
432
432 class SystemExec:
433 class SystemExec:
433 """Access the system and getoutput functions through a stateful interface.
434 """Access the system and getoutput functions through a stateful interface.
434
435
435 Note: here we refer to the system and getoutput functions from this
436 Note: here we refer to the system and getoutput functions from this
436 library, not the ones from the standard python library.
437 library, not the ones from the standard python library.
437
438
438 This class offers the system and getoutput functions as methods, but the
439 This class offers the system and getoutput functions as methods, but the
439 verbose, debug and header parameters can be set for the instance (at
440 verbose, debug and header parameters can be set for the instance (at
440 creation time or later) so that they don't need to be specified on each
441 creation time or later) so that they don't need to be specified on each
441 call.
442 call.
442
443
443 For efficiency reasons, there's no way to override the parameters on a
444 For efficiency reasons, there's no way to override the parameters on a
444 per-call basis other than by setting instance attributes. If you need
445 per-call basis other than by setting instance attributes. If you need
445 local overrides, it's best to directly call system() or getoutput().
446 local overrides, it's best to directly call system() or getoutput().
446
447
447 The following names are provided as alternate options:
448 The following names are provided as alternate options:
448 - xsys: alias to system
449 - xsys: alias to system
449 - bq: alias to getoutput
450 - bq: alias to getoutput
450
451
451 An instance can then be created as:
452 An instance can then be created as:
452 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
453 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
453
454
454 And used as:
455 And used as:
455 >>> sysexec.xsys('pwd')
456 >>> sysexec.xsys('pwd')
456 >>> dirlist = sysexec.bq('ls -l')
457 >>> dirlist = sysexec.bq('ls -l')
457 """
458 """
458
459
459 def __init__(self,verbose=0,debug=0,header='',split=0):
460 def __init__(self,verbose=0,debug=0,header='',split=0):
460 """Specify the instance's values for verbose, debug and header."""
461 """Specify the instance's values for verbose, debug and header."""
461 setattr_list(self,'verbose debug header split')
462 setattr_list(self,'verbose debug header split')
462
463
463 def system(self,cmd):
464 def system(self,cmd):
464 """Stateful interface to system(), with the same keyword parameters."""
465 """Stateful interface to system(), with the same keyword parameters."""
465
466
466 system(cmd,self.verbose,self.debug,self.header)
467 system(cmd,self.verbose,self.debug,self.header)
467
468
468 def shell(self,cmd):
469 def shell(self,cmd):
469 """Stateful interface to shell(), with the same keyword parameters."""
470 """Stateful interface to shell(), with the same keyword parameters."""
470
471
471 shell(cmd,self.verbose,self.debug,self.header)
472 shell(cmd,self.verbose,self.debug,self.header)
472
473
473 xsys = system # alias
474 xsys = system # alias
474
475
475 def getoutput(self,cmd):
476 def getoutput(self,cmd):
476 """Stateful interface to getoutput()."""
477 """Stateful interface to getoutput()."""
477
478
478 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
479 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
479
480
480 def getoutputerror(self,cmd):
481 def getoutputerror(self,cmd):
481 """Stateful interface to getoutputerror()."""
482 """Stateful interface to getoutputerror()."""
482
483
483 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
484 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
484
485
485 bq = getoutput # alias
486 bq = getoutput # alias
486
487
487 #-----------------------------------------------------------------------------
488 #-----------------------------------------------------------------------------
488 def mutex_opts(dict,ex_op):
489 def mutex_opts(dict,ex_op):
489 """Check for presence of mutually exclusive keys in a dict.
490 """Check for presence of mutually exclusive keys in a dict.
490
491
491 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
492 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
492 for op1,op2 in ex_op:
493 for op1,op2 in ex_op:
493 if op1 in dict and op2 in dict:
494 if op1 in dict and op2 in dict:
494 raise ValueError,'\n*** ERROR in Arguments *** '\
495 raise ValueError,'\n*** ERROR in Arguments *** '\
495 'Options '+op1+' and '+op2+' are mutually exclusive.'
496 'Options '+op1+' and '+op2+' are mutually exclusive.'
496
497
497 #-----------------------------------------------------------------------------
498 #-----------------------------------------------------------------------------
498 def get_py_filename(name):
499 def get_py_filename(name):
499 """Return a valid python filename in the current directory.
500 """Return a valid python filename in the current directory.
500
501
501 If the given name is not a file, it adds '.py' and searches again.
502 If the given name is not a file, it adds '.py' and searches again.
502 Raises IOError with an informative message if the file isn't found."""
503 Raises IOError with an informative message if the file isn't found."""
503
504
504 name = os.path.expanduser(name)
505 name = os.path.expanduser(name)
505 if not os.path.isfile(name) and not name.endswith('.py'):
506 if not os.path.isfile(name) and not name.endswith('.py'):
506 name += '.py'
507 name += '.py'
507 if os.path.isfile(name):
508 if os.path.isfile(name):
508 return name
509 return name
509 else:
510 else:
510 raise IOError,'File `%s` not found.' % name
511 raise IOError,'File `%s` not found.' % name
511
512
512 #-----------------------------------------------------------------------------
513 #-----------------------------------------------------------------------------
513 def filefind(fname,alt_dirs = None):
514 def filefind(fname,alt_dirs = None):
514 """Return the given filename either in the current directory, if it
515 """Return the given filename either in the current directory, if it
515 exists, or in a specified list of directories.
516 exists, or in a specified list of directories.
516
517
517 ~ expansion is done on all file and directory names.
518 ~ expansion is done on all file and directory names.
518
519
519 Upon an unsuccessful search, raise an IOError exception."""
520 Upon an unsuccessful search, raise an IOError exception."""
520
521
521 if alt_dirs is None:
522 if alt_dirs is None:
522 try:
523 try:
523 alt_dirs = get_home_dir()
524 alt_dirs = get_home_dir()
524 except HomeDirError:
525 except HomeDirError:
525 alt_dirs = os.getcwd()
526 alt_dirs = os.getcwd()
526 search = [fname] + list_strings(alt_dirs)
527 search = [fname] + list_strings(alt_dirs)
527 search = map(os.path.expanduser,search)
528 search = map(os.path.expanduser,search)
528 #print 'search list for',fname,'list:',search # dbg
529 #print 'search list for',fname,'list:',search # dbg
529 fname = search[0]
530 fname = search[0]
530 if os.path.isfile(fname):
531 if os.path.isfile(fname):
531 return fname
532 return fname
532 for direc in search[1:]:
533 for direc in search[1:]:
533 testname = os.path.join(direc,fname)
534 testname = os.path.join(direc,fname)
534 #print 'testname',testname # dbg
535 #print 'testname',testname # dbg
535 if os.path.isfile(testname):
536 if os.path.isfile(testname):
536 return testname
537 return testname
537 raise IOError,'File' + `fname` + \
538 raise IOError,'File' + `fname` + \
538 ' not found in current or supplied directories:' + `alt_dirs`
539 ' not found in current or supplied directories:' + `alt_dirs`
539
540
540 #----------------------------------------------------------------------------
541 #----------------------------------------------------------------------------
541 def file_read(filename):
542 def file_read(filename):
542 """Read a file and close it. Returns the file source."""
543 """Read a file and close it. Returns the file source."""
543 fobj=open(filename,'r');
544 fobj=open(filename,'r');
544 source = fobj.read();
545 source = fobj.read();
545 fobj.close()
546 fobj.close()
546 return source
547 return source
547
548
548 #----------------------------------------------------------------------------
549 #----------------------------------------------------------------------------
549 def target_outdated(target,deps):
550 def target_outdated(target,deps):
550 """Determine whether a target is out of date.
551 """Determine whether a target is out of date.
551
552
552 target_outdated(target,deps) -> 1/0
553 target_outdated(target,deps) -> 1/0
553
554
554 deps: list of filenames which MUST exist.
555 deps: list of filenames which MUST exist.
555 target: single filename which may or may not exist.
556 target: single filename which may or may not exist.
556
557
557 If target doesn't exist or is older than any file listed in deps, return
558 If target doesn't exist or is older than any file listed in deps, return
558 true, otherwise return false.
559 true, otherwise return false.
559 """
560 """
560 try:
561 try:
561 target_time = os.path.getmtime(target)
562 target_time = os.path.getmtime(target)
562 except os.error:
563 except os.error:
563 return 1
564 return 1
564 for dep in deps:
565 for dep in deps:
565 dep_time = os.path.getmtime(dep)
566 dep_time = os.path.getmtime(dep)
566 if dep_time > target_time:
567 if dep_time > target_time:
567 #print "For target",target,"Dep failed:",dep # dbg
568 #print "For target",target,"Dep failed:",dep # dbg
568 #print "times (dep,tar):",dep_time,target_time # dbg
569 #print "times (dep,tar):",dep_time,target_time # dbg
569 return 1
570 return 1
570 return 0
571 return 0
571
572
572 #-----------------------------------------------------------------------------
573 #-----------------------------------------------------------------------------
573 def target_update(target,deps,cmd):
574 def target_update(target,deps,cmd):
574 """Update a target with a given command given a list of dependencies.
575 """Update a target with a given command given a list of dependencies.
575
576
576 target_update(target,deps,cmd) -> runs cmd if target is outdated.
577 target_update(target,deps,cmd) -> runs cmd if target is outdated.
577
578
578 This is just a wrapper around target_outdated() which calls the given
579 This is just a wrapper around target_outdated() which calls the given
579 command if target is outdated."""
580 command if target is outdated."""
580
581
581 if target_outdated(target,deps):
582 if target_outdated(target,deps):
582 xsys(cmd)
583 xsys(cmd)
583
584
584 #----------------------------------------------------------------------------
585 #----------------------------------------------------------------------------
585 def unquote_ends(istr):
586 def unquote_ends(istr):
586 """Remove a single pair of quotes from the endpoints of a string."""
587 """Remove a single pair of quotes from the endpoints of a string."""
587
588
588 if not istr:
589 if not istr:
589 return istr
590 return istr
590 if (istr[0]=="'" and istr[-1]=="'") or \
591 if (istr[0]=="'" and istr[-1]=="'") or \
591 (istr[0]=='"' and istr[-1]=='"'):
592 (istr[0]=='"' and istr[-1]=='"'):
592 return istr[1:-1]
593 return istr[1:-1]
593 else:
594 else:
594 return istr
595 return istr
595
596
596 #----------------------------------------------------------------------------
597 #----------------------------------------------------------------------------
597 def process_cmdline(argv,names=[],defaults={},usage=''):
598 def process_cmdline(argv,names=[],defaults={},usage=''):
598 """ Process command-line options and arguments.
599 """ Process command-line options and arguments.
599
600
600 Arguments:
601 Arguments:
601
602
602 - argv: list of arguments, typically sys.argv.
603 - argv: list of arguments, typically sys.argv.
603
604
604 - names: list of option names. See DPyGetOpt docs for details on options
605 - names: list of option names. See DPyGetOpt docs for details on options
605 syntax.
606 syntax.
606
607
607 - defaults: dict of default values.
608 - defaults: dict of default values.
608
609
609 - usage: optional usage notice to print if a wrong argument is passed.
610 - usage: optional usage notice to print if a wrong argument is passed.
610
611
611 Return a dict of options and a list of free arguments."""
612 Return a dict of options and a list of free arguments."""
612
613
613 getopt = DPyGetOpt.DPyGetOpt()
614 getopt = DPyGetOpt.DPyGetOpt()
614 getopt.setIgnoreCase(0)
615 getopt.setIgnoreCase(0)
615 getopt.parseConfiguration(names)
616 getopt.parseConfiguration(names)
616
617
617 try:
618 try:
618 getopt.processArguments(argv)
619 getopt.processArguments(argv)
619 except:
620 except:
620 print usage
621 print usage
621 warn(`sys.exc_value`,level=4)
622 warn(`sys.exc_value`,level=4)
622
623
623 defaults.update(getopt.optionValues)
624 defaults.update(getopt.optionValues)
624 args = getopt.freeValues
625 args = getopt.freeValues
625
626
626 return defaults,args
627 return defaults,args
627
628
628 #----------------------------------------------------------------------------
629 #----------------------------------------------------------------------------
629 def optstr2types(ostr):
630 def optstr2types(ostr):
630 """Convert a string of option names to a dict of type mappings.
631 """Convert a string of option names to a dict of type mappings.
631
632
632 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
633 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
633
634
634 This is used to get the types of all the options in a string formatted
635 This is used to get the types of all the options in a string formatted
635 with the conventions of DPyGetOpt. The 'type' None is used for options
636 with the conventions of DPyGetOpt. The 'type' None is used for options
636 which are strings (they need no further conversion). This function's main
637 which are strings (they need no further conversion). This function's main
637 use is to get a typemap for use with read_dict().
638 use is to get a typemap for use with read_dict().
638 """
639 """
639
640
640 typeconv = {None:'',int:'',float:''}
641 typeconv = {None:'',int:'',float:''}
641 typemap = {'s':None,'i':int,'f':float}
642 typemap = {'s':None,'i':int,'f':float}
642 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
643 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
643
644
644 for w in ostr.split():
645 for w in ostr.split():
645 oname,alias,otype = opt_re.match(w).groups()
646 oname,alias,otype = opt_re.match(w).groups()
646 if otype == '' or alias == '!': # simple switches are integers too
647 if otype == '' or alias == '!': # simple switches are integers too
647 otype = 'i'
648 otype = 'i'
648 typeconv[typemap[otype]] += oname + ' '
649 typeconv[typemap[otype]] += oname + ' '
649 return typeconv
650 return typeconv
650
651
651 #----------------------------------------------------------------------------
652 #----------------------------------------------------------------------------
652 def read_dict(filename,type_conv=None,**opt):
653 def read_dict(filename,type_conv=None,**opt):
653
654
654 """Read a dictionary of key=value pairs from an input file, optionally
655 """Read a dictionary of key=value pairs from an input file, optionally
655 performing conversions on the resulting values.
656 performing conversions on the resulting values.
656
657
657 read_dict(filename,type_conv,**opt) -> dict
658 read_dict(filename,type_conv,**opt) -> dict
658
659
659 Only one value per line is accepted, the format should be
660 Only one value per line is accepted, the format should be
660 # optional comments are ignored
661 # optional comments are ignored
661 key value\n
662 key value\n
662
663
663 Args:
664 Args:
664
665
665 - type_conv: A dictionary specifying which keys need to be converted to
666 - type_conv: A dictionary specifying which keys need to be converted to
666 which types. By default all keys are read as strings. This dictionary
667 which types. By default all keys are read as strings. This dictionary
667 should have as its keys valid conversion functions for strings
668 should have as its keys valid conversion functions for strings
668 (int,long,float,complex, or your own). The value for each key
669 (int,long,float,complex, or your own). The value for each key
669 (converter) should be a whitespace separated string containing the names
670 (converter) should be a whitespace separated string containing the names
670 of all the entries in the file to be converted using that function. For
671 of all the entries in the file to be converted using that function. For
671 keys to be left alone, use None as the conversion function (only needed
672 keys to be left alone, use None as the conversion function (only needed
672 with purge=1, see below).
673 with purge=1, see below).
673
674
674 - opt: dictionary with extra options as below (default in parens)
675 - opt: dictionary with extra options as below (default in parens)
675
676
676 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
677 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
677 of the dictionary to be returned. If purge is going to be used, the
678 of the dictionary to be returned. If purge is going to be used, the
678 set of keys to be left as strings also has to be explicitly specified
679 set of keys to be left as strings also has to be explicitly specified
679 using the (non-existent) conversion function None.
680 using the (non-existent) conversion function None.
680
681
681 fs(None): field separator. This is the key/value separator to be used
682 fs(None): field separator. This is the key/value separator to be used
682 when parsing the file. The None default means any whitespace [behavior
683 when parsing the file. The None default means any whitespace [behavior
683 of string.split()].
684 of string.split()].
684
685
685 strip(0): if 1, strip string values of leading/trailinig whitespace.
686 strip(0): if 1, strip string values of leading/trailinig whitespace.
686
687
687 warn(1): warning level if requested keys are not found in file.
688 warn(1): warning level if requested keys are not found in file.
688 - 0: silently ignore.
689 - 0: silently ignore.
689 - 1: inform but proceed.
690 - 1: inform but proceed.
690 - 2: raise KeyError exception.
691 - 2: raise KeyError exception.
691
692
692 no_empty(0): if 1, remove keys with whitespace strings as a value.
693 no_empty(0): if 1, remove keys with whitespace strings as a value.
693
694
694 unique([]): list of keys (or space separated string) which can't be
695 unique([]): list of keys (or space separated string) which can't be
695 repeated. If one such key is found in the file, each new instance
696 repeated. If one such key is found in the file, each new instance
696 overwrites the previous one. For keys not listed here, the behavior is
697 overwrites the previous one. For keys not listed here, the behavior is
697 to make a list of all appearances.
698 to make a list of all appearances.
698
699
699 Example:
700 Example:
700 If the input file test.ini has:
701 If the input file test.ini has:
701 i 3
702 i 3
702 x 4.5
703 x 4.5
703 y 5.5
704 y 5.5
704 s hi ho
705 s hi ho
705 Then:
706 Then:
706
707
707 >>> type_conv={int:'i',float:'x',None:'s'}
708 >>> type_conv={int:'i',float:'x',None:'s'}
708 >>> read_dict('test.ini')
709 >>> read_dict('test.ini')
709 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
710 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
710 >>> read_dict('test.ini',type_conv)
711 >>> read_dict('test.ini',type_conv)
711 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
712 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
712 >>> read_dict('test.ini',type_conv,purge=1)
713 >>> read_dict('test.ini',type_conv,purge=1)
713 {'i': 3, 's': 'hi ho', 'x': 4.5}
714 {'i': 3, 's': 'hi ho', 'x': 4.5}
714 """
715 """
715
716
716 # starting config
717 # starting config
717 opt.setdefault('purge',0)
718 opt.setdefault('purge',0)
718 opt.setdefault('fs',None) # field sep defaults to any whitespace
719 opt.setdefault('fs',None) # field sep defaults to any whitespace
719 opt.setdefault('strip',0)
720 opt.setdefault('strip',0)
720 opt.setdefault('warn',1)
721 opt.setdefault('warn',1)
721 opt.setdefault('no_empty',0)
722 opt.setdefault('no_empty',0)
722 opt.setdefault('unique','')
723 opt.setdefault('unique','')
723 if type(opt['unique']) in StringTypes:
724 if type(opt['unique']) in StringTypes:
724 unique_keys = qw(opt['unique'])
725 unique_keys = qw(opt['unique'])
725 elif type(opt['unique']) in (types.TupleType,types.ListType):
726 elif type(opt['unique']) in (types.TupleType,types.ListType):
726 unique_keys = opt['unique']
727 unique_keys = opt['unique']
727 else:
728 else:
728 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
729 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
729
730
730 dict = {}
731 dict = {}
731 # first read in table of values as strings
732 # first read in table of values as strings
732 file = open(filename,'r')
733 file = open(filename,'r')
733 for line in file.readlines():
734 for line in file.readlines():
734 line = line.strip()
735 line = line.strip()
735 if len(line) and line[0]=='#': continue
736 if len(line) and line[0]=='#': continue
736 if len(line)>0:
737 if len(line)>0:
737 lsplit = line.split(opt['fs'],1)
738 lsplit = line.split(opt['fs'],1)
738 try:
739 try:
739 key,val = lsplit
740 key,val = lsplit
740 except ValueError:
741 except ValueError:
741 key,val = lsplit[0],''
742 key,val = lsplit[0],''
742 key = key.strip()
743 key = key.strip()
743 if opt['strip']: val = val.strip()
744 if opt['strip']: val = val.strip()
744 if val == "''" or val == '""': val = ''
745 if val == "''" or val == '""': val = ''
745 if opt['no_empty'] and (val=='' or val.isspace()):
746 if opt['no_empty'] and (val=='' or val.isspace()):
746 continue
747 continue
747 # if a key is found more than once in the file, build a list
748 # if a key is found more than once in the file, build a list
748 # unless it's in the 'unique' list. In that case, last found in file
749 # unless it's in the 'unique' list. In that case, last found in file
749 # takes precedence. User beware.
750 # takes precedence. User beware.
750 try:
751 try:
751 if dict[key] and key in unique_keys:
752 if dict[key] and key in unique_keys:
752 dict[key] = val
753 dict[key] = val
753 elif type(dict[key]) is types.ListType:
754 elif type(dict[key]) is types.ListType:
754 dict[key].append(val)
755 dict[key].append(val)
755 else:
756 else:
756 dict[key] = [dict[key],val]
757 dict[key] = [dict[key],val]
757 except KeyError:
758 except KeyError:
758 dict[key] = val
759 dict[key] = val
759 # purge if requested
760 # purge if requested
760 if opt['purge']:
761 if opt['purge']:
761 accepted_keys = qwflat(type_conv.values())
762 accepted_keys = qwflat(type_conv.values())
762 for key in dict.keys():
763 for key in dict.keys():
763 if key in accepted_keys: continue
764 if key in accepted_keys: continue
764 del(dict[key])
765 del(dict[key])
765 # now convert if requested
766 # now convert if requested
766 if type_conv==None: return dict
767 if type_conv==None: return dict
767 conversions = type_conv.keys()
768 conversions = type_conv.keys()
768 try: conversions.remove(None)
769 try: conversions.remove(None)
769 except: pass
770 except: pass
770 for convert in conversions:
771 for convert in conversions:
771 for val in qw(type_conv[convert]):
772 for val in qw(type_conv[convert]):
772 try:
773 try:
773 dict[val] = convert(dict[val])
774 dict[val] = convert(dict[val])
774 except KeyError,e:
775 except KeyError,e:
775 if opt['warn'] == 0:
776 if opt['warn'] == 0:
776 pass
777 pass
777 elif opt['warn'] == 1:
778 elif opt['warn'] == 1:
778 print >>sys.stderr, 'Warning: key',val,\
779 print >>sys.stderr, 'Warning: key',val,\
779 'not found in file',filename
780 'not found in file',filename
780 elif opt['warn'] == 2:
781 elif opt['warn'] == 2:
781 raise KeyError,e
782 raise KeyError,e
782 else:
783 else:
783 raise ValueError,'Warning level must be 0,1 or 2'
784 raise ValueError,'Warning level must be 0,1 or 2'
784
785
785 return dict
786 return dict
786
787
787 #----------------------------------------------------------------------------
788 #----------------------------------------------------------------------------
788 def flag_calls(func):
789 def flag_calls(func):
789 """Wrap a function to detect and flag when it gets called.
790 """Wrap a function to detect and flag when it gets called.
790
791
791 This is a decorator which takes a function and wraps it in a function with
792 This is a decorator which takes a function and wraps it in a function with
792 a 'called' attribute. wrapper.called is initialized to False.
793 a 'called' attribute. wrapper.called is initialized to False.
793
794
794 The wrapper.called attribute is set to False right before each call to the
795 The wrapper.called attribute is set to False right before each call to the
795 wrapped function, so if the call fails it remains False. After the call
796 wrapped function, so if the call fails it remains False. After the call
796 completes, wrapper.called is set to True and the output is returned.
797 completes, wrapper.called is set to True and the output is returned.
797
798
798 Testing for truth in wrapper.called allows you to determine if a call to
799 Testing for truth in wrapper.called allows you to determine if a call to
799 func() was attempted and succeeded."""
800 func() was attempted and succeeded."""
800
801
801 def wrapper(*args,**kw):
802 def wrapper(*args,**kw):
802 wrapper.called = False
803 wrapper.called = False
803 out = func(*args,**kw)
804 out = func(*args,**kw)
804 wrapper.called = True
805 wrapper.called = True
805 return out
806 return out
806
807
807 wrapper.called = False
808 wrapper.called = False
808 wrapper.__doc__ = func.__doc__
809 wrapper.__doc__ = func.__doc__
809 return wrapper
810 return wrapper
810
811
811 #----------------------------------------------------------------------------
812 #----------------------------------------------------------------------------
812 class HomeDirError(Error):
813 class HomeDirError(Error):
813 pass
814 pass
814
815
815 def get_home_dir():
816 def get_home_dir():
816 """Return the closest possible equivalent to a 'home' directory.
817 """Return the closest possible equivalent to a 'home' directory.
817
818
818 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
819 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
819
820
820 Currently only Posix and NT are implemented, a HomeDirError exception is
821 Currently only Posix and NT are implemented, a HomeDirError exception is
821 raised for all other OSes. """
822 raised for all other OSes. """
822
823
823 isdir = os.path.isdir
824 isdir = os.path.isdir
824 env = os.environ
825 env = os.environ
825 try:
826 try:
826 homedir = env['HOME']
827 homedir = env['HOME']
827 if not isdir(homedir):
828 if not isdir(homedir):
828 # in case a user stuck some string which does NOT resolve to a
829 # in case a user stuck some string which does NOT resolve to a
829 # valid path, it's as good as if we hadn't foud it
830 # valid path, it's as good as if we hadn't foud it
830 raise KeyError
831 raise KeyError
831 return homedir
832 return homedir
832 except KeyError:
833 except KeyError:
833 if os.name == 'posix':
834 if os.name == 'posix':
834 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
835 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
835 elif os.name == 'nt':
836 elif os.name == 'nt':
836 # For some strange reason, win9x returns 'nt' for os.name.
837 # For some strange reason, win9x returns 'nt' for os.name.
837 try:
838 try:
838 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
839 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
839 if not isdir(homedir):
840 if not isdir(homedir):
840 homedir = os.path.join(env['USERPROFILE'])
841 homedir = os.path.join(env['USERPROFILE'])
841 if not isdir(homedir):
842 if not isdir(homedir):
842 raise HomeDirError
843 raise HomeDirError
843 return homedir
844 return homedir
844 except:
845 except:
845 try:
846 try:
846 # Use the registry to get the 'My Documents' folder.
847 # Use the registry to get the 'My Documents' folder.
847 import _winreg as wreg
848 import _winreg as wreg
848 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
849 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
849 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
850 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
850 homedir = wreg.QueryValueEx(key,'Personal')[0]
851 homedir = wreg.QueryValueEx(key,'Personal')[0]
851 key.Close()
852 key.Close()
852 if not isdir(homedir):
853 if not isdir(homedir):
853 e = ('Invalid "Personal" folder registry key '
854 e = ('Invalid "Personal" folder registry key '
854 'typically "My Documents".\n'
855 'typically "My Documents".\n'
855 'Value: %s\n'
856 'Value: %s\n'
856 'This is not a valid directory on your system.' %
857 'This is not a valid directory on your system.' %
857 homedir)
858 homedir)
858 raise HomeDirError(e)
859 raise HomeDirError(e)
859 return homedir
860 return homedir
860 except HomeDirError:
861 except HomeDirError:
861 raise
862 raise
862 except:
863 except:
863 return 'C:\\'
864 return 'C:\\'
864 elif os.name == 'dos':
865 elif os.name == 'dos':
865 # Desperate, may do absurd things in classic MacOS. May work under DOS.
866 # Desperate, may do absurd things in classic MacOS. May work under DOS.
866 return 'C:\\'
867 return 'C:\\'
867 else:
868 else:
868 raise HomeDirError,'support for your operating system not implemented.'
869 raise HomeDirError,'support for your operating system not implemented.'
869
870
870 #****************************************************************************
871 #****************************************************************************
871 # strings and text
872 # strings and text
872
873
873 class LSString(str):
874 class LSString(str):
874 """String derivative with a special access attributes.
875 """String derivative with a special access attributes.
875
876
876 These are normal strings, but with the special attributes:
877 These are normal strings, but with the special attributes:
877
878
878 .l (or .list) : value as list (split on newlines).
879 .l (or .list) : value as list (split on newlines).
879 .n (or .nlstr): original value (the string itself).
880 .n (or .nlstr): original value (the string itself).
880 .s (or .spstr): value as whitespace-separated string.
881 .s (or .spstr): value as whitespace-separated string.
881
882
882 Any values which require transformations are computed only once and
883 Any values which require transformations are computed only once and
883 cached.
884 cached.
884
885
885 Such strings are very useful to efficiently interact with the shell, which
886 Such strings are very useful to efficiently interact with the shell, which
886 typically only understands whitespace-separated options for commands."""
887 typically only understands whitespace-separated options for commands."""
887
888
888 def get_list(self):
889 def get_list(self):
889 try:
890 try:
890 return self.__list
891 return self.__list
891 except AttributeError:
892 except AttributeError:
892 self.__list = self.split('\n')
893 self.__list = self.split('\n')
893 return self.__list
894 return self.__list
894
895
895 l = list = property(get_list)
896 l = list = property(get_list)
896
897
897 def get_spstr(self):
898 def get_spstr(self):
898 try:
899 try:
899 return self.__spstr
900 return self.__spstr
900 except AttributeError:
901 except AttributeError:
901 self.__spstr = self.replace('\n',' ')
902 self.__spstr = self.replace('\n',' ')
902 return self.__spstr
903 return self.__spstr
903
904
904 s = spstr = property(get_spstr)
905 s = spstr = property(get_spstr)
905
906
906 def get_nlstr(self):
907 def get_nlstr(self):
907 return self
908 return self
908
909
909 n = nlstr = property(get_nlstr)
910 n = nlstr = property(get_nlstr)
910
911
911 def get_paths(self):
912 def get_paths(self):
912 try:
913 try:
913 return self.__paths
914 return self.__paths
914 except AttributeError:
915 except AttributeError:
915 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
916 self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)]
916 return self.__paths
917 return self.__paths
917
918
918 p = paths = property(get_paths)
919 p = paths = property(get_paths)
919
920
920
921
921 #----------------------------------------------------------------------------
922 #----------------------------------------------------------------------------
922 class SList(list):
923 class SList(list):
923 """List derivative with a special access attributes.
924 """List derivative with a special access attributes.
924
925
925 These are normal lists, but with the special attributes:
926 These are normal lists, but with the special attributes:
926
927
927 .l (or .list) : value as list (the list itself).
928 .l (or .list) : value as list (the list itself).
928 .n (or .nlstr): value as a string, joined on newlines.
929 .n (or .nlstr): value as a string, joined on newlines.
929 .s (or .spstr): value as a string, joined on spaces.
930 .s (or .spstr): value as a string, joined on spaces.
930
931
931 Any values which require transformations are computed only once and
932 Any values which require transformations are computed only once and
932 cached."""
933 cached."""
933
934
934 def get_list(self):
935 def get_list(self):
935 return self
936 return self
936
937
937 l = list = property(get_list)
938 l = list = property(get_list)
938
939
939 def get_spstr(self):
940 def get_spstr(self):
940 try:
941 try:
941 return self.__spstr
942 return self.__spstr
942 except AttributeError:
943 except AttributeError:
943 self.__spstr = ' '.join(self)
944 self.__spstr = ' '.join(self)
944 return self.__spstr
945 return self.__spstr
945
946
946 s = spstr = property(get_spstr)
947 s = spstr = property(get_spstr)
947
948
948 def get_nlstr(self):
949 def get_nlstr(self):
949 try:
950 try:
950 return self.__nlstr
951 return self.__nlstr
951 except AttributeError:
952 except AttributeError:
952 self.__nlstr = '\n'.join(self)
953 self.__nlstr = '\n'.join(self)
953 return self.__nlstr
954 return self.__nlstr
954
955
955 n = nlstr = property(get_nlstr)
956 n = nlstr = property(get_nlstr)
956
957
957 def get_paths(self):
958 def get_paths(self):
958 try:
959 try:
959 return self.__paths
960 return self.__paths
960 except AttributeError:
961 except AttributeError:
961 self.__paths = [path(p) for p in self if os.path.exists(p)]
962 self.__paths = [path(p) for p in self if os.path.exists(p)]
962 return self.__paths
963 return self.__paths
963
964
964 p = paths = property(get_paths)
965 p = paths = property(get_paths)
965
966
966 #----------------------------------------------------------------------------
967 #----------------------------------------------------------------------------
967 def esc_quotes(strng):
968 def esc_quotes(strng):
968 """Return the input string with single and double quotes escaped out"""
969 """Return the input string with single and double quotes escaped out"""
969
970
970 return strng.replace('"','\\"').replace("'","\\'")
971 return strng.replace('"','\\"').replace("'","\\'")
971
972
972 #----------------------------------------------------------------------------
973 #----------------------------------------------------------------------------
973 def make_quoted_expr(s):
974 def make_quoted_expr(s):
974 """Return string s in appropriate quotes, using raw string if possible.
975 """Return string s in appropriate quotes, using raw string if possible.
975
976
976 Effectively this turns string: cd \ao\ao\
977 Effectively this turns string: cd \ao\ao\
977 to: r"cd \ao\ao\_"[:-1]
978 to: r"cd \ao\ao\_"[:-1]
978
979
979 Note the use of raw string and padding at the end to allow trailing backslash.
980 Note the use of raw string and padding at the end to allow trailing backslash.
980
981
981 """
982 """
982
983
983 tail = ''
984 tail = ''
984 tailpadding = ''
985 tailpadding = ''
985 raw = ''
986 raw = ''
986 if "\\" in s:
987 if "\\" in s:
987 raw = 'r'
988 raw = 'r'
988 if s.endswith('\\'):
989 if s.endswith('\\'):
989 tail = '[:-1]'
990 tail = '[:-1]'
990 tailpadding = '_'
991 tailpadding = '_'
991 if '"' not in s:
992 if '"' not in s:
992 quote = '"'
993 quote = '"'
993 elif "'" not in s:
994 elif "'" not in s:
994 quote = "'"
995 quote = "'"
995 elif '"""' not in s and not s.endswith('"'):
996 elif '"""' not in s and not s.endswith('"'):
996 quote = '"""'
997 quote = '"""'
997 elif "'''" not in s and not s.endswith("'"):
998 elif "'''" not in s and not s.endswith("'"):
998 quote = "'''"
999 quote = "'''"
999 else:
1000 else:
1000 # give up, backslash-escaped string will do
1001 # give up, backslash-escaped string will do
1001 return '"%s"' % esc_quotes(s)
1002 return '"%s"' % esc_quotes(s)
1002 res = itpl("$raw$quote$s$tailpadding$quote$tail")
1003 res = itpl("$raw$quote$s$tailpadding$quote$tail")
1003 return res
1004 return res
1004
1005
1005
1006
1006 #----------------------------------------------------------------------------
1007 #----------------------------------------------------------------------------
1007 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1008 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
1008 """Take multiple lines of input.
1009 """Take multiple lines of input.
1009
1010
1010 A list with each line of input as a separate element is returned when a
1011 A list with each line of input as a separate element is returned when a
1011 termination string is entered (defaults to a single '.'). Input can also
1012 termination string is entered (defaults to a single '.'). Input can also
1012 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1013 terminate via EOF (^D in Unix, ^Z-RET in Windows).
1013
1014
1014 Lines of input which end in \\ are joined into single entries (and a
1015 Lines of input which end in \\ are joined into single entries (and a
1015 secondary continuation prompt is issued as long as the user terminates
1016 secondary continuation prompt is issued as long as the user terminates
1016 lines with \\). This allows entering very long strings which are still
1017 lines with \\). This allows entering very long strings which are still
1017 meant to be treated as single entities.
1018 meant to be treated as single entities.
1018 """
1019 """
1019
1020
1020 try:
1021 try:
1021 if header:
1022 if header:
1022 header += '\n'
1023 header += '\n'
1023 lines = [raw_input(header + ps1)]
1024 lines = [raw_input(header + ps1)]
1024 except EOFError:
1025 except EOFError:
1025 return []
1026 return []
1026 terminate = [terminate_str]
1027 terminate = [terminate_str]
1027 try:
1028 try:
1028 while lines[-1:] != terminate:
1029 while lines[-1:] != terminate:
1029 new_line = raw_input(ps1)
1030 new_line = raw_input(ps1)
1030 while new_line.endswith('\\'):
1031 while new_line.endswith('\\'):
1031 new_line = new_line[:-1] + raw_input(ps2)
1032 new_line = new_line[:-1] + raw_input(ps2)
1032 lines.append(new_line)
1033 lines.append(new_line)
1033
1034
1034 return lines[:-1] # don't return the termination command
1035 return lines[:-1] # don't return the termination command
1035 except EOFError:
1036 except EOFError:
1036 print
1037 print
1037 return lines
1038 return lines
1038
1039
1039 #----------------------------------------------------------------------------
1040 #----------------------------------------------------------------------------
1040 def raw_input_ext(prompt='', ps2='... '):
1041 def raw_input_ext(prompt='', ps2='... '):
1041 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1042 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
1042
1043
1043 line = raw_input(prompt)
1044 line = raw_input(prompt)
1044 while line.endswith('\\'):
1045 while line.endswith('\\'):
1045 line = line[:-1] + raw_input(ps2)
1046 line = line[:-1] + raw_input(ps2)
1046 return line
1047 return line
1047
1048
1048 #----------------------------------------------------------------------------
1049 #----------------------------------------------------------------------------
1049 def ask_yes_no(prompt,default=None):
1050 def ask_yes_no(prompt,default=None):
1050 """Asks a question and returns an integer 1/0 (y/n) answer.
1051 """Asks a question and returns an integer 1/0 (y/n) answer.
1051
1052
1052 If default is given (one of 'y','n'), it is used if the user input is
1053 If default is given (one of 'y','n'), it is used if the user input is
1053 empty. Otherwise the question is repeated until an answer is given.
1054 empty. Otherwise the question is repeated until an answer is given.
1054 If EOF occurs 20 times consecutively, the default answer is assumed,
1055 If EOF occurs 20 times consecutively, the default answer is assumed,
1055 or if there is no default, an exception is raised to prevent infinite
1056 or if there is no default, an exception is raised to prevent infinite
1056 loops.
1057 loops.
1057
1058
1058 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1059 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1059
1060
1060 answers = {'y':True,'n':False,'yes':True,'no':False}
1061 answers = {'y':True,'n':False,'yes':True,'no':False}
1061 ans = None
1062 ans = None
1062 eofs, max_eofs = 0, 20
1063 eofs, max_eofs = 0, 20
1063 while ans not in answers.keys():
1064 while ans not in answers.keys():
1064 try:
1065 try:
1065 ans = raw_input(prompt+' ').lower()
1066 ans = raw_input(prompt+' ').lower()
1066 if not ans: # response was an empty string
1067 if not ans: # response was an empty string
1067 ans = default
1068 ans = default
1068 eofs = 0
1069 eofs = 0
1069 except (EOFError,KeyboardInterrupt):
1070 except (EOFError,KeyboardInterrupt):
1070 eofs = eofs + 1
1071 eofs = eofs + 1
1071 if eofs >= max_eofs:
1072 if eofs >= max_eofs:
1072 if default in answers.keys():
1073 if default in answers.keys():
1073 ans = default
1074 ans = default
1074 else:
1075 else:
1075 raise
1076 raise
1076
1077
1077 return answers[ans]
1078 return answers[ans]
1078
1079
1079 #----------------------------------------------------------------------------
1080 #----------------------------------------------------------------------------
1080 def marquee(txt='',width=78,mark='*'):
1081 def marquee(txt='',width=78,mark='*'):
1081 """Return the input string centered in a 'marquee'."""
1082 """Return the input string centered in a 'marquee'."""
1082 if not txt:
1083 if not txt:
1083 return (mark*width)[:width]
1084 return (mark*width)[:width]
1084 nmark = (width-len(txt)-2)/len(mark)/2
1085 nmark = (width-len(txt)-2)/len(mark)/2
1085 if nmark < 0: nmark =0
1086 if nmark < 0: nmark =0
1086 marks = mark*nmark
1087 marks = mark*nmark
1087 return '%s %s %s' % (marks,txt,marks)
1088 return '%s %s %s' % (marks,txt,marks)
1088
1089
1089 #----------------------------------------------------------------------------
1090 #----------------------------------------------------------------------------
1090 class EvalDict:
1091 class EvalDict:
1091 """
1092 """
1092 Emulate a dict which evaluates its contents in the caller's frame.
1093 Emulate a dict which evaluates its contents in the caller's frame.
1093
1094
1094 Usage:
1095 Usage:
1095 >>>number = 19
1096 >>>number = 19
1096 >>>text = "python"
1097 >>>text = "python"
1097 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1098 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1098 """
1099 """
1099
1100
1100 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1101 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1101 # modified (shorter) version of:
1102 # modified (shorter) version of:
1102 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1103 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1103 # Skip Montanaro (skip@pobox.com).
1104 # Skip Montanaro (skip@pobox.com).
1104
1105
1105 def __getitem__(self, name):
1106 def __getitem__(self, name):
1106 frame = sys._getframe(1)
1107 frame = sys._getframe(1)
1107 return eval(name, frame.f_globals, frame.f_locals)
1108 return eval(name, frame.f_globals, frame.f_locals)
1108
1109
1109 EvalString = EvalDict # for backwards compatibility
1110 EvalString = EvalDict # for backwards compatibility
1110 #----------------------------------------------------------------------------
1111 #----------------------------------------------------------------------------
1111 def qw(words,flat=0,sep=None,maxsplit=-1):
1112 def qw(words,flat=0,sep=None,maxsplit=-1):
1112 """Similar to Perl's qw() operator, but with some more options.
1113 """Similar to Perl's qw() operator, but with some more options.
1113
1114
1114 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1115 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1115
1116
1116 words can also be a list itself, and with flat=1, the output will be
1117 words can also be a list itself, and with flat=1, the output will be
1117 recursively flattened. Examples:
1118 recursively flattened. Examples:
1118
1119
1119 >>> qw('1 2')
1120 >>> qw('1 2')
1120 ['1', '2']
1121 ['1', '2']
1121 >>> qw(['a b','1 2',['m n','p q']])
1122 >>> qw(['a b','1 2',['m n','p q']])
1122 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1123 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1123 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1124 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1124 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1125 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1125
1126
1126 if type(words) in StringTypes:
1127 if type(words) in StringTypes:
1127 return [word.strip() for word in words.split(sep,maxsplit)
1128 return [word.strip() for word in words.split(sep,maxsplit)
1128 if word and not word.isspace() ]
1129 if word and not word.isspace() ]
1129 if flat:
1130 if flat:
1130 return flatten(map(qw,words,[1]*len(words)))
1131 return flatten(map(qw,words,[1]*len(words)))
1131 return map(qw,words)
1132 return map(qw,words)
1132
1133
1133 #----------------------------------------------------------------------------
1134 #----------------------------------------------------------------------------
1134 def qwflat(words,sep=None,maxsplit=-1):
1135 def qwflat(words,sep=None,maxsplit=-1):
1135 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1136 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1136 return qw(words,1,sep,maxsplit)
1137 return qw(words,1,sep,maxsplit)
1137
1138
1138 #----------------------------------------------------------------------------
1139 #----------------------------------------------------------------------------
1139 def qw_lol(indata):
1140 def qw_lol(indata):
1140 """qw_lol('a b') -> [['a','b']],
1141 """qw_lol('a b') -> [['a','b']],
1141 otherwise it's just a call to qw().
1142 otherwise it's just a call to qw().
1142
1143
1143 We need this to make sure the modules_some keys *always* end up as a
1144 We need this to make sure the modules_some keys *always* end up as a
1144 list of lists."""
1145 list of lists."""
1145
1146
1146 if type(indata) in StringTypes:
1147 if type(indata) in StringTypes:
1147 return [qw(indata)]
1148 return [qw(indata)]
1148 else:
1149 else:
1149 return qw(indata)
1150 return qw(indata)
1150
1151
1151 #-----------------------------------------------------------------------------
1152 #-----------------------------------------------------------------------------
1152 def list_strings(arg):
1153 def list_strings(arg):
1153 """Always return a list of strings, given a string or list of strings
1154 """Always return a list of strings, given a string or list of strings
1154 as input."""
1155 as input."""
1155
1156
1156 if type(arg) in StringTypes: return [arg]
1157 if type(arg) in StringTypes: return [arg]
1157 else: return arg
1158 else: return arg
1158
1159
1159 #----------------------------------------------------------------------------
1160 #----------------------------------------------------------------------------
1160 def grep(pat,list,case=1):
1161 def grep(pat,list,case=1):
1161 """Simple minded grep-like function.
1162 """Simple minded grep-like function.
1162 grep(pat,list) returns occurrences of pat in list, None on failure.
1163 grep(pat,list) returns occurrences of pat in list, None on failure.
1163
1164
1164 It only does simple string matching, with no support for regexps. Use the
1165 It only does simple string matching, with no support for regexps. Use the
1165 option case=0 for case-insensitive matching."""
1166 option case=0 for case-insensitive matching."""
1166
1167
1167 # This is pretty crude. At least it should implement copying only references
1168 # This is pretty crude. At least it should implement copying only references
1168 # to the original data in case it's big. Now it copies the data for output.
1169 # to the original data in case it's big. Now it copies the data for output.
1169 out=[]
1170 out=[]
1170 if case:
1171 if case:
1171 for term in list:
1172 for term in list:
1172 if term.find(pat)>-1: out.append(term)
1173 if term.find(pat)>-1: out.append(term)
1173 else:
1174 else:
1174 lpat=pat.lower()
1175 lpat=pat.lower()
1175 for term in list:
1176 for term in list:
1176 if term.lower().find(lpat)>-1: out.append(term)
1177 if term.lower().find(lpat)>-1: out.append(term)
1177
1178
1178 if len(out): return out
1179 if len(out): return out
1179 else: return None
1180 else: return None
1180
1181
1181 #----------------------------------------------------------------------------
1182 #----------------------------------------------------------------------------
1182 def dgrep(pat,*opts):
1183 def dgrep(pat,*opts):
1183 """Return grep() on dir()+dir(__builtins__).
1184 """Return grep() on dir()+dir(__builtins__).
1184
1185
1185 A very common use of grep() when working interactively."""
1186 A very common use of grep() when working interactively."""
1186
1187
1187 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1188 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1188
1189
1189 #----------------------------------------------------------------------------
1190 #----------------------------------------------------------------------------
1190 def idgrep(pat):
1191 def idgrep(pat):
1191 """Case-insensitive dgrep()"""
1192 """Case-insensitive dgrep()"""
1192
1193
1193 return dgrep(pat,0)
1194 return dgrep(pat,0)
1194
1195
1195 #----------------------------------------------------------------------------
1196 #----------------------------------------------------------------------------
1196 def igrep(pat,list):
1197 def igrep(pat,list):
1197 """Synonym for case-insensitive grep."""
1198 """Synonym for case-insensitive grep."""
1198
1199
1199 return grep(pat,list,case=0)
1200 return grep(pat,list,case=0)
1200
1201
1201 #----------------------------------------------------------------------------
1202 #----------------------------------------------------------------------------
1202 def indent(str,nspaces=4,ntabs=0):
1203 def indent(str,nspaces=4,ntabs=0):
1203 """Indent a string a given number of spaces or tabstops.
1204 """Indent a string a given number of spaces or tabstops.
1204
1205
1205 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1206 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1206 """
1207 """
1207 if str is None:
1208 if str is None:
1208 return
1209 return
1209 ind = '\t'*ntabs+' '*nspaces
1210 ind = '\t'*ntabs+' '*nspaces
1210 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1211 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1211 if outstr.endswith(os.linesep+ind):
1212 if outstr.endswith(os.linesep+ind):
1212 return outstr[:-len(ind)]
1213 return outstr[:-len(ind)]
1213 else:
1214 else:
1214 return outstr
1215 return outstr
1215
1216
1216 #-----------------------------------------------------------------------------
1217 #-----------------------------------------------------------------------------
1217 def native_line_ends(filename,backup=1):
1218 def native_line_ends(filename,backup=1):
1218 """Convert (in-place) a file to line-ends native to the current OS.
1219 """Convert (in-place) a file to line-ends native to the current OS.
1219
1220
1220 If the optional backup argument is given as false, no backup of the
1221 If the optional backup argument is given as false, no backup of the
1221 original file is left. """
1222 original file is left. """
1222
1223
1223 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1224 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1224
1225
1225 bak_filename = filename + backup_suffixes[os.name]
1226 bak_filename = filename + backup_suffixes[os.name]
1226
1227
1227 original = open(filename).read()
1228 original = open(filename).read()
1228 shutil.copy2(filename,bak_filename)
1229 shutil.copy2(filename,bak_filename)
1229 try:
1230 try:
1230 new = open(filename,'wb')
1231 new = open(filename,'wb')
1231 new.write(os.linesep.join(original.splitlines()))
1232 new.write(os.linesep.join(original.splitlines()))
1232 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1233 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1233 new.close()
1234 new.close()
1234 except:
1235 except:
1235 os.rename(bak_filename,filename)
1236 os.rename(bak_filename,filename)
1236 if not backup:
1237 if not backup:
1237 try:
1238 try:
1238 os.remove(bak_filename)
1239 os.remove(bak_filename)
1239 except:
1240 except:
1240 pass
1241 pass
1241
1242
1242 #----------------------------------------------------------------------------
1243 #----------------------------------------------------------------------------
1243 def get_pager_cmd(pager_cmd = None):
1244 def get_pager_cmd(pager_cmd = None):
1244 """Return a pager command.
1245 """Return a pager command.
1245
1246
1246 Makes some attempts at finding an OS-correct one."""
1247 Makes some attempts at finding an OS-correct one."""
1247
1248
1248 if os.name == 'posix':
1249 if os.name == 'posix':
1249 default_pager_cmd = 'less -r' # -r for color control sequences
1250 default_pager_cmd = 'less -r' # -r for color control sequences
1250 elif os.name in ['nt','dos']:
1251 elif os.name in ['nt','dos']:
1251 default_pager_cmd = 'type'
1252 default_pager_cmd = 'type'
1252
1253
1253 if pager_cmd is None:
1254 if pager_cmd is None:
1254 try:
1255 try:
1255 pager_cmd = os.environ['PAGER']
1256 pager_cmd = os.environ['PAGER']
1256 except:
1257 except:
1257 pager_cmd = default_pager_cmd
1258 pager_cmd = default_pager_cmd
1258 return pager_cmd
1259 return pager_cmd
1259
1260
1260 #-----------------------------------------------------------------------------
1261 #-----------------------------------------------------------------------------
1261 def get_pager_start(pager,start):
1262 def get_pager_start(pager,start):
1262 """Return the string for paging files with an offset.
1263 """Return the string for paging files with an offset.
1263
1264
1264 This is the '+N' argument which less and more (under Unix) accept.
1265 This is the '+N' argument which less and more (under Unix) accept.
1265 """
1266 """
1266
1267
1267 if pager in ['less','more']:
1268 if pager in ['less','more']:
1268 if start:
1269 if start:
1269 start_string = '+' + str(start)
1270 start_string = '+' + str(start)
1270 else:
1271 else:
1271 start_string = ''
1272 start_string = ''
1272 else:
1273 else:
1273 start_string = ''
1274 start_string = ''
1274 return start_string
1275 return start_string
1275
1276
1276 #----------------------------------------------------------------------------
1277 #----------------------------------------------------------------------------
1277 if os.name == "nt":
1278 if os.name == "nt":
1278 import msvcrt
1279 import msvcrt
1279 def page_more():
1280 def page_more():
1280 """ Smart pausing between pages
1281 """ Smart pausing between pages
1281
1282
1282 @return: True if need print more lines, False if quit
1283 @return: True if need print more lines, False if quit
1283 """
1284 """
1284 Term.cout.write('---Return to continue, q to quit--- ')
1285 Term.cout.write('---Return to continue, q to quit--- ')
1285 ans = msvcrt.getch()
1286 ans = msvcrt.getch()
1286 if ans in ("q", "Q"):
1287 if ans in ("q", "Q"):
1287 result = False
1288 result = False
1288 else:
1289 else:
1289 result = True
1290 result = True
1290 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1291 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1291 return result
1292 return result
1292 else:
1293 else:
1293 def page_more():
1294 def page_more():
1294 ans = raw_input('---Return to continue, q to quit--- ')
1295 ans = raw_input('---Return to continue, q to quit--- ')
1295 if ans.lower().startswith('q'):
1296 if ans.lower().startswith('q'):
1296 return False
1297 return False
1297 else:
1298 else:
1298 return True
1299 return True
1299
1300
1300 esc_re = re.compile(r"(\x1b[^m]+m)")
1301 esc_re = re.compile(r"(\x1b[^m]+m)")
1301
1302
1302 def page_dumb(strng,start=0,screen_lines=25):
1303 def page_dumb(strng,start=0,screen_lines=25):
1303 """Very dumb 'pager' in Python, for when nothing else works.
1304 """Very dumb 'pager' in Python, for when nothing else works.
1304
1305
1305 Only moves forward, same interface as page(), except for pager_cmd and
1306 Only moves forward, same interface as page(), except for pager_cmd and
1306 mode."""
1307 mode."""
1307
1308
1308 out_ln = strng.splitlines()[start:]
1309 out_ln = strng.splitlines()[start:]
1309 screens = chop(out_ln,screen_lines-1)
1310 screens = chop(out_ln,screen_lines-1)
1310 if len(screens) == 1:
1311 if len(screens) == 1:
1311 print >>Term.cout, os.linesep.join(screens[0])
1312 print >>Term.cout, os.linesep.join(screens[0])
1312 else:
1313 else:
1313 last_escape = ""
1314 last_escape = ""
1314 for scr in screens[0:-1]:
1315 for scr in screens[0:-1]:
1315 hunk = os.linesep.join(scr)
1316 hunk = os.linesep.join(scr)
1316 print >>Term.cout, last_escape + hunk
1317 print >>Term.cout, last_escape + hunk
1317 if not page_more():
1318 if not page_more():
1318 return
1319 return
1319 esc_list = esc_re.findall(hunk)
1320 esc_list = esc_re.findall(hunk)
1320 if len(esc_list) > 0:
1321 if len(esc_list) > 0:
1321 last_escape = esc_list[-1]
1322 last_escape = esc_list[-1]
1322 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1323 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1323
1324
1324 #----------------------------------------------------------------------------
1325 #----------------------------------------------------------------------------
1325 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1326 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1326 """Print a string, piping through a pager after a certain length.
1327 """Print a string, piping through a pager after a certain length.
1327
1328
1328 The screen_lines parameter specifies the number of *usable* lines of your
1329 The screen_lines parameter specifies the number of *usable* lines of your
1329 terminal screen (total lines minus lines you need to reserve to show other
1330 terminal screen (total lines minus lines you need to reserve to show other
1330 information).
1331 information).
1331
1332
1332 If you set screen_lines to a number <=0, page() will try to auto-determine
1333 If you set screen_lines to a number <=0, page() will try to auto-determine
1333 your screen size and will only use up to (screen_size+screen_lines) for
1334 your screen size and will only use up to (screen_size+screen_lines) for
1334 printing, paging after that. That is, if you want auto-detection but need
1335 printing, paging after that. That is, if you want auto-detection but need
1335 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1336 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1336 auto-detection without any lines reserved simply use screen_lines = 0.
1337 auto-detection without any lines reserved simply use screen_lines = 0.
1337
1338
1338 If a string won't fit in the allowed lines, it is sent through the
1339 If a string won't fit in the allowed lines, it is sent through the
1339 specified pager command. If none given, look for PAGER in the environment,
1340 specified pager command. If none given, look for PAGER in the environment,
1340 and ultimately default to less.
1341 and ultimately default to less.
1341
1342
1342 If no system pager works, the string is sent through a 'dumb pager'
1343 If no system pager works, the string is sent through a 'dumb pager'
1343 written in python, very simplistic.
1344 written in python, very simplistic.
1344 """
1345 """
1345
1346
1346 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1347 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1347 TERM = os.environ.get('TERM','dumb')
1348 TERM = os.environ.get('TERM','dumb')
1348 if TERM in ['dumb','emacs'] and os.name != 'nt':
1349 if TERM in ['dumb','emacs'] and os.name != 'nt':
1349 print strng
1350 print strng
1350 return
1351 return
1351 # chop off the topmost part of the string we don't want to see
1352 # chop off the topmost part of the string we don't want to see
1352 str_lines = strng.split(os.linesep)[start:]
1353 str_lines = strng.split(os.linesep)[start:]
1353 str_toprint = os.linesep.join(str_lines)
1354 str_toprint = os.linesep.join(str_lines)
1354 num_newlines = len(str_lines)
1355 num_newlines = len(str_lines)
1355 len_str = len(str_toprint)
1356 len_str = len(str_toprint)
1356
1357
1357 # Dumb heuristics to guesstimate number of on-screen lines the string
1358 # Dumb heuristics to guesstimate number of on-screen lines the string
1358 # takes. Very basic, but good enough for docstrings in reasonable
1359 # takes. Very basic, but good enough for docstrings in reasonable
1359 # terminals. If someone later feels like refining it, it's not hard.
1360 # terminals. If someone later feels like refining it, it's not hard.
1360 numlines = max(num_newlines,int(len_str/80)+1)
1361 numlines = max(num_newlines,int(len_str/80)+1)
1361
1362
1362 if os.name == "nt":
1363 if os.name == "nt":
1363 screen_lines_def = get_console_size(defaulty=25)[1]
1364 screen_lines_def = get_console_size(defaulty=25)[1]
1364 else:
1365 else:
1365 screen_lines_def = 25 # default value if we can't auto-determine
1366 screen_lines_def = 25 # default value if we can't auto-determine
1366
1367
1367 # auto-determine screen size
1368 # auto-determine screen size
1368 if screen_lines <= 0:
1369 if screen_lines <= 0:
1369 if TERM=='xterm':
1370 if TERM=='xterm':
1370 try:
1371 try:
1371 import curses
1372 import curses
1372 if hasattr(curses,'initscr'):
1373 if hasattr(curses,'initscr'):
1373 use_curses = 1
1374 use_curses = 1
1374 else:
1375 else:
1375 use_curses = 0
1376 use_curses = 0
1376 except ImportError:
1377 except ImportError:
1377 use_curses = 0
1378 use_curses = 0
1378 else:
1379 else:
1379 # curses causes problems on many terminals other than xterm.
1380 # curses causes problems on many terminals other than xterm.
1380 use_curses = 0
1381 use_curses = 0
1381 if use_curses:
1382 if use_curses:
1382 scr = curses.initscr()
1383 scr = curses.initscr()
1383 screen_lines_real,screen_cols = scr.getmaxyx()
1384 screen_lines_real,screen_cols = scr.getmaxyx()
1384 curses.endwin()
1385 curses.endwin()
1385 screen_lines += screen_lines_real
1386 screen_lines += screen_lines_real
1386 #print '***Screen size:',screen_lines_real,'lines x',\
1387 #print '***Screen size:',screen_lines_real,'lines x',\
1387 #screen_cols,'columns.' # dbg
1388 #screen_cols,'columns.' # dbg
1388 else:
1389 else:
1389 screen_lines += screen_lines_def
1390 screen_lines += screen_lines_def
1390
1391
1391 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1392 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1392 if numlines <= screen_lines :
1393 if numlines <= screen_lines :
1393 #print '*** normal print' # dbg
1394 #print '*** normal print' # dbg
1394 print >>Term.cout, str_toprint
1395 print >>Term.cout, str_toprint
1395 else:
1396 else:
1396 # Try to open pager and default to internal one if that fails.
1397 # Try to open pager and default to internal one if that fails.
1397 # All failure modes are tagged as 'retval=1', to match the return
1398 # All failure modes are tagged as 'retval=1', to match the return
1398 # value of a failed system command. If any intermediate attempt
1399 # value of a failed system command. If any intermediate attempt
1399 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1400 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1400 pager_cmd = get_pager_cmd(pager_cmd)
1401 pager_cmd = get_pager_cmd(pager_cmd)
1401 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1402 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1402 if os.name == 'nt':
1403 if os.name == 'nt':
1403 if pager_cmd.startswith('type'):
1404 if pager_cmd.startswith('type'):
1404 # The default WinXP 'type' command is failing on complex strings.
1405 # The default WinXP 'type' command is failing on complex strings.
1405 retval = 1
1406 retval = 1
1406 else:
1407 else:
1407 tmpname = tempfile.mktemp('.txt')
1408 tmpname = tempfile.mktemp('.txt')
1408 tmpfile = file(tmpname,'wt')
1409 tmpfile = file(tmpname,'wt')
1409 tmpfile.write(strng)
1410 tmpfile.write(strng)
1410 tmpfile.close()
1411 tmpfile.close()
1411 cmd = "%s < %s" % (pager_cmd,tmpname)
1412 cmd = "%s < %s" % (pager_cmd,tmpname)
1412 if os.system(cmd):
1413 if os.system(cmd):
1413 retval = 1
1414 retval = 1
1414 else:
1415 else:
1415 retval = None
1416 retval = None
1416 os.remove(tmpname)
1417 os.remove(tmpname)
1417 else:
1418 else:
1418 try:
1419 try:
1419 retval = None
1420 retval = None
1420 # if I use popen4, things hang. No idea why.
1421 # if I use popen4, things hang. No idea why.
1421 #pager,shell_out = os.popen4(pager_cmd)
1422 #pager,shell_out = os.popen4(pager_cmd)
1422 pager = os.popen(pager_cmd,'w')
1423 pager = os.popen(pager_cmd,'w')
1423 pager.write(strng)
1424 pager.write(strng)
1424 pager.close()
1425 pager.close()
1425 retval = pager.close() # success returns None
1426 retval = pager.close() # success returns None
1426 except IOError,msg: # broken pipe when user quits
1427 except IOError,msg: # broken pipe when user quits
1427 if msg.args == (32,'Broken pipe'):
1428 if msg.args == (32,'Broken pipe'):
1428 retval = None
1429 retval = None
1429 else:
1430 else:
1430 retval = 1
1431 retval = 1
1431 except OSError:
1432 except OSError:
1432 # Other strange problems, sometimes seen in Win2k/cygwin
1433 # Other strange problems, sometimes seen in Win2k/cygwin
1433 retval = 1
1434 retval = 1
1434 if retval is not None:
1435 if retval is not None:
1435 page_dumb(strng,screen_lines=screen_lines)
1436 page_dumb(strng,screen_lines=screen_lines)
1436
1437
1437 #----------------------------------------------------------------------------
1438 #----------------------------------------------------------------------------
1438 def page_file(fname,start = 0, pager_cmd = None):
1439 def page_file(fname,start = 0, pager_cmd = None):
1439 """Page a file, using an optional pager command and starting line.
1440 """Page a file, using an optional pager command and starting line.
1440 """
1441 """
1441
1442
1442 pager_cmd = get_pager_cmd(pager_cmd)
1443 pager_cmd = get_pager_cmd(pager_cmd)
1443 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1444 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1444
1445
1445 try:
1446 try:
1446 if os.environ['TERM'] in ['emacs','dumb']:
1447 if os.environ['TERM'] in ['emacs','dumb']:
1447 raise EnvironmentError
1448 raise EnvironmentError
1448 xsys(pager_cmd + ' ' + fname)
1449 xsys(pager_cmd + ' ' + fname)
1449 except:
1450 except:
1450 try:
1451 try:
1451 if start > 0:
1452 if start > 0:
1452 start -= 1
1453 start -= 1
1453 page(open(fname).read(),start)
1454 page(open(fname).read(),start)
1454 except:
1455 except:
1455 print 'Unable to show file',`fname`
1456 print 'Unable to show file',`fname`
1456
1457
1457 #----------------------------------------------------------------------------
1458 #----------------------------------------------------------------------------
1458 def snip_print(str,width = 75,print_full = 0,header = ''):
1459 def snip_print(str,width = 75,print_full = 0,header = ''):
1459 """Print a string snipping the midsection to fit in width.
1460 """Print a string snipping the midsection to fit in width.
1460
1461
1461 print_full: mode control:
1462 print_full: mode control:
1462 - 0: only snip long strings
1463 - 0: only snip long strings
1463 - 1: send to page() directly.
1464 - 1: send to page() directly.
1464 - 2: snip long strings and ask for full length viewing with page()
1465 - 2: snip long strings and ask for full length viewing with page()
1465 Return 1 if snipping was necessary, 0 otherwise."""
1466 Return 1 if snipping was necessary, 0 otherwise."""
1466
1467
1467 if print_full == 1:
1468 if print_full == 1:
1468 page(header+str)
1469 page(header+str)
1469 return 0
1470 return 0
1470
1471
1471 print header,
1472 print header,
1472 if len(str) < width:
1473 if len(str) < width:
1473 print str
1474 print str
1474 snip = 0
1475 snip = 0
1475 else:
1476 else:
1476 whalf = int((width -5)/2)
1477 whalf = int((width -5)/2)
1477 print str[:whalf] + ' <...> ' + str[-whalf:]
1478 print str[:whalf] + ' <...> ' + str[-whalf:]
1478 snip = 1
1479 snip = 1
1479 if snip and print_full == 2:
1480 if snip and print_full == 2:
1480 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1481 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1481 page(str)
1482 page(str)
1482 return snip
1483 return snip
1483
1484
1484 #****************************************************************************
1485 #****************************************************************************
1485 # lists, dicts and structures
1486 # lists, dicts and structures
1486
1487
1487 def belong(candidates,checklist):
1488 def belong(candidates,checklist):
1488 """Check whether a list of items appear in a given list of options.
1489 """Check whether a list of items appear in a given list of options.
1489
1490
1490 Returns a list of 1 and 0, one for each candidate given."""
1491 Returns a list of 1 and 0, one for each candidate given."""
1491
1492
1492 return [x in checklist for x in candidates]
1493 return [x in checklist for x in candidates]
1493
1494
1494 #----------------------------------------------------------------------------
1495 #----------------------------------------------------------------------------
1495 def uniq_stable(elems):
1496 def uniq_stable(elems):
1496 """uniq_stable(elems) -> list
1497 """uniq_stable(elems) -> list
1497
1498
1498 Return from an iterable, a list of all the unique elements in the input,
1499 Return from an iterable, a list of all the unique elements in the input,
1499 but maintaining the order in which they first appear.
1500 but maintaining the order in which they first appear.
1500
1501
1501 A naive solution to this problem which just makes a dictionary with the
1502 A naive solution to this problem which just makes a dictionary with the
1502 elements as keys fails to respect the stability condition, since
1503 elements as keys fails to respect the stability condition, since
1503 dictionaries are unsorted by nature.
1504 dictionaries are unsorted by nature.
1504
1505
1505 Note: All elements in the input must be valid dictionary keys for this
1506 Note: All elements in the input must be valid dictionary keys for this
1506 routine to work, as it internally uses a dictionary for efficiency
1507 routine to work, as it internally uses a dictionary for efficiency
1507 reasons."""
1508 reasons."""
1508
1509
1509 unique = []
1510 unique = []
1510 unique_dict = {}
1511 unique_dict = {}
1511 for nn in elems:
1512 for nn in elems:
1512 if nn not in unique_dict:
1513 if nn not in unique_dict:
1513 unique.append(nn)
1514 unique.append(nn)
1514 unique_dict[nn] = None
1515 unique_dict[nn] = None
1515 return unique
1516 return unique
1516
1517
1517 #----------------------------------------------------------------------------
1518 #----------------------------------------------------------------------------
1518 class NLprinter:
1519 class NLprinter:
1519 """Print an arbitrarily nested list, indicating index numbers.
1520 """Print an arbitrarily nested list, indicating index numbers.
1520
1521
1521 An instance of this class called nlprint is available and callable as a
1522 An instance of this class called nlprint is available and callable as a
1522 function.
1523 function.
1523
1524
1524 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1525 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1525 and using 'sep' to separate the index from the value. """
1526 and using 'sep' to separate the index from the value. """
1526
1527
1527 def __init__(self):
1528 def __init__(self):
1528 self.depth = 0
1529 self.depth = 0
1529
1530
1530 def __call__(self,lst,pos='',**kw):
1531 def __call__(self,lst,pos='',**kw):
1531 """Prints the nested list numbering levels."""
1532 """Prints the nested list numbering levels."""
1532 kw.setdefault('indent',' ')
1533 kw.setdefault('indent',' ')
1533 kw.setdefault('sep',': ')
1534 kw.setdefault('sep',': ')
1534 kw.setdefault('start',0)
1535 kw.setdefault('start',0)
1535 kw.setdefault('stop',len(lst))
1536 kw.setdefault('stop',len(lst))
1536 # we need to remove start and stop from kw so they don't propagate
1537 # we need to remove start and stop from kw so they don't propagate
1537 # into a recursive call for a nested list.
1538 # into a recursive call for a nested list.
1538 start = kw['start']; del kw['start']
1539 start = kw['start']; del kw['start']
1539 stop = kw['stop']; del kw['stop']
1540 stop = kw['stop']; del kw['stop']
1540 if self.depth == 0 and 'header' in kw.keys():
1541 if self.depth == 0 and 'header' in kw.keys():
1541 print kw['header']
1542 print kw['header']
1542
1543
1543 for idx in range(start,stop):
1544 for idx in range(start,stop):
1544 elem = lst[idx]
1545 elem = lst[idx]
1545 if type(elem)==type([]):
1546 if type(elem)==type([]):
1546 self.depth += 1
1547 self.depth += 1
1547 self.__call__(elem,itpl('$pos$idx,'),**kw)
1548 self.__call__(elem,itpl('$pos$idx,'),**kw)
1548 self.depth -= 1
1549 self.depth -= 1
1549 else:
1550 else:
1550 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1551 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1551
1552
1552 nlprint = NLprinter()
1553 nlprint = NLprinter()
1553 #----------------------------------------------------------------------------
1554 #----------------------------------------------------------------------------
1554 def all_belong(candidates,checklist):
1555 def all_belong(candidates,checklist):
1555 """Check whether a list of items ALL appear in a given list of options.
1556 """Check whether a list of items ALL appear in a given list of options.
1556
1557
1557 Returns a single 1 or 0 value."""
1558 Returns a single 1 or 0 value."""
1558
1559
1559 return 1-(0 in [x in checklist for x in candidates])
1560 return 1-(0 in [x in checklist for x in candidates])
1560
1561
1561 #----------------------------------------------------------------------------
1562 #----------------------------------------------------------------------------
1562 def sort_compare(lst1,lst2,inplace = 1):
1563 def sort_compare(lst1,lst2,inplace = 1):
1563 """Sort and compare two lists.
1564 """Sort and compare two lists.
1564
1565
1565 By default it does it in place, thus modifying the lists. Use inplace = 0
1566 By default it does it in place, thus modifying the lists. Use inplace = 0
1566 to avoid that (at the cost of temporary copy creation)."""
1567 to avoid that (at the cost of temporary copy creation)."""
1567 if not inplace:
1568 if not inplace:
1568 lst1 = lst1[:]
1569 lst1 = lst1[:]
1569 lst2 = lst2[:]
1570 lst2 = lst2[:]
1570 lst1.sort(); lst2.sort()
1571 lst1.sort(); lst2.sort()
1571 return lst1 == lst2
1572 return lst1 == lst2
1572
1573
1573 #----------------------------------------------------------------------------
1574 #----------------------------------------------------------------------------
1574 def mkdict(**kwargs):
1575 def mkdict(**kwargs):
1575 """Return a dict from a keyword list.
1576 """Return a dict from a keyword list.
1576
1577
1577 It's just syntactic sugar for making ditcionary creation more convenient:
1578 It's just syntactic sugar for making ditcionary creation more convenient:
1578 # the standard way
1579 # the standard way
1579 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1580 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1580 # a cleaner way
1581 # a cleaner way
1581 >>>data = dict(red=1, green=2, blue=3)
1582 >>>data = dict(red=1, green=2, blue=3)
1582
1583
1583 If you need more than this, look at the Struct() class."""
1584 If you need more than this, look at the Struct() class."""
1584
1585
1585 return kwargs
1586 return kwargs
1586
1587
1587 #----------------------------------------------------------------------------
1588 #----------------------------------------------------------------------------
1588 def list2dict(lst):
1589 def list2dict(lst):
1589 """Takes a list of (key,value) pairs and turns it into a dict."""
1590 """Takes a list of (key,value) pairs and turns it into a dict."""
1590
1591
1591 dic = {}
1592 dic = {}
1592 for k,v in lst: dic[k] = v
1593 for k,v in lst: dic[k] = v
1593 return dic
1594 return dic
1594
1595
1595 #----------------------------------------------------------------------------
1596 #----------------------------------------------------------------------------
1596 def list2dict2(lst,default=''):
1597 def list2dict2(lst,default=''):
1597 """Takes a list and turns it into a dict.
1598 """Takes a list and turns it into a dict.
1598 Much slower than list2dict, but more versatile. This version can take
1599 Much slower than list2dict, but more versatile. This version can take
1599 lists with sublists of arbitrary length (including sclars)."""
1600 lists with sublists of arbitrary length (including sclars)."""
1600
1601
1601 dic = {}
1602 dic = {}
1602 for elem in lst:
1603 for elem in lst:
1603 if type(elem) in (types.ListType,types.TupleType):
1604 if type(elem) in (types.ListType,types.TupleType):
1604 size = len(elem)
1605 size = len(elem)
1605 if size == 0:
1606 if size == 0:
1606 pass
1607 pass
1607 elif size == 1:
1608 elif size == 1:
1608 dic[elem] = default
1609 dic[elem] = default
1609 else:
1610 else:
1610 k,v = elem[0], elem[1:]
1611 k,v = elem[0], elem[1:]
1611 if len(v) == 1: v = v[0]
1612 if len(v) == 1: v = v[0]
1612 dic[k] = v
1613 dic[k] = v
1613 else:
1614 else:
1614 dic[elem] = default
1615 dic[elem] = default
1615 return dic
1616 return dic
1616
1617
1617 #----------------------------------------------------------------------------
1618 #----------------------------------------------------------------------------
1618 def flatten(seq):
1619 def flatten(seq):
1619 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1620 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1620
1621
1621 # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in).
1622 # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in).
1622
1623
1623 # if the x=0 isn't made, a *global* variable x is left over after calling
1624 # if the x=0 isn't made, a *global* variable x is left over after calling
1624 # this function, with the value of the last element in the return
1625 # this function, with the value of the last element in the return
1625 # list. This does seem like a bug big time to me.
1626 # list. This does seem like a bug big time to me.
1626
1627
1627 # the problem is fixed with the x=0, which seems to force the creation of
1628 # the problem is fixed with the x=0, which seems to force the creation of
1628 # a local name
1629 # a local name
1629
1630
1630 x = 0
1631 x = 0
1631 return [x for subseq in seq for x in subseq]
1632 return [x for subseq in seq for x in subseq]
1632
1633
1633 #----------------------------------------------------------------------------
1634 #----------------------------------------------------------------------------
1634 def get_slice(seq,start=0,stop=None,step=1):
1635 def get_slice(seq,start=0,stop=None,step=1):
1635 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1636 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1636 if stop == None:
1637 if stop == None:
1637 stop = len(seq)
1638 stop = len(seq)
1638 item = lambda i: seq[i]
1639 item = lambda i: seq[i]
1639 return map(item,xrange(start,stop,step))
1640 return map(item,xrange(start,stop,step))
1640
1641
1641 #----------------------------------------------------------------------------
1642 #----------------------------------------------------------------------------
1642 def chop(seq,size):
1643 def chop(seq,size):
1643 """Chop a sequence into chunks of the given size."""
1644 """Chop a sequence into chunks of the given size."""
1644 chunk = lambda i: seq[i:i+size]
1645 chunk = lambda i: seq[i:i+size]
1645 return map(chunk,xrange(0,len(seq),size))
1646 return map(chunk,xrange(0,len(seq),size))
1646
1647
1647 #----------------------------------------------------------------------------
1648 #----------------------------------------------------------------------------
1648 def with(object, **args):
1649 def with(object, **args):
1649 """Set multiple attributes for an object, similar to Pascal's with.
1650 """Set multiple attributes for an object, similar to Pascal's with.
1650
1651
1651 Example:
1652 Example:
1652 with(jim,
1653 with(jim,
1653 born = 1960,
1654 born = 1960,
1654 haircolour = 'Brown',
1655 haircolour = 'Brown',
1655 eyecolour = 'Green')
1656 eyecolour = 'Green')
1656
1657
1657 Credit: Greg Ewing, in
1658 Credit: Greg Ewing, in
1658 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1659 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1659
1660
1660 object.__dict__.update(args)
1661 object.__dict__.update(args)
1661
1662
1662 #----------------------------------------------------------------------------
1663 #----------------------------------------------------------------------------
1663 def setattr_list(obj,alist,nspace = None):
1664 def setattr_list(obj,alist,nspace = None):
1664 """Set a list of attributes for an object taken from a namespace.
1665 """Set a list of attributes for an object taken from a namespace.
1665
1666
1666 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1667 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1667 alist with their values taken from nspace, which must be a dict (something
1668 alist with their values taken from nspace, which must be a dict (something
1668 like locals() will often do) If nspace isn't given, locals() of the
1669 like locals() will often do) If nspace isn't given, locals() of the
1669 *caller* is used, so in most cases you can omit it.
1670 *caller* is used, so in most cases you can omit it.
1670
1671
1671 Note that alist can be given as a string, which will be automatically
1672 Note that alist can be given as a string, which will be automatically
1672 split into a list on whitespace. If given as a list, it must be a list of
1673 split into a list on whitespace. If given as a list, it must be a list of
1673 *strings* (the variable names themselves), not of variables."""
1674 *strings* (the variable names themselves), not of variables."""
1674
1675
1675 # this grabs the local variables from the *previous* call frame -- that is
1676 # this grabs the local variables from the *previous* call frame -- that is
1676 # the locals from the function that called setattr_list().
1677 # the locals from the function that called setattr_list().
1677 # - snipped from weave.inline()
1678 # - snipped from weave.inline()
1678 if nspace is None:
1679 if nspace is None:
1679 call_frame = sys._getframe().f_back
1680 call_frame = sys._getframe().f_back
1680 nspace = call_frame.f_locals
1681 nspace = call_frame.f_locals
1681
1682
1682 if type(alist) in StringTypes:
1683 if type(alist) in StringTypes:
1683 alist = alist.split()
1684 alist = alist.split()
1684 for attr in alist:
1685 for attr in alist:
1685 val = eval(attr,nspace)
1686 val = eval(attr,nspace)
1686 setattr(obj,attr,val)
1687 setattr(obj,attr,val)
1687
1688
1688 #----------------------------------------------------------------------------
1689 #----------------------------------------------------------------------------
1689 def getattr_list(obj,alist,*args):
1690 def getattr_list(obj,alist,*args):
1690 """getattr_list(obj,alist[, default]) -> attribute list.
1691 """getattr_list(obj,alist[, default]) -> attribute list.
1691
1692
1692 Get a list of named attributes for an object. When a default argument is
1693 Get a list of named attributes for an object. When a default argument is
1693 given, it is returned when the attribute doesn't exist; without it, an
1694 given, it is returned when the attribute doesn't exist; without it, an
1694 exception is raised in that case.
1695 exception is raised in that case.
1695
1696
1696 Note that alist can be given as a string, which will be automatically
1697 Note that alist can be given as a string, which will be automatically
1697 split into a list on whitespace. If given as a list, it must be a list of
1698 split into a list on whitespace. If given as a list, it must be a list of
1698 *strings* (the variable names themselves), not of variables."""
1699 *strings* (the variable names themselves), not of variables."""
1699
1700
1700 if type(alist) in StringTypes:
1701 if type(alist) in StringTypes:
1701 alist = alist.split()
1702 alist = alist.split()
1702 if args:
1703 if args:
1703 if len(args)==1:
1704 if len(args)==1:
1704 default = args[0]
1705 default = args[0]
1705 return map(lambda attr: getattr(obj,attr,default),alist)
1706 return map(lambda attr: getattr(obj,attr,default),alist)
1706 else:
1707 else:
1707 raise ValueError,'getattr_list() takes only one optional argument'
1708 raise ValueError,'getattr_list() takes only one optional argument'
1708 else:
1709 else:
1709 return map(lambda attr: getattr(obj,attr),alist)
1710 return map(lambda attr: getattr(obj,attr),alist)
1710
1711
1711 #----------------------------------------------------------------------------
1712 #----------------------------------------------------------------------------
1712 def map_method(method,object_list,*argseq,**kw):
1713 def map_method(method,object_list,*argseq,**kw):
1713 """map_method(method,object_list,*args,**kw) -> list
1714 """map_method(method,object_list,*args,**kw) -> list
1714
1715
1715 Return a list of the results of applying the methods to the items of the
1716 Return a list of the results of applying the methods to the items of the
1716 argument sequence(s). If more than one sequence is given, the method is
1717 argument sequence(s). If more than one sequence is given, the method is
1717 called with an argument list consisting of the corresponding item of each
1718 called with an argument list consisting of the corresponding item of each
1718 sequence. All sequences must be of the same length.
1719 sequence. All sequences must be of the same length.
1719
1720
1720 Keyword arguments are passed verbatim to all objects called.
1721 Keyword arguments are passed verbatim to all objects called.
1721
1722
1722 This is Python code, so it's not nearly as fast as the builtin map()."""
1723 This is Python code, so it's not nearly as fast as the builtin map()."""
1723
1724
1724 out_list = []
1725 out_list = []
1725 idx = 0
1726 idx = 0
1726 for object in object_list:
1727 for object in object_list:
1727 try:
1728 try:
1728 handler = getattr(object, method)
1729 handler = getattr(object, method)
1729 except AttributeError:
1730 except AttributeError:
1730 out_list.append(None)
1731 out_list.append(None)
1731 else:
1732 else:
1732 if argseq:
1733 if argseq:
1733 args = map(lambda lst:lst[idx],argseq)
1734 args = map(lambda lst:lst[idx],argseq)
1734 #print 'ob',object,'hand',handler,'ar',args # dbg
1735 #print 'ob',object,'hand',handler,'ar',args # dbg
1735 out_list.append(handler(args,**kw))
1736 out_list.append(handler(args,**kw))
1736 else:
1737 else:
1737 out_list.append(handler(**kw))
1738 out_list.append(handler(**kw))
1738 idx += 1
1739 idx += 1
1739 return out_list
1740 return out_list
1740
1741
1741 #----------------------------------------------------------------------------
1742 #----------------------------------------------------------------------------
1742 def import_fail_info(mod_name,fns=None):
1743 def import_fail_info(mod_name,fns=None):
1743 """Inform load failure for a module."""
1744 """Inform load failure for a module."""
1744
1745
1745 if fns == None:
1746 if fns == None:
1746 warn("Loading of %s failed.\n" % (mod_name,))
1747 warn("Loading of %s failed.\n" % (mod_name,))
1747 else:
1748 else:
1748 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1749 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1749
1750
1750 #----------------------------------------------------------------------------
1751 #----------------------------------------------------------------------------
1751 # Proposed popitem() extension, written as a method
1752 # Proposed popitem() extension, written as a method
1752
1753
1753 class NotGiven: pass
1754 class NotGiven: pass
1754
1755
1755 def popkey(dct,key,default=NotGiven):
1756 def popkey(dct,key,default=NotGiven):
1756 """Return dct[key] and delete dct[key].
1757 """Return dct[key] and delete dct[key].
1757
1758
1758 If default is given, return it if dct[key] doesn't exist, otherwise raise
1759 If default is given, return it if dct[key] doesn't exist, otherwise raise
1759 KeyError. """
1760 KeyError. """
1760
1761
1761 try:
1762 try:
1762 val = dct[key]
1763 val = dct[key]
1763 except KeyError:
1764 except KeyError:
1764 if default is NotGiven:
1765 if default is NotGiven:
1765 raise
1766 raise
1766 else:
1767 else:
1767 return default
1768 return default
1768 else:
1769 else:
1769 del dct[key]
1770 del dct[key]
1770 return val
1771 return val
1771 #*************************** end of file <genutils.py> **********************
1772 #*************************** end of file <genutils.py> **********************
1772
1773
@@ -1,174 +1,174 b''
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring & extending ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 The API will probably break when ipython 1.0 is released, but so
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6 will the other configuration method (rc files).
7
7
8 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
9 of the public api.
9 of the public api.
10
10
11 Below is an example that you can just put to a module and import from ipython.
11 Below is an example that you can just put to a module and import from ipython.
12
12
13 A good practice is to install the config script below as e.g.
13 A good practice is to install the config script below as e.g.
14
14
15 ~/.ipython/my_private_conf.py
15 ~/.ipython/my_private_conf.py
16
16
17 And do
17 And do
18
18
19 import_mod my_private_conf
19 import_mod my_private_conf
20
20
21 in ~/.ipython/ipythonrc
21 in ~/.ipython/ipythonrc
22
22
23 That way the module is imported at startup and you can have all your
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
25 stuff) in there.
26
26
27 -----------------------------------------------
27 -----------------------------------------------
28 import IPython.ipapi as ip
28 import IPython.ipapi as ip
29
29
30 def ankka_f(self, arg):
30 def ankka_f(self, arg):
31 print "Ankka",self,"says uppercase:",arg.upper()
31 print "Ankka",self,"says uppercase:",arg.upper()
32
32
33 ip.expose_magic("ankka",ankka_f)
33 ip.expose_magic("ankka",ankka_f)
34
34
35 ip.magic('alias sayhi echo "Testing, hi ok"')
35 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias helloworld echo "Hello world"')
36 ip.magic('alias helloworld echo "Hello world"')
37 ip.system('pwd')
37 ip.system('pwd')
38
38
39 ip.ex('import re')
39 ip.ex('import re')
40 ip.ex("""
40 ip.ex("""
41 def funcci(a,b):
41 def funcci(a,b):
42 print a+b
42 print a+b
43 print funcci(3,4)
43 print funcci(3,4)
44 """)
44 """)
45 ip.ex("funcci(348,9)")
45 ip.ex("funcci(348,9)")
46
46
47 def jed_editor(self,filename, linenum=None):
47 def jed_editor(self,filename, linenum=None):
48 print "Calling my own editor, jed ... via hook!"
48 print "Calling my own editor, jed ... via hook!"
49 import os
49 import os
50 if linenum is None: linenum = 0
50 if linenum is None: linenum = 0
51 os.system('jed +%d %s' % (linenum, filename))
51 os.system('jed +%d %s' % (linenum, filename))
52 print "exiting jed"
52 print "exiting jed"
53
53
54 ip.set_hook('editor',jed_editor)
54 ip.set_hook('editor',jed_editor)
55
55
56 o = ip.options()
56 o = ip.options()
57 o.autocall = 2 # FULL autocall mode
57 o.autocall = 2 # FULL autocall mode
58
58
59 print "done!"
59 print "done!"
60
60
61 '''
61 '''
62
62
63
63
64 class TryNext(Exception):
64 class TryNext(Exception):
65 """ Try next hook exception.
65 """ Try next hook exception.
66
66
67 Raise this in your hook function to indicate that the next
67 Raise this in your hook function to indicate that the next
68 hook handler should be used to handle the operation.
68 hook handler should be used to handle the operation.
69 """
69 """
70
70
71
71
72
72
73 __IP = None
73 __IP = None
74
74
75 def _init_with_shell(ip):
75 def _init_with_shell(ip):
76 global magic
76 global magic
77 magic = ip.ipmagic
77 magic = ip.ipmagic
78 global system
78 global system
79 system = ip.ipsystem
79 system = ip.ipsystem
80 global set_hook
80 global set_hook
81 set_hook = ip.set_hook
81 set_hook = ip.set_hook
82
82
83 global __IP
83 global __IP
84 __IP = ip
84 __IP = ip
85
85
86 def options():
86 def options():
87 """ All configurable variables """
87 """ All configurable variables """
88 return __IP.rc
88 return __IP.rc
89
89
90 def user_ns():
90 def user_ns():
91 return __IP.user_ns
91 return __IP.user_ns
92
92
93 def expose_magic(magicname, func):
93 def expose_magic(magicname, func):
94 ''' Expose own function as magic function for ipython
94 ''' Expose own function as magic function for ipython
95
95
96 def foo_impl(self,parameter_s=''):
96 def foo_impl(self,parameter_s=''):
97 """My very own magic!. (Use docstrings, IPython reads them)."""
97 """My very own magic!. (Use docstrings, IPython reads them)."""
98 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
98 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
99 print 'The self object is:',self
99 print 'The self object is:',self
100
100
101 ipapi.expose_magic("foo",foo_impl)
101 ipapi.expose_magic("foo",foo_impl)
102 '''
102 '''
103
103
104 from IPython import Magic
104 from IPython import Magic
105 import new
105 import new
106 im = new.instancemethod(func,__IP, __IP.__class__)
106 im = new.instancemethod(func,__IP, __IP.__class__)
107 setattr(__IP, "magic_" + magicname, im)
107 setattr(__IP, "magic_" + magicname, im)
108
108
109 class asmagic:
109 class asmagic:
110 """ Decorator for exposing magics in a friendly 2.4 decorator form
110 """ Decorator for exposing magics in a friendly 2.4 decorator form
111
111
112 @ip.asmagic("foo")
112 @ip.asmagic("foo")
113 def f(self,arg):
113 def f(self,arg):
114 pring "arg given:",arg
114 pring "arg given:",arg
115
115
116 After this, %foo is a magic function.
116 After this, %foo is a magic function.
117 """
117 """
118
118
119 def __init__(self,magicname):
119 def __init__(self,magicname):
120 self.name = magicname
120 self.name = magicname
121
121
122 def __call__(self,f):
122 def __call__(self,f):
123 expose_magic(self.name, f)
123 expose_magic(self.name, f)
124 return f
124 return f
125
125
126 class ashook:
126 class ashook:
127 """ Decorator for exposing magics in a friendly 2.4 decorator form
127 """ Decorator for exposing magics in a friendly 2.4 decorator form
128
128
129 @ip.ashook("editor")
129 @ip.ashook("editor")
130 def jed_editor(self,filename, linenum=None):
130 def jed_editor(self,filename, linenum=None):
131 import os
131 import os
132 if linenum is None: linenum = 0
132 if linenum is None: linenum = 0
133 os.system('jed +%d %s' % (linenum, filename))
133 os.system('jed +%d %s' % (linenum, filename))
134
134
135 """
135 """
136
136
137 def __init__(self,name,priority=50):
137 def __init__(self,name,priority=50):
138 self.name = name
138 self.name = name
139 self.prio = priority
139 self.prio = priority
140
140
141 def __call__(self,f):
141 def __call__(self,f):
142 set_hook(self.name, f, self.prio)
142 set_hook(self.name, f, self.prio)
143 return f
143 return f
144
144
145
145
146 def ex(cmd):
146 def ex(cmd):
147 """ Execute a normal python statement in user namespace """
147 """ Execute a normal python statement in user namespace """
148 exec cmd in user_ns()
148 exec cmd in user_ns()
149
149
150 def ev(expr):
150 def ev(expr):
151 """ Evaluate python expression expr in user namespace
151 """ Evaluate python expression expr in user namespace
152
152
153 Returns the result """
153 Returns the result """
154 return eval(expr,user_ns())
154 return eval(expr,user_ns())
155
155
156 def launch_new_instance():
156 def launch_new_instance():
157 """ Creata and start a new ipython instance.
157 """ Create and start a new ipython instance.
158
158
159 This can be called even without having an already initialized
159 This can be called even without having an already initialized
160 ipython session running.
160 ipython session running.
161
161
162 """
162 """
163 import IPython
163 import IPython
164
164
165 IPython.Shell.start().mainloop()
165 IPython.Shell.start().mainloop()
166
166
167 def is_ipython_session():
167 def is_ipython_session():
168 """ Return a true value if running inside IPython.
168 """ Return a true value if running inside IPython.
169
169
170 """
170 """
171
171
172 # Yes, this is the shell object or None - however, it's an implementation
172 # Yes, this is the shell object or None - however, it's an implementation
173 # detail and should not be relied on, only truth value matters.
173 # detail and should not be relied on, only truth value matters.
174 return __IP
174 return __IP
@@ -1,2229 +1,2234 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 Requires Python 2.3 or newer.
5 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1058 2006-01-22 14:30:01Z vivainio $
9 $Id: iplib.py 1077 2006-01-24 18:15:27Z vivainio $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
14 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #
18 #
19 # Note: this code originally subclassed code.InteractiveConsole from the
19 # Note: this code originally subclassed code.InteractiveConsole from the
20 # Python standard library. Over time, all of that class has been copied
20 # Python standard library. Over time, all of that class has been copied
21 # verbatim here for modifications which could not be accomplished by
21 # verbatim here for modifications which could not be accomplished by
22 # subclassing. At this point, there are no dependencies at all on the code
22 # subclassing. At this point, there are no dependencies at all on the code
23 # module anymore (it is not even imported). The Python License (sec. 2)
23 # module anymore (it is not even imported). The Python License (sec. 2)
24 # allows for this, but it's always nice to acknowledge credit where credit is
24 # allows for this, but it's always nice to acknowledge credit where credit is
25 # due.
25 # due.
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 #****************************************************************************
28 #****************************************************************************
29 # Modules and globals
29 # Modules and globals
30
30
31 from __future__ import generators # for 2.2 backwards-compatibility
31 from __future__ import generators # for 2.2 backwards-compatibility
32
32
33 from IPython import Release
33 from IPython import Release
34 __author__ = '%s <%s>\n%s <%s>' % \
34 __author__ = '%s <%s>\n%s <%s>' % \
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 __license__ = Release.license
36 __license__ = Release.license
37 __version__ = Release.version
37 __version__ = Release.version
38
38
39 # Python standard modules
39 # Python standard modules
40 import __main__
40 import __main__
41 import __builtin__
41 import __builtin__
42 import StringIO
42 import StringIO
43 import bdb
43 import bdb
44 import cPickle as pickle
44 import cPickle as pickle
45 import codeop
45 import codeop
46 import exceptions
46 import exceptions
47 import glob
47 import glob
48 import inspect
48 import inspect
49 import keyword
49 import keyword
50 import new
50 import new
51 import os
51 import os
52 import pdb
52 import pdb
53 import pydoc
53 import pydoc
54 import re
54 import re
55 import shutil
55 import shutil
56 import string
56 import string
57 import sys
57 import sys
58 import tempfile
58 import tempfile
59 import traceback
59 import traceback
60 import types
60 import types
61
61
62 from pprint import pprint, pformat
62 from pprint import pprint, pformat
63
63
64 # IPython's own modules
64 # IPython's own modules
65 import IPython
65 import IPython
66 from IPython import OInspect,PyColorize,ultraTB
66 from IPython import OInspect,PyColorize,ultraTB
67 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
67 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
68 from IPython.FakeModule import FakeModule
68 from IPython.FakeModule import FakeModule
69 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
69 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
70 from IPython.Logger import Logger
70 from IPython.Logger import Logger
71 from IPython.Magic import Magic
71 from IPython.Magic import Magic
72 from IPython.Prompts import CachedOutput
72 from IPython.Prompts import CachedOutput
73 from IPython.ipstruct import Struct
73 from IPython.ipstruct import Struct
74 from IPython.background_jobs import BackgroundJobManager
74 from IPython.background_jobs import BackgroundJobManager
75 from IPython.usage import cmd_line_usage,interactive_usage
75 from IPython.usage import cmd_line_usage,interactive_usage
76 from IPython.genutils import *
76 from IPython.genutils import *
77 import IPython.ipapi
77 import IPython.ipapi
78
78
79 # Globals
79 # Globals
80
80
81 # store the builtin raw_input globally, and use this always, in case user code
81 # store the builtin raw_input globally, and use this always, in case user code
82 # overwrites it (like wx.py.PyShell does)
82 # overwrites it (like wx.py.PyShell does)
83 raw_input_original = raw_input
83 raw_input_original = raw_input
84
84
85 # compiled regexps for autoindent management
85 # compiled regexps for autoindent management
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
87
87
88
88
89 #****************************************************************************
89 #****************************************************************************
90 # Some utility function definitions
90 # Some utility function definitions
91
91
92 ini_spaces_re = re.compile(r'^(\s+)')
92 ini_spaces_re = re.compile(r'^(\s+)')
93
93
94 def num_ini_spaces(strng):
94 def num_ini_spaces(strng):
95 """Return the number of initial spaces in a string"""
95 """Return the number of initial spaces in a string"""
96
96
97 ini_spaces = ini_spaces_re.match(strng)
97 ini_spaces = ini_spaces_re.match(strng)
98 if ini_spaces:
98 if ini_spaces:
99 return ini_spaces.end()
99 return ini_spaces.end()
100 else:
100 else:
101 return 0
101 return 0
102
102
103 def softspace(file, newvalue):
103 def softspace(file, newvalue):
104 """Copied from code.py, to remove the dependency"""
104 """Copied from code.py, to remove the dependency"""
105
105
106 oldvalue = 0
106 oldvalue = 0
107 try:
107 try:
108 oldvalue = file.softspace
108 oldvalue = file.softspace
109 except AttributeError:
109 except AttributeError:
110 pass
110 pass
111 try:
111 try:
112 file.softspace = newvalue
112 file.softspace = newvalue
113 except (AttributeError, TypeError):
113 except (AttributeError, TypeError):
114 # "attribute-less object" or "read-only attributes"
114 # "attribute-less object" or "read-only attributes"
115 pass
115 pass
116 return oldvalue
116 return oldvalue
117
117
118
118
119 #****************************************************************************
119 #****************************************************************************
120 # Local use exceptions
120 # Local use exceptions
121 class SpaceInInput(exceptions.Exception): pass
121 class SpaceInInput(exceptions.Exception): pass
122
122
123
123
124 #****************************************************************************
124 #****************************************************************************
125 # Local use classes
125 # Local use classes
126 class Bunch: pass
126 class Bunch: pass
127
127
128 class Undefined: pass
128 class Undefined: pass
129
129
130 class InputList(list):
130 class InputList(list):
131 """Class to store user input.
131 """Class to store user input.
132
132
133 It's basically a list, but slices return a string instead of a list, thus
133 It's basically a list, but slices return a string instead of a list, thus
134 allowing things like (assuming 'In' is an instance):
134 allowing things like (assuming 'In' is an instance):
135
135
136 exec In[4:7]
136 exec In[4:7]
137
137
138 or
138 or
139
139
140 exec In[5:9] + In[14] + In[21:25]"""
140 exec In[5:9] + In[14] + In[21:25]"""
141
141
142 def __getslice__(self,i,j):
142 def __getslice__(self,i,j):
143 return ''.join(list.__getslice__(self,i,j))
143 return ''.join(list.__getslice__(self,i,j))
144
144
145 class SyntaxTB(ultraTB.ListTB):
145 class SyntaxTB(ultraTB.ListTB):
146 """Extension which holds some state: the last exception value"""
146 """Extension which holds some state: the last exception value"""
147
147
148 def __init__(self,color_scheme = 'NoColor'):
148 def __init__(self,color_scheme = 'NoColor'):
149 ultraTB.ListTB.__init__(self,color_scheme)
149 ultraTB.ListTB.__init__(self,color_scheme)
150 self.last_syntax_error = None
150 self.last_syntax_error = None
151
151
152 def __call__(self, etype, value, elist):
152 def __call__(self, etype, value, elist):
153 self.last_syntax_error = value
153 self.last_syntax_error = value
154 ultraTB.ListTB.__call__(self,etype,value,elist)
154 ultraTB.ListTB.__call__(self,etype,value,elist)
155
155
156 def clear_err_state(self):
156 def clear_err_state(self):
157 """Return the current error state and clear it"""
157 """Return the current error state and clear it"""
158 e = self.last_syntax_error
158 e = self.last_syntax_error
159 self.last_syntax_error = None
159 self.last_syntax_error = None
160 return e
160 return e
161
161
162 #****************************************************************************
162 #****************************************************************************
163 # Main IPython class
163 # Main IPython class
164
164
165 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
165 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
166 # until a full rewrite is made. I've cleaned all cross-class uses of
166 # until a full rewrite is made. I've cleaned all cross-class uses of
167 # attributes and methods, but too much user code out there relies on the
167 # attributes and methods, but too much user code out there relies on the
168 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
168 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
169 #
169 #
170 # But at least now, all the pieces have been separated and we could, in
170 # But at least now, all the pieces have been separated and we could, in
171 # principle, stop using the mixin. This will ease the transition to the
171 # principle, stop using the mixin. This will ease the transition to the
172 # chainsaw branch.
172 # chainsaw branch.
173
173
174 # For reference, the following is the list of 'self.foo' uses in the Magic
174 # For reference, the following is the list of 'self.foo' uses in the Magic
175 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
175 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
176 # class, to prevent clashes.
176 # class, to prevent clashes.
177
177
178 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
178 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
179 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
179 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
180 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
180 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
181 # 'self.value']
181 # 'self.value']
182
182
183 class InteractiveShell(object,Magic):
183 class InteractiveShell(object,Magic):
184 """An enhanced console for Python."""
184 """An enhanced console for Python."""
185
185
186 # class attribute to indicate whether the class supports threads or not.
186 # class attribute to indicate whether the class supports threads or not.
187 # Subclasses with thread support should override this as needed.
187 # Subclasses with thread support should override this as needed.
188 isthreaded = False
188 isthreaded = False
189
189
190 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
190 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
191 user_ns = None,user_global_ns=None,banner2='',
191 user_ns = None,user_global_ns=None,banner2='',
192 custom_exceptions=((),None),embedded=False):
192 custom_exceptions=((),None),embedded=False):
193
193
194 # log system
194 # log system
195 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
195 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
196
196
197 # introduce ourselves to IPython.ipapi which is uncallable
197 # introduce ourselves to IPython.ipapi which is uncallable
198 # before it knows an InteractiveShell object.
198 # before it knows an InteractiveShell object.
199 IPython.ipapi._init_with_shell(self)
199 IPython.ipapi._init_with_shell(self)
200
200
201 # some minimal strict typechecks. For some core data structures, I
201 # some minimal strict typechecks. For some core data structures, I
202 # want actual basic python types, not just anything that looks like
202 # want actual basic python types, not just anything that looks like
203 # one. This is especially true for namespaces.
203 # one. This is especially true for namespaces.
204 for ns in (user_ns,user_global_ns):
204 for ns in (user_ns,user_global_ns):
205 if ns is not None and type(ns) != types.DictType:
205 if ns is not None and type(ns) != types.DictType:
206 raise TypeError,'namespace must be a dictionary'
206 raise TypeError,'namespace must be a dictionary'
207
207
208 # Job manager (for jobs run as background threads)
208 # Job manager (for jobs run as background threads)
209 self.jobs = BackgroundJobManager()
209 self.jobs = BackgroundJobManager()
210
210
211 # track which builtins we add, so we can clean up later
211 # track which builtins we add, so we can clean up later
212 self.builtins_added = {}
212 self.builtins_added = {}
213 # This method will add the necessary builtins for operation, but
213 # This method will add the necessary builtins for operation, but
214 # tracking what it did via the builtins_added dict.
214 # tracking what it did via the builtins_added dict.
215 self.add_builtins()
215 self.add_builtins()
216
216
217 # Do the intuitively correct thing for quit/exit: we remove the
217 # Do the intuitively correct thing for quit/exit: we remove the
218 # builtins if they exist, and our own magics will deal with this
218 # builtins if they exist, and our own magics will deal with this
219 try:
219 try:
220 del __builtin__.exit, __builtin__.quit
220 del __builtin__.exit, __builtin__.quit
221 except AttributeError:
221 except AttributeError:
222 pass
222 pass
223
223
224 # Store the actual shell's name
224 # Store the actual shell's name
225 self.name = name
225 self.name = name
226
226
227 # We need to know whether the instance is meant for embedding, since
227 # We need to know whether the instance is meant for embedding, since
228 # global/local namespaces need to be handled differently in that case
228 # global/local namespaces need to be handled differently in that case
229 self.embedded = embedded
229 self.embedded = embedded
230
230
231 # command compiler
231 # command compiler
232 self.compile = codeop.CommandCompiler()
232 self.compile = codeop.CommandCompiler()
233
233
234 # User input buffer
234 # User input buffer
235 self.buffer = []
235 self.buffer = []
236
236
237 # Default name given in compilation of code
237 # Default name given in compilation of code
238 self.filename = '<ipython console>'
238 self.filename = '<ipython console>'
239
239
240 # Make an empty namespace, which extension writers can rely on both
240 # Make an empty namespace, which extension writers can rely on both
241 # existing and NEVER being used by ipython itself. This gives them a
241 # existing and NEVER being used by ipython itself. This gives them a
242 # convenient location for storing additional information and state
242 # convenient location for storing additional information and state
243 # their extensions may require, without fear of collisions with other
243 # their extensions may require, without fear of collisions with other
244 # ipython names that may develop later.
244 # ipython names that may develop later.
245 self.meta = Bunch()
245 self.meta = Bunch()
246
246
247 # Create the namespace where the user will operate. user_ns is
247 # Create the namespace where the user will operate. user_ns is
248 # normally the only one used, and it is passed to the exec calls as
248 # normally the only one used, and it is passed to the exec calls as
249 # the locals argument. But we do carry a user_global_ns namespace
249 # the locals argument. But we do carry a user_global_ns namespace
250 # given as the exec 'globals' argument, This is useful in embedding
250 # given as the exec 'globals' argument, This is useful in embedding
251 # situations where the ipython shell opens in a context where the
251 # situations where the ipython shell opens in a context where the
252 # distinction between locals and globals is meaningful.
252 # distinction between locals and globals is meaningful.
253
253
254 # FIXME. For some strange reason, __builtins__ is showing up at user
254 # FIXME. For some strange reason, __builtins__ is showing up at user
255 # level as a dict instead of a module. This is a manual fix, but I
255 # level as a dict instead of a module. This is a manual fix, but I
256 # should really track down where the problem is coming from. Alex
256 # should really track down where the problem is coming from. Alex
257 # Schmolck reported this problem first.
257 # Schmolck reported this problem first.
258
258
259 # A useful post by Alex Martelli on this topic:
259 # A useful post by Alex Martelli on this topic:
260 # Re: inconsistent value from __builtins__
260 # Re: inconsistent value from __builtins__
261 # Von: Alex Martelli <aleaxit@yahoo.com>
261 # Von: Alex Martelli <aleaxit@yahoo.com>
262 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
262 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
263 # Gruppen: comp.lang.python
263 # Gruppen: comp.lang.python
264
264
265 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
265 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
266 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
266 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
267 # > <type 'dict'>
267 # > <type 'dict'>
268 # > >>> print type(__builtins__)
268 # > >>> print type(__builtins__)
269 # > <type 'module'>
269 # > <type 'module'>
270 # > Is this difference in return value intentional?
270 # > Is this difference in return value intentional?
271
271
272 # Well, it's documented that '__builtins__' can be either a dictionary
272 # Well, it's documented that '__builtins__' can be either a dictionary
273 # or a module, and it's been that way for a long time. Whether it's
273 # or a module, and it's been that way for a long time. Whether it's
274 # intentional (or sensible), I don't know. In any case, the idea is
274 # intentional (or sensible), I don't know. In any case, the idea is
275 # that if you need to access the built-in namespace directly, you
275 # that if you need to access the built-in namespace directly, you
276 # should start with "import __builtin__" (note, no 's') which will
276 # should start with "import __builtin__" (note, no 's') which will
277 # definitely give you a module. Yeah, it's somewhat confusing:-(.
277 # definitely give you a module. Yeah, it's somewhat confusing:-(.
278
278
279 if user_ns is None:
279 if user_ns is None:
280 # Set __name__ to __main__ to better match the behavior of the
280 # Set __name__ to __main__ to better match the behavior of the
281 # normal interpreter.
281 # normal interpreter.
282 user_ns = {'__name__' :'__main__',
282 user_ns = {'__name__' :'__main__',
283 '__builtins__' : __builtin__,
283 '__builtins__' : __builtin__,
284 }
284 }
285
285
286 if user_global_ns is None:
286 if user_global_ns is None:
287 user_global_ns = {}
287 user_global_ns = {}
288
288
289 # Assign namespaces
289 # Assign namespaces
290 # This is the namespace where all normal user variables live
290 # This is the namespace where all normal user variables live
291 self.user_ns = user_ns
291 self.user_ns = user_ns
292 # Embedded instances require a separate namespace for globals.
292 # Embedded instances require a separate namespace for globals.
293 # Normally this one is unused by non-embedded instances.
293 # Normally this one is unused by non-embedded instances.
294 self.user_global_ns = user_global_ns
294 self.user_global_ns = user_global_ns
295 # A namespace to keep track of internal data structures to prevent
295 # A namespace to keep track of internal data structures to prevent
296 # them from cluttering user-visible stuff. Will be updated later
296 # them from cluttering user-visible stuff. Will be updated later
297 self.internal_ns = {}
297 self.internal_ns = {}
298
298
299 # Namespace of system aliases. Each entry in the alias
299 # Namespace of system aliases. Each entry in the alias
300 # table must be a 2-tuple of the form (N,name), where N is the number
300 # table must be a 2-tuple of the form (N,name), where N is the number
301 # of positional arguments of the alias.
301 # of positional arguments of the alias.
302 self.alias_table = {}
302 self.alias_table = {}
303
303
304 # A table holding all the namespaces IPython deals with, so that
304 # A table holding all the namespaces IPython deals with, so that
305 # introspection facilities can search easily.
305 # introspection facilities can search easily.
306 self.ns_table = {'user':user_ns,
306 self.ns_table = {'user':user_ns,
307 'user_global':user_global_ns,
307 'user_global':user_global_ns,
308 'alias':self.alias_table,
308 'alias':self.alias_table,
309 'internal':self.internal_ns,
309 'internal':self.internal_ns,
310 'builtin':__builtin__.__dict__
310 'builtin':__builtin__.__dict__
311 }
311 }
312
312
313 # The user namespace MUST have a pointer to the shell itself.
313 # The user namespace MUST have a pointer to the shell itself.
314 self.user_ns[name] = self
314 self.user_ns[name] = self
315
315
316 # We need to insert into sys.modules something that looks like a
316 # We need to insert into sys.modules something that looks like a
317 # module but which accesses the IPython namespace, for shelve and
317 # module but which accesses the IPython namespace, for shelve and
318 # pickle to work interactively. Normally they rely on getting
318 # pickle to work interactively. Normally they rely on getting
319 # everything out of __main__, but for embedding purposes each IPython
319 # everything out of __main__, but for embedding purposes each IPython
320 # instance has its own private namespace, so we can't go shoving
320 # instance has its own private namespace, so we can't go shoving
321 # everything into __main__.
321 # everything into __main__.
322
322
323 # note, however, that we should only do this for non-embedded
323 # note, however, that we should only do this for non-embedded
324 # ipythons, which really mimic the __main__.__dict__ with their own
324 # ipythons, which really mimic the __main__.__dict__ with their own
325 # namespace. Embedded instances, on the other hand, should not do
325 # namespace. Embedded instances, on the other hand, should not do
326 # this because they need to manage the user local/global namespaces
326 # this because they need to manage the user local/global namespaces
327 # only, but they live within a 'normal' __main__ (meaning, they
327 # only, but they live within a 'normal' __main__ (meaning, they
328 # shouldn't overtake the execution environment of the script they're
328 # shouldn't overtake the execution environment of the script they're
329 # embedded in).
329 # embedded in).
330
330
331 if not embedded:
331 if not embedded:
332 try:
332 try:
333 main_name = self.user_ns['__name__']
333 main_name = self.user_ns['__name__']
334 except KeyError:
334 except KeyError:
335 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
335 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
336 else:
336 else:
337 #print "pickle hack in place" # dbg
337 #print "pickle hack in place" # dbg
338 #print 'main_name:',main_name # dbg
338 #print 'main_name:',main_name # dbg
339 sys.modules[main_name] = FakeModule(self.user_ns)
339 sys.modules[main_name] = FakeModule(self.user_ns)
340
340
341 # List of input with multi-line handling.
341 # List of input with multi-line handling.
342 # Fill its zero entry, user counter starts at 1
342 # Fill its zero entry, user counter starts at 1
343 self.input_hist = InputList(['\n'])
343 self.input_hist = InputList(['\n'])
344 # This one will hold the 'raw' input history, without any
345 # pre-processing. This will allow users to retrieve the input just as
346 # it was exactly typed in by the user, with %hist -r.
347 self.input_hist_raw = InputList(['\n'])
344
348
345 # list of visited directories
349 # list of visited directories
346 try:
350 try:
347 self.dir_hist = [os.getcwd()]
351 self.dir_hist = [os.getcwd()]
348 except IOError, e:
352 except IOError, e:
349 self.dir_hist = []
353 self.dir_hist = []
350
354
351 # dict of output history
355 # dict of output history
352 self.output_hist = {}
356 self.output_hist = {}
353
357
354 # dict of things NOT to alias (keywords, builtins and some magics)
358 # dict of things NOT to alias (keywords, builtins and some magics)
355 no_alias = {}
359 no_alias = {}
356 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
360 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
357 for key in keyword.kwlist + no_alias_magics:
361 for key in keyword.kwlist + no_alias_magics:
358 no_alias[key] = 1
362 no_alias[key] = 1
359 no_alias.update(__builtin__.__dict__)
363 no_alias.update(__builtin__.__dict__)
360 self.no_alias = no_alias
364 self.no_alias = no_alias
361
365
362 # make global variables for user access to these
366 # make global variables for user access to these
363 self.user_ns['_ih'] = self.input_hist
367 self.user_ns['_ih'] = self.input_hist
364 self.user_ns['_oh'] = self.output_hist
368 self.user_ns['_oh'] = self.output_hist
365 self.user_ns['_dh'] = self.dir_hist
369 self.user_ns['_dh'] = self.dir_hist
366
370
367 # user aliases to input and output histories
371 # user aliases to input and output histories
368 self.user_ns['In'] = self.input_hist
372 self.user_ns['In'] = self.input_hist
369 self.user_ns['Out'] = self.output_hist
373 self.user_ns['Out'] = self.output_hist
370
374
371 # Object variable to store code object waiting execution. This is
375 # Object variable to store code object waiting execution. This is
372 # used mainly by the multithreaded shells, but it can come in handy in
376 # used mainly by the multithreaded shells, but it can come in handy in
373 # other situations. No need to use a Queue here, since it's a single
377 # other situations. No need to use a Queue here, since it's a single
374 # item which gets cleared once run.
378 # item which gets cleared once run.
375 self.code_to_run = None
379 self.code_to_run = None
376
380
377 # escapes for automatic behavior on the command line
381 # escapes for automatic behavior on the command line
378 self.ESC_SHELL = '!'
382 self.ESC_SHELL = '!'
379 self.ESC_HELP = '?'
383 self.ESC_HELP = '?'
380 self.ESC_MAGIC = '%'
384 self.ESC_MAGIC = '%'
381 self.ESC_QUOTE = ','
385 self.ESC_QUOTE = ','
382 self.ESC_QUOTE2 = ';'
386 self.ESC_QUOTE2 = ';'
383 self.ESC_PAREN = '/'
387 self.ESC_PAREN = '/'
384
388
385 # And their associated handlers
389 # And their associated handlers
386 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
390 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
387 self.ESC_QUOTE : self.handle_auto,
391 self.ESC_QUOTE : self.handle_auto,
388 self.ESC_QUOTE2 : self.handle_auto,
392 self.ESC_QUOTE2 : self.handle_auto,
389 self.ESC_MAGIC : self.handle_magic,
393 self.ESC_MAGIC : self.handle_magic,
390 self.ESC_HELP : self.handle_help,
394 self.ESC_HELP : self.handle_help,
391 self.ESC_SHELL : self.handle_shell_escape,
395 self.ESC_SHELL : self.handle_shell_escape,
392 }
396 }
393
397
394 # class initializations
398 # class initializations
395 Magic.__init__(self,self)
399 Magic.__init__(self,self)
396
400
397 # Python source parser/formatter for syntax highlighting
401 # Python source parser/formatter for syntax highlighting
398 pyformat = PyColorize.Parser().format
402 pyformat = PyColorize.Parser().format
399 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
403 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
400
404
401 # hooks holds pointers used for user-side customizations
405 # hooks holds pointers used for user-side customizations
402 self.hooks = Struct()
406 self.hooks = Struct()
403
407
404 # Set all default hooks, defined in the IPython.hooks module.
408 # Set all default hooks, defined in the IPython.hooks module.
405 hooks = IPython.hooks
409 hooks = IPython.hooks
406 for hook_name in hooks.__all__:
410 for hook_name in hooks.__all__:
407 # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority
411 # default hooks have priority 100, i.e. low; user hooks should have 0-100 priority
408 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
412 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
409
413
410 # Flag to mark unconditional exit
414 # Flag to mark unconditional exit
411 self.exit_now = False
415 self.exit_now = False
412
416
413 self.usage_min = """\
417 self.usage_min = """\
414 An enhanced console for Python.
418 An enhanced console for Python.
415 Some of its features are:
419 Some of its features are:
416 - Readline support if the readline library is present.
420 - Readline support if the readline library is present.
417 - Tab completion in the local namespace.
421 - Tab completion in the local namespace.
418 - Logging of input, see command-line options.
422 - Logging of input, see command-line options.
419 - System shell escape via ! , eg !ls.
423 - System shell escape via ! , eg !ls.
420 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
424 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
421 - Keeps track of locally defined variables via %who, %whos.
425 - Keeps track of locally defined variables via %who, %whos.
422 - Show object information with a ? eg ?x or x? (use ?? for more info).
426 - Show object information with a ? eg ?x or x? (use ?? for more info).
423 """
427 """
424 if usage: self.usage = usage
428 if usage: self.usage = usage
425 else: self.usage = self.usage_min
429 else: self.usage = self.usage_min
426
430
427 # Storage
431 # Storage
428 self.rc = rc # This will hold all configuration information
432 self.rc = rc # This will hold all configuration information
429 self.pager = 'less'
433 self.pager = 'less'
430 # temporary files used for various purposes. Deleted at exit.
434 # temporary files used for various purposes. Deleted at exit.
431 self.tempfiles = []
435 self.tempfiles = []
432
436
433 # Keep track of readline usage (later set by init_readline)
437 # Keep track of readline usage (later set by init_readline)
434 self.has_readline = False
438 self.has_readline = False
435
439
436 # template for logfile headers. It gets resolved at runtime by the
440 # template for logfile headers. It gets resolved at runtime by the
437 # logstart method.
441 # logstart method.
438 self.loghead_tpl = \
442 self.loghead_tpl = \
439 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
443 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
440 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
444 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
441 #log# opts = %s
445 #log# opts = %s
442 #log# args = %s
446 #log# args = %s
443 #log# It is safe to make manual edits below here.
447 #log# It is safe to make manual edits below here.
444 #log#-----------------------------------------------------------------------
448 #log#-----------------------------------------------------------------------
445 """
449 """
446 # for pushd/popd management
450 # for pushd/popd management
447 try:
451 try:
448 self.home_dir = get_home_dir()
452 self.home_dir = get_home_dir()
449 except HomeDirError,msg:
453 except HomeDirError,msg:
450 fatal(msg)
454 fatal(msg)
451
455
452 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
456 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
453
457
454 # Functions to call the underlying shell.
458 # Functions to call the underlying shell.
455
459
456 # utility to expand user variables via Itpl
460 # utility to expand user variables via Itpl
457 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
461 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
458 self.user_ns))
462 self.user_ns))
459 # The first is similar to os.system, but it doesn't return a value,
463 # The first is similar to os.system, but it doesn't return a value,
460 # and it allows interpolation of variables in the user's namespace.
464 # and it allows interpolation of variables in the user's namespace.
461 self.system = lambda cmd: shell(self.var_expand(cmd),
465 self.system = lambda cmd: shell(self.var_expand(cmd),
462 header='IPython system call: ',
466 header='IPython system call: ',
463 verbose=self.rc.system_verbose)
467 verbose=self.rc.system_verbose)
464 # These are for getoutput and getoutputerror:
468 # These are for getoutput and getoutputerror:
465 self.getoutput = lambda cmd: \
469 self.getoutput = lambda cmd: \
466 getoutput(self.var_expand(cmd),
470 getoutput(self.var_expand(cmd),
467 header='IPython system call: ',
471 header='IPython system call: ',
468 verbose=self.rc.system_verbose)
472 verbose=self.rc.system_verbose)
469 self.getoutputerror = lambda cmd: \
473 self.getoutputerror = lambda cmd: \
470 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
474 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
471 self.user_ns)),
475 self.user_ns)),
472 header='IPython system call: ',
476 header='IPython system call: ',
473 verbose=self.rc.system_verbose)
477 verbose=self.rc.system_verbose)
474
478
475 # RegExp for splitting line contents into pre-char//first
479 # RegExp for splitting line contents into pre-char//first
476 # word-method//rest. For clarity, each group in on one line.
480 # word-method//rest. For clarity, each group in on one line.
477
481
478 # WARNING: update the regexp if the above escapes are changed, as they
482 # WARNING: update the regexp if the above escapes are changed, as they
479 # are hardwired in.
483 # are hardwired in.
480
484
481 # Don't get carried away with trying to make the autocalling catch too
485 # Don't get carried away with trying to make the autocalling catch too
482 # much: it's better to be conservative rather than to trigger hidden
486 # much: it's better to be conservative rather than to trigger hidden
483 # evals() somewhere and end up causing side effects.
487 # evals() somewhere and end up causing side effects.
484
488
485 self.line_split = re.compile(r'^([\s*,;/])'
489 self.line_split = re.compile(r'^([\s*,;/])'
486 r'([\?\w\.]+\w*\s*)'
490 r'([\?\w\.]+\w*\s*)'
487 r'(\(?.*$)')
491 r'(\(?.*$)')
488
492
489 # Original re, keep around for a while in case changes break something
493 # Original re, keep around for a while in case changes break something
490 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
494 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
491 # r'(\s*[\?\w\.]+\w*\s*)'
495 # r'(\s*[\?\w\.]+\w*\s*)'
492 # r'(\(?.*$)')
496 # r'(\(?.*$)')
493
497
494 # RegExp to identify potential function names
498 # RegExp to identify potential function names
495 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
499 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
496
500
497 # RegExp to exclude strings with this start from autocalling. In
501 # RegExp to exclude strings with this start from autocalling. In
498 # particular, all binary operators should be excluded, so that if foo
502 # particular, all binary operators should be excluded, so that if foo
499 # is callable, foo OP bar doesn't become foo(OP bar), which is
503 # is callable, foo OP bar doesn't become foo(OP bar), which is
500 # invalid. The characters '!=()' don't need to be checked for, as the
504 # invalid. The characters '!=()' don't need to be checked for, as the
501 # _prefilter routine explicitely does so, to catch direct calls and
505 # _prefilter routine explicitely does so, to catch direct calls and
502 # rebindings of existing names.
506 # rebindings of existing names.
503
507
504 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
508 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
505 # it affects the rest of the group in square brackets.
509 # it affects the rest of the group in square brackets.
506 self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]'
510 self.re_exclude_auto = re.compile(r'^[<>,&^\|\*/\+-]'
507 '|^is |^not |^in |^and |^or ')
511 '|^is |^not |^in |^and |^or ')
508
512
509 # try to catch also methods for stuff in lists/tuples/dicts: off
513 # try to catch also methods for stuff in lists/tuples/dicts: off
510 # (experimental). For this to work, the line_split regexp would need
514 # (experimental). For this to work, the line_split regexp would need
511 # to be modified so it wouldn't break things at '['. That line is
515 # to be modified so it wouldn't break things at '['. That line is
512 # nasty enough that I shouldn't change it until I can test it _well_.
516 # nasty enough that I shouldn't change it until I can test it _well_.
513 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
517 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
514
518
515 # keep track of where we started running (mainly for crash post-mortem)
519 # keep track of where we started running (mainly for crash post-mortem)
516 self.starting_dir = os.getcwd()
520 self.starting_dir = os.getcwd()
517
521
518 # Various switches which can be set
522 # Various switches which can be set
519 self.CACHELENGTH = 5000 # this is cheap, it's just text
523 self.CACHELENGTH = 5000 # this is cheap, it's just text
520 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
524 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
521 self.banner2 = banner2
525 self.banner2 = banner2
522
526
523 # TraceBack handlers:
527 # TraceBack handlers:
524
528
525 # Syntax error handler.
529 # Syntax error handler.
526 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
530 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
527
531
528 # The interactive one is initialized with an offset, meaning we always
532 # The interactive one is initialized with an offset, meaning we always
529 # want to remove the topmost item in the traceback, which is our own
533 # want to remove the topmost item in the traceback, which is our own
530 # internal code. Valid modes: ['Plain','Context','Verbose']
534 # internal code. Valid modes: ['Plain','Context','Verbose']
531 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
535 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
532 color_scheme='NoColor',
536 color_scheme='NoColor',
533 tb_offset = 1)
537 tb_offset = 1)
534
538
535 # IPython itself shouldn't crash. This will produce a detailed
539 # IPython itself shouldn't crash. This will produce a detailed
536 # post-mortem if it does. But we only install the crash handler for
540 # post-mortem if it does. But we only install the crash handler for
537 # non-threaded shells, the threaded ones use a normal verbose reporter
541 # non-threaded shells, the threaded ones use a normal verbose reporter
538 # and lose the crash handler. This is because exceptions in the main
542 # and lose the crash handler. This is because exceptions in the main
539 # thread (such as in GUI code) propagate directly to sys.excepthook,
543 # thread (such as in GUI code) propagate directly to sys.excepthook,
540 # and there's no point in printing crash dumps for every user exception.
544 # and there's no point in printing crash dumps for every user exception.
541 if self.isthreaded:
545 if self.isthreaded:
542 sys.excepthook = ultraTB.FormattedTB()
546 sys.excepthook = ultraTB.FormattedTB()
543 else:
547 else:
544 from IPython import CrashHandler
548 from IPython import CrashHandler
545 sys.excepthook = CrashHandler.CrashHandler(self)
549 sys.excepthook = CrashHandler.CrashHandler(self)
546
550
547 # The instance will store a pointer to this, so that runtime code
551 # The instance will store a pointer to this, so that runtime code
548 # (such as magics) can access it. This is because during the
552 # (such as magics) can access it. This is because during the
549 # read-eval loop, it gets temporarily overwritten (to deal with GUI
553 # read-eval loop, it gets temporarily overwritten (to deal with GUI
550 # frameworks).
554 # frameworks).
551 self.sys_excepthook = sys.excepthook
555 self.sys_excepthook = sys.excepthook
552
556
553 # and add any custom exception handlers the user may have specified
557 # and add any custom exception handlers the user may have specified
554 self.set_custom_exc(*custom_exceptions)
558 self.set_custom_exc(*custom_exceptions)
555
559
556 # Object inspector
560 # Object inspector
557 self.inspector = OInspect.Inspector(OInspect.InspectColors,
561 self.inspector = OInspect.Inspector(OInspect.InspectColors,
558 PyColorize.ANSICodeColors,
562 PyColorize.ANSICodeColors,
559 'NoColor')
563 'NoColor')
560 # indentation management
564 # indentation management
561 self.autoindent = False
565 self.autoindent = False
562 self.indent_current_nsp = 0
566 self.indent_current_nsp = 0
563
567
564 # Make some aliases automatically
568 # Make some aliases automatically
565 # Prepare list of shell aliases to auto-define
569 # Prepare list of shell aliases to auto-define
566 if os.name == 'posix':
570 if os.name == 'posix':
567 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
571 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
568 'mv mv -i','rm rm -i','cp cp -i',
572 'mv mv -i','rm rm -i','cp cp -i',
569 'cat cat','less less','clear clear',
573 'cat cat','less less','clear clear',
570 # a better ls
574 # a better ls
571 'ls ls -F',
575 'ls ls -F',
572 # long ls
576 # long ls
573 'll ls -lF',
577 'll ls -lF',
574 # color ls
578 # color ls
575 'lc ls -F -o --color',
579 'lc ls -F -o --color',
576 # ls normal files only
580 # ls normal files only
577 'lf ls -F -o --color %l | grep ^-',
581 'lf ls -F -o --color %l | grep ^-',
578 # ls symbolic links
582 # ls symbolic links
579 'lk ls -F -o --color %l | grep ^l',
583 'lk ls -F -o --color %l | grep ^l',
580 # directories or links to directories,
584 # directories or links to directories,
581 'ldir ls -F -o --color %l | grep /$',
585 'ldir ls -F -o --color %l | grep /$',
582 # things which are executable
586 # things which are executable
583 'lx ls -F -o --color %l | grep ^-..x',
587 'lx ls -F -o --color %l | grep ^-..x',
584 )
588 )
585 elif os.name in ['nt','dos']:
589 elif os.name in ['nt','dos']:
586 auto_alias = ('dir dir /on', 'ls dir /on',
590 auto_alias = ('dir dir /on', 'ls dir /on',
587 'ddir dir /ad /on', 'ldir dir /ad /on',
591 'ddir dir /ad /on', 'ldir dir /ad /on',
588 'mkdir mkdir','rmdir rmdir','echo echo',
592 'mkdir mkdir','rmdir rmdir','echo echo',
589 'ren ren','cls cls','copy copy')
593 'ren ren','cls cls','copy copy')
590 else:
594 else:
591 auto_alias = ()
595 auto_alias = ()
592 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
596 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
593 # Call the actual (public) initializer
597 # Call the actual (public) initializer
594 self.init_auto_alias()
598 self.init_auto_alias()
595 # end __init__
599 # end __init__
596
600
597 def post_config_initialization(self):
601 def post_config_initialization(self):
598 """Post configuration init method
602 """Post configuration init method
599
603
600 This is called after the configuration files have been processed to
604 This is called after the configuration files have been processed to
601 'finalize' the initialization."""
605 'finalize' the initialization."""
602
606
603 rc = self.rc
607 rc = self.rc
604
608
605 # Load readline proper
609 # Load readline proper
606 if rc.readline:
610 if rc.readline:
607 self.init_readline()
611 self.init_readline()
608
612
609 # local shortcut, this is used a LOT
613 # local shortcut, this is used a LOT
610 self.log = self.logger.log
614 self.log = self.logger.log
611
615
612 # Initialize cache, set in/out prompts and printing system
616 # Initialize cache, set in/out prompts and printing system
613 self.outputcache = CachedOutput(self,
617 self.outputcache = CachedOutput(self,
614 rc.cache_size,
618 rc.cache_size,
615 rc.pprint,
619 rc.pprint,
616 input_sep = rc.separate_in,
620 input_sep = rc.separate_in,
617 output_sep = rc.separate_out,
621 output_sep = rc.separate_out,
618 output_sep2 = rc.separate_out2,
622 output_sep2 = rc.separate_out2,
619 ps1 = rc.prompt_in1,
623 ps1 = rc.prompt_in1,
620 ps2 = rc.prompt_in2,
624 ps2 = rc.prompt_in2,
621 ps_out = rc.prompt_out,
625 ps_out = rc.prompt_out,
622 pad_left = rc.prompts_pad_left)
626 pad_left = rc.prompts_pad_left)
623
627
624 # user may have over-ridden the default print hook:
628 # user may have over-ridden the default print hook:
625 try:
629 try:
626 self.outputcache.__class__.display = self.hooks.display
630 self.outputcache.__class__.display = self.hooks.display
627 except AttributeError:
631 except AttributeError:
628 pass
632 pass
629
633
630 # I don't like assigning globally to sys, because it means when embedding
634 # I don't like assigning globally to sys, because it means when embedding
631 # instances, each embedded instance overrides the previous choice. But
635 # instances, each embedded instance overrides the previous choice. But
632 # sys.displayhook seems to be called internally by exec, so I don't see a
636 # sys.displayhook seems to be called internally by exec, so I don't see a
633 # way around it.
637 # way around it.
634 sys.displayhook = self.outputcache
638 sys.displayhook = self.outputcache
635
639
636 # Set user colors (don't do it in the constructor above so that it
640 # Set user colors (don't do it in the constructor above so that it
637 # doesn't crash if colors option is invalid)
641 # doesn't crash if colors option is invalid)
638 self.magic_colors(rc.colors)
642 self.magic_colors(rc.colors)
639
643
640 # Set calling of pdb on exceptions
644 # Set calling of pdb on exceptions
641 self.call_pdb = rc.pdb
645 self.call_pdb = rc.pdb
642
646
643 # Load user aliases
647 # Load user aliases
644 for alias in rc.alias:
648 for alias in rc.alias:
645 self.magic_alias(alias)
649 self.magic_alias(alias)
646
650
647 # dynamic data that survives through sessions
651 # dynamic data that survives through sessions
648 # XXX make the filename a config option?
652 # XXX make the filename a config option?
649 persist_base = 'persist'
653 persist_base = 'persist'
650 if rc.profile:
654 if rc.profile:
651 persist_base += '_%s' % rc.profile
655 persist_base += '_%s' % rc.profile
652 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
656 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
653
657
654 try:
658 try:
655 self.persist = pickle.load(file(self.persist_fname))
659 self.persist = pickle.load(file(self.persist_fname))
656 except:
660 except:
657 self.persist = {}
661 self.persist = {}
658
662
659
663
660 for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]:
664 for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]:
661 try:
665 try:
662 obj = pickle.loads(value)
666 obj = pickle.loads(value)
663 except:
667 except:
664
668
665 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key
669 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key
666 print "The error was:",sys.exc_info()[0]
670 print "The error was:",sys.exc_info()[0]
667 continue
671 continue
668
672
669
673
670 self.user_ns[key] = obj
674 self.user_ns[key] = obj
671
675
672 def add_builtins(self):
676 def add_builtins(self):
673 """Store ipython references into the builtin namespace.
677 """Store ipython references into the builtin namespace.
674
678
675 Some parts of ipython operate via builtins injected here, which hold a
679 Some parts of ipython operate via builtins injected here, which hold a
676 reference to IPython itself."""
680 reference to IPython itself."""
677
681
678 builtins_new = dict(__IPYTHON__ = self,
682 builtins_new = dict(__IPYTHON__ = self,
679 ip_set_hook = self.set_hook,
683 ip_set_hook = self.set_hook,
680 jobs = self.jobs,
684 jobs = self.jobs,
681 ipmagic = self.ipmagic,
685 ipmagic = self.ipmagic,
682 ipalias = self.ipalias,
686 ipalias = self.ipalias,
683 ipsystem = self.ipsystem,
687 ipsystem = self.ipsystem,
684 )
688 )
685 for biname,bival in builtins_new.items():
689 for biname,bival in builtins_new.items():
686 try:
690 try:
687 # store the orignal value so we can restore it
691 # store the orignal value so we can restore it
688 self.builtins_added[biname] = __builtin__.__dict__[biname]
692 self.builtins_added[biname] = __builtin__.__dict__[biname]
689 except KeyError:
693 except KeyError:
690 # or mark that it wasn't defined, and we'll just delete it at
694 # or mark that it wasn't defined, and we'll just delete it at
691 # cleanup
695 # cleanup
692 self.builtins_added[biname] = Undefined
696 self.builtins_added[biname] = Undefined
693 __builtin__.__dict__[biname] = bival
697 __builtin__.__dict__[biname] = bival
694
698
695 # Keep in the builtins a flag for when IPython is active. We set it
699 # Keep in the builtins a flag for when IPython is active. We set it
696 # with setdefault so that multiple nested IPythons don't clobber one
700 # with setdefault so that multiple nested IPythons don't clobber one
697 # another. Each will increase its value by one upon being activated,
701 # another. Each will increase its value by one upon being activated,
698 # which also gives us a way to determine the nesting level.
702 # which also gives us a way to determine the nesting level.
699 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
703 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
700
704
701 def clean_builtins(self):
705 def clean_builtins(self):
702 """Remove any builtins which might have been added by add_builtins, or
706 """Remove any builtins which might have been added by add_builtins, or
703 restore overwritten ones to their previous values."""
707 restore overwritten ones to their previous values."""
704 for biname,bival in self.builtins_added.items():
708 for biname,bival in self.builtins_added.items():
705 if bival is Undefined:
709 if bival is Undefined:
706 del __builtin__.__dict__[biname]
710 del __builtin__.__dict__[biname]
707 else:
711 else:
708 __builtin__.__dict__[biname] = bival
712 __builtin__.__dict__[biname] = bival
709 self.builtins_added.clear()
713 self.builtins_added.clear()
710
714
711 def set_hook(self,name,hook, priority = 50):
715 def set_hook(self,name,hook, priority = 50):
712 """set_hook(name,hook) -> sets an internal IPython hook.
716 """set_hook(name,hook) -> sets an internal IPython hook.
713
717
714 IPython exposes some of its internal API as user-modifiable hooks. By
718 IPython exposes some of its internal API as user-modifiable hooks. By
715 adding your function to one of these hooks, you can modify IPython's
719 adding your function to one of these hooks, you can modify IPython's
716 behavior to call at runtime your own routines."""
720 behavior to call at runtime your own routines."""
717
721
718 # At some point in the future, this should validate the hook before it
722 # At some point in the future, this should validate the hook before it
719 # accepts it. Probably at least check that the hook takes the number
723 # accepts it. Probably at least check that the hook takes the number
720 # of args it's supposed to.
724 # of args it's supposed to.
721 dp = getattr(self.hooks, name, None)
725 dp = getattr(self.hooks, name, None)
722 if not dp:
726 if not dp:
723 dp = IPython.hooks.CommandChainDispatcher()
727 dp = IPython.hooks.CommandChainDispatcher()
724
728
725 f = new.instancemethod(hook,self,self.__class__)
729 f = new.instancemethod(hook,self,self.__class__)
726 try:
730 try:
727 dp.add(f,priority)
731 dp.add(f,priority)
728 except AttributeError:
732 except AttributeError:
729 # it was not commandchain, plain old func - replace
733 # it was not commandchain, plain old func - replace
730 dp = f
734 dp = f
731
735
732 setattr(self.hooks,name, dp)
736 setattr(self.hooks,name, dp)
733
737
734
738
735 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
739 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
736
740
737 def set_custom_exc(self,exc_tuple,handler):
741 def set_custom_exc(self,exc_tuple,handler):
738 """set_custom_exc(exc_tuple,handler)
742 """set_custom_exc(exc_tuple,handler)
739
743
740 Set a custom exception handler, which will be called if any of the
744 Set a custom exception handler, which will be called if any of the
741 exceptions in exc_tuple occur in the mainloop (specifically, in the
745 exceptions in exc_tuple occur in the mainloop (specifically, in the
742 runcode() method.
746 runcode() method.
743
747
744 Inputs:
748 Inputs:
745
749
746 - exc_tuple: a *tuple* of valid exceptions to call the defined
750 - exc_tuple: a *tuple* of valid exceptions to call the defined
747 handler for. It is very important that you use a tuple, and NOT A
751 handler for. It is very important that you use a tuple, and NOT A
748 LIST here, because of the way Python's except statement works. If
752 LIST here, because of the way Python's except statement works. If
749 you only want to trap a single exception, use a singleton tuple:
753 you only want to trap a single exception, use a singleton tuple:
750
754
751 exc_tuple == (MyCustomException,)
755 exc_tuple == (MyCustomException,)
752
756
753 - handler: this must be defined as a function with the following
757 - handler: this must be defined as a function with the following
754 basic interface: def my_handler(self,etype,value,tb).
758 basic interface: def my_handler(self,etype,value,tb).
755
759
756 This will be made into an instance method (via new.instancemethod)
760 This will be made into an instance method (via new.instancemethod)
757 of IPython itself, and it will be called if any of the exceptions
761 of IPython itself, and it will be called if any of the exceptions
758 listed in the exc_tuple are caught. If the handler is None, an
762 listed in the exc_tuple are caught. If the handler is None, an
759 internal basic one is used, which just prints basic info.
763 internal basic one is used, which just prints basic info.
760
764
761 WARNING: by putting in your own exception handler into IPython's main
765 WARNING: by putting in your own exception handler into IPython's main
762 execution loop, you run a very good chance of nasty crashes. This
766 execution loop, you run a very good chance of nasty crashes. This
763 facility should only be used if you really know what you are doing."""
767 facility should only be used if you really know what you are doing."""
764
768
765 assert type(exc_tuple)==type(()) , \
769 assert type(exc_tuple)==type(()) , \
766 "The custom exceptions must be given AS A TUPLE."
770 "The custom exceptions must be given AS A TUPLE."
767
771
768 def dummy_handler(self,etype,value,tb):
772 def dummy_handler(self,etype,value,tb):
769 print '*** Simple custom exception handler ***'
773 print '*** Simple custom exception handler ***'
770 print 'Exception type :',etype
774 print 'Exception type :',etype
771 print 'Exception value:',value
775 print 'Exception value:',value
772 print 'Traceback :',tb
776 print 'Traceback :',tb
773 print 'Source code :','\n'.join(self.buffer)
777 print 'Source code :','\n'.join(self.buffer)
774
778
775 if handler is None: handler = dummy_handler
779 if handler is None: handler = dummy_handler
776
780
777 self.CustomTB = new.instancemethod(handler,self,self.__class__)
781 self.CustomTB = new.instancemethod(handler,self,self.__class__)
778 self.custom_exceptions = exc_tuple
782 self.custom_exceptions = exc_tuple
779
783
780 def set_custom_completer(self,completer,pos=0):
784 def set_custom_completer(self,completer,pos=0):
781 """set_custom_completer(completer,pos=0)
785 """set_custom_completer(completer,pos=0)
782
786
783 Adds a new custom completer function.
787 Adds a new custom completer function.
784
788
785 The position argument (defaults to 0) is the index in the completers
789 The position argument (defaults to 0) is the index in the completers
786 list where you want the completer to be inserted."""
790 list where you want the completer to be inserted."""
787
791
788 newcomp = new.instancemethod(completer,self.Completer,
792 newcomp = new.instancemethod(completer,self.Completer,
789 self.Completer.__class__)
793 self.Completer.__class__)
790 self.Completer.matchers.insert(pos,newcomp)
794 self.Completer.matchers.insert(pos,newcomp)
791
795
792 def _get_call_pdb(self):
796 def _get_call_pdb(self):
793 return self._call_pdb
797 return self._call_pdb
794
798
795 def _set_call_pdb(self,val):
799 def _set_call_pdb(self,val):
796
800
797 if val not in (0,1,False,True):
801 if val not in (0,1,False,True):
798 raise ValueError,'new call_pdb value must be boolean'
802 raise ValueError,'new call_pdb value must be boolean'
799
803
800 # store value in instance
804 # store value in instance
801 self._call_pdb = val
805 self._call_pdb = val
802
806
803 # notify the actual exception handlers
807 # notify the actual exception handlers
804 self.InteractiveTB.call_pdb = val
808 self.InteractiveTB.call_pdb = val
805 if self.isthreaded:
809 if self.isthreaded:
806 try:
810 try:
807 self.sys_excepthook.call_pdb = val
811 self.sys_excepthook.call_pdb = val
808 except:
812 except:
809 warn('Failed to activate pdb for threaded exception handler')
813 warn('Failed to activate pdb for threaded exception handler')
810
814
811 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
815 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
812 'Control auto-activation of pdb at exceptions')
816 'Control auto-activation of pdb at exceptions')
813
817
814
818
815 # These special functions get installed in the builtin namespace, to
819 # These special functions get installed in the builtin namespace, to
816 # provide programmatic (pure python) access to magics, aliases and system
820 # provide programmatic (pure python) access to magics, aliases and system
817 # calls. This is important for logging, user scripting, and more.
821 # calls. This is important for logging, user scripting, and more.
818
822
819 # We are basically exposing, via normal python functions, the three
823 # We are basically exposing, via normal python functions, the three
820 # mechanisms in which ipython offers special call modes (magics for
824 # mechanisms in which ipython offers special call modes (magics for
821 # internal control, aliases for direct system access via pre-selected
825 # internal control, aliases for direct system access via pre-selected
822 # names, and !cmd for calling arbitrary system commands).
826 # names, and !cmd for calling arbitrary system commands).
823
827
824 def ipmagic(self,arg_s):
828 def ipmagic(self,arg_s):
825 """Call a magic function by name.
829 """Call a magic function by name.
826
830
827 Input: a string containing the name of the magic function to call and any
831 Input: a string containing the name of the magic function to call and any
828 additional arguments to be passed to the magic.
832 additional arguments to be passed to the magic.
829
833
830 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
834 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
831 prompt:
835 prompt:
832
836
833 In[1]: %name -opt foo bar
837 In[1]: %name -opt foo bar
834
838
835 To call a magic without arguments, simply use ipmagic('name').
839 To call a magic without arguments, simply use ipmagic('name').
836
840
837 This provides a proper Python function to call IPython's magics in any
841 This provides a proper Python function to call IPython's magics in any
838 valid Python code you can type at the interpreter, including loops and
842 valid Python code you can type at the interpreter, including loops and
839 compound statements. It is added by IPython to the Python builtin
843 compound statements. It is added by IPython to the Python builtin
840 namespace upon initialization."""
844 namespace upon initialization."""
841
845
842 args = arg_s.split(' ',1)
846 args = arg_s.split(' ',1)
843 magic_name = args[0]
847 magic_name = args[0]
844 magic_name = magic_name.lstrip(self.ESC_MAGIC)
848 magic_name = magic_name.lstrip(self.ESC_MAGIC)
845
849
846 try:
850 try:
847 magic_args = args[1]
851 magic_args = args[1]
848 except IndexError:
852 except IndexError:
849 magic_args = ''
853 magic_args = ''
850 fn = getattr(self,'magic_'+magic_name,None)
854 fn = getattr(self,'magic_'+magic_name,None)
851 if fn is None:
855 if fn is None:
852 error("Magic function `%s` not found." % magic_name)
856 error("Magic function `%s` not found." % magic_name)
853 else:
857 else:
854 magic_args = self.var_expand(magic_args)
858 magic_args = self.var_expand(magic_args)
855 return fn(magic_args)
859 return fn(magic_args)
856
860
857 def ipalias(self,arg_s):
861 def ipalias(self,arg_s):
858 """Call an alias by name.
862 """Call an alias by name.
859
863
860 Input: a string containing the name of the alias to call and any
864 Input: a string containing the name of the alias to call and any
861 additional arguments to be passed to the magic.
865 additional arguments to be passed to the magic.
862
866
863 ipalias('name -opt foo bar') is equivalent to typing at the ipython
867 ipalias('name -opt foo bar') is equivalent to typing at the ipython
864 prompt:
868 prompt:
865
869
866 In[1]: name -opt foo bar
870 In[1]: name -opt foo bar
867
871
868 To call an alias without arguments, simply use ipalias('name').
872 To call an alias without arguments, simply use ipalias('name').
869
873
870 This provides a proper Python function to call IPython's aliases in any
874 This provides a proper Python function to call IPython's aliases in any
871 valid Python code you can type at the interpreter, including loops and
875 valid Python code you can type at the interpreter, including loops and
872 compound statements. It is added by IPython to the Python builtin
876 compound statements. It is added by IPython to the Python builtin
873 namespace upon initialization."""
877 namespace upon initialization."""
874
878
875 args = arg_s.split(' ',1)
879 args = arg_s.split(' ',1)
876 alias_name = args[0]
880 alias_name = args[0]
877 try:
881 try:
878 alias_args = args[1]
882 alias_args = args[1]
879 except IndexError:
883 except IndexError:
880 alias_args = ''
884 alias_args = ''
881 if alias_name in self.alias_table:
885 if alias_name in self.alias_table:
882 self.call_alias(alias_name,alias_args)
886 self.call_alias(alias_name,alias_args)
883 else:
887 else:
884 error("Alias `%s` not found." % alias_name)
888 error("Alias `%s` not found." % alias_name)
885
889
886 def ipsystem(self,arg_s):
890 def ipsystem(self,arg_s):
887 """Make a system call, using IPython."""
891 """Make a system call, using IPython."""
888
892
889 self.system(arg_s)
893 self.system(arg_s)
890
894
891 def complete(self,text):
895 def complete(self,text):
892 """Return a sorted list of all possible completions on text.
896 """Return a sorted list of all possible completions on text.
893
897
894 Inputs:
898 Inputs:
895
899
896 - text: a string of text to be completed on.
900 - text: a string of text to be completed on.
897
901
898 This is a wrapper around the completion mechanism, similar to what
902 This is a wrapper around the completion mechanism, similar to what
899 readline does at the command line when the TAB key is hit. By
903 readline does at the command line when the TAB key is hit. By
900 exposing it as a method, it can be used by other non-readline
904 exposing it as a method, it can be used by other non-readline
901 environments (such as GUIs) for text completion.
905 environments (such as GUIs) for text completion.
902
906
903 Simple usage example:
907 Simple usage example:
904
908
905 In [1]: x = 'hello'
909 In [1]: x = 'hello'
906
910
907 In [2]: __IP.complete('x.l')
911 In [2]: __IP.complete('x.l')
908 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
912 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
909
913
910 complete = self.Completer.complete
914 complete = self.Completer.complete
911 state = 0
915 state = 0
912 # use a dict so we get unique keys, since ipyhton's multiple
916 # use a dict so we get unique keys, since ipyhton's multiple
913 # completers can return duplicates.
917 # completers can return duplicates.
914 comps = {}
918 comps = {}
915 while True:
919 while True:
916 newcomp = complete(text,state)
920 newcomp = complete(text,state)
917 if newcomp is None:
921 if newcomp is None:
918 break
922 break
919 comps[newcomp] = 1
923 comps[newcomp] = 1
920 state += 1
924 state += 1
921 outcomps = comps.keys()
925 outcomps = comps.keys()
922 outcomps.sort()
926 outcomps.sort()
923 return outcomps
927 return outcomps
924
928
925 def set_completer_frame(self, frame=None):
929 def set_completer_frame(self, frame=None):
926 if frame:
930 if frame:
927 self.Completer.namespace = frame.f_locals
931 self.Completer.namespace = frame.f_locals
928 self.Completer.global_namespace = frame.f_globals
932 self.Completer.global_namespace = frame.f_globals
929 else:
933 else:
930 self.Completer.namespace = self.user_ns
934 self.Completer.namespace = self.user_ns
931 self.Completer.global_namespace = self.user_global_ns
935 self.Completer.global_namespace = self.user_global_ns
932
936
933 def init_auto_alias(self):
937 def init_auto_alias(self):
934 """Define some aliases automatically.
938 """Define some aliases automatically.
935
939
936 These are ALL parameter-less aliases"""
940 These are ALL parameter-less aliases"""
937
941
938 for alias,cmd in self.auto_alias:
942 for alias,cmd in self.auto_alias:
939 self.alias_table[alias] = (0,cmd)
943 self.alias_table[alias] = (0,cmd)
940
944
941 def alias_table_validate(self,verbose=0):
945 def alias_table_validate(self,verbose=0):
942 """Update information about the alias table.
946 """Update information about the alias table.
943
947
944 In particular, make sure no Python keywords/builtins are in it."""
948 In particular, make sure no Python keywords/builtins are in it."""
945
949
946 no_alias = self.no_alias
950 no_alias = self.no_alias
947 for k in self.alias_table.keys():
951 for k in self.alias_table.keys():
948 if k in no_alias:
952 if k in no_alias:
949 del self.alias_table[k]
953 del self.alias_table[k]
950 if verbose:
954 if verbose:
951 print ("Deleting alias <%s>, it's a Python "
955 print ("Deleting alias <%s>, it's a Python "
952 "keyword or builtin." % k)
956 "keyword or builtin." % k)
953
957
954 def set_autoindent(self,value=None):
958 def set_autoindent(self,value=None):
955 """Set the autoindent flag, checking for readline support.
959 """Set the autoindent flag, checking for readline support.
956
960
957 If called with no arguments, it acts as a toggle."""
961 If called with no arguments, it acts as a toggle."""
958
962
959 if not self.has_readline:
963 if not self.has_readline:
960 if os.name == 'posix':
964 if os.name == 'posix':
961 warn("The auto-indent feature requires the readline library")
965 warn("The auto-indent feature requires the readline library")
962 self.autoindent = 0
966 self.autoindent = 0
963 return
967 return
964 if value is None:
968 if value is None:
965 self.autoindent = not self.autoindent
969 self.autoindent = not self.autoindent
966 else:
970 else:
967 self.autoindent = value
971 self.autoindent = value
968
972
969 def rc_set_toggle(self,rc_field,value=None):
973 def rc_set_toggle(self,rc_field,value=None):
970 """Set or toggle a field in IPython's rc config. structure.
974 """Set or toggle a field in IPython's rc config. structure.
971
975
972 If called with no arguments, it acts as a toggle.
976 If called with no arguments, it acts as a toggle.
973
977
974 If called with a non-existent field, the resulting AttributeError
978 If called with a non-existent field, the resulting AttributeError
975 exception will propagate out."""
979 exception will propagate out."""
976
980
977 rc_val = getattr(self.rc,rc_field)
981 rc_val = getattr(self.rc,rc_field)
978 if value is None:
982 if value is None:
979 value = not rc_val
983 value = not rc_val
980 setattr(self.rc,rc_field,value)
984 setattr(self.rc,rc_field,value)
981
985
982 def user_setup(self,ipythondir,rc_suffix,mode='install'):
986 def user_setup(self,ipythondir,rc_suffix,mode='install'):
983 """Install the user configuration directory.
987 """Install the user configuration directory.
984
988
985 Can be called when running for the first time or to upgrade the user's
989 Can be called when running for the first time or to upgrade the user's
986 .ipython/ directory with the mode parameter. Valid modes are 'install'
990 .ipython/ directory with the mode parameter. Valid modes are 'install'
987 and 'upgrade'."""
991 and 'upgrade'."""
988
992
989 def wait():
993 def wait():
990 try:
994 try:
991 raw_input("Please press <RETURN> to start IPython.")
995 raw_input("Please press <RETURN> to start IPython.")
992 except EOFError:
996 except EOFError:
993 print >> Term.cout
997 print >> Term.cout
994 print '*'*70
998 print '*'*70
995
999
996 cwd = os.getcwd() # remember where we started
1000 cwd = os.getcwd() # remember where we started
997 glb = glob.glob
1001 glb = glob.glob
998 print '*'*70
1002 print '*'*70
999 if mode == 'install':
1003 if mode == 'install':
1000 print \
1004 print \
1001 """Welcome to IPython. I will try to create a personal configuration directory
1005 """Welcome to IPython. I will try to create a personal configuration directory
1002 where you can customize many aspects of IPython's functionality in:\n"""
1006 where you can customize many aspects of IPython's functionality in:\n"""
1003 else:
1007 else:
1004 print 'I am going to upgrade your configuration in:'
1008 print 'I am going to upgrade your configuration in:'
1005
1009
1006 print ipythondir
1010 print ipythondir
1007
1011
1008 rcdirend = os.path.join('IPython','UserConfig')
1012 rcdirend = os.path.join('IPython','UserConfig')
1009 cfg = lambda d: os.path.join(d,rcdirend)
1013 cfg = lambda d: os.path.join(d,rcdirend)
1010 try:
1014 try:
1011 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1015 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1012 except IOError:
1016 except IOError:
1013 warning = """
1017 warning = """
1014 Installation error. IPython's directory was not found.
1018 Installation error. IPython's directory was not found.
1015
1019
1016 Check the following:
1020 Check the following:
1017
1021
1018 The ipython/IPython directory should be in a directory belonging to your
1022 The ipython/IPython directory should be in a directory belonging to your
1019 PYTHONPATH environment variable (that is, it should be in a directory
1023 PYTHONPATH environment variable (that is, it should be in a directory
1020 belonging to sys.path). You can copy it explicitly there or just link to it.
1024 belonging to sys.path). You can copy it explicitly there or just link to it.
1021
1025
1022 IPython will proceed with builtin defaults.
1026 IPython will proceed with builtin defaults.
1023 """
1027 """
1024 warn(warning)
1028 warn(warning)
1025 wait()
1029 wait()
1026 return
1030 return
1027
1031
1028 if mode == 'install':
1032 if mode == 'install':
1029 try:
1033 try:
1030 shutil.copytree(rcdir,ipythondir)
1034 shutil.copytree(rcdir,ipythondir)
1031 os.chdir(ipythondir)
1035 os.chdir(ipythondir)
1032 rc_files = glb("ipythonrc*")
1036 rc_files = glb("ipythonrc*")
1033 for rc_file in rc_files:
1037 for rc_file in rc_files:
1034 os.rename(rc_file,rc_file+rc_suffix)
1038 os.rename(rc_file,rc_file+rc_suffix)
1035 except:
1039 except:
1036 warning = """
1040 warning = """
1037
1041
1038 There was a problem with the installation:
1042 There was a problem with the installation:
1039 %s
1043 %s
1040 Try to correct it or contact the developers if you think it's a bug.
1044 Try to correct it or contact the developers if you think it's a bug.
1041 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1045 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1042 warn(warning)
1046 warn(warning)
1043 wait()
1047 wait()
1044 return
1048 return
1045
1049
1046 elif mode == 'upgrade':
1050 elif mode == 'upgrade':
1047 try:
1051 try:
1048 os.chdir(ipythondir)
1052 os.chdir(ipythondir)
1049 except:
1053 except:
1050 print """
1054 print """
1051 Can not upgrade: changing to directory %s failed. Details:
1055 Can not upgrade: changing to directory %s failed. Details:
1052 %s
1056 %s
1053 """ % (ipythondir,sys.exc_info()[1])
1057 """ % (ipythondir,sys.exc_info()[1])
1054 wait()
1058 wait()
1055 return
1059 return
1056 else:
1060 else:
1057 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1061 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1058 for new_full_path in sources:
1062 for new_full_path in sources:
1059 new_filename = os.path.basename(new_full_path)
1063 new_filename = os.path.basename(new_full_path)
1060 if new_filename.startswith('ipythonrc'):
1064 if new_filename.startswith('ipythonrc'):
1061 new_filename = new_filename + rc_suffix
1065 new_filename = new_filename + rc_suffix
1062 # The config directory should only contain files, skip any
1066 # The config directory should only contain files, skip any
1063 # directories which may be there (like CVS)
1067 # directories which may be there (like CVS)
1064 if os.path.isdir(new_full_path):
1068 if os.path.isdir(new_full_path):
1065 continue
1069 continue
1066 if os.path.exists(new_filename):
1070 if os.path.exists(new_filename):
1067 old_file = new_filename+'.old'
1071 old_file = new_filename+'.old'
1068 if os.path.exists(old_file):
1072 if os.path.exists(old_file):
1069 os.remove(old_file)
1073 os.remove(old_file)
1070 os.rename(new_filename,old_file)
1074 os.rename(new_filename,old_file)
1071 shutil.copy(new_full_path,new_filename)
1075 shutil.copy(new_full_path,new_filename)
1072 else:
1076 else:
1073 raise ValueError,'unrecognized mode for install:',`mode`
1077 raise ValueError,'unrecognized mode for install:',`mode`
1074
1078
1075 # Fix line-endings to those native to each platform in the config
1079 # Fix line-endings to those native to each platform in the config
1076 # directory.
1080 # directory.
1077 try:
1081 try:
1078 os.chdir(ipythondir)
1082 os.chdir(ipythondir)
1079 except:
1083 except:
1080 print """
1084 print """
1081 Problem: changing to directory %s failed.
1085 Problem: changing to directory %s failed.
1082 Details:
1086 Details:
1083 %s
1087 %s
1084
1088
1085 Some configuration files may have incorrect line endings. This should not
1089 Some configuration files may have incorrect line endings. This should not
1086 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1090 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1087 wait()
1091 wait()
1088 else:
1092 else:
1089 for fname in glb('ipythonrc*'):
1093 for fname in glb('ipythonrc*'):
1090 try:
1094 try:
1091 native_line_ends(fname,backup=0)
1095 native_line_ends(fname,backup=0)
1092 except IOError:
1096 except IOError:
1093 pass
1097 pass
1094
1098
1095 if mode == 'install':
1099 if mode == 'install':
1096 print """
1100 print """
1097 Successful installation!
1101 Successful installation!
1098
1102
1099 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1103 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1100 IPython manual (there are both HTML and PDF versions supplied with the
1104 IPython manual (there are both HTML and PDF versions supplied with the
1101 distribution) to make sure that your system environment is properly configured
1105 distribution) to make sure that your system environment is properly configured
1102 to take advantage of IPython's features.
1106 to take advantage of IPython's features.
1103
1107
1104 Important note: the configuration system has changed! The old system is
1108 Important note: the configuration system has changed! The old system is
1105 still in place, but its setting may be partly overridden by the settings in
1109 still in place, but its setting may be partly overridden by the settings in
1106 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1110 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1107 if some of the new settings bother you.
1111 if some of the new settings bother you.
1108
1112
1109 """
1113 """
1110 else:
1114 else:
1111 print """
1115 print """
1112 Successful upgrade!
1116 Successful upgrade!
1113
1117
1114 All files in your directory:
1118 All files in your directory:
1115 %(ipythondir)s
1119 %(ipythondir)s
1116 which would have been overwritten by the upgrade were backed up with a .old
1120 which would have been overwritten by the upgrade were backed up with a .old
1117 extension. If you had made particular customizations in those files you may
1121 extension. If you had made particular customizations in those files you may
1118 want to merge them back into the new files.""" % locals()
1122 want to merge them back into the new files.""" % locals()
1119 wait()
1123 wait()
1120 os.chdir(cwd)
1124 os.chdir(cwd)
1121 # end user_setup()
1125 # end user_setup()
1122
1126
1123 def atexit_operations(self):
1127 def atexit_operations(self):
1124 """This will be executed at the time of exit.
1128 """This will be executed at the time of exit.
1125
1129
1126 Saving of persistent data should be performed here. """
1130 Saving of persistent data should be performed here. """
1127
1131
1128 #print '*** IPython exit cleanup ***' # dbg
1132 #print '*** IPython exit cleanup ***' # dbg
1129 # input history
1133 # input history
1130 self.savehist()
1134 self.savehist()
1131
1135
1132 # Cleanup all tempfiles left around
1136 # Cleanup all tempfiles left around
1133 for tfile in self.tempfiles:
1137 for tfile in self.tempfiles:
1134 try:
1138 try:
1135 os.unlink(tfile)
1139 os.unlink(tfile)
1136 except OSError:
1140 except OSError:
1137 pass
1141 pass
1138
1142
1139 # save the "persistent data" catch-all dictionary
1143 # save the "persistent data" catch-all dictionary
1140 try:
1144 try:
1141 pickle.dump(self.persist, open(self.persist_fname,"w"))
1145 pickle.dump(self.persist, open(self.persist_fname,"w"))
1142 except:
1146 except:
1143 print "*** ERROR *** persistent data saving failed."
1147 print "*** ERROR *** persistent data saving failed."
1144
1148
1145 def savehist(self):
1149 def savehist(self):
1146 """Save input history to a file (via readline library)."""
1150 """Save input history to a file (via readline library)."""
1147 try:
1151 try:
1148 self.readline.write_history_file(self.histfile)
1152 self.readline.write_history_file(self.histfile)
1149 except:
1153 except:
1150 print 'Unable to save IPython command history to file: ' + \
1154 print 'Unable to save IPython command history to file: ' + \
1151 `self.histfile`
1155 `self.histfile`
1152
1156
1153 def pre_readline(self):
1157 def pre_readline(self):
1154 """readline hook to be used at the start of each line.
1158 """readline hook to be used at the start of each line.
1155
1159
1156 Currently it handles auto-indent only."""
1160 Currently it handles auto-indent only."""
1157
1161
1158 #debugp('self.indent_current_nsp','pre_readline:')
1162 #debugx('self.indent_current_nsp','pre_readline:')
1159 self.readline.insert_text(self.indent_current_str())
1163 self.readline.insert_text(self.indent_current_str())
1160
1164
1161 def init_readline(self):
1165 def init_readline(self):
1162 """Command history completion/saving/reloading."""
1166 """Command history completion/saving/reloading."""
1163 try:
1167 try:
1164 import readline
1168 import readline
1165 except ImportError:
1169 except ImportError:
1166 self.has_readline = 0
1170 self.has_readline = 0
1167 self.readline = None
1171 self.readline = None
1168 # no point in bugging windows users with this every time:
1172 # no point in bugging windows users with this every time:
1169 if os.name == 'posix':
1173 if os.name == 'posix':
1170 warn('Readline services not available on this platform.')
1174 warn('Readline services not available on this platform.')
1171 else:
1175 else:
1172 import atexit
1176 import atexit
1173 from IPython.completer import IPCompleter
1177 from IPython.completer import IPCompleter
1174 self.Completer = IPCompleter(self,
1178 self.Completer = IPCompleter(self,
1175 self.user_ns,
1179 self.user_ns,
1176 self.user_global_ns,
1180 self.user_global_ns,
1177 self.rc.readline_omit__names,
1181 self.rc.readline_omit__names,
1178 self.alias_table)
1182 self.alias_table)
1179
1183
1180 # Platform-specific configuration
1184 # Platform-specific configuration
1181 if os.name == 'nt':
1185 if os.name == 'nt':
1182 self.readline_startup_hook = readline.set_pre_input_hook
1186 self.readline_startup_hook = readline.set_pre_input_hook
1183 else:
1187 else:
1184 self.readline_startup_hook = readline.set_startup_hook
1188 self.readline_startup_hook = readline.set_startup_hook
1185
1189
1186 # Load user's initrc file (readline config)
1190 # Load user's initrc file (readline config)
1187 inputrc_name = os.environ.get('INPUTRC')
1191 inputrc_name = os.environ.get('INPUTRC')
1188 if inputrc_name is None:
1192 if inputrc_name is None:
1189 home_dir = get_home_dir()
1193 home_dir = get_home_dir()
1190 if home_dir is not None:
1194 if home_dir is not None:
1191 inputrc_name = os.path.join(home_dir,'.inputrc')
1195 inputrc_name = os.path.join(home_dir,'.inputrc')
1192 if os.path.isfile(inputrc_name):
1196 if os.path.isfile(inputrc_name):
1193 try:
1197 try:
1194 readline.read_init_file(inputrc_name)
1198 readline.read_init_file(inputrc_name)
1195 except:
1199 except:
1196 warn('Problems reading readline initialization file <%s>'
1200 warn('Problems reading readline initialization file <%s>'
1197 % inputrc_name)
1201 % inputrc_name)
1198
1202
1199 self.has_readline = 1
1203 self.has_readline = 1
1200 self.readline = readline
1204 self.readline = readline
1201 # save this in sys so embedded copies can restore it properly
1205 # save this in sys so embedded copies can restore it properly
1202 sys.ipcompleter = self.Completer.complete
1206 sys.ipcompleter = self.Completer.complete
1203 readline.set_completer(self.Completer.complete)
1207 readline.set_completer(self.Completer.complete)
1204
1208
1205 # Configure readline according to user's prefs
1209 # Configure readline according to user's prefs
1206 for rlcommand in self.rc.readline_parse_and_bind:
1210 for rlcommand in self.rc.readline_parse_and_bind:
1207 readline.parse_and_bind(rlcommand)
1211 readline.parse_and_bind(rlcommand)
1208
1212
1209 # remove some chars from the delimiters list
1213 # remove some chars from the delimiters list
1210 delims = readline.get_completer_delims()
1214 delims = readline.get_completer_delims()
1211 delims = delims.translate(string._idmap,
1215 delims = delims.translate(string._idmap,
1212 self.rc.readline_remove_delims)
1216 self.rc.readline_remove_delims)
1213 readline.set_completer_delims(delims)
1217 readline.set_completer_delims(delims)
1214 # otherwise we end up with a monster history after a while:
1218 # otherwise we end up with a monster history after a while:
1215 readline.set_history_length(1000)
1219 readline.set_history_length(1000)
1216 try:
1220 try:
1217 #print '*** Reading readline history' # dbg
1221 #print '*** Reading readline history' # dbg
1218 readline.read_history_file(self.histfile)
1222 readline.read_history_file(self.histfile)
1219 except IOError:
1223 except IOError:
1220 pass # It doesn't exist yet.
1224 pass # It doesn't exist yet.
1221
1225
1222 atexit.register(self.atexit_operations)
1226 atexit.register(self.atexit_operations)
1223 del atexit
1227 del atexit
1224
1228
1225 # Configure auto-indent for all platforms
1229 # Configure auto-indent for all platforms
1226 self.set_autoindent(self.rc.autoindent)
1230 self.set_autoindent(self.rc.autoindent)
1227
1231
1228 def _should_recompile(self,e):
1232 def _should_recompile(self,e):
1229 """Utility routine for edit_syntax_error"""
1233 """Utility routine for edit_syntax_error"""
1230
1234
1231 if e.filename in ('<ipython console>','<input>','<string>',
1235 if e.filename in ('<ipython console>','<input>','<string>',
1232 '<console>',None):
1236 '<console>',None):
1233
1237
1234 return False
1238 return False
1235 try:
1239 try:
1236 if not ask_yes_no('Return to editor to correct syntax error? '
1240 if not ask_yes_no('Return to editor to correct syntax error? '
1237 '[Y/n] ','y'):
1241 '[Y/n] ','y'):
1238 return False
1242 return False
1239 except EOFError:
1243 except EOFError:
1240 return False
1244 return False
1241
1245
1242 def int0(x):
1246 def int0(x):
1243 try:
1247 try:
1244 return int(x)
1248 return int(x)
1245 except TypeError:
1249 except TypeError:
1246 return 0
1250 return 0
1247 # always pass integer line and offset values to editor hook
1251 # always pass integer line and offset values to editor hook
1248 self.hooks.fix_error_editor(e.filename,
1252 self.hooks.fix_error_editor(e.filename,
1249 int0(e.lineno),int0(e.offset),e.msg)
1253 int0(e.lineno),int0(e.offset),e.msg)
1250 return True
1254 return True
1251
1255
1252 def edit_syntax_error(self):
1256 def edit_syntax_error(self):
1253 """The bottom half of the syntax error handler called in the main loop.
1257 """The bottom half of the syntax error handler called in the main loop.
1254
1258
1255 Loop until syntax error is fixed or user cancels.
1259 Loop until syntax error is fixed or user cancels.
1256 """
1260 """
1257
1261
1258 while self.SyntaxTB.last_syntax_error:
1262 while self.SyntaxTB.last_syntax_error:
1259 # copy and clear last_syntax_error
1263 # copy and clear last_syntax_error
1260 err = self.SyntaxTB.clear_err_state()
1264 err = self.SyntaxTB.clear_err_state()
1261 if not self._should_recompile(err):
1265 if not self._should_recompile(err):
1262 return
1266 return
1263 try:
1267 try:
1264 # may set last_syntax_error again if a SyntaxError is raised
1268 # may set last_syntax_error again if a SyntaxError is raised
1265 self.safe_execfile(err.filename,self.shell.user_ns)
1269 self.safe_execfile(err.filename,self.shell.user_ns)
1266 except:
1270 except:
1267 self.showtraceback()
1271 self.showtraceback()
1268 else:
1272 else:
1269 f = file(err.filename)
1273 f = file(err.filename)
1270 try:
1274 try:
1271 sys.displayhook(f.read())
1275 sys.displayhook(f.read())
1272 finally:
1276 finally:
1273 f.close()
1277 f.close()
1274
1278
1275 def showsyntaxerror(self, filename=None):
1279 def showsyntaxerror(self, filename=None):
1276 """Display the syntax error that just occurred.
1280 """Display the syntax error that just occurred.
1277
1281
1278 This doesn't display a stack trace because there isn't one.
1282 This doesn't display a stack trace because there isn't one.
1279
1283
1280 If a filename is given, it is stuffed in the exception instead
1284 If a filename is given, it is stuffed in the exception instead
1281 of what was there before (because Python's parser always uses
1285 of what was there before (because Python's parser always uses
1282 "<string>" when reading from a string).
1286 "<string>" when reading from a string).
1283 """
1287 """
1284 etype, value, last_traceback = sys.exc_info()
1288 etype, value, last_traceback = sys.exc_info()
1285 if filename and etype is SyntaxError:
1289 if filename and etype is SyntaxError:
1286 # Work hard to stuff the correct filename in the exception
1290 # Work hard to stuff the correct filename in the exception
1287 try:
1291 try:
1288 msg, (dummy_filename, lineno, offset, line) = value
1292 msg, (dummy_filename, lineno, offset, line) = value
1289 except:
1293 except:
1290 # Not the format we expect; leave it alone
1294 # Not the format we expect; leave it alone
1291 pass
1295 pass
1292 else:
1296 else:
1293 # Stuff in the right filename
1297 # Stuff in the right filename
1294 try:
1298 try:
1295 # Assume SyntaxError is a class exception
1299 # Assume SyntaxError is a class exception
1296 value = SyntaxError(msg, (filename, lineno, offset, line))
1300 value = SyntaxError(msg, (filename, lineno, offset, line))
1297 except:
1301 except:
1298 # If that failed, assume SyntaxError is a string
1302 # If that failed, assume SyntaxError is a string
1299 value = msg, (filename, lineno, offset, line)
1303 value = msg, (filename, lineno, offset, line)
1300 self.SyntaxTB(etype,value,[])
1304 self.SyntaxTB(etype,value,[])
1301
1305
1302 def debugger(self):
1306 def debugger(self):
1303 """Call the pdb debugger."""
1307 """Call the pdb debugger."""
1304
1308
1305 if not self.rc.pdb:
1309 if not self.rc.pdb:
1306 return
1310 return
1307 pdb.pm()
1311 pdb.pm()
1308
1312
1309 def showtraceback(self,exc_tuple = None,filename=None):
1313 def showtraceback(self,exc_tuple = None,filename=None):
1310 """Display the exception that just occurred."""
1314 """Display the exception that just occurred."""
1311
1315
1312 # Though this won't be called by syntax errors in the input line,
1316 # Though this won't be called by syntax errors in the input line,
1313 # there may be SyntaxError cases whith imported code.
1317 # there may be SyntaxError cases whith imported code.
1314 if exc_tuple is None:
1318 if exc_tuple is None:
1315 type, value, tb = sys.exc_info()
1319 type, value, tb = sys.exc_info()
1316 else:
1320 else:
1317 type, value, tb = exc_tuple
1321 type, value, tb = exc_tuple
1318 if type is SyntaxError:
1322 if type is SyntaxError:
1319 self.showsyntaxerror(filename)
1323 self.showsyntaxerror(filename)
1320 else:
1324 else:
1321 self.InteractiveTB()
1325 self.InteractiveTB()
1322 if self.InteractiveTB.call_pdb and self.has_readline:
1326 if self.InteractiveTB.call_pdb and self.has_readline:
1323 # pdb mucks up readline, fix it back
1327 # pdb mucks up readline, fix it back
1324 self.readline.set_completer(self.Completer.complete)
1328 self.readline.set_completer(self.Completer.complete)
1325
1329
1326 def mainloop(self,banner=None):
1330 def mainloop(self,banner=None):
1327 """Creates the local namespace and starts the mainloop.
1331 """Creates the local namespace and starts the mainloop.
1328
1332
1329 If an optional banner argument is given, it will override the
1333 If an optional banner argument is given, it will override the
1330 internally created default banner."""
1334 internally created default banner."""
1331
1335
1332 if self.rc.c: # Emulate Python's -c option
1336 if self.rc.c: # Emulate Python's -c option
1333 self.exec_init_cmd()
1337 self.exec_init_cmd()
1334 if banner is None:
1338 if banner is None:
1335 if self.rc.banner:
1339 if self.rc.banner:
1336 banner = self.BANNER+self.banner2
1340 banner = self.BANNER+self.banner2
1337 else:
1341 else:
1338 banner = ''
1342 banner = ''
1339 self.interact(banner)
1343 self.interact(banner)
1340
1344
1341 def exec_init_cmd(self):
1345 def exec_init_cmd(self):
1342 """Execute a command given at the command line.
1346 """Execute a command given at the command line.
1343
1347
1344 This emulates Python's -c option."""
1348 This emulates Python's -c option."""
1345
1349
1346 sys.argv = ['-c']
1350 sys.argv = ['-c']
1347 self.push(self.rc.c)
1351 self.push(self.rc.c)
1348
1352
1349 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1353 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1350 """Embeds IPython into a running python program.
1354 """Embeds IPython into a running python program.
1351
1355
1352 Input:
1356 Input:
1353
1357
1354 - header: An optional header message can be specified.
1358 - header: An optional header message can be specified.
1355
1359
1356 - local_ns, global_ns: working namespaces. If given as None, the
1360 - local_ns, global_ns: working namespaces. If given as None, the
1357 IPython-initialized one is updated with __main__.__dict__, so that
1361 IPython-initialized one is updated with __main__.__dict__, so that
1358 program variables become visible but user-specific configuration
1362 program variables become visible but user-specific configuration
1359 remains possible.
1363 remains possible.
1360
1364
1361 - stack_depth: specifies how many levels in the stack to go to
1365 - stack_depth: specifies how many levels in the stack to go to
1362 looking for namespaces (when local_ns and global_ns are None). This
1366 looking for namespaces (when local_ns and global_ns are None). This
1363 allows an intermediate caller to make sure that this function gets
1367 allows an intermediate caller to make sure that this function gets
1364 the namespace from the intended level in the stack. By default (0)
1368 the namespace from the intended level in the stack. By default (0)
1365 it will get its locals and globals from the immediate caller.
1369 it will get its locals and globals from the immediate caller.
1366
1370
1367 Warning: it's possible to use this in a program which is being run by
1371 Warning: it's possible to use this in a program which is being run by
1368 IPython itself (via %run), but some funny things will happen (a few
1372 IPython itself (via %run), but some funny things will happen (a few
1369 globals get overwritten). In the future this will be cleaned up, as
1373 globals get overwritten). In the future this will be cleaned up, as
1370 there is no fundamental reason why it can't work perfectly."""
1374 there is no fundamental reason why it can't work perfectly."""
1371
1375
1372 # Get locals and globals from caller
1376 # Get locals and globals from caller
1373 if local_ns is None or global_ns is None:
1377 if local_ns is None or global_ns is None:
1374 call_frame = sys._getframe(stack_depth).f_back
1378 call_frame = sys._getframe(stack_depth).f_back
1375
1379
1376 if local_ns is None:
1380 if local_ns is None:
1377 local_ns = call_frame.f_locals
1381 local_ns = call_frame.f_locals
1378 if global_ns is None:
1382 if global_ns is None:
1379 global_ns = call_frame.f_globals
1383 global_ns = call_frame.f_globals
1380
1384
1381 # Update namespaces and fire up interpreter
1385 # Update namespaces and fire up interpreter
1382
1386
1383 # The global one is easy, we can just throw it in
1387 # The global one is easy, we can just throw it in
1384 self.user_global_ns = global_ns
1388 self.user_global_ns = global_ns
1385
1389
1386 # but the user/local one is tricky: ipython needs it to store internal
1390 # but the user/local one is tricky: ipython needs it to store internal
1387 # data, but we also need the locals. We'll copy locals in the user
1391 # data, but we also need the locals. We'll copy locals in the user
1388 # one, but will track what got copied so we can delete them at exit.
1392 # one, but will track what got copied so we can delete them at exit.
1389 # This is so that a later embedded call doesn't see locals from a
1393 # This is so that a later embedded call doesn't see locals from a
1390 # previous call (which most likely existed in a separate scope).
1394 # previous call (which most likely existed in a separate scope).
1391 local_varnames = local_ns.keys()
1395 local_varnames = local_ns.keys()
1392 self.user_ns.update(local_ns)
1396 self.user_ns.update(local_ns)
1393
1397
1394 # Patch for global embedding to make sure that things don't overwrite
1398 # Patch for global embedding to make sure that things don't overwrite
1395 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1399 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1396 # FIXME. Test this a bit more carefully (the if.. is new)
1400 # FIXME. Test this a bit more carefully (the if.. is new)
1397 if local_ns is None and global_ns is None:
1401 if local_ns is None and global_ns is None:
1398 self.user_global_ns.update(__main__.__dict__)
1402 self.user_global_ns.update(__main__.__dict__)
1399
1403
1400 # make sure the tab-completer has the correct frame information, so it
1404 # make sure the tab-completer has the correct frame information, so it
1401 # actually completes using the frame's locals/globals
1405 # actually completes using the frame's locals/globals
1402 self.set_completer_frame()
1406 self.set_completer_frame()
1403
1407
1404 # before activating the interactive mode, we need to make sure that
1408 # before activating the interactive mode, we need to make sure that
1405 # all names in the builtin namespace needed by ipython point to
1409 # all names in the builtin namespace needed by ipython point to
1406 # ourselves, and not to other instances.
1410 # ourselves, and not to other instances.
1407 self.add_builtins()
1411 self.add_builtins()
1408
1412
1409 self.interact(header)
1413 self.interact(header)
1410
1414
1411 # now, purge out the user namespace from anything we might have added
1415 # now, purge out the user namespace from anything we might have added
1412 # from the caller's local namespace
1416 # from the caller's local namespace
1413 delvar = self.user_ns.pop
1417 delvar = self.user_ns.pop
1414 for var in local_varnames:
1418 for var in local_varnames:
1415 delvar(var,None)
1419 delvar(var,None)
1416 # and clean builtins we may have overridden
1420 # and clean builtins we may have overridden
1417 self.clean_builtins()
1421 self.clean_builtins()
1418
1422
1419 def interact(self, banner=None):
1423 def interact(self, banner=None):
1420 """Closely emulate the interactive Python console.
1424 """Closely emulate the interactive Python console.
1421
1425
1422 The optional banner argument specify the banner to print
1426 The optional banner argument specify the banner to print
1423 before the first interaction; by default it prints a banner
1427 before the first interaction; by default it prints a banner
1424 similar to the one printed by the real Python interpreter,
1428 similar to the one printed by the real Python interpreter,
1425 followed by the current class name in parentheses (so as not
1429 followed by the current class name in parentheses (so as not
1426 to confuse this with the real interpreter -- since it's so
1430 to confuse this with the real interpreter -- since it's so
1427 close!).
1431 close!).
1428
1432
1429 """
1433 """
1430 cprt = 'Type "copyright", "credits" or "license" for more information.'
1434 cprt = 'Type "copyright", "credits" or "license" for more information.'
1431 if banner is None:
1435 if banner is None:
1432 self.write("Python %s on %s\n%s\n(%s)\n" %
1436 self.write("Python %s on %s\n%s\n(%s)\n" %
1433 (sys.version, sys.platform, cprt,
1437 (sys.version, sys.platform, cprt,
1434 self.__class__.__name__))
1438 self.__class__.__name__))
1435 else:
1439 else:
1436 self.write(banner)
1440 self.write(banner)
1437
1441
1438 more = 0
1442 more = 0
1439
1443
1440 # Mark activity in the builtins
1444 # Mark activity in the builtins
1441 __builtin__.__dict__['__IPYTHON__active'] += 1
1445 __builtin__.__dict__['__IPYTHON__active'] += 1
1442
1446
1443 # exit_now is set by a call to %Exit or %Quit
1447 # exit_now is set by a call to %Exit or %Quit
1444 self.exit_now = False
1448 self.exit_now = False
1445 while not self.exit_now:
1449 while not self.exit_now:
1446
1450
1447 try:
1451 try:
1448 if more:
1452 if more:
1449 prompt = self.outputcache.prompt2
1453 prompt = self.outputcache.prompt2
1450 if self.autoindent:
1454 if self.autoindent:
1451 self.readline_startup_hook(self.pre_readline)
1455 self.readline_startup_hook(self.pre_readline)
1452 else:
1456 else:
1453 prompt = self.outputcache.prompt1
1457 prompt = self.outputcache.prompt1
1454 try:
1458 try:
1455 line = self.raw_input(prompt,more)
1459 line = self.raw_input(prompt,more)
1456 if self.autoindent:
1460 if self.autoindent:
1457 self.readline_startup_hook(None)
1461 self.readline_startup_hook(None)
1458 except EOFError:
1462 except EOFError:
1459 if self.autoindent:
1463 if self.autoindent:
1460 self.readline_startup_hook(None)
1464 self.readline_startup_hook(None)
1461 self.write("\n")
1465 self.write("\n")
1462 self.exit()
1466 self.exit()
1463 except:
1467 except:
1464 # exceptions here are VERY RARE, but they can be triggered
1468 # exceptions here are VERY RARE, but they can be triggered
1465 # asynchronously by signal handlers, for example.
1469 # asynchronously by signal handlers, for example.
1466 self.showtraceback()
1470 self.showtraceback()
1467 else:
1471 else:
1468 more = self.push(line)
1472 more = self.push(line)
1469
1473
1470 if (self.SyntaxTB.last_syntax_error and
1474 if (self.SyntaxTB.last_syntax_error and
1471 self.rc.autoedit_syntax):
1475 self.rc.autoedit_syntax):
1472 self.edit_syntax_error()
1476 self.edit_syntax_error()
1473
1477
1474 except KeyboardInterrupt:
1478 except KeyboardInterrupt:
1475 self.write("\nKeyboardInterrupt\n")
1479 self.write("\nKeyboardInterrupt\n")
1476 self.resetbuffer()
1480 self.resetbuffer()
1477 more = 0
1481 more = 0
1478 # keep cache in sync with the prompt counter:
1482 # keep cache in sync with the prompt counter:
1479 self.outputcache.prompt_count -= 1
1483 self.outputcache.prompt_count -= 1
1480
1484
1481 if self.autoindent:
1485 if self.autoindent:
1482 self.indent_current_nsp = 0
1486 self.indent_current_nsp = 0
1483
1487
1484 except bdb.BdbQuit:
1488 except bdb.BdbQuit:
1485 warn("The Python debugger has exited with a BdbQuit exception.\n"
1489 warn("The Python debugger has exited with a BdbQuit exception.\n"
1486 "Because of how pdb handles the stack, it is impossible\n"
1490 "Because of how pdb handles the stack, it is impossible\n"
1487 "for IPython to properly format this particular exception.\n"
1491 "for IPython to properly format this particular exception.\n"
1488 "IPython will resume normal operation.")
1492 "IPython will resume normal operation.")
1489
1493
1490 # We are off again...
1494 # We are off again...
1491 __builtin__.__dict__['__IPYTHON__active'] -= 1
1495 __builtin__.__dict__['__IPYTHON__active'] -= 1
1492
1496
1493 def excepthook(self, type, value, tb):
1497 def excepthook(self, type, value, tb):
1494 """One more defense for GUI apps that call sys.excepthook.
1498 """One more defense for GUI apps that call sys.excepthook.
1495
1499
1496 GUI frameworks like wxPython trap exceptions and call
1500 GUI frameworks like wxPython trap exceptions and call
1497 sys.excepthook themselves. I guess this is a feature that
1501 sys.excepthook themselves. I guess this is a feature that
1498 enables them to keep running after exceptions that would
1502 enables them to keep running after exceptions that would
1499 otherwise kill their mainloop. This is a bother for IPython
1503 otherwise kill their mainloop. This is a bother for IPython
1500 which excepts to catch all of the program exceptions with a try:
1504 which excepts to catch all of the program exceptions with a try:
1501 except: statement.
1505 except: statement.
1502
1506
1503 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1507 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1504 any app directly invokes sys.excepthook, it will look to the user like
1508 any app directly invokes sys.excepthook, it will look to the user like
1505 IPython crashed. In order to work around this, we can disable the
1509 IPython crashed. In order to work around this, we can disable the
1506 CrashHandler and replace it with this excepthook instead, which prints a
1510 CrashHandler and replace it with this excepthook instead, which prints a
1507 regular traceback using our InteractiveTB. In this fashion, apps which
1511 regular traceback using our InteractiveTB. In this fashion, apps which
1508 call sys.excepthook will generate a regular-looking exception from
1512 call sys.excepthook will generate a regular-looking exception from
1509 IPython, and the CrashHandler will only be triggered by real IPython
1513 IPython, and the CrashHandler will only be triggered by real IPython
1510 crashes.
1514 crashes.
1511
1515
1512 This hook should be used sparingly, only in places which are not likely
1516 This hook should be used sparingly, only in places which are not likely
1513 to be true IPython errors.
1517 to be true IPython errors.
1514 """
1518 """
1515
1519
1516 self.InteractiveTB(type, value, tb, tb_offset=0)
1520 self.InteractiveTB(type, value, tb, tb_offset=0)
1517 if self.InteractiveTB.call_pdb and self.has_readline:
1521 if self.InteractiveTB.call_pdb and self.has_readline:
1518 self.readline.set_completer(self.Completer.complete)
1522 self.readline.set_completer(self.Completer.complete)
1519
1523
1520 def call_alias(self,alias,rest=''):
1524 def call_alias(self,alias,rest=''):
1521 """Call an alias given its name and the rest of the line.
1525 """Call an alias given its name and the rest of the line.
1522
1526
1523 This function MUST be given a proper alias, because it doesn't make
1527 This function MUST be given a proper alias, because it doesn't make
1524 any checks when looking up into the alias table. The caller is
1528 any checks when looking up into the alias table. The caller is
1525 responsible for invoking it only with a valid alias."""
1529 responsible for invoking it only with a valid alias."""
1526
1530
1527 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1531 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1528 nargs,cmd = self.alias_table[alias]
1532 nargs,cmd = self.alias_table[alias]
1529 # Expand the %l special to be the user's input line
1533 # Expand the %l special to be the user's input line
1530 if cmd.find('%l') >= 0:
1534 if cmd.find('%l') >= 0:
1531 cmd = cmd.replace('%l',rest)
1535 cmd = cmd.replace('%l',rest)
1532 rest = ''
1536 rest = ''
1533 if nargs==0:
1537 if nargs==0:
1534 # Simple, argument-less aliases
1538 # Simple, argument-less aliases
1535 cmd = '%s %s' % (cmd,rest)
1539 cmd = '%s %s' % (cmd,rest)
1536 else:
1540 else:
1537 # Handle aliases with positional arguments
1541 # Handle aliases with positional arguments
1538 args = rest.split(None,nargs)
1542 args = rest.split(None,nargs)
1539 if len(args)< nargs:
1543 if len(args)< nargs:
1540 error('Alias <%s> requires %s arguments, %s given.' %
1544 error('Alias <%s> requires %s arguments, %s given.' %
1541 (alias,nargs,len(args)))
1545 (alias,nargs,len(args)))
1542 return
1546 return
1543 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1547 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1544 # Now call the macro, evaluating in the user's namespace
1548 # Now call the macro, evaluating in the user's namespace
1545 try:
1549 try:
1546 self.system(cmd)
1550 self.system(cmd)
1547 except:
1551 except:
1548 self.showtraceback()
1552 self.showtraceback()
1549
1553
1550 def indent_current_str(self):
1554 def indent_current_str(self):
1551 """return the current level of indentation as a string"""
1555 """return the current level of indentation as a string"""
1552 return self.indent_current_nsp * ' '
1556 return self.indent_current_nsp * ' '
1553
1557
1554 def autoindent_update(self,line):
1558 def autoindent_update(self,line):
1555 """Keep track of the indent level."""
1559 """Keep track of the indent level."""
1556
1560
1557 #import traceback; traceback.print_stack() # dbg
1561 #debugx('line')
1558 debugp('line')
1562 #debugx('self.indent_current_nsp')
1559 debugp('self.indent_current_nsp')
1560 if self.autoindent:
1563 if self.autoindent:
1561 if line:
1564 if line:
1562 inisp = num_ini_spaces(line)
1565 inisp = num_ini_spaces(line)
1563 if inisp < self.indent_current_nsp:
1566 if inisp < self.indent_current_nsp:
1564 self.indent_current_nsp = inisp
1567 self.indent_current_nsp = inisp
1565
1568
1566 if line[-1] == ':':
1569 if line[-1] == ':':
1567 self.indent_current_nsp += 4
1570 self.indent_current_nsp += 4
1568 elif dedent_re.match(line):
1571 elif dedent_re.match(line):
1569 self.indent_current_nsp -= 4
1572 self.indent_current_nsp -= 4
1570 else:
1573 else:
1571 self.indent_current_nsp = 0
1574 self.indent_current_nsp = 0
1572
1575
1573 def runlines(self,lines):
1576 def runlines(self,lines):
1574 """Run a string of one or more lines of source.
1577 """Run a string of one or more lines of source.
1575
1578
1576 This method is capable of running a string containing multiple source
1579 This method is capable of running a string containing multiple source
1577 lines, as if they had been entered at the IPython prompt. Since it
1580 lines, as if they had been entered at the IPython prompt. Since it
1578 exposes IPython's processing machinery, the given strings can contain
1581 exposes IPython's processing machinery, the given strings can contain
1579 magic calls (%magic), special shell access (!cmd), etc."""
1582 magic calls (%magic), special shell access (!cmd), etc."""
1580
1583
1581 # We must start with a clean buffer, in case this is run from an
1584 # We must start with a clean buffer, in case this is run from an
1582 # interactive IPython session (via a magic, for example).
1585 # interactive IPython session (via a magic, for example).
1583 self.resetbuffer()
1586 self.resetbuffer()
1584 lines = lines.split('\n')
1587 lines = lines.split('\n')
1585 more = 0
1588 more = 0
1586 for line in lines:
1589 for line in lines:
1587 # skip blank lines so we don't mess up the prompt counter, but do
1590 # skip blank lines so we don't mess up the prompt counter, but do
1588 # NOT skip even a blank line if we are in a code block (more is
1591 # NOT skip even a blank line if we are in a code block (more is
1589 # true)
1592 # true)
1590 if line or more:
1593 if line or more:
1591 more = self.push(self.prefilter(line,more))
1594 more = self.push(self.prefilter(line,more))
1592 # IPython's runsource returns None if there was an error
1595 # IPython's runsource returns None if there was an error
1593 # compiling the code. This allows us to stop processing right
1596 # compiling the code. This allows us to stop processing right
1594 # away, so the user gets the error message at the right place.
1597 # away, so the user gets the error message at the right place.
1595 if more is None:
1598 if more is None:
1596 break
1599 break
1597 # final newline in case the input didn't have it, so that the code
1600 # final newline in case the input didn't have it, so that the code
1598 # actually does get executed
1601 # actually does get executed
1599 if more:
1602 if more:
1600 self.push('\n')
1603 self.push('\n')
1601
1604
1602 def runsource(self, source, filename='<input>', symbol='single'):
1605 def runsource(self, source, filename='<input>', symbol='single'):
1603 """Compile and run some source in the interpreter.
1606 """Compile and run some source in the interpreter.
1604
1607
1605 Arguments are as for compile_command().
1608 Arguments are as for compile_command().
1606
1609
1607 One several things can happen:
1610 One several things can happen:
1608
1611
1609 1) The input is incorrect; compile_command() raised an
1612 1) The input is incorrect; compile_command() raised an
1610 exception (SyntaxError or OverflowError). A syntax traceback
1613 exception (SyntaxError or OverflowError). A syntax traceback
1611 will be printed by calling the showsyntaxerror() method.
1614 will be printed by calling the showsyntaxerror() method.
1612
1615
1613 2) The input is incomplete, and more input is required;
1616 2) The input is incomplete, and more input is required;
1614 compile_command() returned None. Nothing happens.
1617 compile_command() returned None. Nothing happens.
1615
1618
1616 3) The input is complete; compile_command() returned a code
1619 3) The input is complete; compile_command() returned a code
1617 object. The code is executed by calling self.runcode() (which
1620 object. The code is executed by calling self.runcode() (which
1618 also handles run-time exceptions, except for SystemExit).
1621 also handles run-time exceptions, except for SystemExit).
1619
1622
1620 The return value is:
1623 The return value is:
1621
1624
1622 - True in case 2
1625 - True in case 2
1623
1626
1624 - False in the other cases, unless an exception is raised, where
1627 - False in the other cases, unless an exception is raised, where
1625 None is returned instead. This can be used by external callers to
1628 None is returned instead. This can be used by external callers to
1626 know whether to continue feeding input or not.
1629 know whether to continue feeding input or not.
1627
1630
1628 The return value can be used to decide whether to use sys.ps1 or
1631 The return value can be used to decide whether to use sys.ps1 or
1629 sys.ps2 to prompt the next line."""
1632 sys.ps2 to prompt the next line."""
1630
1633
1631 try:
1634 try:
1632 code = self.compile(source,filename,symbol)
1635 code = self.compile(source,filename,symbol)
1633 except (OverflowError, SyntaxError, ValueError):
1636 except (OverflowError, SyntaxError, ValueError):
1634 # Case 1
1637 # Case 1
1635 self.showsyntaxerror(filename)
1638 self.showsyntaxerror(filename)
1636 return None
1639 return None
1637
1640
1638 if code is None:
1641 if code is None:
1639 # Case 2
1642 # Case 2
1640 return True
1643 return True
1641
1644
1642 # Case 3
1645 # Case 3
1643 # We store the code object so that threaded shells and
1646 # We store the code object so that threaded shells and
1644 # custom exception handlers can access all this info if needed.
1647 # custom exception handlers can access all this info if needed.
1645 # The source corresponding to this can be obtained from the
1648 # The source corresponding to this can be obtained from the
1646 # buffer attribute as '\n'.join(self.buffer).
1649 # buffer attribute as '\n'.join(self.buffer).
1647 self.code_to_run = code
1650 self.code_to_run = code
1648 # now actually execute the code object
1651 # now actually execute the code object
1649 if self.runcode(code) == 0:
1652 if self.runcode(code) == 0:
1650 return False
1653 return False
1651 else:
1654 else:
1652 return None
1655 return None
1653
1656
1654 def runcode(self,code_obj):
1657 def runcode(self,code_obj):
1655 """Execute a code object.
1658 """Execute a code object.
1656
1659
1657 When an exception occurs, self.showtraceback() is called to display a
1660 When an exception occurs, self.showtraceback() is called to display a
1658 traceback.
1661 traceback.
1659
1662
1660 Return value: a flag indicating whether the code to be run completed
1663 Return value: a flag indicating whether the code to be run completed
1661 successfully:
1664 successfully:
1662
1665
1663 - 0: successful execution.
1666 - 0: successful execution.
1664 - 1: an error occurred.
1667 - 1: an error occurred.
1665 """
1668 """
1666
1669
1667 # Set our own excepthook in case the user code tries to call it
1670 # Set our own excepthook in case the user code tries to call it
1668 # directly, so that the IPython crash handler doesn't get triggered
1671 # directly, so that the IPython crash handler doesn't get triggered
1669 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1672 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1670
1673
1671 # we save the original sys.excepthook in the instance, in case config
1674 # we save the original sys.excepthook in the instance, in case config
1672 # code (such as magics) needs access to it.
1675 # code (such as magics) needs access to it.
1673 self.sys_excepthook = old_excepthook
1676 self.sys_excepthook = old_excepthook
1674 outflag = 1 # happens in more places, so it's easier as default
1677 outflag = 1 # happens in more places, so it's easier as default
1675 try:
1678 try:
1676 try:
1679 try:
1677 # Embedded instances require separate global/local namespaces
1680 # Embedded instances require separate global/local namespaces
1678 # so they can see both the surrounding (local) namespace and
1681 # so they can see both the surrounding (local) namespace and
1679 # the module-level globals when called inside another function.
1682 # the module-level globals when called inside another function.
1680 if self.embedded:
1683 if self.embedded:
1681 exec code_obj in self.user_global_ns, self.user_ns
1684 exec code_obj in self.user_global_ns, self.user_ns
1682 # Normal (non-embedded) instances should only have a single
1685 # Normal (non-embedded) instances should only have a single
1683 # namespace for user code execution, otherwise functions won't
1686 # namespace for user code execution, otherwise functions won't
1684 # see interactive top-level globals.
1687 # see interactive top-level globals.
1685 else:
1688 else:
1686 exec code_obj in self.user_ns
1689 exec code_obj in self.user_ns
1687 finally:
1690 finally:
1688 # Reset our crash handler in place
1691 # Reset our crash handler in place
1689 sys.excepthook = old_excepthook
1692 sys.excepthook = old_excepthook
1690 except SystemExit:
1693 except SystemExit:
1691 self.resetbuffer()
1694 self.resetbuffer()
1692 self.showtraceback()
1695 self.showtraceback()
1693 warn("Type exit or quit to exit IPython "
1696 warn("Type exit or quit to exit IPython "
1694 "(%Exit or %Quit do so unconditionally).",level=1)
1697 "(%Exit or %Quit do so unconditionally).",level=1)
1695 except self.custom_exceptions:
1698 except self.custom_exceptions:
1696 etype,value,tb = sys.exc_info()
1699 etype,value,tb = sys.exc_info()
1697 self.CustomTB(etype,value,tb)
1700 self.CustomTB(etype,value,tb)
1698 except:
1701 except:
1699 self.showtraceback()
1702 self.showtraceback()
1700 else:
1703 else:
1701 outflag = 0
1704 outflag = 0
1702 if softspace(sys.stdout, 0):
1705 if softspace(sys.stdout, 0):
1703 print
1706 print
1704 # Flush out code object which has been run (and source)
1707 # Flush out code object which has been run (and source)
1705 self.code_to_run = None
1708 self.code_to_run = None
1706 return outflag
1709 return outflag
1707
1710
1708 def push(self, line):
1711 def push(self, line):
1709 """Push a line to the interpreter.
1712 """Push a line to the interpreter.
1710
1713
1711 The line should not have a trailing newline; it may have
1714 The line should not have a trailing newline; it may have
1712 internal newlines. The line is appended to a buffer and the
1715 internal newlines. The line is appended to a buffer and the
1713 interpreter's runsource() method is called with the
1716 interpreter's runsource() method is called with the
1714 concatenated contents of the buffer as source. If this
1717 concatenated contents of the buffer as source. If this
1715 indicates that the command was executed or invalid, the buffer
1718 indicates that the command was executed or invalid, the buffer
1716 is reset; otherwise, the command is incomplete, and the buffer
1719 is reset; otherwise, the command is incomplete, and the buffer
1717 is left as it was after the line was appended. The return
1720 is left as it was after the line was appended. The return
1718 value is 1 if more input is required, 0 if the line was dealt
1721 value is 1 if more input is required, 0 if the line was dealt
1719 with in some way (this is the same as runsource()).
1722 with in some way (this is the same as runsource()).
1720 """
1723 """
1721
1724
1722 # autoindent management should be done here, and not in the
1725 # autoindent management should be done here, and not in the
1723 # interactive loop, since that one is only seen by keyboard input. We
1726 # interactive loop, since that one is only seen by keyboard input. We
1724 # need this done correctly even for code run via runlines (which uses
1727 # need this done correctly even for code run via runlines (which uses
1725 # push).
1728 # push).
1726
1729
1727 #print 'push line: <%s>' % line # dbg
1730 #print 'push line: <%s>' % line # dbg
1728 self.autoindent_update(line)
1731 self.autoindent_update(line)
1729
1732
1730 self.buffer.append(line)
1733 self.buffer.append(line)
1731 more = self.runsource('\n'.join(self.buffer), self.filename)
1734 more = self.runsource('\n'.join(self.buffer), self.filename)
1732 if not more:
1735 if not more:
1733 self.resetbuffer()
1736 self.resetbuffer()
1734 return more
1737 return more
1735
1738
1736 def resetbuffer(self):
1739 def resetbuffer(self):
1737 """Reset the input buffer."""
1740 """Reset the input buffer."""
1738 self.buffer[:] = []
1741 self.buffer[:] = []
1739
1742
1740 def raw_input(self,prompt='',continue_prompt=False):
1743 def raw_input(self,prompt='',continue_prompt=False):
1741 """Write a prompt and read a line.
1744 """Write a prompt and read a line.
1742
1745
1743 The returned line does not include the trailing newline.
1746 The returned line does not include the trailing newline.
1744 When the user enters the EOF key sequence, EOFError is raised.
1747 When the user enters the EOF key sequence, EOFError is raised.
1745
1748
1746 Optional inputs:
1749 Optional inputs:
1747
1750
1748 - prompt(''): a string to be printed to prompt the user.
1751 - prompt(''): a string to be printed to prompt the user.
1749
1752
1750 - continue_prompt(False): whether this line is the first one or a
1753 - continue_prompt(False): whether this line is the first one or a
1751 continuation in a sequence of inputs.
1754 continuation in a sequence of inputs.
1752 """
1755 """
1753
1756
1754 line = raw_input_original(prompt)
1757 line = raw_input_original(prompt)
1755 # Try to be reasonably smart about not re-indenting pasted input more
1758 # Try to be reasonably smart about not re-indenting pasted input more
1756 # than necessary. We do this by trimming out the auto-indent initial
1759 # than necessary. We do this by trimming out the auto-indent initial
1757 # spaces, if the user's actual input started itself with whitespace.
1760 # spaces, if the user's actual input started itself with whitespace.
1758 #debugp('self.buffer[-1]')
1761 #debugx('self.buffer[-1]')
1759
1762
1760 debugp('line')
1761 debugp('self.indent_current_nsp')
1762 if self.autoindent:
1763 if self.autoindent:
1763 if num_ini_spaces(line) > self.indent_current_nsp:
1764 if num_ini_spaces(line) > self.indent_current_nsp:
1764 line = line[self.indent_current_nsp:]
1765 line = line[self.indent_current_nsp:]
1765 self.indent_current_nsp = 0
1766 self.indent_current_nsp = 0
1766 debugp('self.indent_current_nsp')
1767
1767
1768 debugp('line')
1768 # store the unfiltered input before the user has any chance to modify
1769 # it.
1770 if line.strip():
1771 if continue_prompt:
1772 self.input_hist_raw[-1] += '%s\n' % line
1773 else:
1774 self.input_hist_raw.append('%s\n' % line)
1775
1769 lineout = self.prefilter(line,continue_prompt)
1776 lineout = self.prefilter(line,continue_prompt)
1770 debugp('lineout')
1771 return lineout
1777 return lineout
1772
1778
1773 def split_user_input(self,line):
1779 def split_user_input(self,line):
1774 """Split user input into pre-char, function part and rest."""
1780 """Split user input into pre-char, function part and rest."""
1775
1781
1776 lsplit = self.line_split.match(line)
1782 lsplit = self.line_split.match(line)
1777 if lsplit is None: # no regexp match returns None
1783 if lsplit is None: # no regexp match returns None
1778 try:
1784 try:
1779 iFun,theRest = line.split(None,1)
1785 iFun,theRest = line.split(None,1)
1780 except ValueError:
1786 except ValueError:
1781 iFun,theRest = line,''
1787 iFun,theRest = line,''
1782 pre = re.match('^(\s*)(.*)',line).groups()[0]
1788 pre = re.match('^(\s*)(.*)',line).groups()[0]
1783 else:
1789 else:
1784 pre,iFun,theRest = lsplit.groups()
1790 pre,iFun,theRest = lsplit.groups()
1785
1791
1786 #print 'line:<%s>' % line # dbg
1792 #print 'line:<%s>' % line # dbg
1787 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1793 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1788 return pre,iFun.strip(),theRest
1794 return pre,iFun.strip(),theRest
1789
1795
1790 def _prefilter(self, line, continue_prompt):
1796 def _prefilter(self, line, continue_prompt):
1791 """Calls different preprocessors, depending on the form of line."""
1797 """Calls different preprocessors, depending on the form of line."""
1792
1798
1793 # All handlers *must* return a value, even if it's blank ('').
1799 # All handlers *must* return a value, even if it's blank ('').
1794
1800
1795 # Lines are NOT logged here. Handlers should process the line as
1801 # Lines are NOT logged here. Handlers should process the line as
1796 # needed, update the cache AND log it (so that the input cache array
1802 # needed, update the cache AND log it (so that the input cache array
1797 # stays synced).
1803 # stays synced).
1798
1804
1799 # This function is _very_ delicate, and since it's also the one which
1805 # This function is _very_ delicate, and since it's also the one which
1800 # determines IPython's response to user input, it must be as efficient
1806 # determines IPython's response to user input, it must be as efficient
1801 # as possible. For this reason it has _many_ returns in it, trying
1807 # as possible. For this reason it has _many_ returns in it, trying
1802 # always to exit as quickly as it can figure out what it needs to do.
1808 # always to exit as quickly as it can figure out what it needs to do.
1803
1809
1804 # This function is the main responsible for maintaining IPython's
1810 # This function is the main responsible for maintaining IPython's
1805 # behavior respectful of Python's semantics. So be _very_ careful if
1811 # behavior respectful of Python's semantics. So be _very_ careful if
1806 # making changes to anything here.
1812 # making changes to anything here.
1807
1813
1808 #.....................................................................
1814 #.....................................................................
1809 # Code begins
1815 # Code begins
1810
1816
1811 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1817 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1812
1818
1813 # save the line away in case we crash, so the post-mortem handler can
1819 # save the line away in case we crash, so the post-mortem handler can
1814 # record it
1820 # record it
1815 self._last_input_line = line
1821 self._last_input_line = line
1816
1822
1817 #print '***line: <%s>' % line # dbg
1823 #print '***line: <%s>' % line # dbg
1818
1824
1819 # the input history needs to track even empty lines
1825 # the input history needs to track even empty lines
1820 if not line.strip():
1826 if not line.strip():
1821 if not continue_prompt:
1827 if not continue_prompt:
1822 self.outputcache.prompt_count -= 1
1828 self.outputcache.prompt_count -= 1
1823 return self.handle_normal(line,continue_prompt)
1829 return self.handle_normal(line,continue_prompt)
1824 #return self.handle_normal('',continue_prompt)
1830 #return self.handle_normal('',continue_prompt)
1825
1831
1826 # print '***cont',continue_prompt # dbg
1832 # print '***cont',continue_prompt # dbg
1827 # special handlers are only allowed for single line statements
1833 # special handlers are only allowed for single line statements
1828 if continue_prompt and not self.rc.multi_line_specials:
1834 if continue_prompt and not self.rc.multi_line_specials:
1829 return self.handle_normal(line,continue_prompt)
1835 return self.handle_normal(line,continue_prompt)
1830
1836
1831 # For the rest, we need the structure of the input
1837 # For the rest, we need the structure of the input
1832 pre,iFun,theRest = self.split_user_input(line)
1838 pre,iFun,theRest = self.split_user_input(line)
1833 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1839 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1834
1840
1835 # First check for explicit escapes in the last/first character
1841 # First check for explicit escapes in the last/first character
1836 handler = None
1842 handler = None
1837 if line[-1] == self.ESC_HELP:
1843 if line[-1] == self.ESC_HELP:
1838 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1844 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1839 if handler is None:
1845 if handler is None:
1840 # look at the first character of iFun, NOT of line, so we skip
1846 # look at the first character of iFun, NOT of line, so we skip
1841 # leading whitespace in multiline input
1847 # leading whitespace in multiline input
1842 handler = self.esc_handlers.get(iFun[0:1])
1848 handler = self.esc_handlers.get(iFun[0:1])
1843 if handler is not None:
1849 if handler is not None:
1844 return handler(line,continue_prompt,pre,iFun,theRest)
1850 return handler(line,continue_prompt,pre,iFun,theRest)
1845 # Emacs ipython-mode tags certain input lines
1851 # Emacs ipython-mode tags certain input lines
1846 if line.endswith('# PYTHON-MODE'):
1852 if line.endswith('# PYTHON-MODE'):
1847 return self.handle_emacs(line,continue_prompt)
1853 return self.handle_emacs(line,continue_prompt)
1848
1854
1849 # Next, check if we can automatically execute this thing
1855 # Next, check if we can automatically execute this thing
1850
1856
1851 # Allow ! in multi-line statements if multi_line_specials is on:
1857 # Allow ! in multi-line statements if multi_line_specials is on:
1852 if continue_prompt and self.rc.multi_line_specials and \
1858 if continue_prompt and self.rc.multi_line_specials and \
1853 iFun.startswith(self.ESC_SHELL):
1859 iFun.startswith(self.ESC_SHELL):
1854 return self.handle_shell_escape(line,continue_prompt,
1860 return self.handle_shell_escape(line,continue_prompt,
1855 pre=pre,iFun=iFun,
1861 pre=pre,iFun=iFun,
1856 theRest=theRest)
1862 theRest=theRest)
1857
1863
1858 # Let's try to find if the input line is a magic fn
1864 # Let's try to find if the input line is a magic fn
1859 oinfo = None
1865 oinfo = None
1860 if hasattr(self,'magic_'+iFun):
1866 if hasattr(self,'magic_'+iFun):
1861 # WARNING: _ofind uses getattr(), so it can consume generators and
1867 # WARNING: _ofind uses getattr(), so it can consume generators and
1862 # cause other side effects.
1868 # cause other side effects.
1863 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1869 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1864 if oinfo['ismagic']:
1870 if oinfo['ismagic']:
1865 # Be careful not to call magics when a variable assignment is
1871 # Be careful not to call magics when a variable assignment is
1866 # being made (ls='hi', for example)
1872 # being made (ls='hi', for example)
1867 if self.rc.automagic and \
1873 if self.rc.automagic and \
1868 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1874 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1869 (self.rc.multi_line_specials or not continue_prompt):
1875 (self.rc.multi_line_specials or not continue_prompt):
1870 return self.handle_magic(line,continue_prompt,
1876 return self.handle_magic(line,continue_prompt,
1871 pre,iFun,theRest)
1877 pre,iFun,theRest)
1872 else:
1878 else:
1873 return self.handle_normal(line,continue_prompt)
1879 return self.handle_normal(line,continue_prompt)
1874
1880
1875 # If the rest of the line begins with an (in)equality, assginment or
1881 # If the rest of the line begins with an (in)equality, assginment or
1876 # function call, we should not call _ofind but simply execute it.
1882 # function call, we should not call _ofind but simply execute it.
1877 # This avoids spurious geattr() accesses on objects upon assignment.
1883 # This avoids spurious geattr() accesses on objects upon assignment.
1878 #
1884 #
1879 # It also allows users to assign to either alias or magic names true
1885 # It also allows users to assign to either alias or magic names true
1880 # python variables (the magic/alias systems always take second seat to
1886 # python variables (the magic/alias systems always take second seat to
1881 # true python code).
1887 # true python code).
1882 if theRest and theRest[0] in '!=()':
1888 if theRest and theRest[0] in '!=()':
1883 return self.handle_normal(line,continue_prompt)
1889 return self.handle_normal(line,continue_prompt)
1884
1890
1885 if oinfo is None:
1891 if oinfo is None:
1886 # let's try to ensure that _oinfo is ONLY called when autocall is
1892 # let's try to ensure that _oinfo is ONLY called when autocall is
1887 # on. Since it has inevitable potential side effects, at least
1893 # on. Since it has inevitable potential side effects, at least
1888 # having autocall off should be a guarantee to the user that no
1894 # having autocall off should be a guarantee to the user that no
1889 # weird things will happen.
1895 # weird things will happen.
1890
1896
1891 if self.rc.autocall:
1897 if self.rc.autocall:
1892 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1898 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1893 else:
1899 else:
1894 # in this case, all that's left is either an alias or
1900 # in this case, all that's left is either an alias or
1895 # processing the line normally.
1901 # processing the line normally.
1896 if iFun in self.alias_table:
1902 if iFun in self.alias_table:
1897 return self.handle_alias(line,continue_prompt,
1903 return self.handle_alias(line,continue_prompt,
1898 pre,iFun,theRest)
1904 pre,iFun,theRest)
1899
1905
1900 else:
1906 else:
1901 return self.handle_normal(line,continue_prompt)
1907 return self.handle_normal(line,continue_prompt)
1902
1908
1903 if not oinfo['found']:
1909 if not oinfo['found']:
1904 return self.handle_normal(line,continue_prompt)
1910 return self.handle_normal(line,continue_prompt)
1905 else:
1911 else:
1906 #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1912 #print 'pre<%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1907 if oinfo['isalias']:
1913 if oinfo['isalias']:
1908 return self.handle_alias(line,continue_prompt,
1914 return self.handle_alias(line,continue_prompt,
1909 pre,iFun,theRest)
1915 pre,iFun,theRest)
1910
1916
1911 if (self.rc.autocall
1917 if (self.rc.autocall
1912 and
1918 and
1913 (
1919 (
1914 #only consider exclusion re if not "," or ";" autoquoting
1920 #only consider exclusion re if not "," or ";" autoquoting
1915 (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2) or
1921 (pre == self.ESC_QUOTE or pre == self.ESC_QUOTE2) or
1916 (not self.re_exclude_auto.match(theRest)))
1922 (not self.re_exclude_auto.match(theRest)))
1917 and
1923 and
1918 self.re_fun_name.match(iFun) and
1924 self.re_fun_name.match(iFun) and
1919 callable(oinfo['obj'])) :
1925 callable(oinfo['obj'])) :
1920 #print 'going auto' # dbg
1926 #print 'going auto' # dbg
1921 return self.handle_auto(line,continue_prompt,
1927 return self.handle_auto(line,continue_prompt,
1922 pre,iFun,theRest,oinfo['obj'])
1928 pre,iFun,theRest,oinfo['obj'])
1923 else:
1929 else:
1924 #print 'was callable?', callable(oinfo['obj']) # dbg
1930 #print 'was callable?', callable(oinfo['obj']) # dbg
1925 return self.handle_normal(line,continue_prompt)
1931 return self.handle_normal(line,continue_prompt)
1926
1932
1927 # If we get here, we have a normal Python line. Log and return.
1933 # If we get here, we have a normal Python line. Log and return.
1928 return self.handle_normal(line,continue_prompt)
1934 return self.handle_normal(line,continue_prompt)
1929
1935
1930 def _prefilter_dumb(self, line, continue_prompt):
1936 def _prefilter_dumb(self, line, continue_prompt):
1931 """simple prefilter function, for debugging"""
1937 """simple prefilter function, for debugging"""
1932 return self.handle_normal(line,continue_prompt)
1938 return self.handle_normal(line,continue_prompt)
1933
1939
1934 # Set the default prefilter() function (this can be user-overridden)
1940 # Set the default prefilter() function (this can be user-overridden)
1935 prefilter = _prefilter
1941 prefilter = _prefilter
1936
1942
1937 def handle_normal(self,line,continue_prompt=None,
1943 def handle_normal(self,line,continue_prompt=None,
1938 pre=None,iFun=None,theRest=None):
1944 pre=None,iFun=None,theRest=None):
1939 """Handle normal input lines. Use as a template for handlers."""
1945 """Handle normal input lines. Use as a template for handlers."""
1940
1946
1941 # With autoindent on, we need some way to exit the input loop, and I
1947 # With autoindent on, we need some way to exit the input loop, and I
1942 # don't want to force the user to have to backspace all the way to
1948 # don't want to force the user to have to backspace all the way to
1943 # clear the line. The rule will be in this case, that either two
1949 # clear the line. The rule will be in this case, that either two
1944 # lines of pure whitespace in a row, or a line of pure whitespace but
1950 # lines of pure whitespace in a row, or a line of pure whitespace but
1945 # of a size different to the indent level, will exit the input loop.
1951 # of a size different to the indent level, will exit the input loop.
1946
1952
1947 if (continue_prompt and self.autoindent and line.isspace() and
1953 if (continue_prompt and self.autoindent and line.isspace() and
1948 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
1954 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
1949 (self.buffer[-1]).isspace() )):
1955 (self.buffer[-1]).isspace() )):
1950 #print 'reset line' # dbg
1951 line = ''
1956 line = ''
1952
1957
1953 self.log(line,continue_prompt)
1958 self.log(line,continue_prompt)
1954 return line
1959 return line
1955
1960
1956 def handle_alias(self,line,continue_prompt=None,
1961 def handle_alias(self,line,continue_prompt=None,
1957 pre=None,iFun=None,theRest=None):
1962 pre=None,iFun=None,theRest=None):
1958 """Handle alias input lines. """
1963 """Handle alias input lines. """
1959
1964
1960 # pre is needed, because it carries the leading whitespace. Otherwise
1965 # pre is needed, because it carries the leading whitespace. Otherwise
1961 # aliases won't work in indented sections.
1966 # aliases won't work in indented sections.
1962 line_out = '%sipalias(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1967 line_out = '%sipalias(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1963 self.log(line_out,continue_prompt)
1968 self.log(line_out,continue_prompt)
1964 return line_out
1969 return line_out
1965
1970
1966 def handle_shell_escape(self, line, continue_prompt=None,
1971 def handle_shell_escape(self, line, continue_prompt=None,
1967 pre=None,iFun=None,theRest=None):
1972 pre=None,iFun=None,theRest=None):
1968 """Execute the line in a shell, empty return value"""
1973 """Execute the line in a shell, empty return value"""
1969
1974
1970 #print 'line in :', `line` # dbg
1975 #print 'line in :', `line` # dbg
1971 # Example of a special handler. Others follow a similar pattern.
1976 # Example of a special handler. Others follow a similar pattern.
1972 if line.lstrip().startswith('!!'):
1977 if line.lstrip().startswith('!!'):
1973 # rewrite iFun/theRest to properly hold the call to %sx and
1978 # rewrite iFun/theRest to properly hold the call to %sx and
1974 # the actual command to be executed, so handle_magic can work
1979 # the actual command to be executed, so handle_magic can work
1975 # correctly
1980 # correctly
1976 theRest = '%s %s' % (iFun[2:],theRest)
1981 theRest = '%s %s' % (iFun[2:],theRest)
1977 iFun = 'sx'
1982 iFun = 'sx'
1978 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,
1983 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,
1979 line.lstrip()[2:]),
1984 line.lstrip()[2:]),
1980 continue_prompt,pre,iFun,theRest)
1985 continue_prompt,pre,iFun,theRest)
1981 else:
1986 else:
1982 cmd=line.lstrip().lstrip('!')
1987 cmd=line.lstrip().lstrip('!')
1983 line_out = '%sipsystem(%s)' % (pre,make_quoted_expr(cmd))
1988 line_out = '%sipsystem(%s)' % (pre,make_quoted_expr(cmd))
1984 # update cache/log and return
1989 # update cache/log and return
1985 self.log(line_out,continue_prompt)
1990 self.log(line_out,continue_prompt)
1986 return line_out
1991 return line_out
1987
1992
1988 def handle_magic(self, line, continue_prompt=None,
1993 def handle_magic(self, line, continue_prompt=None,
1989 pre=None,iFun=None,theRest=None):
1994 pre=None,iFun=None,theRest=None):
1990 """Execute magic functions."""
1995 """Execute magic functions."""
1991
1996
1992
1997
1993 cmd = '%sipmagic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1998 cmd = '%sipmagic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
1994 self.log(cmd,continue_prompt)
1999 self.log(cmd,continue_prompt)
1995 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2000 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1996 return cmd
2001 return cmd
1997
2002
1998 def handle_auto(self, line, continue_prompt=None,
2003 def handle_auto(self, line, continue_prompt=None,
1999 pre=None,iFun=None,theRest=None,obj=None):
2004 pre=None,iFun=None,theRest=None,obj=None):
2000 """Hande lines which can be auto-executed, quoting if requested."""
2005 """Hande lines which can be auto-executed, quoting if requested."""
2001
2006
2002 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2007 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2003
2008
2004 # This should only be active for single-line input!
2009 # This should only be active for single-line input!
2005 if continue_prompt:
2010 if continue_prompt:
2006 self.log(line,continue_prompt)
2011 self.log(line,continue_prompt)
2007 return line
2012 return line
2008
2013
2009 auto_rewrite = True
2014 auto_rewrite = True
2010 if pre == self.ESC_QUOTE:
2015 if pre == self.ESC_QUOTE:
2011 # Auto-quote splitting on whitespace
2016 # Auto-quote splitting on whitespace
2012 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2017 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2013 elif pre == self.ESC_QUOTE2:
2018 elif pre == self.ESC_QUOTE2:
2014 # Auto-quote whole string
2019 # Auto-quote whole string
2015 newcmd = '%s("%s")' % (iFun,theRest)
2020 newcmd = '%s("%s")' % (iFun,theRest)
2016 else:
2021 else:
2017 # Auto-paren.
2022 # Auto-paren.
2018 # We only apply it to argument-less calls if the autocall
2023 # We only apply it to argument-less calls if the autocall
2019 # parameter is set to 2. We only need to check that autocall is <
2024 # parameter is set to 2. We only need to check that autocall is <
2020 # 2, since this function isn't called unless it's at least 1.
2025 # 2, since this function isn't called unless it's at least 1.
2021 if not theRest and (self.rc.autocall < 2):
2026 if not theRest and (self.rc.autocall < 2):
2022 newcmd = '%s %s' % (iFun,theRest)
2027 newcmd = '%s %s' % (iFun,theRest)
2023 auto_rewrite = False
2028 auto_rewrite = False
2024 else:
2029 else:
2025 if theRest.startswith('['):
2030 if theRest.startswith('['):
2026 if hasattr(obj,'__getitem__'):
2031 if hasattr(obj,'__getitem__'):
2027 # Don't autocall in this case: item access for an object
2032 # Don't autocall in this case: item access for an object
2028 # which is BOTH callable and implements __getitem__.
2033 # which is BOTH callable and implements __getitem__.
2029 newcmd = '%s %s' % (iFun,theRest)
2034 newcmd = '%s %s' % (iFun,theRest)
2030 auto_rewrite = False
2035 auto_rewrite = False
2031 else:
2036 else:
2032 # if the object doesn't support [] access, go ahead and
2037 # if the object doesn't support [] access, go ahead and
2033 # autocall
2038 # autocall
2034 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2039 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2035 elif theRest.endswith(';'):
2040 elif theRest.endswith(';'):
2036 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2041 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2037 else:
2042 else:
2038 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2043 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2039
2044
2040 if auto_rewrite:
2045 if auto_rewrite:
2041 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
2046 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
2042 # log what is now valid Python, not the actual user input (without the
2047 # log what is now valid Python, not the actual user input (without the
2043 # final newline)
2048 # final newline)
2044 self.log(newcmd,continue_prompt)
2049 self.log(newcmd,continue_prompt)
2045 return newcmd
2050 return newcmd
2046
2051
2047 def handle_help(self, line, continue_prompt=None,
2052 def handle_help(self, line, continue_prompt=None,
2048 pre=None,iFun=None,theRest=None):
2053 pre=None,iFun=None,theRest=None):
2049 """Try to get some help for the object.
2054 """Try to get some help for the object.
2050
2055
2051 obj? or ?obj -> basic information.
2056 obj? or ?obj -> basic information.
2052 obj?? or ??obj -> more details.
2057 obj?? or ??obj -> more details.
2053 """
2058 """
2054
2059
2055 # We need to make sure that we don't process lines which would be
2060 # We need to make sure that we don't process lines which would be
2056 # otherwise valid python, such as "x=1 # what?"
2061 # otherwise valid python, such as "x=1 # what?"
2057 try:
2062 try:
2058 codeop.compile_command(line)
2063 codeop.compile_command(line)
2059 except SyntaxError:
2064 except SyntaxError:
2060 # We should only handle as help stuff which is NOT valid syntax
2065 # We should only handle as help stuff which is NOT valid syntax
2061 if line[0]==self.ESC_HELP:
2066 if line[0]==self.ESC_HELP:
2062 line = line[1:]
2067 line = line[1:]
2063 elif line[-1]==self.ESC_HELP:
2068 elif line[-1]==self.ESC_HELP:
2064 line = line[:-1]
2069 line = line[:-1]
2065 self.log('#?'+line)
2070 self.log('#?'+line)
2066 if line:
2071 if line:
2067 self.magic_pinfo(line)
2072 self.magic_pinfo(line)
2068 else:
2073 else:
2069 page(self.usage,screen_lines=self.rc.screen_length)
2074 page(self.usage,screen_lines=self.rc.screen_length)
2070 return '' # Empty string is needed here!
2075 return '' # Empty string is needed here!
2071 except:
2076 except:
2072 # Pass any other exceptions through to the normal handler
2077 # Pass any other exceptions through to the normal handler
2073 return self.handle_normal(line,continue_prompt)
2078 return self.handle_normal(line,continue_prompt)
2074 else:
2079 else:
2075 # If the code compiles ok, we should handle it normally
2080 # If the code compiles ok, we should handle it normally
2076 return self.handle_normal(line,continue_prompt)
2081 return self.handle_normal(line,continue_prompt)
2077
2082
2078 def handle_emacs(self,line,continue_prompt=None,
2083 def handle_emacs(self,line,continue_prompt=None,
2079 pre=None,iFun=None,theRest=None):
2084 pre=None,iFun=None,theRest=None):
2080 """Handle input lines marked by python-mode."""
2085 """Handle input lines marked by python-mode."""
2081
2086
2082 # Currently, nothing is done. Later more functionality can be added
2087 # Currently, nothing is done. Later more functionality can be added
2083 # here if needed.
2088 # here if needed.
2084
2089
2085 # The input cache shouldn't be updated
2090 # The input cache shouldn't be updated
2086
2091
2087 return line
2092 return line
2088
2093
2089 def mktempfile(self,data=None):
2094 def mktempfile(self,data=None):
2090 """Make a new tempfile and return its filename.
2095 """Make a new tempfile and return its filename.
2091
2096
2092 This makes a call to tempfile.mktemp, but it registers the created
2097 This makes a call to tempfile.mktemp, but it registers the created
2093 filename internally so ipython cleans it up at exit time.
2098 filename internally so ipython cleans it up at exit time.
2094
2099
2095 Optional inputs:
2100 Optional inputs:
2096
2101
2097 - data(None): if data is given, it gets written out to the temp file
2102 - data(None): if data is given, it gets written out to the temp file
2098 immediately, and the file is closed again."""
2103 immediately, and the file is closed again."""
2099
2104
2100 filename = tempfile.mktemp('.py','ipython_edit_')
2105 filename = tempfile.mktemp('.py','ipython_edit_')
2101 self.tempfiles.append(filename)
2106 self.tempfiles.append(filename)
2102
2107
2103 if data:
2108 if data:
2104 tmp_file = open(filename,'w')
2109 tmp_file = open(filename,'w')
2105 tmp_file.write(data)
2110 tmp_file.write(data)
2106 tmp_file.close()
2111 tmp_file.close()
2107 return filename
2112 return filename
2108
2113
2109 def write(self,data):
2114 def write(self,data):
2110 """Write a string to the default output"""
2115 """Write a string to the default output"""
2111 Term.cout.write(data)
2116 Term.cout.write(data)
2112
2117
2113 def write_err(self,data):
2118 def write_err(self,data):
2114 """Write a string to the default error output"""
2119 """Write a string to the default error output"""
2115 Term.cerr.write(data)
2120 Term.cerr.write(data)
2116
2121
2117 def exit(self):
2122 def exit(self):
2118 """Handle interactive exit.
2123 """Handle interactive exit.
2119
2124
2120 This method sets the exit_now attribute."""
2125 This method sets the exit_now attribute."""
2121
2126
2122 if self.rc.confirm_exit:
2127 if self.rc.confirm_exit:
2123 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2128 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2124 self.exit_now = True
2129 self.exit_now = True
2125 else:
2130 else:
2126 self.exit_now = True
2131 self.exit_now = True
2127 return self.exit_now
2132 return self.exit_now
2128
2133
2129 def safe_execfile(self,fname,*where,**kw):
2134 def safe_execfile(self,fname,*where,**kw):
2130 fname = os.path.expanduser(fname)
2135 fname = os.path.expanduser(fname)
2131
2136
2132 # find things also in current directory
2137 # find things also in current directory
2133 dname = os.path.dirname(fname)
2138 dname = os.path.dirname(fname)
2134 if not sys.path.count(dname):
2139 if not sys.path.count(dname):
2135 sys.path.append(dname)
2140 sys.path.append(dname)
2136
2141
2137 try:
2142 try:
2138 xfile = open(fname)
2143 xfile = open(fname)
2139 except:
2144 except:
2140 print >> Term.cerr, \
2145 print >> Term.cerr, \
2141 'Could not open file <%s> for safe execution.' % fname
2146 'Could not open file <%s> for safe execution.' % fname
2142 return None
2147 return None
2143
2148
2144 kw.setdefault('islog',0)
2149 kw.setdefault('islog',0)
2145 kw.setdefault('quiet',1)
2150 kw.setdefault('quiet',1)
2146 kw.setdefault('exit_ignore',0)
2151 kw.setdefault('exit_ignore',0)
2147 first = xfile.readline()
2152 first = xfile.readline()
2148 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2153 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2149 xfile.close()
2154 xfile.close()
2150 # line by line execution
2155 # line by line execution
2151 if first.startswith(loghead) or kw['islog']:
2156 if first.startswith(loghead) or kw['islog']:
2152 print 'Loading log file <%s> one line at a time...' % fname
2157 print 'Loading log file <%s> one line at a time...' % fname
2153 if kw['quiet']:
2158 if kw['quiet']:
2154 stdout_save = sys.stdout
2159 stdout_save = sys.stdout
2155 sys.stdout = StringIO.StringIO()
2160 sys.stdout = StringIO.StringIO()
2156 try:
2161 try:
2157 globs,locs = where[0:2]
2162 globs,locs = where[0:2]
2158 except:
2163 except:
2159 try:
2164 try:
2160 globs = locs = where[0]
2165 globs = locs = where[0]
2161 except:
2166 except:
2162 globs = locs = globals()
2167 globs = locs = globals()
2163 badblocks = []
2168 badblocks = []
2164
2169
2165 # we also need to identify indented blocks of code when replaying
2170 # we also need to identify indented blocks of code when replaying
2166 # logs and put them together before passing them to an exec
2171 # logs and put them together before passing them to an exec
2167 # statement. This takes a bit of regexp and look-ahead work in the
2172 # statement. This takes a bit of regexp and look-ahead work in the
2168 # file. It's easiest if we swallow the whole thing in memory
2173 # file. It's easiest if we swallow the whole thing in memory
2169 # first, and manually walk through the lines list moving the
2174 # first, and manually walk through the lines list moving the
2170 # counter ourselves.
2175 # counter ourselves.
2171 indent_re = re.compile('\s+\S')
2176 indent_re = re.compile('\s+\S')
2172 xfile = open(fname)
2177 xfile = open(fname)
2173 filelines = xfile.readlines()
2178 filelines = xfile.readlines()
2174 xfile.close()
2179 xfile.close()
2175 nlines = len(filelines)
2180 nlines = len(filelines)
2176 lnum = 0
2181 lnum = 0
2177 while lnum < nlines:
2182 while lnum < nlines:
2178 line = filelines[lnum]
2183 line = filelines[lnum]
2179 lnum += 1
2184 lnum += 1
2180 # don't re-insert logger status info into cache
2185 # don't re-insert logger status info into cache
2181 if line.startswith('#log#'):
2186 if line.startswith('#log#'):
2182 continue
2187 continue
2183 else:
2188 else:
2184 # build a block of code (maybe a single line) for execution
2189 # build a block of code (maybe a single line) for execution
2185 block = line
2190 block = line
2186 try:
2191 try:
2187 next = filelines[lnum] # lnum has already incremented
2192 next = filelines[lnum] # lnum has already incremented
2188 except:
2193 except:
2189 next = None
2194 next = None
2190 while next and indent_re.match(next):
2195 while next and indent_re.match(next):
2191 block += next
2196 block += next
2192 lnum += 1
2197 lnum += 1
2193 try:
2198 try:
2194 next = filelines[lnum]
2199 next = filelines[lnum]
2195 except:
2200 except:
2196 next = None
2201 next = None
2197 # now execute the block of one or more lines
2202 # now execute the block of one or more lines
2198 try:
2203 try:
2199 exec block in globs,locs
2204 exec block in globs,locs
2200 except SystemExit:
2205 except SystemExit:
2201 pass
2206 pass
2202 except:
2207 except:
2203 badblocks.append(block.rstrip())
2208 badblocks.append(block.rstrip())
2204 if kw['quiet']: # restore stdout
2209 if kw['quiet']: # restore stdout
2205 sys.stdout.close()
2210 sys.stdout.close()
2206 sys.stdout = stdout_save
2211 sys.stdout = stdout_save
2207 print 'Finished replaying log file <%s>' % fname
2212 print 'Finished replaying log file <%s>' % fname
2208 if badblocks:
2213 if badblocks:
2209 print >> sys.stderr, ('\nThe following lines/blocks in file '
2214 print >> sys.stderr, ('\nThe following lines/blocks in file '
2210 '<%s> reported errors:' % fname)
2215 '<%s> reported errors:' % fname)
2211
2216
2212 for badline in badblocks:
2217 for badline in badblocks:
2213 print >> sys.stderr, badline
2218 print >> sys.stderr, badline
2214 else: # regular file execution
2219 else: # regular file execution
2215 try:
2220 try:
2216 execfile(fname,*where)
2221 execfile(fname,*where)
2217 except SyntaxError:
2222 except SyntaxError:
2218 etype,evalue = sys.exc_info()[:2]
2223 etype,evalue = sys.exc_info()[:2]
2219 self.SyntaxTB(etype,evalue,[])
2224 self.SyntaxTB(etype,evalue,[])
2220 warn('Failure executing file: <%s>' % fname)
2225 warn('Failure executing file: <%s>' % fname)
2221 except SystemExit,status:
2226 except SystemExit,status:
2222 if not kw['exit_ignore']:
2227 if not kw['exit_ignore']:
2223 self.InteractiveTB()
2228 self.InteractiveTB()
2224 warn('Failure executing file: <%s>' % fname)
2229 warn('Failure executing file: <%s>' % fname)
2225 except:
2230 except:
2226 self.InteractiveTB()
2231 self.InteractiveTB()
2227 warn('Failure executing file: <%s>' % fname)
2232 warn('Failure executing file: <%s>' % fname)
2228
2233
2229 #************************* end of file <iplib.py> *****************************
2234 #************************* end of file <iplib.py> *****************************
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
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