-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·101 lines (84 loc) · 3.21 KB
/
run.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
#!/opt/anaconda3/envs/roster/bin/python3
# coding: utf-8
import argparse
from logging import info
import os
import json
from pathlib import Path
from core.schedule import Schedule
from core.solver import SchedulingProblem
from core.roster import Roster
from core.history import AssignmentHistory
from core.stats import AssignmentStats
from util.helpers import *
import app
def parse_args():
parser = argparse.ArgumentParser(
description="Congregational Scheduling Constraint Solver"
)
parser.add_argument("month", type=int, help="the month (1-12)")
parser.add_argument("year", type=int, help="the year (e.g. 2025)")
parser.add_argument(
"dest_file",
help="the output path for the pdf file",
)
parser.add_argument(
"-s",
"--save_file",
default="data/previous-assignments.json",
help="optional alternative 'save' json file. If not specified previous-assignments.json will be used",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="turn on additional logging"
)
return parser.parse_args()
def main():
"""
TODO handle special events (e.g. Gospel Meetings)
"""
args = parse_args()
month = args.month
year = args.year
pdf_output_file = args.dest_file
output_dir = os.path.dirname(pdf_output_file)
output_file_stem = Path(pdf_output_file).stem
html_dir = f"{output_dir}/output/html"
json_dir = f"{output_dir}/output/json"
html_filename = f"{output_file_stem}.html"
json_filename = f"{output_file_stem}.json"
html_output_path = f"{html_dir}/{html_filename}"
json_output_path = f"{json_dir}/{json_filename}"
tmp_working_path = f"/tmp/{json_filename}"
os.makedirs(html_dir, exist_ok=True)
os.makedirs(json_dir, exist_ok=True)
history = AssignmentHistory(args.save_file)
roster = Roster()
stats = AssignmentStats(roster, history)
schedule = Schedule(year, month)
if os.path.exists(json_output_path):
with open(json_output_path, "r") as f:
print(f"\n\nFound previous schedule in {json_output_path}\n\n")
assignments = json.loads(f.read())
schedule.set_assignments(assignments)
else:
print("Solving new Schedule...")
schedule_problem = SchedulingProblem(schedule, roster, history)
solver_result, solver_assignments, roster = schedule_problem.solve(
verbose=args.verbose
)
write_dict_to_file(schedule.assignments, json_output_path)
options = {
"HTML_OUTPUT_PATH": html_output_path,
"PDF_OUTPUT_PATH": pdf_output_file,
"JSON_OUTPUT_PATH": json_output_path,
"TMP_WORKING_PATH": tmp_working_path,
}
# debug=True causes main to run twice because of the werkzeug reloader process, very sad
# see https://stackoverflow.com/questions/25504149/why-does-running-the-flask-dev-server-run-itself-twice
app.create_app(schedule, roster, history, stats, options=options).run(debug=False)
print("")
print("html: " + term_link(f"file://{html_output_path}"))
print("json: " + term_link(f"file://{json_output_path}"))
print("pdf: " + term_link(f"file://{pdf_output_file}"))
if __name__ == "__main__":
main()