Mercurial > kallithea
comparison rhodecode/lib/__init__.py @ 1818:cf51bbfb120e beta
auto white-space removal
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 29 Dec 2011 07:35:51 +0200 |
parents | 54687aa00724 |
children | 89efedac4e6c |
comparison
equal
deleted
inserted
replaced
1817:523b1011a625 | 1818:cf51bbfb120e |
---|---|
114 | 114 |
115 | 115 |
116 def convert_line_endings(line, mode): | 116 def convert_line_endings(line, mode): |
117 """ | 117 """ |
118 Converts a given line "line end" accordingly to given mode | 118 Converts a given line "line end" accordingly to given mode |
119 | 119 |
120 Available modes are:: | 120 Available modes are:: |
121 0 - Unix | 121 0 - Unix |
122 1 - Mac | 122 1 - Mac |
123 2 - DOS | 123 2 - DOS |
124 | 124 |
125 :param line: given line to convert | 125 :param line: given line to convert |
126 :param mode: mode to convert to | 126 :param mode: mode to convert to |
127 :rtype: str | 127 :rtype: str |
128 :return: converted line according to mode | 128 :return: converted line according to mode |
129 """ | 129 """ |
181 | 181 |
182 | 182 |
183 def safe_unicode(str_, from_encoding='utf8'): | 183 def safe_unicode(str_, from_encoding='utf8'): |
184 """ | 184 """ |
185 safe unicode function. Does few trick to turn str_ into unicode | 185 safe unicode function. Does few trick to turn str_ into unicode |
186 | 186 |
187 In case of UnicodeDecode error we try to return it with encoding detected | 187 In case of UnicodeDecode error we try to return it with encoding detected |
188 by chardet library if it fails fallback to unicode with errors replaced | 188 by chardet library if it fails fallback to unicode with errors replaced |
189 | 189 |
190 :param str_: string to decode | 190 :param str_: string to decode |
191 :rtype: unicode | 191 :rtype: unicode |
214 return unicode(str_, from_encoding, 'replace') | 214 return unicode(str_, from_encoding, 'replace') |
215 | 215 |
216 def safe_str(unicode_, to_encoding='utf8'): | 216 def safe_str(unicode_, to_encoding='utf8'): |
217 """ | 217 """ |
218 safe str function. Does few trick to turn unicode_ into string | 218 safe str function. Does few trick to turn unicode_ into string |
219 | 219 |
220 In case of UnicodeEncodeError we try to return it with encoding detected | 220 In case of UnicodeEncodeError we try to return it with encoding detected |
221 by chardet library if it fails fallback to string with errors replaced | 221 by chardet library if it fails fallback to string with errors replaced |
222 | 222 |
223 :param unicode_: unicode to encode | 223 :param unicode_: unicode to encode |
224 :rtype: str | 224 :rtype: str |
252 | 252 |
253 | 253 |
254 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): | 254 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): |
255 """ | 255 """ |
256 Custom engine_from_config functions that makes sure we use NullPool for | 256 Custom engine_from_config functions that makes sure we use NullPool for |
257 file based sqlite databases. This prevents errors on sqlite. This only | 257 file based sqlite databases. This prevents errors on sqlite. This only |
258 applies to sqlalchemy versions < 0.7.0 | 258 applies to sqlalchemy versions < 0.7.0 |
259 | 259 |
260 """ | 260 """ |
261 import sqlalchemy | 261 import sqlalchemy |
262 from sqlalchemy import engine_from_config as efc | 262 from sqlalchemy import engine_from_config as efc |
311 | 311 |
312 | 312 |
313 def age(curdate): | 313 def age(curdate): |
314 """ | 314 """ |
315 turns a datetime into an age string. | 315 turns a datetime into an age string. |
316 | 316 |
317 :param curdate: datetime object | 317 :param curdate: datetime object |
318 :rtype: unicode | 318 :rtype: unicode |
319 :returns: unicode words describing age | 319 :returns: unicode words describing age |
320 """ | 320 """ |
321 | 321 |
348 | 348 |
349 | 349 |
350 def uri_filter(uri): | 350 def uri_filter(uri): |
351 """ | 351 """ |
352 Removes user:password from given url string | 352 Removes user:password from given url string |
353 | 353 |
354 :param uri: | 354 :param uri: |
355 :rtype: unicode | 355 :rtype: unicode |
356 :returns: filtered list of strings | 356 :returns: filtered list of strings |
357 """ | 357 """ |
358 if not uri: | 358 if not uri: |
359 return '' | 359 return '' |
360 | 360 |
361 proto = '' | 361 proto = '' |
380 | 380 |
381 | 381 |
382 def credentials_filter(uri): | 382 def credentials_filter(uri): |
383 """ | 383 """ |
384 Returns a url with removed credentials | 384 Returns a url with removed credentials |
385 | 385 |
386 :param uri: | 386 :param uri: |
387 """ | 387 """ |
388 | 388 |
389 uri = uri_filter(uri) | 389 uri = uri_filter(uri) |
390 #check if we have port | 390 #check if we have port |
393 | 393 |
394 return ''.join(uri) | 394 return ''.join(uri) |
395 | 395 |
396 def get_changeset_safe(repo, rev): | 396 def get_changeset_safe(repo, rev): |
397 """ | 397 """ |
398 Safe version of get_changeset if this changeset doesn't exists for a | 398 Safe version of get_changeset if this changeset doesn't exists for a |
399 repo it returns a Dummy one instead | 399 repo it returns a Dummy one instead |
400 | 400 |
401 :param repo: | 401 :param repo: |
402 :param rev: | 402 :param rev: |
403 """ | 403 """ |
404 from vcs.backends.base import BaseRepository | 404 from vcs.backends.base import BaseRepository |
405 from vcs.exceptions import RepositoryError | 405 from vcs.exceptions import RepositoryError |
417 | 417 |
418 def get_current_revision(quiet=False): | 418 def get_current_revision(quiet=False): |
419 """ | 419 """ |
420 Returns tuple of (number, id) from repository containing this package | 420 Returns tuple of (number, id) from repository containing this package |
421 or None if repository could not be found. | 421 or None if repository could not be found. |
422 | 422 |
423 :param quiet: prints error for fetching revision if True | 423 :param quiet: prints error for fetching revision if True |
424 """ | 424 """ |
425 | 425 |
426 try: | 426 try: |
427 from vcs import get_repo | 427 from vcs import get_repo |
438 return None | 438 return None |
439 | 439 |
440 def extract_mentioned_users(s): | 440 def extract_mentioned_users(s): |
441 """ | 441 """ |
442 Returns unique usernames from given string s that have @mention | 442 Returns unique usernames from given string s that have @mention |
443 | 443 |
444 :param s: string to get mentions | 444 :param s: string to get mentions |
445 """ | 445 """ |
446 usrs = {} | 446 usrs = {} |
447 for username in re.findall(r'(?:^@|\s@)(\w+)', s): | 447 for username in re.findall(r'(?:^@|\s@)(\w+)', s): |
448 usrs[username] = username | 448 usrs[username] = username |