-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgencfg.py
45 lines (33 loc) · 1.08 KB
/
gencfg.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
# Author: Oyvind Ellefsen
# Version 1.1
# For Atea workshop
import os
import jinja2
import csv
TEMPLATE_FILENAME = 'template.j2'
CSVDATA_FILENAME = 'data.csv'
def get_data(row):
# Hent data fra csv filen
data_fields = {
field_name: field_value
for field_name, field_value in row.items()
}
## ---------------------------------------------------------------------------
## Sett opp Jinja2 og hent template
## ---------------------------------------------------------------------------
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.getcwd()),
trim_blocks=True, lstrip_blocks=True)
template = env.get_template(TEMPLATE_FILENAME)
## ---------------------------------------------------------------------------
## Les CSV og fyll inn data for hver linje
## ---------------------------------------------------------------------------
for row in csv.DictReader(open(CSVDATA_FILENAME)):
get_data(row)
with open(row['HOSTNAME'] + '.txt', 'w+') as f:
f.write(template.render(row).encode('utf-8'))
print ('Writing file ' + row['HOSTNAME'] + '.txt')
f.close()
##
## all done!
##