|
1 | 1 | # The MIT License (MIT)
|
2 | 2 | #
|
3 |
| -# Copyright (c) 2019 Limor Fried for Adafruit Industries |
| 3 | +# Copyright (c) 2019 Limor Fried for Adafruit Industries, Kevin J. Walters |
4 | 4 | #
|
5 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 | 6 | # of this software and associated documentation files (the "Software"), to deal
|
|
26 | 26 | CircuitPython driver for Adafruit PyPortal.
|
27 | 27 |
|
28 | 28 |
|
29 |
| -* Author(s): Limor Fried |
| 29 | +* Author(s): Limor Fried, Kevin J. Walters |
30 | 30 |
|
31 | 31 | Implementation Notes
|
32 | 32 | --------------------
|
@@ -127,6 +127,8 @@ class PyPortal:
|
127 | 127 | ``False``, no wrapping.
|
128 | 128 | :param text_maxlen: The max length of the text for text wrapping. Defaults to 0.
|
129 | 129 | :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. |
130 | 132 | :param image_json_path: The JSON traversal path for a background image to display. Defaults to
|
131 | 133 | ``None``.
|
132 | 134 | :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,
|
154 | 156 | default_bg=0x000000, status_neopixel=None,
|
155 | 157 | text_font=None, text_position=None, text_color=0x808080,
|
156 | 158 | 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, |
158 | 161 | caption_text=None, caption_font=None, caption_position=None,
|
159 | 162 | caption_color=0x808080, image_url_path=None,
|
160 | 163 | 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,
|
331 | 334 | self._text_font = None
|
332 | 335 | self._text = None
|
333 | 336 |
|
| 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 | + |
334 | 347 | self._image_json_path = image_json_path
|
335 | 348 | self._image_url_path = image_url_path
|
336 | 349 | self._image_resize = image_resize
|
@@ -713,6 +726,14 @@ def fetch(self, refresh_url=None):
|
713 | 726 | if self._image_url_path:
|
714 | 727 | image_url = self._image_url_path
|
715 | 728 |
|
| 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 | + |
716 | 737 | # extract desired text/values from json
|
717 | 738 | if self._json_path:
|
718 | 739 | for path in self._json_path:
|
|
0 commit comments