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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import string
  6. import codeop
  7. from IPython.external import guid
  8. from IPython.frontend.zopeinterface import Interface, Attribute
  9. from IPython.kernel.core.history import FrontEndHistory
  10. from IPython.kernel.core.util import Bunch
  11. rc = Bunch()
  12. rc.prompt_in1 = 'In [$number]:  '
  13. rc.prompt_in2 = '...'
  14. rc.prompt_out = 'Out [$number]:  '
  15.  
  16. class IFrontEndFactory(Interface):
  17.     
  18.     def __call__(engine = None, history = None):
  19.         pass
  20.  
  21.  
  22.  
  23. class IFrontEnd(Interface):
  24.     Attribute('input_prompt_template', 'string.Template instance                substituteable with execute result.')
  25.     Attribute('output_prompt_template', 'string.Template instance                substituteable with execute result.')
  26.     Attribute('continuation_prompt_template', 'string.Template instance                substituteable with execute result.')
  27.     
  28.     def update_cell_prompt(result, blockID = None):
  29.         pass
  30.  
  31.     
  32.     def render_result(result):
  33.         pass
  34.  
  35.     
  36.     def render_error(failure):
  37.         pass
  38.  
  39.     
  40.     def input_prompt(number = ''):
  41.         pass
  42.  
  43.     
  44.     def output_prompt(number = ''):
  45.         pass
  46.  
  47.     
  48.     def continuation_prompt():
  49.         pass
  50.  
  51.     
  52.     def is_complete(block):
  53.         pass
  54.  
  55.     
  56.     def get_history_previous(current_block):
  57.         pass
  58.  
  59.     
  60.     def get_history_next():
  61.         pass
  62.  
  63.     
  64.     def complete(self, line):
  65.         pass
  66.  
  67.  
  68.  
  69. class FrontEndBase(object):
  70.     history_cursor = 0
  71.     input_prompt_template = string.Template(rc.prompt_in1)
  72.     output_prompt_template = string.Template(rc.prompt_out)
  73.     continuation_prompt_template = string.Template(rc.prompt_in2)
  74.     
  75.     def __init__(self, shell = None, history = None):
  76.         self.shell = shell
  77.         if history is None:
  78.             self.history = FrontEndHistory(input_cache = [
  79.                 ''])
  80.         else:
  81.             self.history = history
  82.  
  83.     
  84.     def input_prompt(self, number = ''):
  85.         return self.input_prompt_template.safe_substitute({
  86.             'number': number })
  87.  
  88.     
  89.     def continuation_prompt(self):
  90.         return self.continuation_prompt_template.safe_substitute()
  91.  
  92.     
  93.     def output_prompt(self, number = ''):
  94.         return self.output_prompt_template.safe_substitute({
  95.             'number': number })
  96.  
  97.     
  98.     def is_complete(self, block):
  99.         
  100.         try:
  101.             is_complete = codeop.compile_command(block.rstrip() + '\n\n', '<string>', 'exec')
  102.         except:
  103.             return False
  104.  
  105.         lines = block.split('\n')
  106.         if not is_complete is not None and len(lines) == 1:
  107.             pass
  108.         return str(lines[-1]) == ''
  109.  
  110.     
  111.     def execute(self, block, blockID = None):
  112.         if not self.is_complete(block):
  113.             raise Exception('Block is not compilable')
  114.         self.is_complete(block)
  115.         if blockID == None:
  116.             blockID = guid.generate()
  117.         
  118.         
  119.         try:
  120.             result = self.shell.execute(block)
  121.         except Exception:
  122.             e = None
  123.             e = self._add_block_id_for_failure(e, blockID = blockID)
  124.             e = self.update_cell_prompt(e, blockID = blockID)
  125.             e = self.render_error(e)
  126.  
  127.         result = self._add_block_id_for_result(result, blockID = blockID)
  128.         result = self.update_cell_prompt(result, blockID = blockID)
  129.         result = self.render_result(result)
  130.         return result
  131.  
  132.     
  133.     def _add_block_id_for_result(self, result, blockID):
  134.         result['blockID'] = blockID
  135.         return result
  136.  
  137.     
  138.     def _add_block_id_for_failure(self, failure, blockID):
  139.         failure.blockID = blockID
  140.         return failure
  141.  
  142.     
  143.     def _add_history(self, result, block = None):
  144.         self.history.add_items([
  145.             block])
  146.         self.history_cursor += 1
  147.         return result
  148.  
  149.     
  150.     def get_history_previous(self, current_block):
  151.         command = self.history.get_history_item(self.history_cursor - 1)
  152.         if command is not None:
  153.             if self.history_cursor + 1 == len(self.history.input_cache):
  154.                 self.history.input_cache[self.history_cursor] = current_block
  155.             
  156.             self.history_cursor -= 1
  157.         
  158.         return command
  159.  
  160.     
  161.     def get_history_next(self):
  162.         command = self.history.get_history_item(self.history_cursor + 1)
  163.         if command is not None:
  164.             self.history_cursor += 1
  165.         
  166.         return command
  167.  
  168.     
  169.     def update_cell_prompt(self, result, blockID = None):
  170.         raise NotImplementedError
  171.  
  172.     
  173.     def render_result(self, result):
  174.         raise NotImplementedError
  175.  
  176.     
  177.     def render_error(self, failure):
  178.         raise NotImplementedError
  179.  
  180.  
  181.