Skip to content

New optional json_transform arg to PyPortal constructor to post-process parsed JSON #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions adafruit_pyportal.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +26,7 @@
CircuitPython driver for Adafruit PyPortal.


* Author(s): Limor Fried
* Author(s): Limor Fried, Kevin J. Walters

Implementation Notes
--------------------
Expand Down Expand Up @@ -135,6 +135,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_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``.
:param image_resize: What size to resize the image we got from the json_path, make this a tuple
Expand Down Expand Up @@ -162,7 +164,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_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,
success_callback=None, esp=None, external_spi=None, debug=False):
Expand Down Expand Up @@ -350,6 +353,14 @@ 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_transform = []
if json_transform:
if callable(json_transform):
self._json_transform.append(json_transform)
else:
self._json_transform.extend(filter(callable, json_transform))

self._image_json_path = image_json_path
self._image_url_path = image_url_path
self._image_resize = image_resize
Expand Down Expand Up @@ -788,6 +799,15 @@ 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_transform):
try:
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:
for path in self._json_path:
Expand Down