-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpythonstartup.py
More file actions
64 lines (47 loc) · 1.64 KB
/
pythonstartup.py
File metadata and controls
64 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Loaded by interactive Python sessions.
Referenced by the PYTHONSTARTUP environment variable.
For occasional use. If you are looking in here, you probably should
be using ipython instead.
See http://docs.python.org/tutorial/interactive.html
"""
from __future__ import print_function
import atexit
import os.path
import readline
import rlcompleter
try:
from xdg.BaseDirectory import xdg_cache_home
except ImportError:
# xdg not available on all platforms
# pylint: disable=C0103
print("xdg module not available, see ~/.homefiles/pythonstartup.py")
xdg_cache_home = os.path.expanduser("~/.cache")
# Including rlcompleter is required for auto-completion. The assert
# exists to prevent unused import warnings.
assert rlcompleter
# Enable auto-completion with the tab key.
readline.parse_and_bind('tab: complete')
print("Use TAB for auto-completion.")
def _history_pathname():
"""
Determine pathname of where history is stored.
Default is ~/.python_history, but we are trying to cleanup the
user's home directory.
"""
return os.path.join(xdg_cache_home, "python", "history")
def _save_history():
"""Save history to file."""
pathname = _history_pathname()
directory = os.path.dirname(pathname)
if not os.path.exists(directory):
os.mkdir(directory)
readline.write_history_file(pathname)
atexit.register(_save_history)
def _load_history():
"""Load history from file."""
history_pathname = _history_pathname()
if os.path.exists(history_pathname):
readline.read_history_file(history_pathname)
print("History loaded from {}.".format(history_pathname))
_load_history()