python 3.11 & code quality improvements

This commit is contained in:
Ace
2023-11-07 22:49:48 +01:00
parent 4f2dacc35a
commit 93c968da53
24 changed files with 243 additions and 151 deletions

View File

@@ -110,7 +110,8 @@ class Calendar(inkycal_module):
# Allocate space for month-names, weekdays etc.
month_name_height = int(im_height * 0.10)
weekdays_height = int(self.font.getsize('hg')[1] * 1.25)
text_bbox_height = self.font.getbbox("hg")
weekdays_height = int((text_bbox_height[3] - text_bbox_height[1])* 1.25)
logger.debug(f"month_name_height: {month_name_height}")
logger.debug(f"weekdays_height: {weekdays_height}")
@@ -182,15 +183,15 @@ class Calendar(inkycal_module):
]
logger.debug(f'weekday names: {weekday_names}')
for idx, weekday in enumerate(weekday_pos):
for index, weekday in enumerate(weekday_pos):
write(
im_black,
weekday,
(icon_width, weekdays_height),
weekday_names[idx],
weekday_names[index],
font=self.font,
autofit=True,
fill_height=1.0,
fill_height=0.9,
)
# Create a calendar template and flatten (remove nestings)
@@ -207,6 +208,10 @@ class Calendar(inkycal_module):
# remove zeros from calendar since they are not required
calendar_flat = [num for num in calendar_flat if num != 0]
# ensure all numbers have the same size
fontsize_numbers = int(min(icon_width, icon_height) * 0.5)
number_font = ImageFont.truetype(self.font.path, fontsize_numbers)
# Add the numbers on the correct positions
for number in calendar_flat:
if number != int(now.day):
@@ -215,9 +220,7 @@ class Calendar(inkycal_module):
grid[number],
(icon_width, icon_height),
str(number),
font=self.num_font,
fill_height=0.5,
fill_width=0.5,
font=number_font,
)
# Draw a red/black circle with the current day of month in white
@@ -262,10 +265,10 @@ class Calendar(inkycal_module):
from inkycal.modules.ical_parser import iCalendar
# find out how many lines can fit at max in the event section
line_spacing = 0
max_event_lines = events_height // (
self.font.getsize('hg')[1] + line_spacing
)
line_spacing = 2
text_bbox_height = self.font.getbbox("hg")
line_height = text_bbox_height[3] - text_bbox_height[1] + line_spacing
max_event_lines = events_height // (line_height + line_spacing)
# generate list of coordinates for each line
events_offset = im_height - events_height
@@ -329,31 +332,18 @@ class Calendar(inkycal_module):
# Find out how much space (width) the date format requires
lang = self.language
date_width = int(
max(
(
self.font.getsize(
events['begin'].format(self.date_format, locale=lang)
)[0]
for events in upcoming_events
)
)
* 1.1
date_width = int(max((
self.font.getlength(events['begin'].format(self.date_format, locale=lang))
for events in upcoming_events))* 1.1
)
time_width = int(
max(
(
self.font.getsize(
events['begin'].format(self.time_format, locale=lang)
)[0]
for events in upcoming_events
)
)
* 1.1
time_width = int(max((
self.font.getlength(events['begin'].format(self.time_format, locale=lang))
for events in upcoming_events))* 1.1
)
line_height = self.font.getsize('hg')[1] + line_spacing
text_bbox_height = self.font.getbbox("hg")
line_height = text_bbox_height[3] - text_bbox_height[1] + line_spacing
event_width_s = im_width - date_width - time_width
event_width_l = im_width - date_width
@@ -411,12 +401,13 @@ class Calendar(inkycal_module):
cursor += 1
else:
symbol = '- '
while self.font.getsize(symbol)[0] < im_width * 0.9:
while self.font.getlength(symbol) < im_width * 0.9:
symbol += ' -'
write(
im_black,
event_lines[0],
(im_width, self.font.getsize(symbol)[1]),
(im_width, line_height),
symbol,
font=self.font,
)