aggregator.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Aggregation function for CLI specified options and config file options.
  2. This holds the logic that uses the collected and merged config files and
  3. applies the user-specified command-line configuration on top of it.
  4. """
  5. from __future__ import annotations
  6. import argparse
  7. import configparser
  8. import logging
  9. from typing import Sequence
  10. from flake8.options import config
  11. from flake8.options.manager import OptionManager
  12. LOG = logging.getLogger(__name__)
  13. def aggregate_options(
  14. manager: OptionManager,
  15. cfg: configparser.RawConfigParser,
  16. cfg_dir: str,
  17. argv: Sequence[str] | None,
  18. ) -> argparse.Namespace:
  19. """Aggregate and merge CLI and config file options."""
  20. # Get defaults from the option parser
  21. default_values = manager.parse_args([])
  22. # Get the parsed config
  23. parsed_config = config.parse_config(manager, cfg, cfg_dir)
  24. # store the plugin-set extended default ignore / select
  25. default_values.extended_default_ignore = manager.extended_default_ignore
  26. default_values.extended_default_select = manager.extended_default_select
  27. # Merge values parsed from config onto the default values returned
  28. for config_name, value in parsed_config.items():
  29. dest_name = config_name
  30. # If the config name is somehow different from the destination name,
  31. # fetch the destination name from our Option
  32. if not hasattr(default_values, config_name):
  33. dest_val = manager.config_options_dict[config_name].dest
  34. assert isinstance(dest_val, str)
  35. dest_name = dest_val
  36. LOG.debug(
  37. 'Overriding default value of (%s) for "%s" with (%s)',
  38. getattr(default_values, dest_name, None),
  39. dest_name,
  40. value,
  41. )
  42. # Override the default values with the config values
  43. setattr(default_values, dest_name, value)
  44. # Finally parse the command-line options
  45. return manager.parse_args(argv, default_values)