annotate rhodecode/lib/__init__.py @ 1223:f7b24987d5fb beta

fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS, added docs for lib
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 06 Apr 2011 20:35:50 +0200
parents 2c246d24eb90
children 882ac77dc709
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
1 # -*- coding: utf-8 -*-
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
2 """
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
3 rhodecode.lib.__init__
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
5
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
6 Some simple helper functions
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
7
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
8 :created_on: Jan 5, 2011
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
9 :author: marcink
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
11 :license: GPLv3, see COPYING for more details.
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
12 """
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
13 # This program is free software: you can redistribute it and/or modify
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
14 # it under the terms of the GNU General Public License as published by
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
16 # (at your option) any later version.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
17 #
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
18 # This program is distributed in the hope that it will be useful,
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
21 # GNU General Public License for more details.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
22 #
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
23 # You should have received a copy of the GNU General Public License
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
25
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
26
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
27 def str2bool(_str):
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
28 """
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
29 returs True/False value from given string, it tries to translate the
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
30 string into boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
31
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
32 :param _str: string value to translate into boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
33 :rtype: boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
34 :returns: boolean from given string
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
35 """
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
36 if _str is None:
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
37 return False
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
38 if _str in (True, False):
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
39 return _str
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
40 _str = str(_str).strip().lower()
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
41 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
42
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
43
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
44 def generate_api_key(username, salt=None):
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
45 """
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
46 Generates unique API key for given username,if salt is not given
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
47 it'll be generated from some random string
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
48
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
49 :param username: username as string
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
50 :param salt: salt to hash generate KEY
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
51 :rtype: str
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
52 :returns: sha1 hash from username+salt
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
53 """
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
54 from tempfile import _RandomNameSequence
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
55 import hashlib
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
56
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
57 if salt is None:
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
58 salt = _RandomNameSequence().next()
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
59
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
60 return hashlib.sha1(username + salt).hexdigest()
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
61
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
62
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
63 def safe_unicode(_str, from_encoding='utf8'):
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
64 """
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
65 safe unicode function. In case of UnicodeDecode error we try to return
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
66 unicode with errors replace
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
67
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
68 :param _str: string to decode
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
69 :rtype: unicode
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
70 :returns: unicode object
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
71 """
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
72
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
73 if isinstance(_str, unicode):
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
74 return _str
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
75
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
76 try:
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
77 u_str = unicode(_str, from_encoding)
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
78 except UnicodeDecodeError:
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
79 u_str = unicode(_str, from_encoding, 'replace')
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
80
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
81 return u_str