annotate rhodecode/lib/utils2.py @ 3599:8087731920d8 beta

breadcrumbs: make repo names link to summary pages
author Mads Kiilerich <madski@unity3d.com>
date Tue, 26 Mar 2013 15:53:11 +0100
parents 47631be9f449
children 10b4e34841a4
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
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
26 import os
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 import re
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
28 import sys
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
29 import time
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
30 import datetime
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
31 import traceback
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
32 import webob
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
33
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
34 from pylons.i18n.translation import _, ungettext
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35 from rhodecode.lib.vcs.utils.lazy import LazyProperty
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
36 from rhodecode.lib.compat import json
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 def __get_lem():
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 Get language extension map based on what's inside pygments lexers
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 from pygments import lexers
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 from string import lower
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 from collections import defaultdict
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
46
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
47 d = defaultdict(lambda: [])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 def __clean(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 s = s.lstrip('*')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 s = s.lstrip('.')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 if s.find('[') != -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 exts = []
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 start, stop = s.find('['), s.find(']')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 for suffix in s[start + 1:stop]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 exts.append(s[:s.find('[')] + suffix)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 return map(lower, exts)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 return map(lower, [s])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 for lx, t in sorted(lexers.LEXERS.items()):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 m = map(__clean, t[-2])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 if m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 m = reduce(lambda x, y: x + y, m)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 for ext in m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 desc = lx.replace('Lexer', '')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 d[ext].append(desc)
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 return dict(d)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
73
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 def str2bool(_str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 returs True/False value from given string, it tries to translate the
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 string into boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 :param _str: string value to translate into boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 :rtype: boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 :returns: boolean from given string
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 if _str is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 return False
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 if _str in (True, False):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 return _str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 _str = str(_str).strip().lower()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
91 def aslist(obj, sep=None, strip=True):
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
92 """
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
93 Returns given string separated by sep as list
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
94
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
95 :param obj:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
96 :param sep:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
97 :param strip:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
98 """
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
99 if isinstance(obj, (basestring)):
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
100 lst = obj.split(sep)
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
101 if strip:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
102 lst = [v.strip() for v in lst]
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
103 return lst
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
104 elif isinstance(obj, (list, tuple)):
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
105 return obj
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
106 elif obj is None:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
107 return []
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
108 else:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
109 return [obj]
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
110
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
111
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112 def convert_line_endings(line, mode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 Converts a given line "line end" accordingly to given mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 Available modes are::
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 0 - Unix
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 1 - Mac
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 2 - DOS
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 :param line: given line to convert
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 :param mode: mode to convert to
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 :return: converted line according to mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 from string import replace
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 if mode == 0:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 line = replace(line, '\r\n', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 line = replace(line, '\r', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 elif mode == 1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 line = replace(line, '\r\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 line = replace(line, '\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 elif mode == 2:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 return line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 def detect_mode(line, default):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 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
142 given default value is returned
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 :param line: str line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 :param default: default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 :rtype: int
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 :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
148 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 if line.endswith('\r\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 return 2
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 elif line.endswith('\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 return 0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 elif line.endswith('\r'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 return 1
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 return default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 def generate_api_key(username, salt=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161 Generates unique API key for given username, if salt is not given
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 it'll be generated from some random string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 :param username: username as string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 :param salt: salt to hash generate KEY
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 :returns: sha1 hash from username+salt
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 from tempfile import _RandomNameSequence
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 import hashlib
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 if salt is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 salt = _RandomNameSequence().next()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 return hashlib.sha1(username + salt).hexdigest()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177
2845
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
178 def safe_int(val, default=None):
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
179 """
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
180 Returns int() of val if val is not convertable to int use default
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
181 instead
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
182
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
183 :param val:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
184 :param default:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
185 """
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
186
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
187 try:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
188 val = int(val)
3218
c0b0351233ec safe_int should also catch TypeError
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
189 except (ValueError, TypeError):
2845
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
190 val = default
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
191
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
192 return val
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
193
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
194
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 def safe_unicode(str_, from_encoding=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 safe unicode function. Does few trick to turn str_ into unicode
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 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
200 by chardet library if it fails fallback to unicode with errors replaced
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 :param str_: string to decode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 :returns: unicode object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 if isinstance(str_, unicode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 return str_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 if not from_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 import rhodecode
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
211 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
212 'utf8'), sep=',')
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
213 from_encoding = DEFAULT_ENCODINGS
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
214
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
215 if not isinstance(from_encoding, (list, tuple)):
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
216 from_encoding = [from_encoding]
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
217
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
218 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
219 return unicode(str_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
220 except UnicodeDecodeError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221 pass
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
223 for enc in from_encoding:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
224 try:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
225 return unicode(str_, enc)
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
226 except UnicodeDecodeError:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
227 pass
2109
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 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
230 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
231 encoding = chardet.detect(str_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 raise Exception()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 return str_.decode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 except (ImportError, UnicodeDecodeError, Exception):
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
236 return unicode(str_, from_encoding[0], 'replace')
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 def safe_str(unicode_, to_encoding=None):
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 safe str function. Does few trick to turn unicode_ into string
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 In case of UnicodeEncodeError we try to return it with encoding detected
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 by chardet library if it fails fallback to string with errors replaced
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 :param unicode_: unicode to encode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 :returns: str object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
249 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
250
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 # if it's not basestr cast to str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 if not isinstance(unicode_, basestring):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 return str(unicode_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
254
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
255 if isinstance(unicode_, str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 return unicode_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258 if not to_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 import rhodecode
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
260 DEFAULT_ENCODINGS = aslist(rhodecode.CONFIG.get('default_encoding',
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
261 'utf8'), sep=',')
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
262 to_encoding = DEFAULT_ENCODINGS
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
264 if not isinstance(to_encoding, (list, tuple)):
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
265 to_encoding = [to_encoding]
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
266
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
267 for enc in to_encoding:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
268 try:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
269 return unicode_.encode(enc)
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
270 except UnicodeEncodeError:
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
271 pass
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
272
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
273 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
274 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
275 encoding = chardet.detect(unicode_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 raise UnicodeEncodeError()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 return unicode_.encode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 except (ImportError, UnicodeEncodeError):
3008
6e76b4892d72 Implemented #647, option to pass list of default encoding used to encode to/decode from unicode
Marcin Kuzminski <marcin@python-works.com>
parents: 2969
diff changeset
281 return unicode_.encode(to_encoding[0], 'replace')
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
282
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
283 return safe_str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
285
3018
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
286 def remove_suffix(s, suffix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
287 if s.endswith(suffix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
288 s = s[:-1 * len(suffix)]
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
289 return s
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
290
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
291
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
292 def remove_prefix(s, prefix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
293 if s.startswith(prefix):
3056
6104dfd35b16 Implemented #379 defaults settings page for creation of repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3018
diff changeset
294 s = s[len(prefix):]
3018
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
295 return s
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
296
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
297
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
298 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
299 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 Custom engine_from_config functions that makes sure we use NullPool for
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 file based sqlite databases. This prevents errors on sqlite. This only
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 applies to sqlalchemy versions < 0.7.0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305 import sqlalchemy
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 from sqlalchemy import engine_from_config as efc
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 import logging
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 if int(sqlalchemy.__version__.split('.')[1]) < 7:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
310
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
311 # This solution should work for sqlalchemy < 0.7.0, and should use
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312 # proxy=TimerProxy() for execution time profiling
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 from sqlalchemy.pool import NullPool
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315 url = configuration[prefix + 'url']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 if url.startswith('sqlite'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318 kwargs.update({'poolclass': NullPool})
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 return efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 import time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 from sqlalchemy import event
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 from sqlalchemy.engine import Engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 log = logging.getLogger('sqlalchemy.engine')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 engine = efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 def color_sql(sql):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330 COLOR_SEQ = "\033[1;%dm"
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 COLOR_SQL = YELLOW
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 normal = '\x1b[0m'
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 if configuration['debug']:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336 #attach events only for debug configuration
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 def before_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 context._query_start_time = time.time()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 log.info(color_sql(">>>>> STARTING QUERY >>>>>"))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 def after_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 total = time.time() - context._query_start_time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 event.listen(engine, "before_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349 before_cursor_execute)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 event.listen(engine, "after_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 after_cursor_execute)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 return engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
354
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
355
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
356 def age(prevdate, show_short_version=False):
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
358 turns a datetime into an age string.
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
359 If show_short_version is True, then it will generate a not so accurate but shorter string,
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
360 example: 2days ago, instead of 2 days and 23 hours ago.
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
361
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
362 :param prevdate: datetime object
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
363 :param show_short_version: if it should aproximate the date and return a shorter string
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
364 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365 :returns: unicode words describing age
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
366 """
3261
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
367 now = datetime.datetime.now()
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
368 now = now.replace(microsecond=0)
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
369 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
370 deltas = {}
2902
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
371 future = False
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
372
2902
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
373 if prevdate > now:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
374 now, prevdate = prevdate, now
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
375 future = True
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
376
3261
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
377 # Get date parts deltas
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
378 for part in order:
3261
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
379 if future:
3378
24ffe239e3d7 fix dateutil import
Marcin Kuzminski <marcin@python-works.com>
parents: 3261
diff changeset
380 from dateutil import relativedelta
24ffe239e3d7 fix dateutil import
Marcin Kuzminski <marcin@python-works.com>
parents: 3261
diff changeset
381 d = relativedelta.relativedelta(now, prevdate)
3261
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
382 deltas[part] = getattr(d, part + 's')
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
383 else:
54a439edaf99 fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
Marcin Kuzminski <marcin@python-works.com>
parents: 3218
diff changeset
384 deltas[part] = getattr(now, part) - getattr(prevdate, part)
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
385
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
386 # 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
387 # not 1 hour, -59 minutes and -59 seconds)
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
388 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
389 part = order[num]
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
390 carry_part = order[num - 1]
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
391
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
392 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
393 deltas[part] += length
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
394 deltas[carry_part] -= 1
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
395
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
396 # 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
397 # 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
398 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
399 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
400 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
401 (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
402 deltas['day'] += 29
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
403 else:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
404 deltas['day'] += month_lengths[prevdate.month - 1]
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
405
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
406 deltas['month'] -= 1
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
407
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
408 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
409 deltas['month'] += 12
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
410 deltas['year'] -= 1
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
411
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
412 # Format the result
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
413 fmt_funcs = {
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
414 '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
415 '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
416 '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
417 '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
418 '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
419 '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
420 }
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
421
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
422 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
423 value = deltas[part]
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
424 if value == 0:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
425 continue
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
426
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
427 if i < 5:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
428 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
429 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
430 else:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
431 sub_value = 0
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
432
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
433 if sub_value == 0 or show_short_version:
2902
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
434 if future:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
435 return _(u'in %s') % fmt_funcs[part](value)
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
436 else:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
437 return _(u'%s ago') % fmt_funcs[part](value)
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
438 if future:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
439 return _(u'in %s and %s') % (fmt_funcs[part](value),
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
440 fmt_funcs[sub_part](sub_value))
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
441 else:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
442 return _(u'%s and %s ago') % (fmt_funcs[part](value),
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
443 fmt_funcs[sub_part](sub_value))
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
444
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
445 return _(u'just now')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
446
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
447
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
448 def uri_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
449 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
450 Removes user:password from given url string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
451
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
452 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
453 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
454 :returns: filtered list of strings
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
455 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
456 if not uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
457 return ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
458
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
459 proto = ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
460
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
461 for pat in ('https://', 'http://'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
462 if uri.startswith(pat):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
463 uri = uri[len(pat):]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
464 proto = pat
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
465 break
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
466
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
467 # remove passwords and username
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
468 uri = uri[uri.find('@') + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
469
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
470 # get the port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
471 cred_pos = uri.find(':')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
472 if cred_pos == -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
473 host, port = uri, None
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
474 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
475 host, port = uri[:cred_pos], uri[cred_pos + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
476
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
477 return filter(None, [proto, host, port])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
478
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
479
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
480 def credentials_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
481 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
482 Returns a url with removed credentials
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
483
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
484 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
485 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
486
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
487 uri = uri_filter(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
488 #check if we have port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
489 if len(uri) > 2 and uri[2]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
490 uri[2] = ':' + uri[2]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
491
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
492 return ''.join(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
493
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
494
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
495 def get_changeset_safe(repo, rev):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
496 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
497 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
498 repo it returns a Dummy one instead
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
499
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
500 :param repo:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
501 :param rev:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
502 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
503 from rhodecode.lib.vcs.backends.base import BaseRepository
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
504 from rhodecode.lib.vcs.exceptions import RepositoryError
2684
2b6939a77052 Bumped mercurial version to 2.3
Marcin Kuzminski <marcin@python-works.com>
parents: 2674
diff changeset
505 from rhodecode.lib.vcs.backends.base import EmptyChangeset
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
506 if not isinstance(repo, BaseRepository):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
507 raise Exception('You must pass an Repository '
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
508 'object as first argument got %s', type(repo))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
509
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
510 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
511 cs = repo.get_changeset(rev)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
512 except RepositoryError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
513 cs = EmptyChangeset(requested_revision=rev)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
514 return cs
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
515
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
516
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
517 def datetime_to_time(dt):
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
518 if dt:
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
519 return time.mktime(dt.timetuple())
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
520
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
521
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
522 def time_to_datetime(tm):
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
523 if tm:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
524 if isinstance(tm, basestring):
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
525 try:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
526 tm = float(tm)
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
527 except ValueError:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
528 return
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
529 return datetime.datetime.fromtimestamp(tm)
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
530
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
531 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
532
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
533
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
534 def extract_mentioned_users(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
535 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
536 Returns unique usernames from given string s that have @mention
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
537
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
538 :param s: string to get mentions
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
539 """
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
540 usrs = set()
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
541 for username in re.findall(MENTIONS_REGEX, s):
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
542 usrs.add(username)
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
543
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
544 return sorted(list(usrs), key=lambda k: k.lower())
2674
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
545
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
546
2674
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
547 class AttributeDict(dict):
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
548 def __getattr__(self, attr):
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
549 return self.get(attr, None)
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
550 __setattr__ = dict.__setitem__
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
551 __delattr__ = dict.__delitem__
2869
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
552
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
553
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
554 def fix_PATH(os_=None):
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
555 """
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
556 Get current active python path, and append it to PATH variable to fix issues
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
557 of subprocess calls and different python versions
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
558 """
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
559 if os_ is None:
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
560 import os
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
561 else:
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
562 os = os_
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
563
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
564 cur_path = os.path.split(sys.executable)[0]
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
565 if not os.environ['PATH'].startswith(cur_path):
ccbdff90e5a0 fix for issue #578 git hooks sometimes cannot be executed due to different python they runned under, this commit tries to fix that by altering the PATH env variable using current python that rhodecode is running
Marcin Kuzminski <marcin@python-works.com>
parents: 2845
diff changeset
566 os.environ['PATH'] = '%s:%s' % (cur_path, os.environ['PATH'])
2882
12fce5e499d5 obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
567
12fce5e499d5 obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
568
12fce5e499d5 obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
569 def obfuscate_url_pw(engine):
3454
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
570 _url = engine or ''
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
571 from sqlalchemy.engine import url as sa_url
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
572 try:
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
573 _url = sa_url.make_url(engine)
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
574 if _url.password:
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
575 _url.password = 'XXXXX'
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
576 except:
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
577 pass
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
578 return str(_url)
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
579
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
580
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
581 def get_server_url(environ):
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
582 req = webob.Request(environ)
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
583 return req.host_url + req.script_name
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
584
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
585
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
586 def _extract_extras(env=None):
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
587 """
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
588 Extracts the rc extras data from os.environ, and wraps it into named
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
589 AttributeDict object
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
590 """
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
591 if not env:
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
592 env = os.environ
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
593
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
594 try:
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
595 rc_extras = json.loads(env['RC_SCM_DATA'])
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
596 except:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
597 print os.environ
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
598 print >> sys.stderr, traceback.format_exc()
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
599 rc_extras = {}
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
600
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
601 try:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
602 for k in ['username', 'repository', 'locked_by', 'scm', 'make_lock',
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
603 'action', 'ip']:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
604 rc_extras[k]
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
605 except KeyError, e:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
606 raise Exception('Missing key %s in os.environ %s' % (e, rc_extras))
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
607
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
608 return AttributeDict(rc_extras)
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
609
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
610
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
611 def _set_extras(extras):
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
612 os.environ['RC_SCM_DATA'] = json.dumps(extras)