-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythonl
More file actions
executable file
·159 lines (128 loc) · 4.25 KB
/
Copy pathpythonl
File metadata and controls
executable file
·159 lines (128 loc) · 4.25 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
#!/bin/env python3
"""
This file is partly based on the following two examples:
- https://github.com/aroberge/ideas/blob/master/ideas/examples/french.py
- https://stackoverflow.com/a/43573798
"""
import sys
import os
import token_utils
import importlib
import code
from importlib.abc import MetaPathFinder, Loader
from importlib.util import spec_from_file_location
py_to_nl = {
"and" : "en",
"as" : "als",
"assert" : "eis",
"async" : "async",
"await" : "wachtop",
"break" : "stop",
"case" : "geval",
"class" : "klasse",
"continue": "doorgaan",
"def": "def",
"del": "verwijder",
"if": "indien",
"elif": "andien",
"else": "anders",
"except": "behalve",
"False": "Onwaar",
"finally": "uiteindelijk",
"for": "voor",
"from": "van",
"global": "globaal",
"import": "importeer",
"in": "in",
"is": "is",
"lambda": "lambda",
"match": "vergelijk",
"None": "Niets",
"nonlocal": "nietlokaal",
"not": "niet",
"or": "of",
"pass": "pas",
"raise": "hef",
"return": "retour",
"True": "Waar",
"try": "probeer",
"type": "type",
"while": "zolang",
"with": "met",
"yield": "lever",
}
nl_to_py = {nl_kwd: py_kwd for py_kwd, nl_kwd in py_to_nl.items()}
def translate_token(token, translations):
try:
token.string = translations[token.string]
except KeyError:
pass
return token
def translate_source(source, translations = nl_to_py):
source_tokens = token_utils.tokenize(source)
translated_tokens = [translate_token(token, translations) for token in source_tokens]
return token_utils.untokenize(translated_tokens)
class PynlMetaPathFinder(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if path is None or path == "":
path = [os.getcwd()] # top level import --
if "." in fullname:
*parents, name = fullname.split(".")
else:
name = fullname
for entry in path:
if os.path.isdir(os.path.join(entry, name)):
# this module has child modules
filename = os.path.join(entry, name, "__init__.pynl")
submodule_locations = [os.path.join(entry, name)]
else:
filename = os.path.join(entry, name + ".pynl")
submodule_locations = None
if not os.path.exists(filename):
continue
return spec_from_file_location(fullname, filename, loader=PynlLoader(filename),
submodule_search_locations=submodule_locations)
return None # we don't know how to import this
class PynlLoader(Loader):
def __init__(self, filename):
self.filename = filename
def create_module(self, spec):
return None # use default module creation semantics
def exec_module(self, module):
with open(self.filename) as f:
source = f.read()
translated = translate_source(source, translations=nl_to_py)
exec(translated, vars(module))
def exec_main(location, args):
with open(location) as f:
source = f.read()
translated = translate_source(source, translations=nl_to_py)
exec(translated, {'argv_external': [location] + args, '__name__': '__main__'})
def main():
sys.meta_path.insert(0, PynlMetaPathFinder())
# clear any caches
sys.path_importer_cache.clear()
importlib.invalidate_caches()
try:
# inject external arguments into the script
if argv_external == []:
sys.argv = sys.argv[:1] # Remove arguments from sys.argv besides filename
else:
for idx, x in enumerate(argv_external):
sys.argv[idx] = x
except NameError:
pass
try:
location = sys.argv[1]
exec_main(location, sys.argv[2:])
except IndexError:
# Also geen files start dan maar de pythonl shell
shell = code.InteractiveConsole(locals=globals())
native_compile = shell.compile
def compile(code, file, lines):
code = translate_source(code)
return native_compile(code,file,lines)
shell.compile = compile
shell.interact(banner=f"Pythonl (0.1.0) interactive shell {sys.version}")
if __name__ == '__main__':
main()