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