Get rid of old env script

Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
Severin Kaderli 2023-02-08 19:40:06 +01:00
parent 319f774457
commit bba5b16c6f
Signed by: severinkaderli
GPG key ID: F419F8835B72F0C4
6 changed files with 46 additions and 62 deletions

View file

@ -1,20 +1,30 @@
import atexit
import os
import atexit
import readline
from pathlib import Path
XDG_DATA_HOME = os.environ["XDG_DATA_HOME"]
if readline.get_current_history_length() == 0:
state_home = os.environ.get("XDG_STATE_HOME")
if state_home is None:
state_home = Path.home() / ".local" / "state"
else:
state_home = Path(state_home)
if not XDG_DATA_HOME:
histfile = os.path.join(os.path.expanduser(
"~"), ".local", "share", "python", "history")
else:
histfile = os.path.join(XDG_DATA_HOME, "python", "history")
history_path = state_home / "python_history"
if history_path.is_dir():
raise OSError(f"'{history_path}' cannot be a directory")
try:
readline.set_auto_history(False)
readline.read_history_file(histfile)
readline.set_history_length(1000)
except FileNotFoundError:
pass
history = str(history_path)
atexit.register(readline.write_history_file, histfile)
try:
readline.read_history_file(history)
except OSError: # Non existent
pass
def write_history():
try:
readline.write_history_file(history)
except OSError:
pass
atexit.register(write_history)