Skip to content

Commit

Permalink
Tests working for #934
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Jan 20, 2023
1 parent e88cfff commit a8d9237
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
23 changes: 21 additions & 2 deletions examples/post_function_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import typing as t
import photoscript
import pathlib
from osxphotos.cli.import_cli import ReportRecord


def post_function(
photo: photoscript.Photo, filepath: pathlib.Path, verbose: t.Callable, **kwargs
photo: photoscript.Photo,
filepath: pathlib.Path,
verbose: t.Callable,
report_record: ReportRecord,
**kwargs,
):
"""Call this with osxphotos import /file/to/import --post-function post_function.py::post_function
This will get called immediately after the photo has been imported into Photos
Expand All @@ -15,6 +21,7 @@ def post_function(
photo: photoscript.Photo instance for the photo that's just been imported
filepath: pathlib.Path to the file that was imported (this is the path to the source file, not the path inside the Photos library)
verbose: A function to print verbose output if --verbose is set; if --verbose is not set, acts as a no-op (nothing gets printed)
report_record: ReportRecord instance for the photo that's just been imported; update this if you want to change the report output
**kwargs: reserved for future use; recommend you include **kwargs so your function still works if additional arguments are added in future versions
Notes:
Expand All @@ -25,4 +32,16 @@ def post_function(

# add a note to the photo's description
verbose("Adding note to description")
photo.description = f"{photo.description} (imported with osxphotos)"
description = photo.description
description = (
f"{description} (imported with osxphotos)"
if description
else "(imported with osxphotos)"
)

# update report_record if you modify the photo and want the report to reflect the change
# the report_record object passed to the function is mutable so you can update it directly
report_record.description = description

# update the photo's description via the Photo object
photo.description = description
37 changes: 22 additions & 15 deletions osxphotos/cli/import_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ def check_templates_and_exit(

@dataclass
class ReportRecord:
"""Dataclass that records metadata on each file imported for writing to report"""

albums: List[str] = field(default_factory=list)
description: str = ""
error: bool = False
Expand Down Expand Up @@ -663,6 +665,10 @@ def update_report_record(report_record: ReportRecord, photo: Photo, filepath: Pa
report_record.filepath = filepath
report_record.uuid = photo.uuid

# location may be an empty tuple if it wasn't set
if not report_record.location:
report_record.location = photo.location

return report_record


Expand Down Expand Up @@ -1498,23 +1504,24 @@ def import_cli(
report_data[filepath] = ReportRecord(
filepath=filepath, filename=filepath.name
)
report_record = report_data[filepath]
photo, error = import_photo(filepath, dup_check, verbose)
if error:
error_count += 1
report_data[filepath].error = True
report_record.error = True
continue
report_data[filepath].imported = True
report_record.imported = True
imported_count += 1

if clear_metadata:
clear_photo_metadata(photo, filepath, verbose)
report_data[filepath].title = ""
report_data[filepath].description = ""
report_data[filepath].keywords = []
report_record.title = ""
report_record.description = ""
report_record.keywords = []

if clear_location:
clear_photo_location(photo, filepath, verbose)
report_data[filepath].location = (None, None)
report_record.location = (None, None)

if exiftool:
metadata = set_photo_metadata_from_exiftool(
Expand All @@ -1524,10 +1531,10 @@ def import_cli(
merge_keywords,
verbose,
)
report_data[filepath].update_from_metadata(metadata)
report_record.update_from_metadata(metadata)

if title:
report_data.title = set_photo_title(
report_record.title = set_photo_title(
photo,
filepath,
relative_filepath,
Expand All @@ -1537,7 +1544,7 @@ def import_cli(
)

if description:
report_data.description = set_photo_description(
report_record.description = set_photo_description(
photo,
filepath,
relative_filepath,
Expand All @@ -1547,7 +1554,7 @@ def import_cli(
)

if keyword:
report_data.keywords = set_photo_keywords(
report_record.keywords = set_photo_keywords(
photo,
filepath,
relative_filepath,
Expand All @@ -1558,7 +1565,7 @@ def import_cli(
)

if location:
report_data.location = set_photo_location(
report_record.location = set_photo_location(
photo, filepath, location, verbose
)

Expand All @@ -1567,7 +1574,7 @@ def import_cli(
# TODO: ReportRecord doesn't currently record date

if album:
report_data[filepath].albums = add_photo_to_albums(
report_record.albums = add_photo_to_albums(
photo,
filepath,
relative_filepath,
Expand All @@ -1582,15 +1589,15 @@ def import_cli(
# post function is tuple of (function, filename.py::function_name)
verbose(f"Calling post-function [bold]{function[1]}")
try:
function[0](photo, filepath, verbose)
function[0](photo, filepath, verbose, report_record)
except Exception as e:
rich_echo_error(
f"[error]Error running post-function [italic]{function[1]}[/italic]: {e}"
)

# update report data
update_report_record(report_data[filepath], photo, filepath)
import_db.set(str(filepath), report_data[filepath])
update_report_record(report_record, photo, filepath)
import_db.set(str(filepath), report_record)

progress.advance(task)

Expand Down
14 changes: 11 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ def get_os_version():
return (ver, major, minor)


OS_VER = get_os_version()[1]
if OS_VER == "15":
OS_VER = get_os_version()
if OS_VER[0] == "10" and OS_VER[1] == "15":
# Catalina
TEST_LIBRARY = "tests/Test-10.15.7.photoslibrary"
TEST_LIBRARY_IMPORT = TEST_LIBRARY
TEST_LIBRARY_SYNC = TEST_LIBRARY
from tests.config_timewarp_catalina import TEST_LIBRARY_TIMEWARP

TEST_LIBRARY_ADD_LOCATIONS = None
elif OS_VER[0] == "13":
# Ventura
TEST_LIBRARY = "tests/Test-13.0.0.photoslibrary"
TEST_LIBRARY_IMPORT = TEST_LIBRARY
TEST_LIBRARY_SYNC = TEST_LIBRARY
TEST_LIBRARY_TIMEWARP = None
TEST_LIBRARY_ADD_LOCATIONS = None
else:
TEST_LIBRARY = None
Expand Down Expand Up @@ -210,7 +218,7 @@ def pytest_collection_modifyitems(config, items):

if not (config.getoption("--test-import") and TEST_LIBRARY_IMPORT is not None):
skip_test_import = pytest.mark.skip(
reason="need --test-import option and MacOS Catalina to run"
reason="need --test-import option and MacOS Catalina or Ventura to run"
)
for item in items:
if "test_import" in item.keywords:
Expand Down
6 changes: 1 addition & 5 deletions tests/test_cli_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@
except FileNotFoundError:
exiftool_path = None

OS_VER = get_os_version()[1]
if OS_VER != "15":
pytest.skip(allow_module_level=True)


def prompt(message):
"""Helper function for tests that require user input"""
Expand Down Expand Up @@ -1010,7 +1006,7 @@ def test_import_post_function():
with open("foo1.py", "w") as f:
f.writelines(
[
"def foo(photo, filepath, verbose, **kwargs):\n",
"def foo(photo, filepath, verbose, report_record, **kwargs):\n",
" verbose('FOO BAR')\n",
]
)
Expand Down

0 comments on commit a8d9237

Please sign in to comment.