diff --git a/prime_backup/mcdr/command/commands.py b/prime_backup/mcdr/command/commands.py index 4297293..44621fd 100644 --- a/prime_backup/mcdr/command/commands.py +++ b/prime_backup/mcdr/command/commands.py @@ -36,6 +36,13 @@ from prime_backup.mcdr.task.general.show_help_task import ShowHelpTask from prime_backup.mcdr.task.general.show_welcome_task import ShowWelcomeTask from prime_backup.mcdr.task_manager import TaskManager +from prime_backup.mcdr.events import ( + TRIGGER_BACKUP_EVENT, + TRIGGER_RESTORE_EVENT, + TRIGGER_DELETE_EVENT, + TRIGGER_IMPORT_EVENT, + TRIGGER_EXPORT_EVENT, +) from prime_backup.types.backup_filter import BackupFilter from prime_backup.types.backup_tags import BackupTagName from prime_backup.types.hash_method import HashMethod @@ -45,7 +52,6 @@ from prime_backup.utils.mcdr_utils import tr, reply_message, mkcmd from prime_backup.utils.waitable_value import WaitableValue - class CommandManager: def __init__(self, server: PluginServerInterface, task_manager: TaskManager, crontab_manager: CrontabManager): self.server = server @@ -424,3 +430,47 @@ def make_tag_cmd() -> Literal: # --------------- register --------------- self.server.register_command(root) + + def register_event_listeners(self, server: PluginServerInterface): + server.register_event_listener( + TRIGGER_BACKUP_EVENT, + lambda svr, source, comment, operator: self.task_manager.add_task(CreateBackupTask(source, comment=comment, operator=operator)) + ) + server.register_event_listener( + TRIGGER_RESTORE_EVENT, + lambda svr, source, backup_id, needs_confirm=False, fail_soft=False, verify_blob=True: self.task_manager.add_task( + RestoreBackupTask(source, backup_id, needs_confirm=needs_confirm, fail_soft=fail_soft, verify_blob=verify_blob) + ) + ) + server.register_event_listener( + TRIGGER_DELETE_EVENT, + lambda svr, source, backup_ids: self.task_manager.add_task( + DeleteBackupTask(source, backup_ids) + ), + ) + server.register_event_listener( + TRIGGER_IMPORT_EVENT, + lambda svr, source, file_path, backup_format, ensure_meta, meta_override: self.task_manager.add_task( + ImportBackupTask( + source, + file_path, + backup_format=backup_format, + ensure_meta=ensure_meta, + meta_override=meta_override, + ) + ), + ) + server.register_event_listener( + TRIGGER_EXPORT_EVENT, + lambda svr, source, backup_id, export_format, fail_soft, verify_blob, overwrite_existing, create_meta: self.task_manager.add_task( + ExportBackupTask( + source, + backup_id, + export_format=export_format, + fail_soft=fail_soft, + verify_blob=verify_blob, + overwrite_existing=overwrite_existing, + create_meta=create_meta, + ) + ), + ) diff --git a/prime_backup/mcdr/events.py b/prime_backup/mcdr/events.py new file mode 100644 index 0000000..15b960e --- /dev/null +++ b/prime_backup/mcdr/events.py @@ -0,0 +1,15 @@ +from mcdreforged.api.event import LiteralEvent + +from prime_backup.constants import PLUGIN_ID + +# events +BACKUP_DONE_EVENT = LiteralEvent('{}.backup_done'.format(PLUGIN_ID)) # -> source, backup_id +RESTORE_DONE_EVENT = LiteralEvent('{}.restore_done'.format(PLUGIN_ID)) # -> source, backup_id +DELETE_DONE_EVENT = LiteralEvent('{}.delete_done'.format(PLUGIN_ID)) # -> source, backup_id +EXPORT_DONE_EVENT = LiteralEvent('{}.export_done'.format(PLUGIN_ID)) # -> source, backup_id, file_path +IMPORT_DONE_EVENT = LiteralEvent('{}.import_done'.format(PLUGIN_ID)) # -> source, backup_id +TRIGGER_BACKUP_EVENT = LiteralEvent('{}.trigger_backup'.format(PLUGIN_ID)) # <- source, comment, operator +TRIGGER_RESTORE_EVENT = LiteralEvent('{}.trigger_restore'.format(PLUGIN_ID)) # <- source, backup_id, needs_confirm, fail_soft, verify_blob +TRIGGER_DELETE_EVENT = LiteralEvent('{}.trigger_delete'.format(PLUGIN_ID)) # <- source, [backup_id] +TRIGGER_EXPORT_EVENT = LiteralEvent('{}.trigger_export'.format(PLUGIN_ID)) # <- source, backup_id, export_format, fail_soft, verify_blob, overwrite_existing, create_meta +TRIGGER_IMPORT_EVENT = LiteralEvent('{}.trigger_import'.format(PLUGIN_ID)) # <- source, file_path, backup_format, ensure_meta, meta_override \ No newline at end of file diff --git a/prime_backup/mcdr/task/backup/create_backup_task.py b/prime_backup/mcdr/task/backup/create_backup_task.py index 516f2ab..6a514c4 100644 --- a/prime_backup/mcdr/task/backup/create_backup_task.py +++ b/prime_backup/mcdr/task/backup/create_backup_task.py @@ -4,6 +4,7 @@ from mcdreforged.api.all import * from prime_backup.action.create_backup_action import CreateBackupAction +from prime_backup.mcdr.events import BACKUP_DONE_EVENT from prime_backup.mcdr.task import TaskEvent from prime_backup.mcdr.task.basic_task import HeavyTask from prime_backup.mcdr.text_components import TextComponents @@ -66,6 +67,8 @@ def run(self): TextComponents.backup_size(backup), TextComponents.blob_list_summary_store_size(bls), )) + + self.server.dispatch_event(BACKUP_DONE_EVENT, (self.source, backup.id)) finally: if applied_auto_save_off and len(cmds.auto_save_on) > 0: self.server.execute(cmds.auto_save_on) diff --git a/prime_backup/mcdr/task/backup/delete_backup_task.py b/prime_backup/mcdr/task/backup/delete_backup_task.py index 52a264b..d3d5045 100644 --- a/prime_backup/mcdr/task/backup/delete_backup_task.py +++ b/prime_backup/mcdr/task/backup/delete_backup_task.py @@ -4,6 +4,7 @@ from prime_backup.action.delete_backup_action import DeleteBackupAction from prime_backup.action.get_backup_action import GetBackupAction +from prime_backup.mcdr.events import DELETE_DONE_EVENT from prime_backup.mcdr.task.basic_task import HeavyTask from prime_backup.mcdr.text_components import TextComponents from prime_backup.utils import collection_utils @@ -42,3 +43,5 @@ def run(self): TextComponents.backup_id(backup_id, hover=False, click=False), TextComponents.blob_list_summary_store_size(dr.bls), ) + + self.server.dispatch_event(DELETE_DONE_EVENT, (self.source, self.backup_ids)) \ No newline at end of file diff --git a/prime_backup/mcdr/task/backup/export_backup_task.py b/prime_backup/mcdr/task/backup/export_backup_task.py index 4703058..fdfc03e 100644 --- a/prime_backup/mcdr/task/backup/export_backup_task.py +++ b/prime_backup/mcdr/task/backup/export_backup_task.py @@ -4,6 +4,7 @@ from prime_backup.action.export_backup_action import ExportBackupToZipAction, ExportBackupToTarAction from prime_backup.action.get_backup_action import GetBackupAction +from prime_backup.mcdr.events import EXPORT_DONE_EVENT from prime_backup.mcdr.task.basic_task import HeavyTask from prime_backup.mcdr.text_components import TextComponents from prime_backup.types.standalone_backup_format import ZipFormat, StandaloneBackupFormat @@ -78,3 +79,5 @@ def make_output(extension: str) -> Path: self.reply_tr('failures', len(failures)) for line in failures.to_lines(): self.reply(line) + + self.server.dispatch_event(EXPORT_DONE_EVENT, (self.source, backup.id, path.absolute().as_posix())) \ No newline at end of file diff --git a/prime_backup/mcdr/task/backup/import_backup_task.py b/prime_backup/mcdr/task/backup/import_backup_task.py index fe24be1..a5bec61 100644 --- a/prime_backup/mcdr/task/backup/import_backup_task.py +++ b/prime_backup/mcdr/task/backup/import_backup_task.py @@ -4,6 +4,7 @@ from mcdreforged.api.all import * from prime_backup.action.import_backup_action import ImportBackupAction, BackupMetadataNotFound +from prime_backup.mcdr.events import IMPORT_DONE_EVENT from prime_backup.mcdr import mcdr_globals from prime_backup.mcdr.task.basic_task import HeavyTask from prime_backup.mcdr.text_components import TextComponents, TextColors @@ -49,3 +50,5 @@ def run(self) -> None: self.reply_tr('backup_metadata_not_found.suggestion', name=mcdr_globals.metadata.name) else: self.reply_tr('done', t_fp, TextComponents.backup_id(backup)) + + self.server.dispatch_event(IMPORT_DONE_EVENT, (self.source, backup.id)) \ No newline at end of file diff --git a/prime_backup/mcdr/task/backup/restore_backup_task.py b/prime_backup/mcdr/task/backup/restore_backup_task.py index 8f84bb2..cc61e7d 100644 --- a/prime_backup/mcdr/task/backup/restore_backup_task.py +++ b/prime_backup/mcdr/task/backup/restore_backup_task.py @@ -6,6 +6,7 @@ from prime_backup.action.export_backup_action import ExportBackupToDirectoryAction from prime_backup.action.get_backup_action import GetBackupAction from prime_backup.action.list_backup_action import ListBackupAction +from prime_backup.mcdr.events import RESTORE_DONE_EVENT from prime_backup.mcdr.task.basic_task import HeavyTask from prime_backup.mcdr.text_components import TextComponents from prime_backup.types.backup_filter import BackupFilter @@ -117,3 +118,5 @@ def run(self): logger.info('{} restored world to backup #{} (date={}, comment={!r}), pre-restore temp backup: {}'.format( self.source, backup.id, backup.date_str, backup.comment, pre_restore_backup_id, )) + + self.server.dispatch_event(RESTORE_DONE_EVENT, (self.source, backup.id)) \ No newline at end of file diff --git a/prime_backup/types/backup_info.py b/prime_backup/types/backup_info.py index 18b82a8..d6766a5 100644 --- a/prime_backup/types/backup_info.py +++ b/prime_backup/types/backup_info.py @@ -37,6 +37,9 @@ def date(self) -> datetime.datetime: def date_str(self) -> str: return conversion_utils.timestamp_to_local_date_str(self.timestamp_ns) + def to_dict(self) -> dict: + return dataclasses.asdict(self) + @classmethod def of(cls, backup: schema.Backup, *, with_files: bool = False) -> 'Self': """ @@ -53,4 +56,4 @@ def of(cls, backup: schema.Backup, *, with_files: bool = False) -> 'Self': raw_size=backup.file_raw_size_sum or 0, stored_size=backup.file_stored_size_sum or 0, files=list(map(FileInfo.of, backup.files)) if with_files else [], - ) + ) \ No newline at end of file