Exemplo n.º 1
0
    def verify_configs(self, global_config: Config) -> None:
        """Verify all loaded configs have correct scopes and options."""

        error_log = []
        for config in global_config.configs():
            for section in config.sections():
                scope = GLOBAL_SCOPE if section == GLOBAL_SCOPE_CONFIG_SECTION else section
                try:
                    # TODO(#10834): this is broken for subscopes. Once we fix global options to no
                    #  longer be included in self.for_scope(), we should set
                    #  inherit_from_enclosing_scope=True.
                    valid_options_under_scope = set(
                        self.for_scope(scope,
                                       inherit_from_enclosing_scope=False))
                # Only catch ConfigValidationError. Other exceptions will be raised directly.
                except Config.ConfigValidationError:
                    error_log.append(
                        f"Invalid scope [{section}] in {config.config_path}")
                else:
                    # All the options specified under [`section`] in `config` excluding bootstrap defaults.
                    all_options_under_scope = set(
                        config.values.options(section)) - set(
                            config.values.defaults)
                    for option in sorted(all_options_under_scope):
                        if option not in valid_options_under_scope:
                            error_log.append(
                                f"Invalid option '{option}' under [{section}] in {config.config_path}"
                            )

        if error_log:
            for error in error_log:
                logger.error(error)
            raise Config.ConfigValidationError(
                "Invalid config entries detected. See log for details on which entries to update or "
                "remove.\n(Specify --no-verify-config to disable this check.)")
Exemplo n.º 2
0
    def verify_configs_against_options(self, options: Options) -> None:
        """Verify all loaded configs have correct scopes and options.

        :param options: Fully bootstrapped valid options.
        """
        error_log = []
        for config in self.config.configs():
            for section in config.sections():
                scope = GLOBAL_SCOPE if section == GLOBAL_SCOPE_CONFIG_SECTION else section
                try:
                    valid_options_under_scope = set(
                        options.for_scope(scope, include_passive_options=True))
                # Only catch ConfigValidationError. Other exceptions will be raised directly.
                except Config.ConfigValidationError:
                    error_log.append(
                        f"Invalid scope [{section}] in {config.config_path}")
                else:
                    # All the options specified under [`section`] in `config` excluding bootstrap defaults.
                    all_options_under_scope = set(
                        config.values.options(section)) - set(
                            config.values.defaults)
                    for option in sorted(all_options_under_scope):
                        if option not in valid_options_under_scope:
                            error_log.append(
                                f"Invalid option '{option}' under [{section}] in {config.config_path}"
                            )

        if error_log:
            for error in error_log:
                logger.error(error)
            raise Config.ConfigValidationError(
                "Invalid config entries detected. See log for details on which entries to update or "
                "remove.\n(Specify --no-verify-config to disable this check.)")
Exemplo n.º 3
0
 def get_parser(self, scope: str) -> Parser:
     """Returns the parser for the given scope, so code can register on it directly."""
     try:
         return self._parser_by_scope[scope]
     except KeyError:
         raise Config.ConfigValidationError(
             f"No such options scope: {scope}")
Exemplo n.º 4
0
  def verify_configs_against_options(self, options):
    """Verify all loaded configs have correct scopes and options.

    :param options: Fully bootstrapped valid options.
    :return: None.
    """
    error_log = []
    for config in self.config.configs():
      for section in config.sections():
        if section == GLOBAL_SCOPE_CONFIG_SECTION:
          scope = GLOBAL_SCOPE
        else:
          scope = section
        try:
          valid_options_under_scope = set(options.for_scope(scope))
        # Only catch ConfigValidationError. Other exceptions will be raised directly.
        except Config.ConfigValidationError:
          error_log.append("Invalid scope [{}] in {}".format(section, config.configpath))
        else:
          # All the options specified under [`section`] in `config` excluding bootstrap defaults.
          all_options_under_scope = (set(config.configparser.options(section)) -
                                     set(config.configparser.defaults()))
          for option in all_options_under_scope:
            if option not in valid_options_under_scope:
              error_log.append("Invalid option '{}' under [{}] in {}".format(option, section, config.configpath))

    if error_log:
      for error in error_log:
        logger.error(error)
      raise Config.ConfigValidationError("Invalid config entries detected. "
                              "See log for details on which entries to update or remove.\n"
                              "(Specify --no-verify-config to disable this check.)")
Exemplo n.º 5
0
 def get_parser_by_scope(self, scope):
   try:
     return self._parser_by_scope[scope]
   except KeyError:
     raise Config.ConfigValidationError('No such options scope: {}'.format(scope))
Exemplo n.º 6
0
 def get_parser_by_scope(self, scope: str) -> Parser:
     try:
         return self._parser_by_scope[scope]
     except KeyError:
         raise Config.ConfigValidationError(
             f"No such options scope: {scope}")