This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
/
ballot.py
executable file
·102 lines (74 loc) · 2.56 KB
/
ballot.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python3
import io
import os
import click
from datetime import date
from jinja2 import Environment, FileSystemLoader, Template
from ruamel.yaml import YAML
from typing import List
latex_jinja_env = Environment(
block_start_string="\BLOCK{",
block_end_string="}",
variable_start_string="\VAR{",
variable_end_string="}",
comment_start_string="\#{",
comment_end_string="}",
line_statement_prefix="%%",
line_comment_prefix="%#",
trim_blocks=True,
autoescape=False,
loader=FileSystemLoader(os.path.abspath(".")),
)
@click.group()
def cli():
pass
@cli.command(help="Get the names of the ballot papers")
@click.argument("config_file", type=click.File("rb"))
@click.argument("ext", type=click.STRING)
def list(config_file: io.BufferedReader, ext: str):
yaml = YAML()
config = yaml.load(config_file)
for name, _ in config["papers"].items():
print(f"ballot-{name}.{ext}")
@cli.command(help="Generate the TeX source for the ballot papers.")
@click.argument("config_file", type=click.File("rb"))
def tex(config_file: io.BufferedReader):
yaml = YAML()
config = yaml.load(config_file)
click.echo(f"Loaded {config_file.name}")
for name, paper in config["papers"].items():
candidates: List[str] = paper["candidates"]
sorted(candidates)
if config["config"]["ron"]:
candidates.append("Re-open Nominations")
generate_ballot_tex(
name, config["config"]["org"], paper["title"], paper["date"], candidates
)
@cli.command(help="Generate the TeX source for a merged document")
@click.argument("config_file", type=click.File("rb"))
def texpack(config_file: io.BufferedReader):
yaml = YAML()
config = yaml.load(config_file)
click.echo(f"Loaded {config_file.name}")
template = latex_jinja_env.get_template("template-pack.tex")
click.echo(f"Generating Merged Ballot")
render = template.render(ballot_list=config["papers"].items())
with open(f"ballot-pack.tex", "w") as fh:
fh.write(render)
def generate_ballot_tex(
name: str, org: str, title: str, date: date, candidates: List[str]
):
"""Generate the tex source for a ballot paper."""
template = latex_jinja_env.get_template("template-ballot.tex")
click.echo(f"Generating Ballot for {title}")
render = template.render(
name=name,
org=org,
title=title,
date=date.isoformat(),
candidate_list=candidates,
)
with open(f"ballot-{name}.tex", "w") as fh:
fh.write(render)
if __name__ == "__main__":
cli()