[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
This commit is contained in:
Will 2018-02-22 19:36:07 -05:00 committed by GitHub
parent 4e36c2f82d
commit 7c848153f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
import contextlib
import logging import logging
import collections import collections
from weakref import ref
from copy import deepcopy from copy import deepcopy
from typing import Union, Tuple from typing import Union, Tuple
@ -445,6 +447,10 @@ class Group(Value):
await self.set({}) await self.set({})
_config_cogrefs = {}
_config_coreref = None
class Config: class Config:
"""Configuration manager for cogs and Red. """Configuration manager for cogs and Red.
@ -529,6 +535,11 @@ class Config:
cog_name = cog_path_override.stem cog_name = cog_path_override.stem
uuid = str(hash(identifier)) 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 # We have to import this here otherwise we have a circular dependency
from .data_manager import basic_config 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 = get_driver(driver_name, cog_name, data_path_override=cog_path_override,
**driver_details) **driver_details)
return cls(cog_name=cog_name, unique_identifier=uuid, conf = cls(cog_name=cog_name, unique_identifier=uuid,
force_registration=force_registration, force_registration=force_registration,
driver=driver) driver=driver)
_config_cogrefs[cog_name] = ref(conf)
return conf
@classmethod @classmethod
def get_core_conf(cls, force_registration: bool=False): def get_core_conf(cls, force_registration: bool=False):
@ -558,6 +571,10 @@ class Config:
See `force_registration`. 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() core_path = core_data_path()
# We have to import this here otherwise we have a circular dependency # 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 = get_driver(driver_name, "Core", data_path_override=core_path,
**driver_details) **driver_details)
return cls(cog_name="Core", driver=driver, conf = cls(cog_name="Core", driver=driver,
unique_identifier='0', unique_identifier='0',
force_registration=force_registration) force_registration=force_registration)
_config_coreref = ref(conf)
return conf
def __getattr__(self, item: str) -> Union[Group, Value]: def __getattr__(self, item: str) -> Union[Group, Value]:
"""Same as `group.__getattr__` except for global data. """Same as `group.__getattr__` except for global data.