Exemple #1
0
class CommonTest(unittest.TestCase):
    ''' Initialize '''
    def setUp(self):
        self.common = Common()

    ''' Test MIME type from the file '''

    def test_readMime(self):
        # Initialize
        file = "artifacts/css-cheat-sheet-v1.png"

        # Assert
        self.assertEqual("image/png", self.common.readMime(file))

    ''' Test for valid folder when output is True '''

    def test_validateFolderTrue(self):
        # Initialize
        current = os.getcwd()
        folder = os.path.join(current, "tests/artifacts")

        # Execute
        status = self.common.validateFolder(folder)

        # Assert
        self.assertTrue(status)

        # Cleanup
        self.common.changeFolder(current)

    ''' Test for valid folder when output is False '''

    def test_validateFolderFalse(self):
        # Initialize
        current = os.getcwd()
        folder = os.path.join(current, "tests/arti")

        # Execute
        status = self.common.validateFolder(folder)

        # Assert
        self.assertFalse(status)

        # Cleanup
        self.common.changeFolder(current)

    ''' Test folder name from the MIME type '''

    def test_getFolderName(self):
        # Initialize
        mime = self.common.readMime("artifacts/css-cheat-sheet-v1.png")

        # Assert
        self.assertEqual("Image/Png", self.common.getFolderName(mime))

    ''' Test chaning to user's supplied folder '''

    def test_changeFolder(self):
        # Initialize
        current = os.getcwd()
        folder = os.path.join(current, "tests/artifacts")

        # Execute
        self.common.changeFolder(folder)

        # Assert
        self.assertEqual(os.getcwd(), folder)

        # Cleanup
        self.common.changeFolder(current)

    ''' Test create folder if not already exists '''

    def test_createFolder(self):
        # Initialize
        current = os.getcwd()
        folder = os.path.join(current, "tests/artifacts/tmp")

        # Execute
        self.common.createFolder(folder)
        status = os.path.isdir(folder) & os.path.exists(folder)

        # Assert
        self.assertTrue(status)

        # Cleanup
        os.rmdir(folder)

    ''' Test moving files to the newly created folder '''

    def test_moveFileToFolder(self):
        # Initialize
        current = os.getcwd()
        parent = os.path.join(current, "tests/artifacts")
        folder = os.path.join(parent, "tmp")
        file = "css-cheat-sheet-v1.png"
        newfile = "css-cheat-sheet-v1-new.png"

        # Execute
        self.common.changeFolder(parent)
        copyfile(file, newfile)
        self.common.createFolder(folder)
        self.common.moveFileToFolder(folder, newfile)
        status = os.path.isfile(os.path.join(folder, newfile))

        # Assert
        self.assertTrue(status)

        # Cleanup
        os.remove(os.path.join(folder, newfile))
        os.rmdir(folder)
        self.common.changeFolder(current)

    ''' Test folder's permission when output is True '''

    def test_checkFolderPermissionTrue(self):
        # Initialize
        folder = os.path.join(os.getcwd(), "tests/artifacts")

        # Execute
        status = self.common.checkFolderPermission(folder)

        # Assert
        self.assertTrue(status)

    ''' Check folder's permission when output is False '''

    def test_checkFolderPermissionFalse(self):
        # Initialize
        folder = "/etc/apache2/extra"

        # Execute
        status = self.common.checkFolderPermission(folder)

        # Assert
        self.assertFalse(status)
class FileOrganizer:
    ''' Initialize variables '''
    def __init__(self):
        self.common = Common()
        self.currentFolder = ''
        self.currentFiles = ''

    ''' Start '''

    def main(self, folder):
        self.initialize(folder)
        if (self.validateFolder()):
            if (self.folderPermission()):
                self.fetchFiles()
                self.organizeFiles()

    ''' Initialize the program '''

    def initialize(self, folder):
        self.currentFolder = folder if folder != "" else self.fetchInput()

    ''' Request user for the name of the folder '''

    def fetchInput(self):
        try:
            folder = self.common.rawInput("Enter folder to organize: ")
        except:
            folder = ''

        return folder

    ''' Validate Folder '''

    def validateFolder(self):
        return self.common.validateFolder(self.currentFolder)

    ''' Check for folder's permission '''

    def folderPermission(self):
        return self.common.checkFolderPermission(self.currentFolder)

    ''' Fetch all the files from the folder and store them in list '''

    def fetchFiles(self):
        print("Fetching all files from %s" % (os.getcwd()))
        for root, dirs, files in os.walk(os.getcwd()):
            self.currentFiles = [f for f in files if not f[0] == '.']
            break

    ''' Organize files into folders and sub-folders '''

    def organizeFiles(self):
        if (len(self.currentFiles) == 0):
            print("Folder is empty. No files to organize, exiting.")
            return False
        else:
            print("Organizing files into sub-folders.")
            try:
                for file in self.currentFiles:
                    print("")
                    folder = self.common.getFolderName(
                        self.common.readMime(file))
                    self.common.createFolder(folder)
                    self.common.moveFileToFolder(folder, file)
            except:
                print("Exception occured")
                return False
            finally:
                print("Process completed.")
                return True