added --months -m to accept a month or range of months
This commit is contained in:
parent
5c4060cff3
commit
b601ef0c67
48
main.py
48
main.py
@ -165,29 +165,69 @@ def process_month(months, monthly_events, year, month, week_of_month, week, even
|
|||||||
debug_print("", DEBUG)
|
debug_print("", DEBUG)
|
||||||
return events
|
return events
|
||||||
|
|
||||||
|
def process_months(months, monthly_events, year, month_range):
|
||||||
def process_months(months, monthly_events, year):
|
|
||||||
"""process full year of months and return list of events"""
|
"""process full year of months and return list of events"""
|
||||||
events = []
|
events = []
|
||||||
for month in range(1, 13):
|
for month in range(1, 13):
|
||||||
|
if month not in month_range:
|
||||||
|
continue
|
||||||
debug_print(f"MONTH: {month}", DEBUG)
|
debug_print(f"MONTH: {month}", DEBUG)
|
||||||
for week_of_month, week in enumerate(months[month]):
|
for week_of_month, week in enumerate(months[month]):
|
||||||
debug_print(f"{week_of_month} {week}", DEBUG)
|
debug_print(f"{week_of_month} {week}", DEBUG)
|
||||||
events = process_month(months, monthly_events, year, month, week_of_month, week, events)
|
events = process_month(months, monthly_events, year, month, week_of_month, week, events)
|
||||||
return events
|
return events
|
||||||
|
|
||||||
|
def validate_months(ctx, param, value):
|
||||||
|
"""
|
||||||
|
Validates and processes the --months option.
|
||||||
|
Accepts a single number (1-12) or a range (1-3).
|
||||||
|
Defaults to 1-12 if no value is provided.
|
||||||
|
"""
|
||||||
|
if value is None:
|
||||||
|
return list(range(1, 13)) # Default behavior
|
||||||
|
|
||||||
|
parts = value.split('-')
|
||||||
|
|
||||||
|
if len(parts) == 1:
|
||||||
|
# Handle single month input
|
||||||
|
try:
|
||||||
|
month = int(parts[0])
|
||||||
|
if 1 <= month <= 12:
|
||||||
|
return [month]
|
||||||
|
else:
|
||||||
|
raise click.BadParameter("Month must be between 1 and 12.", param=param)
|
||||||
|
except ValueError:
|
||||||
|
raise click.BadParameter("Invalid month format. Please use an integer between 1 and 12.", param=param)
|
||||||
|
|
||||||
|
elif len(parts) == 2:
|
||||||
|
# Handle range input
|
||||||
|
try:
|
||||||
|
start, end = int(parts[0]), int(parts[1])
|
||||||
|
if 1 <= start <= 12 and 1 <= end <= 12 and start <= end:
|
||||||
|
return list(range(start, end + 1))
|
||||||
|
else:
|
||||||
|
raise click.BadParameter("Invalid month range. Both months must be between 1 and 12, and the start month must be less than or equal to the end month.", param=param)
|
||||||
|
except ValueError:
|
||||||
|
raise click.BadParameter("Invalid range format. Please use two integers separated by a hyphen (e.g., 1-3).", param=param)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Handle invalid format
|
||||||
|
raise click.BadParameter("Invalid format. Use a single number (e.g., 3) or a range (e.g., 1-3).", param=param)
|
||||||
|
|
||||||
# @click.argument('filename', type=click.File('w'), default="calendar.csv", required=False)
|
# @click.argument('filename', type=click.File('w'), default="calendar.csv", required=False)
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option("--debug", "-d", is_flag=True, default=False, help="Print debug output")
|
@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.')
|
@click.option('--year', "-y", type=int, default=date.today().year, help='Specify the year.')
|
||||||
|
@click.option('--months', '-m', 'month_range', callback=validate_months, default=None,
|
||||||
|
help='A single month (1-12) or a range (e.g., 1-3). Defaults to all months (1-12).')
|
||||||
@click.argument('filename', default="calendar.csv", required=False)
|
@click.argument('filename', default="calendar.csv", required=False)
|
||||||
def main(debug, year, filename):
|
def main(debug, year, month_range, filename):
|
||||||
"""Create a .csv file of church events for the year. Modify events.py to change events"""
|
"""Create a .csv file of church events for the year. Modify events.py to change events"""
|
||||||
global DEBUG
|
global DEBUG
|
||||||
DEBUG = debug
|
DEBUG = debug
|
||||||
months = initalize_year(year)
|
months = initalize_year(year)
|
||||||
monthly_events = initalize_events(EVENT_LIST)
|
monthly_events = initalize_events(EVENT_LIST)
|
||||||
events = process_months(months, monthly_events, year)
|
events = process_months(months, monthly_events, year, month_range)
|
||||||
# events = sort_events(events)
|
# events = sort_events(events)
|
||||||
write_calendar(events, filename)
|
write_calendar(events, filename)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user