annotate rhodecode/lib/__init__.py @ 2031:82a88013a3fd

merge 1.3 into stable
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 26 Feb 2012 17:25:09 +0200
parents 54e84659cb3a 6020e3884a58
children abe75448253c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
1 # -*- coding: utf-8 -*-
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
2 """
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
3 rhodecode.lib.__init__
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
5
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
6 Some simple helper functions
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
7
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
8 :created_on: Jan 5, 2011
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
9 :author: marcink
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1818
diff changeset
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
11 :license: GPLv3, see COPYING for more details.
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
12 """
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
13 # This program is free software: you can redistribute it and/or modify
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
14 # it under the terms of the GNU General Public License as published by
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
15 # the Free Software Foundation, either version 3 of the License, or
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
16 # (at your option) any later version.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
17 #
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
18 # This program is distributed in the hope that it will be useful,
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
21 # GNU General Public License for more details.
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
22 #
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
23 # You should have received a copy of the GNU General Public License
1206
a671db5bdd58 fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents: 1203
diff changeset
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
914
110a00c181de Added force https option into config files
Marcin Kuzminski <marcin@python-works.com>
parents: 547
diff changeset
25
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
26 import os
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
27 import re
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
28 from rhodecode.lib.vcs.utils.lazy import LazyProperty
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
29
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
30
1302
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
31 def __get_lem():
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
32 from pygments import lexers
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
33 from string import lower
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
34 from collections import defaultdict
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
35
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
36 d = defaultdict(lambda: [])
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
37
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
38 def __clean(s):
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
39 s = s.lstrip('*')
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
40 s = s.lstrip('.')
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
41
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
42 if s.find('[') != -1:
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
43 exts = []
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
44 start, stop = s.find('['), s.find(']')
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
45
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
46 for suffix in s[start + 1:stop]:
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
47 exts.append(s[:s.find('[')] + suffix)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
48 return map(lower, exts)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
49 else:
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
50 return map(lower, [s])
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
51
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
52 for lx, t in sorted(lexers.LEXERS.items()):
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
53 m = map(__clean, t[-2])
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
54 if m:
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
55 m = reduce(lambda x, y: x + y, m)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
56 for ext in m:
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
57 desc = lx.replace('Lexer', '')
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
58 d[ext].append(desc)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
59
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
60 return dict(d)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
61
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
62 # language map is also used by whoosh indexer, which for those specified
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
63 # extensions will index it's content
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
64 LANGUAGES_EXTENSIONS_MAP = __get_lem()
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
65
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
66 # Additional mappings that are not present in the pygments lexers
1302
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
67 # NOTE: that this will overide any mappings in LANGUAGES_EXTENSIONS_MAP
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
68 ADDITIONAL_MAPPINGS = {'xaml': 'XAML'}
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
69
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
70 LANGUAGES_EXTENSIONS_MAP.update(ADDITIONAL_MAPPINGS)
f0e904651f21 moved LANGUAGE_EXTENSION_MAP to lib, and made whoosh indexer use the same map
Marcin Kuzminski <marcin@python-works.com>
parents: 1300
diff changeset
71
1605
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
72 # list of readme files to search in file tree and display in summary
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
73 # attached weights defines the search order lower is first
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
74 ALL_READMES = [
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
75 ('readme', 0), ('README', 0), ('Readme', 0),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
76 ('doc/readme', 1), ('doc/README', 1), ('doc/Readme', 1),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
77 ('Docs/readme', 2), ('Docs/README', 2), ('Docs/Readme', 2),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
78 ('DOCS/readme', 2), ('DOCS/README', 2), ('DOCS/Readme', 2),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
79 ('docs/readme', 2), ('docs/README', 2), ('docs/Readme', 2),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
80 ]
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
81
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
82 # extension together with weights to search lower is first
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
83 RST_EXTS = [
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
84 ('', 0), ('.rst', 1), ('.rest', 1),
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
85 ('.RST', 2), ('.REST', 2),
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
86 ('.txt', 3), ('.TXT', 3)
1605
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
87 ]
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
88
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
89 MARKDOWN_EXTS = [
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
90 ('.md', 1), ('.MD', 1),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
91 ('.mkdn', 2), ('.MKDN', 2),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
92 ('.mdown', 3), ('.MDOWN', 3),
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
93 ('.markdown', 4), ('.MARKDOWN', 4)
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
94 ]
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
95
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
96 PLAIN_EXTS = [('.text', 2), ('.TEXT', 2)]
1605
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
97
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
98 ALL_EXTS = MARKDOWN_EXTS + RST_EXTS + PLAIN_EXTS
df59c0503636 implements #215 Repository view uses a README (text/markdown + rst)
Marcin Kuzminski <marcin@python-works.com>
parents: 1578
diff changeset
99
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
100
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
101 def str2bool(_str):
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
102 """
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
103 returs True/False value from given string, it tries to translate the
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
104 string into boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
105
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
106 :param _str: string value to translate into boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
107 :rtype: boolean
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
108 :returns: boolean from given string
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
109 """
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
110 if _str is None:
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
111 return False
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
112 if _str in (True, False):
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
113 return _str
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
114 _str = str(_str).strip().lower()
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
115 return _str in ('t', 'true', 'y', 'yes', 'on', '1')
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
116
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
117
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
118 def convert_line_endings(line, mode):
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
119 """
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
120 Converts a given line "line end" accordingly to given mode
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
121
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
122 Available modes are::
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
123 0 - Unix
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
124 1 - Mac
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
125 2 - DOS
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
126
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
127 :param line: given line to convert
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
128 :param mode: mode to convert to
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
129 :rtype: str
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
130 :return: converted line according to mode
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
131 """
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
132 from string import replace
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
133
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
134 if mode == 0:
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
135 line = replace(line, '\r\n', '\n')
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
136 line = replace(line, '\r', '\n')
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
137 elif mode == 1:
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
138 line = replace(line, '\r\n', '\r')
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
139 line = replace(line, '\n', '\r')
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
140 elif mode == 2:
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
141 line = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", line)
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
142 return line
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
143
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
144
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
145 def detect_mode(line, default):
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
146 """
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
147 Detects line break for given line, if line break couldn't be found
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
148 given default value is returned
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
149
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
150 :param line: str line
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
151 :param default: default
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
152 :rtype: int
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
153 :return: value of line end on of 0 - Unix, 1 - Mac, 2 - DOS
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
154 """
1305
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
155 if line.endswith('\r\n'):
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
156 return 2
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
157 elif line.endswith('\n'):
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
158 return 0
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
159 elif line.endswith('\r'):
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
160 return 1
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
161 else:
166317d464f3 Added server side file editing with commit
Marcin Kuzminski <marcin@python-works.com>
parents: 1302
diff changeset
162 return default
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
163
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
164
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
165 def generate_api_key(username, salt=None):
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
166 """
1466
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
167 Generates unique API key for given username, if salt is not given
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
168 it'll be generated from some random string
1203
6832ef664673 source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents: 1199
diff changeset
169
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
170 :param username: username as string
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
171 :param salt: salt to hash generate KEY
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
172 :rtype: str
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
173 :returns: sha1 hash from username+salt
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
174 """
1116
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
175 from tempfile import _RandomNameSequence
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
176 import hashlib
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
177
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
178 if salt is None:
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
179 salt = _RandomNameSequence().next()
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
180
716911af91e1 Added api_key into user, api key get's generated again after password change
Marcin Kuzminski <marcin@python-works.com>
parents: 914
diff changeset
181 return hashlib.sha1(username + salt).hexdigest()
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
182
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
183
2016
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
184 def safe_unicode(str_, from_encoding=None):
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
185 """
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
186 safe unicode function. Does few trick to turn str_ into unicode
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
187
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
188 In case of UnicodeDecode error we try to return it with encoding detected
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
189 by chardet library if it fails fallback to unicode with errors replaced
1223
f7b24987d5fb fixed setup.py file to use same platform as defined in main rhodecode PLATFORM_OTHERS,
Marcin Kuzminski <marcin@python-works.com>
parents: 1222
diff changeset
190
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
191 :param str_: string to decode
1222
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
192 :rtype: unicode
2c246d24eb90 simplified safe_unicode function
Marcin Kuzminski <marcin@python-works.com>
parents: 1206
diff changeset
193 :returns: unicode object
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
194 """
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
195 if isinstance(str_, unicode):
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
196 return str_
1199
268fa0b6b2ef Added os.sep in models for better win support
Marcin Kuzminski <marcin@python-works.com>
parents: 1154
diff changeset
197
2016
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
198 if not from_encoding:
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
199 import rhodecode
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
200 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
201 from_encoding = DEFAULT_ENCODING
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
202
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
203 try:
1502
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
204 return unicode(str_)
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
205 except UnicodeDecodeError:
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
206 pass
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
207
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
208 try:
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
209 return unicode(str_, from_encoding)
1154
36fe593dfe4b simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
Marcin Kuzminski <marcin@python-works.com>
parents: 1135
diff changeset
210 except UnicodeDecodeError:
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
211 pass
1502
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
212
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
213 try:
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
214 import chardet
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
215 encoding = chardet.detect(str_)['encoding']
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
216 if encoding is None:
1502
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
217 raise Exception()
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
218 return str_.decode(encoding)
1502
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
219 except (ImportError, UnicodeDecodeError, Exception):
a2efbfe84067 fixes safe_unicode raise UnicodeDecodeError without any parameters
Marcin Kuzminski <marcin@python-works.com>
parents: 1494
diff changeset
220 return unicode(str_, from_encoding, 'replace')
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
221
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
222
2016
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
223 def safe_str(unicode_, to_encoding=None):
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
224 """
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
225 safe str function. Does few trick to turn unicode_ into string
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
226
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
227 In case of UnicodeEncodeError we try to return it with encoding detected
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
228 by chardet library if it fails fallback to string with errors replaced
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
229
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
230 :param unicode_: unicode to encode
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
231 :rtype: str
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
232 :returns: str object
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
233 """
1641
cd1c21af123a Fixed problems with unicode cache keys in celery
Marcin Kuzminski <marcin@python-works.com>
parents: 1639
diff changeset
234
cd1c21af123a Fixed problems with unicode cache keys in celery
Marcin Kuzminski <marcin@python-works.com>
parents: 1639
diff changeset
235 if not isinstance(unicode_, basestring):
cd1c21af123a Fixed problems with unicode cache keys in celery
Marcin Kuzminski <marcin@python-works.com>
parents: 1639
diff changeset
236 return str(unicode_)
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
237
1611
2755c11c90d8 Fixed problems with unicode cache keys in celery
Marcin Kuzminski <marcin@python-works.com>
parents: 1605
diff changeset
238 if not isinstance(unicode_, basestring):
2755c11c90d8 Fixed problems with unicode cache keys in celery
Marcin Kuzminski <marcin@python-works.com>
parents: 1605
diff changeset
239 return str(unicode_)
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
240
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
241 if isinstance(unicode_, str):
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
242 return unicode_
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
243
2016
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
244 if not to_encoding:
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
245 import rhodecode
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
246 DEFAULT_ENCODING = rhodecode.CONFIG.get('default_encoding','utf8')
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
247 to_encoding = DEFAULT_ENCODING
6020e3884a58 implements #212 moved default encoding variable into rhodecode-config. It's now possible to change
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
248
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
249 try:
1494
8e76649f11ad fixes safe_str method
Marcin Kuzminski <marcin@python-works.com>
parents: 1490
diff changeset
250 return unicode_.encode(to_encoding)
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
251 except UnicodeEncodeError:
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
252 pass
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1502
diff changeset
253
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
254 try:
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
255 import chardet
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
256 encoding = chardet.detect(unicode_)['encoding']
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
257 print encoding
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
258 if encoding is None:
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
259 raise UnicodeEncodeError()
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1502
diff changeset
260
1490
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
261 return unicode_.encode(encoding)
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
262 except (ImportError, UnicodeEncodeError):
76b358f81926 extended safe_str and safe_unicode with chardet fallback.
Marcin Kuzminski <marcin@python-works.com>
parents: 1466
diff changeset
263 return unicode_.encode(to_encoding, 'replace')
1401
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
264
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
265 return safe_str
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
266
b7563ad4e7ee Unicode fixes, added safe_str method for global str() operations +better test sandboxing
Marcin Kuzminski <marcin@python-works.com>
parents: 1387
diff changeset
267
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
268 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
269 """
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
270 Custom engine_from_config functions that makes sure we use NullPool for
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
271 file based sqlite databases. This prevents errors on sqlite. This only
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
272 applies to sqlalchemy versions < 0.7.0
1307
c1516b35f91d pep8ify
Marcin Kuzminski <marcin@python-works.com>
parents: 1305
diff changeset
273
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
274 """
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
275 import sqlalchemy
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
276 from sqlalchemy import engine_from_config as efc
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
277 import logging
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
278
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
279 if int(sqlalchemy.__version__.split('.')[1]) < 7:
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
280
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
281 # This solution should work for sqlalchemy < 0.7.0, and should use
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
282 # proxy=TimerProxy() for execution time profiling
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
283
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
284 from sqlalchemy.pool import NullPool
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
285 url = configuration[prefix + 'url']
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
286
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
287 if url.startswith('sqlite'):
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
288 kwargs.update({'poolclass': NullPool})
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
289 return efc(configuration, prefix, **kwargs)
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
290 else:
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
291 import time
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
292 from sqlalchemy import event
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
293 from sqlalchemy.engine import Engine
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
294
1362
4c9bd42f82f9 timerproxy will use the same logger as sqlalchemy, in order to be controlled by sqlalchemy logger settings
Marcin Kuzminski <marcin@python-works.com>
parents: 1360
diff changeset
295 log = logging.getLogger('sqlalchemy.engine')
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
296 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
297 engine = efc(configuration, prefix, **kwargs)
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
298
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
299 def color_sql(sql):
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
300 COLOR_SEQ = "\033[1;%dm"
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
301 COLOR_SQL = YELLOW
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
302 normal = '\x1b[0m'
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
303 return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
304
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
305 if configuration['debug']:
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
306 #attach events only for debug configuration
1300
882ac77dc709 fixed problem with `Cannot operate on a closed database` error, by forcing NullPool when using sqlite database.
Marcin Kuzminski <marcin@python-works.com>
parents: 1223
diff changeset
307
1360
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
308 def before_cursor_execute(conn, cursor, statement,
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
309 parameters, context, executemany):
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
310 context._query_start_time = time.time()
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
311 log.info(color_sql(">>>>> STARTING QUERY >>>>>"))
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
312
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
313
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
314 def after_cursor_execute(conn, cursor, statement,
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
315 parameters, context, executemany):
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
316 total = time.time() - context._query_start_time
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
317 log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total))
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
318
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
319 event.listen(engine, "before_cursor_execute",
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
320 before_cursor_execute)
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
321 event.listen(engine, "after_cursor_execute",
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
322 after_cursor_execute)
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
323
1f47adeb67c2 Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy.
Marcin Kuzminski <marcin@python-works.com>
parents: 1342
diff changeset
324 return engine
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
325
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
326
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
327 def age(curdate):
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
328 """
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
329 turns a datetime into an age string.
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
330
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
331 :param curdate: datetime object
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
332 :rtype: unicode
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
333 :returns: unicode words describing age
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
334 """
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
335
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
336 from datetime import datetime
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
337 from webhelpers.date import time_ago_in_words
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
338
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
339 _ = lambda s: s
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
340
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
341 if not curdate:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
342 return ''
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
343
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
344 agescales = [(_(u"year"), 3600 * 24 * 365),
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
345 (_(u"month"), 3600 * 24 * 30),
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
346 (_(u"day"), 3600 * 24),
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
347 (_(u"hour"), 3600),
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
348 (_(u"minute"), 60),
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
349 (_(u"second"), 1), ]
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
350
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
351 age = datetime.now() - curdate
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
352 age_seconds = (age.days * agescales[2][1]) + age.seconds
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
353 pos = 1
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
354 for scale in agescales:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
355 if scale[1] <= age_seconds:
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
356 if pos == 6:
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
357 pos = 5
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
358 return '%s %s' % (time_ago_in_words(curdate,
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
359 agescales[pos][0]), _('ago'))
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
360 pos += 1
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
361
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
362 return _(u'just now')
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
363
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
364
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
365 def uri_filter(uri):
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
366 """
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
367 Removes user:password from given url string
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
368
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
369 :param uri:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
370 :rtype: unicode
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
371 :returns: filtered list of strings
1342
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
372 """
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
373 if not uri:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
374 return ''
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
375
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
376 proto = ''
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
377
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
378 for pat in ('https://', 'http://'):
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
379 if uri.startswith(pat):
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
380 uri = uri[len(pat):]
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
381 proto = pat
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
382 break
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
383
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
384 # remove passwords and username
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
385 uri = uri[uri.find('@') + 1:]
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
386
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
387 # get the port
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
388 cred_pos = uri.find(':')
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
389 if cred_pos == -1:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
390 host, port = uri, None
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
391 else:
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
392 host, port = uri[:cred_pos], uri[cred_pos + 1:]
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
393
9dacacc5b7c2 When creating a remote repository, with credentials filled, it's good to hide username and password from the clone url. Only administrators can see this in repository settings page
Marcin Kuzminski <marcin@python-works.com>
parents: 1307
diff changeset
394 return filter(None, [proto, host, port])
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
395
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
396
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
397 def credentials_filter(uri):
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
398 """
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
399 Returns a url with removed credentials
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
400
1373
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
401 :param uri:
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
402 """
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
403
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
404 uri = uri_filter(uri)
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
405 #check if we have port
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
406 if len(uri) > 2 and uri[2]:
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
407 uri[2] = ':' + uri[2]
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
408
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
409 return ''.join(uri)
66f03a87141c Fixes issue #201
Marcin Kuzminski <marcin@python-works.com>
parents: 1362
diff changeset
410
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
411
1466
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
412 def get_changeset_safe(repo, rev):
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
413 """
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
414 Safe version of get_changeset if this changeset doesn't exists for a
1466
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
415 repo it returns a Dummy one instead
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
416
1466
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
417 :param repo:
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
418 :param rev:
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
419 """
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
420 from rhodecode.lib.vcs.backends.base import BaseRepository
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
421 from rhodecode.lib.vcs.exceptions import RepositoryError
1466
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
422 if not isinstance(repo, BaseRepository):
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
423 raise Exception('You must pass an Repository '
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
424 'object as first argument got %s', type(repo))
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
425
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
426 try:
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
427 cs = repo.get_changeset(rev)
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
428 except RepositoryError:
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
429 from rhodecode.lib.utils import EmptyChangeset
ad5a543a9b2f Code cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 1401
diff changeset
430 cs = EmptyChangeset(requested_revision=rev)
1514
87ec80c280bb fixed issues with python2.5
Marcin Kuzminski <marcin@python-works.com>
parents: 1502
diff changeset
431 return cs
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
432
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
433
1571
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
434 def get_current_revision(quiet=False):
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
435 """
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
436 Returns tuple of (number, id) from repository containing this package
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
437 or None if repository could not be found.
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
438
1571
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
439 :param quiet: prints error for fetching revision if True
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
440 """
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
441
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
442 try:
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
443 from rhodecode.lib.vcs import get_repo
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
444 from rhodecode.lib.vcs.utils.helpers import get_scm
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
445 repopath = os.path.join(os.path.dirname(__file__), '..', '..')
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
446 scm = get_scm(repopath)[0]
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
447 repo = get_repo(path=repopath, alias=scm)
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
448 tip = repo.get_changeset()
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
449 return (tip.revision, tip.short_id)
1578
67168195a676 Catch all exception on get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1571
diff changeset
450 except Exception, err:
1571
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
451 if not quiet:
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
452 print ("Cannot retrieve rhodecode's revision. Original error "
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
453 "was: %s" % err)
1541
70e646b2806a fixes rhodecode upgrade problem that caused setuptool to crash on importing sqlalchemy models
Marcin Kuzminski <marcin@python-works.com>
parents: 1514
diff changeset
454 return None
1571
a9888895b60d added quiet flag into get_current_revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1541
diff changeset
455
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
456
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
457 def extract_mentioned_users(s):
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
458 """
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
459 Returns unique usernames from given string s that have @mention
1818
cf51bbfb120e auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents: 1713
diff changeset
460
1713
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
461 :param s: string to get mentions
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
462 """
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
463 usrs = {}
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
464 for username in re.findall(r'(?:^@|\s@)(\w+)', s):
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
465 usrs[username] = username
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
466
54687aa00724 Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents: 1611
diff changeset
467 return sorted(usrs.keys())