annotate rhodecode/lib/utils2.py @ 2668:f0851f37d6be beta

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