added to_json() to PhotoInfo

This commit is contained in:
Rhet Turnbull
2019-08-18 15:21:28 -07:00
parent 07c9c31a09
commit be3d6dee8d
4 changed files with 62 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ import sys
from shutil import copyfile
import pprint
import sqlite3
import json
import yaml
import objc
@@ -27,7 +28,7 @@ from . import _applescript
# which Photos library database versions have been tested
# Photos 3.0 (10.13.6) == 3301
# Photos 4.0 (10.14.5) == 4016
# Photos 4.0 (10.4.6) == 4025
# Photos 4.0 (10.4.6) == 4025
# TODO: Should this also use compatibleBackToVersion from LiGlobals?
_TESTED_DB_VERSIONS = ["4025", "4016", "3301"]
@@ -594,6 +595,8 @@ class PhotosDB:
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.
@@ -682,7 +685,24 @@ class PhotoInfo:
"ismissing": self.ismissing(),
"hasadjustments": self.hasadjustments(),
}
return yaml.dump(info,sort_keys=False)
return yaml.dump(info, sort_keys=False)
def to_json(self):
""" return JSON representation """
pic = {
"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 json.dumps(pic)
# compare two PhotoInfo objects for equality
def __eq__(self, other):

View File

@@ -12,7 +12,10 @@ class CLI_Obj:
self.photosdb = osxphotos.PhotosDB(dbfile=db)
self.json = json
CTX_SETTINGS=dict(help_option_names=['-h','--help'])
CTX_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.group(context_settings=CTX_SETTINGS)
@click.option(
"--db",
@@ -103,29 +106,16 @@ def dump(cli_obj):
pdb = cli_obj.photosdb
photos = pdb.photos()
csv_writer = csv.writer(
sys.stdout, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
if cli_obj.json:
dump = []
for p in photos:
dump.append(
{
"uuid": p.uuid(),
"filename": p.filename(),
"date": str(p.date()),
"description": p.description(),
"name": p.name(),
"keywords": p.keywords(),
"albums": p.albums(),
"persons": p.persons(),
"path": p.path(),
"ismissing": p.ismissing(),
"hasadjustments": p.hasadjustments(),
}
)
print(json.dumps(dump))
dump.append(p.to_json())
print(f"[{', '.join(dump)}]")
else:
# dump as CSV
csv_writer = csv.writer(
sys.stdout, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
dump = []
# add headers
dump.append(
@@ -172,12 +162,15 @@ def dump(cli_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)
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.argument("topic", default=None, required=False, nargs=1)
@click.pass_context
def help(ctx, topic, **kw):
""" print help; for help on commands: help <command> """
@@ -186,5 +179,6 @@ def help(ctx, topic, **kw):
else:
print(cli.commands[topic].get_help(ctx))
if __name__ == "__main__":
cli()