示例#1
0
文件: utils.py 项目: cbernet/patacrep
def validate_yaml_schema(data, schema):
    """Check that the data respects the schema

    Will raise `SchemaError` if the schema is not respected.
    """
    schema = Rx.make_schema(schema)

    if isinstance(data, DictOfDict):
        data = dict(data)

    try:
        schema.validate(data)
    except Rx.SchemaMismatch as exception:
        raise errors.SchemaError(rx_exception=exception)
示例#2
0
def validate_yaml_schema(data, schema):
    """Check that the data respects the schema

    Will raise `SchemaError` if the schema is not respected.
    """
    schema = Rx.make_schema(schema)

    if isinstance(data, DictOfDict):
        data = dict(data)

    try:
        schema.validate(data)
    except Rx.SchemaMismatch as exception:
        raise errors.SchemaError(rx_exception=exception)
示例#3
0
def validate_parser_argument(raw_schema):
    """Check that the parser argument respects the schema

    Will raise `ContentError` if the schema is not respected.
    """
    schema = Rx.make_schema(yaml.load(raw_schema))

    def wrap(parse):
        """Wrap the parse function"""
        def wrapped(keyword, argument, config):
            """Check the argument schema before calling the plugin parser"""
            try:
                schema.validate(argument)
            except Rx.SchemaMismatch as exception:
                msg = 'Invalid syntax:\n---\n{}---\n{}'.format(
                    yaml.dump({keyword: argument}, default_flow_style=False),
                    str(exception)
                )
                raise ContentError(keyword, msg)
            return parse(keyword, argument=argument, config=config)
        return wrapped
    return wrap
示例#4
0
def validate_parser_argument(raw_schema):
    """Check that the parser argument respects the schema

    Will raise `ContentError` if the schema is not respected.
    """
    schema = Rx.make_schema(yaml.load(raw_schema))

    def wrap(parse):
        """Wrap the parse function"""
        def wrapped(keyword, argument, config):
            """Check the argument schema before calling the plugin parser"""
            try:
                schema.validate(argument)
            except Rx.SchemaMismatch as exception:
                msg = 'Invalid syntax:\n---\n{}---\n{}'.format(
                    yaml.dump({keyword: argument}, default_flow_style=False),
                    str(exception)
                )
                raise ContentError(keyword, msg)
            return parse(keyword, argument=argument, config=config)
        return wrapped
    return wrap