annotate rhodecode/lib/__init__.py @ 1219:ca614631c2f8

fixed tests for stable
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 05 Apr 2011 20:09:34 +0200
parents 3d9da7893fdb
children 73434499fa72
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
1 # -*- coding: utf-8 -*-
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
2 """
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
3 rhodecode.lib.__init__
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
5
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
6 Some simple helper functions
1152
008bdfdd95c8 fixed str2bool typo
Marcin Kuzminski <marcin@python-works.com>
parents: 1136
diff changeset
7
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
8 :created_on: Jan 5, 2011
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
9 :author: marcink
1152
008bdfdd95c8 fixed str2bool typo
Marcin Kuzminski <marcin@python-works.com>
parents: 1136
diff changeset
10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
11 :license: GPLv3, see COPYING for more details.
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
12 """
1218
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
13 # This program is free software: you can redistribute it and/or modify
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
14 # it under the terms of the GNU General Public License as published by
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
16 # (at your option) any later version.
1152
008bdfdd95c8 fixed str2bool typo
Marcin Kuzminski <marcin@python-works.com>
parents: 1136
diff changeset
17 #
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
18 # This program is distributed in the hope that it will be useful,
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
21 # GNU General Public License for more details.
1152
008bdfdd95c8 fixed str2bool typo
Marcin Kuzminski <marcin@python-works.com>
parents: 1136
diff changeset
22 #
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
23 # You should have received a copy of the GNU General Public License
1218
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
922
b2a70582bce3 fixed base.html. and missing lib str2bool function
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
25
1218
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
26 def str2bool(s):
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
27 if s is None:
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
28 return False
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
29 if s in (True, False):
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
30 return s
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
31 s = str(s).strip().lower()
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
32 return s in ('t', 'true', 'y', 'yes', 'on', '1')
1136
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
33
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
34 def generate_api_key(username, salt=None):
1218
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
35 """
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
36 Generates uniq API key for given username
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
37
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
38 :param username: username as string
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
39 :param salt: salt to hash generate KEY
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
40 """
1136
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
41 from tempfile import _RandomNameSequence
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
42 import hashlib
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
43
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
44 if salt is None:
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
45 salt = _RandomNameSequence().next()
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
46
93b980ebee55 changes for release 1.1.5
Marcin Kuzminski <marcin@python-works.com>
parents: 922
diff changeset
47 return hashlib.sha1(username + salt).hexdigest()
1218
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
48
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
49 def safe_unicode(_str):
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
50 """
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
51 safe unicode function. In case of UnicodeDecode error we try to return
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
52 unicode with errors replace, if this fails we return unicode with
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
53 string_escape decoding
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
54 """
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
55
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
56 if isinstance(_str, unicode):
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
57 return _str
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
58
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
59 try:
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
60 u_str = unicode(_str)
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
61 except UnicodeDecodeError:
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
62 try:
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
63 u_str = _str.decode('utf-8', 'replace')
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
64 except UnicodeDecodeError:
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
65 #incase we have a decode error just represent as byte string
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
66 u_str = unicode(_str.encode('string_escape'))
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
67
3d9da7893fdb fixes for stable
Marcin Kuzminski <marcin@python-works.com>
parents: 1152
diff changeset
68 return u_str