Skip to content

Commit

Permalink
Improve metadata handler by operations.
Browse files Browse the repository at this point in the history
Consecutive calls using the same arguments are cached and served from
cache. This applies for the backend setup, get_metadata and get_keys, as
these are rather expensive calls.

This may be problematic if metadata is written to the image in-between
calls.
  • Loading branch information
jcjgraf committed Jan 8, 2023
1 parent 2999695 commit 4f829cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vimiv/plugins/metadata_piexif.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

import contextlib
import functools
from pathlib import Path
from typing import Any, Sequence, Iterable, Optional, Dict

Expand All @@ -25,6 +26,7 @@
_logger = log.module_logger(__name__)


@functools.lru_cache(1)
def prepare_backend(path: Path) -> Optional[Dict[str, Any]]:
"""Extract all metadata from the image."""
try:
Expand Down Expand Up @@ -78,6 +80,7 @@ def get_date_time(path: Path) -> str:


@exif.register(exif.Operations.get_metadata)
@functools.lru_cache(1)
def get_metadata(path: Path, desired_keys: Sequence[str]) -> exif.MetadataDictT:
"""Get value of all desired keys."""
metadata = prepare_backend(path)
Expand Down Expand Up @@ -126,6 +129,7 @@ def get_metadata(path: Path, desired_keys: Sequence[str]) -> exif.MetadataDictT:


@exif.register(exif.Operations.get_keys)
@functools.lru_cache(1)
def get_keys(path: Path) -> Iterable[str]:
"""Retrieve the key of all metadata values available in the current image."""
metadata = prepare_backend(path)
Expand Down
4 changes: 4 additions & 0 deletions vimiv/plugins/metadata_py3exiv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import contextlib
import functools
import itertools
from pathlib import Path
from typing import Any, Sequence, Iterable, Optional
Expand All @@ -25,6 +26,7 @@
_logger = log.module_logger(__name__)


@functools.lru_cache(1)
def prepare_backend(path: Path) -> Optional[pyexiv2.ImageMetadata]:
"""Load metadata from image."""
try:
Expand Down Expand Up @@ -81,6 +83,7 @@ def get_date_time(path: Path) -> str:


@exif.register(exif.Operations.get_metadata)
@functools.lru_cache(1)
def get_metadata(path: Path, desired_keys: Sequence[str]) -> exif.MetadataDictT:
"""Get value of all desired keys."""
metadata = prepare_backend(path)
Expand Down Expand Up @@ -120,6 +123,7 @@ def get_metadata(path: Path, desired_keys: Sequence[str]) -> exif.MetadataDictT:


@exif.register(exif.Operations.get_keys)
@functools.lru_cache(1)
def get_keys(path: Path) -> Iterable[str]:
"""Retrieve the key of all metadata values available in the current image."""
metadata = prepare_backend(path)
Expand Down

0 comments on commit 4f829cb

Please sign in to comment.