##// END OF EJS Templates
utils: added few typing fixes to str_utils
super-admin -
r1105:24c9b8ea python3
parent child Browse files
Show More
@@ -1,127 +1,133 b''
1 # RhodeCode VCSServer provides access to different vcs backends via network.
1 # RhodeCode VCSServer provides access to different vcs backends via network.
2 # Copyright (C) 2014-2020 RhodeCode GmbH
2 # Copyright (C) 2014-2020 RhodeCode GmbH
3 #
3 #
4 # This program is free software; you can redistribute it and/or modify
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
7 # (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
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
21 log = logging.getLogger(__name__)
23 log = logging.getLogger(__name__)
22
24
23
25
24 def safe_int(val, default=None) -> int:
26 def safe_int(val, default=None) -> int:
25 """
27 """
26 Returns int() of val if val is not convertable to int use default
28 Returns int() of val if val is not convertable to int use default
27 instead
29 instead
28
30
29 :param val:
31 :param val:
30 :param default:
32 :param default:
31 """
33 """
32
34
33 try:
35 try:
34 val = int(val)
36 val = int(val)
35 except (ValueError, TypeError):
37 except (ValueError, TypeError):
36 val = default
38 val = default
37
39
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_
52
60
53 # if it's bytes cast to str
61 # if it's bytes cast to str
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
61 for enc in to_encoding:
69 for enc in to_encoding:
62 try:
70 try:
63 return str(str_, enc)
71 return str(str_, enc)
64 except UnicodeDecodeError:
72 except UnicodeDecodeError:
65 pass
73 pass
66
74
67 return str(str_, to_encoding[0], 'replace')
75 return str(str_, to_encoding[0], 'replace')
68
76
69
77
70 def safe_bytes(str_, from_encoding=None) -> bytes:
78 def safe_bytes(str_, from_encoding=None) -> bytes:
71 """
79 """
72 safe bytes function. Does few trick to turn str_ into bytes string:
80 safe bytes function. Does few trick to turn str_ into bytes string:
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
89 for enc in from_encoding:
95 for enc in from_encoding:
90 try:
96 try:
91 return str_.encode(enc)
97 return str_.encode(enc)
92 except UnicodeDecodeError:
98 except UnicodeDecodeError:
93 pass
99 pass
94
100
95 return str_.encode(from_encoding[0], 'replace')
101 return str_.encode(from_encoding[0], 'replace')
96
102
97
103
98 def ascii_bytes(str_, allow_bytes=False) -> bytes:
104 def ascii_bytes(str_, allow_bytes=False) -> bytes:
99 """
105 """
100 Simple conversion from str to bytes, with assumption that str_ is pure ASCII.
106 Simple conversion from str to bytes, with assumption that str_ is pure ASCII.
101 Fails with UnicodeError on invalid input.
107 Fails with UnicodeError on invalid input.
102 This should be used where encoding and "safe" ambiguity should be avoided.
108 This should be used where encoding and "safe" ambiguity should be avoided.
103 Where strings already have been encoded in other ways but still are unicode
109 Where strings already have been encoded in other ways but still are unicode
104 string - for example to hex, base64, json, urlencoding, or are known to be
110 string - for example to hex, base64, json, urlencoding, or are known to be
105 identifiers.
111 identifiers.
106 """
112 """
107 if allow_bytes and isinstance(str_, bytes):
113 if allow_bytes and isinstance(str_, bytes):
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.
119 This should be used where encoding and "safe" ambiguity should be avoided.
125 This should be used where encoding and "safe" ambiguity should be avoided.
120 Where strings are encoded but also in other ways are known to be ASCII, and
126 Where strings are encoded but also in other ways are known to be ASCII, and
121 where a unicode string is wanted without caring about encoding. For example
127 where a unicode string is wanted without caring about encoding. For example
122 to hex, base64, urlencoding, or are known to be identifiers.
128 to hex, base64, urlencoding, or are known to be identifiers.
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