Get rid of old env script
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
parent
319f774457
commit
bba5b16c6f
6 changed files with 46 additions and 62 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue