mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-07 09:52:30 -05:00
Split public and private config/driver APIs (#6024)
This commit is contained in:
26
redbot/core/_config.py
Normal file
26
redbot/core/_config.py
Normal file
@@ -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)
|
||||
@@ -8,6 +8,8 @@ from .postgres import PostgresDriver
|
||||
|
||||
__all__ = [
|
||||
"get_driver",
|
||||
"get_driver_class",
|
||||
"get_driver_class_include_old",
|
||||
"ConfigCategory",
|
||||
"IdentifierData",
|
||||
"BaseDriver",
|
||||
@@ -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
|
||||
"""
|
||||
@@ -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(
|
||||
|
||||
@@ -21,7 +21,7 @@ from redbot.core.i18n import (
|
||||
)
|
||||
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,
|
||||
|
||||
@@ -35,7 +35,6 @@ log = logging.getLogger("red.config")
|
||||
_T = TypeVar("_T")
|
||||
|
||||
_config_cache = weakref.WeakValueDictionary()
|
||||
_retrieved = weakref.WeakSet()
|
||||
|
||||
|
||||
class ConfigMeta(type):
|
||||
@@ -65,14 +64,6 @@ class ConfigMeta(type):
|
||||
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.
|
||||
|
||||
@@ -1529,16 +1520,6 @@ class Config(metaclass=ConfigMeta):
|
||||
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`.
|
||||
|
||||
Reference in New Issue
Block a user