Mercurial > kallithea
annotate rhodecode/lib/utils2.py @ 3984:425b9eb67d3a
pullrequests: make closed PRs more distinctly grey
It could look like a slight light blue taint.
author | Mads Kiilerich <madski@unity3d.com> |
---|---|
date | Tue, 04 Jun 2013 14:01:46 +0200 |
parents | 5293d4bbb1ea |
children | 55dbc440878b |
rev | line source |
---|---|
2109 | 1 # -*- coding: utf-8 -*- |
2 """ | |
3 rhodecode.lib.utils | |
4 ~~~~~~~~~~~~~~~~~~~ | |
5 | |
6 Some simple helper functions | |
7 | |
8 :created_on: Jan 5, 2011 | |
9 :author: marcink | |
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> | |
11 :license: GPLv3, see COPYING for more details. | |
12 """ | |
13 # This program is free software: you can redistribute it and/or modify | |
14 # it under the terms of the GNU General Public License as published by | |
15 # the Free Software Foundation, either version 3 of the License, or | |
16 # (at your option) any later version. | |
17 # | |
18 # This program is distributed in the hope that it will be useful, | |
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 # GNU General Public License for more details. | |
22 # | |
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/>. | |
25 | |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
26 import os |
2109 | 27 import re |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
28 import sys |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
29 import time |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
30 import uuid |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
31 import datetime |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
32 import traceback |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
33 import webob |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
34 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
35 from pylons.i18n.translation import _, ungettext |
2109 | 36 from rhodecode.lib.vcs.utils.lazy import LazyProperty |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
37 from rhodecode.lib.compat import json |
2109 | 38 |
39 | |
40 def __get_lem(): | |
41 """ | |
42 Get language extension map based on what's inside pygments lexers | |
43 """ | |
44 from pygments import lexers | |
45 from string import lower | |
46 from collections import defaultdict | |
47 | |
48 d = defaultdict(lambda: []) | |
49 | |
50 def __clean(s): | |
51 s = s.lstrip('*') | |
52 s = s.lstrip('.') | |
53 | |
54 if s.find('[') != -1: | |
55 exts = [] | |
56 start, stop = s.find('['), s.find(']') | |
57 | |
58 for suffix in s[start + 1:stop]: | |
59 exts.append(s[:s.find('[')] + suffix) | |
60 return map(lower, exts) | |
61 else: | |
62 return map(lower, [s]) | |
63 | |
64 for lx, t in sorted(lexers.LEXERS.items()): | |
65 m = map(__clean, t[-2]) | |
66 if m: | |
67 m = reduce(lambda x, y: x + y, m) | |
68 for ext in m: | |
69 desc = lx.replace('Lexer', '') | |
70 d[ext].append(desc) | |
71 | |
72 return dict(d) | |
73 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
74 |
2109 | 75 def str2bool(_str): |
76 """ | |
77 returs True/False value from given string, it tries to translate the | |
78 string into boolean | |
79 | |
80 :param _str: string value to translate into boolean | |
81 :rtype: boolean | |
82 :returns: boolean from given string | |
83 """ | |
84 if _str is None: | |
85 return False | |
86 if _str in (True, False): | |
87 return _str | |
88 _str = str(_str).strip().lower() | |
89 return _str in ('t', 'true', 'y', 'yes', 'on', '1') | |
90 | |
91 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
92 def aslist(obj, sep=None, strip=True): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
93 """ |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
94 Returns given string separated by sep as list |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
95 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
96 :param obj: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
97 :param sep: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
98 :param strip: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
99 """ |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
100 if isinstance(obj, (basestring)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
101 lst = obj.split(sep) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
102 if strip: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
103 lst = [v.strip() for v in lst] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
104 return lst |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
105 elif isinstance(obj, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
106 return obj |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
107 elif obj is None: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
108 return [] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
109 else: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
110 return [obj] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
111 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
112 |
2109 | 113 def convert_line_endings(line, mode): |
114 """ | |
115 Converts a given line "line end" accordingly to given mode | |
116 | |
117 Available modes are:: | |
118 0 - Unix | |
119 1 - Mac | |
120 2 - DOS | |
121 | |
122 :param line: given line to convert | |
123 :param mode: mode to convert to | |
124 :rtype: str | |
125 :return: converted line according to mode | |
126 """ | |
127 from string import replace | |
128 | |
129 if mode == 0: | |
130 line = replace(line, '\r\n', '\n') | |
131 line = replace(line, '\r', '\n') | |
132 elif mode == 1: | |
133 line = replace(line, '\r\n', '\r') | |
134 line = replace(line, '\n', '\r') | |
135 elif mode == 2: | |
136 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line) | |
137 return line | |
138 | |
139 | |
140 def detect_mode(line, default): | |
141 """ | |
142 Detects line break for given line, if line break couldn't be found | |
143 given default value is returned | |
144 | |
145 :param line: str line | |
146 :param default: default | |
147 :rtype: int | |
148 :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS | |
149 """ | |
150 if line.endswith('\r\n'): | |
151 return 2 | |
152 elif line.endswith('\n'): | |
153 return 0 | |
154 elif line.endswith('\r'): | |
155 return 1 | |
156 else: | |
157 return default | |
158 | |
159 | |
160 def generate_api_key(username, salt=None): | |
161 """ | |
162 Generates unique API key for given username, if salt is not given | |
163 it'll be generated from some random string | |
164 | |
165 :param username: username as string | |
166 :param salt: salt to hash generate KEY | |
167 :rtype: str | |
168 :returns: sha1 hash from username+salt | |
169 """ | |
170 from tempfile import _RandomNameSequence | |
171 import hashlib | |
172 | |
173 if salt is None: | |
174 salt = _RandomNameSequence().next() | |
175 | |
176 return hashlib.sha1(username + salt).hexdigest() | |
177 | |
178 | |
2845
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
179 def safe_int(val, default=None): |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
180 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
181 Returns int() of val if val is not convertable to int use default |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
182 instead |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
183 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
184 :param val: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
185 :param default: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
186 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
187 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
188 try: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
189 val = int(val) |
3218
c0b0351233ec
safe_int should also catch TypeError
Marcin Kuzminski <marcin@python-works.com>
parents:
3056
diff
changeset
|
190 except (ValueError, TypeError): |
2845
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
191 val = default |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
192 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
193 return val |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
194 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
195 |
2109 | 196 def safe_unicode(str_, from_encoding=None): |
197 """ | |
198 safe unicode function. Does few trick to turn str_ into unicode | |
199 | |
200 In case of UnicodeDecode error we try to return it with encoding detected | |
201 by chardet library if it fails fallback to unicode with errors replaced | |
202 | |
203 :param str_: string to decode | |
204 :rtype: unicode | |
205 :returns: unicode object | |
206 """ | |
207 if isinstance(str_, unicode): | |
208 return str_ | |
209 | |
210 if not from_encoding: | |
211 import rhodecode | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
212 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding', |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
213 'utf8'), sep=',') |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
214 from_encoding = DEFAULT_ENCODINGS |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
215 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
216 if not isinstance(from_encoding, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
217 from_encoding = [from_encoding] |
2109 | 218 |
219 try: | |
220 return unicode(str_) | |
221 except UnicodeDecodeError: | |
222 pass | |
223 | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
224 for enc in from_encoding: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
225 try: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
226 return unicode(str_, enc) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
227 except UnicodeDecodeError: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
228 pass |
2109 | 229 |
230 try: | |
231 import chardet | |
232 encoding = chardet.detect(str_)['encoding'] | |
233 if encoding is None: | |
234 raise Exception() | |
235 return str_.decode(encoding) | |
236 except (ImportError, UnicodeDecodeError, Exception): | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
237 return unicode(str_, from_encoding[0], 'replace') |
2109 | 238 |
239 | |
240 def safe_str(unicode_, to_encoding=None): | |
241 """ | |
242 safe str function. Does few trick to turn unicode_ into string | |
243 | |
244 In case of UnicodeEncodeError we try to return it with encoding detected | |
245 by chardet library if it fails fallback to string with errors replaced | |
246 | |
247 :param unicode_: unicode to encode | |
248 :rtype: str | |
249 :returns: str object | |
250 """ | |
251 | |
252 # if it's not basestr cast to str | |
253 if not isinstance(unicode_, basestring): | |
254 return str(unicode_) | |
255 | |
256 if isinstance(unicode_, str): | |
257 return unicode_ | |
258 | |
259 if not to_encoding: | |
260 import rhodecode | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
261 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding', |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
262 'utf8'), sep=',') |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
263 to_encoding = DEFAULT_ENCODINGS |
2109 | 264 |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
265 if not isinstance(to_encoding, (list, tuple)): |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
266 to_encoding = [to_encoding] |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
267 |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
268 for enc in to_encoding: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
269 try: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
270 return unicode_.encode(enc) |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
271 except UnicodeEncodeError: |
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
272 pass |
2109 | 273 |
274 try: | |
275 import chardet | |
276 encoding = chardet.detect(unicode_)['encoding'] | |
277 if encoding is None: | |
278 raise UnicodeEncodeError() | |
279 | |
280 return unicode_.encode(encoding) | |
281 except (ImportError, UnicodeEncodeError): | |
3008
6e76b4892d72
Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents:
2969
diff
changeset
|
282 return unicode_.encode(to_encoding[0], 'replace') |
2109 | 283 |
284 | |
3018
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
285 def remove_suffix(s, suffix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
286 if s.endswith(suffix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
287 s = s[:-1 * len(suffix)] |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
288 return s |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
289 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
290 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
291 def remove_prefix(s, prefix): |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
292 if s.startswith(prefix): |
3056
6104dfd35b16
Implemented #379 defaults settings page for creation of repositories
Marcin Kuzminski <marcin@python-works.com>
parents:
3018
diff
changeset
|
293 s = s[len(prefix):] |
3018
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
294 return s |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
295 |
023f7873ef59
added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents:
3008
diff
changeset
|
296 |
2109 | 297 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): |
298 """ | |
299 Custom engine_from_config functions that makes sure we use NullPool for | |
300 file based sqlite databases. This prevents errors on sqlite. This only | |
301 applies to sqlalchemy versions < 0.7.0 | |
302 | |
303 """ | |
304 import sqlalchemy | |
305 from sqlalchemy import engine_from_config as efc | |
306 import logging | |
307 | |
308 if int(sqlalchemy.__version__.split('.')[1]) < 7: | |
309 | |
310 # This solution should work for sqlalchemy < 0.7.0, and should use | |
311 # proxy=TimerProxy() for execution time profiling | |
312 | |
313 from sqlalchemy.pool import NullPool | |
314 url = configuration[prefix + 'url'] | |
315 | |
316 if url.startswith('sqlite'): | |
317 kwargs.update({'poolclass': NullPool}) | |
318 return efc(configuration, prefix, **kwargs) | |
319 else: | |
320 import time | |
321 from sqlalchemy import event | |
322 from sqlalchemy.engine import Engine | |
323 | |
324 log = logging.getLogger('sqlalchemy.engine') | |
325 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |
326 engine = efc(configuration, prefix, **kwargs) | |
327 | |
328 def color_sql(sql): | |
329 COLOR_SEQ = "\033[1;%dm" | |
330 COLOR_SQL = YELLOW | |
331 normal = '\x1b[0m' | |
332 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal]) | |
333 | |
334 if configuration['debug']: | |
335 #attach events only for debug configuration | |
336 | |
337 def before_cursor_execute(conn, cursor, statement, | |
338 parameters, context, executemany): | |
339 context._query_start_time = time.time() | |
340 log.info(color_sql(">>>>> STARTING QUERY >>>>>")) | |
341 | |
342 def after_cursor_execute(conn, cursor, statement, | |
343 parameters, context, executemany): | |
344 total = time.time() - context._query_start_time | |
345 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total)) | |
346 | |
347 event.listen(engine, "before_cursor_execute", | |
348 before_cursor_execute) | |
349 event.listen(engine, "after_cursor_execute", | |
350 after_cursor_execute) | |
351 | |
352 return engine | |
353 | |
354 | |
3644
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
355 def age(prevdate, show_short_version=False, now=None): |
2109 | 356 """ |
357 turns a datetime into an age string. | |
3536 | 358 If show_short_version is True, then it will generate a not so accurate but shorter string, |
359 example: 2days ago, instead of 2 days and 23 hours ago. | |
360 | |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
361 :param prevdate: datetime object |
3536 | 362 :param show_short_version: if it should aproximate the date and return a shorter string |
2109 | 363 :rtype: unicode |
364 :returns: unicode words describing age | |
365 """ | |
3644
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
366 now = now or datetime.datetime.now() |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
367 order = ['year', 'month', 'day', 'hour', 'minute', 'second'] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
368 deltas = {} |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
369 future = False |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
370 |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
371 if prevdate > now: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
372 now, prevdate = prevdate, now |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
373 future = True |
3644
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
374 if future: |
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
375 prevdate = prevdate.replace(microsecond=0) |
3261
54a439edaf99
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents:
3218
diff
changeset
|
376 # Get date parts deltas |
3644
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
377 from dateutil import relativedelta |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
378 for part in order: |
3644
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
379 d = relativedelta.relativedelta(now, prevdate) |
71860d0737e7
- age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
380 deltas[part] = getattr(d, part + 's') |
2109 | 381 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
382 # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
383 # not 1 hour, -59 minutes and -59 seconds) |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
384 for num, length in [(5, 60), (4, 60), (3, 24)]: # seconds, minutes, hours |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
385 part = order[num] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
386 carry_part = order[num - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
387 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
388 if deltas[part] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
389 deltas[part] += length |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
390 deltas[carry_part] -= 1 |
2109 | 391 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
392 # Same thing for days except that the increment depends on the (variable) |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
393 # number of days in the month |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
394 month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
395 if deltas['day'] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
396 if prevdate.month == 2 and (prevdate.year % 4 == 0 and |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
397 (prevdate.year % 100 != 0 or prevdate.year % 400 == 0)): |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
398 deltas['day'] += 29 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
399 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
400 deltas['day'] += month_lengths[prevdate.month - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
401 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
402 deltas['month'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
403 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
404 if deltas['month'] < 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
405 deltas['month'] += 12 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
406 deltas['year'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
407 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
408 # Format the result |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
409 fmt_funcs = { |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
410 'year': lambda d: ungettext(u'%d year', '%d years', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
411 'month': lambda d: ungettext(u'%d month', '%d months', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
412 'day': lambda d: ungettext(u'%d day', '%d days', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
413 'hour': lambda d: ungettext(u'%d hour', '%d hours', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
414 'minute': lambda d: ungettext(u'%d minute', '%d minutes', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
415 'second': lambda d: ungettext(u'%d second', '%d seconds', d) % d, |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
416 } |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
417 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
418 for i, part in enumerate(order): |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
419 value = deltas[part] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
420 if value == 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
421 continue |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
422 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
423 if i < 5: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
424 sub_part = order[i + 1] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
425 sub_value = deltas[sub_part] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
426 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
427 sub_value = 0 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
428 |
3536 | 429 if sub_value == 0 or show_short_version: |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
430 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
431 return _(u'in %s') % fmt_funcs[part](value) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
432 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
433 return _(u'%s ago') % fmt_funcs[part](value) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
434 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
435 return _(u'in %s and %s') % (fmt_funcs[part](value), |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
436 fmt_funcs[sub_part](sub_value)) |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
437 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
438 return _(u'%s and %s ago') % (fmt_funcs[part](value), |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
439 fmt_funcs[sub_part](sub_value)) |
2109 | 440 |
441 return _(u'just now') | |
442 | |
443 | |
444 def uri_filter(uri): | |
445 """ | |
446 Removes user:password from given url string | |
447 | |
448 :param uri: | |
449 :rtype: unicode | |
450 :returns: filtered list of strings | |
451 """ | |
452 if not uri: | |
453 return '' | |
454 | |
455 proto = '' | |
456 | |
457 for pat in ('https://', 'http://'): | |
458 if uri.startswith(pat): | |
459 uri = uri[len(pat):] | |
460 proto = pat | |
461 break | |
462 | |
463 # remove passwords and username | |
464 uri = uri[uri.find('@') + 1:] | |
465 | |
466 # get the port | |
467 cred_pos = uri.find(':') | |
468 if cred_pos == -1: | |
469 host, port = uri, None | |
470 else: | |
471 host, port = uri[:cred_pos], uri[cred_pos + 1:] | |
472 | |
473 return filter(None, [proto, host, port]) | |
474 | |
475 | |
476 def credentials_filter(uri): | |
477 """ | |
478 Returns a url with removed credentials | |
479 | |
480 :param uri: | |
481 """ | |
482 | |
483 uri = uri_filter(uri) | |
484 #check if we have port | |
485 if len(uri) > 2 and uri[2]: | |
486 uri[2] = ':' + uri[2] | |
487 | |
488 return ''.join(uri) | |
489 | |
490 | |
491 def get_changeset_safe(repo, rev): | |
492 """ | |
493 Safe version of get_changeset if this changeset doesn't exists for a | |
494 repo it returns a Dummy one instead | |
495 | |
496 :param repo: | |
497 :param rev: | |
498 """ | |
499 from rhodecode.lib.vcs.backends.base import BaseRepository | |
500 from rhodecode.lib.vcs.exceptions import RepositoryError | |
2684
2b6939a77052
Bumped mercurial version to 2.3
Marcin Kuzminski <marcin@python-works.com>
parents:
2674
diff
changeset
|
501 from rhodecode.lib.vcs.backends.base import EmptyChangeset |
2109 | 502 if not isinstance(repo, BaseRepository): |
503 raise Exception('You must pass an Repository ' | |
504 'object as first argument got %s', type(repo)) | |
505 | |
506 try: | |
507 cs = repo.get_changeset(rev) | |
508 except RepositoryError: | |
509 cs = EmptyChangeset(requested_revision=rev) | |
510 return cs | |
511 | |
512 | |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
513 def datetime_to_time(dt): |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
514 if dt: |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
515 return time.mktime(dt.timetuple()) |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
516 |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
517 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
518 def time_to_datetime(tm): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
519 if tm: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
520 if isinstance(tm, basestring): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
521 try: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
522 tm = float(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
523 except ValueError: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
524 return |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
525 return datetime.datetime.fromtimestamp(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
526 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
527 MENTIONS_REGEX = r'(?:^@|\s@)([a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+)(?:\s{1})' |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
528 |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
529 |
2109 | 530 def extract_mentioned_users(s): |
531 """ | |
532 Returns unique usernames from given string s that have @mention | |
533 | |
534 :param s: string to get mentions | |
535 """ | |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
536 usrs = set() |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
537 for username in re.findall(MENTIONS_REGEX, s): |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
538 usrs.add(username) |
2109 | 539 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
540 return sorted(list(usrs), key=lambda k: k.lower()) |
2674
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
541 |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
542 |
2674
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
543 class AttributeDict(dict): |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
544 def __getattr__(self, attr): |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
545 return self.get(attr, None) |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
546 __setattr__ = dict.__setitem__ |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
547 __delattr__ = dict.__delitem__ |
2869
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
548 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
549 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
550 def fix_PATH(os_=None): |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
551 """ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
552 Get current active python path, and append it to PATH variable to fix issues |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
553 of subprocess calls and different python versions |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
554 """ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
555 if os_ is None: |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
556 import os |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
557 else: |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
558 os = os_ |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
559 |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
560 cur_path = os.path.split(sys.executable)[0] |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
561 if not os.environ['PATH'].startswith(cur_path): |
ccbdff90e5a0
fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents:
2845
diff
changeset
|
562 os.environ['PATH'] = '%s:%s' % (cur_path, os.environ['PATH']) |
2882
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
563 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
564 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
565 def obfuscate_url_pw(engine): |
3454
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
566 _url = engine or '' |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
567 from sqlalchemy.engine import url as sa_url |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
568 try: |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
569 _url = sa_url.make_url(engine) |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
570 if _url.password: |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
571 _url.password = 'XXXXX' |
3631
10b4e34841a4
Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents:
3590
diff
changeset
|
572 except Exception: |
3454
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
573 pass |
867c4d7fd2d8
catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents:
3378
diff
changeset
|
574 return str(_url) |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
575 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
576 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
577 def get_server_url(environ): |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
578 req = webob.Request(environ) |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
579 return req.host_url + req.script_name |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
580 |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
581 |
3590
47631be9f449
fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents:
3577
diff
changeset
|
582 def _extract_extras(env=None): |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
583 """ |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
584 Extracts the rc extras data from os.environ, and wraps it into named |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
585 AttributeDict object |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
586 """ |
3590
47631be9f449
fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents:
3577
diff
changeset
|
587 if not env: |
47631be9f449
fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents:
3577
diff
changeset
|
588 env = os.environ |
47631be9f449
fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents:
3577
diff
changeset
|
589 |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
590 try: |
3590
47631be9f449
fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents:
3577
diff
changeset
|
591 rc_extras = json.loads(env['RC_SCM_DATA']) |
3631
10b4e34841a4
Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents:
3590
diff
changeset
|
592 except Exception: |
3577
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
593 print os.environ |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
594 print >> sys.stderr, traceback.format_exc() |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
595 rc_extras = {} |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
596 |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
597 try: |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
598 for k in ['username', 'repository', 'locked_by', 'scm', 'make_lock', |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
599 'action', 'ip']: |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
600 rc_extras[k] |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
601 except KeyError, e: |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
602 raise Exception('Missing key %s in os.environ %s' % (e, rc_extras)) |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
603 |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
604 return AttributeDict(rc_extras) |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
605 |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
606 |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
607 def _set_extras(extras): |
238486bb71ab
Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents:
3552
diff
changeset
|
608 os.environ['RC_SCM_DATA'] = json.dumps(extras) |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
609 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
610 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
611 def unique_id(hexlen=32): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
612 alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
613 return suuid(truncate_to=hexlen, alphabet=alphabet) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
614 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
615 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
616 def suuid(url=None, truncate_to=22, alphabet=None): |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
617 """ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
618 Generate and return a short URL safe UUID. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
619 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
620 If the url parameter is provided, set the namespace to the provided |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
621 URL and generate a UUID. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
622 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
623 :param url to get the uuid for |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
624 :truncate_to: truncate the basic 22 UUID to shorter version |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
625 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
626 The IDs won't be universally unique any longer, but the probability of |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
627 a collision will still be very low. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
628 """ |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
629 # Define our alphabet. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
630 _ALPHABET = alphabet or "23456789ABCDEFGHJKLMNPQRSTUVWXYZ" |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
631 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
632 # If no URL is given, generate a random UUID. |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
633 if url is None: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
634 unique_id = uuid.uuid4().int |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
635 else: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
636 unique_id = uuid.uuid3(uuid.NAMESPACE_URL, url).int |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
637 |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
638 alphabet_length = len(_ALPHABET) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
639 output = [] |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
640 while unique_id > 0: |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
641 digit = unique_id % alphabet_length |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
642 output.append(_ALPHABET[digit]) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
643 unique_id = int(unique_id / alphabet_length) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3644
diff
changeset
|
644 return "".join(output)[:truncate_to] |