-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutemplate_util.py
66 lines (57 loc) · 1.78 KB
/
utemplate_util.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
# (c) 2014-2019 Paul Sokolovsky. MIT license.
import sys
import os
import os.path
try:
import uio as io
except ImportError:
import io
import utemplate.source
import utemplate.compiled
if len(sys.argv) < 3:
print("Usage: %s <cmd> <template> [<template arg>...]" % sys.argv[0])
sys.exit(1)
cmd = sys.argv[1]
package = None
if cmd == "rawcompile":
out_name = sys.argv[2].replace(".", "_") + ".py"
with open(sys.argv[2]) as f_in, open(out_name, "w") as f_out:
c = utemplate.source.Compiler(f_in, f_out)
c.compile()
elif cmd == "compileall":
sys.path.insert(0, ".")
loader = utemplate.source.Loader(package, ".")
for cur_path, dirs, files in os.walk(sys.argv[2]):
for f in files:
if f.endswith(".py"):
continue
fname = cur_path + "/" + f
print(fname)
loader.load(fname)
elif cmd == "compile":
loader = utemplate.source.Loader(package, ".")
try:
os.remove(loader.compiled_path(sys.argv[2]))
except:
pass
# Compiled file created from the current dir, but imported from
# sys.path, so make sure current dir is in sys.path.
sys.path.insert(0, ".")
render = loader.load(sys.argv[2])
elif cmd == "render":
loader = utemplate.compiled.Loader(package, ".")
render = loader.load(sys.argv[2])
for x in render(*sys.argv[3:]):
sys.stdout.write(x)
elif cmd == "run":
f_out = io.StringIO()
with open(sys.argv[2]) as f_in:
c = utemplate.source.Compiler(f_in, f_out,
loader=utemplate.source.Loader(None, os.path.dirname(sys.argv[2]) or "."))
c.compile()
ns = {}
exec(f_out.getvalue(), ns)
for x in ns["render"](*sys.argv[3:]):
sys.stdout.write(x)
else:
print("Unknown command:", cmd)