-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_startup.py
executable file
·85 lines (61 loc) · 2.07 KB
/
python_startup.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# flake8: noqa
import datetime
import importlib
import os
import pathlib
import sys
dt = datetime
class bootstrap:
PYTHON = "PYTHON"
PTPYTHON = "PTPYTHON"
IPYTHON = "IPYTHON"
PTIPYTHON = "PTIPYTHON"
JUPYTER = "JUPYTER"
UNKNOWN = "UNKNOWN"
interpreters = (PYTHON, PTPYTHON, IPYTHON, PTIPYTHON, JUPYTER)
def __init__(self):
if self.interpreter in (self.PYTHON, self.UNKNOWN):
self.using_plain_python = True
self.setup_plain_python()
else:
self.using_plain_python = False
@property
def interpreter(self):
prog_name = pathlib.Path(sys.argv[0]).stem.upper()
if prog_name in self.interpreters:
return prog_name
return self.UNKNOWN
def log(self, message, indent=" ", prefix="## "):
print(f"{prefix}{indent}{message}")
def setup_plain_python(self):
print("")
self.log("+ configuring plain python terminal", indent="")
import readline
import rlcompleter
try:
from jedi.utils import setup_readline
setup_readline()
self.log("- setup jedi autocomplete")
except ImportError:
self.log("- failed to load jedi, falling back to readline")
readline.parse_and_bind("tab: complete")
# History log
history_file = pathlib.Path("~/.python_history").expanduser()
history_file.touch()
def save_history(path=history_file):
import readline
readline.write_history_file(path)
def load_history(path=history_file):
try:
readline.read_history_file(path)
self.log("- loaded history from ~/.python_history")
except Exception:
self.log("- failed to load history from ~/.python_history")
load_history()
import atexit
atexit.register(save_history)
print("", flush=True)
bootstrap.instance = bootstrap()
if bootstrap.instance.using_plain_python:
_bootstrap_pyterm, bootstrap = bootstrap, None
del bootstrap