From 8e9691d6d72399cb497573ad0b9dc43939d29084 Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 16 May 2020 06:48:23 -0700 Subject: [PATCH] Made --exiftool and --export-as-hardlink incompatible in CLI to fix #132 --- osxphotos/__main__.py | 16 ++++++++++++---- tests/test_cli.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/osxphotos/__main__.py b/osxphotos/__main__.py index 240b9988..4a9d2530 100644 --- a/osxphotos/__main__.py +++ b/osxphotos/__main__.py @@ -21,7 +21,10 @@ import osxphotos from ._constants import _EXIF_TOOL_URL, _PHOTOS_4_VERSION, _UNKNOWN_PLACE from ._version import __version__ from .exiftool import get_exiftool_path -from .photoinfo.template import TEMPLATE_SUBSTITUTIONS, TEMPLATE_SUBSTITUTIONS_MULTI_VALUED +from .photoinfo.template import ( + TEMPLATE_SUBSTITUTIONS, + TEMPLATE_SUBSTITUTIONS_MULTI_VALUED, +) from .utils import _copy_file, create_path_by_date @@ -89,7 +92,7 @@ class ExportCommand(click.Command): ) formatter.write("\n") formatter.write_text( - "The templating system may also be used with the --keyword-template option " + "The templating system may also be used with the --keyword-template option " + "to set keywords on export (with --exiftool or --sidecar), " + "for example, to set a new keyword in format 'folder/subfolder/album' to " + 'preserve the folder/album structure, you can use --keyword-template "{folder_album}"' @@ -778,6 +781,7 @@ def query( ] # print help if no non-exclusive term or a double exclusive term is given if not any(nonexclusive + [b ^ n for b, n in exclusive]): + click.echo("Incompatible query options", err=True) click.echo(cli.commands["query"].get_help(ctx), err=True) return @@ -863,7 +867,8 @@ def query( @click.option( "--export-as-hardlink", is_flag=True, - help="Hardlink files instead of copying them. ", + help="Hardlink files instead of copying them. " + "Cannot be used with --exiftool which creates copies of the files with embedded EXIF data.", ) @click.option( "--overwrite", @@ -961,7 +966,8 @@ def query( is_flag=True, help="Use exiftool to write metadata directly to exported photos. " "To use this option, exiftool must be installed and in the path. " - "exiftool may be installed from https://exiftool.org/", + "exiftool may be installed from https://exiftool.org/. " + "Cannot be used with --export-as-hardlink.", ) @click.option( "--directory", @@ -1082,9 +1088,11 @@ def export( (selfie, not_selfie), (panorama, not_panorama), (export_by_date, directory), + (export_as_hardlink, exiftool), (any(place), no_place), ] if any([all(bb) for bb in exclusive]): + click.echo("Incompatible export options",err=True) click.echo(cli.commands["export"].get_help(ctx), err=True) return diff --git a/tests/test_cli.py b/tests/test_cli.py index 94c397e4..5cc08ce1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -243,7 +243,7 @@ def test_export(): assert sorted(files) == sorted(CLI_EXPORT_FILENAMES) -def test_export_using_hardlinks(): +def test_export_as_hardlink(): import glob import os import os.path @@ -263,7 +263,7 @@ def test_export_using_hardlinks(): assert sorted(files) == sorted(CLI_EXPORT_FILENAMES) -def test_export_using_hardlinks_samefile(): +def test_export_as_hardlink_samefile(): # test that --export-as-hardlink actually creates a hardlink # src and dest should be same file import os @@ -291,7 +291,33 @@ def test_export_using_hardlinks_samefile(): assert os.path.exists(CLI_EXPORT_UUID_FILENAME) assert os.path.samefile(CLI_EXPORT_UUID_FILENAME, photo.path) +def test_export_using_hardlinks_incompat_options(): + # test that error shown if --export-as-hardlink used with --exiftool + import os + import osxphotos + from osxphotos.__main__ import export + runner = CliRunner() + cwd = os.getcwd() + photosdb = osxphotos.PhotosDB(dbfile=CLI_PHOTOS_DB) + photo = photosdb.photos(uuid=[CLI_EXPORT_UUID])[0] + + # pylint: disable=not-context-manager + with runner.isolated_filesystem(): + result = runner.invoke( + export, + [ + os.path.join(cwd, CLI_PHOTOS_DB), + ".", + f"--uuid={CLI_EXPORT_UUID}", + "--export-as-hardlink", + "--exiftool", + "-V", + ], + ) + assert result.exit_code == 0 + assert "Incompatible export options" in result.output + def test_export_current_name(): import glob import os