aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/reader/exif.py
blob: 2d0428b0280eac161716575025cb6f7f6bac92b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# standard imports
import typing

# external imports
import pyexiv2

# bsie imports
from bsie.utils import errors, filematcher

# inner-module imports
from . import base

# constants
MATCH_RULE = 'mime=image/jpeg'

# exports
__all__: typing.Sequence[str] = (
    'Exif',
    )


## code ##

class Exif(base.Reader):
    """Use pyexiv2 to read exif metadata from image files."""

    def __init__(self):
        self._match = filematcher.parse(MATCH_RULE)

    def __call__(self, path: str) -> dict:
        # perform quick checks first
        if not self._match(path):
            raise errors.UnsupportedFileFormatError(path)

        try:
            # open the file
            img = pyexiv2.Image(path)
            # read metadata
            return img.read_exif()
        except (TypeError, OSError, RuntimeError) as err:
            raise errors.ReaderError(path) from err

## EOF ##