|
@@ -0,0 +1,259 @@
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+# intentions
|
|
4
|
+# ::pseudo code::
|
|
5
|
+# get file names
|
|
6
|
+
|
|
7
|
+# open & parse
|
|
8
|
+# check if template; if output should I stop for some random reason
|
|
9
|
+
|
|
10
|
+# for templates get input files
|
|
11
|
+# run and save
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+import chevron
|
|
15
|
+import os
|
|
16
|
+from os.path import isfile,islink,isdir
|
|
17
|
+p=print
|
|
18
|
+
|
|
19
|
+class Eobj:pass
|
|
20
|
+
|
|
21
|
+C = Eobj
|
|
22
|
+C.isTemplateStr = '{{{"makestache_template"}}}'
|
|
23
|
+C.isTemplateStr2 = 'THIS_FILE_IS_A_MAKESTACHE_TEMPLATE'
|
|
24
|
+C.isNotTemplateStr = 'this_file_is_not_a_makestache_template'
|
|
25
|
+
|
|
26
|
+# this string prolly shouldn't appear in any files you wouldn't want to actually overwrite
|
|
27
|
+C.ALLOW_TEMPLATE_OVERWRITE = "BUILT_FILE_" + "OVERWRITE_ALLOWED"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+# output_filename # mustache_var
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+p("MAKE.PY")
|
|
38
|
+p("MAKESTACHE")
|
|
39
|
+
|
|
40
|
+def wfile(file_name,out):
|
|
41
|
+ f = open(file_name,"w")
|
|
42
|
+ f.write(out)
|
|
43
|
+ f.close()
|
|
44
|
+def rfile(file_name):
|
|
45
|
+ f = open(file_name,"r")
|
|
46
|
+ ftxt = f.read()
|
|
47
|
+ f.close()
|
|
48
|
+ return ftxt
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+# TODO:rename this class
|
|
52
|
+class JsObj(dict):
|
|
53
|
+ pass
|
|
54
|
+ def __init__(self,*a,**kw):
|
|
55
|
+ super().__init__(*a,**kw)
|
|
56
|
+ self._template_paths = []
|
|
57
|
+ def _maybe_path(self,name, *a,**kw):
|
|
58
|
+ # p("_maybe_path",name)
|
|
59
|
+ if "*" in name:
|
|
60
|
+ if name not in self._template_paths:
|
|
61
|
+ p("possible dependency",name)
|
|
62
|
+ self._template_paths.append(name)
|
|
63
|
+ def __getattr__(self, name):
|
|
64
|
+ # p(">>>>",name,self)
|
|
65
|
+ return self.__getitem__(name)
|
|
66
|
+ def __getitem__(self, name):
|
|
67
|
+ self._maybe_path(name)
|
|
68
|
+ # p("__getitem__","*" in name,name)
|
|
69
|
+ ret = super().__getitem__(name)
|
|
70
|
+ return ret
|
|
71
|
+ def __setattr__(self, name, val):
|
|
72
|
+ return self.__setitem__(name, val)
|
|
73
|
+ def __delattr__(self, name):
|
|
74
|
+ return self.__delitem__(name)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+# yeah yeah I know where reading the same junk
|
|
79
|
+def is_template_file(file_name):
|
|
80
|
+ r={
|
|
81
|
+ "is_template":0
|
|
82
|
+ }
|
|
83
|
+ ftxt = ""
|
|
84
|
+ try:
|
|
85
|
+ # ftxt = rfile(file_name)
|
|
86
|
+ # with open(file_path, "r") as file:
|
|
87
|
+ with open(file_name, "r") as file:
|
|
88
|
+ ftxt = file.read()
|
|
89
|
+ r["is_template"] = is_template_ftxt(ftxt)
|
|
90
|
+ except Exception as excp:
|
|
91
|
+ p("\t\\t\tFILE ERR:::",file_name)
|
|
92
|
+ p("")
|
|
93
|
+
|
|
94
|
+ p("LRN",len(ftxt))
|
|
95
|
+ return r
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+def is_template_ftxt(ftxt):
|
|
100
|
+ ret = 0
|
|
101
|
+ if C.isTemplateStr in ftxt or C.isTemplateStr2 in ftxt:
|
|
102
|
+ ret =1
|
|
103
|
+ if C.isNotTemplateStr in ftxt:
|
|
104
|
+ ret = 0
|
|
105
|
+ return ret
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+# this should be called render or somthing
|
|
110
|
+def parse(ftxt):
|
|
111
|
+ p("PARSE")
|
|
112
|
+ # o={}
|
|
113
|
+ _o={
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+ o=JsObj(_o)
|
|
118
|
+ out = chevron.render(ftxt,o)
|
|
119
|
+ # p(out)
|
|
120
|
+ p("o.idk?")
|
|
121
|
+ p("~",o._template_paths,"~")
|
|
122
|
+ mdict = get_calculated_templates(o._template_paths)
|
|
123
|
+ o2 = {**o,**mdict["file_stache_dict"],
|
|
124
|
+ "output_filename":mdict["outfile"]["path"],
|
|
125
|
+ }
|
|
126
|
+ out2 = chevron.render(ftxt,o2)
|
|
127
|
+ if C.isTemplateStr in ftxt:
|
|
128
|
+ p("IS A TEMPLATE!!!!!!!!!!!!!!!!!!!")
|
|
129
|
+ else:
|
|
130
|
+ p("IS NOT A TEMPLATE!!!!!!!!!!!!!!!!!!!")
|
|
131
|
+ p("THE WE WILL NOT write")
|
|
132
|
+ return ""
|
|
133
|
+
|
|
134
|
+ os.chdir(tglob["OUT_DIR"])
|
|
135
|
+
|
|
136
|
+ # dbg_out
|
|
137
|
+ wfile("mgen1.js",out)
|
|
138
|
+ wfile("mgen2.js",out2)
|
|
139
|
+ p()
|
|
140
|
+ if mdict["output_ok"]:
|
|
141
|
+ p(mdict["outfile"]["_path"])
|
|
142
|
+ p("WRITING TO:",mdict["outfile"]["path"])
|
|
143
|
+ wfile(mdict["outfile"]["path"],out2)
|
|
144
|
+ p("FILE WRITTEN")
|
|
145
|
+ else:
|
|
146
|
+ p("BUILD_NOT FINALIZED")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+def get_calculated_templates(arr):
|
|
151
|
+ # we assume we're in the right dir and don't worry about anything
|
|
152
|
+ ret = {}
|
|
153
|
+ file_deps = {}
|
|
154
|
+ for en in arr:
|
|
155
|
+ file_content_path = en.replace("@*","",1)
|
|
156
|
+ file_content_path = file_content_path.replace("*",".")
|
|
157
|
+ file_deps[en] = {
|
|
158
|
+ "path":file_content_path,
|
|
159
|
+ "content":":::__BAD_PATH__::: does the show go on?",
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+ # now we get our paths
|
|
164
|
+ file_stache_dict = {}
|
|
165
|
+ for var_name,file_data in file_deps.items():
|
|
166
|
+ if ">" in var_name:continue
|
|
167
|
+ ftxt = rfile(file_data["path"])
|
|
168
|
+ file_stache_dict[var_name]=ftxt
|
|
169
|
+ # file_deps[var_name]["content"]=ftxt
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+ # get_out_put_path and set output_ok key
|
|
173
|
+ for en in (_en for _en in file_deps if (">" in _en or 0)):
|
|
174
|
+ v = file_deps[en]
|
|
175
|
+ ret["outfile"]= {
|
|
176
|
+ "_path":v["path"].split(">")[-1],
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+ os.chdir(tglob["OUT_DIR"])
|
|
181
|
+ ret["output_ok"]=None
|
|
182
|
+ current_output_contents=None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+ assert ret["outfile"]["_path"][0] not in r"\/" , "\033[36m \n\twe won't allow absolute paths that easily\033[0m"
|
|
186
|
+ full_path = os.path.abspath(ret["outfile"]["_path"])
|
|
187
|
+ ret["outfile"]["path"] = full_path
|
|
188
|
+ if os.path.isfile(ret["outfile"]["path"]):
|
|
189
|
+ current_output_contents = rfile(ret["outfile"]["path"])
|
|
190
|
+ if C.ALLOW_TEMPLATE_OVERWRITE in current_output_contents:
|
|
191
|
+ ret["output_ok"]="ALLOW_TEMPLATE_OVERWRITE"
|
|
192
|
+ else:
|
|
193
|
+ ret["output_ok"]= False
|
|
194
|
+
|
|
195
|
+ else:
|
|
196
|
+ ret["output_ok"]=1
|
|
197
|
+ p("current_output_ EXIST?",bool(current_output_contents))
|
|
198
|
+ p(":ALLOW_TEMPLATE_OVERWRITE??",ret["output_ok"])
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+ return {
|
|
202
|
+ "file_deps":file_deps,
|
|
203
|
+ "file_stache_dict":file_stache_dict,
|
|
204
|
+ **ret
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+def parse_possible_template(fn):
|
|
208
|
+ ftxt = rfile(fn)
|
|
209
|
+ parse(ftxt)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+def dev_main():
|
|
214
|
+ p("DEV_MAIN")
|
|
215
|
+
|
|
216
|
+ os.chdir(tglob["IN_DIR"])
|
|
217
|
+
|
|
218
|
+ # for en in os.walk(r"C:\ws\repos\script_utils\makestache"):
|
|
219
|
+ # for en in os.listdir(r"C:\ws\repos\script_utils\makestache"):
|
|
220
|
+
|
|
221
|
+ # for now lets skip links you're not really safe im not checking of thinking about junctions or whatever
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+ o = {}
|
|
225
|
+ # for en in os.scandir(r"C:\ws\repos\script_utils\makestache"):
|
|
226
|
+ for en in os.scandir():
|
|
227
|
+ if isfile(en):
|
|
228
|
+ o[en] = is_template_file(en)
|
|
229
|
+ p([en],isfile(en))
|
|
230
|
+ r'''
|
|
231
|
+ for en2 in dir(en):
|
|
232
|
+ if en2[:2]=="__":
|
|
233
|
+ continue
|
|
234
|
+ p(en2)
|
|
235
|
+ return
|
|
236
|
+ # '''
|
|
237
|
+
|
|
238
|
+ p("parse_possible_template")
|
|
239
|
+ p()
|
|
240
|
+ for k,v in o.items():
|
|
241
|
+ if not v["is_template"]:continue
|
|
242
|
+ p(k)
|
|
243
|
+ p(v)
|
|
244
|
+ p()
|
|
245
|
+ parse_possible_template(k)
|
|
246
|
+# this will prolly be a pain later
|
|
247
|
+tglob = {
|
|
248
|
+ # "file_cache":{}
|
|
249
|
+}
|
|
250
|
+tglob["OUT_DIR"] = r"C:\ws\repos\script_utils\makestache\test_out"
|
|
251
|
+tglob["IN_DIR"] = r"C:\ws\repos\script_utils\makestache\test_src"
|
|
252
|
+tglob["OUT_DIR"] = r"C:\ws\repos\script_utils\makestache\test_src"
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+dev_main()
|