startup.py 708 B

1234567891011121314151617181920212223242526272829
  1. '''python startup for cleaning $HOME'''
  2. import os
  3. import atexit
  4. import readline
  5. from pathlib import Path
  6. HISTDIR = os.getenv('XDG_CACHE_HOME', (Path.home() / '.cache'))
  7. HISTFILE = Path(HISTDIR) / 'python_histoty'
  8. HIST_LEN = 0
  9. def save(prev_h_len, file):
  10. '''save history at exit'''
  11. new_h_len = readline.get_current_history_length()
  12. readline.set_history_length(1000)
  13. readline.append_history_file(new_h_len - prev_h_len, file)
  14. if HISTFILE.exists():
  15. readline.read_history_file(str(HISTFILE))
  16. HIST_LEN = readline.get_current_history_length()
  17. else:
  18. HISTFILE.parent.mkdir(750, parents=True, exist_ok=True)
  19. HISTFILE.open("w").close()
  20. atexit.register(save, HIST_LEN, HISTFILE)