diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-09-13 16:25:21 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-09-13 16:25:21 -0500 |
commit | cca25cd36c24e71b13cafee6ff3bc4f696e36eb6 (patch) | |
tree | 2acd48c157d3660e4193bb74e4cb48b9ae687711 /python | |
parent | 49fc201f65a2c285ea8d7de6cb687e53c6656b6f (diff) |
feat(python): add startup.py to force python to conform to XDG_BASE_DIR
Diffstat (limited to 'python')
-rw-r--r-- | python/.config/python/startup.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/python/.config/python/startup.py b/python/.config/python/startup.py new file mode 100644 index 0000000..15fe43c --- /dev/null +++ b/python/.config/python/startup.py @@ -0,0 +1,33 @@ +# Store interactive Python shell history in ~/.cache/python/history +# instead of ~/.python_history. +# +# Create the following .config/python/startup.py file +# and export its path using PYTHONSTARTUP environment variable: +# +# export PYTHONSTARTUP="${XDG_CONFIG_HOME:-$HOME/.config}/python/startup.py" + +import atexit +import os +import readline + +xdg_cache = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache")) +xdg_python_history = os.path.join(xdg_cache, "python", "history") + +histfile = os.getenv("PYTHONHISTFILE", xdg_python_history) +alt_histfile = os.path.expanduser("~/.python_history") + +if not os.path.exists(histfile) and os.path.exists(alt_histfile): + histfile = alt_histfile + +histfile = os.path.abspath(histfile) +cache_directory, _ = os.path.split(histfile) + +os.makedirs(cache_directory, exist_ok=True) + +try: + readline.read_history_file(histfile) + readline.set_history_length(1000) +except FileNotFoundError: + pass + +atexit.register(readline.write_history_file, histfile) |