Add configuration for python
This commit is contained in:
parent
19f2786398
commit
5df789c413
7 changed files with 108 additions and 0 deletions
54
.config/python/startup.py
Normal file
54
.config/python/startup.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"""Move Python's history file to "$XDG_STATE_HOME/python/history".
|
||||
|
||||
Python 3.13+ honors the `$PYTHON_HISTORY` environment variable,
|
||||
so this file is only needed for older interpreters (e.g., via `pyenv`).
|
||||
|
||||
`$PYTHONSTARTUP` is executed in the REPL's own namespace
|
||||
=> Everything is wrapped in a function that is deleted afterwards
|
||||
to avoid polluting the prompt with temporary names
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def _setup_history():
|
||||
if sys.version_info >= (3, 13):
|
||||
return # `$PYTHON_HISTORY` handles it
|
||||
|
||||
try:
|
||||
import readline # because it is not in every build
|
||||
except ImportError:
|
||||
return
|
||||
|
||||
import atexit
|
||||
import os
|
||||
|
||||
state_home = os.environ.get("XDG_STATE_HOME") or os.path.join(
|
||||
os.path.expanduser("~"), ".local", "state"
|
||||
)
|
||||
history_file = os.path.join(state_home, "python", "history")
|
||||
|
||||
os.makedirs(os.path.dirname(history_file), exist_ok=True)
|
||||
if os.path.isdir(history_file):
|
||||
raise OSError(history_file + " must not be a directory")
|
||||
|
||||
try:
|
||||
readline.read_history_file(history_file)
|
||||
except OSError: # `$PYTHON_HISTORY` is non-existent or unreadable
|
||||
pass
|
||||
|
||||
readline.set_auto_history(True)
|
||||
readline.set_history_length(99999)
|
||||
|
||||
def write_history():
|
||||
try:
|
||||
readline.write_history_file(history_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
atexit.register(write_history) # Keep a reference to `write_history`
|
||||
|
||||
|
||||
_setup_history()
|
||||
|
||||
del _setup_history
|
||||
Loading…
Reference in a new issue