Show More
@@ -1,286 +1,286 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.lib.__init__ |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Some simple helper functions |
|
7 | 7 | |
|
8 | 8 | :created_on: Jan 5, 2011 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | def __get_lem(): |
|
28 | 28 | from pygments import lexers |
|
29 | 29 | from string import lower |
|
30 | 30 | from collections import defaultdict |
|
31 | 31 | |
|
32 | 32 | d = defaultdict(lambda: []) |
|
33 | 33 | |
|
34 | 34 | def __clean(s): |
|
35 | 35 | s = s.lstrip('*') |
|
36 | 36 | s = s.lstrip('.') |
|
37 | 37 | |
|
38 | 38 | if s.find('[') != -1: |
|
39 | 39 | exts = [] |
|
40 | 40 | start, stop = s.find('['), s.find(']') |
|
41 | 41 | |
|
42 | 42 | for suffix in s[start + 1:stop]: |
|
43 | 43 | exts.append(s[:s.find('[')] + suffix) |
|
44 | 44 | return map(lower, exts) |
|
45 | 45 | else: |
|
46 | 46 | return map(lower, [s]) |
|
47 | 47 | |
|
48 | 48 | for lx, t in sorted(lexers.LEXERS.items()): |
|
49 | 49 | m = map(__clean, t[-2]) |
|
50 | 50 | if m: |
|
51 | 51 | m = reduce(lambda x, y: x + y, m) |
|
52 | 52 | for ext in m: |
|
53 | 53 | desc = lx.replace('Lexer', '') |
|
54 | 54 | d[ext].append(desc) |
|
55 | 55 | |
|
56 | 56 | return dict(d) |
|
57 | 57 | |
|
58 | 58 | # language map is also used by whoosh indexer, which for those specified |
|
59 | 59 | # extensions will index it's content |
|
60 | 60 | LANGUAGES_EXTENSIONS_MAP = __get_lem() |
|
61 | 61 | |
|
62 | 62 | # Additional mappings that are not present in the pygments lexers |
|
63 | 63 | # NOTE: that this will overide any mappings in LANGUAGES_EXTENSIONS_MAP |
|
64 | 64 | ADDITIONAL_MAPPINGS = {'xaml': 'XAML'} |
|
65 | 65 | |
|
66 | 66 | LANGUAGES_EXTENSIONS_MAP.update(ADDITIONAL_MAPPINGS) |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def str2bool(_str): |
|
70 | 70 | """ |
|
71 | 71 | returs True/False value from given string, it tries to translate the |
|
72 | 72 | string into boolean |
|
73 | 73 | |
|
74 | 74 | :param _str: string value to translate into boolean |
|
75 | 75 | :rtype: boolean |
|
76 | 76 | :returns: boolean from given string |
|
77 | 77 | """ |
|
78 | 78 | if _str is None: |
|
79 | 79 | return False |
|
80 | 80 | if _str in (True, False): |
|
81 | 81 | return _str |
|
82 | 82 | _str = str(_str).strip().lower() |
|
83 | 83 | return _str in ('t', 'true', 'y', 'yes', 'on', '1') |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | def convert_line_endings(temp, mode): |
|
87 | 87 | from string import replace |
|
88 | 88 | #modes: 0 - Unix, 1 - Mac, 2 - DOS |
|
89 | 89 | if mode == 0: |
|
90 | 90 | temp = replace(temp, '\r\n', '\n') |
|
91 | 91 | temp = replace(temp, '\r', '\n') |
|
92 | 92 | elif mode == 1: |
|
93 | 93 | temp = replace(temp, '\r\n', '\r') |
|
94 | 94 | temp = replace(temp, '\n', '\r') |
|
95 | 95 | elif mode == 2: |
|
96 | 96 | import re |
|
97 | 97 | temp = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", temp) |
|
98 | 98 | return temp |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | def detect_mode(line, default): |
|
102 | 102 | """ |
|
103 | 103 | Detects line break for given line, if line break couldn't be found |
|
104 | 104 | given default value is returned |
|
105 | 105 | |
|
106 | 106 | :param line: str line |
|
107 | 107 | :param default: default |
|
108 | 108 | :rtype: int |
|
109 | 109 | :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS |
|
110 | 110 | """ |
|
111 | 111 | if line.endswith('\r\n'): |
|
112 | 112 | return 2 |
|
113 | 113 | elif line.endswith('\n'): |
|
114 | 114 | return 0 |
|
115 | 115 | elif line.endswith('\r'): |
|
116 | 116 | return 1 |
|
117 | 117 | else: |
|
118 | 118 | return default |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | def generate_api_key(username, salt=None): |
|
122 | 122 | """ |
|
123 | 123 | Generates unique API key for given username,if salt is not given |
|
124 | 124 | it'll be generated from some random string |
|
125 | 125 | |
|
126 | 126 | :param username: username as string |
|
127 | 127 | :param salt: salt to hash generate KEY |
|
128 | 128 | :rtype: str |
|
129 | 129 | :returns: sha1 hash from username+salt |
|
130 | 130 | """ |
|
131 | 131 | from tempfile import _RandomNameSequence |
|
132 | 132 | import hashlib |
|
133 | 133 | |
|
134 | 134 | if salt is None: |
|
135 | 135 | salt = _RandomNameSequence().next() |
|
136 | 136 | |
|
137 | 137 | return hashlib.sha1(username + salt).hexdigest() |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | def safe_unicode(_str, from_encoding='utf8'): |
|
141 | 141 | """ |
|
142 | 142 | safe unicode function. In case of UnicodeDecode error we try to return |
|
143 | 143 | unicode with errors replace |
|
144 | 144 | |
|
145 | 145 | :param _str: string to decode |
|
146 | 146 | :rtype: unicode |
|
147 | 147 | :returns: unicode object |
|
148 | 148 | """ |
|
149 | 149 | |
|
150 | 150 | if isinstance(_str, unicode): |
|
151 | 151 | return _str |
|
152 | 152 | |
|
153 | 153 | try: |
|
154 | 154 | u_str = unicode(_str, from_encoding) |
|
155 | 155 | except UnicodeDecodeError: |
|
156 | 156 | u_str = unicode(_str, from_encoding, 'replace') |
|
157 | 157 | |
|
158 | 158 | return u_str |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): |
|
162 | 162 | """ |
|
163 | 163 | Custom engine_from_config functions that makes sure we use NullPool for |
|
164 | 164 | file based sqlite databases. This prevents errors on sqlite. This only |
|
165 | 165 | applies to sqlalchemy versions < 0.7.0 |
|
166 | 166 | |
|
167 | 167 | """ |
|
168 | 168 | import sqlalchemy |
|
169 | 169 | from sqlalchemy import engine_from_config as efc |
|
170 | 170 | import logging |
|
171 | 171 | |
|
172 | 172 | if int(sqlalchemy.__version__.split('.')[1]) < 7: |
|
173 | 173 | |
|
174 | 174 | # This solution should work for sqlalchemy < 0.7.0, and should use |
|
175 | 175 | # proxy=TimerProxy() for execution time profiling |
|
176 | 176 | |
|
177 | 177 | from sqlalchemy.pool import NullPool |
|
178 | 178 | url = configuration[prefix + 'url'] |
|
179 | 179 | |
|
180 | 180 | if url.startswith('sqlite'): |
|
181 | 181 | kwargs.update({'poolclass': NullPool}) |
|
182 | 182 | return efc(configuration, prefix, **kwargs) |
|
183 | 183 | else: |
|
184 | 184 | import time |
|
185 | 185 | from sqlalchemy import event |
|
186 | 186 | from sqlalchemy.engine import Engine |
|
187 | 187 | |
|
188 |
log = logging.getLogger(' |
|
|
188 | log = logging.getLogger('sqlalchemy.engine') | |
|
189 | 189 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) |
|
190 | 190 | engine = efc(configuration, prefix, **kwargs) |
|
191 | 191 | |
|
192 | 192 | def color_sql(sql): |
|
193 | 193 | COLOR_SEQ = "\033[1;%dm" |
|
194 | 194 | COLOR_SQL = YELLOW |
|
195 | 195 | normal = '\x1b[0m' |
|
196 | 196 | return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal]) |
|
197 | 197 | |
|
198 | 198 | if configuration['debug']: |
|
199 | 199 | #attach events only for debug configuration |
|
200 | 200 | |
|
201 | 201 | def before_cursor_execute(conn, cursor, statement, |
|
202 | 202 | parameters, context, executemany): |
|
203 | 203 | context._query_start_time = time.time() |
|
204 | 204 | log.info(color_sql(">>>>> STARTING QUERY >>>>>")) |
|
205 | 205 | |
|
206 | 206 | |
|
207 | 207 | def after_cursor_execute(conn, cursor, statement, |
|
208 | 208 | parameters, context, executemany): |
|
209 | 209 | total = time.time() - context._query_start_time |
|
210 | 210 | log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total)) |
|
211 | 211 | |
|
212 | 212 | event.listen(engine, "before_cursor_execute", |
|
213 | 213 | before_cursor_execute) |
|
214 | 214 | event.listen(engine, "after_cursor_execute", |
|
215 | 215 | after_cursor_execute) |
|
216 | 216 | |
|
217 | 217 | return engine |
|
218 | 218 | |
|
219 | 219 | |
|
220 | 220 | def age(curdate): |
|
221 | 221 | """ |
|
222 | 222 | turns a datetime into an age string. |
|
223 | 223 | |
|
224 | 224 | :param curdate: datetime object |
|
225 | 225 | :rtype: unicode |
|
226 | 226 | :returns: unicode words describing age |
|
227 | 227 | """ |
|
228 | 228 | |
|
229 | 229 | from datetime import datetime |
|
230 | 230 | from webhelpers.date import time_ago_in_words |
|
231 | 231 | |
|
232 | 232 | _ = lambda s:s |
|
233 | 233 | |
|
234 | 234 | if not curdate: |
|
235 | 235 | return '' |
|
236 | 236 | |
|
237 | 237 | agescales = [(_(u"year"), 3600 * 24 * 365), |
|
238 | 238 | (_(u"month"), 3600 * 24 * 30), |
|
239 | 239 | (_(u"day"), 3600 * 24), |
|
240 | 240 | (_(u"hour"), 3600), |
|
241 | 241 | (_(u"minute"), 60), |
|
242 | 242 | (_(u"second"), 1), ] |
|
243 | 243 | |
|
244 | 244 | age = datetime.now() - curdate |
|
245 | 245 | age_seconds = (age.days * agescales[2][1]) + age.seconds |
|
246 | 246 | pos = 1 |
|
247 | 247 | for scale in agescales: |
|
248 | 248 | if scale[1] <= age_seconds: |
|
249 | 249 | if pos == 6:pos = 5 |
|
250 | 250 | return '%s %s' % (time_ago_in_words(curdate, |
|
251 | 251 | agescales[pos][0]), _('ago')) |
|
252 | 252 | pos += 1 |
|
253 | 253 | |
|
254 | 254 | return _(u'just now') |
|
255 | 255 | |
|
256 | 256 | |
|
257 | 257 | def credentials_hidder(uri): |
|
258 | 258 | """ |
|
259 | 259 | Removes user:password from given url string |
|
260 | 260 | |
|
261 | 261 | :param uri: |
|
262 | 262 | :rtype: unicode |
|
263 | 263 | :returns: filtered list of strings |
|
264 | 264 | """ |
|
265 | 265 | if not uri: |
|
266 | 266 | return '' |
|
267 | 267 | |
|
268 | 268 | proto = '' |
|
269 | 269 | |
|
270 | 270 | for pat in ('https://', 'http://'): |
|
271 | 271 | if uri.startswith(pat): |
|
272 | 272 | uri = uri[len(pat):] |
|
273 | 273 | proto = pat |
|
274 | 274 | break |
|
275 | 275 | |
|
276 | 276 | # remove passwords and username |
|
277 | 277 | uri = uri[uri.find('@') + 1:] |
|
278 | 278 | |
|
279 | 279 | # get the port |
|
280 | 280 | cred_pos = uri.find(':') |
|
281 | 281 | if cred_pos == -1: |
|
282 | 282 | host, port = uri, None |
|
283 | 283 | else: |
|
284 | 284 | host, port = uri[:cred_pos], uri[cred_pos + 1:] |
|
285 | 285 | |
|
286 | 286 | return filter(None, [proto, host, port]) |
General Comments 0
You need to be logged in to leave comments.
Login now