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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import re
  6. import sys
  7. import codeop
  8. from frontendbase import FrontEndBase
  9. from IPython.kernel.core.interpreter import Interpreter
  10.  
  11. def common_prefix(strings):
  12.     ref = strings[0]
  13.     prefix = ''
  14.     for size in range(len(ref)):
  15.         test_prefix = ref[:size + 1]
  16.         for string in strings[1:]:
  17.             if not string.startswith(test_prefix):
  18.                 return prefix
  19.         
  20.         prefix = test_prefix
  21.     
  22.     return prefix
  23.  
  24.  
  25. class LineFrontEndBase(FrontEndBase):
  26.     prompt_number = 1
  27.     last_result = dict(number = 0)
  28.     last_prompt = ''
  29.     input_buffer = ''
  30.     debug = False
  31.     banner = None
  32.     
  33.     def __init__(self, shell = None, history = None, banner = None, *args, **kwargs):
  34.         if shell is None:
  35.             shell = Interpreter()
  36.         
  37.         FrontEndBase.__init__(self, shell = shell, history = history)
  38.         if banner is not None:
  39.             self.banner = banner
  40.         
  41.  
  42.     
  43.     def start(self):
  44.         if self.banner is not None:
  45.             self.write(self.banner, refresh = False)
  46.         
  47.         self.new_prompt(self.input_prompt_template.substitute(number = 1))
  48.  
  49.     
  50.     def complete(self, line):
  51.         completions = self.shell.complete(line)
  52.         complete_sep = re.compile('[\\s\\{\\}\\[\\]\\(\\)\\=]')
  53.         if completions:
  54.             prefix = common_prefix(completions)
  55.             residual = complete_sep.split(line)[:-1]
  56.             line = line[:-len(residual)] + prefix
  57.         
  58.         return (line, completions)
  59.  
  60.     
  61.     def render_result(self, result):
  62.         if 'stdout' in result and result['stdout']:
  63.             self.write('\n' + result['stdout'])
  64.         
  65.         if 'display' in result and result['display']:
  66.             self.write('%s%s\n' % (self.output_prompt_template.substitute(number = result['number']), result['display']['pprint']))
  67.         
  68.  
  69.     
  70.     def render_error(self, failure):
  71.         self.write('\n\n' + str(failure) + '\n\n')
  72.         return failure
  73.  
  74.     
  75.     def is_complete(self, string):
  76.         if string in ('', '\n'):
  77.             return True
  78.         if len(self.input_buffer.split('\n')) > 2 and not re.findall('\\n[\\t ]*\\n[\\t ]*$', string):
  79.             return False
  80.         self.capture_output()
  81.         
  82.         try:
  83.             clean_string = string.rstrip('\n')
  84.             is_complete = codeop.compile_command(clean_string, '<string>', 'exec')
  85.             self.release_output()
  86.         except Exception:
  87.             not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
  88.             e = not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
  89.             string in ('', '\n')
  90.             is_complete = True
  91.         except:
  92.             not re.findall('\\n[\\t ]*\\n[\\t ]*$', string)
  93.  
  94.         return is_complete
  95.  
  96.     
  97.     def write(self, string, refresh = True):
  98.         print >>sys.__stderr__, string
  99.  
  100.     
  101.     def execute(self, python_string, raw_string = None):
  102.         if raw_string is None:
  103.             raw_string = python_string
  104.         
  105.         self.last_result = dict(number = self.prompt_number)
  106.         
  107.         try:
  108.             self.history.input_cache[-1] = raw_string.rstrip()
  109.             result = self.shell.execute(python_string)
  110.             self.last_result = result
  111.             self.render_result(result)
  112.         except:
  113.             self.show_traceback()
  114.         finally:
  115.             self.after_execute()
  116.  
  117.  
  118.     
  119.     def prefilter_input(self, string):
  120.         string = string.replace('\r\n', '\n')
  121.         string = string.replace('\t', '    ')
  122.         string = '\n'.join((lambda .0: for l in .0:
  123. l.rstrip())(string.split('\n')))
  124.         return string
  125.  
  126.     
  127.     def after_execute(self):
  128.         self.prompt_number += 1
  129.         self.new_prompt(self.input_prompt_template.substitute(number = self.last_result['number'] + 1))
  130.         self._add_history(None, '')
  131.         self.history_cursor = len(self.history.input_cache) - 1
  132.  
  133.     
  134.     def complete_current_input(self):
  135.         if self.debug:
  136.             print >>sys.__stdout__, 'complete_current_input',
  137.         
  138.         line = self.input_buffer
  139.         (new_line, completions) = self.complete(line)
  140.         if len(completions) > 1:
  141.             self.write_completion(completions, new_line = new_line)
  142.         elif not line == new_line:
  143.             self.input_buffer = new_line
  144.         
  145.         if self.debug:
  146.             print >>sys.__stdout__, 'line', line
  147.             print >>sys.__stdout__, 'new_line', new_line
  148.             print >>sys.__stdout__, completions
  149.         
  150.  
  151.     
  152.     def get_line_width(self):
  153.         return 80
  154.  
  155.     
  156.     def write_completion(self, possibilities, new_line = None):
  157.         if new_line is None:
  158.             new_line = self.input_buffer
  159.         
  160.         self.write('\n')
  161.         max_len = len(max(possibilities, key = len)) + 1
  162.         chars_per_line = self.get_line_width()
  163.         symbols_per_line = max(1, chars_per_line / max_len)
  164.         pos = 1
  165.         completion_string = []
  166.         for symbol in possibilities:
  167.             if pos < symbols_per_line:
  168.                 completion_string.append(symbol.ljust(max_len))
  169.                 pos += 1
  170.                 continue
  171.             completion_string.append(symbol.rstrip() + '\n')
  172.             pos = 1
  173.         
  174.         self.write(''.join(completion_string))
  175.         self.new_prompt(self.input_prompt_template.substitute(number = self.last_result['number'] + 1))
  176.         self.input_buffer = new_line
  177.  
  178.     
  179.     def new_prompt(self, prompt):
  180.         self.input_buffer = ''
  181.         self.write(prompt)
  182.  
  183.     
  184.     def continuation_prompt(self):
  185.         return '.' * (len(self.last_prompt) - 2) + ': '
  186.  
  187.     
  188.     def execute_command(self, command, hidden = False):
  189.         return self.shell.execute(command)
  190.  
  191.     
  192.     def _on_enter(self, new_line_pos = 0):
  193.         current_buffer = self.input_buffer
  194.         prompt_less_buffer = re.sub('^' + self.continuation_prompt(), '', current_buffer).replace('\n' + self.continuation_prompt(), '\n')
  195.         cleaned_buffer = self.prefilter_input(prompt_less_buffer)
  196.         if self.is_complete(cleaned_buffer):
  197.             self.execute(cleaned_buffer, raw_string = current_buffer)
  198.             return True
  199.         new_line_pos = -new_line_pos
  200.         lines = current_buffer.split('\n')[:-1]
  201.         prompt_less_lines = prompt_less_buffer.split('\n')
  202.         new_line = self.continuation_prompt() + self._get_indent_string('\n'.join(prompt_less_lines[:new_line_pos - 1]))
  203.         if len(lines) == 1:
  204.             new_line += '\t'
  205.         elif current_buffer[:-1].split('\n')[-1].rstrip().endswith(':'):
  206.             new_line += '\t'
  207.         
  208.         if new_line_pos == 0:
  209.             lines.append(new_line)
  210.         else:
  211.             lines.insert(new_line_pos, new_line)
  212.         self.input_buffer = '\n'.join(lines)
  213.  
  214.     
  215.     def _get_indent_string(self, string):
  216.         string = string.replace('\t', '    ')
  217.         string = string.split('\n')[-1]
  218.         indent_chars = len(string) - len(string.lstrip())
  219.         indent_string = '\t' * (indent_chars // 4) + ' ' * (indent_chars % 4)
  220.         return indent_string
  221.  
  222.  
  223.