From 206887da2236886beeddbc814a0fdb7ba3f62278 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Wed, 29 May 2019 22:55:26 +0100 Subject: [PATCH 1/4] Adding optional json_transforms arg to PyPortal constructor for a function or list of functions to call with the parsed JSON dict. #38 --- adafruit_pyportal.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index a3daa01..3763abb 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -1,6 +1,6 @@ # The MIT License (MIT) # -# Copyright (c) 2019 Limor Fried for Adafruit Industries +# Copyright (c) 2019 Limor Fried for Adafruit Industries, Kevin J. Walters # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -26,7 +26,7 @@ CircuitPython driver for Adafruit PyPortal. -* Author(s): Limor Fried +* Author(s): Limor Fried, Kevin J. Walters Implementation Notes -------------------- @@ -127,6 +127,8 @@ class PyPortal: ``False``, no wrapping. :param text_maxlen: The max length of the text for text wrapping. Defaults to 0. :param text_transform: A function that will be called on the text before display + :param json_transforms: A function or a list of functions to call with the parsed JSON. + Changes and additions are permitted for the ``dict'' object. :param image_json_path: The JSON traversal path for a background image to display. Defaults to ``None``. :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, default_bg=0x000000, status_neopixel=None, text_font=None, text_position=None, text_color=0x808080, text_wrap=False, text_maxlen=0, text_transform=None, - image_json_path=None, image_resize=None, image_position=None, + json_transforms=None, image_json_path=None, + image_resize=None, image_position=None, caption_text=None, caption_font=None, caption_position=None, caption_color=0x808080, image_url_path=None, 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, self._text_font = None self._text = None + # Add any JSON translators + self._json_transforms = [] + if json_transforms: + if callable(json_transforms): + self._json_transforms.append(json_transforms) + else: + self._json_transforms.extend(filter(lambda fn : callable(fn), + json_transforms + )) + self._image_json_path = image_json_path self._image_url_path = image_url_path self._image_resize = image_resize @@ -713,6 +726,14 @@ def fetch(self, refresh_url=None): if self._image_url_path: image_url = self._image_url_path + # optional JSON post processing, apply any transformations + # these MAY change/add element + for idx, json_transform in enumerate(self._json_transforms): + try: + json_transform(json_out) + except Exception as error: + print("Exception from json_transform: ", idx, error) + # extract desired text/values from json if self._json_path: for path in self._json_path: From c5338593a54dbc5a83b52e5cd0c35bdef1abd748 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Thu, 30 May 2019 10:48:20 +0100 Subject: [PATCH 2/4] Removing unnecessary lambda and now re-throwing exceptions from json_transforms rather than ploughing on. #38 --- adafruit_pyportal.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 3763abb..be286bc 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -340,9 +340,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, if callable(json_transforms): self._json_transforms.append(json_transforms) else: - self._json_transforms.extend(filter(lambda fn : callable(fn), - json_transforms - )) + self._json_transforms.extend(filter(callable, json_transforms)) self._image_json_path = image_json_path self._image_url_path = image_url_path @@ -733,6 +731,7 @@ def fetch(self, refresh_url=None): json_transform(json_out) except Exception as error: print("Exception from json_transform: ", idx, error) + raise # extract desired text/values from json if self._json_path: From d2dec139234abda4627ad2bf9366b9b2b023aa4c Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Thu, 30 May 2019 11:18:50 +0100 Subject: [PATCH 3/4] Sphinx docs fix for PyPortal arg json_transforms - this aint TeX. #38 --- adafruit_pyportal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index be286bc..fe52d45 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -128,7 +128,7 @@ class PyPortal: :param text_maxlen: The max length of the text for text wrapping. Defaults to 0. :param text_transform: A function that will be called on the text before display :param json_transforms: A function or a list of functions to call with the parsed JSON. - Changes and additions are permitted for the ``dict'' object. + Changes and additions are permitted for the ``dict`` object. :param image_json_path: The JSON traversal path for a background image to display. Defaults to ``None``. :param image_resize: What size to resize the image we got from the json_path, make this a tuple From 57b2824fd922dece8d3102cf918f382b26451245 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Sat, 31 Aug 2019 23:19:10 +0100 Subject: [PATCH 4/4] Changing the named parameter json_transforms to be singular to match other similar ones. #38 #39 --- adafruit_pyportal.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index fe52d45..ad251bf 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -127,7 +127,7 @@ class PyPortal: ``False``, no wrapping. :param text_maxlen: The max length of the text for text wrapping. Defaults to 0. :param text_transform: A function that will be called on the text before display - :param json_transforms: A function or a list of functions to call with the parsed JSON. + :param json_transform: A function or a list of functions to call with the parsed JSON. Changes and additions are permitted for the ``dict`` object. :param image_json_path: The JSON traversal path for a background image to display. Defaults to ``None``. @@ -156,7 +156,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, default_bg=0x000000, status_neopixel=None, text_font=None, text_position=None, text_color=0x808080, text_wrap=False, text_maxlen=0, text_transform=None, - json_transforms=None, image_json_path=None, + json_transform=None, image_json_path=None, image_resize=None, image_position=None, caption_text=None, caption_font=None, caption_position=None, caption_color=0x808080, image_url_path=None, @@ -335,12 +335,12 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, self._text = None # Add any JSON translators - self._json_transforms = [] - if json_transforms: - if callable(json_transforms): - self._json_transforms.append(json_transforms) + self._json_transform = [] + if json_transform: + if callable(json_transform): + self._json_transform.append(json_transform) else: - self._json_transforms.extend(filter(callable, json_transforms)) + self._json_transform.extend(filter(callable, json_transform)) self._image_json_path = image_json_path self._image_url_path = image_url_path @@ -726,7 +726,7 @@ def fetch(self, refresh_url=None): # optional JSON post processing, apply any transformations # these MAY change/add element - for idx, json_transform in enumerate(self._json_transforms): + for idx, json_transform in enumerate(self._json_transform): try: json_transform(json_out) except Exception as error: