Mercurial > kallithea
annotate rhodecode/lib/utils2.py @ 2989:190e2ff4595d beta
fix for issue #602, enforce str when setting mercurial UI object. When this is used together with mercurial internal translation system
it can lead to UnicodeDecodeErrors
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 07 Nov 2012 19:38:46 +0100 |
parents | 5085e51fba3a |
children | 6e76b4892d72 |
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 | |
26 import re | |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
27 import time |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
28 import datetime |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
29 import webob |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
30 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
31 from pylons.i18n.translation import _, ungettext |
2109 | 32 from rhodecode.lib.vcs.utils.lazy import LazyProperty |
33 | |
34 | |
35 def __get_lem(): | |
36 """ | |
37 Get language extension map based on what's inside pygments lexers | |
38 """ | |
39 from pygments import lexers | |
40 from string import lower | |
41 from collections import defaultdict | |
42 | |
43 d = defaultdict(lambda: []) | |
44 | |
45 def __clean(s): | |
46 s = s.lstrip('*') | |
47 s = s.lstrip('.') | |
48 | |
49 if s.find('[') != -1: | |
50 exts = [] | |
51 start, stop = s.find('['), s.find(']') | |
52 | |
53 for suffix in s[start + 1:stop]: | |
54 exts.append(s[:s.find('[')] + suffix) | |
55 return map(lower, exts) | |
56 else: | |
57 return map(lower, [s]) | |
58 | |
59 for lx, t in sorted(lexers.LEXERS.items()): | |
60 m = map(__clean, t[-2]) | |
61 if m: | |
62 m = reduce(lambda x, y: x + y, m) | |
63 for ext in m: | |
64 desc = lx.replace('Lexer', '') | |
65 d[ext].append(desc) | |
66 | |
67 return dict(d) | |
68 | |
69 def str2bool(_str): | |
70 """ | |
71 returs True/False value from given string, it tries to translate the | |
72 string into boolean | |
73 | |
74 :param _str: string value to translate into boolean | |
75 :rtype: boolean | |
76 :returns: boolean from given string | |
77 """ | |
78 if _str is None: | |
79 return False | |
80 if _str in (True, False): | |
81 return _str | |
82 _str = str(_str).strip().lower() | |
83 return _str in ('t', 'true', 'y', 'yes', 'on', '1') | |
84 | |
85 | |
86 def convert_line_endings(line, mode): | |
87 """ | |
88 Converts a given line "line end" accordingly to given mode | |
89 | |
90 Available modes are:: | |
91 0 - Unix | |
92 1 - Mac | |
93 2 - DOS | |
94 | |
95 :param line: given line to convert | |
96 :param mode: mode to convert to | |
97 :rtype: str | |
98 :return: converted line according to mode | |
99 """ | |
100 from string import replace | |
101 | |
102 if mode == 0: | |
103 line = replace(line, '\r\n', '\n') | |
104 line = replace(line, '\r', '\n') | |
105 elif mode == 1: | |
106 line = replace(line, '\r\n', '\r') | |
107 line = replace(line, '\n', '\r') | |
108 elif mode == 2: | |
109 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line) | |
110 return line | |
111 | |
112 | |
113 def detect_mode(line, default): | |
114 """ | |
115 Detects line break for given line, if line break couldn't be found | |
116 given default value is returned | |
117 | |
118 :param line: str line | |
119 :param default: default | |
120 :rtype: int | |
121 :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS | |
122 """ | |
123 if line.endswith('\r\n'): | |
124 return 2 | |
125 elif line.endswith('\n'): | |
126 return 0 | |
127 elif line.endswith('\r'): | |
128 return 1 | |
129 else: | |
130 return default | |
131 | |
132 | |
133 def generate_api_key(username, salt=None): | |
134 """ | |
135 Generates unique API key for given username, if salt is not given | |
136 it'll be generated from some random string | |
137 | |
138 :param username: username as string | |
139 :param salt: salt to hash generate KEY | |
140 :rtype: str | |
141 :returns: sha1 hash from username+salt | |
142 """ | |
143 from tempfile import _RandomNameSequence | |
144 import hashlib | |
145 | |
146 if salt is None: | |
147 salt = _RandomNameSequence().next() | |
148 | |
149 return hashlib.sha1(username + salt).hexdigest() | |
150 | |
151 | |
2845
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
152 def safe_int(val, default=None): |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
153 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
154 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
|
155 instead |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
156 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
157 :param val: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
158 :param default: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
159 """ |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
160 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
161 try: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
162 val = int(val) |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
163 except ValueError: |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
164 val = default |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
165 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
166 return val |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
167 |
6b176c679896
failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents:
2759
diff
changeset
|
168 |
2109 | 169 def safe_unicode(str_, from_encoding=None): |
170 """ | |
171 safe unicode function. Does few trick to turn str_ into unicode | |
172 | |
173 In case of UnicodeDecode error we try to return it with encoding detected | |
174 by chardet library if it fails fallback to unicode with errors replaced | |
175 | |
176 :param str_: string to decode | |
177 :rtype: unicode | |
178 :returns: unicode object | |
179 """ | |
180 if isinstance(str_, unicode): | |
181 return str_ | |
182 | |
183 if not from_encoding: | |
184 import rhodecode | |
185 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8') | |
186 from_encoding = DEFAULT_ENCODING | |
187 | |
188 try: | |
189 return unicode(str_) | |
190 except UnicodeDecodeError: | |
191 pass | |
192 | |
193 try: | |
194 return unicode(str_, from_encoding) | |
195 except UnicodeDecodeError: | |
196 pass | |
197 | |
198 try: | |
199 import chardet | |
200 encoding = chardet.detect(str_)['encoding'] | |
201 if encoding is None: | |
202 raise Exception() | |
203 return str_.decode(encoding) | |
204 except (ImportError, UnicodeDecodeError, Exception): | |
205 return unicode(str_, from_encoding, 'replace') | |
206 | |
207 | |
208 def safe_str(unicode_, to_encoding=None): | |
209 """ | |
210 safe str function. Does few trick to turn unicode_ into string | |
211 | |
212 In case of UnicodeEncodeError we try to return it with encoding detected | |
213 by chardet library if it fails fallback to string with errors replaced | |
214 | |
215 :param unicode_: unicode to encode | |
216 :rtype: str | |
217 :returns: str object | |
218 """ | |
219 | |
220 # if it's not basestr cast to str | |
221 if not isinstance(unicode_, basestring): | |
222 return str(unicode_) | |
223 | |
224 if isinstance(unicode_, str): | |
225 return unicode_ | |
226 | |
227 if not to_encoding: | |
228 import rhodecode | |
229 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8') | |
230 to_encoding = DEFAULT_ENCODING | |
231 | |
232 try: | |
233 return unicode_.encode(to_encoding) | |
234 except UnicodeEncodeError: | |
235 pass | |
236 | |
237 try: | |
238 import chardet | |
239 encoding = chardet.detect(unicode_)['encoding'] | |
240 if encoding is None: | |
241 raise UnicodeEncodeError() | |
242 | |
243 return unicode_.encode(encoding) | |
244 except (ImportError, UnicodeEncodeError): | |
245 return unicode_.encode(to_encoding, 'replace') | |
246 | |
247 return safe_str | |
248 | |
249 | |
250 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): | |
251 """ | |
252 Custom engine_from_config functions that makes sure we use NullPool for | |
253 file based sqlite databases. This prevents errors on sqlite. This only | |
254 applies to sqlalchemy versions < 0.7.0 | |
255 | |
256 """ | |
257 import sqlalchemy | |
258 from sqlalchemy import engine_from_config as efc | |
259 import logging | |
260 | |
261 if int(sqlalchemy.__version__.split('.')[1]) < 7: | |
262 | |
263 # This solution should work for sqlalchemy < 0.7.0, and should use | |
264 # proxy=TimerProxy() for execution time profiling | |
265 | |
266 from sqlalchemy.pool import NullPool | |
267 url = configuration[prefix + 'url'] | |
268 | |
269 if url.startswith('sqlite'): | |
270 kwargs.update({'poolclass': NullPool}) | |
271 return efc(configuration, prefix, **kwargs) | |
272 else: | |
273 import time | |
274 from sqlalchemy import event | |
275 from sqlalchemy.engine import Engine | |
276 | |
277 log = logging.getLogger('sqlalchemy.engine') | |
278 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |
279 engine = efc(configuration, prefix, **kwargs) | |
280 | |
281 def color_sql(sql): | |
282 COLOR_SEQ = "\033[1;%dm" | |
283 COLOR_SQL = YELLOW | |
284 normal = '\x1b[0m' | |
285 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal]) | |
286 | |
287 if configuration['debug']: | |
288 #attach events only for debug configuration | |
289 | |
290 def before_cursor_execute(conn, cursor, statement, | |
291 parameters, context, executemany): | |
292 context._query_start_time = time.time() | |
293 log.info(color_sql(">>>>> STARTING QUERY >>>>>")) | |
294 | |
295 def after_cursor_execute(conn, cursor, statement, | |
296 parameters, context, executemany): | |
297 total = time.time() - context._query_start_time | |
298 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total)) | |
299 | |
300 event.listen(engine, "before_cursor_execute", | |
301 before_cursor_execute) | |
302 event.listen(engine, "after_cursor_execute", | |
303 after_cursor_execute) | |
304 | |
305 return engine | |
306 | |
307 | |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
308 def age(prevdate): |
2109 | 309 """ |
310 turns a datetime into an age string. | |
311 | |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
312 :param prevdate: datetime object |
2109 | 313 :rtype: unicode |
314 :returns: unicode words describing age | |
315 """ | |
316 | |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
317 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
|
318 deltas = {} |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
319 future = False |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
320 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
321 # Get date parts deltas |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
322 now = datetime.datetime.now() |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
323 if prevdate > now: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
324 now, prevdate = prevdate, now |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
325 future = True |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
326 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
327 for part in order: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
328 deltas[part] = getattr(now, part) - getattr(prevdate, part) |
2109 | 329 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
330 # 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
|
331 # not 1 hour, -59 minutes and -59 seconds) |
2109 | 332 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
333 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
|
334 part = order[num] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
335 carry_part = order[num - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
336 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
337 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
|
338 deltas[part] += length |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
339 deltas[carry_part] -= 1 |
2109 | 340 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
341 # 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
|
342 # 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
|
343 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
|
344 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
|
345 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
|
346 (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
|
347 deltas['day'] += 29 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
348 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
349 deltas['day'] += month_lengths[prevdate.month - 1] |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
350 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
351 deltas['month'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
352 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
353 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
|
354 deltas['month'] += 12 |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
355 deltas['year'] -= 1 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
356 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
357 # Format the result |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
358 fmt_funcs = { |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
359 '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
|
360 '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
|
361 '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
|
362 '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
|
363 '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
|
364 '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
|
365 } |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
366 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
367 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
|
368 value = deltas[part] |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
369 if value == 0: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
370 continue |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
371 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
372 if i < 5: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
373 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
|
374 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
|
375 else: |
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
376 sub_value = 0 |
2367
86aa4f1f130b
white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents:
2303
diff
changeset
|
377 |
2303
7090e394df06
Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents:
2278
diff
changeset
|
378 if sub_value == 0: |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
379 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
380 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
|
381 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
382 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
|
383 if future: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
384 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
|
385 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
|
386 else: |
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
387 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
|
388 fmt_funcs[sub_part](sub_value)) |
2109 | 389 |
390 return _(u'just now') | |
391 | |
392 | |
393 def uri_filter(uri): | |
394 """ | |
395 Removes user:password from given url string | |
396 | |
397 :param uri: | |
398 :rtype: unicode | |
399 :returns: filtered list of strings | |
400 """ | |
401 if not uri: | |
402 return '' | |
403 | |
404 proto = '' | |
405 | |
406 for pat in ('https://', 'http://'): | |
407 if uri.startswith(pat): | |
408 uri = uri[len(pat):] | |
409 proto = pat | |
410 break | |
411 | |
412 # remove passwords and username | |
413 uri = uri[uri.find('@') + 1:] | |
414 | |
415 # get the port | |
416 cred_pos = uri.find(':') | |
417 if cred_pos == -1: | |
418 host, port = uri, None | |
419 else: | |
420 host, port = uri[:cred_pos], uri[cred_pos + 1:] | |
421 | |
422 return filter(None, [proto, host, port]) | |
423 | |
424 | |
425 def credentials_filter(uri): | |
426 """ | |
427 Returns a url with removed credentials | |
428 | |
429 :param uri: | |
430 """ | |
431 | |
432 uri = uri_filter(uri) | |
433 #check if we have port | |
434 if len(uri) > 2 and uri[2]: | |
435 uri[2] = ':' + uri[2] | |
436 | |
437 return ''.join(uri) | |
438 | |
439 | |
440 def get_changeset_safe(repo, rev): | |
441 """ | |
442 Safe version of get_changeset if this changeset doesn't exists for a | |
443 repo it returns a Dummy one instead | |
444 | |
445 :param repo: | |
446 :param rev: | |
447 """ | |
448 from rhodecode.lib.vcs.backends.base import BaseRepository | |
449 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
|
450 from rhodecode.lib.vcs.backends.base import EmptyChangeset |
2109 | 451 if not isinstance(repo, BaseRepository): |
452 raise Exception('You must pass an Repository ' | |
453 'object as first argument got %s', type(repo)) | |
454 | |
455 try: | |
456 cs = repo.get_changeset(rev) | |
457 except RepositoryError: | |
458 cs = EmptyChangeset(requested_revision=rev) | |
459 return cs | |
460 | |
461 | |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
462 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
|
463 if dt: |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
464 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
|
465 |
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
466 |
2726
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
467 def time_to_datetime(tm): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
468 if tm: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
469 if isinstance(tm, basestring): |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
470 try: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
471 tm = float(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
472 except ValueError: |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
473 return |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
474 return datetime.datetime.fromtimestamp(tm) |
aa17c7a1b8a5
Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents:
2699
diff
changeset
|
475 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
476 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
|
477 |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
478 |
2109 | 479 def extract_mentioned_users(s): |
480 """ | |
481 Returns unique usernames from given string s that have @mention | |
482 | |
483 :param s: string to get mentions | |
484 """ | |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
485 usrs = set() |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
486 for username in re.findall(MENTIONS_REGEX, s): |
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
487 usrs.add(username) |
2109 | 488 |
2201
ea5ff843b200
#426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents:
2109
diff
changeset
|
489 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
|
490 |
2699
4eef5eeb81a3
fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents:
2684
diff
changeset
|
491 |
2674
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
492 class AttributeDict(dict): |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
493 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
|
494 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
|
495 __setattr__ = dict.__setitem__ |
a221706dab50
merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents:
2367
diff
changeset
|
496 __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
|
497 |
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
|
498 |
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
|
499 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
|
500 """ |
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
|
501 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
|
502 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
|
503 """ |
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
|
504 import sys |
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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 |
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
|
510 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
|
511 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
|
512 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
|
513 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
514 |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
515 def obfuscate_url_pw(engine): |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
516 from sqlalchemy.engine import url |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
517 url = url.make_url(engine) |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
518 if url.password: |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2869
diff
changeset
|
519 url.password = 'XXXXX' |
2902
e2b2791d1e7c
fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents:
2882
diff
changeset
|
520 return str(url) |
2969
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
521 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
522 |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
523 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
|
524 req = webob.Request(environ) |
5085e51fba3a
Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents:
2902
diff
changeset
|
525 return req.host_url + req.script_name |