Esempio n. 1
0
    def request_file_load(
            self,
            event: HABApp.core.events.habapp_events.RequestFileLoadEvent):

        path = event.get_path(HABApp.CONFIG.directories.rules)
        path_str = str(path)

        # Only load existing files
        if not path.is_file():
            log.warning(
                f'Rule file {path} does not exist and can not be loaded!')
            return None

        with self.__load_lock:
            # Unload if we have already loaded
            with self.__files_lock:
                already_loaded = path_str in self.files
            if already_loaded:
                self.request_file_unload(event, request_lock=False)

            log.debug(f'Loading file: {path}')
            with self.__files_lock:
                self.files[path_str] = file = RuleFile(self, path)

            if not file.load():
                # If the load has failed we remove it again.
                # Unloading is handled directly in the load function
                self.files.pop(path_str)
                log.warning(f'Failed to load {path_str}!')
                return None

        log.debug(f'File {path_str} successfully loaded!')

        # Do simple checks which prevent errors
        file.check_all_rules()
Esempio n. 2
0
    def filter_func_load(
            event: HABApp.core.events.habapp_events.RequestFileLoadEvent):
        if not func(event.name):
            return None

        name = event.name
        path = event.get_path()

        # Only load existing files
        if not path.is_file():
            log_error(
                logger,
                f'{file_type} file "{path}" does not exist and can not be loaded!'
            )
            return None

        func_load(name, path)
Esempio n. 3
0
def load_file(event: HABApp.core.events.habapp_events.RequestFileLoadEvent):
    path = event.get_path(CONFIG.directories.param)

    with LOCK:  # serialize to get proper error messages
        try:
            with path.open(mode='r', encoding='utf-8') as file:
                data = _yml_setup.load(file)
            if data is None:
                data = {}
            set_parameter_file(path.stem, data)
        except Exception:
            log.error(f"Could not load params from {path.name}!")
            for l in traceback.format_exc().splitlines():
                log.error(l)
            return None

        log.debug(f'Loaded params from {path.name}!')
Esempio n. 4
0
def load_file(event: HABApp.core.events.habapp_events.RequestFileLoadEvent):
    path = event.get_path(HABApp.CONFIG.directories.param)

    with LOCK:  # serialize to get proper error messages
        try:
            with path.open(mode='r', encoding='utf-8') as file:
                data = _yml_setup.load(file)
            if data is None:
                data = {}
            set_parameter_file(path.stem, data)
        except Exception as exc:
            e = HABApp.core.logger.HABAppError(log)
            e.add(f"Could not load params from {path.name}!")
            e.add_exception(exc, add_traceback=True)
            e.dump()
            return None

        log.debug(f'Loaded params from {path.name}!')
Esempio n. 5
0
    def request_file_load(
            self,
            event: HABApp.core.events.habapp_events.RequestFileLoadEvent):

        path = event.get_path(HABApp.CONFIG.directories.rules)
        path_str = str(path)

        # Only load existing files
        if not path.is_file():
            log.warning(
                f'Rule file {path} does not exist and can not be loaded!')
            return None

        try:
            # serialize loading
            self.__load_lock.acquire()

            # Unload if we have already loaded
            with self.__files_lock:
                already_loaded = path_str in self.files
            if already_loaded:
                self.request_file_unload(event, request_lock=False)

            log.debug(f'Loading file: {path}')
            with self.__files_lock:
                self.files[path_str] = file = RuleFile(self, path)
            file.load()
        except Exception:
            log.error(f"Could not (fully) load {path}!")
            for l in traceback.format_exc().splitlines():
                log.error(l)
            return None
        finally:
            self.__load_lock.release()

        log.debug(f'File {path_str} successfully loaded!')

        # Do simple checks which prevent errors
        file.check_all_rules()
Esempio n. 6
0
 async def file_load_event(self, event: HABApp.core.events.habapp_events.RequestFileLoadEvent):
     if HABApp.core.files.file_name.is_config(event.name):
         await self.update_thing_config(event.get_path())