This commit is contained in:
@@ -33,7 +33,7 @@ XMP_KEYS_CAPTION: list[XmpKey] = [
|
|||||||
"IPTC:Caption-Abstract",
|
"IPTC:Caption-Abstract",
|
||||||
]
|
]
|
||||||
|
|
||||||
XMP_KEYS_TAG: list[XmpKey] = [
|
XMP_KEYS_KEYWORDS: list[XmpKey] = [
|
||||||
"XMP:HierarchicalSubject",
|
"XMP:HierarchicalSubject",
|
||||||
"XMP:Subject",
|
"XMP:Subject",
|
||||||
"IPTC:Keywords",
|
"IPTC:Keywords",
|
||||||
@@ -71,25 +71,26 @@ class MetadataWriter:
|
|||||||
return Path(str(self.path) + ".xmp")
|
return Path(str(self.path) + ".xmp")
|
||||||
|
|
||||||
def get_metadata(self, *args, **kwargs) -> Any:
|
def get_metadata(self, *args, **kwargs) -> Any:
|
||||||
|
path = self.sidecar_path
|
||||||
|
if not path.exists():
|
||||||
|
return ""
|
||||||
try:
|
try:
|
||||||
with exiftool.ExifToolHelper() as exif:
|
with exiftool.ExifToolHelper() as exif:
|
||||||
metadata = exif.get_metadata([str(self.path)])[0]
|
metadata = exif.get_metadata([path])[0]
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
log.error(f"Could not read metadata from {self.path}: {exc}")
|
log.error(f"Could not read metadata from {path}: {exc}")
|
||||||
assert metadata
|
return ""
|
||||||
return metadata.get(*args, **kwargs)
|
return metadata.get(*args, **kwargs)
|
||||||
|
|
||||||
def set_metadata(self, metadata_tags: dict[str, Any]) -> bool:
|
def set_metadata(self, metadata_tags: dict[str, Any]) -> bool:
|
||||||
if self.sidecar_path.exists():
|
if self.sidecar_path.exists():
|
||||||
params = ["-overwrite_original"]
|
files, params = [str(self.sidecar_path)], ["-overwrite_original"]
|
||||||
else:
|
else:
|
||||||
params = ["-o", "%f.xmp"]
|
files, params = [str(self.path)], ["-o", str(self.sidecar_path)]
|
||||||
try:
|
try:
|
||||||
with exiftool.ExifToolHelper() as exif:
|
with exiftool.ExifToolHelper() as exif:
|
||||||
exif.set_tags(
|
exif.set_tags(files, tags=metadata_tags, params=params)
|
||||||
[str(self.sidecar_path)], tags=metadata_tags, params=params
|
except Exception as exc: # noqa: BLE001
|
||||||
)
|
|
||||||
except Exception as exc: # noqa
|
|
||||||
log.error(f"Could not write metadata to file {self.sidecar_path}: {exc}")
|
log.error(f"Could not write metadata to file {self.sidecar_path}: {exc}")
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
@@ -104,9 +105,13 @@ class MetadataWriter:
|
|||||||
metadata_tags = {key: caption for key in XMP_KEYS_CAPTION}
|
metadata_tags = {key: caption for key in XMP_KEYS_CAPTION}
|
||||||
self.set_metadata(metadata_tags)
|
self.set_metadata(metadata_tags)
|
||||||
|
|
||||||
def write_keywords(self, keywords: Keywords): ...
|
def write_keywords(self, keywords: Keywords):
|
||||||
|
# TODO
|
||||||
|
...
|
||||||
|
|
||||||
def write_color(self, color: Color): ...
|
def write_color(self, color: Color):
|
||||||
|
# TODO
|
||||||
|
...
|
||||||
|
|
||||||
def write(
|
def write(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
|
import shutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from photools3.metadata_writer import MetadataWriter
|
from photools3.metadata_writer import (
|
||||||
|
XMP_KEYS_CAPTION,
|
||||||
|
MetadataWriter,
|
||||||
|
)
|
||||||
from test.test_photo import PATHS_DNG_EXAMPLES
|
from test.test_photo import PATHS_DNG_EXAMPLES
|
||||||
|
|
||||||
|
|
||||||
@@ -11,16 +15,12 @@ def make_dng_file(tmp_path):
|
|||||||
def _make(index: int = 0) -> Path:
|
def _make(index: int = 0) -> Path:
|
||||||
src = PATHS_DNG_EXAMPLES[index]
|
src = PATHS_DNG_EXAMPLES[index]
|
||||||
dst = tmp_path / f"copy_{index}_{src.name}"
|
dst = tmp_path / f"copy_{index}_{src.name}"
|
||||||
|
shutil.copy(src, dst)
|
||||||
return dst
|
return dst
|
||||||
|
|
||||||
return _make
|
return _make
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def dng_file(make_dng):
|
|
||||||
return make_dng()
|
|
||||||
|
|
||||||
|
|
||||||
def test_read_metadata(make_dng_file):
|
def test_read_metadata(make_dng_file):
|
||||||
"""primitive test for `MetadataWriter`"""
|
"""primitive test for `MetadataWriter`"""
|
||||||
file_path = make_dng_file()
|
file_path = make_dng_file()
|
||||||
@@ -28,28 +28,21 @@ def test_read_metadata(make_dng_file):
|
|||||||
assert result
|
assert result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("idx", range(len(PATHS_DNG_EXAMPLES)))
|
@pytest.mark.parametrize("pic_no", range(len(PATHS_DNG_EXAMPLES)))
|
||||||
def test_read_metadata_sidecar_path(make_dng_file, idx):
|
def test_read_metadata_sidecar_path(make_dng_file, pic_no):
|
||||||
file_path = make_dng_file(idx)
|
file_path = make_dng_file(pic_no)
|
||||||
metadata_writer = MetadataWriter(file_path)
|
metadata_writer = MetadataWriter(file_path)
|
||||||
print(metadata_writer.sidecar_path)
|
print(metadata_writer.sidecar_path)
|
||||||
assert isinstance(metadata_writer.sidecar_path, Path)
|
assert isinstance(metadata_writer.sidecar_path, Path)
|
||||||
assert str(metadata_writer.sidecar_path).lower().endswith(".dng.xmp")
|
assert str(metadata_writer.sidecar_path).lower().endswith(".dng.xmp")
|
||||||
|
|
||||||
|
|
||||||
def test_get_metadata():
|
@pytest.mark.parametrize("pic_no", range(len(PATHS_DNG_EXAMPLES)))
|
||||||
# TODO: Parametrized test on querying metadata fields
|
def test_set_caption(make_dng_file, pic_no):
|
||||||
...
|
metadata_writer = MetadataWriter(make_dng_file(pic_no))
|
||||||
|
metadata_writer.write_caption("This is a test")
|
||||||
|
result = metadata_writer.get_metadata(XMP_KEYS_CAPTION[0])
|
||||||
def test_set_metadata():
|
assert result
|
||||||
# TODO: Paramtrized test on setting metadata fields
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
def test_set_caption_on_empty():
|
|
||||||
# TODO
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
def test_set_caption_on_existing():
|
def test_set_caption_on_existing():
|
||||||
@@ -57,7 +50,7 @@ def test_set_caption_on_existing():
|
|||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
def test_set_keywords_on_empty():
|
def test_set_keywords():
|
||||||
# TODO
|
# TODO
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user