From a88a62d155fd405e8015b18d48065e2dba53acdb Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sun, 8 Aug 2021 11:55:38 +0200 Subject: [PATCH] Change python exiftool wrapper to RhetTbull implementation --- dozo.py | 8 +------- dozo/exiftool.py | 31 +++++++++++++++---------------- dozo/media/media.py | 3 +-- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/dozo.py b/dozo.py index 04d3e33..c3bf67c 100755 --- a/dozo.py +++ b/dozo.py @@ -252,10 +252,4 @@ main.add_command(_batch) if __name__ == '__main__': - #Initialize ExifTool Subprocess - exiftool_addedargs = [ - u'-config', - u'"{}"'.format(constants.exiftool_config) - ] - with ExifTool(executable_=get_exiftool(), addedargs=exiftool_addedargs) as et: - main() + main() diff --git a/dozo/exiftool.py b/dozo/exiftool.py index bc43fed..67b3a17 100644 --- a/dozo/exiftool.py +++ b/dozo/exiftool.py @@ -1,10 +1,6 @@ -""" Yet another simple exiftool wrapper - I rolled my own for following reasons: - 1. I wanted something under MIT license (best alternative was licensed under GPL/BSD) - 2. I wanted singleton behavior so only a single exiftool process was ever running - 3. When used as a context manager, I wanted the operations to batch until exiting the context (improved performance) - If these aren't important to you, I highly recommend you use Sven Marnach's excellent - pyexiftool: https://github.com/smarnach/pyexiftool which provides more functionality """ +""" Yet another simple exiftool wrapper from: +https://github.com/RhetTbull/osxphotos/blob/master/osxphotos/exiftool.py +""" import atexit import json @@ -55,14 +51,15 @@ class _ExifToolProc: return cls.instance - def __init__(self, exiftool=None): + def __init__(self, exiftool=None, logger=logging.getLogger()): """construct _ExifToolProc singleton object or return instance of already created object exiftool: optional path to exiftool binary (if not provided, will search path to find it)""" + self.logger = logger if hasattr(self, "_process_running") and self._process_running: # already running if exiftool is not None and exiftool != self._exiftool: - logging.warning( + self.logger.warning( f"exiftool subprocess already running, " f"ignoring exiftool={exiftool}" ) @@ -95,7 +92,7 @@ class _ExifToolProc: """ start exiftool in batch mode """ if self._process_running: - logging.warning("exiftool already running: {self._process}") + self.logger.warning("exiftool already running: {self._process}") return # open exiftool process @@ -145,7 +142,7 @@ class _ExifToolProc: class ExifTool: """ Basic exiftool interface for reading and writing EXIF tags """ - def __init__(self, filepath, exiftool=None, overwrite=True, flags=None): + def __init__(self, filepath, exiftool=None, overwrite=True, flags=None, logger=logging.getLogger()): """Create ExifTool object Args: @@ -165,7 +162,7 @@ class ExifTool: self.error = None # if running as a context manager, self._context_mgr will be True self._context_mgr = False - self._exiftoolproc = _ExifToolProc(exiftool=exiftool) + self._exiftoolproc = _ExifToolProc(exiftool=exiftool, logger=logger) self._read_exif() @property @@ -391,15 +388,16 @@ class ExifToolCaching(ExifTool): _singletons = {} - def __new__(cls, filepath, exiftool=None): + def __new__(cls, filepath, exiftool=None, logger=logging.getLogger()): """ create new object or return instance of already created singleton """ if filepath not in cls._singletons: - cls._singletons[filepath] = _ExifToolCaching(filepath, exiftool=exiftool) + cls._singletons[filepath] = _ExifToolCaching(filepath, + exiftool=exiftool, logger=logger) return cls._singletons[filepath] class _ExifToolCaching(ExifTool): - def __init__(self, filepath, exiftool=None): + def __init__(self, filepath, exiftool=None, logger=logging.getLogger()): """Create read-only ExifTool object that caches values Args: @@ -411,7 +409,8 @@ class _ExifToolCaching(ExifTool): """ self._json_cache = None self._asdict_cache = {} - super().__init__(filepath, exiftool=exiftool, overwrite=False, flags=None) + super().__init__(filepath, exiftool=exiftool, overwrite=False, + flags=None, logger=logger) def run_commands(self, *commands, no_file=False): if commands[0] not in ["-json", "-ver"]: diff --git a/dozo/media/media.py b/dozo/media/media.py index fe7507b..5c4dd61 100644 --- a/dozo/media/media.py +++ b/dozo/media/media.py @@ -12,7 +12,7 @@ import logging # load modules from dateutil.parser import parse import re -from elodie.external.pyexiftool import ExifTool +from dozo.exiftool import ExifToolCaching class Media(): @@ -576,7 +576,6 @@ class Media(): return None status = '' - status = ExifTool().set_tags(tags, path) for tag, value in tags.items(): status = ExifToolCaching(path, self.logger).setvalue(tag, value) if status.decode().find('unchanged') != -1 or status == '':