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