Exemplo n.º 1
0
 def __module_by_class_or_class_name(self, class_name):
     if isinstance(class_name, str):
         try:
             module_name, class_name = class_name.rsplit('.', 1)
             module = __import__(module_name, globals(), locals(), [class_name], 0)
             clazz = getattr(module, class_name)
         except ImportError as e:
             raise InvalidModuleError("Class doesn't exist: " + str(class_name), e)
         except AttributeError as e:
             raise InvalidModuleError("Class doesn't exist: " + str(class_name), e)
     else:
         clazz = class_name
     # Validations
     assert clazz is not None, "module instance shouldn't be None"
     if not issubclass(clazz, Module):
         raise InvalidModuleError('Module must implement healthcheckbot.common.model.Module class')
     return clazz
Exemplo n.º 2
0
 def __bucket_for_module_instance(self, clazz: Type) -> ModuleType:
     if issubclass(clazz, TriggerModule):
         return ModuleType(self.__triggers, "trigger", TriggerModule)
     elif issubclass(clazz, OutputModule):
         return ModuleType(self.__outputs, "output", OutputModule)
     elif issubclass(clazz, WatcherModule):
         return ModuleType(self.__watchers, "watcher", WatcherModule)
     elif issubclass(clazz, WatcherAssert):
         return ModuleType(self.__watcher_asserts, "assert", WatcherAssert)
     else:
         raise InvalidModuleError("Unknown module type: " + clazz.__name__)
Exemplo n.º 3
0
 def register_module_instance(self, instance_name, class_name) -> Module:
     try:
         module_class = self.__module_by_class_or_class_name(class_name)
         module_type = self.__bucket_for_module_instance(module_class)
         module_inst = module_class(self)
         try:
             module_inst.on_initialized()
         except Exception as e:
             raise InvalidModuleError("Error during initialization", e)
         module_type.bucket[instance_name] = module_inst
         self.__logger.info(
             'Instantiated {} "{}"({})'.format(module_type.name, instance_name, module_class.__name__))
         if isinstance(module_inst, LoopModuleMixin):
             self.__main_loop.append(module_inst)
             self.__logger.info(
                 'Module "{}"({}) registered in application loop'.format(instance_name, module_class.__name__))
         module_inst.name = instance_name
         return module_inst
     except InvalidModuleError as e:
         raise InvalidModuleError('Unable to register module {}: '.format(class_name) + e.message, e)
Exemplo n.º 4
0
 def __bucket_for_module_instance(self, clazz: Type) -> ModuleType:
     if issubclass(clazz, TriggerModule):
         return ModuleType(self.__triggers, 'trigger', TriggerModule)
     elif issubclass(clazz, OutputModule):
         return ModuleType(self.__outputs, 'output', OutputModule)
     elif issubclass(clazz, WatcherModule):
         return ModuleType(self.__watchers, 'watcher', WatcherModule)
     elif issubclass(clazz, WatcherAssert):
         return ModuleType(self.__watcher_asserts, 'assert', WatcherAssert)
     else:
         raise InvalidModuleError('Unknown module type: ' + clazz.__name__)