|
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 | -------------------- |
@@ -135,6 +135,8 @@ class PyPortal: |
135 | 135 | ``False``, no wrapping. |
136 | 136 | :param text_maxlen: The max length of the text for text wrapping. Defaults to 0. |
137 | 137 | :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. |
138 | 140 | :param image_json_path: The JSON traversal path for a background image to display. Defaults to |
139 | 141 | ``None``. |
140 | 142 | :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, |
162 | 164 | default_bg=0x000000, status_neopixel=None, |
163 | 165 | text_font=None, text_position=None, text_color=0x808080, |
164 | 166 | 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, |
166 | 169 | caption_text=None, caption_font=None, caption_position=None, |
167 | 170 | caption_color=0x808080, image_url_path=None, |
168 | 171 | 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, |
350 | 353 | self._text_font = None |
351 | 354 | self._text = None |
352 | 355 |
|
| 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 | + |
353 | 364 | self._image_json_path = image_json_path |
354 | 365 | self._image_url_path = image_url_path |
355 | 366 | self._image_resize = image_resize |
@@ -787,6 +798,15 @@ def fetch(self, refresh_url=None): |
787 | 798 | if self._image_url_path: |
788 | 799 | image_url = self._image_url_path |
789 | 800 |
|
| 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 | + |
790 | 810 | # extract desired text/values from json |
791 | 811 | if self._json_path: |
792 | 812 | for path in self._json_path: |
|
0 commit comments