Skip to content

Commit 206887d

Browse files
author
Kevin J Walters
committed
Adding optional json_transforms arg to PyPortal constructor for a function or list of functions to call with the parsed JSON dict. #38
1 parent 013d0d6 commit 206887d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

adafruit_pyportal.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22
#
3-
# Copyright (c) 2019 Limor Fried for Adafruit Industries
3+
# Copyright (c) 2019 Limor Fried for Adafruit Industries, Kevin J. Walters
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +26,7 @@
2626
CircuitPython driver for Adafruit PyPortal.
2727
2828
29-
* Author(s): Limor Fried
29+
* Author(s): Limor Fried, Kevin J. Walters
3030
3131
Implementation Notes
3232
--------------------
@@ -127,6 +127,8 @@ class PyPortal:
127127
``False``, no wrapping.
128128
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
129129
:param text_transform: A function that will be called on the text before display
130+
:param json_transforms: A function or a list of functions to call with the parsed JSON.
131+
Changes and additions are permitted for the ``dict'' object.
130132
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
131133
``None``.
132134
:param image_resize: What size to resize the image we got from the json_path, make this a tuple
@@ -154,7 +156,8 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
154156
default_bg=0x000000, status_neopixel=None,
155157
text_font=None, text_position=None, text_color=0x808080,
156158
text_wrap=False, text_maxlen=0, text_transform=None,
157-
image_json_path=None, image_resize=None, image_position=None,
159+
json_transforms=None, image_json_path=None,
160+
image_resize=None, image_position=None,
158161
caption_text=None, caption_font=None, caption_position=None,
159162
caption_color=0x808080, image_url_path=None,
160163
success_callback=None, esp=None, external_spi=None, debug=False):
@@ -331,6 +334,16 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
331334
self._text_font = None
332335
self._text = None
333336

337+
# Add any JSON translators
338+
self._json_transforms = []
339+
if json_transforms:
340+
if callable(json_transforms):
341+
self._json_transforms.append(json_transforms)
342+
else:
343+
self._json_transforms.extend(filter(lambda fn : callable(fn),
344+
json_transforms
345+
))
346+
334347
self._image_json_path = image_json_path
335348
self._image_url_path = image_url_path
336349
self._image_resize = image_resize
@@ -713,6 +726,14 @@ def fetch(self, refresh_url=None):
713726
if self._image_url_path:
714727
image_url = self._image_url_path
715728

729+
# optional JSON post processing, apply any transformations
730+
# these MAY change/add element
731+
for idx, json_transform in enumerate(self._json_transforms):
732+
try:
733+
json_transform(json_out)
734+
except Exception as error:
735+
print("Exception from json_transform: ", idx, error)
736+
716737
# extract desired text/values from json
717738
if self._json_path:
718739
for path in self._json_path:

0 commit comments

Comments
 (0)