Show More
@@ -15,6 +15,8 b'' | |||
|
15 | 15 | # along with this program; if not, write to the Free Software Foundation, |
|
16 | 16 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
17 | 17 | |
|
18 | import typing | |
|
19 | import base64 | |
|
18 | 20 | import logging |
|
19 | 21 | |
|
20 | 22 | |
@@ -38,14 +40,20 b' def safe_int(val, default=None) -> int:' | |||
|
38 | 40 | return val |
|
39 | 41 | |
|
40 | 42 | |
|
43 | def base64_to_str(text) -> str: | |
|
44 | return safe_str(base64.encodebytes(safe_bytes(text))).strip() | |
|
45 | ||
|
46 | ||
|
47 | def get_default_encodings() -> typing.List[str]: | |
|
48 | return ['utf8'] | |
|
49 | ||
|
50 | ||
|
41 | 51 | def safe_str(str_, to_encoding=None) -> str: |
|
42 | 52 | """ |
|
43 | 53 | safe str function. Does few trick to turn unicode_ into string |
|
44 | 54 | |
|
45 | 55 | :param str_: str to encode |
|
46 | 56 | :param to_encoding: encode to this type UTF8 default |
|
47 | :rtype: str | |
|
48 | :returns: str object | |
|
49 | 57 | """ |
|
50 | 58 | if isinstance(str_, str): |
|
51 | 59 | return str_ |
@@ -54,7 +62,7 b' def safe_str(str_, to_encoding=None) -> ' | |||
|
54 | 62 | if not isinstance(str_, bytes): |
|
55 | 63 | return str(str_) |
|
56 | 64 | |
|
57 |
to_encoding = to_encoding or |
|
|
65 | to_encoding = to_encoding or get_default_encodings() | |
|
58 | 66 | if not isinstance(to_encoding, (list, tuple)): |
|
59 | 67 | to_encoding = [to_encoding] |
|
60 | 68 | |
@@ -73,16 +81,14 b' def safe_bytes(str_, from_encoding=None)' | |||
|
73 | 81 | |
|
74 | 82 | :param str_: string to decode |
|
75 | 83 | :param from_encoding: encode from this type UTF8 default |
|
76 | :rtype: unicode | |
|
77 | :returns: unicode object | |
|
78 | 84 | """ |
|
79 | 85 | if isinstance(str_, bytes): |
|
80 | 86 | return str_ |
|
81 | 87 | |
|
82 | 88 | if not isinstance(str_, str): |
|
83 |
raise ValueError('safe_bytes cannot convert other types than str: got: { |
|
|
89 | raise ValueError(f'safe_bytes cannot convert other types than str: got: {type(str_)}') | |
|
84 | 90 | |
|
85 |
from_encoding = from_encoding or |
|
|
91 | from_encoding = from_encoding or get_default_encodings() | |
|
86 | 92 | if not isinstance(from_encoding, (list, tuple)): |
|
87 | 93 | from_encoding = [from_encoding] |
|
88 | 94 | |
@@ -108,11 +114,11 b' def ascii_bytes(str_, allow_bytes=False)' | |||
|
108 | 114 | return str_ |
|
109 | 115 | |
|
110 | 116 | if not isinstance(str_, str): |
|
111 |
raise ValueError('ascii_bytes cannot convert other types than str: got: { |
|
|
117 | raise ValueError(f'ascii_bytes cannot convert other types than str: got: {type(str_)}') | |
|
112 | 118 | return str_.encode('ascii') |
|
113 | 119 | |
|
114 | 120 | |
|
115 | def ascii_str(str_): | |
|
121 | def ascii_str(str_) -> str: | |
|
116 | 122 | """ |
|
117 | 123 | Simple conversion from bytes to str, with assumption that str_ is pure ASCII. |
|
118 | 124 | Fails with UnicodeError on invalid input. |
@@ -123,5 +129,5 b' def ascii_str(str_):' | |||
|
123 | 129 | """ |
|
124 | 130 | |
|
125 | 131 | if not isinstance(str_, bytes): |
|
126 |
raise ValueError('ascii_str cannot convert other types than bytes: got: { |
|
|
132 | raise ValueError(f'ascii_str cannot convert other types than bytes: got: {type(str_)}') | |
|
127 | 133 | return str_.decode('ascii') |
General Comments 0
You need to be logged in to leave comments.
Login now