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