Skip to content

Handle empty json and non-data labels better #24

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 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions adafruit_magtag/magtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
self._text_font = []
self._text_line_spacing = []
self._text_anchor_point = []
self._text_is_data = []

gc.collect()

Expand All @@ -127,6 +128,7 @@ def add_text(
text_scale=1,
line_spacing=1.25,
text_anchor_point=(0, 0.5),
is_data=True,
):
"""
Add text labels with settings
Expand All @@ -143,6 +145,10 @@ def add_text(
length. Defaults to 0.
:param text_transform: A function that will be called on the text before display
:param int text_scale: The factor to scale the default size of the text by
:param float line_spacing: The factor to space the lines apart
:param (float, float) text_anchor_point: Values between 0 and 1 to indicate where the text
position is relative to the label
:param bool is_data: If True, fetch will attempt to update the label
"""
if text_font is terminalio.FONT:
self._text_font.append(text_font)
Expand Down Expand Up @@ -174,6 +180,7 @@ def add_text(
self._text_scale.append(text_scale)
self._text_line_spacing.append(line_spacing)
self._text_anchor_point.append(text_anchor_point)
self._text_is_data.append(bool(is_data))

# pylint: enable=too-many-arguments

Expand Down Expand Up @@ -358,17 +365,21 @@ def fetch(self, refresh_url=None, timeout=10):

# fill out all the text blocks
if self._text:
value_index = 0 # In case values and text is not the same
for i in range(len(self._text)):
if not self._text_is_data[i]:
continue
string = None
if self._text_transform[i]:
func = self._text_transform[i]
string = func(values[i])
string = func(values[value_index])
else:
try:
string = "{:,d}".format(int(values[i]))
string = "{:,d}".format(int(values[value_index]))
except (TypeError, ValueError):
string = values[i] # ok its a string
string = values[value_index] # ok its a string
self.set_text(string, index=i, auto_refresh=False)
value_index += 1
self.refresh()
if len(values) == 1:
return values[0]
Expand Down
9 changes: 7 additions & 2 deletions adafruit_magtag/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ def json_traverse(json, path):
"The json_path parameter should be enclosed in a list or tuple."
)
for x in path:
value = value[x]
try:
value = value[x]
except TypeError as error:
raise ValueError(
"The specified json_path was not found in the results."
) from error
gc.collect()
return value

Expand Down Expand Up @@ -512,7 +517,7 @@ def fetch_data(
raise

# extract desired text/values from json
if json_out and json_path:
if json_out is not None and json_path:
for path in json_path:
try:
values.append(self.json_traverse(json_out, path))
Expand Down