Marco Ricci commited on 2025-01-13 14:09:49
Zeige 1 geänderte Dateien mit 24 Einfügungen und 24 Löschungen.
This is getting annoying to type, and error-prone to update.
... | ... |
@@ -15,7 +15,9 @@ import os |
15 | 15 |
import sys |
16 | 16 |
import textwrap |
17 | 17 |
import types |
18 |
-from typing import TYPE_CHECKING, NamedTuple, TextIO, cast |
|
18 |
+from typing import TYPE_CHECKING, NamedTuple, TextIO, Union, cast |
|
19 |
+ |
|
20 |
+from typing_extensions import TypeAlias |
|
19 | 21 |
|
20 | 22 |
import derivepassphrase as dpp |
21 | 23 |
|
... | ... |
@@ -151,19 +153,13 @@ class TranslatedString: |
151 | 153 |
template: ( |
152 | 154 |
str |
153 | 155 |
| TranslatableString |
154 |
- | Label |
|
155 |
- | DebugMsgTemplate |
|
156 |
- | InfoMsgTemplate |
|
157 |
- | WarnMsgTemplate |
|
158 |
- | ErrMsgTemplate |
|
156 |
+ | MsgTemplate |
|
159 | 157 |
), |
160 | 158 |
args_dict: Mapping[str, Any] = types.MappingProxyType({}), |
161 | 159 |
/, |
162 | 160 |
**kwargs: Any, # noqa: ANN401 |
163 | 161 |
) -> None: |
164 |
- if isinstance( |
|
165 |
- template, (Label, DebugMsgTemplate, InfoMsgTemplate, WarnMsgTemplate, ErrMsgTemplate) |
|
166 |
- ): |
|
162 |
+ if isinstance(template, MSG_TEMPLATE_CLASSES): |
|
167 | 163 |
template = cast('TranslatableString', template.value) |
168 | 164 |
self.template = template |
169 | 165 |
self.kwargs = {**args_dict, **kwargs} |
... | ... |
@@ -1667,6 +1663,22 @@ class ErrMsgTemplate(enum.Enum): |
1667 | 1663 |
) |
1668 | 1664 |
|
1669 | 1665 |
|
1666 |
+MsgTemplate: TypeAlias = Union[ |
|
1667 |
+ Label, |
|
1668 |
+ DebugMsgTemplate, |
|
1669 |
+ InfoMsgTemplate, |
|
1670 |
+ WarnMsgTemplate, |
|
1671 |
+ ErrMsgTemplate, |
|
1672 |
+] |
|
1673 |
+MSG_TEMPLATE_CLASSES = ( |
|
1674 |
+ Label, |
|
1675 |
+ DebugMsgTemplate, |
|
1676 |
+ InfoMsgTemplate, |
|
1677 |
+ WarnMsgTemplate, |
|
1678 |
+ ErrMsgTemplate, |
|
1679 |
+) |
|
1680 |
+ |
|
1681 |
+ |
|
1670 | 1682 |
def _write_pot_file(fileobj: TextIO) -> None: # pragma: no cover |
1671 | 1683 |
r"""Write a .po template to the given file object. |
1672 | 1684 |
|
... | ... |
@@ -1680,20 +1692,8 @@ def _write_pot_file(fileobj: TextIO) -> None: # pragma: no cover |
1680 | 1692 |
.po header are hard-coded, as is the source filename. |
1681 | 1693 |
|
1682 | 1694 |
""" # noqa: DOC501 |
1683 |
- entries: dict[ |
|
1684 |
- str, |
|
1685 |
- dict[ |
|
1686 |
- str, |
|
1687 |
- Label | DebugMsgTemplate | InfoMsgTemplate | WarnMsgTemplate | ErrMsgTemplate, |
|
1688 |
- ], |
|
1689 |
- ] = {} |
|
1690 |
- for enum_class in ( |
|
1691 |
- Label, |
|
1692 |
- DebugMsgTemplate, |
|
1693 |
- InfoMsgTemplate, |
|
1694 |
- WarnMsgTemplate, |
|
1695 |
- ErrMsgTemplate, |
|
1696 |
- ): |
|
1695 |
+ entries: dict[str, dict[str, MsgTemplate]] = {} |
|
1696 |
+ for enum_class in MSG_TEMPLATE_CLASSES: |
|
1697 | 1697 |
for member in enum_class.__members__.values(): |
1698 | 1698 |
ctx = member.value.l10n_context |
1699 | 1699 |
msg = member.value.singular |
... | ... |
@@ -1739,7 +1739,7 @@ def _write_pot_file(fileobj: TextIO) -> None: # pragma: no cover |
1739 | 1739 |
|
1740 | 1740 |
|
1741 | 1741 |
def _format_po_entry( |
1742 |
- enum_value: Label | DebugMsgTemplate | InfoMsgTemplate | WarnMsgTemplate | ErrMsgTemplate, |
|
1742 |
+ enum_value: MsgTemplate, |
|
1743 | 1743 |
) -> tuple[str, ...]: # pragma: no cover |
1744 | 1744 |
ret: list[str] = ['\n'] |
1745 | 1745 |
ts = enum_value.value |
1746 | 1746 |