aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/extractor/generic/constant.py
blob: 938e20c6c9e55db92206d2ba29b092ba24214acc (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
49
50
51
52
53
54
55
56
57
58
59
"""The Constant extractor produces pre-specified triples.

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

# bsie imports
from bsie.utils import bsfs, node

# inner-module imports
from .. import base

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


## code ##

class Constant(base.Extractor):
    """Extract information from file's path."""

    CONTENT_READER = None

    # predicate/value pairs to be produced.
    _tuples: typing.Tuple[typing.Tuple[bsfs.schema.Predicate, typing.Any], ...]

    def __init__(
            self,
            schema: str,
            tuples: typing.Iterable[typing.Tuple[bsfs.URI, typing.Any]],
            ):
        super().__init__(bsfs.schema.from_string(base.SCHEMA_PREAMBLE + schema))
        # NOTE: Raises a KeyError if the predicate is not part of the schema
        self._tuples = tuple((self.schema.predicate(p_uri), value) for p_uri, value in tuples)
        # TODO: use schema instance for value checking

    def __eq__(self, other: typing.Any) -> bool:
        return super().__eq__(other) \
           and self._tuples == other._tuples

    def __hash__(self) -> int:
        return hash((super().__hash__(), self._tuples))

    def extract(
            self,
            subject: node.Node,
            content: None,
            principals: typing.Iterable[bsfs.schema.Predicate],
            ) -> typing.Iterator[typing.Tuple[node.Node, bsfs.schema.Predicate, typing.Any]]:
        for pred, value in self._tuples:
            if pred in principals:
                yield subject, pred, value

## EOF ##