Skip to content

Commit

Permalink
config domains: add missing types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mo-ki committed Jan 16, 2025
1 parent 1f4de3b commit c82d3be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
14 changes: 8 additions & 6 deletions cmk/gui/wato/_omd_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 21 additions & 10 deletions cmk/gui/watolib/config_domain_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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())
Expand Down
3 changes: 1 addition & 2 deletions cmk/gui/watolib/config_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion cmk/gui/watolib/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit c82d3be

Please sign in to comment.