You can accomplish this using the readline.set_pre_input_hook ([function]) mechanism of the readline module.
Here is an example that time intervals after 10 seconds without input — not implemented — are a mechanism to silence an alarm if input is provided.
- , . .
, . python2.7, python3 - .
, - input_loop(), , .
readline.c Python .
import readline
import logging
import signal
import os
LOG_FILENAME = '/tmp/completer.log'
HISTORY_FILENAME = '/tmp/completer.hist'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG,)
class YouAreTooSlow(Exception): pass
def get_history_items():
return [ readline.get_history_item(i)
for i in xrange(1, readline.get_current_history_length() + 1)
]
class HistoryCompleter(object):
def __init__(self):
self.matches = []
return
def complete(self, text, state):
response = None
if state == 0:
history_values = get_history_items()
logging.debug('history: %s', history_values)
if text:
self.matches = sorted(h
for h in history_values
if h and h.startswith(text))
else:
self.matches = []
logging.debug('matches: %s', self.matches)
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():
if os.path.exists(HISTORY_FILENAME):
readline.read_history_file(HISTORY_FILENAME)
print 'Max history file length:', readline.get_history_length()
print 'Startup history:', get_history_items()
try:
while True:
line = raw_input('Prompt ("stop" to quit): ')
if line == 'stop':
break
if line:
print 'Adding "%s" to the history' % line
finally:
print 'Final history:', get_history_items()
readline.write_history_file(HISTORY_FILENAME)
def slow_handler(signum, frame):
print 'Signal handler called with signal', signum
raise YouAreTooSlow()
def pre_input_hook():
signal.signal(signal.SIGALRM, slow_handler)
signal.alarm(10)
readline.set_pre_input_hook(pre_input_hook)
readline.set_completer(HistoryCompleter().complete)
readline.parse_and_bind('tab: complete')
input_loop()