-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjinja_csv.py
31 lines (22 loc) · 1.08 KB
/
jinja_csv.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
import os
import sys
from csv_model import CSVDictModel
def render_template_from_csv(csvfile, templatefile, template_path=None, options=None, **kwargs):
model = CSVDictModel.from_file(csvfile)
view = CSVJinjaView(template_path=template_path, options=options)
return view.render_jinja_template(templatefile, model, **kwargs)
def render_template_per_row(csvfile, templatefile, filemapper, template_path=None, options=None, rowkey=0, **kwargs):
model = CSVDictModel.from_file(csvfile)
view = CSVJinjaView(template_path=template_path, options=options)
for output in view.render_template_for_rows(templatefile, model, rowkey, **kwargs):
with open(filemapper(output[0]), 'w') as fp:
fp.write(output[1])
def main():
csvfile = sys.argv[1]
templatefile = sys.argv[2]
#output_path = sys.argv[3]
output = render_template_from_csv(csvfile, templatefile)
print(output, end='')
#render_template_per_row(csvfile, templatefile, lambda name:os.path.join(output_path, '_'.join(name.lower().split()) + '.out'))
if __name__ == '__main__':
main()