def interactive(self):
utils.set_prompt(ps1="(rudra) ", ps2="... ")
import os
import readline
import rlcompleter
import atexit
histfile = os.path.join(os.environ["HOME"], ".rudrahistory")
if os.path.isfile(histfile):
readline.read_history_file(histfile)
atexit.register(readline.write_history_file, histfile)
r = self
print "Use the \"r\" object to analyze files"
vars = globals()
vars.update(locals())
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
del os, histfile, readline, rlcompleter, atexit
code.interact(banner="", local=vars)
python类read_history_file()的实例源码
def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
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 = input('Prompt ("stop" to quit): ')
if line == 'stop':
break
if line:
print('Adding {!r} to the history'.format(line))
finally:
print('Final history:', get_history_items())
readline.write_history_file(HISTORY_FILENAME)
# Register our completer function
def enable_autocomplete_and_history(adir, env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("tab: complete")
history_file = os.path.join(adir, '.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def enable_autocomplete_and_history(adir, env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("tab: complete")
history_file = os.path.join(adir, '.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def __exit__(self, ex_tp, ex_val, ex_tb):
if not haveReadline:
return False
try:
# update the history for this project
readline.write_history_file(self.historyFile)
except Exception as E:
print "Failed to write history to {0} - {1}".format(self.historyFile, E)
# put back the original history!
readline.clear_history()
readline.read_history_file(self.oldhistFile)
os.unlink(self.oldhistFile)
# and put back the old completer
readline.set_completer(self.oldCompleter)
# linegenerators, pass one of these to the "run"
# method of the CommandLineInterface object
#import ctypes
#rllib = ctypes.cdll.LoadLibrary("libreadline.so")
#rl_line_buffer = ctypes.c_char_p.in_dll(rllib, "rl_line_buffer")
#rl_done = ctypes.c_int.in_dll(rllib, "rl_done")
def setup_readline_history(histfn):
import readline
try:
readline.read_history_file(histfn)
except IOError:
# guess it doesn't exist
pass
def save():
try:
readline.write_history_file(histfn)
except IOError:
print ("bad luck, couldn't save readline history to %s" % histfn)
import atexit
atexit.register(save)
def hook_readline_hist():
try:
import readline
except ImportError:
return
histfile = os.environ['HOME'] + '/.web_develop_history' # ???????????????
readline.parse_and_bind('tab: complete')
try:
readline.read_history_file(histfile)
except IOError:
pass # It doesn't exist yet.
def savehist():
try:
readline.write_history_file(histfile)
except:
print 'Unable to save Python command history'
atexit.register(savehist)
def enable_autocomplete_and_history(adir, env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("tab: complete")
history_file = os.path.join(adir, '.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def load_history(self):
global readline
if readline is None:
try:
import readline
except ImportError:
return
if self.history_file_full_path is None:
folder = os.environ.get('USERPROFILE', '')
if not folder:
folder = os.environ.get('HOME', '')
if not folder:
folder = os.path.split(sys.argv[0])[1]
if not folder:
folder = os.path.curdir
self.history_file_full_path = os.path.join(folder,
self.history_file)
try:
if os.path.exists(self.history_file_full_path):
readline.read_history_file(self.history_file_full_path)
except IOError:
e = sys.exc_info()[1]
warnings.warn("Cannot load history file, reason: %s" % str(e))
def pre_input(self, completefn):
if self.raw_input:
if HAVE_READLINE:
import atexit
self.old_completer = readline.get_completer()
# Fix Bug #3129: Limit the history size to consume less memory
readline.set_history_length(self.historysize)
readline.set_completer(completefn)
readline.parse_and_bind(self.completekey+": complete")
try:
readline.read_history_file()
except IOError:
pass
atexit.register(readline.write_history_file)
self.havecolor = True
if mswindows and self.enablecolor:
self.cwrite = readline.GetOutputFile().write_color
else:
self.cwrite = self.stdout.write
def enable_autocomplete_and_history(adir, env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("tab: complete")
history_file = os.path.join(adir, '.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def load_history(self):
global readline
if readline is None:
try:
import readline
except ImportError:
return
if self.history_file_full_path is None:
folder = os.environ.get('USERPROFILE', '')
if not folder:
folder = os.environ.get('HOME', '')
if not folder:
folder = os.path.split(sys.argv[0])[1]
if not folder:
folder = os.path.curdir
self.history_file_full_path = os.path.join(folder,
self.history_file)
try:
if os.path.exists(self.history_file_full_path):
readline.read_history_file(self.history_file_full_path)
except IOError, e:
warnings.warn("Cannot load history file, reason: %s" % str(e))
def enable_autocomplete_and_history(adir, env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("tab: complete")
history_file = os.path.join(adir, '.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def init_readline(complete_method, histfile=None):
"""Init the readline library if available."""
try:
import readline
readline.parse_and_bind("tab: complete")
readline.set_completer(complete_method)
string = readline.get_completer_delims().replace(':', '')
readline.set_completer_delims(string)
if histfile is not None:
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
except:
print('readline is not available :-(')
def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
def init_readline(complete_method, histfile=None):
"""Init the readline library if available."""
try:
import readline
readline.parse_and_bind("tab: complete")
readline.set_completer(complete_method)
string = readline.get_completer_delims().replace(':', '')
readline.set_completer_delims(string)
if histfile is not None:
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
except:
print('readline is not available :-(')
def setup(self):
""" Initialization of third-party libraries
Setting interpreter history.
Setting appropriate completer function.
:return:
"""
if not os.path.exists(self.history_file):
open(self.history_file, 'a+').close()
readline.read_history_file(self.history_file)
readline.set_history_length(self.history_length)
atexit.register(readline.write_history_file, self.history_file)
readline.parse_and_bind('set enable-keypad on')
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
def pre_input(self, completefn):
if self.raw_input:
if HAVE_READLINE:
import atexit
self.old_completer = readline.get_completer()
# Fix Bug #3129: Limit the history size to consume less memory
readline.set_history_length(self.historysize)
readline.set_completer(completefn)
readline.parse_and_bind(self.completekey+": complete")
try:
readline.read_history_file()
except IOError:
pass
atexit.register(readline.write_history_file)
self.havecolor = True
if mswindows and self.enablecolor:
self.cwrite = readline.GetOutputFile().write_color
else:
self.cwrite = self.stdout.write
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def cmdloop_with_history(self):
"""
Better command loop, with history file and tweaked readline
completion delimiters.
"""
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
def init_readline(complete_method, histfile=None):
"""Init the readline library if available."""
try:
import readline
readline.parse_and_bind("tab: complete")
readline.set_completer(complete_method)
string = readline.get_completer_delims().replace(':', '')
readline.set_completer_delims(string)
if histfile is not None:
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
except:
print('readline is not available :-(')
def enable_autocomplete_and_history(adir,env):
try:
import rlcompleter
import atexit
import readline
except ImportError:
pass
else:
readline.parse_and_bind("bind ^I rl_complete"
if sys.platform == 'darwin'
else "tab: complete")
history_file = os.path.join(adir,'.pythonhistory')
try:
readline.read_history_file(history_file)
except IOError:
open(history_file, 'a').close()
atexit.register(readline.write_history_file, history_file)
readline.set_completer(rlcompleter.Completer(env).complete)
def init_history(self, histfile):
#readline.parse_and_bind("bind ^I rl_complete")
# Register our completer function
readline.set_completer(SimpleCompleter(G.cmmands.keys()).complete)
#readline.set_completer(TabCompleter().complete)
### Add autocompletion
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind -e")
readline.parse_and_bind("bind '\t' rl_complete")
else:
readline.parse_and_bind("tab: complete")
# Use the tab key for completion
#readline.parse_and_bind('tab: complete')
if hasattr(readline, "read_history_file"):
try:
readline.read_history_file(histfile)
except:
pass
atexit.register(self.save_history, histfile)
def _load_history(self):
try:
hist_file = self.hist_file()
if hist_file:
readline.read_history_file(os.path.expanduser(hist_file))
except FileNotFoundError:
pass