Exemplo n.º 1
0
 def setUp(self):
     self.mockCMDObject = CMD()
     self.path = rootpath.detect()
     self.test_target_directory = f"{self.path}\\tests\\test_target"
     self.test_file_path = f"{self.test_target_directory}\\test.txt"
     with open(self.test_file_path, "w"):
         pass
Exemplo n.º 2
0
 def __init__(self):
     # getting the config as a dictionary
     self.target_path = CMD().get_config()["locations"]["target"]
     # initializing the list for storing the files
     self.files = list()
     # getting the extensions from static yaml
     self.extensions = CMD().get_extensions_list()
Exemplo n.º 3
0
 def setUp(self):
     self.mockCMDObject = CMD()
     self.path = rootpath.detect()
     self.test_target_directory = f"{self.path}\\tests\\test_target"
     os.mkdir(f"{self.test_target_directory}\\pilot")
     self.valid_file_path = f"{self.test_target_directory}\\test.txt"
     with open(f"{self.test_target_directory}\\test.txt", "w"):
         pass
     self.test_target_directory = f"{self.test_target_directory}\\pilot"
Exemplo n.º 4
0
 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()
Exemplo n.º 5
0
 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()
Exemplo n.º 6
0
class CmdTestGetExtensionsList(unittest.TestCase):
    def setUp(self) -> None:
        self.mockCMDObject = CMD()
        self.numberOfExtensionsTypes = self.mockCMDObject.get_config()

    def runTest(self):
        test_result = self.mockCMDObject.get_extensions_list()
        self.assertTrue(
            len(test_result.keys()) ==
            len(self.numberOfExtensionsTypes["locations"]
                ["div-directories"].split("|")))
Exemplo n.º 7
0
class CmdTestGetLoggerObject(unittest.TestCase):
    def setUp(self) -> None:
        self.mockCMDObject = CMD()

    def runTest(self):
        test_result = self.mockCMDObject.get_logger()
        self.assertTrue(isinstance(test_result, logging.Logger))
Exemplo n.º 8
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()
Exemplo n.º 9
0
class CmdTestMoveFileToFolderMethodWhenFileIsUnavailable(unittest.TestCase):
    def setUp(self):
        self.mockCMDObject = CMD()
        self.path = rootpath.detect()
        self.test_target_directory = f"{self.path}\\tests\\test_target"
        os.mkdir(f"{self.test_target_directory}\\pilot")
        self.test_target_directory = f"{self.test_target_directory}\\pilot"

    def runTest(self):
        invalid_file_path = f"{self.test_target_directory}\\test.txt"
        test_result = self.mockCMDObject.move_file_to_folder(
            invalid_file_path, self.test_target_directory)
        self.assertFalse(test_result)

    def tearDown(self) -> None:
        shutil.rmtree(f"{self.test_target_directory}")
Exemplo n.º 10
0
class DistributionMaster:
    """
    Controls the files distribution of the files of the target directory
    """
    def __init__(self):
        # getting the config as a dictionary
        self.target_path = CMD().get_config()["locations"]["target"]
        # initializing the list for storing the files
        self.files = list()
        # getting the extensions from static yaml
        self.extensions = CMD().get_extensions_list()

    def distribution_controller(self):
        """
        Distributes the files according to the extensions
        :return: None
        """
        # grabbing the files in the target directory
        self.files = [file for file in listdir(self.target_path) if isfile(join(self.target_path, file))]
        # iterating the files in the files' list
        for file in self.files:
            # move the file by extension type
            self.move_file_by_extension_type(file)

    def move_file_by_extension_type(self, file):
        """
        Moves the files on the basis of the extensions
        :param file: string
        :return: None
        """
        # creating the flag to indicate match and folder_location
        match, folder_location = False, ""
        filepath = f"{self.target_path}\\{file}"
        # iterating the through all the extensions received from the static yaml
        for extension_type, extension in self.extensions.items():
            current_file_extension = file.split('.')[-1]
            if current_file_extension in extension:
                match, folder_location = True, extension_type
        # moves the file to the specified location when the match is found
        if match:
            location = f'{self.target_path}\\{folder_location}'
        else:
            #  moves the file to the specified location when the match is not found
            location = f"{self.target_path}\\default"
        # initiated the move of the file to location
        CMD().move_file_to_folder(filepath=filepath, location=location)
Exemplo n.º 11
0
class CmdTestMoveFileToFolderMethodWhenLocationIsAvailable(unittest.TestCase):
    def setUp(self):
        self.mockCMDObject = CMD()
        self.path = rootpath.detect()
        self.test_target_directory = f"{self.path}\\tests\\test_target"
        self.test_file_path = f"{self.test_target_directory}\\test.txt"
        os.mkdir(f"{self.test_target_directory}\\pilot")
        with open(self.test_file_path, "w"):
            pass

    def runTest(self):
        valid_path = f"{self.test_target_directory}\\pilot"
        test_result = self.mockCMDObject.move_file_to_folder(
            self.test_file_path, valid_path)
        self.assertTrue(test_result)

    def tearDown(self) -> None:
        shutil.rmtree(f"{self.test_target_directory}\\pilot")
Exemplo n.º 12
0
class CmdTestMoveFileToFolderMethodWhenLocationIsUnavailable(
        unittest.TestCase):
    def setUp(self):
        self.mockCMDObject = CMD()
        self.path = rootpath.detect()
        self.test_target_directory = f"{self.path}\\tests\\test_target"
        self.test_file_path = f"{self.test_target_directory}\\test.txt"
        with open(self.test_file_path, "w"):
            pass

    def runTest(self):
        invalid_path = f"{self.test_target_directory}\\invalid_directory"
        test_result = self.mockCMDObject.move_file_to_folder(
            self.test_file_path, invalid_path)
        self.assertFalse(test_result)

    def tearDown(self) -> None:
        os.remove(self.test_file_path)
Exemplo n.º 13
0
 def move_file_by_extension_type(self, file):
     """
     Moves the files on the basis of the extensions
     :param file: string
     :return: None
     """
     # creating the flag to indicate match and folder_location
     match, folder_location = False, ""
     filepath = f"{self.target_path}\\{file}"
     # iterating the through all the extensions received from the static yaml
     for extension_type, extension in self.extensions.items():
         current_file_extension = file.split('.')[-1]
         if current_file_extension in extension:
             match, folder_location = True, extension_type
     # moves the file to the specified location when the match is found
     if match:
         location = f'{self.target_path}\\{folder_location}'
     else:
         #  moves the file to the specified location when the match is not found
         location = f"{self.target_path}\\default"
     # initiated the move of the file to location
     CMD().move_file_to_folder(filepath=filepath, location=location)
Exemplo n.º 14
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}.")
Exemplo n.º 15
0
 def setUp(self) -> None:
     self.mockCMDObject = CMD()
     self.numberOfExtensionsTypes = self.mockCMDObject.get_config()
Exemplo n.º 16
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
Exemplo n.º 17
0
 def setUp(self):
     self.mockCMDObject = CMD()
     self.path = rootpath.detect()
     self.test_target_directory = f"{self.path}\\tests\\test_target"
     os.mkdir(f"{self.test_target_directory}\\pilot")
     self.test_target_directory = f"{self.test_target_directory}\\pilot"
Exemplo n.º 18
0
 def setUp(self) -> None:
     self.mockCMDObject = CMD()