aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/base/reader.py
blob: f29e451204b9769c080c121a6b371d8d9e8ac9af (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
45
46
47
48
"""The Reader classes return high-level content structures from files.

The Reader fulfills two purposes:
    First, it brokers between multiple libraries and file formats.
    Second, it separates multiple aspects of a file into distinct content types.

Part of the bsie module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
import abc
import typing

# inner-module imports
from bsie.utils.bsfs import URI, typename

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


## code ##

class Reader(abc.ABC):
    """Read and return some content from a file."""

    # In what data structure content is returned
    CONTENT_TYPE = typing.Union[typing.Any]
    # NOTE: Child classes must also assign a typing.Union even if there's
    # only one options

    def __str__(self) -> str:
        return typename(self)

    def __repr__(self) -> str:
        return f'{typename(self)}()'

    # FIXME: How about using contexts instead of calls?
    @abc.abstractmethod
    def __call__(self, path: URI) -> CONTENT_TYPE:
        """Return some content of the file at *path*.
        Raises a `ReaderError` if the reader cannot make sense of the file format.
        """

## EOF ##