Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
Add broad exception to ensure catching errors caused by missing _imagingft module on some installations. Something seems to have happened in Pillow >8.2.
  • Loading branch information
tmjo committed Feb 5, 2022
1 parent d7485df commit 3b23e7c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions custom_components/norwegianweather/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ def process_weather_image(self, weatherdata, filename=None, qty=6):
f"Processing weather image from {len(weatherdata)} time intervals - creating {qty} images."
)
cnt = 0
font = image_font()
_LOGGER.debug(f"PIL/image version: {Image.__version__}")
font = weatherimage_font()
for data in weatherdata:
time = data.get("time", None)
imagedata = image_create_process_data(time, data, self.location.units)
Expand Down Expand Up @@ -666,19 +667,21 @@ def image_create(imagedata, font):
return newimage


def image_font():
def weatherimage_font():
font = None
try:
font = ImageFont.truetype(CONST_FILE_FONT, 25)
_LOGGER.debug(f"Loaded font {CONST_FILE_FONT} {font}.")
except (TypeError, FileNotFoundError, OSError) as e:
# except (TypeError, FileNotFoundError, OSError) as e:
except Exception as e:
_LOGGER.debug(f"Could not apply truetype font {CONST_FILE_FONT} ({e}).")

if font is None:
try:
font = ImageFont.load(CONST_FILE_FONT_PIL)
_LOGGER.debug(f"Loaded PIL-font {CONST_FILE_FONT_PIL}.")
except (TypeError, FileNotFoundError, OSError, AttributeError) as e:
# except (TypeError, FileNotFoundError, OSError, AttributeError) as e:
except Exception as e:
_LOGGER.debug(
f"Could not apply PIL-font {CONST_FILE_FONT_PIL}. Will have to use ugly default font, sorry. ({e})"
)
Expand Down

0 comments on commit 3b23e7c

Please sign in to comment.