Process the given configuration into a key-value mapping. Returns a tuple of the config dict itself (or None on failure), and a list of log messages during parsing.
(parser: ConfigParser)
| 577 | |
| 578 | |
| 579 | def _parse_config(parser: ConfigParser) -> tuple[ConfigDict | None, list[LogMessage]]: |
| 580 | """Process the given configuration into a key-value mapping. Returns a tuple of the config |
| 581 | dict itself (or None on failure), and a list of log messages during parsing.""" |
| 582 | (success, logs) = _validate_structure(parser) |
| 583 | if not success: |
| 584 | return (None, logs) |
| 585 | config: ConfigDict = {} |
| 586 | success = True |
| 587 | # Re-map deprecated config sections to their replacements. Structure validation above should |
| 588 | # ensure no conflicts between the two. |
| 589 | for deprecated_command in DEPRECATED_COMMANDS: |
| 590 | if deprecated_command in parser: |
| 591 | replacement = DEPRECATED_COMMANDS[deprecated_command] |
| 592 | parser[replacement] = parser[deprecated_command] |
| 593 | del parser[deprecated_command] |
| 594 | for command in CONFIG_MAP: |
| 595 | config[command] = {} |
| 596 | for option in CONFIG_MAP[command]: |
| 597 | if command in parser and option in parser[command]: |
| 598 | # Bind to a local so pyright can narrow inside the isinstance branches. |
| 599 | default_value = CONFIG_MAP[command][option] |
| 600 | try: |
| 601 | value_type = None |
| 602 | if isinstance(default_value, bool): |
| 603 | value_type = "yes/no value" |
| 604 | config[command][option] = parser.getboolean(command, option) |
| 605 | continue |
| 606 | elif isinstance(default_value, int): |
| 607 | value_type = "integer" |
| 608 | config[command][option] = parser.getint(command, option) |
| 609 | continue |
| 610 | elif isinstance(default_value, float): |
| 611 | value_type = "number" |
| 612 | config[command][option] = parser.getfloat(command, option) |
| 613 | continue |
| 614 | elif isinstance(default_value, Enum): |
| 615 | config_value = ( |
| 616 | parser.get(command, option).replace("\n", " ").strip().upper() |
| 617 | ) |
| 618 | try: |
| 619 | parsed = default_value.__class__[config_value] |
| 620 | config[command][option] = parsed |
| 621 | except TypeError: |
| 622 | success = False |
| 623 | logs.append( |
| 624 | ( |
| 625 | logging.ERROR, |
| 626 | "Invalid value for [{}] option {}': {}. Must be one of: {}.".format( |
| 627 | command, |
| 628 | option, |
| 629 | parser.get(command, option), |
| 630 | ", ".join( |
| 631 | str(choice) for choice in CHOICE_MAP[command][option] |
| 632 | ), |
| 633 | ), |
| 634 | ) |
| 635 | ) |
| 636 | continue |
no test coverage detected