5
5
from typing import (
6
6
Any , Callable , Iterable , Union ,
7
7
List , Tuple , Sequence ,
8
+ Mapping ,
9
+ cast ,
8
10
)
9
11
10
12
import appdirs
@@ -39,7 +41,7 @@ def parse_api_version(value: str) -> Tuple[int, str]:
39
41
40
42
def get_env (key : str , default : Any = _undefined , * ,
41
43
clean : Callable [[str ], Any ] = lambda v : v ):
42
- '''
44
+ """
43
45
Retrieves a configuration value from the environment variables.
44
46
The given *key* is uppercased and prefixed by ``"BACKEND_"`` and then
45
47
``"SORNA_"`` if the former does not exist.
@@ -52,7 +54,7 @@ def get_env(key: str, default: Any = _undefined, *,
52
54
The default is returning the value as-is.
53
55
54
56
:returns: The value processed by the *clean* function.
55
- '''
57
+ """
56
58
key = key .upper ()
57
59
v = os .environ .get ('BACKEND_' + key )
58
60
if v is None :
@@ -73,7 +75,7 @@ def bool_env(v: str) -> bool:
73
75
raise ValueError ('Unrecognized value of boolean environment variable' , v )
74
76
75
77
76
- def _clean_urls (v : str ) -> List [URL ]:
78
+ def _clean_urls (v : Union [ URL , str ] ) -> List [URL ]:
77
79
if isinstance (v , URL ):
78
80
return [v ]
79
81
if isinstance (v , str ):
@@ -95,7 +97,7 @@ def _clean_tokens(v):
95
97
96
98
97
99
class APIConfig :
98
- '''
100
+ """
99
101
Represents a set of API client configurations.
100
102
The access key and secret key are mandatory -- they must be set in either
101
103
environment variables or as the explicit arguments.
@@ -129,9 +131,9 @@ class APIConfig:
129
131
access key) to be automatically mounted upon any
130
132
:func:`Kernel.get_or_create()
131
133
<ai.backend.client.kernel.Kernel.get_or_create>` calls.
132
- '''
134
+ """
133
135
134
- DEFAULTS = {
136
+ DEFAULTS : Mapping [ str , Any ] = {
135
137
'endpoint' : 'https://api.backend.ai' ,
136
138
'endpoint_type' : 'api' ,
137
139
'version' : f'v{ API_VERSION [0 ]} .{ API_VERSION [1 ]} ' ,
@@ -141,9 +143,12 @@ class APIConfig:
141
143
'connection_timeout' : 10.0 ,
142
144
'read_timeout' : None ,
143
145
}
144
- '''
146
+ """
145
147
The default values except the access and secret keys.
146
- '''
148
+ """
149
+
150
+ _group : str
151
+ _hash_type : str
147
152
148
153
def __init__ (self , * ,
149
154
endpoint : Union [URL , str ] = None ,
@@ -159,17 +164,17 @@ def __init__(self, *,
159
164
skip_sslcert_validation : bool = None ,
160
165
connection_timeout : float = None ,
161
166
read_timeout : float = None ) -> None :
162
- from . import get_user_agent # noqa; to avoid circular imports
167
+ from . import get_user_agent
163
168
self ._endpoints = (
164
169
_clean_urls (endpoint ) if endpoint else
165
170
get_env ('ENDPOINT' , self .DEFAULTS ['endpoint' ], clean = _clean_urls ))
166
171
random .shuffle (self ._endpoints )
167
- self ._endpoint_type = endpoint_type if endpoint_type \
172
+ self ._endpoint_type = endpoint_type if endpoint_type is not None \
168
173
else get_env ('ENDPOINT_TYPE' , self .DEFAULTS ['endpoint_type' ])
169
- self ._domain = domain if domain else get_env ('DOMAIN' , self .DEFAULTS ['domain' ])
170
- self ._group = group if group else get_env ('GROUP' , self .DEFAULTS ['group' ])
171
- self ._version = version if version else self .DEFAULTS ['version' ]
172
- self ._user_agent = user_agent if user_agent else get_user_agent ()
174
+ self ._domain = domain if domain is not None else get_env ('DOMAIN' , self .DEFAULTS ['domain' ])
175
+ self ._group = group if group is not None else get_env ('GROUP' , self .DEFAULTS ['group' ])
176
+ self ._version = version if version is not None else self .DEFAULTS ['version' ]
177
+ self ._user_agent = user_agent if user_agent is not None else get_user_agent ()
173
178
if self ._endpoint_type == 'api' :
174
179
self ._access_key = access_key if access_key is not None \
175
180
else get_env ('ACCESS_KEY' , '' )
@@ -178,8 +183,8 @@ def __init__(self, *,
178
183
else :
179
184
self ._access_key = 'dummy'
180
185
self ._secret_key = 'dummy'
181
- self ._hash_type = hash_type .lower () if hash_type else \
182
- self .DEFAULTS ['hash_type' ]
186
+ self ._hash_type = hash_type .lower () if hash_type is not None else \
187
+ cast ( str , self .DEFAULTS ['hash_type' ])
183
188
arg_vfolders = set (vfolder_mounts ) if vfolder_mounts else set ()
184
189
env_vfolders = set (get_env ('VFOLDER_MOUNTS' , [], clean = _clean_tokens ))
185
190
self ._vfolder_mounts = [* (arg_vfolders | env_vfolders )]
@@ -198,16 +203,16 @@ def is_anonymous(self) -> bool:
198
203
199
204
@property
200
205
def endpoint (self ) -> URL :
201
- '''
206
+ """
202
207
The currently active endpoint URL.
203
208
This may change if there are multiple configured endpoints
204
209
and the current one is not accessible.
205
- '''
210
+ """
206
211
return self ._endpoints [0 ]
207
212
208
213
@property
209
214
def endpoints (self ) -> Sequence [URL ]:
210
- ''' All configured endpoint URLs.'''
215
+ """ All configured endpoint URLs."""
211
216
return self ._endpoints
212
217
213
218
def rotate_endpoints (self ):
@@ -217,83 +222,83 @@ def rotate_endpoints(self):
217
222
218
223
@property
219
224
def endpoint_type (self ) -> str :
220
- '''
225
+ """
221
226
The configured endpoint type.
222
- '''
227
+ """
223
228
return self ._endpoint_type
224
229
225
230
@property
226
231
def domain (self ) -> str :
227
- ''' The configured domain.'''
232
+ """ The configured domain."""
228
233
return self ._domain
229
234
230
235
@property
231
236
def group (self ) -> str :
232
- ''' The configured group.'''
237
+ """ The configured group."""
233
238
return self ._group
234
239
235
240
@property
236
241
def user_agent (self ) -> str :
237
- ''' The configured user agent string.'''
242
+ """ The configured user agent string."""
238
243
return self ._user_agent
239
244
240
245
@property
241
246
def access_key (self ) -> str :
242
- ''' The configured API access key.'''
247
+ """ The configured API access key."""
243
248
return self ._access_key
244
249
245
250
@property
246
251
def secret_key (self ) -> str :
247
- ''' The configured API secret key.'''
252
+ """ The configured API secret key."""
248
253
return self ._secret_key
249
254
250
255
@property
251
256
def version (self ) -> str :
252
- ''' The configured API protocol version.'''
257
+ """ The configured API protocol version."""
253
258
return self ._version
254
259
255
260
@property
256
261
def hash_type (self ) -> str :
257
- ''' The configured hash algorithm for API authentication signatures.'''
262
+ """ The configured hash algorithm for API authentication signatures."""
258
263
return self ._hash_type
259
264
260
265
@property
261
- def vfolder_mounts (self ) -> Tuple [str , ... ]:
262
- ''' The configured auto-mounted vfolder list.'''
266
+ def vfolder_mounts (self ) -> Sequence [str ]:
267
+ """ The configured auto-mounted vfolder list."""
263
268
return self ._vfolder_mounts
264
269
265
270
@property
266
271
def skip_sslcert_validation (self ) -> bool :
267
- ''' Whether to skip SSL certificate validation for the API gateway.'''
272
+ """ Whether to skip SSL certificate validation for the API gateway."""
268
273
return self ._skip_sslcert_validation
269
274
270
275
@property
271
276
def connection_timeout (self ) -> float :
272
- ''' The maximum allowed duration for making TCP connections to the server.'''
277
+ """ The maximum allowed duration for making TCP connections to the server."""
273
278
return self ._connection_timeout
274
279
275
280
@property
276
281
def read_timeout (self ) -> float :
277
- ''' The maximum allowed waiting time for the first byte of the response from the server.'''
282
+ """ The maximum allowed waiting time for the first byte of the response from the server."""
278
283
return self ._read_timeout
279
284
280
285
281
286
def get_config ():
282
- '''
287
+ """
283
288
Returns the configuration for the current process.
284
289
If there is no explicitly set :class:`APIConfig` instance,
285
290
it will generate a new one from the current environment variables
286
291
and defaults.
287
- '''
292
+ """
288
293
global _config
289
294
if _config is None :
290
295
_config = APIConfig ()
291
296
return _config
292
297
293
298
294
299
def set_config (conf : APIConfig ):
295
- '''
300
+ """
296
301
Sets the configuration used throughout the current process.
297
- '''
302
+ """
298
303
global _config
299
304
_config = conf
0 commit comments