Skip to content

Commit e4245f4

Browse files
committed
Additional linting
1 parent 8044eb0 commit e4245f4

File tree

3 files changed

+71
-67
lines changed

3 files changed

+71
-67
lines changed

adafruit_pyportal/__init__.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,28 @@ def __init__(
134134
):
135135

136136
graphics = Graphics(
137+
default_bg=default_bg,
137138
debug=debug,
138139
)
139140

141+
self._default_bg = default_bg
142+
140143
if external_spi: # If SPI Object Passed
141144
spi = external_spi
142145
else: # Else: Make ESP32 connection
143146
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
144147

148+
if image_json_path or image_url_path:
149+
if self._debug:
150+
print("Init image path")
151+
if not image_position:
152+
image_position = (0, 0) # default to top corner
153+
if not image_resize:
154+
image_resize = (
155+
self.display.width,
156+
self.display.height,
157+
) # default to full screen
158+
145159
network = Network(
146160
status_neopixel=status_neopixel,
147161
esp=esp,
@@ -357,11 +371,17 @@ def fetch(self, refresh_url=None, timeout=10):
357371
except MemoryError:
358372
supervisor.reload()
359373

360-
filename, position = self.network.process_image(
361-
json_out, self.peripherals.sd_check()
362-
)
363-
if filename and position is not None:
364-
self.graphics.set_background(filename, position)
374+
try:
375+
filename, position = self.network.process_image(
376+
json_out, self.peripherals.sd_check()
377+
)
378+
if filename and position is not None:
379+
self.graphics.set_background(filename, position)
380+
except ValueError as error:
381+
print("Error displaying cached image. " + error.args[0])
382+
if self._default_bg is not None:
383+
self.graphics.set_background(self._default_bg)
384+
365385
if content_type == CONTENT_JSON:
366386
values = self.network.process_json(json_out, json_path)
367387
elif content_type == CONTENT_TEXT:

adafruit_pyportal/graphics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class Graphics(GraphicsBase):
4040
"""
4141

4242
# pylint: disable=too-few-public-methods
43-
def __init__(self, *, debug=False):
43+
def __init__(self, *, default_bg=None, debug=False):
4444

45-
super().__init__(board.DISPLAY, default_bg=None, debug=debug)
45+
super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug)
4646
# Tracks whether we've hidden the background when we showed the QR code.
4747
self._qr_only = False
4848

adafruit_pyportal/network.py

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ def __init__(
8989
self._image_resize = image_resize
9090
self._image_position = image_position
9191
self._image_dim_json_path = image_dim_json_path
92-
if image_json_path or image_url_path:
93-
if self._debug:
94-
print("Init image path")
95-
if not self._image_position:
96-
self._image_position = (0, 0) # default to top corner
97-
if not self._image_resize:
98-
self._image_resize = (
99-
self.display.width,
100-
self.display.height,
101-
) # default to full screen
10292

10393
gc.collect()
10494

@@ -160,59 +150,53 @@ def process_image(self, json_data, sd_card=False):
160150
print("image dim:", iwidth, iheight)
161151

162152
if image_url:
163-
try:
164-
print("original URL:", image_url)
165-
if self._convert_image:
166-
if iwidth < iheight:
167-
image_url = self.image_converter_url(
168-
image_url,
169-
int(
170-
self._image_resize[1]
171-
* self._image_resize[1]
172-
/ self._image_resize[0]
173-
),
174-
self._image_resize[1],
175-
)
176-
else:
177-
image_url = self.image_converter_url(
178-
image_url, self._image_resize[0], self._image_resize[1]
179-
)
180-
181-
print("convert URL:", image_url)
182-
# convert image to bitmap and cache
183-
# print("**not actually wgetting**")
184-
filename = "/cache.bmp"
185-
chunk_size = 4096 # default chunk size is 12K (for QSPI)
186-
if sd_card:
187-
filename = "/sd" + filename
188-
chunk_size = 512 # current bug in big SD writes -> stick to 1 block
189-
try:
190-
self.wget(image_url, filename, chunk_size=chunk_size)
191-
except OSError as error:
192-
raise OSError(
193-
"""\n\nNo writable filesystem found for saving datastream. Insert an SD card or set internal filesystem to be unsafe by setting 'disable_concurrent_write_protection' in the mount options in boot.py""" # pylint: disable=line-too-long
194-
) from error
195-
except RuntimeError as error:
196-
raise RuntimeError("wget didn't write a complete file") from error
153+
print("original URL:", image_url)
154+
if self._convert_image:
197155
if iwidth < iheight:
198-
pwidth = int(
199-
self._image_resize[1]
200-
* self._image_resize[1]
201-
/ self._image_resize[0]
202-
)
203-
position = (
204-
self._image_position[0]
205-
+ int((self._image_resize[0] - pwidth) / 2),
206-
self._image_position[1],
156+
image_url = self.image_converter_url(
157+
image_url,
158+
int(
159+
self._image_resize[1]
160+
* self._image_resize[1]
161+
/ self._image_resize[0]
162+
),
163+
self._image_resize[1],
207164
)
208165
else:
209-
position = self._image_position
166+
image_url = self.image_converter_url(
167+
image_url, self._image_resize[0], self._image_resize[1]
168+
)
210169

211-
except ValueError as error:
212-
print("Error displaying cached image. " + error.args[0])
213-
self.set_background(self._default_bg)
214-
finally:
215-
image_url = None
216-
gc.collect()
170+
print("convert URL:", image_url)
171+
# convert image to bitmap and cache
172+
# print("**not actually wgetting**")
173+
filename = "/cache.bmp"
174+
chunk_size = 4096 # default chunk size is 12K (for QSPI)
175+
if sd_card:
176+
filename = "/sd" + filename
177+
chunk_size = 512 # current bug in big SD writes -> stick to 1 block
178+
try:
179+
self.wget(image_url, filename, chunk_size=chunk_size)
180+
except OSError as error:
181+
raise OSError(
182+
"""\n\nNo writable filesystem found for saving datastream. Insert an SD card or set internal filesystem to be unsafe by setting 'disable_concurrent_write_protection' in the mount options in boot.py""" # pylint: disable=line-too-long
183+
) from error
184+
except RuntimeError as error:
185+
raise RuntimeError("wget didn't write a complete file") from error
186+
if iwidth < iheight:
187+
pwidth = int(
188+
self._image_resize[1]
189+
* self._image_resize[1]
190+
/ self._image_resize[0]
191+
)
192+
position = (
193+
self._image_position[0] + int((self._image_resize[0] - pwidth) / 2),
194+
self._image_position[1],
195+
)
196+
else:
197+
position = self._image_position
198+
199+
image_url = None
200+
gc.collect()
217201

218202
return filename, position

0 commit comments

Comments
 (0)