1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#!/usr/bin/env python
#
# Provides readline tools, including:
# - 'tab' completion
# - 'scroll' access to history across python sessions
#
# to activate, run the following in your command shell:
# export PYTHONSTARTUP=$HOME/.python
# export PYTHONHISTORY=$HOME/.pyhistory
# cp ./pythonstartup $PYTHONSTARTUP
#
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2025 The Uncertainty Quantification Foundation.
# License: 3-clause BSD. The full license text is available at:
# - https://github.com/uqfoundation/pox/blob/master/LICENSE
try:
import readline
except ImportError:
print ("Module readline not available.")
else:
import rlcompleter
if readline.__doc__ and 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
import os
import atexit
import sys
_version = 1 # {0: 'all-in-one', 1: 'different-major', 2: 'different-minor'}
_version = ".".join(str(i) for i in sys.version_info[:_version])
try:
historyPath = os.environ["PYTHONHISTORY"] + _version
except KeyError:
historyPath = os.path.expanduser("~"+os.sep+".pyhistory" + _version)
print ("Imported history from: '%s'" % historyPath)
print ("Set history file with PYTHONHISTORY.")
else:
pass
try:
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
except IOError:
print ("Error reading: '%s'" % historyPath)
print ("Skipping history.")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
atexit.register(save_history)
del _version
del os, sys, atexit, readline, rlcompleter, save_history, historyPath
|