Show More
The requested changes are too big and content was truncated. Show full diff
@@ -1,188 +1,189 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 path import path" ) |
|
28 | ip.ex("from 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 | import ipy_which |
|
34 | import ipy_which | |
|
35 | import ipy_stock_completers | |||
35 |
|
36 | |||
36 |
|
37 | |||
37 | ip.ex('import os') |
|
38 | ip.ex('import os') | |
38 | ip.ex("def up(): os.chdir('..')") |
|
39 | ip.ex("def up(): os.chdir('..')") | |
39 |
|
40 | |||
40 | # Get pysh-like prompt for all profiles. |
|
41 | # Get pysh-like prompt for all profiles. | |
41 |
|
42 | |||
42 | o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' |
|
43 | o.prompt_in1= '\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' | |
43 | o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> ' |
|
44 | o.prompt_in2= '\C_Green|\C_LightGreen\D\C_Green> ' | |
44 | o.prompt_out= '<\#> ' |
|
45 | o.prompt_out= '<\#> ' | |
45 |
|
46 | |||
46 | from IPython import Release |
|
47 | from IPython import Release | |
47 |
|
48 | |||
48 | import sys |
|
49 | import sys | |
49 | # I like my banner minimal. |
|
50 | # I like my banner minimal. | |
50 | o.banner = "Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version) |
|
51 | o.banner = "Py %s IPy %s\n" % (sys.version.split('\n')[0],Release.version) | |
51 |
|
52 | |||
52 | # make 'd' an alias for ls -F |
|
53 | # make 'd' an alias for ls -F | |
53 |
|
54 | |||
54 | ip.magic('alias d ls -F --color=auto') |
|
55 | ip.magic('alias d ls -F --color=auto') | |
55 |
|
56 | |||
56 | ip.IP.default_option('cd','-q') |
|
57 | ip.IP.default_option('cd','-q') | |
57 |
|
58 | |||
58 | # If you only rarely want to execute the things you %edit... |
|
59 | # If you only rarely want to execute the things you %edit... | |
59 |
|
60 | |||
60 | #ip.IP.default_option('edit','-x') |
|
61 | #ip.IP.default_option('edit','-x') | |
61 |
|
62 | |||
62 |
|
63 | |||
63 | o.prompts_pad_left="1" |
|
64 | o.prompts_pad_left="1" | |
64 | # Remove all blank lines in between prompts, like a normal shell. |
|
65 | # Remove all blank lines in between prompts, like a normal shell. | |
65 | o.separate_in="0" |
|
66 | o.separate_in="0" | |
66 | o.separate_out="0" |
|
67 | o.separate_out="0" | |
67 | o.separate_out2="0" |
|
68 | o.separate_out2="0" | |
68 |
|
69 | |||
69 | # now alias all syscommands |
|
70 | # now alias all syscommands | |
70 |
|
71 | |||
71 | db = ip.db |
|
72 | db = ip.db | |
72 |
|
73 | |||
73 | syscmds = db.get("syscmdlist",[] ) |
|
74 | syscmds = db.get("syscmdlist",[] ) | |
74 | if not syscmds: |
|
75 | if not syscmds: | |
75 | print textwrap.dedent(""" |
|
76 | print textwrap.dedent(""" | |
76 | System command list not initialized, probably the first run... |
|
77 | System command list not initialized, probably the first run... | |
77 | running %rehashx to refresh the command list. Run %rehashx |
|
78 | running %rehashx to refresh the command list. Run %rehashx | |
78 | again to refresh command list (after installing new software etc.) |
|
79 | again to refresh command list (after installing new software etc.) | |
79 | """) |
|
80 | """) | |
80 | ip.magic('rehashx') |
|
81 | ip.magic('rehashx') | |
81 | syscmds = db.get("syscmdlist") |
|
82 | syscmds = db.get("syscmdlist") | |
82 | for cmd in syscmds: |
|
83 | for cmd in syscmds: | |
83 | #print "al",cmd |
|
84 | #print "al",cmd | |
84 | noext, ext = os.path.splitext(cmd) |
|
85 | noext, ext = os.path.splitext(cmd) | |
85 | ip.IP.alias_table[noext] = (0,cmd) |
|
86 | ip.IP.alias_table[noext] = (0,cmd) | |
86 | extend_shell_behavior(ip) |
|
87 | extend_shell_behavior(ip) | |
87 |
|
88 | |||
88 | def extend_shell_behavior(ip): |
|
89 | def extend_shell_behavior(ip): | |
89 |
|
90 | |||
90 | # Instead of making signature a global variable tie it to IPSHELL. |
|
91 | # Instead of making signature a global variable tie it to IPSHELL. | |
91 | # In future if it is required to distinguish between different |
|
92 | # In future if it is required to distinguish between different | |
92 | # shells we can assign a signature per shell basis |
|
93 | # shells we can assign a signature per shell basis | |
93 | ip.IP.__sig__ = 0xa005 |
|
94 | ip.IP.__sig__ = 0xa005 | |
94 | # mark the IPSHELL with this signature |
|
95 | # mark the IPSHELL with this signature | |
95 | ip.IP.user_ns['__builtins__'].__dict__['__sig__'] = ip.IP.__sig__ |
|
96 | ip.IP.user_ns['__builtins__'].__dict__['__sig__'] = ip.IP.__sig__ | |
96 |
|
97 | |||
97 | from IPython.Itpl import ItplNS |
|
98 | from IPython.Itpl import ItplNS | |
98 | from IPython.genutils import shell |
|
99 | from IPython.genutils import shell | |
99 | # utility to expand user variables via Itpl |
|
100 | # utility to expand user variables via Itpl | |
100 | # xxx do something sensible with depth? |
|
101 | # xxx do something sensible with depth? | |
101 | ip.IP.var_expand = lambda cmd, lvars=None, depth=2: \ |
|
102 | ip.IP.var_expand = lambda cmd, lvars=None, depth=2: \ | |
102 | str(ItplNS(cmd.replace('#','\#'), ip.IP.user_ns, get_locals())) |
|
103 | str(ItplNS(cmd.replace('#','\#'), ip.IP.user_ns, get_locals())) | |
103 |
|
104 | |||
104 | def get_locals(): |
|
105 | def get_locals(): | |
105 | """ Substituting a variable through Itpl deep inside the IPSHELL stack |
|
106 | """ Substituting a variable through Itpl deep inside the IPSHELL stack | |
106 | requires the knowledge of all the variables in scope upto the last |
|
107 | requires the knowledge of all the variables in scope upto the last | |
107 | IPSHELL frame. This routine simply merges all the local variables |
|
108 | IPSHELL frame. This routine simply merges all the local variables | |
108 | on the IPSHELL stack without worrying about their scope rules |
|
109 | on the IPSHELL stack without worrying about their scope rules | |
109 | """ |
|
110 | """ | |
110 | import sys |
|
111 | import sys | |
111 | # note lambda expression constitues a function call |
|
112 | # note lambda expression constitues a function call | |
112 | # hence fno should be incremented by one |
|
113 | # hence fno should be incremented by one | |
113 | getsig = lambda fno: sys._getframe(fno+1).f_globals \ |
|
114 | getsig = lambda fno: sys._getframe(fno+1).f_globals \ | |
114 | ['__builtins__'].__dict__['__sig__'] |
|
115 | ['__builtins__'].__dict__['__sig__'] | |
115 | getlvars = lambda fno: sys._getframe(fno+1).f_locals |
|
116 | getlvars = lambda fno: sys._getframe(fno+1).f_locals | |
116 | # trackback until we enter the IPSHELL |
|
117 | # trackback until we enter the IPSHELL | |
117 | frame_no = 1 |
|
118 | frame_no = 1 | |
118 | sig = ip.IP.__sig__ |
|
119 | sig = ip.IP.__sig__ | |
119 | fsig = ~sig |
|
120 | fsig = ~sig | |
120 | while fsig != sig : |
|
121 | while fsig != sig : | |
121 | try: |
|
122 | try: | |
122 | fsig = getsig(frame_no) |
|
123 | fsig = getsig(frame_no) | |
123 | except (AttributeError, KeyError): |
|
124 | except (AttributeError, KeyError): | |
124 | frame_no += 1 |
|
125 | frame_no += 1 | |
125 | except ValueError: |
|
126 | except ValueError: | |
126 | # stack is depleted |
|
127 | # stack is depleted | |
127 | # call did not originate from IPSHELL |
|
128 | # call did not originate from IPSHELL | |
128 | return {} |
|
129 | return {} | |
129 | first_frame = frame_no |
|
130 | first_frame = frame_no | |
130 | # walk further back until we exit from IPSHELL or deplete stack |
|
131 | # walk further back until we exit from IPSHELL or deplete stack | |
131 | try: |
|
132 | try: | |
132 | while(sig == getsig(frame_no+1)): |
|
133 | while(sig == getsig(frame_no+1)): | |
133 | frame_no += 1 |
|
134 | frame_no += 1 | |
134 | except (AttributeError, KeyError, ValueError): |
|
135 | except (AttributeError, KeyError, ValueError): | |
135 | pass |
|
136 | pass | |
136 | # merge the locals from top down hence overriding |
|
137 | # merge the locals from top down hence overriding | |
137 | # any re-definitions of variables, functions etc. |
|
138 | # any re-definitions of variables, functions etc. | |
138 | lvars = {} |
|
139 | lvars = {} | |
139 | for fno in range(frame_no, first_frame-1, -1): |
|
140 | for fno in range(frame_no, first_frame-1, -1): | |
140 | lvars.update(getlvars(fno)) |
|
141 | lvars.update(getlvars(fno)) | |
141 | #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg |
|
142 | #print '\n'*5, first_frame, frame_no, '\n', lvars, '\n'*5 #dbg | |
142 | return lvars |
|
143 | return lvars | |
143 |
|
144 | |||
144 | def _runlines(lines): |
|
145 | def _runlines(lines): | |
145 | """Run a string of one or more lines of source. |
|
146 | """Run a string of one or more lines of source. | |
146 |
|
147 | |||
147 | This method is capable of running a string containing multiple source |
|
148 | This method is capable of running a string containing multiple source | |
148 | lines, as if they had been entered at the IPython prompt. Since it |
|
149 | lines, as if they had been entered at the IPython prompt. Since it | |
149 | exposes IPython's processing machinery, the given strings can contain |
|
150 | exposes IPython's processing machinery, the given strings can contain | |
150 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
151 | magic calls (%magic), special shell access (!cmd), etc.""" | |
151 |
|
152 | |||
152 | # We must start with a clean buffer, in case this is run from an |
|
153 | # We must start with a clean buffer, in case this is run from an | |
153 | # interactive IPython session (via a magic, for example). |
|
154 | # interactive IPython session (via a magic, for example). | |
154 | ip.IP.resetbuffer() |
|
155 | ip.IP.resetbuffer() | |
155 | lines = lines.split('\n') |
|
156 | lines = lines.split('\n') | |
156 | more = 0 |
|
157 | more = 0 | |
157 | command = '' |
|
158 | command = '' | |
158 | for line in lines: |
|
159 | for line in lines: | |
159 | # skip blank lines so we don't mess up the prompt counter, but do |
|
160 | # skip blank lines so we don't mess up the prompt counter, but do | |
160 | # NOT skip even a blank line if we are in a code block (more is |
|
161 | # NOT skip even a blank line if we are in a code block (more is | |
161 | # true) |
|
162 | # true) | |
162 | # if command is not empty trim the line |
|
163 | # if command is not empty trim the line | |
163 | if command != '' : |
|
164 | if command != '' : | |
164 | line = line.strip() |
|
165 | line = line.strip() | |
165 | # add the broken line to the command |
|
166 | # add the broken line to the command | |
166 | if line and line[-1] == '\\' : |
|
167 | if line and line[-1] == '\\' : | |
167 | command += line[0:-1] + ' ' |
|
168 | command += line[0:-1] + ' ' | |
168 | more = True |
|
169 | more = True | |
169 | continue |
|
170 | continue | |
170 | else : |
|
171 | else : | |
171 | # add the last (current) line to the command |
|
172 | # add the last (current) line to the command | |
172 | command += line |
|
173 | command += line | |
173 | if command or more: |
|
174 | if command or more: | |
174 | more = ip.IP.push(ip.IP.prefilter(command,more)) |
|
175 | more = ip.IP.push(ip.IP.prefilter(command,more)) | |
175 | command = '' |
|
176 | command = '' | |
176 | # IPython's runsource returns None if there was an error |
|
177 | # IPython's runsource returns None if there was an error | |
177 | # compiling the code. This allows us to stop processing right |
|
178 | # compiling the code. This allows us to stop processing right | |
178 | # away, so the user gets the error message at the right place. |
|
179 | # away, so the user gets the error message at the right place. | |
179 | if more is None: |
|
180 | if more is None: | |
180 | break |
|
181 | break | |
181 | # final newline in case the input didn't have it, so that the code |
|
182 | # final newline in case the input didn't have it, so that the code | |
182 | # actually does get executed |
|
183 | # actually does get executed | |
183 | if more: |
|
184 | if more: | |
184 | ip.IP.push('\n') |
|
185 | ip.IP.push('\n') | |
185 |
|
186 | |||
186 | ip.IP.runlines = _runlines |
|
187 | ip.IP.runlines = _runlines | |
187 |
|
188 | |||
188 | main() |
|
189 | main() |
@@ -1,43 +1,43 b'' | |||||
1 | """ User configuration file for IPython |
|
1 | """ User configuration file for IPython | |
2 |
|
2 | |||
3 | This is a more flexible and safe way to configure ipython than *rc files |
|
3 | This is a more flexible and safe way to configure ipython than *rc files | |
4 | (ipythonrc, ipythonrc-pysh etc.) |
|
4 | (ipythonrc, ipythonrc-pysh etc.) | |
5 |
|
5 | |||
6 | This file is always imported on ipython startup. You can import the |
|
6 | This file is always imported on ipython startup. You can import the | |
7 | ipython extensions you need here (see IPython/Extensions directory). |
|
7 | ipython extensions you need here (see IPython/Extensions directory). | |
8 |
|
8 | |||
9 | Feel free to edit this file to customize your ipython experience. |
|
9 | Feel free to edit this file to customize your ipython experience. | |
10 |
|
10 | |||
11 | Note that as such this file does nothing, for backwards compatibility. |
|
11 | Note that as such this file does nothing, for backwards compatibility. | |
12 | Consult e.g. file 'ipy_profile_sh.py' for an example of the things |
|
12 | Consult e.g. file 'ipy_profile_sh.py' for an example of the things | |
13 | you can do here. |
|
13 | you can do here. | |
14 |
|
14 | |||
15 | See http://ipython.scipy.org/moin/IpythonExtensionApi for detailed |
|
15 | See http://ipython.scipy.org/moin/IpythonExtensionApi for detailed | |
16 | description on what you could do here. |
|
16 | description on what you could do here. | |
17 | """ |
|
17 | """ | |
18 |
|
18 | |||
19 | # Most of your config files and extensions will probably start with this import |
|
19 | # Most of your config files and extensions will probably start with this import | |
20 |
|
20 | |||
21 | import IPython.ipapi |
|
21 | import IPython.ipapi | |
22 | ip = IPython.ipapi.get() |
|
22 | ip = IPython.ipapi.get() | |
23 |
|
23 | |||
24 | # You probably want to uncomment this if you did %upgrade -nolegacy |
|
24 | # You probably want to uncomment this if you did %upgrade -nolegacy | |
25 | # import ipy_defaults |
|
25 | # import ipy_defaults | |
26 |
|
26 | |||
27 | def main(): |
|
27 | def main(): | |
28 | # Handy tab-completers for %cd, %run, import etc. |
|
28 | # Handy tab-completers for %cd, %run, import etc. | |
29 | # Try commenting this out if you have completion problems/slowness |
|
29 | # Try commenting this out if you have completion problems/slowness | |
30 | import ipy_stock_completers |
|
30 | # import ipy_stock_completers | |
31 |
|
31 | |||
32 | # uncomment if you want to get ipython -p sh behaviour |
|
32 | # uncomment if you want to get ipython -p sh behaviour | |
33 | # without having to use command line switches |
|
33 | # without having to use command line switches | |
34 |
|
34 | |||
35 | # import ipy_profile_sh |
|
35 | # import ipy_profile_sh | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | o = ip.options |
|
38 | o = ip.options | |
39 | # An example on how to set options |
|
39 | # An example on how to set options | |
40 | #o.autocall = 1 |
|
40 | #o.autocall = 1 | |
41 | o.system_verbose = 0 |
|
41 | o.system_verbose = 0 | |
42 |
|
42 | |||
43 | main() |
|
43 | main() |
1 | NO CONTENT: modified file |
|
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