Show More
@@ -1,4 +1,4 b'' | |||||
1 |
|
|
1 | """IPython customization API | |
2 |
|
2 | |||
3 | Your one-stop module for configuring & extending ipython |
|
3 | Your one-stop module for configuring & extending ipython | |
4 |
|
4 | |||
@@ -58,20 +58,20 b' o = ip.options' | |||||
58 | o.autocall = 2 # FULL autocall mode |
|
58 | o.autocall = 2 # FULL autocall mode | |
59 |
|
59 | |||
60 | print "done!" |
|
60 | print "done!" | |
61 | ''' |
|
61 | """ | |
|
62 | ||||
|
63 | #----------------------------------------------------------------------------- | |||
|
64 | # Modules and globals | |||
62 |
|
65 | |||
63 | # stdlib imports |
|
66 | # stdlib imports | |
64 | import __builtin__ |
|
67 | import __builtin__ | |
65 | import sys |
|
68 | import sys | |
66 |
|
69 | |||
67 | try: # Python 2.3 compatibility |
|
70 | # contains the most recently instantiated IPApi | |
68 | set |
|
71 | _RECENT_IP = None | |
69 | except NameError: |
|
|||
70 | import sets |
|
|||
71 | set = sets.Set |
|
|||
72 |
|
72 | |||
73 | # our own |
|
73 | #----------------------------------------------------------------------------- | |
74 | #from IPython.genutils import warn,error |
|
74 | # Code begins | |
75 |
|
75 | |||
76 | class TryNext(Exception): |
|
76 | class TryNext(Exception): | |
77 | """Try next hook exception. |
|
77 | """Try next hook exception. | |
@@ -109,7 +109,6 b' class IPyAutocall:' | |||||
109 | self._ip = ip |
|
109 | self._ip = ip | |
110 |
|
110 | |||
111 |
|
111 | |||
112 | # contains the most recently instantiated IPApi |
|
|||
113 |
|
112 | |||
114 | class IPythonNotRunning: |
|
113 | class IPythonNotRunning: | |
115 | """Dummy do-nothing class. |
|
114 | """Dummy do-nothing class. | |
@@ -144,8 +143,6 b' class IPythonNotRunning:' | |||||
144 | """Dummy function, which doesn't do anything and emits no warnings.""" |
|
143 | """Dummy function, which doesn't do anything and emits no warnings.""" | |
145 | pass |
|
144 | pass | |
146 |
|
145 | |||
147 | _recent = None |
|
|||
148 |
|
||||
149 |
|
146 | |||
150 | def get(allow_dummy=False,dummy_warn=True): |
|
147 | def get(allow_dummy=False,dummy_warn=True): | |
151 | """Get an IPApi object. |
|
148 | """Get an IPApi object. | |
@@ -159,10 +156,10 b' def get(allow_dummy=False,dummy_warn=True):' | |||||
159 | can be imported as normal modules. You can then direct all the |
|
156 | can be imported as normal modules. You can then direct all the | |
160 | configuration operations against the returned object. |
|
157 | configuration operations against the returned object. | |
161 | """ |
|
158 | """ | |
162 |
global _ |
|
159 | global _RECENT_IP | |
163 |
if allow_dummy and not _ |
|
160 | if allow_dummy and not _RECENT_IP: | |
164 |
_ |
|
161 | _RECENT_IP = IPythonNotRunning(dummy_warn) | |
165 |
return _ |
|
162 | return _RECENT_IP | |
166 |
|
163 | |||
167 | class IPApi: |
|
164 | class IPApi: | |
168 | """ The actual API class for configuring IPython |
|
165 | """ The actual API class for configuring IPython | |
@@ -173,6 +170,8 b' class IPApi:' | |||||
173 |
|
170 | |||
174 | def __init__(self,ip): |
|
171 | def __init__(self,ip): | |
175 |
|
172 | |||
|
173 | global _RECENT_IP | |||
|
174 | ||||
176 | # All attributes exposed here are considered to be the public API of |
|
175 | # All attributes exposed here are considered to be the public API of | |
177 | # IPython. As needs dictate, some of these may be wrapped as |
|
176 | # IPython. As needs dictate, some of these may be wrapped as | |
178 | # properties. |
|
177 | # properties. | |
@@ -201,8 +200,7 b' class IPApi:' | |||||
201 |
|
200 | |||
202 | self.dbg = DebugTools(self) |
|
201 | self.dbg = DebugTools(self) | |
203 |
|
202 | |||
204 | global _recent |
|
203 | _RECENT_IP = self | |
205 | _recent = self |
|
|||
206 |
|
204 | |||
207 | # Use a property for some things which are added to the instance very |
|
205 | # Use a property for some things which are added to the instance very | |
208 | # late. I don't have time right now to disentangle the initialization |
|
206 | # late. I don't have time right now to disentangle the initialization | |
@@ -218,8 +216,8 b' class IPApi:' | |||||
218 | """All configurable variables.""" |
|
216 | """All configurable variables.""" | |
219 |
|
217 | |||
220 | # catch typos by disabling new attribute creation. If new attr creation |
|
218 | # catch typos by disabling new attribute creation. If new attr creation | |
221 |
# is in fact wanted (e.g. when exposing new options), do |
|
219 | # is in fact wanted (e.g. when exposing new options), do | |
222 | # for the received rc struct. |
|
220 | # allow_new_attr(True) for the received rc struct. | |
223 |
|
221 | |||
224 | self.IP.rc.allow_new_attr(False) |
|
222 | self.IP.rc.allow_new_attr(False) | |
225 | return self.IP.rc |
|
223 | return self.IP.rc | |
@@ -267,10 +265,10 b' class IPApi:' | |||||
267 | def cleanup_ipy_script(script): |
|
265 | def cleanup_ipy_script(script): | |
268 | """ Make a script safe for _ip.runlines() |
|
266 | """ Make a script safe for _ip.runlines() | |
269 |
|
267 | |||
270 | - Removes empty lines |
|
268 | - Removes empty lines Suffixes all indented blocks that end with | |
271 |
- |
|
269 | - unindented lines with empty lines | |
272 |
|
||||
273 | """ |
|
270 | """ | |
|
271 | ||||
274 | res = [] |
|
272 | res = [] | |
275 | lines = script.splitlines() |
|
273 | lines = script.splitlines() | |
276 |
|
274 | |||
@@ -290,7 +288,8 b' class IPApi:' | |||||
290 | s.startswith('finally')): |
|
288 | s.startswith('finally')): | |
291 | return True |
|
289 | return True | |
292 |
|
290 | |||
293 |
if level > 0 and newlevel == 0 and |
|
291 | if level > 0 and newlevel == 0 and \ | |
|
292 | not is_secondary_block_start(stripped): | |||
294 | # add empty line |
|
293 | # add empty line | |
295 | res.append('') |
|
294 | res.append('') | |
296 |
|
295 | |||
@@ -305,6 +304,7 b' class IPApi:' | |||||
305 | clean=cleanup_ipy_script(script) |
|
304 | clean=cleanup_ipy_script(script) | |
306 | # print "_ip.runlines() script:\n",clean #dbg |
|
305 | # print "_ip.runlines() script:\n",clean # dbg | |
307 | self.IP.runlines(clean) |
|
306 | self.IP.runlines(clean) | |
|
307 | ||||
308 | def to_user_ns(self,vars, interactive = True): |
|
308 | def to_user_ns(self,vars, interactive = True): | |
309 | """Inject a group of variables into the IPython user namespace. |
|
309 | """Inject a group of variables into the IPython user namespace. | |
310 |
|
310 | |||
@@ -392,7 +392,6 b' class IPApi:' | |||||
392 | for name,val in vdict.iteritems(): |
|
392 | for name,val in vdict.iteritems(): | |
393 | config_ns[name] = val |
|
393 | config_ns[name] = val | |
394 |
|
394 | |||
395 |
|
||||
396 | def expand_alias(self,line): |
|
395 | def expand_alias(self,line): | |
397 | """ Expand an alias in the command line |
|
396 | """ Expand an alias in the command line | |
398 |
|
397 | |||
@@ -425,11 +424,9 b' class IPApi:' | |||||
425 |
|
424 | |||
426 | self.dbg.check_hotname(name) |
|
425 | self.dbg.check_hotname(name) | |
427 |
|
426 | |||
428 |
|
||||
429 | if name in self.IP.alias_table: |
|
427 | if name in self.IP.alias_table: | |
430 |
self.dbg.debug_stack("Alias redefinition: '%s' => '%s' (old '%s')" |
|
428 | self.dbg.debug_stack("Alias redefinition: '%s' => '%s' (old '%s')" | |
431 | (name, cmd, self.IP.alias_table[name])) |
|
429 | % (name, cmd, self.IP.alias_table[name])) | |
432 |
|
||||
433 |
|
430 | |||
434 | if callable(cmd): |
|
431 | if callable(cmd): | |
435 | self.IP.alias_table[name] = cmd |
|
432 | self.IP.alias_table[name] = cmd | |
@@ -440,8 +437,8 b' class IPApi:' | |||||
440 | if isinstance(cmd,basestring): |
|
437 | if isinstance(cmd,basestring): | |
441 | nargs = cmd.count('%s') |
|
438 | nargs = cmd.count('%s') | |
442 | if nargs>0 and cmd.find('%l')>=0: |
|
439 | if nargs>0 and cmd.find('%l')>=0: | |
443 |
raise Exception('The %s and %l specifiers are mutually |
|
440 | raise Exception('The %s and %l specifiers are mutually ' | |
444 | 'in alias definitions.') |
|
441 | 'exclusive in alias definitions.') | |
445 |
|
442 | |||
446 | self.IP.alias_table[name] = (nargs,cmd) |
|
443 | self.IP.alias_table[name] = (nargs,cmd) | |
447 | return |
|
444 | return | |
@@ -494,8 +491,8 b' class IPApi:' | |||||
494 |
|
491 | |||
495 | - run init_ipython(ip) |
|
492 | - run init_ipython(ip) | |
496 | - run ipython_firstrun(ip) |
|
493 | - run ipython_firstrun(ip) | |
497 |
|
||||
498 | """ |
|
494 | """ | |
|
495 | ||||
499 | if mod in self.extensions: |
|
496 | if mod in self.extensions: | |
500 | # just to make sure we don't init it twice |
|
497 | # just to make sure we don't init it twice | |
501 | # note that if you 'load' a module that has already been |
|
498 | # note that if you 'load' a module that has already been | |
@@ -545,6 +542,7 b' class DebugTools:' | |||||
545 | if name in self.hotnames: |
|
542 | if name in self.hotnames: | |
546 | self.debug_stack( "HotName '%s' caught" % name) |
|
543 | self.debug_stack( "HotName '%s' caught" % name) | |
547 |
|
544 | |||
|
545 | ||||
548 | def launch_new_instance(user_ns = None,shellclass = None): |
|
546 | def launch_new_instance(user_ns = None,shellclass = None): | |
549 | """ Make and start a new ipython instance. |
|
547 | """ Make and start a new ipython instance. | |
550 |
|
548 |
General Comments 0
You need to be logged in to leave comments.
Login now