def check_instance(self, idx, provider_ref, **_): """ Checks for individual providers. """ if not isinstance(provider_ref, six.string_types): return checks.Critical( "Provider definition is not a string.", hint="Change definition to be a string in settings.", obj='settings.{}[{}]'.format(self.setting, idx)) try: import_type(provider_ref) except ImportError as ex: return checks.Critical("Unable to import provider type.", hint=str(ex), obj='settings.{}[{}]'.format( self.setting, idx))
def providers(self) -> Dict[str, Any]: """ List of providers defined in settings. """ providers = OrderedDict() # Load and instantiate available providers provider_refs = getattr(settings, self.setting) for provider_ref in provider_refs: providers[provider_ref] = import_type(provider_ref) return providers
def _get_type_definition(self, name): try: type_name, kwargs = self._instance_definitions[name] except KeyError: raise KeyError("Setting definition `{}` not found".format(name)) type_ = import_type(type_name) if self.abc and not issubclass(type_, self.abc): raise TypeError( "Setting definition `{}` is not a subclass of `{}`".format( type_name, self.abc)) return type_, kwargs
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='settings.{}[{}]'.format(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='settings.{}[{}]'.format(self.setting, name)) type_name, kwargs = definition messages = [] try: import_type(type_name) except (ImportError, ValueError): messages.append( checks.Error("Unable to import type `{}`.".format(type_name), hint="Check the type name in definition.", obj='settings.{}[{}]'.format(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='settings.{}[{}]'.format( self.setting, name))) return messages
def check_instance( self, idx: int, provider_ref: str, **_ ) -> Optional[Union["checks.CheckMessage", Sequence["checks.CheckMessage"]]]: """ Checks for individual providers. """ if not isinstance(provider_ref, str): return checks.Critical( "Provider definition is not a string.", hint="Change definition to be a string in settings.", obj=f"settings.{self.setting}[{idx}]", ) try: import_type(provider_ref) except ImportError as ex: return checks.Critical( "Unable to import provider type.", hint=str(ex), obj=f"settings.{self.setting}[{idx}]", ) return None
def _get_type_definition(self, name: str): type_name, kwargs = self._resolve_instance_definition(name) try: type_ = import_type(type_name) except (ImportError, AttributeError) as ex: raise CannotImport( f"Cannot import type `{type_name}` from config '{name}'." ) from ex if self.abc and not issubclass(type_, self.abc): raise InvalidSubType( f"Setting definition `{type_name}` is not a subclass of `{self.abc}`" ) return type_, kwargs
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