3333except ImportError :
3434 rtc = None
3535
36- try :
37- from secrets import secrets
38- except ImportError :
39- print (
40- """WiFi settings are kept in secrets.py, please add them there!
41- the secrets dictionary must contain 'ssid' and 'password' at a minimum
42- in order to use network related features"""
43- )
44-
4536__version__ = "0.0.0+auto.0"
4637__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
4738
6960CONTENT_JSON = const (2 )
7061CONTENT_IMAGE = const (3 )
7162
63+ OLD_SETTINGS = {
64+ "CIRCUITPY_WIFI_SSID" : "ssid" ,
65+ "CIRCUITPY_WIFI_PASSWORD" : "password" ,
66+ "AIO_USERNAME" : "aio_username" ,
67+ "AIO_KEY" : "aio_key" ,
68+ }
69+
7270
7371class HttpError (Exception ):
7472 """HTTP Specific Error"""
@@ -106,11 +104,13 @@ def __init__(
106104 "application/geo+json" ,
107105 ]
108106
107+ self ._settings = {}
109108 if secrets_data is not None :
110- self ._secrets = secrets_data
111- else :
112- self ._secrets = secrets
113- self ._secrets_network = None
109+ for key , value in secrets_data .items ():
110+ if key in OLD_SETTINGS :
111+ key = OLD_SETTINGS .get (key )
112+ self ._settings [key ] = value
113+ self ._wifi_credentials = None
114114
115115 self .requests = None
116116
@@ -124,6 +124,35 @@ def __init__(
124124
125125 gc .collect ()
126126
127+ def _get_setting (self , setting_name , show_error = True ):
128+ if setting_name in self ._settings :
129+ return self ._settings [setting_name ]
130+
131+ old_setting_name = setting_name
132+ if setting_name in OLD_SETTINGS :
133+ old_setting_name = OLD_SETTINGS .get (setting_name )
134+ if os .getenv (setting_name ) is not None :
135+ return os .getenv (setting_name )
136+ try :
137+ from secrets import secrets # pylint: disable=import-outside-toplevel
138+ except ImportError :
139+ secrets = {}
140+ if old_setting_name in secrets .keys ():
141+ self ._settings [setting_name ] = secrets [old_setting_name ]
142+ return self ._settings [setting_name ]
143+ if show_error :
144+ if setting_name in ("CIRCUITPY_WIFI_SSID" , "CIRCUITPY_WIFI_PASSWORD" ):
145+ print (
146+ """WiFi settings are kept in settings.toml, please add them there!
147+ the secrets dictionary must contain 'CIRCUITPY_WIFI_SSID' and 'CIRCUITPY_WIFI_PASSWORD'
148+ at a minimum in order to use network related features"""
149+ )
150+ else :
151+ print (
152+ f"{ setting_name } not found. Please add this setting to settings.toml."
153+ )
154+ return None
155+
127156 def neo_status (self , value ):
128157 """The status NeoPixel.
129158
@@ -186,15 +215,15 @@ def get_strftime(self, time_format, location=None):
186215 api_url = None
187216 reply = None
188217 try :
189- aio_username = self ._secrets [ "aio_username" ]
190- aio_key = self ._secrets [ "aio_key" ]
218+ aio_username = self ._get_setting ( "AIO_USERNAME" )
219+ aio_key = self ._get_setting ( "AIO_KEY" )
191220 except KeyError :
192221 raise KeyError (
193- "\n \n Our 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
222+ "\n \n Our 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
194223 ) from KeyError
195224
196225 if location is None :
197- location = self ._secrets . get ("timezone" , location )
226+ location = self ._get_setting ("timezone" , False )
198227 if location :
199228 print ("Getting time for timezone" , location )
200229 api_url = (TIME_SERVICE + "&tz=%s" ) % (aio_username , aio_key , location )
@@ -337,23 +366,24 @@ def connect(self, max_attempts=10):
337366
338367 """
339368
340- if not self ._secrets_network :
341- if "networks" in self ._secrets :
342- if isinstance (self ._secrets ["networks" ], (list , tuple )):
343- self ._secrets_network = self ._secrets ["networks" ]
369+ if not self ._wifi_credentials :
370+ self ._wifi_credentials = [
371+ {
372+ "ssid" : self ._get_setting ("CIRCUITPY_WIFI_SSID" ),
373+ "password" : self ._get_setting ("CIRCUITPY_WIFI_PASSWORD" ),
374+ }
375+ ]
376+
377+ networks = self ._get_setting ("networks" , False )
378+ if networks is not None :
379+ if isinstance (networks , (list , tuple )):
380+ self ._wifi_credentials = networks
344381 else :
345382 raise TypeError (
346383 "'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
347384 )
348- else :
349- self ._secrets_network = [
350- {
351- "ssid" : self ._secrets ["ssid" ],
352- "password" : self ._secrets ["password" ],
353- }
354- ]
355-
356- for secret_entry in self ._secrets_network :
385+
386+ for secret_entry in self ._wifi_credentials :
357387 self ._wifi .neo_status (STATUS_CONNECTING )
358388 attempt = 1
359389
@@ -365,10 +395,14 @@ def connect(self, max_attempts=10):
365395 or secret_entry ["password" ] == "CHANGE ME"
366396 ):
367397 change_me = "\n " + "*" * 45
368- change_me += "\n Please update the 'secrets.py ' file on your\n "
398+ change_me += "\n Please update the 'settings.toml ' file on your\n "
369399 change_me += "CIRCUITPY drive to include your local WiFi\n "
370- change_me += "access point SSID name in 'ssid' and SSID\n "
371- change_me += "password in 'password'. Then save to reload!\n "
400+ change_me += (
401+ "access point SSID name in 'CIRCUITPY_WIFI_SSID' and SSID\n "
402+ )
403+ change_me += (
404+ "password in 'CIRCUITPY_WIFI_PASSWORD'. Then save to reload!\n "
405+ )
372406 change_me += "*" * 45
373407 raise OSError (change_me )
374408 self ._wifi .neo_status (STATUS_NO_CONNECTION ) # red = not connected
@@ -400,8 +434,8 @@ def _get_io_client(self):
400434 self .connect ()
401435
402436 try :
403- aio_username = self ._secrets [ "aio_username" ]
404- aio_key = self ._secrets [ "aio_key" ]
437+ aio_username = self ._get_setting ( "AIO_USERNAME" )
438+ aio_key = self ._get_setting ( "AIO_KEY" )
405439 except KeyError :
406440 raise KeyError (
407441 "Adafruit IO secrets are kept in secrets.py, please add them there!\n \n "
0 commit comments