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