Mercurial > kallithea
comparison rhodecode/lib/vcs/utils/__init__.py @ 2016:6020e3884a58 beta
implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
default utf8 to some other encoding.
- also added instance-id to config
- update ini files
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 22 Feb 2012 04:30:26 +0200 |
parents | 324ac367a4da |
children | 7e979933ffec |
comparison
equal
deleted
inserted
replaced
2015:5415d0de5970 | 2016:6020e3884a58 |
---|---|
25 """ | 25 """ |
26 | 26 |
27 return datetime.datetime.fromtimestamp(float(unixts)) | 27 return datetime.datetime.fromtimestamp(float(unixts)) |
28 | 28 |
29 | 29 |
30 def safe_unicode(str_, from_encoding='utf8'): | 30 def safe_unicode(str_, from_encoding=None): |
31 """ | 31 """ |
32 safe unicode function. Does few trick to turn str_ into unicode | 32 safe unicode function. Does few trick to turn str_ into unicode |
33 | 33 |
34 In case of UnicodeDecode error we try to return it with encoding detected | 34 In case of UnicodeDecode error we try to return it with encoding detected |
35 by chardet library if it fails fallback to unicode with errors replaced | 35 by chardet library if it fails fallback to unicode with errors replaced |
38 :rtype: unicode | 38 :rtype: unicode |
39 :returns: unicode object | 39 :returns: unicode object |
40 """ | 40 """ |
41 if isinstance(str_, unicode): | 41 if isinstance(str_, unicode): |
42 return str_ | 42 return str_ |
43 | 43 if not from_encoding: |
44 import rhodecode | |
45 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8') | |
46 from_encoding = DEFAULT_ENCODING | |
44 try: | 47 try: |
45 return unicode(str_) | 48 return unicode(str_) |
46 except UnicodeDecodeError: | 49 except UnicodeDecodeError: |
47 pass | 50 pass |
48 | 51 |
59 return str_.decode(encoding) | 62 return str_.decode(encoding) |
60 except (ImportError, UnicodeDecodeError, Exception): | 63 except (ImportError, UnicodeDecodeError, Exception): |
61 return unicode(str_, from_encoding, 'replace') | 64 return unicode(str_, from_encoding, 'replace') |
62 | 65 |
63 | 66 |
64 def safe_str(unicode_, to_encoding='utf8'): | 67 def safe_str(unicode_, to_encoding=None): |
65 """ | 68 """ |
66 safe str function. Does few trick to turn unicode_ into string | 69 safe str function. Does few trick to turn unicode_ into string |
67 | 70 |
68 In case of UnicodeEncodeError we try to return it with encoding detected | 71 In case of UnicodeEncodeError we try to return it with encoding detected |
69 by chardet library if it fails fallback to string with errors replaced | 72 by chardet library if it fails fallback to string with errors replaced |
73 :returns: str object | 76 :returns: str object |
74 """ | 77 """ |
75 | 78 |
76 if isinstance(unicode_, str): | 79 if isinstance(unicode_, str): |
77 return unicode_ | 80 return unicode_ |
78 | 81 if not to_encoding: |
82 import rhodecode | |
83 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8') | |
84 to_encoding = DEFAULT_ENCODING | |
79 try: | 85 try: |
80 return unicode_.encode(to_encoding) | 86 return unicode_.encode(to_encoding) |
81 except UnicodeEncodeError: | 87 except UnicodeEncodeError: |
82 pass | 88 pass |
83 | 89 |