annotate rhodecode/lib/utils2.py @ 2109:8ecfed1d8f8b beta

utils/conf - created temporary utils2 - made config.conf for storing some configurations - fixed some dependency import problems - code cleanup - rc-extensions now properly work for celery
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 07 Mar 2012 02:18:22 +0200
parents
children ea5ff843b200
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 rhodecode.lib.utils
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 ~~~~~~~~~~~~~~~~~~~
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Some simple helper functions
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 :created_on: Jan 5, 2011
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 :author: marcink
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 :license: GPLv3, see COPYING for more details.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 # This program is free software: you can redistribute it and/or modify
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 # it under the terms of the GNU General Public License as published by
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 # (at your option) any later version.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 #
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 # This program is distributed in the hope that it will be useful,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 # GNU General Public License for more details.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 #
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 # You should have received a copy of the GNU General Public License
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26 import re
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 from rhodecode.lib.vcs.utils.lazy import LazyProperty
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 def __get_lem():
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 Get language extension map based on what's inside pygments lexers
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 from pygments import lexers
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 from string import lower
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 from collections import defaultdict
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38 d = defaultdict(lambda: [])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 def __clean(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 s = s.lstrip('*')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 s = s.lstrip('.')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 if s.find('[') != -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 exts = []
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46 start, stop = s.find('['), s.find(']')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 for suffix in s[start + 1:stop]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 exts.append(s[:s.find('[')] + suffix)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 return map(lower, exts)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 return map(lower, [s])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 for lx, t in sorted(lexers.LEXERS.items()):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 m = map(__clean, t[-2])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56 if m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 m = reduce(lambda x, y: x + y, m)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 for ext in m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 desc = lx.replace('Lexer', '')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 d[ext].append(desc)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 return dict(d)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 def str2bool(_str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 returs True/False value from given string, it tries to translate the
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 string into boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 :param _str: string value to translate into boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 :rtype: boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 :returns: boolean from given string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 if _str is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 return False
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 if _str in (True, False):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 return _str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 _str = str(_str).strip().lower()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 def convert_line_endings(line, mode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 Converts a given line "line end" accordingly to given mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 Available modes are::
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 0 - Unix
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 1 - Mac
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 2 - DOS
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 :param line: given line to convert
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 :param mode: mode to convert to
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 :return: converted line according to mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 from string import replace
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 if mode == 0:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 line = replace(line, '\r\n', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 line = replace(line, '\r', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 elif mode == 1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 line = replace(line, '\r\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 line = replace(line, '\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 elif mode == 2:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 return line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 def detect_mode(line, default):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 Detects line break for given line, if line break couldn't be found
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 given default value is returned
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 :param line: str line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 :param default: default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 :rtype: int
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 if line.endswith('\r\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 return 2
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 elif line.endswith('\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 return 0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 elif line.endswith('\r'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 return 1
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 return default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 def generate_api_key(username, salt=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 Generates unique API key for given username, if salt is not given
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 it'll be generated from some random string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 :param username: username as string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 :param salt: salt to hash generate KEY
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 :returns: sha1 hash from username+salt
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 from tempfile import _RandomNameSequence
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 import hashlib
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 if salt is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 salt = _RandomNameSequence().next()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 return hashlib.sha1(username + salt).hexdigest()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 def safe_unicode(str_, from_encoding=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 safe unicode function. Does few trick to turn str_ into unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 In case of UnicodeDecode error we try to return it with encoding detected
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 by chardet library if it fails fallback to unicode with errors replaced
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 :param str_: string to decode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 :returns: unicode object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 if isinstance(str_, unicode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 return str_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 if not from_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 import rhodecode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 from_encoding = DEFAULT_ENCODING
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 return unicode(str_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 except UnicodeDecodeError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 pass
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 return unicode(str_, from_encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 except UnicodeDecodeError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 pass
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 encoding = chardet.detect(str_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 raise Exception()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181 return str_.decode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 except (ImportError, UnicodeDecodeError, Exception):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 return unicode(str_, from_encoding, 'replace')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 def safe_str(unicode_, to_encoding=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 safe str function. Does few trick to turn unicode_ into string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 In case of UnicodeEncodeError we try to return it with encoding detected
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191 by chardet library if it fails fallback to string with errors replaced
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 :param unicode_: unicode to encode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 :returns: str object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 # if it's not basestr cast to str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 if not isinstance(unicode_, basestring):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 return str(unicode_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 if isinstance(unicode_, str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 return unicode_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 if not to_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 import rhodecode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 to_encoding = DEFAULT_ENCODING
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 return unicode_.encode(to_encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 except UnicodeEncodeError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 pass
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217 encoding = chardet.detect(unicode_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 print encoding
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 raise UnicodeEncodeError()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 return unicode_.encode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 except (ImportError, UnicodeEncodeError):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 return unicode_.encode(to_encoding, 'replace')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226 return safe_str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
227
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
228
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
229 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 Custom engine_from_config functions that makes sure we use NullPool for
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 file based sqlite databases. This prevents errors on sqlite. This only
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 applies to sqlalchemy versions < 0.7.0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 import sqlalchemy
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 from sqlalchemy import engine_from_config as efc
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 import logging
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
240 if int(sqlalchemy.__version__.split('.')[1]) < 7:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
242 # This solution should work for sqlalchemy < 0.7.0, and should use
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
243 # proxy=TimerProxy() for execution time profiling
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 from sqlalchemy.pool import NullPool
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246 url = configuration[prefix + 'url']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 if url.startswith('sqlite'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 kwargs.update({'poolclass': NullPool})
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250 return efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 import time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 from sqlalchemy import event
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254 from sqlalchemy.engine import Engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 log = logging.getLogger('sqlalchemy.engine')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 engine = efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 def color_sql(sql):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261 COLOR_SEQ = "\033[1;%dm"
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 COLOR_SQL = YELLOW
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 normal = '\x1b[0m'
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
264 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
265
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
266 if configuration['debug']:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267 #attach events only for debug configuration
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
268
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
269 def before_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
270 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
271 context._query_start_time = time.time()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272 log.info(color_sql(">>>>> STARTING QUERY >>>>>"))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 def after_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 total = time.time() - context._query_start_time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 event.listen(engine, "before_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 before_cursor_execute)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282 event.listen(engine, "after_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 after_cursor_execute)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285 return engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
288 def age(curdate):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
289 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
290 turns a datetime into an age string.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
291
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
292 :param curdate: datetime object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
293 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
294 :returns: unicode words describing age
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
295 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
296
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
297 from datetime import datetime
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 from webhelpers.date import time_ago_in_words
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 _ = lambda s: s
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 if not curdate:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 return ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305 agescales = [(_(u"year"), 3600 * 24 * 365),
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 (_(u"month"), 3600 * 24 * 30),
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 (_(u"day"), 3600 * 24),
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 (_(u"hour"), 3600),
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 (_(u"minute"), 60),
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310 (_(u"second"), 1), ]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 age = datetime.now() - curdate
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 age_seconds = (age.days * agescales[2][1]) + age.seconds
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 pos = 1
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 for scale in agescales:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 if scale[1] <= age_seconds:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 if pos == 6:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 pos = 5
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 return '%s %s' % (time_ago_in_words(curdate,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 agescales[pos][0]), _('ago'))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 pos += 1
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 return _(u'just now')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 def uri_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 Removes user:password from given url string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 :returns: filtered list of strings
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 if not uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 return ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 proto = ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 for pat in ('https://', 'http://'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 if uri.startswith(pat):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 uri = uri[len(pat):]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342 proto = pat
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 break
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 # remove passwords and username
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 uri = uri[uri.find('@') + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 # get the port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 cred_pos = uri.find(':')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 if cred_pos == -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 host, port = uri, None
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 host, port = uri[:cred_pos], uri[cred_pos + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355 return filter(None, [proto, host, port])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358 def credentials_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 Returns a url with removed credentials
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
361
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
362 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
363 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365 uri = uri_filter(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 #check if we have port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
367 if len(uri) > 2 and uri[2]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
368 uri[2] = ':' + uri[2]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
369
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
370 return ''.join(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
371
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
372
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
373 def get_changeset_safe(repo, rev):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
374 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
375 Safe version of get_changeset if this changeset doesn't exists for a
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
376 repo it returns a Dummy one instead
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
377
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
378 :param repo:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
379 :param rev:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
380 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
381 from rhodecode.lib.vcs.backends.base import BaseRepository
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
382 from rhodecode.lib.vcs.exceptions import RepositoryError
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
383 if not isinstance(repo, BaseRepository):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
384 raise Exception('You must pass an Repository '
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
385 'object as first argument got %s', type(repo))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
386
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
387 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
388 cs = repo.get_changeset(rev)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
389 except RepositoryError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
390 from rhodecode.lib.utils import EmptyChangeset
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
391 cs = EmptyChangeset(requested_revision=rev)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
392 return cs
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
393
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
394
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
395 def extract_mentioned_users(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
396 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
397 Returns unique usernames from given string s that have @mention
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
398
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
399 :param s: string to get mentions
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
400 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
401 usrs = {}
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
402 for username in re.findall(r'(?:^@|\s@)(\w+)', s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
403 usrs[username] = username
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
404
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
405 return sorted(usrs.keys())