Added as_dict to ExifTool

This commit is contained in:
Rhet Turnbull
2020-05-11 05:07:19 -07:00
parent ca8f2b8d5c
commit d1af14dbb4
3 changed files with 26 additions and 11 deletions

View File

@@ -1,3 +1,3 @@
""" version info """
__version__ = "0.28.15"
__version__ = "0.28.16"

View File

@@ -10,7 +10,7 @@ import logging
import os
import subprocess
import sys
from functools import lru_cache # pylint: disable=syntax-error
from functools import lru_cache # pylint: disable=syntax-error
from .utils import _debug
@@ -232,20 +232,27 @@ class ExifTool:
ver = self.run_commands("-ver", no_file=True)
return ver.decode("utf-8")
def json(self):
""" return JSON dictionary from exiftool as dict """
def as_dict(self):
""" return dictionary of all EXIF tags and values from exiftool
returns empty dict if no tags
"""
json_str = self.run_commands("-json")
if json_str:
return json.loads(json_str)
exifdict = json.loads(json_str)
return exifdict[0]
else:
return None
return dict()
def json(self):
""" returns JSON string containing all EXIF tags and values from exiftool """
json_str = self.run_commands("-json")
return json_str
def _read_exif(self):
""" read exif data from file """
json = self.json()
self.data = {k: v for k, v in json[0].items()}
data = self.as_dict()
self.data = {k: v for k, v in data.items()}
def __str__(self):
str_ = f"file: {self.file}\nexiftool: {self._exiftoolproc._exiftool}"
return str_

View File

@@ -161,13 +161,21 @@ def test_exiftoolproc_exiftool():
assert exif1._exiftoolproc.exiftool == osxphotos.exiftool.get_exiftool_path()
def test_as_dict():
import osxphotos.exiftool
exif1 = osxphotos.exiftool.ExifTool(TEST_FILE_ONE_KEYWORD)
exifdata = exif1.as_dict()
assert exifdata["XMP:TagsList"] == "wedding"
def test_json():
import osxphotos.exiftool
import json
exif1 = osxphotos.exiftool.ExifTool(TEST_FILE_ONE_KEYWORD)
json1 = exif1.json()
assert json1[0]["XMP:TagsList"] == "wedding"
exifdata = json.loads(exif1.json())
assert exifdata[0]["XMP:TagsList"] == "wedding"
def test_str():