๐Ÿ“ฆ yochem / omhp.nl

๐Ÿ“„ personalschedule.py ยท 304 lines
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304"""
Personal .ics calendars for the cleaning schedule at OMHP.

Currently, these generated calendar files are hosted at
omhp.nl/weektaak/cal/{}.ics, where {} is a placeholder for every tenants name.
"""

import base64
import os
import dataclasses
import datetime as dt
from pathlib import Path
from typing import Iterator
from textwrap import dedent

import ics


Pathable = str | os.PathLike[str]


@dataclasses.dataclass
class WeekCleaning:
    """
    One week of cleaning.

    Currently has three people for the kitchen, one for the toilets, one
    for the showers, and one for the bathroom upstairs.
    """

    week_start: dt.datetime
    kitchen: list[str]
    toilets: str
    showers: str
    upstairs: str

    def cleaners(self) -> list[str]:
        """Return a list of all names doing the cleaning of this week."""
        return self.kitchen + [self.toilets, self.showers, self.upstairs]

    def __contains__(self, person: str) -> bool:
        """Return if a person's name is in the schedule for this week."""
        return person in self.cleaners()

    def __iter__(self) -> Iterator[str]:
        """Iterate over the persons their name in this week's cleaning."""
        for cleaner in self.cleaners():
            yield cleaner

    def jobname(self, name: str) -> str:
        """Given a person's name, return the job description in Dutch."""
        if name in self.kitchen:
            return "Keuken"
        if name == self.toilets:
            return "Wc's"
        if name == self.showers:
            return "Douches"
        if name == self.upstairs:
            return "Boven"

        raise ValueError("name not in cleaning schedule")

    def __str__(self) -> str:
        """Return string representation of this week's cleaning."""
        schedule = dedent(
            f"""
            Keuken ๐Ÿณ
            - {self.kitchen[0]}
            - {self.kitchen[1]}
            - {self.kitchen[2]}

            Wc's ๐Ÿšฝ
            - {self.toilets}

            Douches ๐Ÿšฟ
            - {self.showers}"""
        )

        if self.upstairs != "":
            schedule += dedent(
                f"""

                Badkamer boven ๐Ÿ”
                - {self.upstairs}"""
            )

        schedule += dedent(
            f"""

            Zie https://omhp.nl/weektaak/ voor het hele schema"""
        )

        return schedule


Schedule = list[WeekCleaning]


def csv2schedule(csv_file: Pathable) -> Schedule:
    """
    Convert csv-formatted schedule to a list of WeekCleaning objects.

    Args:
        csv_file: Path to csv-file.

    Returns:
        A schedule (list of WeekCleaning objects).

    Example:
        csv-file content:
            01-01-2023,08-01,2023,Abdula,Bob,Chiara,Darkan,Eloise,Frank

        Becomes:
            [WeekCleaning(
                week_start=datetime(2023, 1, 1),
                kitchen=['Abdula', 'Bob', 'Chiara'],
                toilets='Darkan',
                showers='Eloise',
                upstairs='Frank'
            )]
    """
    data_path = Path(csv_file)
    lines = base64.b32decode(data_path.read_text()).decode("utf-8").splitlines()

    # split and remove lines with empty fields
    weeks = [str(line).split(",") for line in lines[1:]]

    schedule = []

    for begin, _, *kitchen, toilets, showers, upstairs in weeks:
        if not begin:
            break
        begin_date = dt.datetime.strptime(begin, "%d-%m-%Y")
        schedule.append(WeekCleaning(begin_date, kitchen, toilets, showers, upstairs))

    return schedule


def person_index(schedule: Schedule) -> dict[str, Schedule]:
    """
    Create a personal schedule for every person on the schedule.

    This function creates an index for each person on the schedule, mapping
    their name to their personal schedule. This is done because
    `create_calendar()` requires a personal schedule in order to make the
    personal .ics-files.

    Args:
        schedule: The overall schedule for cleaning.

    Returns:
        A mapping of person and their personal schedule.
    """
    index: dict[str, Schedule] = {}

    for week in schedule:
        for person in week:
            if person:
                index[person] = index.get(person, []) + [week]

    return index


def format_filename_template(template_path: Pathable, name: str) -> Path:
    """
    Construct personal filename by inserting the name in the template_path.

    Args:
        template_path: The path for the personal ics-file. Must include {} as
            placeholder for the `name`.
        name: Person's name.

    Returns:
        The formatted filename path.

    Raises:
        ValueError: if `template_path` does not include a format specifier.

    Example:
        >>> format_filename_template('calendars/{}-personal.ics', 'Yochem')
        Path('calendars/yochem-personal.ics')
    """
    formatted_filename = Path(str(template_path).format(name.lower()))
    if formatted_filename == Path(template_path):
        raise ValueError(
            f"filename {template_path} is not a format-string (include `{{}}`)"
        )
    return formatted_filename


def create_calendar(
    person: str, personal_schedule: Schedule, filename: Pathable = "{}.ics"
) -> None:
    """
    Create a calendar (ics-format) for given person with their personal schedule.

    Writes the file to disk at location given by `filename`.

    Args:
        person: Name of person in schedule.
        personal_schedule: Schedule of that person. Each week should include
            the person.
        filename: A path with a format specifier for inserting the name.

    Returns:
        None
    """
    cal = ics.Calendar()

    for week in personal_schedule:
        event = ics.Event(
            name=f"Weektaak: {week.jobname(person)}",
            begin=week.week_start.isoformat(sep=" "),
            duration=dt.timedelta(days=6),
            transparent=True,
            description=str(week),
            created=dt.datetime.now(),
            last_modified=dt.datetime.now(),
        )
        event.make_all_day()
        cal.events.add(event)

    personal_filename = format_filename_template(filename, person)
    personal_filename.parent.mkdir(exist_ok=True, parents=True)
    personal_filename.write_text(cal.serialize())


def admin_calendar(schedule: Schedule, filename: Pathable = "admin.ics") -> None:
    """
    Create a calendar (ics-format) for the admin.

    Writes the file to disk at location given by `filename`.

    Args:
        schedule: Schedule for everyone.
        filename: A path with a format specifier for inserting the name.

    Returns:
        None
    """
    cal = ics.Calendar()

    for week in schedule:
        event = ics.Event(
            name="Weektaak",
            begin=week.week_start.isoformat(sep=" "),
            duration=dt.timedelta(days=6),
            transparent=True,
            description=str(week),
            created=dt.datetime.now(),
            last_modified=dt.datetime.now(),
        )
        event.make_all_day()
        cal.events.add(event)

    Path(filename).write_text(cal.serialize())


def cleanup(ics_dir: Pathable) -> None:
    """
    Remove all files ending with .ics in given directory.

    Does nothing when directory does not exist.
    """
    target_dir = Path(ics_dir)

    if not target_dir.is_dir():
        print(f"nothing to cleanup: {target_dir} is not a directory")
        return

    for file in target_dir.glob("*.ics"):
        file.unlink()


def cli(cfg: dict[str, str]) -> None:
    """
    Command-line interface for converting csv data to a directory of ics-files.

    This function first removes all ics-files in the given output directory and
    then fills it with all personal calendars.
    """
    ics_filename_format = Path(cfg["ics_filename_format"])

    output_dir = ics_filename_format.parent
    cleanup(output_dir)

    schedule = csv2schedule(cfg["data_path"])
    index = person_index(schedule)

    for person, personal_schedule in index.items():
        create_calendar(person, personal_schedule, filename=ics_filename_format)

    if admin_path := cfg.get("admin"):
        admin_calendar(schedule, admin_path)


if __name__ == "__main__":
    config = {
        "ics_filename_format": "public/weektaak/cal/{}.ics",
        "data_path": "data",
        "admin": "public/weektaak/cal/admin.ics",
    }
    cli(config)