"""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.bsfs import URI, schema as _schema from bsie.utils.node import 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[_schema.Predicate, typing.Any], ...] def __init__( self, schema: str, tuples: typing.Iterable[typing.Tuple[URI, typing.Any]], ): super().__init__(_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) # FIXME: 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, content: None, predicates: typing.Iterable[_schema.Predicate], ) -> typing.Iterator[typing.Tuple[Node, _schema.Predicate, typing.Any]]: for pred, value in self._tuples: if pred in predicates: yield subject, pred, value ## EOF ##