diff --git a/osxphotos/_version.py b/osxphotos/_version.py index 6d4c7099..172699df 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.49.2" +__version__ = "0.49.3" diff --git a/osxphotos/cli/exiftool_cli.py b/osxphotos/cli/exiftool_cli.py index 0b15280f..cc783dd9 100644 --- a/osxphotos/cli/exiftool_cli.py +++ b/osxphotos/cli/exiftool_cli.py @@ -350,7 +350,7 @@ def process_files( verbose(f"Skipping missing file [filepath]{file}[/]") report_writer.write(ExportResults(missing=[file])) continue - # zzz put in check for hardlink + # TODO: zzz put in check for hardlink verbose(f"Processing file [filepath]{file}[/] ([num]{count}/{total}[/num])") photo = photosdb.get_photo(uuid) export_options = ExportOptions( diff --git a/osxphotos/cli/exportdb.py b/osxphotos/cli/exportdb.py index c18cf612..340539fe 100644 --- a/osxphotos/cli/exportdb.py +++ b/osxphotos/cli/exportdb.py @@ -1,5 +1,6 @@ """exportdb command for osxphotos CLI""" +import json import pathlib import sys @@ -63,6 +64,18 @@ from .verbose import verbose_print nargs=1, help="Print information about FILE_PATH contained in the database.", ) +@click.option( + "--uuid-files", + metavar="UUID", + nargs=1, + help="List exported files associated with UUID.", +) +@click.option( + "--uuid-info", + metavar="UUID", + nargs=1, + help="Print information about UUID contained in the database.", +) @click.option( "--report", metavar="REPORT_FILE RUN_ID", @@ -110,22 +123,24 @@ from .verbose import verbose_print ) @click.argument("export_db", metavar="EXPORT_DATABASE", type=click.Path(exists=True)) def exportdb( - version, - vacuum, - check_signatures, - update_signatures, - touch_file, - last_run, - save_config, - info, - report, - migrate, - sql, - export_dir, append, - verbose, + check_signatures, dry_run, export_db, + export_dir, + info, + last_run, + migrate, + report, + save_config, + sql, + touch_file, + update_signatures, + uuid_files, + uuid_info, + vacuum, + verbose, + version, ): """Utilities for working with the osxphotos export database""" @@ -163,6 +178,8 @@ def exportdb( sql, touch_file, update_signatures, + uuid_files, + uuid_info, vacuum, version, ] @@ -273,6 +290,37 @@ def exportdb( print(f"[red]File '{info}' not found in export database[/red]") sys.exit(0) + if uuid_info: + # get photoinfo record for a uuid + exportdb = ExportDB(export_db, export_dir) + try: + info_rec = exportdb.get_photoinfo_for_uuid(uuid_info) + except Exception as e: + print(f"[red]Error: {e}[/red]") + sys.exit(1) + else: + if info_rec: + print(json.dumps(json.loads(info_rec), sort_keys=True, indent=2)) + else: + print(f"[red]UUID '{uuid_info}' not found in export database[/red]") + sys.exit(0) + + if uuid_files: + # list files associated with a uuid + exportdb = ExportDB(export_db, export_dir) + try: + file_list = exportdb.get_files_for_uuid(uuid_files) + except Exception as e: + print(f"[red]Error: {e}[/red]") + sys.exit(1) + else: + if file_list: + for f in file_list: + print(f) + else: + print(f"[red]UUID '{uuid_files}' not found in export database[/red]") + sys.exit(0) + if report: exportdb = ExportDB(export_db, export_dir) report_template, run_id = report diff --git a/osxphotos/export_db.py b/osxphotos/export_db.py index bef8487e..5fd62451 100644 --- a/osxphotos/export_db.py +++ b/osxphotos/export_db.py @@ -15,7 +15,7 @@ from contextlib import suppress from io import StringIO from sqlite3 import Error from tempfile import TemporaryDirectory -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Tuple, Union, List from tenacity import retry, stop_after_attempt @@ -171,6 +171,17 @@ class ExportDB: uuid = None return uuid + def get_files_for_uuid(self, uuid: str) -> List: + """query database for UUID and return list of files associated with UUID or empty list""" + conn = self._conn + c = conn.cursor() + c.execute( + "SELECT filepath FROM export_data WHERE uuid = ?", + (uuid,), + ) + results = c.fetchall() + return [os.path.join(self.export_dir, r[0]) for r in results] + def get_photoinfo_for_uuid(self, uuid): """returns the photoinfo JSON struct for a UUID""" conn = self._conn diff --git a/tests/test_export_db.py b/tests/test_export_db.py index 50b0f56b..698829f8 100644 --- a/tests/test_export_db.py +++ b/tests/test_export_db.py @@ -50,6 +50,7 @@ def test_export_db(): assert db.get_uuid_for_file(filepath) is None db.create_file_record(filepath, uuid) assert db.get_uuid_for_file(filepath) == uuid + assert db.get_files_for_uuid(uuid) == [filepath] record = db.get_file_record(filepath) assert record.uuid == uuid @@ -142,6 +143,7 @@ def test_export_db_in_memory(): assert record2.digest == DIGEST_DATA assert record2.src_sig == (7, 8, 9) assert record2.dest_sig == (10, 11, 12) + assert dbram.get_files_for_uuid(uuid) == [filepath] # change some values record2.photoinfo = INFO_DATA2