示例#1
0
    def decode(self, bytestring, **options):
        content = bytestring.decode('utf-8')
        content = content.strip()
        if not content:
            raise ParseError(message='No content.',
                             short_message='No content.',
                             pos=0,
                             lineno=1,
                             colno=1)

        try:
            data = yaml.safe_load(content)
        except (yaml.scanner.ScannerError, yaml.parser.ParserError) as exc:
            if not hasattr(exc, 'index'):
                index = 0
                lineno = 1
                colno = 1
            else:
                index = exc.index
                lineno = exc.line
                colno = exc.column
            raise ParseError(
                message='% at line %d column %d' %
                (exc.problem, exc.problem_mark.line, exc.problem_mark.column),
                short_message=exc.problem,
                pos=index,
                lineno=lineno,
                colno=colno) from None
        except ValueError as exc:
            raise ParseError('Malformed YAML. %s' % exc) from None

        CONFIG.validate(data)
        return data
示例#2
0
def parse_yaml(content, validator=None):
    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode('utf-8', 'ignore')

    if not content.strip():
        marker = Marker(0, '')
        raise ParseError('No content.', marker=marker, base_format='yaml')

    try:
        token = None if validator is None else tokenize_yaml(content)
        data = yaml.safe_load(content)
    except (yaml.scanner.ScannerError, yaml.parser.ParserError) as exc:
        position = getattr(exc, 'index', 0)
        marker = Marker(position, content)
        message = exc.problem
        raise ParseError(message, marker=marker, base_format='yaml') from None

    if validator is None:
        return data

    try:
        return validator.validate(data)
    except ValidationError as exc:
        exc.set_error_context(token, content)
        raise exc
示例#3
0
def tokenize_json(content):
    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode('utf-8', 'ignore')

    if not content.strip():
        message = ErrorMessage(text='No content.',
                               code='parse_error',
                               position=Position(line_no=1,
                                                 column_no=1,
                                                 index=0))
        raise ParseError(messages=[message], summary='Invalid JSON.')

    try:
        decoder = _TokenizingDecoder(content=content)
        return decoder.decode(content)
    except json.decoder.JSONDecodeError as exc:
        message = ErrorMessage(
            text=_strip_endings(exc.msg, [" starting at", " at"]) + ".",
            code='parse_error',
            position=Position(line_no=exc.lineno,
                              column_no=exc.colno,
                              index=exc.pos))
        raise ParseError(messages=[message], summary='Invalid JSON.') from None
示例#4
0
def parse_json(content, validator=None):
    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode('utf-8', 'ignore')

    if not content.strip():
        marker = Marker(0, '')
        raise ParseError('No content.', marker=marker, base_format='json')

    try:
        token = None if validator is None else tokenize_json(content)
        data = json.loads(content)
    except json.decoder.JSONDecodeError as exc:
        message = exc.msg + '.'
        marker = Marker(exc.pos, content)
        raise ParseError(message, marker=marker, base_format='json') from None

    if validator is None:
        return data

    try:
        return validator.validate(data)
    except ValidationError as exc:
        exc.set_error_context(token, content)
        raise exc
示例#5
0
    def decode_yaml(self, bytestring, **options):
        try:
            data = yaml.safe_load(bytestring.decode('utf-8'))
        except yaml.scanner.ScannerError as exc:
            raise ParseError(
                message='% at line %d column %d' %
                (exc.problem, exc.problem_mark.line, exc.problem_mark.column),
                short_message=exc.problem,
                pos=exc.index,
                lineno=exc.line,
                colno=exc.column) from None
        except ValueError as exc:
            raise ParseError('Malformed YAML. %s' % exc) from None

        return self.decode_data(data)
 def decode(self, bytestring, **options):
     try:
         data = json.loads(bytestring.decode("utf-8"), object_pairs_hook=dict_type)
     except ValueError as exc:
         raise ParseError("Malformed JSON. %s" % exc) from None
     jsonschema = JSON_SCHEMA.validate(data)
     return decode(jsonschema)
示例#7
0
 def decode(self, bytestring, **options):
     """
     Return raw JSON data.
     """
     try:
         return json.loads(bytestring.decode("utf-8"),
                           object_pairs_hook=collections.OrderedDict)
     except ValueError as exc:
         raise ParseError("Malformed JSON. %s" % exc) from None
示例#8
0
    def decode_json(self, bytestring, **options):
        try:
            data = json.loads(bytestring.decode('utf-8'))
        except json.decoder.JSONDecodeError as exc:
            if exc.msg.endswith(' starting at'):
                short_msg = exc.msg[:-len(' starting at')] + '.'
            elif exc.msg.endswith(' at'):
                short_msg = exc.msg[:-len(' at')] + '.'
            else:
                short_msg = exc.msg + '.'
            raise ParseError(message=str(exc),
                             short_message=short_msg,
                             pos=exc.pos,
                             lineno=exc.lineno,
                             colno=exc.colno) from None
        except ValueError as exc:
            raise ParseError('Malformed JSON. %s' % exc) from None

        return self.decode_data(data)
示例#9
0
 def decode(self, bytestring, **options):
     try:
         data = json.loads(bytestring.decode('utf-8'),
                           object_pairs_hook=dict_type)
     except ValueError as exc:
         message = ErrorMessage(text='Malformed JSON. %s' % exc,
                                code='parse_failed')
         raise ParseError(messages=[message]) from None
     jsonschema = JSON_SCHEMA.validate(data)
     return decode(jsonschema)
示例#10
0
 def decode(self, bytestring, **options):
     content = bytestring.decode('utf-8')
     content = content.strip()
     if not content:
         raise ParseError(message='No content.',
                          short_message='No content.',
                          pos=0,
                          lineno=1,
                          colno=1)
     if content[0] in '{[':
         return self.decode_json(bytestring, **options)
     return self.decode_yaml(bytestring, **options)
示例#11
0
def infer_json_or_yaml(content):
    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode('utf-8', 'ignore')

    content = content.strip()
    if not content:
        marker = Marker(0, '')
        raise ParseError('No content.', marker=marker)

    return 'json' if content[0] in '{[' else 'yaml'
示例#12
0
def infer_json_or_yaml(content):
    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode("utf-8", "ignore")

    content = content.strip()
    if not content:
        marker = Marker(0, "")
        raise ParseError("No content.", marker=marker)

    return "json" if content[0] in "{[" else "yaml"
示例#13
0
    def decode_yaml(self, bytestring, **options):
        try:
            data = yaml.safe_load(bytestring.decode('utf-8'))
        except (yaml.scanner.ScannerError, yaml.parser.ParserError) as exc:
            if not hasattr(exc, 'index'):
                index = 0
                lineno = 1
                colno = 1
            else:
                index = exc.index
                lineno = exc.line
                colno = exc.column
            raise ParseError(
                message='% at line %d column %d' %
                (exc.problem, exc.problem_mark.line, exc.problem_mark.column),
                short_message=exc.problem,
                pos=index,
                lineno=lineno,
                colno=colno) from None
        except ValueError as exc:
            raise ParseError('Malformed YAML. %s' % exc) from None

        return self.decode_data(data)
示例#14
0
    def decode(self, bytestring, **options):
        try:
            data = json.loads(bytestring.decode('utf-8'))
        except ValueError as exc:
            raise ParseError('Malformed JSON. %s' % exc) from None

        openapi = OPEN_API.validate(data)
        title = lookup(openapi, ['info', 'title'])
        description = lookup(openapi, ['info', 'description'])
        version = lookup(openapi, ['info', 'version'])
        base_url = lookup(openapi, ['servers', 0, 'url'])
        schema_definitions = self.get_schema_definitions(openapi)
        content = self.get_content(openapi, base_url, schema_definitions)
        return Document(title=title, description=description, version=version, url=base_url, content=content)
示例#15
0
def tokenize_yaml(content):
    class CustomLoader(SafeLoader):
        pass

    def construct_mapping(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        mapping = loader.construct_mapping(node)
        return DictToken(mapping, start, end - 1, content=content)

    def construct_sequence(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_sequence(node)
        return ListToken(value, start, end - 1, content=content)

    def construct_scalar(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_scalar(node)
        return ScalarToken(value, start, end - 1, content=content)

    def construct_int(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_yaml_int(node)
        return ScalarToken(value, start, end - 1, content=content)

    def construct_float(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_yaml_float(node)
        return ScalarToken(value, start, end - 1, content=content)

    def construct_bool(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_yaml_bool(node)
        return ScalarToken(value, start, end - 1, content=content)

    def construct_null(loader, node):
        start = node.start_mark.index
        end = node.end_mark.index
        value = loader.construct_yaml_null(node)
        return ScalarToken(value, start, end - 1, content=content)

    CustomLoader.add_constructor(
        yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)

    CustomLoader.add_constructor(
        yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, construct_sequence)

    CustomLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG,
                                 construct_scalar)

    CustomLoader.add_constructor('tag:yaml.org,2002:int', construct_int)

    CustomLoader.add_constructor('tag:yaml.org,2002:float', construct_float)

    CustomLoader.add_constructor('tag:yaml.org,2002:bool', construct_bool)

    CustomLoader.add_constructor('tag:yaml.org,2002:null', construct_null)

    assert isinstance(content, (str, bytes))

    if isinstance(content, bytes):
        content = content.decode('utf-8', 'ignore')

    if not content.strip():
        message = ErrorMessage(text='No content.',
                               code='parse_error',
                               position=Position(line_no=1,
                                                 column_no=1,
                                                 index=0))
        raise ParseError(errors=[message], summary='Invalid YAML.')

    try:
        return yaml.load(content, CustomLoader)
    except (yaml.scanner.ScannerError, yaml.parser.ParserError) as exc:
        index = getattr(exc, 'index', 0)
        message = ErrorMessage(text=exc.problem + ".",
                               code='parse_error',
                               position=_get_position(content, index=index))
        raise ParseError(messages=[message], summary='Invalid YAML.') from None