mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 02:16:09 -05:00
[V3 Config] Implement a true clear method (#1344)
* Add clear methods to drivers * Update config clear methods, add to Value * Add test
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
from typing import Tuple
|
||||
|
||||
__all__ = ["BaseDriver"]
|
||||
|
||||
|
||||
@@ -8,13 +6,18 @@ class BaseDriver:
|
||||
self.cog_name = cog_name
|
||||
self.unique_cog_identifier = None # This is set by Config's init method
|
||||
|
||||
async def get(self, *identifiers: Tuple[str]):
|
||||
async def get(self, *identifiers: str):
|
||||
"""
|
||||
Finds the value indicate by the given identifiers.
|
||||
|
||||
:param identifiers:
|
||||
Parameters
|
||||
----------
|
||||
identifiers
|
||||
A list of identifiers that correspond to nested dict accesses.
|
||||
:return:
|
||||
|
||||
Returns
|
||||
-------
|
||||
Any
|
||||
Stored value.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
@@ -24,18 +27,34 @@ class BaseDriver:
|
||||
Asks users for additional configuration information necessary
|
||||
to use this config driver.
|
||||
|
||||
:return:
|
||||
Returns
|
||||
-------
|
||||
Dict of configuration details.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
async def set(self, *identifiers: Tuple[str], value=None):
|
||||
async def set(self, *identifiers: str, value=None):
|
||||
"""
|
||||
Sets the value of the key indicated by the given identifiers.
|
||||
|
||||
:param identifiers:
|
||||
Parameters
|
||||
----------
|
||||
identifiers
|
||||
A list of identifiers that correspond to nested dict accesses.
|
||||
:param value:
|
||||
value
|
||||
Any JSON serializable python object.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
async def clear(self, *identifiers: str):
|
||||
"""
|
||||
Clears out the value specified by the given identifiers.
|
||||
|
||||
Equivalent to using ``del`` on a dict.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
identifiers
|
||||
A list of identifiers that correspond to nested dict accesses.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -58,3 +58,14 @@ class JSON(BaseDriver):
|
||||
|
||||
partial[full_identifiers[-1]] = value
|
||||
await self.jsonIO._threadsafe_save_json(self.data)
|
||||
|
||||
async def clear(self, *identifiers: str):
|
||||
partial = self.data
|
||||
full_identifiers = (self.unique_cog_identifier, *identifiers)
|
||||
for i in full_identifiers[:-1]:
|
||||
if i not in partial:
|
||||
break
|
||||
partial = partial[i]
|
||||
else:
|
||||
del partial[identifiers[-1]]
|
||||
await self.jsonIO._threadsafe_save_json(self.data)
|
||||
|
||||
@@ -71,7 +71,7 @@ class Mongo(BaseDriver):
|
||||
uuid, identifiers = identifiers[0], identifiers[1:]
|
||||
return uuid, identifiers
|
||||
|
||||
async def get(self, *identifiers: Tuple[str]):
|
||||
async def get(self, *identifiers: str):
|
||||
await self._ensure_connected()
|
||||
|
||||
mongo_collection = self.get_collection()
|
||||
@@ -104,6 +104,17 @@ class Mongo(BaseDriver):
|
||||
upsert=True
|
||||
)
|
||||
|
||||
async def clear(self, *identifiers: str):
|
||||
await self._ensure_connected()
|
||||
|
||||
dot_identifiers = '.'.join(identifiers)
|
||||
mongo_collection = self.get_collection()
|
||||
|
||||
await mongo_collection.update_one(
|
||||
{'_id': self.unique_cog_identifier},
|
||||
update={"$unset": {dot_identifiers: 1}}
|
||||
)
|
||||
|
||||
|
||||
def get_config_details():
|
||||
host = input("Enter host address: ")
|
||||
|
||||
Reference in New Issue
Block a user