Skip to content

Commit

Permalink
Split out get_latest_confs and migrate into redbot.core._config
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen committed May 8, 2023
1 parent 8c552f4 commit 3284654
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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)
2 changes: 1 addition & 1 deletion redbot/core/_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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 @@ -34,7 +34,6 @@
_T = TypeVar("_T")

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


class ConfigMeta(type):
Expand Down Expand Up @@ -64,14 +63,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 @@ -1528,16 +1519,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
5 changes: 3 additions & 2 deletions redbot/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
cli_level_to_log_level,
)
from redbot.core import config, data_manager
from redbot.core._cli import ExitCodes
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,
Expand Down Expand Up @@ -275,7 +276,7 @@ async def do_migration(
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 Down

0 comments on commit 3284654

Please sign in to comment.