def _create_parsers( spec: config_spec.ConfigSpec ) -> typing.List[data_type_parser.DataTypeParser]: """Creates all parsers required to parse a configuration of the provided spec. Args: spec (:class:`config_spec.ConfigSpec`): The spec that describes the configuration that needs to be parsed. Returns: list[:class:`data_type_parser.DataTypeParser`]: The created parsers. Raises: ValueError: If configuration values of an unsupported type are encountered in the ``spec``. """ field_parsers = [] for value_spec in spec: # -> iterate over config values in the spec # create a parser for the currently considered config value if value_spec.data_type is bool: field_parsers.append(bool_parser.BoolParser(value_spec)) elif value_spec.data_type is float: field_parsers.append(float_parser.FloatParser(value_spec)) elif value_spec.data_type is int: field_parsers.append(int_parser.IntParser(value_spec)) elif value_spec.data_type is str: field_parsers.append(str_parser.StrParser(value_spec)) else: raise ValueError( f"Unsupported type of property <{value_spec.name} in the configuration class: " f"{value_spec.data_type}") return field_parsers
def test_parse_raises_a_value_error_if_an_illegal_value_is_provided(self): parser = float_parser.FloatParser(value_spec.ValueSpec("some_config", "Just a test", float, True, None)) argv = "--some-config", "not-a-number" self.assertTrue(parser.fires(argv)) with self.assertRaises(ValueError): parser._parse(argv)
def test_parse_raises_a_value_error_if_an_expected_value_is_missing(self): parser = float_parser.FloatParser(value_spec.ValueSpec("some_config", "Just a test", float, True, None)) argv = "--some-config", self.assertTrue(parser.fires(argv)) with self.assertRaises(ValueError): parser._parse(argv)
def test_parse_json_processes_values_correctly(self): parser = float_parser.FloatParser(value_spec.ValueSpec("some_config", "Just a test", float, True, None)) value = parser.parse_json(666.666) self.assertIsInstance(value, float) self.assertEqual(666.666, value) value = parser.parse_json(666) self.assertIsInstance(value, float) self.assertEqual(666.0, value)
def test_parse_extracts_legal_values_as_expected(self): parser = float_parser.FloatParser(value_spec.ValueSpec("some_config", "Just a test", float, True, None)) value, argv = parser._parse(("--some-config", "666.666", "--another-config", "another value")) self.assertIsInstance(value, float) self.assertEqual((666.666, ("--another-config", "another value")), (value, argv)) value, argv = parser._parse(("--some-config", "666.666")) self.assertIsInstance(value, float) self.assertEqual((666.666, tuple()), (value, argv)) value, argv = parser._parse(("--some-config", "666")) self.assertIsInstance(value, float) self.assertEqual((666.0, tuple()), (value, argv))