home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1900 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  10.6 KB  |  309 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import __builtin__
  5. import sys
  6. _RECENT_IP = None
  7.  
  8. class TryNext(Exception):
  9.     
  10.     def __init__(self, *args, **kwargs):
  11.         self.args = args
  12.         self.kwargs = kwargs
  13.  
  14.  
  15.  
  16. class UsageError(Exception):
  17.     pass
  18.  
  19.  
  20. class IPyAutocall:
  21.     
  22.     def set_ip(self, ip):
  23.         self._ip = ip
  24.  
  25.  
  26.  
  27. class IPythonNotRunning:
  28.     
  29.     def __init__(self, warn = True):
  30.         if warn:
  31.             self.dummy = self._dummy_warn
  32.         else:
  33.             self.dummy = self._dummy_silent
  34.  
  35.     
  36.     def __str__(self):
  37.         return '<IPythonNotRunning>'
  38.  
  39.     __repr__ = __str__
  40.     
  41.     def __getattr__(self, name):
  42.         return self.dummy
  43.  
  44.     
  45.     def _dummy_warn(self, *args, **kw):
  46.         print 'IPython is not running, this is a dummy no-op function'
  47.  
  48.     
  49.     def _dummy_silent(self, *args, **kw):
  50.         pass
  51.  
  52.  
  53.  
  54. def get(allow_dummy = False, dummy_warn = True):
  55.     global _RECENT_IP
  56.     if allow_dummy and not _RECENT_IP:
  57.         _RECENT_IP = IPythonNotRunning(dummy_warn)
  58.     
  59.     return _RECENT_IP
  60.  
  61.  
  62. class IPApi(object):
  63.     
  64.     def __init__(self, ip):
  65.         global _RECENT_IP
  66.         self.magic = ip.ipmagic
  67.         self.system = ip.system
  68.         self.set_hook = ip.set_hook
  69.         self.set_custom_exc = ip.set_custom_exc
  70.         self.user_ns = ip.user_ns
  71.         self.set_crash_handler = ip.set_crash_handler
  72.         self.meta = ip.meta
  73.         self.IP = ip
  74.         self.extensions = { }
  75.         self.dbg = DebugTools(self)
  76.         _RECENT_IP = self
  77.  
  78.     
  79.     def get_db(self):
  80.         return self.IP.db
  81.  
  82.     db = property(get_db, None, None, get_db.__doc__)
  83.     
  84.     def get_options(self):
  85.         self.IP.rc.allow_new_attr(False)
  86.         return self.IP.rc
  87.  
  88.     options = property(get_options, None, None, get_options.__doc__)
  89.     
  90.     def expose_magic(self, magicname, func):
  91.         import new
  92.         im = new.instancemethod(func, self.IP, self.IP.__class__)
  93.         old = getattr(self.IP, 'magic_' + magicname, None)
  94.         if old:
  95.             self.dbg.debug_stack("Magic redefinition '%s', old %s" % (magicname, old))
  96.         
  97.         setattr(self.IP, 'magic_' + magicname, im)
  98.  
  99.     
  100.     def ex(self, cmd):
  101.         exec cmd in self.user_ns
  102.  
  103.     
  104.     def ev(self, expr):
  105.         return eval(expr, self.user_ns)
  106.  
  107.     
  108.     def runlines(self, lines):
  109.         
  110.         def cleanup_ipy_script(script):
  111.             res = []
  112.             lines = script.splitlines()
  113.             level = 0
  114.             for l in lines:
  115.                 lstripped = l.lstrip()
  116.                 stripped = l.strip()
  117.                 if not stripped:
  118.                     continue
  119.                 
  120.                 newlevel = len(l) - len(lstripped)
  121.                 
  122.                 def is_secondary_block_start(s):
  123.                     if not s.endswith(':'):
  124.                         return False
  125.                     if s.startswith('elif') and s.startswith('else') and s.startswith('except') or s.startswith('finally'):
  126.                         return True
  127.  
  128.                 if level > 0 and newlevel == 0 and not is_secondary_block_start(stripped):
  129.                     res.append('')
  130.                 
  131.                 res.append(l)
  132.                 level = newlevel
  133.             
  134.             return '\n'.join(res) + '\n'
  135.  
  136.         if isinstance(lines, basestring):
  137.             script = lines
  138.         else:
  139.             script = '\n'.join(lines)
  140.         clean = cleanup_ipy_script(script)
  141.         self.IP.runlines(clean)
  142.  
  143.     
  144.     def to_user_ns(self, vars, interactive = True):
  145.         if isinstance(vars, dict):
  146.             vdict = vars
  147.         elif isinstance(vars, basestring):
  148.             cf = sys._getframe(1)
  149.             vdict = { }
  150.             for name in vars.split():
  151.                 
  152.                 try:
  153.                     vdict[name] = eval(name, cf.f_globals, cf.f_locals)
  154.                 continue
  155.                 print 'could not get var. %s from %s' % (name, cf.f_code.co_name)
  156.                 continue
  157.  
  158.             
  159.         else:
  160.             raise ValueError('vars must be a string or a dict')
  161.         isinstance(vars, dict).user_ns.update(vdict)
  162.         config_ns = self.IP.user_config_ns
  163.         if interactive:
  164.             for name, val in vdict.iteritems():
  165.                 config_ns.pop(name, None)
  166.             
  167.         else:
  168.             for name, val in vdict.iteritems():
  169.                 config_ns[name] = val
  170.             
  171.  
  172.     
  173.     def expand_alias(self, line):
  174.         (pre, fn, rest) = self.IP.split_user_input(line)
  175.         res = pre + self.IP.expand_aliases(fn, rest)
  176.         return res
  177.  
  178.     
  179.     def itpl(self, s, depth = 1):
  180.         return self.IP.var_expand(s, depth)
  181.  
  182.     
  183.     def defalias(self, name, cmd):
  184.         self.dbg.check_hotname(name)
  185.         if name in self.IP.alias_table:
  186.             self.dbg.debug_stack("Alias redefinition: '%s' => '%s' (old '%s')" % (name, cmd, self.IP.alias_table[name]))
  187.         
  188.         if callable(cmd):
  189.             self.IP.alias_table[name] = cmd
  190.             import IPython.shadowns as IPython
  191.             setattr(IPython.shadowns, name, cmd)
  192.             return None
  193.         if isinstance(cmd, basestring):
  194.             nargs = cmd.count('%s')
  195.             if nargs > 0 and cmd.find('%l') >= 0:
  196.                 raise Exception('The %s and %l specifiers are mutually exclusive in alias definitions.')
  197.             cmd.find('%l') >= 0
  198.             self.IP.alias_table[name] = (nargs, cmd)
  199.             return None
  200.         self.IP.alias_table[name] = cmd
  201.  
  202.     
  203.     def defmacro(self, *args):
  204.         import IPython.macro as IPython
  205.         if len(args) == 1:
  206.             return IPython.macro.Macro(args[0])
  207.         if len(args) == 2:
  208.             self.user_ns[args[0]] = IPython.macro.Macro(args[1])
  209.         else:
  210.             return Exception('_ip.defmacro must be called with 1 or 2 arguments')
  211.         return len(args) == 1
  212.  
  213.     
  214.     def set_next_input(self, s):
  215.         self.IP.rl_next_input = s
  216.  
  217.     
  218.     def load(self, mod):
  219.         if mod in self.extensions:
  220.             return self.extensions[mod]
  221.         __import__(mod)
  222.         m = sys.modules[mod]
  223.         if hasattr(m, 'init_ipython'):
  224.             m.init_ipython(self)
  225.         
  226.         if hasattr(m, 'ipython_firstrun'):
  227.             already_loaded = self.db.get('firstrun_done', set())
  228.             if mod not in already_loaded:
  229.                 m.ipython_firstrun(self)
  230.                 already_loaded.add(mod)
  231.                 self.db['firstrun_done'] = already_loaded
  232.             
  233.         
  234.         self.extensions[mod] = m
  235.         return m
  236.  
  237.  
  238.  
  239. class DebugTools:
  240.     
  241.     def __init__(self, ip):
  242.         self.ip = ip
  243.         self.debugmode = False
  244.         self.hotnames = set()
  245.  
  246.     
  247.     def hotname(self, name_to_catch):
  248.         self.hotnames.add(name_to_catch)
  249.  
  250.     
  251.     def debug_stack(self, msg = None):
  252.         if not self.debugmode:
  253.             return None
  254.         import traceback
  255.         traceback.print_stack()
  256.  
  257.     
  258.     def check_hotname(self, name):
  259.         if name in self.hotnames:
  260.             self.debug_stack("HotName '%s' caught" % name)
  261.         
  262.  
  263.  
  264.  
  265. def launch_new_instance(user_ns = None, shellclass = None):
  266.     ses = make_session(user_ns, shellclass)
  267.     ses.mainloop()
  268.  
  269.  
  270. def make_user_ns(user_ns = None):
  271.     raise NotImplementedError
  272.  
  273.  
  274. def make_user_global_ns(ns = None):
  275.     raise NotImplementedError
  276.  
  277. _make_user_ns = make_user_ns
  278. _make_user_global_ns = make_user_global_ns
  279.  
  280. def make_user_namespaces(user_ns = None, user_global_ns = None):
  281.     if user_ns is None:
  282.         if make_user_ns is not _make_user_ns:
  283.             user_ns = make_user_ns(user_ns)
  284.         else:
  285.             user_ns = {
  286.                 '__name__': '__main__',
  287.                 '__builtins__': __builtin__ }
  288.     else:
  289.         user_ns.setdefault('__name__', '__main__')
  290.         user_ns.setdefault('__builtins__', __builtin__)
  291.     if user_global_ns is None:
  292.         if make_user_global_ns is not _make_user_global_ns:
  293.             user_global_ns = make_user_global_ns(user_global_ns)
  294.         else:
  295.             user_global_ns = user_ns
  296.     
  297.     if type(user_global_ns) is not dict:
  298.         raise TypeError('user_global_ns must be a true dict; got %r' % type(user_global_ns))
  299.     type(user_global_ns) is not dict
  300.     return (user_ns, user_global_ns)
  301.  
  302.  
  303. def make_session(user_ns = None, shellclass = None):
  304.     import IPython.Shell as IPython
  305.     if shellclass is None:
  306.         return IPython.Shell.start(user_ns)
  307.     return shellclass(user_ns = user_ns)
  308.  
  309.