24
24
import gc
25
25
import os
26
26
import time
27
+ import warnings
27
28
28
29
from adafruit_fakerequests import Fake_Requests
29
30
from adafruit_io .adafruit_io import IO_HTTP , AdafruitIO_RequestError
57
58
CONTENT_JSON = const (2 )
58
59
CONTENT_IMAGE = const (3 )
59
60
60
- OLD_SETTINGS = {
61
+ OLD_SECRETS = {
62
+ "ADAFRUIT_AIO_KEY" : "aio_key" ,
63
+ "ADAFRUIT_AIO_USERNAME" : "aio_username" ,
64
+ "AIO_KEY" : "aio_key" ,
65
+ "AIO_USERNAME" : "aio_username" ,
61
66
"CIRCUITPY_WIFI_SSID" : "ssid" ,
62
67
"CIRCUITPY_WIFI_PASSWORD" : "password" ,
63
- "AIO_USERNAME" : "aio_username" ,
64
- "AIO_KEY" : "aio_key" ,
68
+ }
69
+
70
+ OLD_SETTINGS = {
71
+ "ADAFRUIT_AIO_KEY" : "AIO_KEY" ,
72
+ "ADAFRUIT_AIO_USERNAME" : "AIO_USERNAME" ,
65
73
}
66
74
67
75
@@ -83,12 +91,11 @@ class NetworkBase:
83
91
:param bool extract_values: If true, single-length fetched values are automatically extracted
84
92
from lists and tuples. Defaults to ``True``.
85
93
:param debug: Turn on debug print outs. Defaults to False.
86
- :param list secrets_data: An optional list in place of the data contained in the secrets.py file
87
94
88
95
"""
89
96
90
97
def __init__ ( # noqa: PLR0912,PLR0913 Too many branches,Too many arguments in function definition
91
- self , wifi_module , * , extract_values = True , debug = False , secrets_data = None
98
+ self , wifi_module , * , extract_values = True , debug = False
92
99
):
93
100
self ._wifi = wifi_module
94
101
self ._debug = debug
@@ -101,11 +108,6 @@ def __init__( # noqa: PLR0912,PLR0913 Too many branches,Too many arguments in f
101
108
]
102
109
103
110
self ._settings = {}
104
- if secrets_data is not None :
105
- for key , value in secrets_data .items ():
106
- if key in OLD_SETTINGS :
107
- key = OLD_SETTINGS .get (key ) # noqa: PLW2901 `for` loop variable `value` overwritten by assignment target
108
- self ._settings [key ] = value
109
111
self ._wifi_credentials = None
110
112
111
113
self .requests = None
@@ -120,31 +122,44 @@ def __init__( # noqa: PLR0912,PLR0913 Too many branches,Too many arguments in f
120
122
121
123
gc .collect ()
122
124
123
- def _get_setting (self , setting_name , show_error = True ):
125
+ def _get_setting (self , setting_name ):
126
+ # if setting is has already been found, return it
124
127
if setting_name in self ._settings :
125
128
return self ._settings [setting_name ]
126
129
127
- old_setting_name = setting_name
130
+ # if setting is in settings.toml return it
131
+ env_value = os .getenv (setting_name )
132
+ if env_value is not None :
133
+ self ._settings [setting_name ] = env_value
134
+ return env_value
135
+
136
+ # if setting old name is in settings.toml return it
128
137
if setting_name in OLD_SETTINGS :
129
138
old_setting_name = OLD_SETTINGS .get (setting_name )
130
- if os .getenv (setting_name ) is not None :
131
- return os .getenv (setting_name )
139
+ env_value = os .getenv (old_setting_name )
140
+ if env_value is not None :
141
+ self ._settings [setting_name ] = env_value
142
+ return env_value
143
+
132
144
try :
133
145
from secrets import secrets
134
146
except ImportError :
135
- secrets = {}
136
- if old_setting_name in secrets .keys ():
137
- self ._settings [setting_name ] = secrets [old_setting_name ]
138
- return self ._settings [setting_name ]
139
- if show_error :
140
- if setting_name in ("CIRCUITPY_WIFI_SSID" , "CIRCUITPY_WIFI_PASSWORD" ):
141
- print (
142
- """WiFi settings are kept in settings.toml, please add them there!
143
- the secrets dictionary must contain 'CIRCUITPY_WIFI_SSID' and 'CIRCUITPY_WIFI_PASSWORD'
144
- at a minimum in order to use network related features"""
145
- )
146
- else :
147
- print (f"{ setting_name } not found. Please add this setting to settings.toml." )
147
+ return None
148
+
149
+ # if setting is in legacy secrets.py return it
150
+ secrets_setting_name = setting_name
151
+ if setting_name in OLD_SECRETS :
152
+ # translate common names
153
+ secrets_setting_name = OLD_SECRETS .get (setting_name )
154
+ env_value = secrets .get (secrets_setting_name )
155
+ if env_value is not None :
156
+ warnings .warn (
157
+ "The using of `secrets`, is deprecated. Please put your settings in "
158
+ "settings.toml"
159
+ )
160
+ self ._settings [setting_name ] = env_value
161
+ return env_value
162
+
148
163
return None
149
164
150
165
def neo_status (self , value ):
@@ -206,17 +221,17 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
206
221
api_url = None
207
222
reply = None
208
223
try :
209
- aio_username = self ._get_setting ("AIO_USERNAME " )
210
- aio_key = self ._get_setting ("AIO_KEY " )
224
+ aio_username = self ._get_setting ("ADAFRUIT_AIO_USERNAME " )
225
+ aio_key = self ._get_setting ("ADAFRUIT_AIO_KEY " )
211
226
except KeyError :
212
227
raise KeyError (
213
228
"\n \n Our time service requires a login/password to rate-limit. "
214
- "Please register for a free adafruit.io account and place the user/key "
215
- "in your secrets file under 'AIO_USERNAME ' and 'AIO_KEY '"
229
+ "Please register for a free adafruit.io account and place the user/key in "
230
+ "your settings.toml file under 'ADAFRUIT_AIO_USERNAME ' and 'ADAFRUIT_AIO_KEY '"
216
231
) from KeyError
217
232
218
233
if location is None :
219
- location = self ._get_setting ("timezone" , False )
234
+ location = self ._get_setting ("timezone" )
220
235
if location :
221
236
print ("Getting time for timezone" , location )
222
237
api_url = (TIME_SERVICE + "&tz=%s" ) % (aio_username , aio_key , location )
@@ -242,7 +257,8 @@ def get_strftime(self, time_format, location=None, max_attempts=10):
242
257
reply = response .text
243
258
except KeyError :
244
259
raise KeyError (
245
- "Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones"
260
+ "Was unable to lookup the time, try setting 'timezone' in your settings.toml"
261
+ "according to http://worldtimeapi.org/timezones"
246
262
) from KeyError
247
263
# now clean up
248
264
response .close ()
@@ -346,7 +362,7 @@ def wget(self, url, filename, *, chunk_size=12000, headers=None):
346
362
347
363
def connect (self , max_attempts = 10 ):
348
364
"""
349
- Connect to WiFi using the settings found in secrets.py
365
+ Connect to WiFi using the settings found in settings.toml
350
366
351
367
:param max_attempts: The maximum number of attempts to connect to WiFi before
352
368
failing or use None to disable. Defaults to 10.
@@ -361,7 +377,7 @@ def connect(self, max_attempts=10):
361
377
}
362
378
]
363
379
364
- networks = self ._get_setting ("networks" , False )
380
+ networks = self ._get_setting ("networks" )
365
381
if networks is not None :
366
382
if isinstance (networks , (list , tuple )):
367
383
self ._wifi_credentials = networks
@@ -370,14 +386,14 @@ def connect(self, max_attempts=10):
370
386
"'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
371
387
)
372
388
373
- for secret_entry in self ._wifi_credentials :
389
+ for credentials in self ._wifi_credentials :
374
390
self ._wifi .neo_status (STATUS_CONNECTING )
375
391
attempt = 1
376
392
377
393
while not self ._wifi .is_connected :
378
- # secrets dictionary must contain 'ssid ' and 'password' at a minimum
379
- print ("Connecting to AP" , secret_entry ["ssid" ])
380
- if secret_entry ["ssid" ] == "CHANGE ME" or secret_entry ["password" ] == "CHANGE ME" :
394
+ # credentials must contain 'CIRCUITPY_WIFI_SSID ' and 'CIRCUITPY_WIFI_PASSWORD'
395
+ print ("Connecting to AP" , credentials ["ssid" ])
396
+ if credentials ["ssid" ] == "CHANGE ME" or credentials ["password" ] == "CHANGE ME" :
381
397
change_me = "\n " + "*" * 45
382
398
change_me += "\n Please update the 'settings.toml' file on your\n "
383
399
change_me += "CIRCUITPY drive to include your local WiFi\n "
@@ -387,7 +403,7 @@ def connect(self, max_attempts=10):
387
403
raise OSError (change_me )
388
404
self ._wifi .neo_status (STATUS_NO_CONNECTION ) # red = not connected
389
405
try :
390
- self ._wifi .connect (secret_entry ["ssid" ], secret_entry ["password" ])
406
+ self ._wifi .connect (credentials ["ssid" ], credentials ["password" ])
391
407
self .requests = self ._wifi .requests
392
408
self ._wifi .neo_status (STATUS_CONNECTED )
393
409
break
@@ -412,11 +428,11 @@ def _get_io_client(self):
412
428
self .connect ()
413
429
414
430
try :
415
- aio_username = self ._get_setting ("AIO_USERNAME " )
416
- aio_key = self ._get_setting ("AIO_KEY " )
431
+ aio_username = self ._get_setting ("ADAFRUIT_AIO_USERNAME " )
432
+ aio_key = self ._get_setting ("ADAFRUIT_AIO_KEY " )
417
433
except KeyError :
418
434
raise KeyError (
419
- "Adafruit IO secrets are kept in secrets.py , please add them there!\n \n "
435
+ "Adafruit IO settings are kept in settings.toml , please add them there!\n \n "
420
436
) from KeyError
421
437
422
438
self ._io_client = IO_HTTP (aio_username , aio_key , self ._wifi .requests )
0 commit comments