Add configuration for python

Move Python's history into ~/.local/state/python
This commit is contained in:
Alexander Hess 2025-08-30 12:09:41 +02:00
parent 651b161fe1
commit 82d1591551
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
3 changed files with 55 additions and 0 deletions

53
.config/python/startup.py Normal file
View file

@ -0,0 +1,53 @@
"""Move Python's history file to "$XDG_STATE_HOME/python/history"."""
import atexit
import os
import readline
import sys
# For Python 3.13+ let `$PYTHON_HISTORY` handle it
if sys.version_info >= (3, 13):
sys.exit(0)
# For Python 2, do nothing
try:
import pathlib
except ImportError:
sys.exit(0)
if readline.get_current_history_length() == 0:
state_home = os.environ.get("XDG_STATE_HOME")
if state_home is None:
state_home = pathlib.Path.home() / ".local" / "state"
else:
state_home = pathlib.Path(state_home)
history_path = state_home / "python" / "history"
history_path.parent.mkdir(parents=True, exist_ok=True)
history_location = str(history_path)
if history_path.is_dir():
msg = history_location + " must not be a directory"
raise OSError(msg)
try:
readline.read_history_file(history_location)
except OSError: # Non existent
pass
readline.set_auto_history()
readline.set_history_length(99999)
def write_history():
try:
readline.write_history_file(history_location)
except OSError:
pass
atexit.register(write_history)

View file

@ -30,4 +30,6 @@ export VISUAL=$EDITOR
export BAT_CONFIG_PATH="$XDG_CONFIG_HOME/bat/config"
export LESSHISTFILE="$XDG_STATE_HOME/less/history"
export PSQLRC="$XDG_CONFIG_HOME/psql/psqlrc"
export PYTHON_HISTORY="$XDG_STATE_HOME/python/history"
export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py"
export VIMINIT="source $XDG_CONFIG_HOME/vim/vimrc"

View file