plugin: save storage on edits
This commit is contained in:
parent
448ee8477e
commit
4177730164
|
@ -6,7 +6,7 @@ import signal
|
|||
from pathlib import Path
|
||||
from typing import Any, Iterable, Optional
|
||||
|
||||
import irc.client # type: ignore
|
||||
import irc.client
|
||||
from irc.client import Connection, Event, NickMask
|
||||
|
||||
from edmond.log import Logger
|
||||
|
@ -23,7 +23,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
|
|||
self.logger = logger
|
||||
self.plugins: list[Plugin] = []
|
||||
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
|
||||
|
||||
@property
|
||||
|
@ -45,7 +45,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
|
|||
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."""
|
||||
try:
|
||||
with open(self.config["storage_file"], "rt") as storage_file:
|
||||
|
@ -60,7 +60,7 @@ class Bot(irc.client.SimpleIRCClient, Logger):
|
|||
)
|
||||
return {}
|
||||
|
||||
def __save_storage(self) -> None:
|
||||
def save_storage(self) -> None:
|
||||
"""Save storage data to disk."""
|
||||
try:
|
||||
with open(self.config["storage_file"], "wt") as storage_file:
|
||||
|
|
|
@ -63,6 +63,7 @@ class Plugin:
|
|||
|
||||
def __init__(self, bot):
|
||||
from edmond.bot import Bot
|
||||
|
||||
self.bot: Bot = bot
|
||||
# self.name is the plugin name, lowercased, without the Plugin suffix.
|
||||
self.name: str = self.__class__.__name__.lower()[:-6]
|
||||
|
@ -139,6 +140,7 @@ class Plugin:
|
|||
self.bot.storage[name] = {key: value}
|
||||
else:
|
||||
self.bot.storage[name][key] = value
|
||||
self.bot.save_storage()
|
||||
|
||||
def append_storage_list_value(self, key: str, value: Any) -> None:
|
||||
"""Append a value to a list in the plugin persistent storage."""
|
||||
|
@ -148,6 +150,7 @@ class Plugin:
|
|||
self.bot.storage[self.name][key] = [value]
|
||||
else:
|
||||
self.bot.storage[self.name][key].append(value)
|
||||
self.bot.save_storage()
|
||||
|
||||
def remove_storage_list_value(self, key: str, value: Any) -> None:
|
||||
"""Remove a value from a persistent storage list."""
|
||||
|
@ -156,6 +159,7 @@ class Plugin:
|
|||
and key in self.bot.storage[self.name]
|
||||
):
|
||||
self.bot.storage[self.name][key].remove(value)
|
||||
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.
|
||||
|
@ -308,7 +312,9 @@ class Plugin:
|
|||
self.command = 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.
|
||||
|
||||
Handling conditions can be specified by each plugin to create states in
|
||||
|
|
Loading…
Reference in a new issue