Replace ~/.python_history with $XDG_CACHE_HOME/python/history

This commit is contained in:
Fernando Schauenburg 2019-11-03 02:52:18 +01:00
parent a3c3bcf1d6
commit 5bbbfb0632
2 changed files with 19 additions and 11 deletions

View file

@ -19,9 +19,9 @@ export LESS="-i -j.49 -M -R -z-2"
export LESSHISTFILE="$XDG_CACHE_HOME/less/history" export LESSHISTFILE="$XDG_CACHE_HOME/less/history"
export LESSHISTSIZE=1000 export LESSHISTSIZE=1000
export PAGER=less export PAGER=less
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
export VIMINIT='let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" | source $MYVIMRC' export VIMINIT='let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc" | source $MYVIMRC'
# Find out where Homebrew performs installations. If Homebrew is not # Find out where Homebrew performs installations. If Homebrew is not
# installed (e.g. running on Linux), assume /usr/local for our # installed (e.g. running on Linux), assume /usr/local for our
# installations. # installations.

View file

@ -1,13 +1,14 @@
import sys
"""Enable default readline configuration on interactive prompts, by """Enable default readline configuration on interactive prompts, by
registering a sys.__interactivehook__. registering a sys.__interactivehook__.
If the readline module can be imported, the hook will set the Tab key If the readline module can be imported, the hook will set the Tab key as
as completion key and register ~/.python_history as history file. completion key and register a history file, using
This can be overridden in the sitecustomize or usercustomize module, $XDG_CACHE_HOME/python/history if XDG_CACHE_HOME iset and .python_history
or in a PYTHONSTARTUP file. otherwise.
""" """
import os
import sys
def register_readline(): def register_readline():
import atexit import atexit
try: try:
@ -39,8 +40,11 @@ def register_readline():
# each interpreter exit when readline was already configured # each interpreter exit when readline was already configured
# through a PYTHONSTARTUP hook, see: # through a PYTHONSTARTUP hook, see:
# http://bugs.python.org/issue5845#msg198636 # http://bugs.python.org/issue5845#msg198636
history = os.path.join(os.path.expanduser('~'),
'.python_history') history = os.path.join(os.path.expanduser('~'), '.python_history')
if 'XDG_CACHE_HOME' in os.environ:
history = os.path.join(os.environ['XDG_CACHE_HOME'], 'python', 'history')
try: try:
readline.read_history_file(history) readline.read_history_file(history)
except OSError: except OSError:
@ -48,9 +52,13 @@ def register_readline():
def write_history(): def write_history():
try: try:
history_dir = os.path.dirname(history)
if not os.path.isdir(history_dir):
os.makedirs(history_dir)
readline.write_history_file(history) readline.write_history_file(history)
except (FileNotFoundError, PermissionError): except (FileNotFoundError, PermissionError, OSError):
# home directory does not exist or is not writable # history file does not exist or is not writable
# https://bugs.python.org/issue19891 # https://bugs.python.org/issue19891
pass pass