-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecklister.py
61 lines (46 loc) · 1.49 KB
/
checklister.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
import sys
from yaml import load, dump
import jinja2
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
class ChecklistRow:
def __init__(self,item,action):
self.item=str(item)
self.action=str(action)
class ChecklistSection:
def __init__(self,name,rows):
self.name=name
# rows is a list whose elements are dictionnary with only one entry
# We get this pair of key,value by converting row.item to a list and
# accessing the first element
# This is then turned into a sequence of arguments
self.rows = [ChecklistRow(*(list(row.items())[0])) for row in rows]
class Checklist:
def __init__(self,yaml_input):
try:
self.title=str(yaml_input["title"])
sections_yaml=yaml_input["sections"]
except KeyError:
print("YAML input must have a title and a list of sections")
raise
self.sections=[]
for name,rows in sections_yaml.items():
self.sections.append(ChecklistSection(name,rows))
if __name__ == "__main__":
try:
assert len(sys.argv)>2
except AssertionError:
print("Two arguments needed: YAML input, HTML output")
raise
yaml_filename = str(sys.argv[1])
html_filename = str(sys.argv[2])
with open(yaml_filename) as file_:
yaml_data = load(file_,Loader=Loader)
checklist = Checklist(yaml_data)
with open('template.jinja') as file_:
template = jinja2.Template(file_.read())
html_output=template.render(checklist=checklist)
with open(html_filename,"x") as file_:
file_.write(html_output)