From c7f1ba2d8306f83966b495112b3ac70683cbf673 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Tue, 12 Dec 2023 21:40:02 +0800 Subject: [PATCH] translate the pre-restore backup comment --- lang/en_us.yml | 2 +- lang/zh_cn.yml | 2 +- .../mcdr/task/backup/restore_backup_task.py | 3 ++- prime_backup/mcdr/text_components.py | 7 ++++-- prime_backup/utils/backup_utils.py | 22 ++++++++++++++----- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lang/en_us.yml b/lang/en_us.yml index dd41bb7..2014765 100644 --- a/lang/en_us.yml +++ b/lang/en_us.yml @@ -89,7 +89,6 @@ prime_backup: countdown: '{} second later, the world will §cbe restored§r to {}' countdown.hover: Click me or enter command {} to abort restore no_confirm: No choice has been made, restore task aborted - pre_restore_comment: 'Automatic backup before restoring to #{}' aborted: Restore task aborted backup_set_tag: name: set backup tag @@ -380,6 +379,7 @@ prime_backup: backup_comment: none: no comment scheduled_backup: Scheduled Backup + pre_restore: 'Automatic backup before restoring to #{}' backup_full: creator: 'Creator: {}' restore: Restore to backup {} diff --git a/lang/zh_cn.yml b/lang/zh_cn.yml index 6283815..4aab68a 100644 --- a/lang/zh_cn.yml +++ b/lang/zh_cn.yml @@ -89,7 +89,6 @@ prime_backup: countdown: '{}秒后将§c回档§r至{}' countdown.hover: 点我或输入{}以终止回档 no_confirm: 未做出选择, 回档任务中止 - pre_restore_comment: '回档至#{}前的自动备份' aborted: 回档任务中止 backup_set_tag: name: 设置备份标签 @@ -380,6 +379,7 @@ prime_backup: backup_comment: none: 空 scheduled_backup: 定时备份 + pre_restore: '回档至#{}前的自动备份' backup_full: creator: '创建者: {}' restore: 回档至备份{} diff --git a/prime_backup/mcdr/task/backup/restore_backup_task.py b/prime_backup/mcdr/task/backup/restore_backup_task.py index f2eb713..56e6d3d 100644 --- a/prime_backup/mcdr/task/backup/restore_backup_task.py +++ b/prime_backup/mcdr/task/backup/restore_backup_task.py @@ -12,6 +12,7 @@ from prime_backup.types.backup_info import BackupInfo from prime_backup.types.backup_tags import BackupTags, BackupTagName from prime_backup.types.operator import Operator, PrimeBackupOperatorNames +from prime_backup.utils import backup_utils from prime_backup.utils.mcdr_utils import click_and_run, mkcmd from prime_backup.utils.timer import Timer @@ -79,7 +80,7 @@ def run(self): self.logger.info('Creating backup of existing files to avoid idiot') CreateBackupAction( Operator.pb(PrimeBackupOperatorNames.pre_restore), - self.tr('pre_restore_comment', backup.id).to_plain_text(), + backup_utils.create_translated_backup_comment('pre_restore', backup.id), tags=BackupTags().set(BackupTagName.pre_restore_backup, True), ).run() cost_backup = timer.get_and_restart() diff --git a/prime_backup/mcdr/text_components.py b/prime_backup/mcdr/text_components.py index 7cc1e7e..34b1027 100644 --- a/prime_backup/mcdr/text_components.py +++ b/prime_backup/mcdr/text_components.py @@ -63,8 +63,11 @@ def backup_brief(cls, backup: BackupInfo, *, backup_id_fancy: bool = True) -> RT @classmethod def backup_comment(cls, comment: str) -> RTextBase: if len(comment) > 0: - if (key := backup_utils.extract_backup_comment_translation_key(comment)) is not None: - return cls.tr(f'backup_comment.{key}') + if (er := backup_utils.extract_backup_comment_translation_key(comment)) is not None: + args = er.args + if er.key == 'pre_restore' and len(args) == 0: + args = ('?',) + return cls.tr(f'backup_comment.{er.key}', *args) return RText(comment) else: return cls.tr('backup_comment.none').set_color(RColor.gray).set_styles(RStyle.italic) diff --git a/prime_backup/utils/backup_utils.py b/prime_backup/utils/backup_utils.py index 68d7bdc..43f184e 100644 --- a/prime_backup/utils/backup_utils.py +++ b/prime_backup/utils/backup_utils.py @@ -1,18 +1,28 @@ import re -from typing import Optional - +from typing import Optional, NamedTuple, Tuple _PATTERN_WORDS = re.compile(r'\w+') _PATTERN_EXTRACT = re.compile(r'__pb_translated__:(\w+)') +_PATTERN_EXTRACT_WITH_ARGS = re.compile(r'__pb_translated__:(\w+):(.*)') -def create_translated_backup_comment(key: str): +def create_translated_backup_comment(key: str, *args) -> str: if not _PATTERN_WORDS.fullmatch(key): raise ValueError(key) - return f'__pb_translated__:{key}' + comment = f'__pb_translated__:{key}' + if len(args) > 0: + comment += ':' + '|'.join(map(str, args)) + return comment + + +class ExtractResult(NamedTuple): + key: str + args: Tuple[str, ...] -def extract_backup_comment_translation_key(comment: str) -> Optional[None]: +def extract_backup_comment_translation_key(comment: str) -> Optional[ExtractResult]: if (match := _PATTERN_EXTRACT.fullmatch(comment)) is not None: - return match.group(1) + return ExtractResult(match.group(1), ()) + if (match := _PATTERN_EXTRACT_WITH_ARGS.fullmatch(comment)) is not None: + return ExtractResult(match.group(1), tuple(match.group(2).split('|'))) return None