home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1818 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.3 KB  |  302 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = 0.9
  5. __author__ = 'Laurent Dufrechou'
  6. __email__ = 'laurent.dufrechou _at_ gmail.com'
  7. __license__ = 'BSD'
  8. import re
  9. import sys
  10. import os
  11. import locale
  12. from thread_ex import ThreadEx
  13.  
  14. try:
  15.     import IPython
  16. except Exception:
  17.     e = None
  18.     print 'Error importing IPython (%s)' % str(e)
  19.     raise Exception, e
  20.  
  21.  
  22. class _Helper(object):
  23.     
  24.     def __init__(self, pager):
  25.         self._pager = pager
  26.  
  27.     
  28.     def __repr__(self):
  29.         return 'Type help() for interactive help, or help(object) for help about object.'
  30.  
  31.     
  32.     def __call__(self, *args, **kwds):
  33.         
  34.         class DummyWriter(object):
  35.             
  36.             def __init__(self, pager):
  37.                 self._pager = pager
  38.  
  39.             
  40.             def write(self, data):
  41.                 self._pager(data)
  42.  
  43.  
  44.         import pydoc
  45.         pydoc.help.output = DummyWriter(self._pager)
  46.         
  47.         pydoc.help.interact = lambda : 1
  48.         return pydoc.help(*args, **kwds)
  49.  
  50.  
  51.  
  52. class _CodeExecutor(ThreadEx):
  53.     
  54.     def __init__(self, instance):
  55.         ThreadEx.__init__(self)
  56.         self.instance = instance
  57.  
  58.     
  59.     def run(self):
  60.         
  61.         try:
  62.             self.instance._doc_text = None
  63.             self.instance._help_text = None
  64.             self.instance._execute()
  65.             self.instance._after_execute()
  66.         except KeyboardInterrupt:
  67.             pass
  68.  
  69.  
  70.  
  71.  
  72. class NonBlockingIPShell(object):
  73.     
  74.     def __init__(self, argv = [], user_ns = { }, user_global_ns = None, cin = None, cout = None, cerr = None, ask_exit_handler = None):
  75.         self._IP = None
  76.         self.init_ipython0(argv, user_ns, user_global_ns, cin, cout, cerr, ask_exit_handler)
  77.         self._iter_more = 0
  78.         self._history_level = 0
  79.         self._complete_sep = re.compile('[\\s\\{\\}\\[\\]\\(\\)\\=]')
  80.         self._prompt = str(self._IP.outputcache.prompt1).strip()
  81.         self._line_to_execute = ''
  82.         self._threading = True
  83.         self._doc_text = None
  84.         self._help_text = None
  85.         self._add_button = None
  86.  
  87.     
  88.     def init_ipython0(self, argv = [], user_ns = { }, user_global_ns = None, cin = None, cout = None, cerr = None, ask_exit_handler = None):
  89.         if cin:
  90.             IPython.genutils.Term.cin = cin
  91.         
  92.         if cout:
  93.             IPython.genutils.Term.cout = cout
  94.         
  95.         if cerr:
  96.             IPython.genutils.Term.cerr = cerr
  97.         
  98.         excepthook = sys.excepthook
  99.         self.sys_displayhook_ori = sys.displayhook
  100.         self._IP = IPython.Shell.make_IPython(argv, user_ns = user_ns, user_global_ns = user_global_ns, embedded = True, shell_class = IPython.Shell.InteractiveShell)
  101.         self.displayhook = sys.displayhook
  102.         sys.displayhook = self.sys_displayhook_ori
  103.         loc = locale.getpreferredencoding()
  104.         if loc:
  105.             self._IP.stdin_encoding = loc
  106.         
  107.         self._IP.set_hook('show_in_pager', self._pager)
  108.         self._IP.set_hook('shell_hook', self._shell)
  109.         IPython.iplib.raw_input_original = self._raw_input_original
  110.         self._IP.exit = ask_exit_handler
  111.         self._IP.user_ns['help'] = _Helper(self._pager_help)
  112.         ip = IPython.ipapi.get()
  113.         
  114.         def bypass_magic(self, arg):
  115.             print '%this magic is currently disabled.'
  116.  
  117.         ip.expose_magic('cpaste', bypass_magic)
  118.         import __builtin__
  119.         __builtin__.raw_input = self._raw_input
  120.         sys.excepthook = excepthook
  121.  
  122.     
  123.     def do_execute(self, line):
  124.         self._line_to_execute = line
  125.         if self._threading:
  126.             self.ce = _CodeExecutor(self)
  127.             self.ce.start()
  128.         else:
  129.             
  130.             try:
  131.                 self._doc_text = None
  132.                 self._help_text = None
  133.                 self._execute()
  134.                 self._after_execute()
  135.             except KeyboardInterrupt:
  136.                 pass
  137.  
  138.  
  139.     
  140.     def get_threading(self):
  141.         return self._threading
  142.  
  143.     
  144.     def set_threading(self, state):
  145.         self._threading = state
  146.  
  147.     
  148.     def get_doc_text(self):
  149.         return self._doc_text
  150.  
  151.     
  152.     def get_help_text(self):
  153.         return self._help_text
  154.  
  155.     
  156.     def get_banner(self):
  157.         return self._IP.BANNER
  158.  
  159.     
  160.     def get_prompt_count(self):
  161.         return self._IP.outputcache.prompt_count
  162.  
  163.     
  164.     def get_prompt(self):
  165.         return self._prompt
  166.  
  167.     
  168.     def get_indentation(self):
  169.         return self._IP.indent_current_nsp
  170.  
  171.     
  172.     def update_namespace(self, ns_dict):
  173.         self._IP.user_ns.update(ns_dict)
  174.  
  175.     
  176.     def complete(self, line):
  177.         split_line = self._complete_sep.split(line)
  178.         possibilities = self._IP.complete(split_line[-1])
  179.         if possibilities:
  180.             
  181.             def _common_prefix(str1, str2):
  182.                 for i in range(len(str1)):
  183.                     if not str2.startswith(str1[:i + 1]):
  184.                         return str1[:i]
  185.                 
  186.                 return str1
  187.  
  188.             common_prefix = reduce(_common_prefix, possibilities)
  189.             completed = line[:-len(split_line[-1])] + common_prefix
  190.         else:
  191.             completed = line
  192.         return (completed, possibilities)
  193.  
  194.     
  195.     def history_back(self):
  196.         history = ''
  197.         while (history == '' or history == '\n') and self._history_level > 0:
  198.             if self._history_level >= 1:
  199.                 self._history_level -= 1
  200.             
  201.             history = self._get_history()
  202.         return history
  203.  
  204.     
  205.     def history_forward(self):
  206.         history = ''
  207.         while (history == '' or history == '\n') and self._history_level <= self._get_history_max_index():
  208.             if self._history_level < self._get_history_max_index():
  209.                 self._history_level += 1
  210.                 history = self._get_history()
  211.                 continue
  212.             self
  213.             if self._history_level == self._get_history_max_index():
  214.                 history = self._get_history()
  215.                 self._history_level += 1
  216.                 continue
  217.             self
  218.             history = ''
  219.         return history
  220.  
  221.     
  222.     def init_history_index(self):
  223.         self._history_level = self._get_history_max_index() + 1
  224.  
  225.     
  226.     def _after_execute(self):
  227.         pass
  228.  
  229.     
  230.     def _ask_exit(self):
  231.         pass
  232.  
  233.     
  234.     def _get_history_max_index(self):
  235.         return len(self._IP.input_hist_raw) - 1
  236.  
  237.     
  238.     def _get_history(self):
  239.         rv = self._IP.input_hist_raw[self._history_level].strip('\n')
  240.         return rv
  241.  
  242.     
  243.     def _pager_help(self, text):
  244.         pass
  245.  
  246.     
  247.     def _pager(self, IP, text):
  248.         self._doc_text = text
  249.  
  250.     
  251.     def _raw_input_original(self, prompt = ''):
  252.         return self._line_to_execute
  253.  
  254.     
  255.     def _raw_input(self, prompt = ''):
  256.         raise NotImplementedError
  257.  
  258.     
  259.     def _execute(self):
  260.         orig_stdout = sys.stdout
  261.         sys.stdout = IPython.Shell.Term.cout
  262.         
  263.         try:
  264.             line = self._IP.raw_input(None, self._iter_more)
  265.             if self._IP.autoindent:
  266.                 self._IP.readline_startup_hook(None)
  267.         except KeyboardInterrupt:
  268.             self._IP.write('\nKeyboardInterrupt\n')
  269.             self._IP.resetbuffer()
  270.             self._IP.outputcache.prompt_count -= 1
  271.             if self._IP.autoindent:
  272.                 self._IP.indent_current_nsp = 0
  273.             
  274.             self._iter_more = 0
  275.         except:
  276.             self._IP.showtraceback()
  277.  
  278.         self._IP.write(str(self._IP.outputcache.prompt_out).strip())
  279.         self._iter_more = self._IP.push(line)
  280.         if self._IP.SyntaxTB.last_syntax_error and self._IP.rc.autoedit_syntax:
  281.             self._IP.edit_syntax_error()
  282.         
  283.         if self._iter_more:
  284.             self._prompt = str(self._IP.outputcache.prompt2).strip()
  285.             if self._IP.autoindent:
  286.                 self._IP.readline_startup_hook(self._IP.pre_readline)
  287.             
  288.         else:
  289.             self._prompt = str(self._IP.outputcache.prompt1).strip()
  290.             self._IP.indent_current_nsp = 0
  291.         sys.stdout = orig_stdout
  292.  
  293.     
  294.     def _shell(self, ip, cmd):
  295.         (stdin, stdout) = os.popen4(cmd)
  296.         result = stdout.read().decode('cp437').encode(locale.getpreferredencoding())
  297.         print '\x01\x1b[1;36m\x02' + result
  298.         stdout.close()
  299.         stdin.close()
  300.  
  301.  
  302.