Added osxphotos command line tool

This commit is contained in:
Rhet Turnbull 2019-08-18 09:39:55 -07:00
parent cbdc193b83
commit 0e65ab5452
5 changed files with 182 additions and 6 deletions

View File

@ -146,6 +146,14 @@ photosdb.get_photos_library_path()
Returns the path to the Photos library as a string
#### ```get_db_path```
```python
# assumes photosdb is a PhotosDB object (see above)
photosdb.get_db_path()
```
Returns the path to the Photos database PhotosDB was initialized with
#### ```get_db_version```
```python
# assumes photosdb is a PhotosDB object (see above)

View File

@ -3,7 +3,7 @@ import osxphotos
def main():
photosdb = osxphotos.PhotosDB()
print(f"db file = {photosdb.get_photos_library_path()}")
print(f"db file = {photosdb.get_db_path()}")
print(f"db version = {photosdb.get_db_version()}")
print(photosdb.keywords())

View File

@ -18,6 +18,7 @@ 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
@ -191,6 +192,10 @@ class PhotosDB:
# return the database version as stored in LiGlobals table
return self.__db_version
def get_db_path(self):
""" return path to the Photos library database PhotosDB was initialized with """
return os.path.abspath(self._dbfile)
def get_photos_library_path(self):
# return the path to the Photos library
plist_file = Path(

163
osxphotos/cmd_line.py Normal file
View File

@ -0,0 +1,163 @@
import click
import osxphotos
from loguru import logger
import json
import yaml
import csv
import sys
class CLI_Obj:
def __init__(self, db=None, json=False):
self.photosdb = osxphotos.PhotosDB(dbfile=db)
self.json = json
@click.group()
@click.option(
"--db",
required=False,
metavar="<Photos database path>",
default=None,
help="Specify database file",
)
@click.option(
"--json",
required=False,
is_flag=True,
default=False,
help="Print output in JSON format",
)
@click.pass_context
def cli(ctx, db, json):
ctx.obj = CLI_Obj(db=db, json=json)
@cli.command()
@click.pass_obj
def keywords(cli_obj):
keywords = {"keywords": cli_obj.photosdb.keywords_as_dict()}
if cli_obj.json:
print(json.dumps(keywords))
else:
print(yaml.dump(keywords, sort_keys=False))
@cli.command()
@click.pass_obj
def albums(cli_obj):
albums = {"albums": cli_obj.photosdb.albums_as_dict()}
if cli_obj.json:
print(json.dumps(albums))
else:
print(yaml.dump(albums, sort_keys=False))
@cli.command()
@click.pass_obj
def persons(cli_obj):
persons = {"persons": cli_obj.photosdb.persons_as_dict()}
if cli_obj.json:
print(json.dumps(persons))
else:
print(yaml.dump(persons, sort_keys=False))
@cli.command()
@click.pass_obj
def info(cli_obj):
pdb = cli_obj.photosdb
info = {}
info["database_path"] = pdb.get_db_path()
info["database_version"] = pdb.get_db_version()
photos = pdb.photos()
info["photo_count"] = len(photos)
keywords = pdb.keywords_as_dict()
info["keywords_count"] = len(keywords)
info["keywords"] = keywords
albums = pdb.albums_as_dict()
info["albums_count"] = len(albums)
info["albums"] = albums
persons = pdb.persons_as_dict()
info["persons_count"] = len(persons)
info["persons"] = persons
if cli_obj.json:
print(json.dumps(info))
else:
print(yaml.dump(info, sort_keys=False))
@cli.command()
# @click.option('--delim',default=",",help="")
@click.pass_obj
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(
[
p.uuid(),
p.filename(),
str(p.date()),
p.description(),
p.name(),
p.keywords(),
p.albums(),
p.persons(),
p.path(),
p.ismissing(),
p.hasadjustments(),
]
)
print(json.dumps(dump))
else:
dump = []
# add headers
dump.append(
[
"uuid",
"filename",
"date",
"description",
"name",
"keywords",
"albums",
"persons",
"path",
"is_missing",
"has_adjustments",
]
)
for p in photos:
dump.append(
[
p.uuid(),
p.filename(),
str(p.date()),
p.description(),
p.name(),
", ".join(p.keywords()),
", ".join(p.albums()),
", ".join(p.persons()),
p.path(),
p.ismissing(),
p.hasadjustments(),
]
)
for row in dump:
csv_writer.writerow(row)
if __name__ == "__main__":
cli()

View File

@ -38,7 +38,7 @@ with open(path.join(this_directory, "README.md"), encoding="utf-8") as f:
setup(
name="osxphotos",
version="0.10.5",
version="0.10.7",
description="Manipulate (read-only) Apple's Photos app library on Mac OS X",
long_description=long_description,
long_description_content_type="text/markdown",
@ -58,8 +58,8 @@ setup(
"Programming Language :: Python :: 3.6",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=["pyobjc",],
# entry_points = {
# 'console_scripts' : ['osxmetadata=osxmetadata.cmd_line:main'],
# }
install_requires=["pyobjc","pyyaml",],
entry_points = {
'console_scripts' : ['osxphotos=osxphotos.cmd_line:cli'],
}
)