-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (68 loc) · 2.24 KB
/
main.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
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
import csv
import glob
import os
import pdfkit
from Activity import Activity
from jinja2 import Environment, select_autoescape
def getEventFilename(dir_path):
return glob.glob(dir_path + "/tables/events-*.csv")[0]
def getSignupsFilename(dir_path):
return glob.glob(dir_path + "/tables/signups-*.csv")[0]
def mapRowToActivity(row, signups):
return Activity(
row[0],
row[1],
row[16],
None,
row[7],
row[14] + row[17],
row[2],
row[3],
row[4],
row[5],
None,
None,
None,
signups.get(row[1], []),
row[15],
)
def generateHtml(activity):
env = Environment(
autoescape=select_autoescape()
)
templateFile = open("activityTemplate.jinja2", "r")
template = env.from_string(templateFile.read())
return template.render(a=activity)
def main():
dir_path = os.path.dirname(os.path.realpath(__file__))
signupsCsv = getSignupsFilename(dir_path)
signups = {}
with open(signupsCsv) as csv_signups_file:
next(csv_signups_file)
csv_reader = csv.reader(csv_signups_file, delimiter=',')
for row in csv_reader:
signup = signups.get(row[1], [])
signup.append(row[3])
signups[row[1]] = signup
eventCsv = getEventFilename(dir_path)
activities = []
with open(eventCsv) as csv_events_file:
next(csv_events_file)
csv_reader = csv.reader(csv_events_file, delimiter=',')
for row in csv_reader:
activities.append(mapRowToActivity(row, signups))
count = 0
for activity in activities:
count += 1
print("Generating " + str(count) + ". from " + str(len(activities)))
html = generateHtml(activity)
htmlFile = open("htmlOutput/generated.html", "w")
htmlFile.write(html)
htmlFile.close()
output_path = dir_path + "/pdfOutput/generated_" + activity.id + ".pdf"
pdfkit.from_file(htmlFile.name, output_path, options={"encoding": "UTF-8","page-size": "A5",})
print("Done " + str(count) + ". from " + str(len(activities)))
print()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
main()