##// END OF EJS Templates
simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
marcink -
r1154:36fe593d beta
parent child Browse files
Show More
@@ -25,19 +25,21 b''
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # MA 02110-1301, USA.
26 # MA 02110-1301, USA.
27
27
28 def str2bool(v):
28 def str2bool(s):
29 if isinstance(v, (str, unicode)):
29 if s is None:
30 obj = v.strip().lower()
30 return False
31 if obj in ['true', 'yes', 'on', 'y', 't', '1']:
31 if s in (True, False):
32 return True
32 return s
33 elif obj in ['false', 'no', 'off', 'n', 'f', '0']:
33 s = str(s).strip().lower()
34 return False
34 return s in ('t', 'true', 'y', 'yes', 'on', '1')
35 else:
36 if not safe:
37 raise ValueError("String is not true/false: %r" % obj)
38 return bool(obj)
39
35
40 def generate_api_key(username, salt=None):
36 def generate_api_key(username, salt=None):
37 """
38 Generates uniq API key for given username
39
40 :param username: username as string
41 :param salt: salt to hash generate KEY
42 """
41 from tempfile import _RandomNameSequence
43 from tempfile import _RandomNameSequence
42 import hashlib
44 import hashlib
43
45
@@ -45,3 +47,21 b' def generate_api_key(username, salt=None'
45 salt = _RandomNameSequence().next()
47 salt = _RandomNameSequence().next()
46
48
47 return hashlib.sha1(username + salt).hexdigest()
49 return hashlib.sha1(username + salt).hexdigest()
50
51 def safe_unicode(str):
52 """
53 safe unicode function. In case of UnicodeDecode error we try to return
54 unicode with errors replace, if this fails we return unicode with
55 string_escape decoding
56 """
57
58 try:
59 u_str = unicode(str)
60 except UnicodeDecodeError:
61 try:
62 u_str = unicode(str, 'utf-8', 'replace')
63 except UnicodeDecodeError:
64 #incase we have a decode error just represent as byte string
65 u_str = unicode(str(str).encode('string_escape'))
66
67 return u_str
@@ -8,6 +8,7 b' import hashlib'
8 import StringIO
8 import StringIO
9 import urllib
9 import urllib
10
10
11 from datetime import datetime
11 from pygments.formatters import HtmlFormatter
12 from pygments.formatters import HtmlFormatter
12 from pygments import highlight as code_highlight
13 from pygments import highlight as code_highlight
13 from pylons import url, request, config
14 from pylons import url, request, config
@@ -38,7 +39,8 b' from rhodecode.lib.utils import repo_nam'
38 from rhodecode.lib import str2bool
39 from rhodecode.lib import str2bool
39
40
40 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
41 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
41 """Reset button
42 """
43 Reset button
42 """
44 """
43 _set_input_attrs(attrs, type, name, value)
45 _set_input_attrs(attrs, type, name, value)
44 _set_id_attr(attrs, id, name)
46 _set_id_attr(attrs, id, name)
@@ -380,8 +382,6 b' def _age(curdate):'
380 if not curdate:
382 if not curdate:
381 return ''
383 return ''
382
384
383 from datetime import timedelta, datetime
384
385 agescales = [("year", 3600 * 24 * 365),
385 agescales = [("year", 3600 * 24 * 365),
386 ("month", 3600 * 24 * 30),
386 ("month", 3600 * 24 * 30),
387 ("day", 3600 * 24),
387 ("day", 3600 * 24),
@@ -671,22 +671,6 b' class RepoPage(Page):'
671 list.__init__(self, self.items)
671 list.__init__(self, self.items)
672
672
673
673
674 def safe_unicode(str):
675 """safe unicode function. In case of UnicodeDecode error we try to return
676 unicode with errors replace, if this failes we return unicode with
677 string_escape decoding """
678
679 try:
680 u_str = unicode(str)
681 except UnicodeDecodeError:
682 try:
683 u_str = unicode(str, 'utf-8', 'replace')
684 except UnicodeDecodeError:
685 #incase we have a decode error just represent as byte string
686 u_str = unicode(str(str).encode('string_escape'))
687
688 return u_str
689
690 def changed_tooltip(nodes):
674 def changed_tooltip(nodes):
691 if nodes:
675 if nodes:
692 pref = ': <br/> '
676 pref = ': <br/> '
@@ -25,9 +25,14 b''
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 # MA 02110-1301, USA.
26 # MA 02110-1301, USA.
27
27
28 import os
28 import sys
29 import sys
29 import os
30 import logging
30 import traceback
31 import traceback
32
33 from shutil import rmtree
34 from time import mktime
35
31 from os.path import dirname as dn
36 from os.path import dirname as dn
32 from os.path import join as jn
37 from os.path import join as jn
33
38
@@ -37,15 +42,14 b' sys.path.append(project_path)'
37
42
38
43
39 from rhodecode.model.scm import ScmModel
44 from rhodecode.model.scm import ScmModel
40 from rhodecode.lib.helpers import safe_unicode
45 from rhodecode.lib import safe_unicode
41 from whoosh.index import create_in, open_dir
42 from shutil import rmtree
43 from rhodecode.lib.indexers import INDEX_EXTENSIONS, SCHEMA, IDX_NAME
46 from rhodecode.lib.indexers import INDEX_EXTENSIONS, SCHEMA, IDX_NAME
44
47
45 from time import mktime
46 from vcs.exceptions import ChangesetError, RepositoryError
48 from vcs.exceptions import ChangesetError, RepositoryError
47
49
48 import logging
50 from whoosh.index import create_in, open_dir
51
52
49
53
50 log = logging.getLogger('whooshIndexer')
54 log = logging.getLogger('whooshIndexer')
51 # create logger
55 # create logger
General Comments 0
You need to be logged in to leave comments. Login now