##// END OF EJS Templates
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
marcink -
r3008:6e76b489 beta
parent child Browse files
Show More
@@ -76,6 +76,8 b' use_gravatar = true'
76 76
77 77 container_auth_enabled = false
78 78 proxypass_auth_enabled = false
79 ## default encoding used to convert from and to unicode
80 ## can be also a comma seperated list of encoding in case of mixed encodings
79 81 default_encoding = utf8
80 82
81 83 ## overwrite schema of clone url
@@ -76,6 +76,8 b' use_gravatar = true'
76 76
77 77 container_auth_enabled = false
78 78 proxypass_auth_enabled = false
79 ## default encoding used to convert from and to unicode
80 ## can be also a comma seperated list of encoding in case of mixed encodings
79 81 default_encoding = utf8
80 82
81 83 ## overwrite schema of clone url
@@ -76,6 +76,8 b' use_gravatar = true'
76 76
77 77 container_auth_enabled = false
78 78 proxypass_auth_enabled = false
79 ## default encoding used to convert from and to unicode
80 ## can be also a comma seperated list of encoding in case of mixed encodings
79 81 default_encoding = utf8
80 82
81 83 ## overwrite schema of clone url
@@ -66,6 +66,7 b' def __get_lem():'
66 66
67 67 return dict(d)
68 68
69
69 70 def str2bool(_str):
70 71 """
71 72 returs True/False value from given string, it tries to translate the
@@ -83,6 +84,27 b' def str2bool(_str):'
83 84 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
84 85
85 86
87 def aslist(obj, sep=None, strip=True):
88 """
89 Returns given string separated by sep as list
90
91 :param obj:
92 :param sep:
93 :param strip:
94 """
95 if isinstance(obj, (basestring)):
96 lst = obj.split(sep)
97 if strip:
98 lst = [v.strip() for v in lst]
99 return lst
100 elif isinstance(obj, (list, tuple)):
101 return obj
102 elif obj is None:
103 return []
104 else:
105 return [obj]
106
107
86 108 def convert_line_endings(line, mode):
87 109 """
88 110 Converts a given line "line end" accordingly to given mode
@@ -182,18 +204,23 b' def safe_unicode(str_, from_encoding=Non'
182 204
183 205 if not from_encoding:
184 206 import rhodecode
185 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
186 from_encoding = DEFAULT_ENCODING
207 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
208 'utf8'), sep=',')
209 from_encoding = DEFAULT_ENCODINGS
210
211 if not isinstance(from_encoding, (list, tuple)):
212 from_encoding = [from_encoding]
187 213
188 214 try:
189 215 return unicode(str_)
190 216 except UnicodeDecodeError:
191 217 pass
192 218
193 try:
194 return unicode(str_, from_encoding)
195 except UnicodeDecodeError:
196 pass
219 for enc in from_encoding:
220 try:
221 return unicode(str_, enc)
222 except UnicodeDecodeError:
223 pass
197 224
198 225 try:
199 226 import chardet
@@ -202,7 +229,7 b' def safe_unicode(str_, from_encoding=Non'
202 229 raise Exception()
203 230 return str_.decode(encoding)
204 231 except (ImportError, UnicodeDecodeError, Exception):
205 return unicode(str_, from_encoding, 'replace')
232 return unicode(str_, from_encoding[0], 'replace')
206 233
207 234
208 235 def safe_str(unicode_, to_encoding=None):
@@ -226,13 +253,18 b' def safe_str(unicode_, to_encoding=None)'
226 253
227 254 if not to_encoding:
228 255 import rhodecode
229 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
230 to_encoding = DEFAULT_ENCODING
256 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
257 'utf8'), sep=',')
258 to_encoding = DEFAULT_ENCODINGS
231 259
232 try:
233 return unicode_.encode(to_encoding)
234 except UnicodeEncodeError:
235 pass
260 if not isinstance(to_encoding, (list, tuple)):
261 to_encoding = [to_encoding]
262
263 for enc in to_encoding:
264 try:
265 return unicode_.encode(enc)
266 except UnicodeEncodeError:
267 pass
236 268
237 269 try:
238 270 import chardet
@@ -242,7 +274,7 b' def safe_str(unicode_, to_encoding=None)'
242 274
243 275 return unicode_.encode(encoding)
244 276 except (ImportError, UnicodeEncodeError):
245 return unicode_.encode(to_encoding, 'replace')
277 return unicode_.encode(to_encoding[0], 'replace')
246 278
247 279 return safe_str
248 280
@@ -38,12 +38,12 b' def safe_unicode(str_, from_encoding=Non'
38 38 :rtype: unicode
39 39 :returns: unicode object
40 40 """
41 from rhodecode.lib.utils2 import safe_unicode
42 return safe_unicode(str_, from_encoding)
43
41 44 if isinstance(str_, unicode):
42 45 return str_
43 if not from_encoding:
44 import rhodecode
45 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding', 'utf8')
46 from_encoding = DEFAULT_ENCODING
46
47 47 try:
48 48 return unicode(str_)
49 49 except UnicodeDecodeError:
@@ -75,13 +75,12 b' def safe_str(unicode_, to_encoding=None)'
75 75 :rtype: str
76 76 :returns: str object
77 77 """
78 from rhodecode.lib.utils2 import safe_str
79 return safe_str(unicode_, to_encoding)
78 80
79 81 if isinstance(unicode_, str):
80 82 return unicode_
81 if not to_encoding:
82 import rhodecode
83 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding', 'utf8')
84 to_encoding = DEFAULT_ENCODING
83
85 84 try:
86 85 return unicode_.encode(to_encoding)
87 86 except UnicodeEncodeError:
General Comments 0
You need to be logged in to leave comments. Login now