1234567891011121314151617181920212223242526272829 |
- '''python startup for cleaning $HOME'''
- import os
- import atexit
- import readline
- from pathlib import Path
- HISTDIR = os.getenv('XDG_CACHE_HOME', (Path.home() / '.cache'))
- HISTFILE = Path(HISTDIR) / 'python_histoty'
- HIST_LEN = 0
- def save(prev_h_len, file):
- '''save history at exit'''
- new_h_len = readline.get_current_history_length()
- readline.set_history_length(1000)
- readline.append_history_file(new_h_len - prev_h_len, file)
- if HISTFILE.exists():
- readline.read_history_file(str(HISTFILE))
- HIST_LEN = readline.get_current_history_length()
- else:
- HISTFILE.parent.mkdir(750, parents=True, exist_ok=True)
- HISTFILE.open("w").close()
- atexit.register(save, HIST_LEN, HISTFILE)
|