Skip to content

Commit

Permalink
timewarp --inspect and --compare-exif are mutually exclusive. Side no…
Browse files Browse the repository at this point in the history
…te: Would it be best not to generate osxphotos_crash.log file for such situation (a bit over my level to make that change ;) )

Refactor compare_exif_no_markup and compare_exif_with_markup into timewarp_compare_exif
  • Loading branch information
oPromessa committed Sep 22, 2024
1 parent a6e4d5e commit ca7bb32
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 66 deletions.
9 changes: 4 additions & 5 deletions osxphotos/cli/timewarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ def timewarp(
"must be specified."
)

if inspect and compare_exif:
raise click.UsageError("--inspect and --compare-exif are mutually exclusive.")

if date and date_delta:
raise click.UsageError("--date and --date-delta are mutually exclusive.")

Expand Down Expand Up @@ -600,11 +603,7 @@ def timewarp(
)
for photo in photos:
set_crash_data("photo", f"{photo.uuid} {photo.filename}")
diff_results = (
photocomp.compare_exif_no_markup(photo)
if plain
else photocomp.compare_exif_with_markup(photo)
)
diff_results = photocomp.timewarp_compare_exif(photo, plain)

if not plain:
filename = (
Expand Down
89 changes: 28 additions & 61 deletions osxphotos/compare_exif.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,79 +87,45 @@ def compare_exif(self, photo: Photo) -> List[str]:

return [photos_date_str, photos_tz_str, exif_date, exif_offset]

def compare_exif_with_markup(self, photo: Photo) -> ExifDiff:
def timewarp_compare_exif(self, photo: Photo, plain: bool = False) -> ExifDiff:
"""Compare date/time/timezone in Photos to the exif data and return an ExifDiff named tuple;
adds rich markup to strings to show differences
optionally adds rich markup to strings to show differences.
Args:
photo (Photo): Photo object to compare
"""
plain (bool): Flag to determine if plain (True) or markup (False) should be applied
"""
def compare_values(photo_value: str, exif_value: str) -> tuple:
"""Compare two values and return them with or without markup.
Affects nonlocal variable diff (from timewarp_compare_exif) with result.
"""

nonlocal diff
if photo_value != exif_value:
diff = True
if not plain:
return change(photo_value), change(exif_value)
else:
if not plain:
return no_change(photo_value), no_change(exif_value)
return photo_value, exif_value

# Get values from comparison function
photos_date, photos_tz, exif_date, exif_tz = self.compare_exif(photo)
diff = False
photos_date, photos_time = photos_date.split(" ", 1)
try:
exif_date, exif_time = exif_date.split(" ", 1)
except ValueError:
exif_date = exif_date
exif_time = ""

if photos_date != exif_date:
photos_date = change(photos_date)
exif_date = change(exif_date)
diff = True
else:
photos_date = no_change(photos_date)
exif_date = no_change(exif_date)

if photos_time != exif_time:
photos_time = change(photos_time)
exif_time = change(exif_time)
diff = True
else:
photos_time = no_change(photos_time)
exif_time = no_change(exif_time)

if photos_tz != exif_tz:
photos_tz = change(photos_tz)
exif_tz = change(exif_tz)
diff = True
else:
photos_tz = no_change(photos_tz)
exif_tz = no_change(exif_tz)

return ExifDiff(
diff,
photos_date,
photos_time,
photos_tz,
exif_date,
exif_time,
exif_tz,
)

def compare_exif_no_markup(self, photo: Photo) -> ExifDiff:
"""Compare date/time/timezone in Photos to the exif data and return an ExifDiff named tuple;

Args:
photo (Photo): Photo object to compare
"""
photos_date, photos_tz, exif_date, exif_tz = self.compare_exif(photo)
diff = False
# Split date and time
photos_date, photos_time = photos_date.split(" ", 1)
try:
exif_date, exif_time = exif_date.split(" ", 1)
except ValueError:
exif_date = exif_date
exif_time = ""

if photos_date != exif_date:
diff = True

if photos_time != exif_time:
diff = True
exif_time = "" # Handle missing time in exif_date

if photos_tz != exif_tz:
diff = True
# Compare dates, times, and timezones
photos_date, exif_date = compare_values(photos_date, exif_date)
photos_time, exif_time = compare_values(photos_time, exif_time)
photos_tz, exif_tz = compare_values(photos_tz, exif_tz)

return ExifDiff(
diff,
Expand All @@ -170,3 +136,4 @@ def compare_exif_no_markup(self, photo: Photo) -> ExifDiff:
exif_time,
exif_tz,
)

0 comments on commit ca7bb32

Please sign in to comment.