diff options
author | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:22:31 +0100 |
---|---|---|
committer | Matthias Baumgartner <dev@igsor.net> | 2022-12-18 14:22:31 +0100 |
commit | 7582c280ad5324a2f0427999911c7e7abc14a6ab (patch) | |
tree | 0a59bbfe1c44d3497daad9f25ff9e7eb2bf9eb82 /bsie/extractor/generic/constant.py | |
parent | cb49e4567a18de6851286ff672e54f9a91865fe9 (diff) | |
parent | 057e09d6537bf5c39815661a75819081e3e5fda7 (diff) | |
download | bsie-7582c280ad5324a2f0427999911c7e7abc14a6ab.tar.gz bsie-7582c280ad5324a2f0427999911c7e7abc14a6ab.tar.bz2 bsie-7582c280ad5324a2f0427999911c7e7abc14a6ab.zip |
Merge branch 'develop' into main
Diffstat (limited to 'bsie/extractor/generic/constant.py')
-rw-r--r-- | bsie/extractor/generic/constant.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/bsie/extractor/generic/constant.py b/bsie/extractor/generic/constant.py new file mode 100644 index 0000000..11384e6 --- /dev/null +++ b/bsie/extractor/generic/constant.py @@ -0,0 +1,57 @@ +"""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 +""" +# imports +import typing + +# bsie imports +from bsie.base import extractor +from bsie.utils import bsfs, node + +# exports +__all__: typing.Sequence[str] = ( + 'Constant', + ) + + +## code ## + +class Constant(extractor.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.Schema.from_string(extractor.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 ## |