Removed exportdb requirement from PhotoTemplate

This commit is contained in:
Rhet Turnbull
2022-01-24 06:20:34 -08:00
parent b3b1d8f193
commit 6af124e4d3
2 changed files with 9 additions and 29 deletions

View File

@@ -2032,7 +2032,6 @@ def export(
finder_tag_template=finder_tag_template, finder_tag_template=finder_tag_template,
strip=strip, strip=strip,
export_dir=dest, export_dir=dest,
export_db=export_db,
) )
results.xattr_written.extend(tags_written) results.xattr_written.extend(tags_written)
results.xattr_skipped.extend(tags_skipped) results.xattr_skipped.extend(tags_skipped)
@@ -2044,7 +2043,6 @@ def export(
xattr_template, xattr_template,
strip=strip, strip=strip,
export_dir=dest, export_dir=dest,
export_db=export_db,
) )
results.xattr_written.extend(xattr_written) results.xattr_written.extend(xattr_written)
results.xattr_skipped.extend(xattr_skipped) results.xattr_skipped.extend(xattr_skipped)
@@ -2958,7 +2956,7 @@ def _render_suffix_template(
return "" return ""
try: try:
options = RenderOptions(filename=True, export_dir=dest, exportdb=export_db) options = RenderOptions(filename=True, export_dir=dest)
rendered_suffix, unmatched = photo.render_template(suffix_template, options) rendered_suffix, unmatched = photo.render_template(suffix_template, options)
except ValueError as e: except ValueError as e:
raise click.BadOptionUsage( raise click.BadOptionUsage(
@@ -3074,8 +3072,7 @@ def export_photo_to_directory(
return results return results
render_options = RenderOptions( render_options = RenderOptions(
export_dir=export_dir, dest_path=dest_path, exportdb=export_db export_dir=export_dir, dest_path=dest_path)
)
tries = 0 tries = 0
while tries <= retry: while tries <= retry:
@@ -3214,8 +3211,7 @@ def get_filenames_from_template(
filename=True, filename=True,
edited_version=edited, edited_version=edited,
export_dir=export_dir, export_dir=export_dir,
dest_path=dest_path, dest_path=dest_path
exportdb=export_db,
) )
filenames, unmatched = photo.render_template(filename_template, options) filenames, unmatched = photo.render_template(filename_template, options)
except ValueError as e: except ValueError as e:
@@ -3282,8 +3278,7 @@ def get_dirnames_from_template(
# got a directory template, render it and check results are valid # got a directory template, render it and check results are valid
try: try:
options = RenderOptions( options = RenderOptions(
dirname=True, edited_version=edited, exportdb=export_db dirname=True, edited_version=edited)
)
dirnames, unmatched = photo.render_template(directory, options) dirnames, unmatched = photo.render_template(directory, options)
except ValueError as e: except ValueError as e:
raise click.BadOptionUsage( raise click.BadOptionUsage(
@@ -3569,7 +3564,6 @@ def write_finder_tags(
finder_tag_template=None, finder_tag_template=None,
strip=False, strip=False,
export_dir=None, export_dir=None,
export_db=None,
): ):
"""Write Finder tags (extended attributes) to files; only writes attributes if attributes on file differ from what would be written """Write Finder tags (extended attributes) to files; only writes attributes if attributes on file differ from what would be written
@@ -3583,7 +3577,6 @@ def write_finder_tags(
exiftool_merge_keywords: if True, include any keywords in the exif data of the source image as keywords exiftool_merge_keywords: if True, include any keywords in the exif data of the source image as keywords
finder_tag_template: list of templates to evaluate for determining Finder tags finder_tag_template: list of templates to evaluate for determining Finder tags
export_dir: value to use for {export_dir} template export_dir: value to use for {export_dir} template
export_db: an ExportDB object
Returns: Returns:
(list of file paths that were updated with new Finder tags, list of file paths skipped because Finder tags didn't need updating) (list of file paths that were updated with new Finder tags, list of file paths skipped because Finder tags didn't need updating)
@@ -3614,8 +3607,7 @@ def write_finder_tags(
options = RenderOptions( options = RenderOptions(
none_str=_OSXPHOTOS_NONE_SENTINEL, none_str=_OSXPHOTOS_NONE_SENTINEL,
path_sep="/", path_sep="/",
export_dir=export_dir, export_dir=export_dir
exportdb=export_db,
) )
rendered, unmatched = photo.render_template(template_str, options) rendered, unmatched = photo.render_template(template_str, options)
except ValueError as e: except ValueError as e:
@@ -3663,7 +3655,6 @@ def write_extended_attributes(
xattr_template, xattr_template,
strip=False, strip=False,
export_dir=None, export_dir=None,
export_db=None,
): ):
"""Writes extended attributes to exported files """Writes extended attributes to exported files
@@ -3671,7 +3662,6 @@ def write_extended_attributes(
photo: a PhotoInfo object photo: a PhotoInfo object
strip: xattr_template: list of tuples: (attribute name, attribute template) strip: xattr_template: list of tuples: (attribute name, attribute template)
export_dir: value to use for {export_dir} template export_dir: value to use for {export_dir} template
exportdb: an ExportDB object
Returns: Returns:
tuple(list of file paths that were updated with new attributes, list of file paths skipped because attributes didn't need updating) tuple(list of file paths that were updated with new attributes, list of file paths skipped because attributes didn't need updating)
@@ -3683,8 +3673,7 @@ def write_extended_attributes(
options = RenderOptions( options = RenderOptions(
none_str=_OSXPHOTOS_NONE_SENTINEL, none_str=_OSXPHOTOS_NONE_SENTINEL,
path_sep="/", path_sep="/",
export_dir=export_dir, export_dir=export_dir
exportdb=export_db,
) )
rendered, unmatched = photo.render_template(template_str, options) rendered, unmatched = photo.render_template(template_str, options)
except ValueError as e: except ValueError as e:
@@ -3752,8 +3741,7 @@ def run_post_command(
if isinstance(f, tuple): if isinstance(f, tuple):
f = f[0] f = f[0]
render_options = RenderOptions( render_options = RenderOptions(
export_dir=export_dir, filepath=f, exportdb=export_db export_dir=export_dir, filepath=f)
)
template = PhotoTemplate(photo, exiftool_path=exiftool_path) template = PhotoTemplate(photo, exiftool_path=exiftool_path)
command, _ = template.render(command_template, options=render_options) command, _ = template.render(command_template, options=render_options)
command = command[0] if command else None command = command[0] if command else None

View File

@@ -17,7 +17,6 @@ from ._constants import _UNKNOWN_PERSON, TEXT_DETECTION_CONFIDENCE_THRESHOLD
from ._version import __version__ from ._version import __version__
from .datetime_formatter import DateTimeFormatter from .datetime_formatter import DateTimeFormatter
from .exiftool import ExifToolCaching from .exiftool import ExifToolCaching
from .export_db import ExportDB_ABC, ExportDBInMemory
from .path_utils import sanitize_dirname, sanitize_filename, sanitize_pathpart from .path_utils import sanitize_dirname, sanitize_filename, sanitize_pathpart
from .text_detection import detect_text from .text_detection import detect_text
from .utils import expand_and_validate_filepath, load_function from .utils import expand_and_validate_filepath, load_function
@@ -300,7 +299,6 @@ class RenderOptions:
dest_path: set to the destination path of the photo (for use by {function} template), only valid with --filename dest_path: set to the destination path of the photo (for use by {function} template), only valid with --filename
filepath: set to value for filepath of the exported photo if you want to evaluate {filepath} template filepath: set to value for filepath of the exported photo if you want to evaluate {filepath} template
quote: quote path templates for execution in the shell quote: quote path templates for execution in the shell
exportdb: ExportDB object
""" """
none_str: str = "_" none_str: str = "_"
@@ -315,7 +313,6 @@ class RenderOptions:
dest_path: Optional[str] = None dest_path: Optional[str] = None
filepath: Optional[str] = None filepath: Optional[str] = None
quote: bool = False quote: bool = False
exportdb: Optional[ExportDB_ABC] = None
class PhotoTemplateParser: class PhotoTemplateParser:
@@ -384,9 +381,6 @@ class PhotoTemplate:
self.filepath = options.filepath self.filepath = options.filepath
self.quote = options.quote self.quote = options.quote
self.dest_path = options.dest_path self.dest_path = options.dest_path
self.exportdb = options.exportdb or ExportDBInMemory(
None, self.export_dir or "."
)
def render( def render(
self, self,
@@ -420,7 +414,6 @@ class PhotoTemplate:
self.filepath = options.filepath self.filepath = options.filepath
self.quote = options.quote self.quote = options.quote
self.dest_path = options.dest_path self.dest_path = options.dest_path
self.exportdb = options.exportdb or self.exportdb
try: try:
model = self.parser.parse(template) model = self.parser.parse(template)
@@ -1216,7 +1209,7 @@ class PhotoTemplate:
else: else:
values = list(obj) values = list(obj)
elif field == "detected_text": elif field == "detected_text":
values = _get_detected_text(self.photo, self.exportdb, confidence=subfield) values = _get_detected_text(self.photo, confidence=subfield)
else: else:
raise ValueError(f"Unhandled template value: {field}") raise ValueError(f"Unhandled template value: {field}")
@@ -1459,7 +1452,7 @@ def _get_album_by_path(photo, folder_album_path):
return None return None
def _get_detected_text(photo, exportdb, confidence=TEXT_DETECTION_CONFIDENCE_THRESHOLD): def _get_detected_text(photo, confidence=TEXT_DETECTION_CONFIDENCE_THRESHOLD):
"""Returns the detected text for a photo """Returns the detected text for a photo
{detected_text} uses this instead of PhotoInfo.detected_text() to cache the text for all confidence values {detected_text} uses this instead of PhotoInfo.detected_text() to cache the text for all confidence values
""" """
@@ -1475,5 +1468,4 @@ def _get_detected_text(photo, exportdb, confidence=TEXT_DETECTION_CONFIDENCE_THR
# _detected_text caches the text detection results in an extended attribute # _detected_text caches the text detection results in an extended attribute
# so the first time this gets called is slow but repeated accesses are fast # so the first time this gets called is slow but repeated accesses are fast
detected_text = photo._detected_text() detected_text = photo._detected_text()
exportdb.set_detected_text_for_uuid(photo.uuid, json.dumps(detected_text))
return [text for text, conf in detected_text if conf >= confidence] return [text for text, conf in detected_text if conf >= confidence]