Exemplo n.º 1
0
def test_json_parse():
    assert json_parse('["testing", 1, 2, 3]') == ([
        'value',
        [
            'array', '[',
            [
                'elements', ['value', ['string', '"testing"']], ',',
                [
                    'elements', ['value', ['number', ['int', '1']]], ',',
                    [
                        'elements', ['value', ['number', ['int', '2']]], ',',
                        ['elements', ['value', ['number', ['int', '3']]]]
                    ]
                ]
            ], ']'
        ]
    ], '')

    assert json_parse('-123.456e+789') == ([
        'value',
        ['number', ['int', '-123'], ['frac', '.456'], ['exp', 'e+789']]
    ], '')

    assert json_parse(
        '{"age": 21, "state":"CO","occupation":"rides the rodeo"}') == ([
            'value',
            [
                'object', '{',
                [
                    'members',
                    [
                        'pair', ['string', '"age"'], ':',
                        ['value', ['number', ['int', '21']]]
                    ], ',',
                    [
                        'members',
                        [
                            'pair', ['string', '"state"'], ':',
                            ['value', ['string', '"CO"']]
                        ], ',',
                        [
                            'members',
                            [
                                'pair', ['string', '"occupation"'], ':',
                                ['value', ['string', '"rides the rodeo"']]
                            ]
                        ]
                    ]
                ], '}'
            ]
        ], '')
Exemplo n.º 2
0
 def test_to_json(self):
     self.assertEqual(json.dumps(try_bool), json_parse().to_json(try_bool))
     self.assertEqual(json.dumps(try_dict), json_parse().to_json(try_dict))
     self.assertEqual(json.dumps(try_list), json_parse().to_json(try_list))
     self.assertEqual(json.dumps(try_dict_none),
                      json_parse().to_json(try_dict_none))
     self.assertEqual(json.dumps(try_str), json_parse().to_json(try_str))
     self.assertEqual(json.dumps(try_int), json_parse().to_json(try_int))
     self.assertEqual(json.dumps(tr), json_parse().to_json(tr))
     self.assertEqual(json.dumps(try_float),
                      json_parse().to_json(try_float))
     self.assertEqual(json.dumps(try_tuple),
                      json_parse().to_json(try_tuple))
     self.assertEqual(json.dumps(try_none), json_parse().to_json(try_none))
Exemplo n.º 3
0
    async def _fetch_metadata(self) -> tuple[JSONObject, bytes]:
        """Fetch the metadata of the item.

        :returns:   The metadata. May be empty dict if item doesn't exist.
        """
        url = f'https://archive.org/metadata/{self._identifier}'
        self._logger.info(f'Loading {url}')
        async with self._session.get(url) as resp:
            resp.raise_for_status()
            metadata_raw = await resp.read()

            try:
                metadata = json_parse(metadata_raw)
            except ValueError as exc:
                raise IAException('Malformed metadata') from exc

            if metadata and 'error' in metadata:
                raise IAException(metadata['error'])

            # Empty metadata, should be 404
            if not metadata:
                self._logger.info('Got empty metadata, item should be 404')
                if not await self._is_404():
                    raise IAException('Empty response on non-404 item')

            return metadata, metadata_raw
Exemplo n.º 4
0
    async def _load_json_from_cache(self,
                                    path: AsyncPath) -> Optional[JSONObject]:
        raw = await self._load_from_cache(path)
        if raw is None:
            return None

        try:
            return json_parse(raw)
        except ValueError as exc:
            self._logger.error(f'Failed to load {path.name} from cache: {exc}')
            return None
Exemplo n.º 5
0
 def test_loads(self):
     data = json.dumps(try_bool)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_dict)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_list)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_str)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_int)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_float)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_tuple)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     data = json.dumps(try_none)
     self.assertEqual(json.loads(data), json_parse().loads(data))
     try:
         a = json.loads(try_error)
     except BaseException:
         a = None
     self.assertEqual(a, json_parse().loads(try_error))
Exemplo n.º 6
0
    async def has_pending_tasks(self) -> bool:
        async with self._session.get('https://archive.org/services/tasks.php',
                                     params={
                                         'summary': 1,
                                         'identifier': self._identifier
                                     }) as resp:
            summary_raw = await resp.read()
            try:
                summary = json_parse(summary_raw)
                if not summary['success']:
                    raise IAException(summary['error'])

                return any(
                    v != 0
                    for _, v in summary.at_pointer('/value/summary').items())
            except ValueError as exc:
                raise IAException('Unable to parse catalog summary') from exc
Exemplo n.º 7
0
def property_set_deserialize(json) -> PropertySet:
    parsed_object = json_parse(json)

    # Is this a PropertySet?
    if 'name' not in parsed_object:
        raise ParseError('PropertySet invalid: Could not find name string')

    if 'type' not in parsed_object:
        raise ParseError('PropertySet ' + parsed_object['name'] +
                         ' is invalid: Could not find type string')

    if not parsed_object['type'] == 'PropertySet':
        raise ParseError('PropertySet ' + parsed_object['name'] +
                         ' is invalid: Type string is: ' +
                         parsed_object['type'] + ', should be "PropertySet"')

    property_set = PropertySet(parsed_object['name'])

    if 'properties' in parsed_object:
        for property in parsed_object['properties']:
            # Is this a property?
            if 'name' not in property:
                raise ParseError('Property is invalid: No name string found')
            if 'type' not in property:
                raise ParseError('Property ' + property['name'] +
                                 ' is invalid: No type string found')
            if not property['type'] == 'Property':
                raise ParseError('Property ' + property['name'] +
                                 ' is invalid: Type string is ' +
                                 property['type'] + ', should be "Property"')
            if 'value' not in property:
                raise ParseError('Property ' + property['name'] +
                                 ' is invalid: No value found')

            property_set.properties.append(
                Property(property['name'], property['value']))

    return property_set
Exemplo n.º 8
0
 def __init__(self, json_raw: bytes) -> None:
     self._d = json_to_native(json_parse(json_raw))