""" Part of the bsie module. A copy of the license is provided with the project. Author: Matthias Baumgartner, 2022 """ # imports import os import typing # inner-module imports from bsie.base import extractor from bsie.utils import node, ns from bsie.utils.bsfs import URI import bsie.reader.path # exports __all__: typing.Sequence[str] = ( 'Path', ) ## code ## class Path(extractor.Extractor): """Extract information from file's path.""" CONTENT_READER = bsie.reader.path.Path def __init__(self): self.__callmap = { ns.bse.filename: self.__filename, } def schema(self) -> str: return ''' bse:filename a bsfs:Predicate ; rdfs:domain bsfs:Entity ; rdfs:range xsd:string ; rdf:label "File name"^^xsd:string ; schema:description "Filename of entity in some filesystem."^^xsd:string ; owl:maxCardinality "INF"^^xsd:number . ''' def extract( self, subject: node.Node, content: CONTENT_READER.CONTENT_TYPE, predicates: typing.Iterable[URI], ) -> typing.Iterator[typing.Tuple[node.Node, URI, typing.Any]]: for pred in predicates: # find callback clbk = self.__callmap.get(pred) if clbk is None: continue # get value value = clbk(content) if value is None: continue # produce triple yield subject, pred, value def __filename(self, path: str) -> str: try: return os.path.basename(path) except Exception: # FIXME: some kind of error reporting (e.g. logging) return None ## EOF ##