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