Exemplo n.º 1
0
    def test_valid_type_organization(self):
        file_list = ['file.doc', 'file.docx', 'file.pdf',
                     'song.mp3', 'song.wav',
                     'some torrent.torrent',
                     'picture.png', 'picture.jpg',
                     'exec.exe', 'exec.bin', 'exec.app',
                     'exec.osx', 'exec.msi',
                     'presentation.pptx']

        organizer = Organizer('C:')
        organizer.file_list = file_list

        files_by_type = organizer.organize_by_type()

        self.assertCountEqual(files_by_type['Documents'],
                                           ['file.doc', 'file.docx',
                                            'file.pdf'])
        self.assertCountEqual(files_by_type['Music'],
                                           ['song.wav', 'song.mp3'])
        self.assertCountEqual(files_by_type['Torrents'],
                                           ['some torrent.torrent'])
        self.assertCountEqual(files_by_type['Pictures'],
                                           ['picture.png', 'picture.jpg'])
        self.assertCountEqual(files_by_type['Executables'],
                                           ['exec.exe', 'exec.bin',
                                            'exec.app', 'exec.osx',
                                            'exec.msi'])
        self.assertCountEqual(files_by_type['Presentations'],
                                           ['presentation.pptx'])
    def reorganize(self, type_of_reorganization):
        """
        Reorganizes the directory by copying its files
        to their respective destinations determined by the
        organization type. Creates a docx report with
        information about the copied files
        """
        organizer = Organizer(self.source_folder_path)
        organization = defaultdict(list)

        if type_of_reorganization == 'extension':
            organization = organizer.organize_by_extension()

        if type_of_reorganization == 'type':
            organization = organizer.organize_by_type()

        if type_of_reorganization == 'content':
            organization = organizer.organize_by_content(self.match_strings)

        # create folder named as the type of organization (extension,content..)
        type_folder = self.target_folder_path + "\\" + type_of_reorganization
        if os.path.exists(type_folder) is False:
            os.mkdir(type_folder)

        self.create_new_dirs(organization, type_folder)

        # create report
        doc_handler = DocumentHandler()

        # a log dictionary, holding the copied filenames and their destination
        log = {}

        for folder_name in organization:
            self.copy_files(organization[folder_name],
                            type_folder + "\\" + folder_name)
            for file in organization[folder_name]:
                destination = type_folder + "\\" + folder_name
                log[file] = destination

        report = doc_handler.create_report(self.target_folder_path, log)