Example #1
0
import sys
import logging

import yaml
from voluptuous import Schema, MultipleInvalid, Invalid
from voluptuous import Required, All, Range, Length

_schema = Schema({
    Required('username', default='root'): All(str, Length(min=1)),
    Required('password', default='password'): All(str, Length(min=1)),
    Required('port'): All(int, Range(min=1, max=65535)),
    Required('motd', default='Hi'): str,
    Required('simple_commands'): dict,
    Required('pem_key'): str,
    Required('pub_key'): str
    })


def _get_config():
    with open('./config/config.yml') as config_file:
       text = config_file.read()

    config = yaml.load(text)
    try:
        _schema(config)
    except (MultipleInvalid, Invalid) as e:
        logging.error('Invalid config.yml')
        logging.error(str(e))
        sys.exit(1)
    return config
Example #2
0
def skip_repo_fs_check():
    return {
        Optional('skip_repo_fs_check', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #3
0
def delay():
    return {
        Optional('delay', default=0):
        All(Coerce(float), Range(min=0.0, max=3600.0))
    }
Example #4
0
def continue_if_exception():
    return {
        Optional('continue_if_exception', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #5
0
def retry_interval():
    return {
        Optional('retry_interval', default=120):
        All(Coerce(int), Range(min=1, max=600))
    }
Example #6
0
def partial():
    return {
        Optional('partial', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #7
0
def refresh():
    return {
        Optional('refresh', default=True):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #8
0
    'NETWORK_FAILURE': NetworkFailure,
    'SERVICE_FAILURE': ServiceFailure,
    'FIREWALL_TIMEOUT': FirewallTimeout,
    'DELAY': Delay,
    'PACKET_LOSS': PacketLoss
}


def alphabetical_keys(a_dict):
    keys = a_dict.keys()
    keys.sort()
    return keys


BASE_SCHEMA = {
    Required('name'): All(string, Length(min=1)),
    Required('type'): All(string, OneOf(alphabetical_keys(FAULT_TYPES))),
    Required('direction'): All(string, OneOf(alphabetical_keys(DIRECTIONS))),
    Required('to_port'): All(int),
    Optional('from'): All(string),
    Optional('to'): All(string),
    Optional('protocol'): All(string)
}


def build_add_fault_command(shell, params):
    if not params.has_key('type') or params['type'] not in FAULT_TYPES.keys():
        message = 'must be present and one of ' + str(
            alphabetical_keys(FAULT_TYPES))
        exception = MultipleInvalid()
        exception.add(Invalid(message, ['type'], message))
Example #9
0
 def extra_schema(self):
     return {Required('timeout'): All(int)}
# -*- coding: utf-8 -*-
from pagseguro.validators import Email, PhoneArea, PhoneNumber, BrState
from voluptuous import Schema, Required, All, Length, Range, Optional, Match, \
    Any

item_schema = Schema({
    Required('item_id'):
    All(unicode, Length(min=1, max=100)),
    Required('description'):
    All(unicode, Length(min=1, max=100)),
    Required('amount'):
    All(float, Range(min=0.01, max=9999999)),
    Required('quantity'):
    All(int, Range(min=1, max=999)),
    Optional('weight'):
    All(int, Range(max=30000)),
})

#: .. todo:: Validar born_date e CPF
client_schema = Schema({
    'name':
    unicode,
    'email':
    All(Email(), Length(max=60)),
    'phone_area_code':
    All(PhoneArea(), Length(min=2, max=2)),
    'phone_number':
    All(PhoneNumber(), Length(min=7, max=9)),
    'cpf':
    All(
        Match('[\d]{11}',
Example #11
0
        return None
    if isinstance(types, str):
        types = [typ.strip() for typ in types.split(",")]

    unsupported = set(types) - {"reflink", "hardlink", "symlink", "copy"}
    if unsupported:
        raise Invalid("Unsupported cache type(s): {}".format(
            ", ".join(unsupported)))

    return types


# Checks that value is either true or false and converts it to bool
to_bool = Bool = All(
    Lower,
    Any("true", "false"),
    lambda v: v == "true",
    msg="expected true or false",
)


def Choices(*choices):
    """Checks that value belongs to the specified set of values

    Args:
        *choices: pass allowed values as arguments, or pass a list or
            tuple as a single argument
    """
    return Any(*choices, msg="expected one of {}".format(", ".join(choices)))


def ByUrl(mapping):
Example #12
0
def difficulty_validator(value):
    """
    Check if value is a valid difficulty.
    """
    try:
        return Difficulty(value)
    except ValueError:
        expected_values = tuple(member.value for member in Difficulty)
        raise Invalid("Value should be one of: {}".format(expected_values))


CREATE_TEAM_MEMBER_VALIDATOR = Schema({
    Required("name"):
    str,
    Required("level", default=1):
    All(int, Range(min=1)),
    Required("theme", default=""):
    str,
    Required("level_max", default=None):
    Any(None, All(int, Range(min=1))),
    Required("difficulty", default=Difficulty.EASY):
    difficulty_validator,
})


class TeamMember(ModelMixin):
    """
    A TeamMember is a player, each Game has one or multiple TeamMember.
    A TeamMember cannot play multiple games.
    """
Example #13
0
def _device_commands_schema():
    return Schema({
        All(str): Any(list, dict, str),
        Optional('connections'): _device_connections_commands(),
        Optional('users'): _device_user_commands()
    })
Example #14
0
    "singer": str,
    "album": str,
    "state": In(["normal", "banned"])
})

create_election = Schema(
    {
        "name": str,
        "state": In(["ongoing", "finished"]),
        "type": In(["topic", "song"])
    },
    required=True)

get_elections = Schema({
    "offset":
    All(int, Range(min=0)),
    Required("limit"):
    All(int, Range(min=1, max=100)),
    Required("type", default="any"):
    In(["topic", "song", "any"])
})

add_option = Schema({
    Required("electionUid"): str,
    Required("name"): str,
    Required("type"): In(["topic", "song"]),
    "singer": str,
    "cutCommentary": str,
    "album": str,
    "serviceLink": str
})
Example #15
0
def number_of_replicas():
    return {
        Optional('number_of_replicas', default=1):
        All(Coerce(int), Range(min=0, max=10))
    }
Example #16
0
 def extra_schema(self):
     return {
         Optional('probability'): All(float),
         Optional('correlation'): All(int)
     }
Example #17
0
def number_of_shards():
    return {
        Optional('number_of_shards', default=1):
        All(Coerce(int), Range(min=1, max=99))
    }
Example #18
0
def argument_spec_schema(for_collection):
    any_string_types = Any(*string_types)
    schema = {
        any_string_types: {
            'type':
            Any(is_callable, *argument_spec_types),
            'elements':
            Any(*argument_spec_types),
            'default':
            object,
            'fallback':
            Any(
                (is_callable, list_string_types),
                [is_callable, list_string_types],
            ),
            'choices':
            Any([object], (object, )),
            'required':
            bool,
            'no_log':
            bool,
            'aliases':
            Any(list_string_types, tuple(list_string_types)),
            'apply_defaults':
            bool,
            'removed_in_version':
            version(for_collection),
            'removed_at_date':
            date(),
            'removed_from_collection':
            collection_name,
            'options':
            Self,
            'deprecated_aliases':
            Any([
                All(
                    Any(
                        {
                            Required('name'): Any(*string_types),
                            Required('date'): date(),
                            Required('collection_name'): collection_name,
                        },
                        {
                            Required('name'): Any(*string_types),
                            Required('version'): version(for_collection),
                            Required('collection_name'): collection_name,
                        },
                    ),
                    partial(check_removal_version,
                            version_field='version',
                            collection_name_field='collection_name',
                            error_code='invalid-removal-version'))
            ]),
        }
    }
    schema[any_string_types].update(argument_spec_modifiers)
    schemas = All(
        schema,
        Schema({any_string_types: no_required_with_default}),
        Schema({any_string_types: elements_with_list}),
        Schema({any_string_types: options_with_apply_defaults}),
        Schema({any_string_types: option_deprecation}),
    )
    return Schema(schemas)
Example #19
0
def preserve_existing():
    return {
        Optional('preserve_existing', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #20
0
def list_dict_option_schema(for_collection):
    suboption_schema = Schema(
        {
            Required('description'):
            Any(list_string_types, *string_types),
            'required':
            bool,
            'choices':
            list,
            'aliases':
            Any(list_string_types),
            'version_added':
            version(for_collection),
            'version_added_collection':
            collection_name,
            'default':
            json_value,
            # Note: Types are strings, not literal bools, such as True or False
            'type':
            Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json',
                'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
            # in case of type='list' elements define type of individual item in list
            'elements':
            Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json',
                'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
            # Recursive suboptions
            'suboptions':
            Any(None, *list({str_type: Self} for str_type in string_types)),
        },
        extra=PREVENT_EXTRA)

    # This generates list of dicts with keys from string_types and suboption_schema value
    # for example in Python 3: {str: suboption_schema}
    list_dict_suboption_schema = [{
        str_type: suboption_schema
    } for str_type in string_types]

    option_schema = Schema(
        {
            Required('description'):
            Any(list_string_types, *string_types),
            'required':
            bool,
            'choices':
            list,
            'aliases':
            Any(list_string_types),
            'version_added':
            version(for_collection),
            'version_added_collection':
            collection_name,
            'default':
            json_value,
            'suboptions':
            Any(None, *list_dict_suboption_schema),
            # Note: Types are strings, not literal bools, such as True or False
            'type':
            Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json',
                'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
            # in case of type='list' elements define type of individual item in list
            'elements':
            Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int', 'json',
                'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
        },
        extra=PREVENT_EXTRA)

    option_version_added = Schema(All(
        {
            'suboptions':
            Any(None, *[{
                str_type: Self
            } for str_type in string_types]),
        }, partial(version_added, error_code='option-invalid-version-added')),
                                  extra=ALLOW_EXTRA)

    # This generates list of dicts with keys from string_types and option_schema value
    # for example in Python 3: {str: option_schema}
    return [{
        str_type: All(option_schema, option_version_added)
    } for str_type in string_types]
Example #21
0
def remote_ssl_no_validate():
    return {
        Optional('remote_ssl_no_validate', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #22
0
def return_schema(for_collection):
    return_contains_schema = Any(
        All(
            Schema({
                Required('description'):
                Any(list_string_types, *string_types),
                'returned':
                Any(*string_types),  # only returned on top level
                Required('type'):
                Any('bool', 'complex', 'dict', 'float', 'int', 'list', 'str'),
                'version_added':
                version(for_collection),
                'version_added_collection':
                collection_name,
                'sample':
                json_value,
                'example':
                json_value,
                'contains':
                Any(None,
                    *list({str_type: Self} for str_type in string_types)),
                # in case of type='list' elements define type of individual item in list
                'elements':
                Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int',
                    'json', 'jsonarg', 'list', 'path', 'raw', 'sid', 'str'),
            }),
            Schema(return_contains),
            Schema(
                partial(version_added,
                        error_code='option-invalid-version-added')),
        ),
        Schema(type(None)),
    )

    # This generates list of dicts with keys from string_types and return_contains_schema value
    # for example in Python 3: {str: return_contains_schema}
    list_dict_return_contains_schema = [{
        str_type: return_contains_schema
    } for str_type in string_types]

    return Any(
        All(
            Schema({
                any_string_types: {
                    Required('description'):
                    Any(list_string_types, *string_types),
                    Required('returned'):
                    Any(*string_types),
                    Required('type'):
                    Any('bool', 'complex', 'dict', 'float', 'int', 'list',
                        'str'),
                    'version_added':
                    version(for_collection),
                    'version_added_collection':
                    collection_name,
                    'sample':
                    json_value,
                    'example':
                    json_value,
                    'contains':
                    Any(None, *list_dict_return_contains_schema),
                    # in case of type='list' elements define type of individual item in list
                    'elements':
                    Any(None, 'bits', 'bool', 'bytes', 'dict', 'float', 'int',
                        'json', 'jsonarg', 'list', 'path', 'raw', 'sid',
                        'str'),
                }
            }),
            Schema({any_string_types: return_contains}),
            Schema({
                any_string_types:
                partial(version_added,
                        error_code='option-invalid-version-added')
            }),
        ),
        Schema(type(None)),
    )
Example #23
0
def retry_count():
    return {
        Optional('retry_count', default=3): All(Coerce(int),
                                                Range(min=0, max=100))
    }
Example #24
0
def doc_schema(module_name, for_collection=False, deprecated_module=False):

    if module_name.startswith('_'):
        module_name = module_name[1:]
        deprecated_module = True
    doc_schema_dict = {
        Required('module'):
        module_name,
        Required('short_description'):
        Any(*string_types),
        Required('description'):
        Any(list_string_types, *string_types),
        Required('author'):
        All(Any(None, list_string_types, *string_types), author),
        'notes':
        Any(None, list_string_types),
        'seealso':
        Any(None, seealso_schema),
        'requirements':
        list_string_types,
        'todo':
        Any(None, list_string_types, *string_types),
        'options':
        Any(None, *list_dict_option_schema(for_collection)),
        'extends_documentation_fragment':
        Any(list_string_types, *string_types),
        'version_added_collection':
        collection_name,
    }

    if for_collection:
        # Optional
        doc_schema_dict['version_added'] = version(for_collection=True)
    else:
        doc_schema_dict[Required('version_added')] = version(
            for_collection=False)

    if deprecated_module:
        deprecation_required_scheme = {
            Required('deprecated'):
            Any(deprecation_schema(for_collection=for_collection)),
        }

        doc_schema_dict.update(deprecation_required_scheme)

    def add_default_attributes(more=None):
        schema = {
            'description': any_string_types,
            'support': any_string_types,
            'version_added_collection': any_string_types,
            'version_added': any_string_types,
        }
        if more:
            schema.update(more)
        return schema

    doc_schema_dict['attributes'] = Schema(
        All(
            Schema(
                {
                    any_string_types: {
                        Required('description'): any_string_types,
                        Required('support'): Any('full', 'partial', 'none'),
                        'version_added_collection': collection_name,
                        'version_added':
                        version(for_collection=for_collection),
                    },
                },
                extra=ALLOW_EXTRA),
            partial(version_added,
                    error_code='attribute-invalid-version-added',
                    accept_historical=False),
            Schema(
                {
                    any_string_types:
                    add_default_attributes(),
                    'action_group':
                    add_default_attributes({
                        Required('membership'):
                        list_string_types,
                    }),
                    'forced_action_plugin':
                    add_default_attributes({
                        Required('action_plugin'):
                        any_string_types,
                    }),
                    'proprietary':
                    add_default_attributes({
                        Required('platforms'):
                        list_string_types,
                    }),
                },
                extra=PREVENT_EXTRA),
        ))
    return Schema(
        All(
            Schema(doc_schema_dict, extra=PREVENT_EXTRA),
            partial(version_added,
                    error_code='module-invalid-version-added',
                    accept_historical=not for_collection),
        ))
Example #25
0
def count():
    return {Required('count'): All(Coerce(int), Range(min=0, max=10))}
Example #26
0
def max_num_segments():
    return {
        Required('max_num_segments'): All(Coerce(int), Range(min=1, max=32768))
    }
Example #27
0
def slices():
    return {
        Optional('slices', default=1):
        Any(All(Coerce(int), Range(min=1, max=500)), None)
    }
Example #28
0
def allow_ilm_indices():
    return {
        Optional('allow_ilm_indices', default=False):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #29
0
def wait_for_rebalance():
    return {
        Optional('wait_for_rebalance', default=True):
        Any(bool, All(Any(*string_types), Boolean()))
    }
Example #30
0
def test_fix_157():
    s = Schema(All([Any('one', 'two', 'three')]), Length(min=1))
    assert_equal(['one'], s(['one']))
    assert_raises(MultipleInvalid, s, ['four'])