10
10
from typing import Dict , List
11
11
from threading import Semaphore
12
12
import json
13
+ import azure .cosmos .cosmos_client as cosmos_client # pylint: disable=no-name-in-module,import-error
14
+ import azure .cosmos .errors as cosmos_errors # pylint: disable=no-name-in-module,import-error
13
15
from botbuilder .core .storage import Storage , StoreItem
14
- import azure .cosmos .cosmos_client as cosmos_client
15
- import azure .cosmos .errors as cosmos_errors
16
16
17
17
18
18
class CosmosDbConfig :
@@ -74,13 +74,13 @@ def sanitize_key(key) -> str:
8000
74
74
75
75
@staticmethod
76
76
def truncate_key (key : str ) -> str :
77
- MAX_KEY_LEN = 255
77
+ max_key_len = 255
78
78
79
- if len (key ) > MAX_KEY_LEN :
79
+ if len (key ) > max_key_len :
80
80
aux_hash = sha256 (key .encode ("utf-8" ))
81
81
aux_hex = aux_hash .hexdigest ()
82
82
83
- key = key [0 : MAX_KEY_LEN - len (aux_hex )] + aux_hex
83
+ key = key [0 : max_key_len - len (aux_hex )] + aux_hex
84
84
85
85
return key
86
86
@@ -101,8 +101,8 @@ def __init__(
101
101
self .config .endpoint , {"masterKey" : self .config .masterkey }
102
102
)
103
103
# these are set by the functions that check
104
- # the presence of the db and container or creates them
105
- self .db = None
104
+ # the presence of the database and container or creates them
105
+ self .database = None
106
106
self .container = None
107
107
self ._database_creation_options = config .database_creation_options
108
108
self ._container_creation_options = config .container_creation_options
@@ -146,11 +146,11 @@ async def read(self, keys: List[str]) -> Dict[str, object]:
146
146
)
147
147
# return a dict with a key and a StoreItem
148
148
return {r .get ("realId" ): self .__create_si (r ) for r in results }
149
- else :
150
- # No keys passed in, no result to return.
151
- return {}
152
- except TypeError as e :
153
- raise e
149
+
150
+ # No keys passed in, no result to return.
151
+ return {}
152
+ except TypeError as error :
153
+ raise error
154
154
155
155
async def write (self , changes : Dict [str , StoreItem ]):
156
156
"""Save storeitems to storage.
@@ -180,7 +180,7 @@ async def write(self, changes: Dict[str, StoreItem]):
180
180
options = {"disableAutomaticIdGeneration" : True },
181
181
)
182
182
# if we have an etag, do opt. concurrency replace
183
- elif len ( e_tag ) > 0 :
183
+ elif e_tag :
184
184
access_condition = {"type" : "IfMatch" , "condition" : e_tag }
185
185
self .client .ReplaceItem (
186
186
document_link = self .__item_link (
@@ -192,8 +192,8 @@ async def write(self, changes: Dict[str, StoreItem]):
192
192
# error when there is no e_tag
193
193
else :
194
194
raise Exception ("cosmosdb_storage.write(): etag missing" )
195
- except Exception as e :
196
- raise e
195
+ except Exception as error :
196
+ raise error
197
197
198
198
async def delete (self , keys : List [str ]):
199
199
"""Remove storeitems from storage.
@@ -211,18 +211,18 @@ async def delete(self, keys: List[str]):
211
211
options ["partitionKey" ] = self .config .partition_key
212
212
213
213
# call the function for each key
214
- for k in keys :
214
+ for key in keys :
215
215
self .client .DeleteItem (
216
- document_link = self .__item_link (CosmosDbKeyEscape .sanitize_key (k )),
216
+ document_link = self .__item_link (CosmosDbKeyEscape .sanitize_key (key )),
217
217
options = options ,
218
218
)
219
219
# print(res)
220
- except cosmos_errors .HTTPFailure as h :
220
+ except cosmos_errors .HTTPFailure as http_failure :
221
221
# print(h.status_code)
222
- if h .status_code != 404 :
223
- raise h
224
- except TypeError as e :
225
- raise e
222
+ if http_failure .status_code != 404 :
223
+ raise http_failure
224
+ except TypeError as error :
225
+ raise error
226
226
227
227
def __create_si (self , result ) -> StoreItem :
228
228
"""Create a StoreItem from a result out of CosmosDB.
@@ -238,28 +238,28 @@ def __create_si(self, result) -> StoreItem:
238
238
# create and return the StoreItem
239
239
return StoreItem (** doc )
240
240
241
- def __create_dict (self , si : StoreItem ) -> Dict :
241
+ def __create_dict (self , store_item : StoreItem ) -> Dict :
242
242
"""Return the dict of a StoreItem.
243
243
244
244
This eliminates non_magic attributes and the e_tag.
245
245
246
- :param si :
246
+ :param store_item :
247
247
:return dict:
248
248
"""
249
249
# read the content
250
250
non_magic_attr = [
251
- attr for attr in dir (si ) if not attr .startswith ("_" ) or attr .__eq__ ("e_tag" )
251
+ attr for attr in dir (store_item ) if not attr .startswith ("_" ) or attr .__eq__ ("e_tag" )
252
252
]
253
253
# loop through attributes and write and return a dict
254
- return {attr : getattr (si , attr ) for attr in non_magic_attr }
254
+ return {attr : getattr (store_item , attr ) for attr in non_magic_attr }
255
255
256
- def __item_link (self , id ) -> str :
256
+ def __item_link (self , identifier ) -> str :
257
257
"""Return the item link of a item in the container.
258
258
259
- :param id :
259
+ :param identifier :
260
260
:return str:
261
261
"""
262
- return self .__container_link + "/docs/" + id
262
+ return self .__container_link + "/docs/" + identifier
263
263
264
264
@property
265
265
def __container_link (self ) -> str :
@@ -276,28 +276,28 @@ def __database_link(self) -> str:
276
276
277
277
:return str:
278
278
"""
279
- return "dbs/" + self .db
279
+ return "dbs/" + self .database
280
280
281
281
@property
282
282
def __container_exists (self ) -> bool :
283
283
"""Return whether the database and container have been created.
284
284
285
285
:return bool:
286
286
"""
287
- return self .db and self .container
287
+ return self .database and self .container
288
288
289
289
def __create_db_and_container (self ):
290
290
"""Call the get or create methods."""
291
291
with self .__semaphore :
292
292
db_id = self .config .database
293
293
container_name = self .config .container
294
- self .db = self ._get_or_create_database (self .client , db_id )
294
+ self .database = self ._get_or_create_database (self .client , db_id )
295
295
self .container = self ._get_or_create_container (self .client , container_name )
296
296
297
- def _get_or_create_database (self , doc_client , id ) -> str :
297
+ def _get_or_create_database (self , doc_client , id ) -> str : # pylint: disable=invalid-name
298
298
"""Return the database link.
299
299
300
- Check if the database exists or create the db .
300
+ Check if the database exists or create the database .
301
301
302
302
:param doc_client:
303
303
:param id:
@@ -312,13 +312,13 @@ def _get_or_create_database(self, doc_client, id) -> str:
312
312
}
313
313
)
314
314
)
315
- # if there are results, return the first (db names are unique)
316
- if len ( dbs ) > 0 :
315
+ # if there are results, return the first (database names are unique)
316
+ if dbs :
317
317
return dbs [0 ]["id" ]
318
- else :
319
- # create the database if it didn't exist
320
- res = doc_client .CreateDatabase ({"id" : id }, self ._database_creation_options )
321
- return res ["id" ]
318
+
319
+ # create the database if it didn't exist
320
+ res = doc_client .CreateDatabase ({"id" : id }, self ._database_creation_options )
321
+ return res ["id" ]
322
322
323
323
def _get_or_create_container (self , doc_client , container ) -> str :
324
324
"""Return the container link.
@@ -340,13 +340,13 @@ def _get_or_create_container(self, doc_client, container) -> str:
340
340
)
341
341
)
342
342
# if there are results, return the first (container names are unique)
343
- if len ( containers ) > 0 :
343
+ if containers :
344
344
return containers [0 ]["id" ]
345
- else :
346
- # Create a container if it didn't exist
347
- res = doc_client .CreateContainer (
348
- self .__database_link ,
349
- {"id" : container },
350
- self ._container_creation_options ,
351
- )
352
- return res ["id" ]
345
+
346
+ # Create a container if it didn't exist
347
+ res = doc_client .CreateContainer (
348
+ self .__database_link ,
349
+ {"id" : container },
350
+ self ._container_creation_options ,
351
+ )
352
+ return res ["id" ]
0 commit comments