[docs]classCliContextObj:def__init__(self,ctx:click.Context,logger:logging.Logger,global_opts:GlobalCommandLineOptions,)->None:self.ctx=ctxself.logger=loggerself.global_opts=global_optsself._raw_config:RawConfig|None=Noneself._runtime_ctx:RuntimeContext|None=None@propertydefraw_config(self)->RawConfig:ifself._raw_configisNone:self._raw_config=self._init_raw_config()returnself._raw_config@propertydefruntime_ctx(self)->RuntimeContext:""" Lazy load the runtime context. This is done to avoid configuration loading when the command is not run. This is useful for commands like `--help` and `--version` """ifself._runtime_ctxisNone:self._runtime_ctx=self._init_runtime_ctx()returnself._runtime_ctxdef_init_raw_config(self)->RawConfig:config_path=Path(self.global_opts.config_file)conf_file_exists=config_path.exists()was_conf_file_user_provided=bool(self.ctx.get_parameter_source("config_file")notin(ParameterSource.DEFAULT,ParameterSource.DEFAULT_MAP,))# TODO: Evaluate Exeception catchestry:ifwas_conf_file_user_providedandnotconf_file_exists:raiseFileNotFoundError(# noqa: TRY301f"File {self.global_opts.config_file} does not exist")config_obj=({}ifnotconf_file_existselseload_raw_config_file(config_path))ifnotconfig_obj:self.logger.info("configuration empty, falling back to default configuration")returnRawConfig.model_validate(config_obj)exceptFileNotFoundErrorasexc:click.echo(str(exc),err=True)self.ctx.exit(2)except(ValidationError,InvalidConfiguration,InvalidGitRepositoryError,)asexc:click.echo(str(exc),err=True)self.ctx.exit(1)def_init_runtime_ctx(self)->RuntimeContext:# TODO: Evaluate Exception catchestry:runtime=RuntimeContext.from_raw_config(self.raw_config,global_cli_options=self.global_opts,)exceptNotAReleaseBranchasexc:rprint(f"[bold {'red'ifself.global_opts.strictelse'orange1'}]{exc!s}")# If not strict, exit 0 so other processes can continue. For example, in# multibranch CI it might be desirable to run a non-release branch's pipeline# without specifying conditional execution of PSR based on branch nameself.ctx.exit(2ifself.global_opts.strictelse0)except(DetachedHeadGitError,InvalidConfiguration,InvalidGitRepositoryError,ValidationError,)asexc:click.echo(str(exc),err=True)self.ctx.exit(1)# This allows us to mask secrets in the logging# by applying it to all the configured handlersforhandlerinlogging.getLogger().handlers:handler.addFilter(runtime.masker)returnruntime