Esempio n. 1
0
 def __init__(self, message: str, source: object, target: type,
              error: JSONDecodeError):
     """
     Constructor.
     :param message: the message of this error.
     :param source: the object that was to be deserialized.
     :param target: the type to which `source` was to be deserialized.
     :param error: the wrapped `JSONDecodeError`.
     """
     DeserializationError.__init__(self, message, source, target)
     JSONDecodeError.__init__(self, message, error.doc, error.pos)
Esempio n. 2
0
 def __init__(self, file_name):
     """
     Parameterized constructor with file_name parameter to instantiate class instance
     :param file_name:   path to the configuration file to instantiate class instance
     :raise              FileNotFoundError or JSONDecodeError in the file was not found
                         or can not be read as json dictionary
     """
     self.file_name = file_name
     # names of mandatory entities (1st level) in the configuration file
     self._videos = "videos"
     self._report_folder = "report_folder"
     self._report_name = "report_name"
     self._reference_video = "reference_video"
     self._compressed_video = "compressed_video"
     try:
         with open(file_name, 'r') as config:
             self._data = json.load(config)
     except FileNotFoundError:
         raise FileNotFoundError(
             "{} is not found or the path is incorrect".format(
                 self.file_name))
     except JSONDecodeError as json_exc:
         raise JSONDecodeError(
             "{} invalid json file".format(self.file_name), json_exc.doc,
             json_exc.pos)
Esempio n. 3
0
 def decode(self,
            s,
            _w=WHITESPACE.match,
            _ws=WHITESPACE_STR,
            _ilcs=INLINE_COMMENT_STRING_START,
            _ilc=INLINE_COMMENT.match,
            _mlcs=MULTILINE_COMMENT_STRING_START,
            _mlc=MULTILINE_COMMENT.match):
     # pylint: disable=arguments-differ
     idx = 0
     try:
         while True:  # Handle comments and whitespaces
             if s[idx] in _ws:
                 idx = _w(s, idx).end()
             elif s[idx:].startswith(_ilcs):
                 idx = _ilc(s, idx).end()
             elif s[idx:].startswith(_mlcs):
                 idx = _mlc(s, idx).end()
             else:
                 break
     except IndexError:
         pass
     obj, end = self.raw_decode(s, idx)
     end = _w(s, end).end()
     if end != len(s):
         raise JSONDecodeError("Extra data", s, end)
     return obj
Esempio n. 4
0
def _receive_file(access, local_file_path):
    with open(access) as f:
        access = json.load(f)

    http_method = http_method_func(access, 'GET')
    auth_method = auth_method_obj(access)

    verify = True
    if access.get('disableSSLVerification'):
        verify = False

    r = http_method(access['url'],
                    auth=auth_method,
                    verify=verify,
                    timeout=DEFAULT_TIMEOUT)
    r.raise_for_status()
    try:
        data = r.json()
    except JSONDecodeError as e:
        raise JSONDecodeError(
            'Could not parse the http response as json object. Failed with the following message:\n'
            .format(str(e)), e.doc, e.pos)

    with open(local_file_path, 'w') as f:
        json.dump(data, f)
Esempio n. 5
0
    def test_get_monitoring_locations_3_success_with_json_parse_error(self):
        fake_first_url = self.fake_endpoint + '?limit=8&offset=0'

        mock_response_a = mocki({'text': self.mock_json_good, 'status_code': 200}, spec=Response)
        mock_payload_a = copy.deepcopy(self.mock_payload)
        mock_payload_a['next'] = self.fake_endpoint + '?limit=8&offset=8'
        when(mock_response_a).json().thenReturn(mock_payload_a)

        mock_response_b = mocki({'text': self.mock_json_good, 'status_code': 200}, spec=Response)
        mock_payload_b = copy.deepcopy(self.mock_payload)
        mock_payload_b['next'] = self.fake_endpoint + '?limit=8&offset=16'
        when(mock_response_b).json().thenRaise(JSONDecodeError('Bad JSON', 'Bad DOC', 0))

        mock_response_c = mocki({'text': self.mock_json_good, 'status_code': 200}, spec=Response)
        mock_payload_c = copy.deepcopy(self.mock_payload)
        # mock_payload_c['next'] = None  # this is the mock default value. It requires setting if the default changes.
        when(mock_response_c).json().thenReturn(mock_payload_c)

        when(self.mock_session).get(fake_first_url).thenReturn(mock_response_a)
        when(self.mock_session).get(mock_payload_a['next']).thenReturn(mock_response_b)
        when(self.mock_session).get(mock_payload_b['next']).thenReturn(mock_response_c)

        extract = MockExtract(self.mock_session)
        records = extract.get_monitoring_locations(self.fake_endpoint)
        # the base URL is not called in this test. Extract.construct_url() adds the fetch limit in lieu of the default.
        mockito.verify(self.mock_session, times=0).get(self.fake_endpoint)
        # each of the batch request URLs is called once. One is the default but I like explicit indication.
        mockito.verify(self.mock_session, times=1).get(extract.construct_url(self.fake_endpoint))
        # this one will fail by mock conditions after called twice
        mockito.verify(self.mock_session, times=2).get(mock_payload_a['next'])
        mockito.verify(self.mock_session, times=1).get(mock_payload_b['next'])

        self.assertEqual(2, len(records), 'there should be two mock records returned because of the middle fail')
Esempio n. 6
0
    def test_get_announcements_wrong_json_response_handling(
            self, requests_session, get_used_options):
        # wrong JSON structure
        announcements = {
            "type":
            "notice",
            "message":
            "You are using an outdated version of Safety. Please upgrade to Safety version 1.2.3"
        }

        mock = Mock()
        mock.status_code = HTTPStatus.OK.value
        mock.json.return_value = announcements
        requests_session.get.return_value = mock

        self.assertEqual(safety.get_announcements('somekey', {}), [])

        # JSONDecodeError

        mock = Mock()
        mock.status_code = HTTPStatus.OK.value
        mock.json.side_effect = JSONDecodeError(msg='Expecting value',
                                                doc='',
                                                pos=0)
        requests_session.get.return_value = mock

        self.assertEqual(safety.get_announcements('somekey', {}), [])
Esempio n. 7
0
def _parse_json(f, schemacls):
    """
    :param f: file-like object to read json from
    :param schemacls: the schema to parse the properties with
    :return: a dictionary containing the validated object
    :raises: JSONDecodeError if the JSON is malformed
    """
    schema = schemacls()
    try:
        obj, errors = schema.loads(f.read())
        if errors:
            raise JSONDecodeError("schema validation failed {}".format(errors),
                                  "", 0)
        return obj
    except ValueError as e:
        raise JSONDecodeError("failed to decode JSON: {}".format(e), "", pos=0)
    def from_json(self, json_form, overwrite_name=True):
        """     
        Load the table from a JSON string, of the form produced by toJSON().  Note
        that if the overwrite_name parameter = True (the default), this will also
        overwrite the table name.

        Throws InvalidDataException id json_form is malformed

        Args:
            json_form: A JSON form of the Dictionary

        Returns:
            None

        Throws:
            InvalidDataException if json_form is malformed

        """
        try:
            record = loads(json_form)
        except JSONDecodeError(msg):
            raise InvalidDataException(msg)
        if (type(record) != dict):
            raise InvalidDataException(
                f'JSON form of table must be a dictionary, not {type(record)}')
        self._check_fields(record, {"name", "table"}, 'JSON form of table')
        self.load_from_dictionary(record["table"])
        if (overwrite_name):
            self.name = record["name"]
Esempio n. 9
0
def get_message_from_server(socket):
    response = socket.recv(CONFIGS.CONFIG_PROJECT['DEFAULT_CONF'].get('MAX_PACKAGE_LENGTH'))
    try:
        response_decode = response.decode(CONFIGS.CONFIG_PROJECT['DEFAULT_CONF'].get('ENCODING'))
    except:
        raise JSONDecodeError('Ошибка декодирования')
    return json.loads(response_decode)
    async def test_request_json_decode_error(self, request_builder):
        # Set up mocks
        request_mock = AsyncMock(return_value=httpx.Response(request=Mock(), status_code=200, content=b"{}"))
        request_builder._httpx_client.send = request_mock

        # Run
        with patch.object(Response, "json", side_effect=JSONDecodeError("", "", 0)), pytest.raises(JSONDecodeError):
            await request_builder.foo.bar.retrieve(pk=1)
Esempio n. 11
0
def test_deals_with_JSONDecodeError():
    error_msg = 'ANY ERROR MESSAGE'
    m = mock.Mock(status_code=200)
    m.json.side_effect = JSONDecodeError(msg=error_msg, doc='', pos=0)
    with mock.patch('robot.proxymonitor.proxytools.requests.get') as mock_get:
        mock_get.return_value = m
        response = proxytools.get_connection_info()
    assert error_msg in response.get('status')
Esempio n. 12
0
def api_request(url: str) -> dict:
    r = requests.get(url=url, params={'address': 'Singapore'})
    try:
        return r.json()
    except JSONDecodeError:
        with open('JSONDecodeError.txt', 'w') as file:
            file.write(r.text)
            file.close()
        raise JSONDecodeError()
Esempio n. 13
0
 def test_invoke_json_decode_error(self):
     intent = self.intent
     intent.implementation = error_intent
     with patch('skill_sdk.routes.request', new=self.request), \
             patch('skill_sdk.log.prepare_for_logging', side_effect=JSONDecodeError('', '', 0)), \
             patch.object(skill_sdk.skill.Skill, '_intents', new={'TELEKOM_Demo_Intent': intent}):
         from skill_sdk.routes import invoke
         result = invoke()
     self.assertEqual(result.status_code, 400)
    def test_logging_error(self):
        aiohttp.ClientResponse.json = AsyncMock(side_effect=JSONDecodeError('ERROR', '', 0))

        async def test():
            self.data = await self.handler.get_dorks()

        with self.assertLogs(level='ERROR') as log:
            self.loop.run_until_complete(test())
            self.assertIn('Error getting dorks: ERROR: line 1 column 1 (char 0)', log.output[0])
Esempio n. 15
0
    def test_json_decode_error(self):
        cache.clear()

        requests.get = Mock(
            side_effect=JSONDecodeError("Error in JSON format", "", 0))

        res = movie_list(self.req, 'films', 'people')

        self.assertEqual(res.content, b'Problem with the Movie API')
    def test_update_with_json_decode_error(self, mock_session, mock_request):
        """Test updating feed raises exception."""
        home_coordinates = (-31.0, 151.0)
        mock_session.return_value.__enter__.return_value.send\
            .side_effect = JSONDecodeError("", "", 0)

        feed = GenericFeed(home_coordinates, None)
        status, entries = feed.update()
        assert status == UPDATE_ERROR
        self.assertIsNone(entries)
Esempio n. 17
0
    def test_reports_error_when_json_unparseable(self, patch_config, mock_json,
                                                 config, timepoint):
        patch_config.return_value = config
        mock_json.side_effect = JSONDecodeError("", "", 0)

        manifests = dm_utils.get_manifests(timepoint)

        for session in manifests:
            for scan in manifests[session]:
                json_contents = manifests[session][scan]
                assert "Error" in json_contents
Esempio n. 18
0
    def test_fetch_record_block_2_bad_json(self):
        # ensure that the retries works and that if the JSON is bad that it only tries TWICE
        self.assertEqual(self.extract.FETCH_TRIES_FOR_JSON, 2)  # testing that it is reset between tests

        mock_response_a = mocki({'text': 'bad JSON twice', 'status_code': 200}, spec=Response)
        when(self.mock_session).get(self.fake_endpoint).thenReturn(mock_response_a)
        when(mock_response_a).json().thenRaise(JSONDecodeError('', '', 0))

        self.assertRaises(JSONDecodeError,
                          lambda: self.extract.fetch_record_block(self.fake_endpoint, self.mock_session))
        mockito.verify(self.mock_session, times=self.extract.FETCH_TRIES_FOR_JSON).get(self.fake_endpoint)
Esempio n. 19
0
    def test_store_payments_registry_fake_json_decode_error(
            self, shelve, json):
        text = "test"
        shelve.open.return_value.__enter__.return_value = dict()
        json.loads.side_effect = JSONDecodeError("test", "test", 1)

        store_payments_registry_fake(text)

        self.assertEqual(shelve.open.call_count, 0)
        self.assertEqual(shelve.open.return_value.__enter__.return_value,
                         dict())
Esempio n. 20
0
def json_to_list(json_str: str) -> list:
    """
    Transforms the json string into tree made up of dictionaries and list.
    :param json_str: The json string to transform
    :return: the list
    """
    if not re.match(r'\s*\[', json_str):
        raise JSONDecodeError(
            "Unexpected character.  For the root object to be a list the json string must start with",
            json_str, 0)
    return json.loads(json_str)
Esempio n. 21
0
def bad_response_invalid_json():
    """Return a mock response with a 404 status code but with bad json."""
    mock_response = mock.Mock(status_code=404, text='<No JSON!>')
    mock_response.request = mock_request()
    mock_response.json = Mock()
    mock_response.json.side_effect = JSONDecodeError(
        msg='no json',
        doc='<no json>',
        pos=0
    )
    return mock_response
Esempio n. 22
0
def objectFromJSONText(text: str) -> Any:
    """
    Convert JSON text into an object.
    """
    try:
        return loads(text)
    except JSONDecodeError as e:
        raise JSONDecodeError(
            msg="{} in {!r}".format(e.msg, text),
            doc=e.doc,
            pos=e.pos,
        )
Esempio n. 23
0
    def test_fetch_record_block_1_bad_json(self):
        # ensure that the retries works and that if the JSON is bad the first time that it can succeed after
        self.assertEqual(self.extract.FETCH_TRIES_FOR_JSON, 2)  # testing that it is reset between tests

        mock_response_a = mocki({'text': 'bad JSON once', 'status_code': 200}, spec=Response)
        when(self.mock_session).get(self.fake_endpoint).thenReturn(mock_response_a)
        when(mock_response_a).json().thenRaise(JSONDecodeError('', '', 0)).thenReturn(self.mock_json_payload)

        json_response = self.extract.fetch_record_block(self.fake_endpoint, self.mock_session)
        mockito.verify(self.mock_session, times=2).get(self.fake_endpoint)

        self.assertEqual(self.mock_json_payload, json_response)
Esempio n. 24
0
def get_private_key(designation, alternate_key_dir=None):
    keyfiles = get_key_files(alternate_key_dir=alternate_key_dir)
    for keyfile in keyfiles:
        try:
            with open(keyfile, 'r') as keyfile_object:
                key_json = json.loads(keyfile_object.read())

            return key_json[designation]
        except OSError as exc:
            raise OSError('Key directory may be empty', exc)
        except JSONDecodeError:
            raise JSONDecodeError('A problem occured with the key file')
    def test_submit_data_error(self):
        aiohttp.ClientResponse.json = AsyncMock(
            side_effect=JSONDecodeError('ERROR', '', 0))

        async def test():
            self.result = await self.handler.submit_data(self.data)

        with self.assertLogs(level='ERROR') as log:
            self.loop.run_until_complete(test())
            self.assertIn(
                'Error submitting data: ERROR: line 1 column 1 (char 0) {}'.
                format(self.data), log.output[0])
Esempio n. 26
0
def objectFromJSONText(text: str) -> Any:
    """
    Convert JSON text into an object.
    """
    try:
        return loads(text)
    except JSONDecodeError as e:
        raise JSONDecodeError(
            msg=f"{e.msg} in {text!r}",
            doc=e.doc,
            pos=e.pos,
        )
Esempio n. 27
0
    def __init__(self, dsp_config="", protein_seqs=None):

        self.protein_seqs = protein_seqs
        self.dsp_config = dsp_config
        self.parameters = {}

        config_filepath = ""
        #read protein seqs from dataset if not input as parameter,
        # parse DSP parameters from config file
        if not isinstance(dsp_config, str) or dsp_config is None:
            raise TypeError(
                'JSON config file must be a filepath of type string, got type {}.'
                .format(type(dsp_config)))
        if os.path.isfile(self.dsp_config):
            config_filepath = self.dsp_config
        elif os.path.isfile(os.path.join('config', self.dsp_config)):
            config_filepath = os.path.join('config', self.dsp_config)
        else:
            raise OSError('JSON config file not found at path: {}.'.format(
                config_filepath))
        try:
            with open(config_filepath) as f:
                self.parameters = json.load(f)
        except:
            raise JSONDecodeError(
                'Error parsing config JSON file: {}.'.format(config_filepath))

        #read protein seqs from dataset if not input as parameter
        if (self.protein_seqs is None):
            raise ValueError(
                'Protein sequences input parameter cannot be empty or None.')

        #reshape protein sequences to 2 dimensions
        if (self.protein_seqs.ndim != 2):
            try:
                self.protein_seqs = self.protein_seqs.reshape((-1, 1))
            except:
                raise ValueError('Error reshaping input sequences.')

        #set all DSP parameters
        self.dsp_parameters = self.parameters["pyDSP"]
        self.use_dsp = self.dsp_parameters[0]["use_dsp"]
        self.spectrum = self.dsp_parameters[0]["spectrum"]
        self.window_type = self.dsp_parameters[0]["window"]
        self.window = self.dsp_parameters[0]["window"]
        self.filter = self.dsp_parameters[0]["filter"]
        self.convolution = self.dsp_parameters[0]["convolution"]

        #pre-processing of encoded protein sequences
        self.pre_processing()

        #transform sequences into the various informational protein spectra
        self.encode_seqs()
Esempio n. 28
0
    def test_fetch_record_block_2_bad_status(self):
        # ensure that the retries works and that if the STATUS CODE is bad that it only tries TWICE
        self.assertEqual(self.extract.FETCH_TRIES_FOR_STATUS_CODE, 2)  # testing that it is reset between tests

        mock_response_a = mocki({'text': 'bad status code twice', 'status_code': 300}, spec=Response)
        when(mock_response_a).raise_for_status().thenRaise(HTTPError('http error msg', response=mock_response_a))
        when(self.mock_session).get(self.fake_endpoint).thenReturn(mock_response_a)
        when(mock_response_a).json().thenRaise(JSONDecodeError("shouldn't call this", 'place holder', 0))

        # the HTTPError is captured by the tries and reported as a the parent RequestException indicating non-json error
        self.assertRaises(RequestException,
                          lambda: self.extract.fetch_record_block(self.fake_endpoint, self.mock_session))
        mockito.verify(self.mock_session, times=self.extract.FETCH_TRIES_FOR_STATUS_CODE).get(self.fake_endpoint)
Esempio n. 29
0
def test_api_post_request_handles_json_errors(product, monkeypatch):
    mocked_response = Mock(side_effect=JSONDecodeError("", "", 0))
    monkeypatch.setattr("saleor.plugins.avatax.requests.post", mocked_response)

    config = AvataxConfiguration(
        username_or_account="test", password_or_license="test", use_sandbox=False,
    )
    url = "https://www.avatax.api.com/some-get-path"

    response = api_post_request(url, {}, config)

    assert mocked_response.called
    assert response == {}
Esempio n. 30
0
    def test_from_value_dict_and_errors(self, mock_loads, mock_dumps):
        mock_dumps.return_value = 'value'
        mock_loads.side_effect = TypeError

        Geometry.from_value({'dict': 'value'})
        mock_dumps.assert_called_with({'dict': 'value'},
                                      cls=GobTypeJSONEncoder)

        mock_loads.side_effect = JSONDecodeError('msg', 'doc', 0)

        Geometry.from_value({'dict': 'value'})
        mock_dumps.assert_called_with({'dict': 'value'},
                                      cls=GobTypeJSONEncoder)