Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize filenames and handle member issues in attendance.py example script #155

Merged
merged 10 commits into from
Oct 20, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ cython_debug/
# Project specific
exports/*
config.py
Excel/*



54 changes: 41 additions & 13 deletions attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import csv
import os
import re
from datetime import date

from config import password, username
Expand Down Expand Up @@ -38,8 +39,11 @@ async def main():
os.makedirs("./exports")

for e in events:
filename = f"{e['startTimestamp']}-{e['heading']}.csv"
Copy link
Collaborator

@elliot-100 elliot-100 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to refactor out the filename-derivation code into a self-documenting function, e.g. sanitised_filename(event).

filename = str(filename).strip().replace(" ", "_")
filename = re.sub(r"(?u)[^-\w.]", "", filename)
filename = os.path.join(
"./exports", f"{e['startTimestamp']}-{e['heading']}.csv"
"./exports", filename
)
with open(filename, "w", newline="") as csvfile:
spamwriter = csv.writer(
Expand All @@ -50,8 +54,12 @@ async def main():
["Start", "End", "Description", "Name", "Answer", "Organizer"]
)
for o in e["owners"]:
person = await s.get_person(o["id"])
full_name = person["firstName"] + " " + person["lastName"]
try:
person = await s.get_person(o["id"])
except KeyError:
full_name = o["id"]
else:
full_name = person["firstName"] + " " + person["lastName"]
Copy link
Collaborator

@elliot-100 elliot-100 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend refactoring out this repeated line to a function e.g. full_name(person).

spamwriter.writerow(
[
e["startTimestamp"],
Expand All @@ -64,8 +72,12 @@ async def main():
)
if args.a is True:
for r in e["responses"]["acceptedIds"]:
person = await s.get_person(r)
full_name = person["firstName"] + " " + person["lastName"]
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and this repeated block too.

person = await s.get_person(r)
except KeyError:
full_name = r
else:
full_name = person["firstName"] + " " + person["lastName"]
spamwriter.writerow(
[
e["startTimestamp"],
Expand All @@ -76,8 +88,12 @@ async def main():
]
)
for r in e["responses"]["declinedIds"]:
person = await s.get_person(r)
full_name = person["firstName"] + " " + person["lastName"]
try:
person = await s.get_person(r)
except KeyError:
full_name = r
else:
full_name = person["firstName"] + " " + person["lastName"]
spamwriter.writerow(
[
e["startTimestamp"],
Expand All @@ -88,8 +104,12 @@ async def main():
]
)
for r in e["responses"]["unansweredIds"]:
person = await s.get_person(r)
full_name = person["firstName"] + " " + person["lastName"]
try:
person = await s.get_person(r)
except KeyError:
full_name = r
else:
full_name = person["firstName"] + " " + person["lastName"]
spamwriter.writerow(
[
e["startTimestamp"],
Expand All @@ -100,8 +120,12 @@ async def main():
]
)
for r in e["responses"]["unconfirmedIds"]:
person = await s.get_person(r)
full_name = person["firstName"] + " " + person["lastName"]
try:
person = await s.get_person(r)
except KeyError:
full_name = r
else:
full_name = person["firstName"] + " " + person["lastName"]
spamwriter.writerow(
[
e["startTimestamp"],
Expand All @@ -112,8 +136,12 @@ async def main():
]
)
for r in e["responses"]["waitinglistIds"]:
person = await s.get_person(r)
full_name = person["firstName"] + " " + person["lastName"]
try:
person = await s.get_person(r)
except KeyError:
full_name = r
else:
full_name = person["firstName"] + " " + person["lastName"]
spamwriter.writerow(
[
e["startTimestamp"],
Expand Down
Loading