forked from SaadBazaz/urdu_python_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.py
More file actions
83 lines (68 loc) · 2.81 KB
/
kernel.py
File metadata and controls
83 lines (68 loc) · 2.81 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
from ipykernel.kernelbase import Kernel
from ipykernel.ipkernel import IPythonKernel
from universalpython import run_module
import traceback
from io import StringIO
import sys
class UniversalPythonKernel(IPythonKernel):
implementation = 'UniversalPython'
implementation_version = '1.1'
# language_version = '0.1'
language_info = {
'name': 'python',
'version': sys.version.split()[0],
'mimetype': 'text/x-python',
'file_extension': '.py',
}
banner = "UniversalPython kernel"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.current_language = ""
def get_language_and_code(self, code):
lines = code.splitlines()
lang = self.current_language
if lines and lines[0].strip().startswith("# language:"):
lang_flag = lines[0].strip().split(":", 1)[-1].strip()
if lang_flag:
lang = lang_flag
self.current_language = lang # persist for future cells
code = "\n".join(lines[1:]) # Remove the flag line
return lang
def do_execute(self, code, silent, store_history=True, user_expressions=None,
allow_stdin=False):
error_thrown = False
compiled_code = ""
lang = self.get_language_and_code(code)
try:
compiled_code = run_module("lex", code, args = {
'translate': True,
'reverse': False,
'dictionary': '',
'source_language': lang,
'keep': False,
'keep_only': False,
'return': True,
'file': '',
'suppress_warnings': True, # Suppress warnings during compilation
})
except Exception as e:
error_thrown = True
error_message = "Error while compiling code: " + str(e)
print (error_message)
return super(UniversalPythonKernel, self).do_execute(compiled_code, silent, store_history, user_expressions,
allow_stdin)
def do_complete(self, code, cursor_pos):
error_thrown = False
lang = self.get_language_and_code(code)
compiled_code = run_module("lex", code, args = {
'translate': True,
'reverse': False,
'dictionary': '',
'source_language': lang,
'keep': False,
'keep_only': False,
'return': True,
'file': '',
'suppress_warnings': True, # Suppress warnings during compilation
})
return super(UniversalPythonKernel, self).do_complete(compiled_code, cursor_pos)