Added ability to show from export filepath to osxphotos show (#995)

This commit is contained in:
Rhet Turnbull
2023-02-20 08:44:17 -08:00
committed by GitHub
parent 1209d98e6e
commit d3267cb0f0
4 changed files with 35 additions and 2 deletions

View File

@@ -119,7 +119,7 @@ def rich_echo(
# if not outputting to terminal, use a huge width to avoid wrapping # if not outputting to terminal, use a huge width to avoid wrapping
# otherwise tests fail # otherwise tests fail
width = 10_000 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: if markdown:
message = Markdown(message) message = Markdown(message)
# Markdown always adds a new line so disable unless explicitly specified # Markdown always adds a new line so disable unless explicitly specified

View File

@@ -1,10 +1,12 @@
"""osxphotos show command""" """osxphotos show command"""
import pathlib
import re import re
import click import click
from osxphotos._constants import UUID_PATTERN from osxphotos._constants import UUID_PATTERN
from osxphotos.export_db_utils import get_uuid_for_filepath
from osxphotos.photoscript_utils import ( from osxphotos.photoscript_utils import (
photoscript_object_from_name, photoscript_object_from_name,
photoscript_object_from_uuid, photoscript_object_from_uuid,
@@ -34,6 +36,15 @@ def show(ctx, db, uuid_or_name):
osxphotos show IMG_1234.JPG 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: Notes:
This command requires Photos library version 5 or higher. 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}[/]" f"Found [filename]{obj_type}[/] with name: [filepath]{uuid_or_name}[/]"
) )
obj.spotlight() 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: else:
raise ValueError( raise ValueError(
f"could not find asset with name [filepath]{uuid_or_name}[/]" f"could not find asset with name [filepath]{uuid_or_name}[/]"

View File

@@ -103,7 +103,7 @@ class ExportDB:
@retry(stop=stop_after_attempt(MAX_RETRY_ATTEMPTS)) @retry(stop=stop_after_attempt(MAX_RETRY_ATTEMPTS))
def get_file_record(self, filename: Union[pathlib.Path, str]) -> "ExportRecord": 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 Returns: an ExportRecord object or None if filename not found
""" """

View File

@@ -36,6 +36,8 @@ __all__ = [
"export_db_touch_files", "export_db_touch_files",
"export_db_update_signatures", "export_db_update_signatures",
"export_db_vacuum", "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: if fname.is_file() and fname.name == OSXPHOTOS_EXPORT_DB:
return str(fname) return str(fname)
return "" 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 ""