Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #
  4. # Copyright (c) 2008 Doug Hellmann All rights reserved.
  5. #
  6. """
  7. """
  8.  
  9. __version__ = "$Id$"
  10. #end_pymotw_header
  11.  
  12. import readline
  13. import logging
  14.  
  15. LOG_FILENAME = '/tmp/completer.log'
  16. logging.basicConfig(filename=LOG_FILENAME,
  17. level=logging.DEBUG,
  18. )
  19.  
  20. class SimpleCompleter(object):
  21.  
  22. def __init__(self, options):
  23. self.options = sorted(options)
  24. return
  25.  
  26. def complete(self, text, state):
  27. response = None
  28. if state == 0:
  29. # This is the first time for this text,
  30. # so build a match list.
  31. if text:
  32. self.matches = [s
  33. for s in self.options
  34. if s and s.startswith(text)]
  35. logging.debug('%s matches: %s',
  36. repr(text), self.matches)
  37. else:
  38. self.matches = self.options[:]
  39. logging.debug('(empty input) matches: %s',
  40. self.matches)
  41.  
  42. # Return the state'th item from the match list,
  43. # if we have that many.
  44. try:
  45. response = self.matches[state]
  46. except IndexError:
  47. response = None
  48. logging.debug('complete(%s, %s) => %s',
  49. repr(text), state, repr(response))
  50. return response
  51.  
  52. def input_loop():
  53. line = ''
  54. while line != 'stop':
  55. line = raw_input('Prompt ("stop" to quit): ')
  56. print 'Dispatch %s' % line
  57.  
  58. # Register the completer function
  59. OPTIONS = ['start', 'stop', 'list', 'print']
  60. readline.set_completer(SimpleCompleter(OPTIONS).complete)
  61.  
  62. # Use the tab key for completion
  63. readline.parse_and_bind('tab: complete')
  64.  
  65. # Prompt the user for text
  66. input_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement