-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translate the pre-restore backup comment
- Loading branch information
1 parent
66e9274
commit c7f1ba2
Showing
5 changed files
with
25 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |