Added query to cmd_line and added __repr__ and __str__ to osxphotos classes

This commit is contained in:
Rhet Turnbull 2019-08-18 14:56:04 -07:00
parent 9db32a51ca
commit 07c9c31a09
2 changed files with 47 additions and 5 deletions

View File

@ -10,6 +10,7 @@ from shutil import copyfile
import pprint
import sqlite3
import yaml
import objc
import CoreFoundation
from Foundation import *
@ -18,7 +19,6 @@ from . import _applescript
# from loguru import logger
# TODO: add get_database_path (full path to db file)
# TODO: standardize _ and __ as leading char for private variables
# TODO: fix use of ''' and """
# TODO: fix docstrings
@ -592,7 +592,8 @@ class PhotosDB:
photoinfo.append(info)
return photoinfo
def __repr__(self):
return f"osxphotos.PhotosDB(dbfile='{self.get_db_path()}')"
"""
Info about a specific photo, contains all the details we know about the photo
including keywords, persons, albums, uuid, path, etc.
@ -664,6 +665,25 @@ class PhotoInfo:
def hasadjustments(self):
return True if self.__info["hasAdjustments"] == 1 else False
def __repr__(self):
return f"osxphotos.PhotoInfo(db={self.__db}, uuid='{self.__uuid}', info={self.__info})"
def __str__(self):
info = {
"uuid": self.uuid(),
"filename": self.filename(),
"date": str(self.date()),
"description": self.description(),
"name": self.name(),
"keywords": self.keywords(),
"albums": self.albums(),
"persons": self.persons(),
"path": self.path(),
"ismissing": self.ismissing(),
"hasadjustments": self.hasadjustments(),
}
return yaml.dump(info,sort_keys=False)
# compare two PhotoInfo objects for equality
def __eq__(self, other):
if isinstance(other, self.__class__):

View File

@ -12,8 +12,8 @@ class CLI_Obj:
self.photosdb = osxphotos.PhotosDB(dbfile=db)
self.json = json
@click.group()
CTX_SETTINGS=dict(help_option_names=['-h','--help'])
@click.group(context_settings=CTX_SETTINGS)
@click.option(
"--db",
required=False,
@ -97,7 +97,6 @@ def info(cli_obj):
@cli.command()
# @click.option('--delim',default=",",help="")
@click.pass_obj
def dump(cli_obj):
""" print list of all photos & associated info from the Photos library """
@ -164,5 +163,28 @@ def dump(cli_obj):
csv_writer.writerow(row)
@cli.command()
@click.option("--keyword", default=None, multiple=True, help="search for keyword(s)")
@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.pass_obj
@click.pass_context
def query(ctx, cli_obj, keyword, person, album, uuid):
""" query the Photos database using 1 or more search options """
photos = cli_obj.photosdb.photos(keywords=keyword, persons=person, albums=album, uuid=uuid)
print(photos)
pass
@cli.command()
@click.argument('topic', default=None, required=False, nargs=1 )
@click.pass_context
def help(ctx, topic, **kw):
""" print help; for help on commands: help <command> """
if topic is None:
print(ctx.parent.get_help())
else:
print(cli.commands[topic].get_help(ctx))
if __name__ == "__main__":
cli()