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