|
2 | 2 |
|
3 | 3 | import gettext
|
4 | 4 | import locale
|
5 |
| -from collections import UserString, defaultdict |
| 5 | +from collections import defaultdict |
6 | 6 | from gettext import NullTranslations
|
7 | 7 | from os import path
|
8 | 8 | from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
|
9 | 9 |
|
10 | 10 |
|
11 |
| -class _TranslationProxy(UserString): |
| 11 | +class _TranslationProxy: |
12 | 12 | """
|
13 | 13 | Class for proxy strings from gettext translations. This is a helper for the
|
14 | 14 | lazy_* functions from this module.
|
15 | 15 |
|
16 | 16 | The proxy implementation attempts to be as complete as possible, so that
|
17 | 17 | the lazy objects should mostly work as expected, for example for sorting.
|
18 |
| -
|
19 |
| - This inherits from UserString because some docutils versions use UserString |
20 |
| - for their Text nodes, which then checks its argument for being either a |
21 |
| - basestring or UserString, otherwise calls str() -- not unicode() -- on it. |
22 | 18 | """
|
23 | 19 | __slots__ = ('_func', '_args')
|
24 | 20 |
|
25 |
| - def __new__(cls, func: Callable, *args: str) -> object: # type: ignore |
| 21 | + def __new__(cls, func: 'Callable', *args: str) -> '_TranslationProxy': |
26 | 22 | if not args:
|
27 | 23 | # not called with "function" and "arguments", but a plain string
|
28 |
| - return str(func) |
| 24 | + return str(func) # type: ignore[return-value] |
29 | 25 | return object.__new__(cls)
|
30 | 26 |
|
31 |
| - def __getnewargs__(self) -> Tuple[str]: |
| 27 | + def __getnewargs__(self) -> 'Tuple[str]': |
32 | 28 | return (self._func,) + self._args # type: ignore
|
33 | 29 |
|
34 |
| - def __init__(self, func: Callable, *args: str) -> None: |
| 30 | + def __init__(self, func: 'Callable', *args: str) -> None: |
35 | 31 | self._func = func
|
36 | 32 | self._args = args
|
37 | 33 |
|
38 |
| - @property |
39 |
| - def data(self) -> str: # type: ignore |
40 |
| - return self._func(*self._args) |
| 34 | + def __str__(self) -> str: |
| 35 | + return str(self._func(*self._args)) |
41 | 36 |
|
42 |
| - # replace function from UserString; it instantiates a self.__class__ |
43 |
| - # for the encoding result |
| 37 | + def __dir__(self) -> 'List[str]': |
| 38 | + return dir(str) |
44 | 39 |
|
45 |
| - def encode(self, encoding: str = None, errors: str = None) -> bytes: # type: ignore |
46 |
| - if encoding: |
47 |
| - if errors: |
48 |
| - return self.data.encode(encoding, errors) |
49 |
| - else: |
50 |
| - return self.data.encode(encoding) |
51 |
| - else: |
52 |
| - return self.data.encode() |
| 40 | + def __getattr__(self, name: str) -> Any: |
| 41 | + return getattr(self.__str__(), name) |
53 | 42 |
|
54 |
| - def __dir__(self) -> List[str]: |
55 |
| - return dir(str) |
| 43 | + def __getstate__(self) -> 'Tuple[Callable, Tuple[str, ...]]': |
| 44 | + return self._func, self._args |
56 | 45 |
|
57 |
| - def __str__(self) -> str: |
58 |
| - return str(self.data) |
| 46 | + def __setstate__(self, tup: 'Tuple[Callable, Tuple[str]]') -> None: |
| 47 | + self._func, self._args = tup |
| 48 | + |
| 49 | + def __copy__(self) -> '_TranslationProxy': |
| 50 | + return _TranslationProxy(self._func, *self._args) |
59 | 51 |
|
60 |
| - def __add__(self, other: str) -> str: # type: ignore |
61 |
| - return self.data + other |
| 52 | + def __repr__(self) -> str: |
| 53 | + try: |
| 54 | + return 'i' + repr(str(self.__str__())) |
| 55 | + except Exception: |
| 56 | + return f'<{self.__class__.__name__} broken>' |
62 | 57 |
|
63 |
| - def __radd__(self, other: str) -> str: # type: ignore |
64 |
| - return other + self.data |
| 58 | + def __add__(self, other: str) -> str: |
| 59 | + return self.__str__() + other |
65 | 60 |
|
66 |
| - def __mod__(self, other: str) -> str: # type: ignore |
67 |
| - return self.data % other |
| 61 | + def __radd__(self, other: str) -> str: |
| 62 | + return other + self.__str__() |
68 | 63 |
|
69 |
| - def __rmod__(self, other: str) -> str: # type: ignore |
70 |
| - return other % self.data |
| 64 | + def __mod__(self, other: str) -> str: |
| 65 | + return self.__str__() % other |
71 | 66 |
|
72 |
| - def __mul__(self, other: Any) -> str: # type: ignore |
73 |
| - return self.data * other |
| 67 | + def __rmod__(self, other: str) -> str: |
| 68 | + return other % self.__str__() |
74 | 69 |
|
75 |
| - def __rmul__(self, other: Any) -> str: # type: ignore |
76 |
| - return other * self.data |
| 70 | + def __mul__(self, other: Any) -> str: |
| 71 | + return self.__str__() * other |
77 | 72 |
|
78 |
| - def __getattr__(self, name: str) -> Any: |
79 |
| - if name == '__members__': |
80 |
| - return self.__dir__() |
81 |
| - return getattr(self.data, name) |
| 73 | + def __rmul__(self, other: Any) -> str: |
| 74 | + return other * self.__str__() |
82 | 75 |
|
83 |
| - def __getstate__(self) -> Tuple[Callable, Tuple[str, ...]]: |
84 |
| - return self._func, self._args |
| 76 | + def __hash__(self): |
| 77 | + return hash(self.__str__()) |
85 | 78 |
|
86 |
| - def __setstate__(self, tup: Tuple[Callable, Tuple[str]]) -> None: |
87 |
| - self._func, self._args = tup |
| 79 | + def __eq__(self, other): |
| 80 | + return self.__str__() == other |
88 | 81 |
|
89 |
| - def __copy__(self) -> "_TranslationProxy": |
90 |
| - return self |
| 82 | + def __lt__(self, string): |
| 83 | + return self.__str__() < string |
91 | 84 |
|
92 |
| - def __repr__(self) -> str: |
93 |
| - try: |
94 |
| - return 'i' + repr(str(self.data)) |
95 |
| - except Exception: |
96 |
| - return '<%s broken>' % self.__class__.__name__ |
| 85 | + def __contains__(self, char): |
| 86 | + return char in self.__str__() |
| 87 | + |
| 88 | + def __len__(self): |
| 89 | + return len(self.__str__()) |
| 90 | + |
| 91 | + def __getitem__(self, index): |
| 92 | + return self.__str__()[index] |
97 | 93 |
|
98 | 94 |
|
99 | 95 | translators: Dict[Tuple[str, str], NullTranslations] = defaultdict(NullTranslations)
|
|
0 commit comments