diff --git a/elodie.py b/elodie.py index bcda4a2..022141a 100755 --- a/elodie.py +++ b/elodie.py @@ -31,7 +31,7 @@ DB = Db() FILESYSTEM = FileSystem() -def import_file(_file, destination, album_from_folder, trash): +def import_file(_file, destination, album_from_folder, trash, allow_duplicates): """Set file metadata and move it to destination. """ if not os.path.exists(_file): @@ -55,7 +55,7 @@ def import_file(_file, destination, album_from_folder, trash): media.set_album_from_folder() dest_path = FILESYSTEM.process_file(_file, destination, - media, allowDuplicate=False, move=False) + media, allowDuplicate=allow_duplicates, move=False) if dest_path: print('%s -> %s' % (_file, dest_path)) if trash: @@ -75,8 +75,10 @@ def import_file(_file, destination, album_from_folder, trash): help="Use images' folders as their album names.") @click.option('--trash', default=False, is_flag=True, help='After copying files, move the old files to the trash.') +@click.option('--allow-duplicates', default=False, is_flag=True, + help='Import the file even if it\'s already been imported.') @click.argument('paths', nargs=-1, type=click.Path()) -def _import(destination, source, file, album_from_folder, trash, paths): +def _import(destination, source, file, album_from_folder, trash, paths, allow_duplicates): """Import files or directories. """ destination = os.path.expanduser(destination) @@ -96,7 +98,7 @@ def _import(destination, source, file, album_from_folder, trash, paths): for current_file in files: import_file(current_file, destination, album_from_folder, - trash) + trash, allow_duplicates) def update_location(media, file_path, location_name): diff --git a/elodie/tests/elodie_test.py b/elodie/tests/elodie_test.py index 68bb94e..97749bf 100644 --- a/elodie/tests/elodie_test.py +++ b/elodie/tests/elodie_test.py @@ -29,7 +29,7 @@ def test_import_file_text(): shutil.copyfile(helper.get_file('valid.txt'), origin) reset_hash_db() - dest_path = elodie.import_file(origin, folder_destination, False, False) + dest_path = elodie.import_file(origin, folder_destination, False, False, False) restore_hash_db() shutil.rmtree(folder) @@ -46,7 +46,7 @@ def test_import_file_audio(): shutil.copyfile(helper.get_file('audio.m4a'), origin) reset_hash_db() - dest_path = elodie.import_file(origin, folder_destination, False, False) + dest_path = elodie.import_file(origin, folder_destination, False, False, False) restore_hash_db() shutil.rmtree(folder) @@ -62,7 +62,7 @@ def test_import_file_photo(): shutil.copyfile(helper.get_file('plain.jpg'), origin) reset_hash_db() - dest_path = elodie.import_file(origin, folder_destination, False, False) + dest_path = elodie.import_file(origin, folder_destination, False, False, False) restore_hash_db() shutil.rmtree(folder) @@ -78,7 +78,7 @@ def test_import_file_video(): shutil.copyfile(helper.get_file('video.mov'), origin) reset_hash_db() - dest_path = elodie.import_file(origin, folder_destination, False, False) + dest_path = elodie.import_file(origin, folder_destination, False, False, False) restore_hash_db() shutil.rmtree(folder) @@ -86,6 +86,43 @@ def test_import_file_video(): assert helper.path_tz_fix(os.path.join('2015-01-Jan','California','2015-01-19_12-45-11-video.mov')) in dest_path, dest_path +def test_import_file_allow_duplicate_false(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/valid.txt' % folder + shutil.copyfile(helper.get_file('valid.txt'), origin) + + reset_hash_db() + dest_path1 = elodie.import_file(origin, folder_destination, False, False, False) + dest_path2 = elodie.import_file(origin, folder_destination, False, False, False) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert dest_path1 is not None + assert dest_path2 is None + +def test_import_file_allow_duplicate_true(): + temporary_folder, folder = helper.create_working_folder() + temporary_folder_destination, folder_destination = helper.create_working_folder() + + origin = '%s/valid.txt' % folder + shutil.copyfile(helper.get_file('valid.txt'), origin) + + reset_hash_db() + dest_path1 = elodie.import_file(origin, folder_destination, False, False, True) + dest_path2 = elodie.import_file(origin, folder_destination, False, False, True) + restore_hash_db() + + shutil.rmtree(folder) + shutil.rmtree(folder_destination) + + assert dest_path1 is not None + assert dest_path2 is not None + assert dest_path1 == dest_path2 + def test_update_location_on_audio(): temporary_folder, folder = helper.create_working_folder() temporary_folder_destination, folder_destination = helper.create_working_folder()