2019-12-16 06:27:34 +01:00
|
|
|
import re
|
2020-09-05 04:02:13 +02:00
|
|
|
import secrets
|
2023-07-19 00:44:51 +02:00
|
|
|
from typing import Callable, List, Optional, TypeVar
|
2023-04-26 03:24:07 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
T = TypeVar("T")
|
2016-06-03 18:39:57 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-08-01 11:18:37 +02:00
|
|
|
def generate_api_key() -> str:
|
2020-09-05 04:02:13 +02:00
|
|
|
api_key = ""
|
|
|
|
while len(api_key) < 32:
|
|
|
|
# One iteration suffices 99.4992% of the time.
|
|
|
|
api_key += secrets.token_urlsafe(3 * 9).replace("_", "").replace("-", "")
|
|
|
|
return api_key[:32]
|
2018-08-01 11:18:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-12-16 06:27:34 +01:00
|
|
|
def has_api_key_format(key: str) -> bool:
|
|
|
|
return bool(re.fullmatch(r"([A-Za-z0-9]){32}", key))
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-07-25 11:20:48 +02:00
|
|
|
def assert_is_not_none(value: Optional[T]) -> T:
|
|
|
|
assert value is not None
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def process_list_in_batches(
|
2023-04-26 03:18:14 +02:00
|
|
|
lst: List[T], chunk_size: int, process_batch: Callable[[List[T]], None]
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2018-10-15 14:24:13 +02:00
|
|
|
offset = 0
|
|
|
|
|
|
|
|
while True:
|
2021-02-12 08:19:30 +01:00
|
|
|
items = lst[offset : offset + chunk_size]
|
2018-10-15 14:24:13 +02:00
|
|
|
if not items:
|
|
|
|
break
|
|
|
|
process_batch(items)
|
|
|
|
offset += chunk_size
|
2024-03-20 02:30:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def optional_bytes_to_mib(value: Optional[int]) -> Optional[int]:
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return value >> 20
|