Skip to content

Commit

Permalink
Added --dry-run to push-exif, #1259 (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull authored Nov 20, 2023
1 parent 4a8fd58 commit e5aa6b5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
66 changes: 40 additions & 26 deletions osxphotos/cli/push_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@


@query_command(name="push-exif")
@click.option(
"--compare",
is_flag=True,
help="Compare metadata only; do not push (write) metadata.",
)
@click.option(
"--push-edited",
is_flag=True,
Expand Down Expand Up @@ -153,6 +148,16 @@
hidden=OSXPHOTOS_HIDDEN,
help="Force writing metadata to all files and bypass warning.",
)
@click.option(
"--compare",
is_flag=True,
help="Compare metadata only; do not push (write) metadata.",
)
@click.option(
"--dry-run",
is_flag=True,
help="Dry run mode; show what would be done but do not actually write to any files.",
)
@click.argument(
"metadata",
required=True,
Expand All @@ -171,23 +176,24 @@
)
def push_exif(
photos: list[PhotoInfo],
append: bool,
compare: bool,
push_edited: bool,
exiftool_path: str,
description_template: str,
dry_run: bool,
exiftool_flags: tuple[str],
merge_exif_keywords: bool,
merge_exif_persons: bool,
exiftool_path: str,
favorite_rating: bool,
force: bool,
ignore_date_modified: bool,
use_persons_as_keywords: bool,
use_albums_as_keywords: bool,
keyword_template: tuple[str],
merge_exif_keywords: bool,
merge_exif_persons: bool,
metadata: str,
push_edited: bool,
replace_keywords: bool,
description_template: str,
report: str,
append: bool,
force: bool,
metadata: str,
use_albums_as_keywords: bool,
use_persons_as_keywords: bool,
**kwargs,
):
"""Write photo metadata to original files in the Photos library
Expand Down Expand Up @@ -244,7 +250,9 @@ def push_exif(
If you want to compare metadata between Photos and the original files without
writing metadata, use the `--compare` option. This will print a report of any
differences between the metadata in Photos and the original files but will not
modify any files.
modify any files. You may also use the `--dry-run` option to see what would be
done without actually writing any files. This is useful for testing the command
before actually writing metadata to files.
push-exif cannot be used on photos in classic shared albums. These photos will
be automatically skipped.
Expand Down Expand Up @@ -291,7 +299,7 @@ def push_exif(
if not force:
click.confirm("Are you sure you want to continue?", abort=True)

process_photos(photos, options, push_edited, report, append, exiftool_path)
process_photos(photos, options, push_edited, report, append, exiftool_path, dry_run)


def process_photos(
Expand All @@ -301,6 +309,7 @@ def process_photos(
report: str,
append: bool,
exiftool_path: str,
dry_run: bool,
):
"""Process the photos, pushing metadata to files as needed"""

Expand All @@ -312,7 +321,7 @@ def process_photos(

report_writer = (
push_exif_report_writer_factory(report_name, append)
if report
if report and not dry_run
else ReportWriterNoOp()
)

Expand All @@ -329,7 +338,9 @@ def process_photos(
f"[filename]{photo.original_filename}[/] ([uuid]{photo.uuid}[/])"
)
progress.print(f"Processing {photo_str}")
photo_results = process_photo(photo, options, push_edited, update_db)
photo_results = process_photo(
photo, options, push_edited, update_db, dry_run
)
results[photo.uuid] = photo_results
report_writer.write(photo.uuid, photo.original_filename, photo_results)
progress.advance(task)
Expand All @@ -346,6 +357,7 @@ def process_photo(
options: ExifOptions,
push_edited: bool,
update_db: SQLiteKVStore,
dry_run: bool,
) -> PushResults:
"""
Process a photo, writing metadata to files as needed
Expand All @@ -355,6 +367,7 @@ def process_photo(
options (ExifOptions): The options to use when writing metadata
push_edited (bool): Whether to write metadata to edited photos
update_db (SQLiteKVStore): The database to use for storing metadata
dry_run (bool): Whether to do a dry run (don't actually write metadata)
Returns:
PushResults: The results of processing the photo
Expand Down Expand Up @@ -412,13 +425,14 @@ def process_photo(
f"Writing metadata to [filepath]{filepath}[/] (not previously written)"
)
results.written.append(filepath)
warnings, errors = write_exif(photo, filepath, options)
if not errors:
update_db[photo_key] = exif_sidecar
else:
results.error.append((filepath, errors))
if warnings:
results.warning.append((filepath, warnings))
if not dry_run:
warnings, errors = write_exif(photo, filepath, options)
if not errors:
update_db[photo_key] = exif_sidecar
else:
results.error.append((filepath, errors))
if warnings:
results.warning.append((filepath, warnings))
return results


Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli_push_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ def test_cli_push_exif_basic(monkeypatch):
assert sorted(photo.persons) == get_exiftool_persons(photo)


def test_cli_push_exif_dry_run(monkeypatch):
"""Test push-exif command with --dry-run"""
runner = CliRunner()
with runner.isolated_filesystem():
cwd = pathlib.Path(os.getcwd())

if sys.version_info[0:2] <= (3, 9):
monkeypatch.setattr("xdg.xdg_data_home", lambda: cwd)
else:
monkeypatch.setattr("xdg_base_dirs.xdg_data_home", lambda: cwd)

test_library = copy_photos_library(os.path.join(cwd, "Test.photoslibrary"))
result = runner.invoke(
push_exif, ["all", "-V", "--force", "--library", test_library, "--dry-run"]
)
assert result.exit_code == 0
assert (
"Summary: 14 written, 0 updated, 0 skipped, 3 missing, 0 warning, 0 error"
in result.output
)

# verify keywords and persons were not pushed
photosdb = PhotosDB(test_library)
photo = photosdb.get_photo(UUID_KEYWORDS_PERSONS)
assert sorted(photo.keywords) != get_exiftool_keywords(photo)
assert sorted(photo.persons) != get_exiftool_persons(photo)


def test_cli_push_exif_exiftool_option(monkeypatch):
"""Test push-exif command with --exiftool-option"""
# NOTE: Currently no photos that generate warnings in exiftool so can't test that
Expand Down

0 comments on commit e5aa6b5

Please sign in to comment.