Exemple #1
0
 def on_any_event(event):
     """
     Describes the flow on file system events
     :param event:class:`FileSystemEvent`
     :return: None
     """
     # getting a logger object
     logger = CMD().get_logger()
     if event.is_directory:
         return None
     elif event.event_type == "deleted":
         # Event is deleted, you can process it now
         logger.info(f"Watchdog received {event.event_type} event - {event.src_path}")
     elif event.event_type != "deleted":
         # Event is created, modified, moved, you can process it now
         # creating a DistributionMaster object
         distributionMaster = DistributionMaster()
         # calls the controller for file distribution
         distributionMaster.distribution_controller()
         logger.info(f"Watchdog received {event.event_type} event - {event.src_path}.")
Exemple #2
0
class DirectoryWatchController:
    def __init__(self):
        # getting the location of target directory
        self.watch_directory = CMD().get_config()["locations"]["target"]
        # getting the logger object
        self.logger = CMD().get_logger()
        # getting a `watchdog` Observer object
        self.observer = Observer()
        # getting `FileSystemInitializer` object
        self.fileSystemInitializer = FileSystemInitializer()

    def run(self):
        """
        Function which controls and manages the files of Target Directory
        :return: None
        """
        # initializes the target directory
        self.fileSystemInitializer.initialise_target_directory()
        # initializer the categories' directories
        self.fileSystemInitializer.initialise_categorical_directories()
        # creating the event handler
        event_handler = Handler()

        # scheduling the observer object
        self.observer.schedule(event_handler, self.watch_directory, recursive=True)
        # starting the observer object
        self.observer.start()

        try:
            while True:
                # TOS = 5 seconds
                time.sleep(5)
        except Exception:
            # Stops the execution
            self.observer.stop()
            self.logger.info("Observer Stopped !")
        self.observer.join()
Exemple #3
0
class FileSystemInitializer:
    """
    Initializes the related directories
    """

    def __init__(self):
        # getting config as a dictionary
        self.config = CMD().get_config()
        # setting the target directory as a watch directory
        self.watch_directory = self.config["locations"]["target"]
        # setting the categorical directories
        self.categories_locations = self.config["locations"]["div-directories"]
        # getting the logger object
        self.logger = CMD().get_logger()

    def initialise_categorical_directories(self):
        """
        Checks the existence of categorical directories, creates such directory paths as and when required
        :return: None
        """
        # getting the names of the directories
        divide_directories = self.categories_locations.split("|")
        # appending `default` directory for those files which do not match at all
        divide_directories.append("default")

        # iterating through the names of all categorical directories
        for divide_directory in divide_directories:
            # making the paths for checking or creation
            if divide_directory != "default":
                divide_directory_path = self.config["output-locations"][divide_directory]
            else:
                divide_directory_path = f"{self.watch_directory}\\default"

            # checking for non-existence of the directory
            if not os.path.isdir(divide_directory_path):
                # makes the directory
                os.mkdir(divide_directory_path)
                self.logger.warning(f"Successfully created the directory: {divide_directory_path}")
            else:
                # skips if it already exists
                self.logger.info(f"Skipping creation of folder: {divide_directory_path}")

    def initialise_target_directory(self):
        """
        Checks the existence of target Directory, creates such directory paths as and when required
        :return: boolean
        """
        # checks the existence of the target directory
        if os.path.isdir(f"{self.watch_directory}"):
            self.logger.info(f"Skipping creation of folder: 👉 {self.watch_directory} 👈")
        else:
            self.logger.info(f"Target Folder Not Exists !")
            try:
                # creates the target directory if the directory does not exists
                os.mkdir(self.watch_directory)
            except OSError:
                self.logger.warning(f"Creation of the Target Directory {self.watch_directory} failed !")
                return False
            else:
                self.logger.info(f"Successfully created the Target Directory {self.watch_directory}")
        return True