<commit_before>#
# otopi -- plugable installer
# Copyright (C) 2012-2013 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#


"""Common misc functions."""


import builtins
import gettext
_ = lambda m: gettext.dgettext(message=m, domain='otopi')


from . import constants
from . import util


@util.export
def parseTypedValue(value):
    """Parse type:value string into python object."""
    try:
        vtype, value = value.split(':', 1)
    except ValueError:
        raise ValueError(_("Missing variable type"))

    if vtype == constants.Types.NONE:
        value = None
    elif vtype == constants.Types.BOOLEAN:
        value = value not in (0, 'f', 'F', 'false', 'False')
    elif vtype == constants.Types.INTEGER:
        value = int(value)
    elif vtype == constants.Types.STRING:
        pass
    elif vtype == constants.Types.MULTI_STRING:
        value = value.splitlines()
    else:
        raise KeyError(
            _('Invalid variable type {type}').format(
                type=vtype
            )
        )
    return value


@util.export
def typeName(value):
    """Return type of value suitable for parsing"""
    if value is None:
        ret = constants.Types.NONE
    elif isinstance(value, bool):
        ret = constants.Types.BOOLEAN
    elif isinstance(value, int):
        ret = constants.Types.INTEGER
    elif isinstance(value, str) or isinstance(value, builtins.unicode):
        ret = constants.Types.STRING
    elif isinstance(value, list):
        ret = constants.Types.MULTI_STRING
    else:
        ret = constants.Types.OBJECT
    return ret


# vim: expandtab tabstop=4 shiftwidth=4
<commit_msg>core: consider tuple as mutli-string<commit_after>#
# otopi -- plugable installer
# Copyright (C) 2012-2013 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#


"""Common misc functions."""


import builtins
import gettext
_ = lambda m: gettext.dgettext(message=m, domain='otopi')


from . import constants
from . import util


@util.export
def parseTypedValue(value):
    """Parse type:value string into python object."""
    try:
        vtype, value = value.split(':', 1)
    except ValueError:
        raise ValueError(_("Missing variable type"))

    if vtype == constants.Types.NONE:
        value = None
    elif vtype == constants.Types.BOOLEAN:
        value = value not in (0, 'f', 'F', 'false', 'False')
    elif vtype == constants.Types.INTEGER:
        value = int(value)
    elif vtype == constants.Types.STRING:
        pass
    elif vtype == constants.Types.MULTI_STRING:
        value = value.splitlines()
    else:
        raise KeyError(
            _('Invalid variable type {type}').format(
                type=vtype
            )
        )
    return value


@util.export
def typeName(value):
    """Return type of value suitable for parsing"""
    if value is None:
        ret = constants.Types.NONE
    elif isinstance(value, bool):
        ret = constants.Types.BOOLEAN
    elif isinstance(value, int):
        ret = constants.Types.INTEGER
    elif isinstance(value, str) or isinstance(value, builtins.unicode):
        ret = constants.Types.STRING
    elif isinstance(value, list) or isinstance(value, tuple):
        ret = constants.Types.MULTI_STRING
    else:
        ret = constants.Types.OBJECT
    return ret


# vim: expandtab tabstop=4 shiftwidth=4

{'fraction_non_alphanumeric': 0.09202975557917109, 'fraction_numerical': 0.010626992561105207, 'mean_word_length': 3.0051063829787235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725633', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1279, 'n_words': 511}