templar.min.py
3.69 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
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
import string, re, sys
import getopt,os.path, json
from pprint import pprint
debug = False
ifile = ''
folder = ''
ofile = ifile
printit = False
vars=os.environ
recursive = False
class MyTemplate(string.Template):
#delimiter = '{{'
pattern = r'''
\[\[\{(?:
(?P<escaped>\[\[\{)|
(?P<named>[_A-Z][_A-Z0-9]*)\}\]\]|
(?P<braced>[_A-Z][_A-Z0-9]*)\}\]\]|
(?P<invalid>)
)
'''
def usage():
print """ usage: %s [OPTIONS]
OPTIONS:
-i, --ifile=ABSOLUTE_INPUT_FILE_PATH
input template file
by default it will replace in file
-v, --vars=JSON_VARS_FILE
json of variables file to replace in,
by default it loads the environment variables
-o, --ofile=ABS_OUTPUT_FILE_PATH
provide an output file,
override the input file default behaviour
-p, --print
print to stdout instead of replacing the file,
it will do replace in file.
it will override the output file and input file default behaviour
-r, --r
recursively templates all files included in a folder given,
use with -p to only debug/test, use it without -p and it will
replace all files with the templated values
"""%sys.argv[0]
sys.exit(2)
def engine(ifile,ofile,vars):
template_content = open(ifile, "r").read()
template= MyTemplate(template_content)
#print 'MATCHES:', t.pattern.findall(t.template)
outputText = template.safe_substitute(vars)
print 'MATCHES:', template.pattern.findall(template.template)
if printit:
print "AFTER TEMPLATE:"
#print outputText
print outputText.encode('ascii', 'ignore')
else:
f = open(ofile,'w')
f.write(outputText.encode('ascii', 'ignore'))
f.close()
def main(argv):
try:
options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:r:dph', ['ofile=','o=',
'ifile=','i=',
'vars=','v=',
'print','p',
'help','h',
'r='
])
except getopt.GetoptError:
usage()
sys.exit(2)
global debug, vars, ifile, ofile,printit, recursive
print options
for opt, arg in options:
if opt in ('-v','--vars','--v'):
if os.path.isfile(arg):
with open(arg) as data_file:
print "using file"
vars = json.load(data_file)
else:
print "using environment"
vars=os.environ
elif opt in ('-r','--r'):
if os.path.isdir(arg):
recursive= True
folder = arg
else:
print "folder provided [%s] does not exist" %arg
usage()
elif opt in ('-f','--ifile','--i','-i'):
if os.path.isfile(arg):
ifile=arg
ofile=ifile
else:
print "file provided [%s] does not exist" %arg
usage()
elif opt in ("-o" ,"--ofile",'--o') :
ofile = arg
elif opt in ('-p','--print'):
printit=True
elif opt in ('-h','--help'):
usage()
else:
usage()
if debug:
print 'OPTIONS :', options
print 'VARS :', pprint(str(vars))
print 'DEBUG :', debug
print 'OUTPUT :', ofile
print 'INPUT :', ifile
print 'PRINTIT :', printit
print 'REMAINING :', remainder
if recursive :
print folder
for path, dirs, files in os.walk(folder):
for filename in files:
fullpath = os.path.join(path, filename)
if os.path.isfile(fullpath):
engine(fullpath, fullpath, vars)
else:
print "file %s does not exists"%fullpath
elif ifile != '':
engine(ifile, ofile, vars )
else:
print "nothing provided"
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) > 1:
main(sys.argv[1:])
else:
usage()
sys.exit()