2021-12-12 18:53:00 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
CHURCH CALENDAR CSV GENERATOR
|
|
|
|
"""
|
|
|
|
|
|
|
|
import calendar
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime
|
2024-12-29 06:44:43 -07:00
|
|
|
from pprint import pprint
|
2021-12-12 18:53:00 -07:00
|
|
|
import sys
|
|
|
|
import click
|
|
|
|
from dataclass_csv import DataclassWriter
|
2024-12-29 07:30:41 -07:00
|
|
|
from events import EVENT_LIST, YEAR, DEBUG
|
|
|
|
|
|
|
|
|
2024-12-29 07:41:15 -07:00
|
|
|
def debug_print(data, condition=True, end="\n"):
|
2024-12-29 07:30:41 -07:00
|
|
|
"""Conditionally print data using pprint.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data: The data to be printed.
|
|
|
|
condition: A boolean value. If True, the data will be printed.
|
|
|
|
"""
|
|
|
|
if condition:
|
2024-12-29 07:41:15 -07:00
|
|
|
print(data, end=end)
|
2024-12-29 07:30:41 -07:00
|
|
|
|
2021-12-12 18:53:00 -07:00
|
|
|
|
2024-12-28 10:36:08 -07:00
|
|
|
"""
|
|
|
|
build a list of months / weeks / day of the month in those weeks
|
|
|
|
months 1-12, weeks 0-5 days of week 5 6 are the 1st and 2nd of month
|
|
|
|
MONTHS[1] [0] [ 0 0 0 0 0 1 2 ]
|
|
|
|
|
|
|
|
due to how months work
|
|
|
|
week 0 can have null days (represented by 0's)
|
|
|
|
weeks 1 - 4 will have all 7 days present
|
|
|
|
week 5 may have 7 days or nulls
|
|
|
|
week 6 can have a monday and/or tuesday in them if the 1st falls on a
|
|
|
|
saturday or sunday in week 0
|
|
|
|
|
|
|
|
EVENT_LIST has weeks as 1-6 while MONTHS has weeks as 0-5, offsetting will
|
|
|
|
be necessary
|
|
|
|
"""
|
|
|
|
MONTHS: dict[int, list[list[int]]]
|
|
|
|
MONTHS = dict(enumerate([calendar.monthcalendar(YEAR, month) for month in
|
|
|
|
range(1, 13)], start=1))
|
2021-12-12 18:53:00 -07:00
|
|
|
|
2024-12-29 06:46:28 -07:00
|
|
|
""" convert EVENT_LIST into monthly_events """
|
|
|
|
monthly_events = [[] for _ in range(7)]
|
2024-12-28 10:36:08 -07:00
|
|
|
for event in EVENT_LIST:
|
2024-12-29 06:46:28 -07:00
|
|
|
monthly_events[event[0]].append(event[1:])
|
2021-12-12 18:53:00 -07:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Event():
|
|
|
|
"""Event for CSV export"""
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
event_name: str
|
|
|
|
venue_name: str
|
|
|
|
organizer_name: str
|
|
|
|
start_date: str
|
|
|
|
start_time: str
|
|
|
|
end_date: str
|
|
|
|
end_time: str
|
|
|
|
all_day_event: str
|
|
|
|
categories: str
|
|
|
|
event_cost: str
|
|
|
|
event_phone: str
|
|
|
|
event_website: str
|
|
|
|
show_map_link: str
|
|
|
|
show_map: str
|
|
|
|
event_description: str
|
|
|
|
|
|
|
|
def suffix(day: int) -> str:
|
|
|
|
"""convert day to suffix"""
|
|
|
|
result: str = 'th'
|
|
|
|
if day in [1, 21, 31]:
|
|
|
|
result = 'st'
|
|
|
|
elif day in [2, 22]:
|
|
|
|
result = 'nd'
|
|
|
|
elif day in [3, 23]:
|
|
|
|
result = 'rd'
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def sort_events(events: list[Event]) -> list[Event]:
|
|
|
|
"""sort events"""
|
|
|
|
events = sorted(events, key=lambda k: (k.start_date, k.start_time))
|
|
|
|
return events
|
|
|
|
|
|
|
|
def add_event(events: list[Event],
|
|
|
|
name: str,
|
|
|
|
categories: str,
|
|
|
|
description: str,
|
|
|
|
start,
|
|
|
|
finish) -> None:
|
|
|
|
"""create event"""
|
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
event = Event(name,
|
|
|
|
"Main Campus",
|
|
|
|
"",
|
|
|
|
start.strftime("%Y-%m-%d"),
|
|
|
|
start.strftime("%I:%M %p"),
|
|
|
|
finish.strftime("%Y-%m-%d"),
|
|
|
|
finish.strftime("%I:%M %p"),
|
|
|
|
"FALSE",
|
|
|
|
categories,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
description)
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
|
|
|
|
def write_calendar(events: list[Event]) -> None:
|
|
|
|
"""write calendar to csv file"""
|
|
|
|
with open("calendar.csv", "w", encoding="utf-8") as handle:
|
|
|
|
writer =DataclassWriter(handle, events, Event)
|
|
|
|
writer.map("event_name").to("EVENT NAME")
|
|
|
|
writer.map("venue_name").to("VENUE NAME")
|
|
|
|
writer.map("organizer_name").to("ORGANIZER NAME")
|
|
|
|
writer.map("start_date").to("START DATE")
|
|
|
|
writer.map("start_time").to("START TIME")
|
|
|
|
writer.map("end_date").to("END DATE")
|
|
|
|
writer.map("end_time").to("END TIME")
|
|
|
|
writer.map("all_day_event").to("ALL DAY EVENT")
|
|
|
|
writer.map("categories").to("CATEGORIES")
|
|
|
|
writer.map("event_cost").to("EVENT COST")
|
|
|
|
writer.map("event_phone").to("EVENT PHONE")
|
|
|
|
writer.map("event_website").to("EVENT_WEBSITE")
|
|
|
|
writer.map("show_map_link").to("SHOW MAP LINK?")
|
|
|
|
writer.map("show_map").to("SHOW MAP?")
|
|
|
|
writer.map("event_description").to("EVENT DESCRIPTION")
|
|
|
|
writer.write()
|
|
|
|
|
2024-12-29 07:05:07 -07:00
|
|
|
|
2024-12-29 07:46:00 -07:00
|
|
|
def process_day(events, day_events):
|
|
|
|
"""process all events for one day"""
|
|
|
|
for event_item in day_events:
|
|
|
|
debug_print(f" {event_item}", DEBUG)
|
|
|
|
return events
|
2024-12-29 07:20:53 -07:00
|
|
|
|
|
|
|
def process_month(MONTHS, monthly_events, week_of_month, week, events):
|
|
|
|
"""process one MONTHS of events"""
|
|
|
|
for day_of_week, day in enumerate(week):
|
|
|
|
if day > 0:
|
|
|
|
"""
|
|
|
|
month, week_of_month, day_of_week, day
|
|
|
|
use monthly_events[day_of_week]
|
|
|
|
"""
|
2024-12-29 07:46:00 -07:00
|
|
|
debug_print(f" {day_of_week}-{day:02d}", DEBUG)
|
2024-12-29 07:20:53 -07:00
|
|
|
day_events = [x[1:] for x in monthly_events[day_of_week] if week_of_month + 1 in x[0]]
|
2024-12-29 07:46:00 -07:00
|
|
|
# debug_print(day_events, DEBUG)
|
|
|
|
events = process_day(events, day_events)
|
2024-12-29 07:30:41 -07:00
|
|
|
debug_print("", DEBUG)
|
2024-12-29 07:20:53 -07:00
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
def process_months(MONTHS, monthly_events):
|
|
|
|
"""process full year of MONTHS and return list of events"""
|
2024-12-29 07:05:07 -07:00
|
|
|
events = []
|
|
|
|
for month in range(1, 13):
|
2024-12-29 07:30:41 -07:00
|
|
|
debug_print(f"MONTH: {month}", DEBUG)
|
2024-12-29 07:05:07 -07:00
|
|
|
for week_of_month, week in enumerate(MONTHS[month]):
|
2024-12-29 07:30:41 -07:00
|
|
|
debug_print(f"{week_of_month} {week}", DEBUG)
|
2024-12-29 07:20:53 -07:00
|
|
|
events = process_month(MONTHS, monthly_events, week_of_month, week, events)
|
2024-12-29 07:05:07 -07:00
|
|
|
return events
|
|
|
|
|
2021-12-12 18:53:00 -07:00
|
|
|
@click.command()
|
|
|
|
def main():
|
|
|
|
"""create csv file"""
|
2024-12-29 07:20:53 -07:00
|
|
|
events = process_months(MONTHS, monthly_events)
|
2024-12-29 07:05:07 -07:00
|
|
|
events = sort_events(events)
|
|
|
|
write_calendar(events)
|
2021-12-12 18:53:00 -07:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main() # pylint: disable=E1120
|