aboutsummaryrefslogtreecommitdiff
path: root/spgen.py
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-12-14 17:01:38 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2024-12-14 17:01:38 +0100
commit362a954add901904cf1234b96d84debb9262c556 (patch)
treedc60f08c6d045dcc8fc677595c277042bef6b60e /spgen.py
downloadspgen-362a954add901904cf1234b96d84debb9262c556.tar.gz
spgen-362a954add901904cf1234b96d84debb9262c556.zip
add initial commit
Diffstat (limited to 'spgen.py')
-rwxr-xr-xspgen.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/spgen.py b/spgen.py
new file mode 100755
index 0000000..30b5956
--- /dev/null
+++ b/spgen.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+import argparse
+import datetime
+import random
+from string import Template
+import sys
+
+
+class SPTemplate(Template):
+ delimiter = '%%'
+
+
+def print_sp_template(template: SPTemplate, duration: str, names: list[str]):
+ defs = [rf'\csdef{{name{i+1}}}{{{n}}}' for i, n in enumerate(names)]
+ defs.append(rf'\csdef{{duration}}{{{duration}}}')
+ print(template.substitute({'defs': '\n'.join(defs)}))
+
+
+def main():
+ """Entry point generating a random seating plan from students 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)
+ args = parser.parse_args()
+
+ names = [line[:-1] for line in sys.stdin.readlines()]
+ random.shuffle(names)
+
+ 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)
+
+
+if __name__ == '__main__':
+ main()