From 98ae744e7087244a5cb9dc3bcba7ee30ccf699cd Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 18 Apr 2022 16:34:06 -0500 Subject: [PATCH 1/3] add checks for network none and raise error if user attempting to use network features --- adafruit_portalbase/__init__.py | 50 ++++++++++++++++++++++++++++----- adafruit_portalbase/network.py | 3 +- 2 files changed, 45 insertions(+), 8 deletions(-) mode change 100755 => 100644 adafruit_portalbase/__init__.py diff --git a/adafruit_portalbase/__init__.py b/adafruit_portalbase/__init__.py old mode 100755 new mode 100644 index 6db809b..cb43b00 --- a/adafruit_portalbase/__init__.py +++ b/adafruit_portalbase/__init__.py @@ -85,17 +85,32 @@ def __init__( except ImportError: self._alarm = None self._debug = debug + if url and self.network is None: + raise RuntimeError("network must not be None to get data from a url") self.url = url + if headers and self.network is None: + raise RuntimeError("network must not be None to send headers") self._headers = headers + if json_path and self.network is None: + raise RuntimeError("network must not be None to use json_path") self._json_path = None self.json_path = json_path + if regexp_path and self.network is None: + raise RuntimeError("network must not be None to use regexp_path") self._regexp_path = regexp_path + + if success_callback and self.network is None: + raise RuntimeError("network must not be None to use success_callback") self._success_callback = success_callback # Add any JSON translators + if json_transform: - self.network.add_json_transform(json_transform) + if self.network is not None: + self.network.add_json_transform(json_transform) + else: + raise RuntimeError("network must not be None to use json_transform.") def _load_font(self, font): """ @@ -416,6 +431,9 @@ def fetch(self, refresh_url=None, timeout=10): :param int timeout: The timeout period in seconds. """ + + if self.network is None: + raise RuntimeError("network must not be None to use fetch()") if refresh_url: self.url = refresh_url values = [] @@ -459,7 +477,10 @@ def _fill_text_labels(self, values): def get_local_time(self, location=None): """Accessor function for get_local_time()""" - return self.network.get_local_time(location=location) + if self.network is not None: + return self.network.get_local_time(location=location) + + raise RuntimeError("network must not be None to use get_local_time()") def push_to_io(self, feed_key, data, metadata=None, precision=None): """Push data to an adafruit.io feed @@ -470,8 +491,12 @@ def push_to_io(self, feed_key, data, metadata=None, precision=None): :param int precision: Optional amount of precision points to send with floating point data """ - - self.network.push_to_io(feed_key, data, metadata=metadata, precision=precision) + if self.network is not None: + self.network.push_to_io( + feed_key, data, metadata=metadata, precision=precision + ) + else: + raise RuntimeError("network must not be None to use push_to_io()") def get_io_data(self, feed_key): """Return all values from the Adafruit IO Feed Data that matches the feed key @@ -479,8 +504,10 @@ def get_io_data(self, feed_key): :param str feed_key: Name of feed key to receive data from. """ + if self.network is not None: + return self.network.get_io_data(feed_key) - return self.network.get_io_data(feed_key) + raise RuntimeError("network must not be None to use get_io_data()") def get_io_feed(self, feed_key, detailed=False): """Return the Adafruit IO Feed that matches the feed key @@ -489,7 +516,10 @@ def get_io_feed(self, feed_key, detailed=False): :param bool detailed: Whether to return additional detailed information """ - return self.network.get_io_feed(feed_key, detailed) + if self.network is not None: + return self.network.get_io_feed(feed_key, detailed) + + raise RuntimeError("network must not be None to use get_io_feed()") def get_io_group(self, group_key): """Return the Adafruit IO Group that matches the group key @@ -497,7 +527,10 @@ def get_io_group(self, group_key): :param str group_key: Name of group key to match. """ - return self.network.get_io_group(group_key) + if self.network is not None: + return self.network.get_io_group(group_key) + + raise RuntimeError("network must not be None to use get_io_group()") @property def json_path(self): @@ -509,6 +542,9 @@ def json_path(self): @json_path.setter def json_path(self, value): + if value is not None and self.network is None: + raise RuntimeError("network must not be None to use json_path.") + if value: if isinstance(value[0], (list, tuple)): self._json_path = value diff --git a/adafruit_portalbase/network.py b/adafruit_portalbase/network.py index d06a3ab..1196211 100644 --- a/adafruit_portalbase/network.py +++ b/adafruit_portalbase/network.py @@ -38,7 +38,8 @@ except ImportError: print( """WiFi settings are kept in secrets.py, please add them there! -the secrets dictionary must contain 'ssid' and 'password' at a minimum""" +the secrets dictionary must contain 'ssid' and 'password' at a minimum +in order to use network related features""" ) __version__ = "0.0.0-auto.0" From c4264077c953a49d6ea968af5e5e1ed07432be92 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 18 Apr 2022 16:38:55 -0500 Subject: [PATCH 2/3] swap raise and return --- adafruit_portalbase/__init__.py | 35 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/adafruit_portalbase/__init__.py b/adafruit_portalbase/__init__.py index cb43b00..5718513 100644 --- a/adafruit_portalbase/__init__.py +++ b/adafruit_portalbase/__init__.py @@ -477,10 +477,10 @@ def _fill_text_labels(self, values): def get_local_time(self, location=None): """Accessor function for get_local_time()""" - if self.network is not None: - return self.network.get_local_time(location=location) + if self.network is None: + raise RuntimeError("network must not be None to use get_local_time()") - raise RuntimeError("network must not be None to use get_local_time()") + return self.network.get_local_time(location=location) def push_to_io(self, feed_key, data, metadata=None, precision=None): """Push data to an adafruit.io feed @@ -491,23 +491,23 @@ def push_to_io(self, feed_key, data, metadata=None, precision=None): :param int precision: Optional amount of precision points to send with floating point data """ - if self.network is not None: - self.network.push_to_io( - feed_key, data, metadata=metadata, precision=precision - ) - else: + if self.network is None: raise RuntimeError("network must not be None to use push_to_io()") + self.network.push_to_io( + feed_key, data, metadata=metadata, precision=precision + ) + def get_io_data(self, feed_key): """Return all values from the Adafruit IO Feed Data that matches the feed key :param str feed_key: Name of feed key to receive data from. """ - if self.network is not None: - return self.network.get_io_data(feed_key) + if self.network is None: + raise RuntimeError("network must not be None to use get_io_data()") - raise RuntimeError("network must not be None to use get_io_data()") + return self.network.get_io_data(feed_key) def get_io_feed(self, feed_key, detailed=False): """Return the Adafruit IO Feed that matches the feed key @@ -516,10 +516,10 @@ def get_io_feed(self, feed_key, detailed=False): :param bool detailed: Whether to return additional detailed information """ - if self.network is not None: - return self.network.get_io_feed(feed_key, detailed) + if self.network is None: + raise RuntimeError("network must not be None to use get_io_feed()") - raise RuntimeError("network must not be None to use get_io_feed()") + return self.network.get_io_feed(feed_key, detailed) def get_io_group(self, group_key): """Return the Adafruit IO Group that matches the group key @@ -527,10 +527,9 @@ def get_io_group(self, group_key): :param str group_key: Name of group key to match. """ - if self.network is not None: - return self.network.get_io_group(group_key) - - raise RuntimeError("network must not be None to use get_io_group()") + if self.network is None: + raise RuntimeError("network must not be None to use get_io_group()") + return self.network.get_io_group(group_key) @property def json_path(self): From 4656a446b067adce068e4d1ff154cbf83ec8e553 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 18 Apr 2022 16:50:38 -0500 Subject: [PATCH 3/3] code format --- adafruit_portalbase/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_portalbase/__init__.py b/adafruit_portalbase/__init__.py index 5718513..6b01ab7 100644 --- a/adafruit_portalbase/__init__.py +++ b/adafruit_portalbase/__init__.py @@ -494,9 +494,7 @@ def push_to_io(self, feed_key, data, metadata=None, precision=None): if self.network is None: raise RuntimeError("network must not be None to use push_to_io()") - self.network.push_to_io( - feed_key, data, metadata=metadata, precision=precision - ) + self.network.push_to_io(feed_key, data, metadata=metadata, precision=precision) def get_io_data(self, feed_key): """Return all values from the Adafruit IO Feed Data that matches the feed key