-
Notifications
You must be signed in to change notification settings - Fork 0
/
.pythonrc
153 lines (110 loc) · 4.33 KB
/
.pythonrc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import code
import subprocess
import sys
import os
class Prompt:
def __init__(self):
self.__prompt = lambda x=[]: subprocess.check_output(
[
"oh-my-posh",
"print",
"primary",
"--shell",
f"python ({'.'.join(str(x) for x in sys.version_info[:3])})",
]
+ x
).decode("utf-8")
def __str__(self):
return self.__prompt()
def __call__(self, x: list = []):
return self.__prompt(x)
class GlobeFilterConsole(code.InteractiveConsole):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__prompt = Prompt()
sys.ps1 = Prompt()
def runsource(self, source, filename="<input>", symbol="single"):
"""Compile and run some source in the interpreter.
Arguments are as for compile_command().
One of several things can happen:
1) The input is incorrect; compile_command() raised an
exception (SyntaxError or OverflowError). A syntax traceback
will be printed by calling the showsyntaxerror() method.
2) The input is incomplete, and more input is required;
compile_command() returned None. Nothing happens.
3) The input is complete; compile_command() returned a code
object. The code is executed by calling self.runcode() (which
also handles run-time exceptions, except for SystemExit).
The return value is True in case 2, False in the other cases (unless
an exception is raised). The return value can be used to
decide whether to use sys.ps1 or sys.ps2 to prompt the next
line.
"""
try:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
process = subprocess.run(self.buffer[-1], shell=True, capture_output=True)
return_code = process.returncode
if return_code > 0:
self.showsyntaxerror(filename)
else:
self.write(process.stdout.decode("utf-8"))
return False
if code is None:
# Case 2
return True
# Case 3
self.runcode(code)
return False
def runcode(self, code):
"""Execute a code object.
When an exception occurs, self.showtraceback() is called to
display a traceback. All exceptions are caught except
SystemExit, which is reraised.
A note about KeyboardInterrupt: this exception may occur
elsewhere in this code, and may not always be caught. The
caller should be prepared to deal with it.
"""
try:
exec(code, self.locals)
except SystemExit:
raise
except Exception:
process = subprocess.run(self.buffer[-1], shell=True, capture_output=True)
return_code = process.returncode
if return_code > 0:
self.showtraceback()
else:
self.write(process.stdout.decode("utf-8"))
def write(self, data: str):
"""Write a string.
The base implementation writes to sys.stderr; a subclass may
replace this with a different implementation.
"""
sys.stderr.write(data)
def raw_input(self, prompt: str = None):
"""Write a prompt and read a line.
The returned line does not include the trailing newline.
When the user enters the EOF key sequence, EOFError is raised.
The base implementation uses the built-in function
input(); a subclass may replace this with a different
implementation.
"""
return input(prompt or self.__prompt())
class cls(object):
def __repr__(self):
os.system("cls" if os.name == "nt" else "clear")
return ""
class exit(object):
exit = exit # original object
def __repr__(self):
self.exit() # call original
return ""
quit = exit = exit()
clear = cls = cls()
console = GlobeFilterConsole(
locals={"cls": cls, "clear": clear, "exit": exit, "quit": exit}
)
console.interact(banner="", exitmsg="")