def move_file(self, file_path, filename, provider, success=True): """Move the files from the current directory to the _Processed if successful, else _Error if unsuccessful. Creates _Processed and _Error directories within current directory if they don't exist. :param file_path: str - current directory location :param filename: str - file name in the current directory to move :param provider: dict - Ingest provider details to which the current directory has been configured :param success: bool - default value is True. When True moves to _Processed directory else _Error directory. :raises IngestFileError.folderCreateError() if creation of _Processed or _Error directories fails :raises IngestFileError.fileMoveError() if failed to move the file pointed by filename """ try: if not os.path.exists(os.path.join(file_path, "_PROCESSED/")): os.makedirs(os.path.join(file_path, "_PROCESSED/")) if not os.path.exists(os.path.join(file_path, "_ERROR/")): os.makedirs(os.path.join(file_path, "_ERROR/")) except Exception as ex: raise IngestFileError.folderCreateError(ex, provider) try: if success: shutil.copy2(os.path.join(file_path, filename), os.path.join(file_path, "_PROCESSED/")) else: shutil.copy2(os.path.join(file_path, filename), os.path.join(file_path, "_ERROR/")) except Exception as ex: raise IngestFileError.fileMoveError(ex, provider) finally: os.remove(os.path.join(file_path, filename))
def test_raise_folderCreateError(self): with assert_raises(IngestFileError) as error_context: ex = Exception("Testing folderCreateError") raise IngestFileError.folderCreateError(ex, self.provider) exception = error_context.exception self.assertTrue(exception.code == 3001) self.assertTrue(exception.message == "Destination folder could not be created") self.assertIsNotNone(exception.system_exception) self.assertEqual(exception.system_exception.args[0], "Testing folderCreateError") self.assertEqual(len(self.mock_logger_handler.messages['error']), 1) self.assertEqual(self.mock_logger_handler.messages['error'][0], "IngestFileError Error 3001 - Destination folder could not be created: " "Testing folderCreateError on channel TestProvider")
def test_raise_folderCreateError(self): with assert_raises(IngestFileError) as error_context: ex = Exception("Testing folderCreateError") raise IngestFileError.folderCreateError(ex, self.provider) exception = error_context.exception self.assertTrue(exception.code == 3001) self.assertTrue( exception.message == "Destination folder could not be created") self.assertIsNotNone(exception.system_exception) self.assertEquals(exception.system_exception.args[0], "Testing folderCreateError") self.assertEqual(len(self.mock_logger_handler.messages['error']), 1) self.assertEqual( self.mock_logger_handler.messages['error'][0], "IngestFileError Error 3001 - Destination folder could not be created: " "Testing folderCreateError on channel TestProvider")
def move_file(self, filepath, filename, provider, success=True): """ Move the files from the current directory to the _Processed directory in successful else _Error if unsuccessful. Creates _Processed and _Error directory within current directory """ try: if not os.path.exists(os.path.join(filepath, "_PROCESSED/")): os.makedirs(os.path.join(filepath, "_PROCESSED/")) if not os.path.exists(os.path.join(filepath, "_ERROR/")): os.makedirs(os.path.join(filepath, "_ERROR/")) except Exception as ex: raise IngestFileError.folderCreateError(ex, provider) try: if success: shutil.copy2(os.path.join(filepath, filename), os.path.join(filepath, "_PROCESSED/")) else: shutil.copy2(os.path.join(filepath, filename), os.path.join(filepath, "_ERROR/")) except Exception as ex: raise IngestFileError.fileMoveError(ex, provider) finally: os.remove(os.path.join(filepath, filename))