Updated to work with settings.toml and secrets.py

This commit is contained in:
Melissa LeBlanc-Williams 2023-07-26 09:17:00 -07:00
parent 98d1d12b79
commit 243b4e6216

View file

@ -33,15 +33,6 @@ try:
except ImportError: except ImportError:
rtc = None rtc = None
try:
from secrets import secrets
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
in order to use network related features"""
)
__version__ = "0.0.0+auto.0" __version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
@ -69,6 +60,13 @@ CONTENT_TEXT = const(1)
CONTENT_JSON = const(2) CONTENT_JSON = const(2)
CONTENT_IMAGE = const(3) CONTENT_IMAGE = const(3)
OLD_SETTINGS = {
"CIRCUITPY_WIFI_SSID": "ssid",
"CIRCUITPY_WIFI_PASSWORD": "password",
"AIO_USERNAME": "aio_username",
"AIO_KEY": "aio_key",
}
class HttpError(Exception): class HttpError(Exception):
"""HTTP Specific Error""" """HTTP Specific Error"""
@ -106,11 +104,13 @@ class NetworkBase:
"application/geo+json", "application/geo+json",
] ]
self._settings = {}
if secrets_data is not None: if secrets_data is not None:
self._secrets = secrets_data for key, value in secrets_data.items():
else: if key in OLD_SETTINGS:
self._secrets = secrets key = OLD_SETTINGS.get(key)
self._secrets_network = None self._settings[key] = value
self._wifi_credentials = None
self.requests = None self.requests = None
@ -124,6 +124,35 @@ class NetworkBase:
gc.collect() gc.collect()
def _get_setting(self, setting_name, show_error=True):
if setting_name in self._settings:
return self._settings[setting_name]
old_setting_name = setting_name
if setting_name in OLD_SETTINGS:
old_setting_name = OLD_SETTINGS.get(setting_name)
if os.getenv(setting_name) is not None:
return os.getenv(setting_name)
try:
from secrets import secrets # pylint: disable=import-outside-toplevel
except ImportError:
secrets = {}
if old_setting_name in secrets.keys():
self._settings[setting_name] = secrets[old_setting_name]
return self._settings[setting_name]
if show_error:
if setting_name in ("CIRCUITPY_WIFI_SSID", "CIRCUITPY_WIFI_PASSWORD"):
print(
"""WiFi settings are kept in settings.toml, please add them there!
the secrets dictionary must contain 'CIRCUITPY_WIFI_SSID' and 'CIRCUITPY_WIFI_PASSWORD'
at a minimum in order to use network related features"""
)
else:
print(
f"{setting_name} not found. Please add this setting to settings.toml."
)
return None
def neo_status(self, value): def neo_status(self, value):
"""The status NeoPixel. """The status NeoPixel.
@ -186,15 +215,15 @@ class NetworkBase:
api_url = None api_url = None
reply = None reply = None
try: try:
aio_username = self._secrets["aio_username"] aio_username = self._get_setting("AIO_USERNAME")
aio_key = self._secrets["aio_key"] aio_key = self._get_setting("AIO_KEY")
except KeyError: except KeyError:
raise KeyError( raise KeyError(
"\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'" # pylint: disable=line-too-long "\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'AIO_USERNAME' and 'AIO_KEY'" # pylint: disable=line-too-long
) from KeyError ) from KeyError
if location is None: if location is None:
location = self._secrets.get("timezone", location) location = self._get_setting("timezone", False)
if location: if location:
print("Getting time for timezone", location) print("Getting time for timezone", location)
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location) api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
@ -337,23 +366,24 @@ class NetworkBase:
""" """
if not self._secrets_network: if not self._wifi_credentials:
if "networks" in self._secrets: self._wifi_credentials = [
if isinstance(self._secrets["networks"], (list, tuple)): {
self._secrets_network = self._secrets["networks"] "ssid": self._get_setting("CIRCUITPY_WIFI_SSID"),
"password": self._get_setting("CIRCUITPY_WIFI_PASSWORD"),
}
]
networks = self._get_setting("networks", False)
if networks is not None:
if isinstance(networks, (list, tuple)):
self._wifi_credentials = networks
else: else:
raise TypeError( raise TypeError(
"'networks' must be a list/tuple of dicts of 'ssid' and 'password'" "'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
) )
else:
self._secrets_network = [
{
"ssid": self._secrets["ssid"],
"password": self._secrets["password"],
}
]
for secret_entry in self._secrets_network: for secret_entry in self._wifi_credentials:
self._wifi.neo_status(STATUS_CONNECTING) self._wifi.neo_status(STATUS_CONNECTING)
attempt = 1 attempt = 1
@ -365,10 +395,14 @@ class NetworkBase:
or secret_entry["password"] == "CHANGE ME" or secret_entry["password"] == "CHANGE ME"
): ):
change_me = "\n" + "*" * 45 change_me = "\n" + "*" * 45
change_me += "\nPlease update the 'secrets.py' file on your\n" change_me += "\nPlease update the 'settings.toml' file on your\n"
change_me += "CIRCUITPY drive to include your local WiFi\n" change_me += "CIRCUITPY drive to include your local WiFi\n"
change_me += "access point SSID name in 'ssid' and SSID\n" change_me += (
change_me += "password in 'password'. Then save to reload!\n" "access point SSID name in 'CIRCUITPY_WIFI_SSID' and SSID\n"
)
change_me += (
"password in 'CIRCUITPY_WIFI_PASSWORD'. Then save to reload!\n"
)
change_me += "*" * 45 change_me += "*" * 45
raise OSError(change_me) raise OSError(change_me)
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
@ -400,8 +434,8 @@ class NetworkBase:
self.connect() self.connect()
try: try:
aio_username = self._secrets["aio_username"] aio_username = self._get_setting("AIO_USERNAME")
aio_key = self._secrets["aio_key"] aio_key = self._get_setting("AIO_KEY")
except KeyError: except KeyError:
raise KeyError( raise KeyError(
"Adafruit IO secrets are kept in secrets.py, please add them there!\n\n" "Adafruit IO secrets are kept in secrets.py, please add them there!\n\n"