From c82d3be199e5c6c8c75a7bb2067fbd4ef68791ac Mon Sep 17 00:00:00 2001 From: Moritz Kiemer Date: Wed, 15 Jan 2025 15:18:24 +0100 Subject: [PATCH] config domains: add missing types The `GlobalSettings` type is not too useful, but add it anyway. More importantly, make it clear that `config_dir` and `config_file` are meant to return `str`, not `Path`. Change-Id: I584b4f17956441f84ebc82f0d49b8a67e92f2728 --- cmk/gui/wato/_omd_configuration.py | 14 ++++++------ cmk/gui/watolib/config_domain_name.py | 31 ++++++++++++++++++--------- cmk/gui/watolib/config_domains.py | 3 +-- cmk/gui/watolib/global_settings.py | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cmk/gui/wato/_omd_configuration.py b/cmk/gui/wato/_omd_configuration.py index 25e309297c7..e6b1c89717e 100644 --- a/cmk/gui/wato/_omd_configuration.py +++ b/cmk/gui/wato/_omd_configuration.py @@ -410,9 +410,10 @@ def _write_config_file(self): store.save_text_to_file(config_file_path, output) def get_effective_config(self): - config = self.load(site_specific=False) - config.update(self.load(site_specific=True)) - return config + return { + **self.load(site_specific=False), + **self.load(site_specific=True), + } def default_globals(self) -> Mapping[str, Any]: return { @@ -530,9 +531,10 @@ def _write_config_file(self): store.save_text_to_file(config_file_path, output) def _get_effective_config(self): - config = self.load(site_specific=False) - config.update(self.load(site_specific=True)) - return config + return { + **self.load(site_specific=False), + **self.load(site_specific=True), + } def default_globals(self) -> Mapping[str, Any]: return { diff --git a/cmk/gui/watolib/config_domain_name.py b/cmk/gui/watolib/config_domain_name.py index db44b298ca3..5f1e0b81011 100644 --- a/cmk/gui/watolib/config_domain_name.py +++ b/cmk/gui/watolib/config_domain_name.py @@ -85,19 +85,21 @@ def enabled(cls) -> bool: return True @classmethod - def get_all_default_globals(cls) -> Mapping[str, Any]: + def get_all_default_globals(cls) -> GlobalSettings: return _get_all_default_globals() @abc.abstractmethod - def config_dir(self): + def config_dir(self) -> str: raise NotImplementedError() - def config_file(self, site_specific): + def config_file(self, site_specific: bool) -> str: if site_specific: return os.path.join(self.config_dir(), "sitespecific.mk") return os.path.join(self.config_dir(), "global.mk") - def load_full_config(self, site_specific=False, custom_site_path=None): + def load_full_config( + self, site_specific: bool = False, custom_site_path: str | None = None + ) -> GlobalSettings: filename = Path(self.config_file(site_specific)) if custom_site_path: filename = Path(custom_site_path) / filename.relative_to(cmk.utils.paths.omd_root) @@ -116,13 +118,20 @@ def load_full_config(self, site_specific=False, custom_site_path=None): except Exception as e: raise MKGeneralException(_("Cannot read configuration file %s: %s") % (filename, e)) - def load(self, site_specific=False, custom_site_path=None): + def load( + self, site_specific: bool = False, custom_site_path: str | None = None + ) -> GlobalSettings: return filter_unknown_settings(self.load_full_config(site_specific, custom_site_path)) - def load_site_globals(self, custom_site_path=None): + def load_site_globals(self, custom_site_path: str | None = None) -> GlobalSettings: return self.load(site_specific=True, custom_site_path=custom_site_path) - def save(self, settings, site_specific=False, custom_site_path=None): + def save( + self, + settings: GlobalSettings, + site_specific: bool = False, + custom_site_path: str | None = None, + ) -> None: filename = self.config_file(site_specific) if custom_site_path: filename = os.path.join( @@ -136,11 +145,13 @@ def save(self, settings, site_specific=False, custom_site_path=None): store.makedirs(os.path.dirname(filename)) store.save_text_to_file(filename, output) - def save_site_globals(self, settings, custom_site_path=None): + def save_site_globals( + self, settings: GlobalSettings, custom_site_path: str | None = None + ) -> None: self.save(settings, site_specific=True, custom_site_path=custom_site_path) @abc.abstractmethod - def default_globals(self) -> Mapping[str, Any]: + def default_globals(self) -> GlobalSettings: """Returns a dictionary that contains the default settings of all configuration variables of this config domain.""" raise NotImplementedError() @@ -164,7 +175,7 @@ def get_domain_request(cls, settings: list[SerializedSettings]) -> DomainRequest @request_memoize() -def _get_all_default_globals() -> dict[str, Any]: +def _get_all_default_globals() -> GlobalSettings: settings: dict[str, Any] = {} for domain in ABCConfigDomain.enabled_domains(): settings.update(domain.default_globals()) diff --git a/cmk/gui/watolib/config_domains.py b/cmk/gui/watolib/config_domains.py index ee6b97a0251..a237419bac6 100644 --- a/cmk/gui/watolib/config_domains.py +++ b/cmk/gui/watolib/config_domains.py @@ -146,8 +146,7 @@ def activate(self, settings: SerializedSettings | None = None) -> ConfigurationW f"Resetting the default language '{get_language_alias(dflt_lang)}' to 'English' due to " "globally disabled commmunity translations (Global settings > User interface)." ) - gui_config = self.load() - gui_config.pop("default_language", None) + gui_config = {k: v for k, v in self.load().items() if k != "default_language"} self.save(gui_config) active_config.default_language = "en" diff --git a/cmk/gui/watolib/global_settings.py b/cmk/gui/watolib/global_settings.py index 1eb0ff722c0..4cd0a2cc62f 100644 --- a/cmk/gui/watolib/global_settings.py +++ b/cmk/gui/watolib/global_settings.py @@ -23,7 +23,7 @@ def load_configuration_settings( site_specific: bool = False, custom_site_path: str | None = None, full_config: bool = False ) -> GlobalSettings: - settings = {} + settings: dict[str, Any] = {} for domain in ABCConfigDomain.enabled_domains(): if full_config: settings.update(domain.load_full_config())