Metadata writer #17

Merged
mathias2784 merged 15 commits from metadata-writer into master 2026-10-10 12:22:15 +00:00
2 changed files with 114 additions and 27 deletions
Showing only changes of commit 63d628c354 - Show all commits
+4 -8
View File
@@ -17,14 +17,11 @@ _EXIFTOOL_MISSING_MSG = (
if shutil.which("exiftool") is None: if shutil.which("exiftool") is None:
raise FileNotFoundError(_EXIFTOOL_MISSING_MSG) raise FileNotFoundError(_EXIFTOOL_MISSING_MSG)
# File extensions that should use XMP sidecars
SIDECAR_EXTENSIONS = {".dng", ".nef", ".cr2", ".cr3", ".arw", ".raf", ".orf", ".rw2"}
XmpKey = Annotated[str, StringConstraints(pattern=r"^\w+\:\w+")] XmpKey = Annotated[str, StringConstraints(pattern=r"^\w+\:\w+")]
type Caption = str type Caption = str
type Keywords = list[str] type Keywords = list[str]
type Color = str | None type Color = int
type XmpMetadata = dict[str, Any] type XmpMetadata = dict[str, Any]
XMP_KEYS_CAPTION: list[XmpKey] = [ XMP_KEYS_CAPTION: list[XmpKey] = [
@@ -133,7 +130,7 @@ class MetadataWriter:
if not include_prefix and existing_no_prefix and not override: if not include_prefix and existing_no_prefix and not override:
return return
if include_prefix: if include_prefix:
caption = settings.metadata_prefix.join([existing_no_prefix, f" {caption}"]) caption = f"{existing_no_prefix}{_prefixed(caption)}"
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)
@@ -146,14 +143,13 @@ class MetadataWriter:
self.set_metadata(metadata_tags) self.set_metadata(metadata_tags)
def write_color(self, color: Color): def write_color(self, color: Color):
# TODO self.set_metadata({"XMP-darktable:Colorlabels": color})
...
def write( def write(
self, self,
caption: Caption | None = None, caption: Caption | None = None,
keywords: Keywords | None = None, keywords: Keywords | None = None,
color: Color = None, color: Color | None = None,
): ):
if caption is not None: if caption is not None:
self.write_caption(caption) self.write_caption(caption)
+109 -18
View File
@@ -37,22 +37,56 @@ def test_read_metadata(make_dng_file, pic):
file_path = make_dng_file(pic) file_path = make_dng_file(pic)
result = MetadataWriter(file_path) result = MetadataWriter(file_path)
assert result assert result
assert result.metadata
assert isinstance(result.metadata, dict)
@pytest.mark.parametrize(
"pic, expected_keywords",
[
(PATHS_DNG_EXAMPLES[0], ["architektur", "zeit|abend"]),
(PATHS_DNG_EXAMPLES[1], ["architektur"]),
(PATHS_DNG_EXAMPLES[2], ["morgen", "tiere|vögel"]),
(
PATHS_DNG_EXAMPLES[3],
[
"abend",
"architektur",
"bewegungsunschärfe",
"lichter",
"mainz",
"spiegelung",
"wasser",
],
),
(
PATHS_DNG_EXAMPLES[4],
["grau", "landschaft", "landschaft|weinberge", "personen", "tiefe", "weg"],
),
],
)
def test_get_keywords(make_dng_file, pic, expected_keywords):
path = make_dng_file(pic)
result = MetadataWriter(path).get_keywords()
assert sorted(result) == sorted(expected_keywords)
def test_prefixed(): def test_prefixed():
txt = "bla_bla" txt = "bla_bla"
txt_prefixed = _prefixed(txt) txt_prefixed = _prefixed(txt)
assert txt_prefixed == "photools: bla_bla" assert txt_prefixed == "photools|bla_bla"
def test_is_prefixed(): @pytest.mark.parametrize(
txt_prefixed = "photools: some text" "txt, expected_result",
txt_not_prefixed = "prefix is missing here" [
txt_false_prefix = "some_perfix: but not the right one" ("photools|some text", True),
("prefix is missing here", False),
assert _is_prefixed(txt_prefixed) ("some_perfix|but not the right one", False),
assert not _is_prefixed(txt_not_prefixed) ],
assert not _is_prefixed(txt_false_prefix) )
def test_is_prefixed(txt, expected_result):
assert _is_prefixed(txt) == expected_result
@pytest.mark.parametrize("pic", PATHS_DNG_EXAMPLES) @pytest.mark.parametrize("pic", PATHS_DNG_EXAMPLES)
@@ -74,7 +108,7 @@ def test_set_caption(make_dng_file, pic):
assert metadata_writer.sidecar_path.exists() # ... but now! assert metadata_writer.sidecar_path.exists() # ... but now!
result = metadata_writer.get_caption() result = metadata_writer.get_caption()
assert result assert result
assert result == "photools: This is a test" assert result == "photools|This is a test"
def test_set_caption_on_existing(metadata_writer_on_existing): def test_set_caption_on_existing(metadata_writer_on_existing):
@@ -86,32 +120,89 @@ def test_set_caption_on_existing(metadata_writer_on_existing):
metadata_writer_on_existing.write_caption("Another caption") metadata_writer_on_existing.write_caption("Another caption")
assert ( assert (
metadata_writer_on_existing.get_caption() metadata_writer_on_existing.get_caption()
== "Let's pretend this is the caption of the original image. photools: Another caption" == "Let's pretend this is the caption of the original image. photools|Another caption"
) )
metadata_writer_on_existing.write_caption("Updated caption") metadata_writer_on_existing.write_caption("Updated caption")
assert ( assert (
metadata_writer_on_existing.get_caption() metadata_writer_on_existing.get_caption()
== "Let's pretend this is the caption of the original image. photools: Updated caption" == "Let's pretend this is the caption of the original image. photools|Updated caption"
) )
metadata_writer_on_existing.write_caption("Don't do this!", override=False) metadata_writer_on_existing.write_caption("Don't do this!", override=False)
assert ( assert (
metadata_writer_on_existing.get_caption() metadata_writer_on_existing.get_caption()
== "Let's pretend this is the caption of the original image. photools: Updated caption" == "Let's pretend this is the caption of the original image. photools|Updated caption"
) )
@pytest.mark.parametrize("pic", PATHS_DNG_EXAMPLES) @pytest.mark.parametrize(
def test_set_keywords(make_dng_file, pic): "pic, expected_keywords",
[
(
PATHS_DNG_EXAMPLES[0],
[
"architektur",
"zeit|abend",
"photools|hund",
"photools|katze",
"photools|maus",
],
),
(
PATHS_DNG_EXAMPLES[1],
["architektur", "photools|hund", "photools|katze", "photools|maus"],
),
(
PATHS_DNG_EXAMPLES[2],
[
"morgen",
"tiere|vögel",
"photools|hund",
"photools|katze",
"photools|maus",
],
),
(
PATHS_DNG_EXAMPLES[3],
[
"abend",
"architektur",
"bewegungsunschärfe",
"lichter",
"mainz",
"spiegelung",
"wasser",
"photools|hund",
"photools|katze",
"photools|maus",
],
),
(
PATHS_DNG_EXAMPLES[4],
[
"grau",
"landschaft",
"landschaft|weinberge",
"personen",
"tiefe",
"weg",
"photools|hund",
"photools|katze",
"photools|maus",
],
),
],
)
def test_set_keywords(make_dng_file, pic, expected_keywords):
metadata_writer = MetadataWriter(make_dng_file(pic)) metadata_writer = MetadataWriter(make_dng_file(pic))
metadata_writer.write_keywords(["hund", "katze", "maus"]) metadata_writer.write_keywords(["hund", "katze", "maus"])
result = metadata_writer.get_keywords() result = metadata_writer.get_keywords()
assert result assert result
assert isinstance(result, list) assert isinstance(result, list)
assert "photools: hund" in result assert "photools|hund" in result
assert "photools: katze" in result assert "photools|katze" in result
assert "photools: maus" in result assert "photools|maus" in result
def test_set_keywords_on_existing(): def test_set_keywords_on_existing():