Exemple #1
0
 def _parse_toml(file_path, file_information):
     parser = TomlParser()
     try:
         return parser.parse_toml_file(file_path,
                                       file_information=file_information)
     except KeyError as e:
         raise UserFileLoadException(f"The following key is missing: {e}")
     except (NotImplementedError, ValueError) as e:
         raise UserFileLoadException(e)
    def test_parse_toml_file_calls_get_all_states(self):
        test_dict = {"toml_file_version": 0}

        parser = TomlParser(toml_reader=self.get_mocked_reader(test_dict))
        with mock.patch("sans.user_file.toml_parsers.toml_parser.TomlV1Parser"
                        ) as mocked_parser:
            parsed = parser.parse_toml_file(mock.NonCallableMock,
                                            file_information=None)
            mocked_parser.return_value.get_all_states.assert_called_once()
            self.assertEqual(
                mocked_parser.return_value.get_all_states.return_value, parsed)
    def test_returns_v1_parser(self):
        test_dict = {"toml_file_version": 0}

        parser = TomlParser(toml_reader=self.get_mocked_reader(test_dict))
        mocked_file_info = mock.NonCallableMock()
        with mock.patch("sans.user_file.toml_parsers.toml_parser.TomlV1Parser"
                        ) as mocked_import:
            parser_version = parser.get_toml_parser(
                toml_file_path=mock.NonCallableMock,
                file_information=mocked_file_info)
            self.assertEqual(mocked_import.return_value, parser_version)
            # Check correct params were forwarded on
            mocked_import.assert_called_once_with(
                test_dict, file_information=mocked_file_info)
Exemple #4
0
    def _process_user_file(self, command, file_information):
        """
        Processes a user file and retain the parased tags

        @param command: the command with the user file path
        """
        file_name = command.values[0]

        if file_name.casefold().endswith(".toml".casefold()):
            toml_file_reader = TomlParser()
            new_state_entries = toml_file_reader.parse_toml_file(
                toml_file_path=file_name, file_information=file_information)
        else:
            # Now comes the fun part where we try to coerce this to put out a State* object
            user_file_reader = UserFileReader(file_name)
            old_param_mapping = user_file_reader.read_user_file()
            command_adapter = CommandInterfaceAdapter(
                processed_state=old_param_mapping,
                file_information=file_information)
            new_state_entries = command_adapter.get_all_states(
                file_information=file_information)

        new_state_entries.data = self._data_info
        return new_state_entries
 def test_throws_for_missing_version(self):
     parser = TomlParser(toml_reader=self.get_mocked_reader({}))
     with self.assertRaises(KeyError):
         parser.get_toml_parser(toml_file_path=mock.NonCallableMock,
                                file_information=None)
 def test_throws_for_unknown_version(self):
     test_dict = {"toml_file_version": 100}
     parser = TomlParser(toml_reader=self.get_mocked_reader(test_dict))
     with self.assertRaises(NotImplementedError):
         parser.get_toml_parser(toml_file_path=mock.NonCallableMock,
                                file_information=None)