plugin: save storage on edits

This commit is contained in:
dece 2022-08-15 13:35:42 +02:00
parent 448ee8477e
commit 4177730164
2 changed files with 11 additions and 5 deletions

View file

@ -6,7 +6,7 @@ import signal
from pathlib import Path from pathlib import Path
from typing import Any, Iterable, Optional from typing import Any, Iterable, Optional
import irc.client # type: ignore import irc.client
from irc.client import Connection, Event, NickMask from irc.client import Connection, Event, NickMask
from edmond.log import Logger from edmond.log import Logger
@ -23,7 +23,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.logger = logger self.logger = logger
self.plugins: list[Plugin] = [] self.plugins: list[Plugin] = []
self.values: dict[str, Any] = {} self.values: dict[str, Any] = {}
self.storage: dict[str, Any] = self.__get_storage() self.storage: dict[str, Any] = self.get_storage()
self.done: bool = False self.done: bool = False
@property @property
@ -45,7 +45,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
self.values[self.CHANNELS_RUNTIME_KEY] = [] self.values[self.CHANNELS_RUNTIME_KEY] = []
return self.values[self.CHANNELS_RUNTIME_KEY] return self.values[self.CHANNELS_RUNTIME_KEY]
def __get_storage(self) -> dict: def get_storage(self) -> dict:
"""Load data from storage.""" """Load data from storage."""
try: try:
with open(self.config["storage_file"], "rt") as storage_file: with open(self.config["storage_file"], "rt") as storage_file:
@ -60,7 +60,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
) )
return {} return {}
def __save_storage(self) -> None: def save_storage(self) -> None:
"""Save storage data to disk.""" """Save storage data to disk."""
try: try:
with open(self.config["storage_file"], "wt") as storage_file: with open(self.config["storage_file"], "wt") as storage_file:

View file

@ -63,6 +63,7 @@ class Plugin:
def __init__(self, bot): def __init__(self, bot):
from edmond.bot import Bot from edmond.bot import Bot
self.bot: Bot = bot self.bot: Bot = bot
# self.name is the plugin name, lowercased, without the Plugin suffix. # self.name is the plugin name, lowercased, without the Plugin suffix.
self.name: str = self.__class__.__name__.lower()[:-6] self.name: str = self.__class__.__name__.lower()[:-6]
@ -139,6 +140,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
self.bot.save_storage()
def append_storage_list_value(self, key: str, value: Any) -> None: def append_storage_list_value(self, key: str, value: Any) -> None:
"""Append a value to a list in the plugin persistent storage.""" """Append a value to a list in the plugin persistent storage."""
@ -148,6 +150,7 @@ class Plugin:
self.bot.storage[self.name][key] = [value] self.bot.storage[self.name][key] = [value]
else: else:
self.bot.storage[self.name][key].append(value) self.bot.storage[self.name][key].append(value)
self.bot.save_storage()
def remove_storage_list_value(self, key: str, value: Any) -> None: def remove_storage_list_value(self, key: str, value: Any) -> None:
"""Remove a value from a persistent storage list.""" """Remove a value from a persistent storage list."""
@ -156,6 +159,7 @@ class Plugin:
and key in self.bot.storage[self.name] and key in self.bot.storage[self.name]
): ):
self.bot.storage[self.name][key].remove(value) self.bot.storage[self.name][key].remove(value)
self.bot.save_storage()
def should_read_message(self, message: str) -> Optional[str]: def should_read_message(self, message: str) -> Optional[str]:
"""Return a message content if it has been addressed to me, else None. """Return a message content if it has been addressed to me, else None.
@ -308,7 +312,9 @@ class Plugin:
self.command = command self.command = command
self.bot.log_i(f"Processing command from p. {self.name}: {command}") self.bot.log_i(f"Processing command from p. {self.name}: {command}")
def respects_handling_conditions(self, exclude_conditions: Optional[dict] = None): def respects_handling_conditions(
self, exclude_conditions: Optional[dict] = None
):
"""Check if handling conditions are valid. """Check if handling conditions are valid.
Handling conditions can be specified by each plugin to create states in Handling conditions can be specified by each plugin to create states in