Beispiel #1
0
def run_application(config: dict, application: ApplicationManager = None):
    if application is None:
        # Do bootstrap
        application = bootstrap(config)
    try:
        logger.info("Started main application loop")
        application.main_loop()
    except KeyboardInterrupt:
        application.shutdown()
        logger.info("Bye")
        exit()
Beispiel #2
0
def instantiate_modules_for_section(
    section_name: str,
    section_config: dict,
    application: ApplicationManager,
    cb: Callable[[ApplicationManager, Module, dict], None] = None,
    instance_name_prefix="",
) -> List[Module]:
    result = []
    for name, module_def in section_config.items():
        if not (isinstance(module_def, dict) and "provider" in module_def):
            raise ConfigValidationError(
                "/".join((section_name, name)),
                'Should be dictionary containing mandatory key "provider"',
            )
        module_def = add_expression_evaluator(module_def)
        instance = application.register_module_instance(
            instance_name_prefix + name, module_def.get("provider"))
        try:
            # Parse and set parameters
            for param_def in instance.PARAMS:
                if param_def.name in module_def:
                    val = module_def.get(param_def.name)
                    if param_def.parser is not None:
                        val = param_def.parser.parse(
                            val,
                            application,
                            "/".join((section_name, name, param_def.name)),
                        )
                    val = param_def.sanitize(val)
                    param_def.validate(val)
                    setattr(instance, param_def.name, val)
                elif param_def.is_required:
                    raise ConfigValidationError(
                        "/".join((section_name, name, param_def.name)),
                        "Parameter {} is required".format(param_def.name),
                    )
            # Run validation of the module device
            instance.validate()
            if cb is not None:
                cb(application, instance, module_def)
            instance.on_configured()
            logger.info("Configured module {}({}))".format(
                name, instance.__class__.__name__))
            result.append(instance)
        except Exception as e:
            raise ConfigValidationError(
                "/".join((section_name, name)),
                "Invalid module configuration: " + str(e),
                e,
            )
    return result
Beispiel #3
0
def _register_watcher_module(application: ApplicationManager,
                             watcher: WatcherModule, module_def: dict):
    for trigger_name in module_def.get('triggers', tuple()):
        trigger = application.get_trigger_by_name(trigger_name)
        if trigger is None:
            raise ConfigValidationError(
                'triggers',
                "Trigger \'{}\' doesn't exist".format(trigger_name))
        trigger.register_watcher(watcher)
    assertions = instantiate_modules_for_section(
        'watchers/{}/custom_assertions'.format(watcher.name),
        module_def.get('custom_assertions', dict()),
        application,
        instance_name_prefix=watcher.name + '__')
    watcher.custom_assertions = assertions
Beispiel #4
0
def save_instance_config(config: dict, application: ApplicationManager):
    if 'app' in config:
        app_config = add_expression_evaluator(config.get('app', {}))
        settings = application.get_instance_settings()
        settings.id = app_config.get('id', 'healthcheckbot0')
        # Context Path
        for path in app_config.get('classpath', []):
            if isinstance(path, str):
                if not os.path.exists(path):
                    raise ConfigValidationError(
                        'app/classpath', 'path ' + path + ' doesn\'t exist')
                settings.context_path.append(path)
            else:
                raise ConfigValidationError(
                    'app/classpath',
                    'Context path must be the list of strings')
Beispiel #5
0
def save_instance_config(config: dict, application: ApplicationManager):
    if "app" in config:
        app_config = add_expression_evaluator(config.get("app", {}))
        settings = application.get_instance_settings()
        settings.id = app_config.get("id", "healthcheckbot0")
        # Context Path
        for path in app_config.get("classpath", []):
            if isinstance(path, str):
                if not os.path.exists(path):
                    raise ConfigValidationError(
                        "app/classpath", "path " + path + " doesn't exist")
                settings.context_path.append(path)
            else:
                raise ConfigValidationError(
                    "app/classpath",
                    "Context path must be the list of strings")
Beispiel #6
0
def load_context_path(application: ApplicationManager):
    context_path = application.get_instance_settings().context_path
    for p in context_path:
        sys.path.append(p)
Beispiel #7
0
def new_application(enable_cli=False):
    application = ApplicationManager()
    application.get_instance_settings().enable_cli = enable_cli
    return application