def check_definition(self, config_definitions: Dict[str, Dict[str, Any]], name: str, **_): """ Checks for individual definitions. """ definition = config_definitions[name] if not isinstance(definition, dict): return checks.Critical( "Config definition entry is not a dict.", hint="Change definition to be a dict in settings.", obj=f"settings.{self.setting}[{name}]", ) messages = [] # Check required definitions exist for key in self.required_keys: if key not in definition: messages.append( checks.Critical( f"Config definition entry does not contain `{key}` value.", obj=f"settings.{self.setting}[{name}]", )) # Check for un-known values if self._args: for key in definition: if key not in self._args: messages.append( checks.Warn( f"Config definition entry contains unknown value `{key}`.", obj=f"settings.{self.setting}[{name}][{key}]", )) return messages
def check_instance(self, idx: int, provider_ref: str, **kwargs): message = super().check_instance(idx, provider_ref, **kwargs) if message: return message if self.multi_messages: return checks.Info("foo"), checks.Warn("bar")
def checks(self, **kwargs): """ Run checks to ensure settings are valid, secondly run checks against individual definitions in settings. """ settings_ = kwargs['settings'] # Check settings are defined if not hasattr(settings_, self.setting): return checks.Critical( "Instance definitions missing from settings.", hint="Add a {} entry into settings.".format(self.setting), obj="settings.{}".format(self.setting)) instance_definitions = getattr(settings_, self.setting) if instance_definitions is None: return # Nothing is defined so end now. if not isinstance(instance_definitions, dict): return checks.Critical( "Instance definitions defined in settings not a dict instance.", hint="Change setting {} to be a dict in settings file.".format( self.setting), obj="settings.{}".format(self.setting)) # Take over the lock while checks are being performed with self._type_definitions_lock: # Backup definitions, replace and clear cache, this is to make it # easier to write checks for instances. instance_definitions_orig = self._instance_definitions try: self._instance_definitions = instance_definitions self._type_definitions.clear() messages = [] # Check default is defined if self.default_name not in instance_definitions: messages.append( checks.Warn( "Default definition not defined.", hint="The default instance type `{}` is not defined." .format(self.default_name), obj="settings.{}".format(self.setting))) # Check instance definitions for name in instance_definitions: message = self.check_instance(instance_definitions, name, **kwargs) if isinstance(message, checks.CheckMessage): messages.append(message) elif message: messages += message return messages finally: # Put definitions back and clear cache. self._instance_definitions = instance_definitions_orig self._type_definitions.clear()
def double_check(**_): return ( checks.Warn("Warn message", "Remove warning messages", obj="App"), checks.Info( "Info message", ["Just a tip really message.", "This is also a multi-paragraph hint as an example of what can be done."], obj="App" ), )
def test_checks__multiple_instance_messages(self): target = ProviderFactoryTest(multi_messages=True) with settings.modify() as ctx: ctx.TEST_PROVIDERS = ("tests.conf.helpers.test_providers.ProviderBaseTest",) actual = target.checks(settings=settings) assert actual == [checks.Info("foo"), checks.Warn("bar")]
def checks(self, **kwargs): """ Run checks to ensure settings are valid, secondly run checks against individual definitions in settings. """ settings_ = kwargs["settings"] # Check settings are defined if not hasattr(settings_, self.setting): return checks.Critical( "Config definitions missing from settings.", hint=f"Add a {self.setting} entry into settings.", obj=f"settings.{self.setting}", ) config_definitions = getattr(settings_, self.setting) if config_definitions is None: return None # Nothing is defined so end now. if not isinstance(config_definitions, dict): return checks.Critical( "Config definitions defined in settings not a dict instance.", hint= f"Change setting {self.setting} to be a dict in settings file.", obj=f"settings.{self.setting}", ) messages = [] # Check default is defined if self.default_name not in config_definitions: messages.append( checks.Warn( "Default definition not defined.", hint=f"Add a `{self.default_name}` entry.", obj=f"settings.{self.setting}", )) # Check instance definitions for name in config_definitions: message = self.check_definition(config_definitions, name, **kwargs) if isinstance(message, checks.CheckMessage): messages.append(message) elif message: messages += message return messages
def check_definition(self, config_definitions, name, **_): """ Checks for individual definitions. :param config_definitions: :param name: :return: """ definition = config_definitions[name] if not isinstance(definition, dict): return checks.Critical( "Config definition entry is not a dict.", hint="Change definition to be a dict in settings.", obj='settings.{}[{}]'.format(self.setting, name) ) messages = [] # Check required definitions exist for key in self.required_keys: if key not in definition: messages.append(checks.Critical( "Config definition entry does not contain `{}` value.".format(key), obj='settings.{}[{}]'.format(self.setting, name) )) # Check for un-known values if self._args: for key in definition: if key not in self._args: messages.append(checks.Warn( "Config definition entry contains unknown value `{}`.".format(key), obj='settings.{}[{}][{}]'.format(self.setting, name, key) )) return messages
def check_instance(self, instance_definitions, name, **_): """ Checks for individual instances. """ definition = instance_definitions[name] if not isinstance(definition, (tuple, list)): return checks.Critical( "Instance definition is not a list/tuple.", hint= "Change definition to be a list/tuple (type_name, kwargs) in settings.", obj=f"settings.{self.setting}[{name}]", ) if len(definition) != 2: return checks.Critical( "Instance definition is not a type name, kwarg (dict) pair.", hint= "Change definition to be a list/tuple (type_name, kwargs) in settings.", obj=f"settings.{self.setting}[{name}]", ) type_name, kwargs = definition messages = [] if type_name.upper() == "ALIAS": target_name = kwargs.get("name") if not target_name: messages.append( checks.Critical( "Name of alias target not defined", hint= "An alias entry must provide a `name` value that refers to another entry.", obj=f"settings.{self.setting}[{name}]", )) elif target_name not in instance_definitions: messages.append( checks.Critical( "Alias target not defined", hint= "The target specified by the alias does not exist, check the `name` value.", obj=f"settings.{self.setting}[{name}][{target_name}]", )) if len(kwargs) > 1: messages.append( checks.Warn( "Alias contains unknown arguments", hint="An alias entry must only provide a `name` value.", obj=f"settings.{self.setting}[{name}]", )) else: try: import_type(type_name) except (ImportError, ValueError, AttributeError): messages.append( checks.Error( f"Unable to import type `{type_name}`.", hint="Check the type name in definition.", obj=f"settings.{self.setting}[{name}]", )) if not isinstance(kwargs, dict): messages.append( checks.Critical( "Instance kwargs is not a dict.", hint="Change kwargs definition to be a dict.", obj=f"settings.{self.setting}[{name}]", )) return messages