diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-02-28 13:58:17 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-02-28 13:58:17 +0100 |
| commit | 757a8eb53de15124a149f2c9e13ec64f07c100a9 (patch) | |
| tree | 7f63c6117e0ee0b329cecbec0390f10e18206b4f /config/python/startup.py | |
| parent | d7f0a32d7c5f6935c08f9c4adcab76c1fdc16c78 (diff) | |
| download | dotfiles-757a8eb53de15124a149f2c9e13ec64f07c100a9.tar.gz dotfiles-757a8eb53de15124a149f2c9e13ec64f07c100a9.tar.bz2 dotfiles-757a8eb53de15124a149f2c9e13ec64f07c100a9.zip | |
Added python startup script to avoid writting python interpretor hisotry in ~/.python_history
Diffstat (limited to 'config/python/startup.py')
| -rw-r--r-- | config/python/startup.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/config/python/startup.py b/config/python/startup.py new file mode 100644 index 0000000..900a568 --- /dev/null +++ b/config/python/startup.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 + +import os +import sys +from pathlib import Path + + +def register_readline(): + import atexit + try: + import readline + except ImportError: + return + + # Reading the initialization (config) file may not be enough to set a + # completion key, so we set one first and then read the file. + readline_doc = getattr(readline, '__doc__', '') + if readline_doc is not None and 'libedit' in readline_doc: + readline.parse_and_bind('bind ^I rl_complete') + else: + readline.parse_and_bind('tab: complete') + + try: + readline.read_init_file() + except OSError: + # An OSError here could have many causes, but the most likely one + # is that there's no .inputrc file (or .editrc file in the case of + # Mac OS X + libedit) in the expected location. In that case, we + # want to ignore the exception. + pass + + if readline.get_current_history_length() == 0: + # If no history was loaded, default to .python_history. + # The guard is necessary to avoid doubling history size at + # each interpreter exit when readline was already configured + # through a PYTHONSTARTUP hook, see: + # http://bugs.python.org/issue5845#msg198636 + history = "" + if "XDG_CACHE_HOME" in os.environ: + history = Path(os.environ["XDG_CACHE_HOME"]) / "python" / "history" + history.parent.mkdir(parents=True, exist_ok=True) + else: + history = os.path.join(os.path.expanduser('~'), + '.python_history') + try: + readline.read_history_file(history) + except OSError: + pass + + def write_history(): + try: + readline.write_history_file(history) + except OSError: + # bpo-19891, bpo-41193: Home directory does not exist + # or is not writable, or the filesystem is read-only. + pass + + atexit.register(write_history) + + +sys.__interactivehook__ = register_readline + +sys.ps1 = "> " +sys.ps2 = ". " |
