dotfiles/home/.config/python/pythonrc.py

26 lines
753 B
Python
Raw Normal View History

2023-08-01 23:45:32 +00:00
import atexit
import os
import readline
2023-10-10 19:32:38 +00:00
from functools import partial
2023-08-01 23:45:32 +00:00
from pathlib import Path
2023-10-10 19:32:38 +00:00
from types import ModuleType
2023-08-01 23:45:32 +00:00
cache_xdg_dir = Path(os.environ.get("XDG_CACHE_HOME", str(Path("~/.cache"))))
cache_xdg_dir.mkdir(exist_ok=True, parents=True)
history_file = cache_xdg_dir.joinpath("python_history")
readline.read_history_file(history_file)
2023-10-10 19:32:38 +00:00
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.
"""
2023-08-01 23:45:32 +00:00
readline.write_history_file(history_file)
2023-10-10 19:32:38 +00:00
atexit.register(partial(write_history, readline, history_file))