2023-04-10 04:08:07 +02:00
|
|
|
# This file moves Python's history file to $XDG_STATE_HOME/python/history
|
|
|
|
# Adapted from: https://unix.stackexchange.com/questions/630642/change-location-of-python-history
|
|
|
|
|
|
|
|
import os
|
|
|
|
import atexit
|
|
|
|
import readline
|
|
|
|
|
2024-07-04 11:19:15 +02:00
|
|
|
try:
|
|
|
|
import pathlib
|
|
|
|
except ImportError: # not part of python2
|
|
|
|
pass
|
|
|
|
else:
|
2023-04-10 04:08:07 +02:00
|
|
|
|
2024-07-04 11:19:15 +02:00
|
|
|
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)
|
2023-04-10 04:08:07 +02:00
|
|
|
|
2024-07-04 11:19:15 +02:00
|
|
|
history_path = state_home / "python" / "history"
|
|
|
|
if history_path.is_dir():
|
|
|
|
raise OSError(history_path + " cannot be a directory")
|
2023-04-10 04:08:07 +02:00
|
|
|
|
2024-07-04 11:19:15 +02:00
|
|
|
history = str(history_path)
|
2023-04-10 04:08:07 +02:00
|
|
|
|
|
|
|
try:
|
2024-07-04 11:19:15 +02:00
|
|
|
readline.read_history_file(history)
|
|
|
|
except OSError: # Non existent
|
2023-04-10 04:08:07 +02:00
|
|
|
pass
|
|
|
|
|
2024-07-04 11:19:15 +02:00
|
|
|
def write_history():
|
|
|
|
try:
|
|
|
|
readline.write_history_file(history)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
atexit.register(write_history)
|