Feature timewarp function (#678)

* Added constraints, fixed themes for timewarp

* Updated progress to use rich_progress

* Added --function to timewarp, #676
This commit is contained in:
Rhet Turnbull
2022-05-01 22:22:01 -07:00
committed by GitHub
parent 25f35699d8
commit 70d3247e8c
8 changed files with 307 additions and 31 deletions

View File

@@ -1,11 +1,20 @@
""" Test data for timewarp command on Catalina/Photos 5 """
import datetime
import pathlib
from tests.parse_timewarp_output import CompareValues, InspectValues
TEST_LIBRARY_TIMEWARP = "tests/TestTimeWarp-10.15.7.photoslibrary"
def get_file_timestamp(file: str) -> str:
"""Get timestamp of file"""
return datetime.datetime.fromtimestamp(pathlib.Path(file).stat().st_mtime).strftime(
"%Y-%m-%d %H:%M:%S"
)
CATALINA_PHOTOS_5 = {
"filenames": {
"pumpkins": "IMG_6522.jpeg",
@@ -258,7 +267,9 @@ CATALINA_PHOTOS_5 = {
"post": CompareValues(
"IMG_6506.jpeg",
"7E9DF2EE-A5B0-4077-80EC-30565221A3B9",
"2021-10-08 16:11:09",
get_file_timestamp(
f"{TEST_LIBRARY_TIMEWARP}/originals/7/7E9DF2EE-A5B0-4077-80EC-30565221A3B9.jpeg"
),
"",
"-0700",
"",
@@ -302,7 +313,7 @@ CATALINA_PHOTOS_5 = {
"parameters": [("-0200", "2021-10-04 23:00:00-0200")]
},
"video_push_exif": {
# IMG_6501.jpeg
# IMG_6551.mov
"pre": CompareValues(
"IMG_6551.mov",
"16BEC0BE-4188-44F1-A8F1-7250E978AD12",
@@ -321,7 +332,7 @@ CATALINA_PHOTOS_5 = {
),
},
"video_pull_exif": {
# IMG_6501.jpeg
# IMG_6551.jpeg
"pre": CompareValues(
"IMG_6551.mov",
"16BEC0BE-4188-44F1-A8F1-7250E978AD12",
@@ -339,4 +350,16 @@ CATALINA_PHOTOS_5 = {
"-0200",
),
},
"function": {
# IMG_6501.jpeg
"uuid": "2F00448D-3C0D-477A-9B10-5F21DCAB405A",
"expected": InspectValues(
"IMG_6501.jpeg",
"2F00448D-3C0D-477A-9B10-5F21DCAB405A",
"2020-09-01 18:53:02-0700",
"2020-09-01 18:53:02-0700",
"-0700",
"GMT-0700",
),
},
}

View File

@@ -46,7 +46,7 @@ def ask_user_to_make_selection(
video: set to True if asking for a video instead of a photo
"""
# needs to be called with a suspend_capture fixture
photo_or_video = "photo" if not video else "video"
photo_or_video = "video" if video else "photo"
tries = 0
while tries < retry:
with suspend_capture:
@@ -935,3 +935,35 @@ def test_video_pull_exif(photoslib, suspend_capture, output_file):
)
output_values = parse_compare_exif(output_file)
assert output_values[0] == post_test
@pytest.mark.timewarp
def test_select_pears_3(photoslib, suspend_capture):
"""Force user to select the right photo for following tests"""
assert ask_user_to_make_selection(photoslib, suspend_capture, "pears")
@pytest.mark.timewarp
def test_function(photoslib, suspend_capture, output_file):
"""Test timewarp function"""
from osxphotos.cli.timewarp import timewarp
expected = TEST_DATA["function"]["expected"]
runner = CliRunner()
result = runner.invoke(
timewarp,
[
"--function",
"tests/timewarp_function_example.py::get_date_time_timezone",
],
terminal_width=TERMINAL_WIDTH,
)
assert result.exit_code == 0
result = runner.invoke(
timewarp,
["--inspect", "--plain", "-o", output_file],
terminal_width=TERMINAL_WIDTH,
)
output_values = parse_inspect_output(output_file)
assert output_values[0] == expected

View File

@@ -0,0 +1,46 @@
"""Example function for use with `osxphotos timewarp --function`
Call this as: `osxphotos timewarp --function timewarp_function_example.py::get_date_time_timezone`
"""
from datetime import datetime, timedelta
from typing import Callable, Optional, Tuple
from photoscript import Photo
def get_date_time_timezone(
photo: Photo, path: Optional[str], tz_sec: int, tz_name: str, verbose: Callable
) -> Tuple[datetime, int]:
"""Example function for use with `osxphotos timewarp --function`
Args:
photo: Photo object
path: path to photo, which may be None if photo is not on disk
tz_sec: timezone offset from UTC in seconds
tz_name: timezone name
verbose: function to print verbose messages
Returns:
tuple of (new date/time as datetime.datetime, and new timezone offset from UTC in seconds as int)
"""
# this example adds 3 hours to the date/time and subtracts 1 hour from the timezone
# the photo's date/time can be accessed as photo.date
# photo.date is a datetime.datetime object
# the date/time is naive (timezone unaware) as will be in local timezone
date = photo.date
# add 3 hours
date = date + timedelta(hours=3)
# subtract 1 hour from timezone
timezone = tz_sec - 3600
# verbose(msg) prints a message to the console if user used --verbose option
# otherwise it does nothing
# photo's filename can be access as photo.filename
verbose(f"Updating {photo.filename} date/time: {date} and timezone: {timezone}")
return date, timezone