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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import operator
  6. import string
  7. import sys
  8. import os
  9. from pyreadline.unicode_helper import ensure_unicode, ensure_str
  10. if 'pyreadline' in sys.modules:
  11.     pyreadline = sys.modules['pyreadline']
  12. else:
  13.     import pyreadline
  14. import lineobj
  15. import exceptions
  16.  
  17. class EscapeHistory(exceptions.Exception):
  18.     pass
  19.  
  20. from pyreadline.logger import log_sock
  21. _ignore_leading_spaces = False
  22.  
  23. class LineHistory(object):
  24.     
  25.     def __init__(self):
  26.         self.history = []
  27.         self._history_length = 100
  28.         self._history_cursor = 0
  29.         self.history_filename = os.path.expanduser('~/.history')
  30.         self.lastcommand = None
  31.         self.query = ''
  32.  
  33.     
  34.     def get_history_length(self):
  35.         value = self._history_length
  36.         log_sock('get_history_length:%d' % value, 'history')
  37.         return value
  38.  
  39.     
  40.     def set_history_length(self, value):
  41.         log_sock('set_history_length: old:%d new:%d' % (self._history_length, value), 'history')
  42.         self._history_length = value
  43.  
  44.     
  45.     def get_history_cursor(self):
  46.         value = self._history_cursor
  47.         log_sock('get_history_cursor:%d' % value, 'history')
  48.         return value
  49.  
  50.     
  51.     def set_history_cursor(self, value):
  52.         log_sock('set_history_cursor: old:%d new:%d' % (self._history_cursor, value), 'history')
  53.         self._history_cursor = value
  54.  
  55.     history_length = property(get_history_length, set_history_length)
  56.     history_cursor = property(get_history_cursor, set_history_cursor)
  57.     
  58.     def clear_history(self):
  59.         self.history[:] = []
  60.         self.history_cursor = 0
  61.  
  62.     
  63.     def read_history_file(self, filename = None):
  64.         if filename is None:
  65.             filename = self.history_filename
  66.         
  67.         
  68.         try:
  69.             for line in open(filename, 'r'):
  70.                 self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))
  71.         except IOError:
  72.             self.history = []
  73.             self.history_cursor = 0
  74.  
  75.  
  76.     
  77.     def write_history_file(self, filename = None):
  78.         if filename is None:
  79.             filename = self.history_filename
  80.         
  81.         fp = open(filename, 'wb')
  82.         for line in self.history[-(self.history_length):]:
  83.             fp.write(ensure_str(line.get_line_text()))
  84.             fp.write('\n')
  85.         
  86.         fp.close()
  87.  
  88.     
  89.     def add_history(self, line):
  90.         if not line.get_line_text():
  91.             pass
  92.         elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text():
  93.             pass
  94.         else:
  95.             self.history.append(line)
  96.         self.history_cursor = len(self.history)
  97.  
  98.     
  99.     def previous_history(self, current):
  100.         if self.history_cursor == len(self.history):
  101.             self.history.append(current.copy())
  102.         
  103.         if self.history_cursor > 0:
  104.             self.history_cursor -= 1
  105.             current.set_line(self.history[self.history_cursor].get_line_text())
  106.             current.point = lineobj.EndOfLine
  107.         
  108.  
  109.     
  110.     def next_history(self, current):
  111.         if self.history_cursor < len(self.history) - 1:
  112.             self.history_cursor += 1
  113.             current.set_line(self.history[self.history_cursor].get_line_text())
  114.         
  115.  
  116.     
  117.     def beginning_of_history(self):
  118.         self.history_cursor = 0
  119.         if len(self.history) > 0:
  120.             self.l_buffer = self.history[0]
  121.         
  122.  
  123.     
  124.     def end_of_history(self, current):
  125.         self.history_cursor = len(self.history)
  126.         current.set_line(self.history[-1].get_line_text())
  127.  
  128.     
  129.     def reverse_search_history(self, searchfor, startpos = None):
  130.         if startpos is None:
  131.             startpos = self.history_cursor
  132.         
  133.         if res:
  134.             self.history_cursor -= res[0][0]
  135.             return res[0][1].get_line_text()
  136.         return ''
  137.  
  138.     
  139.     def forward_search_history(self, searchfor, startpos = None):
  140.         if startpos is None:
  141.             startpos = self.history_cursor
  142.         
  143.         if res:
  144.             self.history_cursor += res[0][0]
  145.             return res[0][1].get_line_text()
  146.         return ''
  147.  
  148.     
  149.     def _non_i_search(self, direction, current):
  150.         c = pyreadline.rl.console
  151.         line = current.get_line_text()
  152.         query = ''
  153.         while None:
  154.             scroll = c.write_scrolling(':%s' % query)
  155.             pyreadline.rl._update_prompt_pos(scroll)
  156.             pyreadline.rl._clear_after()
  157.             event = c.getkeypress()
  158.             if event.keyinfo.keyname == 'backspace':
  159.                 if len(query) > 0:
  160.                     query = query[:-1]
  161.                 else:
  162.                     break
  163.             len(query) > 0
  164.             if event.char in string.letters + string.digits + string.punctuation + ' ':
  165.                 query += event.char
  166.                 continue
  167.             if event.keyinfo.keyname == 'return':
  168.                 break
  169.                 continue
  170.             pyreadline.rl._bell()
  171.             continue
  172.             res = ''
  173.             if query:
  174.                 if direction == -1:
  175.                     res = self.reverse_search_history(query)
  176.                 else:
  177.                     res = self.forward_search_history(query)
  178.             
  179.         return lineobj.ReadLineTextBuffer(res, point = 0)
  180.  
  181.     
  182.     def non_incremental_reverse_search_history(self, current):
  183.         return self._non_i_search(-1, current)
  184.  
  185.     
  186.     def non_incremental_forward_search_history(self, current):
  187.         return self._non_i_search(1, current)
  188.  
  189.     
  190.     def _search(self, direction, partial):
  191.         
  192.         try:
  193.             if self.lastcommand != self.history_search_forward and self.lastcommand != self.history_search_backward:
  194.                 self.query = ''.join(partial[0:partial.point].get_line_text())
  195.             
  196.             hcstart = max(self.history_cursor, 0)
  197.             hc = self.history_cursor + direction
  198.             while (direction < 0 or hc >= 0 or direction > 0) and hc < len(self.history):
  199.                 h = self.history[hc]
  200.                 if not self.query:
  201.                     self.history_cursor = hc
  202.                     result = lineobj.ReadLineTextBuffer(h, point = len(h.get_line_text()))
  203.                     return result
  204.                 if h.get_line_text().startswith(self.query) and h != partial.get_line_text():
  205.                     self.history_cursor = hc
  206.                     result = lineobj.ReadLineTextBuffer(h, point = partial.point)
  207.                     return result
  208.                 hc += direction
  209.                 continue
  210.                 h != partial.get_line_text()
  211.             if len(self.history) == 0:
  212.                 pass
  213.             elif hc >= len(self.history) and not (self.query):
  214.                 self.history_cursor = len(self.history)
  215.                 return lineobj.ReadLineTextBuffer('', point = 0)
  216.             if self.history[max(min(hcstart, len(self.history) - 1), 0)].get_line_text().startswith(self.query) and self.query:
  217.                 return lineobj.ReadLineTextBuffer(self.history[max(min(hcstart, len(self.history) - 1), 0)], point = partial.point)
  218.             return lineobj.ReadLineTextBuffer(partial, point = partial.point)
  219.             return lineobj.ReadLineTextBuffer(self.query, point = min(len(self.query), partial.point))
  220.         except IndexError:
  221.             raise 
  222.  
  223.  
  224.     
  225.     def history_search_forward(self, partial):
  226.         q = self._search(1, partial)
  227.         return q
  228.  
  229.     
  230.     def history_search_backward(self, partial):
  231.         q = self._search(-1, partial)
  232.         return q
  233.  
  234.  
  235. if __name__ == '__main__':
  236.     q = LineHistory()
  237.     RL = lineobj.ReadLineTextBuffer
  238.     q.add_history(RL('aaaa'))
  239.     q.add_history(RL('aaba'))
  240.     q.add_history(RL('aaca'))
  241.     q.add_history(RL('akca'))
  242.     q.add_history(RL('bbb'))
  243.     q.add_history(RL('ako'))
  244.  
  245.