def test_returns_false_if_names_not_match(self): initial_file = self.tempdir.write('123.txt', '') for i in range(1, 10): filename = '123 - dup ({}).txt'.format(str(i)) d = File(initial_file) with self.subTest(i): compare(filename, d.find_suitable_name(initial_file)) self.tempdir.write(os.path.join(self.tempdir.path, filename), '')
def sort_files(self, src=None, send_message=None): """Move files in relation to their extensions and categories. This function runs according to the patterns of the sdir module. """ source_path = src or self.src destination_path = self.dst or source_path string_pattern = self.search_string_pattern file_types = self.file_types glob_pattern = self.glob_pattern group_folder_name = self.group_folder_name string_glob_pattern = string_pattern + glob_pattern glob_files = (i for item in file_types for i in iglob( os.path.join(source_path, string_glob_pattern + item)) if os.path.isfile(i)) send_message(through=['status', 'progress_text'], msg='Searching for files...', weight=1) for file_ in glob_files: file_instance = File(os.path.join(source_path, file_)) initial_path = file_instance.path initial_name = file_instance.name last_modified = os.path.getmtime(initial_path) file_instance.move_to(destination_path, group=self.group, group_folder_name=group_folder_name, by_extension=self.by_extension) new_path = file_instance.path if initial_path != new_path: msg = 'Moving {} to {}'.format(file_instance.name, new_path) send_message(through=['status', 'progress_text'], 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_file_not_created(self): dir_1 = self.tempdir.makedir('abc/fig/one/two/three') file_1 = os.path.join(dir_1, 'my awesome cat.txt') f1 = File(file_1) with self.subTest(1): compare(False, f1.exists) f1.touch() with self.subTest(2): compare(True, f1.exists) with self.subTest(3): self.assertRaises(FileExistsError, f1.touch, exist_ok=False)
def test_returns_false_if_grouping_fails_group_true(self): dir_1 = self.tempdir.makedir('abc/fig/') dir_2 = self.tempdir.makedir('one/twp/') self.tempdir.makedir('some/dir/') file_1 = self.tempdir.write( os.path.join(dir_1, 'my awesome cat.txt'), '') self.tempdir.write(os.path.join(dir_1, 'my awesome cat.txt'), '') self.tempdir.write(os.path.join(dir_1, '1.jpeg'), '') f1 = File(file_1) f1.move_to(dst_root_path=dir_2, group=True, by_extension=False, group_folder_name=None) with self.subTest(1): final_path = os.path.join(dir_2, 'document', 'my awesome cat.txt') compare(final_path, f1.path)
def test_returns_false_if_parent_attributes_not_set(self): d = File(self.tempdir.path) with self.subTest(1): compare(self.tempdir.path, d.path) with self.subTest(2): compare(os.path.basename(self.tempdir.path), d.name) with self.subTest(3): compare(os.path.dirname(self.tempdir.path), d.parent)
def test_returns_false_if_pathlib_methods_fail(self): path = self.tempdir.makedir('abc/fig/') file_ = self.tempdir.write(os.path.join(path, 'my awesome cat.txt'), '') d = File(file_) with self.subTest(1): compare('.txt', d.suffix) with self.subTest(2): compare('my awesome cat', d.stem)
def test_returns_false_if_emptynameerror_not_raised(self): temp_path = self.tempdir.path dir_1 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two', 'three') dir_2 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two') self.tempdir.makedir(dir_1) file_1 = self.tempdir.write( os.path.join(dir_1, 'my awesome cat.txt'), '') f1 = File(file_1) self.assertRaises(EmptyNameError, f1.move_to, dst_root_path=dir_2, group=True, by_extension=True, group_folder_name=' ')
def test_returns_false_if_relocation_of_file_without_extension_fails(self): temp_path = self.tempdir.path dir_1 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two', 'three') dir_2 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two') dir_3 = os.path.join(temp_path, 'abc', 'fig') self.tempdir.makedir(dir_1) file_1 = self.tempdir.write( os.path.join(dir_1, '146 awesome street $# yea'), '') f1 = File(file_1) f1.move_to(dst_root_path=dir_2, group=True, by_extension=True, group_folder_name='sample dir') with self.subTest(1): compare(True, f1.exists) with self.subTest(2): final_path = os.path.join(dir_2, 'sample dir', 'UNDEFINED', '146 awesome street $# yea') compare(final_path, f1.path) f1.move_to(dst_root_path=dir_3, group=False, by_extension=True, group_folder_name='sample dir') with self.subTest(3): final_path = os.path.join(dir_3, 'UNDEFINED', '146 awesome street $# yea') compare(final_path, f1.path)
def test_returns_false_if_relocation_to_parent_folder_fails(self): temp_path = self.tempdir.path dir_1 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two', 'three') dir_2 = os.path.join(temp_path, 'abc', 'fig', 'one', 'two') dir_3 = os.path.join(temp_path, 'abc', 'fig') self.tempdir.makedir(dir_1) file_1 = self.tempdir.write(os.path.join(dir_1, 'my awesome cat.txt'), '') f1 = File(file_1) f1.move_to(dst_root_path=dir_2, group=True, by_extension=True, group_folder_name='sample dir') with self.subTest(1): final_path = os.path.join(dir_2, 'sample dir', 'TXT', 'my awesome cat.txt') compare(final_path, f1.path) f1.move_to(dst_root_path=dir_3, group=True, by_extension=True, group_folder_name='sample dir') with self.subTest(2): final_path = os.path.join(dir_3, 'sample dir', 'TXT', 'my awesome cat.txt') compare(final_path, f1.path)
def test_returns_false_if_path_change_is_relative(self): d = File(self.tempdir.path) with self.subTest(1): compare(self.tempdir.path, d.path) if os.name != 'nt': with self.subTest(2): compare(False, d.hidden_path) with self.subTest(3): compare('UNDEFINED', d.default_category) def call(x): d.path = x self.assertRaises(RelativePathError, call, '.')
def test_returns_false_if_attributes_not_set(self): file_path = self.tempdir.write('123.txt', '') d = File(file_path) with self.subTest(1): compare(file_path, d.path) if os.name != 'nt': with self.subTest(2): compare(False, d.hidden_path) with self.subTest(3): compare(os.path.basename(file_path), d.name) with self.subTest(4): compare(True, d.exists) with self.subTest(5): compare('document', d.category) with self.subTest(6): compare('txt', d.extension) with self.subTest(7): compare(d.path, str(d))
def test_returns_false_if_relocation_to_existing_child_folder_fails(self): dir_1 = self.tempdir.makedir('abc/fig/') dir_2 = self.tempdir.makedir('abc/fig/one/two') dir_3 = self.tempdir.makedir('abc/fig/one/two/three/') file_1 = self.tempdir.write( os.path.join(dir_1, 'my awesome cat.txt'), '') f1 = File(file_1) f1.move_to(dst_root_path=dir_2, group=True, by_extension=True, group_folder_name='sample dir') with self.subTest(1): final_path = os.path.join( dir_2, 'sample dir', 'TXT', 'my awesome cat.txt') compare(final_path, f1.path) f1.move_to(dst_root_path=dir_3, group=True, by_extension=True, group_folder_name='sample dir') with self.subTest(2): final_path = os.path.join( dir_3, 'sample dir', 'TXT', 'my awesome cat.txt') compare(final_path, f1.path)
def test_returns_false_if_file_moving_fails(self): dir_1 = self.tempdir.makedir('abc/fig/') dir_2 = self.tempdir.makedir('one/twp/') dir_3 = self.tempdir.makedir('some/dir/') file_ = self.tempdir.write( os.path.join(dir_1, 'my awesome cat.txt'), '') f = File(file_) f.move_to(dir_2, group=False) with self.subTest(1): compare(os.path.join(dir_2, 'TXT', 'my awesome cat.txt'), f.path) with self.subTest(2): compare(True, f.exists) f.move_to(dir_3, group=True, by_extension=True) with self.subTest(3): compare(os.path.join(dir_3, 'document', 'TXT', 'my awesome cat.txt'), f.path) with self.subTest(4): compare(True, f.exists) with self.subTest(5): compare(False, os.path.isfile(os.path.join( dir_2, 'TXT', 'my awesome cat.txt')))