-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjinja_utils.py
executable file
·258 lines (222 loc) · 7.65 KB
/
jinja_utils.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from log_utils import logger_print
import subprocess
from tempfile import TemporaryDirectory
import black
# from humps import kebabize
import jinja2
import shutil
import os
import pyright_utils # for checking if really installed.
import re
# live share's triple quote issue isn't fixed.
import humps # default to snake case!
import ast
def remove_typehint(paramDef: str) -> str:
tree_def = ast.parse("def func({}): ...".format(paramDef)).body[0]
args = []
for elem in ast.walk(tree_def):
if isinstance(elem, ast.arg):
argName = elem.arg # str
args.append(argName)
return ",".join([f"{argName}={argName}" for argName in args])
def camelize_with_space(string):
return humps.camelize(string.replace(" ", "-"))
# ref: https://www.geeksforgeeks.org/python-program-to-convert-camel-case-string-to-snake-case/
def c2s(_str):
"""
Camel case to snake case.
"""
# return humps.kebabize(_str).replace("-", "_")
# res = [_str[0].lower()]
# for c in _str[1:]:
# if c in ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
# res.append("_")
# res.append(c.lower())
# else:
# res.append(c)
# return "".join(res)
return humps.decamelize(_str)
def s2c(_str, lower: bool):
"""
Snake case to camel case.
"""
# assert not _str.startswith("_")
# lst = _str.split("_")
# first_letter = lst[0][0]
# lst[0] = (first_letter.lower() if lower else first_letter.upper()) + lst[0][1:]
# for i in range(1, len(lst)):
# lst[i] = lst[i].title()
# return "".join(lst)
return getattr(humps, "camelize" if lower else "pascalize")(_str)
def s2cl(_str):
"""
Snake case to camel case (starting with lower letter).
"""
return s2c(_str, True)
def s2cu(_str):
"""
Snake case to camel case (starting with upper letter).
"""
return s2c(_str, False)
class NeverUndefined(jinja2.StrictUndefined):
def __init__(self, *args, **kwargs):
# ARGS: ("parameter 'myvar2' was not provided",)
# KWARGS: {'name': 'myvar2'}
if len(args) == 1:
info = args[0]
elif "name" in kwargs.keys():
info = f"Undefined variable '{kwargs['name']}"
else:
infoList = ["Not allowing any undefined variable."]
infoList.append(f"ARGS: {args}")
infoList.append(f"KWARGS: {kwargs}")
info = "\n".join(infoList)
raise Exception(info)
def load_render_and_format(
template_path: str,
output_path: str,
render_params: dict,
banner: str,
needFormat: bool = True,
):
tpl = load_template(template_path)
result = tpl.render(**render_params)
logger_print()
logger_print("______________________[{}]".format(banner))
logger_print(result)
# import black.Mode
output_path_elems = output_path.split(".")
output_path_elems.insert(-1, "new")
if os.path.exists(output_path):
with open(output_path, "r") as f:
backup_content = f.read()
else:
backup_content = ""
with open(tmp_output_path := ".".join(output_path_elems), "w+") as f:
f.write(result)
if not needFormat:
shutil.move(tmp_output_path, output_path)
return
try:
# TODO: add more test, like checking for undefined variables, before rewriting the source file.
# TODO: add rollback mechanism in makefile
result = black.format_str(result, mode=black.Mode())
logger_print("Formatter Ok.")
# with TemporaryDirectory() as TP:
with open(output_path, "w+") as f:
f.write(result)
# do further type checking.
# typechecker_input_path = os.path.join(
# TP, base_output_path := os.path.basename(output_path)
# )
# with open(typechecker_input_path, "w+") as f:
# f.write(typechecker_input_path)
# output = subprocess.run(
# ["pyright", typechecker_input_path],
# capture_output=True,
# encoding="utf-8",
# )
run_result = pyright_utils.run(
output_path, capture_output=True, encoding="utf-8"
)
typeErrors = [
e.strip().replace(
os.path.basename(output_path), os.path.basename(tmp_output_path)
)
for e in re.findall(
pyright_utils.errorRegex, run_result.stdout, re.MULTILINE
)
]
# breakpoint()
if run_result.stderr:
typeErrors.append("")
typeErrors.append(f"Pyright error:\n{run_result.stderr}")
if typeErrors:
typeErrors.insert(0, f"Type error found in file {repr(output_path)}")
raise Exception(f"\n{' '*4}".join(typeErrors))
logger_print("Pyright Ok.")
os.remove(tmp_output_path)
except:
import traceback
traceback.print_exc()
# os.remove(tmp_output_path)
with open(output_path, "w+") as f:
f.write(backup_content)
# ref: https://www.geeksforgeeks.org/python-os-utime-method/
# do not set this to 0 or something. will cause error.
os.utime(
output_path,
times=(
os.path.getatime(template_path) - 1000000,
os.path.getmtime(template_path) - 1000000,
),
) # to make this older than template, must update!
raise Exception(
f"Code check failed.\nTemporary cache saved to: '{tmp_output_path}'"
)
logger_print("=" * 40)
def lstrip(string: str):
lines = string.split("\n")
result_lines = []
for line in lines:
result_lines.append(line.lstrip())
# if stripped_line := line.lstrip():
# result_lines.append(stripped_line)
result = "\n".join(result_lines).strip("\n")
return result
def code_and_template_path(base_name):
code_path = f"{base_name}.py"
template_path = f"{code_path}.j2"
return code_path, template_path
def load_template(template_path, extra_func_dict={}):
try:
assert template_path.endswith(".j2")
except:
Exception(f"jinja template path '{template_path}' is malformed.")
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath=["./", "../"]),
extensions=[
"jinja2_error.ErrorExtension",
"jinja2.ext.do",
"jinja2.ext.loopcontrols",
],
trim_blocks=True,
lstrip_blocks=True,
# undefined=jinja2.StrictUndefined,
undefined=NeverUndefined,
)
tpl = env.get_template(template_path)
# def myJoin(mstr, mlist):
# logger_print("STR:", repr(mstr))
# logger_print("LIST:", repr(mlist))
# return mstr.join(mlist)
func_dict = dict(
list=list,
str=str,
_dict=dict,
_set=set, # avoid name collision
tuple=tuple,
ord=ord,
len=len,
repr=repr,
c2s=c2s,
# s2c=s2c,
s2cl=s2cl,
s2cu=s2cu,
zip=zip,
cws=camelize_with_space,
lstrip=lstrip,
remove_typehint=remove_typehint,
kebabize=humps.kebabize,
pascalize=humps.pascalize,
# enumerate=enumerate,
# eval=eval,
# join=myJoin
** extra_func_dict,
)
tpl.globals.update(func_dict)
return tpl
def test(cmd: list, exec="python3" if os.name != "nt" else "python"):
cmd = [exec] + cmd
p = subprocess.run(cmd)
p.check_returncode()