-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel.py
More file actions
186 lines (146 loc) · 5.21 KB
/
Copy pathkernel.py
File metadata and controls
186 lines (146 loc) · 5.21 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import cairosvg, os, re, subprocess, tempfile
from IPython.display import *
from metakernel import MetaKernel, Magic
class GPPMagics(Magic):
def line_CC(self, a=''):
self.kernel._vars["CC"] = a
def line_BCFLAGS(self, a=''):
self.line_BC()
self.kernel._vars["BCFLAGS"] = a
def line_CFLAGS(self, a=''):
self.line_C()
self.kernel._vars["CFLAGS"] = a
def line_CPPFLAGS(self, a=''):
self.kernel._cellcontents = ""
self.kernel._vars["CPPFLAGS"] = a
def line_LDFLAGS(self, a=''):
self.kernel._vars["LDFLAGS"] = a
def line_NFLAGS(self, a=''):
self.line_NGSPICE()
self.kernel._vars["NFLAGS"] = a
def line_OFLAGS(self, a=''):
self.line_OCTAVE()
self.kernel._vars["OFLAGS"] = a
def line_PFLAGS(self, a=''):
self.line_PUML()
self.kernel._vars["PFLAGS"] = a
def line_SQ3FLAGS(self, a=''):
self.line_SQLITE3()
self.kernel._vars["SQ3FLAGS"] = a
def line_BC(self, a=''):
self.kernel._cellcontents = "bc"
def line_C(self, a=''):
self.kernel._cellcontents = "c"
def line_NGSPICE(self, a=''):
self.kernel._cellcontents = "ngspice"
def line_OCTAVE(self, a=''):
self.kernel._cellcontents = "octave"
def line_PUML(self, a=''):
self.kernel._cellcontents = "puml"
def line_SQLITE3(self, a=''):
self.kernel._cellcontents = "sqlite3"
def line_cd(self, a=''):
os.chdir(os.path.expanduser(a)) if a and os.path.isdir(os.path.expanduser(a)) else self.kernel.Print(os.getcwd())
def line_print(self, a=''):
self.kernel.Print(self.kernel._vars.get(a, f"{a} does not exist") if a else str(self.kernel._vars))
def line_reset(self, a=''):
self.kernel._vars = {
"CC": "g++",
"CFLAGS": "-std=c23 -Wall -Wextra -march=native -O3 -fno-plt -fno-stack-protector -s -pipe",
"CPPFLAGS": "-std=c++26 -Wall -Wextra -march=native -O3 -fno-plt -fno-stack-protector -s -pipe",
"LDFLAGS": "",
"BCFLAGS": "-l",
"NFLAGS": "",
"OFLAGS": "",
"PFLAGS": "-tpng -darkmode",
"SQ3FLAGS": "",
}
class GPPKernel(MetaKernel):
implementation = 'Jupyter GPP Kernel'
implementation_version = '0.1'
language = 'c++'
language_info = {
'name': 'c++',
'mimetype': 'text/x-cpp',
'file_extension': '.cpp',
}
banner = implementation
_cellcontents = ""
_pat1 = re.compile(rb"^[\n\r]+|\s+\Z")
_pat2 = re.compile(rb"(?s)(?P<png>\x89PNG[\r\n\x1a\n].*?\x49\x45\x4e\x44\xae\x42\x60\x82)|"
rb"(?P<svg>(?:<\?xml[^>]*\?>.*?)?<svg[^>]*>.*?<\/svg>)")
def _extract(self, output):
def remove(match):
match match.lastgroup:
case "png": self.Display(Image(match.group(0)))
case "svg": self.Display(Image(cairosvg.svg2png(bytestring=match.group(0))))
return b""
return re.sub(self._pat1, b"", re.sub(self._pat2, remove, output))
def _exec_gpp(self, code):
with tempfile.NamedTemporaryFile(dir=tempfile.gettempdir(), suffix=".out") as tmpfile:
filename = tmpfile.name
lang = "c" if "c" == self._cellcontents or self._vars['CC'] in ('gcc', 'clang') else "c++"
flags = self._vars['CFLAGS' if 'c' == lang else 'CPPFLAGS']
result = subprocess.run(
f"{self._vars['CC']} {flags} -x{lang} {self._vars['LDFLAGS']} -o{filename} -&&{filename}",
input=code.encode(),
capture_output=True,
shell=True,
)
try: os.remove(filename)
except: pass
return result
def _exec_bc(self, code):
return subprocess.run(
f"bc {self._vars['BCFLAGS']} -q <<< '{code}'",
capture_output=True,
shell=True,
)
def _exec_ngspice(self, code):
return subprocess.run(
f"ngspice {self._vars['NFLAGS']} -b",
input=code.encode(),
capture_output=True,
shell=True,
)
def _exec_octave(self, code):
return subprocess.run(
f"octave {self._vars['OFLAGS']} -Wq",
input=code.encode(),
capture_output=True,
shell=True,
)
def _exec_puml(self, code):
return subprocess.run(
f"plantuml {self._vars['PFLAGS']} -p",
input=code.encode(),
capture_output=True,
shell=True,
)
def _exec_sqlite3(self, code):
return subprocess.run(
f"sqlite3 {self._vars['SQ3FLAGS']}",
input=code.encode(),
capture_output=True,
shell=True,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.register_magics(GPPMagics)
self.call_magic('%reset')
def do_execute_direct(self, code, silent=False):
match self._cellcontents:
case "bc": result = self._exec_bc(code)
case "ngspice": result = self._exec_ngspice(code)
case "octave": result = self._exec_octave(code)
case "puml": result = self._exec_puml(code)
case "sqlite3": result = self._exec_sqlite3(code)
case _: result = self._exec_gpp(code)
self._cellcontents = ""
self.kernel_resp = {
'execution_count': self.execution_count,
'payload': [],
'status': 'error' if result.returncode else 'ok',
}
if (output := result.stderr) and (output := self._extract(output)): self.Error(output.decode())
if (output := result.stdout) and (output := self._extract(output)): self.Write(output.decode())