Show More
@@ -1,272 +1,273 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 9 |
|
|
8 | $Id: InterpreterExec.py 1039 2006-01-20 23:59:33Z vivainio $""" | |
|
9 | 9 | |
|
10 | 10 | #***************************************************************************** |
|
11 | 11 | # Copyright (C) 2004 W.J. van der Laan <gnufnork@hetdigitalegat.nl> |
|
12 | 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 | # This is redundant, ipy_user_conf.py will determine this | |
|
245 | 246 | # Load all of $PATH as aliases |
|
246 | if os.name == 'posix': | |
|
247 | # %rehash is very fast, but it doesn't check for executability, it simply | |
|
248 | # dumps everything in $PATH as an alias. Use rehashx if you want more | |
|
249 | # checks. | |
|
250 | __IPYTHON__.magic_rehash() | |
|
251 | else: | |
|
252 | # Windows users: the list of extensions considered executable is read from | |
|
253 | # the environment variable 'pathext'. If this is undefined, IPython | |
|
254 | # defaults to EXE, COM and BAT. | |
|
255 | # %rehashx is the one which does extension analysis, at the cost of | |
|
256 | # being much slower than %rehash. | |
|
257 | __IPYTHON__.magic_rehashx() | |
|
247 | #if os.name == 'posix': | |
|
248 | # # %rehash is very fast, but it doesn't check for executability, it simply | |
|
249 | # # dumps everything in $PATH as an alias. Use rehashx if you want more | |
|
250 | # # checks. | |
|
251 | # __IPYTHON__.magic_rehash() | |
|
252 | #else: | |
|
253 | # # Windows users: the list of extensions considered executable is read from | |
|
254 | # # the environment variable 'pathext'. If this is undefined, IPython | |
|
255 | # # defaults to EXE, COM and BAT. | |
|
256 | # # %rehashx is the one which does extension analysis, at the cost of | |
|
257 | # # being much slower than %rehash. | |
|
258 | # __IPYTHON__.magic_rehashx() | |
|
258 | 259 | |
|
259 | 260 | # Remove %sc,%sx if present as aliases |
|
260 | 261 | __IPYTHON__.magic_unalias('sc') |
|
261 | 262 | __IPYTHON__.magic_unalias('sx') |
|
262 | 263 | |
|
263 | 264 | # We need different criteria for line-splitting, so that aliases such as |
|
264 | 265 | # 'gnome-terminal' are interpreted as a single alias instead of variable |
|
265 | 266 | # 'gnome' minus variable 'terminal'. |
|
266 | 267 | import re |
|
267 | 268 | __IPYTHON__.line_split = re.compile(r'^([\s*,;/])' |
|
268 | 269 | r'([\?\w\.\-\+]+\w*\s*)' |
|
269 | 270 | r'(\(?.*$)') |
|
270 | 271 | |
|
271 | 272 | # Namespace cleanup |
|
272 | 273 | del re |
@@ -1,43 +1,49 b'' | |||
|
1 | 1 | """ User configuration file for IPython |
|
2 | 2 | |
|
3 | 3 | This is a more flexible and safe way to configure ipython than *rc files |
|
4 | 4 | (ipythonrc, ipythonrc-pysh etc.) |
|
5 | 5 | |
|
6 | 6 | This file is always imported on ipython startup. You should import all the |
|
7 | 7 | ipython extensions you need here (see IPython/Extensions directory). |
|
8 | 8 | |
|
9 | 9 | Feel free to edit this file to customize your ipython experience. If |
|
10 | 10 | you wish to only use the old config system, it's perfectly ok to make this file |
|
11 | 11 | empty. |
|
12 | 12 | |
|
13 | 13 | """ |
|
14 | 14 | |
|
15 | 15 | # Most of your config files and extensions will probably start with this import |
|
16 | 16 | |
|
17 | 17 | import IPython.ipapi as ip |
|
18 | 18 | |
|
19 | import os | |
|
19 | 20 | |
|
20 | 21 | o = ip.options() |
|
21 | 22 | # autocall 1 ('smart') is default anyway, this is just an |
|
22 | 23 | # example on how to set an option |
|
23 | 24 | o.autocall = 1 |
|
24 | 25 | |
|
25 | 26 | if o.profile == 'pysh': |
|
26 | 27 | # Jason Orendorff's path class is handy to have in user namespace |
|
27 | 28 | # if you are doing shell-like stuff |
|
28 | 29 | ip.ex("from IPython.path import path" ) |
|
29 | 30 | |
|
30 | 31 | # Uncomment these lines to get pysh-like prompt for all profiles. |
|
31 | 32 | |
|
32 | 33 | #o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' |
|
33 | 34 | #o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> ' |
|
34 | 35 | #o.prompt_out= '<\#> ' |
|
35 | 36 | |
|
36 | 37 | # make 'd' an alias for ls -F |
|
37 | 38 | |
|
38 | 39 | ip.magic('alias d ls -F --color=auto') |
|
39 | 40 | |
|
40 |
# Make available all system commands |
|
|
41 | # startup on slow machines, and to conserve a bit of memory | |
|
41 | # Make available all system commands through "rehashing" immediately. | |
|
42 | # You can comment these lines out to speed up startup on very slow | |
|
43 | # machines, and to conserve a bit of memory. | |
|
42 | 44 | |
|
43 | ip.magic('rehashx') No newline at end of file | |
|
45 | if os.name=='posix': | |
|
46 | ip.magic('rehash') | |
|
47 | else: | |
|
48 | #slightly slower, but better results esp. with Windows | |
|
49 | ip.magic('rehashx') |
General Comments 0
You need to be logged in to leave comments.
Login now