22
22
23
23
import dateutil .parser as date_parser
24
24
25
+
25
26
def has_bad_first_or_last_char (val : str ) -> bool :
26
27
"""Returns true if a string starts with any of: {," ; or ends with any of: },".
27
28
@@ -31,7 +32,9 @@ def has_bad_first_or_last_char(val: str) -> bool:
31
32
Returns:
32
33
Whether or not the string starts or ends with bad characters.
33
34
"""
34
- return val is not None and (val .startswith ('{' ) or val .startswith ('"' ) or val .endswith ('}' ) or val .endswith ('"' ))
35
+ return val is not None and (val .startswith ('{' ) or val .startswith ('"' )
36
+ or val .endswith ('}' ) or val .endswith ('"' ))
37
+
35
38
36
39
def remove_null_values (dictionary : dict ) -> dict :
37
40
"""Create a new dictionary without keys mapped to null values.
@@ -46,6 +49,7 @@ def remove_null_values(dictionary: dict) -> dict:
46
49
return {k : v for (k , v ) in dictionary .items () if v is not None }
47
50
return dictionary
48
51
52
+
49
53
def cleanup_values (dictionary : dict ) -> dict :
50
54
"""Create a new dictionary with boolean values converted to strings.
51
55
@@ -62,18 +66,21 @@ def cleanup_values(dictionary: dict) -> dict:
62
66
return {k : cleanup_value (v ) for (k , v ) in dictionary .items ()}
63
67
return dictionary
64
68
69
+
65
70
def cleanup_value (value : any ) -> any :
66
71
"""Convert a boolean value to string."""
67
72
if isinstance (value , bool ):
68
73
return 'true' if value else 'false'
69
74
return value
70
75
76
+
71
77
def strip_extra_slashes (value : str ) -> str :
72
78
"""Combine multiple trailing slashes to a single slash"""
73
79
if value .endswith ('//' ):
74
80
return value .rstrip ('/' ) + '/'
75
81
return value
76
82
83
+
77
84
def datetime_to_string (val : datetime .datetime ) -> str :
78
85
"""Convert a datetime object to string.
79
86
@@ -93,6 +100,7 @@ def datetime_to_string(val: datetime.datetime) -> str:
93
100
return val .isoformat ().replace ('+00:00' , 'Z' )
94
101
return val
95
102
103
+
96
104
def string_to_datetime (string : str ) -> datetime .datetime :
97
105
"""De-serializes string to datetime.
98
106
@@ -107,6 +115,7 @@ def string_to_datetime(string: str) -> datetime.datetime:
107
115
return val
108
116
return val .replace (tzinfo = datetime .timezone .utc )
109
117
118
+
110
119
def date_to_string (val : datetime .date ) -> str :
111
120
"""Convert a date object to string.
112
121
@@ -120,6 +129,7 @@ def date_to_string(val: datetime.date) -> str:
120
129
return str (val )
121
130
return val
122
131
132
+
123
133
def string_to_date (string : str ) -> datetime .date :
124
134
"""De-serializes string to date.
125
135
@@ -131,6 +141,7 @@ def string_to_date(string: str) -> datetime.date:
131
141
"""
132
142
return date_parser .parse (string ).date ()
133
143
144
+
134
145
def convert_model (val : any ) -> dict :
135
146
"""Convert a model object into an equivalent dict.
136
147
@@ -147,6 +158,7 @@ def convert_model(val: any) -> dict:
147
158
# Consider raising a ValueError here in the next major release
148
159
return val
149
160
161
+
150
162
def convert_list (val : Union [str , List [str ]]) -> str :
151
163
"""Convert a list of strings into comma-separated string.
152
164
@@ -163,6 +175,7 @@ def convert_list(val: Union[str, List[str]]) -> str:
163
175
# Consider raising a ValueError here in the next major release
164
176
return val
165
177
178
+
166
179
def read_external_sources (service_name : str ) -> dict :
167
180
"""Look for external configuration of a service.
168
181
@@ -189,6 +202,7 @@ def read_external_sources(service_name: str) -> dict:
189
202
190
203
return config
191
204
205
+
192
206
def __read_from_env_variables (service_name : str ) -> dict :
193
207
"""Return a config object based on environment variables for a service.
194
208
@@ -203,7 +217,10 @@ def __read_from_env_variables(service_name: str) -> dict:
203
217
_parse_key_and_update_config (config , service_name , key , value )
204
218
return config
205
219
206
- def __read_from_credential_file (service_name : str , * , separator : str = '=' ) -> dict :
220
+
221
+ def __read_from_credential_file (service_name : str ,
222
+ * ,
223
+ separator : str = '=' ) -> dict :
207
224
"""Return a config object based on credentials file for a service.
208
225
209
226
Args:
@@ -222,8 +239,7 @@ def __read_from_credential_file(service_name: str, *, separator: str = '=') -> d
222
239
223
240
# Current working directory
224
241
if credential_file_path is None :
225
- file_path = join (
226
- getcwd (), default_credentials_file_name )
242
+ file_path = join (getcwd (), default_credentials_file_name )
227
243
if isfile (file_path ):
228
244
credential_file_path = file_path
229
245
@@ -241,14 +257,18 @@ def __read_from_credential_file(service_name: str, *, separator: str = '=') -> d
241
257
if len (key_val ) == 2 :
242
258
key = key_val [0 ]
243
259
value = key_val [1 ]
244
- _parse_key_and_update_config (config , service_name , key , value )
260
+ _parse_key_and_update_config (config , service_name , key ,
261
+ value )
245
262
return config
246
263
247
- def _parse_key_and_update_config (config : dict , service_name : str , key : str , value : str ) -> None :
264
+
265
+ def _parse_key_and_update_config (config : dict , service_name : str , key : str ,
266
+ value : str ) -> None :
248
267
service_name = service_name .replace (' ' , '_' ).replace ('-' , '_' ).upper ()
249
268
if key .startswith (service_name ):
250
269
config [key [len (service_name ) + 1 :]] = value
251
270
271
+
252
272
def __read_from_vcap_services (service_name : str ) -> dict :
253
273
"""Return a config object based on the vcap services environment variable.
254
274
@@ -264,29 +284,39 @@ def __read_from_vcap_services(service_name: str) -> dict:
264
284
services = json_import .loads (vcap_services )
265
285
for key in services .keys ():
266
286
for i in range (len (services [key ])):
267
- if vcap_service_credentials and isinstance (vcap_service_credentials , dict ):
287
+ if vcap_service_credentials and isinstance (
288
+ vcap_service_credentials , dict ):
268
289
break
269
290
if services [key ][i ].get ('name' ) == service_name :
270
- vcap_service_credentials = services [key ][i ].get ('credentials' , {})
291
+ vcap_service_credentials = services [key ][i ].get (
292
+ 'credentials' , {})
271
293
if not vcap_service_credentials :
272
294
if service_name in services .keys ():
273
295
service = services .get (service_name )
274
296
if service :
275
- vcap_service_credentials = service [0 ].get ('credentials' , {})
297
+ vcap_service_credentials = service [0 ].get (
298
+ 'credentials' , {})
276
299
277
- if vcap_service_credentials and isinstance (vcap_service_credentials , dict ):
300
+ if vcap_service_credentials and isinstance (vcap_service_credentials ,
301
+ dict ):
278
302
new_vcap_creds = {}
279
- if vcap_service_credentials .get ('username' ) and vcap_service_credentials .get ('password' ): # cf
303
+ # cf
304
+ if vcap_service_credentials .get (
305
+ 'username' ) and vcap_service_credentials .get ('password' ):
280
306
new_vcap_creds ['AUTH_TYPE' ] = 'basic'
281
- new_vcap_creds ['USERNAME' ] = vcap_service_credentials .get ('username' )
282
- new_vcap_creds ['PASSWORD' ] = vcap_service_credentials .get ('password' )
307
+ new_vcap_creds ['USERNAME' ] = vcap_service_credentials .get (
308
+ 'username' )
309
+ new_vcap_creds ['PASSWORD' ] = vcap_service_credentials .get (
310
+ 'password' )
283
311
vcap_service_credentials = new_vcap_creds
284
312
elif vcap_service_credentials .get ('iam_apikey' ):
285
313
new_vcap_creds ['AUTH_TYPE' ] = 'iam'
286
- new_vcap_creds ['APIKEY' ] = vcap_service_credentials .get ('iam_apikey' )
314
+ new_vcap_creds ['APIKEY' ] = vcap_service_credentials .get (
315
+ 'iam_apikey' )
287
316
vcap_service_credentials = new_vcap_creds
288
317
elif vcap_service_credentials .get ('apikey' ):
289
318
new_vcap_creds ['AUTH_TYPE' ] = 'iam'
290
- new_vcap_creds ['APIKEY' ] = vcap_service_credentials .get ('apikey' )
319
+ new_vcap_creds ['APIKEY' ] = vcap_service_credentials .get (
320
+ 'apikey' )
291
321
vcap_service_credentials = new_vcap_creds
292
322
return vcap_service_credentials
0 commit comments