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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import re
  7. from glob import glob
  8. import clipboard
  9. import logger
  10. import console
  11. from logger import log, log_sock
  12. from error import ReadlineError, GetSetError
  13. from pyreadline.keysyms.common import make_KeyPress_from_keydescr
  14. import pyreadline.lineeditor.lineobj as lineobj
  15. import pyreadline.lineeditor.history as history
  16. import release
  17. from modes import editingmodes
  18. in_ironpython = 'IronPython' in sys.version
  19. if in_ironpython:
  20.     import System
  21.     default_prompt = '>>> '
  22. else:
  23.     default_prompt = ''
  24.     import pdb
  25.  
  26. def quote_char(c):
  27.     if ord(c) > 0:
  28.         return c
  29.  
  30.  
  31. def inword(buffer, point):
  32.     return buffer[point:point + 1] in [
  33.         A - Za - z0 - 9]
  34.  
  35.  
  36. class Readline(object):
  37.     
  38.     def __init__(self):
  39.         self.startup_hook = None
  40.         self.pre_input_hook = None
  41.         self.completer = None
  42.         self.completer_delims = ' \t\n"\\\'`@$><=;|&{('
  43.         self.console = console.Console()
  44.         self.size = self.console.size()
  45.         self.prompt_color = None
  46.         self.command_color = None
  47.         self.selection_color = self.console.saveattr << 4
  48.         self.key_dispatch = { }
  49.         self.previous_func = None
  50.         self.first_prompt = True
  51.         self.next_meta = False
  52.         self.tabstop = 4
  53.         self.allow_ctrl_c = False
  54.         self.ctrl_c_tap_time_interval = 0.3
  55.         self.debug = False
  56.         self.begidx = 0
  57.         self.endidx = 0
  58.         self.show_all_if_ambiguous = 'off'
  59.         self.mark_directories = 'on'
  60.         self.bell_style = 'none'
  61.         self.mark = -1
  62.         self.l_buffer = lineobj.ReadLineTextBuffer('')
  63.         self._history = history.LineHistory()
  64.         self.editingmodes = [ mode(self) for mode in editingmodes ]
  65.         for mode in self.editingmodes:
  66.             mode.init_editing_mode(None)
  67.         
  68.         self.mode = self.editingmodes[0]
  69.         self.read_inputrc()
  70.         log('\n'.join(self.rl_settings_to_string()))
  71.         self.enable_ipython_paste_for_paths = True
  72.         self.enable_ipython_paste_list_of_lists = True
  73.         self.enable_win32_clipboard = True
  74.         self.paste_line_buffer = []
  75.  
  76.     
  77.     def _g(x):
  78.         
  79.         def g(self):
  80.             raise GetSetError('GET %s' % x)
  81.  
  82.         
  83.         def s(self, q):
  84.             raise GetSetError('SET %s' % x)
  85.  
  86.         return (g, s)
  87.  
  88.     line_buffer = property(*_g('line_buffer'))
  89.     line_cursor = property(*_g('line_buffer'))
  90.     undo_stack = property(*_g('undo_stack'))
  91.     history_length = property(*_g('history_length'))
  92.     history = property(*_g('history'))
  93.     history_cursor = property(*_g('history_cursor'))
  94.     
  95.     def parse_and_bind(self, string):
  96.         
  97.         try:
  98.             log('parse_and_bind("%s")' % string)
  99.             if string.startswith('#'):
  100.                 return None
  101.             if string.startswith('set'):
  102.                 m = re.compile('set\\s+([-a-zA-Z0-9]+)\\s+(.+)\\s*$').match(string)
  103.                 if m:
  104.                     var_name = m.group(1)
  105.                     val = m.group(2)
  106.                     
  107.                     try:
  108.                         setattr(self, var_name.replace('-', '_'), val)
  109.                     except AttributeError:
  110.                         string.startswith('#')
  111.                         string.startswith('#')
  112.                         log('unknown var="%s" val="%s"' % (var_name, val))
  113.                     except:
  114.                         string.startswith('#')<EXCEPTION MATCH>AttributeError
  115.                     
  116.  
  117.                 string.startswith('#')
  118.                 log('bad set "%s"' % string)
  119.                 return None
  120.             m = re.compile('\\s*(.+)\\s*:\\s*([-a-zA-Z]+)\\s*$').match(string)
  121.             if m:
  122.                 key = m.group(1)
  123.                 func_name = m.group(2)
  124.                 py_name = func_name.replace('-', '_')
  125.                 
  126.                 try:
  127.                     func = getattr(self.mode, py_name)
  128.                 except AttributeError:
  129.                     string.startswith('set')
  130.                     string.startswith('set')
  131.                     string.startswith('#')
  132.                     log('unknown func key="%s" func="%s"' % (key, func_name))
  133.                     if self.debug:
  134.                         print 'pyreadline parse_and_bind error, unknown function to bind: "%s"' % func_name
  135.                     
  136.                     return None
  137.  
  138.                 self.mode._bind_key(key, func)
  139.         except:
  140.             log('error')
  141.             raise 
  142.  
  143.  
  144.     
  145.     def get_line_buffer(self):
  146.         return self.l_buffer.get_line_text()
  147.  
  148.     
  149.     def insert_text(self, string):
  150.         self.l_buffer.insert_text(string)
  151.  
  152.     
  153.     def read_init_file(self, filename = None):
  154.         log('read_init_file("%s")' % filename)
  155.  
  156.     
  157.     def add_history(self, line):
  158.         self._history.add_history(line)
  159.  
  160.     
  161.     def get_history_length(self):
  162.         return self._history.get_history_length()
  163.  
  164.     
  165.     def set_history_length(self, length):
  166.         self._history.set_history_length(length)
  167.  
  168.     
  169.     def clear_history(self):
  170.         self._history.clear_history()
  171.  
  172.     
  173.     def read_history_file(self, filename = None):
  174.         self._history.read_history_file(filename)
  175.  
  176.     
  177.     def write_history_file(self, filename = None):
  178.         self._history.write_history_file(filename)
  179.  
  180.     
  181.     def set_completer(self, function = None):
  182.         log('set_completer')
  183.         self.completer = function
  184.  
  185.     
  186.     def get_completer(self):
  187.         log('get_completer')
  188.         return self.completer
  189.  
  190.     
  191.     def get_begidx(self):
  192.         return self.begidx
  193.  
  194.     
  195.     def get_endidx(self):
  196.         return self.endidx
  197.  
  198.     
  199.     def set_completer_delims(self, string):
  200.         self.completer_delims = string
  201.  
  202.     
  203.     def get_completer_delims(self):
  204.         return self.completer_delims
  205.  
  206.     
  207.     def set_startup_hook(self, function = None):
  208.         self.startup_hook = function
  209.  
  210.     
  211.     def set_pre_input_hook(self, function = None):
  212.         self.pre_input_hook = function
  213.  
  214.     
  215.     def rl_settings_to_string(self):
  216.         out = [
  217.             '%-20s: %s' % ('show all if ambigous', self.show_all_if_ambiguous)]
  218.         out.append('%-20s: %s' % ('mark_directories', self.mark_directories))
  219.         out.append('%-20s: %s' % ('bell_style', self.bell_style))
  220.         out.append('%-20s: %s' % ('mark_directories', self.mark_directories))
  221.         out.append('------------- key bindings ------------')
  222.         tablepat = '%-7s %-7s %-7s %-15s %-15s '
  223.         out.append(tablepat % ('Control', 'Meta', 'Shift', 'Keycode/char', 'Function'))
  224.         bindings = [ (k[0], k[1], k[2], k[3], v.__name__) for k, v in self.mode.key_dispatch.iteritems() ]
  225.         bindings.sort()
  226.         for key in bindings:
  227.             out.append(tablepat % key)
  228.         
  229.         return out
  230.  
  231.     
  232.     def _bell(self):
  233.         if self.bell_style == 'none':
  234.             pass
  235.         elif self.bell_style == 'visible':
  236.             raise NotImplementedError('Bellstyle visible is not implemented yet.')
  237.         elif self.bell_style == 'audible':
  238.             self.console.bell()
  239.         else:
  240.             raise ReadlineError('Bellstyle %s unknown.' % self.bell_style)
  241.         return self.bell_style == 'none'
  242.  
  243.     
  244.     def _clear_after(self):
  245.         c = self.console
  246.         (x, y) = c.pos()
  247.         (w, h) = c.size()
  248.         c.rectangle((x, y, w + 1, y + 1))
  249.         c.rectangle((0, y + 1, w, min(y + 3, h)))
  250.  
  251.     
  252.     def _set_cursor(self):
  253.         c = self.console
  254.         (xc, yc) = self.prompt_end_pos
  255.         (w, h) = c.size()
  256.         xc += self.l_buffer.visible_line_width()
  257.         while xc >= w:
  258.             xc -= w
  259.             yc += 1
  260.         c.pos(xc, yc)
  261.  
  262.     
  263.     def _print_prompt(self):
  264.         c = self.console
  265.         (x, y) = c.pos()
  266.         n = c.write_scrolling(self.prompt, self.prompt_color)
  267.         self.prompt_begin_pos = (x, y - n)
  268.         self.prompt_end_pos = c.pos()
  269.         self.size = c.size()
  270.  
  271.     
  272.     def _update_prompt_pos(self, n):
  273.         if n != 0:
  274.             (bx, by) = self.prompt_begin_pos
  275.             (ex, ey) = self.prompt_end_pos
  276.             self.prompt_begin_pos = (bx, by - n)
  277.             self.prompt_end_pos = (ex, ey - n)
  278.         
  279.  
  280.     
  281.     def _update_line(self):
  282.         c = self.console
  283.         c.cursor(0)
  284.         c.pos(*self.prompt_end_pos)
  285.         ltext = self.l_buffer.quoted_text()
  286.         if self.l_buffer.enable_selection and self.l_buffer.selection_mark >= 0:
  287.             start = len(self.l_buffer[:self.l_buffer.selection_mark].quoted_text())
  288.             stop = len(self.l_buffer[:self.l_buffer.point].quoted_text())
  289.             if start > stop:
  290.                 stop = start
  291.                 start = stop
  292.             
  293.             n = c.write_scrolling(ltext[:start], self.command_color)
  294.             n = c.write_scrolling(ltext[start:stop], self.selection_color)
  295.             n = c.write_scrolling(ltext[stop:], self.command_color)
  296.         else:
  297.             n = c.write_scrolling(ltext, self.command_color)
  298.         (x, y) = c.pos()
  299.         (w, h) = c.size()
  300.         if y >= h - 1 or n > 0:
  301.             c.scroll_window(-1)
  302.             c.scroll((0, 0, w, h), 0, -1)
  303.             n += 1
  304.         
  305.         self._update_prompt_pos(n)
  306.         if hasattr(c, 'clear_to_end_of_window'):
  307.             c.clear_to_end_of_window()
  308.         else:
  309.             self._clear_after()
  310.         c.cursor(1)
  311.         self._set_cursor()
  312.  
  313.     
  314.     def readline(self, prompt = ''):
  315.         return self.mode.readline(prompt)
  316.  
  317.     
  318.     def read_inputrc(self, inputrcpath = os.path.expanduser('~/pyreadlineconfig.ini')):
  319.         modes = []([ (x.mode, x) for x in self.editingmodes ])
  320.         mode = self.editingmodes[0].mode
  321.         
  322.         def setmode(name):
  323.             self.mode = modes[name]
  324.  
  325.         
  326.         def bind_key(key, name):
  327.             log('bind %s %s' % (key, name))
  328.             if hasattr(modes[mode], name):
  329.                 modes[mode]._bind_key(key, getattr(modes[mode], name))
  330.             else:
  331.                 print "Trying to bind unknown command '%s' to key '%s'" % (name, key)
  332.  
  333.         
  334.         def un_bind_key(key):
  335.             keyinfo = make_KeyPress_from_keydescr(key).tuple()
  336.             if keyinfo in modes[mode].key_dispatch:
  337.                 del modes[mode].key_dispatch[keyinfo]
  338.             
  339.  
  340.         
  341.         def bind_exit_key(key):
  342.             modes[mode]._bind_exit_key(key)
  343.  
  344.         
  345.         def un_bind_exit_key(key):
  346.             keyinfo = make_KeyPress_from_keydescr(key).tuple()
  347.             if keyinfo in modes[mode].exit_dispatch:
  348.                 del modes[mode].exit_dispatch[keyinfo]
  349.             
  350.  
  351.         
  352.         def setkill_ring_to_clipboard(killring):
  353.             import pyreadline.lineeditor.lineobj as pyreadline
  354.             pyreadline.lineeditor.lineobj.kill_ring_to_clipboard = killring
  355.  
  356.         
  357.         def sethistoryfilename(filename):
  358.             self._history.history_filename = os.path.expanduser(filename)
  359.  
  360.         
  361.         def setbellstyle(mode):
  362.             self.bell_style = mode
  363.  
  364.         
  365.         def sethistorylength(length):
  366.             self._history.history_length = int(length)
  367.  
  368.         
  369.         def allow_ctrl_c(mode):
  370.             log_sock('allow_ctrl_c:%s:%s' % (self.allow_ctrl_c, mode))
  371.             self.allow_ctrl_c = mode
  372.  
  373.         
  374.         def setbellstyle(mode):
  375.             self.bell_style = mode
  376.  
  377.         
  378.         def show_all_if_ambiguous(mode):
  379.             self.show_all_if_ambiguous = mode
  380.  
  381.         
  382.         def ctrl_c_tap_time_interval(mode):
  383.             self.ctrl_c_tap_time_interval = mode
  384.  
  385.         
  386.         def mark_directories(mode):
  387.             self.mark_directories = mode
  388.  
  389.         
  390.         def completer_delims(mode):
  391.             self.completer_delims = mode
  392.  
  393.         
  394.         def debug_output(on, filename = ('pyreadline_debug_log.txt',)):
  395.             if on in ('on', 'on_nologfile'):
  396.                 self.debug = True
  397.             
  398.             logger.start_log(on, filename)
  399.             logger.log('STARTING LOG')
  400.  
  401.         
  402.         def set_prompt_color(color):
  403.             trtable = {
  404.                 'black': 0,
  405.                 'darkred': 4,
  406.                 'darkgreen': 2,
  407.                 'darkyellow': 6,
  408.                 'darkblue': 1,
  409.                 'darkmagenta': 5,
  410.                 'darkcyan': 3,
  411.                 'gray': 7,
  412.                 'red': 12,
  413.                 'green': 10,
  414.                 'yellow': 14,
  415.                 'blue': 9,
  416.                 'magenta': 13,
  417.                 'cyan': 11,
  418.                 'white': 15 }
  419.             self.prompt_color = trtable.get(color.lower(), 7)
  420.  
  421.         
  422.         def set_input_color(color):
  423.             trtable = {
  424.                 'black': 0,
  425.                 'darkred': 4,
  426.                 'darkgreen': 2,
  427.                 'darkyellow': 6,
  428.                 'darkblue': 1,
  429.                 'darkmagenta': 5,
  430.                 'darkcyan': 3,
  431.                 'gray': 7,
  432.                 'red': 12,
  433.                 'green': 10,
  434.                 'yellow': 14,
  435.                 'blue': 9,
  436.                 'magenta': 13,
  437.                 'cyan': 11,
  438.                 'white': 15 }
  439.             self.command_color = trtable.get(color.lower(), 7)
  440.  
  441.         loc = {
  442.             'branch': release.branch,
  443.             'version': release.version,
  444.             'mode': mode,
  445.             'modes': modes,
  446.             'set_mode': setmode,
  447.             'bind_key': bind_key,
  448.             'bind_exit_key': bind_exit_key,
  449.             'un_bind_key': un_bind_key,
  450.             'un_bind_exit_key': un_bind_exit_key,
  451.             'bell_style': setbellstyle,
  452.             'mark_directories': mark_directories,
  453.             'show_all_if_ambiguous': show_all_if_ambiguous,
  454.             'completer_delims': completer_delims,
  455.             'debug_output': debug_output,
  456.             'history_filename': sethistoryfilename,
  457.             'history_length': sethistorylength,
  458.             'set_prompt_color': set_prompt_color,
  459.             'set_input_color': set_input_color,
  460.             'allow_ctrl_c': allow_ctrl_c,
  461.             'ctrl_c_tap_time_interval': ctrl_c_tap_time_interval,
  462.             'kill_ring_to_clipboard': setkill_ring_to_clipboard }
  463.         if os.path.isfile(inputrcpath):
  464.             
  465.             try:
  466.                 execfile(inputrcpath, loc, loc)
  467.             except Exception:
  468.                 ((((((((((((None, (None, (None, (None, (dict, []))))),),),),),),),),),),),)
  469.                 x = ((((((((((((None, (None, (None, (None, (dict, []))))),),),),),),),),),),),)
  470.                 raise 
  471.                 import traceback
  472.                 print >>sys.stderr, 'Error reading .pyinputrc'
  473.                 (filepath, lineno) = traceback.extract_tb(sys.exc_traceback)[1][:2]
  474.                 print >>sys.stderr, 'Line: %s in file %s' % (lineno, filepath)
  475.                 print >>sys.stderr, x
  476.                 raise ReadlineError('Error reading .pyinputrc')
  477.             except:
  478.                 ((((((((((((None, (None, (None, (None, (dict, []))))),),),),),),),),),),),)<EXCEPTION MATCH>Exception
  479.             
  480.  
  481.         ((((((((((((None, (None, (None, (None, (dict, []))))),),),),),),),),),),),)
  482.  
  483.  
  484.  
  485. def CTRL(c):
  486.     return chr(ord(c) - ord('@'))
  487.  
  488. rl = Readline()
  489.  
  490. def GetOutputFile():
  491.     return rl.console
  492.  
  493. parse_and_bind = rl.parse_and_bind
  494. get_line_buffer = rl.get_line_buffer
  495. insert_text = rl.insert_text
  496. read_init_file = rl.read_init_file
  497. add_history = rl.add_history
  498. get_history_length = rl.get_history_length
  499. set_history_length = rl.set_history_length
  500. clear_history = rl.clear_history
  501. read_history_file = rl.read_history_file
  502. write_history_file = rl.write_history_file
  503. set_completer = rl.set_completer
  504. get_completer = rl.get_completer
  505. get_begidx = rl.get_begidx
  506. get_endidx = rl.get_endidx
  507. set_completer_delims = rl.set_completer_delims
  508. get_completer_delims = rl.get_completer_delims
  509. set_startup_hook = rl.set_startup_hook
  510. set_pre_input_hook = rl.set_pre_input_hook
  511. if __name__ == '__main__':
  512.     res = [ rl.readline('In[%d] ' % i) for i in range(3) ]
  513.     print res
  514. else:
  515.     console.install_readline(rl.readline)
  516.