changeset 1154:36fe593dfe4b beta

simplified str2bool, and moved safe_unicode out of helpers since it was not html specific function
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 18 Mar 2011 19:39:48 +0100
parents 0c5629ce52e4
children 37a9421f32a0
files rhodecode/lib/__init__.py rhodecode/lib/helpers.py rhodecode/lib/indexers/daemon.py
diffstat 3 files changed, 44 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/__init__.py	Fri Mar 18 16:43:36 2011 +0100
+++ b/rhodecode/lib/__init__.py	Fri Mar 18 19:39:48 2011 +0100
@@ -25,19 +25,21 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
-def str2bool(v):
-    if isinstance(v, (str, unicode)):
-        obj = v.strip().lower()
-        if obj in ['true', 'yes', 'on', 'y', 't', '1']:
-            return True
-        elif obj in ['false', 'no', 'off', 'n', 'f', '0']:
-            return False
-        else:
-            if not safe:
-                raise ValueError("String is not true/false: %r" % obj)
-    return bool(obj)
+def str2bool(s):
+    if s is None:
+        return False
+    if s in (True, False):
+        return s
+    s = str(s).strip().lower()
+    return s in ('t', 'true', 'y', 'yes', 'on', '1')
 
 def generate_api_key(username, salt=None):
+    """
+    Generates uniq API key for given username
+    
+    :param username: username as string
+    :param salt: salt to hash generate KEY
+    """
     from tempfile import _RandomNameSequence
     import hashlib
 
@@ -45,3 +47,21 @@
         salt = _RandomNameSequence().next()
 
     return hashlib.sha1(username + salt).hexdigest()
+
+def safe_unicode(str):
+    """
+    safe unicode function. In case of UnicodeDecode error we try to return
+    unicode with errors replace, if this fails we return unicode with 
+    string_escape decoding 
+    """
+
+    try:
+        u_str = unicode(str)
+    except UnicodeDecodeError:
+        try:
+            u_str = unicode(str, 'utf-8', 'replace')
+        except UnicodeDecodeError:
+            #incase we have a decode error just represent as byte string
+            u_str = unicode(str(str).encode('string_escape'))
+
+    return u_str
--- a/rhodecode/lib/helpers.py	Fri Mar 18 16:43:36 2011 +0100
+++ b/rhodecode/lib/helpers.py	Fri Mar 18 19:39:48 2011 +0100
@@ -8,6 +8,7 @@
 import StringIO
 import urllib
 
+from datetime import datetime
 from pygments.formatters import HtmlFormatter
 from pygments import highlight as code_highlight
 from pylons import url, request, config
@@ -38,7 +39,8 @@
 from rhodecode.lib import str2bool
 
 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
-    """Reset button
+    """
+    Reset button
     """
     _set_input_attrs(attrs, type, name, value)
     _set_id_attr(attrs, id, name)
@@ -380,8 +382,6 @@
     if not curdate:
         return ''
 
-    from datetime import timedelta, datetime
-
     agescales = [("year", 3600 * 24 * 365),
                  ("month", 3600 * 24 * 30),
                  ("day", 3600 * 24),
@@ -671,22 +671,6 @@
         list.__init__(self, self.items)
 
 
-def safe_unicode(str):
-    """safe unicode function. In case of UnicodeDecode error we try to return
-    unicode with errors replace, if this failes we return unicode with 
-    string_escape decoding """
-
-    try:
-        u_str = unicode(str)
-    except UnicodeDecodeError:
-        try:
-            u_str = unicode(str, 'utf-8', 'replace')
-        except UnicodeDecodeError:
-            #incase we have a decode error just represent as byte string
-            u_str = unicode(str(str).encode('string_escape'))
-
-    return u_str
-
 def changed_tooltip(nodes):
     if nodes:
         pref = ': <br/> '
--- a/rhodecode/lib/indexers/daemon.py	Fri Mar 18 16:43:36 2011 +0100
+++ b/rhodecode/lib/indexers/daemon.py	Fri Mar 18 19:39:48 2011 +0100
@@ -25,9 +25,14 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.
 
+import os
 import sys
-import os
+import logging
 import traceback
+
+from shutil import rmtree
+from time import mktime
+
 from os.path import dirname as dn
 from os.path import join as jn
 
@@ -37,15 +42,14 @@
 
 
 from rhodecode.model.scm import ScmModel
-from rhodecode.lib.helpers import safe_unicode
-from whoosh.index import create_in, open_dir
-from shutil import rmtree
+from rhodecode.lib import safe_unicode
 from rhodecode.lib.indexers import INDEX_EXTENSIONS, SCHEMA, IDX_NAME
 
-from time import mktime
 from vcs.exceptions import ChangesetError, RepositoryError
 
-import logging
+from whoosh.index import create_in, open_dir
+
+
 
 log = logging.getLogger('whooshIndexer')
 # create logger