home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __docformat__ = 'restructuredtext en'
- import re
- import sys
- import codeop
- from frontendbase import FrontEndBase
- from IPython.kernel.core.interpreter import Interpreter
-
- def common_prefix(strings):
- ref = strings[0]
- prefix = ''
- for size in range(len(ref)):
- test_prefix = ref[:size + 1]
- for string in strings[1:]:
- if not string.startswith(test_prefix):
- return prefix
-
- prefix = test_prefix
-
- return prefix
-
-
- class LineFrontEndBase(FrontEndBase):
- prompt_number = 1
- last_result = dict(number = 0)
- last_prompt = ''
- input_buffer = ''
- debug = False
- banner = None
-
- def __init__(self, shell = None, history = None, banner = None, *args, **kwargs):
- if shell is None:
- shell = Interpreter()
-
- FrontEndBase.__init__(self, shell = shell, history = history)
- if banner is not None:
- self.banner = banner
-
-
-
- def start(self):
- if self.banner is not None:
- self.write(self.banner, refresh = False)
-
- self.new_prompt(self.input_prompt_template.substitute(number = 1))
-
-
- def complete(self, line):
- completions = self.shell.complete(line)
- complete_sep = re.compile('[\\s\\{\\}\\[\\]\\(\\)\\=]')
- if completions:
- prefix = common_prefix(completions)
- residual = complete_sep.split(line)[:-1]
- line = line[:-len(residual)] + prefix
-
- return (line, completions)
-
-
- def render_result(self, result):
- if 'stdout' in result and result['stdout']:
- self.write('\n' + result['stdout'])
-
- if 'display' in result and result['display']:
- self.write('%s%s\n' % (self.output_prompt_template.substitute(number = result['number']), result['display']['pprint']))
-
-
-
- def render_error(self, failure):
- self.write('\n\n' + str(failure) + '\n\n')
- return failure
-
-
- def is_complete(self, string):
- if string in ('', '\n'):
- return True
- if len(self.input_buffer.split('\n')) > 2 and not re.findall('\\n[\\t ]*\\n[\\t ]*$', string):
- return False
- self.capture_output()
-
- try:
- clean_string = string.rstrip('\n')
- is_complete = codeop.compile_command(clean_string, '<string>', 'exec')
- self.release_output()
- except Exception:
- not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
- e = not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
- string in ('', '\n')
- is_complete = True
- except:
- not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
-
- return is_complete
-
-
- def write(self, string, refresh = True):
- print >>sys.__stderr__, string
-
-
- def execute(self, python_string, raw_string = None):
- if raw_string is None:
- raw_string = python_string
-
- self.last_result = dict(number = self.prompt_number)
-
- try:
- self.history.input_cache[-1] = raw_string.rstrip()
- result = self.shell.execute(python_string)
- self.last_result = result
- self.render_result(result)
- except:
- self.show_traceback()
- finally:
- self.after_execute()
-
-
-
- def prefilter_input(self, string):
- string = string.replace('\r\n', '\n')
- string = string.replace('\t', ' ')
- string = '\n'.join((lambda .0: for l in .0:
- l.rstrip())(string.split('\n')))
- return string
-
-
- def after_execute(self):
- self.prompt_number += 1
- self.new_prompt(self.input_prompt_template.substitute(number = self.last_result['number'] + 1))
- self._add_history(None, '')
- self.history_cursor = len(self.history.input_cache) - 1
-
-
- def complete_current_input(self):
- if self.debug:
- print >>sys.__stdout__, 'complete_current_input',
-
- line = self.input_buffer
- (new_line, completions) = self.complete(line)
- if len(completions) > 1:
- self.write_completion(completions, new_line = new_line)
- elif not line == new_line:
- self.input_buffer = new_line
-
- if self.debug:
- print >>sys.__stdout__, 'line', line
- print >>sys.__stdout__, 'new_line', new_line
- print >>sys.__stdout__, completions
-
-
-
- def get_line_width(self):
- return 80
-
-
- def write_completion(self, possibilities, new_line = None):
- if new_line is None:
- new_line = self.input_buffer
-
- self.write('\n')
- max_len = len(max(possibilities, key = len)) + 1
- chars_per_line = self.get_line_width()
- symbols_per_line = max(1, chars_per_line / max_len)
- pos = 1
- completion_string = []
- for symbol in possibilities:
- if pos < symbols_per_line:
- completion_string.append(symbol.ljust(max_len))
- pos += 1
- continue
- completion_string.append(symbol.rstrip() + '\n')
- pos = 1
-
- self.write(''.join(completion_string))
- self.new_prompt(self.input_prompt_template.substitute(number = self.last_result['number'] + 1))
- self.input_buffer = new_line
-
-
- def new_prompt(self, prompt):
- self.input_buffer = ''
- self.write(prompt)
-
-
- def continuation_prompt(self):
- return '.' * (len(self.last_prompt) - 2) + ': '
-
-
- def execute_command(self, command, hidden = False):
- return self.shell.execute(command)
-
-
- def _on_enter(self, new_line_pos = 0):
- current_buffer = self.input_buffer
- prompt_less_buffer = re.sub('^' + self.continuation_prompt(), '', current_buffer).replace('\n' + self.continuation_prompt(), '\n')
- cleaned_buffer = self.prefilter_input(prompt_less_buffer)
- if self.is_complete(cleaned_buffer):
- self.execute(cleaned_buffer, raw_string = current_buffer)
- return True
- new_line_pos = -new_line_pos
- lines = current_buffer.split('\n')[:-1]
- prompt_less_lines = prompt_less_buffer.split('\n')
- new_line = self.continuation_prompt() + self._get_indent_string('\n'.join(prompt_less_lines[:new_line_pos - 1]))
- if len(lines) == 1:
- new_line += '\t'
- elif current_buffer[:-1].split('\n')[-1].rstrip().endswith(':'):
- new_line += '\t'
-
- if new_line_pos == 0:
- lines.append(new_line)
- else:
- lines.insert(new_line_pos, new_line)
- self.input_buffer = '\n'.join(lines)
-
-
- def _get_indent_string(self, string):
- string = string.replace('\t', ' ')
- string = string.split('\n')[-1]
- indent_chars = len(string) - len(string.lstrip())
- indent_string = '\t' * (indent_chars // 4) + ' ' * (indent_chars % 4)
- return indent_string
-
-
-