mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-06 01:12:33 -05:00
[Config] Asynchronous getters (#907)
* Make config get async * Asyncify alias * Asyncify bank * Asyncify cog manager * IT BOOTS * Asyncify core commands * Asyncify repo manager * Asyncify downloader * Asyncify economy * Asyncify alias TESTS * Asyncify economy TESTS * Asyncify downloader TESTS * Asyncify config TESTS * A bank thing * Asyncify Bank cog * Warning message in docs * Update docs with await syntax * Update docs with await syntax
This commit is contained in:
@@ -38,6 +38,14 @@ class Value:
|
||||
def identifiers(self):
|
||||
return tuple(str(i) for i in self._identifiers)
|
||||
|
||||
async def _get(self, default):
|
||||
driver = self.spawner.get_driver()
|
||||
try:
|
||||
ret = await driver.get(self.identifiers)
|
||||
except KeyError:
|
||||
return default or self.default
|
||||
return ret
|
||||
|
||||
def __call__(self, default=None):
|
||||
"""
|
||||
Each :py:class:`Value` object is created by the :py:meth:`Group.__getattr__` method.
|
||||
@@ -46,25 +54,26 @@ class Value:
|
||||
|
||||
For example::
|
||||
|
||||
foo = conf.guild(some_guild).foo()
|
||||
foo = await conf.guild(some_guild).foo()
|
||||
|
||||
# Is equivalent to this
|
||||
|
||||
group_obj = conf.guild(some_guild)
|
||||
value_obj = conf.foo
|
||||
foo = value_obj()
|
||||
foo = await value_obj()
|
||||
|
||||
.. important::
|
||||
|
||||
This is now, for all intents and purposes, a coroutine.
|
||||
|
||||
:param default:
|
||||
This argument acts as an override for the registered default provided by :py:attr:`default`. This argument
|
||||
is ignored if its value is :python:`None`.
|
||||
:type default: Optional[object]
|
||||
:return:
|
||||
A coroutine object that must be awaited.
|
||||
"""
|
||||
driver = self.spawner.get_driver()
|
||||
try:
|
||||
ret = driver.get(self.identifiers)
|
||||
except KeyError:
|
||||
return default or self.default
|
||||
return ret
|
||||
return self._get(default)
|
||||
|
||||
async def set(self, value):
|
||||
"""
|
||||
@@ -182,7 +191,7 @@ class Group(Value):
|
||||
|
||||
return not isinstance(default, dict)
|
||||
|
||||
def get_attr(self, item: str, default=None, resolve=True):
|
||||
async def get_attr(self, item: str, default=None, resolve=True):
|
||||
"""
|
||||
This is available to use as an alternative to using normal Python attribute access. It is required if you find
|
||||
a need for dynamic attribute access.
|
||||
@@ -198,7 +207,7 @@ class Group(Value):
|
||||
user = ctx.author
|
||||
|
||||
# Where the value of item is the name of the data field in Config
|
||||
await ctx.send(self.conf.user(user).get_attr(item))
|
||||
await ctx.send(await self.conf.user(user).get_attr(item))
|
||||
|
||||
:param str item:
|
||||
The name of the data field in :py:class:`.Config`.
|
||||
@@ -211,20 +220,20 @@ class Group(Value):
|
||||
"""
|
||||
value = getattr(self, item)
|
||||
if resolve:
|
||||
return value(default=default)
|
||||
return await value(default=default)
|
||||
else:
|
||||
return value
|
||||
|
||||
def all(self) -> dict:
|
||||
async def all(self) -> dict:
|
||||
"""
|
||||
This method allows you to get "all" of a particular group of data. It will return the dictionary of all data
|
||||
for a particular Guild/Channel/Role/User/Member etc.
|
||||
|
||||
:rtype: dict
|
||||
"""
|
||||
return self()
|
||||
return await self()
|
||||
|
||||
def all_from_kind(self) -> dict:
|
||||
async def all_from_kind(self) -> dict:
|
||||
"""
|
||||
This method allows you to get all data from all entries in a given Kind. It will return a dictionary of Kind
|
||||
ID's -> data.
|
||||
@@ -232,7 +241,7 @@ class Group(Value):
|
||||
:rtype: dict
|
||||
"""
|
||||
# noinspection PyTypeChecker
|
||||
return self._super_group()
|
||||
return await self._super_group()
|
||||
|
||||
async def set(self, value):
|
||||
if not isinstance(value, dict):
|
||||
@@ -292,18 +301,18 @@ class MemberGroup(Group):
|
||||
)
|
||||
return group_obj
|
||||
|
||||
def all_guilds(self) -> dict:
|
||||
async def all_guilds(self) -> dict:
|
||||
"""
|
||||
Returns a dict of :code:`GUILD_ID -> MEMBER_ID -> data`.
|
||||
|
||||
:rtype: dict
|
||||
"""
|
||||
# noinspection PyTypeChecker
|
||||
return self._super_group()
|
||||
return await self._super_group()
|
||||
|
||||
def all(self) -> dict:
|
||||
async def all(self) -> dict:
|
||||
# noinspection PyTypeChecker
|
||||
return self._guild_group()
|
||||
return await self._guild_group()
|
||||
|
||||
|
||||
class Config:
|
||||
@@ -315,7 +324,7 @@ class Config:
|
||||
however the process for accessing global data is a bit different. There is no :python:`global` method
|
||||
because global data is accessed by normal attribute access::
|
||||
|
||||
conf.foo()
|
||||
await conf.foo()
|
||||
|
||||
.. py:attribute:: cog_name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user