Added --replace-keywords to batch-edit #1018 (#1019)

This commit is contained in:
Rhet Turnbull 2023-03-14 11:58:13 -07:00 committed by GitHub
parent 3ad4c7a4cc
commit 1d1b69601f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 5 deletions

View File

@ -38,7 +38,13 @@ from .param_types import Latitude, Longitude, TemplateString
metavar="KEYWORD_TEMPLATE",
type=TemplateString(),
multiple=True,
help="Set keywords of photo. May be specified multiple times.",
help="Add keywords to photo. May be specified multiple times.",
)
@click.option(
"--replace-keywords",
is_flag=True,
help="When specified with --keyword, replace existing keywords. "
"Default is to add to existing keywords.",
)
@click.option(
"--location",
@ -59,6 +65,7 @@ def batch_edit(
title,
description,
keyword,
replace_keywords,
location,
dry_run,
undo,
@ -77,11 +84,11 @@ def batch_edit(
--verbose \\
--title "California vacation 2023 {created.year}-{created.dd}-{created.mm} {counter:03d}" \\
--description "{place.name}" \\
--keyword "Family" --keyword "Travel" --keyword "{keyword}"
--keyword "Family" --keyword "Travel"
This will set the title to "California vacation 2023 2023-02-20 001", and so on,
the description to the reverse geolocation place name,
and the keywords to "Family", "Travel", and any existing keywords of the photo.
and add the keywords to "Family", "Travel".
--title, --description, and --keyword may be any valid template string.
See https://rhettbull.github.io/osxphotos/template_help.html
@ -103,6 +110,13 @@ def batch_edit(
)
sys.exit(1)
if replace_keywords and not keyword:
echo_error(
"[error] Cannot specify --replace-keywords without --keyword. "
"Use --help for more information."
)
sys.exit(1)
if not photos:
echo_error("[error] No photos selected")
sys.exit(1)
@ -124,7 +138,7 @@ def batch_edit(
save_photo_undo_info(undo_store, photo)
set_photo_title_from_template(photo, title, dry_run)
set_photo_description_from_template(photo, description, dry_run)
set_photo_keywords_from_template(photo, keyword, dry_run)
set_photo_keywords_from_template(photo, keyword, replace_keywords, dry_run)
set_photo_location(photo, location, dry_run)
@ -245,7 +259,10 @@ def set_photo_description_from_template(
def set_photo_keywords_from_template(
photo: osxphotos.PhotoInfo, keyword_template: list[str], dry_run: bool
photo: osxphotos.PhotoInfo,
keyword_template: list[str],
replace_keywords: bool,
dry_run: bool,
):
"""Set photo keywords from template"""
if not keyword_template:
@ -267,6 +284,9 @@ def set_photo_keywords_from_template(
)
return
if not replace_keywords:
keywords.update(photo.keywords)
verbose(
f"Setting [i]keywords[/] to {', '.join(f'[bold]{kw}[/]' for kw in keywords)}"
)

View File

@ -131,6 +131,7 @@ def test_batch_edit_undo(photoslib):
"Test",
"--keyword",
"test",
"--replace-keywords",
"--location",
"34.052235",
"-118.243683",
@ -164,3 +165,56 @@ def test_batch_edit_undo(photoslib):
assert photo.description == "Pumpkin Farm"
assert photo.keywords == ["kids"]
assert photo.location == (41.256566, -95.940257)
@pytest.mark.test_batch_edit
def test_batch_edit_replace_keywords(photoslib):
"""Test batch-edit command with --replace-keywords"""
photo = photoslib.selection[0]
assert photo.uuid == TEST_DATA_BATCH_EDIT["uuid"]
photo.title = "Pumpkin Farm"
photo.description = "Pumpkin Farm"
photo.keywords = ["kids"]
with CliRunner().isolated_filesystem():
# First test that omitting --replace-keywords adds keywords
result = CliRunner().invoke(
batch_edit,
[
"--keyword",
"test",
],
)
assert result.exit_code == 0
photo = osxphotos.PhotosDB().get_photo(TEST_DATA_BATCH_EDIT["uuid"])
assert sorted(photo.keywords) == ["kids", "test"]
result = CliRunner().invoke(
batch_edit,
[
"--keyword",
"test2",
"--replace-keywords",
],
)
assert result.exit_code == 0
photo = osxphotos.PhotosDB().get_photo(TEST_DATA_BATCH_EDIT["uuid"])
assert photo.keywords == ["test2"]
@pytest.mark.test_batch_edit
def test_batch_edit_replace_keywords_error(photoslib):
"""Test batch-edit command with --replace-keywords when no keywords specified"""
photo = photoslib.selection[0]
assert photo.uuid == TEST_DATA_BATCH_EDIT["uuid"]
with CliRunner().isolated_filesystem():
result = CliRunner().invoke(
batch_edit,
[
"--title",
"test",
"--replace-keywords",
],
)
assert result.exit_code != 0