minified version of templar using String template
Showing
3 changed files
with
143 additions
and
0 deletions
src/examples/template.j2
0 → 100644
src/examples/vars.ini
0 → 100644
1 | variable=hello world |
src/templar.min.py
0 → 100755
1 | #!/usr/bin/python | ||
2 | import string, re, sys | ||
3 | import getopt,os.path | ||
4 | |||
5 | |||
6 | debug = False | ||
7 | ifile = '' | ||
8 | ofile = ifile | ||
9 | printit = False | ||
10 | vars=os.environ | ||
11 | |||
12 | |||
13 | class MyTemplate(string.Template): | ||
14 | #delimiter = '{{' | ||
15 | pattern = r''' | ||
16 | \[\[\{(?: | ||
17 | (?P<escaped>\[\[\{)| | ||
18 | (?P<named>[_a-z][_a-z0-9]*)\}\]\]| | ||
19 | (?P<braced>[_a-z][_a-z0-9]*)\}\]\]| | ||
20 | (?P<invalid>) | ||
21 | ) | ||
22 | ''' | ||
23 | |||
24 | |||
25 | def usage(): | ||
26 | print """ usage: %s [OPTIONS] | ||
27 | |||
28 | OPTIONS: | ||
29 | -i, --ifile=ABSOLUTE_INPUT_FILE_PATH | ||
30 | input template file | ||
31 | by default it will replace in file | ||
32 | |||
33 | -v, --vars=JSON_VARS_FILE | ||
34 | json of variables file to replace in, | ||
35 | by default it loads the environment variables | ||
36 | |||
37 | -o, --ofile=ABS_OUTPUT_FILE_PATH | ||
38 | provide an output file, | ||
39 | override the input file default behaviour | ||
40 | -p, --print | ||
41 | print to stdout instead of replacing the file, | ||
42 | it will do replace in file. | ||
43 | it will override the output file and input file default behaviour | ||
44 | |||
45 | """%sys.argv[0] | ||
46 | sys.exit(2) | ||
47 | |||
48 | |||
49 | def engine(ifile,ofile,vars): | ||
50 | template_content = open(ifile, "r").read() | ||
51 | template= MyTemplate(template_content) | ||
52 | |||
53 | #print 'MATCHES:', t.pattern.findall(t.template) | ||
54 | outputText = template.safe_substitute(vars) | ||
55 | print 'MATCHES:', template.pattern.findall(template.template) | ||
56 | |||
57 | |||
58 | if printit: | ||
59 | print "AFTER TEMPLATE:" | ||
60 | #print outputText | ||
61 | print outputText.encode('ascii', 'ignore') | ||
62 | else: | ||
63 | f = open(ofile,'w') | ||
64 | f.write(outputText.encode('ascii', 'ignore')) | ||
65 | f.close() | ||
66 | |||
67 | |||
68 | |||
69 | |||
70 | def main(argv): | ||
71 | try: | ||
72 | options, remainder = getopt.getopt(sys.argv[1:], 'o:f:i:v:dph', ['ofile=','o=', | ||
73 | 'ifile=','i=', | ||
74 | 'vars=','v=', | ||
75 | 'print','p', | ||
76 | 'help','h' | ||
77 | ]) | ||
78 | except getopt.GetoptError: | ||
79 | usage() | ||
80 | sys.exit(2) | ||
81 | |||
82 | global debug, vars, ifile, ofile,printit | ||
83 | |||
84 | for opt, arg in options: | ||
85 | if opt in ('-v','--vars','--v'): | ||
86 | if os.path.isfile(arg): | ||
87 | with open(arg) as data_file: | ||
88 | print "using file" | ||
89 | vars = json.load(data_file) | ||
90 | else: | ||
91 | print "using environment" | ||
92 | vars=os.environ | ||
93 | |||
94 | elif opt in ('-f','--ifile','--i','-i'): | ||
95 | if os.path.isfile(arg): | ||
96 | ifile=arg | ||
97 | ofile=ifile | ||
98 | else: | ||
99 | print "file provided [%s] does not exist" %arg | ||
100 | usage() | ||
101 | |||
102 | elif opt in ("-o" ,"--ofile",'--o') : | ||
103 | ofile = arg | ||
104 | elif opt in ('-p','--print'): | ||
105 | printit=True | ||
106 | elif opt in ('-h','--help'): | ||
107 | usage() | ||
108 | else: | ||
109 | usage() | ||
110 | |||
111 | if debug: | ||
112 | print 'OPTIONS :', options | ||
113 | print 'VARS :', pprint(vars) | ||
114 | print 'DEBUG :', debug | ||
115 | print 'OUTPUT :', ofile | ||
116 | print 'INPUT :', ifile | ||
117 | print 'PRINTIT :', printit | ||
118 | print 'REMAINING :', remainder | ||
119 | |||
120 | |||
121 | |||
122 | engine(ifile, ofile, vars ) | ||
123 | |||
124 | |||
125 | |||
126 | |||
127 | if __name__ == "__main__": | ||
128 | if len(sys.argv) > 1: | ||
129 | main(sys.argv[1:]) | ||
130 | else: | ||
131 | usage() | ||
132 | sys.exit() | ||
133 | |||
134 | |||
135 | |||
136 | |||
137 |
-
Please register or sign in to post a comment