from parallels.plesk import messages


def parse_bool_value(value, invalid_default=None):
    """Parse boolean value from hosting description file

    If value is invalid, function will throw exception, unless
    invalid_default parameter is specified.

    :type value: str | unicode | bool
    :type invalid_default: bool
    :rtype: bool
    """
    if value in (1, True, '1', 'on', 'true', 'enabled'):
        return True
    elif value in (0, False, '0', 'off', 'false', 'disabled'):
        return False
    else:
        if invalid_default is None:
            raise Exception(
                messages.INVALID_BOOLEAN_VALUE % value
            )
        else:
            return invalid_default


def create_dict(**kwargs):
    return {key: value for key, value in kwargs.items() if value is not None}
