From b36b7e7eb2a258f864f34363de4b6d9228ee6090 Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sun, 24 Nov 2019 20:14:59 -0800 Subject: [PATCH] Added hidden/favorite/missing to cmd_line --- README.md | 14 +++++++++- osxphotos/__init__.py | 3 +- osxphotos/cmd_line.py | 64 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f880455d..30aec30f 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,14 @@ Options: --person TEXT search for person(s) --album TEXT search for album(s) --uuid TEXT search for UUID(s) + --favorite search for photos marked favorite + --notfavorite search for photos not marked favorite + --hidden search for photos marked hidden + --nothidden search for photos not marked hidden + --missing search for photos missing from disk + --notmissing search for photos present on disk (e.g. not missing) --json Print output in JSON format - -h, --help Show this message and exit. + -h, --help Show this message and exit. ``` Example: find all photos with keyword "Kids" and output results to json file named results.json: @@ -319,6 +325,12 @@ Returns `True` if the original image file is missing on disk, otherwise `False`. #### `hasadjustments()` Returns `True` if the file has been edited in Photos, otherwise `False` +#### `favorite()` +Returns `True` if the picture has been marked as a favorite, otherwise `False` + +#### `hidden()` +Returns `True` if the picture has been marked as hidden, otherwise `False` + #### `to_json()` Returns a JSON representation of all photo info diff --git a/osxphotos/__init__.py b/osxphotos/__init__.py index 654b4a67..b459a63f 100644 --- a/osxphotos/__init__.py +++ b/osxphotos/__init__.py @@ -21,8 +21,9 @@ from Foundation import * from . import _applescript +# TODO: Add hidden, favorite, missing to photos and command line query? +# TODO: Update README for favorite, missing # TODO: Add test for __str__ and to_json -# TODO: Add favorites, hidden # TODO: Add location # TODO: standardize _ and __ as leading char for private variables # TODO: fix docstrings diff --git a/osxphotos/cmd_line.py b/osxphotos/cmd_line.py index 7892ce3f..37e47d6b 100644 --- a/osxphotos/cmd_line.py +++ b/osxphotos/cmd_line.py @@ -126,6 +126,14 @@ def dump(cli_obj): @click.option("--person", default=None, multiple=True, help="search for person(s)") @click.option("--album", default=None, multiple=True, help="search for album(s)") @click.option("--uuid", default=None, multiple=True, help="search for UUID(s)") +@click.option("--favorite", is_flag=True, help="search for photos marked favorite") +@click.option( + "--notfavorite", is_flag=True, help="search for photos not marked favorite" +) +@click.option("--hidden", is_flag=True, help="search for photos marked hidden") +@click.option("--nothidden", is_flag=True, help="search for photos not marked hidden") +@click.option("--missing", is_flag=True, help="search for photos missing from disk") +@click.option("--notmissing", is_flag=True, help="search for photos present on disk (e.g. not missing)") @click.option( "--json", required=False, @@ -135,17 +143,69 @@ def dump(cli_obj): ) @click.pass_obj @click.pass_context -def query(ctx, cli_obj, keyword, person, album, uuid, json): +def query( + ctx, + cli_obj, + keyword, + person, + album, + uuid, + json, + favorite, + notfavorite, + hidden, + nothidden, + missing, + notmissing, +): """ query the Photos database using 1 or more search options """ # if no query terms, show help and return - if not keyword and not person and not album and not uuid: + if ( + not keyword + and not person + and not album + and not uuid + and not favorite + and not notfavorite + and not hidden + and not nothidden + and not missing + and not notmissing + ): print(cli.commands["query"].get_help(ctx)) return + elif favorite and notfavorite: + # can't search for both favorite and notfavorite + print(cli.commands["query"].get_help(ctx)) + return + elif hidden and nothidden: + # can't search for both hidden and nothidden + print(cli.commands["query"].get_help(ctx)) + return + elif missing and notmissing: + # can't search for both missing and notmissing + print(cli.commands["query"].get_help(ctx)) + return else: photos = cli_obj.photosdb.photos( keywords=keyword, persons=person, albums=album, uuid=uuid ) + if favorite: + photos = [p for p in photos if p.favorite()] + elif notfavorite: + photos = [p for p in photos if not p.favorite()] + + if hidden: + photos = [p for p in photos if p.hidden()] + elif nothidden: + photos = [p for p in photos if not p.hidden()] + + if missing: + photos = [p for p in photos if p.ismissing()] + elif notmissing: + photos = [p for p in photos if not p.ismissing()] + print_photo_info(photos, cli_obj.json or json)