annotate rhodecode/lib/__init__.py @ 1199:268fa0b6b2ef beta

Added os.sep in models for better win support fixed safe unicode function to not use str as param, and skip execution on unicode
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 02 Apr 2011 21:19:16 +0200
parents 36fe593dfe4b
children 6832ef664673
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
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
7
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
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
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 """
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
13 # This program is free software; you can redistribute it and/or
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
14 # modify it under the terms of the GNU General Public License
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
15 # as published by the Free Software Foundation; version 2
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
16 # of the License or (at your opinion) any later version of the license.
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
17 #
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.
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
22 #
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
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
24 # along with this program; if not, write to the Free Software
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
26 # MA 02110-1301, USA.
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
27
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
28 def str2bool(s):
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
29 if s is None:
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
30 return False
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
31 if s in (True, False):
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
32 return s
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
33 s = str(s).strip().lower()
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
34 return s in ('t', 'true', 'y', 'yes', 'on', '1')
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
35
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
36 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
37 """
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
38 Generates uniq API key for given username
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
39
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
40 :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
41 :param salt: salt to hash generate KEY
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
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 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
44 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
45
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
46 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
47 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
48
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
49 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
50
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
51 def safe_unicode(_str):
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
52 """
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 safe unicode function. In case of UnicodeDecode error we try to return
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
54 unicode with errors replace, if this fails we return unicode with
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
55 string_escape decoding
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
56 """
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
57
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
58 if isinstance(_str, unicode):
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
59 return _str
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
60
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 try:
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
62 u_str = unicode(_str)
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
63 except UnicodeDecodeError:
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 try:
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
65 u_str = _str.decode('utf-8', '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
66 except UnicodeDecodeError:
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
67 #incase we have a decode error just represent as byte string
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
68 u_str = unicode(_str.encode('string_escape'))
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
69
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
70 return u_str