From 7c848153f84fa04375480c19e4a6d33a5e08da96 Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 22 Feb 2018 19:36:07 -0500 Subject: [PATCH] [V3 Config] Limit config objects to a single one per cog (#1337) * Limit config objects to a single one per cog * do it correctly * Do nicer thing * do it right... * I will get this right eventually --- redbot/core/config.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/redbot/core/config.py b/redbot/core/config.py index 81ecc7073..713b091a5 100644 --- a/redbot/core/config.py +++ b/redbot/core/config.py @@ -1,5 +1,7 @@ +import contextlib import logging import collections +from weakref import ref from copy import deepcopy from typing import Union, Tuple @@ -445,6 +447,10 @@ class Group(Value): await self.set({}) +_config_cogrefs = {} +_config_coreref = None + + class Config: """Configuration manager for cogs and Red. @@ -529,6 +535,11 @@ class Config: cog_name = cog_path_override.stem uuid = str(hash(identifier)) + with contextlib.suppress(KeyError): + conf = _config_cogrefs[cog_name]() + if conf is not None: + return conf + # We have to import this here otherwise we have a circular dependency from .data_manager import basic_config @@ -541,9 +552,11 @@ class Config: driver = get_driver(driver_name, cog_name, data_path_override=cog_path_override, **driver_details) - return cls(cog_name=cog_name, unique_identifier=uuid, + conf = cls(cog_name=cog_name, unique_identifier=uuid, force_registration=force_registration, driver=driver) + _config_cogrefs[cog_name] = ref(conf) + return conf @classmethod def get_core_conf(cls, force_registration: bool=False): @@ -558,6 +571,10 @@ class Config: See `force_registration`. """ + global _config_coreref + if _config_coreref is not None and _config_coreref() is not None: + return _config_coreref() + core_path = core_data_path() # We have to import this here otherwise we have a circular dependency @@ -568,9 +585,11 @@ class Config: driver = get_driver(driver_name, "Core", data_path_override=core_path, **driver_details) - return cls(cog_name="Core", driver=driver, + conf = cls(cog_name="Core", driver=driver, unique_identifier='0', force_registration=force_registration) + _config_coreref = ref(conf) + return conf def __getattr__(self, item: str) -> Union[Group, Value]: """Same as `group.__getattr__` except for global data.