Improved caching and utils structure
This commit is contained in:
1
modules/__init__.py
Normal file
1
modules/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import utils, database, migrator, scheduler
|
8
modules/utils/__init__.py
Normal file
8
modules/utils/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from .autocomplete_utils import (
|
||||
autocomplete_active_events,
|
||||
autocomplete_event_stages,
|
||||
autocomplete_languages,
|
||||
autocomplete_timezones,
|
||||
)
|
||||
from .cache_utils import restore_from_cache
|
||||
from .logging_utils import get_logger, get_logging_config
|
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Dict, Any
|
||||
from typing import Any, Dict, List
|
||||
from zoneinfo import ZoneInfo, available_timezones
|
||||
|
||||
from bson import ObjectId
|
||||
@@ -9,28 +9,23 @@ from pymongo import ASCENDING
|
||||
from modules.database import col_events, col_stages
|
||||
|
||||
|
||||
def hex_to_int(hex_color: str) -> int:
|
||||
return int(hex_color.lstrip("#"), 16)
|
||||
|
||||
|
||||
def int_to_hex(integer_color: int) -> str:
|
||||
return "#" + format(integer_color, "06x")
|
||||
|
||||
|
||||
# TODO Maybe move to a separate module
|
||||
async def autocomplete_timezones(ctx: AutocompleteContext) -> List[str]:
|
||||
"""Return available timezones"""
|
||||
|
||||
return sorted(list(available_timezones()))
|
||||
|
||||
|
||||
# TODO Maybe move to a separate module
|
||||
async def autocomplete_languages(ctx: AutocompleteContext) -> List[str]:
|
||||
"""Return locales supported by the bot"""
|
||||
|
||||
# TODO Discord normally uses a different set of locales.
|
||||
# For example, "en" being "en-US", etc. This will require changes to locale handling later.
|
||||
return ctx.bot.locales.keys()
|
||||
|
||||
|
||||
# TODO Maybe move to a separate module
|
||||
async def autocomplete_active_events(ctx: AutocompleteContext) -> List[OptionChoice]:
|
||||
"""Return list of active events"""
|
||||
|
||||
query: Dict[str, Any] = {
|
||||
"ended": None,
|
||||
"ends": {"$gt": datetime.now(tz=ZoneInfo("UTC"))},
|
||||
@@ -45,8 +40,9 @@ async def autocomplete_active_events(ctx: AutocompleteContext) -> List[OptionCho
|
||||
return event_names
|
||||
|
||||
|
||||
# TODO Maybe move to a separate module
|
||||
async def autocomplete_event_stages(ctx: AutocompleteContext) -> List[OptionChoice]:
|
||||
"""Return list of stages of the event"""
|
||||
|
||||
event_id: str | None = ctx.options["event"]
|
||||
|
||||
if event_id is None:
|
10
modules/utils/cache_utils.py
Normal file
10
modules/utils/cache_utils.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from bson import ObjectId
|
||||
from libbot.cache.classes import Cache
|
||||
|
||||
|
||||
def restore_from_cache(
|
||||
cache_prefix: str, cache_key: str | int | ObjectId, cache: Optional[Cache] = None
|
||||
) -> Dict[str, Any] | None:
|
||||
return None if cache is None else cache.get_json(f"{cache_prefix}_{cache_key}")
|
6
modules/utils/color.py
Normal file
6
modules/utils/color.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def hex_to_int(hex_color: str) -> int:
|
||||
return int(hex_color.lstrip("#"), 16)
|
||||
|
||||
|
||||
def int_to_hex(integer_color: int) -> str:
|
||||
return "#" + format(integer_color, "06x")
|
Reference in New Issue
Block a user