add optional argument for month

This commit is contained in:
fram3d 2023-09-27 17:40:11 +02:00
parent a321ebe765
commit 62da74bf99
Signed by: fram3d
GPG Key ID: 938920E709EEA32A

View File

@ -1,5 +1,6 @@
#! /usr/bin/env python3
import argparse
import freetype
import io
from PIL import Image, ImageDraw, ImageFont
@ -11,10 +12,16 @@ CURRENT_TIME = dt.date.today()
NEXT_MONTH = CURRENT_TIME + relativedelta.relativedelta(months=1, day=1)
DAYS_OF_WEEK_SR = ("PON", "UTO", "SRE", "ČET", "PET", "SUB", "NED")
MONTHS_SR = ("Januar", "Februar", "Mart", "April", "Maj", "Jun", "Jul", "Avgust",\
"Septembar", "Oktobar", "Novembar", "Decembar")
"Septembar", "Oktobar", "Novembar", "Decembar")
def parseArgs(parser):
"""
Parse all arguments and return the list of argument values
"""
parser.add_argument("month", metavar = "MM", help = "two digit number representing the month for which to generate poster", default = "empty", nargs = "?")
return parser.parse_args()
def load_events(csv_path:str) -> list[dict]:
def load_events(csv_path:str, month:int) -> list[dict]:
events = []
with open(csv_path) as csv_file:
csv_reader = csv.reader(csv_file)
@ -27,11 +34,11 @@ def load_events(csv_path:str) -> list[dict]:
current_event = {"date":event_date_parsed,
"time":event_time,
"title":event_title.strip()}
if event_date_parsed >= NEXT_MONTH:
if event_date_parsed >= month:
events.append(current_event)
return events
def drawPoster(events, bg, fg):
def drawPoster(events, bg, fg, month:int):
fontFacade = ImageFont.truetype('./site/font/Facade-Sud.woff', size=110)
fontIosevka = ImageFont.truetype('./site/font/iosevka-regular.woff', size=60)
fontIosevkaSmall = ImageFont.truetype('./site/font/iosevka-regular.woff', size=45)
@ -45,7 +52,7 @@ def drawPoster(events, bg, fg):
_, _, w, _ = draw.textbbox((0, 0), header, font=fontFacade)
draw.text(((W-w)/2, 120), header, font=fontFacade, fill=fg)
subheader = f"Plan za {MONTHS_SR[NEXT_MONTH.month - 1]}"
subheader = f"Plan za {MONTHS_SR[month.month - 1]}"
_, _, w, _ = draw.textbbox((0, 0), subheader, font=fontIosevka)
draw.text(((W-w)/2, 240), subheader, font=fontIosevka, fill=fg)
@ -79,11 +86,26 @@ def drawPoster(events, bg, fg):
return img
def main():
# Parse arguments
parser = argparse.ArgumentParser(description="Generate images of the poster")
args = parseArgs(parser)
events = load_events("dogadjaji.csv")
# Set month based on user input
month = NEXT_MONTH
if args.month.isdigit():
month = dt.date(CURRENT_TIME.year, int(args.month), 1)
elif args.month != "empty":
print("Month has to be specified as a number. I will use next month as the default")
img = drawPoster(events, (0, 0, 0), (20, 250, 50))
img.save('poster_dark.png')
# Load events and draw a poseter
events = load_events("dogadjaji.csv", month)
img = drawPoster(events, (255, 255, 255), (0, 0, 0))
img.save('poster_light.png')
img = drawPoster(events, (0, 0, 0), (20, 250, 50), month)
img.save('poster_dark.png')
img = drawPoster(events, (255, 255, 255), (0, 0, 0), month)
img.save('poster_light.png')
if __name__ == "__main__":
main()