aboutsummaryrefslogtreecommitdiff
path: root/spgen.py
blob: a72f4408ca692491e641c0206717fbfa965ab341 (plain)
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
#!/usr/bin/env python3

import argparse
import datetime
import random
from string import Template
import sys


class SPTemplate(Template):
    """Seating plan template

    %% is used as delimiter because the most templates are written in LaTeX.
    """
    delimiter = '%%'


def print_sp_template(template: SPTemplate, duration: str, names: list[str],
                      seats: list[int]):
    """Print the template including our name definitions"""
    defs = []
    seating = {i: '' for i in range(1, 33)}
    for i, name in enumerate(names):
        seat = seats[i]
        seating[seat] = name
        defs.append(rf'\csdef{{name{seat}}}{{{name}}}')

    defs.append(rf'\csdef{{duration}}{{{duration}}}')

    subs = {
        'defs': '\n'.join(defs)
    } | {
        f'name{seat}': name
        for seat, name in seating.items()
    }

    print(template.substitute(subs))


def main():
    """Entry point generating a random seating plan from student names"""

    parser = argparse.ArgumentParser()
    parser.add_argument('template')
    duration_group = parser.add_mutually_exclusive_group()
    duration_group.add_argument(
        '--duration',
        default=f'KW: {datetime.datetime.now().isocalendar()[1]}')
    duration_group.add_argument('--week', type=int)
    duration_group.add_argument('--seats', type=int)
    args = parser.parse_args()

    names = [line[:-1] for line in sys.stdin.readlines()]
    seats = list(range(1, (args.seats if args.seats else len(names)) + 1))
    random.shuffle(seats)

    duration = args.duration
    if args.week:
        # https://stackoverflow.com/questions/56163008/how-to-get-all-dates-of-week-based-on-week-number-in-python
        # Starts with knowing the day of the week
        week_day = datetime.datetime.now().isocalendar()[2]
        week_offset = args.week - datetime.datetime.now().isocalendar()[1]

        # Calculates Starting date (Sunday) for this case by subtracting
        # current date with time delta of the day of the week
        start_date = datetime.datetime.now() - datetime.timedelta(
            days=week_day) + datetime.timedelta(days=(week_offset * 7) + 1)

        # Prints the list of dates in a current week
        duration = (
            f'KW: {args.week}~~~{start_date.strftime("%d.%m")} - '
            f'{(start_date + datetime.timedelta(days=4)).strftime("%d.%m")}')

    with open(args.template, 'r', encoding='utf-8') as template_file:
        template = SPTemplate(template_file.read())
        print_sp_template(template, duration, names, seats)


if __name__ == '__main__':
    main()