Mercurial > kallithea
comparison rhodecode/lib/__init__.py @ 1228:73434499fa72
merges for stable
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 09 Apr 2011 11:22:32 +0200 |
parents | 3d9da7893fdb |
children | bf263968da47 |
comparison
equal
deleted
inserted
replaced
1227:2182a2005278 | 1228:73434499fa72 |
---|---|
21 # GNU General Public License for more details. | 21 # GNU General Public License for more details. |
22 # | 22 # |
23 # You should have received a copy of the GNU General Public License | 23 # You should have received a copy of the GNU General Public License |
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 24 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
25 | 25 |
26 def str2bool(s): | 26 |
27 if s is None: | 27 def str2bool(_str): |
28 """ | |
29 returs True/False value from given string, it tries to translate the | |
30 string into boolean | |
31 | |
32 :param _str: string value to translate into boolean | |
33 :rtype: boolean | |
34 :returns: boolean from given string | |
35 """ | |
36 if _str is None: | |
28 return False | 37 return False |
29 if s in (True, False): | 38 if _str in (True, False): |
30 return s | 39 return _str |
31 s = str(s).strip().lower() | 40 _str = str(_str).strip().lower() |
32 return s in ('t', 'true', 'y', 'yes', 'on', '1') | 41 return _str in ('t', 'true', 'y', 'yes', 'on', '1') |
42 | |
33 | 43 |
34 def generate_api_key(username, salt=None): | 44 def generate_api_key(username, salt=None): |
35 """ | 45 """ |
36 Generates uniq API key for given username | 46 Generates unique API key for given username,if salt is not given |
47 it'll be generated from some random string | |
37 | 48 |
38 :param username: username as string | 49 :param username: username as string |
39 :param salt: salt to hash generate KEY | 50 :param salt: salt to hash generate KEY |
51 :rtype: str | |
52 :returns: sha1 hash from username+salt | |
40 """ | 53 """ |
41 from tempfile import _RandomNameSequence | 54 from tempfile import _RandomNameSequence |
42 import hashlib | 55 import hashlib |
43 | 56 |
44 if salt is None: | 57 if salt is None: |
45 salt = _RandomNameSequence().next() | 58 salt = _RandomNameSequence().next() |
46 | 59 |
47 return hashlib.sha1(username + salt).hexdigest() | 60 return hashlib.sha1(username + salt).hexdigest() |
48 | 61 |
49 def safe_unicode(_str): | 62 |
63 def safe_unicode(_str, from_encoding='utf8'): | |
50 """ | 64 """ |
51 safe unicode function. In case of UnicodeDecode error we try to return | 65 safe unicode function. In case of UnicodeDecode error we try to return |
52 unicode with errors replace, if this fails we return unicode with | 66 unicode with errors replace |
53 string_escape decoding | 67 |
68 :param _str: string to decode | |
69 :rtype: unicode | |
70 :returns: unicode object | |
54 """ | 71 """ |
55 | 72 |
56 if isinstance(_str, unicode): | 73 if isinstance(_str, unicode): |
57 return _str | 74 return _str |
58 | 75 |
59 try: | 76 try: |
60 u_str = unicode(_str) | 77 u_str = unicode(_str, from_encoding) |
61 except UnicodeDecodeError: | 78 except UnicodeDecodeError: |
62 try: | 79 u_str = unicode(_str, from_encoding, 'replace') |
63 u_str = _str.decode('utf-8', 'replace') | |
64 except UnicodeDecodeError: | |
65 #incase we have a decode error just represent as byte string | |
66 u_str = unicode(_str.encode('string_escape')) | |
67 | 80 |
68 return u_str | 81 return u_str |