forked from mikeboers/PyTomCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess
executable file
·54 lines (41 loc) · 1.15 KB
/
preprocess
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
#!/usr/bin/env python
from __future__ import print_function
from ast import literal_eval
from optparse import OptionParser
import os
import sys
import re
import meta
from mako.exceptions import text_error_template
from mako.lookup import TemplateLookup
from mako.template import Template
if __name__ == '__main__':
parser = OptionParser("usage: %prog [FILENAME]")
parser.add_option('-D', action='append', dest='defs')
opts, args = parser.parse_args(sys.argv[1:])
defs = {}
for x in opts.defs or []:
k, v = (x.split('=', 1) + ['True'])[:2]
try:
v = literal_eval(v)
except (ValueError, SyntaxError):
pass
defs[k] = v
if len(args) not in (0, 1):
parser.error("wrong number of arguments") # Will exit
if (len(args) == 0) or (args[0] == "-"):
fo = sys.stdin
else:
filename = args[0]
if not os.path.isfile(filename):
raise SystemExit("error: can't find %s" % filename)
fo = open(filename)
context = meta.__dict__.copy()
context.update(defs)
data = fo.read()
try:
lookup = TemplateLookup(["."])
print(Template(data, lookup=lookup).render(**context))
except:
sys.stderr.write(text_error_template().render())
exit(1)