refactor and add cli options

- refactor the creation of MONTHS into a definition
- refactor the creation of event_list from EVENTS into a definition
- both of these are initalized in main()
- added option --debug/-d to enable debug printing, default = off
- added option --year/-y for year, default = current year
This commit is contained in:
Fredrick W. Warren 2024-12-29 09:56:29 -07:00
parent bf5234aa6e
commit 1c480360df

51
main.py
View File

@ -5,23 +5,14 @@ CHURCH CALENDAR CSV GENERATOR
import calendar import calendar
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import date, datetime
from pprint import pprint from pprint import pprint
import sys import sys
import click import click
from dataclass_csv import DataclassWriter from dataclass_csv import DataclassWriter
from events import EVENT_LIST, YEAR, DEBUG from events import EVENT_LIST
def debug_print(data, condition=True, end="\n"):
"""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:
print(data, end=end)
""" """
@ -39,14 +30,30 @@ due to how months work
EVENT_LIST has weeks as 1-6 while MONTHS has weeks as 0-5, offsetting will EVENT_LIST has weeks as 1-6 while MONTHS has weeks as 0-5, offsetting will
be necessary be necessary
""" """
MONTHS: dict[int, list[list[int]]]
MONTHS = dict(enumerate([calendar.monthcalendar(YEAR, month) for month in
range(1, 13)], start=1))
""" convert EVENT_LIST into monthly_events """ def initalize_events(event_list):
monthly_events = [[] for _ in range(7)] """ convert EVENT_LIST into monthly_events """
for event in EVENT_LIST: monthly_events = [[] for _ in range(7)]
for event in event_list:
monthly_events[event[0]].append(event[1:]) monthly_events[event[0]].append(event[1:])
return monthly_events
def debug_print(data, condition=True, end="\n"):
"""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:
print(data, end=end)
def initalize_year(year: int) -> dict[int, list[list[int]]]:
months = dict(enumerate
([calendar.monthcalendar(year, month) for month in range(1, 13)],
start=1))
return months
@dataclass @dataclass
class Event(): class Event():
@ -170,9 +177,15 @@ def process_months(MONTHS, monthly_events, year):
return events return events
@click.command() @click.command()
def main(): @click.option("--debug", "-d", is_flag=True, default=False, help="Print debug output")
@click.option('--year', "-y", type=int, default=date.today().year, help='Specify the year.')
def main(debug, year):
"""create csv file""" """create csv file"""
events = process_months(MONTHS, monthly_events, YEAR) global DEBUG
DEBUG = debug
months = initalize_year(year)
monthly_events = initalize_events(EVENT_LIST)
events = process_months(months, monthly_events, year)
# events = sort_events(events) # events = sort_events(events)
write_calendar(events) write_calendar(events)
sys.exit(0) sys.exit(0)