home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __docformat__ = 'restructuredtext en'
- import string
- import codeop
- from IPython.external import guid
- from IPython.frontend.zopeinterface import Interface, Attribute
- from IPython.kernel.core.history import FrontEndHistory
- from IPython.kernel.core.util import Bunch
- rc = Bunch()
- rc.prompt_in1 = 'In [$number]: '
- rc.prompt_in2 = '...'
- rc.prompt_out = 'Out [$number]: '
-
- class IFrontEndFactory(Interface):
-
- def __call__(engine = None, history = None):
- pass
-
-
-
- class IFrontEnd(Interface):
- Attribute('input_prompt_template', 'string.Template instance substituteable with execute result.')
- Attribute('output_prompt_template', 'string.Template instance substituteable with execute result.')
- Attribute('continuation_prompt_template', 'string.Template instance substituteable with execute result.')
-
- def update_cell_prompt(result, blockID = None):
- pass
-
-
- def render_result(result):
- pass
-
-
- def render_error(failure):
- pass
-
-
- def input_prompt(number = ''):
- pass
-
-
- def output_prompt(number = ''):
- pass
-
-
- def continuation_prompt():
- pass
-
-
- def is_complete(block):
- pass
-
-
- def get_history_previous(current_block):
- pass
-
-
- def get_history_next():
- pass
-
-
- def complete(self, line):
- pass
-
-
-
- class FrontEndBase(object):
- history_cursor = 0
- input_prompt_template = string.Template(rc.prompt_in1)
- output_prompt_template = string.Template(rc.prompt_out)
- continuation_prompt_template = string.Template(rc.prompt_in2)
-
- def __init__(self, shell = None, history = None):
- self.shell = shell
- if history is None:
- self.history = FrontEndHistory(input_cache = [
- ''])
- else:
- self.history = history
-
-
- def input_prompt(self, number = ''):
- return self.input_prompt_template.safe_substitute({
- 'number': number })
-
-
- def continuation_prompt(self):
- return self.continuation_prompt_template.safe_substitute()
-
-
- def output_prompt(self, number = ''):
- return self.output_prompt_template.safe_substitute({
- 'number': number })
-
-
- def is_complete(self, block):
-
- try:
- is_complete = codeop.compile_command(block.rstrip() + '\n\n', '<string>', 'exec')
- except:
- return False
-
- lines = block.split('\n')
- if not is_complete is not None and len(lines) == 1:
- pass
- return str(lines[-1]) == ''
-
-
- def execute(self, block, blockID = None):
- if not self.is_complete(block):
- raise Exception('Block is not compilable')
- self.is_complete(block)
- if blockID == None:
- blockID = guid.generate()
-
-
- try:
- result = self.shell.execute(block)
- except Exception:
- e = None
- e = self._add_block_id_for_failure(e, blockID = blockID)
- e = self.update_cell_prompt(e, blockID = blockID)
- e = self.render_error(e)
-
- result = self._add_block_id_for_result(result, blockID = blockID)
- result = self.update_cell_prompt(result, blockID = blockID)
- result = self.render_result(result)
- return result
-
-
- def _add_block_id_for_result(self, result, blockID):
- result['blockID'] = blockID
- return result
-
-
- def _add_block_id_for_failure(self, failure, blockID):
- failure.blockID = blockID
- return failure
-
-
- def _add_history(self, result, block = None):
- self.history.add_items([
- block])
- self.history_cursor += 1
- return result
-
-
- def get_history_previous(self, current_block):
- command = self.history.get_history_item(self.history_cursor - 1)
- if command is not None:
- if self.history_cursor + 1 == len(self.history.input_cache):
- self.history.input_cache[self.history_cursor] = current_block
-
- self.history_cursor -= 1
-
- return command
-
-
- def get_history_next(self):
- command = self.history.get_history_item(self.history_cursor + 1)
- if command is not None:
- self.history_cursor += 1
-
- return command
-
-
- def update_cell_prompt(self, result, blockID = None):
- raise NotImplementedError
-
-
- def render_result(self, result):
- raise NotImplementedError
-
-
- def render_error(self, failure):
- raise NotImplementedError
-
-
-