##// END OF EJS Templates
core: break down some utils for better imports
super-admin -
r4915:f87c218e default
parent child Browse files
Show More
@@ -0,0 +1,38 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import hashlib
22 from rhodecode.lib.str_utils import safe_bytes
23
24
25 def md5(s):
26 return hashlib.md5(s).hexdigest()
27
28
29 def md5_safe(s):
30 return md5(safe_bytes(s))
31
32
33 def sha1(s):
34 return hashlib.sha1(s).hexdigest()
35
36
37 def sha1_safe(s):
38 return sha1(safe_bytes(s))
@@ -0,0 +1,135 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import logging
22 import rhodecode
23 from rhodecode.lib.type_utils import aslist
24
25 log = logging.getLogger(__name__)
26
27
28 def safe_int(val, default=None) -> int:
29 """
30 Returns int() of val if val is not convertable to int use default
31 instead
32
33 :param val:
34 :param default:
35 """
36
37 try:
38 val = int(val)
39 except (ValueError, TypeError):
40 val = default
41
42 return val
43
44
45 def get_default_encodings():
46 return aslist(rhodecode.CONFIG.get('default_encoding', 'utf8'), sep=',')
47
48
49 def safe_str(str_, to_encoding=None) -> str:
50 """
51 safe str function. Does few trick to turn unicode_ into string
52
53 :param str_: str to encode
54 :param to_encoding: encode to this type UTF8 default
55 :rtype: str
56 :returns: str object
57 """
58 if isinstance(str_, str):
59 return str_
60
61 # if it's bytes cast to str
62 if not isinstance(str_, bytes):
63 return str(str_)
64
65 to_encoding = to_encoding or get_default_encodings()
66 if not isinstance(to_encoding, (list, tuple)):
67 to_encoding = [to_encoding]
68
69 for enc in to_encoding:
70 try:
71 return str(str_, enc)
72 except UnicodeDecodeError:
73 pass
74
75 return str(str_, to_encoding[0], 'replace')
76
77
78 def safe_bytes(str_, from_encoding=None) -> bytes:
79 """
80 safe bytes function. Does few trick to turn str_ into bytes string:
81
82 :param str_: string to decode
83 :param from_encoding: encode from this type UTF8 default
84 :rtype: unicode
85 :returns: unicode object
86 """
87 if isinstance(str_, bytes):
88 return str_
89
90 if not isinstance(str_, str):
91 raise ValueError('safe_bytes cannot convert other types than str: got: {}'.format(type(str_)))
92
93 from_encoding = from_encoding or get_default_encodings()
94 if not isinstance(from_encoding, (list, tuple)):
95 from_encoding = [from_encoding]
96
97 for enc in from_encoding:
98 try:
99 return str_.encode(enc)
100 except UnicodeDecodeError:
101 pass
102
103 return str_.encode(from_encoding[0], 'replace')
104
105
106 def ascii_bytes(str_, allow_bytes=False) -> bytes:
107 """
108 Simple conversion from str to bytes, with assumption that str_ is pure ASCII.
109 Fails with UnicodeError on invalid input.
110 This should be used where encoding and "safe" ambiguity should be avoided.
111 Where strings already have been encoded in other ways but still are unicode
112 string - for example to hex, base64, json, urlencoding, or are known to be
113 identifiers.
114 """
115 if allow_bytes and isinstance(str_, bytes):
116 return str_
117
118 if not isinstance(str_, str):
119 raise ValueError('ascii_bytes cannot convert other types than str: got: {}'.format(type(str_)))
120 return str_.encode('ascii')
121
122
123 def ascii_str(str_):
124 """
125 Simple conversion from bytes to str, with assumption that str_ is pure ASCII.
126 Fails with UnicodeError on invalid input.
127 This should be used where encoding and "safe" ambiguity should be avoided.
128 Where strings are encoded but also in other ways are known to be ASCII, and
129 where a unicode string is wanted without caring about encoding. For example
130 to hex, base64, urlencoding, or are known to be identifiers.
131 """
132
133 if not isinstance(str_, bytes):
134 raise ValueError('ascii_str cannot convert other types than bytes: got: {}'.format(type(str_)))
135 return str_.decode('ascii')
@@ -0,0 +1,61 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2011-2020 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import logging
22
23 log = logging.getLogger(__name__)
24
25
26 def str2bool(str_):
27 """
28 returns True/False value from given string, it tries to translate the
29 string into boolean
30
31 :param str_: string value to translate into boolean
32 :rtype: boolean
33 :returns: boolean from given string
34 """
35 if str_ is None:
36 return False
37 if str_ in (True, False):
38 return str_
39 str_ = str(str_).strip().lower()
40 return str_ in ('t', 'true', 'y', 'yes', 'on', '1')
41
42
43 def aslist(obj, sep=None, strip=True):
44 """
45 Returns given string separated by sep as list
46
47 :param obj:
48 :param sep:
49 :param strip:
50 """
51 if isinstance(obj, str):
52 lst = obj.split(sep)
53 if strip:
54 lst = [v.strip() for v in lst]
55 return lst
56 elif isinstance(obj, (list, tuple)):
57 return obj
58 elif obj is None:
59 return []
60 else:
61 return [obj]
@@ -26,7 +26,6 b' Some simple helper functions'
26 import collections
26 import collections
27 import datetime
27 import datetime
28 import dateutil.relativedelta
28 import dateutil.relativedelta
29 import hashlib
30 import logging
29 import logging
31 import re
30 import re
32 import sys
31 import sys
@@ -52,22 +51,12 b' from pyramid.settings import asbool'
52
51
53 import rhodecode
52 import rhodecode
54 from rhodecode.translation import _, _pluralize
53 from rhodecode.translation import _, _pluralize
55
54 from rhodecode.lib.str_utils import safe_str, safe_int, safe_bytes
56
55 from rhodecode.lib.hash_utils import md5, md5_safe, sha1, sha1_safe
57 def md5(s):
56 from rhodecode.lib.type_utils import aslist, str2bool
58 return hashlib.md5(s).hexdigest()
59
60
57
61 def md5_safe(s):
58 #TODO: there's no longer safe_unicode, we mock it now, but should remove it
62 return md5(safe_str(s))
59 safe_unicode = safe_str
63
64
65 def sha1(s):
66 return hashlib.sha1(s).hexdigest()
67
68
69 def sha1_safe(s):
70 return sha1(safe_str(s))
71
60
72
61
73 def __get_lem(extra_mapping=None):
62 def __get_lem(extra_mapping=None):
@@ -110,44 +99,6 b' def __get_lem(extra_mapping=None):'
110 return data
99 return data
111
100
112
101
113 def str2bool(_str):
114 """
115 returns True/False value from given string, it tries to translate the
116 string into boolean
117
118 :param _str: string value to translate into boolean
119 :rtype: boolean
120 :returns: boolean from given string
121 """
122 if _str is None:
123 return False
124 if _str in (True, False):
125 return _str
126 _str = str(_str).strip().lower()
127 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
128
129
130 def aslist(obj, sep=None, strip=True):
131 """
132 Returns given string separated by sep as list
133
134 :param obj:
135 :param sep:
136 :param strip:
137 """
138 if isinstance(obj, (basestring,)):
139 lst = obj.split(sep)
140 if strip:
141 lst = [v.strip() for v in lst]
142 return lst
143 elif isinstance(obj, (list, tuple)):
144 return obj
145 elif obj is None:
146 return []
147 else:
148 return [obj]
149
150
151 def convert_line_endings(line, mode):
102 def convert_line_endings(line, mode):
152 """
103 """
153 Converts a given line "line end" accordingly to given mode
104 Converts a given line "line end" accordingly to given mode
@@ -193,115 +144,6 b' def detect_mode(line, default):'
193 return default
144 return default
194
145
195
146
196 def safe_int(val, default=None):
197 """
198 Returns int() of val if val is not convertable to int use default
199 instead
200
201 :param val:
202 :param default:
203 """
204
205 try:
206 val = int(val)
207 except (ValueError, TypeError):
208 val = default
209
210 return val
211
212
213 def safe_unicode(str_, from_encoding=None, use_chardet=False):
214 """
215 safe unicode function. Does few trick to turn str_ into unicode
216
217 In case of UnicodeDecode error, we try to return it with encoding detected
218 by chardet library if it fails fallback to unicode with errors replaced
219
220 :param str_: string to decode
221 :rtype: unicode
222 :returns: unicode object
223 """
224 if isinstance(str_, unicode):
225 return str_
226
227 if not from_encoding:
228 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
229 'utf8'), sep=',')
230 from_encoding = DEFAULT_ENCODINGS
231
232 if not isinstance(from_encoding, (list, tuple)):
233 from_encoding = [from_encoding]
234
235 try:
236 return unicode(str_)
237 except UnicodeDecodeError:
238 pass
239
240 for enc in from_encoding:
241 try:
242 return unicode(str_, enc)
243 except UnicodeDecodeError:
244 pass
245
246 if use_chardet:
247 try:
248 import chardet
249 encoding = chardet.detect(str_)['encoding']
250 if encoding is None:
251 raise Exception()
252 return str_.decode(encoding)
253 except (ImportError, UnicodeDecodeError, Exception):
254 return unicode(str_, from_encoding[0], 'replace')
255 else:
256 return unicode(str_, from_encoding[0], 'replace')
257
258 def safe_str(unicode_, to_encoding=None, use_chardet=False):
259 """
260 safe str function. Does few trick to turn unicode_ into string
261
262 In case of UnicodeEncodeError, we try to return it with encoding detected
263 by chardet library if it fails fallback to string with errors replaced
264
265 :param unicode_: unicode to encode
266 :rtype: str
267 :returns: str object
268 """
269
270 # if it's not basestr cast to str
271 if not isinstance(unicode_, str):
272 return str(unicode_)
273
274 if isinstance(unicode_, str):
275 return unicode_
276
277 if not to_encoding:
278 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
279 'utf8'), sep=',')
280 to_encoding = DEFAULT_ENCODINGS
281
282 if not isinstance(to_encoding, (list, tuple)):
283 to_encoding = [to_encoding]
284
285 for enc in to_encoding:
286 try:
287 return unicode_.encode(enc)
288 except UnicodeEncodeError:
289 pass
290
291 if use_chardet:
292 try:
293 import chardet
294 encoding = chardet.detect(unicode_)['encoding']
295 if encoding is None:
296 raise UnicodeEncodeError()
297
298 return unicode_.encode(encoding)
299 except (ImportError, UnicodeEncodeError):
300 return unicode_.encode(to_encoding[0], 'replace')
301 else:
302 return unicode_.encode(to_encoding[0], 'replace')
303
304
305 def remove_suffix(s, suffix):
147 def remove_suffix(s, suffix):
306 if s.endswith(suffix):
148 if s.endswith(suffix):
307 s = s[:-1 * len(suffix)]
149 s = s[:-1 * len(suffix)]
General Comments 0
You need to be logged in to leave comments. Login now