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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import sys
  6. import pydoc
  7. import os
  8. import re
  9. import __builtin__
  10. from IPython.ipmaker import make_IPython
  11. from IPython.ipapi import IPApi
  12. from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
  13. from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
  14. from IPython.genutils import Term
  15. from linefrontendbase import LineFrontEndBase, common_prefix
  16.  
  17. def mk_system_call(system_call_function, command):
  18.     
  19.     def my_system_call(args):
  20.         system_call_function('%s %s' % (command, args))
  21.  
  22.     my_system_call.__doc__ = 'Calls %s' % command
  23.     return my_system_call
  24.  
  25.  
  26. class PrefilterFrontEnd(LineFrontEndBase):
  27.     debug = False
  28.     
  29.     def __init__(self, ipython0 = None, argv = None, *args, **kwargs):
  30.         if argv is None:
  31.             argv = []
  32.         
  33.         iplib = iplib
  34.         import IPython
  35.         iplib.InteractiveShell.isthreaded = True
  36.         LineFrontEndBase.__init__(self, *args, **kwargs)
  37.         self.shell.output_trap = RedirectorOutputTrap(out_callback = self.write, err_callback = self.write)
  38.         self.shell.traceback_trap = SyncTracebackTrap(formatters = self.shell.traceback_trap.formatters)
  39.         self.save_output_hooks()
  40.         if ipython0 is None:
  41.             
  42.             def my_rawinput(x = None):
  43.                 return '\n'
  44.  
  45.             old_rawinput = __builtin__.raw_input
  46.             __builtin__.raw_input = my_rawinput
  47.             ipython0 = make_IPython(argv = argv, user_ns = self.shell.user_ns, user_global_ns = self.shell.user_global_ns)
  48.             __builtin__.raw_input = old_rawinput
  49.         
  50.         self.ipython0 = ipython0
  51.         self.ipython0.set_hook(('show_in_pager',), (lambda s, string: self.write('\n' + string)))
  52.         self.ipython0.write = self.write
  53.         self._ip = _ip = IPApi(self.ipython0)
  54.         self._ip.system = self.system_call
  55.         if not sys.platform.startswith('win'):
  56.             self.ipython0.magic_ls = mk_system_call(self.system_call, 'ls -CF')
  57.         
  58.         self.release_output()
  59.         if 'banner' not in kwargs and self.banner is None:
  60.             self.banner = self.ipython0.BANNER
  61.         
  62.         self.start()
  63.  
  64.     
  65.     def show_traceback(self):
  66.         self.ipython0.showtraceback(tb_offset = -1)
  67.         self.release_output()
  68.  
  69.     
  70.     def execute(self, python_string, raw_string = None):
  71.         if self.debug:
  72.             print 'Executing Python code:', repr(python_string)
  73.         
  74.         self.capture_output()
  75.         LineFrontEndBase.execute(self, python_string, raw_string = raw_string)
  76.         self.release_output()
  77.  
  78.     
  79.     def save_output_hooks(self):
  80.         self._PrefilterFrontEnd__old_cout_write = Term.cout.write
  81.         self._PrefilterFrontEnd__old_cerr_write = Term.cerr.write
  82.         self._PrefilterFrontEnd__old_stdout = sys.stdout
  83.         self._PrefilterFrontEnd__old_stderr = sys.stderr
  84.         self._PrefilterFrontEnd__old_help_output = pydoc.help.output
  85.         self._PrefilterFrontEnd__old_display_hook = sys.displayhook
  86.  
  87.     
  88.     def capture_output(self):
  89.         self.save_output_hooks()
  90.         Term.cout.write = self.write
  91.         Term.cerr.write = self.write
  92.         sys.stdout = Term.cout
  93.         sys.stderr = Term.cerr
  94.         pydoc.help.output = self.shell.output_trap.out
  95.  
  96.     
  97.     def release_output(self):
  98.         Term.cout.write = self._PrefilterFrontEnd__old_cout_write
  99.         Term.cerr.write = self._PrefilterFrontEnd__old_cerr_write
  100.         sys.stdout = self._PrefilterFrontEnd__old_stdout
  101.         sys.stderr = self._PrefilterFrontEnd__old_stderr
  102.         pydoc.help.output = self._PrefilterFrontEnd__old_help_output
  103.         sys.displayhook = self._PrefilterFrontEnd__old_display_hook
  104.  
  105.     
  106.     def complete(self, line):
  107.         word = self._get_completion_text(line)
  108.         completions = self.ipython0.complete(word)
  109.         
  110.         key = lambda x: x.replace('_', '')
  111.         completions.sort(key = key)
  112.         if completions:
  113.             prefix = common_prefix(completions)
  114.             line = line[:-len(word)] + prefix
  115.         
  116.         return (line, completions)
  117.  
  118.     
  119.     def prefilter_input(self, input_string):
  120.         input_string = LineFrontEndBase.prefilter_input(self, input_string)
  121.         filtered_lines = []
  122.         self.capture_output()
  123.         self.last_result = dict(number = self.prompt_number)
  124.         
  125.         try:
  126.             for line in input_string.split('\n'):
  127.                 filtered_lines.append(self.ipython0.prefilter(line, False).rstrip())
  128.         except:
  129.             self.ipython0.showsyntaxerror()
  130.             self.after_execute()
  131.         finally:
  132.             self.release_output()
  133.  
  134.         filtered_string = '\n'.join(filtered_lines)
  135.         return filtered_string
  136.  
  137.     
  138.     def system_call(self, command_string):
  139.         return os.system(command_string)
  140.  
  141.     
  142.     def do_exit(self):
  143.         self.ipython0.atexit_operations()
  144.  
  145.     
  146.     def _get_completion_text(self, line):
  147.         expression = '\\s|=|,|:|\\((?!.*\\))|\\[(?!.*\\])|\\{(?!.*\\})'
  148.         complete_sep = re.compile(expression)
  149.         text = complete_sep.split(line)[-1]
  150.         return text
  151.  
  152.  
  153.