plugin: allow plugins to skip write of storage v.

Plugins may want to write several values at once and skip the first
writes to only save the values at the last call.
This commit is contained in:
dece 2022-09-09 18:11:44 +02:00
parent 255cdcaac2
commit e601a77f72

View file

@ -162,6 +162,7 @@ class Plugin:
key: str,
value: Any,
ns: Optional[str] = None,
skip_save: bool = False,
) -> None:
"""Set a value in the plugin persistent storage."""
name = ns or self.name
@ -169,13 +170,15 @@ class Plugin:
self.bot.storage[name] = {key: value}
else:
self.bot.storage[name][key] = value
self.bot.save_storage()
if not skip_save:
self.bot.save_storage()
def append_storage_list_value(
self,
key: str,
value: Any,
ns: str = None,
skip_save: bool = False,
) -> None:
"""Append a value to a list in the plugin persistent storage."""
name = ns or self.name
@ -185,19 +188,22 @@ class Plugin:
self.bot.storage[name][key] = [value]
else:
self.bot.storage[name][key].append(value)
self.bot.save_storage()
if not skip_save:
self.bot.save_storage()
def remove_storage_list_value(
self,
key: str,
value: Any,
ns: Optional[str] = None,
skip_save: bool = False,
) -> None:
"""Remove a value from a persistent storage list."""
name = ns or self.name
if name in self.bot.storage and key in self.bot.storage[name]:
self.bot.storage[name][key].remove(value)
self.bot.save_storage()
if not skip_save:
self.bot.save_storage()
def should_read_message(self, message: str) -> Optional[str]:
"""Return a message content if it has been addressed to me, else None.