Skip to content

Commit 636609f

Browse files
authored
Merge pull request #39 from kevinjwalters/master
New optional json_transform arg to PyPortal constructor to post-process parsed JSON
2 parents 4cdcbc8 + 3026914 commit 636609f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

adafruit_pyportal.py

Lines changed: 23 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
--------------------
@@ -135,6 +135,8 @@ class PyPortal:
135135
``False``, no wrapping.
136136
:param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
137137
:param text_transform: A function that will be called on the text before display
138+
:param json_transform: A function or a list of functions to call with the parsed JSON.
139+
Changes and additions are permitted for the ``dict`` object.
138140
:param image_json_path: The JSON traversal path for a background image to display. Defaults to
139141
``None``.
140142
:param image_resize: What size to resize the image we got from the json_path, make this a tuple
@@ -162,7 +164,8 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
162164
default_bg=0x000000, status_neopixel=None,
163165
text_font=None, text_position=None, text_color=0x808080,
164166
text_wrap=False, text_maxlen=0, text_transform=None,
165-
image_json_path=None, image_resize=None, image_position=None,
167+
json_transform=None, image_json_path=None,
168+
image_resize=None, image_position=None,
166169
caption_text=None, caption_font=None, caption_position=None,
167170
caption_color=0x808080, image_url_path=None,
168171
success_callback=None, esp=None, external_spi=None, debug=False):
@@ -350,6 +353,14 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
350353
self._text_font = None
351354
self._text = None
352355

356+
# Add any JSON translators
357+
self._json_transform = []
358+
if json_transform:
359+
if callable(json_transform):
360+
self._json_transform.append(json_transform)
361+
else:
362+
self._json_transform.extend(filter(callable, json_transform))
363+
353364
self._image_json_path = image_json_path
354365
self._image_url_path = image_url_path
355366
self._image_resize = image_resize
@@ -787,6 +798,15 @@ def fetch(self, refresh_url=None):
787798
if self._image_url_path:
788799
image_url = self._image_url_path
789800

801+
# optional JSON post processing, apply any transformations
802+
# these MAY change/add element
803+
for idx, json_transform in enumerate(self._json_transform):
804+
try:
805+
json_transform(json_out)
806+
except Exception as error:
807+
print("Exception from json_transform: ", idx, error)
808+
raise
809+
790810
# extract desired text/values from json
791811
if self._json_path:
792812
for path in self._json_path:

0 commit comments

Comments
 (0)