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