|
29 | 29 | List, |
30 | 30 | Optional, |
31 | 31 | TextIO, |
| 32 | + Tuple, |
32 | 33 | Type, |
33 | 34 | TypeVar, |
34 | 35 | Union, |
@@ -320,11 +321,7 @@ def _load_agent_config(self, file_pointer: TextIO) -> AgentConfig: |
320 | 321 |
|
321 | 322 | def _load_service_config(self, file_pointer: TextIO) -> PackageConfiguration: |
322 | 323 | """Load a service configuration.""" |
323 | | - configuration_data = yaml_load_all(file_pointer) |
324 | | - if not configuration_data: |
325 | | - raise ValueError("Service configuration file was empty.") |
326 | | - |
327 | | - service_config, *overrides = configuration_data |
| 324 | + service_config, overrides = parse_service_yaml(file_pointer) |
328 | 325 | self.validate(service_config) |
329 | 326 | key_order = list(service_config.keys()) |
330 | 327 | service = self.configuration_class.from_json(service_config) |
@@ -384,6 +381,44 @@ def _process_component_section( |
384 | 381 | return component_id |
385 | 382 |
|
386 | 383 |
|
| 384 | +def parse_service_yaml( |
| 385 | + file_pointer: TextIO, |
| 386 | +) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]: |
| 387 | + """ |
| 388 | + Parse a service configuration YAML stream into its raw documents. |
| 389 | +
|
| 390 | + Returns the head service configuration document and any override |
| 391 | + documents without applying schema validation or constructing a |
| 392 | + typed configuration object. Callers that need to apply additional |
| 393 | + transformations (e.g. environment variable substitution) before |
| 394 | + validation can use this helper as the parse step. |
| 395 | +
|
| 396 | + :param file_pointer: an open text stream pointing at a service.yaml. |
| 397 | + :return: a ``(service_config, overrides)`` tuple where ``service_config`` |
| 398 | + is the head document and ``overrides`` is the list of trailing |
| 399 | + documents. |
| 400 | + :raises ValueError: if the stream is empty, or if the head document is |
| 401 | + not a YAML mapping (e.g. a bare ``---``, a scalar, or a sequence). |
| 402 | + :raises yaml.YAMLError: if the stream contains malformed YAML and the underlying parser fails (propagated as-is so callers can handle parse errors and shape errors distinctly). # noqa: DAR402 |
| 403 | + """ |
| 404 | + configuration_data = yaml_load_all(file_pointer) |
| 405 | + if not configuration_data: |
| 406 | + raise ValueError("Service configuration file was empty.") |
| 407 | + service_config, *overrides = configuration_data |
| 408 | + # The head document must be a mapping. `yaml_load_all` happily returns |
| 409 | + # `[None]` / `[False]` / `[0]` / `[""]` / `[[]]` for malformed YAML |
| 410 | + # (bare `---`, a scalar, a sequence, etc.) — surface those at parse |
| 411 | + # time rather than as opaque AttributeErrors from the downstream |
| 412 | + # validator. Distinct from the empty-stream message so an operator |
| 413 | + # reading the traceback can tell the two cases apart. |
| 414 | + if not isinstance(service_config, dict): |
| 415 | + raise ValueError( |
| 416 | + "Service configuration head document must be a YAML mapping, " |
| 417 | + f"got {type(service_config).__name__}." |
| 418 | + ) |
| 419 | + return service_config, overrides |
| 420 | + |
| 421 | + |
387 | 422 | class ConfigLoaders: |
388 | 423 | """Configuration Loader class to load any package type.""" |
389 | 424 |
|
|
0 commit comments