-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIdstore.py
40 lines (29 loc) · 914 Bytes
/
Idstore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from base64 import urlsafe_b64encode
from os import urandom
from typing import Set
class Idstore:
_ID_SIZE = 21
_ids: Set[str]
def __init__(self):
self._ids = set()
def _create_id(self) -> str:
b = urandom(self._ID_SIZE)
strid = urlsafe_b64encode(b).decode()
return strid[: self._ID_SIZE]
def register(self, *args):
"""
Registers the ids in every item provided (must be retrieved with download_user_content)
"""
for e in args:
if "id" in e:
self._ids.add(e["id"])
def create(self) -> str:
"""
Create a new unique id, that hasn't been registered yet, and register it
:return: Created id
"""
new_id = next(iter(self._ids))
while new_id in self._ids:
new_id = self._create_id()
self._ids.add(new_id)
return new_id