annotate rhodecode/lib/utils2.py @ 4147:1c8f818787b3 rhodecode-2.2.5-gpl

old style: show the full link box on summary page - no overlap or truncation
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jul 2014 19:03:23 -0400
parents ffd45b185016
children 7e5f8c12a3fc
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 # This program is free software: you can redistribute it and/or modify
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 # 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
4 # the Free Software Foundation, either version 3 of the License, or
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 # (at your option) any later version.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 #
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 # This program is distributed in the hope that it will be useful,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 # GNU General Public License for more details.
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 #
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 # You should have received a copy of the GNU General Public License
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
15 rhodecode.lib.utils
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
16 ~~~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
18 Some simple helper functions
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
20 :created_on: Jan 5, 2011
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
24 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
25
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
27 import os
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 import re
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
29 import sys
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
30 import time
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
31 import uuid
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
32 import datetime
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
33 import traceback
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
34 import webob
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
35 import urllib
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
36 import urlobject
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
37
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
38 from pylons.i18n.translation import _, ungettext
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 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
40 from rhodecode.lib.compat import json
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
43 def __get_lem():
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
44 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
45 Get language extension map based on what's inside pygments lexers
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 from pygments import lexers
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48 from string import lower
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49 from collections import defaultdict
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 d = defaultdict(lambda: [])
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 def __clean(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 s = s.lstrip('*')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 s = s.lstrip('.')
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 if s.find('[') != -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 exts = []
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59 start, stop = s.find('['), s.find(']')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 for suffix in s[start + 1:stop]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 exts.append(s[:s.find('[')] + suffix)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 return map(lower, exts)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 return map(lower, [s])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 for lx, t in sorted(lexers.LEXERS.items()):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 m = map(__clean, t[-2])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69 if m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 m = reduce(lambda x, y: x + y, m)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 for ext in m:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 desc = lx.replace('Lexer', '')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 d[ext].append(desc)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 return dict(d)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76
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
77
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 def str2bool(_str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 returs True/False value from given string, it tries to translate the
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 string into boolean
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 :param _str: string value to translate into boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84 :rtype: boolean
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 :returns: boolean from given string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 if _str is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 return False
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 if _str in (True, False):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 return _str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 _str = str(_str).strip().lower()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94
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
95 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
96 """
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 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
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 :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
100 :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
101 :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
102 """
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 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
104 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
105 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
106 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
107 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
108 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
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 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
111 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
112 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
113 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
114
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
115
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 def convert_line_endings(line, mode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 Converts a given line "line end" accordingly to given mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 Available modes are::
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 0 - Unix
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 1 - Mac
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 2 - DOS
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 :param line: given line to convert
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 :param mode: mode to convert to
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 :return: converted line according to mode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 from string import replace
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 if mode == 0:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 line = replace(line, '\r\n', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134 line = replace(line, '\r', '\n')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 elif mode == 1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 line = replace(line, '\r\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 line = replace(line, '\n', '\r')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 elif mode == 2:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140 return line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143 def detect_mode(line, default):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 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
146 given default value is returned
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 :param line: str line
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 :param default: default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 :rtype: int
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151 :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
152 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 if line.endswith('\r\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 return 2
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 elif line.endswith('\n'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 return 0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 elif line.endswith('\r'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 return 1
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 return default
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 def generate_api_key(username, salt=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 Generates unique API key for given username, if salt is not given
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 it'll be generated from some random string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168 :param username: username as string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 :param salt: salt to hash generate KEY
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 :returns: sha1 hash from username+salt
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 from tempfile import _RandomNameSequence
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 import hashlib
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 if salt is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 salt = _RandomNameSequence().next()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 return hashlib.sha1(username + salt).hexdigest()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181
2845
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
182 def safe_int(val, default=None):
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
183 """
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
184 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
185 instead
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 :param val:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
188 :param default:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
189 """
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
190
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
191 try:
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
192 val = int(val)
3218
c0b0351233ec safe_int should also catch TypeError
Marcin Kuzminski <marcin@python-works.com>
parents: 3056
diff changeset
193 except (ValueError, TypeError):
2845
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
194 val = default
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
195
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
196 return val
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
197
6b176c679896 failsafe the GET `page` argument
Marcin Kuzminski <marcin@python-works.com>
parents: 2759
diff changeset
198
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 def safe_unicode(str_, from_encoding=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 safe unicode function. Does few trick to turn str_ into unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 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
204 by chardet library if it fails fallback to unicode with errors replaced
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 :param str_: string to decode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 :returns: unicode object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 if isinstance(str_, unicode):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211 return str_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213 if not from_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 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
215 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
216 '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
217 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
218
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
219 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
220 from_encoding = [from_encoding]
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
221
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
222 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
223 return unicode(str_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
224 except UnicodeDecodeError:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
225 pass
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
226
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
227 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
228 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
229 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
230 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
231 pass
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
232
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
233 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
234 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
235 encoding = chardet.detect(str_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
236 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
237 raise Exception()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
238 return str_.decode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
239 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
240 return unicode(str_, from_encoding[0], 'replace')
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
241
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 def safe_str(unicode_, to_encoding=None):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
244 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
245 safe str function. Does few trick to turn unicode_ into string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
246
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
247 In case of UnicodeEncodeError we try to return it with encoding detected
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
248 by chardet library if it fails fallback to string with errors replaced
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 :param unicode_: unicode to encode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
251 :rtype: str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
252 :returns: str object
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
253 """
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 it's not basestr cast to str
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
256 if not isinstance(unicode_, basestring):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
257 return str(unicode_)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
258
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
259 if isinstance(unicode_, str):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
260 return unicode_
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
261
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
262 if not to_encoding:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
263 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
264 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
265 '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
266 to_encoding = DEFAULT_ENCODINGS
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
267
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
268 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
269 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
270
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 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
272 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
273 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
274 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
275 pass
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
276
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
277 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
278 import chardet
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
279 encoding = chardet.detect(unicode_)['encoding']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
280 if encoding is None:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
281 raise UnicodeEncodeError()
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 unicode_.encode(encoding)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
284 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
285 return unicode_.encode(to_encoding[0], 'replace')
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
286
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
287
3018
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
288 def remove_suffix(s, suffix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
289 if s.endswith(suffix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
290 s = s[:-1 * len(suffix)]
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
291 return s
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
292
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
293
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
294 def remove_prefix(s, prefix):
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
295 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
296 s = s[len(prefix):]
3018
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
297 return s
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
298
023f7873ef59 added caching layer into RSS/ATOM feeds
Marcin Kuzminski <marcin@python-works.com>
parents: 3008
diff changeset
299
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
300 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
301 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
302 Custom engine_from_config functions that makes sure we use NullPool for
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
303 file based sqlite databases. This prevents errors on sqlite. This only
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
304 applies to sqlalchemy versions < 0.7.0
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
305
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
306 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
307 import sqlalchemy
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
308 from sqlalchemy import engine_from_config as efc
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
309 import logging
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 if int(sqlalchemy.__version__.split('.')[1]) < 7:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
312
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
313 # This solution should work for sqlalchemy < 0.7.0, and should use
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
314 # proxy=TimerProxy() for execution time profiling
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
315
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
316 from sqlalchemy.pool import NullPool
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
317 url = configuration[prefix + 'url']
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
318
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
319 if url.startswith('sqlite'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
320 kwargs.update({'poolclass': NullPool})
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
321 return efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
322 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
323 import time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
324 from sqlalchemy import event
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
325 from sqlalchemy.engine import Engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
326
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
327 log = logging.getLogger('sqlalchemy.engine')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
328 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
329 engine = efc(configuration, prefix, **kwargs)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
330
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
331 def color_sql(sql):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
332 COLOR_SEQ = "\033[1;%dm"
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
333 COLOR_SQL = YELLOW
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
334 normal = '\x1b[0m'
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
335 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
336
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
337 if configuration['debug']:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
338 #attach events only for debug configuration
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
339
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
340 def before_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
341 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
342 context._query_start_time = time.time()
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
343 log.info(color_sql(">>>>> STARTING QUERY >>>>>"))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
344
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
345 def after_cursor_execute(conn, cursor, statement,
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
346 parameters, context, executemany):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
347 total = time.time() - context._query_start_time
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
348 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
349
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
350 event.listen(engine, "before_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
351 before_cursor_execute)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
352 event.listen(engine, "after_cursor_execute",
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
353 after_cursor_execute)
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 return engine
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
356
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
357
3644
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
358 def age(prevdate, show_short_version=False, now=None):
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
359 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
360 turns a datetime into an age string.
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
361 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
362 example: 2days ago, instead of 2 days and 23 hours ago.
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
363
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
364 :param prevdate: datetime object
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
365 :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
366 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
367 :returns: unicode words describing age
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
368 """
3644
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
369 now = now or datetime.datetime.now()
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
370 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
371 deltas = {}
2902
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
372 future = False
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
373
2902
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
374 if prevdate > now:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
375 now, prevdate = prevdate, now
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
376 future = True
3644
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
377 if future:
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
378 prevdate = prevdate.replace(microsecond=0)
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 # Get date parts deltas
3644
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
380 from dateutil import relativedelta
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
381 for part in order:
3644
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
382 d = relativedelta.relativedelta(now, prevdate)
71860d0737e7 - age tests cannot be dynamic, there are cases when age calculation
Marcin Kuzminski <marcin@python-works.com>
parents: 3631
diff changeset
383 deltas[part] = getattr(d, part + 's')
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
384
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
385 # 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
386 # 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
387 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
388 part = order[num]
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
389 carry_part = order[num - 1]
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
390
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
391 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
392 deltas[part] += length
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
393 deltas[carry_part] -= 1
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
394
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
395 # 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
396 # 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
397 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
398 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
399 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
400 (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
401 deltas['day'] += 29
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
402 else:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
403 deltas['day'] += month_lengths[prevdate.month - 1]
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
404
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
405 deltas['month'] -= 1
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
406
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
407 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
408 deltas['month'] += 12
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
409 deltas['year'] -= 1
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
410
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
411 # Format the result
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
412 fmt_funcs = {
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
413 '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
414 '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
415 '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
416 '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
417 '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
418 '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
419 }
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
420
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
421 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
422 value = deltas[part]
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
423 if value == 0:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
424 continue
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
425
2303
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
426 if i < 5:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
427 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
428 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
429 else:
7090e394df06 Rewrite of the age() utility function so it can be translated.
Vincent Duvert <vincent@duvert.net>
parents: 2278
diff changeset
430 sub_value = 0
2367
86aa4f1f130b white space cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 2303
diff changeset
431
3536
ed48d17836a4 WIP: Changelog view
Leonardo <leo@unity3d.com>
parents: 3378
diff changeset
432 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
433 if future:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
434 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
435 else:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
436 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
437 if future:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
438 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
439 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
440 else:
e2b2791d1e7c fixed #597 commits in future get negative age.
Marcin Kuzminski <marcin@python-works.com>
parents: 2882
diff changeset
441 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
442 fmt_funcs[sub_part](sub_value))
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
443
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
444 return _(u'just now')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
445
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 def uri_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
448 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
449 Removes user:password from given url string
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
450
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
451 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
452 :rtype: unicode
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
453 :returns: filtered list of strings
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
454 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
455 if not uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
456 return ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
457
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
458 proto = ''
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
459
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
460 for pat in ('https://', 'http://'):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
461 if uri.startswith(pat):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
462 uri = uri[len(pat):]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
463 proto = pat
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
464 break
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
465
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
466 # remove passwords and username
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
467 uri = uri[uri.find('@') + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
468
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
469 # get the port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
470 cred_pos = uri.find(':')
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
471 if cred_pos == -1:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
472 host, port = uri, None
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
473 else:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
474 host, port = uri[:cred_pos], uri[cred_pos + 1:]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
475
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
476 return filter(None, [proto, host, port])
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
477
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 def credentials_filter(uri):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
480 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
481 Returns a url with removed credentials
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
482
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
483 :param uri:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
484 """
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 uri = uri_filter(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
487 #check if we have port
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
488 if len(uri) > 2 and uri[2]:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
489 uri[2] = ':' + uri[2]
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
490
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
491 return ''.join(uri)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
492
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
493
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
494 def get_clone_url(uri_tmpl, qualifed_home_url, repo_name, repo_id, **override):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
495 parsed_url = urlobject.URLObject(qualifed_home_url)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
496 decoded_path = safe_unicode(urllib.unquote(parsed_url.path.rstrip('/')))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
497 args = {
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
498 'scheme': parsed_url.scheme,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
499 'user': '',
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
500 'netloc': parsed_url.netloc+decoded_path, # path if we use proxy-prefix
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
501 'prefix': decoded_path,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
502 'repo': repo_name,
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
503 'repoid': str(repo_id)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
504 }
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
505 args.update(override)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
506 args['user'] = urllib.quote(safe_str(args['user']))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
507
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
508 for k, v in args.items():
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
509 uri_tmpl = uri_tmpl.replace('{%s}' % k, v)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
510
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
511 # remove leading @ sign if it's present. Case of empty user
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
512 url_obj = urlobject.URLObject(uri_tmpl)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
513 url = url_obj.with_netloc(url_obj.netloc.lstrip('@'))
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
514
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
515 return safe_unicode(url)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
516
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
517
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
518 def get_changeset_safe(repo, rev):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
519 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
520 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
521 repo it returns a Dummy one instead
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
522
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
523 :param repo:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
524 :param rev:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
525 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
526 from rhodecode.lib.vcs.backends.base import BaseRepository
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
527 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
528 from rhodecode.lib.vcs.backends.base import EmptyChangeset
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
529 if not isinstance(repo, BaseRepository):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
530 raise Exception('You must pass an Repository '
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
531 'object as first argument got %s', type(repo))
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
532
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
533 try:
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
534 cs = repo.get_changeset(rev)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
535 except (RepositoryError, LookupError):
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
536 cs = EmptyChangeset(requested_revision=rev)
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
537 return cs
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
538
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
539
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
540 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
541 if dt:
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
542 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
543
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
544
2726
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
545 def time_to_datetime(tm):
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
546 if tm:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
547 if isinstance(tm, basestring):
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
548 try:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
549 tm = float(tm)
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
550 except ValueError:
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
551 return
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
552 return datetime.datetime.fromtimestamp(tm)
aa17c7a1b8a5 Implemented basic locking functionality.
Marcin Kuzminski <marcin@python-works.com>
parents: 2699
diff changeset
553
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
554 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
555
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
556
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
557 def extract_mentioned_users(s):
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
558 """
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
559 Returns unique usernames from given string s that have @mention
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
560
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
561 :param s: string to get mentions
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
562 """
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
563 usrs = set()
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
564 for username in re.findall(MENTIONS_REGEX, s):
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
565 usrs.add(username)
2109
8ecfed1d8f8b utils/conf
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
566
2201
ea5ff843b200 #426 fixed mention extracting regex
Marcin Kuzminski <marcin@python-works.com>
parents: 2109
diff changeset
567 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
568
2699
4eef5eeb81a3 fixed sorting by last_login in users admin page
Marcin Kuzminski <marcin@python-works.com>
parents: 2684
diff changeset
569
2674
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
570 class AttributeDict(dict):
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
571 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
572 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
573 __setattr__ = dict.__setitem__
a221706dab50 merged + fixed pull request #62: Implemented metatags and visualisation options.
Marcin Kuzminski <marcin@python-works.com>
parents: 2367
diff changeset
574 __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
575
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
576
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
577 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
578 """
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
579 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
580 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
581 """
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
582 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
583 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
584 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
585 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
586
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
587 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
588 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
589 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
590
12fce5e499d5 obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
591
12fce5e499d5 obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents: 2869
diff changeset
592 def obfuscate_url_pw(engine):
3454
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
593 _url = engine or ''
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
594 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
595 try:
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
596 _url = sa_url.make_url(engine)
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
597 if _url.password:
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
598 _url.password = 'XXXXX'
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3590
diff changeset
599 except Exception:
3454
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
600 pass
867c4d7fd2d8 catch errors in obfuscate password function
Marcin Kuzminski <marcin@python-works.com>
parents: 3378
diff changeset
601 return str(_url)
2969
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
602
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
603
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
604 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
605 req = webob.Request(environ)
5085e51fba3a Implemented #628: Pass server URL to rc-extensions hooks
Marcin Kuzminski <marcin@python-works.com>
parents: 2902
diff changeset
606 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
607
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
608
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
609 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
610 """
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
611 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
612 AttributeDict object
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
613 """
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
614 if not env:
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
615 env = os.environ
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
616
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
617 try:
3590
47631be9f449 fix GIT env extraction
Marcin Kuzminski <marcin@python-works.com>
parents: 3577
diff changeset
618 rc_extras = json.loads(env['RC_SCM_DATA'])
3631
10b4e34841a4 Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents: 3590
diff changeset
619 except Exception:
3577
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
620 print os.environ
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
621 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
622 rc_extras = {}
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
623
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
624 try:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
625 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
626 'action', 'ip']:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
627 rc_extras[k]
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
628 except KeyError, e:
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
629 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
630
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
631 return AttributeDict(rc_extras)
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
632
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
633
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
634 def _set_extras(extras):
238486bb71ab Switched handling of RhodeCode extra params in consistent way
Marcin Kuzminski <marcin@python-works.com>
parents: 3552
diff changeset
635 os.environ['RC_SCM_DATA'] = json.dumps(extras)
3840
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
636
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
637
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
638 def unique_id(hexlen=32):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
639 alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz"
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
640 return suuid(truncate_to=hexlen, alphabet=alphabet)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
641
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
642
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
643 def suuid(url=None, truncate_to=22, alphabet=None):
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
644 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
645 Generate and return a short URL safe UUID.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
646
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
647 If the url parameter is provided, set the namespace to the provided
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
648 URL and generate a UUID.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
649
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
650 :param url to get the uuid for
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
651 :truncate_to: truncate the basic 22 UUID to shorter version
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
652
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
653 The IDs won't be universally unique any longer, but the probability of
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
654 a collision will still be very low.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
655 """
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
656 # Define our alphabet.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
657 _ALPHABET = alphabet or "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
658
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
659 # If no URL is given, generate a random UUID.
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
660 if url is None:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
661 unique_id = uuid.uuid4().int
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
662 else:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
663 unique_id = uuid.uuid3(uuid.NAMESPACE_URL, url).int
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
664
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
665 alphabet_length = len(_ALPHABET)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
666 output = []
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
667 while unique_id > 0:
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
668 digit = unique_id % alphabet_length
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
669 output.append(_ALPHABET[digit])
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
670 unique_id = int(unique_id / alphabet_length)
dc4644865e8b Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents: 3644
diff changeset
671 return "".join(output)[:truncate_to]
4012
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
672
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
673
4012
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
674 def get_current_rhodecode_user():
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
675 """
4014
7c0eff86b249 fixed typo
Marcin Kuzminski <marcin@python-works.com>
parents: 4012
diff changeset
676 Gets rhodecode user from threadlocal tmpl_context variable if it's
4012
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
677 defined, else returns None.
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
678 """
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
679 from pylons import tmpl_context
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
680 if hasattr(tmpl_context, 'rhodecode_user'):
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
681 return tmpl_context.rhodecode_user
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
682
55dbc440878b Fixed bug with log_delete hook didn't properly store
Marcin Kuzminski <marcin@python-works.com>
parents: 3960
diff changeset
683 return None
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
684
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
685
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
686 class OptionalAttr(object):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
687 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
688 Special Optional Option that defines other attribute. Example::
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
689
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
690 def test(apiuser, userid=Optional(OAttr('apiuser')):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
691 user = Optional.extract(userid)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
692 # calls
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
693
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
694 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
695
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
696 def __init__(self, attr_name):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
697 self.attr_name = attr_name
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
698
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
699 def __repr__(self):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
700 return '<OptionalAttr:%s>' % self.attr_name
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
701
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
702 def __call__(self):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
703 return self
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
704
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
705 #alias
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
706 OAttr = OptionalAttr
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
707
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
708
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
709 class Optional(object):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
710 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
711 Defines an optional parameter::
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
712
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
713 param = param.getval() if isinstance(param, Optional) else param
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
714 param = param() if isinstance(param, Optional) else param
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
715
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
716 is equivalent of::
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
717
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
718 param = Optional.extract(param)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
719
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
720 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
721
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
722 def __init__(self, type_):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
723 self.type_ = type_
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
724
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
725 def __repr__(self):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
726 return '<Optional:%s>' % self.type_.__repr__()
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
727
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
728 def __call__(self):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
729 return self.getval()
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
730
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
731 def getval(self):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
732 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
733 returns value from this Optional instance
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
734 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
735 if isinstance(self.type_, OAttr):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
736 # use params name
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
737 return self.type_.attr_name
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
738 return self.type_
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
739
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
740 @classmethod
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
741 def extract(cls, val):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
742 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
743 Extracts value from Optional() instance
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
744
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
745 :param val:
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
746 :return: original value if it's not Optional instance else
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
747 value of instance
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
748 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
749 if isinstance(val, cls):
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
750 return val.getval()
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 4014
diff changeset
751 return val