##// END OF EJS Templates
helpers: unicode/str add flag to control usage of chardet....
marcink -
r4109:19a6ab7e default
parent child Browse files
Show More
@@ -207,7 +207,7 b' def safe_int(val, default=None):'
207 207 return val
208 208
209 209
210 def safe_unicode(str_, from_encoding=None):
210 def safe_unicode(str_, from_encoding=None, use_chardet=False):
211 211 """
212 212 safe unicode function. Does few trick to turn str_ into unicode
213 213
@@ -240,17 +240,19 b' def safe_unicode(str_, from_encoding=Non'
240 240 except UnicodeDecodeError:
241 241 pass
242 242
243 try:
244 import chardet
245 encoding = chardet.detect(str_)['encoding']
246 if encoding is None:
247 raise Exception()
248 return str_.decode(encoding)
249 except (ImportError, UnicodeDecodeError, Exception):
243 if use_chardet:
244 try:
245 import chardet
246 encoding = chardet.detect(str_)['encoding']
247 if encoding is None:
248 raise Exception()
249 return str_.decode(encoding)
250 except (ImportError, UnicodeDecodeError, Exception):
251 return unicode(str_, from_encoding[0], 'replace')
252 else:
250 253 return unicode(str_, from_encoding[0], 'replace')
251 254
252
253 def safe_str(unicode_, to_encoding=None):
255 def safe_str(unicode_, to_encoding=None, use_chardet=False):
254 256 """
255 257 safe str function. Does few trick to turn unicode_ into string
256 258
@@ -283,14 +285,17 b' def safe_str(unicode_, to_encoding=None)'
283 285 except UnicodeEncodeError:
284 286 pass
285 287
286 try:
287 import chardet
288 encoding = chardet.detect(unicode_)['encoding']
289 if encoding is None:
290 raise UnicodeEncodeError()
288 if use_chardet:
289 try:
290 import chardet
291 encoding = chardet.detect(unicode_)['encoding']
292 if encoding is None:
293 raise UnicodeEncodeError()
291 294
292 return unicode_.encode(encoding)
293 except (ImportError, UnicodeEncodeError):
295 return unicode_.encode(encoding)
296 except (ImportError, UnicodeEncodeError):
297 return unicode_.encode(to_encoding[0], 'replace')
298 else:
294 299 return unicode_.encode(to_encoding[0], 'replace')
295 300
296 301
General Comments 0
You need to be logged in to leave comments. Login now