mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-20 18:06:08 -05:00
[V3 Config] Fix unloading and implement singleton driver (#1458)
* Add the identifier as an initialization parameter * Remove config object singleton and opt for a shared JSON datastore * Fix bot unloading to deal with memory leaks * Fix tests * Fix clear all bug
This commit is contained in:
@@ -2,9 +2,9 @@ __all__ = ["BaseDriver"]
|
||||
|
||||
|
||||
class BaseDriver:
|
||||
def __init__(self, cog_name):
|
||||
def __init__(self, cog_name, identifier):
|
||||
self.cog_name = cog_name
|
||||
self.unique_cog_identifier = None # This is set by Config's init method
|
||||
self.unique_cog_identifier = identifier
|
||||
|
||||
async def get(self, *identifiers: str):
|
||||
"""
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
import weakref
|
||||
import logging
|
||||
|
||||
from ..json_io import JsonIO
|
||||
|
||||
@@ -8,6 +10,28 @@ from .red_base import BaseDriver
|
||||
__all__ = ["JSON"]
|
||||
|
||||
|
||||
_shared_datastore = {}
|
||||
_driver_counts = {}
|
||||
_finalizers = []
|
||||
|
||||
log = logging.getLogger("redbot.json_driver")
|
||||
|
||||
|
||||
def finalize_driver(cog_name):
|
||||
if cog_name not in _driver_counts:
|
||||
return
|
||||
|
||||
_driver_counts[cog_name] -= 1
|
||||
|
||||
if _driver_counts[cog_name] == 0:
|
||||
if cog_name in _shared_datastore:
|
||||
del _shared_datastore[cog_name]
|
||||
|
||||
for f in _finalizers:
|
||||
if not f.alive:
|
||||
_finalizers.remove(f)
|
||||
|
||||
|
||||
class JSON(BaseDriver):
|
||||
"""
|
||||
Subclass of :py:class:`.red_base.BaseDriver`.
|
||||
@@ -20,9 +44,9 @@ class JSON(BaseDriver):
|
||||
|
||||
The path in which to store the file indicated by :py:attr:`file_name`.
|
||||
"""
|
||||
def __init__(self, cog_name, *, data_path_override: Path=None,
|
||||
def __init__(self, cog_name, identifier, *, data_path_override: Path=None,
|
||||
file_name_override: str="settings.json"):
|
||||
super().__init__(cog_name)
|
||||
super().__init__(cog_name, identifier)
|
||||
self.file_name = file_name_override
|
||||
if data_path_override:
|
||||
self.data_path = data_path_override
|
||||
@@ -35,6 +59,26 @@ class JSON(BaseDriver):
|
||||
|
||||
self.jsonIO = JsonIO(self.data_path)
|
||||
|
||||
self._load_data()
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
return _shared_datastore.get(self.cog_name)
|
||||
|
||||
@data.setter
|
||||
def data(self, value):
|
||||
_shared_datastore[self.cog_name] = value
|
||||
|
||||
def _load_data(self):
|
||||
if self.cog_name not in _driver_counts:
|
||||
_driver_counts[self.cog_name] = 0
|
||||
_driver_counts[self.cog_name] += 1
|
||||
|
||||
_finalizers.append(weakref.finalize(self, finalize_driver, self.cog_name))
|
||||
|
||||
if self.data is not None:
|
||||
return
|
||||
|
||||
try:
|
||||
self.data = self.jsonIO._load_json()
|
||||
except FileNotFoundError:
|
||||
@@ -65,8 +109,11 @@ class JSON(BaseDriver):
|
||||
try:
|
||||
for i in full_identifiers[:-1]:
|
||||
partial = partial[i]
|
||||
del partial[identifiers[-1]]
|
||||
del partial[full_identifiers[-1]]
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
await self.jsonIO._threadsafe_save_json(self.data)
|
||||
|
||||
def get_config_details(self):
|
||||
return
|
||||
|
||||
@@ -32,8 +32,8 @@ class Mongo(BaseDriver):
|
||||
"""
|
||||
Subclass of :py:class:`.red_base.BaseDriver`.
|
||||
"""
|
||||
def __init__(self, cog_name, **kwargs):
|
||||
super().__init__(cog_name)
|
||||
def __init__(self, cog_name, identifier, **kwargs):
|
||||
super().__init__(cog_name, identifier)
|
||||
|
||||
if _conn is None:
|
||||
_initialize(**kwargs)
|
||||
@@ -105,10 +105,15 @@ class Mongo(BaseDriver):
|
||||
dot_identifiers = '.'.join(identifiers)
|
||||
mongo_collection = self.get_collection()
|
||||
|
||||
await mongo_collection.update_one(
|
||||
{'_id': self.unique_cog_identifier},
|
||||
update={"$unset": {dot_identifiers: 1}}
|
||||
)
|
||||
if len(identifiers) > 0:
|
||||
await mongo_collection.update_one(
|
||||
{'_id': self.unique_cog_identifier},
|
||||
update={"$unset": {dot_identifiers: 1}}
|
||||
)
|
||||
else:
|
||||
await mongo_collection.delete_one(
|
||||
{'_id': self.unique_cog_identifier}
|
||||
)
|
||||
|
||||
|
||||
def get_config_details():
|
||||
|
||||
Reference in New Issue
Block a user