def test_returns_false_if_original_folder_not_exists(self): temp_path = self.tempdir.path write = lambda path: self.tempdir.write(path, '') isfile = os.path.isfile dir_ = self.tempdir.makedir('one/two') dir_2 = self.tempdir.makedir('one/two/document/PDF/') file_1 = write('abc/for/PDF/this long name.pdf') file_2 = write( 'abc/for/PDF/somefolder/another/and another/JPEG/abc.jpeg') file_3 = write('abc/for/PDF/somefolder/%s' % SORTER_IGNORE_FILENAME) with self.subTest(1): compare(True, os.path.isfile(file_3)) with self.subTest(2): compare(True, os.path.isdir(os.path.dirname(file_2))) dir_1 = Folder(os.path.dirname(file_1)) dir_1.move_to(dir_) with self.subTest(3): self.tempdir.compare([ '{}/'.format('document'), '{}/{}'.format('document', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/'.format('document', 'PDF'), '{}/{}/{}'.format('document', 'PDF', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'PDF', 'this long name.pdf'), ], path=dir_) with self.subTest(4): compare(True, os.path.isdir(dir_2))
def test_retuns_false_folder_with_multiple_subfolders_relocation_failse(self): temp_path = self.tempdir.path write = lambda path: self.tempdir.write(path, '') dir_ = self.tempdir.makedir('one/two') self.tempdir.makedir('one/two/document/PDF/') file_1 = write('abc/for/PDF/this long name.pdf') write('abc/for/PDF/somefolder/another/and another/JPEG/abc.jpeg') dir_1 = Folder(os.path.dirname(file_1)) dir_1.move_to(dir_) with self.subTest(1): self.tempdir.compare([ '{}/'.format('document'), '{}/{}'.format('document', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/'.format('document', 'PDF'), '{}/{}/{}'.format('document', 'PDF', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}/'.format('document', 'PDF', 'somefolder'), '{}/{}/{}'.format('document', 'PDF', 'this long name.pdf'), '{}/{}/{}/{}/'.format('document', 'PDF', 'somefolder', 'another'), '{}/{}/{}/{}/{}/'.format('document', 'PDF', 'somefolder', 'another', 'and another'), '{}/{}/{}/{}/{}/{}/'.format('document', 'PDF', 'somefolder', 'another', 'and another', 'JPEG'), '{}/{}/{}/{}/{}/{}/{}'.format('document', 'PDF', 'somefolder', 'another', 'and another', 'JPEG', 'abc.jpeg'), ], path=dir_)
def _sort_folders_operation(self, send_message): source_path = self.src destination_path = self.dst or self.src search_string_pattern = self.search_string_pattern search_string = self.search_string folder_list_matching_pattern = iglob(os.path.join( source_path, search_string_pattern + '*')) folders = (folder for folder in folder_list_matching_pattern if os.path.isdir( folder) and not has_signore_file(folder)) send_message(through=['status'], msg='Searching for folders...', weight=1) for folder in folders: folder_path = os.path.join(source_path, folder) folder_instance = Folder(folder_path) initial_path = folder_instance.path initial_name = folder_instance.name last_modified = os.path.getmtime(initial_path) msg = 'Moving {} to {}'.format( folder_instance.path, destination_path) send_message(through=['status'], msg=msg, weight=1) folder_instance.move_to( destination_path, group_folder_name=search_string) new_path = folder_instance.path if initial_path != new_path: msg = 'Moved {} to {}'.format(folder_instance.name, new_path) send_message(through=['status'], msg=msg, weight=1) # Write to DB hash_path = hashlib.md5( initial_path.encode('utf-8')).hexdigest() file_dict = {'filename': initial_name, 'filepath_hash': hash_path, 'last_modified': datetime.fromtimestamp(last_modified)} path_dict = {'source': initial_path, 'destination': new_path} this_file_dict = {initial_name: { 'file': file_dict, 'path': path_dict}} self.database_dict.update(this_file_dict)
def test_returns_false_if_grouping_failed(self): dir_1 = self.tempdir.makedir('abc/for/document') dir_2 = self.tempdir.makedir('abc/for/PDF') dir_3 = self.tempdir.makedir('abc/for/last') dir_ = self.tempdir.makedir('one/two') d1 = Folder(dir_1) d2 = Folder(dir_2) d3 = Folder(dir_3) with self.subTest(1): compare(dir_1, d1.path) d1.move_to(dir_) with self.subTest(2): compare(os.path.join(dir_, 'document'), d1.path) with self.subTest(3): compare(dir_2, d2.path) d2.move_to(dir_) with self.subTest(4): compare(os.path.join(dir_, 'document', 'PDF'), d2.path) with self.subTest(5): compare(dir_3, d3.path) d3.move_to(dir_) with self.subTest(6): compare(os.path.join(dir_, 'FOLDERS', 'last'), d3.path)
def test_returns_false_if_original_folder_exists(self): temp_path = self.tempdir.path write = lambda path: self.tempdir.write(path, '') isfile = os.path.isfile dir_ = self.tempdir.makedir('one/two') self.tempdir.makedir('one/two/document/PDF/') file_1 = write('abc/for/PDF/this long name.pdf') dir_1 = Folder(os.path.dirname(file_1)) dir_1.move_to(dir_) with self.subTest(1): self.tempdir.compare([ '{}/'.format('document'), '{}/{}'.format('document', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/'.format('document', 'PDF'), '{}/{}/{}'.format('document', 'PDF', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'PDF', 'this long name.pdf'), ], path=dir_) with self.subTest(2): compare(False, os.path.isdir(os.path.dirname(file_1)))
def _sort_folders_operation(self, send_message): source_path = self.src destination_path = self.dst or self.src search_string_pattern = self.search_string_pattern search_string = self.search_string folder_list_matching_pattern = iglob( os.path.join(source_path, search_string_pattern + '*')) folders = (folder for folder in folder_list_matching_pattern if os.path.isdir(folder) and not has_signore_file(folder)) send_message(through=['status', 'progress_text'], msg='Searching for folders...', weight=1) for folder in folders: folder_path = os.path.join(source_path, folder) folder_instance = Folder(folder_path) msg = 'Moving {} to {}'.format(folder_instance.path, destination_path) send_message(through=['status', 'progress_text'], msg=msg, weight=1) folder_instance.move_to(destination_path, group_folder_name=search_string)
def test_folder_grouping_with_files(self): write = lambda path: self.tempdir.write(path, '') isfile = os.path.isfile dir_ = self.tempdir.makedir('one/two') file_1 = write('abc/for/PDF/this long name.pdf') file_2 = write('abc/for/DOCX/y.docx') file_3 = write('abc/for/DOC/123 54.doc') file_4 = write('abc/for/none/xw') file_5 = write('abc/for/PDF/second.pdf') dir_1 = Folder(os.path.dirname(file_1)) dir_2 = Folder(os.path.dirname(file_2)) dir_3 = Folder(os.path.dirname(file_3)) dir_4 = Folder(os.path.dirname(file_4)) dir_5 = Folder(os.path.dirname(file_5)) dir_6 = Folder(dir_) with self.subTest(1): compare([True, True, True, True], [isfile(file_1), isfile(file_2), isfile(file_3), isfile(file_4)]) dir_1.move_to(dir_) dir_2.move_to(dir_) dir_3.move_to(dir_) dir_4.move_to(dir_) dir_5.move_to(dir_) dir_6.move_to(dir_) with self.subTest(3): self.tempdir.compare([ '{}/'.format('document'), '{}/'.format('FOLDERS'), '{}/{}'.format('document', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}'.format('FOLDERS', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/'.format('document', 'PDF'), '{}/{}/'.format('document', 'DOCX'), '{}/{}/'.format('document', 'DOC'), '{}/{}/'.format('FOLDERS', 'none'), '{}/{}/{}'.format('document', 'PDF', 'this long name.pdf'), '{}/{}/{}'.format('document', 'PDF', 'second.pdf'), '{}/{}/{}'.format('document', 'DOCX', 'y.docx'), '{}/{}/{}'.format('document', 'DOC', '123 54.doc'), '{}/{}/{}'.format('FOLDERS', 'none', 'xw'), '{}/{}/{}'.format('document', 'PDF', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'DOCX', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'DOC', SORTER_FOLDER_IDENTITY_FILENAME), ], path=dir_) with self.subTest(4): compare(os.path.join(dir_, 'document', 'PDF'), dir_5.path)
def test_folder_grouping_with_files_and_existing_destinations(self): temp_path = self.tempdir.path write = lambda path: self.tempdir.write(path, '') isfile = os.path.isfile dir_ = self.tempdir.makedir('one/two') self.tempdir.makedir('one/two/document/PDF/') self.tempdir.makedir('one/two/document/DOCX') self.tempdir.makedir('one/two/document/DOC') file_1 = write('abc/for/PDF/this long name.pdf') file_2 = write('abc/for/DOCX/y.docx') file_3 = write('abc/for/DOC/123 54.doc') file_4 = write('abc/for/none/xw') file_5 = write('abc/for/PDF/second.pdf') file_6 = write('abc/for/image/JPEG/abc.jpeg') dir_1 = Folder(os.path.dirname(file_1)) dir_2 = Folder(os.path.dirname(file_2)) dir_3 = Folder(os.path.dirname(file_3)) dir_4 = Folder(os.path.dirname(file_4)) dir_5 = Folder(os.path.dirname(file_5)) dir_6 = Folder(os.path.dirname(os.path.dirname(file_6))) dir_7 = Folder(dir_) with self.subTest(1): compare([True, True, True, True], [isfile(file_1), isfile(file_2), isfile(file_3), isfile(file_4)]) with self.subTest(2): compare('document', dir_1.category_folder) dir_1.move_to(dir_) dir_2.move_to(dir_) dir_3.move_to(dir_) dir_4.move_to(dir_) dir_5.move_to(dir_) dir_6.move_to(dir_) dir_7.move_to(dir_) with self.subTest(3): compare(True, dir_1.for_sorter) with self.subTest(4): self.tempdir.compare([ '{}/'.format('document'), '{}/'.format('image'), '{}/'.format('FOLDERS'), '{}/{}/'.format('document', 'PDF'), '{}/{}/'.format('document', 'DOCX'), '{}/{}/'.format('document', 'DOC'), '{}/{}/'.format('image', 'JPEG'), '{}/{}/'.format('FOLDERS', 'none'), '{}/{}'.format('document', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}'.format('image', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}'.format('FOLDERS', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'PDF', 'this long name.pdf'), '{}/{}/{}'.format('document', 'PDF', 'second.pdf'), '{}/{}/{}'.format('document', 'DOCX', 'y.docx'), '{}/{}/{}'.format('document', 'DOC', '123 54.doc'), '{}/{}/{}'.format('image', 'JPEG', 'abc.jpeg'), '{}/{}/{}'.format('FOLDERS', 'none', 'xw'), '{}/{}/{}'.format('document', 'PDF', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'DOCX', SORTER_FOLDER_IDENTITY_FILENAME), '{}/{}/{}'.format('document', 'DOC', SORTER_FOLDER_IDENTITY_FILENAME), ], path=dir_) with self.subTest(5): compare(os.path.join(dir_, 'document', 'PDF'), dir_5.path) with self.subTest(6): compare(True, os.path.isdir(dir_1.path)) with self.subTest(7): compare(os.path.join(temp_path, dir_, 'document', 'PDF'), dir_1.path) with self.subTest(8): compare(True, os.path.isfile(os.path.join( dir_1.path, SORTER_FOLDER_IDENTITY_FILENAME))) with self.subTest(9): compare(False, os.path.exists(os.path.dirname(file_1)))