diff --git a/osxphotos/cli/click_rich_echo.py b/osxphotos/cli/click_rich_echo.py index df1514f1..727b06df 100644 --- a/osxphotos/cli/click_rich_echo.py +++ b/osxphotos/cli/click_rich_echo.py @@ -119,7 +119,7 @@ def rich_echo( # if not outputting to terminal, use a huge width to avoid wrapping # otherwise tests fail width = 10_000 - console = get_rich_console() or Console(theme or get_rich_theme(), width=width) + console = get_rich_console() or Console(theme=theme or get_rich_theme(), width=width) if markdown: message = Markdown(message) # Markdown always adds a new line so disable unless explicitly specified diff --git a/osxphotos/cli/show_command.py b/osxphotos/cli/show_command.py index c5ed5bc5..63660eaf 100644 --- a/osxphotos/cli/show_command.py +++ b/osxphotos/cli/show_command.py @@ -1,10 +1,12 @@ """osxphotos show command""" +import pathlib import re import click from osxphotos._constants import UUID_PATTERN +from osxphotos.export_db_utils import get_uuid_for_filepath from osxphotos.photoscript_utils import ( photoscript_object_from_name, photoscript_object_from_uuid, @@ -34,6 +36,15 @@ def show(ctx, db, uuid_or_name): osxphotos show IMG_1234.JPG + show can also be used to show a photo exported with `osxphotos export`: + + osxphotos show /path/to/exported/photo.jpg + + In this case, the UUID_OR_NAME is the path to the exported photo and osxphotos + will attempt to find the export database to match the photo to the original in + Photos. If your export database is not in the default location in the root of the + export directory, this will not work. + Notes: This command requires Photos library version 5 or higher. @@ -65,6 +76,16 @@ def show(ctx, db, uuid_or_name): f"Found [filename]{obj_type}[/] with name: [filepath]{uuid_or_name}[/]" ) obj.spotlight() + elif uuid := get_uuid_for_filepath(pathlib.Path(uuid_or_name).resolve()): + if not (obj := photoscript_object_from_uuid(uuid, db)): + raise ValueError( + f"could not find asset with UUID [uuid]{uuid}[/] for file [filepath]{uuid_or_name}[/]" + ) + obj_type = obj.__class__.__name__ + echo( + f"Found [filename]{obj_type}[/] from export database: [filepath]{uuid_or_name}[/]" + ) + obj.spotlight() else: raise ValueError( f"could not find asset with name [filepath]{uuid_or_name}[/]" diff --git a/osxphotos/export_db.py b/osxphotos/export_db.py index 11dbfc4e..4e733947 100644 --- a/osxphotos/export_db.py +++ b/osxphotos/export_db.py @@ -103,7 +103,7 @@ class ExportDB: @retry(stop=stop_after_attempt(MAX_RETRY_ATTEMPTS)) def get_file_record(self, filename: Union[pathlib.Path, str]) -> "ExportRecord": - """get info for filename and uuid + """get info for filename Returns: an ExportRecord object or None if filename not found """ diff --git a/osxphotos/export_db_utils.py b/osxphotos/export_db_utils.py index 43dc5a3b..2d25d550 100644 --- a/osxphotos/export_db_utils.py +++ b/osxphotos/export_db_utils.py @@ -36,6 +36,8 @@ __all__ = [ "export_db_touch_files", "export_db_update_signatures", "export_db_vacuum", + "find_export_db_for_filepath", + "get_uuid_for_filepath", ] @@ -559,3 +561,13 @@ def find_export_db_for_filepath(filepath: Union[str, pathlib.Path]) -> str: if fname.is_file() and fname.name == OSXPHOTOS_EXPORT_DB: return str(fname) return "" + + +def get_uuid_for_filepath(filepath: Union[str, pathlib.Path]) -> str: + """Find the UUID for a given filepath, traversing the directory tree to find the export database""" + filepath = pathlib.Path(filepath) + if export_db_path := find_export_db_for_filepath(filepath): + export_root = pathlib.Path(export_db_path).parent + exportdb = ExportDB(export_db_path, export_root) + return record.uuid if (record := exportdb.get_file_record(filepath)) else "" + return ""