readline - Persistent history in python cmd module -
is there way configure cmd module python keep persistent history after interactive shell has been closed?
when press , down keys access commands entered shell on previous occasions ran python script ones have entered during session.
if cmd uses set_completer imported readline module
readline automatically keeps history of enter. need add hooks load , store history.
use readline.read_history_file(filename) read history file. use readline.write_history_file() tell readline persist history far. may want use readline.set_history_length() keep file growing without bound:
import os.path try: import readline except importerror: readline = none histfile = os.path.expanduser('~/.someconsole_history') histfile_size = 1000 class someconsole(cmd.cmd): def preloop(self): if readline , os.path.exists(histfile): readline.read_history_file(histfile) def postloop(self): if readline: readline.set_history_length(histfile_size) readline.write_history_file(histfile) i used cmd.preloop() , cmd.postloop() hooks trigger loading , saving points command loop starts , ends.
if don't have readline installed, simulate still adding precmd() method , record entered commands yourself.
Comments
Post a Comment