home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import __builtin__
- import sys
- _RECENT_IP = None
-
- class TryNext(Exception):
-
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kwargs = kwargs
-
-
-
- class UsageError(Exception):
- pass
-
-
- class IPyAutocall:
-
- def set_ip(self, ip):
- self._ip = ip
-
-
-
- class IPythonNotRunning:
-
- def __init__(self, warn = True):
- if warn:
- self.dummy = self._dummy_warn
- else:
- self.dummy = self._dummy_silent
-
-
- def __str__(self):
- return '<IPythonNotRunning>'
-
- __repr__ = __str__
-
- def __getattr__(self, name):
- return self.dummy
-
-
- def _dummy_warn(self, *args, **kw):
- print 'IPython is not running, this is a dummy no-op function'
-
-
- def _dummy_silent(self, *args, **kw):
- pass
-
-
-
- def get(allow_dummy = False, dummy_warn = True):
- global _RECENT_IP
- if allow_dummy and not _RECENT_IP:
- _RECENT_IP = IPythonNotRunning(dummy_warn)
-
- return _RECENT_IP
-
-
- class IPApi(object):
-
- def __init__(self, ip):
- global _RECENT_IP
- self.magic = ip.ipmagic
- self.system = ip.system
- self.set_hook = ip.set_hook
- self.set_custom_exc = ip.set_custom_exc
- self.user_ns = ip.user_ns
- self.set_crash_handler = ip.set_crash_handler
- self.meta = ip.meta
- self.IP = ip
- self.extensions = { }
- self.dbg = DebugTools(self)
- _RECENT_IP = self
-
-
- def get_db(self):
- return self.IP.db
-
- db = property(get_db, None, None, get_db.__doc__)
-
- def get_options(self):
- self.IP.rc.allow_new_attr(False)
- return self.IP.rc
-
- options = property(get_options, None, None, get_options.__doc__)
-
- def expose_magic(self, magicname, func):
- import new
- im = new.instancemethod(func, self.IP, self.IP.__class__)
- old = getattr(self.IP, 'magic_' + magicname, None)
- if old:
- self.dbg.debug_stack("Magic redefinition '%s', old %s" % (magicname, old))
-
- setattr(self.IP, 'magic_' + magicname, im)
-
-
- def ex(self, cmd):
- exec cmd in self.user_ns
-
-
- def ev(self, expr):
- return eval(expr, self.user_ns)
-
-
- def runlines(self, lines):
-
- def cleanup_ipy_script(script):
- res = []
- lines = script.splitlines()
- level = 0
- for l in lines:
- lstripped = l.lstrip()
- stripped = l.strip()
- if not stripped:
- continue
-
- newlevel = len(l) - len(lstripped)
-
- def is_secondary_block_start(s):
- if not s.endswith(':'):
- return False
- if s.startswith('elif') and s.startswith('else') and s.startswith('except') or s.startswith('finally'):
- return True
-
- if level > 0 and newlevel == 0 and not is_secondary_block_start(stripped):
- res.append('')
-
- res.append(l)
- level = newlevel
-
- return '\n'.join(res) + '\n'
-
- if isinstance(lines, basestring):
- script = lines
- else:
- script = '\n'.join(lines)
- clean = cleanup_ipy_script(script)
- self.IP.runlines(clean)
-
-
- def to_user_ns(self, vars, interactive = True):
- if isinstance(vars, dict):
- vdict = vars
- elif isinstance(vars, basestring):
- cf = sys._getframe(1)
- vdict = { }
- for name in vars.split():
-
- try:
- vdict[name] = eval(name, cf.f_globals, cf.f_locals)
- continue
- print 'could not get var. %s from %s' % (name, cf.f_code.co_name)
- continue
-
-
- else:
- raise ValueError('vars must be a string or a dict')
- isinstance(vars, dict).user_ns.update(vdict)
- config_ns = self.IP.user_config_ns
- if interactive:
- for name, val in vdict.iteritems():
- config_ns.pop(name, None)
-
- else:
- for name, val in vdict.iteritems():
- config_ns[name] = val
-
-
-
- def expand_alias(self, line):
- (pre, fn, rest) = self.IP.split_user_input(line)
- res = pre + self.IP.expand_aliases(fn, rest)
- return res
-
-
- def itpl(self, s, depth = 1):
- return self.IP.var_expand(s, depth)
-
-
- def defalias(self, name, cmd):
- self.dbg.check_hotname(name)
- if name in self.IP.alias_table:
- self.dbg.debug_stack("Alias redefinition: '%s' => '%s' (old '%s')" % (name, cmd, self.IP.alias_table[name]))
-
- if callable(cmd):
- self.IP.alias_table[name] = cmd
- import IPython.shadowns as IPython
- setattr(IPython.shadowns, name, cmd)
- return None
- if isinstance(cmd, basestring):
- nargs = cmd.count('%s')
- if nargs > 0 and cmd.find('%l') >= 0:
- raise Exception('The %s and %l specifiers are mutually exclusive in alias definitions.')
- cmd.find('%l') >= 0
- self.IP.alias_table[name] = (nargs, cmd)
- return None
- self.IP.alias_table[name] = cmd
-
-
- def defmacro(self, *args):
- import IPython.macro as IPython
- if len(args) == 1:
- return IPython.macro.Macro(args[0])
- if len(args) == 2:
- self.user_ns[args[0]] = IPython.macro.Macro(args[1])
- else:
- return Exception('_ip.defmacro must be called with 1 or 2 arguments')
- return len(args) == 1
-
-
- def set_next_input(self, s):
- self.IP.rl_next_input = s
-
-
- def load(self, mod):
- if mod in self.extensions:
- return self.extensions[mod]
- __import__(mod)
- m = sys.modules[mod]
- if hasattr(m, 'init_ipython'):
- m.init_ipython(self)
-
- if hasattr(m, 'ipython_firstrun'):
- already_loaded = self.db.get('firstrun_done', set())
- if mod not in already_loaded:
- m.ipython_firstrun(self)
- already_loaded.add(mod)
- self.db['firstrun_done'] = already_loaded
-
-
- self.extensions[mod] = m
- return m
-
-
-
- class DebugTools:
-
- def __init__(self, ip):
- self.ip = ip
- self.debugmode = False
- self.hotnames = set()
-
-
- def hotname(self, name_to_catch):
- self.hotnames.add(name_to_catch)
-
-
- def debug_stack(self, msg = None):
- if not self.debugmode:
- return None
- import traceback
- traceback.print_stack()
-
-
- def check_hotname(self, name):
- if name in self.hotnames:
- self.debug_stack("HotName '%s' caught" % name)
-
-
-
-
- def launch_new_instance(user_ns = None, shellclass = None):
- ses = make_session(user_ns, shellclass)
- ses.mainloop()
-
-
- def make_user_ns(user_ns = None):
- raise NotImplementedError
-
-
- def make_user_global_ns(ns = None):
- raise NotImplementedError
-
- _make_user_ns = make_user_ns
- _make_user_global_ns = make_user_global_ns
-
- def make_user_namespaces(user_ns = None, user_global_ns = None):
- if user_ns is None:
- if make_user_ns is not _make_user_ns:
- user_ns = make_user_ns(user_ns)
- else:
- user_ns = {
- '__name__': '__main__',
- '__builtins__': __builtin__ }
- else:
- user_ns.setdefault('__name__', '__main__')
- user_ns.setdefault('__builtins__', __builtin__)
- if user_global_ns is None:
- if make_user_global_ns is not _make_user_global_ns:
- user_global_ns = make_user_global_ns(user_global_ns)
- else:
- user_global_ns = user_ns
-
- if type(user_global_ns) is not dict:
- raise TypeError('user_global_ns must be a true dict; got %r' % type(user_global_ns))
- type(user_global_ns) is not dict
- return (user_ns, user_global_ns)
-
-
- def make_session(user_ns = None, shellclass = None):
- import IPython.Shell as IPython
- if shellclass is None:
- return IPython.Shell.start(user_ns)
- return shellclass(user_ns = user_ns)
-
-