[V3 Config] Adjust functionality of get_attr (#1342)

* Intermediate commit

* Add defaulting stuff to config

* Remove set_attr in favor of set_raw

* Modify get_attr

* Fix issue with clearing data
This commit is contained in:
Will
2018-02-26 15:13:01 -05:00
committed by palmtree5
parent c428982c00
commit 64af7800dc
5 changed files with 54 additions and 81 deletions

View File

@@ -267,17 +267,13 @@ class Group(Value):
return not isinstance(default, dict)
def get_attr(self, item: str, default=None, resolve=True):
def get_attr(self, item: str):
"""Manually get an attribute of this Group.
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. It may be required if you find a need for dynamic
attribute access.
Note
----
Use of this method should be avoided wherever possible.
Example
-------
A possible use case::
@@ -287,32 +283,20 @@ class Group(Value):
user = ctx.author
# Where the value of item is the name of the data field in Config
await ctx.send(await self.conf.user(user).get_attr(item))
await ctx.send(await self.conf.user(user).get_attr(item).foo())
Parameters
----------
item : str
The name of the data field in `Config`.
default
This is an optional override to the registered default for this
item.
resolve : bool
If this is :code:`True` this function will return a coroutine that
resolves to a "real" data value when awaited. If :code:`False`,
this method acts the same as `__getattr__`.
Returns
-------
`types.coroutine` or `Value` or `Group`
The attribute which was requested, its type depending on the value
of :code:`resolve`.
`Value` or `Group`
The attribute which was requested.
"""
value = getattr(self, item)
if resolve:
return value(default=default)
else:
return value
return self.__getattr__(item)
async def get_raw(self, *nested_path: str, default=...):
"""
@@ -350,6 +334,16 @@ class Group(Value):
"""
path = [str(p) for p in nested_path]
if default is ...:
poss_default = self.defaults
for ident in path:
try:
poss_default = poss_default[ident]
except KeyError:
break
else:
default = poss_default
try:
return deepcopy(await self.driver.get(*self.identifiers, *path))
except KeyError:
@@ -398,27 +392,6 @@ class Group(Value):
)
await super().set(value)
async def set_attr(self, item: str, value):
"""Set an attribute by its name.
Similar to `get_attr` in the way it can be used to dynamically set
attributes by name.
Note
----
Use of this method should be avoided wherever possible.
Parameters
----------
item : str
The name of the attribute being set.
value
The raw data value to set the attribute as.
"""
value_obj = getattr(self, item)
await value_obj.set(value)
async def set_raw(self, *nested_path: str, value):
"""
Allows a developer to set data as if it was stored in a standard

View File

@@ -77,8 +77,8 @@ class Case:
case_emb = await self.message_content()
await self.message.edit(embed=case_emb)
await _conf.guild(self.guild).cases.set_attr(
str(self.case_number), self.to_json()
await _conf.guild(self.guild).cases.set_raw(
str(self.case_number), value=self.to_json()
)
async def message_content(self):
@@ -245,7 +245,7 @@ class CaseType:
"case_str": self.case_str,
"audit_type": self.audit_type
}
await _conf.casetypes.set_attr(self.name, data)
await _conf.casetypes.set_raw(self.name, value=data)
async def is_enabled(self) -> bool:
"""
@@ -262,8 +262,8 @@ class CaseType:
"""
if not self.guild:
return False
return await _conf.guild(self.guild).casetypes.get_attr(self.name,
self.default_setting)
return await _conf.guild(self.guild).casetypes.get_raw(
self.name, default=self.default_setting)
async def set_enabled(self, enabled: bool):
"""
@@ -275,7 +275,7 @@ class CaseType:
True if the case should be enabled, otherwise False"""
if not self.guild:
return
await _conf.guild(self.guild).casetypes.set_attr(self.name, enabled)
await _conf.guild(self.guild).casetypes.set_raw(self.name, value=enabled)
@classmethod
def from_json(cls, data: dict):
@@ -310,7 +310,7 @@ async def get_next_case_number(guild: discord.Guild) -> str:
"""
cases = sorted(
(await _conf.guild(guild).get_attr("cases")),
(await _conf.guild(guild).get_raw("cases")),
key=lambda x: int(x),
reverse=True
)
@@ -342,11 +342,12 @@ async def get_case(case_number: int, guild: discord.Guild,
If there is no case for the specified number
"""
case = await _conf.guild(guild).cases.get_attr(str(case_number))
if case is None:
try:
case = await _conf.guild(guild).cases.get_raw(str(case_number))
except KeyError as e:
raise RuntimeError(
"That case does not exist for guild {}".format(guild.name)
)
) from e
mod_channel = await get_modlog_channel(guild)
return await Case.from_json(mod_channel, bot, case)
@@ -368,7 +369,7 @@ async def get_all_cases(guild: discord.Guild, bot: Red) -> List[Case]:
A list of all cases for the guild
"""
cases = await _conf.guild(guild).get_attr("cases")
cases = await _conf.guild(guild).get_raw("cases")
case_numbers = list(cases.keys())
case_list = []
for case in case_numbers:
@@ -440,7 +441,7 @@ async def create_case(guild: discord.Guild, created_at: datetime, action_type: s
case_emb = await case.message_content()
msg = await mod_channel.send(embed=case_emb)
case.message = msg
await _conf.guild(guild).cases.set_attr(str(next_case_number), case.to_json())
await _conf.guild(guild).cases.set_raw(str(next_case_number), value=case.to_json())
return case
@@ -459,7 +460,7 @@ async def get_casetype(name: str, guild: discord.Guild=None) -> Union[CaseType,
-------
CaseType or None
"""
casetypes = await _conf.get_attr("casetypes")
casetypes = await _conf.get_raw("casetypes")
if name in casetypes:
data = casetypes[name]
data["name"] = name
@@ -480,7 +481,7 @@ async def get_all_casetypes(guild: discord.Guild=None) -> List[CaseType]:
A list of case types
"""
casetypes = await _conf.get_attr("casetypes")
casetypes = await _conf.get_raw("casetypes", default={})
typelist = []
for ct in casetypes.keys():
data = casetypes[ct]