From f84f2aff15f00ff701730d34746f0c49639a5f1c Mon Sep 17 00:00:00 2001 From: Mathias Laurin Date: Wed, 25 Oct 2023 12:46:47 +0200 Subject: [PATCH] rulesets as sequences instead of iterables We will need to iterate over the rulesets twice at least in order to fix the cache ID. So let's type the structure as a `Sequence`. CMK-14467 Change-Id: Iecdf6616c7f1bc2da63deffc4063209a37dc2c92 --- cmk/base/default_config/base.py | 2 +- cmk/utils/rulesets/ruleset_matcher.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmk/base/default_config/base.py b/cmk/base/default_config/base.py index 412b7694fc2..2ae714ad6a8 100644 --- a/cmk/base/default_config/base.py +++ b/cmk/base/default_config/base.py @@ -250,7 +250,7 @@ class _PeriodicDiscovery(TypedDict): clusters: dict[HostName, list[HostName]] = {} clustered_services: list[RuleSpec[object]] = [] # new in 1.1.4 -clustered_services_of: dict[HostAddress, Iterable[RuleSpec[object]]] = {} +clustered_services_of: dict[HostAddress, Sequence[RuleSpec[object]]] = {} # new for 1.2.5i1 Wato Rule clustered_services_mapping: list[RuleSpec[HostAddress]] = [] clustered_services_configuration: list[ diff --git a/cmk/utils/rulesets/ruleset_matcher.py b/cmk/utils/rulesets/ruleset_matcher.py index 475c5ef56d3..e8f3b78fa0e 100644 --- a/cmk/utils/rulesets/ruleset_matcher.py +++ b/cmk/utils/rulesets/ruleset_matcher.py @@ -171,7 +171,7 @@ def __init__( tuple[HostName, ServiceName, Item | None], RulesetMatchObject ] = {} - def get_host_bool_value(self, hostname: HostName, ruleset: Iterable[RuleSpec[bool]]) -> bool: + def get_host_bool_value(self, hostname: HostName, ruleset: Sequence[RuleSpec[bool]]) -> bool: """Compute outcome of a ruleset set that just says yes/no The binary match only cares about the first matching rule of an object. @@ -189,7 +189,7 @@ def get_host_bool_value(self, hostname: HostName, ruleset: Iterable[RuleSpec[boo def get_host_merged_dict( self, hostname: HostName, - ruleset: Iterable[RuleSpec[Mapping[str, TRuleValue]]], + ruleset: Sequence[RuleSpec[Mapping[str, TRuleValue]]], ) -> Mapping[str, TRuleValue]: """Returns a dictionary of the merged dict values of the matched rules The first dict setting a key defines the final value. @@ -203,7 +203,7 @@ def get_host_merged_dict( def get_host_values( self, hostname: HostName | HostAddress, - ruleset: Iterable[RuleSpec[TRuleValue]], + ruleset: Sequence[RuleSpec[TRuleValue]], ) -> Sequence[TRuleValue]: """Returns a generator of the values of the matched rules.""" @@ -258,7 +258,7 @@ def _checkgroup_match_object( ) def get_service_bool_value( - self, hostname: HostName, description: ServiceName, ruleset: Iterable[RuleSpec[TRuleValue]] + self, hostname: HostName, description: ServiceName, ruleset: Sequence[RuleSpec[TRuleValue]] ) -> bool: """Compute outcome of a ruleset set that just says yes/no @@ -278,7 +278,7 @@ def get_service_merged_dict( self, hostname: HostName, description: ServiceName, - ruleset: Iterable[RuleSpec[Mapping[str, TRuleValue]]], + ruleset: Sequence[RuleSpec[Mapping[str, TRuleValue]]], ) -> Mapping[str, TRuleValue]: """Returns a dictionary of the merged dict values of the matched rules The first dict setting a key defines the final value. @@ -295,7 +295,7 @@ def get_service_merged_dict( return merged def service_extra_conf( - self, hostname: HostName, description: ServiceName, ruleset: Iterable[RuleSpec[TRuleValue]] + self, hostname: HostName, description: ServiceName, ruleset: Sequence[RuleSpec[TRuleValue]] ) -> list[TRuleValue]: """Compute outcome of a service rule set that has an item.""" return list( @@ -309,7 +309,7 @@ def get_checkgroup_ruleset_values( hostname: HostName, description: ServiceName, item: Item, - ruleset: Iterable[RuleSpec[TRuleValue]], + ruleset: Sequence[RuleSpec[TRuleValue]], ) -> list[TRuleValue]: return list( self.get_service_ruleset_values( @@ -320,7 +320,7 @@ def get_checkgroup_ruleset_values( def get_service_ruleset_values( self, match_object: RulesetMatchObject, - ruleset: Iterable[RuleSpec[TRuleValue]], + ruleset: Sequence[RuleSpec[TRuleValue]], ) -> Iterator[TRuleValue]: """Returns a generator of the values of the matched rules""" with_foreign_hosts = ( @@ -472,7 +472,7 @@ def set_all_processed_hosts(self, all_processed_hosts: Iterable[HostName]) -> No ) def get_host_ruleset( - self, ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool + self, ruleset: Sequence[RuleSpec[TRuleValue]], with_foreign_hosts: bool ) -> Mapping[HostAddress, Sequence[TRuleValue]]: def _impl( ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool @@ -494,7 +494,7 @@ def _impl( return self.__host_ruleset_cache.setdefault(cache_id, _impl(ruleset, with_foreign_hosts)) def get_service_ruleset( - self, ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool + self, ruleset: Sequence[RuleSpec[TRuleValue]], with_foreign_hosts: bool ) -> PreprocessedServiceRuleset[TRuleValue]: def _impl( ruleset: Iterable[RuleSpec[TRuleValue]], with_foreign_hosts: bool