Changeset - cf0620647130
[Not reviewed]
default
0 16 0
Mads Kiilerich (mads) - 6 years ago 2020-04-24 15:17:54
mads@kiilerich.com
Grafted from: 63fa6045734d
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.
16 files changed with 48 insertions and 87 deletions:
0 comments (0 inline, 0 general)
kallithea/bin/kallithea_cli_ssh.py
Show inline comments
 
@@ -21,7 +21,7 @@ import click
 

	
 
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 @@ def ssh_serve(user_id, key_id):
 
    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)
 

	
kallithea/config/app_cfg.py
Show inline comments
 
@@ -29,7 +29,6 @@ from alembic.migration import MigrationC
 
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.simplegit 
 
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 @@ def setup_configuration(app):
 
    # 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 @@ def setup_application(app):
 
    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
kallithea/controllers/feed.py
Show inline comments
 
@@ -39,7 +39,7 @@ from kallithea.lib import helpers as h
 
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 @@ class FeedController(BaseRepoController)
 
        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>')
kallithea/controllers/files.py
Show inline comments
 
@@ -46,7 +46,7 @@ from kallithea.lib.auth import HasRepoPe
 
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 @@ class FilesController(BaseRepoController
 
        # 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:
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -21,7 +21,7 @@ import traceback
 

	
 
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 @@ def authenticate(username, password, env
 
            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
kallithea/lib/auth_modules/auth_container.py
Show inline comments
 
@@ -29,7 +29,7 @@ import logging
 

	
 
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 @@ class KallitheaAuthPlugin(auth_modules.K
 
            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)
kallithea/lib/base.py
Show inline comments
 
@@ -49,7 +49,7 @@ from kallithea.lib import auth_modules, 
 
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 @@ class BaseController(TGController):
 
        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 @@ class BaseController(TGController):
 
        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'))
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -42,7 +42,7 @@ from kallithea.lib.helpers import person
 
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 @@ def send_email(recipients, subject, body
 
    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"
kallithea/lib/helpers.py
Show inline comments
 
@@ -48,7 +48,7 @@ from kallithea.lib.markup_renderer impor
 
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 @@ def show_id(cs):
 
    """
 
    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:
kallithea/lib/middleware/https_fixup.py
Show inline comments
 
@@ -26,7 +26,7 @@ Original author and date, and relevant c
 
"""
 

	
 

	
 
from kallithea.lib.utils2 import str2bool
 
from kallithea.lib.utils2 import asbool
 

	
 

	
 
class HttpsFixup(object):
 
@@ -37,11 +37,11 @@ class HttpsFixup(object):
 

	
 
    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 @@ class HttpsFixup(object):
 
        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
kallithea/lib/utils2.py
Show inline comments
 
@@ -38,6 +38,7 @@ import urllib.parse
 
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 @@ except ImportError:
 

	
 

	
 
# mute pyflakes "imported but unused"
 
assert asbool
 
assert aslist
 
assert ascii_bytes
 
assert ascii_str
 
assert safe_bytes
 
@@ -58,44 +61,6 @@ assert safe_str
 
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
kallithea/model/db.py
Show inline comments
 
@@ -46,7 +46,7 @@ from webob.exc import HTTPNotFound
 
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 @@ class Setting(Base, BaseDbModel):
 
        '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 @@ class Repository(Base, BaseDbModel):
 
        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
kallithea/model/permission.py
Show inline comments
 
@@ -31,7 +31,7 @@ import traceback
 

	
 
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 @@ class PermissionModel(object):
 
        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):
kallithea/model/ssh_key.py
Show inline comments
 
@@ -29,7 +29,7 @@ from tg import config
 
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 @@ class SshKeyModel(object):
 
        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')
kallithea/model/validators.py
Show inline comments
 
@@ -32,7 +32,7 @@ from kallithea.lib.auth import HasPermis
 
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 @@ def ValidPerms(type_='repo'):
 
                         '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
kallithea/tests/other/test_libs.py
Show inline comments
 
@@ -119,12 +119,10 @@ class TestLibs(base.TestController):
 
                           ('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
0 comments (0 inline, 0 general)