Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Internal" Metadata Support Plugin #676

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions vimiv/plugins/metadata_general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# vim: ft=python fileencoding=utf-8 sw=4 et sts=4

# This file is part of vimiv.
# Copyright 2017-2023 Christian Karl (karlch) <karlch at protonmail dot com>
# License: GNU GPL v3, see the "LICENSE" and "AUTHORS" files for details.

"""Metadata plugin provides all sort of information not provided by EXIF, IPTC, or XMP.

Check warning on line 7 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L7

Added line #L7 was not covered by tests

Properties:
- Get data not present in Exif, ICMP or XMP.
"""

import contextlib
from typing import Any, Sequence, Iterable, Optional, Dict, Tuple, Callable

Check warning on line 14 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L13-L14

Added lines #L13 - L14 were not covered by tests

from PyQt5.QtGui import QImageReader

Check warning on line 16 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L16

Added line #L16 was not covered by tests

from vimiv.imutils import metadata
from vimiv.utils import log, files

Check warning on line 19 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L18-L19

Added lines #L18 - L19 were not covered by tests

_logger = log.module_logger(__name__)

Check warning on line 21 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L21

Added line #L21 was not covered by tests


class MetadataGeneral(metadata.MetadataPlugin):

Check warning on line 24 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L24

Added line #L24 was not covered by tests
"""Provides all sort of information not provided by EXIF, IPTC, or XMP.

Implements `get_metadata`, and `get_keys` only.
"""

def __init__(self, path: str) -> None:
self._path = path
self._reader: Optional[QImageReader] = None

Check warning on line 32 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L30-L32

Added lines #L30 - L32 were not covered by tests

# For each key, store (description, function)
self._registry: Dict[str, Tuple[str, Callable]] = {

Check warning on line 35 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L35

Added line #L35 was not covered by tests
"Vimiv.FileSize": ("File Size", self._get_filesize),
"Vimiv.XDimension": (
"Pixel X Dimension",
self._get_xdimension,
),
"Vimiv.YDimension": (
"Pixel Y Dimension",
self._get_ydimension,
),
"Vimiv.FileType": ("File Type", self._get_filetype),
}

@staticmethod
def name() -> str:

Check warning on line 49 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L48-L49

Added lines #L48 - L49 were not covered by tests
"""No backend is used, return plugin name."""
return "general"

Check warning on line 51 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L51

Added line #L51 was not covered by tests

@staticmethod
def version() -> str:

Check warning on line 54 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L53-L54

Added lines #L53 - L54 were not covered by tests
"""No backend used, return empty string."""
return ""

Check warning on line 56 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L56

Added line #L56 was not covered by tests

@property
def reader(self) -> QImageReader:

Check warning on line 59 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L58-L59

Added lines #L58 - L59 were not covered by tests
"""Return QImageReader instance of _path."""
if self._reader is None:
self._reader = QImageReader(self._path)
return self._reader

Check warning on line 63 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L61-L63

Added lines #L61 - L63 were not covered by tests

def get_metadata(self, desired_keys: Sequence[str]) -> metadata.MetadataDictT:

Check warning on line 65 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L65

Added line #L65 was not covered by tests
"""Get value of all desired keys for the current image."""
out = {}

Check warning on line 67 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L67

Added line #L67 was not covered by tests

for key in desired_keys:
with contextlib.suppress(KeyError):
desc, func = self._registry[key]
out[key] = (desc, func())

Check warning on line 72 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L69-L72

Added lines #L69 - L72 were not covered by tests

return out

Check warning on line 74 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L74

Added line #L74 was not covered by tests

def get_keys(self) -> Iterable[str]:

Check warning on line 76 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L76

Added line #L76 was not covered by tests
"""Get the keys for all metadata values available for the current image."""
return self._registry.keys()

Check warning on line 78 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L78

Added line #L78 was not covered by tests

def _get_filesize(self) -> str:

Check warning on line 80 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L80

Added line #L80 was not covered by tests
"""Get the file size."""
return files.get_size_file(self._path)

Check warning on line 82 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L82

Added line #L82 was not covered by tests

def _get_filetype(self) -> str:

Check warning on line 84 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L84

Added line #L84 was not covered by tests
"""Get the file type."""
out = files.imghdr.what(self._path)
if out:
return out
return ""

Check warning on line 89 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L86-L89

Added lines #L86 - L89 were not covered by tests

def _get_xdimension(self) -> str:

Check warning on line 91 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L91

Added line #L91 was not covered by tests
"""Get the x dimension in pixels.

Does only works for image formats supported by QT natively.
"""
return str(self.reader.size().width())

Check warning on line 96 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L96

Added line #L96 was not covered by tests

def _get_ydimension(self) -> str:

Check warning on line 98 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L98

Added line #L98 was not covered by tests
"""Get the y dimension in pixels.

Does only works for image formats supported by QT natively.
"""
return str(self.reader.size().height())

Check warning on line 103 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L103

Added line #L103 was not covered by tests


def init(*_args: Any, **_kwargs: Any) -> None:

Check warning on line 106 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L106

Added line #L106 was not covered by tests
"""Register `MetadataGeneral` as a metadata handler."""
metadata.register(MetadataGeneral)

Check warning on line 108 in vimiv/plugins/metadata_general.py

View check run for this annotation

Codecov / codecov/patch

vimiv/plugins/metadata_general.py#L108

Added line #L108 was not covered by tests
Loading