11
11
# See the License for the specific language governing permissions and
12
12
# limitations under the License.
13
13
14
- import abc
14
+ from abc import ABC , abstractmethod
15
15
import numbers
16
16
import requests
17
17
import threading
28
28
from .helpers import validator
29
29
from .optimizely_config import OptimizelyConfigService
30
30
31
- ABC = abc .ABCMeta ('ABC' , (object ,), {'__slots__' : ()})
32
-
33
31
34
32
class BaseConfigManager (ABC ):
35
33
""" Base class for Optimizely's config manager. """
@@ -62,7 +60,7 @@ def _validate_instantiation_options(self):
62
60
if not validator .is_notification_center_valid (self .notification_center ):
63
61
raise optimizely_exceptions .InvalidInputException (enums .Errors .INVALID_INPUT .format ('notification_center' ))
64
62
65
- @abc . abstractmethod
63
+ @abstractmethod
66
64
def get_config (self ):
67
65
""" Get config for use by optimizely.Optimizely.
68
66
The config should be an instance of project_config.ProjectConfig."""
@@ -86,7 +84,7 @@ def __init__(
86
84
validation upon object invocation. By default
87
85
JSON schema validation will be performed.
88
86
"""
89
- super (StaticConfigManager , self ).__init__ (
87
+ super ().__init__ (
90
88
logger = logger , error_handler = error_handler , notification_center = notification_center ,
91
89
)
92
90
self ._config = None
@@ -134,7 +132,7 @@ def _set_config(self, datafile):
134
132
self .notification_center .send_notifications (enums .NotificationTypes .OPTIMIZELY_CONFIG_UPDATE )
135
133
self .logger .debug (
136
134
'Received new datafile and updated config. '
137
- 'Old revision number: {}. New revision number: {}.' . format ( previous_revision , config .get_revision ())
135
+ f 'Old revision number: { previous_revision } . New revision number: { config .get_revision ()} .'
138
136
)
139
137
140
138
def get_config (self ):
@@ -186,7 +184,7 @@ def __init__(
186
184
187
185
"""
188
186
self ._config_ready_event = threading .Event ()
189
- super (PollingConfigManager , self ).__init__ (
187
+ super ().__init__ (
190
188
datafile = datafile ,
191
189
logger = logger ,
192
190
error_handler = error_handler ,
@@ -200,7 +198,7 @@ def __init__(
200
198
self .set_blocking_timeout (blocking_timeout )
201
199
self .last_modified = None
202
200
self ._polling_thread = threading .Thread (target = self ._run )
203
- self ._polling_thread .setDaemon ( True )
201
+ self ._polling_thread .daemon = True
204
202
self ._polling_thread .start ()
205
203
206
204
@staticmethod
@@ -231,7 +229,7 @@ def get_datafile_url(sdk_key, url, url_template):
231
229
return url_template .format (sdk_key = sdk_key )
232
230
except (AttributeError , KeyError ):
233
231
raise optimizely_exceptions .InvalidInputException (
234
- 'Invalid url_template {} provided.' . format ( url_template )
232
+ f 'Invalid url_template { url_template } provided.'
235
233
)
236
234
237
235
return url
@@ -243,7 +241,7 @@ def _set_config(self, datafile):
243
241
datafile: JSON string representing the Optimizely project.
244
242
"""
245
243
if datafile or self ._config_ready_event .is_set ():
246
- super (PollingConfigManager , self )._set_config (datafile = datafile )
244
+ super ()._set_config (datafile = datafile )
247
245
self ._config_ready_event .set ()
248
246
249
247
def get_config (self ):
@@ -265,19 +263,18 @@ def set_update_interval(self, update_interval):
265
263
"""
266
264
if update_interval is None :
267
265
update_interval = enums .ConfigManager .DEFAULT_UPDATE_INTERVAL
268
- self .logger .debug ('Setting config update interval to default value {}.' . format ( update_interval ) )
266
+ self .logger .debug (f 'Setting config update interval to default value { update_interval } .' )
269
267
270
268
if not isinstance (update_interval , (int , float )):
271
269
raise optimizely_exceptions .InvalidInputException (
272
- 'Invalid update_interval "{}" provided.' . format ( update_interval )
270
+ f 'Invalid update_interval "{ update_interval } " provided.'
273
271
)
274
272
275
273
# If polling interval is less than or equal to 0 then set it to default update interval.
276
274
if update_interval <= 0 :
277
275
self .logger .debug (
278
- 'update_interval value {} too small. Defaulting to {}' .format (
279
- update_interval , enums .ConfigManager .DEFAULT_UPDATE_INTERVAL
280
- )
276
+ f'update_interval value { update_interval } too small. '
277
+ f'Defaulting to { enums .ConfigManager .DEFAULT_UPDATE_INTERVAL } '
281
278
)
282
279
update_interval = enums .ConfigManager .DEFAULT_UPDATE_INTERVAL
283
280
@@ -291,19 +288,18 @@ def set_blocking_timeout(self, blocking_timeout):
291
288
"""
292
289
if blocking_timeout is None :
293
290
blocking_timeout = enums .ConfigManager .DEFAULT_BLOCKING_TIMEOUT
294
- self .logger .debug ('Setting config blocking timeout to default value {}.' . format ( blocking_timeout ) )
291
+ self .logger .debug (f 'Setting config blocking timeout to default value { blocking_timeout } .' )
295
292
296
293
if not isinstance (blocking_timeout , (numbers .Integral , float )):
297
294
raise optimizely_exceptions .InvalidInputException (
298
- 'Invalid blocking timeout "{}" provided.' . format ( blocking_timeout )
295
+ f 'Invalid blocking timeout "{ blocking_timeout } " provided.'
299
296
)
300
297
301
298
# If blocking timeout is less than 0 then set it to default blocking timeout.
302
299
if blocking_timeout < 0 :
303
300
self .logger .debug (
304
- 'blocking timeout value {} too small. Defaulting to {}' .format (
305
- blocking_timeout , enums .ConfigManager .DEFAULT_BLOCKING_TIMEOUT
306
- )
301
+ f'blocking timeout value { blocking_timeout } too small. '
302
+ f'Defaulting to { enums .ConfigManager .DEFAULT_BLOCKING_TIMEOUT } '
307
303
)
308
304
blocking_timeout = enums .ConfigManager .DEFAULT_BLOCKING_TIMEOUT
309
305
@@ -326,12 +322,12 @@ def _handle_response(self, response):
326
322
try :
327
323
response .raise_for_status ()
328
324
except requests_exceptions .RequestException as err :
329
- self .logger .error ('Fetching datafile from {} failed. Error: {}' . format ( self . datafile_url , str ( err )) )
325
+ self .logger .error (f 'Fetching datafile from { self . datafile_url } failed. Error: { err } ' )
330
326
return
331
327
332
328
# Leave datafile and config unchanged if it has not been modified.
333
329
if response .status_code == http_status_codes .not_modified :
334
- self .logger .debug ('Not updating config as datafile has not updated since {}.' . format ( self .last_modified ) )
330
+ self .logger .debug (f 'Not updating config as datafile has not updated since { self .last_modified } .' )
335
331
return
336
332
337
333
self .set_last_modified (response .headers )
@@ -349,7 +345,7 @@ def fetch_datafile(self):
349
345
self .datafile_url , headers = request_headers , timeout = enums .ConfigManager .REQUEST_TIMEOUT ,
350
346
)
351
347
except requests_exceptions .RequestException as err :
352
- self .logger .error ('Fetching datafile from {} failed. Error: {}' . format ( self . datafile_url , str ( err )) )
348
+ self .logger .error (f 'Fetching datafile from { self . datafile_url } failed. Error: { err } ' )
353
349
return
354
350
355
351
self ._handle_response (response )
@@ -367,7 +363,7 @@ def _run(self):
367
363
time .sleep (self .update_interval )
368
364
except (OSError , OverflowError ) as err :
369
365
self .logger .error (
370
- 'Error in time.sleep. ' ' Provided update_interval value may be too big. Error: {}' . format ( str ( err ))
366
+ f 'Error in time.sleep. Provided update_interval value may be too big. Error: { err } '
371
367
)
372
368
raise
373
369
@@ -396,7 +392,7 @@ def __init__(
396
392
**kwargs: Refer to keyword arguments descriptions in PollingConfigManager.
397
393
"""
398
394
self ._set_datafile_access_token (datafile_access_token )
399
- super (AuthDatafilePollingConfigManager , self ).__init__ (* args , ** kwargs )
395
+ super ().__init__ (* args , ** kwargs )
400
396
401
397
def _set_datafile_access_token (self , datafile_access_token ):
402
398
""" Checks for valid access token input and sets it. """
@@ -421,7 +417,7 @@ def fetch_datafile(self):
421
417
self .datafile_url , headers = request_headers , timeout = enums .ConfigManager .REQUEST_TIMEOUT ,
422
418
)
423
419
except requests_exceptions .RequestException as err :
424
- self .logger .error ('Fetching datafile from {} failed. Error: {}' . format ( self . datafile_url , str ( err )) )
420
+ self .logger .error (f 'Fetching datafile from { self . datafile_url } failed. Error: { err } ' )
425
421
return
426
422
427
423
self ._handle_response (response )
0 commit comments