changeset 8544:cf0620647130

lib: drop own asbool implementation and consistently use tg.support.converters as utils2.asbool str2bool never reported error on odd input such as '' or '-1', but the tg asbool behaviour of raising ValueError("String is not true/false: %r" % obj) in that case seems fine.
author Mads Kiilerich <mads@kiilerich.com>
date Fri, 24 Apr 2020 15:17:54 +0200
parents 4d1aa80e5d0a
children 3a02b678b5e7
files kallithea/bin/kallithea_cli_ssh.py kallithea/config/app_cfg.py kallithea/controllers/feed.py kallithea/controllers/files.py kallithea/lib/auth_modules/__init__.py kallithea/lib/auth_modules/auth_container.py kallithea/lib/base.py kallithea/lib/celerylib/tasks.py kallithea/lib/helpers.py kallithea/lib/middleware/https_fixup.py kallithea/lib/utils2.py kallithea/model/db.py kallithea/model/permission.py kallithea/model/ssh_key.py kallithea/model/validators.py kallithea/tests/other/test_libs.py
diffstat 16 files changed, 48 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/bin/kallithea_cli_ssh.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/bin/kallithea_cli_ssh.py	Fri Apr 24 15:17:54 2020 +0200
@@ -21,7 +21,7 @@
 
 import kallithea
 import kallithea.bin.kallithea_cli_base as cli_base
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.lib.vcs.backends.git.ssh import GitSshHandler
 from kallithea.lib.vcs.backends.hg.ssh import MercurialSshHandler
 from kallithea.model.ssh_key import SshKeyModel, SshKeyModelException
@@ -40,8 +40,7 @@
     protocol access. The access will be granted as the specified user ID, and
     logged as using the specified key ID.
     """
-    ssh_enabled = kallithea.CONFIG.get('ssh_enabled', False)
-    if not str2bool(ssh_enabled):
+    if not asbool(kallithea.CONFIG.get('ssh_enabled', False)):
         sys.stderr.write("SSH access is disabled.\n")
         return sys.exit(1)
 
--- a/kallithea/config/app_cfg.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/config/app_cfg.py	Fri Apr 24 15:17:54 2020 +0200
@@ -29,7 +29,6 @@
 from alembic.script.base import ScriptDirectory
 from sqlalchemy import create_engine
 from tg.configuration import AppConfig
-from tg.support.converters import asbool
 
 import kallithea.lib.locale
 import kallithea.model.base
@@ -41,7 +40,7 @@
 from kallithea.lib.middleware.simplehg import SimpleHg
 from kallithea.lib.middleware.wrapper import RequestWrapper
 from kallithea.lib.utils import check_git_version, load_rcextensions, set_app_settings, set_indexer_config, set_vcs_config
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.model import db
 
 
@@ -160,7 +159,7 @@
     # store some globals into kallithea
     kallithea.DEFAULT_USER_ID = db.User.get_default_user().user_id
 
-    if str2bool(config.get('use_celery')):
+    if asbool(config.get('use_celery')):
         kallithea.CELERY_APP = celerypylons.make_app()
     kallithea.CONFIG = config
 
@@ -205,7 +204,7 @@
     app = PermanentRepoUrl(app, config)
 
     # Optional and undocumented wrapper - gives more verbose request/response logging, but has a slight overhead
-    if str2bool(config.get('use_wsgi_wrapper')):
+    if asbool(config.get('use_wsgi_wrapper')):
         app = RequestWrapper(app, config)
 
     return app
--- a/kallithea/controllers/feed.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/controllers/feed.py	Fri Apr 24 15:17:54 2020 +0200
@@ -39,7 +39,7 @@
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.base import BaseRepoController
 from kallithea.lib.diffs import DiffProcessor
-from kallithea.lib.utils2 import safe_int, safe_str, str2bool
+from kallithea.lib.utils2 import asbool, safe_int, safe_str
 
 
 log = logging.getLogger(__name__)
@@ -92,7 +92,7 @@
         desc_msg.append(h.urlify_text(cs.message))
         desc_msg.append('\n')
         desc_msg.extend(changes)
-        if str2bool(CONFIG.get('rss_include_diff', False)):
+        if asbool(CONFIG.get('rss_include_diff', False)):
             desc_msg.append('\n\n')
             desc_msg.append(safe_str(raw_diff))
         desc_msg.append('</pre>')
--- a/kallithea/controllers/files.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/controllers/files.py	Fri Apr 24 15:17:54 2020 +0200
@@ -46,7 +46,7 @@
 from kallithea.lib.base import BaseRepoController, jsonify, render
 from kallithea.lib.exceptions import NonRelativePathError
 from kallithea.lib.utils import action_logger
-from kallithea.lib.utils2 import convert_line_endings, detect_mode, safe_int, safe_str, str2bool
+from kallithea.lib.utils2 import asbool, convert_line_endings, detect_mode, safe_int, safe_str
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.vcs.conf import settings
 from kallithea.lib.vcs.exceptions import (ChangesetDoesNotExistError, ChangesetError, EmptyRepositoryError, ImproperArchiveTypeError, NodeAlreadyExistsError,
@@ -577,7 +577,7 @@
         # to reduce JS and callbacks
 
         if request.GET.get('show_rev'):
-            if str2bool(request.GET.get('annotate', 'False')):
+            if asbool(request.GET.get('annotate', 'False')):
                 _url = url('files_annotate_home', repo_name=c.repo_name,
                            revision=diff1, f_path=c.f_path)
             else:
--- a/kallithea/lib/auth_modules/__init__.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/auth_modules/__init__.py	Fri Apr 24 15:17:54 2020 +0200
@@ -21,7 +21,7 @@
 
 from kallithea.lib.auth import AuthUser, PasswordGenerator
 from kallithea.lib.compat import hybrid_property
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.model.db import Setting, User
 from kallithea.model.meta import Session
 from kallithea.model.user import UserModel
@@ -350,7 +350,7 @@
             plugin_settings[v["name"]] = setting.app_settings_value if setting else None
         log.debug('Settings for auth plugin %s: %s', plugin_name, plugin_settings)
 
-        if not str2bool(plugin_settings["enabled"]):
+        if not asbool(plugin_settings["enabled"]):
             log.info("Authentication plugin %s is disabled, skipping for %s",
                      module, username)
             continue
--- a/kallithea/lib/auth_modules/auth_container.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/auth_modules/auth_container.py	Fri Apr 24 15:17:54 2020 +0200
@@ -29,7 +29,7 @@
 
 from kallithea.lib import auth_modules
 from kallithea.lib.compat import hybrid_property
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.model.db import Setting
 
 
@@ -131,7 +131,7 @@
             username = environ.get(header)
             log.debug('extracted %s:%s', header, username)
 
-        if username and str2bool(settings.get('clean_username')):
+        if username and asbool(settings.get('clean_username')):
             log.debug('Received username %s from container', username)
             username = self._clean_username(username)
             log.debug('New cleanup user is: %s', username)
--- a/kallithea/lib/base.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/base.py	Fri Apr 24 15:17:54 2020 +0200
@@ -49,7 +49,7 @@
 from kallithea.lib.auth import AuthUser, HasPermissionAnyMiddleware
 from kallithea.lib.exceptions import UserCreationError
 from kallithea.lib.utils import get_repo_slug, is_valid_repo
-from kallithea.lib.utils2 import AttributeDict, ascii_bytes, safe_int, safe_str, set_hook_environment, str2bool
+from kallithea.lib.utils2 import AttributeDict, asbool, ascii_bytes, safe_int, safe_str, set_hook_environment
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, EmptyRepositoryError, RepositoryError
 from kallithea.model import meta
 from kallithea.model.db import PullRequest, Repository, Setting, User
@@ -375,14 +375,14 @@
         c.visual = AttributeDict({})
 
         ## DB stored
-        c.visual.show_public_icon = str2bool(rc_config.get('show_public_icon'))
-        c.visual.show_private_icon = str2bool(rc_config.get('show_private_icon'))
-        c.visual.stylify_metalabels = str2bool(rc_config.get('stylify_metalabels'))
+        c.visual.show_public_icon = asbool(rc_config.get('show_public_icon'))
+        c.visual.show_private_icon = asbool(rc_config.get('show_private_icon'))
+        c.visual.stylify_metalabels = asbool(rc_config.get('stylify_metalabels'))
         c.visual.page_size = safe_int(rc_config.get('dashboard_items', 100))
         c.visual.admin_grid_items = safe_int(rc_config.get('admin_grid_items', 100))
-        c.visual.repository_fields = str2bool(rc_config.get('repository_fields'))
-        c.visual.show_version = str2bool(rc_config.get('show_version'))
-        c.visual.use_gravatar = str2bool(rc_config.get('use_gravatar'))
+        c.visual.repository_fields = asbool(rc_config.get('repository_fields'))
+        c.visual.show_version = asbool(rc_config.get('show_version'))
+        c.visual.use_gravatar = asbool(rc_config.get('use_gravatar'))
         c.visual.gravatar_url = rc_config.get('gravatar_url')
 
         c.ga_code = rc_config.get('ga_code')
@@ -404,9 +404,9 @@
         c.clone_ssh_tmpl = rc_config.get('clone_ssh_tmpl') or Repository.DEFAULT_CLONE_SSH
 
         ## INI stored
-        c.visual.allow_repo_location_change = str2bool(config.get('allow_repo_location_change', True))
-        c.visual.allow_custom_hooks_settings = str2bool(config.get('allow_custom_hooks_settings', True))
-        c.ssh_enabled = str2bool(config.get('ssh_enabled', False))
+        c.visual.allow_repo_location_change = asbool(config.get('allow_repo_location_change', True))
+        c.visual.allow_custom_hooks_settings = asbool(config.get('allow_custom_hooks_settings', True))
+        c.ssh_enabled = asbool(config.get('ssh_enabled', False))
 
         c.instance_id = config.get('instance_id')
         c.issues_url = config.get('bugtracker', url('issues_url'))
--- a/kallithea/lib/celerylib/tasks.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/celerylib/tasks.py	Fri Apr 24 15:17:54 2020 +0200
@@ -42,7 +42,7 @@
 from kallithea.lib.hooks import log_create_repository
 from kallithea.lib.rcmail.smtp_mailer import SmtpMailer
 from kallithea.lib.utils import action_logger
-from kallithea.lib.utils2 import ascii_bytes, str2bool
+from kallithea.lib.utils2 import asbool, ascii_bytes
 from kallithea.lib.vcs.utils import author_email
 from kallithea.model.db import RepoGroup, Repository, Statistics, User
 
@@ -289,9 +289,9 @@
     passwd = email_config.get('smtp_password')
     mail_server = email_config.get('smtp_server')
     mail_port = email_config.get('smtp_port')
-    tls = str2bool(email_config.get('smtp_use_tls'))
-    ssl = str2bool(email_config.get('smtp_use_ssl'))
-    debug = str2bool(email_config.get('debug'))
+    tls = asbool(email_config.get('smtp_use_tls'))
+    ssl = asbool(email_config.get('smtp_use_ssl'))
+    debug = asbool(email_config.get('debug'))
     smtp_auth = email_config.get('smtp_auth')
 
     logmsg = ("Mail details:\n"
--- a/kallithea/lib/helpers.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/helpers.py	Fri Apr 24 15:17:54 2020 +0200
@@ -48,7 +48,7 @@
 from kallithea.lib.pygmentsutils import get_custom_lexer
 from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict
 from kallithea.lib.utils2 import age as _age
-from kallithea.lib.utils2 import credentials_filter, safe_bytes, safe_int, safe_str, str2bool, time_to_datetime
+from kallithea.lib.utils2 import asbool, credentials_filter, safe_bytes, safe_int, safe_str, time_to_datetime
 from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset
 from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
 #==============================================================================
@@ -526,7 +526,7 @@
     """
     from kallithea import CONFIG
     def_len = safe_int(CONFIG.get('show_sha_length', 12))
-    show_rev = str2bool(CONFIG.get('show_revision_number', False))
+    show_rev = asbool(CONFIG.get('show_revision_number', False))
 
     raw_id = cs.raw_id[:def_len]
     if show_rev:
--- a/kallithea/lib/middleware/https_fixup.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/middleware/https_fixup.py	Fri Apr 24 15:17:54 2020 +0200
@@ -26,7 +26,7 @@
 """
 
 
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 
 
 class HttpsFixup(object):
@@ -37,11 +37,11 @@
 
     def __call__(self, environ, start_response):
         self.__fixup(environ)
-        debug = str2bool(self.config.get('debug'))
+        debug = asbool(self.config.get('debug'))
         is_ssl = environ['wsgi.url_scheme'] == 'https'
 
         def custom_start_response(status, headers, exc_info=None):
-            if is_ssl and str2bool(self.config.get('use_htsts')) and not debug:
+            if is_ssl and asbool(self.config.get('use_htsts')) and not debug:
                 headers.append(('Strict-Transport-Security',
                                 'max-age=8640000; includeSubDomains'))
             return start_response(status, headers, exc_info)
@@ -66,7 +66,7 @@
         org_proto = proto
 
         # if we have force, just override
-        if str2bool(self.config.get('force_https')):
+        if asbool(self.config.get('force_https')):
             proto = 'https'
 
         environ['wsgi.url_scheme'] = proto
--- a/kallithea/lib/utils2.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/lib/utils2.py	Fri Apr 24 15:17:54 2020 +0200
@@ -38,6 +38,7 @@
 import urlobject
 from tg.i18n import ugettext as _
 from tg.i18n import ungettext
+from tg.support.converters import asbool, aslist
 from webhelpers2.text import collapse, remove_formatting, strip_tags
 
 from kallithea.lib.vcs.utils import ascii_bytes, ascii_str, safe_bytes, safe_str  # re-export
@@ -51,6 +52,8 @@
 
 
 # mute pyflakes "imported but unused"
+assert asbool
+assert aslist
 assert ascii_bytes
 assert ascii_str
 assert safe_bytes
@@ -58,44 +61,6 @@
 assert LazyProperty
 
 
-def str2bool(_str):
-    """
-    returns True/False value from given string, it tries to translate the
-    string into boolean
-
-    :param _str: string value to translate into boolean
-    :rtype: boolean
-    :returns: boolean from given string
-    """
-    if _str is None:
-        return False
-    if _str in (True, False):
-        return _str
-    _str = str(_str).strip().lower()
-    return _str in ('t', 'true', 'y', 'yes', 'on', '1')
-
-
-def aslist(obj, sep=None, strip=True):
-    """
-    Returns given string separated by sep as list
-
-    :param obj:
-    :param sep:
-    :param strip:
-    """
-    if isinstance(obj, (str)):
-        lst = obj.split(sep)
-        if strip:
-            lst = [v.strip() for v in lst]
-        return lst
-    elif isinstance(obj, (list, tuple)):
-        return obj
-    elif obj is None:
-        return []
-    else:
-        return [obj]
-
-
 def convert_line_endings(line, mode):
     """
     Converts a given line  "line end" according to given mode
--- a/kallithea/model/db.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/model/db.py	Fri Apr 24 15:17:54 2020 +0200
@@ -46,7 +46,7 @@
 import kallithea
 from kallithea.lib import ext_json
 from kallithea.lib.exceptions import DefaultUserException
-from kallithea.lib.utils2 import (Optional, ascii_bytes, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_bytes, safe_int, safe_str, str2bool,
+from kallithea.lib.utils2 import (Optional, asbool, ascii_bytes, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_bytes, safe_int, safe_str,
                                   urlreadable)
 from kallithea.lib.vcs import get_backend
 from kallithea.lib.vcs.backends.base import EmptyChangeset
@@ -185,7 +185,7 @@
         'str': safe_bytes,
         'int': safe_int,
         'unicode': safe_str,
-        'bool': str2bool,
+        'bool': asbool,
         'list': functools.partial(aslist, sep=',')
     }
     DEFAULT_UPDATE_URL = ''
@@ -1164,7 +1164,7 @@
         if with_pullrequests:
             data['pull_requests'] = repo.pull_requests_other
         rc_config = Setting.get_app_settings()
-        repository_fields = str2bool(rc_config.get('repository_fields'))
+        repository_fields = asbool(rc_config.get('repository_fields'))
         if repository_fields:
             for f in self.extra_fields:
                 data[f.field_key_prefixed] = f.field_value
--- a/kallithea/model/permission.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/model/permission.py	Fri Apr 24 15:17:54 2020 +0200
@@ -31,7 +31,7 @@
 
 from sqlalchemy.exc import DatabaseError
 
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.model.db import Permission, Session, User, UserRepoGroupToPerm, UserRepoToPerm, UserToPerm, UserUserGroupToPerm
 
 
@@ -97,7 +97,7 @@
         try:
             # stage 1 set anonymous access
             if perm_user.is_default_user:
-                perm_user.active = str2bool(form_result['anonymous'])
+                perm_user.active = asbool(form_result['anonymous'])
 
             # stage 2 reset defaults and set them from form data
             def _make_new(usr, perm_name):
--- a/kallithea/model/ssh_key.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/model/ssh_key.py	Fri Apr 24 15:17:54 2020 +0200
@@ -29,7 +29,7 @@
 from tg.i18n import ugettext as _
 
 from kallithea.lib import ssh
-from kallithea.lib.utils2 import str2bool
+from kallithea.lib.utils2 import asbool
 from kallithea.lib.vcs.exceptions import RepositoryError
 from kallithea.model.db import User, UserSshKeys
 from kallithea.model.meta import Session
@@ -95,7 +95,7 @@
         return user_ssh_keys
 
     def write_authorized_keys(self):
-        if not str2bool(config.get('ssh_enabled', False)):
+        if not asbool(config.get('ssh_enabled', False)):
             log.error("Will not write SSH authorized_keys file - ssh_enabled is not configured")
             return
         authorized_keys = config.get('ssh_authorized_keys')
--- a/kallithea/model/validators.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/model/validators.py	Fri Apr 24 15:17:54 2020 +0200
@@ -32,7 +32,7 @@
 from kallithea.lib.compat import OrderedSet
 from kallithea.lib.exceptions import InvalidCloneUriException, LdapImportError
 from kallithea.lib.utils import is_valid_repo_uri
-from kallithea.lib.utils2 import aslist, repo_name_slug, str2bool
+from kallithea.lib.utils2 import asbool, aslist, repo_name_slug
 from kallithea.model import db
 from kallithea.model.db import RepoGroup, Repository, User, UserGroup
 
@@ -568,7 +568,7 @@
                          'g': 'users_group'
                     }[k[0]]
                     if member_name == User.DEFAULT_USER_NAME:
-                        if str2bool(value.get('repo_private')):
+                        if asbool(value.get('repo_private')):
                             # set none for default when updating to
                             # private repo protects against form manipulation
                             v = EMPTY_PERM
--- a/kallithea/tests/other/test_libs.py	Sun Jun 07 21:56:15 2020 +0200
+++ b/kallithea/tests/other/test_libs.py	Fri Apr 24 15:17:54 2020 +0200
@@ -119,12 +119,10 @@
                            ('F', False),
                            ('FALSE', False),
                            ('0', False),
-                           ('-1', False),
-                           ('', False)
     ])
-    def test_str2bool(self, str_bool, expected):
-        from kallithea.lib.utils2 import str2bool
-        assert str2bool(str_bool) == expected
+    def test_asbool(self, str_bool, expected):
+        from kallithea.lib.utils2 import asbool
+        assert asbool(str_bool) == expected
 
     def test_mention_extractor(self):
         from kallithea.lib.utils2 import extract_mentioned_usernames