| 664 | raise ValueError("Invalid date") |
| 665 | |
| 666 | def _replace_tokens(self, token: str, locale: Locale) -> str: |
| 667 | if token.startswith("[") and token.endswith("]"): |
| 668 | return token[1:-1] |
| 669 | elif token.startswith("\\"): |
| 670 | if len(token) == 2 and token[1] in {"[", "]"}: |
| 671 | return "" |
| 672 | |
| 673 | return token |
| 674 | elif token not in self._REGEX_TOKENS and token not in self._LOCALIZABLE_TOKENS: |
| 675 | raise ValueError(f"Unsupported token: {token}") |
| 676 | |
| 677 | if token in self._LOCALIZABLE_TOKENS: |
| 678 | values = self._LOCALIZABLE_TOKENS[token] |
| 679 | if callable(values): |
| 680 | candidates = values(locale) |
| 681 | else: |
| 682 | candidates = tuple( |
| 683 | locale.translation( |
| 684 | cast("str", self._LOCALIZABLE_TOKENS[token]) |
| 685 | ).values() |
| 686 | ) |
| 687 | else: |
| 688 | candidates = cast("Sequence[str]", self._REGEX_TOKENS[token]) |
| 689 | |
| 690 | if not candidates: |
| 691 | raise ValueError(f"Unsupported token: {token}") |
| 692 | |
| 693 | if not isinstance(candidates, tuple): |
| 694 | candidates = (cast("str", candidates),) |
| 695 | |
| 696 | pattern = f"(?P<{token}>{'|'.join(candidates)})" |
| 697 | |
| 698 | return pattern |