示例#1
0
    def __check_permissions(self):
        """
        Check permissions on directories before performing the operations

        :raise ValueError if input directory is equal to output directory
        :raise NotADirectoryError
        :raise PermissionError
        :raise LinksError
        """
        sources = self.__dispatcher_config.dispatcher_sources
        destinations = self.__dispatcher_config.dispatcher_destinations

        for source in sources:
            Validation.is_dir(
                sources[source],
                f"Missing input directory '{sources[source]}'"
            )
            Validation.can_read(
                sources[source],
                f"Missing read permission on '{sources[source]}'"
            )
            Validation.can_write(
                sources[source],
                f"Missing write permission on '{sources[source]}'"
            )

        for destination in destinations:
            try:
                Validation.is_dir_writeable(
                    destinations[destination],
                    f"Directory '{destinations[destination]}' *must* exists and be writable"
                )
            except NotADirectoryError:
                parent_directory = Path(destinations[destination]).parent
                Validation.can_write(
                    parent_directory,
                    f"Missing write permission on '{parent_directory}'"
                )
                FileObserver.__LOG.info(f"Creating missing destination directory '{destinations[destination]}'")
                # create if not exists
                Path(destinations[destination]).mkdir(parents=True, exist_ok=True)

            for source in sources:
                Validation.are_symlinks(
                    sources[source],
                    destinations[destination],
                    f"Input ('{sources[source]}') and output ('{destinations[destination]}') directory can not be the same (or symlinks)"
                )
    def load(cls, log_filename: str = None) -> None:
        if log_filename is not None:
            from pathlib import Path
            from util import Validation
            parent_directory = Path(log_filename).parent
            Validation.is_dir_writeable(
                parent_directory,
                f"Directory '{parent_directory}' must exists and be writable")

        with cls.__LOCK:
            # formatters
            cls._FORMATTER = LogManager.__configure_formatter()
            # handlers
            cls._HANDLER_CONSOLE = LogManager.__configure_handler_console()
            if log_filename is not None:
                LogManager._LOG_FILENAME = log_filename
                cls._HANDLER_FILE = LogManager.__configure_handler_file(
                    log_filename)
            # loggers
            cls._LOGGER_ROOT = LogManager.__configure_logger_root()
            cls._LOGGER_OBSERVER = LogManager.__configure_logger_observer()
            cls._LOGGER_CONVERTER = LogManager.__configure_logger_dispatcher()