python: fix history location & persist history

This commit is contained in:
ItsDrike 2024-06-27 19:17:44 +02:00
parent 8a6311b896
commit 46f5fa83e8
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
2 changed files with 25 additions and 24 deletions

View file

@ -57,34 +57,34 @@ in {
'';
"python/pythonrc.py".text = ''
import atexit
def is_vanilla() -> bool:
import sys
return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]
def setup_history():
import os
import atexit
import readline
from functools import partial
from pathlib import Path
from types import ModuleType
cache_xdg_dir = Path(
os.environ.get("XDG_CACHE_HOME", str(Path("~/.cache").expanduser()))
)
cache_xdg_dir.mkdir(exist_ok=True, parents=True)
if state_home := os.environ.get('XDG_STATE_HOME'):
state_home = Path(state_home)
else:
state_home = Path.home() / '.local' / 'state'
history_file = cache_xdg_dir.joinpath("python_history")
history_file.touch()
history: Path = state_home / 'python_history'
readline.read_history_file(history_file)
# https://github.com/python/cpython/issues/105694
if not history.is_file():
readline.write_history_file(str(history)) # breaks on macos + python3 without this.
readline.read_history_file(str(history))
atexit.register(readline.write_history_file, str(history))
def write_history(readline: ModuleType, history_file: Path) -> None:
"""
We need to get ``readline`` and ``history_file`` as arguments, as it
seems they get garbage collected when the function is registered and
the program ends, even though we refer to them here.
"""
readline.write_history_file(history_file)
atexit.register(partial(write_history, readline, history_file))
if is_vanilla():
setup_history()
'';
};

View file

@ -87,6 +87,7 @@
".cache/walker/history.gob"
".config/pcmanfm-qt/default/recent-files.conf"
".config/qalculate/qalculate-gtk.cfg"
".local/state/python_history"
];
persistentDataMountPoint = "/data/Data";