pythonrc 758 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/python3
  2. import os
  3. import atexit
  4. import readline
  5. from pathlib import Path
  6. if readline.get_current_history_length() == 0:
  7. state_home = os.environ.get("XDG_STATE_HOME")
  8. if state_home is None:
  9. state_home = Path.home() / ".local" / "state"
  10. else:
  11. state_home = Path(state_home)
  12. history_path = state_home / "python_history"
  13. if history_path.is_dir():
  14. raise OSError(f"'{history_path}' cannot be a directory")
  15. history = str(history_path)
  16. try:
  17. readline.read_history_file(history)
  18. except OSError: # Non existent
  19. pass
  20. def write_history():
  21. try:
  22. readline.write_history_file(history)
  23. except OSError:
  24. pass
  25. atexit.register(write_history)