Skip to content

Commit

Permalink
Split public and private config/driver APIs (#6024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen authored Jan 26, 2025
1 parent b13b1f8 commit f962aeb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
- any:
- redbot/core/_drivers/**/*
- "!redbot/core/_drivers/**/locales/*"
- redbot/core/_config.py
- redbot/core/config.py
# Docs
- docs/framework_config.rst
Expand Down
26 changes: 26 additions & 0 deletions redbot/core/_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import weakref
from typing import Tuple, Type

from redbot.core.config import Config, _config_cache
from redbot.core._drivers import BaseDriver

__all__ = ("get_latest_confs", "migrate")

_retrieved = weakref.WeakSet()


def get_latest_confs() -> Tuple[Config, ...]:
global _retrieved
ret = set(_config_cache.values()) - set(_retrieved)
_retrieved |= ret
return tuple(ret)


async def migrate(cur_driver_cls: Type[BaseDriver], new_driver_cls: Type[BaseDriver]) -> None:
"""Migrate from one driver type to another."""
# Get custom group data
core_conf = Config.get_core_conf(allow_old=True)
core_conf.init_custom("CUSTOM_GROUPS", 2)
all_custom_group_data = await core_conf.custom("CUSTOM_GROUPS").all()

await cur_driver_cls.migrate_to(new_driver_cls, all_custom_group_data)
6 changes: 4 additions & 2 deletions redbot/core/_drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

__all__ = [
"get_driver",
"get_driver_class",
"get_driver_class_include_old",
"ConfigCategory",
"IdentifierData",
"BaseDriver",
Expand All @@ -32,7 +34,7 @@ class BackendType(enum.Enum):
_DRIVER_CLASSES = {BackendType.JSON: JsonDriver, BackendType.POSTGRES: PostgresDriver}


def _get_driver_class_include_old(storage_type: Optional[BackendType] = None) -> Type[BaseDriver]:
def get_driver_class_include_old(storage_type: Optional[BackendType] = None) -> Type[BaseDriver]:
"""
ONLY for use in CLI for moving data away from a no longer supported backend
"""
Expand Down Expand Up @@ -115,7 +117,7 @@ def get_driver(
if not allow_old:
driver_cls: Type[BaseDriver] = get_driver_class(storage_type)
else:
driver_cls: Type[BaseDriver] = _get_driver_class_include_old(storage_type)
driver_cls: Type[BaseDriver] = get_driver_class_include_old(storage_type)
except ValueError:
if storage_type in (BackendType.MONGOV1, BackendType.MONGO):
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion redbot/core/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from .. import __version__ as red_version, version_info as red_version_info
from . import commands
from .config import get_latest_confs
from ._config import get_latest_confs
from .utils._internal_utils import (
fuzzy_command_search,
format_fuzzy_results,
Expand Down
19 changes: 0 additions & 19 deletions redbot/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
_T = TypeVar("_T")

_config_cache = weakref.WeakValueDictionary()
_retrieved = weakref.WeakSet()


class ConfigMeta(type):
Expand Down Expand Up @@ -65,14 +64,6 @@ def __call__(
return instance


def get_latest_confs() -> Tuple["Config"]:
global _retrieved
ret = set(_config_cache.values()) - set(_retrieved)
_retrieved |= ret
# noinspection PyTypeChecker
return tuple(ret)


class _ValueCtxManager(Awaitable[_T], AsyncContextManager[_T]): # pylint: disable=duplicate-bases
"""Context manager implementation of config values.
Expand Down Expand Up @@ -1529,16 +1520,6 @@ def get_custom_lock(self, group_identifier: str) -> asyncio.Lock:
return self._lock_cache.setdefault(id_data, asyncio.Lock())


async def migrate(cur_driver_cls: Type[BaseDriver], new_driver_cls: Type[BaseDriver]) -> None:
"""Migrate from one driver type to another."""
# Get custom group data
core_conf = Config.get_core_conf(allow_old=True)
core_conf.init_custom("CUSTOM_GROUPS", 2)
all_custom_group_data = await core_conf.custom("CUSTOM_GROUPS").all()

await cur_driver_cls.migrate_to(new_driver_cls, all_custom_group_data)


def _str_key_dict(value: Dict[Any, _T]) -> Dict[str, _T]:
"""
Recursively casts all keys in the given `dict` to `str`.
Expand Down
22 changes: 14 additions & 8 deletions redbot/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
create_backup as red_create_backup,
cli_level_to_log_level,
)
from redbot.core import config, data_manager, _drivers
from redbot.core import config, data_manager
from redbot.core._config import migrate
from redbot.core._cli import ExitCodes
from redbot.core.data_manager import appdir, config_dir, config_file
from redbot.core._drivers import BackendType, IdentifierData
from redbot.core._drivers import (
BackendType,
IdentifierData,
get_driver_class,
get_driver_class_include_old,
)

conversion_log = logging.getLogger("red.converter")

Expand Down Expand Up @@ -211,7 +217,7 @@ def basic_setup(
storage_type = get_storage_type(backend, interactive=interactive)

default_dirs["STORAGE_TYPE"] = storage_type.value
driver_cls = _drivers.get_driver_class(storage_type)
driver_cls = get_driver_class(storage_type)
default_dirs["STORAGE_DETAILS"] = driver_cls.get_config_details()

if name in instance_data:
Expand Down Expand Up @@ -262,15 +268,15 @@ def get_target_backend(backend: str) -> BackendType:
async def do_migration(
current_backend: BackendType, target_backend: BackendType
) -> Dict[str, Any]:
cur_driver_cls = _drivers._get_driver_class_include_old(current_backend)
new_driver_cls = _drivers.get_driver_class(target_backend)
cur_driver_cls = get_driver_class_include_old(current_backend)
new_driver_cls = get_driver_class(target_backend)
cur_storage_details = data_manager.storage_details()
new_storage_details = new_driver_cls.get_config_details()

await cur_driver_cls.initialize(**cur_storage_details)
await new_driver_cls.initialize(**new_storage_details)

await config.migrate(cur_driver_cls, new_driver_cls)
await migrate(cur_driver_cls, new_driver_cls)

await cur_driver_cls.teardown()
await new_driver_cls.teardown()
Expand All @@ -284,7 +290,7 @@ async def create_backup(instance: str, destination_folder: Path = Path.home()) -
if backend_type != BackendType.JSON:
await do_migration(backend_type, BackendType.JSON)
print("Backing up the instance's data...")
driver_cls = _drivers.get_driver_class()
driver_cls = get_driver_class()
await driver_cls.initialize(**data_manager.storage_details())
backup_fpath = await red_create_backup(destination_folder)
await driver_cls.teardown()
Expand Down Expand Up @@ -320,7 +326,7 @@ async def remove_instance(
if _create_backup is True:
await create_backup(instance)

driver_cls = _drivers.get_driver_class(backend)
driver_cls = get_driver_class(backend)
if delete_data is True:
await driver_cls.initialize(**data_manager.storage_details())
try:
Expand Down

0 comments on commit f962aeb

Please sign in to comment.