Implemented show command, #964 (#982)

This commit is contained in:
Rhet Turnbull
2023-02-12 08:11:38 -08:00
committed by GitHub
parent ce297ced0a
commit b03670dc70
11 changed files with 353 additions and 24 deletions

View File

@@ -79,6 +79,7 @@ from .photo_inspect import photo_inspect
from .places import places
from .query import query
from .repl import repl
from .show_command import show
from .snap_diff import diff, snap
from .sync import sync
from .theme import theme
@@ -125,6 +126,7 @@ __all__ = [
"run",
"selection_command",
"set_debug",
"show",
"snap",
"tutorial",
"uuid",

View File

@@ -35,6 +35,7 @@ from .photo_inspect import photo_inspect
from .places import places
from .query import query
from .repl import repl
from .show_command import show
from .snap_diff import diff, snap
from .sync import sync
from .theme import theme
@@ -130,6 +131,7 @@ for command in [
query,
repl,
run,
show,
snap,
sync,
theme,

View File

@@ -27,10 +27,15 @@ from .cli_params import (
)
from .click_rich_echo import rich_click_echo as echo
from .click_rich_echo import rich_echo_error as echo_error
from .click_rich_echo import set_rich_theme
from .color_themes import get_theme
from .verbose import verbose, verbose_print
logger = logging.getLogger("osxphotos")
# ensure echo, echo_error are configured with correct theme
set_rich_theme(get_theme())
__all__ = [
"abort",
"echo",

View File

@@ -14,7 +14,7 @@ from typing import Dict
import click
from osxphotos import PhotosDB
from osxphotos._constants import _PHOTOS_4_VERSION
from osxphotos._constants import _PHOTOS_4_VERSION, UUID_PATTERN
from osxphotos.fileutil import FileUtil
from osxphotos.utils import increment_filename, pluralize
@@ -131,8 +131,7 @@ def scan_for_files(directory: str, uuid_dict: Dict):
Note: modifies uuid_dict
"""
uuid_pattern = r"([0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})"
uuid_regex = re.compile(uuid_pattern)
uuid_regex = re.compile(UUID_PATTERN)
for dirpath, dirname, filenames in os.walk(directory):
for filename in filenames:
if match := uuid_regex.match(filename):

View File

@@ -0,0 +1,74 @@
"""osxphotos show command"""
import re
import click
from osxphotos._constants import UUID_PATTERN
from osxphotos.photoscript_utils import (
photoscript_object_from_name,
photoscript_object_from_uuid,
)
from osxphotos.photosdb.photosdb_utils import get_photos_library_version
from osxphotos.utils import get_last_library_path
from .cli_commands import echo, echo_error
from .cli_params import DB_OPTION
from .click_rich_echo import set_rich_theme
@click.command(name="show")
@DB_OPTION
@click.argument("uuid_or_name", metavar="UUID_OR_NAME", nargs=1, required=True)
@click.pass_context
def show(ctx, db, uuid_or_name):
"""Show photo, album, or folder in Photos from UUID_OR_NAME
Examples:
osxphotos show 12345678-1234-1234-1234-123456789012
osxphotos show "My Album"
osxphotos show "My Folder"
osxphotos show IMG_1234.JPG
Notes:
This command requires Photos library version 5 or higher.
Currently this command cannot be used to show subfolders in Photos.
"""
db = db or get_last_library_path()
if not db:
echo(
"Could not find Photos library. Use --library/--db to specify path to Photos library."
)
ctx.exit(1)
if get_photos_library_version(db) < 5:
echo_error("[error]show command requires Photos library version 5 or higher")
ctx.exit(1)
try:
if re.match(UUID_PATTERN, uuid_or_name):
if not (obj := photoscript_object_from_uuid(uuid_or_name, db)):
raise ValueError(
f"could not find asset with UUID [uuid]{uuid_or_name}[/]"
)
obj_type = obj.__class__.__name__
echo(f"Found [filename]{obj_type}[/] with UUID: [uuid]{uuid_or_name}[/]")
obj.spotlight()
elif obj := photoscript_object_from_name(uuid_or_name, db):
obj_type = obj.__class__.__name__
echo(
f"Found [filename]{obj_type}[/] with name: [filepath]{uuid_or_name}[/]"
)
obj.spotlight()
else:
raise ValueError(
f"could not find asset with name [filepath]{uuid_or_name}[/]"
)
except Exception as e:
echo_error(f"[error]Error finding asset [uuid]{uuid_or_name}[/]: {e}")
ctx.exit(1)