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, key: str,
value: Any, value: Any,
ns: Optional[str] = None, ns: Optional[str] = None,
skip_save: bool = False,
) -> None: ) -> None:
"""Set a value in the plugin persistent storage.""" """Set a value in the plugin persistent storage."""
name = ns or self.name name = ns or self.name
@ -169,6 +170,7 @@ class Plugin:
self.bot.storage[name] = {key: value} self.bot.storage[name] = {key: value}
else: else:
self.bot.storage[name][key] = value self.bot.storage[name][key] = value
if not skip_save:
self.bot.save_storage() self.bot.save_storage()
def append_storage_list_value( def append_storage_list_value(
@ -176,6 +178,7 @@ class Plugin:
key: str, key: str,
value: Any, value: Any,
ns: str = None, ns: str = None,
skip_save: bool = False,
) -> None: ) -> None:
"""Append a value to a list in the plugin persistent storage.""" """Append a value to a list in the plugin persistent storage."""
name = ns or self.name name = ns or self.name
@ -185,6 +188,7 @@ class Plugin:
self.bot.storage[name][key] = [value] self.bot.storage[name][key] = [value]
else: else:
self.bot.storage[name][key].append(value) self.bot.storage[name][key].append(value)
if not skip_save:
self.bot.save_storage() self.bot.save_storage()
def remove_storage_list_value( def remove_storage_list_value(
@ -192,11 +196,13 @@ class Plugin:
key: str, key: str,
value: Any, value: Any,
ns: Optional[str] = None, ns: Optional[str] = None,
skip_save: bool = False,
) -> None: ) -> None:
"""Remove a value from a persistent storage list.""" """Remove a value from a persistent storage list."""
name = ns or self.name name = ns or self.name
if name in self.bot.storage and key in self.bot.storage[name]: if name in self.bot.storage and key in self.bot.storage[name]:
self.bot.storage[name][key].remove(value) self.bot.storage[name][key].remove(value)
if not skip_save:
self.bot.save_storage() self.bot.save_storage()
def should_read_message(self, message: str) -> Optional[str]: def should_read_message(self, message: str) -> Optional[str]: