-
Notifications
You must be signed in to change notification settings - Fork 4
/
go_brr.py
executable file
·150 lines (110 loc) · 4.26 KB
/
go_brr.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
import argparse
import csv
import math
import os
import random
import shutil
import traceback
from pathlib import Path
import csv2pdf
import rosters
import seatingchart
OUT_PATH = Path.cwd() / "out"
HTML_PATH = Path.home() / "html" / "seating"
HTML_IMAGES_PATH = HTML_PATH / "images"
HTML_PDF_PATH = HTML_PATH / "seat.pdf"
# ANSI color codes
GREEN = "\033[01;92m"
RED = "\033[01;91m"
END = "\033[0m"
ASCII_ART = r"""
________ ________ ________ ________ ________
|\ ____\|\ __ \ |\ __ \|\ __ \|\ __ \
\ \ \___|\ \ \|\ \ \ \ \|\ /\ \ \|\ \ \ \|\ \
\ \ \ __\ \ \\\ \ \ \ __ \ \ _ _\ \ _ _\
\ \ \|\ \ \ \\\ \ \ \ \|\ \ \ \\ \\ \ \\ \|
\ \_______\ \_______\ \ \_______\ \__\\ _\\ \__\\ _\
\|_______|\|_______| \|_______|\|__|\|__|\|__|\|__|
"""
def rename_images(img_path: Path) -> None:
for img in img_path.glob("*"):
if img.suffix == ".jpeg":
img.rename(img.with_suffix('.jpg'))
def create_html_directory(html_path: Path) -> None:
# Remove the directory if it exists
if html_path.exists():
shutil.rmtree(HTML_PATH)
# Create the directory
html_path.mkdir(parents=True, exist_ok=True)
html_path.chmod(0o755)
def init_seat_csv(seat_csv_path: Path) -> None:
seat_csv_path.write_text("Uni,Name,Seat,Room\n")
seat_csv_path.chmod(0o744)
def main():
print(ASCII_ART)
parser = argparse.ArgumentParser(description="Go brrr with the seating charts")
parser.add_argument("rooms", type=str, metavar='<rooms_file>')
parser.add_argument("roster", type=str, metavar='<roster_file>')
args = parser.parse_args()
ROOM_FILE = Path(args.rooms)
students = rosters.load_roster(args.roster)
print(f"Found {len(students)} students in the roster file")
random.shuffle(students)
student_count = len(students)
seat_count = 0
rename_images(Path("images"))
create_html_directory(HTML_PATH)
# Create the file
seat_csv_path = HTML_PATH / "seat.csv"
init_seat_csv(seat_csv_path)
with ROOM_FILE.open('r') as f:
rooms = [r.strip().split() for r in f.readlines() if r.strip()]
print("Rooms:")
for room_name, count in rooms:
print(f"- {room_name}: {count} seats")
seat_count += int(count)
print(f"Total seats: {seat_count}")
student_index = 0
for room in rooms:
rname, rcount = room[0], room[1]
ROOM_OUT_PATH = Path(OUT_PATH) / rname
ROOM_ROSTER_PATH = ROOM_OUT_PATH / f"roster_{rname}.csv"
ROOM_LIST_PATH = ROOM_OUT_PATH / f"list_{rname}.csv"
ROOM_CHART_PATH = ROOM_OUT_PATH / f"chart_{rname}.html"
ROOM_IMAGES_PATH = ROOM_OUT_PATH / "images"
HTML_ROOM_PATH = HTML_PATH / f"{rname}.html"
shutil.rmtree(ROOM_OUT_PATH, ignore_errors=True)
ROOM_OUT_PATH.mkdir(parents=True)
ROOM_IMAGES_PATH.symlink_to(Path("images"))
room_students = []
num_seats = math.ceil(student_count / seat_count * int(rcount))
for _ in range(num_seats):
if student_index < student_count:
student = students[student_index]
student_index += 1
room_students.append(student)
rosters.save_roster(room_students, ROOM_ROSTER_PATH)
try:
seatingchart.run(slug=rname, layout=rname)
except Exception as e:
print(traceback.format_exc())
print(RED + "Error: seatingchart.py failed" + END)
exit(1)
shutil.copy(ROOM_CHART_PATH, HTML_ROOM_PATH)
with ROOM_LIST_PATH.open('r') as list_file:
with seat_csv_path.open('a') as seat_file:
for line in list_file:
modified_line = line.strip() + f",{rname}\n"
seat_file.write(modified_line)
HTML_ROOM_PATH.chmod(0o644)
shutil.copytree("images", HTML_IMAGES_PATH)
HTML_IMAGES_PATH.chmod(0o711)
for img in HTML_IMAGES_PATH.glob("*.*"):
img.chmod(0o644)
csv2pdf.convert(seat_csv_path, HTML_PDF_PATH)
HTML_PDF_PATH.chmod(0o644)
seat_csv_path.unlink()
print(GREEN + "Success!" + END)
if __name__ == "__main__":
main()