Show More
@@ -1,197 +1,202 b'' | |||
|
1 | 1 | """Shell mode for IPython. |
|
2 | 2 | |
|
3 | 3 | Start ipython in shell mode by invoking "ipython -p sh" |
|
4 | 4 | |
|
5 | 5 | (the old version, "ipython -p pysh" still works but this is the more "modern" |
|
6 | 6 | shell mode and is recommended for users who don't care about pysh-mode |
|
7 | 7 | compatibility) |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | from IPython import ipapi |
|
11 | 11 | import os,textwrap |
|
12 | 12 | |
|
13 | 13 | # The import below effectively obsoletes your old-style ipythonrc[.ini], |
|
14 | 14 | # so consider yourself warned! |
|
15 | 15 | |
|
16 | 16 | import ipy_defaults |
|
17 | 17 | |
|
18 | 18 | def main(): |
|
19 | 19 | ip = ipapi.get() |
|
20 | 20 | o = ip.options |
|
21 | 21 | # autocall to "full" mode (smart mode is default, I like full mode) |
|
22 | 22 | |
|
23 | 23 | o.autocall = 2 |
|
24 | 24 | |
|
25 | 25 | # Jason Orendorff's path class is handy to have in user namespace |
|
26 | 26 | # if you are doing shell-like stuff |
|
27 | 27 | try: |
|
28 | 28 | ip.ex("from path import path" ) |
|
29 | 29 | except ImportError: |
|
30 | 30 | pass |
|
31 | 31 | |
|
32 | 32 | # beefed up %env is handy in shell mode |
|
33 | 33 | import envpersist |
|
34 | 34 | import ipy_which |
|
35 | 35 | import ipy_stock_completers |
|
36 | 36 | import ipy_rehashdir |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | ip.ex('import os') |
|
40 | 40 | ip.ex("def up(): os.chdir('..')") |
|
41 | 41 | |
|
42 | 42 | # Nice prompt |
|
43 | 43 | |
|
44 | 44 | o.prompt_in1= r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#> ' |
|
45 | 45 | o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green> ' |
|
46 | 46 | o.prompt_out= '<\#> ' |
|
47 | 47 | |
|
48 | 48 | from IPython import Release |
|
49 | 49 | |
|
50 | 50 | import sys |
|
51 | 51 | # I like my banner minimal. |
|
52 |
o.banner = "Py %s |
|
|
52 | o.banner = "IPython %s [on Py %s]\n" % (Release.version,sys.version.split(None,1)[0]) | |
|
53 | 53 | |
|
54 | 54 | # make 'd' an alias for ls -F |
|
55 | 55 | |
|
56 | 56 | ip.magic('alias d ls -F --color=auto') |
|
57 | 57 | |
|
58 | 58 | ip.IP.default_option('cd','-q') |
|
59 | 59 | |
|
60 | 60 | # If you only rarely want to execute the things you %edit... |
|
61 | 61 | |
|
62 | 62 | #ip.IP.default_option('edit','-x') |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | o.prompts_pad_left="1" |
|
66 | 66 | # Remove all blank lines in between prompts, like a normal shell. |
|
67 | 67 | o.separate_in="0" |
|
68 | 68 | o.separate_out="0" |
|
69 | 69 | o.separate_out2="0" |
|
70 | 70 | |
|
71 | 71 | # now alias all syscommands |
|
72 | 72 | |
|
73 | 73 | db = ip.db |
|
74 | 74 | |
|
75 | 75 | syscmds = db.get("syscmdlist",[] ) |
|
76 | 76 | if not syscmds: |
|
77 | 77 | print textwrap.dedent(""" |
|
78 | 78 | System command list not initialized, probably the first run... |
|
79 | 79 | running %rehashx to refresh the command list. Run %rehashx |
|
80 | 80 | again to refresh command list (after installing new software etc.) |
|
81 | 81 | """) |
|
82 | 82 | ip.magic('rehashx') |
|
83 | 83 | syscmds = db.get("syscmdlist") |
|
84 | 84 | |
|
85 | 85 | # locase aliases on win#2 only |
|
86 | 86 | if os.name == 'posix': |
|
87 | 87 | mapper = lambda s:s |
|
88 | 88 | else: |
|
89 | 89 | def mapper(s): return s.lower() |
|
90 | 90 | |
|
91 | 91 | for cmd in syscmds: |
|
92 | 92 | #print "al",cmd |
|
93 | 93 | noext, ext = os.path.splitext(cmd) |
|
94 | 94 | ip.IP.alias_table[mapper(noext)] = (0,cmd) |
|
95 | ||
|
96 | ||
|
95 | 97 | extend_shell_behavior(ip) |
|
96 | 98 | |
|
99 | # XXX You do not need to understand the next function! | |
|
100 | # This should probably be moved out of profile | |
|
101 | ||
|
97 | 102 | def extend_shell_behavior(ip): |
|
98 | 103 | |
|
99 | 104 | # Instead of making signature a global variable tie it to IPSHELL. |
|
100 | 105 | # In future if it is required to distinguish between different |
|
101 | 106 | # shells we can assign a signature per shell basis |
|
102 | 107 | ip.IP.__sig__ = 0xa005 |
|
103 | 108 | # mark the IPSHELL with this signature |
|
104 | 109 | ip.IP.user_ns['__builtins__'].__dict__['__sig__'] = ip.IP.__sig__ |
|
105 | 110 | |
|
106 | 111 | from IPython.Itpl import ItplNS |
|
107 | 112 | from IPython.genutils import shell |
|
108 | 113 | # utility to expand user variables via Itpl |
|
109 | 114 | # xxx do something sensible with depth? |
|
110 | 115 | ip.IP.var_expand = lambda cmd, lvars=None, depth=2: \ |
|
111 | 116 | str(ItplNS(cmd.replace('#','\#'), ip.IP.user_ns, get_locals())) |
|
112 | 117 | |
|
113 | 118 | def get_locals(): |
|
114 | 119 | """ Substituting a variable through Itpl deep inside the IPSHELL stack |
|
115 | 120 | requires the knowledge of all the variables in scope upto the last |
|
116 | 121 | IPSHELL frame. This routine simply merges all the local variables |
|
117 | 122 | on the IPSHELL stack without worrying about their scope rules |
|
118 | 123 | """ |
|
119 | 124 | import sys |
|
120 | 125 | # note lambda expression constitues a function call |
|
121 | 126 | # hence fno should be incremented by one |
|
122 | 127 | getsig = lambda fno: sys._getframe(fno+1).f_globals \ |
|
123 | 128 | ['__builtins__'].__dict__['__sig__'] |
|
124 | 129 | getlvars = lambda fno: sys._getframe(fno+1).f_locals |
|
125 | 130 | # trackback until we enter the IPSHELL |
|
126 | 131 | frame_no = 1 |
|
127 | 132 | sig = ip.IP.__sig__ |
|
128 | 133 | fsig = ~sig |
|
129 | 134 | while fsig != sig : |
|
130 | 135 | try: |
|
131 | 136 | fsig = getsig(frame_no) |
|
132 | 137 | except (AttributeError, KeyError): |
|
133 | 138 | frame_no += 1 |
|
134 | 139 | except ValueError: |
|
135 | 140 | # stack is depleted |
|
136 | 141 | # call did not originate from IPSHELL |
|
137 | 142 | return {} |
|
138 | 143 | first_frame = frame_no |
|
139 | 144 | # walk further back until we exit from IPSHELL or deplete stack |
|
140 | 145 | try: |
|
141 | 146 | while(sig == getsig(frame_no+1)): |
|
142 | 147 | frame_no += 1 |
|
143 | 148 | except (AttributeError, KeyError, ValueError): |
|
144 | 149 | pass |
|
145 | 150 | # merge the locals from top down hence overriding |
|
146 | 151 | # any re-definitions of variables, functions etc. |
|
147 | 152 | lvars = {} |
|
148 | 153 | for fno in range(frame_no, first_frame-1, -1): |
|
149 | 154 | lvars.update(getlvars(fno)) |
|
150 | 155 | #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg |
|
151 | 156 | return lvars |
|
152 | 157 | |
|
153 | 158 | def _runlines(lines): |
|
154 | 159 | """Run a string of one or more lines of source. |
|
155 | 160 | |
|
156 | 161 | This method is capable of running a string containing multiple source |
|
157 | 162 | lines, as if they had been entered at the IPython prompt. Since it |
|
158 | 163 | exposes IPython's processing machinery, the given strings can contain |
|
159 | 164 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
160 | 165 | |
|
161 | 166 | # We must start with a clean buffer, in case this is run from an |
|
162 | 167 | # interactive IPython session (via a magic, for example). |
|
163 | 168 | ip.IP.resetbuffer() |
|
164 | 169 | lines = lines.split('\n') |
|
165 | 170 | more = 0 |
|
166 | 171 | command = '' |
|
167 | 172 | for line in lines: |
|
168 | 173 | # skip blank lines so we don't mess up the prompt counter, but do |
|
169 | 174 | # NOT skip even a blank line if we are in a code block (more is |
|
170 | 175 | # true) |
|
171 | 176 | # if command is not empty trim the line |
|
172 | 177 | if command != '' : |
|
173 | 178 | line = line.strip() |
|
174 | 179 | # add the broken line to the command |
|
175 | 180 | if line and line[-1] == '\\' : |
|
176 | 181 | command += line[0:-1] + ' ' |
|
177 | 182 | more = True |
|
178 | 183 | continue |
|
179 | 184 | else : |
|
180 | 185 | # add the last (current) line to the command |
|
181 | 186 | command += line |
|
182 | 187 | if command or more: |
|
183 | 188 | more = ip.IP.push(ip.IP.prefilter(command,more)) |
|
184 | 189 | command = '' |
|
185 | 190 | # IPython's runsource returns None if there was an error |
|
186 | 191 | # compiling the code. This allows us to stop processing right |
|
187 | 192 | # away, so the user gets the error message at the right place. |
|
188 | 193 | if more is None: |
|
189 | 194 | break |
|
190 | 195 | # final newline in case the input didn't have it, so that the code |
|
191 | 196 | # actually does get executed |
|
192 | 197 | if more: |
|
193 | 198 | ip.IP.push('\n') |
|
194 | 199 | |
|
195 | 200 | ip.IP.runlines = _runlines |
|
196 | 201 | |
|
197 | 202 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now