gh-129 Add --allow-duplicates flag for command line program

This commit is contained in:
Jaisen Mathai 2016-09-07 21:08:13 -07:00
parent a197adb19f
commit 341173e381
2 changed files with 47 additions and 8 deletions

View File

@ -31,7 +31,7 @@ DB = Db()
FILESYSTEM = FileSystem() 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. """Set file metadata and move it to destination.
""" """
if not os.path.exists(_file): if not os.path.exists(_file):
@ -55,7 +55,7 @@ def import_file(_file, destination, album_from_folder, trash):
media.set_album_from_folder() media.set_album_from_folder()
dest_path = FILESYSTEM.process_file(_file, destination, dest_path = FILESYSTEM.process_file(_file, destination,
media, allowDuplicate=False, move=False) media, allowDuplicate=allow_duplicates, move=False)
if dest_path: if dest_path:
print('%s -> %s' % (_file, dest_path)) print('%s -> %s' % (_file, dest_path))
if trash: if trash:
@ -75,8 +75,10 @@ def import_file(_file, destination, album_from_folder, trash):
help="Use images' folders as their album names.") help="Use images' folders as their album names.")
@click.option('--trash', default=False, is_flag=True, @click.option('--trash', default=False, is_flag=True,
help='After copying files, move the old files to the trash.') 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()) @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. """Import files or directories.
""" """
destination = os.path.expanduser(destination) destination = os.path.expanduser(destination)
@ -96,7 +98,7 @@ def _import(destination, source, file, album_from_folder, trash, paths):
for current_file in files: for current_file in files:
import_file(current_file, destination, album_from_folder, import_file(current_file, destination, album_from_folder,
trash) trash, allow_duplicates)
def update_location(media, file_path, location_name): def update_location(media, file_path, location_name):

View File

@ -29,7 +29,7 @@ def test_import_file_text():
shutil.copyfile(helper.get_file('valid.txt'), origin) shutil.copyfile(helper.get_file('valid.txt'), origin)
reset_hash_db() 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() restore_hash_db()
shutil.rmtree(folder) shutil.rmtree(folder)
@ -46,7 +46,7 @@ def test_import_file_audio():
shutil.copyfile(helper.get_file('audio.m4a'), origin) shutil.copyfile(helper.get_file('audio.m4a'), origin)
reset_hash_db() 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() restore_hash_db()
shutil.rmtree(folder) shutil.rmtree(folder)
@ -62,7 +62,7 @@ def test_import_file_photo():
shutil.copyfile(helper.get_file('plain.jpg'), origin) shutil.copyfile(helper.get_file('plain.jpg'), origin)
reset_hash_db() 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() restore_hash_db()
shutil.rmtree(folder) shutil.rmtree(folder)
@ -78,7 +78,7 @@ def test_import_file_video():
shutil.copyfile(helper.get_file('video.mov'), origin) shutil.copyfile(helper.get_file('video.mov'), origin)
reset_hash_db() 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() restore_hash_db()
shutil.rmtree(folder) 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 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(): def test_update_location_on_audio():
temporary_folder, folder = helper.create_working_folder() temporary_folder, folder = helper.create_working_folder()
temporary_folder_destination, folder_destination = helper.create_working_folder() temporary_folder_destination, folder_destination = helper.create_working_folder()