[code.view]

[top] / python / PyMOTW / readline / readline_completer.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import readline
     import logging
     
     LOG_FILENAME = '/tmp/completer.log'
     logging.basicConfig(filename=LOG_FILENAME,
                         level=logging.DEBUG,
                         )
     
     class SimpleCompleter(object):
         
         def __init__(self, options):
             self.options = sorted(options)
             return
     
         def complete(self, text, state):
             response = None
             if state == 0:
                 # This is the first time for this text, so build a match list.
                 if text:
                     self.matches = [s 
                                     for s in self.options
                                     if s and s.startswith(text)]
                     logging.debug('%s matches: %s', repr(text), self.matches)
                 else:
                     self.matches = self.options[:]
                     logging.debug('(empty input) matches: %s', self.matches)
             
             # Return the state'th item from the match list,
             # if we have that many.
             try:
                 response = self.matches[state]
             except IndexError:
                 response = None
             logging.debug('complete(%s, %s) => %s', 
                           repr(text), state, repr(response))
             return response
     
     def input_loop():
         line = ''
         while line != 'stop':
             line = raw_input('Prompt ("stop" to quit): ')
             print 'Dispatch %s' % line
     
     # Register our completer function
     readline.set_completer(SimpleCompleter(['start', 'stop', 'list', 'print']).complete)
     
     # Use the tab key for completion
     readline.parse_and_bind('tab: complete')
     
     # Prompt the user for text
     input_loop()
     

[top] / python / PyMOTW / readline / readline_completer.py

contact | logmethods.com