Changed return val of _should_update_photo to enum for easier debugging

This commit is contained in:
Rhet Turnbull
2022-03-09 06:52:17 -08:00
parent fccd746c58
commit bbcc3acba9

View File

@@ -1,7 +1,6 @@
""" PhotoExport class to export photos """ PhotoExport class to export photos
""" """
import dataclasses import dataclasses
import hashlib import hashlib
import json import json
@@ -13,6 +12,7 @@ import tempfile
import typing as t import typing as t
from collections import namedtuple # pylint: disable=syntax-error from collections import namedtuple # pylint: disable=syntax-error
from dataclasses import asdict, dataclass from dataclasses import asdict, dataclass
from enum import Enum
import photoscript import photoscript
from mako.template import Template from mako.template import Template
@@ -62,6 +62,17 @@ if t.TYPE_CHECKING:
# retry if download_missing/use_photos_export fails the first time (which sometimes it does) # retry if download_missing/use_photos_export fails the first time (which sometimes it does)
MAX_PHOTOSCRIPT_RETRIES = 3 MAX_PHOTOSCRIPT_RETRIES = 3
# return values for _should_update_photo
class ShouldUpdate(Enum):
NOT_IN_DATABASE = 1
HARDLINK_DIFFERENT_FILES = 2
NOT_HARDLINK_SAME_FILES = 3
DEST_SIG_DIFFERENT = 4
EXPORT_OPTIONS_DIFFERENT = 5
EXIFTOOL_DIFFERENT = 6
EDITED_SIG_DIFFERENT = 7
DIGEST_DIFFERENT = 8
class ExportError(Exception): class ExportError(Exception):
"""error during export""" """error during export"""
@@ -680,7 +691,7 @@ class PhotoExporter:
def _should_update_photo( def _should_update_photo(
self, src: pathlib.Path, dest: pathlib.Path, options: ExportOptions self, src: pathlib.Path, dest: pathlib.Path, options: ExportOptions
) -> bool: ) -> t.Literal[True, False]:
"""Return True if photo should be updated, else False""" """Return True if photo should be updated, else False"""
export_db = options.export_db export_db = options.export_db
fileutil = options.fileutil fileutil = options.fileutil
@@ -689,42 +700,45 @@ class PhotoExporter:
if not file_record: if not file_record:
# photo doesn't exist in database, should update # photo doesn't exist in database, should update
return True return ShouldUpdate.NOT_IN_DATABASE
if options.export_as_hardlink and not dest.samefile(src): if options.export_as_hardlink and not dest.samefile(src):
# different files, should update # different files, should update
return True return ShouldUpdate.HARDLINK_DIFFERENT_FILES
if not options.export_as_hardlink and dest.samefile(src): if not options.export_as_hardlink and dest.samefile(src):
# same file but not exporting as hardlink, should update # same file but not exporting as hardlink, should update
return True return ShouldUpdate.NOT_HARDLINK_SAME_FILES
if not options.ignore_signature and not fileutil.cmp_file_sig( if not options.ignore_signature and not fileutil.cmp_file_sig(
dest, file_record.dest_sig dest, file_record.dest_sig
): ):
# destination file doesn't match what was last exported # destination file doesn't match what was last exported
return True return ShouldUpdate.DEST_SIG_DIFFERENT
if file_record.export_options != options.bit_flags: if file_record.export_options != options.bit_flags:
# exporting with different set of options (e.g. exiftool), should update # exporting with different set of options (e.g. exiftool), should update
# need to check this before exiftool in case exiftool options are different # need to check this before exiftool in case exiftool options are different
# and export database is missing; this will always be True if database is missing # and export database is missing; this will always be True if database is missing
# as it'll be None and bit_flags will be an int # as it'll be None and bit_flags will be an int
return True return ShouldUpdate.EXPORT_OPTIONS_DIFFERENT
if options.exiftool: if options.exiftool:
current_exifdata = self._exiftool_json_sidecar(options=options) current_exifdata = self._exiftool_json_sidecar(options=options)
return current_exifdata != file_record.exifdata rv = current_exifdata != file_record.exifdata
# if using exiftool, don't need to continue checking edited below
# as exiftool will be used to update edited file
return ShouldUpdate.EXIFTOOL_DIFFERENT if rv else False
if options.edited and not fileutil.cmp_file_sig(src, file_record.src_sig): if options.edited and not fileutil.cmp_file_sig(src, file_record.src_sig):
# edited file in Photos doesn't match what was last exported # edited file in Photos doesn't match what was last exported
return True return ShouldUpdate.EDITED_SIG_DIFFERENT
if options.force_update: if options.force_update:
current_digest = hexdigest(self.photo.json()) current_digest = hexdigest(self.photo.json())
if current_digest != file_record.digest: if current_digest != file_record.digest:
# metadata in Photos changed, force update # metadata in Photos changed, force update
return True return ShouldUpdate.DIGEST_DIFFERENT
# photo should not be updated # photo should not be updated
return False return False