##// 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 container_auth_enabled = false
77 container_auth_enabled = false
78 proxypass_auth_enabled = false
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 default_encoding = utf8
81 default_encoding = utf8
80
82
81 ## overwrite schema of clone url
83 ## overwrite schema of clone url
@@ -76,6 +76,8 b' use_gravatar = true'
76
76
77 container_auth_enabled = false
77 container_auth_enabled = false
78 proxypass_auth_enabled = false
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 default_encoding = utf8
81 default_encoding = utf8
80
82
81 ## overwrite schema of clone url
83 ## overwrite schema of clone url
@@ -76,6 +76,8 b' use_gravatar = true'
76
76
77 container_auth_enabled = false
77 container_auth_enabled = false
78 proxypass_auth_enabled = false
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 default_encoding = utf8
81 default_encoding = utf8
80
82
81 ## overwrite schema of clone url
83 ## overwrite schema of clone url
@@ -66,6 +66,7 b' def __get_lem():'
66
66
67 return dict(d)
67 return dict(d)
68
68
69
69 def str2bool(_str):
70 def str2bool(_str):
70 """
71 """
71 returs True/False value from given string, it tries to translate the
72 returs True/False value from given string, it tries to translate the
@@ -83,6 +84,27 b' def str2bool(_str):'
83 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
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 def convert_line_endings(line, mode):
108 def convert_line_endings(line, mode):
87 """
109 """
88 Converts a given line "line end" accordingly to given mode
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 if not from_encoding:
205 if not from_encoding:
184 import rhodecode
206 import rhodecode
185 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
207 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
186 from_encoding = 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 try:
214 try:
189 return unicode(str_)
215 return unicode(str_)
190 except UnicodeDecodeError:
216 except UnicodeDecodeError:
191 pass
217 pass
192
218
193 try:
219 for enc in from_encoding:
194 return unicode(str_, from_encoding)
220 try:
195 except UnicodeDecodeError:
221 return unicode(str_, enc)
196 pass
222 except UnicodeDecodeError:
223 pass
197
224
198 try:
225 try:
199 import chardet
226 import chardet
@@ -202,7 +229,7 b' def safe_unicode(str_, from_encoding=Non'
202 raise Exception()
229 raise Exception()
203 return str_.decode(encoding)
230 return str_.decode(encoding)
204 except (ImportError, UnicodeDecodeError, Exception):
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 def safe_str(unicode_, to_encoding=None):
235 def safe_str(unicode_, to_encoding=None):
@@ -226,13 +253,18 b' def safe_str(unicode_, to_encoding=None)'
226
253
227 if not to_encoding:
254 if not to_encoding:
228 import rhodecode
255 import rhodecode
229 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
256 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
230 to_encoding = DEFAULT_ENCODING
257 'utf8'), sep=',')
258 to_encoding = DEFAULT_ENCODINGS
231
259
232 try:
260 if not isinstance(to_encoding, (list, tuple)):
233 return unicode_.encode(to_encoding)
261 to_encoding = [to_encoding]
234 except UnicodeEncodeError:
262
235 pass
263 for enc in to_encoding:
264 try:
265 return unicode_.encode(enc)
266 except UnicodeEncodeError:
267 pass
236
268
237 try:
269 try:
238 import chardet
270 import chardet
@@ -242,7 +274,7 b' def safe_str(unicode_, to_encoding=None)'
242
274
243 return unicode_.encode(encoding)
275 return unicode_.encode(encoding)
244 except (ImportError, UnicodeEncodeError):
276 except (ImportError, UnicodeEncodeError):
245 return unicode_.encode(to_encoding, 'replace')
277 return unicode_.encode(to_encoding[0], 'replace')
246
278
247 return safe_str
279 return safe_str
248
280
@@ -38,12 +38,12 b' def safe_unicode(str_, from_encoding=Non'
38 :rtype: unicode
38 :rtype: unicode
39 :returns: unicode object
39 :returns: unicode object
40 """
40 """
41 from rhodecode.lib.utils2 import safe_unicode
42 return safe_unicode(str_, from_encoding)
43
41 if isinstance(str_, unicode):
44 if isinstance(str_, unicode):
42 return str_
45 return str_
43 if not from_encoding:
46
44 import rhodecode
45 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding', 'utf8')
46 from_encoding = DEFAULT_ENCODING
47 try:
47 try:
48 return unicode(str_)
48 return unicode(str_)
49 except UnicodeDecodeError:
49 except UnicodeDecodeError:
@@ -75,13 +75,12 b' def safe_str(unicode_, to_encoding=None)'
75 :rtype: str
75 :rtype: str
76 :returns: str object
76 :returns: str object
77 """
77 """
78 from rhodecode.lib.utils2 import safe_str
79 return safe_str(unicode_, to_encoding)
78
80
79 if isinstance(unicode_, str):
81 if isinstance(unicode_, str):
80 return unicode_
82 return unicode_
81 if not to_encoding:
83
82 import rhodecode
83 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding', 'utf8')
84 to_encoding = DEFAULT_ENCODING
85 try:
84 try:
86 return unicode_.encode(to_encoding)
85 return unicode_.encode(to_encoding)
87 except UnicodeEncodeError:
86 except UnicodeEncodeError:
General Comments 0
You need to be logged in to leave comments. Login now