Skip to content

Commit

Permalink
Initial reset implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Sep 19, 2024
1 parent 3dea370 commit 9e13ab4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
29 changes: 20 additions & 9 deletions osxphotos/cli/timewarp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from osxphotos.exiftool import get_exiftool_path
from osxphotos.photodates import (
get_photo_date_added,
reset_photo_date_time_tz,
set_photo_date_added,
set_photo_date_from_filename,
update_photo_date_time,
Expand Down Expand Up @@ -163,10 +164,8 @@ def get_help(self, ctx):
**Note**: The time zone of the parsed date/time is assumed to be the local time zone.
If the parse pattern includes a time zone, the photo's time will be converted from
the specified time zone to the local time zone. osxphotos import does not
currently support setting the time zone of imported photos.
See also `osxphotos help timewarp` for more information on the timewarp
command which can be used to change the time zone of photos after import.
the specified time zone to the local time zone. The timewarp command does not currently
setting the timezone when parsing the filename.
""" # noqa: E501
Expand Down Expand Up @@ -462,7 +461,10 @@ def timewarp(
if add_to_album and not compare_exif:
raise click.UsageError("--add-to-album must be used with --compare-exif.")

# ZZZ add check for --reset and Photos >= 8 , warn if not supported
if reset and float(PhotosLibrary().version) < 8.0:
raise click.UsageError(
"--reset may only be used with Photos version 8.0 and later (macOS Ventura and later)"
)

verbose = verbose_print(verbose=verbose_flag, timestamp=timestamp, theme=theme)

Expand Down Expand Up @@ -562,6 +564,12 @@ def timewarp(
library_path=library,
)

reset_photo_date_time_ = partial(
reset_photo_date_time_tz,
library_path=library,
verbose=verbose,
)

if function:
update_photo_from_function_ = partial(
update_photo_from_function,
Expand Down Expand Up @@ -650,10 +658,11 @@ def timewarp(
)
sys.exit(0)

if timezone:
tz_updater = PhotoTimeZoneUpdater(
timezone, verbose=verbose, library_path=library
)
tz_updater = (
PhotoTimeZoneUpdater(timezone, verbose=verbose, library_path=library)
if timezone
else None
)

if any([push_exif, pull_exif, function]):
# ExifDateTimeUpdater used to get photo path for --function
Expand All @@ -672,6 +681,8 @@ def timewarp(
)
for photo in photos:
set_crash_data("photo", f"{photo.uuid} {photo.filename}")
if reset:
reset_photo_date_time_(photo)
if parse_date:
set_photo_date_from_filename_(photo, photo.filename, parse_date)
if pull_exif:
Expand Down
40 changes: 35 additions & 5 deletions osxphotos/photodates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,39 @@
MACOS_TIME_EPOCH = datetime.datetime(2001, 1, 1, 0, 0, 0)


def reset_photo_date_time_tz(photo: photoscript.Photo, verbose: Callable[..., None]):
"""Reset the date/time/timezone of a photo to the original values"""
...
def reset_photo_date_time_tz(
photo: photoscript.Photo, library_path: str, verbose: Callable[..., None]
):
"""Reset the date/time/timezone of a photo to the original values
Args:
photo: photo to reset
library_path: path to the Photos library
verbose: callable to print verbose output
"""
date_original = get_photo_date_original(photo, library_path)
if not date_original:
verbose(
f"Could not get original date for photo {photo.filename} ({photo.uuid})"
)
return
date_current = get_photo_date_created(photo, library_path)
tz = date_original.tzinfo.key
tz_updater = PhotoTimeZoneUpdater(
timezone=Timezone(tz), verbose=verbose, library_path=library_path
)
tz_updater.update_photo(photo)
update_photo_date_time(
photo=photo,
date=date_original.date(),
time=date_original.time(),
date_delta=None,
time_delta=None,
verbose=verbose,
)
verbose(
f"Reset original date/time for photo [filename]{photo.filename}[/filename] "
f"([uuid]{photo.uuid}[/uuid]) from: [time]{date_current}[/time] to [time]{date_original}[/time]"
)


def update_photo_date_time(
Expand Down Expand Up @@ -370,7 +400,7 @@ def _get_photo_date_original(
wait=wait_exponential(multiplier=1, min=0.100, max=5),
stop=stop_after_attempt(5),
)
def _get_photo_date_created(
def get_photo_date_created(
photo: photoscript.Photo,
library_path: str | None = None,
) -> datetime.datetime:
Expand Down Expand Up @@ -432,6 +462,6 @@ def get_photo_date_original(
ValueError if library_path is None and Photos library path cannot be determined
FileNotFoundError if Photos database path cannot be found
"""
return _get_photo_date_original(photo, library_path) or _get_photo_date_created(
return _get_photo_date_original(photo, library_path) or get_photo_date_created(
photo, library_path
)

0 comments on commit 9e13ab4

Please sign in to comment.