Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,164 +1,164 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for coloring text in ANSI terminals. |
|
3 | 3 | |
|
4 |
$Id: ColorANSI.py 9 |
|
|
4 | $Id: ColorANSI.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 |
# Copyright (C) 2002-200 |
|
|
7 | # Copyright (C) 2002-2006 Fernando Perez. <fperez@colorado.edu> | |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | |
|
17 | 17 | __all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable'] |
|
18 | 18 | |
|
19 | 19 | import os |
|
20 | 20 | |
|
21 | 21 | from IPython.Struct import Struct |
|
22 | 22 | |
|
23 | 23 | def make_color_table(in_class): |
|
24 | 24 | """Build a set of color attributes in a class. |
|
25 | 25 | |
|
26 | 26 | Helper function for building the *TermColors classes.""" |
|
27 | 27 | |
|
28 | 28 | color_templates = ( |
|
29 | 29 | ("Black" , "0;30"), |
|
30 | 30 | ("Red" , "0;31"), |
|
31 | 31 | ("Green" , "0;32"), |
|
32 | 32 | ("Brown" , "0;33"), |
|
33 | 33 | ("Blue" , "0;34"), |
|
34 | 34 | ("Purple" , "0;35"), |
|
35 | 35 | ("Cyan" , "0;36"), |
|
36 | 36 | ("LightGray" , "0;37"), |
|
37 | 37 | ("DarkGray" , "1;30"), |
|
38 | 38 | ("LightRed" , "1;31"), |
|
39 | 39 | ("LightGreen" , "1;32"), |
|
40 | 40 | ("Yellow" , "1;33"), |
|
41 | 41 | ("LightBlue" , "1;34"), |
|
42 | 42 | ("LightPurple" , "1;35"), |
|
43 | 43 | ("LightCyan" , "1;36"), |
|
44 | 44 | ("White" , "1;37"), ) |
|
45 | 45 | |
|
46 | 46 | for name,value in color_templates: |
|
47 | 47 | setattr(in_class,name,in_class._base % value) |
|
48 | 48 | |
|
49 | 49 | class TermColors: |
|
50 | 50 | """Color escape sequences. |
|
51 | 51 | |
|
52 | 52 | This class defines the escape sequences for all the standard (ANSI?) |
|
53 | 53 | colors in terminals. Also defines a NoColor escape which is just the null |
|
54 | 54 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
55 | 55 | confused by color escapes. |
|
56 | 56 | |
|
57 | 57 | This class should be used as a mixin for building color schemes.""" |
|
58 | 58 | |
|
59 | 59 | NoColor = '' # for color schemes in color-less terminals. |
|
60 | 60 | Normal = '\033[0m' # Reset normal coloring |
|
61 | 61 | _base = '\033[%sm' # Template for all other colors |
|
62 | 62 | |
|
63 | 63 | # Build the actual color table as a set of class attributes: |
|
64 | 64 | make_color_table(TermColors) |
|
65 | 65 | |
|
66 | 66 | class InputTermColors: |
|
67 | 67 | """Color escape sequences for input prompts. |
|
68 | 68 | |
|
69 | 69 | This class is similar to TermColors, but the escapes are wrapped in \001 |
|
70 | 70 | and \002 so that readline can properly know the length of each line and |
|
71 | 71 | can wrap lines accordingly. Use this class for any colored text which |
|
72 | 72 | needs to be used in input prompts, such as in calls to raw_input(). |
|
73 | 73 | |
|
74 | 74 | This class defines the escape sequences for all the standard (ANSI?) |
|
75 | 75 | colors in terminals. Also defines a NoColor escape which is just the null |
|
76 | 76 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
77 | 77 | confused by color escapes. |
|
78 | 78 | |
|
79 | 79 | This class should be used as a mixin for building color schemes.""" |
|
80 | 80 | |
|
81 | 81 | NoColor = '' # for color schemes in color-less terminals. |
|
82 | 82 | Normal = '\001\033[0m\002' # Reset normal coloring |
|
83 | 83 | _base = '\001\033[%sm\002' # Template for all other colors |
|
84 | 84 | |
|
85 | 85 | # Build the actual color table as a set of class attributes: |
|
86 | 86 | make_color_table(InputTermColors) |
|
87 | 87 | |
|
88 | 88 | class ColorScheme: |
|
89 | 89 | """Generic color scheme class. Just a name and a Struct.""" |
|
90 | 90 | def __init__(self,__scheme_name_,colordict=None,**colormap): |
|
91 | 91 | self.name = __scheme_name_ |
|
92 | 92 | if colordict is None: |
|
93 | 93 | self.colors = Struct(**colormap) |
|
94 | 94 | else: |
|
95 | 95 | self.colors = Struct(colordict) |
|
96 | 96 | |
|
97 | 97 | def copy(self,name=None): |
|
98 | 98 | """Return a full copy of the object, optionally renaming it.""" |
|
99 | 99 | if name is None: |
|
100 | 100 | name = self.name |
|
101 | 101 | return ColorScheme(name,self.colors.__dict__) |
|
102 | 102 | |
|
103 | 103 | class ColorSchemeTable(dict): |
|
104 | 104 | """General class to handle tables of color schemes. |
|
105 | 105 | |
|
106 | 106 | It's basically a dict of color schemes with a couple of shorthand |
|
107 | 107 | attributes and some convenient methods. |
|
108 | 108 | |
|
109 | 109 | active_scheme_name -> obvious |
|
110 | 110 | active_colors -> actual color table of the active scheme""" |
|
111 | 111 | |
|
112 | 112 | def __init__(self,scheme_list=None,default_scheme=''): |
|
113 | 113 | """Create a table of color schemes. |
|
114 | 114 | |
|
115 | 115 | The table can be created empty and manually filled or it can be |
|
116 | 116 | created with a list of valid color schemes AND the specification for |
|
117 | 117 | the default active scheme. |
|
118 | 118 | """ |
|
119 | 119 | |
|
120 | 120 | # create object attributes to be set later |
|
121 | 121 | self.active_scheme_name = '' |
|
122 | 122 | self.active_colors = None |
|
123 | 123 | |
|
124 | 124 | if scheme_list: |
|
125 | 125 | if default_scheme == '': |
|
126 | 126 | raise ValueError,'you must specify the default color scheme' |
|
127 | 127 | for scheme in scheme_list: |
|
128 | 128 | self.add_scheme(scheme) |
|
129 | 129 | self.set_active_scheme(default_scheme) |
|
130 | 130 | |
|
131 | 131 | def copy(self): |
|
132 | 132 | """Return full copy of object""" |
|
133 | 133 | return ColorSchemeTable(self.values(),self.active_scheme_name) |
|
134 | 134 | |
|
135 | 135 | def add_scheme(self,new_scheme): |
|
136 | 136 | """Add a new color scheme to the table.""" |
|
137 | 137 | if not isinstance(new_scheme,ColorScheme): |
|
138 | 138 | raise ValueError,'ColorSchemeTable only accepts ColorScheme instances' |
|
139 | 139 | self[new_scheme.name] = new_scheme |
|
140 | 140 | |
|
141 | 141 | def set_active_scheme(self,scheme,case_sensitive=0): |
|
142 | 142 | """Set the currently active scheme. |
|
143 | 143 | |
|
144 | 144 | Names are by default compared in a case-insensitive way, but this can |
|
145 | 145 | be changed by setting the parameter case_sensitive to true.""" |
|
146 | 146 | |
|
147 | 147 | scheme_names = self.keys() |
|
148 | 148 | if case_sensitive: |
|
149 | 149 | valid_schemes = scheme_names |
|
150 | 150 | scheme_test = scheme |
|
151 | 151 | else: |
|
152 | 152 | valid_schemes = [s.lower() for s in scheme_names] |
|
153 | 153 | scheme_test = scheme.lower() |
|
154 | 154 | try: |
|
155 | 155 | scheme_idx = valid_schemes.index(scheme_test) |
|
156 | 156 | except ValueError: |
|
157 | 157 | raise ValueError,'Unrecognized color scheme: ' + scheme + \ |
|
158 | 158 | '\nValid schemes: '+str(scheme_names).replace("'', ",'') |
|
159 | 159 | else: |
|
160 | 160 | active = scheme_names[scheme_idx] |
|
161 | 161 | self.active_scheme_name = active |
|
162 | 162 | self.active_colors = self[active].colors |
|
163 | 163 | # Now allow using '' as an index for the current active scheme |
|
164 | 164 | self[''] = self[active] |
@@ -1,116 +1,116 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Configuration loader |
|
3 | 3 | |
|
4 |
$Id: ConfigLoader.py 9 |
|
|
4 | $Id: ConfigLoader.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 |
# Copyright (C) 2001-200 |
|
|
7 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | |
|
17 | 17 | import exceptions |
|
18 | 18 | import os |
|
19 | 19 | from pprint import pprint |
|
20 | 20 | |
|
21 | 21 | from IPython import ultraTB |
|
22 | 22 | from IPython.Struct import Struct |
|
23 | 23 | from IPython.genutils import * |
|
24 | 24 | |
|
25 | 25 | class ConfigLoaderError(exceptions.Exception): |
|
26 | 26 | """Exception for ConfigLoader class.""" |
|
27 | 27 | |
|
28 | 28 | def __init__(self,args=None): |
|
29 | 29 | self.args = args |
|
30 | 30 | |
|
31 | 31 | class ConfigLoader: |
|
32 | 32 | |
|
33 | 33 | """Configuration file loader capable of handling recursive inclusions and |
|
34 | 34 | with parametrized conflict resolution for multiply found keys.""" |
|
35 | 35 | |
|
36 | 36 | def __init__(self,conflict=None,field_sep=None,reclimit=15): |
|
37 | 37 | |
|
38 | 38 | """The reclimit parameter controls the number of recursive |
|
39 | 39 | configuration file inclusions. This way we can stop early on (before |
|
40 | 40 | python's own recursion limit is hit) if there is a circular |
|
41 | 41 | inclusion. |
|
42 | 42 | |
|
43 | 43 | - conflict: dictionary for conflict resolutions (see Struct.merge()) |
|
44 | 44 | |
|
45 | 45 | """ |
|
46 | 46 | self.conflict = conflict |
|
47 | 47 | self.field_sep = field_sep |
|
48 | 48 | self.reset(reclimit) |
|
49 | 49 | |
|
50 | 50 | def reset(self,reclimit=15): |
|
51 | 51 | self.reclimit = reclimit |
|
52 | 52 | self.recdepth = 0 |
|
53 | 53 | self.included = [] |
|
54 | 54 | |
|
55 | 55 | def load(self,fname,convert=None,recurse_key='',incpath = '.',**kw): |
|
56 | 56 | """Load a configuration file, return the resulting Struct. |
|
57 | 57 | |
|
58 | 58 | Call: load_config(fname,convert=None,conflict=None,recurse_key='') |
|
59 | 59 | |
|
60 | 60 | - fname: file to load from. |
|
61 | 61 | - convert: dictionary of type conversions (see read_dict()) |
|
62 | 62 | - recurse_key: keyword in dictionary to trigger recursive file |
|
63 | 63 | inclusions. |
|
64 | 64 | """ |
|
65 | 65 | |
|
66 | 66 | if self.recdepth > self.reclimit: |
|
67 | 67 | raise ConfigLoaderError, 'maximum recursive inclusion of rcfiles '+\ |
|
68 | 68 | 'exceeded: ' + `self.recdepth` + \ |
|
69 | 69 | '.\nMaybe you have a circular chain of inclusions?' |
|
70 | 70 | self.recdepth += 1 |
|
71 | 71 | fname = filefind(fname,incpath) |
|
72 | 72 | data = Struct() |
|
73 | 73 | # avoid including the same file more than once |
|
74 | 74 | if fname in self.included: |
|
75 | 75 | return data |
|
76 | 76 | Xinfo = ultraTB.AutoFormattedTB() |
|
77 | 77 | if convert==None and recurse_key : convert = {qwflat:recurse_key} |
|
78 | 78 | # for production, change warn to 0: |
|
79 | 79 | data.merge(read_dict(fname,convert,fs=self.field_sep,strip=1, |
|
80 | 80 | warn=0,no_empty=0,**kw)) |
|
81 | 81 | # keep track of successfully loaded files |
|
82 | 82 | self.included.append(fname) |
|
83 | 83 | if recurse_key in data.keys(): |
|
84 | 84 | for incfilename in data[recurse_key]: |
|
85 | 85 | found=0 |
|
86 | 86 | try: |
|
87 | 87 | incfile = filefind(incfilename,incpath) |
|
88 | 88 | except IOError: |
|
89 | 89 | if os.name in ['nt','dos']: |
|
90 | 90 | try: |
|
91 | 91 | # Try again with '.ini' extension |
|
92 | 92 | incfilename += '.ini' |
|
93 | 93 | incfile = filefind(incfilename,incpath) |
|
94 | 94 | except IOError: |
|
95 | 95 | found = 0 |
|
96 | 96 | else: |
|
97 | 97 | found = 1 |
|
98 | 98 | else: |
|
99 | 99 | found = 0 |
|
100 | 100 | else: |
|
101 | 101 | found = 1 |
|
102 | 102 | if found: |
|
103 | 103 | try: |
|
104 | 104 | data.merge(self.load(incfile,convert,recurse_key, |
|
105 | 105 | incpath,**kw), |
|
106 | 106 | self.conflict) |
|
107 | 107 | except: |
|
108 | 108 | Xinfo() |
|
109 | 109 | warn('Problem loading included file: '+ |
|
110 | 110 | `incfilename` + '. Ignoring it...') |
|
111 | 111 | else: |
|
112 | 112 | warn('File `%s` not found. Included by %s' % (incfilename,fname)) |
|
113 | 113 | |
|
114 | 114 | return data |
|
115 | 115 | |
|
116 | 116 | # end ConfigLoader |
@@ -1,111 +1,111 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """sys.excepthook for IPython itself, leaves a detailed report on disk. |
|
3 | 3 | |
|
4 |
$Id: CrashHandler.py 9 |
|
|
4 | $Id: CrashHandler.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 |
# Copyright (C) 2001-200 |
|
|
7 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | __version__ = Release.version |
|
17 | 17 | |
|
18 | 18 | #**************************************************************************** |
|
19 | 19 | # Required modules |
|
20 | 20 | |
|
21 | 21 | # From the standard library |
|
22 | 22 | import os |
|
23 | 23 | import sys |
|
24 | 24 | from pprint import pprint,pformat |
|
25 | 25 | |
|
26 | 26 | # Homebrewed |
|
27 | 27 | from IPython.Itpl import Itpl,itpl,printpl |
|
28 | 28 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
29 | 29 | from IPython import ultraTB |
|
30 | 30 | from IPython.genutils import * |
|
31 | 31 | |
|
32 | 32 | #**************************************************************************** |
|
33 | 33 | class CrashHandler: |
|
34 | 34 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
35 | 35 | |
|
36 | 36 | def __init__(self,IP): |
|
37 | 37 | self.IP = IP # IPython instance |
|
38 | 38 | self.bug_contact = Release.authors['Fernando'][0] |
|
39 | 39 | self.mailto = Release.authors['Fernando'][1] |
|
40 | 40 | |
|
41 | 41 | def __call__(self,etype, evalue, etb): |
|
42 | 42 | |
|
43 | 43 | # Report tracebacks shouldn't use color in general (safer for users) |
|
44 | 44 | color_scheme = 'NoColor' |
|
45 | 45 | |
|
46 | 46 | # Use this ONLY for developer debugging (keep commented out for release) |
|
47 | 47 | #color_scheme = 'Linux' # dbg |
|
48 | 48 | |
|
49 | 49 | try: |
|
50 | 50 | rptdir = self.IP.rc.ipythondir |
|
51 | 51 | except: |
|
52 | 52 | rptdir = os.getcwd() |
|
53 | 53 | if not os.path.isdir(rptdir): |
|
54 | 54 | rptdir = os.getcwd() |
|
55 | 55 | self.report_name = os.path.join(rptdir,'IPython_crash_report.txt') |
|
56 | 56 | self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1) |
|
57 | 57 | traceback = self.TBhandler.text(etype,evalue,etb,context=31) |
|
58 | 58 | |
|
59 | 59 | # print traceback to screen |
|
60 | 60 | print >> sys.stderr, traceback |
|
61 | 61 | |
|
62 | 62 | # and generate a complete report on disk |
|
63 | 63 | try: |
|
64 | 64 | report = open(self.report_name,'w') |
|
65 | 65 | except: |
|
66 | 66 | print >> sys.stderr, 'Could not create crash report on disk.' |
|
67 | 67 | return |
|
68 | 68 | |
|
69 | 69 | msg = itpl('\n'+'*'*70+'\n' |
|
70 | 70 | """ |
|
71 | 71 | Oops, IPython crashed. We do our best to make it stable, but... |
|
72 | 72 | |
|
73 | 73 | A crash report was automatically generated with the following information: |
|
74 | 74 | - A verbatim copy of the traceback above this text. |
|
75 | 75 | - A copy of your input history during this session. |
|
76 | 76 | - Data on your current IPython configuration. |
|
77 | 77 | |
|
78 | 78 | It was left in the file named: |
|
79 | 79 | \t'$self.report_name' |
|
80 | 80 | If you can email this file to the developers, the information in it will help |
|
81 | 81 | them in understanding and correcting the problem. |
|
82 | 82 | |
|
83 | 83 | You can mail it to $self.bug_contact at $self.mailto |
|
84 | 84 | with the subject 'IPython Crash Report'. |
|
85 | 85 | |
|
86 | 86 | If you want to do it now, the following command will work (under Unix): |
|
87 | 87 | mail -s 'IPython Crash Report' $self.mailto < $self.report_name |
|
88 | 88 | |
|
89 | 89 | To ensure accurate tracking of this issue, please file a report about it at: |
|
90 | 90 | http://www.scipy.net/roundup/ipython (IPython's online bug tracker). |
|
91 | 91 | """) |
|
92 | 92 | print >> sys.stderr, msg |
|
93 | 93 | |
|
94 | 94 | sec_sep = '\n\n'+'*'*75+'\n\n' |
|
95 | 95 | report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n') |
|
96 | 96 | report.write('IPython version: %s \n\n' % Release.version) |
|
97 | 97 | report.write('SVN revision : %s \n\n' % Release.revision) |
|
98 | 98 | report.write('Platform info : os.name -> %s, sys.platform -> %s' % |
|
99 | 99 | (os.name,sys.platform) ) |
|
100 | 100 | report.write(sec_sep+'Current user configuration structure:\n\n') |
|
101 | 101 | report.write(pformat(self.IP.rc.dict())) |
|
102 | 102 | report.write(sec_sep+'Crash traceback:\n\n' + traceback) |
|
103 | 103 | try: |
|
104 | 104 | report.write(sec_sep+"History of session input:") |
|
105 | 105 | for line in self.IP.user_ns['_ih']: |
|
106 | 106 | report.write(line) |
|
107 | 107 | report.write('\n*** Last line of input (may not be in above history):\n') |
|
108 | 108 | report.write(self.IP._last_input_line+'\n') |
|
109 | 109 | except: |
|
110 | 110 | pass |
|
111 | 111 | report.close() |
@@ -1,270 +1,286 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Pdb debugger class. |
|
4 | 4 | |
|
5 | 5 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
6 | 6 | the command line completion of other programs which include this isn't |
|
7 | 7 | damaged. |
|
8 | 8 | |
|
9 | 9 | In the future, this class will be expanded with improvements over the standard |
|
10 | 10 | pdb. |
|
11 | 11 | |
|
12 | 12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor |
|
13 | 13 | changes. Licensing should therefore be under the standard Python terms. For |
|
14 | 14 | details on the PSF (Python Software Foundation) standard license, see: |
|
15 | 15 | |
|
16 | 16 | http://www.python.org/2.2.3/license.html |
|
17 | 17 | |
|
18 |
$Id: Debugger.py 9 |
|
|
18 | $Id: Debugger.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
19 | ||
|
20 | #***************************************************************************** | |
|
21 | # | |
|
22 | # Since this file is essentially a modified copy of the pdb module which is | |
|
23 | # part of the standard Python distribution, I assume that the proper procedure | |
|
24 | # is to maintain its copyright as belonging to the Python Software Foundation | |
|
25 | # (in addition to my own, for all new code). | |
|
26 | # | |
|
27 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
|
28 | # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu> | |
|
29 | # | |
|
30 | # Distributed under the terms of the BSD License. The full license is in | |
|
31 | # the file COPYING, distributed as part of this software. | |
|
32 | # | |
|
33 | #***************************************************************************** | |
|
34 | ||
|
19 | 35 | |
|
20 | 36 | from IPython import Release |
|
21 | 37 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
22 | 38 | __license__ = 'Python' |
|
23 | 39 | |
|
24 | 40 | import bdb |
|
25 | 41 | import cmd |
|
26 | 42 | import linecache |
|
27 | 43 | import os |
|
28 | 44 | import pdb |
|
29 | 45 | import sys |
|
30 | 46 | |
|
31 | 47 | from IPython import PyColorize, ColorANSI |
|
32 | 48 | from IPython.genutils import Term |
|
33 | 49 | from IPython.excolors import ExceptionColors |
|
34 | 50 | |
|
35 | 51 | def _file_lines(fname): |
|
36 | 52 | """Return the contents of a named file as a list of lines. |
|
37 | 53 | |
|
38 | 54 | This function never raises an IOError exception: if the file can't be |
|
39 | 55 | read, it simply returns an empty list.""" |
|
40 | 56 | |
|
41 | 57 | try: |
|
42 | 58 | outfile = open(fname) |
|
43 | 59 | except IOError: |
|
44 | 60 | return [] |
|
45 | 61 | else: |
|
46 | 62 | out = outfile.readlines() |
|
47 | 63 | outfile.close() |
|
48 | 64 | return out |
|
49 | 65 | |
|
50 | 66 | |
|
51 | 67 | class Pdb(pdb.Pdb): |
|
52 | 68 | """Modified Pdb class, does not load readline.""" |
|
53 | 69 | def __init__(self,color_scheme='NoColor'): |
|
54 | 70 | bdb.Bdb.__init__(self) |
|
55 | 71 | cmd.Cmd.__init__(self,completekey=None) # don't load readline |
|
56 | 72 | self.prompt = 'ipdb> ' # The default prompt is '(Pdb)' |
|
57 | 73 | self.aliases = {} |
|
58 | 74 | |
|
59 | 75 | # Read $HOME/.pdbrc and ./.pdbrc |
|
60 | 76 | try: |
|
61 | 77 | self.rcLines = _file_lines(os.path.join(os.environ['HOME'], |
|
62 | 78 | ".pdbrc")) |
|
63 | 79 | except KeyError: |
|
64 | 80 | self.rcLines = [] |
|
65 | 81 | self.rcLines.extend(_file_lines(".pdbrc")) |
|
66 | 82 | |
|
67 | 83 | # Create color table: we copy the default one from the traceback |
|
68 | 84 | # module and add a few attributes needed for debugging |
|
69 | 85 | self.color_scheme_table = ExceptionColors.copy() |
|
70 | 86 | |
|
71 | 87 | # shorthands |
|
72 | 88 | C = ColorANSI.TermColors |
|
73 | 89 | cst = self.color_scheme_table |
|
74 | 90 | |
|
75 | 91 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor |
|
76 | 92 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor |
|
77 | 93 | |
|
78 | 94 | cst['Linux'].colors.breakpoint_enabled = C.LightRed |
|
79 | 95 | cst['Linux'].colors.breakpoint_disabled = C.Red |
|
80 | 96 | |
|
81 | 97 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed |
|
82 | 98 | cst['LightBG'].colors.breakpoint_disabled = C.Red |
|
83 | 99 | |
|
84 | 100 | self.set_colors(color_scheme) |
|
85 | 101 | |
|
86 | 102 | def set_colors(self, scheme): |
|
87 | 103 | """Shorthand access to the color table scheme selector method.""" |
|
88 | 104 | self.color_scheme_table.set_active_scheme(scheme) |
|
89 | 105 | |
|
90 | 106 | |
|
91 | 107 | def interaction(self, frame, traceback): |
|
92 | 108 | __IPYTHON__.set_completer_frame(frame) |
|
93 | 109 | pdb.Pdb.interaction(self, frame, traceback) |
|
94 | 110 | |
|
95 | 111 | |
|
96 | 112 | def do_up(self, arg): |
|
97 | 113 | pdb.Pdb.do_up(self, arg) |
|
98 | 114 | __IPYTHON__.set_completer_frame(self.curframe) |
|
99 | 115 | do_u = do_up |
|
100 | 116 | |
|
101 | 117 | |
|
102 | 118 | def do_down(self, arg): |
|
103 | 119 | pdb.Pdb.do_down(self, arg) |
|
104 | 120 | __IPYTHON__.set_completer_frame(self.curframe) |
|
105 | 121 | do_d = do_down |
|
106 | 122 | |
|
107 | 123 | |
|
108 | 124 | def postloop(self): |
|
109 | 125 | __IPYTHON__.set_completer_frame(None) |
|
110 | 126 | |
|
111 | 127 | |
|
112 | 128 | def print_stack_trace(self): |
|
113 | 129 | try: |
|
114 | 130 | for frame_lineno in self.stack: |
|
115 | 131 | self.print_stack_entry(frame_lineno, context = 5) |
|
116 | 132 | except KeyboardInterrupt: |
|
117 | 133 | pass |
|
118 | 134 | |
|
119 | 135 | |
|
120 | 136 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', |
|
121 | 137 | context = 3): |
|
122 | 138 | frame, lineno = frame_lineno |
|
123 | 139 | print >>Term.cout, self.format_stack_entry(frame_lineno, '', context) |
|
124 | 140 | |
|
125 | 141 | |
|
126 | 142 | def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): |
|
127 | 143 | import linecache, repr |
|
128 | 144 | |
|
129 | 145 | ret = "" |
|
130 | 146 | |
|
131 | 147 | Colors = self.color_scheme_table.active_colors |
|
132 | 148 | ColorsNormal = Colors.Normal |
|
133 | 149 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
134 | 150 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) |
|
135 | 151 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
136 | 152 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, |
|
137 | 153 | ColorsNormal) |
|
138 | 154 | |
|
139 | 155 | frame, lineno = frame_lineno |
|
140 | 156 | |
|
141 | 157 | return_value = '' |
|
142 | 158 | if '__return__' in frame.f_locals: |
|
143 | 159 | rv = frame.f_locals['__return__'] |
|
144 | 160 | #return_value += '->' |
|
145 | 161 | return_value += repr.repr(rv) + '\n' |
|
146 | 162 | ret += return_value |
|
147 | 163 | |
|
148 | 164 | #s = filename + '(' + `lineno` + ')' |
|
149 | 165 | filename = self.canonic(frame.f_code.co_filename) |
|
150 | 166 | link = tpl_link % filename |
|
151 | 167 | |
|
152 | 168 | if frame.f_code.co_name: |
|
153 | 169 | func = frame.f_code.co_name |
|
154 | 170 | else: |
|
155 | 171 | func = "<lambda>" |
|
156 | 172 | |
|
157 | 173 | call = '' |
|
158 | 174 | if func != '?': |
|
159 | 175 | if '__args__' in frame.f_locals: |
|
160 | 176 | args = repr.repr(frame.f_locals['__args__']) |
|
161 | 177 | else: |
|
162 | 178 | args = '()' |
|
163 | 179 | call = tpl_call % (func, args) |
|
164 | 180 | |
|
165 | 181 | level = '%s %s\n' % (link, call) |
|
166 | 182 | ret += level |
|
167 | 183 | |
|
168 | 184 | start = lineno - 1 - context//2 |
|
169 | 185 | lines = linecache.getlines(filename) |
|
170 | 186 | start = max(start, 0) |
|
171 | 187 | start = min(start, len(lines) - context) |
|
172 | 188 | lines = lines[start : start + context] |
|
173 | 189 | |
|
174 | 190 | for i in range(len(lines)): |
|
175 | 191 | line = lines[i] |
|
176 | 192 | if start + 1 + i == lineno: |
|
177 | 193 | ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True) |
|
178 | 194 | else: |
|
179 | 195 | ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False) |
|
180 | 196 | |
|
181 | 197 | return ret |
|
182 | 198 | |
|
183 | 199 | |
|
184 | 200 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): |
|
185 | 201 | bp_mark = "" |
|
186 | 202 | bp_mark_color = "" |
|
187 | 203 | |
|
188 | 204 | bp = None |
|
189 | 205 | if lineno in self.get_file_breaks(filename): |
|
190 | 206 | bps = self.get_breaks(filename, lineno) |
|
191 | 207 | bp = bps[-1] |
|
192 | 208 | |
|
193 | 209 | if bp: |
|
194 | 210 | Colors = self.color_scheme_table.active_colors |
|
195 | 211 | bp_mark = str(bp.number) |
|
196 | 212 | bp_mark_color = Colors.breakpoint_enabled |
|
197 | 213 | if not bp.enabled: |
|
198 | 214 | bp_mark_color = Colors.breakpoint_disabled |
|
199 | 215 | |
|
200 | 216 | numbers_width = 7 |
|
201 | 217 | if arrow: |
|
202 | 218 | # This is the line with the error |
|
203 | 219 | pad = numbers_width - len(str(lineno)) - len(bp_mark) |
|
204 | 220 | if pad >= 3: |
|
205 | 221 | marker = '-'*(pad-3) + '-> ' |
|
206 | 222 | elif pad == 2: |
|
207 | 223 | marker = '> ' |
|
208 | 224 | elif pad == 1: |
|
209 | 225 | marker = '>' |
|
210 | 226 | else: |
|
211 | 227 | marker = '' |
|
212 | 228 | num = '%s%s' % (marker, str(lineno)) |
|
213 | 229 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
214 | 230 | else: |
|
215 | 231 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) |
|
216 | 232 | line = tpl_line % (bp_mark_color + bp_mark, num, line) |
|
217 | 233 | |
|
218 | 234 | return line |
|
219 | 235 | |
|
220 | 236 | |
|
221 | 237 | def do_list(self, arg): |
|
222 | 238 | self.lastcmd = 'list' |
|
223 | 239 | last = None |
|
224 | 240 | if arg: |
|
225 | 241 | try: |
|
226 | 242 | x = eval(arg, {}, {}) |
|
227 | 243 | if type(x) == type(()): |
|
228 | 244 | first, last = x |
|
229 | 245 | first = int(first) |
|
230 | 246 | last = int(last) |
|
231 | 247 | if last < first: |
|
232 | 248 | # Assume it's a count |
|
233 | 249 | last = first + last |
|
234 | 250 | else: |
|
235 | 251 | first = max(1, int(x) - 5) |
|
236 | 252 | except: |
|
237 | 253 | print '*** Error in argument:', `arg` |
|
238 | 254 | return |
|
239 | 255 | elif self.lineno is None: |
|
240 | 256 | first = max(1, self.curframe.f_lineno - 5) |
|
241 | 257 | else: |
|
242 | 258 | first = self.lineno + 1 |
|
243 | 259 | if last is None: |
|
244 | 260 | last = first + 10 |
|
245 | 261 | filename = self.curframe.f_code.co_filename |
|
246 | 262 | try: |
|
247 | 263 | Colors = self.color_scheme_table.active_colors |
|
248 | 264 | ColorsNormal = Colors.Normal |
|
249 | 265 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) |
|
250 | 266 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) |
|
251 | 267 | src = [] |
|
252 | 268 | for lineno in range(first, last+1): |
|
253 | 269 | line = linecache.getline(filename, lineno) |
|
254 | 270 | if not line: |
|
255 | 271 | break |
|
256 | 272 | |
|
257 | 273 | if lineno == self.curframe.f_lineno: |
|
258 | 274 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) |
|
259 | 275 | else: |
|
260 | 276 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) |
|
261 | 277 | |
|
262 | 278 | src.append(line) |
|
263 | 279 | self.lineno = lineno |
|
264 | 280 | |
|
265 | 281 | print >>Term.cout, ''.join(src) |
|
266 | 282 | |
|
267 | 283 | except KeyboardInterrupt: |
|
268 | 284 | pass |
|
269 | 285 | |
|
270 | 286 | do_l = do_list |
@@ -1,272 +1,272 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Modified input prompt for executing files. |
|
3 | 3 | |
|
4 | 4 | We define a special input line filter to allow typing lines which begin with |
|
5 | 5 | '~', '/' or '.'. If one of those strings is encountered, it is automatically |
|
6 | 6 | executed. |
|
7 | 7 | |
|
8 |
$Id: InterpreterExec.py |
|
|
8 | $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2004 W.J. van der Laan <gnufnork@hetdigitalegat.nl> |
|
12 | # Copyright (C) 2004 Fernando Perez <fperez@colorado.edu> | |
|
12 | # Copyright (C) 2004-2006 Fernando Perez <fperez@colorado.edu> | |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #***************************************************************************** |
|
17 | 17 | |
|
18 | 18 | from IPython import Release |
|
19 | 19 | __author__ = 'W.J. van der Laan <gnufnork@hetdigitalegat.nl>, '\ |
|
20 | 20 | '%s <%s>' % Release.authors['Fernando'] |
|
21 | 21 | __license__ = Release.license |
|
22 | 22 | |
|
23 | 23 | def prefilter_shell(self,line,continuation): |
|
24 | 24 | """Alternate prefilter, modified for shell-like functionality. |
|
25 | 25 | |
|
26 | 26 | - Execute all lines beginning with '~', '/' or '.' |
|
27 | 27 | - $var=cmd <=> %sc var=cmd |
|
28 | 28 | - $$var=cmd <=> %sc -l var=cmd |
|
29 | 29 | """ |
|
30 | 30 | |
|
31 | 31 | if line: |
|
32 | 32 | l0 = line[0] |
|
33 | 33 | if l0 in '~/.': |
|
34 | 34 | return self._prefilter("!%s"%line,continuation) |
|
35 | 35 | elif l0=='$': |
|
36 | 36 | lrest = line[1:] |
|
37 | 37 | if lrest.startswith('$'): |
|
38 | 38 | # $$var=cmd <=> %sc -l var=cmd |
|
39 | 39 | return self._prefilter("%ssc -l %s" % (self.ESC_MAGIC,lrest[1:]), |
|
40 | 40 | continuation) |
|
41 | 41 | else: |
|
42 | 42 | # $var=cmd <=> %sc var=cmd |
|
43 | 43 | return self._prefilter("%ssc %s" % (self.ESC_MAGIC,lrest), |
|
44 | 44 | continuation) |
|
45 | 45 | else: |
|
46 | 46 | return self._prefilter(line,continuation) |
|
47 | 47 | else: |
|
48 | 48 | return self._prefilter(line,continuation) |
|
49 | 49 | |
|
50 | 50 | # Rebind this to be the new IPython prefilter: |
|
51 | 51 | from IPython.iplib import InteractiveShell |
|
52 | 52 | InteractiveShell.prefilter = prefilter_shell |
|
53 | 53 | # Clean up the namespace. |
|
54 | 54 | del InteractiveShell,prefilter_shell |
|
55 | 55 | |
|
56 | 56 | # Provide pysh and further shell-oriented services |
|
57 | 57 | import os,sys,shutil |
|
58 | 58 | from IPython.genutils import system,shell,getoutput,getoutputerror |
|
59 | 59 | |
|
60 | 60 | # Short aliases for getting shell output as a string and a list |
|
61 | 61 | sout = getoutput |
|
62 | 62 | lout = lambda cmd: getoutput(cmd,split=1) |
|
63 | 63 | |
|
64 | 64 | # Empty function, meant as a docstring holder so help(pysh) works. |
|
65 | 65 | def pysh(): |
|
66 | 66 | """Pysh is a set of modules and extensions to IPython which make shell-like |
|
67 | 67 | usage with Python syntax more convenient. Keep in mind that pysh is NOT a |
|
68 | 68 | full-blown shell, so don't try to make it your /etc/passwd entry! |
|
69 | 69 | |
|
70 | 70 | In particular, it has no job control, so if you type Ctrl-Z (under Unix), |
|
71 | 71 | you'll suspend pysh itself, not the process you just started. |
|
72 | 72 | |
|
73 | 73 | Since pysh is really nothing but a customized IPython, you should |
|
74 | 74 | familiarize yourself with IPython's features. This brief help mainly |
|
75 | 75 | documents areas in which pysh differs from the normal IPython. |
|
76 | 76 | |
|
77 | 77 | ALIASES |
|
78 | 78 | ------- |
|
79 | 79 | All of your $PATH has been loaded as IPython aliases, so you should be |
|
80 | 80 | able to type any normal system command and have it executed. See %alias? |
|
81 | 81 | and %unalias? for details on the alias facilities. |
|
82 | 82 | |
|
83 | 83 | SPECIAL SYNTAX |
|
84 | 84 | -------------- |
|
85 | 85 | Any lines which begin with '~', '/' and '.' will be executed as shell |
|
86 | 86 | commands instead of as Python code. The special escapes below are also |
|
87 | 87 | recognized. !cmd is valid in single or multi-line input, all others are |
|
88 | 88 | only valid in single-line input: |
|
89 | 89 | |
|
90 | 90 | !cmd - pass 'cmd' directly to the shell |
|
91 | 91 | !!cmd - execute 'cmd' and return output as a list (split on '\\n') |
|
92 | 92 | $var=cmd - capture output of cmd into var, as a string |
|
93 | 93 | $$var=cmd - capture output of cmd into var, as a list (split on '\\n') |
|
94 | 94 | |
|
95 | 95 | The $/$$ syntaxes make Python variables from system output, which you can |
|
96 | 96 | later use for further scripting. The converse is also possible: when |
|
97 | 97 | executing an alias or calling to the system via !/!!, you can expand any |
|
98 | 98 | python variable or expression by prepending it with $. Full details of |
|
99 | 99 | the allowed syntax can be found in Python's PEP 215. |
|
100 | 100 | |
|
101 | 101 | A few brief examples will illustrate these: |
|
102 | 102 | |
|
103 | 103 | fperez[~/test]|3> !ls *s.py |
|
104 | 104 | scopes.py strings.py |
|
105 | 105 | |
|
106 | 106 | ls is an internal alias, so there's no need to use !: |
|
107 | 107 | fperez[~/test]|4> ls *s.py |
|
108 | 108 | scopes.py* strings.py |
|
109 | 109 | |
|
110 | 110 | !!ls will return the output into a Python variable: |
|
111 | 111 | fperez[~/test]|5> !!ls *s.py |
|
112 | 112 | <5> ['scopes.py', 'strings.py'] |
|
113 | 113 | fperez[~/test]|6> print _5 |
|
114 | 114 | ['scopes.py', 'strings.py'] |
|
115 | 115 | |
|
116 | 116 | $ and $$ allow direct capture to named variables: |
|
117 | 117 | fperez[~/test]|7> $astr = ls *s.py |
|
118 | 118 | fperez[~/test]|8> astr |
|
119 | 119 | <8> 'scopes.py\\nstrings.py' |
|
120 | 120 | |
|
121 | 121 | fperez[~/test]|9> $$alist = ls *s.py |
|
122 | 122 | fperez[~/test]|10> alist |
|
123 | 123 | <10> ['scopes.py', 'strings.py'] |
|
124 | 124 | |
|
125 | 125 | alist is now a normal python list you can loop over. Using $ will expand |
|
126 | 126 | back the python values when alias calls are made: |
|
127 | 127 | fperez[~/test]|11> for f in alist: |
|
128 | 128 | |..> print 'file',f, |
|
129 | 129 | |..> wc -l $f |
|
130 | 130 | |..> |
|
131 | 131 | file scopes.py 13 scopes.py |
|
132 | 132 | file strings.py 4 strings.py |
|
133 | 133 | |
|
134 | 134 | Note that you may need to protect your variables with braces if you want |
|
135 | 135 | to append strings to their names. To copy all files in alist to .bak |
|
136 | 136 | extensions, you must use: |
|
137 | 137 | fperez[~/test]|12> for f in alist: |
|
138 | 138 | |..> cp $f ${f}.bak |
|
139 | 139 | |
|
140 | 140 | If you try using $f.bak, you'll get an AttributeError exception saying |
|
141 | 141 | that your string object doesn't have a .bak attribute. This is because |
|
142 | 142 | the $ expansion mechanism allows you to expand full Python expressions: |
|
143 | 143 | fperez[~/test]|13> echo "sys.platform is: $sys.platform" |
|
144 | 144 | sys.platform is: linux2 |
|
145 | 145 | |
|
146 | 146 | IPython's input history handling is still active, which allows you to |
|
147 | 147 | rerun a single block of multi-line input by simply using exec: |
|
148 | 148 | fperez[~/test]|14> $$alist = ls *.eps |
|
149 | 149 | fperez[~/test]|15> exec _i11 |
|
150 | 150 | file image2.eps 921 image2.eps |
|
151 | 151 | file image.eps 921 image.eps |
|
152 | 152 | |
|
153 | 153 | While these are new special-case syntaxes, they are designed to allow very |
|
154 | 154 | efficient use of the shell with minimal typing. At an interactive shell |
|
155 | 155 | prompt, conciseness of expression wins over readability. |
|
156 | 156 | |
|
157 | 157 | USEFUL FUNCTIONS AND MODULES |
|
158 | 158 | ---------------------------- |
|
159 | 159 | The os, sys and shutil modules from the Python standard library are |
|
160 | 160 | automatically loaded. Some additional functions, useful for shell usage, |
|
161 | 161 | are listed below. You can request more help about them with '?'. |
|
162 | 162 | |
|
163 | 163 | shell - execute a command in the underlying system shell |
|
164 | 164 | system - like shell(), but return the exit status of the command |
|
165 | 165 | sout - capture the output of a command as a string |
|
166 | 166 | lout - capture the output of a command as a list (split on '\\n') |
|
167 | 167 | getoutputerror - capture (output,error) of a shell command |
|
168 | 168 | |
|
169 | 169 | sout/lout are the functional equivalents of $/$$. They are provided to |
|
170 | 170 | allow you to capture system output in the middle of true python code, |
|
171 | 171 | function definitions, etc (where $ and $$ are invalid). |
|
172 | 172 | |
|
173 | 173 | DIRECTORY MANAGEMENT |
|
174 | 174 | -------------------- |
|
175 | 175 | Since each command passed by pysh to the underlying system is executed in |
|
176 | 176 | a subshell which exits immediately, you can NOT use !cd to navigate the |
|
177 | 177 | filesystem. |
|
178 | 178 | |
|
179 | 179 | Pysh provides its own builtin '%cd' magic command to move in the |
|
180 | 180 | filesystem (the % is not required with automagic on). It also maintains a |
|
181 | 181 | list of visited directories (use %dhist to see it) and allows direct |
|
182 | 182 | switching to any of them. Type 'cd?' for more details. |
|
183 | 183 | |
|
184 | 184 | %pushd, %popd and %dirs are provided for directory stack handling. |
|
185 | 185 | |
|
186 | 186 | PROMPT CUSTOMIZATION |
|
187 | 187 | -------------------- |
|
188 | 188 | |
|
189 | 189 | The supplied ipythonrc-pysh profile comes with an example of a very |
|
190 | 190 | colored and detailed prompt, mainly to serve as an illustration. The |
|
191 | 191 | valid escape sequences, besides color names, are: |
|
192 | 192 | |
|
193 | 193 | \\# - Prompt number. |
|
194 | 194 | \\D - Dots, as many as there are digits in \\# (so they align). |
|
195 | 195 | \\w - Current working directory (cwd). |
|
196 | 196 | \\W - Basename of current working directory. |
|
197 | 197 | \\XN - Where N=0..5. N terms of the cwd, with $HOME written as ~. |
|
198 | 198 | \\YN - Where N=0..5. Like XN, but if ~ is term N+1 it's also shown. |
|
199 | 199 | \\u - Username. |
|
200 | 200 | \\H - Full hostname. |
|
201 | 201 | \\h - Hostname up to first '.' |
|
202 | 202 | \\$ - Root symbol ($ or #). |
|
203 | 203 | \\t - Current time, in H:M:S format. |
|
204 | 204 | \\v - IPython release version. |
|
205 | 205 | \\n - Newline. |
|
206 | 206 | \\r - Carriage return. |
|
207 | 207 | \\\\ - An explicitly escaped '\\'. |
|
208 | 208 | |
|
209 | 209 | You can configure your prompt colors using any ANSI color escape. Each |
|
210 | 210 | color escape sets the color for any subsequent text, until another escape |
|
211 | 211 | comes in and changes things. The valid color escapes are: |
|
212 | 212 | |
|
213 | 213 | \\C_Black |
|
214 | 214 | \\C_Blue |
|
215 | 215 | \\C_Brown |
|
216 | 216 | \\C_Cyan |
|
217 | 217 | \\C_DarkGray |
|
218 | 218 | \\C_Green |
|
219 | 219 | \\C_LightBlue |
|
220 | 220 | \\C_LightCyan |
|
221 | 221 | \\C_LightGray |
|
222 | 222 | \\C_LightGreen |
|
223 | 223 | \\C_LightPurple |
|
224 | 224 | \\C_LightRed |
|
225 | 225 | \\C_Purple |
|
226 | 226 | \\C_Red |
|
227 | 227 | \\C_White |
|
228 | 228 | \\C_Yellow |
|
229 | 229 | \\C_Normal - Stop coloring, defaults to your terminal settings. |
|
230 | 230 | """ |
|
231 | 231 | pass |
|
232 | 232 | |
|
233 | 233 | # Configure a few things. Much of this is fairly hackish, since IPython |
|
234 | 234 | # doesn't really expose a clean API for it. Be careful if you start making |
|
235 | 235 | # many modifications here. |
|
236 | 236 | |
|
237 | 237 | print """\ |
|
238 | 238 | Welcome to pysh, a set of extensions to IPython for shell usage. |
|
239 | 239 | help(pysh) -> help on the installed shell extensions and syntax. |
|
240 | 240 | """ |
|
241 | 241 | |
|
242 | 242 | # Set the 'cd' command to quiet mode, a more shell-like behavior |
|
243 | 243 | __IPYTHON__.default_option('cd','-q') |
|
244 | 244 | |
|
245 | 245 | # Load all of $PATH as aliases |
|
246 | 246 | if os.name == 'posix': |
|
247 | 247 | # %rehash is very fast, but it doesn't check for executability, it simply |
|
248 | 248 | # dumps everything in $PATH as an alias. Use rehashx if you want more |
|
249 | 249 | # checks. |
|
250 | 250 | __IPYTHON__.magic_rehash() |
|
251 | 251 | else: |
|
252 | 252 | # Windows users: the list of extensions considered executable is read from |
|
253 | 253 | # the environment variable 'pathext'. If this is undefined, IPython |
|
254 | 254 | # defaults to EXE, COM and BAT. |
|
255 | 255 | # %rehashx is the one which does extension analysis, at the cost of |
|
256 | 256 | # being much slower than %rehash. |
|
257 | 257 | __IPYTHON__.magic_rehashx() |
|
258 | 258 | |
|
259 | 259 | # Remove %sc,%sx if present as aliases |
|
260 | 260 | __IPYTHON__.magic_unalias('sc') |
|
261 | 261 | __IPYTHON__.magic_unalias('sx') |
|
262 | 262 | |
|
263 | 263 | # We need different criteria for line-splitting, so that aliases such as |
|
264 | 264 | # 'gnome-terminal' are interpreted as a single alias instead of variable |
|
265 | 265 | # 'gnome' minus variable 'terminal'. |
|
266 | 266 | import re |
|
267 | 267 | __IPYTHON__.line_split = re.compile(r'^([\s*,;/])' |
|
268 | 268 | r'([\?\w\.\-\+]+\w*\s*)' |
|
269 | 269 | r'(\(?.*$)') |
|
270 | 270 | |
|
271 | 271 | # Namespace cleanup |
|
272 | 272 | del re |
@@ -1,91 +1,91 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Modified input prompt for entering text with >>> or ... at the start. |
|
3 | 3 | |
|
4 | 4 | We define a special input line filter to allow typing lines which begin with |
|
5 | 5 | '>>> ' or '... '. These two strings, if present at the start of the input |
|
6 | 6 | line, are stripped. This allows for direct pasting of code from examples such |
|
7 | 7 | as those available in the standard Python tutorial. |
|
8 | 8 | |
|
9 | 9 | Normally pasting such code is one chunk is impossible because of the |
|
10 | 10 | extraneous >>> and ..., requiring one to do a line by line paste with careful |
|
11 | 11 | removal of those characters. This module allows pasting that kind of |
|
12 | 12 | multi-line examples in one pass. |
|
13 | 13 | |
|
14 | 14 | Here is an 'screenshot' of a section of the tutorial pasted into IPython with |
|
15 | 15 | this feature enabled: |
|
16 | 16 | |
|
17 | 17 | In [1]: >>> def fib2(n): # return Fibonacci series up to n |
|
18 | 18 | ...: ... '''Return a list containing the Fibonacci series up to n.''' |
|
19 | 19 | ...: ... result = [] |
|
20 | 20 | ...: ... a, b = 0, 1 |
|
21 | 21 | ...: ... while b < n: |
|
22 | 22 | ...: ... result.append(b) # see below |
|
23 | 23 | ...: ... a, b = b, a+b |
|
24 | 24 | ...: ... return result |
|
25 | 25 | ...: |
|
26 | 26 | |
|
27 | 27 | In [2]: fib2(10) |
|
28 | 28 | Out[2]: [1, 1, 2, 3, 5, 8] |
|
29 | 29 | |
|
30 | 30 | The >>> and ... are stripped from the input so that the python interpreter |
|
31 | 31 | only sees the real part of the code. |
|
32 | 32 | |
|
33 | 33 | All other input is processed normally. |
|
34 | 34 | """ |
|
35 | 35 | #***************************************************************************** |
|
36 |
# Copyright (C) 2001-200 |
|
|
36 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
37 | 37 | # |
|
38 | 38 | # Distributed under the terms of the BSD License. The full license is in |
|
39 | 39 | # the file COPYING, distributed as part of this software. |
|
40 | 40 | #***************************************************************************** |
|
41 | 41 | |
|
42 | 42 | from IPython import Release |
|
43 | 43 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
44 | 44 | __license__ = Release.license |
|
45 | 45 | |
|
46 | 46 | # This file is an example of how to modify IPython's line-processing behavior |
|
47 | 47 | # without touching the internal code. We'll define an alternate pre-processing |
|
48 | 48 | # stage which allows a special form of input (which is invalid Python syntax) |
|
49 | 49 | # for certain quantities, rewrites a line of proper Python in those cases, and |
|
50 | 50 | # then passes it off to IPython's normal processor for further work. |
|
51 | 51 | |
|
52 | 52 | # With this kind of customization, IPython can be adapted for many |
|
53 | 53 | # special-purpose scenarios providing alternate input syntaxes. |
|
54 | 54 | |
|
55 | 55 | # This file can be imported like a regular module. |
|
56 | 56 | |
|
57 | 57 | # IPython has a prefilter() function that analyzes each input line. We redefine |
|
58 | 58 | # it here to first pre-process certain forms of input |
|
59 | 59 | |
|
60 | 60 | # The prototype of any alternate prefilter must be like this one (the name |
|
61 | 61 | # doesn't matter): |
|
62 | 62 | # - line is a string containing the user input line. |
|
63 | 63 | # - continuation is a parameter which tells us if we are processing a first line of |
|
64 | 64 | # user input or the second or higher of a multi-line statement. |
|
65 | 65 | |
|
66 | 66 | def prefilter_paste(self,line,continuation): |
|
67 | 67 | """Alternate prefilter for input of pasted code from an interpreter. |
|
68 | 68 | """ |
|
69 | 69 | |
|
70 | 70 | from re import match |
|
71 | 71 | |
|
72 | 72 | if match(r'^>>> |^\.\.\. ',line): |
|
73 | 73 | # In the end, always call the default IPython _prefilter() function. |
|
74 | 74 | # Note that self must be passed explicitly, b/c we're calling the |
|
75 | 75 | # unbound class method (since this method will overwrite the instance |
|
76 | 76 | # prefilter()) |
|
77 | 77 | return self._prefilter(line[4:],continuation) |
|
78 | 78 | elif line.strip() == '...': |
|
79 | 79 | return self._prefilter('',continuation) |
|
80 | 80 | else: |
|
81 | 81 | return self._prefilter(line,continuation) |
|
82 | 82 | |
|
83 | 83 | # Rebind this to be the new IPython prefilter: |
|
84 | 84 | from IPython.iplib import InteractiveShell |
|
85 | 85 | InteractiveShell.prefilter = prefilter_paste |
|
86 | 86 | |
|
87 | 87 | # Clean up the namespace. |
|
88 | 88 | del InteractiveShell,prefilter_paste |
|
89 | 89 | |
|
90 | 90 | # Just a heads up at the console |
|
91 | 91 | print '*** Pasting of code with ">>>" or "..." has been enabled.' |
@@ -1,233 +1,233 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Logger class for IPython's logging facilities. |
|
4 | 4 | |
|
5 |
$Id: Logger.py 9 |
|
|
5 | $Id: Logger.py 994 2006-01-08 08:29:44Z fperez $ | |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #***************************************************************************** |
|
9 | 9 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
10 |
# Copyright (C) 2001-200 |
|
|
10 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | #**************************************************************************** |
|
17 | 17 | # Modules and globals |
|
18 | 18 | |
|
19 | 19 | from IPython import Release |
|
20 | 20 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
21 | 21 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
22 | 22 | __license__ = Release.license |
|
23 | 23 | |
|
24 | 24 | # Python standard modules |
|
25 | 25 | import glob |
|
26 | 26 | import os |
|
27 | 27 | import time |
|
28 | 28 | |
|
29 | 29 | #**************************************************************************** |
|
30 | 30 | # FIXME: This class isn't a mixin anymore, but it still needs attributes from |
|
31 | 31 | # ipython and does input cache management. Finish cleanup later... |
|
32 | 32 | |
|
33 | 33 | class Logger(object): |
|
34 | 34 | """A Logfile class with different policies for file creation""" |
|
35 | 35 | |
|
36 | 36 | def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'): |
|
37 | 37 | |
|
38 | 38 | self._i00,self._i,self._ii,self._iii = '','','','' |
|
39 | 39 | |
|
40 | 40 | # this is the full ipython instance, we need some attributes from it |
|
41 | 41 | # which won't exist until later. What a mess, clean up later... |
|
42 | 42 | self.shell = shell |
|
43 | 43 | |
|
44 | 44 | self.logfname = logfname |
|
45 | 45 | self.loghead = loghead |
|
46 | 46 | self.logmode = logmode |
|
47 | 47 | self.logfile = None |
|
48 | 48 | |
|
49 | 49 | # whether to also log output |
|
50 | 50 | self.log_output = False |
|
51 | 51 | |
|
52 | 52 | # whether to put timestamps before each log entry |
|
53 | 53 | self.timestamp = False |
|
54 | 54 | |
|
55 | 55 | # activity control flags |
|
56 | 56 | self.log_active = False |
|
57 | 57 | |
|
58 | 58 | # logmode is a validated property |
|
59 | 59 | def _set_mode(self,mode): |
|
60 | 60 | if mode not in ['append','backup','global','over','rotate']: |
|
61 | 61 | raise ValueError,'invalid log mode %s given' % mode |
|
62 | 62 | self._logmode = mode |
|
63 | 63 | |
|
64 | 64 | def _get_mode(self): |
|
65 | 65 | return self._logmode |
|
66 | 66 | |
|
67 | 67 | logmode = property(_get_mode,_set_mode) |
|
68 | 68 | |
|
69 | 69 | def logstart(self,logfname=None,loghead=None,logmode=None, |
|
70 | 70 | log_output=False,timestamp=False): |
|
71 | 71 | """Generate a new log-file with a default header. |
|
72 | 72 | |
|
73 | 73 | Raises RuntimeError if the log has already been started""" |
|
74 | 74 | |
|
75 | 75 | if self.logfile is not None: |
|
76 | 76 | raise RuntimeError('Log file is already active: %s' % |
|
77 | 77 | self.logfname) |
|
78 | 78 | |
|
79 | 79 | self.log_active = True |
|
80 | 80 | |
|
81 | 81 | # The three parameters can override constructor defaults |
|
82 | 82 | if logfname: self.logfname = logfname |
|
83 | 83 | if loghead: self.loghead = loghead |
|
84 | 84 | if logmode: self.logmode = logmode |
|
85 | 85 | self.timestamp = timestamp |
|
86 | 86 | self.log_output = log_output |
|
87 | 87 | |
|
88 | 88 | # init depending on the log mode requested |
|
89 | 89 | isfile = os.path.isfile |
|
90 | 90 | logmode = self.logmode |
|
91 | 91 | |
|
92 | 92 | if logmode == 'append': |
|
93 | 93 | self.logfile = open(self.logfname,'a') |
|
94 | 94 | |
|
95 | 95 | elif logmode == 'backup': |
|
96 | 96 | if isfile(self.logfname): |
|
97 | 97 | backup_logname = self.logfname+'~' |
|
98 | 98 | # Manually remove any old backup, since os.rename may fail |
|
99 | 99 | # under Windows. |
|
100 | 100 | if isfile(backup_logname): |
|
101 | 101 | os.remove(backup_logname) |
|
102 | 102 | os.rename(self.logfname,backup_logname) |
|
103 | 103 | self.logfile = open(self.logfname,'w') |
|
104 | 104 | |
|
105 | 105 | elif logmode == 'global': |
|
106 | 106 | self.logfname = os.path.join(self.shell.home_dir,self.logfname) |
|
107 | 107 | self.logfile = open(self.logfname, 'a') |
|
108 | 108 | |
|
109 | 109 | elif logmode == 'over': |
|
110 | 110 | if isfile(self.logfname): |
|
111 | 111 | os.remove(self.logfname) |
|
112 | 112 | self.logfile = open(self.logfname,'w') |
|
113 | 113 | |
|
114 | 114 | elif logmode == 'rotate': |
|
115 | 115 | if isfile(self.logfname): |
|
116 | 116 | if isfile(self.logfname+'.001~'): |
|
117 | 117 | old = glob.glob(self.logfname+'.*~') |
|
118 | 118 | old.sort() |
|
119 | 119 | old.reverse() |
|
120 | 120 | for f in old: |
|
121 | 121 | root, ext = os.path.splitext(f) |
|
122 | 122 | num = int(ext[1:-1])+1 |
|
123 | 123 | os.rename(f, root+'.'+`num`.zfill(3)+'~') |
|
124 | 124 | os.rename(self.logfname, self.logfname+'.001~') |
|
125 | 125 | self.logfile = open(self.logfname,'w') |
|
126 | 126 | |
|
127 | 127 | if logmode != 'append': |
|
128 | 128 | self.logfile.write(self.loghead) |
|
129 | 129 | |
|
130 | 130 | self.logfile.flush() |
|
131 | 131 | |
|
132 | 132 | def switch_log(self,val): |
|
133 | 133 | """Switch logging on/off. val should be ONLY a boolean.""" |
|
134 | 134 | |
|
135 | 135 | if val not in [False,True,0,1]: |
|
136 | 136 | raise ValueError, \ |
|
137 | 137 | 'Call switch_log ONLY with a boolean argument, not with:',val |
|
138 | 138 | |
|
139 | 139 | label = {0:'OFF',1:'ON',False:'OFF',True:'ON'} |
|
140 | 140 | |
|
141 | 141 | if self.logfile is None: |
|
142 | 142 | print """ |
|
143 | 143 | Logging hasn't been started yet (use logstart for that). |
|
144 | 144 | |
|
145 | 145 | %logon/%logoff are for temporarily starting and stopping logging for a logfile |
|
146 | 146 | which already exists. But you must first start the logging process with |
|
147 | 147 | %logstart (optionally giving a logfile name).""" |
|
148 | 148 | |
|
149 | 149 | else: |
|
150 | 150 | if self.log_active == val: |
|
151 | 151 | print 'Logging is already',label[val] |
|
152 | 152 | else: |
|
153 | 153 | print 'Switching logging',label[val] |
|
154 | 154 | self.log_active = not self.log_active |
|
155 | 155 | self.log_active_out = self.log_active |
|
156 | 156 | |
|
157 | 157 | def logstate(self): |
|
158 | 158 | """Print a status message about the logger.""" |
|
159 | 159 | if self.logfile is None: |
|
160 | 160 | print 'Logging has not been activated.' |
|
161 | 161 | else: |
|
162 | 162 | state = self.log_active and 'active' or 'temporarily suspended' |
|
163 | 163 | print 'Filename :',self.logfname |
|
164 | 164 | print 'Mode :',self.logmode |
|
165 | 165 | print 'Output logging :',self.log_output |
|
166 | 166 | print 'Timestamping :',self.timestamp |
|
167 | 167 | print 'State :',state |
|
168 | 168 | |
|
169 | 169 | def log(self, line,continuation=None): |
|
170 | 170 | """Write the line to a log and create input cache variables _i*.""" |
|
171 | 171 | |
|
172 | 172 | # update the auto _i tables |
|
173 | 173 | #print '***logging line',line # dbg |
|
174 | 174 | #print '***cache_count', self.shell.outputcache.prompt_count # dbg |
|
175 | 175 | try: |
|
176 | 176 | input_hist = self.shell.user_ns['_ih'] |
|
177 | 177 | except: |
|
178 | 178 | print 'userns:',self.shell.user_ns.keys() |
|
179 | 179 | return |
|
180 | 180 | |
|
181 | 181 | if not continuation and line: |
|
182 | 182 | self._iii = self._ii |
|
183 | 183 | self._ii = self._i |
|
184 | 184 | self._i = self._i00 |
|
185 | 185 | # put back the final \n of every input line |
|
186 | 186 | self._i00 = line+'\n' |
|
187 | 187 | #print 'Logging input:<%s>' % line # dbg |
|
188 | 188 | input_hist.append(self._i00) |
|
189 | 189 | #print '---[%s]' % (len(input_hist)-1,) # dbg |
|
190 | 190 | |
|
191 | 191 | # hackish access to top-level namespace to create _i1,_i2... dynamically |
|
192 | 192 | to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii} |
|
193 | 193 | if self.shell.outputcache.do_full_cache: |
|
194 | 194 | in_num = self.shell.outputcache.prompt_count |
|
195 | 195 | # add blank lines if the input cache fell out of sync. This can |
|
196 | 196 | # happen for embedded instances which get killed via C-D and then |
|
197 | 197 | # get resumed. |
|
198 | 198 | while in_num >= len(input_hist): |
|
199 | 199 | input_hist.append('\n') |
|
200 | 200 | # but if the opposite is true (a macro can produce multiple inputs |
|
201 | 201 | # with no output display called), then bring the output counter in |
|
202 | 202 | # sync: |
|
203 | 203 | last_num = len(input_hist)-1 |
|
204 | 204 | if in_num != last_num: |
|
205 | 205 | in_num = self.shell.outputcache.prompt_count = last_num |
|
206 | 206 | new_i = '_i%s' % in_num |
|
207 | 207 | if continuation: |
|
208 | 208 | self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line) |
|
209 | 209 | input_hist[in_num] = self._i00 |
|
210 | 210 | to_main[new_i] = self._i00 |
|
211 | 211 | self.shell.user_ns.update(to_main) |
|
212 | 212 | self.log_write(line) |
|
213 | 213 | |
|
214 | 214 | def log_write(self,data,kind='input'): |
|
215 | 215 | """Write data to the log file, if active""" |
|
216 | 216 | |
|
217 | 217 | if self.log_active and data: |
|
218 | 218 | write = self.logfile.write |
|
219 | 219 | if kind=='input': |
|
220 | 220 | if self.timestamp: |
|
221 | 221 | write(time.strftime('# %a, %d %b %Y %H:%M:%S\n', |
|
222 | 222 | time.localtime())) |
|
223 | 223 | write('%s\n' % data) |
|
224 | 224 | elif kind=='output' and self.log_output: |
|
225 | 225 | odata = '\n'.join(['#[Out]# %s' % s |
|
226 | 226 | for s in data.split('\n')]) |
|
227 | 227 | write('%s\n' % odata) |
|
228 | 228 | self.logfile.flush() |
|
229 | 229 | |
|
230 | 230 | def close_log(self): |
|
231 | 231 | self.logfile.close() |
|
232 | 232 | self.logfile = None |
|
233 | 233 | self.logfname = '' |
@@ -1,2707 +1,2736 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 99 |
|
|
4 | $Id: Magic.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
8 |
# Copyright (C) 2001-200 |
|
|
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | #**************************************************************************** |
|
15 | 15 | # Modules and globals |
|
16 | 16 | |
|
17 | 17 | from IPython import Release |
|
18 | 18 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
19 | 19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | |
|
22 | 22 | # Python standard modules |
|
23 | 23 | import __builtin__ |
|
24 | 24 | import bdb |
|
25 | 25 | import inspect |
|
26 | 26 | import os |
|
27 | 27 | import pdb |
|
28 | 28 | import pydoc |
|
29 | 29 | import sys |
|
30 | 30 | import re |
|
31 | 31 | import tempfile |
|
32 | 32 | import time |
|
33 | 33 | import cPickle as pickle |
|
34 | 34 | from cStringIO import StringIO |
|
35 | 35 | from getopt import getopt |
|
36 | 36 | from pprint import pprint, pformat |
|
37 | 37 | |
|
38 | 38 | # profile isn't bundled by default in Debian for license reasons |
|
39 | 39 | try: |
|
40 | 40 | import profile,pstats |
|
41 | 41 | except ImportError: |
|
42 | 42 | profile = pstats = None |
|
43 | 43 | |
|
44 | 44 | # Homebrewed |
|
45 | 45 | from IPython import Debugger, OInspect, wildcard |
|
46 | 46 | from IPython.FakeModule import FakeModule |
|
47 | 47 | from IPython.Itpl import Itpl, itpl, printpl,itplns |
|
48 | 48 | from IPython.PyColorize import Parser |
|
49 | 49 | from IPython.Struct import Struct |
|
50 | 50 | from IPython.macro import Macro |
|
51 | 51 | from IPython.genutils import * |
|
52 | 52 | |
|
53 | 53 | #*************************************************************************** |
|
54 | 54 | # Utility functions |
|
55 | 55 | def on_off(tag): |
|
56 | 56 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
57 | 57 | return ['OFF','ON'][tag] |
|
58 | 58 | |
|
59 | class Bunch: pass | |
|
59 | 60 | |
|
60 | 61 | #*************************************************************************** |
|
61 | 62 | # Main class implementing Magic functionality |
|
62 | 63 | class Magic: |
|
63 | 64 | """Magic functions for InteractiveShell. |
|
64 | 65 | |
|
65 | 66 | Shell functions which can be reached as %function_name. All magic |
|
66 | 67 | functions should accept a string, which they can parse for their own |
|
67 | 68 | needs. This can make some functions easier to type, eg `%cd ../` |
|
68 | 69 | vs. `%cd("../")` |
|
69 | 70 | |
|
70 | 71 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
71 | 72 | at the command line, but it is is needed in the definition. """ |
|
72 | 73 | |
|
73 | 74 | # class globals |
|
74 | 75 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
75 | 76 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
76 | 77 | |
|
77 | 78 | #...................................................................... |
|
78 | 79 | # some utility functions |
|
79 | 80 | |
|
80 | 81 | def __init__(self,shell): |
|
81 | 82 | |
|
82 | 83 | self.options_table = {} |
|
83 | 84 | if profile is None: |
|
84 | 85 | self.magic_prun = self.profile_missing_notice |
|
85 | 86 | self.shell = shell |
|
86 | 87 | |
|
88 | # namespace for holding state we may need | |
|
89 | self._magic_state = Bunch() | |
|
90 | ||
|
87 | 91 | def profile_missing_notice(self, *args, **kwargs): |
|
88 | 92 | error("""\ |
|
89 | 93 | The profile module could not be found. If you are a Debian user, |
|
90 | 94 | it has been removed from the standard Debian package because of its non-free |
|
91 | 95 | license. To use profiling, please install"python2.3-profiler" from non-free.""") |
|
92 | 96 | |
|
93 | 97 | def default_option(self,fn,optstr): |
|
94 | 98 | """Make an entry in the options_table for fn, with value optstr""" |
|
95 | 99 | |
|
96 | 100 | if fn not in self.lsmagic(): |
|
97 | 101 | error("%s is not a magic function" % fn) |
|
98 | 102 | self.options_table[fn] = optstr |
|
99 | 103 | |
|
100 | 104 | def lsmagic(self): |
|
101 | 105 | """Return a list of currently available magic functions. |
|
102 | 106 | |
|
103 | 107 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
104 | 108 | ['magic_ls','magic_cd',...]""" |
|
105 | 109 | |
|
106 | 110 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
107 | 111 | |
|
108 | 112 | # magics in class definition |
|
109 | 113 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
110 | 114 | callable(Magic.__dict__[fn]) |
|
111 | 115 | # in instance namespace (run-time user additions) |
|
112 | 116 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
113 | 117 | callable(self.__dict__[fn]) |
|
114 | 118 | # and bound magics by user (so they can access self): |
|
115 | 119 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
116 | 120 | callable(self.__class__.__dict__[fn]) |
|
117 | 121 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
118 | 122 | filter(inst_magic,self.__dict__.keys()) + \ |
|
119 | 123 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
120 | 124 | out = [] |
|
121 | 125 | for fn in magics: |
|
122 | 126 | out.append(fn.replace('magic_','',1)) |
|
123 | 127 | out.sort() |
|
124 | 128 | return out |
|
125 | 129 | |
|
126 | 130 | def extract_input_slices(self,slices): |
|
127 | 131 | """Return as a string a set of input history slices. |
|
128 | 132 | |
|
129 | 133 | The set of slices is given as a list of strings (like ['1','4:8','9'], |
|
130 | 134 | since this function is for use by magic functions which get their |
|
131 | 135 | arguments as strings. |
|
132 | 136 | |
|
133 | 137 | Note that slices can be called with two notations: |
|
134 | 138 | |
|
135 | 139 | N:M -> standard python form, means including items N...(M-1). |
|
136 | 140 | |
|
137 | 141 | N-M -> include items N..M (closed endpoint).""" |
|
138 | 142 | |
|
139 | 143 | cmds = [] |
|
140 | 144 | for chunk in slices: |
|
141 | 145 | if ':' in chunk: |
|
142 | 146 | ini,fin = map(int,chunk.split(':')) |
|
143 | 147 | elif '-' in chunk: |
|
144 | 148 | ini,fin = map(int,chunk.split('-')) |
|
145 | 149 | fin += 1 |
|
146 | 150 | else: |
|
147 | 151 | ini = int(chunk) |
|
148 | 152 | fin = ini+1 |
|
149 | 153 | cmds.append(self.shell.input_hist[ini:fin]) |
|
150 | 154 | return cmds |
|
151 | 155 | |
|
152 | 156 | def _ofind(self,oname): |
|
153 | 157 | """Find an object in the available namespaces. |
|
154 | 158 | |
|
155 | 159 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
156 | 160 | |
|
157 | 161 | Has special code to detect magic functions. |
|
158 | 162 | """ |
|
159 | 163 | |
|
160 | 164 | oname = oname.strip() |
|
161 | 165 | |
|
162 | 166 | # Namespaces to search in: |
|
163 | 167 | user_ns = self.shell.user_ns |
|
164 | 168 | internal_ns = self.shell.internal_ns |
|
165 | 169 | builtin_ns = __builtin__.__dict__ |
|
166 | 170 | alias_ns = self.shell.alias_table |
|
167 | 171 | |
|
168 | 172 | # Put them in a list. The order is important so that we find things in |
|
169 | 173 | # the same order that Python finds them. |
|
170 | 174 | namespaces = [ ('Interactive',user_ns), |
|
171 | 175 | ('IPython internal',internal_ns), |
|
172 | 176 | ('Python builtin',builtin_ns), |
|
173 | 177 | ('Alias',alias_ns), |
|
174 | 178 | ] |
|
175 | 179 | |
|
176 | 180 | # initialize results to 'null' |
|
177 | 181 | found = 0; obj = None; ospace = None; ds = None; |
|
178 | 182 | ismagic = 0; isalias = 0 |
|
179 | 183 | |
|
180 | 184 | # Look for the given name by splitting it in parts. If the head is |
|
181 | 185 | # found, then we look for all the remaining parts as members, and only |
|
182 | 186 | # declare success if we can find them all. |
|
183 | 187 | oname_parts = oname.split('.') |
|
184 | 188 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
185 | 189 | for nsname,ns in namespaces: |
|
186 | 190 | try: |
|
187 | 191 | obj = ns[oname_head] |
|
188 | 192 | except KeyError: |
|
189 | 193 | continue |
|
190 | 194 | else: |
|
191 | 195 | for part in oname_rest: |
|
192 | 196 | try: |
|
193 | 197 | obj = getattr(obj,part) |
|
194 | 198 | except: |
|
195 | 199 | # Blanket except b/c some badly implemented objects |
|
196 | 200 | # allow __getattr__ to raise exceptions other than |
|
197 | 201 | # AttributeError, which then crashes IPython. |
|
198 | 202 | break |
|
199 | 203 | else: |
|
200 | 204 | # If we finish the for loop (no break), we got all members |
|
201 | 205 | found = 1 |
|
202 | 206 | ospace = nsname |
|
203 | 207 | if ns == alias_ns: |
|
204 | 208 | isalias = 1 |
|
205 | 209 | break # namespace loop |
|
206 | 210 | |
|
207 | 211 | # Try to see if it's magic |
|
208 | 212 | if not found: |
|
209 | 213 | if oname.startswith(self.shell.ESC_MAGIC): |
|
210 | 214 | oname = oname[1:] |
|
211 | 215 | obj = getattr(self,'magic_'+oname,None) |
|
212 | 216 | if obj is not None: |
|
213 | 217 | found = 1 |
|
214 | 218 | ospace = 'IPython internal' |
|
215 | 219 | ismagic = 1 |
|
216 | 220 | |
|
217 | 221 | # Last try: special-case some literals like '', [], {}, etc: |
|
218 | 222 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
219 | 223 | obj = eval(oname_head) |
|
220 | 224 | found = 1 |
|
221 | 225 | ospace = 'Interactive' |
|
222 | 226 | |
|
223 | 227 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
224 | 228 | 'ismagic':ismagic, 'isalias':isalias} |
|
225 | 229 | |
|
226 | 230 | def arg_err(self,func): |
|
227 | 231 | """Print docstring if incorrect arguments were passed""" |
|
228 | 232 | print 'Error in arguments:' |
|
229 | 233 | print OInspect.getdoc(func) |
|
230 | 234 | |
|
231 | 235 | def format_latex(self,strng): |
|
232 | 236 | """Format a string for latex inclusion.""" |
|
233 | 237 | |
|
234 | 238 | # Characters that need to be escaped for latex: |
|
235 | 239 | escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE) |
|
236 | 240 | # Magic command names as headers: |
|
237 | 241 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, |
|
238 | 242 | re.MULTILINE) |
|
239 | 243 | # Magic commands |
|
240 | 244 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, |
|
241 | 245 | re.MULTILINE) |
|
242 | 246 | # Paragraph continue |
|
243 | 247 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
244 | 248 | |
|
245 | 249 | # The "\n" symbol |
|
246 | 250 | newline_re = re.compile(r'\\n') |
|
247 | 251 | |
|
248 | 252 | # Now build the string for output: |
|
249 | 253 | strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng) |
|
250 | 254 | strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng) |
|
251 | 255 | strng = par_re.sub(r'\\\\',strng) |
|
252 | 256 | strng = escape_re.sub(r'\\\1',strng) |
|
253 | 257 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
254 | 258 | return strng |
|
255 | 259 | |
|
256 | 260 | def format_screen(self,strng): |
|
257 | 261 | """Format a string for screen printing. |
|
258 | 262 | |
|
259 | 263 | This removes some latex-type format codes.""" |
|
260 | 264 | # Paragraph continue |
|
261 | 265 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
262 | 266 | strng = par_re.sub('',strng) |
|
263 | 267 | return strng |
|
264 | 268 | |
|
265 | 269 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
266 | 270 | """Parse options passed to an argument string. |
|
267 | 271 | |
|
268 | 272 | The interface is similar to that of getopt(), but it returns back a |
|
269 | 273 | Struct with the options as keys and the stripped argument string still |
|
270 | 274 | as a string. |
|
271 | 275 | |
|
272 | 276 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
273 | 277 | This allows us to easily expand variables, glob files, quote |
|
274 | 278 | arguments, etc. |
|
275 | 279 | |
|
276 | 280 | Options: |
|
277 | 281 | -mode: default 'string'. If given as 'list', the argument string is |
|
278 | 282 | returned as a list (split on whitespace) instead of a string. |
|
279 | 283 | |
|
280 | 284 | -list_all: put all option values in lists. Normally only options |
|
281 | 285 | appearing more than once are put in a list.""" |
|
282 | 286 | |
|
283 | 287 | # inject default options at the beginning of the input line |
|
284 | 288 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
285 | 289 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
286 | 290 | |
|
287 | 291 | mode = kw.get('mode','string') |
|
288 | 292 | if mode not in ['string','list']: |
|
289 | 293 | raise ValueError,'incorrect mode given: %s' % mode |
|
290 | 294 | # Get options |
|
291 | 295 | list_all = kw.get('list_all',0) |
|
292 | 296 | |
|
293 | 297 | # Check if we have more than one argument to warrant extra processing: |
|
294 | 298 | odict = {} # Dictionary with options |
|
295 | 299 | args = arg_str.split() |
|
296 | 300 | if len(args) >= 1: |
|
297 | 301 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
298 | 302 | # need to look for options |
|
299 | 303 | argv = shlex_split(arg_str) |
|
300 | 304 | # Do regular option processing |
|
301 | 305 | opts,args = getopt(argv,opt_str,*long_opts) |
|
302 | 306 | for o,a in opts: |
|
303 | 307 | if o.startswith('--'): |
|
304 | 308 | o = o[2:] |
|
305 | 309 | else: |
|
306 | 310 | o = o[1:] |
|
307 | 311 | try: |
|
308 | 312 | odict[o].append(a) |
|
309 | 313 | except AttributeError: |
|
310 | 314 | odict[o] = [odict[o],a] |
|
311 | 315 | except KeyError: |
|
312 | 316 | if list_all: |
|
313 | 317 | odict[o] = [a] |
|
314 | 318 | else: |
|
315 | 319 | odict[o] = a |
|
316 | 320 | |
|
317 | 321 | # Prepare opts,args for return |
|
318 | 322 | opts = Struct(odict) |
|
319 | 323 | if mode == 'string': |
|
320 | 324 | args = ' '.join(args) |
|
321 | 325 | |
|
322 | 326 | return opts,args |
|
323 | 327 | |
|
324 | 328 | #...................................................................... |
|
325 | 329 | # And now the actual magic functions |
|
326 | 330 | |
|
327 | 331 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
328 | 332 | def magic_lsmagic(self, parameter_s = ''): |
|
329 | 333 | """List currently available magic functions.""" |
|
330 | 334 | mesc = self.shell.ESC_MAGIC |
|
331 | 335 | print 'Available magic functions:\n'+mesc+\ |
|
332 | 336 | (' '+mesc).join(self.lsmagic()) |
|
333 | 337 | print '\n' + Magic.auto_status[self.shell.rc.automagic] |
|
334 | 338 | return None |
|
335 | 339 | |
|
336 | 340 | def magic_magic(self, parameter_s = ''): |
|
337 | 341 | """Print information about the magic function system.""" |
|
338 | 342 | |
|
339 | 343 | mode = '' |
|
340 | 344 | try: |
|
341 | 345 | if parameter_s.split()[0] == '-latex': |
|
342 | 346 | mode = 'latex' |
|
343 | 347 | except: |
|
344 | 348 | pass |
|
345 | 349 | |
|
346 | 350 | magic_docs = [] |
|
347 | 351 | for fname in self.lsmagic(): |
|
348 | 352 | mname = 'magic_' + fname |
|
349 | 353 | for space in (Magic,self,self.__class__): |
|
350 | 354 | try: |
|
351 | 355 | fn = space.__dict__[mname] |
|
352 | 356 | except KeyError: |
|
353 | 357 | pass |
|
354 | 358 | else: |
|
355 | 359 | break |
|
356 | 360 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, |
|
357 | 361 | fname,fn.__doc__)) |
|
358 | 362 | magic_docs = ''.join(magic_docs) |
|
359 | 363 | |
|
360 | 364 | if mode == 'latex': |
|
361 | 365 | print self.format_latex(magic_docs) |
|
362 | 366 | return |
|
363 | 367 | else: |
|
364 | 368 | magic_docs = self.format_screen(magic_docs) |
|
365 | 369 | |
|
366 | 370 | outmsg = """ |
|
367 | 371 | IPython's 'magic' functions |
|
368 | 372 | =========================== |
|
369 | 373 | |
|
370 | 374 | The magic function system provides a series of functions which allow you to |
|
371 | 375 | control the behavior of IPython itself, plus a lot of system-type |
|
372 | 376 | features. All these functions are prefixed with a % character, but parameters |
|
373 | 377 | are given without parentheses or quotes. |
|
374 | 378 | |
|
375 | 379 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
376 | 380 | %automagic function), you don't need to type in the % explicitly. By default, |
|
377 | 381 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
378 | 382 | |
|
379 | 383 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
380 | 384 | to 'mydir', if it exists. |
|
381 | 385 | |
|
382 | 386 | You can define your own magic functions to extend the system. See the supplied |
|
383 | 387 | ipythonrc and example-magic.py files for details (in your ipython |
|
384 | 388 | configuration directory, typically $HOME/.ipython/). |
|
385 | 389 | |
|
386 | 390 | You can also define your own aliased names for magic functions. In your |
|
387 | 391 | ipythonrc file, placing a line like: |
|
388 | 392 | |
|
389 | 393 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
390 | 394 | |
|
391 | 395 | will define %pf as a new name for %profile. |
|
392 | 396 | |
|
393 | 397 | You can also call magics in code using the ipmagic() function, which IPython |
|
394 | 398 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. |
|
395 | 399 | |
|
396 | 400 | For a list of the available magic functions, use %lsmagic. For a description |
|
397 | 401 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
398 | 402 | |
|
399 | 403 | Currently the magic system has the following functions:\n""" |
|
400 | 404 | |
|
401 | 405 | mesc = self.shell.ESC_MAGIC |
|
402 | 406 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
403 | 407 | "\n\n%s%s\n\n%s" % (outmsg, |
|
404 | 408 | magic_docs,mesc,mesc, |
|
405 | 409 | (' '+mesc).join(self.lsmagic()), |
|
406 | 410 | Magic.auto_status[self.shell.rc.automagic] ) ) |
|
407 | 411 | |
|
408 | 412 | page(outmsg,screen_lines=self.shell.rc.screen_length) |
|
409 | 413 | |
|
410 | 414 | def magic_automagic(self, parameter_s = ''): |
|
411 | 415 | """Make magic functions callable without having to type the initial %. |
|
412 | 416 | |
|
413 | 417 | Toggles on/off (when off, you must call it as %automagic, of |
|
414 | 418 | course). Note that magic functions have lowest priority, so if there's |
|
415 | 419 | a variable whose name collides with that of a magic fn, automagic |
|
416 | 420 | won't work for that function (you get the variable instead). However, |
|
417 | 421 | if you delete the variable (del var), the previously shadowed magic |
|
418 | 422 | function becomes visible to automagic again.""" |
|
419 | 423 | |
|
420 | 424 | rc = self.shell.rc |
|
421 | 425 | rc.automagic = not rc.automagic |
|
422 | 426 | print '\n' + Magic.auto_status[rc.automagic] |
|
423 | 427 | |
|
424 | 428 | def magic_autocall(self, parameter_s = ''): |
|
425 | 429 | """Make functions callable without having to type parentheses. |
|
426 | 430 | |
|
427 | This cycles the autocall command line through its three valid values | |
|
428 | (0->Off, 1->Smart, 2->Full)""" | |
|
431 | Usage: | |
|
432 | ||
|
433 | %autocall [mode] | |
|
434 | ||
|
435 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the | |
|
436 | value is toggled on and off (remembering the previous state).""" | |
|
429 | 437 | |
|
430 | 438 | rc = self.shell.rc |
|
431 | rc.autocall = not rc.autocall | |
|
439 | ||
|
440 | if parameter_s: | |
|
441 | arg = int(parameter_s) | |
|
442 | else: | |
|
443 | arg = 'toggle' | |
|
444 | ||
|
445 | if not arg in (0,1,2,'toggle'): | |
|
446 | error('Valid modes: (0->Off, 1->Smart, 2->Full') | |
|
447 | return | |
|
448 | ||
|
449 | if arg in (0,1,2): | |
|
450 | rc.autocall = arg | |
|
451 | else: # toggle | |
|
452 | if rc.autocall: | |
|
453 | self._magic_state.autocall_save = rc.autocall | |
|
454 | rc.autocall = 0 | |
|
455 | else: | |
|
456 | try: | |
|
457 | rc.autocall = self._magic_state.autocall_save | |
|
458 | except AttributeError: | |
|
459 | rc.autocall = self._magic_state.autocall_save = 1 | |
|
460 | ||
|
432 | 461 | print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall] |
|
433 | 462 | |
|
434 | 463 | def magic_autoindent(self, parameter_s = ''): |
|
435 | 464 | """Toggle autoindent on/off (if available).""" |
|
436 | 465 | |
|
437 | 466 | self.shell.set_autoindent() |
|
438 | 467 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
439 | 468 | |
|
440 | 469 | def magic_system_verbose(self, parameter_s = ''): |
|
441 | 470 | """Toggle verbose printing of system calls on/off.""" |
|
442 | 471 | |
|
443 | 472 | self.shell.rc_set_toggle('system_verbose') |
|
444 | 473 | print "System verbose printing is:",\ |
|
445 | 474 | ['OFF','ON'][self.shell.rc.system_verbose] |
|
446 | 475 | |
|
447 | 476 | def magic_history(self, parameter_s = ''): |
|
448 | 477 | """Print input history (_i<n> variables), with most recent last. |
|
449 | 478 | |
|
450 | 479 | %history [-n] -> print at most 40 inputs (some may be multi-line)\\ |
|
451 | 480 | %history [-n] n -> print at most n inputs\\ |
|
452 | 481 | %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
453 | 482 | |
|
454 | 483 | Each input's number <n> is shown, and is accessible as the |
|
455 | 484 | automatically generated variable _i<n>. Multi-line statements are |
|
456 | 485 | printed starting at a new line for easy copy/paste. |
|
457 | 486 | |
|
458 | 487 | If option -n is used, input numbers are not printed. This is useful if |
|
459 | 488 | you want to get a printout of many lines which can be directly pasted |
|
460 | 489 | into a text editor. |
|
461 | 490 | |
|
462 | 491 | This feature is only available if numbered prompts are in use.""" |
|
463 | 492 | |
|
464 | 493 | shell = self.shell |
|
465 | 494 | if not shell.outputcache.do_full_cache: |
|
466 | 495 | print 'This feature is only available if numbered prompts are in use.' |
|
467 | 496 | return |
|
468 | 497 | opts,args = self.parse_options(parameter_s,'n',mode='list') |
|
469 | 498 | |
|
470 | 499 | input_hist = shell.input_hist |
|
471 | 500 | default_length = 40 |
|
472 | 501 | if len(args) == 0: |
|
473 | 502 | final = len(input_hist) |
|
474 | 503 | init = max(1,final-default_length) |
|
475 | 504 | elif len(args) == 1: |
|
476 | 505 | final = len(input_hist) |
|
477 | 506 | init = max(1,final-int(args[0])) |
|
478 | 507 | elif len(args) == 2: |
|
479 | 508 | init,final = map(int,args) |
|
480 | 509 | else: |
|
481 | 510 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
482 | 511 | print self.magic_hist.__doc__ |
|
483 | 512 | return |
|
484 | 513 | width = len(str(final)) |
|
485 | 514 | line_sep = ['','\n'] |
|
486 | 515 | print_nums = not opts.has_key('n') |
|
487 | 516 | for in_num in range(init,final): |
|
488 | 517 | inline = input_hist[in_num] |
|
489 | 518 | multiline = int(inline.count('\n') > 1) |
|
490 | 519 | if print_nums: |
|
491 | 520 | print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]), |
|
492 | 521 | print inline, |
|
493 | 522 | |
|
494 | 523 | def magic_hist(self, parameter_s=''): |
|
495 | 524 | """Alternate name for %history.""" |
|
496 | 525 | return self.magic_history(parameter_s) |
|
497 | 526 | |
|
498 | 527 | def magic_p(self, parameter_s=''): |
|
499 | 528 | """Just a short alias for Python's 'print'.""" |
|
500 | 529 | exec 'print ' + parameter_s in self.shell.user_ns |
|
501 | 530 | |
|
502 | 531 | def magic_r(self, parameter_s=''): |
|
503 | 532 | """Repeat previous input. |
|
504 | 533 | |
|
505 | 534 | If given an argument, repeats the previous command which starts with |
|
506 | 535 | the same string, otherwise it just repeats the previous input. |
|
507 | 536 | |
|
508 | 537 | Shell escaped commands (with ! as first character) are not recognized |
|
509 | 538 | by this system, only pure python code and magic commands. |
|
510 | 539 | """ |
|
511 | 540 | |
|
512 | 541 | start = parameter_s.strip() |
|
513 | 542 | esc_magic = self.shell.ESC_MAGIC |
|
514 | 543 | # Identify magic commands even if automagic is on (which means |
|
515 | 544 | # the in-memory version is different from that typed by the user). |
|
516 | 545 | if self.shell.rc.automagic: |
|
517 | 546 | start_magic = esc_magic+start |
|
518 | 547 | else: |
|
519 | 548 | start_magic = start |
|
520 | 549 | # Look through the input history in reverse |
|
521 | 550 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
522 | 551 | input = self.shell.input_hist[n] |
|
523 | 552 | # skip plain 'r' lines so we don't recurse to infinity |
|
524 | 553 | if input != 'ipmagic("r")\n' and \ |
|
525 | 554 | (input.startswith(start) or input.startswith(start_magic)): |
|
526 | 555 | #print 'match',`input` # dbg |
|
527 | 556 | print 'Executing:',input, |
|
528 | 557 | self.shell.runlines(input) |
|
529 | 558 | return |
|
530 | 559 | print 'No previous input matching `%s` found.' % start |
|
531 | 560 | |
|
532 | 561 | def magic_page(self, parameter_s=''): |
|
533 | 562 | """Pretty print the object and display it through a pager. |
|
534 | 563 | |
|
535 | 564 | If no parameter is given, use _ (last output).""" |
|
536 | 565 | # After a function contributed by Olivier Aubert, slightly modified. |
|
537 | 566 | |
|
538 | 567 | oname = parameter_s and parameter_s or '_' |
|
539 | 568 | info = self._ofind(oname) |
|
540 | 569 | if info['found']: |
|
541 | 570 | page(pformat(info['obj'])) |
|
542 | 571 | else: |
|
543 | 572 | print 'Object `%s` not found' % oname |
|
544 | 573 | |
|
545 | 574 | def magic_profile(self, parameter_s=''): |
|
546 | 575 | """Print your currently active IPyhton profile.""" |
|
547 | 576 | if self.shell.rc.profile: |
|
548 | 577 | printpl('Current IPython profile: $self.shell.rc.profile.') |
|
549 | 578 | else: |
|
550 | 579 | print 'No profile active.' |
|
551 | 580 | |
|
552 | 581 | def _inspect(self,meth,oname,**kw): |
|
553 | 582 | """Generic interface to the inspector system. |
|
554 | 583 | |
|
555 | 584 | This function is meant to be called by pdef, pdoc & friends.""" |
|
556 | 585 | |
|
557 | 586 | oname = oname.strip() |
|
558 | 587 | info = Struct(self._ofind(oname)) |
|
559 | 588 | if info.found: |
|
560 | 589 | pmethod = getattr(self.shell.inspector,meth) |
|
561 | 590 | formatter = info.ismagic and self.format_screen or None |
|
562 | 591 | if meth == 'pdoc': |
|
563 | 592 | pmethod(info.obj,oname,formatter) |
|
564 | 593 | elif meth == 'pinfo': |
|
565 | 594 | pmethod(info.obj,oname,formatter,info,**kw) |
|
566 | 595 | else: |
|
567 | 596 | pmethod(info.obj,oname) |
|
568 | 597 | else: |
|
569 | 598 | print 'Object `%s` not found.' % oname |
|
570 | 599 | return 'not found' # so callers can take other action |
|
571 | 600 | |
|
572 | 601 | def magic_pdef(self, parameter_s=''): |
|
573 | 602 | """Print the definition header for any callable object. |
|
574 | 603 | |
|
575 | 604 | If the object is a class, print the constructor information.""" |
|
576 | 605 | self._inspect('pdef',parameter_s) |
|
577 | 606 | |
|
578 | 607 | def magic_pdoc(self, parameter_s=''): |
|
579 | 608 | """Print the docstring for an object. |
|
580 | 609 | |
|
581 | 610 | If the given object is a class, it will print both the class and the |
|
582 | 611 | constructor docstrings.""" |
|
583 | 612 | self._inspect('pdoc',parameter_s) |
|
584 | 613 | |
|
585 | 614 | def magic_psource(self, parameter_s=''): |
|
586 | 615 | """Print (or run through pager) the source code for an object.""" |
|
587 | 616 | self._inspect('psource',parameter_s) |
|
588 | 617 | |
|
589 | 618 | def magic_pfile(self, parameter_s=''): |
|
590 | 619 | """Print (or run through pager) the file where an object is defined. |
|
591 | 620 | |
|
592 | 621 | The file opens at the line where the object definition begins. IPython |
|
593 | 622 | will honor the environment variable PAGER if set, and otherwise will |
|
594 | 623 | do its best to print the file in a convenient form. |
|
595 | 624 | |
|
596 | 625 | If the given argument is not an object currently defined, IPython will |
|
597 | 626 | try to interpret it as a filename (automatically adding a .py extension |
|
598 | 627 | if needed). You can thus use %pfile as a syntax highlighting code |
|
599 | 628 | viewer.""" |
|
600 | 629 | |
|
601 | 630 | # first interpret argument as an object name |
|
602 | 631 | out = self._inspect('pfile',parameter_s) |
|
603 | 632 | # if not, try the input as a filename |
|
604 | 633 | if out == 'not found': |
|
605 | 634 | try: |
|
606 | 635 | filename = get_py_filename(parameter_s) |
|
607 | 636 | except IOError,msg: |
|
608 | 637 | print msg |
|
609 | 638 | return |
|
610 | 639 | page(self.shell.inspector.format(file(filename).read())) |
|
611 | 640 | |
|
612 | 641 | def magic_pinfo(self, parameter_s=''): |
|
613 | 642 | """Provide detailed information about an object. |
|
614 | 643 | |
|
615 | 644 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
616 | 645 | |
|
617 | 646 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
618 | 647 | |
|
619 | 648 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
620 | 649 | detail_level = 0 |
|
621 | 650 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
622 | 651 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
623 | 652 | pinfo,qmark1,oname,qmark2 = \ |
|
624 | 653 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
625 | 654 | if pinfo or qmark1 or qmark2: |
|
626 | 655 | detail_level = 1 |
|
627 | 656 | if "*" in oname: |
|
628 | 657 | self.magic_psearch(oname) |
|
629 | 658 | else: |
|
630 | 659 | self._inspect('pinfo',oname,detail_level=detail_level) |
|
631 | 660 | |
|
632 | 661 | def magic_psearch(self, parameter_s=''): |
|
633 | 662 | """Search for object in namespaces by wildcard. |
|
634 | 663 | |
|
635 | 664 | %psearch [options] PATTERN [OBJECT TYPE] |
|
636 | 665 | |
|
637 | 666 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
638 | 667 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
639 | 668 | rest of the command line must be unchanged (options come first), so |
|
640 | 669 | for example the following forms are equivalent |
|
641 | 670 | |
|
642 | 671 | %psearch -i a* function |
|
643 | 672 | -i a* function? |
|
644 | 673 | ?-i a* function |
|
645 | 674 | |
|
646 | 675 | Arguments: |
|
647 | 676 | |
|
648 | 677 | PATTERN |
|
649 | 678 | |
|
650 | 679 | where PATTERN is a string containing * as a wildcard similar to its |
|
651 | 680 | use in a shell. The pattern is matched in all namespaces on the |
|
652 | 681 | search path. By default objects starting with a single _ are not |
|
653 | 682 | matched, many IPython generated objects have a single |
|
654 | 683 | underscore. The default is case insensitive matching. Matching is |
|
655 | 684 | also done on the attributes of objects and not only on the objects |
|
656 | 685 | in a module. |
|
657 | 686 | |
|
658 | 687 | [OBJECT TYPE] |
|
659 | 688 | |
|
660 | 689 | Is the name of a python type from the types module. The name is |
|
661 | 690 | given in lowercase without the ending type, ex. StringType is |
|
662 | 691 | written string. By adding a type here only objects matching the |
|
663 | 692 | given type are matched. Using all here makes the pattern match all |
|
664 | 693 | types (this is the default). |
|
665 | 694 | |
|
666 | 695 | Options: |
|
667 | 696 | |
|
668 | 697 | -a: makes the pattern match even objects whose names start with a |
|
669 | 698 | single underscore. These names are normally ommitted from the |
|
670 | 699 | search. |
|
671 | 700 | |
|
672 | 701 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
673 | 702 | these options is given, the default is read from your ipythonrc |
|
674 | 703 | file. The option name which sets this value is |
|
675 | 704 | 'wildcards_case_sensitive'. If this option is not specified in your |
|
676 | 705 | ipythonrc file, IPython's internal default is to do a case sensitive |
|
677 | 706 | search. |
|
678 | 707 | |
|
679 | 708 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
680 | 709 | specifiy can be searched in any of the following namespaces: |
|
681 | 710 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
682 | 711 | 'builtin' and 'user' are the search defaults. Note that you should |
|
683 | 712 | not use quotes when specifying namespaces. |
|
684 | 713 | |
|
685 | 714 | 'Builtin' contains the python module builtin, 'user' contains all |
|
686 | 715 | user data, 'alias' only contain the shell aliases and no python |
|
687 | 716 | objects, 'internal' contains objects used by IPython. The |
|
688 | 717 | 'user_global' namespace is only used by embedded IPython instances, |
|
689 | 718 | and it contains module-level globals. You can add namespaces to the |
|
690 | 719 | search with -s or exclude them with -e (these options can be given |
|
691 | 720 | more than once). |
|
692 | 721 | |
|
693 | 722 | Examples: |
|
694 | 723 | |
|
695 | 724 | %psearch a* -> objects beginning with an a |
|
696 | 725 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
697 | 726 | %psearch a* function -> all functions beginning with an a |
|
698 | 727 | %psearch re.e* -> objects beginning with an e in module re |
|
699 | 728 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
700 | 729 | %psearch r*.* string -> all strings in modules beginning with r |
|
701 | 730 | |
|
702 | 731 | Case sensitve search: |
|
703 | 732 | |
|
704 | 733 | %psearch -c a* list all object beginning with lower case a |
|
705 | 734 | |
|
706 | 735 | Show objects beginning with a single _: |
|
707 | 736 | |
|
708 | 737 | %psearch -a _* list objects beginning with a single underscore""" |
|
709 | 738 | |
|
710 | 739 | # default namespaces to be searched |
|
711 | 740 | def_search = ['user','builtin'] |
|
712 | 741 | |
|
713 | 742 | # Process options/args |
|
714 | 743 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
715 | 744 | opt = opts.get |
|
716 | 745 | shell = self.shell |
|
717 | 746 | psearch = shell.inspector.psearch |
|
718 | 747 | |
|
719 | 748 | # select case options |
|
720 | 749 | if opts.has_key('i'): |
|
721 | 750 | ignore_case = True |
|
722 | 751 | elif opts.has_key('c'): |
|
723 | 752 | ignore_case = False |
|
724 | 753 | else: |
|
725 | 754 | ignore_case = not shell.rc.wildcards_case_sensitive |
|
726 | 755 | |
|
727 | 756 | # Build list of namespaces to search from user options |
|
728 | 757 | def_search.extend(opt('s',[])) |
|
729 | 758 | ns_exclude = ns_exclude=opt('e',[]) |
|
730 | 759 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
731 | 760 | |
|
732 | 761 | # Call the actual search |
|
733 | 762 | try: |
|
734 | 763 | psearch(args,shell.ns_table,ns_search, |
|
735 | 764 | show_all=opt('a'),ignore_case=ignore_case) |
|
736 | 765 | except: |
|
737 | 766 | shell.showtraceback() |
|
738 | 767 | |
|
739 | 768 | def magic_who_ls(self, parameter_s=''): |
|
740 | 769 | """Return a sorted list of all interactive variables. |
|
741 | 770 | |
|
742 | 771 | If arguments are given, only variables of types matching these |
|
743 | 772 | arguments are returned.""" |
|
744 | 773 | |
|
745 | 774 | user_ns = self.shell.user_ns |
|
746 | 775 | internal_ns = self.shell.internal_ns |
|
747 | 776 | user_config_ns = self.shell.user_config_ns |
|
748 | 777 | out = [] |
|
749 | 778 | typelist = parameter_s.split() |
|
750 | 779 | |
|
751 | 780 | for i in user_ns: |
|
752 | 781 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
753 | 782 | and not (i in internal_ns or i in user_config_ns): |
|
754 | 783 | if typelist: |
|
755 | 784 | if type(user_ns[i]).__name__ in typelist: |
|
756 | 785 | out.append(i) |
|
757 | 786 | else: |
|
758 | 787 | out.append(i) |
|
759 | 788 | out.sort() |
|
760 | 789 | return out |
|
761 | 790 | |
|
762 | 791 | def magic_who(self, parameter_s=''): |
|
763 | 792 | """Print all interactive variables, with some minimal formatting. |
|
764 | 793 | |
|
765 | 794 | If any arguments are given, only variables whose type matches one of |
|
766 | 795 | these are printed. For example: |
|
767 | 796 | |
|
768 | 797 | %who function str |
|
769 | 798 | |
|
770 | 799 | will only list functions and strings, excluding all other types of |
|
771 | 800 | variables. To find the proper type names, simply use type(var) at a |
|
772 | 801 | command line to see how python prints type names. For example: |
|
773 | 802 | |
|
774 | 803 | In [1]: type('hello')\\ |
|
775 | 804 | Out[1]: <type 'str'> |
|
776 | 805 | |
|
777 | 806 | indicates that the type name for strings is 'str'. |
|
778 | 807 | |
|
779 | 808 | %who always excludes executed names loaded through your configuration |
|
780 | 809 | file and things which are internal to IPython. |
|
781 | 810 | |
|
782 | 811 | This is deliberate, as typically you may load many modules and the |
|
783 | 812 | purpose of %who is to show you only what you've manually defined.""" |
|
784 | 813 | |
|
785 | 814 | varlist = self.magic_who_ls(parameter_s) |
|
786 | 815 | if not varlist: |
|
787 | 816 | print 'Interactive namespace is empty.' |
|
788 | 817 | return |
|
789 | 818 | |
|
790 | 819 | # if we have variables, move on... |
|
791 | 820 | |
|
792 | 821 | # stupid flushing problem: when prompts have no separators, stdout is |
|
793 | 822 | # getting lost. I'm starting to think this is a python bug. I'm having |
|
794 | 823 | # to force a flush with a print because even a sys.stdout.flush |
|
795 | 824 | # doesn't seem to do anything! |
|
796 | 825 | |
|
797 | 826 | count = 0 |
|
798 | 827 | for i in varlist: |
|
799 | 828 | print i+'\t', |
|
800 | 829 | count += 1 |
|
801 | 830 | if count > 8: |
|
802 | 831 | count = 0 |
|
803 | 832 | |
|
804 | 833 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? |
|
805 | 834 | |
|
806 | 835 | print # well, this does force a flush at the expense of an extra \n |
|
807 | 836 | |
|
808 | 837 | def magic_whos(self, parameter_s=''): |
|
809 | 838 | """Like %who, but gives some extra information about each variable. |
|
810 | 839 | |
|
811 | 840 | The same type filtering of %who can be applied here. |
|
812 | 841 | |
|
813 | 842 | For all variables, the type is printed. Additionally it prints: |
|
814 | 843 | |
|
815 | 844 | - For {},[],(): their length. |
|
816 | 845 | |
|
817 | 846 | - For Numeric arrays, a summary with shape, number of elements, |
|
818 | 847 | typecode and size in memory. |
|
819 | 848 | |
|
820 | 849 | - Everything else: a string representation, snipping their middle if |
|
821 | 850 | too long.""" |
|
822 | 851 | |
|
823 | 852 | varnames = self.magic_who_ls(parameter_s) |
|
824 | 853 | if not varnames: |
|
825 | 854 | print 'Interactive namespace is empty.' |
|
826 | 855 | return |
|
827 | 856 | |
|
828 | 857 | # if we have variables, move on... |
|
829 | 858 | |
|
830 | 859 | # for these types, show len() instead of data: |
|
831 | 860 | seq_types = [types.DictType,types.ListType,types.TupleType] |
|
832 | 861 | |
|
833 | 862 | # for Numeric arrays, display summary info |
|
834 | 863 | try: |
|
835 | 864 | import Numeric |
|
836 | 865 | except ImportError: |
|
837 | 866 | array_type = None |
|
838 | 867 | else: |
|
839 | 868 | array_type = Numeric.ArrayType.__name__ |
|
840 | 869 | |
|
841 | 870 | # Find all variable names and types so we can figure out column sizes |
|
842 | 871 | get_vars = lambda i: self.shell.user_ns[i] |
|
843 | 872 | type_name = lambda v: type(v).__name__ |
|
844 | 873 | varlist = map(get_vars,varnames) |
|
845 | 874 | |
|
846 | 875 | typelist = [] |
|
847 | 876 | for vv in varlist: |
|
848 | 877 | tt = type_name(vv) |
|
849 | 878 | if tt=='instance': |
|
850 | 879 | typelist.append(str(vv.__class__)) |
|
851 | 880 | else: |
|
852 | 881 | typelist.append(tt) |
|
853 | 882 | |
|
854 | 883 | # column labels and # of spaces as separator |
|
855 | 884 | varlabel = 'Variable' |
|
856 | 885 | typelabel = 'Type' |
|
857 | 886 | datalabel = 'Data/Info' |
|
858 | 887 | colsep = 3 |
|
859 | 888 | # variable format strings |
|
860 | 889 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" |
|
861 | 890 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' |
|
862 | 891 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
863 | 892 | # find the size of the columns to format the output nicely |
|
864 | 893 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
865 | 894 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
866 | 895 | # table header |
|
867 | 896 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
868 | 897 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
869 | 898 | # and the table itself |
|
870 | 899 | kb = 1024 |
|
871 | 900 | Mb = 1048576 # kb**2 |
|
872 | 901 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
873 | 902 | print itpl(vformat), |
|
874 | 903 | if vtype in seq_types: |
|
875 | 904 | print len(var) |
|
876 | 905 | elif vtype==array_type: |
|
877 | 906 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
878 | 907 | vsize = Numeric.size(var) |
|
879 | 908 | vbytes = vsize*var.itemsize() |
|
880 | 909 | if vbytes < 100000: |
|
881 | 910 | print aformat % (vshape,vsize,var.typecode(),vbytes) |
|
882 | 911 | else: |
|
883 | 912 | print aformat % (vshape,vsize,var.typecode(),vbytes), |
|
884 | 913 | if vbytes < Mb: |
|
885 | 914 | print '(%s kb)' % (vbytes/kb,) |
|
886 | 915 | else: |
|
887 | 916 | print '(%s Mb)' % (vbytes/Mb,) |
|
888 | 917 | else: |
|
889 | 918 | vstr = str(var).replace('\n','\\n') |
|
890 | 919 | if len(vstr) < 50: |
|
891 | 920 | print vstr |
|
892 | 921 | else: |
|
893 | 922 | printpl(vfmt_short) |
|
894 | 923 | |
|
895 | 924 | def magic_reset(self, parameter_s=''): |
|
896 | 925 | """Resets the namespace by removing all names defined by the user. |
|
897 | 926 | |
|
898 | 927 | Input/Output history are left around in case you need them.""" |
|
899 | 928 | |
|
900 | 929 | ans = raw_input( |
|
901 | 930 | "Once deleted, variables cannot be recovered. Proceed (y/n)? ") |
|
902 | 931 | if not ans.lower() == 'y': |
|
903 | 932 | print 'Nothing done.' |
|
904 | 933 | return |
|
905 | 934 | user_ns = self.shell.user_ns |
|
906 | 935 | for i in self.magic_who_ls(): |
|
907 | 936 | del(user_ns[i]) |
|
908 | 937 | |
|
909 | 938 | def magic_config(self,parameter_s=''): |
|
910 | 939 | """Show IPython's internal configuration.""" |
|
911 | 940 | |
|
912 | 941 | page('Current configuration structure:\n'+ |
|
913 | 942 | pformat(self.shell.rc.dict())) |
|
914 | 943 | |
|
915 | 944 | def magic_logstart(self,parameter_s=''): |
|
916 | 945 | """Start logging anywhere in a session. |
|
917 | 946 | |
|
918 | 947 | %logstart [-o|-t] [log_name [log_mode]] |
|
919 | 948 | |
|
920 | 949 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
921 | 950 | current directory, in 'rotate' mode (see below). |
|
922 | 951 | |
|
923 | 952 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
924 | 953 | history up to that point and then continues logging. |
|
925 | 954 | |
|
926 | 955 | %logstart takes a second optional parameter: logging mode. This can be one |
|
927 | 956 | of (note that the modes are given unquoted):\\ |
|
928 | 957 | append: well, that says it.\\ |
|
929 | 958 | backup: rename (if exists) to name~ and start name.\\ |
|
930 | 959 | global: single logfile in your home dir, appended to.\\ |
|
931 | 960 | over : overwrite existing log.\\ |
|
932 | 961 | rotate: create rotating logs name.1~, name.2~, etc. |
|
933 | 962 | |
|
934 | 963 | Options: |
|
935 | 964 | |
|
936 | 965 | -o: log also IPython's output. In this mode, all commands which |
|
937 | 966 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
938 | 967 | their corresponding input line. The output lines are always |
|
939 | 968 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
940 | 969 | Python code. |
|
941 | 970 | |
|
942 | 971 | Since this marker is always the same, filtering only the output from |
|
943 | 972 | a log is very easy, using for example a simple awk call: |
|
944 | 973 | |
|
945 | 974 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
946 | 975 | |
|
947 | 976 | -t: put timestamps before each input line logged (these are put in |
|
948 | 977 | comments).""" |
|
949 | 978 | |
|
950 | 979 | opts,par = self.parse_options(parameter_s,'ot') |
|
951 | 980 | log_output = 'o' in opts |
|
952 | 981 | timestamp = 't' in opts |
|
953 | 982 | |
|
954 | 983 | rc = self.shell.rc |
|
955 | 984 | logger = self.shell.logger |
|
956 | 985 | |
|
957 | 986 | # if no args are given, the defaults set in the logger constructor by |
|
958 | 987 | # ipytohn remain valid |
|
959 | 988 | if par: |
|
960 | 989 | try: |
|
961 | 990 | logfname,logmode = par.split() |
|
962 | 991 | except: |
|
963 | 992 | logfname = par |
|
964 | 993 | logmode = 'backup' |
|
965 | 994 | else: |
|
966 | 995 | logfname = logger.logfname |
|
967 | 996 | logmode = logger.logmode |
|
968 | 997 | # put logfname into rc struct as if it had been called on the command |
|
969 | 998 | # line, so it ends up saved in the log header Save it in case we need |
|
970 | 999 | # to restore it... |
|
971 | 1000 | old_logfile = rc.opts.get('logfile','') |
|
972 | 1001 | if logfname: |
|
973 | 1002 | logfname = os.path.expanduser(logfname) |
|
974 | 1003 | rc.opts.logfile = logfname |
|
975 | 1004 | loghead = self.shell.loghead_tpl % (rc.opts,rc.args) |
|
976 | 1005 | try: |
|
977 | 1006 | started = logger.logstart(logfname,loghead,logmode, |
|
978 | 1007 | log_output,timestamp) |
|
979 | 1008 | except: |
|
980 | 1009 | rc.opts.logfile = old_logfile |
|
981 | 1010 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
982 | 1011 | else: |
|
983 | 1012 | # log input history up to this point, optionally interleaving |
|
984 | 1013 | # output if requested |
|
985 | 1014 | |
|
986 | 1015 | if timestamp: |
|
987 | 1016 | # disable timestamping for the previous history, since we've |
|
988 | 1017 | # lost those already (no time machine here). |
|
989 | 1018 | logger.timestamp = False |
|
990 | 1019 | if log_output: |
|
991 | 1020 | log_write = logger.log_write |
|
992 | 1021 | input_hist = self.shell.input_hist |
|
993 | 1022 | output_hist = self.shell.output_hist |
|
994 | 1023 | for n in range(1,len(input_hist)-1): |
|
995 | 1024 | log_write(input_hist[n].rstrip()) |
|
996 | 1025 | if n in output_hist: |
|
997 | 1026 | log_write(repr(output_hist[n]),'output') |
|
998 | 1027 | else: |
|
999 | 1028 | logger.log_write(self.shell.input_hist[1:]) |
|
1000 | 1029 | if timestamp: |
|
1001 | 1030 | # re-enable timestamping |
|
1002 | 1031 | logger.timestamp = True |
|
1003 | 1032 | |
|
1004 | 1033 | print ('Activating auto-logging. ' |
|
1005 | 1034 | 'Current session state plus future input saved.') |
|
1006 | 1035 | logger.logstate() |
|
1007 | 1036 | |
|
1008 | 1037 | def magic_logoff(self,parameter_s=''): |
|
1009 | 1038 | """Temporarily stop logging. |
|
1010 | 1039 | |
|
1011 | 1040 | You must have previously started logging.""" |
|
1012 | 1041 | self.shell.logger.switch_log(0) |
|
1013 | 1042 | |
|
1014 | 1043 | def magic_logon(self,parameter_s=''): |
|
1015 | 1044 | """Restart logging. |
|
1016 | 1045 | |
|
1017 | 1046 | This function is for restarting logging which you've temporarily |
|
1018 | 1047 | stopped with %logoff. For starting logging for the first time, you |
|
1019 | 1048 | must use the %logstart function, which allows you to specify an |
|
1020 | 1049 | optional log filename.""" |
|
1021 | 1050 | |
|
1022 | 1051 | self.shell.logger.switch_log(1) |
|
1023 | 1052 | |
|
1024 | 1053 | def magic_logstate(self,parameter_s=''): |
|
1025 | 1054 | """Print the status of the logging system.""" |
|
1026 | 1055 | |
|
1027 | 1056 | self.shell.logger.logstate() |
|
1028 | 1057 | |
|
1029 | 1058 | def magic_pdb(self, parameter_s=''): |
|
1030 | 1059 | """Control the calling of the pdb interactive debugger. |
|
1031 | 1060 | |
|
1032 | 1061 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
1033 | 1062 | argument it works as a toggle. |
|
1034 | 1063 | |
|
1035 | 1064 | When an exception is triggered, IPython can optionally call the |
|
1036 | 1065 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
1037 | 1066 | this feature on and off.""" |
|
1038 | 1067 | |
|
1039 | 1068 | par = parameter_s.strip().lower() |
|
1040 | 1069 | |
|
1041 | 1070 | if par: |
|
1042 | 1071 | try: |
|
1043 | 1072 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
1044 | 1073 | except KeyError: |
|
1045 | 1074 | print ('Incorrect argument. Use on/1, off/0, ' |
|
1046 | 1075 | 'or nothing for a toggle.') |
|
1047 | 1076 | return |
|
1048 | 1077 | else: |
|
1049 | 1078 | # toggle |
|
1050 | 1079 | new_pdb = not self.shell.InteractiveTB.call_pdb |
|
1051 | 1080 | |
|
1052 | 1081 | # set on the shell |
|
1053 | 1082 | self.shell.call_pdb = new_pdb |
|
1054 | 1083 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
1055 | 1084 | |
|
1056 | 1085 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
1057 | 1086 | opts=None,arg_lst=None,prog_ns=None): |
|
1058 | 1087 | |
|
1059 | 1088 | """Run a statement through the python code profiler. |
|
1060 | 1089 | |
|
1061 | 1090 | Usage:\\ |
|
1062 | 1091 | %prun [options] statement |
|
1063 | 1092 | |
|
1064 | 1093 | The given statement (which doesn't require quote marks) is run via the |
|
1065 | 1094 | python profiler in a manner similar to the profile.run() function. |
|
1066 | 1095 | Namespaces are internally managed to work correctly; profile.run |
|
1067 | 1096 | cannot be used in IPython because it makes certain assumptions about |
|
1068 | 1097 | namespaces which do not hold under IPython. |
|
1069 | 1098 | |
|
1070 | 1099 | Options: |
|
1071 | 1100 | |
|
1072 | 1101 | -l <limit>: you can place restrictions on what or how much of the |
|
1073 | 1102 | profile gets printed. The limit value can be: |
|
1074 | 1103 | |
|
1075 | 1104 | * A string: only information for function names containing this string |
|
1076 | 1105 | is printed. |
|
1077 | 1106 | |
|
1078 | 1107 | * An integer: only these many lines are printed. |
|
1079 | 1108 | |
|
1080 | 1109 | * A float (between 0 and 1): this fraction of the report is printed |
|
1081 | 1110 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
1082 | 1111 | |
|
1083 | 1112 | You can combine several limits with repeated use of the option. For |
|
1084 | 1113 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
1085 | 1114 | information about class constructors. |
|
1086 | 1115 | |
|
1087 | 1116 | -r: return the pstats.Stats object generated by the profiling. This |
|
1088 | 1117 | object has all the information about the profile in it, and you can |
|
1089 | 1118 | later use it for further analysis or in other functions. |
|
1090 | 1119 | |
|
1091 | 1120 | Since magic functions have a particular form of calling which prevents |
|
1092 | 1121 | you from writing something like:\\ |
|
1093 | 1122 | In [1]: p = %prun -r print 4 # invalid!\\ |
|
1094 | 1123 | you must instead use IPython's automatic variables to assign this:\\ |
|
1095 | 1124 | In [1]: %prun -r print 4 \\ |
|
1096 | 1125 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ |
|
1097 | 1126 | In [2]: stats = _ |
|
1098 | 1127 | |
|
1099 | 1128 | If you really need to assign this value via an explicit function call, |
|
1100 | 1129 | you can always tap directly into the true name of the magic function |
|
1101 | 1130 | by using the ipmagic function (which IPython automatically adds to the |
|
1102 | 1131 | builtins):\\ |
|
1103 | 1132 | In [3]: stats = ipmagic('prun','-r print 4') |
|
1104 | 1133 | |
|
1105 | 1134 | You can type ipmagic? for more details on ipmagic. |
|
1106 | 1135 | |
|
1107 | 1136 | -s <key>: sort profile by given key. You can provide more than one key |
|
1108 | 1137 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
1109 | 1138 | default sorting key is 'time'. |
|
1110 | 1139 | |
|
1111 | 1140 | The following is copied verbatim from the profile documentation |
|
1112 | 1141 | referenced below: |
|
1113 | 1142 | |
|
1114 | 1143 | When more than one key is provided, additional keys are used as |
|
1115 | 1144 | secondary criteria when the there is equality in all keys selected |
|
1116 | 1145 | before them. |
|
1117 | 1146 | |
|
1118 | 1147 | Abbreviations can be used for any key names, as long as the |
|
1119 | 1148 | abbreviation is unambiguous. The following are the keys currently |
|
1120 | 1149 | defined: |
|
1121 | 1150 | |
|
1122 | 1151 | Valid Arg Meaning\\ |
|
1123 | 1152 | "calls" call count\\ |
|
1124 | 1153 | "cumulative" cumulative time\\ |
|
1125 | 1154 | "file" file name\\ |
|
1126 | 1155 | "module" file name\\ |
|
1127 | 1156 | "pcalls" primitive call count\\ |
|
1128 | 1157 | "line" line number\\ |
|
1129 | 1158 | "name" function name\\ |
|
1130 | 1159 | "nfl" name/file/line\\ |
|
1131 | 1160 | "stdname" standard name\\ |
|
1132 | 1161 | "time" internal time |
|
1133 | 1162 | |
|
1134 | 1163 | Note that all sorts on statistics are in descending order (placing |
|
1135 | 1164 | most time consuming items first), where as name, file, and line number |
|
1136 | 1165 | searches are in ascending order (i.e., alphabetical). The subtle |
|
1137 | 1166 | distinction between "nfl" and "stdname" is that the standard name is a |
|
1138 | 1167 | sort of the name as printed, which means that the embedded line |
|
1139 | 1168 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
1140 | 1169 | would (if the file names were the same) appear in the string order |
|
1141 | 1170 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
1142 | 1171 | line numbers. In fact, sort_stats("nfl") is the same as |
|
1143 | 1172 | sort_stats("name", "file", "line"). |
|
1144 | 1173 | |
|
1145 | 1174 | -T <filename>: save profile results as shown on screen to a text |
|
1146 | 1175 | file. The profile is still shown on screen. |
|
1147 | 1176 | |
|
1148 | 1177 | -D <filename>: save (via dump_stats) profile statistics to given |
|
1149 | 1178 | filename. This data is in a format understod by the pstats module, and |
|
1150 | 1179 | is generated by a call to the dump_stats() method of profile |
|
1151 | 1180 | objects. The profile is still shown on screen. |
|
1152 | 1181 | |
|
1153 | 1182 | If you want to run complete programs under the profiler's control, use |
|
1154 | 1183 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
1155 | 1184 | contains profiler specific options as described here. |
|
1156 | 1185 | |
|
1157 | 1186 | You can read the complete documentation for the profile module with:\\ |
|
1158 | 1187 | In [1]: import profile; profile.help() """ |
|
1159 | 1188 | |
|
1160 | 1189 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
1161 | 1190 | # protect user quote marks |
|
1162 | 1191 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") |
|
1163 | 1192 | |
|
1164 | 1193 | if user_mode: # regular user call |
|
1165 | 1194 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', |
|
1166 | 1195 | list_all=1) |
|
1167 | 1196 | namespace = self.shell.user_ns |
|
1168 | 1197 | else: # called to run a program by %run -p |
|
1169 | 1198 | try: |
|
1170 | 1199 | filename = get_py_filename(arg_lst[0]) |
|
1171 | 1200 | except IOError,msg: |
|
1172 | 1201 | error(msg) |
|
1173 | 1202 | return |
|
1174 | 1203 | |
|
1175 | 1204 | arg_str = 'execfile(filename,prog_ns)' |
|
1176 | 1205 | namespace = locals() |
|
1177 | 1206 | |
|
1178 | 1207 | opts.merge(opts_def) |
|
1179 | 1208 | |
|
1180 | 1209 | prof = profile.Profile() |
|
1181 | 1210 | try: |
|
1182 | 1211 | prof = prof.runctx(arg_str,namespace,namespace) |
|
1183 | 1212 | sys_exit = '' |
|
1184 | 1213 | except SystemExit: |
|
1185 | 1214 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
1186 | 1215 | |
|
1187 | 1216 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
1188 | 1217 | |
|
1189 | 1218 | lims = opts.l |
|
1190 | 1219 | if lims: |
|
1191 | 1220 | lims = [] # rebuild lims with ints/floats/strings |
|
1192 | 1221 | for lim in opts.l: |
|
1193 | 1222 | try: |
|
1194 | 1223 | lims.append(int(lim)) |
|
1195 | 1224 | except ValueError: |
|
1196 | 1225 | try: |
|
1197 | 1226 | lims.append(float(lim)) |
|
1198 | 1227 | except ValueError: |
|
1199 | 1228 | lims.append(lim) |
|
1200 | 1229 | |
|
1201 | 1230 | # trap output |
|
1202 | 1231 | sys_stdout = sys.stdout |
|
1203 | 1232 | stdout_trap = StringIO() |
|
1204 | 1233 | try: |
|
1205 | 1234 | sys.stdout = stdout_trap |
|
1206 | 1235 | stats.print_stats(*lims) |
|
1207 | 1236 | finally: |
|
1208 | 1237 | sys.stdout = sys_stdout |
|
1209 | 1238 | output = stdout_trap.getvalue() |
|
1210 | 1239 | output = output.rstrip() |
|
1211 | 1240 | |
|
1212 | 1241 | page(output,screen_lines=self.shell.rc.screen_length) |
|
1213 | 1242 | print sys_exit, |
|
1214 | 1243 | |
|
1215 | 1244 | dump_file = opts.D[0] |
|
1216 | 1245 | text_file = opts.T[0] |
|
1217 | 1246 | if dump_file: |
|
1218 | 1247 | prof.dump_stats(dump_file) |
|
1219 | 1248 | print '\n*** Profile stats marshalled to file',\ |
|
1220 | 1249 | `dump_file`+'.',sys_exit |
|
1221 | 1250 | if text_file: |
|
1222 | 1251 | file(text_file,'w').write(output) |
|
1223 | 1252 | print '\n*** Profile printout saved to text file',\ |
|
1224 | 1253 | `text_file`+'.',sys_exit |
|
1225 | 1254 | |
|
1226 | 1255 | if opts.has_key('r'): |
|
1227 | 1256 | return stats |
|
1228 | 1257 | else: |
|
1229 | 1258 | return None |
|
1230 | 1259 | |
|
1231 | 1260 | def magic_run(self, parameter_s ='',runner=None): |
|
1232 | 1261 | """Run the named file inside IPython as a program. |
|
1233 | 1262 | |
|
1234 | 1263 | Usage:\\ |
|
1235 | 1264 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
1236 | 1265 | |
|
1237 | 1266 | Parameters after the filename are passed as command-line arguments to |
|
1238 | 1267 | the program (put in sys.argv). Then, control returns to IPython's |
|
1239 | 1268 | prompt. |
|
1240 | 1269 | |
|
1241 | 1270 | This is similar to running at a system prompt:\\ |
|
1242 | 1271 | $ python file args\\ |
|
1243 | 1272 | but with the advantage of giving you IPython's tracebacks, and of |
|
1244 | 1273 | loading all variables into your interactive namespace for further use |
|
1245 | 1274 | (unless -p is used, see below). |
|
1246 | 1275 | |
|
1247 | 1276 | The file is executed in a namespace initially consisting only of |
|
1248 | 1277 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1249 | 1278 | sees its environment as if it were being run as a stand-alone |
|
1250 | 1279 | program. But after execution, the IPython interactive namespace gets |
|
1251 | 1280 | updated with all variables defined in the program (except for __name__ |
|
1252 | 1281 | and sys.argv). This allows for very convenient loading of code for |
|
1253 | 1282 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1254 | 1283 | |
|
1255 | 1284 | Options: |
|
1256 | 1285 | |
|
1257 | 1286 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
1258 | 1287 | without extension (as python does under import). This allows running |
|
1259 | 1288 | scripts and reloading the definitions in them without calling code |
|
1260 | 1289 | protected by an ' if __name__ == "__main__" ' clause. |
|
1261 | 1290 | |
|
1262 | 1291 | -i: run the file in IPython's namespace instead of an empty one. This |
|
1263 | 1292 | is useful if you are experimenting with code written in a text editor |
|
1264 | 1293 | which depends on variables defined interactively. |
|
1265 | 1294 | |
|
1266 | 1295 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
1267 | 1296 | being run. This is particularly useful if IPython is being used to |
|
1268 | 1297 | run unittests, which always exit with a sys.exit() call. In such |
|
1269 | 1298 | cases you are interested in the output of the test results, not in |
|
1270 | 1299 | seeing a traceback of the unittest module. |
|
1271 | 1300 | |
|
1272 | 1301 | -t: print timing information at the end of the run. IPython will give |
|
1273 | 1302 | you an estimated CPU time consumption for your script, which under |
|
1274 | 1303 | Unix uses the resource module to avoid the wraparound problems of |
|
1275 | 1304 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
1276 | 1305 | is also given (for Windows platforms this is reported as 0.0). |
|
1277 | 1306 | |
|
1278 | 1307 | If -t is given, an additional -N<N> option can be given, where <N> |
|
1279 | 1308 | must be an integer indicating how many times you want the script to |
|
1280 | 1309 | run. The final timing report will include total and per run results. |
|
1281 | 1310 | |
|
1282 | 1311 | For example (testing the script uniq_stable.py): |
|
1283 | 1312 | |
|
1284 | 1313 | In [1]: run -t uniq_stable |
|
1285 | 1314 | |
|
1286 | 1315 | IPython CPU timings (estimated):\\ |
|
1287 | 1316 | User : 0.19597 s.\\ |
|
1288 | 1317 | System: 0.0 s.\\ |
|
1289 | 1318 | |
|
1290 | 1319 | In [2]: run -t -N5 uniq_stable |
|
1291 | 1320 | |
|
1292 | 1321 | IPython CPU timings (estimated):\\ |
|
1293 | 1322 | Total runs performed: 5\\ |
|
1294 | 1323 | Times : Total Per run\\ |
|
1295 | 1324 | User : 0.910862 s, 0.1821724 s.\\ |
|
1296 | 1325 | System: 0.0 s, 0.0 s. |
|
1297 | 1326 | |
|
1298 | 1327 | -d: run your program under the control of pdb, the Python debugger. |
|
1299 | 1328 | This allows you to execute your program step by step, watch variables, |
|
1300 | 1329 | etc. Internally, what IPython does is similar to calling: |
|
1301 | 1330 | |
|
1302 | 1331 | pdb.run('execfile("YOURFILENAME")') |
|
1303 | 1332 | |
|
1304 | 1333 | with a breakpoint set on line 1 of your file. You can change the line |
|
1305 | 1334 | number for this automatic breakpoint to be <N> by using the -bN option |
|
1306 | 1335 | (where N must be an integer). For example: |
|
1307 | 1336 | |
|
1308 | 1337 | %run -d -b40 myscript |
|
1309 | 1338 | |
|
1310 | 1339 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
1311 | 1340 | the first breakpoint must be set on a line which actually does |
|
1312 | 1341 | something (not a comment or docstring) for it to stop execution. |
|
1313 | 1342 | |
|
1314 | 1343 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
1315 | 1344 | first enter 'c' (without qoutes) to start execution up to the first |
|
1316 | 1345 | breakpoint. |
|
1317 | 1346 | |
|
1318 | 1347 | Entering 'help' gives information about the use of the debugger. You |
|
1319 | 1348 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
1320 | 1349 | at a prompt. |
|
1321 | 1350 | |
|
1322 | 1351 | -p: run program under the control of the Python profiler module (which |
|
1323 | 1352 | prints a detailed report of execution times, function calls, etc). |
|
1324 | 1353 | |
|
1325 | 1354 | You can pass other options after -p which affect the behavior of the |
|
1326 | 1355 | profiler itself. See the docs for %prun for details. |
|
1327 | 1356 | |
|
1328 | 1357 | In this mode, the program's variables do NOT propagate back to the |
|
1329 | 1358 | IPython interactive namespace (because they remain in the namespace |
|
1330 | 1359 | where the profiler executes them). |
|
1331 | 1360 | |
|
1332 | 1361 | Internally this triggers a call to %prun, see its documentation for |
|
1333 | 1362 | details on the options available specifically for profiling.""" |
|
1334 | 1363 | |
|
1335 | 1364 | # get arguments and set sys.argv for program to be run. |
|
1336 | 1365 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', |
|
1337 | 1366 | mode='list',list_all=1) |
|
1338 | 1367 | |
|
1339 | 1368 | try: |
|
1340 | 1369 | filename = get_py_filename(arg_lst[0]) |
|
1341 | 1370 | except IndexError: |
|
1342 | 1371 | warn('you must provide at least a filename.') |
|
1343 | 1372 | print '\n%run:\n',OInspect.getdoc(self.magic_run) |
|
1344 | 1373 | return |
|
1345 | 1374 | except IOError,msg: |
|
1346 | 1375 | error(msg) |
|
1347 | 1376 | return |
|
1348 | 1377 | |
|
1349 | 1378 | # Control the response to exit() calls made by the script being run |
|
1350 | 1379 | exit_ignore = opts.has_key('e') |
|
1351 | 1380 | |
|
1352 | 1381 | # Make sure that the running script gets a proper sys.argv as if it |
|
1353 | 1382 | # were run from a system shell. |
|
1354 | 1383 | save_argv = sys.argv # save it for later restoring |
|
1355 | 1384 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename |
|
1356 | 1385 | |
|
1357 | 1386 | if opts.has_key('i'): |
|
1358 | 1387 | prog_ns = self.shell.user_ns |
|
1359 | 1388 | __name__save = self.shell.user_ns['__name__'] |
|
1360 | 1389 | prog_ns['__name__'] = '__main__' |
|
1361 | 1390 | else: |
|
1362 | 1391 | if opts.has_key('n'): |
|
1363 | 1392 | name = os.path.splitext(os.path.basename(filename))[0] |
|
1364 | 1393 | else: |
|
1365 | 1394 | name = '__main__' |
|
1366 | 1395 | prog_ns = {'__name__':name} |
|
1367 | 1396 | |
|
1368 | 1397 | # pickle fix. See iplib for an explanation. But we need to make sure |
|
1369 | 1398 | # that, if we overwrite __main__, we replace it at the end |
|
1370 | 1399 | if prog_ns['__name__'] == '__main__': |
|
1371 | 1400 | restore_main = sys.modules['__main__'] |
|
1372 | 1401 | else: |
|
1373 | 1402 | restore_main = False |
|
1374 | 1403 | |
|
1375 | 1404 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) |
|
1376 | 1405 | |
|
1377 | 1406 | stats = None |
|
1378 | 1407 | try: |
|
1379 | 1408 | if opts.has_key('p'): |
|
1380 | 1409 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
|
1381 | 1410 | else: |
|
1382 | 1411 | if opts.has_key('d'): |
|
1383 | 1412 | deb = Debugger.Pdb(self.shell.rc.colors) |
|
1384 | 1413 | # reset Breakpoint state, which is moronically kept |
|
1385 | 1414 | # in a class |
|
1386 | 1415 | bdb.Breakpoint.next = 1 |
|
1387 | 1416 | bdb.Breakpoint.bplist = {} |
|
1388 | 1417 | bdb.Breakpoint.bpbynumber = [None] |
|
1389 | 1418 | # Set an initial breakpoint to stop execution |
|
1390 | 1419 | maxtries = 10 |
|
1391 | 1420 | bp = int(opts.get('b',[1])[0]) |
|
1392 | 1421 | checkline = deb.checkline(filename,bp) |
|
1393 | 1422 | if not checkline: |
|
1394 | 1423 | for bp in range(bp+1,bp+maxtries+1): |
|
1395 | 1424 | if deb.checkline(filename,bp): |
|
1396 | 1425 | break |
|
1397 | 1426 | else: |
|
1398 | 1427 | msg = ("\nI failed to find a valid line to set " |
|
1399 | 1428 | "a breakpoint\n" |
|
1400 | 1429 | "after trying up to line: %s.\n" |
|
1401 | 1430 | "Please set a valid breakpoint manually " |
|
1402 | 1431 | "with the -b option." % bp) |
|
1403 | 1432 | error(msg) |
|
1404 | 1433 | return |
|
1405 | 1434 | # if we find a good linenumber, set the breakpoint |
|
1406 | 1435 | deb.do_break('%s:%s' % (filename,bp)) |
|
1407 | 1436 | # Start file run |
|
1408 | 1437 | print "NOTE: Enter 'c' at the", |
|
1409 | 1438 | print "ipdb> prompt to start your script." |
|
1410 | 1439 | try: |
|
1411 | 1440 | deb.run('execfile("%s")' % filename,prog_ns) |
|
1412 | 1441 | except: |
|
1413 | 1442 | etype, value, tb = sys.exc_info() |
|
1414 | 1443 | # Skip three frames in the traceback: the %run one, |
|
1415 | 1444 | # one inside bdb.py, and the command-line typed by the |
|
1416 | 1445 | # user (run by exec in pdb itself). |
|
1417 | 1446 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) |
|
1418 | 1447 | else: |
|
1419 | 1448 | if runner is None: |
|
1420 | 1449 | runner = self.shell.safe_execfile |
|
1421 | 1450 | if opts.has_key('t'): |
|
1422 | 1451 | try: |
|
1423 | 1452 | nruns = int(opts['N'][0]) |
|
1424 | 1453 | if nruns < 1: |
|
1425 | 1454 | error('Number of runs must be >=1') |
|
1426 | 1455 | return |
|
1427 | 1456 | except (KeyError): |
|
1428 | 1457 | nruns = 1 |
|
1429 | 1458 | if nruns == 1: |
|
1430 | 1459 | t0 = clock2() |
|
1431 | 1460 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1432 | 1461 | t1 = clock2() |
|
1433 | 1462 | t_usr = t1[0]-t0[0] |
|
1434 | 1463 | t_sys = t1[1]-t1[1] |
|
1435 | 1464 | print "\nIPython CPU timings (estimated):" |
|
1436 | 1465 | print " User : %10s s." % t_usr |
|
1437 | 1466 | print " System: %10s s." % t_sys |
|
1438 | 1467 | else: |
|
1439 | 1468 | runs = range(nruns) |
|
1440 | 1469 | t0 = clock2() |
|
1441 | 1470 | for nr in runs: |
|
1442 | 1471 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1443 | 1472 | t1 = clock2() |
|
1444 | 1473 | t_usr = t1[0]-t0[0] |
|
1445 | 1474 | t_sys = t1[1]-t1[1] |
|
1446 | 1475 | print "\nIPython CPU timings (estimated):" |
|
1447 | 1476 | print "Total runs performed:",nruns |
|
1448 | 1477 | print " Times : %10s %10s" % ('Total','Per run') |
|
1449 | 1478 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) |
|
1450 | 1479 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) |
|
1451 | 1480 | |
|
1452 | 1481 | else: |
|
1453 | 1482 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1454 | 1483 | if opts.has_key('i'): |
|
1455 | 1484 | self.shell.user_ns['__name__'] = __name__save |
|
1456 | 1485 | else: |
|
1457 | 1486 | # update IPython interactive namespace |
|
1458 | 1487 | del prog_ns['__name__'] |
|
1459 | 1488 | self.shell.user_ns.update(prog_ns) |
|
1460 | 1489 | finally: |
|
1461 | 1490 | sys.argv = save_argv |
|
1462 | 1491 | if restore_main: |
|
1463 | 1492 | sys.modules['__main__'] = restore_main |
|
1464 | 1493 | return stats |
|
1465 | 1494 | |
|
1466 | 1495 | def magic_runlog(self, parameter_s =''): |
|
1467 | 1496 | """Run files as logs. |
|
1468 | 1497 | |
|
1469 | 1498 | Usage:\\ |
|
1470 | 1499 | %runlog file1 file2 ... |
|
1471 | 1500 | |
|
1472 | 1501 | Run the named files (treating them as log files) in sequence inside |
|
1473 | 1502 | the interpreter, and return to the prompt. This is much slower than |
|
1474 | 1503 | %run because each line is executed in a try/except block, but it |
|
1475 | 1504 | allows running files with syntax errors in them. |
|
1476 | 1505 | |
|
1477 | 1506 | Normally IPython will guess when a file is one of its own logfiles, so |
|
1478 | 1507 | you can typically use %run even for logs. This shorthand allows you to |
|
1479 | 1508 | force any file to be treated as a log file.""" |
|
1480 | 1509 | |
|
1481 | 1510 | for f in parameter_s.split(): |
|
1482 | 1511 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1483 | 1512 | self.shell.user_ns,islog=1) |
|
1484 | 1513 | |
|
1485 | 1514 | def magic_time(self,parameter_s = ''): |
|
1486 | 1515 | """Time execution of a Python statement or expression. |
|
1487 | 1516 | |
|
1488 | 1517 | The CPU and wall clock times are printed, and the value of the |
|
1489 | 1518 | expression (if any) is returned. Note that under Win32, system time |
|
1490 | 1519 | is always reported as 0, since it can not be measured. |
|
1491 | 1520 | |
|
1492 | 1521 | This function provides very basic timing functionality. In Python |
|
1493 | 1522 | 2.3, the timeit module offers more control and sophistication, but for |
|
1494 | 1523 | now IPython supports Python 2.2, so we can not rely on timeit being |
|
1495 | 1524 | present. |
|
1496 | 1525 | |
|
1497 | 1526 | Some examples: |
|
1498 | 1527 | |
|
1499 | 1528 | In [1]: time 2**128 |
|
1500 | 1529 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1501 | 1530 | Wall time: 0.00 |
|
1502 | 1531 | Out[1]: 340282366920938463463374607431768211456L |
|
1503 | 1532 | |
|
1504 | 1533 | In [2]: n = 1000000 |
|
1505 | 1534 | |
|
1506 | 1535 | In [3]: time sum(range(n)) |
|
1507 | 1536 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1508 | 1537 | Wall time: 1.37 |
|
1509 | 1538 | Out[3]: 499999500000L |
|
1510 | 1539 | |
|
1511 | 1540 | In [4]: time print 'hello world' |
|
1512 | 1541 | hello world |
|
1513 | 1542 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1514 | 1543 | Wall time: 0.00 |
|
1515 | 1544 | """ |
|
1516 | 1545 | |
|
1517 | 1546 | # fail immediately if the given expression can't be compiled |
|
1518 | 1547 | try: |
|
1519 | 1548 | mode = 'eval' |
|
1520 | 1549 | code = compile(parameter_s,'<timed eval>',mode) |
|
1521 | 1550 | except SyntaxError: |
|
1522 | 1551 | mode = 'exec' |
|
1523 | 1552 | code = compile(parameter_s,'<timed exec>',mode) |
|
1524 | 1553 | # skew measurement as little as possible |
|
1525 | 1554 | glob = self.shell.user_ns |
|
1526 | 1555 | clk = clock2 |
|
1527 | 1556 | wtime = time.time |
|
1528 | 1557 | # time execution |
|
1529 | 1558 | wall_st = wtime() |
|
1530 | 1559 | if mode=='eval': |
|
1531 | 1560 | st = clk() |
|
1532 | 1561 | out = eval(code,glob) |
|
1533 | 1562 | end = clk() |
|
1534 | 1563 | else: |
|
1535 | 1564 | st = clk() |
|
1536 | 1565 | exec code in glob |
|
1537 | 1566 | end = clk() |
|
1538 | 1567 | out = None |
|
1539 | 1568 | wall_end = wtime() |
|
1540 | 1569 | # Compute actual times and report |
|
1541 | 1570 | wall_time = wall_end-wall_st |
|
1542 | 1571 | cpu_user = end[0]-st[0] |
|
1543 | 1572 | cpu_sys = end[1]-st[1] |
|
1544 | 1573 | cpu_tot = cpu_user+cpu_sys |
|
1545 | 1574 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
1546 | 1575 | (cpu_user,cpu_sys,cpu_tot) |
|
1547 | 1576 | print "Wall time: %.2f" % wall_time |
|
1548 | 1577 | return out |
|
1549 | 1578 | |
|
1550 | 1579 | def magic_macro(self,parameter_s = ''): |
|
1551 | 1580 | """Define a set of input lines as a macro for future re-execution. |
|
1552 | 1581 | |
|
1553 | 1582 | Usage:\\ |
|
1554 | 1583 | %macro name n1-n2 n3-n4 ... n5 .. n6 ... |
|
1555 | 1584 | |
|
1556 | 1585 | This will define a global variable called `name` which is a string |
|
1557 | 1586 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1558 | 1587 | above) from your input history into a single string. This variable |
|
1559 | 1588 | acts like an automatic function which re-executes those lines as if |
|
1560 | 1589 | you had typed them. You just type 'name' at the prompt and the code |
|
1561 | 1590 | executes. |
|
1562 | 1591 | |
|
1563 | 1592 | The notation for indicating number ranges is: n1-n2 means 'use line |
|
1564 | 1593 | numbers n1,...n2' (the endpoint is included). That is, '5-7' means |
|
1565 | 1594 | using the lines numbered 5,6 and 7. |
|
1566 | 1595 | |
|
1567 | 1596 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
1568 | 1597 | notation, where N:M means numbers N through M-1. |
|
1569 | 1598 | |
|
1570 | 1599 | For example, if your history contains (%hist prints it): |
|
1571 | 1600 | |
|
1572 | 1601 | 44: x=1\\ |
|
1573 | 1602 | 45: y=3\\ |
|
1574 | 1603 | 46: z=x+y\\ |
|
1575 | 1604 | 47: print x\\ |
|
1576 | 1605 | 48: a=5\\ |
|
1577 | 1606 | 49: print 'x',x,'y',y\\ |
|
1578 | 1607 | |
|
1579 | 1608 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1580 | 1609 | called my_macro with: |
|
1581 | 1610 | |
|
1582 | 1611 | In [51]: %macro my_macro 44-47 49 |
|
1583 | 1612 | |
|
1584 | 1613 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1585 | 1614 | in one pass. |
|
1586 | 1615 | |
|
1587 | 1616 | You don't need to give the line-numbers in order, and any given line |
|
1588 | 1617 | number can appear multiple times. You can assemble macros with any |
|
1589 | 1618 | lines from your input history in any order. |
|
1590 | 1619 | |
|
1591 | 1620 | The macro is a simple object which holds its value in an attribute, |
|
1592 | 1621 | but IPython's display system checks for macros and executes them as |
|
1593 | 1622 | code instead of printing them when you type their name. |
|
1594 | 1623 | |
|
1595 | 1624 | You can view a macro's contents by explicitly printing it with: |
|
1596 | 1625 | |
|
1597 | 1626 | 'print macro_name'. |
|
1598 | 1627 | |
|
1599 | 1628 | For one-off cases which DON'T contain magic function calls in them you |
|
1600 | 1629 | can obtain similar results by explicitly executing slices from your |
|
1601 | 1630 | input history with: |
|
1602 | 1631 | |
|
1603 | 1632 | In [60]: exec In[44:48]+In[49]""" |
|
1604 | 1633 | |
|
1605 | 1634 | args = parameter_s.split() |
|
1606 | 1635 | name,ranges = args[0], args[1:] |
|
1607 | 1636 | #print 'rng',ranges # dbg |
|
1608 | 1637 | lines = self.extract_input_slices(ranges) |
|
1609 | 1638 | macro = Macro(lines) |
|
1610 | 1639 | self.shell.user_ns.update({name:macro}) |
|
1611 | 1640 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1612 | 1641 | print 'Macro contents:' |
|
1613 | 1642 | print macro, |
|
1614 | 1643 | |
|
1615 | 1644 | def magic_save(self,parameter_s = ''): |
|
1616 | 1645 | """Save a set of lines to a given filename. |
|
1617 | 1646 | |
|
1618 | 1647 | Usage:\\ |
|
1619 | 1648 | %save filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
1620 | 1649 | |
|
1621 | 1650 | This function uses the same syntax as %macro for line extraction, but |
|
1622 | 1651 | instead of creating a macro it saves the resulting string to the |
|
1623 | 1652 | filename you specify. |
|
1624 | 1653 | |
|
1625 | 1654 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
1626 | 1655 | it asks for confirmation before overwriting existing files.""" |
|
1627 | 1656 | |
|
1628 | 1657 | args = parameter_s.split() |
|
1629 | 1658 | fname,ranges = args[0], args[1:] |
|
1630 | 1659 | if not fname.endswith('.py'): |
|
1631 | 1660 | fname += '.py' |
|
1632 | 1661 | if os.path.isfile(fname): |
|
1633 | 1662 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) |
|
1634 | 1663 | if ans.lower() not in ['y','yes']: |
|
1635 | 1664 | print 'Operation cancelled.' |
|
1636 | 1665 | return |
|
1637 | 1666 | cmds = ''.join(self.extract_input_slices(ranges)) |
|
1638 | 1667 | f = file(fname,'w') |
|
1639 | 1668 | f.write(cmds) |
|
1640 | 1669 | f.close() |
|
1641 | 1670 | print 'The following commands were written to file `%s`:' % fname |
|
1642 | 1671 | print cmds |
|
1643 | 1672 | |
|
1644 | 1673 | def _edit_macro(self,mname,macro): |
|
1645 | 1674 | """open an editor with the macro data in a file""" |
|
1646 | 1675 | filename = self.shell.mktempfile(macro.value) |
|
1647 | 1676 | self.shell.hooks.editor(filename) |
|
1648 | 1677 | |
|
1649 | 1678 | # and make a new macro object, to replace the old one |
|
1650 | 1679 | mfile = open(filename) |
|
1651 | 1680 | mvalue = mfile.read() |
|
1652 | 1681 | mfile.close() |
|
1653 | 1682 | self.shell.user_ns[mname] = Macro(mvalue) |
|
1654 | 1683 | |
|
1655 | 1684 | def magic_ed(self,parameter_s=''): |
|
1656 | 1685 | """Alias to %edit.""" |
|
1657 | 1686 | return self.magic_edit(parameter_s) |
|
1658 | 1687 | |
|
1659 | 1688 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
1660 | 1689 | """Bring up an editor and execute the resulting code. |
|
1661 | 1690 | |
|
1662 | 1691 | Usage: |
|
1663 | 1692 | %edit [options] [args] |
|
1664 | 1693 | |
|
1665 | 1694 | %edit runs IPython's editor hook. The default version of this hook is |
|
1666 | 1695 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
1667 | 1696 | environment variable $EDITOR. If this isn't found, it will default to |
|
1668 | 1697 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
1669 | 1698 | docstring for how to change the editor hook. |
|
1670 | 1699 | |
|
1671 | 1700 | You can also set the value of this editor via the command line option |
|
1672 | 1701 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
1673 | 1702 | specifically for IPython an editor different from your typical default |
|
1674 | 1703 | (and for Windows users who typically don't set environment variables). |
|
1675 | 1704 | |
|
1676 | 1705 | This command allows you to conveniently edit multi-line code right in |
|
1677 | 1706 | your IPython session. |
|
1678 | 1707 | |
|
1679 | 1708 | If called without arguments, %edit opens up an empty editor with a |
|
1680 | 1709 | temporary file and will execute the contents of this file when you |
|
1681 | 1710 | close it (don't forget to save it!). |
|
1682 | 1711 | |
|
1683 | 1712 | Options: |
|
1684 | 1713 | |
|
1685 | 1714 | -p: this will call the editor with the same data as the previous time |
|
1686 | 1715 | it was used, regardless of how long ago (in your current session) it |
|
1687 | 1716 | was. |
|
1688 | 1717 | |
|
1689 | 1718 | -x: do not execute the edited code immediately upon exit. This is |
|
1690 | 1719 | mainly useful if you are editing programs which need to be called with |
|
1691 | 1720 | command line arguments, which you can then do using %run. |
|
1692 | 1721 | |
|
1693 | 1722 | Arguments: |
|
1694 | 1723 | |
|
1695 | 1724 | If arguments are given, the following possibilites exist: |
|
1696 | 1725 | |
|
1697 | 1726 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
1698 | 1727 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
1699 | 1728 | loaded into the editor. The syntax is the same of the %macro command. |
|
1700 | 1729 | |
|
1701 | 1730 | - If the argument doesn't start with a number, it is evaluated as a |
|
1702 | 1731 | variable and its contents loaded into the editor. You can thus edit |
|
1703 | 1732 | any string which contains python code (including the result of |
|
1704 | 1733 | previous edits). |
|
1705 | 1734 | |
|
1706 | 1735 | - If the argument is the name of an object (other than a string), |
|
1707 | 1736 | IPython will try to locate the file where it was defined and open the |
|
1708 | 1737 | editor at the point where it is defined. You can use `%edit function` |
|
1709 | 1738 | to load an editor exactly at the point where 'function' is defined, |
|
1710 | 1739 | edit it and have the file be executed automatically. |
|
1711 | 1740 | |
|
1712 | 1741 | If the object is a macro (see %macro for details), this opens up your |
|
1713 | 1742 | specified editor with a temporary file containing the macro's data. |
|
1714 | 1743 | Upon exit, the macro is reloaded with the contents of the file. |
|
1715 | 1744 | |
|
1716 | 1745 | Note: opening at an exact line is only supported under Unix, and some |
|
1717 | 1746 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1718 | 1747 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1719 | 1748 | (X)Emacs, vi, jed, pico and joe all do. |
|
1720 | 1749 | |
|
1721 | 1750 | - If the argument is not found as a variable, IPython will look for a |
|
1722 | 1751 | file with that name (adding .py if necessary) and load it into the |
|
1723 | 1752 | editor. It will execute its contents with execfile() when you exit, |
|
1724 | 1753 | loading any code in the file into your interactive namespace. |
|
1725 | 1754 | |
|
1726 | 1755 | After executing your code, %edit will return as output the code you |
|
1727 | 1756 | typed in the editor (except when it was an existing file). This way |
|
1728 | 1757 | you can reload the code in further invocations of %edit as a variable, |
|
1729 | 1758 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
1730 | 1759 | the output. |
|
1731 | 1760 | |
|
1732 | 1761 | Note that %edit is also available through the alias %ed. |
|
1733 | 1762 | |
|
1734 | 1763 | This is an example of creating a simple function inside the editor and |
|
1735 | 1764 | then modifying it. First, start up the editor: |
|
1736 | 1765 | |
|
1737 | 1766 | In [1]: ed\\ |
|
1738 | 1767 | Editing... done. Executing edited code...\\ |
|
1739 | 1768 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' |
|
1740 | 1769 | |
|
1741 | 1770 | We can then call the function foo(): |
|
1742 | 1771 | |
|
1743 | 1772 | In [2]: foo()\\ |
|
1744 | 1773 | foo() was defined in an editing session |
|
1745 | 1774 | |
|
1746 | 1775 | Now we edit foo. IPython automatically loads the editor with the |
|
1747 | 1776 | (temporary) file where foo() was previously defined: |
|
1748 | 1777 | |
|
1749 | 1778 | In [3]: ed foo\\ |
|
1750 | 1779 | Editing... done. Executing edited code... |
|
1751 | 1780 | |
|
1752 | 1781 | And if we call foo() again we get the modified version: |
|
1753 | 1782 | |
|
1754 | 1783 | In [4]: foo()\\ |
|
1755 | 1784 | foo() has now been changed! |
|
1756 | 1785 | |
|
1757 | 1786 | Here is an example of how to edit a code snippet successive |
|
1758 | 1787 | times. First we call the editor: |
|
1759 | 1788 | |
|
1760 | 1789 | In [8]: ed\\ |
|
1761 | 1790 | Editing... done. Executing edited code...\\ |
|
1762 | 1791 | hello\\ |
|
1763 | 1792 | Out[8]: "print 'hello'\\n" |
|
1764 | 1793 | |
|
1765 | 1794 | Now we call it again with the previous output (stored in _): |
|
1766 | 1795 | |
|
1767 | 1796 | In [9]: ed _\\ |
|
1768 | 1797 | Editing... done. Executing edited code...\\ |
|
1769 | 1798 | hello world\\ |
|
1770 | 1799 | Out[9]: "print 'hello world'\\n" |
|
1771 | 1800 | |
|
1772 | 1801 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
1773 | 1802 | |
|
1774 | 1803 | In [10]: ed _8\\ |
|
1775 | 1804 | Editing... done. Executing edited code...\\ |
|
1776 | 1805 | hello again\\ |
|
1777 | 1806 | Out[10]: "print 'hello again'\\n" |
|
1778 | 1807 | |
|
1779 | 1808 | |
|
1780 | 1809 | Changing the default editor hook: |
|
1781 | 1810 | |
|
1782 | 1811 | If you wish to write your own editor hook, you can put it in a |
|
1783 | 1812 | configuration file which you load at startup time. The default hook |
|
1784 | 1813 | is defined in the IPython.hooks module, and you can use that as a |
|
1785 | 1814 | starting example for further modifications. That file also has |
|
1786 | 1815 | general instructions on how to set a new hook for use once you've |
|
1787 | 1816 | defined it.""" |
|
1788 | 1817 | |
|
1789 | 1818 | # FIXME: This function has become a convoluted mess. It needs a |
|
1790 | 1819 | # ground-up rewrite with clean, simple logic. |
|
1791 | 1820 | |
|
1792 | 1821 | def make_filename(arg): |
|
1793 | 1822 | "Make a filename from the given args" |
|
1794 | 1823 | try: |
|
1795 | 1824 | filename = get_py_filename(arg) |
|
1796 | 1825 | except IOError: |
|
1797 | 1826 | if args.endswith('.py'): |
|
1798 | 1827 | filename = arg |
|
1799 | 1828 | else: |
|
1800 | 1829 | filename = None |
|
1801 | 1830 | return filename |
|
1802 | 1831 | |
|
1803 | 1832 | # custom exceptions |
|
1804 | 1833 | class DataIsObject(Exception): pass |
|
1805 | 1834 | |
|
1806 | 1835 | opts,args = self.parse_options(parameter_s,'px') |
|
1807 | 1836 | |
|
1808 | 1837 | # Default line number value |
|
1809 | 1838 | lineno = None |
|
1810 | 1839 | if opts.has_key('p'): |
|
1811 | 1840 | args = '_%s' % last_call[0] |
|
1812 | 1841 | if not self.shell.user_ns.has_key(args): |
|
1813 | 1842 | args = last_call[1] |
|
1814 | 1843 | |
|
1815 | 1844 | # use last_call to remember the state of the previous call, but don't |
|
1816 | 1845 | # let it be clobbered by successive '-p' calls. |
|
1817 | 1846 | try: |
|
1818 | 1847 | last_call[0] = self.shell.outputcache.prompt_count |
|
1819 | 1848 | if not opts.has_key('p'): |
|
1820 | 1849 | last_call[1] = parameter_s |
|
1821 | 1850 | except: |
|
1822 | 1851 | pass |
|
1823 | 1852 | |
|
1824 | 1853 | # by default this is done with temp files, except when the given |
|
1825 | 1854 | # arg is a filename |
|
1826 | 1855 | use_temp = 1 |
|
1827 | 1856 | |
|
1828 | 1857 | if re.match(r'\d',args): |
|
1829 | 1858 | # Mode where user specifies ranges of lines, like in %macro. |
|
1830 | 1859 | # This means that you can't edit files whose names begin with |
|
1831 | 1860 | # numbers this way. Tough. |
|
1832 | 1861 | ranges = args.split() |
|
1833 | 1862 | data = ''.join(self.extract_input_slices(ranges)) |
|
1834 | 1863 | elif args.endswith('.py'): |
|
1835 | 1864 | filename = make_filename(args) |
|
1836 | 1865 | data = '' |
|
1837 | 1866 | use_temp = 0 |
|
1838 | 1867 | elif args: |
|
1839 | 1868 | try: |
|
1840 | 1869 | # Load the parameter given as a variable. If not a string, |
|
1841 | 1870 | # process it as an object instead (below) |
|
1842 | 1871 | |
|
1843 | 1872 | #print '*** args',args,'type',type(args) # dbg |
|
1844 | 1873 | data = eval(args,self.shell.user_ns) |
|
1845 | 1874 | if not type(data) in StringTypes: |
|
1846 | 1875 | raise DataIsObject |
|
1847 | 1876 | |
|
1848 | 1877 | except (NameError,SyntaxError): |
|
1849 | 1878 | # given argument is not a variable, try as a filename |
|
1850 | 1879 | filename = make_filename(args) |
|
1851 | 1880 | if filename is None: |
|
1852 | 1881 | warn("Argument given (%s) can't be found as a variable " |
|
1853 | 1882 | "or as a filename." % args) |
|
1854 | 1883 | return |
|
1855 | 1884 | |
|
1856 | 1885 | data = '' |
|
1857 | 1886 | use_temp = 0 |
|
1858 | 1887 | except DataIsObject: |
|
1859 | 1888 | |
|
1860 | 1889 | # macros have a special edit function |
|
1861 | 1890 | if isinstance(data,Macro): |
|
1862 | 1891 | self._edit_macro(args,data) |
|
1863 | 1892 | return |
|
1864 | 1893 | |
|
1865 | 1894 | # For objects, try to edit the file where they are defined |
|
1866 | 1895 | try: |
|
1867 | 1896 | filename = inspect.getabsfile(data) |
|
1868 | 1897 | datafile = 1 |
|
1869 | 1898 | except TypeError: |
|
1870 | 1899 | filename = make_filename(args) |
|
1871 | 1900 | datafile = 1 |
|
1872 | 1901 | warn('Could not find file where `%s` is defined.\n' |
|
1873 | 1902 | 'Opening a file named `%s`' % (args,filename)) |
|
1874 | 1903 | # Now, make sure we can actually read the source (if it was in |
|
1875 | 1904 | # a temp file it's gone by now). |
|
1876 | 1905 | if datafile: |
|
1877 | 1906 | try: |
|
1878 | 1907 | lineno = inspect.getsourcelines(data)[1] |
|
1879 | 1908 | except IOError: |
|
1880 | 1909 | filename = make_filename(args) |
|
1881 | 1910 | if filename is None: |
|
1882 | 1911 | warn('The file `%s` where `%s` was defined cannot ' |
|
1883 | 1912 | 'be read.' % (filename,data)) |
|
1884 | 1913 | return |
|
1885 | 1914 | use_temp = 0 |
|
1886 | 1915 | else: |
|
1887 | 1916 | data = '' |
|
1888 | 1917 | |
|
1889 | 1918 | if use_temp: |
|
1890 | 1919 | filename = self.shell.mktempfile(data) |
|
1891 | 1920 | |
|
1892 | 1921 | # do actual editing here |
|
1893 | 1922 | print 'Editing...', |
|
1894 | 1923 | sys.stdout.flush() |
|
1895 | 1924 | self.shell.hooks.editor(filename,lineno) |
|
1896 | 1925 | if opts.has_key('x'): # -x prevents actual execution |
|
1897 | 1926 | |
|
1898 | 1927 | else: |
|
1899 | 1928 | print 'done. Executing edited code...' |
|
1900 | 1929 | try: |
|
1901 | 1930 | self.shell.safe_execfile(filename,self.shell.user_ns) |
|
1902 | 1931 | except IOError,msg: |
|
1903 | 1932 | if msg.filename == filename: |
|
1904 | 1933 | warn('File not found. Did you forget to save?') |
|
1905 | 1934 | return |
|
1906 | 1935 | else: |
|
1907 | 1936 | self.shell.showtraceback() |
|
1908 | 1937 | except: |
|
1909 | 1938 | self.shell.showtraceback() |
|
1910 | 1939 | |
|
1911 | 1940 | def magic_xmode(self,parameter_s = ''): |
|
1912 | 1941 | """Switch modes for the exception handlers. |
|
1913 | 1942 | |
|
1914 | 1943 | Valid modes: Plain, Context and Verbose. |
|
1915 | 1944 | |
|
1916 | 1945 | If called without arguments, acts as a toggle.""" |
|
1917 | 1946 | |
|
1918 | 1947 | def xmode_switch_err(name): |
|
1919 | 1948 | warn('Error changing %s exception modes.\n%s' % |
|
1920 | 1949 | (name,sys.exc_info()[1])) |
|
1921 | 1950 | |
|
1922 | 1951 | shell = self.shell |
|
1923 | 1952 | new_mode = parameter_s.strip().capitalize() |
|
1924 | 1953 | try: |
|
1925 | 1954 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
1926 | 1955 | print 'Exception reporting mode:',shell.InteractiveTB.mode |
|
1927 | 1956 | except: |
|
1928 | 1957 | xmode_switch_err('user') |
|
1929 | 1958 | |
|
1930 | 1959 | # threaded shells use a special handler in sys.excepthook |
|
1931 | 1960 | if shell.isthreaded: |
|
1932 | 1961 | try: |
|
1933 | 1962 | shell.sys_excepthook.set_mode(mode=new_mode) |
|
1934 | 1963 | except: |
|
1935 | 1964 | xmode_switch_err('threaded') |
|
1936 | 1965 | |
|
1937 | 1966 | def magic_colors(self,parameter_s = ''): |
|
1938 | 1967 | """Switch color scheme for prompts, info system and exception handlers. |
|
1939 | 1968 | |
|
1940 | 1969 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
1941 | 1970 | |
|
1942 | 1971 | Color scheme names are not case-sensitive.""" |
|
1943 | 1972 | |
|
1944 | 1973 | def color_switch_err(name): |
|
1945 | 1974 | warn('Error changing %s color schemes.\n%s' % |
|
1946 | 1975 | (name,sys.exc_info()[1])) |
|
1947 | 1976 | |
|
1948 | 1977 | |
|
1949 | 1978 | new_scheme = parameter_s.strip() |
|
1950 | 1979 | if not new_scheme: |
|
1951 | 1980 | print 'You must specify a color scheme.' |
|
1952 | 1981 | return |
|
1953 | 1982 | # Under Windows, check for Gary Bishop's readline, which is necessary |
|
1954 | 1983 | # for ANSI coloring |
|
1955 | 1984 | if os.name in ['nt','dos']: |
|
1956 | 1985 | try: |
|
1957 | 1986 | import readline |
|
1958 | 1987 | except ImportError: |
|
1959 | 1988 | has_readline = 0 |
|
1960 | 1989 | else: |
|
1961 | 1990 | try: |
|
1962 | 1991 | readline.GetOutputFile() |
|
1963 | 1992 | except AttributeError: |
|
1964 | 1993 | has_readline = 0 |
|
1965 | 1994 | else: |
|
1966 | 1995 | has_readline = 1 |
|
1967 | 1996 | if not has_readline: |
|
1968 | 1997 | msg = """\ |
|
1969 | 1998 | Proper color support under MS Windows requires Gary Bishop's readline library. |
|
1970 | 1999 | You can find it at: |
|
1971 | 2000 | http://sourceforge.net/projects/uncpythontools |
|
1972 | 2001 | Gary's readline needs the ctypes module, from: |
|
1973 | 2002 | http://starship.python.net/crew/theller/ctypes |
|
1974 | 2003 | |
|
1975 | 2004 | Defaulting color scheme to 'NoColor'""" |
|
1976 | 2005 | new_scheme = 'NoColor' |
|
1977 | 2006 | warn(msg) |
|
1978 | 2007 | # local shortcut |
|
1979 | 2008 | shell = self.shell |
|
1980 | 2009 | |
|
1981 | 2010 | # Set prompt colors |
|
1982 | 2011 | try: |
|
1983 | 2012 | shell.outputcache.set_colors(new_scheme) |
|
1984 | 2013 | except: |
|
1985 | 2014 | color_switch_err('prompt') |
|
1986 | 2015 | else: |
|
1987 | 2016 | shell.rc.colors = \ |
|
1988 | 2017 | shell.outputcache.color_table.active_scheme_name |
|
1989 | 2018 | # Set exception colors |
|
1990 | 2019 | try: |
|
1991 | 2020 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
1992 | 2021 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
1993 | 2022 | except: |
|
1994 | 2023 | color_switch_err('exception') |
|
1995 | 2024 | |
|
1996 | 2025 | # threaded shells use a verbose traceback in sys.excepthook |
|
1997 | 2026 | if shell.isthreaded: |
|
1998 | 2027 | try: |
|
1999 | 2028 | shell.sys_excepthook.set_colors(scheme=new_scheme) |
|
2000 | 2029 | except: |
|
2001 | 2030 | color_switch_err('system exception handler') |
|
2002 | 2031 | |
|
2003 | 2032 | # Set info (for 'object?') colors |
|
2004 | 2033 | if shell.rc.color_info: |
|
2005 | 2034 | try: |
|
2006 | 2035 | shell.inspector.set_active_scheme(new_scheme) |
|
2007 | 2036 | except: |
|
2008 | 2037 | color_switch_err('object inspector') |
|
2009 | 2038 | else: |
|
2010 | 2039 | shell.inspector.set_active_scheme('NoColor') |
|
2011 | 2040 | |
|
2012 | 2041 | def magic_color_info(self,parameter_s = ''): |
|
2013 | 2042 | """Toggle color_info. |
|
2014 | 2043 | |
|
2015 | 2044 | The color_info configuration parameter controls whether colors are |
|
2016 | 2045 | used for displaying object details (by things like %psource, %pfile or |
|
2017 | 2046 | the '?' system). This function toggles this value with each call. |
|
2018 | 2047 | |
|
2019 | 2048 | Note that unless you have a fairly recent pager (less works better |
|
2020 | 2049 | than more) in your system, using colored object information displays |
|
2021 | 2050 | will not work properly. Test it and see.""" |
|
2022 | 2051 | |
|
2023 | 2052 | self.shell.rc.color_info = 1 - self.shell.rc.color_info |
|
2024 | 2053 | self.magic_colors(self.shell.rc.colors) |
|
2025 | 2054 | print 'Object introspection functions have now coloring:', |
|
2026 | 2055 | print ['OFF','ON'][self.shell.rc.color_info] |
|
2027 | 2056 | |
|
2028 | 2057 | def magic_Pprint(self, parameter_s=''): |
|
2029 | 2058 | """Toggle pretty printing on/off.""" |
|
2030 | 2059 | |
|
2031 | 2060 | self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint |
|
2032 | 2061 | print 'Pretty printing has been turned', \ |
|
2033 | 2062 | ['OFF','ON'][self.shell.outputcache.Pprint] |
|
2034 | 2063 | |
|
2035 | 2064 | def magic_exit(self, parameter_s=''): |
|
2036 | 2065 | """Exit IPython, confirming if configured to do so. |
|
2037 | 2066 | |
|
2038 | 2067 | You can configure whether IPython asks for confirmation upon exit by |
|
2039 | 2068 | setting the confirm_exit flag in the ipythonrc file.""" |
|
2040 | 2069 | |
|
2041 | 2070 | self.shell.exit() |
|
2042 | 2071 | |
|
2043 | 2072 | def magic_quit(self, parameter_s=''): |
|
2044 | 2073 | """Exit IPython, confirming if configured to do so (like %exit)""" |
|
2045 | 2074 | |
|
2046 | 2075 | self.shell.exit() |
|
2047 | 2076 | |
|
2048 | 2077 | def magic_Exit(self, parameter_s=''): |
|
2049 | 2078 | """Exit IPython without confirmation.""" |
|
2050 | 2079 | |
|
2051 | 2080 | self.shell.exit_now = True |
|
2052 | 2081 | |
|
2053 | 2082 | def magic_Quit(self, parameter_s=''): |
|
2054 | 2083 | """Exit IPython without confirmation (like %Exit).""" |
|
2055 | 2084 | |
|
2056 | 2085 | self.shell.exit_now = True |
|
2057 | 2086 | |
|
2058 | 2087 | #...................................................................... |
|
2059 | 2088 | # Functions to implement unix shell-type things |
|
2060 | 2089 | |
|
2061 | 2090 | def magic_alias(self, parameter_s = ''): |
|
2062 | 2091 | """Define an alias for a system command. |
|
2063 | 2092 | |
|
2064 | 2093 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
2065 | 2094 | |
|
2066 | 2095 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
2067 | 2096 | params' (from your underlying operating system). |
|
2068 | 2097 | |
|
2069 | 2098 | Aliases have lower precedence than magic functions and Python normal |
|
2070 | 2099 | variables, so if 'foo' is both a Python variable and an alias, the |
|
2071 | 2100 | alias can not be executed until 'del foo' removes the Python variable. |
|
2072 | 2101 | |
|
2073 | 2102 | You can use the %l specifier in an alias definition to represent the |
|
2074 | 2103 | whole line when the alias is called. For example: |
|
2075 | 2104 | |
|
2076 | 2105 | In [2]: alias all echo "Input in brackets: <%l>"\\ |
|
2077 | 2106 | In [3]: all hello world\\ |
|
2078 | 2107 | Input in brackets: <hello world> |
|
2079 | 2108 | |
|
2080 | 2109 | You can also define aliases with parameters using %s specifiers (one |
|
2081 | 2110 | per parameter): |
|
2082 | 2111 | |
|
2083 | 2112 | In [1]: alias parts echo first %s second %s\\ |
|
2084 | 2113 | In [2]: %parts A B\\ |
|
2085 | 2114 | first A second B\\ |
|
2086 | 2115 | In [3]: %parts A\\ |
|
2087 | 2116 | Incorrect number of arguments: 2 expected.\\ |
|
2088 | 2117 | parts is an alias to: 'echo first %s second %s' |
|
2089 | 2118 | |
|
2090 | 2119 | Note that %l and %s are mutually exclusive. You can only use one or |
|
2091 | 2120 | the other in your aliases. |
|
2092 | 2121 | |
|
2093 | 2122 | Aliases expand Python variables just like system calls using ! or !! |
|
2094 | 2123 | do: all expressions prefixed with '$' get expanded. For details of |
|
2095 | 2124 | the semantic rules, see PEP-215: |
|
2096 | 2125 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
2097 | 2126 | IPython for variable expansion. If you want to access a true shell |
|
2098 | 2127 | variable, an extra $ is necessary to prevent its expansion by IPython: |
|
2099 | 2128 | |
|
2100 | 2129 | In [6]: alias show echo\\ |
|
2101 | 2130 | In [7]: PATH='A Python string'\\ |
|
2102 | 2131 | In [8]: show $PATH\\ |
|
2103 | 2132 | A Python string\\ |
|
2104 | 2133 | In [9]: show $$PATH\\ |
|
2105 | 2134 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
2106 | 2135 | |
|
2107 | 2136 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
2108 | 2137 | and %rehashx functions, which automatically create aliases for the |
|
2109 | 2138 | contents of your $PATH. |
|
2110 | 2139 | |
|
2111 | 2140 | If called with no parameters, %alias prints the current alias table.""" |
|
2112 | 2141 | |
|
2113 | 2142 | par = parameter_s.strip() |
|
2114 | 2143 | if not par: |
|
2115 | 2144 | if self.shell.rc.automagic: |
|
2116 | 2145 | prechar = '' |
|
2117 | 2146 | else: |
|
2118 | 2147 | prechar = self.shell.ESC_MAGIC |
|
2119 | 2148 | print 'Alias\t\tSystem Command\n'+'-'*30 |
|
2120 | 2149 | atab = self.shell.alias_table |
|
2121 | 2150 | aliases = atab.keys() |
|
2122 | 2151 | aliases.sort() |
|
2123 | 2152 | for alias in aliases: |
|
2124 | 2153 | print prechar+alias+'\t\t'+atab[alias][1] |
|
2125 | 2154 | print '-'*30+'\nTotal number of aliases:',len(aliases) |
|
2126 | 2155 | return |
|
2127 | 2156 | try: |
|
2128 | 2157 | alias,cmd = par.split(None,1) |
|
2129 | 2158 | except: |
|
2130 | 2159 | print OInspect.getdoc(self.magic_alias) |
|
2131 | 2160 | else: |
|
2132 | 2161 | nargs = cmd.count('%s') |
|
2133 | 2162 | if nargs>0 and cmd.find('%l')>=0: |
|
2134 | 2163 | error('The %s and %l specifiers are mutually exclusive ' |
|
2135 | 2164 | 'in alias definitions.') |
|
2136 | 2165 | else: # all looks OK |
|
2137 | 2166 | self.shell.alias_table[alias] = (nargs,cmd) |
|
2138 | 2167 | self.shell.alias_table_validate(verbose=1) |
|
2139 | 2168 | # end magic_alias |
|
2140 | 2169 | |
|
2141 | 2170 | def magic_unalias(self, parameter_s = ''): |
|
2142 | 2171 | """Remove an alias""" |
|
2143 | 2172 | |
|
2144 | 2173 | aname = parameter_s.strip() |
|
2145 | 2174 | if aname in self.shell.alias_table: |
|
2146 | 2175 | del self.shell.alias_table[aname] |
|
2147 | 2176 | |
|
2148 | 2177 | def magic_rehash(self, parameter_s = ''): |
|
2149 | 2178 | """Update the alias table with all entries in $PATH. |
|
2150 | 2179 | |
|
2151 | 2180 | This version does no checks on execute permissions or whether the |
|
2152 | 2181 | contents of $PATH are truly files (instead of directories or something |
|
2153 | 2182 | else). For such a safer (but slower) version, use %rehashx.""" |
|
2154 | 2183 | |
|
2155 | 2184 | # This function (and rehashx) manipulate the alias_table directly |
|
2156 | 2185 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
2157 | 2186 | # typical Linux box involves several thousand entries, so efficiency |
|
2158 | 2187 | # here is a top concern. |
|
2159 | 2188 | |
|
2160 | 2189 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2161 | 2190 | alias_table = self.shell.alias_table |
|
2162 | 2191 | for pdir in path: |
|
2163 | 2192 | for ff in os.listdir(pdir): |
|
2164 | 2193 | # each entry in the alias table must be (N,name), where |
|
2165 | 2194 | # N is the number of positional arguments of the alias. |
|
2166 | 2195 | alias_table[ff] = (0,ff) |
|
2167 | 2196 | # Make sure the alias table doesn't contain keywords or builtins |
|
2168 | 2197 | self.shell.alias_table_validate() |
|
2169 | 2198 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
2170 | 2199 | # aliases since %rehash will probably clobber them |
|
2171 | 2200 | self.shell.init_auto_alias() |
|
2172 | 2201 | |
|
2173 | 2202 | def magic_rehashx(self, parameter_s = ''): |
|
2174 | 2203 | """Update the alias table with all executable files in $PATH. |
|
2175 | 2204 | |
|
2176 | 2205 | This version explicitly checks that every entry in $PATH is a file |
|
2177 | 2206 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
2178 | 2207 | |
|
2179 | 2208 | Under Windows, it checks executability as a match agains a |
|
2180 | 2209 | '|'-separated string of extensions, stored in the IPython config |
|
2181 | 2210 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ |
|
2182 | 2211 | |
|
2183 | 2212 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2184 | 2213 | alias_table = self.shell.alias_table |
|
2185 | 2214 | |
|
2186 | 2215 | if os.name == 'posix': |
|
2187 | 2216 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
2188 | 2217 | os.access(fname,os.X_OK) |
|
2189 | 2218 | else: |
|
2190 | 2219 | |
|
2191 | 2220 | try: |
|
2192 | 2221 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
2193 | 2222 | except KeyError: |
|
2194 | 2223 | winext = 'exe|com|bat' |
|
2195 | 2224 | |
|
2196 | 2225 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
2197 | 2226 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
2198 | 2227 | savedir = os.getcwd() |
|
2199 | 2228 | try: |
|
2200 | 2229 | # write the whole loop for posix/Windows so we don't have an if in |
|
2201 | 2230 | # the innermost part |
|
2202 | 2231 | if os.name == 'posix': |
|
2203 | 2232 | for pdir in path: |
|
2204 | 2233 | os.chdir(pdir) |
|
2205 | 2234 | for ff in os.listdir(pdir): |
|
2206 | 2235 | if isexec(ff): |
|
2207 | 2236 | # each entry in the alias table must be (N,name), |
|
2208 | 2237 | # where N is the number of positional arguments of the |
|
2209 | 2238 | # alias. |
|
2210 | 2239 | alias_table[ff] = (0,ff) |
|
2211 | 2240 | else: |
|
2212 | 2241 | for pdir in path: |
|
2213 | 2242 | os.chdir(pdir) |
|
2214 | 2243 | for ff in os.listdir(pdir): |
|
2215 | 2244 | if isexec(ff): |
|
2216 | 2245 | alias_table[execre.sub(r'\1',ff)] = (0,ff) |
|
2217 | 2246 | # Make sure the alias table doesn't contain keywords or builtins |
|
2218 | 2247 | self.shell.alias_table_validate() |
|
2219 | 2248 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
2220 | 2249 | # modified aliases since %rehashx will probably clobber them |
|
2221 | 2250 | self.shell.init_auto_alias() |
|
2222 | 2251 | finally: |
|
2223 | 2252 | os.chdir(savedir) |
|
2224 | 2253 | |
|
2225 | 2254 | def magic_pwd(self, parameter_s = ''): |
|
2226 | 2255 | """Return the current working directory path.""" |
|
2227 | 2256 | return os.getcwd() |
|
2228 | 2257 | |
|
2229 | 2258 | def magic_cd(self, parameter_s=''): |
|
2230 | 2259 | """Change the current working directory. |
|
2231 | 2260 | |
|
2232 | 2261 | This command automatically maintains an internal list of directories |
|
2233 | 2262 | you visit during your IPython session, in the variable _dh. The |
|
2234 | 2263 | command %dhist shows this history nicely formatted. |
|
2235 | 2264 | |
|
2236 | 2265 | Usage: |
|
2237 | 2266 | |
|
2238 | 2267 | cd 'dir': changes to directory 'dir'. |
|
2239 | 2268 | |
|
2240 | 2269 | cd -: changes to the last visited directory. |
|
2241 | 2270 | |
|
2242 | 2271 | cd -<n>: changes to the n-th directory in the directory history. |
|
2243 | 2272 | |
|
2244 | 2273 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
2245 | 2274 | (note: cd <bookmark_name> is enough if there is no |
|
2246 | 2275 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
2247 | 2276 | |
|
2248 | 2277 | Options: |
|
2249 | 2278 | |
|
2250 | 2279 | -q: quiet. Do not print the working directory after the cd command is |
|
2251 | 2280 | executed. By default IPython's cd command does print this directory, |
|
2252 | 2281 | since the default prompts do not display path information. |
|
2253 | 2282 | |
|
2254 | 2283 | Note that !cd doesn't work for this purpose because the shell where |
|
2255 | 2284 | !command runs is immediately discarded after executing 'command'.""" |
|
2256 | 2285 | |
|
2257 | 2286 | parameter_s = parameter_s.strip() |
|
2258 | 2287 | bkms = self.shell.persist.get("bookmarks",{}) |
|
2259 | 2288 | |
|
2260 | 2289 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
2261 | 2290 | # jump in directory history by number |
|
2262 | 2291 | if numcd: |
|
2263 | 2292 | nn = int(numcd.group(2)) |
|
2264 | 2293 | try: |
|
2265 | 2294 | ps = self.shell.user_ns['_dh'][nn] |
|
2266 | 2295 | except IndexError: |
|
2267 | 2296 | print 'The requested directory does not exist in history.' |
|
2268 | 2297 | return |
|
2269 | 2298 | else: |
|
2270 | 2299 | opts = {} |
|
2271 | 2300 | else: |
|
2272 | 2301 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
2273 | 2302 | # jump to previous |
|
2274 | 2303 | if ps == '-': |
|
2275 | 2304 | try: |
|
2276 | 2305 | ps = self.shell.user_ns['_dh'][-2] |
|
2277 | 2306 | except IndexError: |
|
2278 | 2307 | print 'No previous directory to change to.' |
|
2279 | 2308 | return |
|
2280 | 2309 | # jump to bookmark |
|
2281 | 2310 | elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)): |
|
2282 | 2311 | if bkms.has_key(ps): |
|
2283 | 2312 | target = bkms[ps] |
|
2284 | 2313 | print '(bookmark:%s) -> %s' % (ps,target) |
|
2285 | 2314 | ps = target |
|
2286 | 2315 | else: |
|
2287 | 2316 | if bkms: |
|
2288 | 2317 | error("Bookmark '%s' not found. " |
|
2289 | 2318 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
2290 | 2319 | else: |
|
2291 | 2320 | print "Bookmarks not set - use %bookmark <bookmarkname>" |
|
2292 | 2321 | return |
|
2293 | 2322 | |
|
2294 | 2323 | # at this point ps should point to the target dir |
|
2295 | 2324 | if ps: |
|
2296 | 2325 | try: |
|
2297 | 2326 | os.chdir(os.path.expanduser(ps)) |
|
2298 | 2327 | except OSError: |
|
2299 | 2328 | print sys.exc_info()[1] |
|
2300 | 2329 | else: |
|
2301 | 2330 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2302 | 2331 | else: |
|
2303 | 2332 | os.chdir(self.shell.home_dir) |
|
2304 | 2333 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2305 | 2334 | if not 'q' in opts: |
|
2306 | 2335 | print self.shell.user_ns['_dh'][-1] |
|
2307 | 2336 | |
|
2308 | 2337 | def magic_dhist(self, parameter_s=''): |
|
2309 | 2338 | """Print your history of visited directories. |
|
2310 | 2339 | |
|
2311 | 2340 | %dhist -> print full history\\ |
|
2312 | 2341 | %dhist n -> print last n entries only\\ |
|
2313 | 2342 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
2314 | 2343 | |
|
2315 | 2344 | This history is automatically maintained by the %cd command, and |
|
2316 | 2345 | always available as the global list variable _dh. You can use %cd -<n> |
|
2317 | 2346 | to go to directory number <n>.""" |
|
2318 | 2347 | |
|
2319 | 2348 | dh = self.shell.user_ns['_dh'] |
|
2320 | 2349 | if parameter_s: |
|
2321 | 2350 | try: |
|
2322 | 2351 | args = map(int,parameter_s.split()) |
|
2323 | 2352 | except: |
|
2324 | 2353 | self.arg_err(Magic.magic_dhist) |
|
2325 | 2354 | return |
|
2326 | 2355 | if len(args) == 1: |
|
2327 | 2356 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
2328 | 2357 | elif len(args) == 2: |
|
2329 | 2358 | ini,fin = args |
|
2330 | 2359 | else: |
|
2331 | 2360 | self.arg_err(Magic.magic_dhist) |
|
2332 | 2361 | return |
|
2333 | 2362 | else: |
|
2334 | 2363 | ini,fin = 0,len(dh) |
|
2335 | 2364 | nlprint(dh, |
|
2336 | 2365 | header = 'Directory history (kept in _dh)', |
|
2337 | 2366 | start=ini,stop=fin) |
|
2338 | 2367 | |
|
2339 | 2368 | def magic_env(self, parameter_s=''): |
|
2340 | 2369 | """List environment variables.""" |
|
2341 | 2370 | |
|
2342 | 2371 | return os.environ.data |
|
2343 | 2372 | |
|
2344 | 2373 | def magic_pushd(self, parameter_s=''): |
|
2345 | 2374 | """Place the current dir on stack and change directory. |
|
2346 | 2375 | |
|
2347 | 2376 | Usage:\\ |
|
2348 | 2377 | %pushd ['dirname'] |
|
2349 | 2378 | |
|
2350 | 2379 | %pushd with no arguments does a %pushd to your home directory. |
|
2351 | 2380 | """ |
|
2352 | 2381 | if parameter_s == '': parameter_s = '~' |
|
2353 | 2382 | dir_s = self.shell.dir_stack |
|
2354 | 2383 | if len(dir_s)>0 and os.path.expanduser(parameter_s) != \ |
|
2355 | 2384 | os.path.expanduser(self.shell.dir_stack[0]): |
|
2356 | 2385 | try: |
|
2357 | 2386 | self.magic_cd(parameter_s) |
|
2358 | 2387 | dir_s.insert(0,os.getcwd().replace(self.home_dir,'~')) |
|
2359 | 2388 | self.magic_dirs() |
|
2360 | 2389 | except: |
|
2361 | 2390 | print 'Invalid directory' |
|
2362 | 2391 | else: |
|
2363 | 2392 | print 'You are already there!' |
|
2364 | 2393 | |
|
2365 | 2394 | def magic_popd(self, parameter_s=''): |
|
2366 | 2395 | """Change to directory popped off the top of the stack. |
|
2367 | 2396 | """ |
|
2368 | 2397 | if len (self.shell.dir_stack) > 1: |
|
2369 | 2398 | self.shell.dir_stack.pop(0) |
|
2370 | 2399 | self.magic_cd(self.shell.dir_stack[0]) |
|
2371 | 2400 | print self.shell.dir_stack[0] |
|
2372 | 2401 | else: |
|
2373 | 2402 | print "You can't remove the starting directory from the stack:",\ |
|
2374 | 2403 | self.shell.dir_stack |
|
2375 | 2404 | |
|
2376 | 2405 | def magic_dirs(self, parameter_s=''): |
|
2377 | 2406 | """Return the current directory stack.""" |
|
2378 | 2407 | |
|
2379 | 2408 | return self.shell.dir_stack[:] |
|
2380 | 2409 | |
|
2381 | 2410 | def magic_sc(self, parameter_s=''): |
|
2382 | 2411 | """Shell capture - execute a shell command and capture its output. |
|
2383 | 2412 | |
|
2384 | 2413 | %sc [options] varname=command |
|
2385 | 2414 | |
|
2386 | 2415 | IPython will run the given command using commands.getoutput(), and |
|
2387 | 2416 | will then update the user's interactive namespace with a variable |
|
2388 | 2417 | called varname, containing the value of the call. Your command can |
|
2389 | 2418 | contain shell wildcards, pipes, etc. |
|
2390 | 2419 | |
|
2391 | 2420 | The '=' sign in the syntax is mandatory, and the variable name you |
|
2392 | 2421 | supply must follow Python's standard conventions for valid names. |
|
2393 | 2422 | |
|
2394 | 2423 | Options: |
|
2395 | 2424 | |
|
2396 | 2425 | -l: list output. Split the output on newlines into a list before |
|
2397 | 2426 | assigning it to the given variable. By default the output is stored |
|
2398 | 2427 | as a single string. |
|
2399 | 2428 | |
|
2400 | 2429 | -v: verbose. Print the contents of the variable. |
|
2401 | 2430 | |
|
2402 | 2431 | In most cases you should not need to split as a list, because the |
|
2403 | 2432 | returned value is a special type of string which can automatically |
|
2404 | 2433 | provide its contents either as a list (split on newlines) or as a |
|
2405 | 2434 | space-separated string. These are convenient, respectively, either |
|
2406 | 2435 | for sequential processing or to be passed to a shell command. |
|
2407 | 2436 | |
|
2408 | 2437 | For example: |
|
2409 | 2438 | |
|
2410 | 2439 | # Capture into variable a |
|
2411 | 2440 | In [9]: sc a=ls *py |
|
2412 | 2441 | |
|
2413 | 2442 | # a is a string with embedded newlines |
|
2414 | 2443 | In [10]: a |
|
2415 | 2444 | Out[10]: 'setup.py\nwin32_manual_post_install.py' |
|
2416 | 2445 | |
|
2417 | 2446 | # which can be seen as a list: |
|
2418 | 2447 | In [11]: a.l |
|
2419 | 2448 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] |
|
2420 | 2449 | |
|
2421 | 2450 | # or as a whitespace-separated string: |
|
2422 | 2451 | In [12]: a.s |
|
2423 | 2452 | Out[12]: 'setup.py win32_manual_post_install.py' |
|
2424 | 2453 | |
|
2425 | 2454 | # a.s is useful to pass as a single command line: |
|
2426 | 2455 | In [13]: !wc -l $a.s |
|
2427 | 2456 | 146 setup.py |
|
2428 | 2457 | 130 win32_manual_post_install.py |
|
2429 | 2458 | 276 total |
|
2430 | 2459 | |
|
2431 | 2460 | # while the list form is useful to loop over: |
|
2432 | 2461 | In [14]: for f in a.l: |
|
2433 | 2462 | ....: !wc -l $f |
|
2434 | 2463 | ....: |
|
2435 | 2464 | 146 setup.py |
|
2436 | 2465 | 130 win32_manual_post_install.py |
|
2437 | 2466 | |
|
2438 | 2467 | Similiarly, the lists returned by the -l option are also special, in |
|
2439 | 2468 | the sense that you can equally invoke the .s attribute on them to |
|
2440 | 2469 | automatically get a whitespace-separated string from their contents: |
|
2441 | 2470 | |
|
2442 | 2471 | In [1]: sc -l b=ls *py |
|
2443 | 2472 | |
|
2444 | 2473 | In [2]: b |
|
2445 | 2474 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] |
|
2446 | 2475 | |
|
2447 | 2476 | In [3]: b.s |
|
2448 | 2477 | Out[3]: 'setup.py win32_manual_post_install.py' |
|
2449 | 2478 | |
|
2450 | 2479 | In summary, both the lists and strings used for ouptut capture have |
|
2451 | 2480 | the following special attributes: |
|
2452 | 2481 | |
|
2453 | 2482 | .l (or .list) : value as list. |
|
2454 | 2483 | .n (or .nlstr): value as newline-separated string. |
|
2455 | 2484 | .s (or .spstr): value as space-separated string. |
|
2456 | 2485 | """ |
|
2457 | 2486 | |
|
2458 | 2487 | opts,args = self.parse_options(parameter_s,'lv') |
|
2459 | 2488 | # Try to get a variable name and command to run |
|
2460 | 2489 | try: |
|
2461 | 2490 | # the variable name must be obtained from the parse_options |
|
2462 | 2491 | # output, which uses shlex.split to strip options out. |
|
2463 | 2492 | var,_ = args.split('=',1) |
|
2464 | 2493 | var = var.strip() |
|
2465 | 2494 | # But the the command has to be extracted from the original input |
|
2466 | 2495 | # parameter_s, not on what parse_options returns, to avoid the |
|
2467 | 2496 | # quote stripping which shlex.split performs on it. |
|
2468 | 2497 | _,cmd = parameter_s.split('=',1) |
|
2469 | 2498 | except ValueError: |
|
2470 | 2499 | var,cmd = '','' |
|
2471 | 2500 | if not var: |
|
2472 | 2501 | error('you must specify a variable to assign the command to.') |
|
2473 | 2502 | return |
|
2474 | 2503 | # If all looks ok, proceed |
|
2475 | 2504 | out,err = self.shell.getoutputerror(cmd) |
|
2476 | 2505 | if err: |
|
2477 | 2506 | print >> Term.cerr,err |
|
2478 | 2507 | if opts.has_key('l'): |
|
2479 | 2508 | out = SList(out.split('\n')) |
|
2480 | 2509 | else: |
|
2481 | 2510 | out = LSString(out) |
|
2482 | 2511 | if opts.has_key('v'): |
|
2483 | 2512 | print '%s ==\n%s' % (var,pformat(out)) |
|
2484 | 2513 | self.shell.user_ns.update({var:out}) |
|
2485 | 2514 | |
|
2486 | 2515 | def magic_sx(self, parameter_s=''): |
|
2487 | 2516 | """Shell execute - run a shell command and capture its output. |
|
2488 | 2517 | |
|
2489 | 2518 | %sx command |
|
2490 | 2519 | |
|
2491 | 2520 | IPython will run the given command using commands.getoutput(), and |
|
2492 | 2521 | return the result formatted as a list (split on '\\n'). Since the |
|
2493 | 2522 | output is _returned_, it will be stored in ipython's regular output |
|
2494 | 2523 | cache Out[N] and in the '_N' automatic variables. |
|
2495 | 2524 | |
|
2496 | 2525 | Notes: |
|
2497 | 2526 | |
|
2498 | 2527 | 1) If an input line begins with '!!', then %sx is automatically |
|
2499 | 2528 | invoked. That is, while: |
|
2500 | 2529 | !ls |
|
2501 | 2530 | causes ipython to simply issue system('ls'), typing |
|
2502 | 2531 | !!ls |
|
2503 | 2532 | is a shorthand equivalent to: |
|
2504 | 2533 | %sx ls |
|
2505 | 2534 | |
|
2506 | 2535 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
2507 | 2536 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
2508 | 2537 | to process line-oriented shell output via further python commands. |
|
2509 | 2538 | %sc is meant to provide much finer control, but requires more |
|
2510 | 2539 | typing. |
|
2511 | 2540 | |
|
2512 | 2541 | 3) Just like %sc -l, this is a list with special attributes: |
|
2513 | 2542 | |
|
2514 | 2543 | .l (or .list) : value as list. |
|
2515 | 2544 | .n (or .nlstr): value as newline-separated string. |
|
2516 | 2545 | .s (or .spstr): value as whitespace-separated string. |
|
2517 | 2546 | |
|
2518 | 2547 | This is very useful when trying to use such lists as arguments to |
|
2519 | 2548 | system commands.""" |
|
2520 | 2549 | |
|
2521 | 2550 | if parameter_s: |
|
2522 | 2551 | out,err = self.shell.getoutputerror(parameter_s) |
|
2523 | 2552 | if err: |
|
2524 | 2553 | print >> Term.cerr,err |
|
2525 | 2554 | return SList(out.split('\n')) |
|
2526 | 2555 | |
|
2527 | 2556 | def magic_bg(self, parameter_s=''): |
|
2528 | 2557 | """Run a job in the background, in a separate thread. |
|
2529 | 2558 | |
|
2530 | 2559 | For example, |
|
2531 | 2560 | |
|
2532 | 2561 | %bg myfunc(x,y,z=1) |
|
2533 | 2562 | |
|
2534 | 2563 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the |
|
2535 | 2564 | execution starts, a message will be printed indicating the job |
|
2536 | 2565 | number. If your job number is 5, you can use |
|
2537 | 2566 | |
|
2538 | 2567 | myvar = jobs.result(5) or myvar = jobs[5].result |
|
2539 | 2568 | |
|
2540 | 2569 | to assign this result to variable 'myvar'. |
|
2541 | 2570 | |
|
2542 | 2571 | IPython has a job manager, accessible via the 'jobs' object. You can |
|
2543 | 2572 | type jobs? to get more information about it, and use jobs.<TAB> to see |
|
2544 | 2573 | its attributes. All attributes not starting with an underscore are |
|
2545 | 2574 | meant for public use. |
|
2546 | 2575 | |
|
2547 | 2576 | In particular, look at the jobs.new() method, which is used to create |
|
2548 | 2577 | new jobs. This magic %bg function is just a convenience wrapper |
|
2549 | 2578 | around jobs.new(), for expression-based jobs. If you want to create a |
|
2550 | 2579 | new job with an explicit function object and arguments, you must call |
|
2551 | 2580 | jobs.new() directly. |
|
2552 | 2581 | |
|
2553 | 2582 | The jobs.new docstring also describes in detail several important |
|
2554 | 2583 | caveats associated with a thread-based model for background job |
|
2555 | 2584 | execution. Type jobs.new? for details. |
|
2556 | 2585 | |
|
2557 | 2586 | You can check the status of all jobs with jobs.status(). |
|
2558 | 2587 | |
|
2559 | 2588 | The jobs variable is set by IPython into the Python builtin namespace. |
|
2560 | 2589 | If you ever declare a variable named 'jobs', you will shadow this |
|
2561 | 2590 | name. You can either delete your global jobs variable to regain |
|
2562 | 2591 | access to the job manager, or make a new name and assign it manually |
|
2563 | 2592 | to the manager (stored in IPython's namespace). For example, to |
|
2564 | 2593 | assign the job manager to the Jobs name, use: |
|
2565 | 2594 | |
|
2566 | 2595 | Jobs = __builtins__.jobs""" |
|
2567 | 2596 | |
|
2568 | 2597 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2569 | 2598 | |
|
2570 | 2599 | def magic_store(self, parameter_s=''): |
|
2571 | 2600 | """Lightweight persistence for python variables. |
|
2572 | 2601 | |
|
2573 | 2602 | Example: |
|
2574 | 2603 | |
|
2575 | 2604 | ville@badger[~]|1> A = ['hello',10,'world']\\ |
|
2576 | 2605 | ville@badger[~]|2> %store A\\ |
|
2577 | 2606 | ville@badger[~]|3> Exit |
|
2578 | 2607 | |
|
2579 | 2608 | (IPython session is closed and started again...) |
|
2580 | 2609 | |
|
2581 | 2610 | ville@badger:~$ ipython -p pysh\\ |
|
2582 | 2611 | ville@badger[~]|1> print A |
|
2583 | 2612 | |
|
2584 | 2613 | ['hello', 10, 'world'] |
|
2585 | 2614 | |
|
2586 | 2615 | Usage: |
|
2587 | 2616 | |
|
2588 | 2617 | %store - Show list of all variables and their current values\\ |
|
2589 | 2618 | %store <var> - Store the *current* value of the variable to disk\\ |
|
2590 | 2619 | %store -d <var> - Remove the variable and its value from storage\\ |
|
2591 | 2620 | %store -r - Remove all variables from storage |
|
2592 | 2621 | |
|
2593 | 2622 | It should be noted that if you change the value of a variable, you |
|
2594 | 2623 | need to %store it again if you want to persist the new value. |
|
2595 | 2624 | |
|
2596 | 2625 | Note also that the variables will need to be pickleable; most basic |
|
2597 | 2626 | python types can be safely %stored. |
|
2598 | 2627 | """ |
|
2599 | 2628 | |
|
2600 | 2629 | opts,args = self.parse_options(parameter_s,'dr',mode='list') |
|
2601 | 2630 | # delete |
|
2602 | 2631 | if opts.has_key('d'): |
|
2603 | 2632 | try: |
|
2604 | 2633 | todel = args[0] |
|
2605 | 2634 | except IndexError: |
|
2606 | 2635 | error('You must provide the variable to forget') |
|
2607 | 2636 | else: |
|
2608 | 2637 | try: |
|
2609 | 2638 | del self.shell.persist['S:' + todel] |
|
2610 | 2639 | except: |
|
2611 | 2640 | error("Can't delete variable '%s'" % todel) |
|
2612 | 2641 | # reset |
|
2613 | 2642 | elif opts.has_key('r'): |
|
2614 | 2643 | for k in self.shell.persist.keys(): |
|
2615 | 2644 | if k.startswith('S:'): |
|
2616 | 2645 | del self.shell.persist[k] |
|
2617 | 2646 | |
|
2618 | 2647 | # run without arguments -> list variables & values |
|
2619 | 2648 | elif not args: |
|
2620 | 2649 | vars = [v[2:] for v in self.shell.persist.keys() |
|
2621 | 2650 | if v.startswith('S:')] |
|
2622 | 2651 | vars.sort() |
|
2623 | 2652 | if vars: |
|
2624 | 2653 | size = max(map(len,vars)) |
|
2625 | 2654 | else: |
|
2626 | 2655 | size = 0 |
|
2627 | 2656 | |
|
2628 | 2657 | print 'Stored variables and their in-memory values:' |
|
2629 | 2658 | fmt = '%-'+str(size)+'s -> %s' |
|
2630 | 2659 | get = self.shell.user_ns.get |
|
2631 | 2660 | for var in vars: |
|
2632 | 2661 | # print 30 first characters from every var |
|
2633 | 2662 | print fmt % (var,repr(get(var,'<unavailable>'))[:50]) |
|
2634 | 2663 | |
|
2635 | 2664 | # default action - store the variable |
|
2636 | 2665 | else: |
|
2637 | 2666 | pickled = pickle.dumps(self.shell.user_ns[args[0] ]) |
|
2638 | 2667 | self.shell.persist[ 'S:' + args[0] ] = pickled |
|
2639 | 2668 | print "Stored '%s' (%d bytes)" % (args[0], len(pickled)) |
|
2640 | 2669 | |
|
2641 | 2670 | def magic_bookmark(self, parameter_s=''): |
|
2642 | 2671 | """Manage IPython's bookmark system. |
|
2643 | 2672 | |
|
2644 | 2673 | %bookmark <name> - set bookmark to current dir |
|
2645 | 2674 | %bookmark <name> <dir> - set bookmark to <dir> |
|
2646 | 2675 | %bookmark -l - list all bookmarks |
|
2647 | 2676 | %bookmark -d <name> - remove bookmark |
|
2648 | 2677 | %bookmark -r - remove all bookmarks |
|
2649 | 2678 | |
|
2650 | 2679 | You can later on access a bookmarked folder with: |
|
2651 | 2680 | %cd -b <name> |
|
2652 | 2681 | or simply '%cd <name>' if there is no directory called <name> AND |
|
2653 | 2682 | there is such a bookmark defined. |
|
2654 | 2683 | |
|
2655 | 2684 | Your bookmarks persist through IPython sessions, but they are |
|
2656 | 2685 | associated with each profile.""" |
|
2657 | 2686 | |
|
2658 | 2687 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
2659 | 2688 | if len(args) > 2: |
|
2660 | 2689 | error('You can only give at most two arguments') |
|
2661 | 2690 | return |
|
2662 | 2691 | |
|
2663 | 2692 | bkms = self.shell.persist.get('bookmarks',{}) |
|
2664 | 2693 | |
|
2665 | 2694 | if opts.has_key('d'): |
|
2666 | 2695 | try: |
|
2667 | 2696 | todel = args[0] |
|
2668 | 2697 | except IndexError: |
|
2669 | 2698 | error('You must provide a bookmark to delete') |
|
2670 | 2699 | else: |
|
2671 | 2700 | try: |
|
2672 | 2701 | del bkms[todel] |
|
2673 | 2702 | except: |
|
2674 | 2703 | error("Can't delete bookmark '%s'" % todel) |
|
2675 | 2704 | elif opts.has_key('r'): |
|
2676 | 2705 | bkms = {} |
|
2677 | 2706 | elif opts.has_key('l'): |
|
2678 | 2707 | bks = bkms.keys() |
|
2679 | 2708 | bks.sort() |
|
2680 | 2709 | if bks: |
|
2681 | 2710 | size = max(map(len,bks)) |
|
2682 | 2711 | else: |
|
2683 | 2712 | size = 0 |
|
2684 | 2713 | fmt = '%-'+str(size)+'s -> %s' |
|
2685 | 2714 | print 'Current bookmarks:' |
|
2686 | 2715 | for bk in bks: |
|
2687 | 2716 | print fmt % (bk,bkms[bk]) |
|
2688 | 2717 | else: |
|
2689 | 2718 | if not args: |
|
2690 | 2719 | error("You must specify the bookmark name") |
|
2691 | 2720 | elif len(args)==1: |
|
2692 | 2721 | bkms[args[0]] = os.getcwd() |
|
2693 | 2722 | elif len(args)==2: |
|
2694 | 2723 | bkms[args[0]] = args[1] |
|
2695 | 2724 | self.shell.persist['bookmarks'] = bkms |
|
2696 | 2725 | |
|
2697 | 2726 | def magic_pycat(self, parameter_s=''): |
|
2698 | 2727 | """Show a syntax-highlighted file through a pager. |
|
2699 | 2728 | |
|
2700 | 2729 | This magic is similar to the cat utility, but it will assume the file |
|
2701 | 2730 | to be Python source and will show it with syntax highlighting. """ |
|
2702 | 2731 | |
|
2703 | 2732 | filename = get_py_filename(parameter_s) |
|
2704 | 2733 | page(self.shell.colorize(file_read(filename)), |
|
2705 | 2734 | screen_lines=self.shell.rc.screen_length) |
|
2706 | 2735 | |
|
2707 | 2736 | # end Magic |
@@ -1,583 +1,583 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Classes for handling input/output prompts. |
|
4 | 4 | |
|
5 |
$Id: Prompts.py 9 |
|
|
5 | $Id: Prompts.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
6 | 6 | |
|
7 | 7 | #***************************************************************************** |
|
8 |
# Copyright (C) 2001-200 |
|
|
8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | from IPython import Release |
|
15 | 15 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
16 | 16 | __license__ = Release.license |
|
17 | 17 | __version__ = Release.version |
|
18 | 18 | |
|
19 | 19 | #**************************************************************************** |
|
20 | 20 | # Required modules |
|
21 | 21 | import __builtin__ |
|
22 | 22 | import os |
|
23 | 23 | import socket |
|
24 | 24 | import sys |
|
25 | 25 | import time |
|
26 | 26 | from pprint import pprint,pformat |
|
27 | 27 | |
|
28 | 28 | # IPython's own |
|
29 | 29 | from IPython import ColorANSI |
|
30 | 30 | from IPython.Itpl import ItplNS |
|
31 | 31 | from IPython.Struct import Struct |
|
32 | 32 | from IPython.macro import Macro |
|
33 | 33 | from IPython.genutils import * |
|
34 | 34 | |
|
35 | 35 | #**************************************************************************** |
|
36 | 36 | #Color schemes for Prompts. |
|
37 | 37 | |
|
38 | 38 | PromptColors = ColorANSI.ColorSchemeTable() |
|
39 | 39 | InputColors = ColorANSI.InputTermColors # just a shorthand |
|
40 | 40 | Colors = ColorANSI.TermColors # just a shorthand |
|
41 | 41 | |
|
42 | 42 | PromptColors.add_scheme(ColorANSI.ColorScheme( |
|
43 | 43 | 'NoColor', |
|
44 | 44 | in_prompt = InputColors.NoColor, # Input prompt |
|
45 | 45 | in_number = InputColors.NoColor, # Input prompt number |
|
46 | 46 | in_prompt2 = InputColors.NoColor, # Continuation prompt |
|
47 | 47 | in_normal = InputColors.NoColor, # color off (usu. Colors.Normal) |
|
48 | 48 | |
|
49 | 49 | out_prompt = Colors.NoColor, # Output prompt |
|
50 | 50 | out_number = Colors.NoColor, # Output prompt number |
|
51 | 51 | |
|
52 | 52 | normal = Colors.NoColor # color off (usu. Colors.Normal) |
|
53 | 53 | )) |
|
54 | 54 | |
|
55 | 55 | # make some schemes as instances so we can copy them for modification easily: |
|
56 | 56 | __PColLinux = ColorANSI.ColorScheme( |
|
57 | 57 | 'Linux', |
|
58 | 58 | in_prompt = InputColors.Green, |
|
59 | 59 | in_number = InputColors.LightGreen, |
|
60 | 60 | in_prompt2 = InputColors.Green, |
|
61 | 61 | in_normal = InputColors.Normal, # color off (usu. Colors.Normal) |
|
62 | 62 | |
|
63 | 63 | out_prompt = Colors.Red, |
|
64 | 64 | out_number = Colors.LightRed, |
|
65 | 65 | |
|
66 | 66 | normal = Colors.Normal |
|
67 | 67 | ) |
|
68 | 68 | # Don't forget to enter it into the table! |
|
69 | 69 | PromptColors.add_scheme(__PColLinux) |
|
70 | 70 | |
|
71 | 71 | # Slightly modified Linux for light backgrounds |
|
72 | 72 | __PColLightBG = __PColLinux.copy('LightBG') |
|
73 | 73 | |
|
74 | 74 | __PColLightBG.colors.update( |
|
75 | 75 | in_prompt = InputColors.Blue, |
|
76 | 76 | in_number = InputColors.LightBlue, |
|
77 | 77 | in_prompt2 = InputColors.Blue |
|
78 | 78 | ) |
|
79 | 79 | PromptColors.add_scheme(__PColLightBG) |
|
80 | 80 | |
|
81 | 81 | del Colors,InputColors |
|
82 | 82 | |
|
83 | 83 | #----------------------------------------------------------------------------- |
|
84 | 84 | def multiple_replace(dict, text): |
|
85 | 85 | """ Replace in 'text' all occurences of any key in the given |
|
86 | 86 | dictionary by its corresponding value. Returns the new string.""" |
|
87 | 87 | |
|
88 | 88 | # Function by Xavier Defrang, originally found at: |
|
89 | 89 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 |
|
90 | 90 | |
|
91 | 91 | # Create a regular expression from the dictionary keys |
|
92 | 92 | regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) |
|
93 | 93 | # For each match, look-up corresponding value in dictionary |
|
94 | 94 | return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text) |
|
95 | 95 | |
|
96 | 96 | #----------------------------------------------------------------------------- |
|
97 | 97 | # Special characters that can be used in prompt templates, mainly bash-like |
|
98 | 98 | |
|
99 | 99 | # If $HOME isn't defined (Windows), make it an absurd string so that it can |
|
100 | 100 | # never be expanded out into '~'. Basically anything which can never be a |
|
101 | 101 | # reasonable directory name will do, we just want the $HOME -> '~' operation |
|
102 | 102 | # to become a no-op. We pre-compute $HOME here so it's not done on every |
|
103 | 103 | # prompt call. |
|
104 | 104 | |
|
105 | 105 | # FIXME: |
|
106 | 106 | |
|
107 | 107 | # - This should be turned into a class which does proper namespace management, |
|
108 | 108 | # since the prompt specials need to be evaluated in a certain namespace. |
|
109 | 109 | # Currently it's just globals, which need to be managed manually by code |
|
110 | 110 | # below. |
|
111 | 111 | |
|
112 | 112 | # - I also need to split up the color schemes from the prompt specials |
|
113 | 113 | # somehow. I don't have a clean design for that quite yet. |
|
114 | 114 | |
|
115 | 115 | HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~") |
|
116 | 116 | |
|
117 | 117 | # We precompute a few more strings here for the prompt_specials, which are |
|
118 | 118 | # fixed once ipython starts. This reduces the runtime overhead of computing |
|
119 | 119 | # prompt strings. |
|
120 | 120 | USER = os.environ.get("USER") |
|
121 | 121 | HOSTNAME = socket.gethostname() |
|
122 | 122 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] |
|
123 | 123 | ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0] |
|
124 | 124 | |
|
125 | 125 | prompt_specials_color = { |
|
126 | 126 | # Prompt/history count |
|
127 | 127 | '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
128 | 128 | '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
129 | 129 | # Prompt/history count, with the actual digits replaced by dots. Used |
|
130 | 130 | # mainly in continuation prompts (prompt_in2) |
|
131 | 131 | '\\D': '${"."*len(str(self.cache.prompt_count))}', |
|
132 | 132 | # Current working directory |
|
133 | 133 | '\\w': '${os.getcwd()}', |
|
134 | 134 | # Current time |
|
135 | 135 | '\\t' : '${time.strftime("%H:%M:%S")}', |
|
136 | 136 | # Basename of current working directory. |
|
137 | 137 | # (use os.sep to make this portable across OSes) |
|
138 | 138 | '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep, |
|
139 | 139 | # These X<N> are an extension to the normal bash prompts. They return |
|
140 | 140 | # N terms of the path, after replacing $HOME with '~' |
|
141 | 141 | '\\X0': '${os.getcwd().replace("%s","~")}' % HOME, |
|
142 | 142 | '\\X1': '${self.cwd_filt(1)}', |
|
143 | 143 | '\\X2': '${self.cwd_filt(2)}', |
|
144 | 144 | '\\X3': '${self.cwd_filt(3)}', |
|
145 | 145 | '\\X4': '${self.cwd_filt(4)}', |
|
146 | 146 | '\\X5': '${self.cwd_filt(5)}', |
|
147 | 147 | # Y<N> are similar to X<N>, but they show '~' if it's the directory |
|
148 | 148 | # N+1 in the list. Somewhat like %cN in tcsh. |
|
149 | 149 | '\\Y0': '${self.cwd_filt2(0)}', |
|
150 | 150 | '\\Y1': '${self.cwd_filt2(1)}', |
|
151 | 151 | '\\Y2': '${self.cwd_filt2(2)}', |
|
152 | 152 | '\\Y3': '${self.cwd_filt2(3)}', |
|
153 | 153 | '\\Y4': '${self.cwd_filt2(4)}', |
|
154 | 154 | '\\Y5': '${self.cwd_filt2(5)}', |
|
155 | 155 | # Hostname up to first . |
|
156 | 156 | '\\h': HOSTNAME_SHORT, |
|
157 | 157 | # Full hostname |
|
158 | 158 | '\\H': HOSTNAME, |
|
159 | 159 | # Username of current user |
|
160 | 160 | '\\u': USER, |
|
161 | 161 | # Escaped '\' |
|
162 | 162 | '\\\\': '\\', |
|
163 | 163 | # Newline |
|
164 | 164 | '\\n': '\n', |
|
165 | 165 | # Carriage return |
|
166 | 166 | '\\r': '\r', |
|
167 | 167 | # Release version |
|
168 | 168 | '\\v': __version__, |
|
169 | 169 | # Root symbol ($ or #) |
|
170 | 170 | '\\$': ROOT_SYMBOL, |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | # A copy of the prompt_specials dictionary but with all color escapes removed, |
|
174 | 174 | # so we can correctly compute the prompt length for the auto_rewrite method. |
|
175 | 175 | prompt_specials_nocolor = prompt_specials_color.copy() |
|
176 | 176 | prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}' |
|
177 | 177 | prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}' |
|
178 | 178 | |
|
179 | 179 | # Add in all the InputTermColors color escapes as valid prompt characters. |
|
180 | 180 | # They all get added as \\C_COLORNAME, so that we don't have any conflicts |
|
181 | 181 | # with a color name which may begin with a letter used by any other of the |
|
182 | 182 | # allowed specials. This of course means that \\C will never be allowed for |
|
183 | 183 | # anything else. |
|
184 | 184 | input_colors = ColorANSI.InputTermColors |
|
185 | 185 | for _color in dir(input_colors): |
|
186 | 186 | if _color[0] != '_': |
|
187 | 187 | c_name = '\\C_'+_color |
|
188 | 188 | prompt_specials_color[c_name] = getattr(input_colors,_color) |
|
189 | 189 | prompt_specials_nocolor[c_name] = '' |
|
190 | 190 | |
|
191 | 191 | # we default to no color for safety. Note that prompt_specials is a global |
|
192 | 192 | # variable used by all prompt objects. |
|
193 | 193 | prompt_specials = prompt_specials_nocolor |
|
194 | 194 | |
|
195 | 195 | #----------------------------------------------------------------------------- |
|
196 | 196 | def str_safe(arg): |
|
197 | 197 | """Convert to a string, without ever raising an exception. |
|
198 | 198 | |
|
199 | 199 | If str(arg) fails, <ERROR: ... > is returned, where ... is the exception |
|
200 | 200 | error message.""" |
|
201 | 201 | |
|
202 | 202 | try: |
|
203 | 203 | out = str(arg) |
|
204 | 204 | except UnicodeError: |
|
205 | 205 | try: |
|
206 | 206 | out = arg.encode('utf_8','replace') |
|
207 | 207 | except Exception,msg: |
|
208 | 208 | # let's keep this little duplication here, so that the most common |
|
209 | 209 | # case doesn't suffer from a double try wrapping. |
|
210 | 210 | out = '<ERROR: %s>' % msg |
|
211 | 211 | except Exception,msg: |
|
212 | 212 | out = '<ERROR: %s>' % msg |
|
213 | 213 | return out |
|
214 | 214 | |
|
215 | 215 | class BasePrompt: |
|
216 | 216 | """Interactive prompt similar to Mathematica's.""" |
|
217 | 217 | def __init__(self,cache,sep,prompt,pad_left=False): |
|
218 | 218 | |
|
219 | 219 | # Hack: we access information about the primary prompt through the |
|
220 | 220 | # cache argument. We need this, because we want the secondary prompt |
|
221 | 221 | # to be aligned with the primary one. Color table info is also shared |
|
222 | 222 | # by all prompt classes through the cache. Nice OO spaghetti code! |
|
223 | 223 | self.cache = cache |
|
224 | 224 | self.sep = sep |
|
225 | 225 | |
|
226 | 226 | # regexp to count the number of spaces at the end of a prompt |
|
227 | 227 | # expression, useful for prompt auto-rewriting |
|
228 | 228 | self.rspace = re.compile(r'(\s*)$') |
|
229 | 229 | # Flag to left-pad prompt strings to match the length of the primary |
|
230 | 230 | # prompt |
|
231 | 231 | self.pad_left = pad_left |
|
232 | 232 | # Set template to create each actual prompt (where numbers change) |
|
233 | 233 | self.p_template = prompt |
|
234 | 234 | self.set_p_str() |
|
235 | 235 | |
|
236 | 236 | def set_p_str(self): |
|
237 | 237 | """ Set the interpolating prompt strings. |
|
238 | 238 | |
|
239 | 239 | This must be called every time the color settings change, because the |
|
240 | 240 | prompt_specials global may have changed.""" |
|
241 | 241 | |
|
242 | 242 | import os,time # needed in locals for prompt string handling |
|
243 | 243 | loc = locals() |
|
244 | 244 | self.p_str = ItplNS('%s%s%s' % |
|
245 | 245 | ('${self.sep}${self.col_p}', |
|
246 | 246 | multiple_replace(prompt_specials, self.p_template), |
|
247 | 247 | '${self.col_norm}'),self.cache.user_ns,loc) |
|
248 | 248 | |
|
249 | 249 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
250 | 250 | self.p_template), |
|
251 | 251 | self.cache.user_ns,loc) |
|
252 | 252 | |
|
253 | 253 | def write(self,msg): # dbg |
|
254 | 254 | sys.stdout.write(msg) |
|
255 | 255 | return '' |
|
256 | 256 | |
|
257 | 257 | def __str__(self): |
|
258 | 258 | """Return a string form of the prompt. |
|
259 | 259 | |
|
260 | 260 | This for is useful for continuation and output prompts, since it is |
|
261 | 261 | left-padded to match lengths with the primary one (if the |
|
262 | 262 | self.pad_left attribute is set).""" |
|
263 | 263 | |
|
264 | 264 | out_str = str_safe(self.p_str) |
|
265 | 265 | if self.pad_left: |
|
266 | 266 | # We must find the amount of padding required to match lengths, |
|
267 | 267 | # taking the color escapes (which are invisible on-screen) into |
|
268 | 268 | # account. |
|
269 | 269 | esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor)) |
|
270 | 270 | format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad) |
|
271 | 271 | return format % out_str |
|
272 | 272 | else: |
|
273 | 273 | return out_str |
|
274 | 274 | |
|
275 | 275 | # these path filters are put in as methods so that we can control the |
|
276 | 276 | # namespace where the prompt strings get evaluated |
|
277 | 277 | def cwd_filt(self,depth): |
|
278 | 278 | """Return the last depth elements of the current working directory. |
|
279 | 279 | |
|
280 | 280 | $HOME is always replaced with '~'. |
|
281 | 281 | If depth==0, the full path is returned.""" |
|
282 | 282 | |
|
283 | 283 | cwd = os.getcwd().replace(HOME,"~") |
|
284 | 284 | out = os.sep.join(cwd.split(os.sep)[-depth:]) |
|
285 | 285 | if out: |
|
286 | 286 | return out |
|
287 | 287 | else: |
|
288 | 288 | return os.sep |
|
289 | 289 | |
|
290 | 290 | def cwd_filt2(self,depth): |
|
291 | 291 | """Return the last depth elements of the current working directory. |
|
292 | 292 | |
|
293 | 293 | $HOME is always replaced with '~'. |
|
294 | 294 | If depth==0, the full path is returned.""" |
|
295 | 295 | |
|
296 | 296 | cwd = os.getcwd().replace(HOME,"~").split(os.sep) |
|
297 | 297 | if '~' in cwd and len(cwd) == depth+1: |
|
298 | 298 | depth += 1 |
|
299 | 299 | out = os.sep.join(cwd[-depth:]) |
|
300 | 300 | if out: |
|
301 | 301 | return out |
|
302 | 302 | else: |
|
303 | 303 | return os.sep |
|
304 | 304 | |
|
305 | 305 | class Prompt1(BasePrompt): |
|
306 | 306 | """Input interactive prompt similar to Mathematica's.""" |
|
307 | 307 | |
|
308 | 308 | def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True): |
|
309 | 309 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
310 | 310 | |
|
311 | 311 | def set_colors(self): |
|
312 | 312 | self.set_p_str() |
|
313 | 313 | Colors = self.cache.color_table.active_colors # shorthand |
|
314 | 314 | self.col_p = Colors.in_prompt |
|
315 | 315 | self.col_num = Colors.in_number |
|
316 | 316 | self.col_norm = Colors.in_normal |
|
317 | 317 | # We need a non-input version of these escapes for the '--->' |
|
318 | 318 | # auto-call prompts used in the auto_rewrite() method. |
|
319 | 319 | self.col_p_ni = self.col_p.replace('\001','').replace('\002','') |
|
320 | 320 | self.col_norm_ni = Colors.normal |
|
321 | 321 | |
|
322 | 322 | def __str__(self): |
|
323 | 323 | self.cache.prompt_count += 1 |
|
324 | 324 | self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1] |
|
325 | 325 | return str_safe(self.p_str) |
|
326 | 326 | |
|
327 | 327 | def auto_rewrite(self): |
|
328 | 328 | """Print a string of the form '--->' which lines up with the previous |
|
329 | 329 | input string. Useful for systems which re-write the user input when |
|
330 | 330 | handling automatically special syntaxes.""" |
|
331 | 331 | |
|
332 | 332 | curr = str(self.cache.last_prompt) |
|
333 | 333 | nrspaces = len(self.rspace.search(curr).group()) |
|
334 | 334 | return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1), |
|
335 | 335 | ' '*nrspaces,self.col_norm_ni) |
|
336 | 336 | |
|
337 | 337 | class PromptOut(BasePrompt): |
|
338 | 338 | """Output interactive prompt similar to Mathematica's.""" |
|
339 | 339 | |
|
340 | 340 | def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True): |
|
341 | 341 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
342 | 342 | if not self.p_template: |
|
343 | 343 | self.__str__ = lambda: '' |
|
344 | 344 | |
|
345 | 345 | def set_colors(self): |
|
346 | 346 | self.set_p_str() |
|
347 | 347 | Colors = self.cache.color_table.active_colors # shorthand |
|
348 | 348 | self.col_p = Colors.out_prompt |
|
349 | 349 | self.col_num = Colors.out_number |
|
350 | 350 | self.col_norm = Colors.normal |
|
351 | 351 | |
|
352 | 352 | class Prompt2(BasePrompt): |
|
353 | 353 | """Interactive continuation prompt.""" |
|
354 | 354 | |
|
355 | 355 | def __init__(self,cache,prompt=' .\\D.: ',pad_left=True): |
|
356 | 356 | self.cache = cache |
|
357 | 357 | self.p_template = prompt |
|
358 | 358 | self.pad_left = pad_left |
|
359 | 359 | self.set_p_str() |
|
360 | 360 | |
|
361 | 361 | def set_p_str(self): |
|
362 | 362 | import os,time # needed in locals for prompt string handling |
|
363 | 363 | loc = locals() |
|
364 | 364 | self.p_str = ItplNS('%s%s%s' % |
|
365 | 365 | ('${self.col_p2}', |
|
366 | 366 | multiple_replace(prompt_specials, self.p_template), |
|
367 | 367 | '$self.col_norm'), |
|
368 | 368 | self.cache.user_ns,loc) |
|
369 | 369 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
370 | 370 | self.p_template), |
|
371 | 371 | self.cache.user_ns,loc) |
|
372 | 372 | |
|
373 | 373 | def set_colors(self): |
|
374 | 374 | self.set_p_str() |
|
375 | 375 | Colors = self.cache.color_table.active_colors |
|
376 | 376 | self.col_p2 = Colors.in_prompt2 |
|
377 | 377 | self.col_norm = Colors.in_normal |
|
378 | 378 | # FIXME (2004-06-16) HACK: prevent crashes for users who haven't |
|
379 | 379 | # updated their prompt_in2 definitions. Remove eventually. |
|
380 | 380 | self.col_p = Colors.out_prompt |
|
381 | 381 | self.col_num = Colors.out_number |
|
382 | 382 | |
|
383 | 383 | #----------------------------------------------------------------------------- |
|
384 | 384 | class CachedOutput: |
|
385 | 385 | """Class for printing output from calculations while keeping a cache of |
|
386 | 386 | reults. It dynamically creates global variables prefixed with _ which |
|
387 | 387 | contain these results. |
|
388 | 388 | |
|
389 | 389 | Meant to be used as a sys.displayhook replacement, providing numbered |
|
390 | 390 | prompts and cache services. |
|
391 | 391 | |
|
392 | 392 | Initialize with initial and final values for cache counter (this defines |
|
393 | 393 | the maximum size of the cache.""" |
|
394 | 394 | |
|
395 | 395 | def __init__(self,shell,cache_size,Pprint, |
|
396 | 396 | colors='NoColor',input_sep='\n', |
|
397 | 397 | output_sep='\n',output_sep2='', |
|
398 | 398 | ps1 = None, ps2 = None,ps_out = None,pad_left=True): |
|
399 | 399 | |
|
400 | 400 | cache_size_min = 20 |
|
401 | 401 | if cache_size <= 0: |
|
402 | 402 | self.do_full_cache = 0 |
|
403 | 403 | cache_size = 0 |
|
404 | 404 | elif cache_size < cache_size_min: |
|
405 | 405 | self.do_full_cache = 0 |
|
406 | 406 | cache_size = 0 |
|
407 | 407 | warn('caching was disabled (min value for cache size is %s).' % |
|
408 | 408 | cache_size_min,level=3) |
|
409 | 409 | else: |
|
410 | 410 | self.do_full_cache = 1 |
|
411 | 411 | |
|
412 | 412 | self.cache_size = cache_size |
|
413 | 413 | self.input_sep = input_sep |
|
414 | 414 | |
|
415 | 415 | # we need a reference to the user-level namespace |
|
416 | 416 | self.shell = shell |
|
417 | 417 | self.user_ns = shell.user_ns |
|
418 | 418 | # and to the user's input |
|
419 | 419 | self.input_hist = shell.input_hist |
|
420 | 420 | # and to the user's logger, for logging output |
|
421 | 421 | self.logger = shell.logger |
|
422 | 422 | |
|
423 | 423 | # Set input prompt strings and colors |
|
424 | 424 | if cache_size == 0: |
|
425 | 425 | if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> ' |
|
426 | 426 | if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... ' |
|
427 | 427 | self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ') |
|
428 | 428 | self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ') |
|
429 | 429 | self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','') |
|
430 | 430 | |
|
431 | 431 | self.color_table = PromptColors |
|
432 | 432 | self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str, |
|
433 | 433 | pad_left=pad_left) |
|
434 | 434 | self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left) |
|
435 | 435 | self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str, |
|
436 | 436 | pad_left=pad_left) |
|
437 | 437 | self.set_colors(colors) |
|
438 | 438 | |
|
439 | 439 | # other more normal stuff |
|
440 | 440 | # b/c each call to the In[] prompt raises it by 1, even the first. |
|
441 | 441 | self.prompt_count = 0 |
|
442 | 442 | self.cache_count = 1 |
|
443 | 443 | # Store the last prompt string each time, we need it for aligning |
|
444 | 444 | # continuation and auto-rewrite prompts |
|
445 | 445 | self.last_prompt = '' |
|
446 | 446 | self.entries = [None] # output counter starts at 1 for the user |
|
447 | 447 | self.Pprint = Pprint |
|
448 | 448 | self.output_sep = output_sep |
|
449 | 449 | self.output_sep2 = output_sep2 |
|
450 | 450 | self._,self.__,self.___ = '','','' |
|
451 | 451 | self.pprint_types = map(type,[(),[],{}]) |
|
452 | 452 | |
|
453 | 453 | # these are deliberately global: |
|
454 | 454 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
455 | 455 | self.user_ns.update(to_user_ns) |
|
456 | 456 | |
|
457 | 457 | def _set_prompt_str(self,p_str,cache_def,no_cache_def): |
|
458 | 458 | if p_str is None: |
|
459 | 459 | if self.do_full_cache: |
|
460 | 460 | return cache_def |
|
461 | 461 | else: |
|
462 | 462 | return no_cache_def |
|
463 | 463 | else: |
|
464 | 464 | return p_str |
|
465 | 465 | |
|
466 | 466 | def set_colors(self,colors): |
|
467 | 467 | """Set the active color scheme and configure colors for the three |
|
468 | 468 | prompt subsystems.""" |
|
469 | 469 | |
|
470 | 470 | # FIXME: the prompt_specials global should be gobbled inside this |
|
471 | 471 | # class instead. Do it when cleaning up the whole 3-prompt system. |
|
472 | 472 | global prompt_specials |
|
473 | 473 | if colors.lower()=='nocolor': |
|
474 | 474 | prompt_specials = prompt_specials_nocolor |
|
475 | 475 | else: |
|
476 | 476 | prompt_specials = prompt_specials_color |
|
477 | 477 | |
|
478 | 478 | self.color_table.set_active_scheme(colors) |
|
479 | 479 | self.prompt1.set_colors() |
|
480 | 480 | self.prompt2.set_colors() |
|
481 | 481 | self.prompt_out.set_colors() |
|
482 | 482 | |
|
483 | 483 | def __call__(self,arg=None): |
|
484 | 484 | """Printing with history cache management. |
|
485 | 485 | |
|
486 | 486 | This is invoked everytime the interpreter needs to print, and is |
|
487 | 487 | activated by setting the variable sys.displayhook to it.""" |
|
488 | 488 | |
|
489 | 489 | # If something injected a '_' variable in __builtin__, delete |
|
490 | 490 | # ipython's automatic one so we don't clobber that. gettext() in |
|
491 | 491 | # particular uses _, so we need to stay away from it. |
|
492 | 492 | if '_' in __builtin__.__dict__: |
|
493 | 493 | try: |
|
494 | 494 | del self.user_ns['_'] |
|
495 | 495 | except KeyError: |
|
496 | 496 | pass |
|
497 | 497 | if arg is not None: |
|
498 | 498 | cout_write = Term.cout.write # fast lookup |
|
499 | 499 | # first handle the cache and counters |
|
500 | 500 | # but avoid recursive reference when displaying _oh/Out |
|
501 | 501 | if arg is not self.user_ns['_oh']: |
|
502 | 502 | self.update(arg) |
|
503 | 503 | # do not print output if input ends in ';' |
|
504 | 504 | if self.input_hist[self.prompt_count].endswith(';\n'): |
|
505 | 505 | return |
|
506 | 506 | # don't use print, puts an extra space |
|
507 | 507 | cout_write(self.output_sep) |
|
508 | 508 | if self.do_full_cache: |
|
509 | 509 | cout_write(str(self.prompt_out)) |
|
510 | 510 | |
|
511 | 511 | if isinstance(arg,Macro): |
|
512 | 512 | print 'Executing Macro...' |
|
513 | 513 | # in case the macro takes a long time to execute |
|
514 | 514 | Term.cout.flush() |
|
515 | 515 | self.shell.runlines(arg.value) |
|
516 | 516 | return None |
|
517 | 517 | |
|
518 | 518 | # and now call a possibly user-defined print mechanism |
|
519 | 519 | self.display(arg) |
|
520 | 520 | if self.logger.log_output: |
|
521 | 521 | self.logger.log_write(repr(arg),'output') |
|
522 | 522 | cout_write(self.output_sep2) |
|
523 | 523 | Term.cout.flush() |
|
524 | 524 | |
|
525 | 525 | def _display(self,arg): |
|
526 | 526 | """Default printer method, uses pprint. |
|
527 | 527 | |
|
528 | 528 | This can be over-ridden by the users to implement special formatting |
|
529 | 529 | of certain types of output.""" |
|
530 | 530 | |
|
531 | 531 | if self.Pprint: |
|
532 | 532 | out = pformat(arg) |
|
533 | 533 | if '\n' in out: |
|
534 | 534 | # So that multi-line strings line up with the left column of |
|
535 | 535 | # the screen, instead of having the output prompt mess up |
|
536 | 536 | # their first line. |
|
537 | 537 | Term.cout.write('\n') |
|
538 | 538 | print >>Term.cout, out |
|
539 | 539 | else: |
|
540 | 540 | print >>Term.cout, arg |
|
541 | 541 | |
|
542 | 542 | # Assign the default display method: |
|
543 | 543 | display = _display |
|
544 | 544 | |
|
545 | 545 | def update(self,arg): |
|
546 | 546 | #print '***cache_count', self.cache_count # dbg |
|
547 | 547 | if self.cache_count >= self.cache_size and self.do_full_cache: |
|
548 | 548 | self.flush() |
|
549 | 549 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
550 | 550 | # we cause buggy behavior for things like gettext). |
|
551 | 551 | if '_' not in __builtin__.__dict__: |
|
552 | 552 | self.___ = self.__ |
|
553 | 553 | self.__ = self._ |
|
554 | 554 | self._ = arg |
|
555 | 555 | self.user_ns.update({'_':self._,'__':self.__,'___':self.___}) |
|
556 | 556 | |
|
557 | 557 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
558 | 558 | to_main = {} |
|
559 | 559 | if self.do_full_cache: |
|
560 | 560 | self.cache_count += 1 |
|
561 | 561 | self.entries.append(arg) |
|
562 | 562 | new_result = '_'+`self.prompt_count` |
|
563 | 563 | to_main[new_result] = self.entries[-1] |
|
564 | 564 | self.user_ns.update(to_main) |
|
565 | 565 | self.user_ns['_oh'][self.prompt_count] = arg |
|
566 | 566 | |
|
567 | 567 | def flush(self): |
|
568 | 568 | if not self.do_full_cache: |
|
569 | 569 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
570 | 570 | "if full caching is not enabled!" |
|
571 | 571 | warn('Output cache limit (currently '+\ |
|
572 | 572 | `self.cache_count`+' entries) hit.\n' |
|
573 | 573 | 'Flushing cache and resetting history counter...\n' |
|
574 | 574 | 'The only history variables available will be _,__,___ and _1\n' |
|
575 | 575 | 'with the current result.') |
|
576 | 576 | # delete auto-generated vars from global namespace |
|
577 | 577 | for n in range(1,self.prompt_count + 1): |
|
578 | 578 | key = '_'+`n` |
|
579 | 579 | try: |
|
580 | 580 | del self.user_ns[key] |
|
581 | 581 | except: pass |
|
582 | 582 | self.prompt_count = 1 |
|
583 | 583 | self.cache_count = 1 |
@@ -1,76 +1,76 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Release data for the IPython project. |
|
3 | 3 | |
|
4 |
$Id: Release.py 99 |
|
|
4 | $Id: Release.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 |
# Copyright (C) 2001-200 |
|
|
7 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
8 | 8 | # |
|
9 | 9 | # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray |
|
10 | 10 | # <n8gray@caltech.edu> |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | # Name of the package for release purposes. This is the name which labels |
|
17 | 17 | # the tarballs and RPMs made by distutils, so it's best to lowercase it. |
|
18 | 18 | name = 'ipython' |
|
19 | 19 | |
|
20 | 20 | # For versions with substrings (like 0.6.16.svn), use an extra . to separate |
|
21 | 21 | # the new substring. We have to avoid using either dashes or underscores, |
|
22 | 22 | # because bdist_rpm does not accept dashes (an RPM) convention, and |
|
23 | 23 | # bdist_deb does not accept underscores (a Debian convention). |
|
24 | 24 | |
|
25 | 25 | version = '0.7.0.rc8' |
|
26 | 26 | |
|
27 |
revision = '$Revision: 99 |
|
|
27 | revision = '$Revision: 994 $' | |
|
28 | 28 | |
|
29 | 29 | description = "An enhanced interactive Python shell." |
|
30 | 30 | |
|
31 | 31 | long_description = \ |
|
32 | 32 | """ |
|
33 | 33 | IPython provides a replacement for the interactive Python interpreter with |
|
34 | 34 | extra functionality. |
|
35 | 35 | |
|
36 | 36 | Main features: |
|
37 | 37 | |
|
38 | 38 | * Comprehensive object introspection. |
|
39 | 39 | |
|
40 | 40 | * Input history, persistent across sessions. |
|
41 | 41 | |
|
42 | 42 | * Caching of output results during a session with automatically generated |
|
43 | 43 | references. |
|
44 | 44 | |
|
45 | 45 | * Readline based name completion. |
|
46 | 46 | |
|
47 | 47 | * Extensible system of 'magic' commands for controlling the environment and |
|
48 | 48 | performing many tasks related either to IPython or the operating system. |
|
49 | 49 | |
|
50 | 50 | * Configuration system with easy switching between different setups (simpler |
|
51 | 51 | than changing $PYTHONSTARTUP environment variables every time). |
|
52 | 52 | |
|
53 | 53 | * Session logging and reloading. |
|
54 | 54 | |
|
55 | 55 | * Extensible syntax processing for special purpose situations. |
|
56 | 56 | |
|
57 | 57 | * Access to the system shell with user-extensible alias system. |
|
58 | 58 | |
|
59 | 59 | * Easily embeddable in other Python programs. |
|
60 | 60 | |
|
61 | 61 | * Integrated access to the pdb debugger and the Python profiler. """ |
|
62 | 62 | |
|
63 | 63 | license = 'BSD' |
|
64 | 64 | |
|
65 | 65 | authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'), |
|
66 | 66 | 'Janko' : ('Janko Hauser','jhauser@zscout.de'), |
|
67 | 67 | 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu') |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | url = 'http://ipython.scipy.org' |
|
71 | 71 | |
|
72 | 72 | download_url = 'http://ipython.scipy.org/dist' |
|
73 | 73 | |
|
74 | 74 | platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME'] |
|
75 | 75 | |
|
76 | 76 | keywords = ['Interactive','Interpreter','Shell'] |
@@ -1,917 +1,917 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """IPython Shell classes. |
|
3 | 3 | |
|
4 | 4 | All the matplotlib support code was co-developed with John Hunter, |
|
5 | 5 | matplotlib's author. |
|
6 | 6 | |
|
7 |
$Id: Shell.py 99 |
|
|
7 | $Id: Shell.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
8 | 8 | |
|
9 | 9 | #***************************************************************************** |
|
10 |
# Copyright (C) 2001-200 |
|
|
10 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> | |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | from IPython import Release |
|
17 | 17 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
18 | 18 | __license__ = Release.license |
|
19 | 19 | |
|
20 | 20 | # Code begins |
|
21 | 21 | import __main__ |
|
22 | 22 | import __builtin__ |
|
23 | 23 | import os |
|
24 | 24 | import sys |
|
25 | 25 | import signal |
|
26 | 26 | import threading |
|
27 | 27 | |
|
28 | 28 | import IPython |
|
29 | 29 | from IPython import ultraTB |
|
30 | 30 | from IPython.genutils import Term,warn,error,flag_calls |
|
31 | 31 | from IPython.iplib import InteractiveShell |
|
32 | 32 | from IPython.ipmaker import make_IPython |
|
33 | 33 | from IPython.Magic import Magic |
|
34 | 34 | from IPython.Struct import Struct |
|
35 | 35 | |
|
36 | 36 | # global flag to pass around information about Ctrl-C without exceptions |
|
37 | 37 | KBINT = False |
|
38 | 38 | |
|
39 | 39 | # global flag to turn on/off Tk support. |
|
40 | 40 | USE_TK = False |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
|
43 | 43 | # This class is trivial now, but I want to have it in to publish a clean |
|
44 | 44 | # interface. Later when the internals are reorganized, code that uses this |
|
45 | 45 | # shouldn't have to change. |
|
46 | 46 | |
|
47 | 47 | class IPShell: |
|
48 | 48 | """Create an IPython instance.""" |
|
49 | 49 | |
|
50 | 50 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
51 | 51 | debug=1,shell_class=InteractiveShell): |
|
52 | 52 | self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns, |
|
53 | 53 | debug=debug,shell_class=shell_class) |
|
54 | 54 | |
|
55 | 55 | def mainloop(self,sys_exit=0,banner=None): |
|
56 | 56 | self.IP.mainloop(banner) |
|
57 | 57 | if sys_exit: |
|
58 | 58 | sys.exit() |
|
59 | 59 | |
|
60 | 60 | #----------------------------------------------------------------------------- |
|
61 | 61 | class IPShellEmbed: |
|
62 | 62 | """Allow embedding an IPython shell into a running program. |
|
63 | 63 | |
|
64 | 64 | Instances of this class are callable, with the __call__ method being an |
|
65 | 65 | alias to the embed() method of an InteractiveShell instance. |
|
66 | 66 | |
|
67 | 67 | Usage (see also the example-embed.py file for a running example): |
|
68 | 68 | |
|
69 | 69 | ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override]) |
|
70 | 70 | |
|
71 | 71 | - argv: list containing valid command-line options for IPython, as they |
|
72 | 72 | would appear in sys.argv[1:]. |
|
73 | 73 | |
|
74 | 74 | For example, the following command-line options: |
|
75 | 75 | |
|
76 | 76 | $ ipython -prompt_in1 'Input <\\#>' -colors LightBG |
|
77 | 77 | |
|
78 | 78 | would be passed in the argv list as: |
|
79 | 79 | |
|
80 | 80 | ['-prompt_in1','Input <\\#>','-colors','LightBG'] |
|
81 | 81 | |
|
82 | 82 | - banner: string which gets printed every time the interpreter starts. |
|
83 | 83 | |
|
84 | 84 | - exit_msg: string which gets printed every time the interpreter exits. |
|
85 | 85 | |
|
86 | 86 | - rc_override: a dict or Struct of configuration options such as those |
|
87 | 87 | used by IPython. These options are read from your ~/.ipython/ipythonrc |
|
88 | 88 | file when the Shell object is created. Passing an explicit rc_override |
|
89 | 89 | dict with any options you want allows you to override those values at |
|
90 | 90 | creation time without having to modify the file. This way you can create |
|
91 | 91 | embeddable instances configured in any way you want without editing any |
|
92 | 92 | global files (thus keeping your interactive IPython configuration |
|
93 | 93 | unchanged). |
|
94 | 94 | |
|
95 | 95 | Then the ipshell instance can be called anywhere inside your code: |
|
96 | 96 | |
|
97 | 97 | ipshell(header='') -> Opens up an IPython shell. |
|
98 | 98 | |
|
99 | 99 | - header: string printed by the IPython shell upon startup. This can let |
|
100 | 100 | you know where in your code you are when dropping into the shell. Note |
|
101 | 101 | that 'banner' gets prepended to all calls, so header is used for |
|
102 | 102 | location-specific information. |
|
103 | 103 | |
|
104 | 104 | For more details, see the __call__ method below. |
|
105 | 105 | |
|
106 | 106 | When the IPython shell is exited with Ctrl-D, normal program execution |
|
107 | 107 | resumes. |
|
108 | 108 | |
|
109 | 109 | This functionality was inspired by a posting on comp.lang.python by cmkl |
|
110 | 110 | <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and |
|
111 | 111 | by the IDL stop/continue commands.""" |
|
112 | 112 | |
|
113 | 113 | def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None): |
|
114 | 114 | """Note that argv here is a string, NOT a list.""" |
|
115 | 115 | self.set_banner(banner) |
|
116 | 116 | self.set_exit_msg(exit_msg) |
|
117 | 117 | self.set_dummy_mode(0) |
|
118 | 118 | |
|
119 | 119 | # sys.displayhook is a global, we need to save the user's original |
|
120 | 120 | # Don't rely on __displayhook__, as the user may have changed that. |
|
121 | 121 | self.sys_displayhook_ori = sys.displayhook |
|
122 | 122 | |
|
123 | 123 | # save readline completer status |
|
124 | 124 | try: |
|
125 | 125 | #print 'Save completer',sys.ipcompleter # dbg |
|
126 | 126 | self.sys_ipcompleter_ori = sys.ipcompleter |
|
127 | 127 | except: |
|
128 | 128 | pass # not nested with IPython |
|
129 | 129 | |
|
130 | 130 | # FIXME. Passing user_ns breaks namespace handling. |
|
131 | 131 | #self.IP = make_IPython(argv,user_ns=__main__.__dict__) |
|
132 | 132 | self.IP = make_IPython(argv,rc_override=rc_override,embedded=True) |
|
133 | 133 | |
|
134 | 134 | # copy our own displayhook also |
|
135 | 135 | self.sys_displayhook_embed = sys.displayhook |
|
136 | 136 | # and leave the system's display hook clean |
|
137 | 137 | sys.displayhook = self.sys_displayhook_ori |
|
138 | 138 | # don't use the ipython crash handler so that user exceptions aren't |
|
139 | 139 | # trapped |
|
140 | 140 | sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors, |
|
141 | 141 | mode = self.IP.rc.xmode, |
|
142 | 142 | call_pdb = self.IP.rc.pdb) |
|
143 | 143 | self.restore_system_completer() |
|
144 | 144 | |
|
145 | 145 | def restore_system_completer(self): |
|
146 | 146 | """Restores the readline completer which was in place. |
|
147 | 147 | |
|
148 | 148 | This allows embedded IPython within IPython not to disrupt the |
|
149 | 149 | parent's completion. |
|
150 | 150 | """ |
|
151 | 151 | |
|
152 | 152 | try: |
|
153 | 153 | self.IP.readline.set_completer(self.sys_ipcompleter_ori) |
|
154 | 154 | sys.ipcompleter = self.sys_ipcompleter_ori |
|
155 | 155 | except: |
|
156 | 156 | pass |
|
157 | 157 | |
|
158 | 158 | def __call__(self,header='',local_ns=None,global_ns=None,dummy=None): |
|
159 | 159 | """Activate the interactive interpreter. |
|
160 | 160 | |
|
161 | 161 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
162 | 162 | the interpreter shell with the given local and global namespaces, and |
|
163 | 163 | optionally print a header string at startup. |
|
164 | 164 | |
|
165 | 165 | The shell can be globally activated/deactivated using the |
|
166 | 166 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
167 | 167 | for debugging globally. |
|
168 | 168 | |
|
169 | 169 | However, *each* time you call the shell you can override the current |
|
170 | 170 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
171 | 171 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
172 | 172 | can still have a specific call work by making it as IPShell(dummy=0). |
|
173 | 173 | |
|
174 | 174 | The optional keyword parameter dummy controls whether the call |
|
175 | 175 | actually does anything. """ |
|
176 | 176 | |
|
177 | 177 | # Allow the dummy parameter to override the global __dummy_mode |
|
178 | 178 | if dummy or (dummy != 0 and self.__dummy_mode): |
|
179 | 179 | return |
|
180 | 180 | |
|
181 | 181 | # Set global subsystems (display,completions) to our values |
|
182 | 182 | sys.displayhook = self.sys_displayhook_embed |
|
183 | 183 | if self.IP.has_readline: |
|
184 | 184 | self.IP.readline.set_completer(self.IP.Completer.complete) |
|
185 | 185 | |
|
186 | 186 | if self.banner and header: |
|
187 | 187 | format = '%s\n%s\n' |
|
188 | 188 | else: |
|
189 | 189 | format = '%s%s\n' |
|
190 | 190 | banner = format % (self.banner,header) |
|
191 | 191 | |
|
192 | 192 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
193 | 193 | # our call and get the original caller's namespaces. |
|
194 | 194 | self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1) |
|
195 | 195 | |
|
196 | 196 | if self.exit_msg: |
|
197 | 197 | print self.exit_msg |
|
198 | 198 | |
|
199 | 199 | # Restore global systems (display, completion) |
|
200 | 200 | sys.displayhook = self.sys_displayhook_ori |
|
201 | 201 | self.restore_system_completer() |
|
202 | 202 | |
|
203 | 203 | def set_dummy_mode(self,dummy): |
|
204 | 204 | """Sets the embeddable shell's dummy mode parameter. |
|
205 | 205 | |
|
206 | 206 | set_dummy_mode(dummy): dummy = 0 or 1. |
|
207 | 207 | |
|
208 | 208 | This parameter is persistent and makes calls to the embeddable shell |
|
209 | 209 | silently return without performing any action. This allows you to |
|
210 | 210 | globally activate or deactivate a shell you're using with a single call. |
|
211 | 211 | |
|
212 | 212 | If you need to manually""" |
|
213 | 213 | |
|
214 | 214 | if dummy not in [0,1,False,True]: |
|
215 | 215 | raise ValueError,'dummy parameter must be boolean' |
|
216 | 216 | self.__dummy_mode = dummy |
|
217 | 217 | |
|
218 | 218 | def get_dummy_mode(self): |
|
219 | 219 | """Return the current value of the dummy mode parameter. |
|
220 | 220 | """ |
|
221 | 221 | return self.__dummy_mode |
|
222 | 222 | |
|
223 | 223 | def set_banner(self,banner): |
|
224 | 224 | """Sets the global banner. |
|
225 | 225 | |
|
226 | 226 | This banner gets prepended to every header printed when the shell |
|
227 | 227 | instance is called.""" |
|
228 | 228 | |
|
229 | 229 | self.banner = banner |
|
230 | 230 | |
|
231 | 231 | def set_exit_msg(self,exit_msg): |
|
232 | 232 | """Sets the global exit_msg. |
|
233 | 233 | |
|
234 | 234 | This exit message gets printed upon exiting every time the embedded |
|
235 | 235 | shell is called. It is None by default. """ |
|
236 | 236 | |
|
237 | 237 | self.exit_msg = exit_msg |
|
238 | 238 | |
|
239 | 239 | #----------------------------------------------------------------------------- |
|
240 | 240 | def sigint_handler (signum,stack_frame): |
|
241 | 241 | """Sigint handler for threaded apps. |
|
242 | 242 | |
|
243 | 243 | This is a horrible hack to pass information about SIGINT _without_ using |
|
244 | 244 | exceptions, since I haven't been able to properly manage cross-thread |
|
245 | 245 | exceptions in GTK/WX. In fact, I don't think it can be done (or at least |
|
246 | 246 | that's my understanding from a c.l.py thread where this was discussed).""" |
|
247 | 247 | |
|
248 | 248 | global KBINT |
|
249 | 249 | |
|
250 | 250 | print '\nKeyboardInterrupt - Press <Enter> to continue.', |
|
251 | 251 | Term.cout.flush() |
|
252 | 252 | # Set global flag so that runsource can know that Ctrl-C was hit |
|
253 | 253 | KBINT = True |
|
254 | 254 | |
|
255 | 255 | class MTInteractiveShell(InteractiveShell): |
|
256 | 256 | """Simple multi-threaded shell.""" |
|
257 | 257 | |
|
258 | 258 | # Threading strategy taken from: |
|
259 | 259 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian |
|
260 | 260 | # McErlean and John Finlay. Modified with corrections by Antoon Pardon, |
|
261 | 261 | # from the pygtk mailing list, to avoid lockups with system calls. |
|
262 | 262 | |
|
263 | 263 | # class attribute to indicate whether the class supports threads or not. |
|
264 | 264 | # Subclasses with thread support should override this as needed. |
|
265 | 265 | isthreaded = True |
|
266 | 266 | |
|
267 | 267 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
268 | 268 | user_ns=None,user_global_ns=None,banner2='',**kw): |
|
269 | 269 | """Similar to the normal InteractiveShell, but with threading control""" |
|
270 | 270 | |
|
271 | 271 | InteractiveShell.__init__(self,name,usage,rc,user_ns, |
|
272 | 272 | user_global_ns,banner2) |
|
273 | 273 | |
|
274 | 274 | # Locking control variable |
|
275 | 275 | self.thread_ready = threading.Condition() |
|
276 | 276 | |
|
277 | 277 | # Stuff to do at closing time |
|
278 | 278 | self._kill = False |
|
279 | 279 | on_kill = kw.get('on_kill') |
|
280 | 280 | if on_kill is None: |
|
281 | 281 | on_kill = [] |
|
282 | 282 | # Check that all things to kill are callable: |
|
283 | 283 | for t in on_kill: |
|
284 | 284 | if not callable(t): |
|
285 | 285 | raise TypeError,'on_kill must be a list of callables' |
|
286 | 286 | self.on_kill = on_kill |
|
287 | 287 | |
|
288 | 288 | def runsource(self, source, filename="<input>", symbol="single"): |
|
289 | 289 | """Compile and run some source in the interpreter. |
|
290 | 290 | |
|
291 | 291 | Modified version of code.py's runsource(), to handle threading issues. |
|
292 | 292 | See the original for full docstring details.""" |
|
293 | 293 | |
|
294 | 294 | global KBINT |
|
295 | 295 | |
|
296 | 296 | # If Ctrl-C was typed, we reset the flag and return right away |
|
297 | 297 | if KBINT: |
|
298 | 298 | KBINT = False |
|
299 | 299 | return False |
|
300 | 300 | |
|
301 | 301 | try: |
|
302 | 302 | code = self.compile(source, filename, symbol) |
|
303 | 303 | except (OverflowError, SyntaxError, ValueError): |
|
304 | 304 | # Case 1 |
|
305 | 305 | self.showsyntaxerror(filename) |
|
306 | 306 | return False |
|
307 | 307 | |
|
308 | 308 | if code is None: |
|
309 | 309 | # Case 2 |
|
310 | 310 | return True |
|
311 | 311 | |
|
312 | 312 | # Case 3 |
|
313 | 313 | # Store code in self, so the execution thread can handle it |
|
314 | 314 | self.thread_ready.acquire() |
|
315 | 315 | self.code_to_run = code |
|
316 | 316 | self.thread_ready.wait() # Wait until processed in timeout interval |
|
317 | 317 | self.thread_ready.release() |
|
318 | 318 | |
|
319 | 319 | return False |
|
320 | 320 | |
|
321 | 321 | def runcode(self): |
|
322 | 322 | """Execute a code object. |
|
323 | 323 | |
|
324 | 324 | Multithreaded wrapper around IPython's runcode().""" |
|
325 | 325 | |
|
326 | 326 | # lock thread-protected stuff |
|
327 | 327 | self.thread_ready.acquire() |
|
328 | 328 | |
|
329 | 329 | # Install sigint handler |
|
330 | 330 | try: |
|
331 | 331 | signal.signal(signal.SIGINT, sigint_handler) |
|
332 | 332 | except SystemError: |
|
333 | 333 | # This happens under Windows, which seems to have all sorts |
|
334 | 334 | # of problems with signal handling. Oh well... |
|
335 | 335 | pass |
|
336 | 336 | |
|
337 | 337 | if self._kill: |
|
338 | 338 | print >>Term.cout, 'Closing threads...', |
|
339 | 339 | Term.cout.flush() |
|
340 | 340 | for tokill in self.on_kill: |
|
341 | 341 | tokill() |
|
342 | 342 | print >>Term.cout, 'Done.' |
|
343 | 343 | |
|
344 | 344 | # Run pending code by calling parent class |
|
345 | 345 | if self.code_to_run is not None: |
|
346 | 346 | self.thread_ready.notify() |
|
347 | 347 | InteractiveShell.runcode(self,self.code_to_run) |
|
348 | 348 | |
|
349 | 349 | # We're done with thread-protected variables |
|
350 | 350 | self.thread_ready.release() |
|
351 | 351 | # This MUST return true for gtk threading to work |
|
352 | 352 | return True |
|
353 | 353 | |
|
354 | 354 | def kill (self): |
|
355 | 355 | """Kill the thread, returning when it has been shut down.""" |
|
356 | 356 | self.thread_ready.acquire() |
|
357 | 357 | self._kill = True |
|
358 | 358 | self.thread_ready.release() |
|
359 | 359 | |
|
360 | 360 | class MatplotlibShellBase: |
|
361 | 361 | """Mixin class to provide the necessary modifications to regular IPython |
|
362 | 362 | shell classes for matplotlib support. |
|
363 | 363 | |
|
364 | 364 | Given Python's MRO, this should be used as the FIRST class in the |
|
365 | 365 | inheritance hierarchy, so that it overrides the relevant methods.""" |
|
366 | 366 | |
|
367 | 367 | def _matplotlib_config(self,name): |
|
368 | 368 | """Return various items needed to setup the user's shell with matplotlib""" |
|
369 | 369 | |
|
370 | 370 | # Initialize matplotlib to interactive mode always |
|
371 | 371 | import matplotlib |
|
372 | 372 | from matplotlib import backends |
|
373 | 373 | matplotlib.interactive(True) |
|
374 | 374 | |
|
375 | 375 | def use(arg): |
|
376 | 376 | """IPython wrapper for matplotlib's backend switcher. |
|
377 | 377 | |
|
378 | 378 | In interactive use, we can not allow switching to a different |
|
379 | 379 | interactive backend, since thread conflicts will most likely crash |
|
380 | 380 | the python interpreter. This routine does a safety check first, |
|
381 | 381 | and refuses to perform a dangerous switch. It still allows |
|
382 | 382 | switching to non-interactive backends.""" |
|
383 | 383 | |
|
384 | 384 | if arg in backends.interactive_bk and arg != self.mpl_backend: |
|
385 | 385 | m=('invalid matplotlib backend switch.\n' |
|
386 | 386 | 'This script attempted to switch to the interactive ' |
|
387 | 387 | 'backend: `%s`\n' |
|
388 | 388 | 'Your current choice of interactive backend is: `%s`\n\n' |
|
389 | 389 | 'Switching interactive matplotlib backends at runtime\n' |
|
390 | 390 | 'would crash the python interpreter, ' |
|
391 | 391 | 'and IPython has blocked it.\n\n' |
|
392 | 392 | 'You need to either change your choice of matplotlib backend\n' |
|
393 | 393 | 'by editing your .matplotlibrc file, or run this script as a \n' |
|
394 | 394 | 'standalone file from the command line, not using IPython.\n' % |
|
395 | 395 | (arg,self.mpl_backend) ) |
|
396 | 396 | raise RuntimeError, m |
|
397 | 397 | else: |
|
398 | 398 | self.mpl_use(arg) |
|
399 | 399 | self.mpl_use._called = True |
|
400 | 400 | |
|
401 | 401 | self.matplotlib = matplotlib |
|
402 | 402 | self.mpl_backend = matplotlib.rcParams['backend'] |
|
403 | 403 | |
|
404 | 404 | # we also need to block switching of interactive backends by use() |
|
405 | 405 | self.mpl_use = matplotlib.use |
|
406 | 406 | self.mpl_use._called = False |
|
407 | 407 | # overwrite the original matplotlib.use with our wrapper |
|
408 | 408 | matplotlib.use = use |
|
409 | 409 | |
|
410 | 410 | |
|
411 | 411 | # This must be imported last in the matplotlib series, after |
|
412 | 412 | # backend/interactivity choices have been made |
|
413 | 413 | try: |
|
414 | 414 | import matplotlib.pylab as pylab |
|
415 | 415 | self.pylab = pylab |
|
416 | 416 | self.pylab_name = 'pylab' |
|
417 | 417 | except ImportError: |
|
418 | 418 | import matplotlib.matlab as matlab |
|
419 | 419 | self.pylab = matlab |
|
420 | 420 | self.pylab_name = 'matlab' |
|
421 | 421 | |
|
422 | 422 | self.pylab.show._needmain = False |
|
423 | 423 | # We need to detect at runtime whether show() is called by the user. |
|
424 | 424 | # For this, we wrap it into a decorator which adds a 'called' flag. |
|
425 | 425 | self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive) |
|
426 | 426 | |
|
427 | 427 | # Build a user namespace initialized with matplotlib/matlab features. |
|
428 | 428 | user_ns = {'__name__':'__main__', |
|
429 | 429 | '__builtins__' : __builtin__ } |
|
430 | 430 | |
|
431 | 431 | # Be careful not to remove the final \n in the code string below, or |
|
432 | 432 | # things will break badly with py22 (I think it's a python bug, 2.3 is |
|
433 | 433 | # OK). |
|
434 | 434 | pname = self.pylab_name # Python can't interpolate dotted var names |
|
435 | 435 | exec ("import matplotlib\n" |
|
436 | 436 | "import matplotlib.%(pname)s as %(pname)s\n" |
|
437 | 437 | "from matplotlib.%(pname)s import *\n" % locals()) in user_ns |
|
438 | 438 | |
|
439 | 439 | # Build matplotlib info banner |
|
440 | 440 | b=""" |
|
441 | 441 | Welcome to pylab, a matplotlib-based Python environment. |
|
442 | 442 | For more information, type 'help(pylab)'. |
|
443 | 443 | """ |
|
444 | 444 | return user_ns,b |
|
445 | 445 | |
|
446 | 446 | def mplot_exec(self,fname,*where,**kw): |
|
447 | 447 | """Execute a matplotlib script. |
|
448 | 448 | |
|
449 | 449 | This is a call to execfile(), but wrapped in safeties to properly |
|
450 | 450 | handle interactive rendering and backend switching.""" |
|
451 | 451 | |
|
452 | 452 | #print '*** Matplotlib runner ***' # dbg |
|
453 | 453 | # turn off rendering until end of script |
|
454 | 454 | isInteractive = self.matplotlib.rcParams['interactive'] |
|
455 | 455 | self.matplotlib.interactive(False) |
|
456 | 456 | self.safe_execfile(fname,*where,**kw) |
|
457 | 457 | self.matplotlib.interactive(isInteractive) |
|
458 | 458 | # make rendering call now, if the user tried to do it |
|
459 | 459 | if self.pylab.draw_if_interactive.called: |
|
460 | 460 | self.pylab.draw() |
|
461 | 461 | self.pylab.draw_if_interactive.called = False |
|
462 | 462 | |
|
463 | 463 | # if a backend switch was performed, reverse it now |
|
464 | 464 | if self.mpl_use._called: |
|
465 | 465 | self.matplotlib.rcParams['backend'] = self.mpl_backend |
|
466 | 466 | |
|
467 | 467 | def magic_run(self,parameter_s=''): |
|
468 | 468 | Magic.magic_run(self,parameter_s,runner=self.mplot_exec) |
|
469 | 469 | |
|
470 | 470 | # Fix the docstring so users see the original as well |
|
471 | 471 | magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__, |
|
472 | 472 | "\n *** Modified %run for Matplotlib," |
|
473 | 473 | " with proper interactive handling ***") |
|
474 | 474 | |
|
475 | 475 | # Now we provide 2 versions of a matplotlib-aware IPython base shells, single |
|
476 | 476 | # and multithreaded. Note that these are meant for internal use, the IPShell* |
|
477 | 477 | # classes below are the ones meant for public consumption. |
|
478 | 478 | |
|
479 | 479 | class MatplotlibShell(MatplotlibShellBase,InteractiveShell): |
|
480 | 480 | """Single-threaded shell with matplotlib support.""" |
|
481 | 481 | |
|
482 | 482 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
483 | 483 | user_ns=None,user_global_ns=None,**kw): |
|
484 | 484 | user_ns,b2 = self._matplotlib_config(name) |
|
485 | 485 | InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
486 | 486 | banner2=b2,**kw) |
|
487 | 487 | |
|
488 | 488 | class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell): |
|
489 | 489 | """Multi-threaded shell with matplotlib support.""" |
|
490 | 490 | |
|
491 | 491 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
492 | 492 | user_ns=None,user_global_ns=None, **kw): |
|
493 | 493 | user_ns,b2 = self._matplotlib_config(name) |
|
494 | 494 | MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns, |
|
495 | 495 | banner2=b2,**kw) |
|
496 | 496 | |
|
497 | 497 | #----------------------------------------------------------------------------- |
|
498 | 498 | # Utility functions for the different GUI enabled IPShell* classes. |
|
499 | 499 | |
|
500 | 500 | def get_tk(): |
|
501 | 501 | """Tries to import Tkinter and returns a withdrawn Tkinter root |
|
502 | 502 | window. If Tkinter is already imported or not available, this |
|
503 | 503 | returns None. This function calls `hijack_tk` underneath. |
|
504 | 504 | """ |
|
505 | 505 | if not USE_TK or sys.modules.has_key('Tkinter'): |
|
506 | 506 | return None |
|
507 | 507 | else: |
|
508 | 508 | try: |
|
509 | 509 | import Tkinter |
|
510 | 510 | except ImportError: |
|
511 | 511 | return None |
|
512 | 512 | else: |
|
513 | 513 | hijack_tk() |
|
514 | 514 | r = Tkinter.Tk() |
|
515 | 515 | r.withdraw() |
|
516 | 516 | return r |
|
517 | 517 | |
|
518 | 518 | def hijack_tk(): |
|
519 | 519 | """Modifies Tkinter's mainloop with a dummy so when a module calls |
|
520 | 520 | mainloop, it does not block. |
|
521 | 521 | |
|
522 | 522 | """ |
|
523 | 523 | def misc_mainloop(self, n=0): |
|
524 | 524 | pass |
|
525 | 525 | def tkinter_mainloop(n=0): |
|
526 | 526 | pass |
|
527 | 527 | |
|
528 | 528 | import Tkinter |
|
529 | 529 | Tkinter.Misc.mainloop = misc_mainloop |
|
530 | 530 | Tkinter.mainloop = tkinter_mainloop |
|
531 | 531 | |
|
532 | 532 | def update_tk(tk): |
|
533 | 533 | """Updates the Tkinter event loop. This is typically called from |
|
534 | 534 | the respective WX or GTK mainloops. |
|
535 | 535 | """ |
|
536 | 536 | if tk: |
|
537 | 537 | tk.update() |
|
538 | 538 | |
|
539 | 539 | def hijack_wx(): |
|
540 | 540 | """Modifies wxPython's MainLoop with a dummy so user code does not |
|
541 | 541 | block IPython. The hijacked mainloop function is returned. |
|
542 | 542 | """ |
|
543 | 543 | def dummy_mainloop(*args, **kw): |
|
544 | 544 | pass |
|
545 | 545 | import wxPython |
|
546 | 546 | ver = wxPython.__version__ |
|
547 | 547 | orig_mainloop = None |
|
548 | 548 | if ver[:3] >= '2.5': |
|
549 | 549 | import wx |
|
550 | 550 | if hasattr(wx, '_core_'): core = getattr(wx, '_core_') |
|
551 | 551 | elif hasattr(wx, '_core'): core = getattr(wx, '_core') |
|
552 | 552 | else: raise AttributeError('Could not find wx core module') |
|
553 | 553 | orig_mainloop = core.PyApp_MainLoop |
|
554 | 554 | core.PyApp_MainLoop = dummy_mainloop |
|
555 | 555 | elif ver[:3] == '2.4': |
|
556 | 556 | orig_mainloop = wxPython.wxc.wxPyApp_MainLoop |
|
557 | 557 | wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop |
|
558 | 558 | else: |
|
559 | 559 | warn("Unable to find either wxPython version 2.4 or >= 2.5.") |
|
560 | 560 | return orig_mainloop |
|
561 | 561 | |
|
562 | 562 | def hijack_gtk(): |
|
563 | 563 | """Modifies pyGTK's mainloop with a dummy so user code does not |
|
564 | 564 | block IPython. This function returns the original `gtk.mainloop` |
|
565 | 565 | function that has been hijacked. |
|
566 | 566 | """ |
|
567 | 567 | def dummy_mainloop(*args, **kw): |
|
568 | 568 | pass |
|
569 | 569 | import gtk |
|
570 | 570 | if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main |
|
571 | 571 | else: orig_mainloop = gtk.mainloop |
|
572 | 572 | gtk.mainloop = dummy_mainloop |
|
573 | 573 | gtk.main = dummy_mainloop |
|
574 | 574 | return orig_mainloop |
|
575 | 575 | |
|
576 | 576 | #----------------------------------------------------------------------------- |
|
577 | 577 | # The IPShell* classes below are the ones meant to be run by external code as |
|
578 | 578 | # IPython instances. Note that unless a specific threading strategy is |
|
579 | 579 | # desired, the factory function start() below should be used instead (it |
|
580 | 580 | # selects the proper threaded class). |
|
581 | 581 | |
|
582 | 582 | class IPShellGTK(threading.Thread): |
|
583 | 583 | """Run a gtk mainloop() in a separate thread. |
|
584 | 584 | |
|
585 | 585 | Python commands can be passed to the thread where they will be executed. |
|
586 | 586 | This is implemented by periodically checking for passed code using a |
|
587 | 587 | GTK timeout callback.""" |
|
588 | 588 | |
|
589 | 589 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
590 | 590 | |
|
591 | 591 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
592 | 592 | debug=1,shell_class=MTInteractiveShell): |
|
593 | 593 | |
|
594 | 594 | import gtk |
|
595 | 595 | |
|
596 | 596 | self.gtk = gtk |
|
597 | 597 | self.gtk_mainloop = hijack_gtk() |
|
598 | 598 | |
|
599 | 599 | # Allows us to use both Tk and GTK. |
|
600 | 600 | self.tk = get_tk() |
|
601 | 601 | |
|
602 | 602 | if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit |
|
603 | 603 | else: mainquit = self.gtk.mainquit |
|
604 | 604 | |
|
605 | 605 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
606 | 606 | user_global_ns=user_global_ns, |
|
607 | 607 | debug=debug, |
|
608 | 608 | shell_class=shell_class, |
|
609 | 609 | on_kill=[mainquit]) |
|
610 | 610 | |
|
611 | 611 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
612 | 612 | # method only and .run() needs it. The actual value will be set by |
|
613 | 613 | # .mainloop(). |
|
614 | 614 | self._banner = None |
|
615 | 615 | |
|
616 | 616 | threading.Thread.__init__(self) |
|
617 | 617 | |
|
618 | 618 | def run(self): |
|
619 | 619 | self.IP.mainloop(self._banner) |
|
620 | 620 | self.IP.kill() |
|
621 | 621 | |
|
622 | 622 | def mainloop(self,sys_exit=0,banner=None): |
|
623 | 623 | |
|
624 | 624 | self._banner = banner |
|
625 | 625 | |
|
626 | 626 | if self.gtk.pygtk_version >= (2,4,0): |
|
627 | 627 | import gobject |
|
628 | 628 | gobject.timeout_add(self.TIMEOUT, self.on_timer) |
|
629 | 629 | else: |
|
630 | 630 | self.gtk.timeout_add(self.TIMEOUT, self.on_timer) |
|
631 | 631 | |
|
632 | 632 | if sys.platform != 'win32': |
|
633 | 633 | try: |
|
634 | 634 | if self.gtk.gtk_version[0] >= 2: |
|
635 | 635 | self.gtk.threads_init() |
|
636 | 636 | except AttributeError: |
|
637 | 637 | pass |
|
638 | 638 | except RuntimeError: |
|
639 | 639 | error('Your pyGTK likely has not been compiled with ' |
|
640 | 640 | 'threading support.\n' |
|
641 | 641 | 'The exception printout is below.\n' |
|
642 | 642 | 'You can either rebuild pyGTK with threads, or ' |
|
643 | 643 | 'try using \n' |
|
644 | 644 | 'matplotlib with a different backend (like Tk or WX).\n' |
|
645 | 645 | 'Note that matplotlib will most likely not work in its ' |
|
646 | 646 | 'current state!') |
|
647 | 647 | self.IP.InteractiveTB() |
|
648 | 648 | self.start() |
|
649 | 649 | self.gtk.threads_enter() |
|
650 | 650 | self.gtk_mainloop() |
|
651 | 651 | self.gtk.threads_leave() |
|
652 | 652 | self.join() |
|
653 | 653 | |
|
654 | 654 | def on_timer(self): |
|
655 | 655 | update_tk(self.tk) |
|
656 | 656 | return self.IP.runcode() |
|
657 | 657 | |
|
658 | 658 | |
|
659 | 659 | class IPShellWX(threading.Thread): |
|
660 | 660 | """Run a wx mainloop() in a separate thread. |
|
661 | 661 | |
|
662 | 662 | Python commands can be passed to the thread where they will be executed. |
|
663 | 663 | This is implemented by periodically checking for passed code using a |
|
664 | 664 | GTK timeout callback.""" |
|
665 | 665 | |
|
666 | 666 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
667 | 667 | |
|
668 | 668 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
669 | 669 | debug=1,shell_class=MTInteractiveShell): |
|
670 | 670 | |
|
671 | 671 | import wxPython.wx as wx |
|
672 | 672 | |
|
673 | 673 | threading.Thread.__init__(self) |
|
674 | 674 | self.wx = wx |
|
675 | 675 | self.wx_mainloop = hijack_wx() |
|
676 | 676 | |
|
677 | 677 | # Allows us to use both Tk and GTK. |
|
678 | 678 | self.tk = get_tk() |
|
679 | 679 | |
|
680 | 680 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
681 | 681 | user_global_ns=user_global_ns, |
|
682 | 682 | debug=debug, |
|
683 | 683 | shell_class=shell_class, |
|
684 | 684 | on_kill=[self.wxexit]) |
|
685 | 685 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
686 | 686 | # method only and .run() needs it. The actual value will be set by |
|
687 | 687 | # .mainloop(). |
|
688 | 688 | self._banner = None |
|
689 | 689 | |
|
690 | 690 | self.app = None |
|
691 | 691 | |
|
692 | 692 | def wxexit(self, *args): |
|
693 | 693 | if self.app is not None: |
|
694 | 694 | self.app.agent.timer.Stop() |
|
695 | 695 | self.app.ExitMainLoop() |
|
696 | 696 | |
|
697 | 697 | def run(self): |
|
698 | 698 | self.IP.mainloop(self._banner) |
|
699 | 699 | self.IP.kill() |
|
700 | 700 | |
|
701 | 701 | def mainloop(self,sys_exit=0,banner=None): |
|
702 | 702 | |
|
703 | 703 | self._banner = banner |
|
704 | 704 | |
|
705 | 705 | self.start() |
|
706 | 706 | |
|
707 | 707 | class TimerAgent(self.wx.wxMiniFrame): |
|
708 | 708 | wx = self.wx |
|
709 | 709 | IP = self.IP |
|
710 | 710 | tk = self.tk |
|
711 | 711 | def __init__(self, parent, interval): |
|
712 | 712 | style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ |
|
713 | 713 | self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200), |
|
714 | 714 | size=(100, 100),style=style) |
|
715 | 715 | self.Show(False) |
|
716 | 716 | self.interval = interval |
|
717 | 717 | self.timerId = self.wx.wxNewId() |
|
718 | 718 | |
|
719 | 719 | def StartWork(self): |
|
720 | 720 | self.timer = self.wx.wxTimer(self, self.timerId) |
|
721 | 721 | self.wx.EVT_TIMER(self, self.timerId, self.OnTimer) |
|
722 | 722 | self.timer.Start(self.interval) |
|
723 | 723 | |
|
724 | 724 | def OnTimer(self, event): |
|
725 | 725 | update_tk(self.tk) |
|
726 | 726 | self.IP.runcode() |
|
727 | 727 | |
|
728 | 728 | class App(self.wx.wxApp): |
|
729 | 729 | wx = self.wx |
|
730 | 730 | TIMEOUT = self.TIMEOUT |
|
731 | 731 | def OnInit(self): |
|
732 | 732 | 'Create the main window and insert the custom frame' |
|
733 | 733 | self.agent = TimerAgent(None, self.TIMEOUT) |
|
734 | 734 | self.agent.Show(self.wx.false) |
|
735 | 735 | self.agent.StartWork() |
|
736 | 736 | return self.wx.true |
|
737 | 737 | |
|
738 | 738 | self.app = App(redirect=False) |
|
739 | 739 | self.wx_mainloop(self.app) |
|
740 | 740 | self.join() |
|
741 | 741 | |
|
742 | 742 | |
|
743 | 743 | class IPShellQt(threading.Thread): |
|
744 | 744 | """Run a Qt event loop in a separate thread. |
|
745 | 745 | |
|
746 | 746 | Python commands can be passed to the thread where they will be executed. |
|
747 | 747 | This is implemented by periodically checking for passed code using a |
|
748 | 748 | Qt timer / slot.""" |
|
749 | 749 | |
|
750 | 750 | TIMEOUT = 100 # Millisecond interval between timeouts. |
|
751 | 751 | |
|
752 | 752 | def __init__(self,argv=None,user_ns=None,user_global_ns=None, |
|
753 | 753 | debug=0,shell_class=MTInteractiveShell): |
|
754 | 754 | |
|
755 | 755 | import qt |
|
756 | 756 | |
|
757 | 757 | class newQApplication: |
|
758 | 758 | def __init__( self ): |
|
759 | 759 | self.QApplication = qt.QApplication |
|
760 | 760 | |
|
761 | 761 | def __call__( *args, **kwargs ): |
|
762 | 762 | return qt.qApp |
|
763 | 763 | |
|
764 | 764 | def exec_loop( *args, **kwargs ): |
|
765 | 765 | pass |
|
766 | 766 | |
|
767 | 767 | def __getattr__( self, name ): |
|
768 | 768 | return getattr( self.QApplication, name ) |
|
769 | 769 | |
|
770 | 770 | qt.QApplication = newQApplication() |
|
771 | 771 | |
|
772 | 772 | # Allows us to use both Tk and QT. |
|
773 | 773 | self.tk = get_tk() |
|
774 | 774 | |
|
775 | 775 | self.IP = make_IPython(argv,user_ns=user_ns, |
|
776 | 776 | user_global_ns=user_global_ns, |
|
777 | 777 | debug=debug, |
|
778 | 778 | shell_class=shell_class, |
|
779 | 779 | on_kill=[qt.qApp.exit]) |
|
780 | 780 | |
|
781 | 781 | # HACK: slot for banner in self; it will be passed to the mainloop |
|
782 | 782 | # method only and .run() needs it. The actual value will be set by |
|
783 | 783 | # .mainloop(). |
|
784 | 784 | self._banner = None |
|
785 | 785 | |
|
786 | 786 | threading.Thread.__init__(self) |
|
787 | 787 | |
|
788 | 788 | def run(self): |
|
789 | 789 | self.IP.mainloop(self._banner) |
|
790 | 790 | self.IP.kill() |
|
791 | 791 | |
|
792 | 792 | def mainloop(self,sys_exit=0,banner=None): |
|
793 | 793 | |
|
794 | 794 | import qt |
|
795 | 795 | |
|
796 | 796 | self._banner = banner |
|
797 | 797 | |
|
798 | 798 | if qt.QApplication.startingUp(): |
|
799 | 799 | a = qt.QApplication.QApplication(sys.argv) |
|
800 | 800 | self.timer = qt.QTimer() |
|
801 | 801 | qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer ) |
|
802 | 802 | |
|
803 | 803 | self.start() |
|
804 | 804 | self.timer.start( self.TIMEOUT, True ) |
|
805 | 805 | while True: |
|
806 | 806 | if self.IP._kill: break |
|
807 | 807 | qt.qApp.exec_loop() |
|
808 | 808 | self.join() |
|
809 | 809 | |
|
810 | 810 | def on_timer(self): |
|
811 | 811 | update_tk(self.tk) |
|
812 | 812 | result = self.IP.runcode() |
|
813 | 813 | self.timer.start( self.TIMEOUT, True ) |
|
814 | 814 | return result |
|
815 | 815 | |
|
816 | 816 | # A set of matplotlib public IPython shell classes, for single-threaded |
|
817 | 817 | # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use. |
|
818 | 818 | class IPShellMatplotlib(IPShell): |
|
819 | 819 | """Subclass IPShell with MatplotlibShell as the internal shell. |
|
820 | 820 | |
|
821 | 821 | Single-threaded class, meant for the Tk* and FLTK* backends. |
|
822 | 822 | |
|
823 | 823 | Having this on a separate class simplifies the external driver code.""" |
|
824 | 824 | |
|
825 | 825 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
826 | 826 | IPShell.__init__(self,argv,user_ns,user_global_ns,debug, |
|
827 | 827 | shell_class=MatplotlibShell) |
|
828 | 828 | |
|
829 | 829 | class IPShellMatplotlibGTK(IPShellGTK): |
|
830 | 830 | """Subclass IPShellGTK with MatplotlibMTShell as the internal shell. |
|
831 | 831 | |
|
832 | 832 | Multi-threaded class, meant for the GTK* backends.""" |
|
833 | 833 | |
|
834 | 834 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
835 | 835 | IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug, |
|
836 | 836 | shell_class=MatplotlibMTShell) |
|
837 | 837 | |
|
838 | 838 | class IPShellMatplotlibWX(IPShellWX): |
|
839 | 839 | """Subclass IPShellWX with MatplotlibMTShell as the internal shell. |
|
840 | 840 | |
|
841 | 841 | Multi-threaded class, meant for the WX* backends.""" |
|
842 | 842 | |
|
843 | 843 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
844 | 844 | IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug, |
|
845 | 845 | shell_class=MatplotlibMTShell) |
|
846 | 846 | |
|
847 | 847 | class IPShellMatplotlibQt(IPShellQt): |
|
848 | 848 | """Subclass IPShellQt with MatplotlibMTShell as the internal shell. |
|
849 | 849 | |
|
850 | 850 | Multi-threaded class, meant for the Qt* backends.""" |
|
851 | 851 | |
|
852 | 852 | def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1): |
|
853 | 853 | IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug, |
|
854 | 854 | shell_class=MatplotlibMTShell) |
|
855 | 855 | |
|
856 | 856 | #----------------------------------------------------------------------------- |
|
857 | 857 | # Factory functions to actually start the proper thread-aware shell |
|
858 | 858 | |
|
859 | 859 | def _matplotlib_shell_class(): |
|
860 | 860 | """Factory function to handle shell class selection for matplotlib. |
|
861 | 861 | |
|
862 | 862 | The proper shell class to use depends on the matplotlib backend, since |
|
863 | 863 | each backend requires a different threading strategy.""" |
|
864 | 864 | |
|
865 | 865 | try: |
|
866 | 866 | import matplotlib |
|
867 | 867 | except ImportError: |
|
868 | 868 | error('matplotlib could NOT be imported! Starting normal IPython.') |
|
869 | 869 | sh_class = IPShell |
|
870 | 870 | else: |
|
871 | 871 | backend = matplotlib.rcParams['backend'] |
|
872 | 872 | if backend.startswith('GTK'): |
|
873 | 873 | sh_class = IPShellMatplotlibGTK |
|
874 | 874 | elif backend.startswith('WX'): |
|
875 | 875 | sh_class = IPShellMatplotlibWX |
|
876 | 876 | elif backend.startswith('Qt'): |
|
877 | 877 | sh_class = IPShellMatplotlibQt |
|
878 | 878 | else: |
|
879 | 879 | sh_class = IPShellMatplotlib |
|
880 | 880 | #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg |
|
881 | 881 | return sh_class |
|
882 | 882 | |
|
883 | 883 | # This is the one which should be called by external code. |
|
884 | 884 | def start(): |
|
885 | 885 | """Return a running shell instance, dealing with threading options. |
|
886 | 886 | |
|
887 | 887 | This is a factory function which will instantiate the proper IPython shell |
|
888 | 888 | based on the user's threading choice. Such a selector is needed because |
|
889 | 889 | different GUI toolkits require different thread handling details.""" |
|
890 | 890 | |
|
891 | 891 | global USE_TK |
|
892 | 892 | # Crude sys.argv hack to extract the threading options. |
|
893 | 893 | argv = sys.argv |
|
894 | 894 | if len(argv) > 1: |
|
895 | 895 | if len(argv) > 2: |
|
896 | 896 | arg2 = argv[2] |
|
897 | 897 | if arg2.endswith('-tk'): |
|
898 | 898 | USE_TK = True |
|
899 | 899 | arg1 = argv[1] |
|
900 | 900 | if arg1.endswith('-gthread'): |
|
901 | 901 | shell = IPShellGTK |
|
902 | 902 | elif arg1.endswith( '-qthread' ): |
|
903 | 903 | shell = IPShellQt |
|
904 | 904 | elif arg1.endswith('-wthread'): |
|
905 | 905 | shell = IPShellWX |
|
906 | 906 | elif arg1.endswith('-pylab'): |
|
907 | 907 | shell = _matplotlib_shell_class() |
|
908 | 908 | else: |
|
909 | 909 | shell = IPShell |
|
910 | 910 | else: |
|
911 | 911 | shell = IPShell |
|
912 | 912 | return shell() |
|
913 | 913 | |
|
914 | 914 | # Some aliases for backwards compatibility |
|
915 | 915 | IPythonShell = IPShell |
|
916 | 916 | IPythonShellEmbed = IPShellEmbed |
|
917 | 917 | #************************ End of file <Shell.py> *************************** |
@@ -1,496 +1,496 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Manage background (threaded) jobs conveniently from an interactive shell. |
|
3 | 3 | |
|
4 | 4 | This module provides a BackgroundJobManager class. This is the main class |
|
5 | 5 | meant for public usage, it implements an object which can create and manage |
|
6 | 6 | new background jobs. |
|
7 | 7 | |
|
8 | 8 | It also provides the actual job classes managed by these BackgroundJobManager |
|
9 | 9 | objects, see their docstrings below. |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | This system was inspired by discussions with B. Granger and the |
|
13 | 13 | BackgroundCommand class described in the book Python Scripting for |
|
14 | 14 | Computational Science, by H. P. Langtangen: |
|
15 | 15 | |
|
16 | 16 | http://folk.uio.no/hpl/scripting |
|
17 | 17 | |
|
18 | 18 | (although ultimately no code from this text was used, as IPython's system is a |
|
19 | 19 | separate implementation). |
|
20 | 20 | |
|
21 |
$Id: background_jobs.py 9 |
|
|
21 | $Id: background_jobs.py 994 2006-01-08 08:29:44Z fperez $ | |
|
22 | 22 | """ |
|
23 | 23 | |
|
24 | 24 | #***************************************************************************** |
|
25 | # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu> | |
|
25 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> | |
|
26 | 26 | # |
|
27 | 27 | # Distributed under the terms of the BSD License. The full license is in |
|
28 | 28 | # the file COPYING, distributed as part of this software. |
|
29 | 29 | #***************************************************************************** |
|
30 | 30 | |
|
31 | 31 | from IPython import Release |
|
32 | 32 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
33 | 33 | __license__ = Release.license |
|
34 | 34 | |
|
35 | 35 | # Code begins |
|
36 | 36 | import sys |
|
37 | 37 | import threading |
|
38 | 38 | |
|
39 | 39 | from IPython.ultraTB import AutoFormattedTB |
|
40 | 40 | from IPython.genutils import warn,error |
|
41 | 41 | |
|
42 | 42 | class BackgroundJobManager: |
|
43 | 43 | """Class to manage a pool of backgrounded threaded jobs. |
|
44 | 44 | |
|
45 | 45 | Below, we assume that 'jobs' is a BackgroundJobManager instance. |
|
46 | 46 | |
|
47 | 47 | Usage summary (see the method docstrings for details): |
|
48 | 48 | |
|
49 | 49 | jobs.new(...) -> start a new job |
|
50 | 50 | |
|
51 | 51 | jobs() or jobs.status() -> print status summary of all jobs |
|
52 | 52 | |
|
53 | 53 | jobs[N] -> returns job number N. |
|
54 | 54 | |
|
55 | 55 | foo = jobs[N].result -> assign to variable foo the result of job N |
|
56 | 56 | |
|
57 | 57 | jobs[N].traceback() -> print the traceback of dead job N |
|
58 | 58 | |
|
59 | 59 | jobs.remove(N) -> remove (finished) job N |
|
60 | 60 | |
|
61 | 61 | jobs.flush_finished() -> remove all finished jobs |
|
62 | 62 | |
|
63 | 63 | As a convenience feature, BackgroundJobManager instances provide the |
|
64 | 64 | utility result and traceback methods which retrieve the corresponding |
|
65 | 65 | information from the jobs list: |
|
66 | 66 | |
|
67 | 67 | jobs.result(N) <--> jobs[N].result |
|
68 | 68 | jobs.traceback(N) <--> jobs[N].traceback() |
|
69 | 69 | |
|
70 | 70 | While this appears minor, it allows you to use tab completion |
|
71 | 71 | interactively on the job manager instance. |
|
72 | 72 | |
|
73 | 73 | In interactive mode, IPython provides the magic fuction %bg for quick |
|
74 | 74 | creation of backgrounded expression-based jobs. Type bg? for details.""" |
|
75 | 75 | |
|
76 | 76 | def __init__(self): |
|
77 | 77 | # Lists for job management |
|
78 | 78 | self.jobs_run = [] |
|
79 | 79 | self.jobs_comp = [] |
|
80 | 80 | self.jobs_dead = [] |
|
81 | 81 | # A dict of all jobs, so users can easily access any of them |
|
82 | 82 | self.jobs_all = {} |
|
83 | 83 | # For reporting |
|
84 | 84 | self._comp_report = [] |
|
85 | 85 | self._dead_report = [] |
|
86 | 86 | # Store status codes locally for fast lookups |
|
87 | 87 | self._s_created = BackgroundJobBase.stat_created_c |
|
88 | 88 | self._s_running = BackgroundJobBase.stat_running_c |
|
89 | 89 | self._s_completed = BackgroundJobBase.stat_completed_c |
|
90 | 90 | self._s_dead = BackgroundJobBase.stat_dead_c |
|
91 | 91 | |
|
92 | 92 | def new(self,func_or_exp,*args,**kwargs): |
|
93 | 93 | """Add a new background job and start it in a separate thread. |
|
94 | 94 | |
|
95 | 95 | There are two types of jobs which can be created: |
|
96 | 96 | |
|
97 | 97 | 1. Jobs based on expressions which can be passed to an eval() call. |
|
98 | 98 | The expression must be given as a string. For example: |
|
99 | 99 | |
|
100 | 100 | job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]]) |
|
101 | 101 | |
|
102 | 102 | The given expression is passed to eval(), along with the optional |
|
103 | 103 | global/local dicts provided. If no dicts are given, they are |
|
104 | 104 | extracted automatically from the caller's frame. |
|
105 | 105 | |
|
106 | 106 | A Python statement is NOT a valid eval() expression. Basically, you |
|
107 | 107 | can only use as an eval() argument something which can go on the right |
|
108 | 108 | of an '=' sign and be assigned to a variable. |
|
109 | 109 | |
|
110 | 110 | For example,"print 'hello'" is not valid, but '2+3' is. |
|
111 | 111 | |
|
112 | 112 | 2. Jobs given a function object, optionally passing additional |
|
113 | 113 | positional arguments: |
|
114 | 114 | |
|
115 | 115 | job_manager.new(myfunc,x,y) |
|
116 | 116 | |
|
117 | 117 | The function is called with the given arguments. |
|
118 | 118 | |
|
119 | 119 | If you need to pass keyword arguments to your function, you must |
|
120 | 120 | supply them as a dict named kw: |
|
121 | 121 | |
|
122 | 122 | job_manager.new(myfunc,x,y,kw=dict(z=1)) |
|
123 | 123 | |
|
124 | 124 | The reason for this assymmetry is that the new() method needs to |
|
125 | 125 | maintain access to its own keywords, and this prevents name collisions |
|
126 | 126 | between arguments to new() and arguments to your own functions. |
|
127 | 127 | |
|
128 | 128 | In both cases, the result is stored in the job.result field of the |
|
129 | 129 | background job object. |
|
130 | 130 | |
|
131 | 131 | |
|
132 | 132 | Notes and caveats: |
|
133 | 133 | |
|
134 | 134 | 1. All threads running share the same standard output. Thus, if your |
|
135 | 135 | background jobs generate output, it will come out on top of whatever |
|
136 | 136 | you are currently writing. For this reason, background jobs are best |
|
137 | 137 | used with silent functions which simply return their output. |
|
138 | 138 | |
|
139 | 139 | 2. Threads also all work within the same global namespace, and this |
|
140 | 140 | system does not lock interactive variables. So if you send job to the |
|
141 | 141 | background which operates on a mutable object for a long time, and |
|
142 | 142 | start modifying that same mutable object interactively (or in another |
|
143 | 143 | backgrounded job), all sorts of bizarre behaviour will occur. |
|
144 | 144 | |
|
145 | 145 | 3. If a background job is spending a lot of time inside a C extension |
|
146 | 146 | module which does not release the Python Global Interpreter Lock |
|
147 | 147 | (GIL), this will block the IPython prompt. This is simply because the |
|
148 | 148 | Python interpreter can only switch between threads at Python |
|
149 | 149 | bytecodes. While the execution is inside C code, the interpreter must |
|
150 | 150 | simply wait unless the extension module releases the GIL. |
|
151 | 151 | |
|
152 | 152 | 4. There is no way, due to limitations in the Python threads library, |
|
153 | 153 | to kill a thread once it has started.""" |
|
154 | 154 | |
|
155 | 155 | if callable(func_or_exp): |
|
156 | 156 | kw = kwargs.get('kw',{}) |
|
157 | 157 | job = BackgroundJobFunc(func_or_exp,*args,**kw) |
|
158 | 158 | elif isinstance(func_or_exp,basestring): |
|
159 | 159 | if not args: |
|
160 | 160 | frame = sys._getframe(1) |
|
161 | 161 | glob, loc = frame.f_globals, frame.f_locals |
|
162 | 162 | elif len(args)==1: |
|
163 | 163 | glob = loc = args[0] |
|
164 | 164 | elif len(args)==2: |
|
165 | 165 | glob,loc = args |
|
166 | 166 | else: |
|
167 | 167 | raise ValueError,\ |
|
168 | 168 | 'Expression jobs take at most 2 args (globals,locals)' |
|
169 | 169 | job = BackgroundJobExpr(func_or_exp,glob,loc) |
|
170 | 170 | else: |
|
171 | 171 | raise |
|
172 | 172 | jkeys = self.jobs_all.keys() |
|
173 | 173 | if jkeys: |
|
174 | 174 | job.num = max(jkeys)+1 |
|
175 | 175 | else: |
|
176 | 176 | job.num = 0 |
|
177 | 177 | self.jobs_run.append(job) |
|
178 | 178 | self.jobs_all[job.num] = job |
|
179 | 179 | print 'Starting job # %s in a separate thread.' % job.num |
|
180 | 180 | job.start() |
|
181 | 181 | return job |
|
182 | 182 | |
|
183 | 183 | def __getitem__(self,key): |
|
184 | 184 | return self.jobs_all[key] |
|
185 | 185 | |
|
186 | 186 | def __call__(self): |
|
187 | 187 | """An alias to self.status(), |
|
188 | 188 | |
|
189 | 189 | This allows you to simply call a job manager instance much like the |
|
190 | 190 | Unix jobs shell command.""" |
|
191 | 191 | |
|
192 | 192 | return self.status() |
|
193 | 193 | |
|
194 | 194 | def _update_status(self): |
|
195 | 195 | """Update the status of the job lists. |
|
196 | 196 | |
|
197 | 197 | This method moves finished jobs to one of two lists: |
|
198 | 198 | - self.jobs_comp: jobs which completed successfully |
|
199 | 199 | - self.jobs_dead: jobs which finished but died. |
|
200 | 200 | |
|
201 | 201 | It also copies those jobs to corresponding _report lists. These lists |
|
202 | 202 | are used to report jobs completed/dead since the last update, and are |
|
203 | 203 | then cleared by the reporting function after each call.""" |
|
204 | 204 | |
|
205 | 205 | run,comp,dead = self._s_running,self._s_completed,self._s_dead |
|
206 | 206 | jobs_run = self.jobs_run |
|
207 | 207 | for num in range(len(jobs_run)): |
|
208 | 208 | job = jobs_run[num] |
|
209 | 209 | stat = job.stat_code |
|
210 | 210 | if stat == run: |
|
211 | 211 | continue |
|
212 | 212 | elif stat == comp: |
|
213 | 213 | self.jobs_comp.append(job) |
|
214 | 214 | self._comp_report.append(job) |
|
215 | 215 | jobs_run[num] = False |
|
216 | 216 | elif stat == dead: |
|
217 | 217 | self.jobs_dead.append(job) |
|
218 | 218 | self._dead_report.append(job) |
|
219 | 219 | jobs_run[num] = False |
|
220 | 220 | self.jobs_run = filter(None,self.jobs_run) |
|
221 | 221 | |
|
222 | 222 | def _group_report(self,group,name): |
|
223 | 223 | """Report summary for a given job group. |
|
224 | 224 | |
|
225 | 225 | Return True if the group had any elements.""" |
|
226 | 226 | |
|
227 | 227 | if group: |
|
228 | 228 | print '%s jobs:' % name |
|
229 | 229 | for job in group: |
|
230 | 230 | print '%s : %s' % (job.num,job) |
|
231 | 231 | |
|
232 | 232 | return True |
|
233 | 233 | |
|
234 | 234 | def _group_flush(self,group,name): |
|
235 | 235 | """Flush a given job group |
|
236 | 236 | |
|
237 | 237 | Return True if the group had any elements.""" |
|
238 | 238 | |
|
239 | 239 | njobs = len(group) |
|
240 | 240 | if njobs: |
|
241 | 241 | plural = {1:''}.setdefault(njobs,'s') |
|
242 | 242 | print 'Flushing %s %s job%s.' % (njobs,name,plural) |
|
243 | 243 | group[:] = [] |
|
244 | 244 | return True |
|
245 | 245 | |
|
246 | 246 | def _status_new(self): |
|
247 | 247 | """Print the status of newly finished jobs. |
|
248 | 248 | |
|
249 | 249 | Return True if any new jobs are reported. |
|
250 | 250 | |
|
251 | 251 | This call resets its own state every time, so it only reports jobs |
|
252 | 252 | which have finished since the last time it was called.""" |
|
253 | 253 | |
|
254 | 254 | self._update_status() |
|
255 | 255 | new_comp = self._group_report(self._comp_report,'Completed') |
|
256 | 256 | new_dead = self._group_report(self._dead_report, |
|
257 | 257 | 'Dead, call job.traceback() for details') |
|
258 | 258 | self._comp_report[:] = [] |
|
259 | 259 | self._dead_report[:] = [] |
|
260 | 260 | return new_comp or new_dead |
|
261 | 261 | |
|
262 | 262 | def status(self,verbose=0): |
|
263 | 263 | """Print a status of all jobs currently being managed.""" |
|
264 | 264 | |
|
265 | 265 | self._update_status() |
|
266 | 266 | self._group_report(self.jobs_run,'Running') |
|
267 | 267 | self._group_report(self.jobs_comp,'Completed') |
|
268 | 268 | self._group_report(self.jobs_dead,'Dead') |
|
269 | 269 | # Also flush the report queues |
|
270 | 270 | self._comp_report[:] = [] |
|
271 | 271 | self._dead_report[:] = [] |
|
272 | 272 | |
|
273 | 273 | def remove(self,num): |
|
274 | 274 | """Remove a finished (completed or dead) job.""" |
|
275 | 275 | |
|
276 | 276 | try: |
|
277 | 277 | job = self.jobs_all[num] |
|
278 | 278 | except KeyError: |
|
279 | 279 | error('Job #%s not found' % num) |
|
280 | 280 | else: |
|
281 | 281 | stat_code = job.stat_code |
|
282 | 282 | if stat_code == self._s_running: |
|
283 | 283 | error('Job #%s is still running, it can not be removed.' % num) |
|
284 | 284 | return |
|
285 | 285 | elif stat_code == self._s_completed: |
|
286 | 286 | self.jobs_comp.remove(job) |
|
287 | 287 | elif stat_code == self._s_dead: |
|
288 | 288 | self.jobs_dead.remove(job) |
|
289 | 289 | |
|
290 | 290 | def flush_finished(self): |
|
291 | 291 | """Flush all jobs finished (completed and dead) from lists. |
|
292 | 292 | |
|
293 | 293 | Running jobs are never flushed. |
|
294 | 294 | |
|
295 | 295 | It first calls _status_new(), to update info. If any jobs have |
|
296 | 296 | completed since the last _status_new() call, the flush operation |
|
297 | 297 | aborts.""" |
|
298 | 298 | |
|
299 | 299 | if self._status_new(): |
|
300 | 300 | error('New jobs completed since last '\ |
|
301 | 301 | '_status_new(), aborting flush.') |
|
302 | 302 | return |
|
303 | 303 | |
|
304 | 304 | # Remove the finished jobs from the master dict |
|
305 | 305 | jobs_all = self.jobs_all |
|
306 | 306 | for job in self.jobs_comp+self.jobs_dead: |
|
307 | 307 | del(jobs_all[job.num]) |
|
308 | 308 | |
|
309 | 309 | # Now flush these lists completely |
|
310 | 310 | fl_comp = self._group_flush(self.jobs_comp,'Completed') |
|
311 | 311 | fl_dead = self._group_flush(self.jobs_dead,'Dead') |
|
312 | 312 | if not (fl_comp or fl_dead): |
|
313 | 313 | print 'No jobs to flush.' |
|
314 | 314 | |
|
315 | 315 | def result(self,num): |
|
316 | 316 | """result(N) -> return the result of job N.""" |
|
317 | 317 | try: |
|
318 | 318 | return self.jobs_all[num].result |
|
319 | 319 | except KeyError: |
|
320 | 320 | error('Job #%s not found' % num) |
|
321 | 321 | |
|
322 | 322 | def traceback(self,num): |
|
323 | 323 | try: |
|
324 | 324 | self.jobs_all[num].traceback() |
|
325 | 325 | except KeyError: |
|
326 | 326 | error('Job #%s not found' % num) |
|
327 | 327 | |
|
328 | 328 | |
|
329 | 329 | class BackgroundJobBase(threading.Thread): |
|
330 | 330 | """Base class to build BackgroundJob classes. |
|
331 | 331 | |
|
332 | 332 | The derived classes must implement: |
|
333 | 333 | |
|
334 | 334 | - Their own __init__, since the one here raises NotImplementedError. The |
|
335 | 335 | derived constructor must call self._init() at the end, to provide common |
|
336 | 336 | initialization. |
|
337 | 337 | |
|
338 | 338 | - A strform attribute used in calls to __str__. |
|
339 | 339 | |
|
340 | 340 | - A call() method, which will make the actual execution call and must |
|
341 | 341 | return a value to be held in the 'result' field of the job object.""" |
|
342 | 342 | |
|
343 | 343 | # Class constants for status, in string and as numerical codes (when |
|
344 | 344 | # updating jobs lists, we don't want to do string comparisons). This will |
|
345 | 345 | # be done at every user prompt, so it has to be as fast as possible |
|
346 | 346 | stat_created = 'Created'; stat_created_c = 0 |
|
347 | 347 | stat_running = 'Running'; stat_running_c = 1 |
|
348 | 348 | stat_completed = 'Completed'; stat_completed_c = 2 |
|
349 | 349 | stat_dead = 'Dead (Exception), call job.traceback() for details' |
|
350 | 350 | stat_dead_c = -1 |
|
351 | 351 | |
|
352 | 352 | def __init__(self): |
|
353 | 353 | raise NotImplementedError, \ |
|
354 | 354 | "This class can not be instantiated directly." |
|
355 | 355 | |
|
356 | 356 | def _init(self): |
|
357 | 357 | """Common initialization for all BackgroundJob objects""" |
|
358 | 358 | |
|
359 | 359 | for attr in ['call','strform']: |
|
360 | 360 | assert hasattr(self,attr), "Missing attribute <%s>" % attr |
|
361 | 361 | |
|
362 | 362 | # The num tag can be set by an external job manager |
|
363 | 363 | self.num = None |
|
364 | 364 | |
|
365 | 365 | self.status = BackgroundJobBase.stat_created |
|
366 | 366 | self.stat_code = BackgroundJobBase.stat_created_c |
|
367 | 367 | self.finished = False |
|
368 | 368 | self.result = '<BackgroundJob has not completed>' |
|
369 | 369 | # reuse the ipython traceback handler if we can get to it, otherwise |
|
370 | 370 | # make a new one |
|
371 | 371 | try: |
|
372 | 372 | self._make_tb = __IPYTHON__.InteractiveTB.text |
|
373 | 373 | except: |
|
374 | 374 | self._make_tb = AutoFormattedTB(mode = 'Context', |
|
375 | 375 | color_scheme='NoColor', |
|
376 | 376 | tb_offset = 1).text |
|
377 | 377 | # Hold a formatted traceback if one is generated. |
|
378 | 378 | self._tb = None |
|
379 | 379 | |
|
380 | 380 | threading.Thread.__init__(self) |
|
381 | 381 | |
|
382 | 382 | def __str__(self): |
|
383 | 383 | return self.strform |
|
384 | 384 | |
|
385 | 385 | def __repr__(self): |
|
386 | 386 | return '<BackgroundJob: %s>' % self.strform |
|
387 | 387 | |
|
388 | 388 | def traceback(self): |
|
389 | 389 | print self._tb |
|
390 | 390 | |
|
391 | 391 | def run(self): |
|
392 | 392 | try: |
|
393 | 393 | self.status = BackgroundJobBase.stat_running |
|
394 | 394 | self.stat_code = BackgroundJobBase.stat_running_c |
|
395 | 395 | self.result = self.call() |
|
396 | 396 | except: |
|
397 | 397 | self.status = BackgroundJobBase.stat_dead |
|
398 | 398 | self.stat_code = BackgroundJobBase.stat_dead_c |
|
399 | 399 | self.finished = None |
|
400 | 400 | self.result = ('<BackgroundJob died, call job.traceback() for details>') |
|
401 | 401 | self._tb = self._make_tb() |
|
402 | 402 | else: |
|
403 | 403 | self.status = BackgroundJobBase.stat_completed |
|
404 | 404 | self.stat_code = BackgroundJobBase.stat_completed_c |
|
405 | 405 | self.finished = True |
|
406 | 406 | |
|
407 | 407 | class BackgroundJobExpr(BackgroundJobBase): |
|
408 | 408 | """Evaluate an expression as a background job (uses a separate thread).""" |
|
409 | 409 | |
|
410 | 410 | def __init__(self,expression,glob=None,loc=None): |
|
411 | 411 | """Create a new job from a string which can be fed to eval(). |
|
412 | 412 | |
|
413 | 413 | global/locals dicts can be provided, which will be passed to the eval |
|
414 | 414 | call.""" |
|
415 | 415 | |
|
416 | 416 | # fail immediately if the given expression can't be compiled |
|
417 | 417 | self.code = compile(expression,'<BackgroundJob compilation>','eval') |
|
418 | 418 | |
|
419 | 419 | if glob is None: |
|
420 | 420 | glob = {} |
|
421 | 421 | if loc is None: |
|
422 | 422 | loc = {} |
|
423 | 423 | |
|
424 | 424 | self.expression = self.strform = expression |
|
425 | 425 | self.glob = glob |
|
426 | 426 | self.loc = loc |
|
427 | 427 | self._init() |
|
428 | 428 | |
|
429 | 429 | def call(self): |
|
430 | 430 | return eval(self.code,self.glob,self.loc) |
|
431 | 431 | |
|
432 | 432 | class BackgroundJobFunc(BackgroundJobBase): |
|
433 | 433 | """Run a function call as a background job (uses a separate thread).""" |
|
434 | 434 | |
|
435 | 435 | def __init__(self,func,*args,**kwargs): |
|
436 | 436 | """Create a new job from a callable object. |
|
437 | 437 | |
|
438 | 438 | Any positional arguments and keyword args given to this constructor |
|
439 | 439 | after the initial callable are passed directly to it.""" |
|
440 | 440 | |
|
441 | 441 | assert callable(func),'first argument must be callable' |
|
442 | 442 | |
|
443 | 443 | if args is None: |
|
444 | 444 | args = [] |
|
445 | 445 | if kwargs is None: |
|
446 | 446 | kwargs = {} |
|
447 | 447 | |
|
448 | 448 | self.func = func |
|
449 | 449 | self.args = args |
|
450 | 450 | self.kwargs = kwargs |
|
451 | 451 | # The string form will only include the function passed, because |
|
452 | 452 | # generating string representations of the arguments is a potentially |
|
453 | 453 | # _very_ expensive operation (e.g. with large arrays). |
|
454 | 454 | self.strform = str(func) |
|
455 | 455 | self._init() |
|
456 | 456 | |
|
457 | 457 | def call(self): |
|
458 | 458 | return self.func(*self.args,**self.kwargs) |
|
459 | 459 | |
|
460 | 460 | |
|
461 | 461 | if __name__=='__main__': |
|
462 | 462 | |
|
463 | 463 | import time |
|
464 | 464 | |
|
465 | 465 | def sleepfunc(interval=2,*a,**kw): |
|
466 | 466 | args = dict(interval=interval, |
|
467 | 467 | args=a, |
|
468 | 468 | kwargs=kw) |
|
469 | 469 | time.sleep(interval) |
|
470 | 470 | return args |
|
471 | 471 | |
|
472 | 472 | def diefunc(interval=2,*a,**kw): |
|
473 | 473 | time.sleep(interval) |
|
474 | 474 | die |
|
475 | 475 | |
|
476 | 476 | def printfunc(interval=1,reps=5): |
|
477 | 477 | for n in range(reps): |
|
478 | 478 | time.sleep(interval) |
|
479 | 479 | print 'In the background...' |
|
480 | 480 | |
|
481 | 481 | jobs = BackgroundJobManager() |
|
482 | 482 | # first job will have # 0 |
|
483 | 483 | jobs.new(sleepfunc,4) |
|
484 | 484 | jobs.new(sleepfunc,kw={'reps':2}) |
|
485 | 485 | # This makes a job which will die |
|
486 | 486 | jobs.new(diefunc,1) |
|
487 | 487 | jobs.new('printfunc(1,3)') |
|
488 | 488 | |
|
489 | 489 | # after a while, you can get the traceback of a dead job. Run the line |
|
490 | 490 | # below again interactively until it prints a traceback (check the status |
|
491 | 491 | # of the job): |
|
492 | 492 | print jobs[1].status |
|
493 | 493 | jobs[1].traceback() |
|
494 | 494 | |
|
495 | 495 | # Run this line again until the printed result changes |
|
496 | 496 | print "The result of job #0 is:",jobs[0].result |
@@ -1,556 +1,556 b'' | |||
|
1 | 1 | """Word completion for IPython. |
|
2 | 2 | |
|
3 | 3 | This module is a fork of the rlcompleter module in the Python standard |
|
4 | 4 | library. The original enhancements made to rlcompleter have been sent |
|
5 | 5 | upstream and were accepted as of Python 2.3, but we need a lot more |
|
6 | 6 | functionality specific to IPython, so this module will continue to live as an |
|
7 | 7 | IPython-specific utility. |
|
8 | 8 | |
|
9 | 9 | --------------------------------------------------------------------------- |
|
10 | 10 | Original rlcompleter documentation: |
|
11 | 11 | |
|
12 | 12 | This requires the latest extension to the readline module (the |
|
13 | 13 | completes keywords, built-ins and globals in __main__; when completing |
|
14 | 14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
15 | 15 | completes its attributes. |
|
16 | 16 | |
|
17 | 17 | It's very cool to do "import string" type "string.", hit the |
|
18 | 18 | completion key (twice), and see the list of names defined by the |
|
19 | 19 | string module! |
|
20 | 20 | |
|
21 | 21 | Tip: to use the tab key as the completion key, call |
|
22 | 22 | |
|
23 | 23 | readline.parse_and_bind("tab: complete") |
|
24 | 24 | |
|
25 | 25 | Notes: |
|
26 | 26 | |
|
27 | 27 | - Exceptions raised by the completer function are *ignored* (and |
|
28 | 28 | generally cause the completion to fail). This is a feature -- since |
|
29 | 29 | readline sets the tty device in raw (or cbreak) mode, printing a |
|
30 | 30 | traceback wouldn't work well without some complicated hoopla to save, |
|
31 | 31 | reset and restore the tty state. |
|
32 | 32 | |
|
33 | 33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
34 | 34 | application defined code to be executed if an object with a |
|
35 | 35 | __getattr__ hook is found. Since it is the responsibility of the |
|
36 | 36 | application (or the user) to enable this feature, I consider this an |
|
37 | 37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
38 | 38 | indexing operations) are *not* evaluated. |
|
39 | 39 | |
|
40 | 40 | - GNU readline is also used by the built-in functions input() and |
|
41 | 41 | raw_input(), and thus these also benefit/suffer from the completer |
|
42 | 42 | features. Clearly an interactive application can benefit by |
|
43 | 43 | specifying its own completer function and using raw_input() for all |
|
44 | 44 | its input. |
|
45 | 45 | |
|
46 | 46 | - When the original stdin is not a tty device, GNU readline is never |
|
47 | 47 | used, and this module (and the readline module) are silently inactive. |
|
48 | 48 | |
|
49 | 49 | """ |
|
50 | 50 | |
|
51 | 51 | #***************************************************************************** |
|
52 | 52 | # |
|
53 | 53 | # Since this file is essentially a minimally modified copy of the rlcompleter |
|
54 | 54 | # module which is part of the standard Python distribution, I assume that the |
|
55 | 55 | # proper procedure is to maintain its copyright as belonging to the Python |
|
56 | 56 | # Software Foundation (in addition to my own, for all new code). |
|
57 | 57 | # |
|
58 | 58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
59 |
# Copyright (C) 2001-200 |
|
|
59 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
60 | 60 | # |
|
61 | 61 | # Distributed under the terms of the BSD License. The full license is in |
|
62 | 62 | # the file COPYING, distributed as part of this software. |
|
63 | 63 | # |
|
64 | 64 | #***************************************************************************** |
|
65 | 65 | |
|
66 | 66 | import __builtin__ |
|
67 | 67 | import __main__ |
|
68 | 68 | import glob |
|
69 | 69 | import keyword |
|
70 | 70 | import os |
|
71 | 71 | import re |
|
72 | 72 | import readline |
|
73 | 73 | import sys |
|
74 | 74 | import types |
|
75 | 75 | |
|
76 | 76 | # Python 2.4 offers sets as a builtin |
|
77 | 77 | try: |
|
78 | 78 | set([1,2]) |
|
79 | 79 | except NameError: |
|
80 | 80 | from sets import Set as set |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | from IPython.genutils import shlex_split |
|
84 | 84 | |
|
85 | 85 | __all__ = ['Completer','IPCompleter'] |
|
86 | 86 | |
|
87 | 87 | def get_class_members(cls): |
|
88 | 88 | ret = dir(cls) |
|
89 | 89 | if hasattr(cls,'__bases__'): |
|
90 | 90 | for base in cls.__bases__: |
|
91 | 91 | ret.extend(get_class_members(base)) |
|
92 | 92 | return ret |
|
93 | 93 | |
|
94 | 94 | class Completer: |
|
95 | 95 | def __init__(self,namespace=None,global_namespace=None): |
|
96 | 96 | """Create a new completer for the command line. |
|
97 | 97 | |
|
98 | 98 | Completer([namespace,global_namespace]) -> completer instance. |
|
99 | 99 | |
|
100 | 100 | If unspecified, the default namespace where completions are performed |
|
101 | 101 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
102 | 102 | given as dictionaries. |
|
103 | 103 | |
|
104 | 104 | An optional second namespace can be given. This allows the completer |
|
105 | 105 | to handle cases where both the local and global scopes need to be |
|
106 | 106 | distinguished. |
|
107 | 107 | |
|
108 | 108 | Completer instances should be used as the completion mechanism of |
|
109 | 109 | readline via the set_completer() call: |
|
110 | 110 | |
|
111 | 111 | readline.set_completer(Completer(my_namespace).complete) |
|
112 | 112 | """ |
|
113 | 113 | |
|
114 | 114 | # some minimal strict typechecks. For some core data structures, I |
|
115 | 115 | # want actual basic python types, not just anything that looks like |
|
116 | 116 | # one. This is especially true for namespaces. |
|
117 | 117 | for ns in (namespace,global_namespace): |
|
118 | 118 | if ns is not None and type(ns) != types.DictType: |
|
119 | 119 | raise TypeError,'namespace must be a dictionary' |
|
120 | 120 | |
|
121 | 121 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
122 | 122 | # specific namespace or to use __main__.__dict__. This will allow us |
|
123 | 123 | # to bind to __main__.__dict__ at completion time, not now. |
|
124 | 124 | if namespace is None: |
|
125 | 125 | self.use_main_ns = 1 |
|
126 | 126 | else: |
|
127 | 127 | self.use_main_ns = 0 |
|
128 | 128 | self.namespace = namespace |
|
129 | 129 | |
|
130 | 130 | # The global namespace, if given, can be bound directly |
|
131 | 131 | if global_namespace is None: |
|
132 | 132 | self.global_namespace = {} |
|
133 | 133 | else: |
|
134 | 134 | self.global_namespace = global_namespace |
|
135 | 135 | |
|
136 | 136 | def complete(self, text, state): |
|
137 | 137 | """Return the next possible completion for 'text'. |
|
138 | 138 | |
|
139 | 139 | This is called successively with state == 0, 1, 2, ... until it |
|
140 | 140 | returns None. The completion should begin with 'text'. |
|
141 | 141 | |
|
142 | 142 | """ |
|
143 | 143 | if self.use_main_ns: |
|
144 | 144 | self.namespace = __main__.__dict__ |
|
145 | 145 | |
|
146 | 146 | if state == 0: |
|
147 | 147 | if "." in text: |
|
148 | 148 | self.matches = self.attr_matches(text) |
|
149 | 149 | else: |
|
150 | 150 | self.matches = self.global_matches(text) |
|
151 | 151 | try: |
|
152 | 152 | return self.matches[state] |
|
153 | 153 | except IndexError: |
|
154 | 154 | return None |
|
155 | 155 | |
|
156 | 156 | def global_matches(self, text): |
|
157 | 157 | """Compute matches when text is a simple name. |
|
158 | 158 | |
|
159 | 159 | Return a list of all keywords, built-in functions and names currently |
|
160 | 160 | defined in self.namespace or self.global_namespace that match. |
|
161 | 161 | |
|
162 | 162 | """ |
|
163 | 163 | matches = [] |
|
164 | 164 | match_append = matches.append |
|
165 | 165 | n = len(text) |
|
166 | 166 | for lst in [keyword.kwlist, |
|
167 | 167 | __builtin__.__dict__.keys(), |
|
168 | 168 | self.namespace.keys(), |
|
169 | 169 | self.global_namespace.keys()]: |
|
170 | 170 | for word in lst: |
|
171 | 171 | if word[:n] == text and word != "__builtins__": |
|
172 | 172 | match_append(word) |
|
173 | 173 | return matches |
|
174 | 174 | |
|
175 | 175 | def attr_matches(self, text): |
|
176 | 176 | """Compute matches when text contains a dot. |
|
177 | 177 | |
|
178 | 178 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
179 | 179 | evaluatable in self.namespace or self.global_namespace, it will be |
|
180 | 180 | evaluated and its attributes (as revealed by dir()) are used as |
|
181 | 181 | possible completions. (For class instances, class members are are |
|
182 | 182 | also considered.) |
|
183 | 183 | |
|
184 | 184 | WARNING: this can still invoke arbitrary C code, if an object |
|
185 | 185 | with a __getattr__ hook is evaluated. |
|
186 | 186 | |
|
187 | 187 | """ |
|
188 | 188 | import re |
|
189 | 189 | |
|
190 | 190 | # Another option, seems to work great. Catches things like ''.<tab> |
|
191 | 191 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
192 | 192 | |
|
193 | 193 | if not m: |
|
194 | 194 | return [] |
|
195 | 195 | |
|
196 | 196 | expr, attr = m.group(1, 3) |
|
197 | 197 | try: |
|
198 | 198 | object = eval(expr, self.namespace) |
|
199 | 199 | except: |
|
200 | 200 | object = eval(expr, self.global_namespace) |
|
201 | 201 | |
|
202 | 202 | # Start building the attribute list via dir(), and then complete it |
|
203 | 203 | # with a few extra special-purpose calls. |
|
204 | 204 | words = dir(object) |
|
205 | 205 | |
|
206 | 206 | if hasattr(object,'__class__'): |
|
207 | 207 | words.append('__class__') |
|
208 | 208 | words.extend(get_class_members(object.__class__)) |
|
209 | 209 | |
|
210 | 210 | # this is the 'dir' function for objects with Enthought's traits |
|
211 | 211 | if hasattr(object, 'trait_names'): |
|
212 | 212 | words.extend(object.trait_names()) |
|
213 | 213 | # eliminate possible duplicates, as some traits may also appear as |
|
214 | 214 | # normal attributes in the dir() call. |
|
215 | 215 | words = set(words) |
|
216 | 216 | |
|
217 | 217 | # filter out non-string attributes which may be stuffed by dir() calls |
|
218 | 218 | # and poor coding in third-party modules |
|
219 | 219 | words = [w for w in words |
|
220 | 220 | if isinstance(w, basestring) and w != "__builtins__"] |
|
221 | 221 | # Build match list to return |
|
222 | 222 | n = len(attr) |
|
223 | 223 | return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
224 | 224 | |
|
225 | 225 | class IPCompleter(Completer): |
|
226 | 226 | """Extension of the completer class with IPython-specific features""" |
|
227 | 227 | |
|
228 | 228 | def __init__(self,shell,namespace=None,global_namespace=None, |
|
229 | 229 | omit__names=0,alias_table=None): |
|
230 | 230 | """IPCompleter() -> completer |
|
231 | 231 | |
|
232 | 232 | Return a completer object suitable for use by the readline library |
|
233 | 233 | via readline.set_completer(). |
|
234 | 234 | |
|
235 | 235 | Inputs: |
|
236 | 236 | |
|
237 | 237 | - shell: a pointer to the ipython shell itself. This is needed |
|
238 | 238 | because this completer knows about magic functions, and those can |
|
239 | 239 | only be accessed via the ipython instance. |
|
240 | 240 | |
|
241 | 241 | - namespace: an optional dict where completions are performed. |
|
242 | 242 | |
|
243 | 243 | - global_namespace: secondary optional dict for completions, to |
|
244 | 244 | handle cases (such as IPython embedded inside functions) where |
|
245 | 245 | both Python scopes are visible. |
|
246 | 246 | |
|
247 | 247 | - The optional omit__names parameter sets the completer to omit the |
|
248 | 248 | 'magic' names (__magicname__) for python objects unless the text |
|
249 | 249 | to be completed explicitly starts with one or more underscores. |
|
250 | 250 | |
|
251 | 251 | - If alias_table is supplied, it should be a dictionary of aliases |
|
252 | 252 | to complete. """ |
|
253 | 253 | |
|
254 | 254 | Completer.__init__(self,namespace,global_namespace) |
|
255 | 255 | self.magic_prefix = shell.name+'.magic_' |
|
256 | 256 | self.magic_escape = shell.ESC_MAGIC |
|
257 | 257 | self.readline = readline |
|
258 | 258 | delims = self.readline.get_completer_delims() |
|
259 | 259 | delims = delims.replace(self.magic_escape,'') |
|
260 | 260 | self.readline.set_completer_delims(delims) |
|
261 | 261 | self.get_line_buffer = self.readline.get_line_buffer |
|
262 | 262 | self.omit__names = omit__names |
|
263 | 263 | self.merge_completions = shell.rc.readline_merge_completions |
|
264 | 264 | |
|
265 | 265 | if alias_table is None: |
|
266 | 266 | alias_table = {} |
|
267 | 267 | self.alias_table = alias_table |
|
268 | 268 | # Regexp to split filenames with spaces in them |
|
269 | 269 | self.space_name_re = re.compile(r'([^\\] )') |
|
270 | 270 | # Hold a local ref. to glob.glob for speed |
|
271 | 271 | self.glob = glob.glob |
|
272 | 272 | |
|
273 | 273 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
274 | 274 | # buffers, to avoid completion problems. |
|
275 | 275 | term = os.environ.get('TERM','xterm') |
|
276 | 276 | self.dumb_terminal = term in ['dumb','emacs'] |
|
277 | 277 | |
|
278 | 278 | # Special handling of backslashes needed in win32 platforms |
|
279 | 279 | if sys.platform == "win32": |
|
280 | 280 | self.clean_glob = self._clean_glob_win32 |
|
281 | 281 | else: |
|
282 | 282 | self.clean_glob = self._clean_glob |
|
283 | 283 | self.matchers = [self.python_matches, |
|
284 | 284 | self.file_matches, |
|
285 | 285 | self.alias_matches, |
|
286 | 286 | self.python_func_kw_matches] |
|
287 | 287 | |
|
288 | 288 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
289 | 289 | def all_completions(self, text): |
|
290 | 290 | """Return all possible completions for the benefit of emacs.""" |
|
291 | 291 | |
|
292 | 292 | completions = [] |
|
293 | 293 | comp_append = completions.append |
|
294 | 294 | try: |
|
295 | 295 | for i in xrange(sys.maxint): |
|
296 | 296 | res = self.complete(text, i) |
|
297 | 297 | |
|
298 | 298 | if not res: break |
|
299 | 299 | |
|
300 | 300 | comp_append(res) |
|
301 | 301 | #XXX workaround for ``notDefined.<tab>`` |
|
302 | 302 | except NameError: |
|
303 | 303 | pass |
|
304 | 304 | return completions |
|
305 | 305 | # /end Alex Schmolck code. |
|
306 | 306 | |
|
307 | 307 | def _clean_glob(self,text): |
|
308 | 308 | return self.glob("%s*" % text) |
|
309 | 309 | |
|
310 | 310 | def _clean_glob_win32(self,text): |
|
311 | 311 | return [f.replace("\\","/") |
|
312 | 312 | for f in self.glob("%s*" % text)] |
|
313 | 313 | |
|
314 | 314 | def file_matches(self, text): |
|
315 | 315 | """Match filneames, expanding ~USER type strings. |
|
316 | 316 | |
|
317 | 317 | Most of the seemingly convoluted logic in this completer is an |
|
318 | 318 | attempt to handle filenames with spaces in them. And yet it's not |
|
319 | 319 | quite perfect, because Python's readline doesn't expose all of the |
|
320 | 320 | GNU readline details needed for this to be done correctly. |
|
321 | 321 | |
|
322 | 322 | For a filename with a space in it, the printed completions will be |
|
323 | 323 | only the parts after what's already been typed (instead of the |
|
324 | 324 | full completions, as is normally done). I don't think with the |
|
325 | 325 | current (as of Python 2.3) Python readline it's possible to do |
|
326 | 326 | better.""" |
|
327 | 327 | |
|
328 | 328 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
329 | 329 | |
|
330 | 330 | # chars that require escaping with backslash - i.e. chars |
|
331 | 331 | # that readline treats incorrectly as delimiters, but we |
|
332 | 332 | # don't want to treat as delimiters in filename matching |
|
333 | 333 | # when escaped with backslash |
|
334 | 334 | |
|
335 | 335 | protectables = ' ()[]{}' |
|
336 | 336 | |
|
337 | 337 | def protect_filename(s): |
|
338 | 338 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
339 | 339 | for ch in s]) |
|
340 | 340 | |
|
341 | 341 | lbuf = self.get_line_buffer()[:self.readline.get_endidx()] |
|
342 | 342 | open_quotes = 0 # track strings with open quotes |
|
343 | 343 | try: |
|
344 | 344 | lsplit = shlex_split(lbuf)[-1] |
|
345 | 345 | except ValueError: |
|
346 | 346 | # typically an unmatched ", or backslash without escaped char. |
|
347 | 347 | if lbuf.count('"')==1: |
|
348 | 348 | open_quotes = 1 |
|
349 | 349 | lsplit = lbuf.split('"')[-1] |
|
350 | 350 | elif lbuf.count("'")==1: |
|
351 | 351 | open_quotes = 1 |
|
352 | 352 | lsplit = lbuf.split("'")[-1] |
|
353 | 353 | else: |
|
354 | 354 | return None |
|
355 | 355 | except IndexError: |
|
356 | 356 | # tab pressed on empty line |
|
357 | 357 | lsplit = "" |
|
358 | 358 | |
|
359 | 359 | if lsplit != protect_filename(lsplit): |
|
360 | 360 | # if protectables are found, do matching on the whole escaped |
|
361 | 361 | # name |
|
362 | 362 | has_protectables = 1 |
|
363 | 363 | text0,text = text,lsplit |
|
364 | 364 | else: |
|
365 | 365 | has_protectables = 0 |
|
366 | 366 | text = os.path.expanduser(text) |
|
367 | 367 | |
|
368 | 368 | if text == "": |
|
369 | 369 | return [protect_filename(f) for f in self.glob("*")] |
|
370 | 370 | |
|
371 | 371 | m0 = self.clean_glob(text.replace('\\','')) |
|
372 | 372 | if has_protectables: |
|
373 | 373 | # If we had protectables, we need to revert our changes to the |
|
374 | 374 | # beginning of filename so that we don't double-write the part |
|
375 | 375 | # of the filename we have so far |
|
376 | 376 | len_lsplit = len(lsplit) |
|
377 | 377 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] |
|
378 | 378 | else: |
|
379 | 379 | if open_quotes: |
|
380 | 380 | # if we have a string with an open quote, we don't need to |
|
381 | 381 | # protect the names at all (and we _shouldn't_, as it |
|
382 | 382 | # would cause bugs when the filesystem call is made). |
|
383 | 383 | matches = m0 |
|
384 | 384 | else: |
|
385 | 385 | matches = [protect_filename(f) for f in m0] |
|
386 | 386 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
387 | 387 | # Takes care of links to directories also. Use '/' |
|
388 | 388 | # explicitly, even under Windows, so that name completions |
|
389 | 389 | # don't end up escaped. |
|
390 | 390 | matches[0] += '/' |
|
391 | 391 | return matches |
|
392 | 392 | |
|
393 | 393 | def alias_matches(self, text): |
|
394 | 394 | """Match internal system aliases""" |
|
395 | 395 | #print 'Completer->alias_matches:',text # dbg |
|
396 | 396 | text = os.path.expanduser(text) |
|
397 | 397 | aliases = self.alias_table.keys() |
|
398 | 398 | if text == "": |
|
399 | 399 | return aliases |
|
400 | 400 | else: |
|
401 | 401 | return [alias for alias in aliases if alias.startswith(text)] |
|
402 | 402 | |
|
403 | 403 | def python_matches(self,text): |
|
404 | 404 | """Match attributes or global python names""" |
|
405 | 405 | #print 'Completer->python_matches' # dbg |
|
406 | 406 | if "." in text: |
|
407 | 407 | try: |
|
408 | 408 | matches = self.attr_matches(text) |
|
409 | 409 | if text.endswith('.') and self.omit__names: |
|
410 | 410 | if self.omit__names == 1: |
|
411 | 411 | # true if txt is _not_ a __ name, false otherwise: |
|
412 | 412 | no__name = (lambda txt: |
|
413 | 413 | re.match(r'.*\.__.*?__',txt) is None) |
|
414 | 414 | else: |
|
415 | 415 | # true if txt is _not_ a _ name, false otherwise: |
|
416 | 416 | no__name = (lambda txt: |
|
417 | 417 | re.match(r'.*\._.*?',txt) is None) |
|
418 | 418 | matches = filter(no__name, matches) |
|
419 | 419 | except NameError: |
|
420 | 420 | # catches <undefined attributes>.<tab> |
|
421 | 421 | matches = [] |
|
422 | 422 | else: |
|
423 | 423 | matches = self.global_matches(text) |
|
424 | 424 | # this is so completion finds magics when automagic is on: |
|
425 | 425 | if matches == [] and not text.startswith(os.sep): |
|
426 | 426 | matches = self.attr_matches(self.magic_prefix+text) |
|
427 | 427 | return matches |
|
428 | 428 | |
|
429 | 429 | def _default_arguments(self, obj): |
|
430 | 430 | """Return the list of default arguments of obj if it is callable, |
|
431 | 431 | or empty list otherwise.""" |
|
432 | 432 | |
|
433 | 433 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
434 | 434 | # for classes, check for __init__,__new__ |
|
435 | 435 | if inspect.isclass(obj): |
|
436 | 436 | obj = (getattr(obj,'__init__',None) or |
|
437 | 437 | getattr(obj,'__new__',None)) |
|
438 | 438 | # for all others, check if they are __call__able |
|
439 | 439 | elif hasattr(obj, '__call__'): |
|
440 | 440 | obj = obj.__call__ |
|
441 | 441 | # XXX: is there a way to handle the builtins ? |
|
442 | 442 | try: |
|
443 | 443 | args,_,_1,defaults = inspect.getargspec(obj) |
|
444 | 444 | if defaults: |
|
445 | 445 | return args[-len(defaults):] |
|
446 | 446 | except TypeError: pass |
|
447 | 447 | return [] |
|
448 | 448 | |
|
449 | 449 | def python_func_kw_matches(self,text): |
|
450 | 450 | """Match named parameters (kwargs) of the last open function""" |
|
451 | 451 | |
|
452 | 452 | if "." in text: # a parameter cannot be dotted |
|
453 | 453 | return [] |
|
454 | 454 | try: regexp = self.__funcParamsRegex |
|
455 | 455 | except AttributeError: |
|
456 | 456 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
457 | 457 | '.*?' | # single quoted strings or |
|
458 | 458 | ".*?" | # double quoted strings or |
|
459 | 459 | \w+ | # identifier |
|
460 | 460 | \S # other characters |
|
461 | 461 | ''', re.VERBOSE | re.DOTALL) |
|
462 | 462 | # 1. find the nearest identifier that comes before an unclosed |
|
463 | 463 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
464 | 464 | tokens = regexp.findall(self.get_line_buffer()) |
|
465 | 465 | tokens.reverse() |
|
466 | 466 | iterTokens = iter(tokens); openPar = 0 |
|
467 | 467 | for token in iterTokens: |
|
468 | 468 | if token == ')': |
|
469 | 469 | openPar -= 1 |
|
470 | 470 | elif token == '(': |
|
471 | 471 | openPar += 1 |
|
472 | 472 | if openPar > 0: |
|
473 | 473 | # found the last unclosed parenthesis |
|
474 | 474 | break |
|
475 | 475 | else: |
|
476 | 476 | return [] |
|
477 | 477 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
478 | 478 | ids = [] |
|
479 | 479 | isId = re.compile(r'\w+$').match |
|
480 | 480 | while True: |
|
481 | 481 | try: |
|
482 | 482 | ids.append(iterTokens.next()) |
|
483 | 483 | if not isId(ids[-1]): |
|
484 | 484 | ids.pop(); break |
|
485 | 485 | if not iterTokens.next() == '.': |
|
486 | 486 | break |
|
487 | 487 | except StopIteration: |
|
488 | 488 | break |
|
489 | 489 | # lookup the candidate callable matches either using global_matches |
|
490 | 490 | # or attr_matches for dotted names |
|
491 | 491 | if len(ids) == 1: |
|
492 | 492 | callableMatches = self.global_matches(ids[0]) |
|
493 | 493 | else: |
|
494 | 494 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
495 | 495 | argMatches = [] |
|
496 | 496 | for callableMatch in callableMatches: |
|
497 | 497 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
498 | 498 | self.namespace)) |
|
499 | 499 | except: continue |
|
500 | 500 | for namedArg in namedArgs: |
|
501 | 501 | if namedArg.startswith(text): |
|
502 | 502 | argMatches.append("%s=" %namedArg) |
|
503 | 503 | return argMatches |
|
504 | 504 | |
|
505 | 505 | def complete(self, text, state): |
|
506 | 506 | """Return the next possible completion for 'text'. |
|
507 | 507 | |
|
508 | 508 | This is called successively with state == 0, 1, 2, ... until it |
|
509 | 509 | returns None. The completion should begin with 'text'. """ |
|
510 | 510 | |
|
511 | 511 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
512 | 512 | |
|
513 | 513 | # if there is only a tab on a line with only whitespace, instead |
|
514 | 514 | # of the mostly useless 'do you want to see all million |
|
515 | 515 | # completions' message, just do the right thing and give the user |
|
516 | 516 | # his tab! Incidentally, this enables pasting of tabbed text from |
|
517 | 517 | # an editor (as long as autoindent is off). |
|
518 | 518 | |
|
519 | 519 | # don't apply this on 'dumb' terminals, such as emacs buffers, so we |
|
520 | 520 | # don't interfere with their own tab-completion mechanism. |
|
521 | 521 | if not (self.dumb_terminal or self.get_line_buffer().strip()): |
|
522 | 522 | self.readline.insert_text('\t') |
|
523 | 523 | return None |
|
524 | 524 | |
|
525 | 525 | magic_escape = self.magic_escape |
|
526 | 526 | magic_prefix = self.magic_prefix |
|
527 | 527 | |
|
528 | 528 | try: |
|
529 | 529 | if text.startswith(magic_escape): |
|
530 | 530 | text = text.replace(magic_escape,magic_prefix) |
|
531 | 531 | elif text.startswith('~'): |
|
532 | 532 | text = os.path.expanduser(text) |
|
533 | 533 | if state == 0: |
|
534 | 534 | # Extend the list of completions with the results of each |
|
535 | 535 | # matcher, so we return results to the user from all |
|
536 | 536 | # namespaces. |
|
537 | 537 | if self.merge_completions: |
|
538 | 538 | self.matches = [] |
|
539 | 539 | for matcher in self.matchers: |
|
540 | 540 | self.matches.extend(matcher(text)) |
|
541 | 541 | else: |
|
542 | 542 | for matcher in self.matchers: |
|
543 | 543 | self.matches = matcher(text) |
|
544 | 544 | if self.matches: |
|
545 | 545 | break |
|
546 | 546 | |
|
547 | 547 | try: |
|
548 | 548 | return self.matches[state].replace(magic_prefix,magic_escape) |
|
549 | 549 | except IndexError: |
|
550 | 550 | return None |
|
551 | 551 | except: |
|
552 | 552 | #from IPython.ultraTB import AutoFormattedTB; # dbg |
|
553 | 553 | #tb=AutoFormattedTB('Verbose');tb() #dbg |
|
554 | 554 | |
|
555 | 555 | # If completion fails, don't annoy the user. |
|
556 | 556 | return None |
@@ -1,309 +1,309 b'' | |||
|
1 | 1 | """Module for interactive demos using IPython. |
|
2 | 2 | |
|
3 | 3 | This module implements a single class, Demo, for running Python scripts |
|
4 | 4 | interactively in IPython for demonstrations. With very simple markup (a few |
|
5 | 5 | tags in comments), you can control points where the script stops executing and |
|
6 | 6 | returns control to IPython. |
|
7 | 7 | |
|
8 | 8 | The file is run in its own empty namespace (though you can pass it a string of |
|
9 | 9 | arguments as if in a command line environment, and it will see those as |
|
10 | 10 | sys.argv). But at each stop, the global IPython namespace is updated with the |
|
11 | 11 | current internal demo namespace, so you can work interactively with the data |
|
12 | 12 | accumulated so far. |
|
13 | 13 | |
|
14 | 14 | By default, each block of code is printed (with syntax highlighting) before |
|
15 | 15 | executing it and you have to confirm execution. This is intended to show the |
|
16 | 16 | code to an audience first so you can discuss it, and only proceed with |
|
17 | 17 | execution once you agree. There are a few tags which allow you to modify this |
|
18 | 18 | behavior. |
|
19 | 19 | |
|
20 | 20 | The supported tags are: |
|
21 | 21 | |
|
22 | 22 | # <demo> --- stop --- |
|
23 | 23 | |
|
24 | 24 | Defines block boundaries, the points where IPython stops execution of the |
|
25 | 25 | file and returns to the interactive prompt. |
|
26 | 26 | |
|
27 | 27 | # <demo> silent |
|
28 | 28 | |
|
29 | 29 | Make a block execute silently (and hence automatically). Typically used in |
|
30 | 30 | cases where you have some boilerplate or initialization code which you need |
|
31 | 31 | executed but do not want to be seen in the demo. |
|
32 | 32 | |
|
33 | 33 | # <demo> auto |
|
34 | 34 | |
|
35 | 35 | Make a block execute automatically, but still being printed. Useful for |
|
36 | 36 | simple code which does not warrant discussion, since it avoids the extra |
|
37 | 37 | manual confirmation. |
|
38 | 38 | |
|
39 | 39 | # <demo> auto_all |
|
40 | 40 | |
|
41 | 41 | This tag can _only_ be in the first block, and if given it overrides the |
|
42 | 42 | individual auto tags to make the whole demo fully automatic (no block asks |
|
43 | 43 | for confirmation). It can also be given at creation time (or the attribute |
|
44 | 44 | set later) to override what's in the file. |
|
45 | 45 | |
|
46 | 46 | While _any_ python file can be run as a Demo instance, if there are no stop |
|
47 | 47 | tags the whole file will run in a single block (no different that calling |
|
48 | 48 | first %pycat and then %run). The minimal markup to make this useful is to |
|
49 | 49 | place a set of stop tags; the other tags are only there to let you fine-tune |
|
50 | 50 | the execution. |
|
51 | 51 | |
|
52 | 52 | This is probably best explained with the simple example file below. You can |
|
53 | 53 | copy this into a file named ex_demo.py, and try running it via: |
|
54 | 54 | |
|
55 | 55 | from IPython.demo import Demo |
|
56 | 56 | d = Demo('ex_demo.py') |
|
57 | 57 | d() <--- Call the d object (omit the parens if you have autocall on). |
|
58 | 58 | |
|
59 | 59 | Each time you call the demo object, it runs the next block. The demo object |
|
60 | 60 | has a few useful methods for navigation, like again(), jump(), seek() and |
|
61 | 61 | back(). It can be reset for a new run via reset() or reloaded from disk (in |
|
62 | 62 | case you've edited the source) via reload(). See their docstrings below. |
|
63 | 63 | |
|
64 | 64 | #################### EXAMPLE DEMO <ex_demo.py> ############################### |
|
65 | 65 | '''A simple interactive demo to illustrate the use of IPython's Demo class.''' |
|
66 | 66 | |
|
67 | 67 | print 'Hello, welcome to an interactive IPython demo.' |
|
68 | 68 | |
|
69 | 69 | # The mark below defines a block boundary, which is a point where IPython will |
|
70 | 70 | # stop execution and return to the interactive prompt. |
|
71 | 71 | # Note that in actual interactive execution, |
|
72 | 72 | # <demo> --- stop --- |
|
73 | 73 | |
|
74 | 74 | x = 1 |
|
75 | 75 | y = 2 |
|
76 | 76 | |
|
77 | 77 | # <demo> --- stop --- |
|
78 | 78 | |
|
79 | 79 | # the mark below makes this block as silent |
|
80 | 80 | # <demo> silent |
|
81 | 81 | |
|
82 | 82 | print 'This is a silent block, which gets executed but not printed.' |
|
83 | 83 | |
|
84 | 84 | # <demo> --- stop --- |
|
85 | 85 | # <demo> auto |
|
86 | 86 | print 'This is an automatic block.' |
|
87 | 87 | print 'It is executed without asking for confirmation, but printed.' |
|
88 | 88 | z = x+y |
|
89 | 89 | |
|
90 | 90 | print 'z=',x |
|
91 | 91 | |
|
92 | 92 | # <demo> --- stop --- |
|
93 | 93 | # This is just another normal block. |
|
94 | 94 | print 'z is now:', z |
|
95 | 95 | |
|
96 | 96 | print 'bye!' |
|
97 | 97 | ################### END EXAMPLE DEMO <ex_demo.py> ############################ |
|
98 | 98 | |
|
99 | 99 | WARNING: this module uses Python 2.3 features, so it won't work in 2.2 |
|
100 | 100 | environments. |
|
101 | 101 | """ |
|
102 | 102 | #***************************************************************************** |
|
103 |
# |
|
|
103 | # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu> | |
|
104 | 104 | # |
|
105 | 105 | # Distributed under the terms of the BSD License. The full license is in |
|
106 | 106 | # the file COPYING, distributed as part of this software. |
|
107 | 107 | # |
|
108 | 108 | #***************************************************************************** |
|
109 | 109 | |
|
110 | 110 | import exceptions |
|
111 | 111 | import re |
|
112 | 112 | import sys |
|
113 | 113 | |
|
114 | 114 | from IPython.PyColorize import Parser |
|
115 | 115 | from IPython.genutils import marquee, shlex_split, file_read |
|
116 | 116 | |
|
117 | 117 | __all__ = ['Demo','DemoError'] |
|
118 | 118 | |
|
119 | 119 | class DemoError(exceptions.Exception): pass |
|
120 | 120 | |
|
121 | 121 | def re_mark(mark): |
|
122 | 122 | return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE) |
|
123 | 123 | |
|
124 | 124 | class Demo: |
|
125 | 125 | |
|
126 | 126 | re_stop = re_mark('---\s?stop\s?---') |
|
127 | 127 | re_silent = re_mark('silent') |
|
128 | 128 | re_auto = re_mark('auto') |
|
129 | 129 | re_auto_all = re_mark('auto_all') |
|
130 | 130 | |
|
131 | 131 | def __init__(self,fname,arg_str='',auto_all=None): |
|
132 | 132 | """Make a new demo object. To run the demo, simply call the object. |
|
133 | 133 | |
|
134 | 134 | See the module docstring for full details and an example (you can use |
|
135 | 135 | IPython.Demo? in IPython to see it). |
|
136 | 136 | |
|
137 | 137 | Inputs: |
|
138 | 138 | |
|
139 | 139 | - fname = filename. |
|
140 | 140 | |
|
141 | 141 | Optional inputs: |
|
142 | 142 | |
|
143 | 143 | - arg_str(''): a string of arguments, internally converted to a list |
|
144 | 144 | just like sys.argv, so the demo script can see a similar |
|
145 | 145 | environment. |
|
146 | 146 | |
|
147 | 147 | - auto_all(None): global flag to run all blocks automatically without |
|
148 | 148 | confirmation. This attribute overrides the block-level tags and |
|
149 | 149 | applies to the whole demo. It is an attribute of the object, and |
|
150 | 150 | can be changed at runtime simply by reassigning it to a boolean |
|
151 | 151 | value. |
|
152 | 152 | """ |
|
153 | 153 | |
|
154 | 154 | self.fname = fname |
|
155 | 155 | self.sys_argv = [fname] + shlex_split(arg_str) |
|
156 | 156 | self.auto_all = auto_all |
|
157 | 157 | |
|
158 | 158 | # get a few things from ipython. While it's a bit ugly design-wise, |
|
159 | 159 | # it ensures that things like color scheme and the like are always in |
|
160 | 160 | # sync with the ipython mode being used. This class is only meant to |
|
161 | 161 | # be used inside ipython anyways, so it's OK. |
|
162 | 162 | self.ip_showtb = __IPYTHON__.showtraceback |
|
163 | 163 | self.ip_ns = __IPYTHON__.user_ns |
|
164 | 164 | self.ip_colorize = __IPYTHON__.pycolorize |
|
165 | 165 | |
|
166 | 166 | # load user data and initialize data structures |
|
167 | 167 | self.reload() |
|
168 | 168 | |
|
169 | 169 | def reload(self): |
|
170 | 170 | """Reload source from disk and initialize state.""" |
|
171 | 171 | # read data and parse into blocks |
|
172 | 172 | self.src = file_read(self.fname) |
|
173 | 173 | src_b = [b.strip() for b in self.re_stop.split(self.src) if b] |
|
174 | 174 | self._silent = [bool(self.re_silent.findall(b)) for b in src_b] |
|
175 | 175 | self._auto = [bool(self.re_auto.findall(b)) for b in src_b] |
|
176 | 176 | |
|
177 | 177 | # if auto_all is not given (def. None), we read it from the file |
|
178 | 178 | if self.auto_all is None: |
|
179 | 179 | self.auto_all = bool(self.re_auto_all.findall(src_b[0])) |
|
180 | 180 | else: |
|
181 | 181 | self.auto_all = bool(self.auto_all) |
|
182 | 182 | |
|
183 | 183 | # Clean the sources from all markup so it doesn't get displayed when |
|
184 | 184 | # running the demo |
|
185 | 185 | src_blocks = [] |
|
186 | 186 | auto_strip = lambda s: self.re_auto.sub('',s) |
|
187 | 187 | for i,b in enumerate(src_b): |
|
188 | 188 | if self._auto[i]: |
|
189 | 189 | src_blocks.append(auto_strip(b)) |
|
190 | 190 | else: |
|
191 | 191 | src_blocks.append(b) |
|
192 | 192 | # remove the auto_all marker |
|
193 | 193 | src_blocks[0] = self.re_auto_all.sub('',src_blocks[0]) |
|
194 | 194 | |
|
195 | 195 | self.nblocks = len(src_blocks) |
|
196 | 196 | self.src_blocks = src_blocks |
|
197 | 197 | |
|
198 | 198 | # also build syntax-highlighted source |
|
199 | 199 | self.src_blocks_colored = map(self.ip_colorize,self.src_blocks) |
|
200 | 200 | |
|
201 | 201 | # ensure clean namespace and seek offset |
|
202 | 202 | self.reset() |
|
203 | 203 | |
|
204 | 204 | def reset(self): |
|
205 | 205 | """Reset the namespace and seek pointer to restart the demo""" |
|
206 | 206 | self.user_ns = {} |
|
207 | 207 | self.finished = False |
|
208 | 208 | self.block_index = 0 |
|
209 | 209 | |
|
210 | 210 | def _validate_index(self,index): |
|
211 | 211 | if index<0 or index>=self.nblocks: |
|
212 | 212 | raise ValueError('invalid block index %s' % index) |
|
213 | 213 | |
|
214 | 214 | def seek(self,index): |
|
215 | 215 | """Move the current seek pointer to the given block""" |
|
216 | 216 | self._validate_index(index) |
|
217 | 217 | self.block_index = index |
|
218 | 218 | self.finished = False |
|
219 | 219 | |
|
220 | 220 | def back(self,num=1): |
|
221 | 221 | """Move the seek pointer back num blocks (default is 1).""" |
|
222 | 222 | self.seek(self.block_index-num) |
|
223 | 223 | |
|
224 | 224 | def jump(self,num): |
|
225 | 225 | """Jump a given number of blocks relative to the current one.""" |
|
226 | 226 | self.seek(self.block_index+num) |
|
227 | 227 | |
|
228 | 228 | def again(self): |
|
229 | 229 | """Move the seek pointer back one block and re-execute.""" |
|
230 | 230 | self.back(1) |
|
231 | 231 | self() |
|
232 | 232 | |
|
233 | 233 | def show(self,index=None): |
|
234 | 234 | """Show a single block on screen""" |
|
235 | 235 | if index is None: |
|
236 | 236 | if self.finished: |
|
237 | 237 | print 'Demo finished. Use reset() if you want to rerun it.' |
|
238 | 238 | return |
|
239 | 239 | index = self.block_index |
|
240 | 240 | else: |
|
241 | 241 | self._validate_index(index) |
|
242 | 242 | print marquee('<%s> block # %s (%s remaining)' % |
|
243 | 243 | (self.fname,index,self.nblocks-index-1)) |
|
244 | 244 | print self.src_blocks_colored[index], |
|
245 | 245 | |
|
246 | 246 | def show_all(self): |
|
247 | 247 | """Show entire demo on screen, block by block""" |
|
248 | 248 | |
|
249 | 249 | fname = self.fname |
|
250 | 250 | nblocks = self.nblocks |
|
251 | 251 | silent = self._silent |
|
252 | 252 | for index,block in enumerate(self.src_blocks_colored): |
|
253 | 253 | if silent[index]: |
|
254 | 254 | print marquee('<%s> SILENT block # %s (%s remaining)' % |
|
255 | 255 | (fname,index,nblocks-index-1)) |
|
256 | 256 | else: |
|
257 | 257 | print marquee('<%s> block # %s (%s remaining)' % |
|
258 | 258 | (fname,index,nblocks-index-1)) |
|
259 | 259 | print block, |
|
260 | 260 | |
|
261 | 261 | def __call__(self,index=None): |
|
262 | 262 | """run a block of the demo. |
|
263 | 263 | |
|
264 | 264 | If index is given, it should be an integer >=1 and <= nblocks. This |
|
265 | 265 | means that the calling convention is one off from typical Python |
|
266 | 266 | lists. The reason for the inconsistency is that the demo always |
|
267 | 267 | prints 'Block n/N, and N is the total, so it would be very odd to use |
|
268 | 268 | zero-indexing here.""" |
|
269 | 269 | |
|
270 | 270 | if index is None and self.finished: |
|
271 | 271 | print 'Demo finished. Use reset() if you want to rerun it.' |
|
272 | 272 | return |
|
273 | 273 | if index is None: |
|
274 | 274 | index = self.block_index |
|
275 | 275 | self._validate_index(index) |
|
276 | 276 | try: |
|
277 | 277 | next_block = self.src_blocks[index] |
|
278 | 278 | self.block_index += 1 |
|
279 | 279 | if self._silent[index]: |
|
280 | 280 | print marquee('Executing silent block # %s (%s remaining)' % |
|
281 | 281 | (index,self.nblocks-index-1)) |
|
282 | 282 | else: |
|
283 | 283 | self.show(index) |
|
284 | 284 | if self.auto_all or self._auto[index]: |
|
285 | 285 | print marquee('output') |
|
286 | 286 | else: |
|
287 | 287 | print marquee('Press <q> to quit, <Enter> to execute...'), |
|
288 | 288 | ans = raw_input().strip() |
|
289 | 289 | if ans: |
|
290 | 290 | print marquee('Block NOT executed') |
|
291 | 291 | return |
|
292 | 292 | try: |
|
293 | 293 | save_argv = sys.argv |
|
294 | 294 | sys.argv = self.sys_argv |
|
295 | 295 | exec next_block in self.user_ns |
|
296 | 296 | finally: |
|
297 | 297 | sys.argv = save_argv |
|
298 | 298 | |
|
299 | 299 | except: |
|
300 | 300 | self.ip_showtb(filename=self.fname) |
|
301 | 301 | else: |
|
302 | 302 | self.ip_ns.update(self.user_ns) |
|
303 | 303 | |
|
304 | 304 | if self.block_index == self.nblocks: |
|
305 | 305 | |
|
306 | 306 | print marquee(' END OF DEMO ') |
|
307 | 307 | print marquee('Use reset() if you want to rerun it.') |
|
308 | 308 | self.finished = True |
|
309 | 309 |
@@ -1,109 +1,109 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Color schemes for exception handling code in IPython. |
|
4 | 4 | |
|
5 | 5 | $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $""" |
|
6 | 6 | |
|
7 | 7 | #***************************************************************************** |
|
8 | # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu> | |
|
8 | # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu> | |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | from IPython import Release |
|
15 | 15 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
16 | 16 | __license__ = Release.license |
|
17 | 17 | __version__ = Release.version |
|
18 | 18 | |
|
19 | 19 | #**************************************************************************** |
|
20 | 20 | # Required modules |
|
21 | 21 | from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme |
|
22 | 22 | |
|
23 | 23 | ExceptionColors = ColorSchemeTable() |
|
24 | 24 | |
|
25 | 25 | # Populate it with color schemes |
|
26 | 26 | C = TermColors # shorthand and local lookup |
|
27 | 27 | ExceptionColors.add_scheme(ColorScheme( |
|
28 | 28 | 'NoColor', |
|
29 | 29 | # The color to be used for the top line |
|
30 | 30 | topline = C.NoColor, |
|
31 | 31 | |
|
32 | 32 | # The colors to be used in the traceback |
|
33 | 33 | filename = C.NoColor, |
|
34 | 34 | lineno = C.NoColor, |
|
35 | 35 | name = C.NoColor, |
|
36 | 36 | vName = C.NoColor, |
|
37 | 37 | val = C.NoColor, |
|
38 | 38 | em = C.NoColor, |
|
39 | 39 | |
|
40 | 40 | # Emphasized colors for the last frame of the traceback |
|
41 | 41 | normalEm = C.NoColor, |
|
42 | 42 | filenameEm = C.NoColor, |
|
43 | 43 | linenoEm = C.NoColor, |
|
44 | 44 | nameEm = C.NoColor, |
|
45 | 45 | valEm = C.NoColor, |
|
46 | 46 | |
|
47 | 47 | # Colors for printing the exception |
|
48 | 48 | excName = C.NoColor, |
|
49 | 49 | line = C.NoColor, |
|
50 | 50 | caret = C.NoColor, |
|
51 | 51 | Normal = C.NoColor |
|
52 | 52 | )) |
|
53 | 53 | |
|
54 | 54 | # make some schemes as instances so we can copy them for modification easily |
|
55 | 55 | ExceptionColors.add_scheme(ColorScheme( |
|
56 | 56 | 'Linux', |
|
57 | 57 | # The color to be used for the top line |
|
58 | 58 | topline = C.LightRed, |
|
59 | 59 | |
|
60 | 60 | # The colors to be used in the traceback |
|
61 | 61 | filename = C.Green, |
|
62 | 62 | lineno = C.Green, |
|
63 | 63 | name = C.Purple, |
|
64 | 64 | vName = C.Cyan, |
|
65 | 65 | val = C.Green, |
|
66 | 66 | em = C.LightCyan, |
|
67 | 67 | |
|
68 | 68 | # Emphasized colors for the last frame of the traceback |
|
69 | 69 | normalEm = C.LightCyan, |
|
70 | 70 | filenameEm = C.LightGreen, |
|
71 | 71 | linenoEm = C.LightGreen, |
|
72 | 72 | nameEm = C.LightPurple, |
|
73 | 73 | valEm = C.LightBlue, |
|
74 | 74 | |
|
75 | 75 | # Colors for printing the exception |
|
76 | 76 | excName = C.LightRed, |
|
77 | 77 | line = C.Yellow, |
|
78 | 78 | caret = C.White, |
|
79 | 79 | Normal = C.Normal |
|
80 | 80 | )) |
|
81 | 81 | |
|
82 | 82 | # For light backgrounds, swap dark/light colors |
|
83 | 83 | ExceptionColors.add_scheme(ColorScheme( |
|
84 | 84 | 'LightBG', |
|
85 | 85 | # The color to be used for the top line |
|
86 | 86 | topline = C.Red, |
|
87 | 87 | |
|
88 | 88 | # The colors to be used in the traceback |
|
89 | 89 | filename = C.LightGreen, |
|
90 | 90 | lineno = C.LightGreen, |
|
91 | 91 | name = C.LightPurple, |
|
92 | 92 | vName = C.Cyan, |
|
93 | 93 | val = C.LightGreen, |
|
94 | 94 | em = C.Cyan, |
|
95 | 95 | |
|
96 | 96 | # Emphasized colors for the last frame of the traceback |
|
97 | 97 | normalEm = C.Cyan, |
|
98 | 98 | filenameEm = C.Green, |
|
99 | 99 | linenoEm = C.Green, |
|
100 | 100 | nameEm = C.Purple, |
|
101 | 101 | valEm = C.Blue, |
|
102 | 102 | |
|
103 | 103 | # Colors for printing the exception |
|
104 | 104 | excName = C.Red, |
|
105 | 105 | #line = C.Brown, # brown often is displayed as yellow |
|
106 | 106 | line = C.Red, |
|
107 | 107 | caret = C.Normal, |
|
108 | 108 | Normal = C.Normal |
|
109 | 109 | )) |
@@ -1,1717 +1,1717 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | General purpose utilities. |
|
4 | 4 | |
|
5 | 5 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
6 | 6 | these things are also convenient when working at the command line. |
|
7 | 7 | |
|
8 |
$Id: genutils.py 99 |
|
|
8 | $Id: genutils.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 |
# Copyright (C) 2001-200 |
|
|
11 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #***************************************************************************** |
|
16 | 16 | |
|
17 | 17 | from __future__ import generators # 2.2 compatibility |
|
18 | 18 | |
|
19 | 19 | from IPython import Release |
|
20 | 20 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
21 | 21 | __license__ = Release.license |
|
22 | 22 | |
|
23 | 23 | #**************************************************************************** |
|
24 | 24 | # required modules from the Python standard library |
|
25 | 25 | import __main__ |
|
26 | 26 | import commands |
|
27 | 27 | import os |
|
28 | 28 | import re |
|
29 | 29 | import shlex |
|
30 | 30 | import shutil |
|
31 | 31 | import sys |
|
32 | 32 | import tempfile |
|
33 | 33 | import time |
|
34 | 34 | import types |
|
35 | 35 | |
|
36 | 36 | # Other IPython utilities |
|
37 | 37 | from IPython.Itpl import Itpl,itpl,printpl |
|
38 | 38 | from IPython import DPyGetOpt |
|
39 | 39 | |
|
40 | 40 | if os.name == "nt": |
|
41 | 41 | from IPython.winconsole import get_console_size |
|
42 | 42 | |
|
43 | 43 | # Build objects which appeared in Python 2.3 for 2.2, to make ipython |
|
44 | 44 | # 2.2-friendly |
|
45 | 45 | try: |
|
46 | 46 | basestring |
|
47 | 47 | except NameError: |
|
48 | 48 | import types |
|
49 | 49 | basestring = (types.StringType, types.UnicodeType) |
|
50 | 50 | True = 1==1 |
|
51 | 51 | False = 1==0 |
|
52 | 52 | |
|
53 | 53 | def enumerate(obj): |
|
54 | 54 | i = -1 |
|
55 | 55 | for item in obj: |
|
56 | 56 | i += 1 |
|
57 | 57 | yield i, item |
|
58 | 58 | |
|
59 | 59 | # add these to the builtin namespace, so that all modules find them |
|
60 | 60 | import __builtin__ |
|
61 | 61 | __builtin__.basestring = basestring |
|
62 | 62 | __builtin__.True = True |
|
63 | 63 | __builtin__.False = False |
|
64 | 64 | __builtin__.enumerate = enumerate |
|
65 | 65 | |
|
66 | 66 | # Try to use shlex.split for converting an input string into a sys.argv-type |
|
67 | 67 | # list. This appeared in Python 2.3, so here's a quick backport for 2.2. |
|
68 | 68 | try: |
|
69 | 69 | shlex_split = shlex.split |
|
70 | 70 | except AttributeError: |
|
71 | 71 | _quotesre = re.compile(r'[\'"](.*)[\'"]') |
|
72 | 72 | _wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
|
73 | 73 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?' |
|
74 | 74 | 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' |
|
75 | 75 | 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s' |
|
76 | 76 | % os.sep) |
|
77 | 77 | |
|
78 | 78 | def shlex_split(s): |
|
79 | 79 | """Simplified backport to Python 2.2 of shlex.split(). |
|
80 | 80 | |
|
81 | 81 | This is a quick and dirty hack, since the shlex module under 2.2 lacks |
|
82 | 82 | several of the features needed to really match the functionality of |
|
83 | 83 | shlex.split() in 2.3.""" |
|
84 | 84 | |
|
85 | 85 | lex = shlex.shlex(StringIO(s)) |
|
86 | 86 | # Try to get options, extensions and path separators as characters |
|
87 | 87 | lex.wordchars = _wordchars |
|
88 | 88 | lex.commenters = '' |
|
89 | 89 | # Make a list out of the lexer by hand, since in 2.2 it's not an |
|
90 | 90 | # iterator. |
|
91 | 91 | lout = [] |
|
92 | 92 | while 1: |
|
93 | 93 | token = lex.get_token() |
|
94 | 94 | if token == '': |
|
95 | 95 | break |
|
96 | 96 | # Try to handle quoted tokens correctly |
|
97 | 97 | quotes = _quotesre.match(token) |
|
98 | 98 | if quotes: |
|
99 | 99 | token = quotes.group(1) |
|
100 | 100 | lout.append(token) |
|
101 | 101 | return lout |
|
102 | 102 | |
|
103 | 103 | #**************************************************************************** |
|
104 | 104 | # Exceptions |
|
105 | 105 | class Error(Exception): |
|
106 | 106 | """Base class for exceptions in this module.""" |
|
107 | 107 | pass |
|
108 | 108 | |
|
109 | 109 | #---------------------------------------------------------------------------- |
|
110 | 110 | class IOStream: |
|
111 | 111 | def __init__(self,stream,fallback): |
|
112 | 112 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
113 | 113 | stream = fallback |
|
114 | 114 | self.stream = stream |
|
115 | 115 | self._swrite = stream.write |
|
116 | 116 | self.flush = stream.flush |
|
117 | 117 | |
|
118 | 118 | def write(self,data): |
|
119 | 119 | try: |
|
120 | 120 | self._swrite(data) |
|
121 | 121 | except: |
|
122 | 122 | try: |
|
123 | 123 | # print handles some unicode issues which may trip a plain |
|
124 | 124 | # write() call. Attempt to emulate write() by using a |
|
125 | 125 | # trailing comma |
|
126 | 126 | print >> self.stream, data, |
|
127 | 127 | except: |
|
128 | 128 | # if we get here, something is seriously broken. |
|
129 | 129 | print >> sys.stderr, \ |
|
130 | 130 | 'ERROR - failed to write data to stream:', stream |
|
131 | 131 | |
|
132 | 132 | class IOTerm: |
|
133 | 133 | """ Term holds the file or file-like objects for handling I/O operations. |
|
134 | 134 | |
|
135 | 135 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
136 | 136 | Windows they can can replaced to allow editing the strings before they are |
|
137 | 137 | displayed.""" |
|
138 | 138 | |
|
139 | 139 | # In the future, having IPython channel all its I/O operations through |
|
140 | 140 | # this class will make it easier to embed it into other environments which |
|
141 | 141 | # are not a normal terminal (such as a GUI-based shell) |
|
142 | 142 | def __init__(self,cin=None,cout=None,cerr=None): |
|
143 | 143 | self.cin = IOStream(cin,sys.stdin) |
|
144 | 144 | self.cout = IOStream(cout,sys.stdout) |
|
145 | 145 | self.cerr = IOStream(cerr,sys.stderr) |
|
146 | 146 | |
|
147 | 147 | # Global variable to be used for all I/O |
|
148 | 148 | Term = IOTerm() |
|
149 | 149 | |
|
150 | 150 | # Windows-specific code to load Gary Bishop's readline and configure it |
|
151 | 151 | # automatically for the users |
|
152 | 152 | # Note: os.name on cygwin returns posix, so this should only pick up 'native' |
|
153 | 153 | # windows. Cygwin returns 'cygwin' for sys.platform. |
|
154 | 154 | if os.name == 'nt': |
|
155 | 155 | try: |
|
156 | 156 | import readline |
|
157 | 157 | except ImportError: |
|
158 | 158 | pass |
|
159 | 159 | else: |
|
160 | 160 | try: |
|
161 | 161 | _out = readline.GetOutputFile() |
|
162 | 162 | except AttributeError: |
|
163 | 163 | pass |
|
164 | 164 | else: |
|
165 | 165 | # Remake Term to use the readline i/o facilities |
|
166 | 166 | Term = IOTerm(cout=_out,cerr=_out) |
|
167 | 167 | del _out |
|
168 | 168 | |
|
169 | 169 | #**************************************************************************** |
|
170 | 170 | # Generic warning/error printer, used by everything else |
|
171 | 171 | def warn(msg,level=2,exit_val=1): |
|
172 | 172 | """Standard warning printer. Gives formatting consistency. |
|
173 | 173 | |
|
174 | 174 | Output is sent to Term.cerr (sys.stderr by default). |
|
175 | 175 | |
|
176 | 176 | Options: |
|
177 | 177 | |
|
178 | 178 | -level(2): allows finer control: |
|
179 | 179 | 0 -> Do nothing, dummy function. |
|
180 | 180 | 1 -> Print message. |
|
181 | 181 | 2 -> Print 'WARNING:' + message. (Default level). |
|
182 | 182 | 3 -> Print 'ERROR:' + message. |
|
183 | 183 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
184 | 184 | |
|
185 | 185 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
186 | 186 | warning. Ignored for all other levels.""" |
|
187 | 187 | |
|
188 | 188 | if level>0: |
|
189 | 189 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
190 | 190 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
191 | 191 | if level == 4: |
|
192 | 192 | print >> Term.cerr,'Exiting.\n' |
|
193 | 193 | sys.exit(exit_val) |
|
194 | 194 | |
|
195 | 195 | def info(msg): |
|
196 | 196 | """Equivalent to warn(msg,level=1).""" |
|
197 | 197 | |
|
198 | 198 | warn(msg,level=1) |
|
199 | 199 | |
|
200 | 200 | def error(msg): |
|
201 | 201 | """Equivalent to warn(msg,level=3).""" |
|
202 | 202 | |
|
203 | 203 | warn(msg,level=3) |
|
204 | 204 | |
|
205 | 205 | def fatal(msg,exit_val=1): |
|
206 | 206 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
207 | 207 | |
|
208 | 208 | warn(msg,exit_val=exit_val,level=4) |
|
209 | 209 | |
|
210 | 210 | |
|
211 | 211 | # useful for debugging |
|
212 | 212 | def debugp(expr): |
|
213 | 213 | """Print the value of an expression from the caller's frame. |
|
214 | 214 | |
|
215 | 215 | Takes an expression, evaluates it in the caller's frame and prints both |
|
216 | 216 | the given expression and the resulting value. The input must be of a form |
|
217 | 217 | suitable for eval().""" |
|
218 | 218 | |
|
219 | 219 | cf = sys._getframe(1) |
|
220 | 220 | print '[DBG] %s -> %r' % (expr,eval(expr,cf.f_globals,cf.f_locals)) |
|
221 | 221 | |
|
222 | 222 | #---------------------------------------------------------------------------- |
|
223 | 223 | StringTypes = types.StringTypes |
|
224 | 224 | |
|
225 | 225 | # Basic timing functionality |
|
226 | 226 | |
|
227 | 227 | # If possible (Unix), use the resource module instead of time.clock() |
|
228 | 228 | try: |
|
229 | 229 | import resource |
|
230 | 230 | def clock(): |
|
231 | 231 | """clock() -> floating point number |
|
232 | 232 | |
|
233 | 233 | Return the CPU time in seconds (user time only, system time is |
|
234 | 234 | ignored) since the start of the process. This is done via a call to |
|
235 | 235 | resource.getrusage, so it avoids the wraparound problems in |
|
236 | 236 | time.clock().""" |
|
237 | 237 | |
|
238 | 238 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
239 | 239 | |
|
240 | 240 | def clock2(): |
|
241 | 241 | """clock2() -> (t_user,t_system) |
|
242 | 242 | |
|
243 | 243 | Similar to clock(), but return a tuple of user/system times.""" |
|
244 | 244 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
245 | 245 | |
|
246 | 246 | except ImportError: |
|
247 | 247 | clock = time.clock |
|
248 | 248 | def clock2(): |
|
249 | 249 | """Under windows, system CPU time can't be measured. |
|
250 | 250 | |
|
251 | 251 | This just returns clock() and zero.""" |
|
252 | 252 | return time.clock(),0.0 |
|
253 | 253 | |
|
254 | 254 | def timings_out(reps,func,*args,**kw): |
|
255 | 255 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
256 | 256 | |
|
257 | 257 | Execute a function reps times, return a tuple with the elapsed total |
|
258 | 258 | CPU time in seconds, the time per call and the function's output. |
|
259 | 259 | |
|
260 | 260 | Under Unix, the return value is the sum of user+system time consumed by |
|
261 | 261 | the process, computed via the resource module. This prevents problems |
|
262 | 262 | related to the wraparound effect which the time.clock() function has. |
|
263 | 263 | |
|
264 | 264 | Under Windows the return value is in wall clock seconds. See the |
|
265 | 265 | documentation for the time module for more details.""" |
|
266 | 266 | |
|
267 | 267 | reps = int(reps) |
|
268 | 268 | assert reps >=1, 'reps must be >= 1' |
|
269 | 269 | if reps==1: |
|
270 | 270 | start = clock() |
|
271 | 271 | out = func(*args,**kw) |
|
272 | 272 | tot_time = clock()-start |
|
273 | 273 | else: |
|
274 | 274 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
275 | 275 | start = clock() |
|
276 | 276 | for dummy in rng: func(*args,**kw) |
|
277 | 277 | out = func(*args,**kw) # one last time |
|
278 | 278 | tot_time = clock()-start |
|
279 | 279 | av_time = tot_time / reps |
|
280 | 280 | return tot_time,av_time,out |
|
281 | 281 | |
|
282 | 282 | def timings(reps,func,*args,**kw): |
|
283 | 283 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
284 | 284 | |
|
285 | 285 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
286 | 286 | time in seconds and the time per call. These are just the first two values |
|
287 | 287 | in timings_out().""" |
|
288 | 288 | |
|
289 | 289 | return timings_out(reps,func,*args,**kw)[0:2] |
|
290 | 290 | |
|
291 | 291 | def timing(func,*args,**kw): |
|
292 | 292 | """timing(func,*args,**kw) -> t_total |
|
293 | 293 | |
|
294 | 294 | Execute a function once, return the elapsed total CPU time in |
|
295 | 295 | seconds. This is just the first value in timings_out().""" |
|
296 | 296 | |
|
297 | 297 | return timings_out(1,func,*args,**kw)[0] |
|
298 | 298 | |
|
299 | 299 | #**************************************************************************** |
|
300 | 300 | # file and system |
|
301 | 301 | |
|
302 | 302 | def system(cmd,verbose=0,debug=0,header=''): |
|
303 | 303 | """Execute a system command, return its exit status. |
|
304 | 304 | |
|
305 | 305 | Options: |
|
306 | 306 | |
|
307 | 307 | - verbose (0): print the command to be executed. |
|
308 | 308 | |
|
309 | 309 | - debug (0): only print, do not actually execute. |
|
310 | 310 | |
|
311 | 311 | - header (''): Header to print on screen prior to the executed command (it |
|
312 | 312 | is only prepended to the command, no newlines are added). |
|
313 | 313 | |
|
314 | 314 | Note: a stateful version of this function is available through the |
|
315 | 315 | SystemExec class.""" |
|
316 | 316 | |
|
317 | 317 | stat = 0 |
|
318 | 318 | if verbose or debug: print header+cmd |
|
319 | 319 | sys.stdout.flush() |
|
320 | 320 | if not debug: stat = os.system(cmd) |
|
321 | 321 | return stat |
|
322 | 322 | |
|
323 | 323 | # This function is used by ipython in a lot of places to make system calls. |
|
324 | 324 | # We need it to be slightly different under win32, due to the vagaries of |
|
325 | 325 | # 'network shares'. A win32 override is below. |
|
326 | 326 | |
|
327 | 327 | def shell(cmd,verbose=0,debug=0,header=''): |
|
328 | 328 | """Execute a command in the system shell, always return None. |
|
329 | 329 | |
|
330 | 330 | Options: |
|
331 | 331 | |
|
332 | 332 | - verbose (0): print the command to be executed. |
|
333 | 333 | |
|
334 | 334 | - debug (0): only print, do not actually execute. |
|
335 | 335 | |
|
336 | 336 | - header (''): Header to print on screen prior to the executed command (it |
|
337 | 337 | is only prepended to the command, no newlines are added). |
|
338 | 338 | |
|
339 | 339 | Note: this is similar to genutils.system(), but it returns None so it can |
|
340 | 340 | be conveniently used in interactive loops without getting the return value |
|
341 | 341 | (typically 0) printed many times.""" |
|
342 | 342 | |
|
343 | 343 | stat = 0 |
|
344 | 344 | if verbose or debug: print header+cmd |
|
345 | 345 | # flush stdout so we don't mangle python's buffering |
|
346 | 346 | sys.stdout.flush() |
|
347 | 347 | if not debug: |
|
348 | 348 | os.system(cmd) |
|
349 | 349 | |
|
350 | 350 | # override shell() for win32 to deal with network shares |
|
351 | 351 | if os.name in ('nt','dos'): |
|
352 | 352 | |
|
353 | 353 | shell_ori = shell |
|
354 | 354 | |
|
355 | 355 | def shell(cmd,verbose=0,debug=0,header=''): |
|
356 | 356 | if os.getcwd().startswith(r"\\"): |
|
357 | 357 | path = os.getcwd() |
|
358 | 358 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
359 | 359 | # as cmd.exe cannot handle UNC addresses) |
|
360 | 360 | os.chdir("c:") |
|
361 | 361 | # issue pushd to the UNC-share and then run the command |
|
362 | 362 | try: |
|
363 | 363 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
364 | 364 | finally: |
|
365 | 365 | os.chdir(path) |
|
366 | 366 | else: |
|
367 | 367 | shell_ori(cmd,verbose,debug,header) |
|
368 | 368 | |
|
369 | 369 | shell.__doc__ = shell_ori.__doc__ |
|
370 | 370 | |
|
371 | 371 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
372 | 372 | """Dummy substitute for perl's backquotes. |
|
373 | 373 | |
|
374 | 374 | Executes a command and returns the output. |
|
375 | 375 | |
|
376 | 376 | Accepts the same arguments as system(), plus: |
|
377 | 377 | |
|
378 | 378 | - split(0): if true, the output is returned as a list split on newlines. |
|
379 | 379 | |
|
380 | 380 | Note: a stateful version of this function is available through the |
|
381 | 381 | SystemExec class.""" |
|
382 | 382 | |
|
383 | 383 | if verbose or debug: print header+cmd |
|
384 | 384 | if not debug: |
|
385 | 385 | output = commands.getoutput(cmd) |
|
386 | 386 | if split: |
|
387 | 387 | return output.split('\n') |
|
388 | 388 | else: |
|
389 | 389 | return output |
|
390 | 390 | |
|
391 | 391 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
392 | 392 | """Return (standard output,standard error) of executing cmd in a shell. |
|
393 | 393 | |
|
394 | 394 | Accepts the same arguments as system(), plus: |
|
395 | 395 | |
|
396 | 396 | - split(0): if true, each of stdout/err is returned as a list split on |
|
397 | 397 | newlines. |
|
398 | 398 | |
|
399 | 399 | Note: a stateful version of this function is available through the |
|
400 | 400 | SystemExec class.""" |
|
401 | 401 | |
|
402 | 402 | if verbose or debug: print header+cmd |
|
403 | 403 | if not cmd: |
|
404 | 404 | if split: |
|
405 | 405 | return [],[] |
|
406 | 406 | else: |
|
407 | 407 | return '','' |
|
408 | 408 | if not debug: |
|
409 | 409 | pin,pout,perr = os.popen3(cmd) |
|
410 | 410 | tout = pout.read().rstrip() |
|
411 | 411 | terr = perr.read().rstrip() |
|
412 | 412 | pin.close() |
|
413 | 413 | pout.close() |
|
414 | 414 | perr.close() |
|
415 | 415 | if split: |
|
416 | 416 | return tout.split('\n'),terr.split('\n') |
|
417 | 417 | else: |
|
418 | 418 | return tout,terr |
|
419 | 419 | |
|
420 | 420 | # for compatibility with older naming conventions |
|
421 | 421 | xsys = system |
|
422 | 422 | bq = getoutput |
|
423 | 423 | |
|
424 | 424 | class SystemExec: |
|
425 | 425 | """Access the system and getoutput functions through a stateful interface. |
|
426 | 426 | |
|
427 | 427 | Note: here we refer to the system and getoutput functions from this |
|
428 | 428 | library, not the ones from the standard python library. |
|
429 | 429 | |
|
430 | 430 | This class offers the system and getoutput functions as methods, but the |
|
431 | 431 | verbose, debug and header parameters can be set for the instance (at |
|
432 | 432 | creation time or later) so that they don't need to be specified on each |
|
433 | 433 | call. |
|
434 | 434 | |
|
435 | 435 | For efficiency reasons, there's no way to override the parameters on a |
|
436 | 436 | per-call basis other than by setting instance attributes. If you need |
|
437 | 437 | local overrides, it's best to directly call system() or getoutput(). |
|
438 | 438 | |
|
439 | 439 | The following names are provided as alternate options: |
|
440 | 440 | - xsys: alias to system |
|
441 | 441 | - bq: alias to getoutput |
|
442 | 442 | |
|
443 | 443 | An instance can then be created as: |
|
444 | 444 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
445 | 445 | |
|
446 | 446 | And used as: |
|
447 | 447 | >>> sysexec.xsys('pwd') |
|
448 | 448 | >>> dirlist = sysexec.bq('ls -l') |
|
449 | 449 | """ |
|
450 | 450 | |
|
451 | 451 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
452 | 452 | """Specify the instance's values for verbose, debug and header.""" |
|
453 | 453 | setattr_list(self,'verbose debug header split') |
|
454 | 454 | |
|
455 | 455 | def system(self,cmd): |
|
456 | 456 | """Stateful interface to system(), with the same keyword parameters.""" |
|
457 | 457 | |
|
458 | 458 | system(cmd,self.verbose,self.debug,self.header) |
|
459 | 459 | |
|
460 | 460 | def shell(self,cmd): |
|
461 | 461 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
462 | 462 | |
|
463 | 463 | shell(cmd,self.verbose,self.debug,self.header) |
|
464 | 464 | |
|
465 | 465 | xsys = system # alias |
|
466 | 466 | |
|
467 | 467 | def getoutput(self,cmd): |
|
468 | 468 | """Stateful interface to getoutput().""" |
|
469 | 469 | |
|
470 | 470 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
471 | 471 | |
|
472 | 472 | def getoutputerror(self,cmd): |
|
473 | 473 | """Stateful interface to getoutputerror().""" |
|
474 | 474 | |
|
475 | 475 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
476 | 476 | |
|
477 | 477 | bq = getoutput # alias |
|
478 | 478 | |
|
479 | 479 | #----------------------------------------------------------------------------- |
|
480 | 480 | def mutex_opts(dict,ex_op): |
|
481 | 481 | """Check for presence of mutually exclusive keys in a dict. |
|
482 | 482 | |
|
483 | 483 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
484 | 484 | for op1,op2 in ex_op: |
|
485 | 485 | if op1 in dict and op2 in dict: |
|
486 | 486 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
487 | 487 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
488 | 488 | |
|
489 | 489 | #----------------------------------------------------------------------------- |
|
490 | 490 | def get_py_filename(name): |
|
491 | 491 | """Return a valid python filename in the current directory. |
|
492 | 492 | |
|
493 | 493 | If the given name is not a file, it adds '.py' and searches again. |
|
494 | 494 | Raises IOError with an informative message if the file isn't found.""" |
|
495 | 495 | |
|
496 | 496 | name = os.path.expanduser(name) |
|
497 | 497 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
498 | 498 | name += '.py' |
|
499 | 499 | if os.path.isfile(name): |
|
500 | 500 | return name |
|
501 | 501 | else: |
|
502 | 502 | raise IOError,'File `%s` not found.' % name |
|
503 | 503 | |
|
504 | 504 | #----------------------------------------------------------------------------- |
|
505 | 505 | def filefind(fname,alt_dirs = None): |
|
506 | 506 | """Return the given filename either in the current directory, if it |
|
507 | 507 | exists, or in a specified list of directories. |
|
508 | 508 | |
|
509 | 509 | ~ expansion is done on all file and directory names. |
|
510 | 510 | |
|
511 | 511 | Upon an unsuccessful search, raise an IOError exception.""" |
|
512 | 512 | |
|
513 | 513 | if alt_dirs is None: |
|
514 | 514 | try: |
|
515 | 515 | alt_dirs = get_home_dir() |
|
516 | 516 | except HomeDirError: |
|
517 | 517 | alt_dirs = os.getcwd() |
|
518 | 518 | search = [fname] + list_strings(alt_dirs) |
|
519 | 519 | search = map(os.path.expanduser,search) |
|
520 | 520 | #print 'search list for',fname,'list:',search # dbg |
|
521 | 521 | fname = search[0] |
|
522 | 522 | if os.path.isfile(fname): |
|
523 | 523 | return fname |
|
524 | 524 | for direc in search[1:]: |
|
525 | 525 | testname = os.path.join(direc,fname) |
|
526 | 526 | #print 'testname',testname # dbg |
|
527 | 527 | if os.path.isfile(testname): |
|
528 | 528 | return testname |
|
529 | 529 | raise IOError,'File' + `fname` + \ |
|
530 | 530 | ' not found in current or supplied directories:' + `alt_dirs` |
|
531 | 531 | |
|
532 | 532 | #---------------------------------------------------------------------------- |
|
533 | 533 | def file_read(filename): |
|
534 | 534 | """Read a file and close it. Returns the file source.""" |
|
535 | 535 | fobj=open(filename,'r'); |
|
536 | 536 | source = fobj.read(); |
|
537 | 537 | fobj.close() |
|
538 | 538 | return source |
|
539 | 539 | |
|
540 | 540 | #---------------------------------------------------------------------------- |
|
541 | 541 | def target_outdated(target,deps): |
|
542 | 542 | """Determine whether a target is out of date. |
|
543 | 543 | |
|
544 | 544 | target_outdated(target,deps) -> 1/0 |
|
545 | 545 | |
|
546 | 546 | deps: list of filenames which MUST exist. |
|
547 | 547 | target: single filename which may or may not exist. |
|
548 | 548 | |
|
549 | 549 | If target doesn't exist or is older than any file listed in deps, return |
|
550 | 550 | true, otherwise return false. |
|
551 | 551 | """ |
|
552 | 552 | try: |
|
553 | 553 | target_time = os.path.getmtime(target) |
|
554 | 554 | except os.error: |
|
555 | 555 | return 1 |
|
556 | 556 | for dep in deps: |
|
557 | 557 | dep_time = os.path.getmtime(dep) |
|
558 | 558 | if dep_time > target_time: |
|
559 | 559 | #print "For target",target,"Dep failed:",dep # dbg |
|
560 | 560 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
561 | 561 | return 1 |
|
562 | 562 | return 0 |
|
563 | 563 | |
|
564 | 564 | #----------------------------------------------------------------------------- |
|
565 | 565 | def target_update(target,deps,cmd): |
|
566 | 566 | """Update a target with a given command given a list of dependencies. |
|
567 | 567 | |
|
568 | 568 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
569 | 569 | |
|
570 | 570 | This is just a wrapper around target_outdated() which calls the given |
|
571 | 571 | command if target is outdated.""" |
|
572 | 572 | |
|
573 | 573 | if target_outdated(target,deps): |
|
574 | 574 | xsys(cmd) |
|
575 | 575 | |
|
576 | 576 | #---------------------------------------------------------------------------- |
|
577 | 577 | def unquote_ends(istr): |
|
578 | 578 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
579 | 579 | |
|
580 | 580 | if not istr: |
|
581 | 581 | return istr |
|
582 | 582 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
583 | 583 | (istr[0]=='"' and istr[-1]=='"'): |
|
584 | 584 | return istr[1:-1] |
|
585 | 585 | else: |
|
586 | 586 | return istr |
|
587 | 587 | |
|
588 | 588 | #---------------------------------------------------------------------------- |
|
589 | 589 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
590 | 590 | """ Process command-line options and arguments. |
|
591 | 591 | |
|
592 | 592 | Arguments: |
|
593 | 593 | |
|
594 | 594 | - argv: list of arguments, typically sys.argv. |
|
595 | 595 | |
|
596 | 596 | - names: list of option names. See DPyGetOpt docs for details on options |
|
597 | 597 | syntax. |
|
598 | 598 | |
|
599 | 599 | - defaults: dict of default values. |
|
600 | 600 | |
|
601 | 601 | - usage: optional usage notice to print if a wrong argument is passed. |
|
602 | 602 | |
|
603 | 603 | Return a dict of options and a list of free arguments.""" |
|
604 | 604 | |
|
605 | 605 | getopt = DPyGetOpt.DPyGetOpt() |
|
606 | 606 | getopt.setIgnoreCase(0) |
|
607 | 607 | getopt.parseConfiguration(names) |
|
608 | 608 | |
|
609 | 609 | try: |
|
610 | 610 | getopt.processArguments(argv) |
|
611 | 611 | except: |
|
612 | 612 | print usage |
|
613 | 613 | warn(`sys.exc_value`,level=4) |
|
614 | 614 | |
|
615 | 615 | defaults.update(getopt.optionValues) |
|
616 | 616 | args = getopt.freeValues |
|
617 | 617 | |
|
618 | 618 | return defaults,args |
|
619 | 619 | |
|
620 | 620 | #---------------------------------------------------------------------------- |
|
621 | 621 | def optstr2types(ostr): |
|
622 | 622 | """Convert a string of option names to a dict of type mappings. |
|
623 | 623 | |
|
624 | 624 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
625 | 625 | |
|
626 | 626 | This is used to get the types of all the options in a string formatted |
|
627 | 627 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
628 | 628 | which are strings (they need no further conversion). This function's main |
|
629 | 629 | use is to get a typemap for use with read_dict(). |
|
630 | 630 | """ |
|
631 | 631 | |
|
632 | 632 | typeconv = {None:'',int:'',float:''} |
|
633 | 633 | typemap = {'s':None,'i':int,'f':float} |
|
634 | 634 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
635 | 635 | |
|
636 | 636 | for w in ostr.split(): |
|
637 | 637 | oname,alias,otype = opt_re.match(w).groups() |
|
638 | 638 | if otype == '' or alias == '!': # simple switches are integers too |
|
639 | 639 | otype = 'i' |
|
640 | 640 | typeconv[typemap[otype]] += oname + ' ' |
|
641 | 641 | return typeconv |
|
642 | 642 | |
|
643 | 643 | #---------------------------------------------------------------------------- |
|
644 | 644 | def read_dict(filename,type_conv=None,**opt): |
|
645 | 645 | |
|
646 | 646 | """Read a dictionary of key=value pairs from an input file, optionally |
|
647 | 647 | performing conversions on the resulting values. |
|
648 | 648 | |
|
649 | 649 | read_dict(filename,type_conv,**opt) -> dict |
|
650 | 650 | |
|
651 | 651 | Only one value per line is accepted, the format should be |
|
652 | 652 | # optional comments are ignored |
|
653 | 653 | key value\n |
|
654 | 654 | |
|
655 | 655 | Args: |
|
656 | 656 | |
|
657 | 657 | - type_conv: A dictionary specifying which keys need to be converted to |
|
658 | 658 | which types. By default all keys are read as strings. This dictionary |
|
659 | 659 | should have as its keys valid conversion functions for strings |
|
660 | 660 | (int,long,float,complex, or your own). The value for each key |
|
661 | 661 | (converter) should be a whitespace separated string containing the names |
|
662 | 662 | of all the entries in the file to be converted using that function. For |
|
663 | 663 | keys to be left alone, use None as the conversion function (only needed |
|
664 | 664 | with purge=1, see below). |
|
665 | 665 | |
|
666 | 666 | - opt: dictionary with extra options as below (default in parens) |
|
667 | 667 | |
|
668 | 668 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
669 | 669 | of the dictionary to be returned. If purge is going to be used, the |
|
670 | 670 | set of keys to be left as strings also has to be explicitly specified |
|
671 | 671 | using the (non-existent) conversion function None. |
|
672 | 672 | |
|
673 | 673 | fs(None): field separator. This is the key/value separator to be used |
|
674 | 674 | when parsing the file. The None default means any whitespace [behavior |
|
675 | 675 | of string.split()]. |
|
676 | 676 | |
|
677 | 677 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
678 | 678 | |
|
679 | 679 | warn(1): warning level if requested keys are not found in file. |
|
680 | 680 | - 0: silently ignore. |
|
681 | 681 | - 1: inform but proceed. |
|
682 | 682 | - 2: raise KeyError exception. |
|
683 | 683 | |
|
684 | 684 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
685 | 685 | |
|
686 | 686 | unique([]): list of keys (or space separated string) which can't be |
|
687 | 687 | repeated. If one such key is found in the file, each new instance |
|
688 | 688 | overwrites the previous one. For keys not listed here, the behavior is |
|
689 | 689 | to make a list of all appearances. |
|
690 | 690 | |
|
691 | 691 | Example: |
|
692 | 692 | If the input file test.ini has: |
|
693 | 693 | i 3 |
|
694 | 694 | x 4.5 |
|
695 | 695 | y 5.5 |
|
696 | 696 | s hi ho |
|
697 | 697 | Then: |
|
698 | 698 | |
|
699 | 699 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
700 | 700 | >>> read_dict('test.ini') |
|
701 | 701 | {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'} |
|
702 | 702 | >>> read_dict('test.ini',type_conv) |
|
703 | 703 | {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'} |
|
704 | 704 | >>> read_dict('test.ini',type_conv,purge=1) |
|
705 | 705 | {'i': 3, 's': 'hi ho', 'x': 4.5} |
|
706 | 706 | """ |
|
707 | 707 | |
|
708 | 708 | # starting config |
|
709 | 709 | opt.setdefault('purge',0) |
|
710 | 710 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
711 | 711 | opt.setdefault('strip',0) |
|
712 | 712 | opt.setdefault('warn',1) |
|
713 | 713 | opt.setdefault('no_empty',0) |
|
714 | 714 | opt.setdefault('unique','') |
|
715 | 715 | if type(opt['unique']) in StringTypes: |
|
716 | 716 | unique_keys = qw(opt['unique']) |
|
717 | 717 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
718 | 718 | unique_keys = opt['unique'] |
|
719 | 719 | else: |
|
720 | 720 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
721 | 721 | |
|
722 | 722 | dict = {} |
|
723 | 723 | # first read in table of values as strings |
|
724 | 724 | file = open(filename,'r') |
|
725 | 725 | for line in file.readlines(): |
|
726 | 726 | line = line.strip() |
|
727 | 727 | if len(line) and line[0]=='#': continue |
|
728 | 728 | if len(line)>0: |
|
729 | 729 | lsplit = line.split(opt['fs'],1) |
|
730 | 730 | try: |
|
731 | 731 | key,val = lsplit |
|
732 | 732 | except ValueError: |
|
733 | 733 | key,val = lsplit[0],'' |
|
734 | 734 | key = key.strip() |
|
735 | 735 | if opt['strip']: val = val.strip() |
|
736 | 736 | if val == "''" or val == '""': val = '' |
|
737 | 737 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
738 | 738 | continue |
|
739 | 739 | # if a key is found more than once in the file, build a list |
|
740 | 740 | # unless it's in the 'unique' list. In that case, last found in file |
|
741 | 741 | # takes precedence. User beware. |
|
742 | 742 | try: |
|
743 | 743 | if dict[key] and key in unique_keys: |
|
744 | 744 | dict[key] = val |
|
745 | 745 | elif type(dict[key]) is types.ListType: |
|
746 | 746 | dict[key].append(val) |
|
747 | 747 | else: |
|
748 | 748 | dict[key] = [dict[key],val] |
|
749 | 749 | except KeyError: |
|
750 | 750 | dict[key] = val |
|
751 | 751 | # purge if requested |
|
752 | 752 | if opt['purge']: |
|
753 | 753 | accepted_keys = qwflat(type_conv.values()) |
|
754 | 754 | for key in dict.keys(): |
|
755 | 755 | if key in accepted_keys: continue |
|
756 | 756 | del(dict[key]) |
|
757 | 757 | # now convert if requested |
|
758 | 758 | if type_conv==None: return dict |
|
759 | 759 | conversions = type_conv.keys() |
|
760 | 760 | try: conversions.remove(None) |
|
761 | 761 | except: pass |
|
762 | 762 | for convert in conversions: |
|
763 | 763 | for val in qw(type_conv[convert]): |
|
764 | 764 | try: |
|
765 | 765 | dict[val] = convert(dict[val]) |
|
766 | 766 | except KeyError,e: |
|
767 | 767 | if opt['warn'] == 0: |
|
768 | 768 | pass |
|
769 | 769 | elif opt['warn'] == 1: |
|
770 | 770 | print >>sys.stderr, 'Warning: key',val,\ |
|
771 | 771 | 'not found in file',filename |
|
772 | 772 | elif opt['warn'] == 2: |
|
773 | 773 | raise KeyError,e |
|
774 | 774 | else: |
|
775 | 775 | raise ValueError,'Warning level must be 0,1 or 2' |
|
776 | 776 | |
|
777 | 777 | return dict |
|
778 | 778 | |
|
779 | 779 | #---------------------------------------------------------------------------- |
|
780 | 780 | def flag_calls(func): |
|
781 | 781 | """Wrap a function to detect and flag when it gets called. |
|
782 | 782 | |
|
783 | 783 | This is a decorator which takes a function and wraps it in a function with |
|
784 | 784 | a 'called' attribute. wrapper.called is initialized to False. |
|
785 | 785 | |
|
786 | 786 | The wrapper.called attribute is set to False right before each call to the |
|
787 | 787 | wrapped function, so if the call fails it remains False. After the call |
|
788 | 788 | completes, wrapper.called is set to True and the output is returned. |
|
789 | 789 | |
|
790 | 790 | Testing for truth in wrapper.called allows you to determine if a call to |
|
791 | 791 | func() was attempted and succeeded.""" |
|
792 | 792 | |
|
793 | 793 | def wrapper(*args,**kw): |
|
794 | 794 | wrapper.called = False |
|
795 | 795 | out = func(*args,**kw) |
|
796 | 796 | wrapper.called = True |
|
797 | 797 | return out |
|
798 | 798 | |
|
799 | 799 | wrapper.called = False |
|
800 | 800 | wrapper.__doc__ = func.__doc__ |
|
801 | 801 | return wrapper |
|
802 | 802 | |
|
803 | 803 | #---------------------------------------------------------------------------- |
|
804 | 804 | class HomeDirError(Error): |
|
805 | 805 | pass |
|
806 | 806 | |
|
807 | 807 | def get_home_dir(): |
|
808 | 808 | """Return the closest possible equivalent to a 'home' directory. |
|
809 | 809 | |
|
810 | 810 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
811 | 811 | |
|
812 | 812 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
813 | 813 | raised for all other OSes. """ |
|
814 | 814 | |
|
815 | 815 | isdir = os.path.isdir |
|
816 | 816 | env = os.environ |
|
817 | 817 | try: |
|
818 | 818 | homedir = env['HOME'] |
|
819 | 819 | if not isdir(homedir): |
|
820 | 820 | # in case a user stuck some string which does NOT resolve to a |
|
821 | 821 | # valid path, it's as good as if we hadn't foud it |
|
822 | 822 | raise KeyError |
|
823 | 823 | return homedir |
|
824 | 824 | except KeyError: |
|
825 | 825 | if os.name == 'posix': |
|
826 | 826 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
827 | 827 | elif os.name == 'nt': |
|
828 | 828 | # For some strange reason, win9x returns 'nt' for os.name. |
|
829 | 829 | try: |
|
830 | 830 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
831 | 831 | if not isdir(homedir): |
|
832 | 832 | homedir = os.path.join(env['USERPROFILE']) |
|
833 | 833 | if not isdir(homedir): |
|
834 | 834 | raise HomeDirError |
|
835 | 835 | return homedir |
|
836 | 836 | except: |
|
837 | 837 | try: |
|
838 | 838 | # Use the registry to get the 'My Documents' folder. |
|
839 | 839 | import _winreg as wreg |
|
840 | 840 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
841 | 841 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
842 | 842 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
843 | 843 | key.Close() |
|
844 | 844 | if not isdir(homedir): |
|
845 | 845 | e = ('Invalid "Personal" folder registry key ' |
|
846 | 846 | 'typically "My Documents".\n' |
|
847 | 847 | 'Value: %s\n' |
|
848 | 848 | 'This is not a valid directory on your system.' % |
|
849 | 849 | homedir) |
|
850 | 850 | raise HomeDirError(e) |
|
851 | 851 | return homedir |
|
852 | 852 | except HomeDirError: |
|
853 | 853 | raise |
|
854 | 854 | except: |
|
855 | 855 | return 'C:\\' |
|
856 | 856 | elif os.name == 'dos': |
|
857 | 857 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
858 | 858 | return 'C:\\' |
|
859 | 859 | else: |
|
860 | 860 | raise HomeDirError,'support for your operating system not implemented.' |
|
861 | 861 | |
|
862 | 862 | #**************************************************************************** |
|
863 | 863 | # strings and text |
|
864 | 864 | |
|
865 | 865 | class LSString(str): |
|
866 | 866 | """String derivative with a special access attributes. |
|
867 | 867 | |
|
868 | 868 | These are normal strings, but with the special attributes: |
|
869 | 869 | |
|
870 | 870 | .l (or .list) : value as list (split on newlines). |
|
871 | 871 | .n (or .nlstr): original value (the string itself). |
|
872 | 872 | .s (or .spstr): value as whitespace-separated string. |
|
873 | 873 | |
|
874 | 874 | Any values which require transformations are computed only once and |
|
875 | 875 | cached. |
|
876 | 876 | |
|
877 | 877 | Such strings are very useful to efficiently interact with the shell, which |
|
878 | 878 | typically only understands whitespace-separated options for commands.""" |
|
879 | 879 | |
|
880 | 880 | def get_list(self): |
|
881 | 881 | try: |
|
882 | 882 | return self.__list |
|
883 | 883 | except AttributeError: |
|
884 | 884 | self.__list = self.split('\n') |
|
885 | 885 | return self.__list |
|
886 | 886 | |
|
887 | 887 | l = list = property(get_list) |
|
888 | 888 | |
|
889 | 889 | def get_spstr(self): |
|
890 | 890 | try: |
|
891 | 891 | return self.__spstr |
|
892 | 892 | except AttributeError: |
|
893 | 893 | self.__spstr = self.replace('\n',' ') |
|
894 | 894 | return self.__spstr |
|
895 | 895 | |
|
896 | 896 | s = spstr = property(get_spstr) |
|
897 | 897 | |
|
898 | 898 | def get_nlstr(self): |
|
899 | 899 | return self |
|
900 | 900 | |
|
901 | 901 | n = nlstr = property(get_nlstr) |
|
902 | 902 | |
|
903 | 903 | #---------------------------------------------------------------------------- |
|
904 | 904 | class SList(list): |
|
905 | 905 | """List derivative with a special access attributes. |
|
906 | 906 | |
|
907 | 907 | These are normal lists, but with the special attributes: |
|
908 | 908 | |
|
909 | 909 | .l (or .list) : value as list (the list itself). |
|
910 | 910 | .n (or .nlstr): value as a string, joined on newlines. |
|
911 | 911 | .s (or .spstr): value as a string, joined on spaces. |
|
912 | 912 | |
|
913 | 913 | Any values which require transformations are computed only once and |
|
914 | 914 | cached.""" |
|
915 | 915 | |
|
916 | 916 | def get_list(self): |
|
917 | 917 | return self |
|
918 | 918 | |
|
919 | 919 | l = list = property(get_list) |
|
920 | 920 | |
|
921 | 921 | def get_spstr(self): |
|
922 | 922 | try: |
|
923 | 923 | return self.__spstr |
|
924 | 924 | except AttributeError: |
|
925 | 925 | self.__spstr = ' '.join(self) |
|
926 | 926 | return self.__spstr |
|
927 | 927 | |
|
928 | 928 | s = spstr = property(get_spstr) |
|
929 | 929 | |
|
930 | 930 | def get_nlstr(self): |
|
931 | 931 | try: |
|
932 | 932 | return self.__nlstr |
|
933 | 933 | except AttributeError: |
|
934 | 934 | self.__nlstr = '\n'.join(self) |
|
935 | 935 | return self.__nlstr |
|
936 | 936 | |
|
937 | 937 | n = nlstr = property(get_nlstr) |
|
938 | 938 | |
|
939 | 939 | #---------------------------------------------------------------------------- |
|
940 | 940 | # This can be replaced with an isspace() call once we drop 2.2 compatibility |
|
941 | 941 | _isspace_match = re.compile(r'^\s+$').match |
|
942 | 942 | def isspace(s): |
|
943 | 943 | return bool(_isspace_match(s)) |
|
944 | 944 | |
|
945 | 945 | #---------------------------------------------------------------------------- |
|
946 | 946 | def esc_quotes(strng): |
|
947 | 947 | """Return the input string with single and double quotes escaped out""" |
|
948 | 948 | |
|
949 | 949 | return strng.replace('"','\\"').replace("'","\\'") |
|
950 | 950 | |
|
951 | 951 | #---------------------------------------------------------------------------- |
|
952 | 952 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
953 | 953 | """Take multiple lines of input. |
|
954 | 954 | |
|
955 | 955 | A list with each line of input as a separate element is returned when a |
|
956 | 956 | termination string is entered (defaults to a single '.'). Input can also |
|
957 | 957 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
958 | 958 | |
|
959 | 959 | Lines of input which end in \\ are joined into single entries (and a |
|
960 | 960 | secondary continuation prompt is issued as long as the user terminates |
|
961 | 961 | lines with \\). This allows entering very long strings which are still |
|
962 | 962 | meant to be treated as single entities. |
|
963 | 963 | """ |
|
964 | 964 | |
|
965 | 965 | try: |
|
966 | 966 | if header: |
|
967 | 967 | header += '\n' |
|
968 | 968 | lines = [raw_input(header + ps1)] |
|
969 | 969 | except EOFError: |
|
970 | 970 | return [] |
|
971 | 971 | terminate = [terminate_str] |
|
972 | 972 | try: |
|
973 | 973 | while lines[-1:] != terminate: |
|
974 | 974 | new_line = raw_input(ps1) |
|
975 | 975 | while new_line.endswith('\\'): |
|
976 | 976 | new_line = new_line[:-1] + raw_input(ps2) |
|
977 | 977 | lines.append(new_line) |
|
978 | 978 | |
|
979 | 979 | return lines[:-1] # don't return the termination command |
|
980 | 980 | except EOFError: |
|
981 | 981 | |
|
982 | 982 | return lines |
|
983 | 983 | |
|
984 | 984 | #---------------------------------------------------------------------------- |
|
985 | 985 | def raw_input_ext(prompt='', ps2='... '): |
|
986 | 986 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
987 | 987 | |
|
988 | 988 | line = raw_input(prompt) |
|
989 | 989 | while line.endswith('\\'): |
|
990 | 990 | line = line[:-1] + raw_input(ps2) |
|
991 | 991 | return line |
|
992 | 992 | |
|
993 | 993 | #---------------------------------------------------------------------------- |
|
994 | 994 | def ask_yes_no(prompt,default=None): |
|
995 | 995 | """Asks a question and returns an integer 1/0 (y/n) answer. |
|
996 | 996 | |
|
997 | 997 | If default is given (one of 'y','n'), it is used if the user input is |
|
998 | 998 | empty. Otherwise the question is repeated until an answer is given. |
|
999 | 999 | If EOF occurs 20 times consecutively, the default answer is assumed, |
|
1000 | 1000 | or if there is no default, an exception is raised to prevent infinite |
|
1001 | 1001 | loops. |
|
1002 | 1002 | |
|
1003 | 1003 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1004 | 1004 | |
|
1005 | 1005 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1006 | 1006 | ans = None |
|
1007 | 1007 | eofs, max_eofs = 0, 20 |
|
1008 | 1008 | while ans not in answers.keys(): |
|
1009 | 1009 | try: |
|
1010 | 1010 | ans = raw_input(prompt+' ').lower() |
|
1011 | 1011 | if not ans: # response was an empty string |
|
1012 | 1012 | ans = default |
|
1013 | 1013 | eofs = 0 |
|
1014 | 1014 | except (EOFError,KeyboardInterrupt): |
|
1015 | 1015 | eofs = eofs + 1 |
|
1016 | 1016 | if eofs >= max_eofs: |
|
1017 | 1017 | if default in answers.keys(): |
|
1018 | 1018 | ans = default |
|
1019 | 1019 | else: |
|
1020 | 1020 | raise |
|
1021 | 1021 | |
|
1022 | 1022 | return answers[ans] |
|
1023 | 1023 | |
|
1024 | 1024 | #---------------------------------------------------------------------------- |
|
1025 | 1025 | def marquee(txt='',width=78,mark='*'): |
|
1026 | 1026 | """Return the input string centered in a 'marquee'.""" |
|
1027 | 1027 | if not txt: |
|
1028 | 1028 | return (mark*width)[:width] |
|
1029 | 1029 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1030 | 1030 | if nmark < 0: nmark =0 |
|
1031 | 1031 | marks = mark*nmark |
|
1032 | 1032 | return '%s %s %s' % (marks,txt,marks) |
|
1033 | 1033 | |
|
1034 | 1034 | #---------------------------------------------------------------------------- |
|
1035 | 1035 | class EvalDict: |
|
1036 | 1036 | """ |
|
1037 | 1037 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1038 | 1038 | |
|
1039 | 1039 | Usage: |
|
1040 | 1040 | >>>number = 19 |
|
1041 | 1041 | >>>text = "python" |
|
1042 | 1042 | >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1043 | 1043 | """ |
|
1044 | 1044 | |
|
1045 | 1045 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1046 | 1046 | # modified (shorter) version of: |
|
1047 | 1047 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1048 | 1048 | # Skip Montanaro (skip@pobox.com). |
|
1049 | 1049 | |
|
1050 | 1050 | def __getitem__(self, name): |
|
1051 | 1051 | frame = sys._getframe(1) |
|
1052 | 1052 | return eval(name, frame.f_globals, frame.f_locals) |
|
1053 | 1053 | |
|
1054 | 1054 | EvalString = EvalDict # for backwards compatibility |
|
1055 | 1055 | #---------------------------------------------------------------------------- |
|
1056 | 1056 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1057 | 1057 | """Similar to Perl's qw() operator, but with some more options. |
|
1058 | 1058 | |
|
1059 | 1059 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1060 | 1060 | |
|
1061 | 1061 | words can also be a list itself, and with flat=1, the output will be |
|
1062 | 1062 | recursively flattened. Examples: |
|
1063 | 1063 | |
|
1064 | 1064 | >>> qw('1 2') |
|
1065 | 1065 | ['1', '2'] |
|
1066 | 1066 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1067 | 1067 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1068 | 1068 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1069 | 1069 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """ |
|
1070 | 1070 | |
|
1071 | 1071 | if type(words) in StringTypes: |
|
1072 | 1072 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1073 | 1073 | if word and not word.isspace() ] |
|
1074 | 1074 | if flat: |
|
1075 | 1075 | return flatten(map(qw,words,[1]*len(words))) |
|
1076 | 1076 | return map(qw,words) |
|
1077 | 1077 | |
|
1078 | 1078 | #---------------------------------------------------------------------------- |
|
1079 | 1079 | def qwflat(words,sep=None,maxsplit=-1): |
|
1080 | 1080 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1081 | 1081 | return qw(words,1,sep,maxsplit) |
|
1082 | 1082 | |
|
1083 | 1083 | #---------------------------------------------------------------------------- |
|
1084 | 1084 | def qw_lol(indata): |
|
1085 | 1085 | """qw_lol('a b') -> [['a','b']], |
|
1086 | 1086 | otherwise it's just a call to qw(). |
|
1087 | 1087 | |
|
1088 | 1088 | We need this to make sure the modules_some keys *always* end up as a |
|
1089 | 1089 | list of lists.""" |
|
1090 | 1090 | |
|
1091 | 1091 | if type(indata) in StringTypes: |
|
1092 | 1092 | return [qw(indata)] |
|
1093 | 1093 | else: |
|
1094 | 1094 | return qw(indata) |
|
1095 | 1095 | |
|
1096 | 1096 | #----------------------------------------------------------------------------- |
|
1097 | 1097 | def list_strings(arg): |
|
1098 | 1098 | """Always return a list of strings, given a string or list of strings |
|
1099 | 1099 | as input.""" |
|
1100 | 1100 | |
|
1101 | 1101 | if type(arg) in StringTypes: return [arg] |
|
1102 | 1102 | else: return arg |
|
1103 | 1103 | |
|
1104 | 1104 | #---------------------------------------------------------------------------- |
|
1105 | 1105 | def grep(pat,list,case=1): |
|
1106 | 1106 | """Simple minded grep-like function. |
|
1107 | 1107 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1108 | 1108 | |
|
1109 | 1109 | It only does simple string matching, with no support for regexps. Use the |
|
1110 | 1110 | option case=0 for case-insensitive matching.""" |
|
1111 | 1111 | |
|
1112 | 1112 | # This is pretty crude. At least it should implement copying only references |
|
1113 | 1113 | # to the original data in case it's big. Now it copies the data for output. |
|
1114 | 1114 | out=[] |
|
1115 | 1115 | if case: |
|
1116 | 1116 | for term in list: |
|
1117 | 1117 | if term.find(pat)>-1: out.append(term) |
|
1118 | 1118 | else: |
|
1119 | 1119 | lpat=pat.lower() |
|
1120 | 1120 | for term in list: |
|
1121 | 1121 | if term.lower().find(lpat)>-1: out.append(term) |
|
1122 | 1122 | |
|
1123 | 1123 | if len(out): return out |
|
1124 | 1124 | else: return None |
|
1125 | 1125 | |
|
1126 | 1126 | #---------------------------------------------------------------------------- |
|
1127 | 1127 | def dgrep(pat,*opts): |
|
1128 | 1128 | """Return grep() on dir()+dir(__builtins__). |
|
1129 | 1129 | |
|
1130 | 1130 | A very common use of grep() when working interactively.""" |
|
1131 | 1131 | |
|
1132 | 1132 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1133 | 1133 | |
|
1134 | 1134 | #---------------------------------------------------------------------------- |
|
1135 | 1135 | def idgrep(pat): |
|
1136 | 1136 | """Case-insensitive dgrep()""" |
|
1137 | 1137 | |
|
1138 | 1138 | return dgrep(pat,0) |
|
1139 | 1139 | |
|
1140 | 1140 | #---------------------------------------------------------------------------- |
|
1141 | 1141 | def igrep(pat,list): |
|
1142 | 1142 | """Synonym for case-insensitive grep.""" |
|
1143 | 1143 | |
|
1144 | 1144 | return grep(pat,list,case=0) |
|
1145 | 1145 | |
|
1146 | 1146 | #---------------------------------------------------------------------------- |
|
1147 | 1147 | def indent(str,nspaces=4,ntabs=0): |
|
1148 | 1148 | """Indent a string a given number of spaces or tabstops. |
|
1149 | 1149 | |
|
1150 | 1150 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1151 | 1151 | """ |
|
1152 | 1152 | if str is None: |
|
1153 | 1153 | return |
|
1154 | 1154 | ind = '\t'*ntabs+' '*nspaces |
|
1155 | 1155 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1156 | 1156 | if outstr.endswith(os.linesep+ind): |
|
1157 | 1157 | return outstr[:-len(ind)] |
|
1158 | 1158 | else: |
|
1159 | 1159 | return outstr |
|
1160 | 1160 | |
|
1161 | 1161 | #----------------------------------------------------------------------------- |
|
1162 | 1162 | def native_line_ends(filename,backup=1): |
|
1163 | 1163 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1164 | 1164 | |
|
1165 | 1165 | If the optional backup argument is given as false, no backup of the |
|
1166 | 1166 | original file is left. """ |
|
1167 | 1167 | |
|
1168 | 1168 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1169 | 1169 | |
|
1170 | 1170 | bak_filename = filename + backup_suffixes[os.name] |
|
1171 | 1171 | |
|
1172 | 1172 | original = open(filename).read() |
|
1173 | 1173 | shutil.copy2(filename,bak_filename) |
|
1174 | 1174 | try: |
|
1175 | 1175 | new = open(filename,'wb') |
|
1176 | 1176 | new.write(os.linesep.join(original.splitlines())) |
|
1177 | 1177 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1178 | 1178 | new.close() |
|
1179 | 1179 | except: |
|
1180 | 1180 | os.rename(bak_filename,filename) |
|
1181 | 1181 | if not backup: |
|
1182 | 1182 | try: |
|
1183 | 1183 | os.remove(bak_filename) |
|
1184 | 1184 | except: |
|
1185 | 1185 | pass |
|
1186 | 1186 | |
|
1187 | 1187 | #---------------------------------------------------------------------------- |
|
1188 | 1188 | def get_pager_cmd(pager_cmd = None): |
|
1189 | 1189 | """Return a pager command. |
|
1190 | 1190 | |
|
1191 | 1191 | Makes some attempts at finding an OS-correct one.""" |
|
1192 | 1192 | |
|
1193 | 1193 | if os.name == 'posix': |
|
1194 | 1194 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1195 | 1195 | elif os.name in ['nt','dos']: |
|
1196 | 1196 | default_pager_cmd = 'type' |
|
1197 | 1197 | |
|
1198 | 1198 | if pager_cmd is None: |
|
1199 | 1199 | try: |
|
1200 | 1200 | pager_cmd = os.environ['PAGER'] |
|
1201 | 1201 | except: |
|
1202 | 1202 | pager_cmd = default_pager_cmd |
|
1203 | 1203 | return pager_cmd |
|
1204 | 1204 | |
|
1205 | 1205 | #----------------------------------------------------------------------------- |
|
1206 | 1206 | def get_pager_start(pager,start): |
|
1207 | 1207 | """Return the string for paging files with an offset. |
|
1208 | 1208 | |
|
1209 | 1209 | This is the '+N' argument which less and more (under Unix) accept. |
|
1210 | 1210 | """ |
|
1211 | 1211 | |
|
1212 | 1212 | if pager in ['less','more']: |
|
1213 | 1213 | if start: |
|
1214 | 1214 | start_string = '+' + str(start) |
|
1215 | 1215 | else: |
|
1216 | 1216 | start_string = '' |
|
1217 | 1217 | else: |
|
1218 | 1218 | start_string = '' |
|
1219 | 1219 | return start_string |
|
1220 | 1220 | |
|
1221 | 1221 | #---------------------------------------------------------------------------- |
|
1222 | 1222 | if os.name == "nt": |
|
1223 | 1223 | import msvcrt |
|
1224 | 1224 | def page_more(): |
|
1225 | 1225 | """ Smart pausing between pages |
|
1226 | 1226 | |
|
1227 | 1227 | @return: True if need print more lines, False if quit |
|
1228 | 1228 | """ |
|
1229 | 1229 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1230 | 1230 | ans = msvcrt.getch() |
|
1231 | 1231 | if ans in ("q", "Q"): |
|
1232 | 1232 | result = False |
|
1233 | 1233 | else: |
|
1234 | 1234 | result = True |
|
1235 | 1235 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1236 | 1236 | return result |
|
1237 | 1237 | else: |
|
1238 | 1238 | def page_more(): |
|
1239 | 1239 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1240 | 1240 | if ans.lower().startswith('q'): |
|
1241 | 1241 | return False |
|
1242 | 1242 | else: |
|
1243 | 1243 | return True |
|
1244 | 1244 | |
|
1245 | 1245 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1246 | 1246 | |
|
1247 | 1247 | def page_dumb(strng,start=0,screen_lines=25): |
|
1248 | 1248 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1249 | 1249 | |
|
1250 | 1250 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1251 | 1251 | mode.""" |
|
1252 | 1252 | |
|
1253 | 1253 | out_ln = strng.splitlines()[start:] |
|
1254 | 1254 | screens = chop(out_ln,screen_lines-1) |
|
1255 | 1255 | if len(screens) == 1: |
|
1256 | 1256 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1257 | 1257 | else: |
|
1258 | 1258 | last_escape = "" |
|
1259 | 1259 | for scr in screens[0:-1]: |
|
1260 | 1260 | hunk = os.linesep.join(scr) |
|
1261 | 1261 | print >>Term.cout, last_escape + hunk |
|
1262 | 1262 | if not page_more(): |
|
1263 | 1263 | return |
|
1264 | 1264 | esc_list = esc_re.findall(hunk) |
|
1265 | 1265 | if len(esc_list) > 0: |
|
1266 | 1266 | last_escape = esc_list[-1] |
|
1267 | 1267 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1268 | 1268 | |
|
1269 | 1269 | #---------------------------------------------------------------------------- |
|
1270 | 1270 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1271 | 1271 | """Print a string, piping through a pager after a certain length. |
|
1272 | 1272 | |
|
1273 | 1273 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1274 | 1274 | terminal screen (total lines minus lines you need to reserve to show other |
|
1275 | 1275 | information). |
|
1276 | 1276 | |
|
1277 | 1277 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1278 | 1278 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1279 | 1279 | printing, paging after that. That is, if you want auto-detection but need |
|
1280 | 1280 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1281 | 1281 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1282 | 1282 | |
|
1283 | 1283 | If a string won't fit in the allowed lines, it is sent through the |
|
1284 | 1284 | specified pager command. If none given, look for PAGER in the environment, |
|
1285 | 1285 | and ultimately default to less. |
|
1286 | 1286 | |
|
1287 | 1287 | If no system pager works, the string is sent through a 'dumb pager' |
|
1288 | 1288 | written in python, very simplistic. |
|
1289 | 1289 | """ |
|
1290 | 1290 | |
|
1291 | 1291 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1292 | 1292 | TERM = os.environ.get('TERM','dumb') |
|
1293 | 1293 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1294 | 1294 | print strng |
|
1295 | 1295 | return |
|
1296 | 1296 | # chop off the topmost part of the string we don't want to see |
|
1297 | 1297 | str_lines = strng.split(os.linesep)[start:] |
|
1298 | 1298 | str_toprint = os.linesep.join(str_lines) |
|
1299 | 1299 | num_newlines = len(str_lines) |
|
1300 | 1300 | len_str = len(str_toprint) |
|
1301 | 1301 | |
|
1302 | 1302 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1303 | 1303 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1304 | 1304 | # terminals. If someone later feels like refining it, it's not hard. |
|
1305 | 1305 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1306 | 1306 | |
|
1307 | 1307 | if os.name == "nt": |
|
1308 | 1308 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1309 | 1309 | else: |
|
1310 | 1310 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1311 | 1311 | |
|
1312 | 1312 | # auto-determine screen size |
|
1313 | 1313 | if screen_lines <= 0: |
|
1314 | 1314 | if TERM=='xterm': |
|
1315 | 1315 | try: |
|
1316 | 1316 | import curses |
|
1317 | 1317 | if hasattr(curses,'initscr'): |
|
1318 | 1318 | use_curses = 1 |
|
1319 | 1319 | else: |
|
1320 | 1320 | use_curses = 0 |
|
1321 | 1321 | except ImportError: |
|
1322 | 1322 | use_curses = 0 |
|
1323 | 1323 | else: |
|
1324 | 1324 | # curses causes problems on many terminals other than xterm. |
|
1325 | 1325 | use_curses = 0 |
|
1326 | 1326 | if use_curses: |
|
1327 | 1327 | scr = curses.initscr() |
|
1328 | 1328 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1329 | 1329 | curses.endwin() |
|
1330 | 1330 | screen_lines += screen_lines_real |
|
1331 | 1331 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1332 | 1332 | #screen_cols,'columns.' # dbg |
|
1333 | 1333 | else: |
|
1334 | 1334 | screen_lines += screen_lines_def |
|
1335 | 1335 | |
|
1336 | 1336 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1337 | 1337 | if numlines <= screen_lines : |
|
1338 | 1338 | #print '*** normal print' # dbg |
|
1339 | 1339 | print >>Term.cout, str_toprint |
|
1340 | 1340 | else: |
|
1341 | 1341 | # Try to open pager and default to internal one if that fails. |
|
1342 | 1342 | # All failure modes are tagged as 'retval=1', to match the return |
|
1343 | 1343 | # value of a failed system command. If any intermediate attempt |
|
1344 | 1344 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1345 | 1345 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1346 | 1346 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1347 | 1347 | if os.name == 'nt': |
|
1348 | 1348 | if pager_cmd.startswith('type'): |
|
1349 | 1349 | # The default WinXP 'type' command is failing on complex strings. |
|
1350 | 1350 | retval = 1 |
|
1351 | 1351 | else: |
|
1352 | 1352 | tmpname = tempfile.mktemp('.txt') |
|
1353 | 1353 | tmpfile = file(tmpname,'wt') |
|
1354 | 1354 | tmpfile.write(strng) |
|
1355 | 1355 | tmpfile.close() |
|
1356 | 1356 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1357 | 1357 | if os.system(cmd): |
|
1358 | 1358 | retval = 1 |
|
1359 | 1359 | else: |
|
1360 | 1360 | retval = None |
|
1361 | 1361 | os.remove(tmpname) |
|
1362 | 1362 | else: |
|
1363 | 1363 | try: |
|
1364 | 1364 | retval = None |
|
1365 | 1365 | # if I use popen4, things hang. No idea why. |
|
1366 | 1366 | #pager,shell_out = os.popen4(pager_cmd) |
|
1367 | 1367 | pager = os.popen(pager_cmd,'w') |
|
1368 | 1368 | pager.write(strng) |
|
1369 | 1369 | pager.close() |
|
1370 | 1370 | retval = pager.close() # success returns None |
|
1371 | 1371 | except IOError,msg: # broken pipe when user quits |
|
1372 | 1372 | if msg.args == (32,'Broken pipe'): |
|
1373 | 1373 | retval = None |
|
1374 | 1374 | else: |
|
1375 | 1375 | retval = 1 |
|
1376 | 1376 | except OSError: |
|
1377 | 1377 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1378 | 1378 | retval = 1 |
|
1379 | 1379 | if retval is not None: |
|
1380 | 1380 | page_dumb(strng,screen_lines=screen_lines) |
|
1381 | 1381 | |
|
1382 | 1382 | #---------------------------------------------------------------------------- |
|
1383 | 1383 | def page_file(fname,start = 0, pager_cmd = None): |
|
1384 | 1384 | """Page a file, using an optional pager command and starting line. |
|
1385 | 1385 | """ |
|
1386 | 1386 | |
|
1387 | 1387 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1388 | 1388 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1389 | 1389 | |
|
1390 | 1390 | try: |
|
1391 | 1391 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1392 | 1392 | raise EnvironmentError |
|
1393 | 1393 | xsys(pager_cmd + ' ' + fname) |
|
1394 | 1394 | except: |
|
1395 | 1395 | try: |
|
1396 | 1396 | if start > 0: |
|
1397 | 1397 | start -= 1 |
|
1398 | 1398 | page(open(fname).read(),start) |
|
1399 | 1399 | except: |
|
1400 | 1400 | print 'Unable to show file',`fname` |
|
1401 | 1401 | |
|
1402 | 1402 | #---------------------------------------------------------------------------- |
|
1403 | 1403 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1404 | 1404 | """Print a string snipping the midsection to fit in width. |
|
1405 | 1405 | |
|
1406 | 1406 | print_full: mode control: |
|
1407 | 1407 | - 0: only snip long strings |
|
1408 | 1408 | - 1: send to page() directly. |
|
1409 | 1409 | - 2: snip long strings and ask for full length viewing with page() |
|
1410 | 1410 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1411 | 1411 | |
|
1412 | 1412 | if print_full == 1: |
|
1413 | 1413 | page(header+str) |
|
1414 | 1414 | return 0 |
|
1415 | 1415 | |
|
1416 | 1416 | print header, |
|
1417 | 1417 | if len(str) < width: |
|
1418 | 1418 | print str |
|
1419 | 1419 | snip = 0 |
|
1420 | 1420 | else: |
|
1421 | 1421 | whalf = int((width -5)/2) |
|
1422 | 1422 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1423 | 1423 | snip = 1 |
|
1424 | 1424 | if snip and print_full == 2: |
|
1425 | 1425 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1426 | 1426 | page(str) |
|
1427 | 1427 | return snip |
|
1428 | 1428 | |
|
1429 | 1429 | #**************************************************************************** |
|
1430 | 1430 | # lists, dicts and structures |
|
1431 | 1431 | |
|
1432 | 1432 | def belong(candidates,checklist): |
|
1433 | 1433 | """Check whether a list of items appear in a given list of options. |
|
1434 | 1434 | |
|
1435 | 1435 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1436 | 1436 | |
|
1437 | 1437 | return [x in checklist for x in candidates] |
|
1438 | 1438 | |
|
1439 | 1439 | #---------------------------------------------------------------------------- |
|
1440 | 1440 | def uniq_stable(elems): |
|
1441 | 1441 | """uniq_stable(elems) -> list |
|
1442 | 1442 | |
|
1443 | 1443 | Return from an iterable, a list of all the unique elements in the input, |
|
1444 | 1444 | but maintaining the order in which they first appear. |
|
1445 | 1445 | |
|
1446 | 1446 | A naive solution to this problem which just makes a dictionary with the |
|
1447 | 1447 | elements as keys fails to respect the stability condition, since |
|
1448 | 1448 | dictionaries are unsorted by nature. |
|
1449 | 1449 | |
|
1450 | 1450 | Note: All elements in the input must be valid dictionary keys for this |
|
1451 | 1451 | routine to work, as it internally uses a dictionary for efficiency |
|
1452 | 1452 | reasons.""" |
|
1453 | 1453 | |
|
1454 | 1454 | unique = [] |
|
1455 | 1455 | unique_dict = {} |
|
1456 | 1456 | for nn in elems: |
|
1457 | 1457 | if nn not in unique_dict: |
|
1458 | 1458 | unique.append(nn) |
|
1459 | 1459 | unique_dict[nn] = None |
|
1460 | 1460 | return unique |
|
1461 | 1461 | |
|
1462 | 1462 | #---------------------------------------------------------------------------- |
|
1463 | 1463 | class NLprinter: |
|
1464 | 1464 | """Print an arbitrarily nested list, indicating index numbers. |
|
1465 | 1465 | |
|
1466 | 1466 | An instance of this class called nlprint is available and callable as a |
|
1467 | 1467 | function. |
|
1468 | 1468 | |
|
1469 | 1469 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1470 | 1470 | and using 'sep' to separate the index from the value. """ |
|
1471 | 1471 | |
|
1472 | 1472 | def __init__(self): |
|
1473 | 1473 | self.depth = 0 |
|
1474 | 1474 | |
|
1475 | 1475 | def __call__(self,lst,pos='',**kw): |
|
1476 | 1476 | """Prints the nested list numbering levels.""" |
|
1477 | 1477 | kw.setdefault('indent',' ') |
|
1478 | 1478 | kw.setdefault('sep',': ') |
|
1479 | 1479 | kw.setdefault('start',0) |
|
1480 | 1480 | kw.setdefault('stop',len(lst)) |
|
1481 | 1481 | # we need to remove start and stop from kw so they don't propagate |
|
1482 | 1482 | # into a recursive call for a nested list. |
|
1483 | 1483 | start = kw['start']; del kw['start'] |
|
1484 | 1484 | stop = kw['stop']; del kw['stop'] |
|
1485 | 1485 | if self.depth == 0 and 'header' in kw.keys(): |
|
1486 | 1486 | print kw['header'] |
|
1487 | 1487 | |
|
1488 | 1488 | for idx in range(start,stop): |
|
1489 | 1489 | elem = lst[idx] |
|
1490 | 1490 | if type(elem)==type([]): |
|
1491 | 1491 | self.depth += 1 |
|
1492 | 1492 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1493 | 1493 | self.depth -= 1 |
|
1494 | 1494 | else: |
|
1495 | 1495 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1496 | 1496 | |
|
1497 | 1497 | nlprint = NLprinter() |
|
1498 | 1498 | #---------------------------------------------------------------------------- |
|
1499 | 1499 | def all_belong(candidates,checklist): |
|
1500 | 1500 | """Check whether a list of items ALL appear in a given list of options. |
|
1501 | 1501 | |
|
1502 | 1502 | Returns a single 1 or 0 value.""" |
|
1503 | 1503 | |
|
1504 | 1504 | return 1-(0 in [x in checklist for x in candidates]) |
|
1505 | 1505 | |
|
1506 | 1506 | #---------------------------------------------------------------------------- |
|
1507 | 1507 | def sort_compare(lst1,lst2,inplace = 1): |
|
1508 | 1508 | """Sort and compare two lists. |
|
1509 | 1509 | |
|
1510 | 1510 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1511 | 1511 | to avoid that (at the cost of temporary copy creation).""" |
|
1512 | 1512 | if not inplace: |
|
1513 | 1513 | lst1 = lst1[:] |
|
1514 | 1514 | lst2 = lst2[:] |
|
1515 | 1515 | lst1.sort(); lst2.sort() |
|
1516 | 1516 | return lst1 == lst2 |
|
1517 | 1517 | |
|
1518 | 1518 | #---------------------------------------------------------------------------- |
|
1519 | 1519 | def mkdict(**kwargs): |
|
1520 | 1520 | """Return a dict from a keyword list. |
|
1521 | 1521 | |
|
1522 | 1522 | It's just syntactic sugar for making ditcionary creation more convenient: |
|
1523 | 1523 | # the standard way |
|
1524 | 1524 | >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 } |
|
1525 | 1525 | # a cleaner way |
|
1526 | 1526 | >>>data = dict(red=1, green=2, blue=3) |
|
1527 | 1527 | |
|
1528 | 1528 | If you need more than this, look at the Struct() class.""" |
|
1529 | 1529 | |
|
1530 | 1530 | return kwargs |
|
1531 | 1531 | |
|
1532 | 1532 | #---------------------------------------------------------------------------- |
|
1533 | 1533 | def list2dict(lst): |
|
1534 | 1534 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1535 | 1535 | |
|
1536 | 1536 | dic = {} |
|
1537 | 1537 | for k,v in lst: dic[k] = v |
|
1538 | 1538 | return dic |
|
1539 | 1539 | |
|
1540 | 1540 | #---------------------------------------------------------------------------- |
|
1541 | 1541 | def list2dict2(lst,default=''): |
|
1542 | 1542 | """Takes a list and turns it into a dict. |
|
1543 | 1543 | Much slower than list2dict, but more versatile. This version can take |
|
1544 | 1544 | lists with sublists of arbitrary length (including sclars).""" |
|
1545 | 1545 | |
|
1546 | 1546 | dic = {} |
|
1547 | 1547 | for elem in lst: |
|
1548 | 1548 | if type(elem) in (types.ListType,types.TupleType): |
|
1549 | 1549 | size = len(elem) |
|
1550 | 1550 | if size == 0: |
|
1551 | 1551 | pass |
|
1552 | 1552 | elif size == 1: |
|
1553 | 1553 | dic[elem] = default |
|
1554 | 1554 | else: |
|
1555 | 1555 | k,v = elem[0], elem[1:] |
|
1556 | 1556 | if len(v) == 1: v = v[0] |
|
1557 | 1557 | dic[k] = v |
|
1558 | 1558 | else: |
|
1559 | 1559 | dic[elem] = default |
|
1560 | 1560 | return dic |
|
1561 | 1561 | |
|
1562 | 1562 | #---------------------------------------------------------------------------- |
|
1563 | 1563 | def flatten(seq): |
|
1564 | 1564 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1565 | 1565 | |
|
1566 | 1566 | # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in). |
|
1567 | 1567 | |
|
1568 | 1568 | # if the x=0 isn't made, a *global* variable x is left over after calling |
|
1569 | 1569 | # this function, with the value of the last element in the return |
|
1570 | 1570 | # list. This does seem like a bug big time to me. |
|
1571 | 1571 | |
|
1572 | 1572 | # the problem is fixed with the x=0, which seems to force the creation of |
|
1573 | 1573 | # a local name |
|
1574 | 1574 | |
|
1575 | 1575 | x = 0 |
|
1576 | 1576 | return [x for subseq in seq for x in subseq] |
|
1577 | 1577 | |
|
1578 | 1578 | #---------------------------------------------------------------------------- |
|
1579 | 1579 | def get_slice(seq,start=0,stop=None,step=1): |
|
1580 | 1580 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1581 | 1581 | if stop == None: |
|
1582 | 1582 | stop = len(seq) |
|
1583 | 1583 | item = lambda i: seq[i] |
|
1584 | 1584 | return map(item,xrange(start,stop,step)) |
|
1585 | 1585 | |
|
1586 | 1586 | #---------------------------------------------------------------------------- |
|
1587 | 1587 | def chop(seq,size): |
|
1588 | 1588 | """Chop a sequence into chunks of the given size.""" |
|
1589 | 1589 | chunk = lambda i: seq[i:i+size] |
|
1590 | 1590 | return map(chunk,xrange(0,len(seq),size)) |
|
1591 | 1591 | |
|
1592 | 1592 | #---------------------------------------------------------------------------- |
|
1593 | 1593 | def with(object, **args): |
|
1594 | 1594 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1595 | 1595 | |
|
1596 | 1596 | Example: |
|
1597 | 1597 | with(jim, |
|
1598 | 1598 | born = 1960, |
|
1599 | 1599 | haircolour = 'Brown', |
|
1600 | 1600 | eyecolour = 'Green') |
|
1601 | 1601 | |
|
1602 | 1602 | Credit: Greg Ewing, in |
|
1603 | 1603 | http://mail.python.org/pipermail/python-list/2001-May/040703.html""" |
|
1604 | 1604 | |
|
1605 | 1605 | object.__dict__.update(args) |
|
1606 | 1606 | |
|
1607 | 1607 | #---------------------------------------------------------------------------- |
|
1608 | 1608 | def setattr_list(obj,alist,nspace = None): |
|
1609 | 1609 | """Set a list of attributes for an object taken from a namespace. |
|
1610 | 1610 | |
|
1611 | 1611 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1612 | 1612 | alist with their values taken from nspace, which must be a dict (something |
|
1613 | 1613 | like locals() will often do) If nspace isn't given, locals() of the |
|
1614 | 1614 | *caller* is used, so in most cases you can omit it. |
|
1615 | 1615 | |
|
1616 | 1616 | Note that alist can be given as a string, which will be automatically |
|
1617 | 1617 | split into a list on whitespace. If given as a list, it must be a list of |
|
1618 | 1618 | *strings* (the variable names themselves), not of variables.""" |
|
1619 | 1619 | |
|
1620 | 1620 | # this grabs the local variables from the *previous* call frame -- that is |
|
1621 | 1621 | # the locals from the function that called setattr_list(). |
|
1622 | 1622 | # - snipped from weave.inline() |
|
1623 | 1623 | if nspace is None: |
|
1624 | 1624 | call_frame = sys._getframe().f_back |
|
1625 | 1625 | nspace = call_frame.f_locals |
|
1626 | 1626 | |
|
1627 | 1627 | if type(alist) in StringTypes: |
|
1628 | 1628 | alist = alist.split() |
|
1629 | 1629 | for attr in alist: |
|
1630 | 1630 | val = eval(attr,nspace) |
|
1631 | 1631 | setattr(obj,attr,val) |
|
1632 | 1632 | |
|
1633 | 1633 | #---------------------------------------------------------------------------- |
|
1634 | 1634 | def getattr_list(obj,alist,*args): |
|
1635 | 1635 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1636 | 1636 | |
|
1637 | 1637 | Get a list of named attributes for an object. When a default argument is |
|
1638 | 1638 | given, it is returned when the attribute doesn't exist; without it, an |
|
1639 | 1639 | exception is raised in that case. |
|
1640 | 1640 | |
|
1641 | 1641 | Note that alist can be given as a string, which will be automatically |
|
1642 | 1642 | split into a list on whitespace. If given as a list, it must be a list of |
|
1643 | 1643 | *strings* (the variable names themselves), not of variables.""" |
|
1644 | 1644 | |
|
1645 | 1645 | if type(alist) in StringTypes: |
|
1646 | 1646 | alist = alist.split() |
|
1647 | 1647 | if args: |
|
1648 | 1648 | if len(args)==1: |
|
1649 | 1649 | default = args[0] |
|
1650 | 1650 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1651 | 1651 | else: |
|
1652 | 1652 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1653 | 1653 | else: |
|
1654 | 1654 | return map(lambda attr: getattr(obj,attr),alist) |
|
1655 | 1655 | |
|
1656 | 1656 | #---------------------------------------------------------------------------- |
|
1657 | 1657 | def map_method(method,object_list,*argseq,**kw): |
|
1658 | 1658 | """map_method(method,object_list,*args,**kw) -> list |
|
1659 | 1659 | |
|
1660 | 1660 | Return a list of the results of applying the methods to the items of the |
|
1661 | 1661 | argument sequence(s). If more than one sequence is given, the method is |
|
1662 | 1662 | called with an argument list consisting of the corresponding item of each |
|
1663 | 1663 | sequence. All sequences must be of the same length. |
|
1664 | 1664 | |
|
1665 | 1665 | Keyword arguments are passed verbatim to all objects called. |
|
1666 | 1666 | |
|
1667 | 1667 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1668 | 1668 | |
|
1669 | 1669 | out_list = [] |
|
1670 | 1670 | idx = 0 |
|
1671 | 1671 | for object in object_list: |
|
1672 | 1672 | try: |
|
1673 | 1673 | handler = getattr(object, method) |
|
1674 | 1674 | except AttributeError: |
|
1675 | 1675 | out_list.append(None) |
|
1676 | 1676 | else: |
|
1677 | 1677 | if argseq: |
|
1678 | 1678 | args = map(lambda lst:lst[idx],argseq) |
|
1679 | 1679 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
1680 | 1680 | out_list.append(handler(args,**kw)) |
|
1681 | 1681 | else: |
|
1682 | 1682 | out_list.append(handler(**kw)) |
|
1683 | 1683 | idx += 1 |
|
1684 | 1684 | return out_list |
|
1685 | 1685 | |
|
1686 | 1686 | #---------------------------------------------------------------------------- |
|
1687 | 1687 | def import_fail_info(mod_name,fns=None): |
|
1688 | 1688 | """Inform load failure for a module.""" |
|
1689 | 1689 | |
|
1690 | 1690 | if fns == None: |
|
1691 | 1691 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
1692 | 1692 | else: |
|
1693 | 1693 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
1694 | 1694 | |
|
1695 | 1695 | #---------------------------------------------------------------------------- |
|
1696 | 1696 | # Proposed popitem() extension, written as a method |
|
1697 | 1697 | |
|
1698 | 1698 | class NotGiven: pass |
|
1699 | 1699 | |
|
1700 | 1700 | def popkey(dct,key,default=NotGiven): |
|
1701 | 1701 | """Return dct[key] and delete dct[key]. |
|
1702 | 1702 | |
|
1703 | 1703 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
1704 | 1704 | KeyError. """ |
|
1705 | 1705 | |
|
1706 | 1706 | try: |
|
1707 | 1707 | val = dct[key] |
|
1708 | 1708 | except KeyError: |
|
1709 | 1709 | if default is NotGiven: |
|
1710 | 1710 | raise |
|
1711 | 1711 | else: |
|
1712 | 1712 | return default |
|
1713 | 1713 | else: |
|
1714 | 1714 | del dct[key] |
|
1715 | 1715 | return val |
|
1716 | 1716 | #*************************** end of file <genutils.py> ********************** |
|
1717 | 1717 |
@@ -1,2153 +1,2163 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 99 |
|
|
9 | $Id: iplib.py 994 2006-01-08 08:29:44Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 |
# Copyright (C) 2001-200 |
|
|
14 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, all of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. At this point, there are no dependencies at all on the code |
|
23 | 23 | # module anymore (it is not even imported). The Python License (sec. 2) |
|
24 | 24 | # allows for this, but it's always nice to acknowledge credit where credit is |
|
25 | 25 | # due. |
|
26 | 26 | #***************************************************************************** |
|
27 | 27 | |
|
28 | 28 | #**************************************************************************** |
|
29 | 29 | # Modules and globals |
|
30 | 30 | |
|
31 | 31 | from __future__ import generators # for 2.2 backwards-compatibility |
|
32 | 32 | |
|
33 | 33 | from IPython import Release |
|
34 | 34 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
35 | 35 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
36 | 36 | __license__ = Release.license |
|
37 | 37 | __version__ = Release.version |
|
38 | 38 | |
|
39 | 39 | # Python standard modules |
|
40 | 40 | import __main__ |
|
41 | 41 | import __builtin__ |
|
42 | 42 | import StringIO |
|
43 | 43 | import bdb |
|
44 | 44 | import cPickle as pickle |
|
45 | 45 | import codeop |
|
46 | 46 | import exceptions |
|
47 | 47 | import glob |
|
48 | 48 | import inspect |
|
49 | 49 | import keyword |
|
50 | 50 | import new |
|
51 | 51 | import os |
|
52 | 52 | import pdb |
|
53 | 53 | import pydoc |
|
54 | 54 | import re |
|
55 | 55 | import shutil |
|
56 | 56 | import string |
|
57 | 57 | import sys |
|
58 | 58 | import tempfile |
|
59 | 59 | import traceback |
|
60 | 60 | import types |
|
61 | 61 | |
|
62 | 62 | from pprint import pprint, pformat |
|
63 | 63 | |
|
64 | 64 | # IPython's own modules |
|
65 | 65 | import IPython |
|
66 | 66 | from IPython import OInspect,PyColorize,ultraTB |
|
67 | 67 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names |
|
68 | 68 | from IPython.FakeModule import FakeModule |
|
69 | 69 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
70 | 70 | from IPython.Logger import Logger |
|
71 | 71 | from IPython.Magic import Magic |
|
72 | 72 | from IPython.Prompts import CachedOutput |
|
73 | 73 | from IPython.Struct import Struct |
|
74 | 74 | from IPython.background_jobs import BackgroundJobManager |
|
75 | 75 | from IPython.usage import cmd_line_usage,interactive_usage |
|
76 | 76 | from IPython.genutils import * |
|
77 | 77 | |
|
78 | # Globals | |
|
79 | ||
|
78 | 80 | # store the builtin raw_input globally, and use this always, in case user code |
|
79 | 81 | # overwrites it (like wx.py.PyShell does) |
|
80 | 82 | raw_input_original = raw_input |
|
81 | 83 | |
|
82 | 84 | # compiled regexps for autoindent management |
|
83 | 85 | ini_spaces_re = re.compile(r'^(\s+)') |
|
84 | 86 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
85 | 87 | |
|
88 | ||
|
86 | 89 | #**************************************************************************** |
|
87 | 90 | # Some utility function definitions |
|
88 | 91 | |
|
89 | 92 | def softspace(file, newvalue): |
|
90 | 93 | """Copied from code.py, to remove the dependency""" |
|
91 | 94 | oldvalue = 0 |
|
92 | 95 | try: |
|
93 | 96 | oldvalue = file.softspace |
|
94 | 97 | except AttributeError: |
|
95 | 98 | pass |
|
96 | 99 | try: |
|
97 | 100 | file.softspace = newvalue |
|
98 | 101 | except (AttributeError, TypeError): |
|
99 | 102 | # "attribute-less object" or "read-only attributes" |
|
100 | 103 | pass |
|
101 | 104 | return oldvalue |
|
102 | 105 | |
|
103 | #**************************************************************************** | |
|
104 | ||
|
105 | 106 | |
|
106 | 107 | #**************************************************************************** |
|
107 | 108 | # Local use exceptions |
|
108 | 109 | class SpaceInInput(exceptions.Exception): pass |
|
109 | 110 | |
|
111 | ||
|
110 | 112 | #**************************************************************************** |
|
111 | 113 | # Local use classes |
|
112 | 114 | class Bunch: pass |
|
113 | 115 | |
|
114 | 116 | class Undefined: pass |
|
115 | 117 | |
|
116 | 118 | class InputList(list): |
|
117 | 119 | """Class to store user input. |
|
118 | 120 | |
|
119 | 121 | It's basically a list, but slices return a string instead of a list, thus |
|
120 | 122 | allowing things like (assuming 'In' is an instance): |
|
121 | 123 | |
|
122 | 124 | exec In[4:7] |
|
123 | 125 | |
|
124 | 126 | or |
|
125 | 127 | |
|
126 | 128 | exec In[5:9] + In[14] + In[21:25]""" |
|
127 | 129 | |
|
128 | 130 | def __getslice__(self,i,j): |
|
129 | 131 | return ''.join(list.__getslice__(self,i,j)) |
|
130 | 132 | |
|
131 | 133 | class SyntaxTB(ultraTB.ListTB): |
|
132 | 134 | """Extension which holds some state: the last exception value""" |
|
133 | 135 | |
|
134 | 136 | def __init__(self,color_scheme = 'NoColor'): |
|
135 | 137 | ultraTB.ListTB.__init__(self,color_scheme) |
|
136 | 138 | self.last_syntax_error = None |
|
137 | 139 | |
|
138 | 140 | def __call__(self, etype, value, elist): |
|
139 | 141 | self.last_syntax_error = value |
|
140 | 142 | ultraTB.ListTB.__call__(self,etype,value,elist) |
|
141 | 143 | |
|
142 | 144 | def clear_err_state(self): |
|
143 | 145 | """Return the current error state and clear it""" |
|
144 | 146 | e = self.last_syntax_error |
|
145 | 147 | self.last_syntax_error = None |
|
146 | 148 | return e |
|
147 | 149 | |
|
148 | 150 | #**************************************************************************** |
|
149 | 151 | # Main IPython class |
|
150 | 152 | |
|
151 | 153 | # FIXME: the Magic class is a mixin for now, and will unfortunately remain so |
|
152 | 154 | # until a full rewrite is made. I've cleaned all cross-class uses of |
|
153 | 155 | # attributes and methods, but too much user code out there relies on the |
|
154 | 156 | # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage. |
|
155 | 157 | # |
|
156 | 158 | # But at least now, all the pieces have been separated and we could, in |
|
157 | 159 | # principle, stop using the mixin. This will ease the transition to the |
|
158 | 160 | # chainsaw branch. |
|
159 | 161 | |
|
160 | 162 | # For reference, the following is the list of 'self.foo' uses in the Magic |
|
161 | 163 | # class as of 2005-12-28. These are names we CAN'T use in the main ipython |
|
162 | 164 | # class, to prevent clashes. |
|
163 | 165 | |
|
164 | 166 | # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind', |
|
165 | 167 | # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic', |
|
166 | 168 | # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', |
|
167 | 169 | # 'self.value'] |
|
168 | 170 | |
|
169 | 171 | class InteractiveShell(object,Magic): |
|
170 | 172 | """An enhanced console for Python.""" |
|
171 | 173 | |
|
172 | 174 | # class attribute to indicate whether the class supports threads or not. |
|
173 | 175 | # Subclasses with thread support should override this as needed. |
|
174 | 176 | isthreaded = False |
|
175 | 177 | |
|
176 | 178 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
177 | 179 | user_ns = None,user_global_ns=None,banner2='', |
|
178 | 180 | custom_exceptions=((),None),embedded=False): |
|
179 | 181 | |
|
180 | 182 | # some minimal strict typechecks. For some core data structures, I |
|
181 | 183 | # want actual basic python types, not just anything that looks like |
|
182 | 184 | # one. This is especially true for namespaces. |
|
183 | 185 | for ns in (user_ns,user_global_ns): |
|
184 | 186 | if ns is not None and type(ns) != types.DictType: |
|
185 | 187 | raise TypeError,'namespace must be a dictionary' |
|
186 | 188 | |
|
187 | 189 | # Job manager (for jobs run as background threads) |
|
188 | 190 | self.jobs = BackgroundJobManager() |
|
189 | 191 | |
|
190 | 192 | # track which builtins we add, so we can clean up later |
|
191 | 193 | self.builtins_added = {} |
|
192 | 194 | # This method will add the necessary builtins for operation, but |
|
193 | 195 | # tracking what it did via the builtins_added dict. |
|
194 | 196 | self.add_builtins() |
|
195 | 197 | |
|
196 | 198 | # Do the intuitively correct thing for quit/exit: we remove the |
|
197 | 199 | # builtins if they exist, and our own magics will deal with this |
|
198 | 200 | try: |
|
199 | 201 | del __builtin__.exit, __builtin__.quit |
|
200 | 202 | except AttributeError: |
|
201 | 203 | pass |
|
202 | 204 | |
|
203 | 205 | # Store the actual shell's name |
|
204 | 206 | self.name = name |
|
205 | 207 | |
|
206 | 208 | # We need to know whether the instance is meant for embedding, since |
|
207 | 209 | # global/local namespaces need to be handled differently in that case |
|
208 | 210 | self.embedded = embedded |
|
209 | 211 | |
|
210 | 212 | # command compiler |
|
211 | 213 | self.compile = codeop.CommandCompiler() |
|
212 | 214 | |
|
213 | 215 | # User input buffer |
|
214 | 216 | self.buffer = [] |
|
215 | 217 | |
|
216 | 218 | # Default name given in compilation of code |
|
217 | 219 | self.filename = '<ipython console>' |
|
218 | 220 | |
|
219 | 221 | # Make an empty namespace, which extension writers can rely on both |
|
220 | 222 | # existing and NEVER being used by ipython itself. This gives them a |
|
221 | 223 | # convenient location for storing additional information and state |
|
222 | 224 | # their extensions may require, without fear of collisions with other |
|
223 | 225 | # ipython names that may develop later. |
|
224 | 226 | self.meta = Bunch() |
|
225 | 227 | |
|
226 | 228 | # Create the namespace where the user will operate. user_ns is |
|
227 | 229 | # normally the only one used, and it is passed to the exec calls as |
|
228 | 230 | # the locals argument. But we do carry a user_global_ns namespace |
|
229 | 231 | # given as the exec 'globals' argument, This is useful in embedding |
|
230 | 232 | # situations where the ipython shell opens in a context where the |
|
231 | 233 | # distinction between locals and globals is meaningful. |
|
232 | 234 | |
|
233 | 235 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
234 | 236 | # level as a dict instead of a module. This is a manual fix, but I |
|
235 | 237 | # should really track down where the problem is coming from. Alex |
|
236 | 238 | # Schmolck reported this problem first. |
|
237 | 239 | |
|
238 | 240 | # A useful post by Alex Martelli on this topic: |
|
239 | 241 | # Re: inconsistent value from __builtins__ |
|
240 | 242 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
241 | 243 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
242 | 244 | # Gruppen: comp.lang.python |
|
243 | 245 | |
|
244 | 246 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
245 | 247 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
246 | 248 | # > <type 'dict'> |
|
247 | 249 | # > >>> print type(__builtins__) |
|
248 | 250 | # > <type 'module'> |
|
249 | 251 | # > Is this difference in return value intentional? |
|
250 | 252 | |
|
251 | 253 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
252 | 254 | # or a module, and it's been that way for a long time. Whether it's |
|
253 | 255 | # intentional (or sensible), I don't know. In any case, the idea is |
|
254 | 256 | # that if you need to access the built-in namespace directly, you |
|
255 | 257 | # should start with "import __builtin__" (note, no 's') which will |
|
256 | 258 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
257 | 259 | |
|
258 | 260 | if user_ns is None: |
|
259 | 261 | # Set __name__ to __main__ to better match the behavior of the |
|
260 | 262 | # normal interpreter. |
|
261 | 263 | user_ns = {'__name__' :'__main__', |
|
262 | 264 | '__builtins__' : __builtin__, |
|
263 | 265 | } |
|
264 | 266 | |
|
265 | 267 | if user_global_ns is None: |
|
266 | 268 | user_global_ns = {} |
|
267 | 269 | |
|
268 | 270 | # Assign namespaces |
|
269 | 271 | # This is the namespace where all normal user variables live |
|
270 | 272 | self.user_ns = user_ns |
|
271 | 273 | # Embedded instances require a separate namespace for globals. |
|
272 | 274 | # Normally this one is unused by non-embedded instances. |
|
273 | 275 | self.user_global_ns = user_global_ns |
|
274 | 276 | # A namespace to keep track of internal data structures to prevent |
|
275 | 277 | # them from cluttering user-visible stuff. Will be updated later |
|
276 | 278 | self.internal_ns = {} |
|
277 | 279 | |
|
278 | 280 | # Namespace of system aliases. Each entry in the alias |
|
279 | 281 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
280 | 282 | # of positional arguments of the alias. |
|
281 | 283 | self.alias_table = {} |
|
282 | 284 | |
|
283 | 285 | # A table holding all the namespaces IPython deals with, so that |
|
284 | 286 | # introspection facilities can search easily. |
|
285 | 287 | self.ns_table = {'user':user_ns, |
|
286 | 288 | 'user_global':user_global_ns, |
|
287 | 289 | 'alias':self.alias_table, |
|
288 | 290 | 'internal':self.internal_ns, |
|
289 | 291 | 'builtin':__builtin__.__dict__ |
|
290 | 292 | } |
|
291 | 293 | |
|
292 | 294 | # The user namespace MUST have a pointer to the shell itself. |
|
293 | 295 | self.user_ns[name] = self |
|
294 | 296 | |
|
295 | 297 | # We need to insert into sys.modules something that looks like a |
|
296 | 298 | # module but which accesses the IPython namespace, for shelve and |
|
297 | 299 | # pickle to work interactively. Normally they rely on getting |
|
298 | 300 | # everything out of __main__, but for embedding purposes each IPython |
|
299 | 301 | # instance has its own private namespace, so we can't go shoving |
|
300 | 302 | # everything into __main__. |
|
301 | 303 | |
|
302 | 304 | # note, however, that we should only do this for non-embedded |
|
303 | 305 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
304 | 306 | # namespace. Embedded instances, on the other hand, should not do |
|
305 | 307 | # this because they need to manage the user local/global namespaces |
|
306 | 308 | # only, but they live within a 'normal' __main__ (meaning, they |
|
307 | 309 | # shouldn't overtake the execution environment of the script they're |
|
308 | 310 | # embedded in). |
|
309 | 311 | |
|
310 | 312 | if not embedded: |
|
311 | 313 | try: |
|
312 | 314 | main_name = self.user_ns['__name__'] |
|
313 | 315 | except KeyError: |
|
314 | 316 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
315 | 317 | else: |
|
316 | 318 | #print "pickle hack in place" # dbg |
|
317 | 319 | #print 'main_name:',main_name # dbg |
|
318 | 320 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
319 | 321 | |
|
320 | 322 | # List of input with multi-line handling. |
|
321 | 323 | # Fill its zero entry, user counter starts at 1 |
|
322 | 324 | self.input_hist = InputList(['\n']) |
|
323 | 325 | |
|
324 | 326 | # list of visited directories |
|
325 | 327 | try: |
|
326 | 328 | self.dir_hist = [os.getcwd()] |
|
327 | 329 | except IOError, e: |
|
328 | 330 | self.dir_hist = [] |
|
329 | 331 | |
|
330 | 332 | # dict of output history |
|
331 | 333 | self.output_hist = {} |
|
332 | 334 | |
|
333 | 335 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
334 | 336 | no_alias = {} |
|
335 | 337 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
336 | 338 | for key in keyword.kwlist + no_alias_magics: |
|
337 | 339 | no_alias[key] = 1 |
|
338 | 340 | no_alias.update(__builtin__.__dict__) |
|
339 | 341 | self.no_alias = no_alias |
|
340 | 342 | |
|
341 | 343 | # make global variables for user access to these |
|
342 | 344 | self.user_ns['_ih'] = self.input_hist |
|
343 | 345 | self.user_ns['_oh'] = self.output_hist |
|
344 | 346 | self.user_ns['_dh'] = self.dir_hist |
|
345 | 347 | |
|
346 | 348 | # user aliases to input and output histories |
|
347 | 349 | self.user_ns['In'] = self.input_hist |
|
348 | 350 | self.user_ns['Out'] = self.output_hist |
|
349 | 351 | |
|
350 | 352 | # Object variable to store code object waiting execution. This is |
|
351 | 353 | # used mainly by the multithreaded shells, but it can come in handy in |
|
352 | 354 | # other situations. No need to use a Queue here, since it's a single |
|
353 | 355 | # item which gets cleared once run. |
|
354 | 356 | self.code_to_run = None |
|
355 | 357 | |
|
356 | 358 | # escapes for automatic behavior on the command line |
|
357 | 359 | self.ESC_SHELL = '!' |
|
358 | 360 | self.ESC_HELP = '?' |
|
359 | 361 | self.ESC_MAGIC = '%' |
|
360 | 362 | self.ESC_QUOTE = ',' |
|
361 | 363 | self.ESC_QUOTE2 = ';' |
|
362 | 364 | self.ESC_PAREN = '/' |
|
363 | 365 | |
|
364 | 366 | # And their associated handlers |
|
365 | 367 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
366 | 368 | self.ESC_QUOTE : self.handle_auto, |
|
367 | 369 | self.ESC_QUOTE2 : self.handle_auto, |
|
368 | 370 | self.ESC_MAGIC : self.handle_magic, |
|
369 | 371 | self.ESC_HELP : self.handle_help, |
|
370 | 372 | self.ESC_SHELL : self.handle_shell_escape, |
|
371 | 373 | } |
|
372 | 374 | |
|
373 | 375 | # class initializations |
|
374 | 376 | Magic.__init__(self,self) |
|
375 | 377 | |
|
376 | 378 | # Python source parser/formatter for syntax highlighting |
|
377 | 379 | pyformat = PyColorize.Parser().format |
|
378 | 380 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
379 | 381 | |
|
380 | 382 | # hooks holds pointers used for user-side customizations |
|
381 | 383 | self.hooks = Struct() |
|
382 | 384 | |
|
383 | 385 | # Set all default hooks, defined in the IPython.hooks module. |
|
384 | 386 | hooks = IPython.hooks |
|
385 | 387 | for hook_name in hooks.__all__: |
|
386 | 388 | self.set_hook(hook_name,getattr(hooks,hook_name)) |
|
387 | 389 | |
|
388 | 390 | # Flag to mark unconditional exit |
|
389 | 391 | self.exit_now = False |
|
390 | 392 | |
|
391 | 393 | self.usage_min = """\ |
|
392 | 394 | An enhanced console for Python. |
|
393 | 395 | Some of its features are: |
|
394 | 396 | - Readline support if the readline library is present. |
|
395 | 397 | - Tab completion in the local namespace. |
|
396 | 398 | - Logging of input, see command-line options. |
|
397 | 399 | - System shell escape via ! , eg !ls. |
|
398 | 400 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
399 | 401 | - Keeps track of locally defined variables via %who, %whos. |
|
400 | 402 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
401 | 403 | """ |
|
402 | 404 | if usage: self.usage = usage |
|
403 | 405 | else: self.usage = self.usage_min |
|
404 | 406 | |
|
405 | 407 | # Storage |
|
406 | 408 | self.rc = rc # This will hold all configuration information |
|
407 | 409 | self.pager = 'less' |
|
408 | 410 | # temporary files used for various purposes. Deleted at exit. |
|
409 | 411 | self.tempfiles = [] |
|
410 | 412 | |
|
411 | 413 | # Keep track of readline usage (later set by init_readline) |
|
412 | 414 | self.has_readline = False |
|
413 | 415 | |
|
414 | 416 | # template for logfile headers. It gets resolved at runtime by the |
|
415 | 417 | # logstart method. |
|
416 | 418 | self.loghead_tpl = \ |
|
417 | 419 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
418 | 420 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
419 | 421 | #log# opts = %s |
|
420 | 422 | #log# args = %s |
|
421 | 423 | #log# It is safe to make manual edits below here. |
|
422 | 424 | #log#----------------------------------------------------------------------- |
|
423 | 425 | """ |
|
424 | 426 | # for pushd/popd management |
|
425 | 427 | try: |
|
426 | 428 | self.home_dir = get_home_dir() |
|
427 | 429 | except HomeDirError,msg: |
|
428 | 430 | fatal(msg) |
|
429 | 431 | |
|
430 | 432 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
431 | 433 | |
|
432 | 434 | # Functions to call the underlying shell. |
|
433 | 435 | |
|
434 | 436 | # utility to expand user variables via Itpl |
|
435 | 437 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
436 | 438 | self.user_ns)) |
|
437 | 439 | # The first is similar to os.system, but it doesn't return a value, |
|
438 | 440 | # and it allows interpolation of variables in the user's namespace. |
|
439 | 441 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
440 | 442 | header='IPython system call: ', |
|
441 | 443 | verbose=self.rc.system_verbose) |
|
442 | 444 | # These are for getoutput and getoutputerror: |
|
443 | 445 | self.getoutput = lambda cmd: \ |
|
444 | 446 | getoutput(self.var_expand(cmd), |
|
445 | 447 | header='IPython system call: ', |
|
446 | 448 | verbose=self.rc.system_verbose) |
|
447 | 449 | self.getoutputerror = lambda cmd: \ |
|
448 | 450 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), |
|
449 | 451 | self.user_ns)), |
|
450 | 452 | header='IPython system call: ', |
|
451 | 453 | verbose=self.rc.system_verbose) |
|
452 | 454 | |
|
453 | 455 | # RegExp for splitting line contents into pre-char//first |
|
454 | 456 | # word-method//rest. For clarity, each group in on one line. |
|
455 | 457 | |
|
456 | 458 | # WARNING: update the regexp if the above escapes are changed, as they |
|
457 | 459 | # are hardwired in. |
|
458 | 460 | |
|
459 | 461 | # Don't get carried away with trying to make the autocalling catch too |
|
460 | 462 | # much: it's better to be conservative rather than to trigger hidden |
|
461 | 463 | # evals() somewhere and end up causing side effects. |
|
462 | 464 | |
|
463 | 465 | self.line_split = re.compile(r'^([\s*,;/])' |
|
464 | 466 | r'([\?\w\.]+\w*\s*)' |
|
465 | 467 | r'(\(?.*$)') |
|
466 | 468 | |
|
467 | 469 | # Original re, keep around for a while in case changes break something |
|
468 | 470 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
469 | 471 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
470 | 472 | # r'(\(?.*$)') |
|
471 | 473 | |
|
472 | 474 | # RegExp to identify potential function names |
|
473 | 475 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
474 | 476 | # RegExp to exclude strings with this start from autocalling |
|
475 | 477 | self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ') |
|
476 | 478 | |
|
477 | 479 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
478 | 480 | # (experimental). For this to work, the line_split regexp would need |
|
479 | 481 | # to be modified so it wouldn't break things at '['. That line is |
|
480 | 482 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
481 | 483 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
482 | 484 | |
|
483 | 485 | # keep track of where we started running (mainly for crash post-mortem) |
|
484 | 486 | self.starting_dir = os.getcwd() |
|
485 | 487 | |
|
486 | 488 | # Various switches which can be set |
|
487 | 489 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
488 | 490 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
489 | 491 | self.banner2 = banner2 |
|
490 | 492 | |
|
491 | 493 | # TraceBack handlers: |
|
492 | 494 | |
|
493 | 495 | # Syntax error handler. |
|
494 | 496 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
495 | 497 | |
|
496 | 498 | # The interactive one is initialized with an offset, meaning we always |
|
497 | 499 | # want to remove the topmost item in the traceback, which is our own |
|
498 | 500 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
499 | 501 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
500 | 502 | color_scheme='NoColor', |
|
501 | 503 | tb_offset = 1) |
|
502 | 504 | |
|
503 | 505 | # IPython itself shouldn't crash. This will produce a detailed |
|
504 | 506 | # post-mortem if it does. But we only install the crash handler for |
|
505 | 507 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
506 | 508 | # and lose the crash handler. This is because exceptions in the main |
|
507 | 509 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
508 | 510 | # and there's no point in printing crash dumps for every user exception. |
|
509 | 511 | if self.isthreaded: |
|
510 | 512 | sys.excepthook = ultraTB.FormattedTB() |
|
511 | 513 | else: |
|
512 | 514 | from IPython import CrashHandler |
|
513 | 515 | sys.excepthook = CrashHandler.CrashHandler(self) |
|
514 | 516 | |
|
515 | 517 | # The instance will store a pointer to this, so that runtime code |
|
516 | 518 | # (such as magics) can access it. This is because during the |
|
517 | 519 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
518 | 520 | # frameworks). |
|
519 | 521 | self.sys_excepthook = sys.excepthook |
|
520 | 522 | |
|
521 | 523 | # and add any custom exception handlers the user may have specified |
|
522 | 524 | self.set_custom_exc(*custom_exceptions) |
|
523 | 525 | |
|
524 | 526 | # Object inspector |
|
525 | 527 | self.inspector = OInspect.Inspector(OInspect.InspectColors, |
|
526 | 528 | PyColorize.ANSICodeColors, |
|
527 | 529 | 'NoColor') |
|
528 | 530 | # indentation management |
|
529 | 531 | self.autoindent = False |
|
530 | 532 | self.indent_current_nsp = 0 |
|
531 | 533 | self.indent_current = '' # actual indent string |
|
532 | 534 | |
|
533 | 535 | # Make some aliases automatically |
|
534 | 536 | # Prepare list of shell aliases to auto-define |
|
535 | 537 | if os.name == 'posix': |
|
536 | 538 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
537 | 539 | 'mv mv -i','rm rm -i','cp cp -i', |
|
538 | 540 | 'cat cat','less less','clear clear', |
|
539 | 541 | # a better ls |
|
540 | 542 | 'ls ls -F', |
|
541 | 543 | # long ls |
|
542 | 544 | 'll ls -lF', |
|
543 | 545 | # color ls |
|
544 | 546 | 'lc ls -F -o --color', |
|
545 | 547 | # ls normal files only |
|
546 | 548 | 'lf ls -F -o --color %l | grep ^-', |
|
547 | 549 | # ls symbolic links |
|
548 | 550 | 'lk ls -F -o --color %l | grep ^l', |
|
549 | 551 | # directories or links to directories, |
|
550 | 552 | 'ldir ls -F -o --color %l | grep /$', |
|
551 | 553 | # things which are executable |
|
552 | 554 | 'lx ls -F -o --color %l | grep ^-..x', |
|
553 | 555 | ) |
|
554 | 556 | elif os.name in ['nt','dos']: |
|
555 | 557 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
556 | 558 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
557 | 559 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
558 | 560 | 'ren ren','cls cls','copy copy') |
|
559 | 561 | else: |
|
560 | 562 | auto_alias = () |
|
561 | 563 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) |
|
562 | 564 | # Call the actual (public) initializer |
|
563 | 565 | self.init_auto_alias() |
|
564 | 566 | # end __init__ |
|
565 | 567 | |
|
566 | 568 | def post_config_initialization(self): |
|
567 | 569 | """Post configuration init method |
|
568 | 570 | |
|
569 | 571 | This is called after the configuration files have been processed to |
|
570 | 572 | 'finalize' the initialization.""" |
|
571 | 573 | |
|
572 | 574 | rc = self.rc |
|
573 | 575 | |
|
574 | 576 | # Load readline proper |
|
575 | 577 | if rc.readline: |
|
576 | 578 | self.init_readline() |
|
577 | 579 | |
|
578 | 580 | # log system |
|
579 | 581 | self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate') |
|
580 | 582 | # local shortcut, this is used a LOT |
|
581 | 583 | self.log = self.logger.log |
|
582 | 584 | |
|
583 | 585 | # Initialize cache, set in/out prompts and printing system |
|
584 | 586 | self.outputcache = CachedOutput(self, |
|
585 | 587 | rc.cache_size, |
|
586 | 588 | rc.pprint, |
|
587 | 589 | input_sep = rc.separate_in, |
|
588 | 590 | output_sep = rc.separate_out, |
|
589 | 591 | output_sep2 = rc.separate_out2, |
|
590 | 592 | ps1 = rc.prompt_in1, |
|
591 | 593 | ps2 = rc.prompt_in2, |
|
592 | 594 | ps_out = rc.prompt_out, |
|
593 | 595 | pad_left = rc.prompts_pad_left) |
|
594 | 596 | |
|
595 | 597 | # user may have over-ridden the default print hook: |
|
596 | 598 | try: |
|
597 | 599 | self.outputcache.__class__.display = self.hooks.display |
|
598 | 600 | except AttributeError: |
|
599 | 601 | pass |
|
600 | 602 | |
|
601 | 603 | # I don't like assigning globally to sys, because it means when embedding |
|
602 | 604 | # instances, each embedded instance overrides the previous choice. But |
|
603 | 605 | # sys.displayhook seems to be called internally by exec, so I don't see a |
|
604 | 606 | # way around it. |
|
605 | 607 | sys.displayhook = self.outputcache |
|
606 | 608 | |
|
607 | 609 | # Set user colors (don't do it in the constructor above so that it |
|
608 | 610 | # doesn't crash if colors option is invalid) |
|
609 | 611 | self.magic_colors(rc.colors) |
|
610 | 612 | |
|
611 | 613 | # Set calling of pdb on exceptions |
|
612 | 614 | self.call_pdb = rc.pdb |
|
613 | 615 | |
|
614 | 616 | # Load user aliases |
|
615 | 617 | for alias in rc.alias: |
|
616 | 618 | self.magic_alias(alias) |
|
617 | 619 | |
|
618 | 620 | # dynamic data that survives through sessions |
|
619 | 621 | # XXX make the filename a config option? |
|
620 | 622 | persist_base = 'persist' |
|
621 | 623 | if rc.profile: |
|
622 | 624 | persist_base += '_%s' % rc.profile |
|
623 | 625 | self.persist_fname = os.path.join(rc.ipythondir,persist_base) |
|
624 | 626 | |
|
625 | 627 | try: |
|
626 | 628 | self.persist = pickle.load(file(self.persist_fname)) |
|
627 | 629 | except: |
|
628 | 630 | self.persist = {} |
|
629 | 631 | |
|
630 | 632 | |
|
631 | 633 | for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]: |
|
632 | 634 | try: |
|
633 | 635 | obj = pickle.loads(value) |
|
634 | 636 | except: |
|
635 | 637 | |
|
636 | 638 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key |
|
637 | 639 | print "The error was:",sys.exc_info()[0] |
|
638 | 640 | continue |
|
639 | 641 | |
|
640 | 642 | |
|
641 | 643 | self.user_ns[key] = obj |
|
642 | 644 | |
|
643 | 645 | def add_builtins(self): |
|
644 | 646 | """Store ipython references into the builtin namespace. |
|
645 | 647 | |
|
646 | 648 | Some parts of ipython operate via builtins injected here, which hold a |
|
647 | 649 | reference to IPython itself.""" |
|
648 | 650 | |
|
649 | 651 | builtins_new = dict(__IPYTHON__ = self, |
|
650 | 652 | ip_set_hook = self.set_hook, |
|
651 | 653 | jobs = self.jobs, |
|
652 | 654 | ipmagic = self.ipmagic, |
|
653 | 655 | ipalias = self.ipalias, |
|
654 | 656 | ipsystem = self.ipsystem, |
|
655 | 657 | ) |
|
656 | 658 | for biname,bival in builtins_new.items(): |
|
657 | 659 | try: |
|
658 | 660 | # store the orignal value so we can restore it |
|
659 | 661 | self.builtins_added[biname] = __builtin__.__dict__[biname] |
|
660 | 662 | except KeyError: |
|
661 | 663 | # or mark that it wasn't defined, and we'll just delete it at |
|
662 | 664 | # cleanup |
|
663 | 665 | self.builtins_added[biname] = Undefined |
|
664 | 666 | __builtin__.__dict__[biname] = bival |
|
665 | 667 | |
|
666 | 668 | # Keep in the builtins a flag for when IPython is active. We set it |
|
667 | 669 | # with setdefault so that multiple nested IPythons don't clobber one |
|
668 | 670 | # another. Each will increase its value by one upon being activated, |
|
669 | 671 | # which also gives us a way to determine the nesting level. |
|
670 | 672 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
671 | 673 | |
|
672 | 674 | def clean_builtins(self): |
|
673 | 675 | """Remove any builtins which might have been added by add_builtins, or |
|
674 | 676 | restore overwritten ones to their previous values.""" |
|
675 | 677 | for biname,bival in self.builtins_added.items(): |
|
676 | 678 | if bival is Undefined: |
|
677 | 679 | del __builtin__.__dict__[biname] |
|
678 | 680 | else: |
|
679 | 681 | __builtin__.__dict__[biname] = bival |
|
680 | 682 | self.builtins_added.clear() |
|
681 | 683 | |
|
682 | 684 | def set_hook(self,name,hook): |
|
683 | 685 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
684 | 686 | |
|
685 | 687 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
686 | 688 | resetting one of these hooks, you can modify IPython's behavior to |
|
687 | 689 | call at runtime your own routines.""" |
|
688 | 690 | |
|
689 | 691 | # At some point in the future, this should validate the hook before it |
|
690 | 692 | # accepts it. Probably at least check that the hook takes the number |
|
691 | 693 | # of args it's supposed to. |
|
692 | 694 | setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
693 | 695 | |
|
694 | 696 | def set_custom_exc(self,exc_tuple,handler): |
|
695 | 697 | """set_custom_exc(exc_tuple,handler) |
|
696 | 698 | |
|
697 | 699 | Set a custom exception handler, which will be called if any of the |
|
698 | 700 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
699 | 701 | runcode() method. |
|
700 | 702 | |
|
701 | 703 | Inputs: |
|
702 | 704 | |
|
703 | 705 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
704 | 706 | handler for. It is very important that you use a tuple, and NOT A |
|
705 | 707 | LIST here, because of the way Python's except statement works. If |
|
706 | 708 | you only want to trap a single exception, use a singleton tuple: |
|
707 | 709 | |
|
708 | 710 | exc_tuple == (MyCustomException,) |
|
709 | 711 | |
|
710 | 712 | - handler: this must be defined as a function with the following |
|
711 | 713 | basic interface: def my_handler(self,etype,value,tb). |
|
712 | 714 | |
|
713 | 715 | This will be made into an instance method (via new.instancemethod) |
|
714 | 716 | of IPython itself, and it will be called if any of the exceptions |
|
715 | 717 | listed in the exc_tuple are caught. If the handler is None, an |
|
716 | 718 | internal basic one is used, which just prints basic info. |
|
717 | 719 | |
|
718 | 720 | WARNING: by putting in your own exception handler into IPython's main |
|
719 | 721 | execution loop, you run a very good chance of nasty crashes. This |
|
720 | 722 | facility should only be used if you really know what you are doing.""" |
|
721 | 723 | |
|
722 | 724 | assert type(exc_tuple)==type(()) , \ |
|
723 | 725 | "The custom exceptions must be given AS A TUPLE." |
|
724 | 726 | |
|
725 | 727 | def dummy_handler(self,etype,value,tb): |
|
726 | 728 | print '*** Simple custom exception handler ***' |
|
727 | 729 | print 'Exception type :',etype |
|
728 | 730 | print 'Exception value:',value |
|
729 | 731 | print 'Traceback :',tb |
|
730 | 732 | print 'Source code :','\n'.join(self.buffer) |
|
731 | 733 | |
|
732 | 734 | if handler is None: handler = dummy_handler |
|
733 | 735 | |
|
734 | 736 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
735 | 737 | self.custom_exceptions = exc_tuple |
|
736 | 738 | |
|
737 | 739 | def set_custom_completer(self,completer,pos=0): |
|
738 | 740 | """set_custom_completer(completer,pos=0) |
|
739 | 741 | |
|
740 | 742 | Adds a new custom completer function. |
|
741 | 743 | |
|
742 | 744 | The position argument (defaults to 0) is the index in the completers |
|
743 | 745 | list where you want the completer to be inserted.""" |
|
744 | 746 | |
|
745 | 747 | newcomp = new.instancemethod(completer,self.Completer, |
|
746 | 748 | self.Completer.__class__) |
|
747 | 749 | self.Completer.matchers.insert(pos,newcomp) |
|
748 | 750 | |
|
749 | 751 | def _get_call_pdb(self): |
|
750 | 752 | return self._call_pdb |
|
751 | 753 | |
|
752 | 754 | def _set_call_pdb(self,val): |
|
753 | 755 | |
|
754 | 756 | if val not in (0,1,False,True): |
|
755 | 757 | raise ValueError,'new call_pdb value must be boolean' |
|
756 | 758 | |
|
757 | 759 | # store value in instance |
|
758 | 760 | self._call_pdb = val |
|
759 | 761 | |
|
760 | 762 | # notify the actual exception handlers |
|
761 | 763 | self.InteractiveTB.call_pdb = val |
|
762 | 764 | if self.isthreaded: |
|
763 | 765 | try: |
|
764 | 766 | self.sys_excepthook.call_pdb = val |
|
765 | 767 | except: |
|
766 | 768 | warn('Failed to activate pdb for threaded exception handler') |
|
767 | 769 | |
|
768 | 770 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
769 | 771 | 'Control auto-activation of pdb at exceptions') |
|
770 | 772 | |
|
771 | 773 | |
|
772 | 774 | # These special functions get installed in the builtin namespace, to |
|
773 | 775 | # provide programmatic (pure python) access to magics, aliases and system |
|
774 | 776 | # calls. This is important for logging, user scripting, and more. |
|
775 | 777 | |
|
776 | 778 | # We are basically exposing, via normal python functions, the three |
|
777 | 779 | # mechanisms in which ipython offers special call modes (magics for |
|
778 | 780 | # internal control, aliases for direct system access via pre-selected |
|
779 | 781 | # names, and !cmd for calling arbitrary system commands). |
|
780 | 782 | |
|
781 | 783 | def ipmagic(self,arg_s): |
|
782 | 784 | """Call a magic function by name. |
|
783 | 785 | |
|
784 | 786 | Input: a string containing the name of the magic function to call and any |
|
785 | 787 | additional arguments to be passed to the magic. |
|
786 | 788 | |
|
787 | 789 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
788 | 790 | prompt: |
|
789 | 791 | |
|
790 | 792 | In[1]: %name -opt foo bar |
|
791 | 793 | |
|
792 | 794 | To call a magic without arguments, simply use ipmagic('name'). |
|
793 | 795 | |
|
794 | 796 | This provides a proper Python function to call IPython's magics in any |
|
795 | 797 | valid Python code you can type at the interpreter, including loops and |
|
796 | 798 | compound statements. It is added by IPython to the Python builtin |
|
797 | 799 | namespace upon initialization.""" |
|
798 | 800 | |
|
799 | 801 | args = arg_s.split(' ',1) |
|
800 | 802 | magic_name = args[0] |
|
801 | 803 | if magic_name.startswith(self.ESC_MAGIC): |
|
802 | 804 | magic_name = magic_name[1:] |
|
803 | 805 | try: |
|
804 | 806 | magic_args = args[1] |
|
805 | 807 | except IndexError: |
|
806 | 808 | magic_args = '' |
|
807 | 809 | fn = getattr(self,'magic_'+magic_name,None) |
|
808 | 810 | if fn is None: |
|
809 | 811 | error("Magic function `%s` not found." % magic_name) |
|
810 | 812 | else: |
|
811 | 813 | magic_args = self.var_expand(magic_args) |
|
812 | 814 | return fn(magic_args) |
|
813 | 815 | |
|
814 | 816 | def ipalias(self,arg_s): |
|
815 | 817 | """Call an alias by name. |
|
816 | 818 | |
|
817 | 819 | Input: a string containing the name of the alias to call and any |
|
818 | 820 | additional arguments to be passed to the magic. |
|
819 | 821 | |
|
820 | 822 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
821 | 823 | prompt: |
|
822 | 824 | |
|
823 | 825 | In[1]: name -opt foo bar |
|
824 | 826 | |
|
825 | 827 | To call an alias without arguments, simply use ipalias('name'). |
|
826 | 828 | |
|
827 | 829 | This provides a proper Python function to call IPython's aliases in any |
|
828 | 830 | valid Python code you can type at the interpreter, including loops and |
|
829 | 831 | compound statements. It is added by IPython to the Python builtin |
|
830 | 832 | namespace upon initialization.""" |
|
831 | 833 | |
|
832 | 834 | args = arg_s.split(' ',1) |
|
833 | 835 | alias_name = args[0] |
|
834 | 836 | try: |
|
835 | 837 | alias_args = args[1] |
|
836 | 838 | except IndexError: |
|
837 | 839 | alias_args = '' |
|
838 | 840 | if alias_name in self.alias_table: |
|
839 | 841 | self.call_alias(alias_name,alias_args) |
|
840 | 842 | else: |
|
841 | 843 | error("Alias `%s` not found." % alias_name) |
|
842 | 844 | |
|
843 | 845 | def ipsystem(self,arg_s): |
|
844 | 846 | """Make a system call, using IPython.""" |
|
845 | 847 | self.system(arg_s) |
|
846 | 848 | |
|
847 | 849 | def complete(self,text): |
|
848 | 850 | """Return a sorted list of all possible completions on text. |
|
849 | 851 | |
|
850 | 852 | Inputs: |
|
851 | 853 | |
|
852 | 854 | - text: a string of text to be completed on. |
|
853 | 855 | |
|
854 | 856 | This is a wrapper around the completion mechanism, similar to what |
|
855 | 857 | readline does at the command line when the TAB key is hit. By |
|
856 | 858 | exposing it as a method, it can be used by other non-readline |
|
857 | 859 | environments (such as GUIs) for text completion. |
|
858 | 860 | |
|
859 | 861 | Simple usage example: |
|
860 | 862 | |
|
861 | 863 | In [1]: x = 'hello' |
|
862 | 864 | |
|
863 | 865 | In [2]: __IP.complete('x.l') |
|
864 | 866 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
865 | 867 | |
|
866 | 868 | complete = self.Completer.complete |
|
867 | 869 | state = 0 |
|
868 | 870 | # use a dict so we get unique keys, since ipyhton's multiple |
|
869 | 871 | # completers can return duplicates. |
|
870 | 872 | comps = {} |
|
871 | 873 | while True: |
|
872 | 874 | newcomp = complete(text,state) |
|
873 | 875 | if newcomp is None: |
|
874 | 876 | break |
|
875 | 877 | comps[newcomp] = 1 |
|
876 | 878 | state += 1 |
|
877 | 879 | outcomps = comps.keys() |
|
878 | 880 | outcomps.sort() |
|
879 | 881 | return outcomps |
|
880 | 882 | |
|
881 | 883 | def set_completer_frame(self, frame): |
|
882 | 884 | if frame: |
|
883 | 885 | self.Completer.namespace = frame.f_locals |
|
884 | 886 | self.Completer.global_namespace = frame.f_globals |
|
885 | 887 | else: |
|
886 | 888 | self.Completer.namespace = self.user_ns |
|
887 | 889 | self.Completer.global_namespace = self.user_global_ns |
|
888 | 890 | |
|
889 | 891 | def init_auto_alias(self): |
|
890 | 892 | """Define some aliases automatically. |
|
891 | 893 | |
|
892 | 894 | These are ALL parameter-less aliases""" |
|
893 | 895 | for alias,cmd in self.auto_alias: |
|
894 | 896 | self.alias_table[alias] = (0,cmd) |
|
895 | 897 | |
|
896 | 898 | def alias_table_validate(self,verbose=0): |
|
897 | 899 | """Update information about the alias table. |
|
898 | 900 | |
|
899 | 901 | In particular, make sure no Python keywords/builtins are in it.""" |
|
900 | 902 | |
|
901 | 903 | no_alias = self.no_alias |
|
902 | 904 | for k in self.alias_table.keys(): |
|
903 | 905 | if k in no_alias: |
|
904 | 906 | del self.alias_table[k] |
|
905 | 907 | if verbose: |
|
906 | 908 | print ("Deleting alias <%s>, it's a Python " |
|
907 | 909 | "keyword or builtin." % k) |
|
908 | 910 | |
|
909 | 911 | def set_autoindent(self,value=None): |
|
910 | 912 | """Set the autoindent flag, checking for readline support. |
|
911 | 913 | |
|
912 | 914 | If called with no arguments, it acts as a toggle.""" |
|
913 | 915 | |
|
914 | 916 | if not self.has_readline: |
|
915 | 917 | if os.name == 'posix': |
|
916 | 918 | warn("The auto-indent feature requires the readline library") |
|
917 | 919 | self.autoindent = 0 |
|
918 | 920 | return |
|
919 | 921 | if value is None: |
|
920 | 922 | self.autoindent = not self.autoindent |
|
921 | 923 | else: |
|
922 | 924 | self.autoindent = value |
|
923 | 925 | |
|
924 | 926 | def rc_set_toggle(self,rc_field,value=None): |
|
925 | 927 | """Set or toggle a field in IPython's rc config. structure. |
|
926 | 928 | |
|
927 | 929 | If called with no arguments, it acts as a toggle. |
|
928 | 930 | |
|
929 | 931 | If called with a non-existent field, the resulting AttributeError |
|
930 | 932 | exception will propagate out.""" |
|
931 | 933 | |
|
932 | 934 | rc_val = getattr(self.rc,rc_field) |
|
933 | 935 | if value is None: |
|
934 | 936 | value = not rc_val |
|
935 | 937 | setattr(self.rc,rc_field,value) |
|
936 | 938 | |
|
937 | 939 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
938 | 940 | """Install the user configuration directory. |
|
939 | 941 | |
|
940 | 942 | Can be called when running for the first time or to upgrade the user's |
|
941 | 943 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
942 | 944 | and 'upgrade'.""" |
|
943 | 945 | |
|
944 | 946 | def wait(): |
|
945 | 947 | try: |
|
946 | 948 | raw_input("Please press <RETURN> to start IPython.") |
|
947 | 949 | except EOFError: |
|
948 | 950 | print >> Term.cout |
|
949 | 951 | print '*'*70 |
|
950 | 952 | |
|
951 | 953 | cwd = os.getcwd() # remember where we started |
|
952 | 954 | glb = glob.glob |
|
953 | 955 | print '*'*70 |
|
954 | 956 | if mode == 'install': |
|
955 | 957 | print \ |
|
956 | 958 | """Welcome to IPython. I will try to create a personal configuration directory |
|
957 | 959 | where you can customize many aspects of IPython's functionality in:\n""" |
|
958 | 960 | else: |
|
959 | 961 | print 'I am going to upgrade your configuration in:' |
|
960 | 962 | |
|
961 | 963 | print ipythondir |
|
962 | 964 | |
|
963 | 965 | rcdirend = os.path.join('IPython','UserConfig') |
|
964 | 966 | cfg = lambda d: os.path.join(d,rcdirend) |
|
965 | 967 | try: |
|
966 | 968 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
967 | 969 | except IOError: |
|
968 | 970 | warning = """ |
|
969 | 971 | Installation error. IPython's directory was not found. |
|
970 | 972 | |
|
971 | 973 | Check the following: |
|
972 | 974 | |
|
973 | 975 | The ipython/IPython directory should be in a directory belonging to your |
|
974 | 976 | PYTHONPATH environment variable (that is, it should be in a directory |
|
975 | 977 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
976 | 978 | |
|
977 | 979 | IPython will proceed with builtin defaults. |
|
978 | 980 | """ |
|
979 | 981 | warn(warning) |
|
980 | 982 | wait() |
|
981 | 983 | return |
|
982 | 984 | |
|
983 | 985 | if mode == 'install': |
|
984 | 986 | try: |
|
985 | 987 | shutil.copytree(rcdir,ipythondir) |
|
986 | 988 | os.chdir(ipythondir) |
|
987 | 989 | rc_files = glb("ipythonrc*") |
|
988 | 990 | for rc_file in rc_files: |
|
989 | 991 | os.rename(rc_file,rc_file+rc_suffix) |
|
990 | 992 | except: |
|
991 | 993 | warning = """ |
|
992 | 994 | |
|
993 | 995 | There was a problem with the installation: |
|
994 | 996 | %s |
|
995 | 997 | Try to correct it or contact the developers if you think it's a bug. |
|
996 | 998 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
997 | 999 | warn(warning) |
|
998 | 1000 | wait() |
|
999 | 1001 | return |
|
1000 | 1002 | |
|
1001 | 1003 | elif mode == 'upgrade': |
|
1002 | 1004 | try: |
|
1003 | 1005 | os.chdir(ipythondir) |
|
1004 | 1006 | except: |
|
1005 | 1007 | print """ |
|
1006 | 1008 | Can not upgrade: changing to directory %s failed. Details: |
|
1007 | 1009 | %s |
|
1008 | 1010 | """ % (ipythondir,sys.exc_info()[1]) |
|
1009 | 1011 | wait() |
|
1010 | 1012 | return |
|
1011 | 1013 | else: |
|
1012 | 1014 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1013 | 1015 | for new_full_path in sources: |
|
1014 | 1016 | new_filename = os.path.basename(new_full_path) |
|
1015 | 1017 | if new_filename.startswith('ipythonrc'): |
|
1016 | 1018 | new_filename = new_filename + rc_suffix |
|
1017 | 1019 | # The config directory should only contain files, skip any |
|
1018 | 1020 | # directories which may be there (like CVS) |
|
1019 | 1021 | if os.path.isdir(new_full_path): |
|
1020 | 1022 | continue |
|
1021 | 1023 | if os.path.exists(new_filename): |
|
1022 | 1024 | old_file = new_filename+'.old' |
|
1023 | 1025 | if os.path.exists(old_file): |
|
1024 | 1026 | os.remove(old_file) |
|
1025 | 1027 | os.rename(new_filename,old_file) |
|
1026 | 1028 | shutil.copy(new_full_path,new_filename) |
|
1027 | 1029 | else: |
|
1028 | 1030 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1029 | 1031 | |
|
1030 | 1032 | # Fix line-endings to those native to each platform in the config |
|
1031 | 1033 | # directory. |
|
1032 | 1034 | try: |
|
1033 | 1035 | os.chdir(ipythondir) |
|
1034 | 1036 | except: |
|
1035 | 1037 | print """ |
|
1036 | 1038 | Problem: changing to directory %s failed. |
|
1037 | 1039 | Details: |
|
1038 | 1040 | %s |
|
1039 | 1041 | |
|
1040 | 1042 | Some configuration files may have incorrect line endings. This should not |
|
1041 | 1043 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1042 | 1044 | wait() |
|
1043 | 1045 | else: |
|
1044 | 1046 | for fname in glb('ipythonrc*'): |
|
1045 | 1047 | try: |
|
1046 | 1048 | native_line_ends(fname,backup=0) |
|
1047 | 1049 | except IOError: |
|
1048 | 1050 | pass |
|
1049 | 1051 | |
|
1050 | 1052 | if mode == 'install': |
|
1051 | 1053 | print """ |
|
1052 | 1054 | Successful installation! |
|
1053 | 1055 | |
|
1054 | 1056 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1055 | 1057 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1056 | 1058 | distribution) to make sure that your system environment is properly configured |
|
1057 | 1059 | to take advantage of IPython's features.""" |
|
1058 | 1060 | else: |
|
1059 | 1061 | print """ |
|
1060 | 1062 | Successful upgrade! |
|
1061 | 1063 | |
|
1062 | 1064 | All files in your directory: |
|
1063 | 1065 | %(ipythondir)s |
|
1064 | 1066 | which would have been overwritten by the upgrade were backed up with a .old |
|
1065 | 1067 | extension. If you had made particular customizations in those files you may |
|
1066 | 1068 | want to merge them back into the new files.""" % locals() |
|
1067 | 1069 | wait() |
|
1068 | 1070 | os.chdir(cwd) |
|
1069 | 1071 | # end user_setup() |
|
1070 | 1072 | |
|
1071 | 1073 | def atexit_operations(self): |
|
1072 | 1074 | """This will be executed at the time of exit. |
|
1073 | 1075 | |
|
1074 | 1076 | Saving of persistent data should be performed here. """ |
|
1075 | 1077 | |
|
1076 | 1078 | # input history |
|
1077 | 1079 | self.savehist() |
|
1078 | 1080 | |
|
1079 | 1081 | # Cleanup all tempfiles left around |
|
1080 | 1082 | for tfile in self.tempfiles: |
|
1081 | 1083 | try: |
|
1082 | 1084 | os.unlink(tfile) |
|
1083 | 1085 | except OSError: |
|
1084 | 1086 | pass |
|
1085 | 1087 | |
|
1086 | 1088 | # save the "persistent data" catch-all dictionary |
|
1087 | 1089 | try: |
|
1088 | 1090 | pickle.dump(self.persist, open(self.persist_fname,"w")) |
|
1089 | 1091 | except: |
|
1090 | 1092 | print "*** ERROR *** persistent data saving failed." |
|
1091 | 1093 | |
|
1092 | 1094 | def savehist(self): |
|
1093 | 1095 | """Save input history to a file (via readline library).""" |
|
1094 | 1096 | try: |
|
1095 | 1097 | self.readline.write_history_file(self.histfile) |
|
1096 | 1098 | except: |
|
1097 | 1099 | print 'Unable to save IPython command history to file: ' + \ |
|
1098 | 1100 | `self.histfile` |
|
1099 | 1101 | |
|
1100 | 1102 | def pre_readline(self): |
|
1101 | 1103 | """readline hook to be used at the start of each line. |
|
1102 | 1104 | |
|
1103 | 1105 | Currently it handles auto-indent only.""" |
|
1104 | 1106 | |
|
1105 | 1107 | self.readline.insert_text(self.indent_current) |
|
1106 | 1108 | |
|
1107 | 1109 | def init_readline(self): |
|
1108 | 1110 | """Command history completion/saving/reloading.""" |
|
1109 | 1111 | try: |
|
1110 | 1112 | import readline |
|
1111 | 1113 | except ImportError: |
|
1112 | 1114 | self.has_readline = 0 |
|
1113 | 1115 | self.readline = None |
|
1114 | 1116 | # no point in bugging windows users with this every time: |
|
1115 | 1117 | if os.name == 'posix': |
|
1116 | 1118 | warn('Readline services not available on this platform.') |
|
1117 | 1119 | else: |
|
1118 | 1120 | import atexit |
|
1119 | 1121 | from IPython.completer import IPCompleter |
|
1120 | 1122 | self.Completer = IPCompleter(self, |
|
1121 | 1123 | self.user_ns, |
|
1122 | 1124 | self.user_global_ns, |
|
1123 | 1125 | self.rc.readline_omit__names, |
|
1124 | 1126 | self.alias_table) |
|
1125 | 1127 | |
|
1126 | 1128 | # Platform-specific configuration |
|
1127 | 1129 | if os.name == 'nt': |
|
1128 | 1130 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1129 | 1131 | else: |
|
1130 | 1132 | self.readline_startup_hook = readline.set_startup_hook |
|
1131 | 1133 | |
|
1132 | 1134 | # Load user's initrc file (readline config) |
|
1133 | 1135 | inputrc_name = os.environ.get('INPUTRC') |
|
1134 | 1136 | if inputrc_name is None: |
|
1135 | 1137 | home_dir = get_home_dir() |
|
1136 | 1138 | if home_dir is not None: |
|
1137 | 1139 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1138 | 1140 | if os.path.isfile(inputrc_name): |
|
1139 | 1141 | try: |
|
1140 | 1142 | readline.read_init_file(inputrc_name) |
|
1141 | 1143 | except: |
|
1142 | 1144 | warn('Problems reading readline initialization file <%s>' |
|
1143 | 1145 | % inputrc_name) |
|
1144 | 1146 | |
|
1145 | 1147 | self.has_readline = 1 |
|
1146 | 1148 | self.readline = readline |
|
1147 | 1149 | # save this in sys so embedded copies can restore it properly |
|
1148 | 1150 | sys.ipcompleter = self.Completer.complete |
|
1149 | 1151 | readline.set_completer(self.Completer.complete) |
|
1150 | 1152 | |
|
1151 | 1153 | # Configure readline according to user's prefs |
|
1152 | 1154 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1153 | 1155 | readline.parse_and_bind(rlcommand) |
|
1154 | 1156 | |
|
1155 | 1157 | # remove some chars from the delimiters list |
|
1156 | 1158 | delims = readline.get_completer_delims() |
|
1157 | 1159 | delims = delims.translate(string._idmap, |
|
1158 | 1160 | self.rc.readline_remove_delims) |
|
1159 | 1161 | readline.set_completer_delims(delims) |
|
1160 | 1162 | # otherwise we end up with a monster history after a while: |
|
1161 | 1163 | readline.set_history_length(1000) |
|
1162 | 1164 | try: |
|
1163 | 1165 | #print '*** Reading readline history' # dbg |
|
1164 | 1166 | readline.read_history_file(self.histfile) |
|
1165 | 1167 | except IOError: |
|
1166 | 1168 | pass # It doesn't exist yet. |
|
1167 | 1169 | |
|
1168 | 1170 | atexit.register(self.atexit_operations) |
|
1169 | 1171 | del atexit |
|
1170 | 1172 | |
|
1171 | 1173 | # Configure auto-indent for all platforms |
|
1172 | 1174 | self.set_autoindent(self.rc.autoindent) |
|
1173 | 1175 | |
|
1174 | 1176 | def _should_recompile(self,e): |
|
1175 | 1177 | """Utility routine for edit_syntax_error""" |
|
1176 | 1178 | |
|
1177 | 1179 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1178 | 1180 | '<console>'): |
|
1179 | 1181 | return False |
|
1180 | 1182 | try: |
|
1181 | 1183 | if not ask_yes_no('Return to editor to correct syntax error? ' |
|
1182 | 1184 | '[Y/n] ','y'): |
|
1183 | 1185 | return False |
|
1184 | 1186 | except EOFError: |
|
1185 | 1187 | return False |
|
1186 | self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg) | |
|
1188 | ||
|
1189 | def int0(x): | |
|
1190 | try: | |
|
1191 | return int(x) | |
|
1192 | except TypeError: | |
|
1193 | return 0 | |
|
1194 | # always pass integer line and offset values to editor hook | |
|
1195 | self.hooks.fix_error_editor(e.filename, | |
|
1196 | int0(e.lineno),int0(e.offset),e.msg) | |
|
1187 | 1197 | return True |
|
1188 | 1198 | |
|
1189 | 1199 | def edit_syntax_error(self): |
|
1190 | 1200 | """The bottom half of the syntax error handler called in the main loop. |
|
1191 | 1201 | |
|
1192 | 1202 | Loop until syntax error is fixed or user cancels. |
|
1193 | 1203 | """ |
|
1194 | 1204 | |
|
1195 | 1205 | while self.SyntaxTB.last_syntax_error: |
|
1196 | 1206 | # copy and clear last_syntax_error |
|
1197 | 1207 | err = self.SyntaxTB.clear_err_state() |
|
1198 | 1208 | if not self._should_recompile(err): |
|
1199 | 1209 | return |
|
1200 | 1210 | try: |
|
1201 | 1211 | # may set last_syntax_error again if a SyntaxError is raised |
|
1202 | 1212 | self.safe_execfile(err.filename,self.shell.user_ns) |
|
1203 | 1213 | except: |
|
1204 | 1214 | self.showtraceback() |
|
1205 | 1215 | else: |
|
1206 | 1216 | f = file(err.filename) |
|
1207 | 1217 | try: |
|
1208 | 1218 | sys.displayhook(f.read()) |
|
1209 | 1219 | finally: |
|
1210 | 1220 | f.close() |
|
1211 | 1221 | |
|
1212 | 1222 | def showsyntaxerror(self, filename=None): |
|
1213 | 1223 | """Display the syntax error that just occurred. |
|
1214 | 1224 | |
|
1215 | 1225 | This doesn't display a stack trace because there isn't one. |
|
1216 | 1226 | |
|
1217 | 1227 | If a filename is given, it is stuffed in the exception instead |
|
1218 | 1228 | of what was there before (because Python's parser always uses |
|
1219 | 1229 | "<string>" when reading from a string). |
|
1220 | 1230 | """ |
|
1221 | 1231 | etype, value, last_traceback = sys.exc_info() |
|
1222 | 1232 | if filename and etype is SyntaxError: |
|
1223 | 1233 | # Work hard to stuff the correct filename in the exception |
|
1224 | 1234 | try: |
|
1225 | 1235 | msg, (dummy_filename, lineno, offset, line) = value |
|
1226 | 1236 | except: |
|
1227 | 1237 | # Not the format we expect; leave it alone |
|
1228 | 1238 | pass |
|
1229 | 1239 | else: |
|
1230 | 1240 | # Stuff in the right filename |
|
1231 | 1241 | try: |
|
1232 | 1242 | # Assume SyntaxError is a class exception |
|
1233 | 1243 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1234 | 1244 | except: |
|
1235 | 1245 | # If that failed, assume SyntaxError is a string |
|
1236 | 1246 | value = msg, (filename, lineno, offset, line) |
|
1237 | 1247 | self.SyntaxTB(etype,value,[]) |
|
1238 | 1248 | |
|
1239 | 1249 | def debugger(self): |
|
1240 | 1250 | """Call the pdb debugger.""" |
|
1241 | 1251 | |
|
1242 | 1252 | if not self.rc.pdb: |
|
1243 | 1253 | return |
|
1244 | 1254 | pdb.pm() |
|
1245 | 1255 | |
|
1246 | 1256 | def showtraceback(self,exc_tuple = None,filename=None): |
|
1247 | 1257 | """Display the exception that just occurred.""" |
|
1248 | 1258 | |
|
1249 | 1259 | # Though this won't be called by syntax errors in the input line, |
|
1250 | 1260 | # there may be SyntaxError cases whith imported code. |
|
1251 | 1261 | if exc_tuple is None: |
|
1252 | 1262 | type, value, tb = sys.exc_info() |
|
1253 | 1263 | else: |
|
1254 | 1264 | type, value, tb = exc_tuple |
|
1255 | 1265 | if type is SyntaxError: |
|
1256 | 1266 | self.showsyntaxerror(filename) |
|
1257 | 1267 | else: |
|
1258 | 1268 | self.InteractiveTB() |
|
1259 | 1269 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1260 | 1270 | # pdb mucks up readline, fix it back |
|
1261 | 1271 | self.readline.set_completer(self.Completer.complete) |
|
1262 | 1272 | |
|
1263 | 1273 | def mainloop(self,banner=None): |
|
1264 | 1274 | """Creates the local namespace and starts the mainloop. |
|
1265 | 1275 | |
|
1266 | 1276 | If an optional banner argument is given, it will override the |
|
1267 | 1277 | internally created default banner.""" |
|
1268 | 1278 | |
|
1269 | 1279 | if self.rc.c: # Emulate Python's -c option |
|
1270 | 1280 | self.exec_init_cmd() |
|
1271 | 1281 | if banner is None: |
|
1272 | 1282 | if self.rc.banner: |
|
1273 | 1283 | banner = self.BANNER+self.banner2 |
|
1274 | 1284 | else: |
|
1275 | 1285 | banner = '' |
|
1276 | 1286 | self.interact(banner) |
|
1277 | 1287 | |
|
1278 | 1288 | def exec_init_cmd(self): |
|
1279 | 1289 | """Execute a command given at the command line. |
|
1280 | 1290 | |
|
1281 | 1291 | This emulates Python's -c option.""" |
|
1282 | 1292 | |
|
1283 | 1293 | sys.argv = ['-c'] |
|
1284 | 1294 | self.push(self.rc.c) |
|
1285 | 1295 | |
|
1286 | 1296 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1287 | 1297 | """Embeds IPython into a running python program. |
|
1288 | 1298 | |
|
1289 | 1299 | Input: |
|
1290 | 1300 | |
|
1291 | 1301 | - header: An optional header message can be specified. |
|
1292 | 1302 | |
|
1293 | 1303 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1294 | 1304 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1295 | 1305 | program variables become visible but user-specific configuration |
|
1296 | 1306 | remains possible. |
|
1297 | 1307 | |
|
1298 | 1308 | - stack_depth: specifies how many levels in the stack to go to |
|
1299 | 1309 | looking for namespaces (when local_ns and global_ns are None). This |
|
1300 | 1310 | allows an intermediate caller to make sure that this function gets |
|
1301 | 1311 | the namespace from the intended level in the stack. By default (0) |
|
1302 | 1312 | it will get its locals and globals from the immediate caller. |
|
1303 | 1313 | |
|
1304 | 1314 | Warning: it's possible to use this in a program which is being run by |
|
1305 | 1315 | IPython itself (via %run), but some funny things will happen (a few |
|
1306 | 1316 | globals get overwritten). In the future this will be cleaned up, as |
|
1307 | 1317 | there is no fundamental reason why it can't work perfectly.""" |
|
1308 | 1318 | |
|
1309 | 1319 | # Get locals and globals from caller |
|
1310 | 1320 | if local_ns is None or global_ns is None: |
|
1311 | 1321 | call_frame = sys._getframe(stack_depth).f_back |
|
1312 | 1322 | |
|
1313 | 1323 | if local_ns is None: |
|
1314 | 1324 | local_ns = call_frame.f_locals |
|
1315 | 1325 | if global_ns is None: |
|
1316 | 1326 | global_ns = call_frame.f_globals |
|
1317 | 1327 | |
|
1318 | 1328 | # Update namespaces and fire up interpreter |
|
1319 | 1329 | |
|
1320 | 1330 | # The global one is easy, we can just throw it in |
|
1321 | 1331 | self.user_global_ns = global_ns |
|
1322 | 1332 | |
|
1323 | 1333 | # but the user/local one is tricky: ipython needs it to store internal |
|
1324 | 1334 | # data, but we also need the locals. We'll copy locals in the user |
|
1325 | 1335 | # one, but will track what got copied so we can delete them at exit. |
|
1326 | 1336 | # This is so that a later embedded call doesn't see locals from a |
|
1327 | 1337 | # previous call (which most likely existed in a separate scope). |
|
1328 | 1338 | local_varnames = local_ns.keys() |
|
1329 | 1339 | self.user_ns.update(local_ns) |
|
1330 | 1340 | |
|
1331 | 1341 | # Patch for global embedding to make sure that things don't overwrite |
|
1332 | 1342 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1333 | 1343 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1334 | 1344 | if local_ns is None and global_ns is None: |
|
1335 | 1345 | self.user_global_ns.update(__main__.__dict__) |
|
1336 | 1346 | |
|
1337 | 1347 | # make sure the tab-completer has the correct frame information, so it |
|
1338 | 1348 | # actually completes using the frame's locals/globals |
|
1339 | 1349 | self.set_completer_frame(call_frame) |
|
1340 | 1350 | |
|
1341 | 1351 | # before activating the interactive mode, we need to make sure that |
|
1342 | 1352 | # all names in the builtin namespace needed by ipython point to |
|
1343 | 1353 | # ourselves, and not to other instances. |
|
1344 | 1354 | self.add_builtins() |
|
1345 | 1355 | |
|
1346 | 1356 | self.interact(header) |
|
1347 | 1357 | |
|
1348 | 1358 | # now, purge out the user namespace from anything we might have added |
|
1349 | 1359 | # from the caller's local namespace |
|
1350 | 1360 | delvar = self.user_ns.pop |
|
1351 | 1361 | for var in local_varnames: |
|
1352 | 1362 | delvar(var,None) |
|
1353 | 1363 | # and clean builtins we may have overridden |
|
1354 | 1364 | self.clean_builtins() |
|
1355 | 1365 | |
|
1356 | 1366 | def interact(self, banner=None): |
|
1357 | 1367 | """Closely emulate the interactive Python console. |
|
1358 | 1368 | |
|
1359 | 1369 | The optional banner argument specify the banner to print |
|
1360 | 1370 | before the first interaction; by default it prints a banner |
|
1361 | 1371 | similar to the one printed by the real Python interpreter, |
|
1362 | 1372 | followed by the current class name in parentheses (so as not |
|
1363 | 1373 | to confuse this with the real interpreter -- since it's so |
|
1364 | 1374 | close!). |
|
1365 | 1375 | |
|
1366 | 1376 | """ |
|
1367 | 1377 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1368 | 1378 | if banner is None: |
|
1369 | 1379 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1370 | 1380 | (sys.version, sys.platform, cprt, |
|
1371 | 1381 | self.__class__.__name__)) |
|
1372 | 1382 | else: |
|
1373 | 1383 | self.write(banner) |
|
1374 | 1384 | |
|
1375 | 1385 | more = 0 |
|
1376 | 1386 | |
|
1377 | 1387 | # Mark activity in the builtins |
|
1378 | 1388 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1379 | 1389 | |
|
1380 | 1390 | # exit_now is set by a call to %Exit or %Quit |
|
1381 | 1391 | self.exit_now = False |
|
1382 | 1392 | while not self.exit_now: |
|
1383 | 1393 | |
|
1384 | 1394 | try: |
|
1385 | 1395 | if more: |
|
1386 | 1396 | prompt = self.outputcache.prompt2 |
|
1387 | 1397 | if self.autoindent: |
|
1388 | 1398 | self.readline_startup_hook(self.pre_readline) |
|
1389 | 1399 | else: |
|
1390 | 1400 | prompt = self.outputcache.prompt1 |
|
1391 | 1401 | try: |
|
1392 | 1402 | line = self.raw_input(prompt,more) |
|
1393 | 1403 | if self.autoindent: |
|
1394 | 1404 | self.readline_startup_hook(None) |
|
1395 | 1405 | except EOFError: |
|
1396 | 1406 | if self.autoindent: |
|
1397 | 1407 | self.readline_startup_hook(None) |
|
1398 | 1408 | self.write("\n") |
|
1399 | 1409 | self.exit() |
|
1400 | 1410 | else: |
|
1401 | 1411 | more = self.push(line) |
|
1402 | 1412 | |
|
1403 | 1413 | if (self.SyntaxTB.last_syntax_error and |
|
1404 | 1414 | self.rc.autoedit_syntax): |
|
1405 | 1415 | self.edit_syntax_error() |
|
1406 | 1416 | |
|
1407 | 1417 | except KeyboardInterrupt: |
|
1408 | 1418 | self.write("\nKeyboardInterrupt\n") |
|
1409 | 1419 | self.resetbuffer() |
|
1410 | 1420 | more = 0 |
|
1411 | 1421 | # keep cache in sync with the prompt counter: |
|
1412 | 1422 | self.outputcache.prompt_count -= 1 |
|
1413 | 1423 | |
|
1414 | 1424 | if self.autoindent: |
|
1415 | 1425 | self.indent_current_nsp = 0 |
|
1416 | 1426 | self.indent_current = ' '* self.indent_current_nsp |
|
1417 | 1427 | |
|
1418 | 1428 | except bdb.BdbQuit: |
|
1419 | 1429 | warn("The Python debugger has exited with a BdbQuit exception.\n" |
|
1420 | 1430 | "Because of how pdb handles the stack, it is impossible\n" |
|
1421 | 1431 | "for IPython to properly format this particular exception.\n" |
|
1422 | 1432 | "IPython will resume normal operation.") |
|
1423 | 1433 | |
|
1424 | 1434 | # We are off again... |
|
1425 | 1435 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1426 | 1436 | |
|
1427 | 1437 | def excepthook(self, type, value, tb): |
|
1428 | 1438 | """One more defense for GUI apps that call sys.excepthook. |
|
1429 | 1439 | |
|
1430 | 1440 | GUI frameworks like wxPython trap exceptions and call |
|
1431 | 1441 | sys.excepthook themselves. I guess this is a feature that |
|
1432 | 1442 | enables them to keep running after exceptions that would |
|
1433 | 1443 | otherwise kill their mainloop. This is a bother for IPython |
|
1434 | 1444 | which excepts to catch all of the program exceptions with a try: |
|
1435 | 1445 | except: statement. |
|
1436 | 1446 | |
|
1437 | 1447 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1438 | 1448 | any app directly invokes sys.excepthook, it will look to the user like |
|
1439 | 1449 | IPython crashed. In order to work around this, we can disable the |
|
1440 | 1450 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1441 | 1451 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1442 | 1452 | call sys.excepthook will generate a regular-looking exception from |
|
1443 | 1453 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1444 | 1454 | crashes. |
|
1445 | 1455 | |
|
1446 | 1456 | This hook should be used sparingly, only in places which are not likely |
|
1447 | 1457 | to be true IPython errors. |
|
1448 | 1458 | """ |
|
1449 | 1459 | |
|
1450 | 1460 | self.InteractiveTB(type, value, tb, tb_offset=0) |
|
1451 | 1461 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1452 | 1462 | self.readline.set_completer(self.Completer.complete) |
|
1453 | 1463 | |
|
1454 | 1464 | def call_alias(self,alias,rest=''): |
|
1455 | 1465 | """Call an alias given its name and the rest of the line. |
|
1456 | 1466 | |
|
1457 | 1467 | This function MUST be given a proper alias, because it doesn't make |
|
1458 | 1468 | any checks when looking up into the alias table. The caller is |
|
1459 | 1469 | responsible for invoking it only with a valid alias.""" |
|
1460 | 1470 | |
|
1461 | 1471 | #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg |
|
1462 | 1472 | nargs,cmd = self.alias_table[alias] |
|
1463 | 1473 | # Expand the %l special to be the user's input line |
|
1464 | 1474 | if cmd.find('%l') >= 0: |
|
1465 | 1475 | cmd = cmd.replace('%l',rest) |
|
1466 | 1476 | rest = '' |
|
1467 | 1477 | if nargs==0: |
|
1468 | 1478 | # Simple, argument-less aliases |
|
1469 | 1479 | cmd = '%s %s' % (cmd,rest) |
|
1470 | 1480 | else: |
|
1471 | 1481 | # Handle aliases with positional arguments |
|
1472 | 1482 | args = rest.split(None,nargs) |
|
1473 | 1483 | if len(args)< nargs: |
|
1474 | 1484 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1475 | 1485 | (alias,nargs,len(args))) |
|
1476 | 1486 | return |
|
1477 | 1487 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1478 | 1488 | # Now call the macro, evaluating in the user's namespace |
|
1479 | 1489 | try: |
|
1480 | 1490 | self.system(cmd) |
|
1481 | 1491 | except: |
|
1482 | 1492 | self.showtraceback() |
|
1483 | 1493 | |
|
1484 | 1494 | def autoindent_update(self,line): |
|
1485 | 1495 | """Keep track of the indent level.""" |
|
1486 | 1496 | if self.autoindent: |
|
1487 | 1497 | if line: |
|
1488 | 1498 | ini_spaces = ini_spaces_re.match(line) |
|
1489 | 1499 | if ini_spaces: |
|
1490 | 1500 | nspaces = ini_spaces.end() |
|
1491 | 1501 | else: |
|
1492 | 1502 | nspaces = 0 |
|
1493 | 1503 | self.indent_current_nsp = nspaces |
|
1494 | 1504 | |
|
1495 | 1505 | if line[-1] == ':': |
|
1496 | 1506 | self.indent_current_nsp += 4 |
|
1497 | 1507 | elif dedent_re.match(line): |
|
1498 | 1508 | self.indent_current_nsp -= 4 |
|
1499 | 1509 | else: |
|
1500 | 1510 | self.indent_current_nsp = 0 |
|
1501 | 1511 | |
|
1502 | 1512 | # indent_current is the actual string to be inserted |
|
1503 | 1513 | # by the readline hooks for indentation |
|
1504 | 1514 | self.indent_current = ' '* self.indent_current_nsp |
|
1505 | 1515 | |
|
1506 | 1516 | def runlines(self,lines): |
|
1507 | 1517 | """Run a string of one or more lines of source. |
|
1508 | 1518 | |
|
1509 | 1519 | This method is capable of running a string containing multiple source |
|
1510 | 1520 | lines, as if they had been entered at the IPython prompt. Since it |
|
1511 | 1521 | exposes IPython's processing machinery, the given strings can contain |
|
1512 | 1522 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1513 | 1523 | |
|
1514 | 1524 | # We must start with a clean buffer, in case this is run from an |
|
1515 | 1525 | # interactive IPython session (via a magic, for example). |
|
1516 | 1526 | self.resetbuffer() |
|
1517 | 1527 | lines = lines.split('\n') |
|
1518 | 1528 | more = 0 |
|
1519 | 1529 | for line in lines: |
|
1520 | 1530 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1521 | 1531 | # NOT skip even a blank line if we are in a code block (more is |
|
1522 | 1532 | # true) |
|
1523 | 1533 | if line or more: |
|
1524 | 1534 | more = self.push(self.prefilter(line,more)) |
|
1525 | 1535 | # IPython's runsource returns None if there was an error |
|
1526 | 1536 | # compiling the code. This allows us to stop processing right |
|
1527 | 1537 | # away, so the user gets the error message at the right place. |
|
1528 | 1538 | if more is None: |
|
1529 | 1539 | break |
|
1530 | 1540 | # final newline in case the input didn't have it, so that the code |
|
1531 | 1541 | # actually does get executed |
|
1532 | 1542 | if more: |
|
1533 | 1543 | self.push('\n') |
|
1534 | 1544 | |
|
1535 | 1545 | def runsource(self, source, filename='<input>', symbol='single'): |
|
1536 | 1546 | """Compile and run some source in the interpreter. |
|
1537 | 1547 | |
|
1538 | 1548 | Arguments are as for compile_command(). |
|
1539 | 1549 | |
|
1540 | 1550 | One several things can happen: |
|
1541 | 1551 | |
|
1542 | 1552 | 1) The input is incorrect; compile_command() raised an |
|
1543 | 1553 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1544 | 1554 | will be printed by calling the showsyntaxerror() method. |
|
1545 | 1555 | |
|
1546 | 1556 | 2) The input is incomplete, and more input is required; |
|
1547 | 1557 | compile_command() returned None. Nothing happens. |
|
1548 | 1558 | |
|
1549 | 1559 | 3) The input is complete; compile_command() returned a code |
|
1550 | 1560 | object. The code is executed by calling self.runcode() (which |
|
1551 | 1561 | also handles run-time exceptions, except for SystemExit). |
|
1552 | 1562 | |
|
1553 | 1563 | The return value is: |
|
1554 | 1564 | |
|
1555 | 1565 | - True in case 2 |
|
1556 | 1566 | |
|
1557 | 1567 | - False in the other cases, unless an exception is raised, where |
|
1558 | 1568 | None is returned instead. This can be used by external callers to |
|
1559 | 1569 | know whether to continue feeding input or not. |
|
1560 | 1570 | |
|
1561 | 1571 | The return value can be used to decide whether to use sys.ps1 or |
|
1562 | 1572 | sys.ps2 to prompt the next line.""" |
|
1563 | 1573 | |
|
1564 | 1574 | try: |
|
1565 | 1575 | code = self.compile(source,filename,symbol) |
|
1566 | 1576 | except (OverflowError, SyntaxError, ValueError): |
|
1567 | 1577 | # Case 1 |
|
1568 | 1578 | self.showsyntaxerror(filename) |
|
1569 | 1579 | return None |
|
1570 | 1580 | |
|
1571 | 1581 | if code is None: |
|
1572 | 1582 | # Case 2 |
|
1573 | 1583 | return True |
|
1574 | 1584 | |
|
1575 | 1585 | # Case 3 |
|
1576 | 1586 | # We store the code object so that threaded shells and |
|
1577 | 1587 | # custom exception handlers can access all this info if needed. |
|
1578 | 1588 | # The source corresponding to this can be obtained from the |
|
1579 | 1589 | # buffer attribute as '\n'.join(self.buffer). |
|
1580 | 1590 | self.code_to_run = code |
|
1581 | 1591 | # now actually execute the code object |
|
1582 | 1592 | if self.runcode(code) == 0: |
|
1583 | 1593 | return False |
|
1584 | 1594 | else: |
|
1585 | 1595 | return None |
|
1586 | 1596 | |
|
1587 | 1597 | def runcode(self,code_obj): |
|
1588 | 1598 | """Execute a code object. |
|
1589 | 1599 | |
|
1590 | 1600 | When an exception occurs, self.showtraceback() is called to display a |
|
1591 | 1601 | traceback. |
|
1592 | 1602 | |
|
1593 | 1603 | Return value: a flag indicating whether the code to be run completed |
|
1594 | 1604 | successfully: |
|
1595 | 1605 | |
|
1596 | 1606 | - 0: successful execution. |
|
1597 | 1607 | - 1: an error occurred. |
|
1598 | 1608 | """ |
|
1599 | 1609 | |
|
1600 | 1610 | # Set our own excepthook in case the user code tries to call it |
|
1601 | 1611 | # directly, so that the IPython crash handler doesn't get triggered |
|
1602 | 1612 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1603 | 1613 | |
|
1604 | 1614 | # we save the original sys.excepthook in the instance, in case config |
|
1605 | 1615 | # code (such as magics) needs access to it. |
|
1606 | 1616 | self.sys_excepthook = old_excepthook |
|
1607 | 1617 | outflag = 1 # happens in more places, so it's easier as default |
|
1608 | 1618 | try: |
|
1609 | 1619 | try: |
|
1610 | 1620 | # Embedded instances require separate global/local namespaces |
|
1611 | 1621 | # so they can see both the surrounding (local) namespace and |
|
1612 | 1622 | # the module-level globals when called inside another function. |
|
1613 | 1623 | if self.embedded: |
|
1614 | 1624 | exec code_obj in self.user_global_ns, self.user_ns |
|
1615 | 1625 | # Normal (non-embedded) instances should only have a single |
|
1616 | 1626 | # namespace for user code execution, otherwise functions won't |
|
1617 | 1627 | # see interactive top-level globals. |
|
1618 | 1628 | else: |
|
1619 | 1629 | exec code_obj in self.user_ns |
|
1620 | 1630 | finally: |
|
1621 | 1631 | # Reset our crash handler in place |
|
1622 | 1632 | sys.excepthook = old_excepthook |
|
1623 | 1633 | except SystemExit: |
|
1624 | 1634 | self.resetbuffer() |
|
1625 | 1635 | self.showtraceback() |
|
1626 | 1636 | warn("Type exit or quit to exit IPython " |
|
1627 | 1637 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
1628 | 1638 | except self.custom_exceptions: |
|
1629 | 1639 | etype,value,tb = sys.exc_info() |
|
1630 | 1640 | self.CustomTB(etype,value,tb) |
|
1631 | 1641 | except: |
|
1632 | 1642 | self.showtraceback() |
|
1633 | 1643 | else: |
|
1634 | 1644 | outflag = 0 |
|
1635 | 1645 | if softspace(sys.stdout, 0): |
|
1636 | 1646 | |
|
1637 | 1647 | # Flush out code object which has been run (and source) |
|
1638 | 1648 | self.code_to_run = None |
|
1639 | 1649 | return outflag |
|
1640 | 1650 | |
|
1641 | 1651 | def push(self, line): |
|
1642 | 1652 | """Push a line to the interpreter. |
|
1643 | 1653 | |
|
1644 | 1654 | The line should not have a trailing newline; it may have |
|
1645 | 1655 | internal newlines. The line is appended to a buffer and the |
|
1646 | 1656 | interpreter's runsource() method is called with the |
|
1647 | 1657 | concatenated contents of the buffer as source. If this |
|
1648 | 1658 | indicates that the command was executed or invalid, the buffer |
|
1649 | 1659 | is reset; otherwise, the command is incomplete, and the buffer |
|
1650 | 1660 | is left as it was after the line was appended. The return |
|
1651 | 1661 | value is 1 if more input is required, 0 if the line was dealt |
|
1652 | 1662 | with in some way (this is the same as runsource()). |
|
1653 | 1663 | """ |
|
1654 | 1664 | |
|
1655 | 1665 | # autoindent management should be done here, and not in the |
|
1656 | 1666 | # interactive loop, since that one is only seen by keyboard input. We |
|
1657 | 1667 | # need this done correctly even for code run via runlines (which uses |
|
1658 | 1668 | # push). |
|
1659 | 1669 | |
|
1660 | 1670 | #print 'push line: <%s>' % line # dbg |
|
1661 | 1671 | self.autoindent_update(line) |
|
1662 | 1672 | |
|
1663 | 1673 | self.buffer.append(line) |
|
1664 | 1674 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1665 | 1675 | if not more: |
|
1666 | 1676 | self.resetbuffer() |
|
1667 | 1677 | return more |
|
1668 | 1678 | |
|
1669 | 1679 | def resetbuffer(self): |
|
1670 | 1680 | """Reset the input buffer.""" |
|
1671 | 1681 | self.buffer[:] = [] |
|
1672 | 1682 | |
|
1673 | 1683 | def raw_input(self,prompt='',continue_prompt=False): |
|
1674 | 1684 | """Write a prompt and read a line. |
|
1675 | 1685 | |
|
1676 | 1686 | The returned line does not include the trailing newline. |
|
1677 | 1687 | When the user enters the EOF key sequence, EOFError is raised. |
|
1678 | 1688 | |
|
1679 | 1689 | Optional inputs: |
|
1680 | 1690 | |
|
1681 | 1691 | - prompt(''): a string to be printed to prompt the user. |
|
1682 | 1692 | |
|
1683 | 1693 | - continue_prompt(False): whether this line is the first one or a |
|
1684 | 1694 | continuation in a sequence of inputs. |
|
1685 | 1695 | """ |
|
1686 | 1696 | |
|
1687 | 1697 | line = raw_input_original(prompt) |
|
1688 | 1698 | # Try to be reasonably smart about not re-indenting pasted input more |
|
1689 | 1699 | # than necessary. We do this by trimming out the auto-indent initial |
|
1690 | 1700 | # spaces, if the user's actual input started itself with whitespace. |
|
1691 | 1701 | if self.autoindent: |
|
1692 | 1702 | line2 = line[self.indent_current_nsp:] |
|
1693 | 1703 | if line2[0:1] in (' ','\t'): |
|
1694 | 1704 | line = line2 |
|
1695 | 1705 | return self.prefilter(line,continue_prompt) |
|
1696 | 1706 | |
|
1697 | 1707 | def split_user_input(self,line): |
|
1698 | 1708 | """Split user input into pre-char, function part and rest.""" |
|
1699 | 1709 | |
|
1700 | 1710 | lsplit = self.line_split.match(line) |
|
1701 | 1711 | if lsplit is None: # no regexp match returns None |
|
1702 | 1712 | try: |
|
1703 | 1713 | iFun,theRest = line.split(None,1) |
|
1704 | 1714 | except ValueError: |
|
1705 | 1715 | iFun,theRest = line,'' |
|
1706 | 1716 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1707 | 1717 | else: |
|
1708 | 1718 | pre,iFun,theRest = lsplit.groups() |
|
1709 | 1719 | |
|
1710 | 1720 | #print 'line:<%s>' % line # dbg |
|
1711 | 1721 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1712 | 1722 | return pre,iFun.strip(),theRest |
|
1713 | 1723 | |
|
1714 | 1724 | def _prefilter(self, line, continue_prompt): |
|
1715 | 1725 | """Calls different preprocessors, depending on the form of line.""" |
|
1716 | 1726 | |
|
1717 | 1727 | # All handlers *must* return a value, even if it's blank (''). |
|
1718 | 1728 | |
|
1719 | 1729 | # Lines are NOT logged here. Handlers should process the line as |
|
1720 | 1730 | # needed, update the cache AND log it (so that the input cache array |
|
1721 | 1731 | # stays synced). |
|
1722 | 1732 | |
|
1723 | 1733 | # This function is _very_ delicate, and since it's also the one which |
|
1724 | 1734 | # determines IPython's response to user input, it must be as efficient |
|
1725 | 1735 | # as possible. For this reason it has _many_ returns in it, trying |
|
1726 | 1736 | # always to exit as quickly as it can figure out what it needs to do. |
|
1727 | 1737 | |
|
1728 | 1738 | # This function is the main responsible for maintaining IPython's |
|
1729 | 1739 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1730 | 1740 | # making changes to anything here. |
|
1731 | 1741 | |
|
1732 | 1742 | #..................................................................... |
|
1733 | 1743 | # Code begins |
|
1734 | 1744 | |
|
1735 | 1745 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1736 | 1746 | |
|
1737 | 1747 | # save the line away in case we crash, so the post-mortem handler can |
|
1738 | 1748 | # record it |
|
1739 | 1749 | self._last_input_line = line |
|
1740 | 1750 | |
|
1741 | 1751 | #print '***line: <%s>' % line # dbg |
|
1742 | 1752 | |
|
1743 | 1753 | # the input history needs to track even empty lines |
|
1744 | 1754 | if not line.strip(): |
|
1745 | 1755 | if not continue_prompt: |
|
1746 | 1756 | self.outputcache.prompt_count -= 1 |
|
1747 | 1757 | return self.handle_normal(line,continue_prompt) |
|
1748 | 1758 | #return self.handle_normal('',continue_prompt) |
|
1749 | 1759 | |
|
1750 | 1760 | # print '***cont',continue_prompt # dbg |
|
1751 | 1761 | # special handlers are only allowed for single line statements |
|
1752 | 1762 | if continue_prompt and not self.rc.multi_line_specials: |
|
1753 | 1763 | return self.handle_normal(line,continue_prompt) |
|
1754 | 1764 | |
|
1755 | 1765 | # For the rest, we need the structure of the input |
|
1756 | 1766 | pre,iFun,theRest = self.split_user_input(line) |
|
1757 | 1767 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1758 | 1768 | |
|
1759 | 1769 | # First check for explicit escapes in the last/first character |
|
1760 | 1770 | handler = None |
|
1761 | 1771 | if line[-1] == self.ESC_HELP: |
|
1762 | 1772 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
1763 | 1773 | if handler is None: |
|
1764 | 1774 | # look at the first character of iFun, NOT of line, so we skip |
|
1765 | 1775 | # leading whitespace in multiline input |
|
1766 | 1776 | handler = self.esc_handlers.get(iFun[0:1]) |
|
1767 | 1777 | if handler is not None: |
|
1768 | 1778 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
1769 | 1779 | # Emacs ipython-mode tags certain input lines |
|
1770 | 1780 | if line.endswith('# PYTHON-MODE'): |
|
1771 | 1781 | return self.handle_emacs(line,continue_prompt) |
|
1772 | 1782 | |
|
1773 | 1783 | # Next, check if we can automatically execute this thing |
|
1774 | 1784 | |
|
1775 | 1785 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
1776 | 1786 | if continue_prompt and self.rc.multi_line_specials and \ |
|
1777 | 1787 | iFun.startswith(self.ESC_SHELL): |
|
1778 | 1788 | return self.handle_shell_escape(line,continue_prompt, |
|
1779 | 1789 | pre=pre,iFun=iFun, |
|
1780 | 1790 | theRest=theRest) |
|
1781 | 1791 | |
|
1782 | 1792 | # Let's try to find if the input line is a magic fn |
|
1783 | 1793 | oinfo = None |
|
1784 | 1794 | if hasattr(self,'magic_'+iFun): |
|
1785 | 1795 | # WARNING: _ofind uses getattr(), so it can consume generators and |
|
1786 | 1796 | # cause other side effects. |
|
1787 | 1797 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1788 | 1798 | if oinfo['ismagic']: |
|
1789 | 1799 | # Be careful not to call magics when a variable assignment is |
|
1790 | 1800 | # being made (ls='hi', for example) |
|
1791 | 1801 | if self.rc.automagic and \ |
|
1792 | 1802 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
1793 | 1803 | (self.rc.multi_line_specials or not continue_prompt): |
|
1794 | 1804 | return self.handle_magic(line,continue_prompt, |
|
1795 | 1805 | pre,iFun,theRest) |
|
1796 | 1806 | else: |
|
1797 | 1807 | return self.handle_normal(line,continue_prompt) |
|
1798 | 1808 | |
|
1799 | 1809 | # If the rest of the line begins with an (in)equality, assginment or |
|
1800 | 1810 | # function call, we should not call _ofind but simply execute it. |
|
1801 | 1811 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
1802 | 1812 | # |
|
1803 | 1813 | # It also allows users to assign to either alias or magic names true |
|
1804 | 1814 | # python variables (the magic/alias systems always take second seat to |
|
1805 | 1815 | # true python code). |
|
1806 | 1816 | if theRest and theRest[0] in '!=()': |
|
1807 | 1817 | return self.handle_normal(line,continue_prompt) |
|
1808 | 1818 | |
|
1809 | 1819 | if oinfo is None: |
|
1810 | 1820 | # let's try to ensure that _oinfo is ONLY called when autocall is |
|
1811 | 1821 | # on. Since it has inevitable potential side effects, at least |
|
1812 | 1822 | # having autocall off should be a guarantee to the user that no |
|
1813 | 1823 | # weird things will happen. |
|
1814 | 1824 | |
|
1815 | 1825 | if self.rc.autocall: |
|
1816 | 1826 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1817 | 1827 | else: |
|
1818 | 1828 | # in this case, all that's left is either an alias or |
|
1819 | 1829 | # processing the line normally. |
|
1820 | 1830 | if iFun in self.alias_table: |
|
1821 | 1831 | return self.handle_alias(line,continue_prompt, |
|
1822 | 1832 | pre,iFun,theRest) |
|
1823 | 1833 | else: |
|
1824 | 1834 | return self.handle_normal(line,continue_prompt) |
|
1825 | 1835 | |
|
1826 | 1836 | if not oinfo['found']: |
|
1827 | 1837 | return self.handle_normal(line,continue_prompt) |
|
1828 | 1838 | else: |
|
1829 | 1839 | #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg |
|
1830 | 1840 | if oinfo['isalias']: |
|
1831 | 1841 | return self.handle_alias(line,continue_prompt, |
|
1832 | 1842 | pre,iFun,theRest) |
|
1833 | 1843 | |
|
1834 | 1844 | if self.rc.autocall and \ |
|
1835 | 1845 | not self.re_exclude_auto.match(theRest) and \ |
|
1836 | 1846 | self.re_fun_name.match(iFun) and \ |
|
1837 | 1847 | callable(oinfo['obj']) : |
|
1838 | 1848 | #print 'going auto' # dbg |
|
1839 | 1849 | return self.handle_auto(line,continue_prompt, |
|
1840 | 1850 | pre,iFun,theRest,oinfo['obj']) |
|
1841 | 1851 | else: |
|
1842 | 1852 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
1843 | 1853 | return self.handle_normal(line,continue_prompt) |
|
1844 | 1854 | |
|
1845 | 1855 | # If we get here, we have a normal Python line. Log and return. |
|
1846 | 1856 | return self.handle_normal(line,continue_prompt) |
|
1847 | 1857 | |
|
1848 | 1858 | def _prefilter_dumb(self, line, continue_prompt): |
|
1849 | 1859 | """simple prefilter function, for debugging""" |
|
1850 | 1860 | return self.handle_normal(line,continue_prompt) |
|
1851 | 1861 | |
|
1852 | 1862 | # Set the default prefilter() function (this can be user-overridden) |
|
1853 | 1863 | prefilter = _prefilter |
|
1854 | 1864 | |
|
1855 | 1865 | def handle_normal(self,line,continue_prompt=None, |
|
1856 | 1866 | pre=None,iFun=None,theRest=None): |
|
1857 | 1867 | """Handle normal input lines. Use as a template for handlers.""" |
|
1858 | 1868 | |
|
1859 | 1869 | # With autoindent on, we need some way to exit the input loop, and I |
|
1860 | 1870 | # don't want to force the user to have to backspace all the way to |
|
1861 | 1871 | # clear the line. The rule will be in this case, that either two |
|
1862 | 1872 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
1863 | 1873 | # of a size different to the indent level, will exit the input loop. |
|
1864 | 1874 | |
|
1865 | 1875 | if (continue_prompt and self.autoindent and isspace(line) and |
|
1866 | 1876 | (line != self.indent_current or isspace(self.buffer[-1]))): |
|
1867 | 1877 | line = '' |
|
1868 | 1878 | |
|
1869 | 1879 | self.log(line,continue_prompt) |
|
1870 | 1880 | return line |
|
1871 | 1881 | |
|
1872 | 1882 | def handle_alias(self,line,continue_prompt=None, |
|
1873 | 1883 | pre=None,iFun=None,theRest=None): |
|
1874 | 1884 | """Handle alias input lines. """ |
|
1875 | 1885 | |
|
1876 | 1886 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
1877 | 1887 | # aliases won't work in indented sections. |
|
1878 | 1888 | line_out = '%sipalias("%s %s")' % (pre,iFun,esc_quotes(theRest)) |
|
1879 | 1889 | self.log(line_out,continue_prompt) |
|
1880 | 1890 | return line_out |
|
1881 | 1891 | |
|
1882 | 1892 | def handle_shell_escape(self, line, continue_prompt=None, |
|
1883 | 1893 | pre=None,iFun=None,theRest=None): |
|
1884 | 1894 | """Execute the line in a shell, empty return value""" |
|
1885 | 1895 | |
|
1886 | 1896 | #print 'line in :', `line` # dbg |
|
1887 | 1897 | # Example of a special handler. Others follow a similar pattern. |
|
1888 | 1898 | if continue_prompt: # multi-line statements |
|
1889 | 1899 | if iFun.startswith('!!'): |
|
1890 | 1900 | print 'SyntaxError: !! is not allowed in multiline statements' |
|
1891 | 1901 | return pre |
|
1892 | 1902 | else: |
|
1893 | 1903 | cmd = ("%s %s" % (iFun[1:],theRest)) |
|
1894 | 1904 | line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd + "_") |
|
1895 | 1905 | else: # single-line input |
|
1896 | 1906 | if line.startswith('!!'): |
|
1897 | 1907 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
1898 | 1908 | # the actual command to be executed, so handle_magic can work |
|
1899 | 1909 | # correctly |
|
1900 | 1910 | theRest = '%s %s' % (iFun[2:],theRest) |
|
1901 | 1911 | iFun = 'sx' |
|
1902 | 1912 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), |
|
1903 | 1913 | continue_prompt,pre,iFun,theRest) |
|
1904 | 1914 | else: |
|
1905 | 1915 | cmd=line[1:] |
|
1906 | 1916 | line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd +"_") |
|
1907 | 1917 | # update cache/log and return |
|
1908 | 1918 | self.log(line_out,continue_prompt) |
|
1909 | 1919 | return line_out |
|
1910 | 1920 | |
|
1911 | 1921 | def handle_magic(self, line, continue_prompt=None, |
|
1912 | 1922 | pre=None,iFun=None,theRest=None): |
|
1913 | 1923 | """Execute magic functions. |
|
1914 | 1924 | |
|
1915 | 1925 | Also log them with a prepended # so the log is clean Python.""" |
|
1916 | 1926 | |
|
1917 | 1927 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) |
|
1918 | 1928 | self.log(cmd,continue_prompt) |
|
1919 | 1929 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
1920 | 1930 | return cmd |
|
1921 | 1931 | |
|
1922 | 1932 | def handle_auto(self, line, continue_prompt=None, |
|
1923 | 1933 | pre=None,iFun=None,theRest=None,obj=None): |
|
1924 | 1934 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
1925 | 1935 | |
|
1926 | 1936 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1927 | 1937 | |
|
1928 | 1938 | # This should only be active for single-line input! |
|
1929 | 1939 | if continue_prompt: |
|
1930 | 1940 | self.log(line,continue_prompt) |
|
1931 | 1941 | return line |
|
1932 | 1942 | |
|
1933 | 1943 | auto_rewrite = True |
|
1934 | 1944 | if pre == self.ESC_QUOTE: |
|
1935 | 1945 | # Auto-quote splitting on whitespace |
|
1936 | 1946 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
1937 | 1947 | elif pre == self.ESC_QUOTE2: |
|
1938 | 1948 | # Auto-quote whole string |
|
1939 | 1949 | newcmd = '%s("%s")' % (iFun,theRest) |
|
1940 | 1950 | else: |
|
1941 | 1951 | # Auto-paren. |
|
1942 | 1952 | # We only apply it to argument-less calls if the autocall |
|
1943 | 1953 | # parameter is set to 2. We only need to check that autocall is < |
|
1944 | 1954 | # 2, since this function isn't called unless it's at least 1. |
|
1945 | 1955 | if not theRest and (self.rc.autocall < 2): |
|
1946 | 1956 | newcmd = '%s %s' % (iFun,theRest) |
|
1947 | 1957 | auto_rewrite = False |
|
1948 | 1958 | else: |
|
1949 | 1959 | if theRest.startswith('['): |
|
1950 | 1960 | if hasattr(obj,'__getitem__'): |
|
1951 | 1961 | # Don't autocall in this case: item access for an object |
|
1952 | 1962 | # which is BOTH callable and implements __getitem__. |
|
1953 | 1963 | newcmd = '%s %s' % (iFun,theRest) |
|
1954 | 1964 | auto_rewrite = False |
|
1955 | 1965 | else: |
|
1956 | 1966 | # if the object doesn't support [] access, go ahead and |
|
1957 | 1967 | # autocall |
|
1958 | 1968 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
1959 | 1969 | elif theRest.endswith(';'): |
|
1960 | 1970 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
1961 | 1971 | else: |
|
1962 | 1972 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
1963 | 1973 | |
|
1964 | 1974 | if auto_rewrite: |
|
1965 | 1975 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
1966 | 1976 | # log what is now valid Python, not the actual user input (without the |
|
1967 | 1977 | # final newline) |
|
1968 | 1978 | self.log(newcmd,continue_prompt) |
|
1969 | 1979 | return newcmd |
|
1970 | 1980 | |
|
1971 | 1981 | def handle_help(self, line, continue_prompt=None, |
|
1972 | 1982 | pre=None,iFun=None,theRest=None): |
|
1973 | 1983 | """Try to get some help for the object. |
|
1974 | 1984 | |
|
1975 | 1985 | obj? or ?obj -> basic information. |
|
1976 | 1986 | obj?? or ??obj -> more details. |
|
1977 | 1987 | """ |
|
1978 | 1988 | |
|
1979 | 1989 | # We need to make sure that we don't process lines which would be |
|
1980 | 1990 | # otherwise valid python, such as "x=1 # what?" |
|
1981 | 1991 | try: |
|
1982 | 1992 | codeop.compile_command(line) |
|
1983 | 1993 | except SyntaxError: |
|
1984 | 1994 | # We should only handle as help stuff which is NOT valid syntax |
|
1985 | 1995 | if line[0]==self.ESC_HELP: |
|
1986 | 1996 | line = line[1:] |
|
1987 | 1997 | elif line[-1]==self.ESC_HELP: |
|
1988 | 1998 | line = line[:-1] |
|
1989 | 1999 | self.log('#?'+line) |
|
1990 | 2000 | if line: |
|
1991 | 2001 | self.magic_pinfo(line) |
|
1992 | 2002 | else: |
|
1993 | 2003 | page(self.usage,screen_lines=self.rc.screen_length) |
|
1994 | 2004 | return '' # Empty string is needed here! |
|
1995 | 2005 | except: |
|
1996 | 2006 | # Pass any other exceptions through to the normal handler |
|
1997 | 2007 | return self.handle_normal(line,continue_prompt) |
|
1998 | 2008 | else: |
|
1999 | 2009 | # If the code compiles ok, we should handle it normally |
|
2000 | 2010 | return self.handle_normal(line,continue_prompt) |
|
2001 | 2011 | |
|
2002 | 2012 | def handle_emacs(self,line,continue_prompt=None, |
|
2003 | 2013 | pre=None,iFun=None,theRest=None): |
|
2004 | 2014 | """Handle input lines marked by python-mode.""" |
|
2005 | 2015 | |
|
2006 | 2016 | # Currently, nothing is done. Later more functionality can be added |
|
2007 | 2017 | # here if needed. |
|
2008 | 2018 | |
|
2009 | 2019 | # The input cache shouldn't be updated |
|
2010 | 2020 | |
|
2011 | 2021 | return line |
|
2012 | 2022 | |
|
2013 | 2023 | def mktempfile(self,data=None): |
|
2014 | 2024 | """Make a new tempfile and return its filename. |
|
2015 | 2025 | |
|
2016 | 2026 | This makes a call to tempfile.mktemp, but it registers the created |
|
2017 | 2027 | filename internally so ipython cleans it up at exit time. |
|
2018 | 2028 | |
|
2019 | 2029 | Optional inputs: |
|
2020 | 2030 | |
|
2021 | 2031 | - data(None): if data is given, it gets written out to the temp file |
|
2022 | 2032 | immediately, and the file is closed again.""" |
|
2023 | 2033 | |
|
2024 | 2034 | filename = tempfile.mktemp('.py') |
|
2025 | 2035 | self.tempfiles.append(filename) |
|
2026 | 2036 | |
|
2027 | 2037 | if data: |
|
2028 | 2038 | tmp_file = open(filename,'w') |
|
2029 | 2039 | tmp_file.write(data) |
|
2030 | 2040 | tmp_file.close() |
|
2031 | 2041 | return filename |
|
2032 | 2042 | |
|
2033 | 2043 | def write(self,data): |
|
2034 | 2044 | """Write a string to the default output""" |
|
2035 | 2045 | Term.cout.write(data) |
|
2036 | 2046 | |
|
2037 | 2047 | def write_err(self,data): |
|
2038 | 2048 | """Write a string to the default error output""" |
|
2039 | 2049 | Term.cerr.write(data) |
|
2040 | 2050 | |
|
2041 | 2051 | def exit(self): |
|
2042 | 2052 | """Handle interactive exit. |
|
2043 | 2053 | |
|
2044 | 2054 | This method sets the exit_now attribute.""" |
|
2045 | 2055 | |
|
2046 | 2056 | if self.rc.confirm_exit: |
|
2047 | 2057 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2048 | 2058 | self.exit_now = True |
|
2049 | 2059 | else: |
|
2050 | 2060 | self.exit_now = True |
|
2051 | 2061 | return self.exit_now |
|
2052 | 2062 | |
|
2053 | 2063 | def safe_execfile(self,fname,*where,**kw): |
|
2054 | 2064 | fname = os.path.expanduser(fname) |
|
2055 | 2065 | |
|
2056 | 2066 | # find things also in current directory |
|
2057 | 2067 | dname = os.path.dirname(fname) |
|
2058 | 2068 | if not sys.path.count(dname): |
|
2059 | 2069 | sys.path.append(dname) |
|
2060 | 2070 | |
|
2061 | 2071 | try: |
|
2062 | 2072 | xfile = open(fname) |
|
2063 | 2073 | except: |
|
2064 | 2074 | print >> Term.cerr, \ |
|
2065 | 2075 | 'Could not open file <%s> for safe execution.' % fname |
|
2066 | 2076 | return None |
|
2067 | 2077 | |
|
2068 | 2078 | kw.setdefault('islog',0) |
|
2069 | 2079 | kw.setdefault('quiet',1) |
|
2070 | 2080 | kw.setdefault('exit_ignore',0) |
|
2071 | 2081 | first = xfile.readline() |
|
2072 | 2082 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2073 | 2083 | xfile.close() |
|
2074 | 2084 | # line by line execution |
|
2075 | 2085 | if first.startswith(loghead) or kw['islog']: |
|
2076 | 2086 | print 'Loading log file <%s> one line at a time...' % fname |
|
2077 | 2087 | if kw['quiet']: |
|
2078 | 2088 | stdout_save = sys.stdout |
|
2079 | 2089 | sys.stdout = StringIO.StringIO() |
|
2080 | 2090 | try: |
|
2081 | 2091 | globs,locs = where[0:2] |
|
2082 | 2092 | except: |
|
2083 | 2093 | try: |
|
2084 | 2094 | globs = locs = where[0] |
|
2085 | 2095 | except: |
|
2086 | 2096 | globs = locs = globals() |
|
2087 | 2097 | badblocks = [] |
|
2088 | 2098 | |
|
2089 | 2099 | # we also need to identify indented blocks of code when replaying |
|
2090 | 2100 | # logs and put them together before passing them to an exec |
|
2091 | 2101 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2092 | 2102 | # file. It's easiest if we swallow the whole thing in memory |
|
2093 | 2103 | # first, and manually walk through the lines list moving the |
|
2094 | 2104 | # counter ourselves. |
|
2095 | 2105 | indent_re = re.compile('\s+\S') |
|
2096 | 2106 | xfile = open(fname) |
|
2097 | 2107 | filelines = xfile.readlines() |
|
2098 | 2108 | xfile.close() |
|
2099 | 2109 | nlines = len(filelines) |
|
2100 | 2110 | lnum = 0 |
|
2101 | 2111 | while lnum < nlines: |
|
2102 | 2112 | line = filelines[lnum] |
|
2103 | 2113 | lnum += 1 |
|
2104 | 2114 | # don't re-insert logger status info into cache |
|
2105 | 2115 | if line.startswith('#log#'): |
|
2106 | 2116 | continue |
|
2107 | 2117 | else: |
|
2108 | 2118 | # build a block of code (maybe a single line) for execution |
|
2109 | 2119 | block = line |
|
2110 | 2120 | try: |
|
2111 | 2121 | next = filelines[lnum] # lnum has already incremented |
|
2112 | 2122 | except: |
|
2113 | 2123 | next = None |
|
2114 | 2124 | while next and indent_re.match(next): |
|
2115 | 2125 | block += next |
|
2116 | 2126 | lnum += 1 |
|
2117 | 2127 | try: |
|
2118 | 2128 | next = filelines[lnum] |
|
2119 | 2129 | except: |
|
2120 | 2130 | next = None |
|
2121 | 2131 | # now execute the block of one or more lines |
|
2122 | 2132 | try: |
|
2123 | 2133 | exec block in globs,locs |
|
2124 | 2134 | except SystemExit: |
|
2125 | 2135 | pass |
|
2126 | 2136 | except: |
|
2127 | 2137 | badblocks.append(block.rstrip()) |
|
2128 | 2138 | if kw['quiet']: # restore stdout |
|
2129 | 2139 | sys.stdout.close() |
|
2130 | 2140 | sys.stdout = stdout_save |
|
2131 | 2141 | print 'Finished replaying log file <%s>' % fname |
|
2132 | 2142 | if badblocks: |
|
2133 | 2143 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2134 | 2144 | '<%s> reported errors:' % fname) |
|
2135 | 2145 | |
|
2136 | 2146 | for badline in badblocks: |
|
2137 | 2147 | print >> sys.stderr, badline |
|
2138 | 2148 | else: # regular file execution |
|
2139 | 2149 | try: |
|
2140 | 2150 | execfile(fname,*where) |
|
2141 | 2151 | except SyntaxError: |
|
2142 | 2152 | etype,evalue = sys.exc_info()[:2] |
|
2143 | 2153 | self.SyntaxTB(etype,evalue,[]) |
|
2144 | 2154 | warn('Failure executing file: <%s>' % fname) |
|
2145 | 2155 | except SystemExit,status: |
|
2146 | 2156 | if not kw['exit_ignore']: |
|
2147 | 2157 | self.InteractiveTB() |
|
2148 | 2158 | warn('Failure executing file: <%s>' % fname) |
|
2149 | 2159 | except: |
|
2150 | 2160 | self.InteractiveTB() |
|
2151 | 2161 | warn('Failure executing file: <%s>' % fname) |
|
2152 | 2162 | |
|
2153 | 2163 | #************************* end of file <iplib.py> ***************************** |
@@ -1,701 +1,701 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or better. |
|
6 | 6 | |
|
7 | 7 | This file contains the main make_IPython() starter function. |
|
8 | 8 | |
|
9 |
$Id: ipmaker.py 99 |
|
|
9 | $Id: ipmaker.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
10 | 10 | |
|
11 | 11 | #***************************************************************************** |
|
12 |
# Copyright (C) 2001-200 |
|
|
12 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #***************************************************************************** |
|
17 | 17 | |
|
18 | 18 | from IPython import Release |
|
19 | 19 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | __version__ = Release.version |
|
22 | 22 | |
|
23 | 23 | credits._Printer__data = """ |
|
24 | 24 | Python: %s |
|
25 | 25 | |
|
26 | 26 | IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users. |
|
27 | 27 | See http://ipython.scipy.org for more information.""" \ |
|
28 | 28 | % credits._Printer__data |
|
29 | 29 | |
|
30 | 30 | copyright._Printer__data += """ |
|
31 | 31 | |
|
32 | 32 | Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray. |
|
33 | 33 | All Rights Reserved.""" |
|
34 | 34 | |
|
35 | 35 | #**************************************************************************** |
|
36 | 36 | # Required modules |
|
37 | 37 | |
|
38 | 38 | # From the standard library |
|
39 | 39 | import __main__ |
|
40 | 40 | import __builtin__ |
|
41 | 41 | import os |
|
42 | 42 | import re |
|
43 | 43 | import sys |
|
44 | 44 | import types |
|
45 | 45 | from pprint import pprint,pformat |
|
46 | 46 | |
|
47 | 47 | # Our own |
|
48 | 48 | from IPython import DPyGetOpt |
|
49 | 49 | from IPython.Struct import Struct |
|
50 | 50 | from IPython.OutputTrap import OutputTrap |
|
51 | 51 | from IPython.ConfigLoader import ConfigLoader |
|
52 | 52 | from IPython.iplib import InteractiveShell |
|
53 | 53 | from IPython.usage import cmd_line_usage,interactive_usage |
|
54 | 54 | from IPython.genutils import * |
|
55 | 55 | |
|
56 | 56 | #----------------------------------------------------------------------------- |
|
57 | 57 | def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1, |
|
58 | 58 | rc_override=None,shell_class=InteractiveShell, |
|
59 | 59 | embedded=False,**kw): |
|
60 | 60 | """This is a dump of IPython into a single function. |
|
61 | 61 | |
|
62 | 62 | Later it will have to be broken up in a sensible manner. |
|
63 | 63 | |
|
64 | 64 | Arguments: |
|
65 | 65 | |
|
66 | 66 | - argv: a list similar to sys.argv[1:]. It should NOT contain the desired |
|
67 | 67 | script name, b/c DPyGetOpt strips the first argument only for the real |
|
68 | 68 | sys.argv. |
|
69 | 69 | |
|
70 | 70 | - user_ns: a dict to be used as the user's namespace.""" |
|
71 | 71 | |
|
72 | 72 | #---------------------------------------------------------------------- |
|
73 | 73 | # Defaults and initialization |
|
74 | 74 | |
|
75 | 75 | # For developer debugging, deactivates crash handler and uses pdb. |
|
76 | 76 | DEVDEBUG = False |
|
77 | 77 | |
|
78 | 78 | if argv is None: |
|
79 | 79 | argv = sys.argv |
|
80 | 80 | |
|
81 | 81 | # __IP is the main global that lives throughout and represents the whole |
|
82 | 82 | # application. If the user redefines it, all bets are off as to what |
|
83 | 83 | # happens. |
|
84 | 84 | |
|
85 | 85 | # __IP is the name of he global which the caller will have accessible as |
|
86 | 86 | # __IP.name. We set its name via the first parameter passed to |
|
87 | 87 | # InteractiveShell: |
|
88 | 88 | |
|
89 | 89 | IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns, |
|
90 | 90 | embedded=embedded,**kw) |
|
91 | 91 | |
|
92 | 92 | # Put 'help' in the user namespace |
|
93 | 93 | from site import _Helper |
|
94 | 94 | IP.user_ns['help'] = _Helper() |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | if DEVDEBUG: |
|
98 | 98 | # For developer debugging only (global flag) |
|
99 | 99 | from IPython import ultraTB |
|
100 | 100 | sys.excepthook = ultraTB.VerboseTB(call_pdb=1) |
|
101 | 101 | |
|
102 | 102 | IP.BANNER_PARTS = ['Python %s\n' |
|
103 | 103 | 'Type "copyright", "credits" or "license" ' |
|
104 | 104 | 'for more information.\n' |
|
105 | 105 | % (sys.version.split('\n')[0],), |
|
106 | 106 | "IPython %s -- An enhanced Interactive Python." |
|
107 | 107 | % (__version__,), |
|
108 | 108 | """? -> Introduction to IPython's features. |
|
109 | 109 | %magic -> Information about IPython's 'magic' % functions. |
|
110 | 110 | help -> Python's own help system. |
|
111 | 111 | object? -> Details about 'object'. ?object also works, ?? prints more. |
|
112 | 112 | """ ] |
|
113 | 113 | |
|
114 | 114 | IP.usage = interactive_usage |
|
115 | 115 | |
|
116 | 116 | # Platform-dependent suffix and directory names. We use _ipython instead |
|
117 | 117 | # of .ipython under win32 b/c there's software that breaks with .named |
|
118 | 118 | # directories on that platform. |
|
119 | 119 | if os.name == 'posix': |
|
120 | 120 | rc_suffix = '' |
|
121 | 121 | ipdir_def = '.ipython' |
|
122 | 122 | else: |
|
123 | 123 | rc_suffix = '.ini' |
|
124 | 124 | ipdir_def = '_ipython' |
|
125 | 125 | |
|
126 | 126 | # default directory for configuration |
|
127 | 127 | ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
128 | 128 | os.path.join(IP.home_dir,ipdir_def))) |
|
129 | 129 | |
|
130 | 130 | # we need the directory where IPython itself is installed |
|
131 | 131 | import IPython |
|
132 | 132 | IPython_dir = os.path.dirname(IPython.__file__) |
|
133 | 133 | del IPython |
|
134 | 134 | |
|
135 | 135 | #------------------------------------------------------------------------- |
|
136 | 136 | # Command line handling |
|
137 | 137 | |
|
138 | 138 | # Valid command line options (uses DPyGetOpt syntax, like Perl's |
|
139 | 139 | # GetOpt::Long) |
|
140 | 140 | |
|
141 | 141 | # Any key not listed here gets deleted even if in the file (like session |
|
142 | 142 | # or profile). That's deliberate, to maintain the rc namespace clean. |
|
143 | 143 | |
|
144 | 144 | # Each set of options appears twice: under _conv only the names are |
|
145 | 145 | # listed, indicating which type they must be converted to when reading the |
|
146 | 146 | # ipythonrc file. And under DPyGetOpt they are listed with the regular |
|
147 | 147 | # DPyGetOpt syntax (=s,=i,:f,etc). |
|
148 | 148 | |
|
149 | 149 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
150 | 150 | cmdline_opts = ('autocall=i autoindent! automagic! banner! cache_size|cs=i ' |
|
151 | 151 | 'c=s classic|cl color_info! colors=s confirm_exit! ' |
|
152 | 152 | 'debug! deep_reload! editor=s log|l messages! nosep pdb! ' |
|
153 | 153 | 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s ' |
|
154 | 154 | 'quick screen_length|sl=i prompts_pad_left=i ' |
|
155 | 155 | 'logfile|lf=s logplay|lp=s profile|p=s ' |
|
156 | 156 | 'readline! readline_merge_completions! ' |
|
157 | 157 | 'readline_omit__names! ' |
|
158 | 158 | 'rcfile=s separate_in|si=s separate_out|so=s ' |
|
159 | 159 | 'separate_out2|so2=s xmode=s wildcards_case_sensitive! ' |
|
160 | 160 | 'magic_docstrings system_verbose! ' |
|
161 | 161 | 'multi_line_specials! ' |
|
162 | 162 | 'autoedit_syntax!') |
|
163 | 163 | |
|
164 | 164 | # Options that can *only* appear at the cmd line (not in rcfiles). |
|
165 | 165 | |
|
166 | 166 | # The "ignore" option is a kludge so that Emacs buffers don't crash, since |
|
167 | 167 | # the 'C-c !' command in emacs automatically appends a -i option at the end. |
|
168 | 168 | cmdline_only = ('help ignore|i ipythondir=s Version upgrade ' |
|
169 | 169 | 'gthread! qthread! wthread! pylab! tk!') |
|
170 | 170 | |
|
171 | 171 | # Build the actual name list to be used by DPyGetOpt |
|
172 | 172 | opts_names = qw(cmdline_opts) + qw(cmdline_only) |
|
173 | 173 | |
|
174 | 174 | # Set sensible command line defaults. |
|
175 | 175 | # This should have everything from cmdline_opts and cmdline_only |
|
176 | 176 | opts_def = Struct(autocall = 1, |
|
177 | 177 | autoedit_syntax = 1, |
|
178 | 178 | autoindent=0, |
|
179 | 179 | automagic = 1, |
|
180 | 180 | banner = 1, |
|
181 | 181 | cache_size = 1000, |
|
182 | 182 | c = '', |
|
183 | 183 | classic = 0, |
|
184 | 184 | colors = 'NoColor', |
|
185 | 185 | color_info = 0, |
|
186 | 186 | confirm_exit = 1, |
|
187 | 187 | debug = 0, |
|
188 | 188 | deep_reload = 0, |
|
189 | 189 | editor = '0', |
|
190 | 190 | help = 0, |
|
191 | 191 | ignore = 0, |
|
192 | 192 | ipythondir = ipythondir, |
|
193 | 193 | log = 0, |
|
194 | 194 | logfile = '', |
|
195 | 195 | logplay = '', |
|
196 | 196 | multi_line_specials = 1, |
|
197 | 197 | messages = 1, |
|
198 | 198 | nosep = 0, |
|
199 | 199 | pdb = 0, |
|
200 | 200 | pprint = 0, |
|
201 | 201 | profile = '', |
|
202 | 202 | prompt_in1 = 'In [\\#]: ', |
|
203 | 203 | prompt_in2 = ' .\\D.: ', |
|
204 | 204 | prompt_out = 'Out[\\#]: ', |
|
205 | 205 | prompts_pad_left = 1, |
|
206 | 206 | quick = 0, |
|
207 | 207 | readline = 1, |
|
208 | 208 | readline_merge_completions = 1, |
|
209 | 209 | readline_omit__names = 0, |
|
210 | 210 | rcfile = 'ipythonrc' + rc_suffix, |
|
211 | 211 | screen_length = 0, |
|
212 | 212 | separate_in = '\n', |
|
213 | 213 | separate_out = '\n', |
|
214 | 214 | separate_out2 = '', |
|
215 | 215 | system_verbose = 0, |
|
216 | 216 | gthread = 0, |
|
217 | 217 | qthread = 0, |
|
218 | 218 | wthread = 0, |
|
219 | 219 | pylab = 0, |
|
220 | 220 | tk = 0, |
|
221 | 221 | upgrade = 0, |
|
222 | 222 | Version = 0, |
|
223 | 223 | xmode = 'Verbose', |
|
224 | 224 | wildcards_case_sensitive = 1, |
|
225 | 225 | magic_docstrings = 0, # undocumented, for doc generation |
|
226 | 226 | ) |
|
227 | 227 | |
|
228 | 228 | # Things that will *only* appear in rcfiles (not at the command line). |
|
229 | 229 | # Make sure there's a space before each end of line (they get auto-joined!) |
|
230 | 230 | rcfile_opts = { qwflat: 'include import_mod import_all execfile ', |
|
231 | 231 | qw_lol: 'import_some ', |
|
232 | 232 | # for things with embedded whitespace: |
|
233 | 233 | list_strings:'execute alias readline_parse_and_bind ', |
|
234 | 234 | # Regular strings need no conversion: |
|
235 | 235 | None:'readline_remove_delims ', |
|
236 | 236 | } |
|
237 | 237 | # Default values for these |
|
238 | 238 | rc_def = Struct(include = [], |
|
239 | 239 | import_mod = [], |
|
240 | 240 | import_all = [], |
|
241 | 241 | import_some = [[]], |
|
242 | 242 | execute = [], |
|
243 | 243 | execfile = [], |
|
244 | 244 | alias = [], |
|
245 | 245 | readline_parse_and_bind = [], |
|
246 | 246 | readline_remove_delims = '', |
|
247 | 247 | ) |
|
248 | 248 | |
|
249 | 249 | # Build the type conversion dictionary from the above tables: |
|
250 | 250 | typeconv = rcfile_opts.copy() |
|
251 | 251 | typeconv.update(optstr2types(cmdline_opts)) |
|
252 | 252 | |
|
253 | 253 | # FIXME: the None key appears in both, put that back together by hand. Ugly! |
|
254 | 254 | typeconv[None] += ' ' + rcfile_opts[None] |
|
255 | 255 | |
|
256 | 256 | # Remove quotes at ends of all strings (used to protect spaces) |
|
257 | 257 | typeconv[unquote_ends] = typeconv[None] |
|
258 | 258 | del typeconv[None] |
|
259 | 259 | |
|
260 | 260 | # Build the list we'll use to make all config decisions with defaults: |
|
261 | 261 | opts_all = opts_def.copy() |
|
262 | 262 | opts_all.update(rc_def) |
|
263 | 263 | |
|
264 | 264 | # Build conflict resolver for recursive loading of config files: |
|
265 | 265 | # - preserve means the outermost file maintains the value, it is not |
|
266 | 266 | # overwritten if an included file has the same key. |
|
267 | 267 | # - add_flip applies + to the two values, so it better make sense to add |
|
268 | 268 | # those types of keys. But it flips them first so that things loaded |
|
269 | 269 | # deeper in the inclusion chain have lower precedence. |
|
270 | 270 | conflict = {'preserve': ' '.join([ typeconv[int], |
|
271 | 271 | typeconv[unquote_ends] ]), |
|
272 | 272 | 'add_flip': ' '.join([ typeconv[qwflat], |
|
273 | 273 | typeconv[qw_lol], |
|
274 | 274 | typeconv[list_strings] ]) |
|
275 | 275 | } |
|
276 | 276 | |
|
277 | 277 | # Now actually process the command line |
|
278 | 278 | getopt = DPyGetOpt.DPyGetOpt() |
|
279 | 279 | getopt.setIgnoreCase(0) |
|
280 | 280 | |
|
281 | 281 | getopt.parseConfiguration(opts_names) |
|
282 | 282 | |
|
283 | 283 | try: |
|
284 | 284 | getopt.processArguments(argv) |
|
285 | 285 | except: |
|
286 | 286 | print cmd_line_usage |
|
287 | 287 | warn('\nError in Arguments: ' + `sys.exc_value`) |
|
288 | 288 | sys.exit(1) |
|
289 | 289 | |
|
290 | 290 | # convert the options dict to a struct for much lighter syntax later |
|
291 | 291 | opts = Struct(getopt.optionValues) |
|
292 | 292 | args = getopt.freeValues |
|
293 | 293 | |
|
294 | 294 | # this is the struct (which has default values at this point) with which |
|
295 | 295 | # we make all decisions: |
|
296 | 296 | opts_all.update(opts) |
|
297 | 297 | |
|
298 | 298 | # Options that force an immediate exit |
|
299 | 299 | if opts_all.help: |
|
300 | 300 | page(cmd_line_usage) |
|
301 | 301 | sys.exit() |
|
302 | 302 | |
|
303 | 303 | if opts_all.Version: |
|
304 | 304 | print __version__ |
|
305 | 305 | sys.exit() |
|
306 | 306 | |
|
307 | 307 | if opts_all.magic_docstrings: |
|
308 | 308 | IP.magic_magic('-latex') |
|
309 | 309 | sys.exit() |
|
310 | 310 | |
|
311 | 311 | # Create user config directory if it doesn't exist. This must be done |
|
312 | 312 | # *after* getting the cmd line options. |
|
313 | 313 | if not os.path.isdir(opts_all.ipythondir): |
|
314 | 314 | IP.user_setup(opts_all.ipythondir,rc_suffix,'install') |
|
315 | 315 | |
|
316 | 316 | # upgrade user config files while preserving a copy of the originals |
|
317 | 317 | if opts_all.upgrade: |
|
318 | 318 | IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade') |
|
319 | 319 | |
|
320 | 320 | # check mutually exclusive options in the *original* command line |
|
321 | 321 | mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'), |
|
322 | 322 | qw('classic profile'),qw('classic rcfile')]) |
|
323 | 323 | |
|
324 | 324 | #--------------------------------------------------------------------------- |
|
325 | 325 | # Log replay |
|
326 | 326 | |
|
327 | 327 | # if -logplay, we need to 'become' the other session. That basically means |
|
328 | 328 | # replacing the current command line environment with that of the old |
|
329 | 329 | # session and moving on. |
|
330 | 330 | |
|
331 | 331 | # this is needed so that later we know we're in session reload mode, as |
|
332 | 332 | # opts_all will get overwritten: |
|
333 | 333 | load_logplay = 0 |
|
334 | 334 | |
|
335 | 335 | if opts_all.logplay: |
|
336 | 336 | load_logplay = opts_all.logplay |
|
337 | 337 | opts_debug_save = opts_all.debug |
|
338 | 338 | try: |
|
339 | 339 | logplay = open(opts_all.logplay) |
|
340 | 340 | except IOError: |
|
341 | 341 | if opts_all.debug: IP.InteractiveTB() |
|
342 | 342 | warn('Could not open logplay file '+`opts_all.logplay`) |
|
343 | 343 | # restore state as if nothing had happened and move on, but make |
|
344 | 344 | # sure that later we don't try to actually load the session file |
|
345 | 345 | logplay = None |
|
346 | 346 | load_logplay = 0 |
|
347 | 347 | del opts_all.logplay |
|
348 | 348 | else: |
|
349 | 349 | try: |
|
350 | 350 | logplay.readline() |
|
351 | 351 | logplay.readline(); |
|
352 | 352 | # this reloads that session's command line |
|
353 | 353 | cmd = logplay.readline()[6:] |
|
354 | 354 | exec cmd |
|
355 | 355 | # restore the true debug flag given so that the process of |
|
356 | 356 | # session loading itself can be monitored. |
|
357 | 357 | opts.debug = opts_debug_save |
|
358 | 358 | # save the logplay flag so later we don't overwrite the log |
|
359 | 359 | opts.logplay = load_logplay |
|
360 | 360 | # now we must update our own structure with defaults |
|
361 | 361 | opts_all.update(opts) |
|
362 | 362 | # now load args |
|
363 | 363 | cmd = logplay.readline()[6:] |
|
364 | 364 | exec cmd |
|
365 | 365 | logplay.close() |
|
366 | 366 | except: |
|
367 | 367 | logplay.close() |
|
368 | 368 | if opts_all.debug: IP.InteractiveTB() |
|
369 | 369 | warn("Logplay file lacking full configuration information.\n" |
|
370 | 370 | "I'll try to read it, but some things may not work.") |
|
371 | 371 | |
|
372 | 372 | #------------------------------------------------------------------------- |
|
373 | 373 | # set up output traps: catch all output from files, being run, modules |
|
374 | 374 | # loaded, etc. Then give it to the user in a clean form at the end. |
|
375 | 375 | |
|
376 | 376 | msg_out = 'Output messages. ' |
|
377 | 377 | msg_err = 'Error messages. ' |
|
378 | 378 | msg_sep = '\n' |
|
379 | 379 | msg = Struct(config = OutputTrap('Configuration Loader',msg_out, |
|
380 | 380 | msg_err,msg_sep,debug, |
|
381 | 381 | quiet_out=1), |
|
382 | 382 | user_exec = OutputTrap('User File Execution',msg_out, |
|
383 | 383 | msg_err,msg_sep,debug), |
|
384 | 384 | logplay = OutputTrap('Log Loader',msg_out, |
|
385 | 385 | msg_err,msg_sep,debug), |
|
386 | 386 | summary = '' |
|
387 | 387 | ) |
|
388 | 388 | |
|
389 | 389 | #------------------------------------------------------------------------- |
|
390 | 390 | # Process user ipythonrc-type configuration files |
|
391 | 391 | |
|
392 | 392 | # turn on output trapping and log to msg.config |
|
393 | 393 | # remember that with debug on, trapping is actually disabled |
|
394 | 394 | msg.config.trap_all() |
|
395 | 395 | |
|
396 | 396 | # look for rcfile in current or default directory |
|
397 | 397 | try: |
|
398 | 398 | opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir) |
|
399 | 399 | except IOError: |
|
400 | 400 | if opts_all.debug: IP.InteractiveTB() |
|
401 | 401 | warn('Configuration file %s not found. Ignoring request.' |
|
402 | 402 | % (opts_all.rcfile) ) |
|
403 | 403 | |
|
404 | 404 | # 'profiles' are a shorthand notation for config filenames |
|
405 | 405 | if opts_all.profile: |
|
406 | 406 | try: |
|
407 | 407 | opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile |
|
408 | 408 | + rc_suffix, |
|
409 | 409 | opts_all.ipythondir) |
|
410 | 410 | except IOError: |
|
411 | 411 | if opts_all.debug: IP.InteractiveTB() |
|
412 | 412 | opts.profile = '' # remove profile from options if invalid |
|
413 | 413 | warn('Profile configuration file %s not found. Ignoring request.' |
|
414 | 414 | % (opts_all.profile) ) |
|
415 | 415 | |
|
416 | 416 | # load the config file |
|
417 | 417 | rcfiledata = None |
|
418 | 418 | if opts_all.quick: |
|
419 | 419 | print 'Launching IPython in quick mode. No config file read.' |
|
420 | 420 | elif opts_all.classic: |
|
421 | 421 | print 'Launching IPython in classic mode. No config file read.' |
|
422 | 422 | elif opts_all.rcfile: |
|
423 | 423 | try: |
|
424 | 424 | cfg_loader = ConfigLoader(conflict) |
|
425 | 425 | rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv, |
|
426 | 426 | 'include',opts_all.ipythondir, |
|
427 | 427 | purge = 1, |
|
428 | 428 | unique = conflict['preserve']) |
|
429 | 429 | except: |
|
430 | 430 | IP.InteractiveTB() |
|
431 | 431 | warn('Problems loading configuration file '+ |
|
432 | 432 | `opts_all.rcfile`+ |
|
433 | 433 | '\nStarting with default -bare bones- configuration.') |
|
434 | 434 | else: |
|
435 | 435 | warn('No valid configuration file found in either currrent directory\n'+ |
|
436 | 436 | 'or in the IPython config. directory: '+`opts_all.ipythondir`+ |
|
437 | 437 | '\nProceeding with internal defaults.') |
|
438 | 438 | |
|
439 | 439 | #------------------------------------------------------------------------ |
|
440 | 440 | # Set exception handlers in mode requested by user. |
|
441 | 441 | otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode |
|
442 | 442 | IP.magic_xmode(opts_all.xmode) |
|
443 | 443 | otrap.release_out() |
|
444 | 444 | |
|
445 | 445 | #------------------------------------------------------------------------ |
|
446 | 446 | # Execute user config |
|
447 | 447 | |
|
448 | 448 | # Create a valid config structure with the right precedence order: |
|
449 | 449 | # defaults < rcfile < command line. This needs to be in the instance, so |
|
450 | 450 | # that method calls below that rely on it find it. |
|
451 | 451 | IP.rc = rc_def.copy() |
|
452 | 452 | |
|
453 | 453 | # Work with a local alias inside this routine to avoid unnecessary |
|
454 | 454 | # attribute lookups. |
|
455 | 455 | IP_rc = IP.rc |
|
456 | 456 | |
|
457 | 457 | IP_rc.update(opts_def) |
|
458 | 458 | if rcfiledata: |
|
459 | 459 | # now we can update |
|
460 | 460 | IP_rc.update(rcfiledata) |
|
461 | 461 | IP_rc.update(opts) |
|
462 | 462 | IP_rc.update(rc_override) |
|
463 | 463 | |
|
464 | 464 | # Store the original cmd line for reference: |
|
465 | 465 | IP_rc.opts = opts |
|
466 | 466 | IP_rc.args = args |
|
467 | 467 | |
|
468 | 468 | # create a *runtime* Struct like rc for holding parameters which may be |
|
469 | 469 | # created and/or modified by runtime user extensions. |
|
470 | 470 | IP.runtime_rc = Struct() |
|
471 | 471 | |
|
472 | 472 | # from this point on, all config should be handled through IP_rc, |
|
473 | 473 | # opts* shouldn't be used anymore. |
|
474 | 474 | |
|
475 | 475 | # add personal .ipython dir to sys.path so that users can put things in |
|
476 | 476 | # there for customization |
|
477 | 477 | sys.path.append(IP_rc.ipythondir) |
|
478 | 478 | sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran |
|
479 | 479 | |
|
480 | 480 | # update IP_rc with some special things that need manual |
|
481 | 481 | # tweaks. Basically options which affect other options. I guess this |
|
482 | 482 | # should just be written so that options are fully orthogonal and we |
|
483 | 483 | # wouldn't worry about this stuff! |
|
484 | 484 | |
|
485 | 485 | if IP_rc.classic: |
|
486 | 486 | IP_rc.quick = 1 |
|
487 | 487 | IP_rc.cache_size = 0 |
|
488 | 488 | IP_rc.pprint = 0 |
|
489 | 489 | IP_rc.prompt_in1 = '>>> ' |
|
490 | 490 | IP_rc.prompt_in2 = '... ' |
|
491 | 491 | IP_rc.prompt_out = '' |
|
492 | 492 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
493 | 493 | IP_rc.colors = 'NoColor' |
|
494 | 494 | IP_rc.xmode = 'Plain' |
|
495 | 495 | |
|
496 | 496 | # configure readline |
|
497 | 497 | # Define the history file for saving commands in between sessions |
|
498 | 498 | if IP_rc.profile: |
|
499 | 499 | histfname = 'history-%s' % IP_rc.profile |
|
500 | 500 | else: |
|
501 | 501 | histfname = 'history' |
|
502 | 502 | IP.histfile = os.path.join(opts_all.ipythondir,histfname) |
|
503 | 503 | |
|
504 | 504 | # update exception handlers with rc file status |
|
505 | 505 | otrap.trap_out() # I don't want these messages ever. |
|
506 | 506 | IP.magic_xmode(IP_rc.xmode) |
|
507 | 507 | otrap.release_out() |
|
508 | 508 | |
|
509 | 509 | # activate logging if requested and not reloading a log |
|
510 | 510 | if IP_rc.logplay: |
|
511 | 511 | IP.magic_logstart(IP_rc.logplay + ' append') |
|
512 | 512 | elif IP_rc.logfile: |
|
513 | 513 | IP.magic_logstart(IP_rc.logfile) |
|
514 | 514 | elif IP_rc.log: |
|
515 | 515 | IP.magic_logstart() |
|
516 | 516 | |
|
517 | 517 | # find user editor so that it we don't have to look it up constantly |
|
518 | 518 | if IP_rc.editor.strip()=='0': |
|
519 | 519 | try: |
|
520 | 520 | ed = os.environ['EDITOR'] |
|
521 | 521 | except KeyError: |
|
522 | 522 | if os.name == 'posix': |
|
523 | 523 | ed = 'vi' # the only one guaranteed to be there! |
|
524 | 524 | else: |
|
525 | 525 | ed = 'notepad' # same in Windows! |
|
526 | 526 | IP_rc.editor = ed |
|
527 | 527 | |
|
528 | 528 | # Keep track of whether this is an embedded instance or not (useful for |
|
529 | 529 | # post-mortems). |
|
530 | 530 | IP_rc.embedded = IP.embedded |
|
531 | 531 | |
|
532 | 532 | # Recursive reload |
|
533 | 533 | try: |
|
534 | 534 | from IPython import deep_reload |
|
535 | 535 | if IP_rc.deep_reload: |
|
536 | 536 | __builtin__.reload = deep_reload.reload |
|
537 | 537 | else: |
|
538 | 538 | __builtin__.dreload = deep_reload.reload |
|
539 | 539 | del deep_reload |
|
540 | 540 | except ImportError: |
|
541 | 541 | pass |
|
542 | 542 | |
|
543 | 543 | # Save the current state of our namespace so that the interactive shell |
|
544 | 544 | # can later know which variables have been created by us from config files |
|
545 | 545 | # and loading. This way, loading a file (in any way) is treated just like |
|
546 | 546 | # defining things on the command line, and %who works as expected. |
|
547 | 547 | |
|
548 | 548 | # DON'T do anything that affects the namespace beyond this point! |
|
549 | 549 | IP.internal_ns.update(__main__.__dict__) |
|
550 | 550 | |
|
551 | 551 | #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who |
|
552 | 552 | |
|
553 | 553 | # Now run through the different sections of the users's config |
|
554 | 554 | if IP_rc.debug: |
|
555 | 555 | print 'Trying to execute the following configuration structure:' |
|
556 | 556 | print '(Things listed first are deeper in the inclusion tree and get' |
|
557 | 557 | print 'loaded first).\n' |
|
558 | 558 | pprint(IP_rc.__dict__) |
|
559 | 559 | |
|
560 | 560 | for mod in IP_rc.import_mod: |
|
561 | 561 | try: |
|
562 | 562 | exec 'import '+mod in IP.user_ns |
|
563 | 563 | except : |
|
564 | 564 | IP.InteractiveTB() |
|
565 | 565 | import_fail_info(mod) |
|
566 | 566 | |
|
567 | 567 | for mod_fn in IP_rc.import_some: |
|
568 | 568 | if mod_fn == []: break |
|
569 | 569 | mod,fn = mod_fn[0],','.join(mod_fn[1:]) |
|
570 | 570 | try: |
|
571 | 571 | exec 'from '+mod+' import '+fn in IP.user_ns |
|
572 | 572 | except : |
|
573 | 573 | IP.InteractiveTB() |
|
574 | 574 | import_fail_info(mod,fn) |
|
575 | 575 | |
|
576 | 576 | for mod in IP_rc.import_all: |
|
577 | 577 | try: |
|
578 | 578 | exec 'from '+mod+' import *' in IP.user_ns |
|
579 | 579 | except : |
|
580 | 580 | IP.InteractiveTB() |
|
581 | 581 | import_fail_info(mod) |
|
582 | 582 | |
|
583 | 583 | for code in IP_rc.execute: |
|
584 | 584 | try: |
|
585 | 585 | exec code in IP.user_ns |
|
586 | 586 | except: |
|
587 | 587 | IP.InteractiveTB() |
|
588 | 588 | warn('Failure executing code: ' + `code`) |
|
589 | 589 | |
|
590 | 590 | # Execute the files the user wants in ipythonrc |
|
591 | 591 | for file in IP_rc.execfile: |
|
592 | 592 | try: |
|
593 | 593 | file = filefind(file,sys.path+[IPython_dir]) |
|
594 | 594 | except IOError: |
|
595 | 595 | warn(itpl('File $file not found. Skipping it.')) |
|
596 | 596 | else: |
|
597 | 597 | IP.safe_execfile(os.path.expanduser(file),IP.user_ns) |
|
598 | 598 | |
|
599 | 599 | # release stdout and stderr and save config log into a global summary |
|
600 | 600 | msg.config.release_all() |
|
601 | 601 | if IP_rc.messages: |
|
602 | 602 | msg.summary += msg.config.summary_all() |
|
603 | 603 | |
|
604 | 604 | #------------------------------------------------------------------------ |
|
605 | 605 | # Setup interactive session |
|
606 | 606 | |
|
607 | 607 | # Now we should be fully configured. We can then execute files or load |
|
608 | 608 | # things only needed for interactive use. Then we'll open the shell. |
|
609 | 609 | |
|
610 | 610 | # Take a snapshot of the user namespace before opening the shell. That way |
|
611 | 611 | # we'll be able to identify which things were interactively defined and |
|
612 | 612 | # which were defined through config files. |
|
613 | 613 | IP.user_config_ns = IP.user_ns.copy() |
|
614 | 614 | |
|
615 | 615 | # Force reading a file as if it were a session log. Slower but safer. |
|
616 | 616 | if load_logplay: |
|
617 | 617 | print 'Replaying log...' |
|
618 | 618 | try: |
|
619 | 619 | if IP_rc.debug: |
|
620 | 620 | logplay_quiet = 0 |
|
621 | 621 | else: |
|
622 | 622 | logplay_quiet = 1 |
|
623 | 623 | |
|
624 | 624 | msg.logplay.trap_all() |
|
625 | 625 | IP.safe_execfile(load_logplay,IP.user_ns, |
|
626 | 626 | islog = 1, quiet = logplay_quiet) |
|
627 | 627 | msg.logplay.release_all() |
|
628 | 628 | if IP_rc.messages: |
|
629 | 629 | msg.summary += msg.logplay.summary_all() |
|
630 | 630 | except: |
|
631 | 631 | warn('Problems replaying logfile %s.' % load_logplay) |
|
632 | 632 | IP.InteractiveTB() |
|
633 | 633 | |
|
634 | 634 | # Load remaining files in command line |
|
635 | 635 | msg.user_exec.trap_all() |
|
636 | 636 | |
|
637 | 637 | # Do NOT execute files named in the command line as scripts to be loaded |
|
638 | 638 | # by embedded instances. Doing so has the potential for an infinite |
|
639 | 639 | # recursion if there are exceptions thrown in the process. |
|
640 | 640 | |
|
641 | 641 | # XXX FIXME: the execution of user files should be moved out to after |
|
642 | 642 | # ipython is fully initialized, just as if they were run via %run at the |
|
643 | 643 | # ipython prompt. This would also give them the benefit of ipython's |
|
644 | 644 | # nice tracebacks. |
|
645 | 645 | |
|
646 | 646 | if not embedded and IP_rc.args: |
|
647 | 647 | name_save = IP.user_ns['__name__'] |
|
648 | 648 | IP.user_ns['__name__'] = '__main__' |
|
649 | 649 | try: |
|
650 | 650 | # Set our own excepthook in case the user code tries to call it |
|
651 | 651 | # directly. This prevents triggering the IPython crash handler. |
|
652 | 652 | old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook |
|
653 | 653 | for run in args: |
|
654 | 654 | IP.safe_execfile(run,IP.user_ns) |
|
655 | 655 | finally: |
|
656 | 656 | # Reset our crash handler in place |
|
657 | 657 | sys.excepthook = old_excepthook |
|
658 | 658 | |
|
659 | 659 | IP.user_ns['__name__'] = name_save |
|
660 | 660 | |
|
661 | 661 | msg.user_exec.release_all() |
|
662 | 662 | if IP_rc.messages: |
|
663 | 663 | msg.summary += msg.user_exec.summary_all() |
|
664 | 664 | |
|
665 | 665 | # since we can't specify a null string on the cmd line, 0 is the equivalent: |
|
666 | 666 | if IP_rc.nosep: |
|
667 | 667 | IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0' |
|
668 | 668 | if IP_rc.separate_in == '0': IP_rc.separate_in = '' |
|
669 | 669 | if IP_rc.separate_out == '0': IP_rc.separate_out = '' |
|
670 | 670 | if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = '' |
|
671 | 671 | IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n') |
|
672 | 672 | IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n') |
|
673 | 673 | IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n') |
|
674 | 674 | |
|
675 | 675 | # Determine how many lines at the bottom of the screen are needed for |
|
676 | 676 | # showing prompts, so we can know wheter long strings are to be printed or |
|
677 | 677 | # paged: |
|
678 | 678 | num_lines_bot = IP_rc.separate_in.count('\n')+1 |
|
679 | 679 | IP_rc.screen_length = IP_rc.screen_length - num_lines_bot |
|
680 | 680 | |
|
681 | 681 | # configure startup banner |
|
682 | 682 | if IP_rc.c: # regular python doesn't print the banner with -c |
|
683 | 683 | IP_rc.banner = 0 |
|
684 | 684 | if IP_rc.banner: |
|
685 | 685 | BANN_P = IP.BANNER_PARTS |
|
686 | 686 | else: |
|
687 | 687 | BANN_P = [] |
|
688 | 688 | |
|
689 | 689 | if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile) |
|
690 | 690 | |
|
691 | 691 | # add message log (possibly empty) |
|
692 | 692 | if msg.summary: BANN_P.append(msg.summary) |
|
693 | 693 | # Final banner is a string |
|
694 | 694 | IP.BANNER = '\n'.join(BANN_P) |
|
695 | 695 | |
|
696 | 696 | # Finalize the IPython instance. This assumes the rc structure is fully |
|
697 | 697 | # in place. |
|
698 | 698 | IP.post_config_initialization() |
|
699 | 699 | |
|
700 | 700 | return IP |
|
701 | 701 | #************************ end of file <ipmaker.py> ************************** |
@@ -1,858 +1,857 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | ultraTB.py -- Spice up your tracebacks! |
|
4 | 4 | |
|
5 | 5 | * ColorTB |
|
6 | 6 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | 7 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | 8 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | 9 | text editor. |
|
10 | 10 | |
|
11 | 11 | Installation instructions for ColorTB: |
|
12 | 12 | import sys,ultraTB |
|
13 | 13 | sys.excepthook = ultraTB.ColorTB() |
|
14 | 14 | |
|
15 | 15 | * VerboseTB |
|
16 | 16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
17 | 17 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
18 | 18 | and intended it for CGI programmers, but why should they have all the fun? I |
|
19 | 19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
20 | 20 | but kind of neat, and maybe useful for long-running programs that you believe |
|
21 | 21 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
22 | 22 | Give it a shot--you'll love it or you'll hate it. |
|
23 | 23 | |
|
24 | 24 | Note: |
|
25 | 25 | |
|
26 | 26 | The Verbose mode prints the variables currently visible where the exception |
|
27 | 27 | happened (shortening their strings if too long). This can potentially be |
|
28 | 28 | very slow, if you happen to have a huge data structure whose string |
|
29 | 29 | representation is complex to compute. Your computer may appear to freeze for |
|
30 | 30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
31 | 31 | with Ctrl-C (maybe hitting it more than once). |
|
32 | 32 | |
|
33 | 33 | If you encounter this kind of situation often, you may want to use the |
|
34 | 34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
35 | 35 | variables (but otherwise includes the information and context given by |
|
36 | 36 | Verbose). |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | Installation instructions for ColorTB: |
|
40 | 40 | import sys,ultraTB |
|
41 | 41 | sys.excepthook = ultraTB.VerboseTB() |
|
42 | 42 | |
|
43 | 43 | Note: Much of the code in this module was lifted verbatim from the standard |
|
44 | 44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
45 | 45 | |
|
46 | 46 | * Color schemes |
|
47 | 47 | The colors are defined in the class TBTools through the use of the |
|
48 | 48 | ColorSchemeTable class. Currently the following exist: |
|
49 | 49 | |
|
50 | 50 | - NoColor: allows all of this module to be used in any terminal (the color |
|
51 | 51 | escapes are just dummy blank strings). |
|
52 | 52 | |
|
53 | 53 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
54 | 54 | or very dark background). |
|
55 | 55 | |
|
56 | 56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
57 | 57 | in light background terminals. |
|
58 | 58 | |
|
59 | 59 | You can implement other color schemes easily, the syntax is fairly |
|
60 | 60 | self-explanatory. Please send back new schemes you develop to the author for |
|
61 | 61 | possible inclusion in future releases. |
|
62 | 62 | |
|
63 |
$Id: ultraTB.py 99 |
|
|
63 | $Id: ultraTB.py 994 2006-01-08 08:29:44Z fperez $""" | |
|
64 | 64 | |
|
65 | 65 | #***************************************************************************** |
|
66 | 66 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
67 | 67 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
68 | 68 | # |
|
69 | 69 | # Distributed under the terms of the BSD License. The full license is in |
|
70 | 70 | # the file COPYING, distributed as part of this software. |
|
71 | 71 | #***************************************************************************** |
|
72 | 72 | |
|
73 | 73 | from IPython import Release |
|
74 | 74 | __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+ |
|
75 | 75 | Release.authors['Fernando']) |
|
76 | 76 | __license__ = Release.license |
|
77 | 77 | |
|
78 | 78 | # Required modules |
|
79 | 79 | import inspect |
|
80 | 80 | import keyword |
|
81 | 81 | import linecache |
|
82 | 82 | import os |
|
83 | 83 | import pydoc |
|
84 | 84 | import string |
|
85 | 85 | import sys |
|
86 | 86 | import time |
|
87 | 87 | import tokenize |
|
88 | 88 | import traceback |
|
89 | 89 | import types |
|
90 | 90 | |
|
91 | 91 | # IPython's own modules |
|
92 | 92 | # Modified pdb which doesn't damage IPython's readline handling |
|
93 | 93 | from IPython import Debugger |
|
94 | 94 | from IPython.Struct import Struct |
|
95 | 95 | from IPython.excolors import ExceptionColors |
|
96 | 96 | from IPython.genutils import Term,uniq_stable,error,info |
|
97 | 97 | |
|
98 | 98 | # Globals |
|
99 | 99 | # amount of space to put line numbers before verbose tracebacks |
|
100 | 100 | INDENT_SIZE = 8 |
|
101 | 101 | |
|
102 | 102 | #--------------------------------------------------------------------------- |
|
103 | 103 | # Code begins |
|
104 | 104 | |
|
105 | 105 | # Utility functions |
|
106 | 106 | def inspect_error(): |
|
107 | 107 | """Print a message about internal inspect errors. |
|
108 | 108 | |
|
109 | 109 | These are unfortunately quite common.""" |
|
110 | 110 | |
|
111 | 111 | error('Internal Python error in the inspect module.\n' |
|
112 | 112 | 'Below is the traceback from this internal error.\n') |
|
113 | 113 | |
|
114 | 114 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): |
|
115 | 115 | import linecache |
|
116 | 116 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
117 | 117 | |
|
118 | 118 | records = inspect.getinnerframes(etb, context) |
|
119 | 119 | |
|
120 | 120 | # If the error is at the console, don't build any context, since it would |
|
121 | 121 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
122 | 122 | # console) |
|
123 | 123 | rec_check = records[tb_offset:] |
|
124 | 124 | try: |
|
125 | 125 | rname = rec_check[0][1] |
|
126 | 126 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
127 | 127 | return rec_check |
|
128 | 128 | except IndexError: |
|
129 | 129 | pass |
|
130 | 130 | |
|
131 | 131 | aux = traceback.extract_tb(etb) |
|
132 | 132 | assert len(records) == len(aux) |
|
133 | 133 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
134 | 134 | maybeStart = lnum-1 - context//2 |
|
135 | 135 | start = max(maybeStart, 0) |
|
136 | 136 | end = start + context |
|
137 | 137 | lines = linecache.getlines(file)[start:end] |
|
138 | 138 | # pad with empty lines if necessary |
|
139 | 139 | if maybeStart < 0: |
|
140 | 140 | lines = (['\n'] * -maybeStart) + lines |
|
141 | 141 | if len(lines) < context: |
|
142 | 142 | lines += ['\n'] * (context - len(lines)) |
|
143 | assert len(lines) == context | |
|
144 | 143 | buf = list(records[i]) |
|
145 | 144 | buf[LNUM_POS] = lnum |
|
146 | 145 | buf[INDEX_POS] = lnum - 1 - start |
|
147 | 146 | buf[LINES_POS] = lines |
|
148 | 147 | records[i] = tuple(buf) |
|
149 | 148 | return records[tb_offset:] |
|
150 | 149 | |
|
151 | 150 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
152 | 151 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
153 | 152 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
154 | 153 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
155 | 154 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None): |
|
156 | 155 | numbers_width = INDENT_SIZE - 1 |
|
157 | 156 | res = [] |
|
158 | 157 | i = lnum - index |
|
159 | 158 | for line in lines: |
|
160 | 159 | if i == lnum: |
|
161 | 160 | # This is the line with the error |
|
162 | 161 | pad = numbers_width - len(str(i)) |
|
163 | 162 | if pad >= 3: |
|
164 | 163 | marker = '-'*(pad-3) + '-> ' |
|
165 | 164 | elif pad == 2: |
|
166 | 165 | marker = '> ' |
|
167 | 166 | elif pad == 1: |
|
168 | 167 | marker = '>' |
|
169 | 168 | else: |
|
170 | 169 | marker = '' |
|
171 | 170 | num = marker + str(i) |
|
172 | 171 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, |
|
173 | 172 | Colors.line, line, Colors.Normal) |
|
174 | 173 | else: |
|
175 | 174 | num = '%*s' % (numbers_width,i) |
|
176 | 175 | line = '%s%s%s %s' %(Colors.lineno, num, |
|
177 | 176 | Colors.Normal, line) |
|
178 | 177 | |
|
179 | 178 | res.append(line) |
|
180 | 179 | if lvals and i == lnum: |
|
181 | 180 | res.append(lvals + '\n') |
|
182 | 181 | i = i + 1 |
|
183 | 182 | return res |
|
184 | 183 | |
|
185 | 184 | #--------------------------------------------------------------------------- |
|
186 | 185 | # Module classes |
|
187 | 186 | class TBTools: |
|
188 | 187 | """Basic tools used by all traceback printer classes.""" |
|
189 | 188 | |
|
190 | 189 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): |
|
191 | 190 | # Whether to call the interactive pdb debugger after printing |
|
192 | 191 | # tracebacks or not |
|
193 | 192 | self.call_pdb = call_pdb |
|
194 | 193 | |
|
195 | 194 | # Create color table |
|
196 | 195 | self.color_scheme_table = ExceptionColors |
|
197 | 196 | |
|
198 | 197 | self.set_colors(color_scheme) |
|
199 | 198 | self.old_scheme = color_scheme # save initial value for toggles |
|
200 | 199 | |
|
201 | 200 | if call_pdb: |
|
202 | 201 | self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name) |
|
203 | 202 | else: |
|
204 | 203 | self.pdb = None |
|
205 | 204 | |
|
206 | 205 | def set_colors(self,*args,**kw): |
|
207 | 206 | """Shorthand access to the color table scheme selector method.""" |
|
208 | 207 | |
|
209 | 208 | self.color_scheme_table.set_active_scheme(*args,**kw) |
|
210 | 209 | # for convenience, set Colors to the active scheme |
|
211 | 210 | self.Colors = self.color_scheme_table.active_colors |
|
212 | 211 | |
|
213 | 212 | def color_toggle(self): |
|
214 | 213 | """Toggle between the currently active color scheme and NoColor.""" |
|
215 | 214 | |
|
216 | 215 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
217 | 216 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
218 | 217 | self.Colors = self.color_scheme_table.active_colors |
|
219 | 218 | else: |
|
220 | 219 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
221 | 220 | self.color_scheme_table.set_active_scheme('NoColor') |
|
222 | 221 | self.Colors = self.color_scheme_table.active_colors |
|
223 | 222 | |
|
224 | 223 | #--------------------------------------------------------------------------- |
|
225 | 224 | class ListTB(TBTools): |
|
226 | 225 | """Print traceback information from a traceback list, with optional color. |
|
227 | 226 | |
|
228 | 227 | Calling: requires 3 arguments: |
|
229 | 228 | (etype, evalue, elist) |
|
230 | 229 | as would be obtained by: |
|
231 | 230 | etype, evalue, tb = sys.exc_info() |
|
232 | 231 | if tb: |
|
233 | 232 | elist = traceback.extract_tb(tb) |
|
234 | 233 | else: |
|
235 | 234 | elist = None |
|
236 | 235 | |
|
237 | 236 | It can thus be used by programs which need to process the traceback before |
|
238 | 237 | printing (such as console replacements based on the code module from the |
|
239 | 238 | standard library). |
|
240 | 239 | |
|
241 | 240 | Because they are meant to be called without a full traceback (only a |
|
242 | 241 | list), instances of this class can't call the interactive pdb debugger.""" |
|
243 | 242 | |
|
244 | 243 | def __init__(self,color_scheme = 'NoColor'): |
|
245 | 244 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
246 | 245 | |
|
247 | 246 | def __call__(self, etype, value, elist): |
|
248 | 247 | print >> Term.cerr, self.text(etype,value,elist) |
|
249 | 248 | |
|
250 | 249 | def text(self,etype, value, elist,context=5): |
|
251 | 250 | """Return a color formatted string with the traceback info.""" |
|
252 | 251 | |
|
253 | 252 | Colors = self.Colors |
|
254 | 253 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] |
|
255 | 254 | if elist: |
|
256 | 255 | out_string.append('Traceback %s(most recent call last)%s:' % \ |
|
257 | 256 | (Colors.normalEm, Colors.Normal) + '\n') |
|
258 | 257 | out_string.extend(self._format_list(elist)) |
|
259 | 258 | lines = self._format_exception_only(etype, value) |
|
260 | 259 | for line in lines[:-1]: |
|
261 | 260 | out_string.append(" "+line) |
|
262 | 261 | out_string.append(lines[-1]) |
|
263 | 262 | return ''.join(out_string) |
|
264 | 263 | |
|
265 | 264 | def _format_list(self, extracted_list): |
|
266 | 265 | """Format a list of traceback entry tuples for printing. |
|
267 | 266 | |
|
268 | 267 | Given a list of tuples as returned by extract_tb() or |
|
269 | 268 | extract_stack(), return a list of strings ready for printing. |
|
270 | 269 | Each string in the resulting list corresponds to the item with the |
|
271 | 270 | same index in the argument list. Each string ends in a newline; |
|
272 | 271 | the strings may contain internal newlines as well, for those items |
|
273 | 272 | whose source text line is not None. |
|
274 | 273 | |
|
275 | 274 | Lifted almost verbatim from traceback.py |
|
276 | 275 | """ |
|
277 | 276 | |
|
278 | 277 | Colors = self.Colors |
|
279 | 278 | list = [] |
|
280 | 279 | for filename, lineno, name, line in extracted_list[:-1]: |
|
281 | 280 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
282 | 281 | (Colors.filename, filename, Colors.Normal, |
|
283 | 282 | Colors.lineno, lineno, Colors.Normal, |
|
284 | 283 | Colors.name, name, Colors.Normal) |
|
285 | 284 | if line: |
|
286 | 285 | item = item + ' %s\n' % line.strip() |
|
287 | 286 | list.append(item) |
|
288 | 287 | # Emphasize the last entry |
|
289 | 288 | filename, lineno, name, line = extracted_list[-1] |
|
290 | 289 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
291 | 290 | (Colors.normalEm, |
|
292 | 291 | Colors.filenameEm, filename, Colors.normalEm, |
|
293 | 292 | Colors.linenoEm, lineno, Colors.normalEm, |
|
294 | 293 | Colors.nameEm, name, Colors.normalEm, |
|
295 | 294 | Colors.Normal) |
|
296 | 295 | if line: |
|
297 | 296 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), |
|
298 | 297 | Colors.Normal) |
|
299 | 298 | list.append(item) |
|
300 | 299 | return list |
|
301 | 300 | |
|
302 | 301 | def _format_exception_only(self, etype, value): |
|
303 | 302 | """Format the exception part of a traceback. |
|
304 | 303 | |
|
305 | 304 | The arguments are the exception type and value such as given by |
|
306 | 305 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
307 | 306 | in a newline. Normally, the list contains a single string; however, |
|
308 | 307 | for SyntaxError exceptions, it contains several lines that (when |
|
309 | 308 | printed) display detailed information about where the syntax error |
|
310 | 309 | occurred. The message indicating which exception occurred is the |
|
311 | 310 | always last string in the list. |
|
312 | 311 | |
|
313 | 312 | Also lifted nearly verbatim from traceback.py |
|
314 | 313 | """ |
|
315 | 314 | |
|
316 | 315 | Colors = self.Colors |
|
317 | 316 | list = [] |
|
318 | 317 | if type(etype) == types.ClassType: |
|
319 | 318 | stype = Colors.excName + etype.__name__ + Colors.Normal |
|
320 | 319 | else: |
|
321 | 320 | stype = etype # String exceptions don't get special coloring |
|
322 | 321 | if value is None: |
|
323 | 322 | list.append( str(stype) + '\n') |
|
324 | 323 | else: |
|
325 | 324 | if etype is SyntaxError: |
|
326 | 325 | try: |
|
327 | 326 | msg, (filename, lineno, offset, line) = value |
|
328 | 327 | except: |
|
329 | 328 | pass |
|
330 | 329 | else: |
|
331 | 330 | #print 'filename is',filename # dbg |
|
332 | 331 | if not filename: filename = "<string>" |
|
333 | 332 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ |
|
334 | 333 | (Colors.normalEm, |
|
335 | 334 | Colors.filenameEm, filename, Colors.normalEm, |
|
336 | 335 | Colors.linenoEm, lineno, Colors.Normal )) |
|
337 | 336 | if line is not None: |
|
338 | 337 | i = 0 |
|
339 | 338 | while i < len(line) and line[i].isspace(): |
|
340 | 339 | i = i+1 |
|
341 | 340 | list.append('%s %s%s\n' % (Colors.line, |
|
342 | 341 | line.strip(), |
|
343 | 342 | Colors.Normal)) |
|
344 | 343 | if offset is not None: |
|
345 | 344 | s = ' ' |
|
346 | 345 | for c in line[i:offset-1]: |
|
347 | 346 | if c.isspace(): |
|
348 | 347 | s = s + c |
|
349 | 348 | else: |
|
350 | 349 | s = s + ' ' |
|
351 | 350 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
352 | 351 | Colors.Normal) ) |
|
353 | 352 | value = msg |
|
354 | 353 | s = self._some_str(value) |
|
355 | 354 | if s: |
|
356 | 355 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, |
|
357 | 356 | Colors.Normal, s)) |
|
358 | 357 | else: |
|
359 | 358 | list.append('%s\n' % str(stype)) |
|
360 | 359 | return list |
|
361 | 360 | |
|
362 | 361 | def _some_str(self, value): |
|
363 | 362 | # Lifted from traceback.py |
|
364 | 363 | try: |
|
365 | 364 | return str(value) |
|
366 | 365 | except: |
|
367 | 366 | return '<unprintable %s object>' % type(value).__name__ |
|
368 | 367 | |
|
369 | 368 | #---------------------------------------------------------------------------- |
|
370 | 369 | class VerboseTB(TBTools): |
|
371 | 370 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
372 | 371 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
373 | 372 | |
|
374 | 373 | Modified version which optionally strips the topmost entries from the |
|
375 | 374 | traceback, to be used with alternate interpreters (because their own code |
|
376 | 375 | would appear in the traceback).""" |
|
377 | 376 | |
|
378 | 377 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, |
|
379 | 378 | call_pdb = 0, include_vars=1): |
|
380 | 379 | """Specify traceback offset, headers and color scheme. |
|
381 | 380 | |
|
382 | 381 | Define how many frames to drop from the tracebacks. Calling it with |
|
383 | 382 | tb_offset=1 allows use of this handler in interpreters which will have |
|
384 | 383 | their own code at the top of the traceback (VerboseTB will first |
|
385 | 384 | remove that frame before printing the traceback info).""" |
|
386 | 385 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) |
|
387 | 386 | self.tb_offset = tb_offset |
|
388 | 387 | self.long_header = long_header |
|
389 | 388 | self.include_vars = include_vars |
|
390 | 389 | |
|
391 | 390 | def text(self, etype, evalue, etb, context=5): |
|
392 | 391 | """Return a nice text document describing the traceback.""" |
|
393 | 392 | |
|
394 | 393 | # some locals |
|
395 | 394 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
396 | 395 | ColorsNormal = Colors.Normal # used a lot |
|
397 | 396 | indent = ' '*INDENT_SIZE |
|
398 | 397 | text_repr = pydoc.text.repr |
|
399 | 398 | exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal) |
|
400 | 399 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
401 | 400 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
402 | 401 | |
|
403 | 402 | # some internal-use functions |
|
404 | 403 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) |
|
405 | 404 | def nullrepr(value, repr=text_repr): return '' |
|
406 | 405 | |
|
407 | 406 | # meat of the code begins |
|
408 | 407 | if type(etype) is types.ClassType: |
|
409 | 408 | etype = etype.__name__ |
|
410 | 409 | |
|
411 | 410 | if self.long_header: |
|
412 | 411 | # Header with the exception type, python version, and date |
|
413 | 412 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable |
|
414 | 413 | date = time.ctime(time.time()) |
|
415 | 414 | |
|
416 | 415 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, |
|
417 | 416 | exc, ' '*(75-len(str(etype))-len(pyver)), |
|
418 | 417 | pyver, string.rjust(date, 75) ) |
|
419 | 418 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ |
|
420 | 419 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
421 | 420 | else: |
|
422 | 421 | # Simplified header |
|
423 | 422 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, |
|
424 | 423 | string.rjust('Traceback (most recent call last)', |
|
425 | 424 | 75 - len(str(etype)) ) ) |
|
426 | 425 | frames = [] |
|
427 | 426 | # Flush cache before calling inspect. This helps alleviate some of the |
|
428 | 427 | # problems with python 2.3's inspect.py. |
|
429 | 428 | linecache.checkcache() |
|
430 | 429 | # Drop topmost frames if requested |
|
431 | 430 | try: |
|
432 | 431 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
433 | 432 | # problems, but it generates empty tracebacks for console errors |
|
434 | 433 | # (5 blanks lines) where none should be returned. |
|
435 | 434 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] |
|
436 | 435 | #print 'python records:', records # dbg |
|
437 | 436 | records = _fixed_getinnerframes(etb, context,self.tb_offset) |
|
438 | 437 | #print 'alex records:', records # dbg |
|
439 | 438 | except: |
|
440 | 439 | |
|
441 | 440 | # FIXME: I've been getting many crash reports from python 2.3 |
|
442 | 441 | # users, traceable to inspect.py. If I can find a small test-case |
|
443 | 442 | # to reproduce this, I should either write a better workaround or |
|
444 | 443 | # file a bug report against inspect (if that's the real problem). |
|
445 | 444 | # So far, I haven't been able to find an isolated example to |
|
446 | 445 | # reproduce the problem. |
|
447 | 446 | inspect_error() |
|
448 | 447 | traceback.print_exc(file=Term.cerr) |
|
449 | 448 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
450 | 449 | return '' |
|
451 | 450 | |
|
452 | 451 | # build some color string templates outside these nested loops |
|
453 | 452 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) |
|
454 | 453 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
455 | 454 | ColorsNormal) |
|
456 | 455 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
457 | 456 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
458 | 457 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
459 | 458 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
460 | 459 | Colors.vName, ColorsNormal) |
|
461 | 460 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
462 | 461 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
463 | 462 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, |
|
464 | 463 | ColorsNormal) |
|
465 | 464 | |
|
466 | 465 | # now, loop over all records printing context and info |
|
467 | 466 | abspath = os.path.abspath |
|
468 | 467 | for frame, file, lnum, func, lines, index in records: |
|
469 | 468 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
470 | 469 | try: |
|
471 | 470 | file = file and abspath(file) or '?' |
|
472 | 471 | except OSError: |
|
473 | 472 | # if file is '<console>' or something not in the filesystem, |
|
474 | 473 | # the abspath call will throw an OSError. Just ignore it and |
|
475 | 474 | # keep the original file string. |
|
476 | 475 | pass |
|
477 | 476 | link = tpl_link % file |
|
478 | 477 | try: |
|
479 | 478 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
480 | 479 | except: |
|
481 | 480 | # This can happen due to a bug in python2.3. We should be |
|
482 | 481 | # able to remove this try/except when 2.4 becomes a |
|
483 | 482 | # requirement. Bug details at http://python.org/sf/1005466 |
|
484 | 483 | inspect_error() |
|
485 | 484 | traceback.print_exc(file=Term.cerr) |
|
486 | 485 | info("\nIPython's exception reporting continues...\n") |
|
487 | 486 | |
|
488 | 487 | if func == '?': |
|
489 | 488 | call = '' |
|
490 | 489 | else: |
|
491 | 490 | # Decide whether to include variable details or not |
|
492 | 491 | var_repr = self.include_vars and eqrepr or nullrepr |
|
493 | 492 | try: |
|
494 | 493 | call = tpl_call % (func,inspect.formatargvalues(args, |
|
495 | 494 | varargs, varkw, |
|
496 | 495 | locals,formatvalue=var_repr)) |
|
497 | 496 | except KeyError: |
|
498 | 497 | # Very odd crash from inspect.formatargvalues(). The |
|
499 | 498 | # scenario under which it appeared was a call to |
|
500 | 499 | # view(array,scale) in NumTut.view.view(), where scale had |
|
501 | 500 | # been defined as a scalar (it should be a tuple). Somehow |
|
502 | 501 | # inspect messes up resolving the argument list of view() |
|
503 | 502 | # and barfs out. At some point I should dig into this one |
|
504 | 503 | # and file a bug report about it. |
|
505 | 504 | inspect_error() |
|
506 | 505 | traceback.print_exc(file=Term.cerr) |
|
507 | 506 | info("\nIPython's exception reporting continues...\n") |
|
508 | 507 | call = tpl_call_fail % func |
|
509 | 508 | |
|
510 | 509 | # Initialize a list of names on the current line, which the |
|
511 | 510 | # tokenizer below will populate. |
|
512 | 511 | names = [] |
|
513 | 512 | |
|
514 | 513 | def tokeneater(token_type, token, start, end, line): |
|
515 | 514 | """Stateful tokeneater which builds dotted names. |
|
516 | 515 | |
|
517 | 516 | The list of names it appends to (from the enclosing scope) can |
|
518 | 517 | contain repeated composite names. This is unavoidable, since |
|
519 | 518 | there is no way to disambguate partial dotted structures until |
|
520 | 519 | the full list is known. The caller is responsible for pruning |
|
521 | 520 | the final list of duplicates before using it.""" |
|
522 | 521 | |
|
523 | 522 | # build composite names |
|
524 | 523 | if token == '.': |
|
525 | 524 | try: |
|
526 | 525 | names[-1] += '.' |
|
527 | 526 | # store state so the next token is added for x.y.z names |
|
528 | 527 | tokeneater.name_cont = True |
|
529 | 528 | return |
|
530 | 529 | except IndexError: |
|
531 | 530 | pass |
|
532 | 531 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
533 | 532 | if tokeneater.name_cont: |
|
534 | 533 | # Dotted names |
|
535 | 534 | names[-1] += token |
|
536 | 535 | tokeneater.name_cont = False |
|
537 | 536 | else: |
|
538 | 537 | # Regular new names. We append everything, the caller |
|
539 | 538 | # will be responsible for pruning the list later. It's |
|
540 | 539 | # very tricky to try to prune as we go, b/c composite |
|
541 | 540 | # names can fool us. The pruning at the end is easy |
|
542 | 541 | # to do (or the caller can print a list with repeated |
|
543 | 542 | # names if so desired. |
|
544 | 543 | names.append(token) |
|
545 | 544 | elif token_type == tokenize.NEWLINE: |
|
546 | 545 | raise IndexError |
|
547 | 546 | # we need to store a bit of state in the tokenizer to build |
|
548 | 547 | # dotted names |
|
549 | 548 | tokeneater.name_cont = False |
|
550 | 549 | |
|
551 | 550 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
552 | 551 | line = getline(file, lnum[0]) |
|
553 | 552 | lnum[0] += 1 |
|
554 | 553 | return line |
|
555 | 554 | |
|
556 | 555 | # Build the list of names on this line of code where the exception |
|
557 | 556 | # occurred. |
|
558 | 557 | try: |
|
559 | 558 | # This builds the names list in-place by capturing it from the |
|
560 | 559 | # enclosing scope. |
|
561 | 560 | tokenize.tokenize(linereader, tokeneater) |
|
562 | 561 | except IndexError: |
|
563 | 562 | # signals exit of tokenizer |
|
564 | 563 | pass |
|
565 | 564 | except tokenize.TokenError,msg: |
|
566 | 565 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
567 | 566 | "The following traceback may be corrupted or invalid\n" |
|
568 | 567 | "The error message is: %s\n" % msg) |
|
569 | 568 | error(_m) |
|
570 | 569 | |
|
571 | 570 | # prune names list of duplicates, but keep the right order |
|
572 | 571 | unique_names = uniq_stable(names) |
|
573 | 572 | |
|
574 | 573 | # Start loop over vars |
|
575 | 574 | lvals = [] |
|
576 | 575 | if self.include_vars: |
|
577 | 576 | for name_full in unique_names: |
|
578 | 577 | name_base = name_full.split('.',1)[0] |
|
579 | 578 | if name_base in frame.f_code.co_varnames: |
|
580 | 579 | if locals.has_key(name_base): |
|
581 | 580 | try: |
|
582 | 581 | value = repr(eval(name_full,locals)) |
|
583 | 582 | except: |
|
584 | 583 | value = undefined |
|
585 | 584 | else: |
|
586 | 585 | value = undefined |
|
587 | 586 | name = tpl_local_var % name_full |
|
588 | 587 | else: |
|
589 | 588 | if frame.f_globals.has_key(name_base): |
|
590 | 589 | try: |
|
591 | 590 | value = repr(eval(name_full,frame.f_globals)) |
|
592 | 591 | except: |
|
593 | 592 | value = undefined |
|
594 | 593 | else: |
|
595 | 594 | value = undefined |
|
596 | 595 | name = tpl_global_var % name_full |
|
597 | 596 | lvals.append(tpl_name_val % (name,value)) |
|
598 | 597 | if lvals: |
|
599 | 598 | lvals = '%s%s' % (indent,em_normal.join(lvals)) |
|
600 | 599 | else: |
|
601 | 600 | lvals = '' |
|
602 | 601 | |
|
603 | 602 | level = '%s %s\n' % (link,call) |
|
604 | 603 | |
|
605 | 604 | if index is None: |
|
606 | 605 | frames.append(level) |
|
607 | 606 | else: |
|
608 | 607 | frames.append('%s%s' % (level,''.join( |
|
609 | 608 | _formatTracebackLines(lnum,index,lines,self.Colors,lvals)))) |
|
610 | 609 | |
|
611 | 610 | # Get (safely) a string form of the exception info |
|
612 | 611 | try: |
|
613 | 612 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
614 | 613 | except: |
|
615 | 614 | # User exception is improperly defined. |
|
616 | 615 | etype,evalue = str,sys.exc_info()[:2] |
|
617 | 616 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
618 | 617 | # ... and format it |
|
619 | 618 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, |
|
620 | 619 | ColorsNormal, evalue_str)] |
|
621 | 620 | if type(evalue) is types.InstanceType: |
|
622 | 621 | try: |
|
623 | 622 | names = [w for w in dir(evalue) if isinstance(w, basestring)] |
|
624 | 623 | except: |
|
625 | 624 | # Every now and then, an object with funny inernals blows up |
|
626 | 625 | # when dir() is called on it. We do the best we can to report |
|
627 | 626 | # the problem and continue |
|
628 | 627 | _m = '%sException reporting error (object with broken dir())%s:' |
|
629 | 628 | exception.append(_m % (Colors.excName,ColorsNormal)) |
|
630 | 629 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) |
|
631 | 630 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, |
|
632 | 631 | ColorsNormal, evalue_str)) |
|
633 | 632 | names = [] |
|
634 | 633 | for name in names: |
|
635 | 634 | value = text_repr(getattr(evalue, name)) |
|
636 | 635 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
637 | 636 | # return all our info assembled as a single string |
|
638 | 637 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) |
|
639 | 638 | |
|
640 | 639 | def debugger(self): |
|
641 | 640 | """Call up the pdb debugger if desired, always clean up the tb reference. |
|
642 | 641 | |
|
643 | 642 | If the call_pdb flag is set, the pdb interactive debugger is |
|
644 | 643 | invoked. In all cases, the self.tb reference to the current traceback |
|
645 | 644 | is deleted to prevent lingering references which hamper memory |
|
646 | 645 | management. |
|
647 | 646 | |
|
648 | 647 | Note that each call to pdb() does an 'import readline', so if your app |
|
649 | 648 | requires a special setup for the readline completers, you'll have to |
|
650 | 649 | fix that by hand after invoking the exception handler.""" |
|
651 | 650 | |
|
652 | 651 | if self.call_pdb: |
|
653 | 652 | if self.pdb is None: |
|
654 | 653 | self.pdb = Debugger.Pdb( |
|
655 | 654 | self.color_scheme_table.active_scheme_name) |
|
656 | 655 | # the system displayhook may have changed, restore the original |
|
657 | 656 | # for pdb |
|
658 | 657 | dhook = sys.displayhook |
|
659 | 658 | sys.displayhook = sys.__displayhook__ |
|
660 | 659 | self.pdb.reset() |
|
661 | 660 | # Find the right frame so we don't pop up inside ipython itself |
|
662 | 661 | etb = self.tb |
|
663 | 662 | while self.tb.tb_next is not None: |
|
664 | 663 | self.tb = self.tb.tb_next |
|
665 | 664 | try: |
|
666 | 665 | if etb and etb.tb_next: |
|
667 | 666 | etb = etb.tb_next |
|
668 | 667 | self.pdb.botframe = etb.tb_frame |
|
669 | 668 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
670 | 669 | except: |
|
671 | 670 | print '*** ERROR ***' |
|
672 | 671 | print 'This version of pdb has a bug and crashed.' |
|
673 | 672 | print 'Returning to IPython...' |
|
674 | 673 | sys.displayhook = dhook |
|
675 | 674 | del self.tb |
|
676 | 675 | |
|
677 | 676 | def handler(self, info=None): |
|
678 | 677 | (etype, evalue, etb) = info or sys.exc_info() |
|
679 | 678 | self.tb = etb |
|
680 | 679 | print >> Term.cerr, self.text(etype, evalue, etb) |
|
681 | 680 | |
|
682 | 681 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
683 | 682 | # out the right info on its own. |
|
684 | 683 | def __call__(self, etype=None, evalue=None, etb=None): |
|
685 | 684 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
686 | 685 | if etb is None: |
|
687 | 686 | self.handler() |
|
688 | 687 | else: |
|
689 | 688 | self.handler((etype, evalue, etb)) |
|
690 | 689 | self.debugger() |
|
691 | 690 | |
|
692 | 691 | #---------------------------------------------------------------------------- |
|
693 | 692 | class FormattedTB(VerboseTB,ListTB): |
|
694 | 693 | """Subclass ListTB but allow calling with a traceback. |
|
695 | 694 | |
|
696 | 695 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
697 | 696 | |
|
698 | 697 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
699 | 698 | |
|
700 | 699 | Allows a tb_offset to be specified. This is useful for situations where |
|
701 | 700 | one needs to remove a number of topmost frames from the traceback (such as |
|
702 | 701 | occurs with python programs that themselves execute other python code, |
|
703 | 702 | like Python shells). """ |
|
704 | 703 | |
|
705 | 704 | def __init__(self, mode = 'Plain', color_scheme='Linux', |
|
706 | 705 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): |
|
707 | 706 | |
|
708 | 707 | # NEVER change the order of this list. Put new modes at the end: |
|
709 | 708 | self.valid_modes = ['Plain','Context','Verbose'] |
|
710 | 709 | self.verbose_modes = self.valid_modes[1:3] |
|
711 | 710 | |
|
712 | 711 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, |
|
713 | 712 | call_pdb=call_pdb,include_vars=include_vars) |
|
714 | 713 | self.set_mode(mode) |
|
715 | 714 | |
|
716 | 715 | def _extract_tb(self,tb): |
|
717 | 716 | if tb: |
|
718 | 717 | return traceback.extract_tb(tb) |
|
719 | 718 | else: |
|
720 | 719 | return None |
|
721 | 720 | |
|
722 | 721 | def text(self, etype, value, tb,context=5,mode=None): |
|
723 | 722 | """Return formatted traceback. |
|
724 | 723 | |
|
725 | 724 | If the optional mode parameter is given, it overrides the current |
|
726 | 725 | mode.""" |
|
727 | 726 | |
|
728 | 727 | if mode is None: |
|
729 | 728 | mode = self.mode |
|
730 | 729 | if mode in self.verbose_modes: |
|
731 | 730 | # verbose modes need a full traceback |
|
732 | 731 | return VerboseTB.text(self,etype, value, tb,context=5) |
|
733 | 732 | else: |
|
734 | 733 | # We must check the source cache because otherwise we can print |
|
735 | 734 | # out-of-date source code. |
|
736 | 735 | linecache.checkcache() |
|
737 | 736 | # Now we can extract and format the exception |
|
738 | 737 | elist = self._extract_tb(tb) |
|
739 | 738 | if len(elist) > self.tb_offset: |
|
740 | 739 | del elist[:self.tb_offset] |
|
741 | 740 | return ListTB.text(self,etype,value,elist) |
|
742 | 741 | |
|
743 | 742 | def set_mode(self,mode=None): |
|
744 | 743 | """Switch to the desired mode. |
|
745 | 744 | |
|
746 | 745 | If mode is not specified, cycles through the available modes.""" |
|
747 | 746 | |
|
748 | 747 | if not mode: |
|
749 | 748 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ |
|
750 | 749 | len(self.valid_modes) |
|
751 | 750 | self.mode = self.valid_modes[new_idx] |
|
752 | 751 | elif mode not in self.valid_modes: |
|
753 | 752 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ |
|
754 | 753 | 'Valid modes: '+str(self.valid_modes) |
|
755 | 754 | else: |
|
756 | 755 | self.mode = mode |
|
757 | 756 | # include variable details only in 'Verbose' mode |
|
758 | 757 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
759 | 758 | |
|
760 | 759 | # some convenient shorcuts |
|
761 | 760 | def plain(self): |
|
762 | 761 | self.set_mode(self.valid_modes[0]) |
|
763 | 762 | |
|
764 | 763 | def context(self): |
|
765 | 764 | self.set_mode(self.valid_modes[1]) |
|
766 | 765 | |
|
767 | 766 | def verbose(self): |
|
768 | 767 | self.set_mode(self.valid_modes[2]) |
|
769 | 768 | |
|
770 | 769 | #---------------------------------------------------------------------------- |
|
771 | 770 | class AutoFormattedTB(FormattedTB): |
|
772 | 771 | """A traceback printer which can be called on the fly. |
|
773 | 772 | |
|
774 | 773 | It will find out about exceptions by itself. |
|
775 | 774 | |
|
776 | 775 | A brief example: |
|
777 | 776 | |
|
778 | 777 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
779 | 778 | try: |
|
780 | 779 | ... |
|
781 | 780 | except: |
|
782 | 781 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
783 | 782 | """ |
|
784 | 783 | def __call__(self,etype=None,evalue=None,etb=None, |
|
785 | 784 | out=None,tb_offset=None): |
|
786 | 785 | """Print out a formatted exception traceback. |
|
787 | 786 | |
|
788 | 787 | Optional arguments: |
|
789 | 788 | - out: an open file-like object to direct output to. |
|
790 | 789 | |
|
791 | 790 | - tb_offset: the number of frames to skip over in the stack, on a |
|
792 | 791 | per-call basis (this overrides temporarily the instance's tb_offset |
|
793 | 792 | given at initialization time. """ |
|
794 | 793 | |
|
795 | 794 | if out is None: |
|
796 | 795 | out = Term.cerr |
|
797 | 796 | if tb_offset is not None: |
|
798 | 797 | tb_offset, self.tb_offset = self.tb_offset, tb_offset |
|
799 | 798 | print >> out, self.text(etype, evalue, etb) |
|
800 | 799 | self.tb_offset = tb_offset |
|
801 | 800 | else: |
|
802 | 801 | print >> out, self.text(etype, evalue, etb) |
|
803 | 802 | self.debugger() |
|
804 | 803 | |
|
805 | 804 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): |
|
806 | 805 | if etype is None: |
|
807 | 806 | etype,value,tb = sys.exc_info() |
|
808 | 807 | self.tb = tb |
|
809 | 808 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) |
|
810 | 809 | |
|
811 | 810 | #--------------------------------------------------------------------------- |
|
812 | 811 | # A simple class to preserve Nathan's original functionality. |
|
813 | 812 | class ColorTB(FormattedTB): |
|
814 | 813 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
815 | 814 | def __init__(self,color_scheme='Linux',call_pdb=0): |
|
816 | 815 | FormattedTB.__init__(self,color_scheme=color_scheme, |
|
817 | 816 | call_pdb=call_pdb) |
|
818 | 817 | |
|
819 | 818 | #---------------------------------------------------------------------------- |
|
820 | 819 | # module testing (minimal) |
|
821 | 820 | if __name__ == "__main__": |
|
822 | 821 | def spam(c, (d, e)): |
|
823 | 822 | x = c + d |
|
824 | 823 | y = c * d |
|
825 | 824 | foo(x, y) |
|
826 | 825 | |
|
827 | 826 | def foo(a, b, bar=1): |
|
828 | 827 | eggs(a, b + bar) |
|
829 | 828 | |
|
830 | 829 | def eggs(f, g, z=globals()): |
|
831 | 830 | h = f + g |
|
832 | 831 | i = f - g |
|
833 | 832 | return h / i |
|
834 | 833 | |
|
835 | 834 | print '' |
|
836 | 835 | print '*** Before ***' |
|
837 | 836 | try: |
|
838 | 837 | print spam(1, (2, 3)) |
|
839 | 838 | except: |
|
840 | 839 | traceback.print_exc() |
|
841 | 840 | print '' |
|
842 | 841 | |
|
843 | 842 | handler = ColorTB() |
|
844 | 843 | print '*** ColorTB ***' |
|
845 | 844 | try: |
|
846 | 845 | print spam(1, (2, 3)) |
|
847 | 846 | except: |
|
848 | 847 | apply(handler, sys.exc_info() ) |
|
849 | 848 | print '' |
|
850 | 849 | |
|
851 | 850 | handler = VerboseTB() |
|
852 | 851 | print '*** VerboseTB ***' |
|
853 | 852 | try: |
|
854 | 853 | print spam(1, (2, 3)) |
|
855 | 854 | except: |
|
856 | 855 | apply(handler, sys.exc_info() ) |
|
857 | 856 | print '' |
|
858 | 857 |
@@ -1,43 +1,46 b'' | |||
|
1 | """Set of functions to work with console on Windows. | |
|
1 | 2 | """ |
|
2 | Set of functions to work with console on Windows. | |
|
3 | 3 | |
|
4 | Author: Alexander Belchenko (e-mail: bialix AT ukr.net) | |
|
5 | License: Public domain | |
|
6 | """ | |
|
4 | #***************************************************************************** | |
|
5 | # Copyright (C) 2005 Alexander Belchenko <bialix@ukr.net> | |
|
6 | # | |
|
7 | # This file is placed in the public domain. | |
|
8 | # | |
|
9 | #***************************************************************************** | |
|
7 | 10 | |
|
8 | 11 | __author__ = 'Alexander Belchenko (e-mail: bialix AT ukr.net)' |
|
9 | 12 | __license__ = 'Public domain' |
|
10 | 13 | |
|
11 | 14 | import struct |
|
12 | 15 | |
|
13 | 16 | try: |
|
14 | 17 | import ctypes |
|
15 | 18 | except ImportError: |
|
16 | 19 | ctypes = None |
|
17 | 20 | |
|
18 | 21 | def get_console_size(defaultx=80, defaulty=25): |
|
19 | 22 | """ Return size of current console. |
|
20 | 23 | |
|
21 | 24 | This function try to determine actual size of current working |
|
22 | 25 | console window and return tuple (sizex, sizey) if success, |
|
23 | 26 | or default size (defaultx, defaulty) otherwise. |
|
24 | 27 | |
|
25 | 28 | Dependencies: ctypes should be installed. |
|
26 | 29 | """ |
|
27 | 30 | if ctypes is None: |
|
28 | 31 | # no ctypes is found |
|
29 | 32 | return (defaultx, defaulty) |
|
30 | 33 | |
|
31 | 34 | h = ctypes.windll.kernel32.GetStdHandle(-11) |
|
32 | 35 | csbi = ctypes.create_string_buffer(22) |
|
33 | 36 | res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) |
|
34 | 37 | |
|
35 | 38 | if res: |
|
36 | 39 | (bufx, bufy, curx, cury, wattr, |
|
37 | 40 | left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", |
|
38 | 41 | csbi.raw) |
|
39 | 42 | sizex = right - left + 1 |
|
40 | 43 | sizey = bottom - top + 1 |
|
41 | 44 | return (sizex, sizey) |
|
42 | 45 | else: |
|
43 | 46 | return (defaultx, defaulty) |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now