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/path.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/path.py')
-rw-r--r-- | bsie/extractor/generic/path.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/bsie/extractor/generic/path.py b/bsie/extractor/generic/path.py new file mode 100644 index 0000000..7018e12 --- /dev/null +++ b/bsie/extractor/generic/path.py @@ -0,0 +1,74 @@ +""" + +Part of the bsie module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import os +import typing + +# bsie imports +from bsie.base import extractor +from bsie.utils import bsfs, node, ns + +# exports +__all__: typing.Sequence[str] = ( + 'Path', + ) + + +## code ## + +class Path(extractor.Extractor): + """Extract information from file's path.""" + + CONTENT_READER = 'bsie.reader.path.Path' + + # mapping from predicate to handler function. + _callmap: typing.Dict[bsfs.schema.Predicate, typing.Callable[[str], typing.Any]] + + def __init__(self): + super().__init__(bsfs.schema.Schema.from_string(extractor.SCHEMA_PREAMBLE + ''' + bse:filename rdfs:subClassOf bsfs:Predicate ; + rdfs:domain bsfs:File ; + rdfs:range xsd:string ; + rdfs:label "File name"^^xsd:string ; + schema:description "Filename of entity in some filesystem."^^xsd:string ; + bsfs:unique "false"^^xsd:boolean . + ''')) + self._callmap = { + self.schema.predicate(ns.bse.filename): self.__filename, + } + + def extract( + self, + subject: node.Node, + content: str, + principals: typing.Iterable[bsfs.schema.Predicate], + ) -> typing.Iterator[typing.Tuple[node.Node, bsfs.schema.Predicate, typing.Any]]: + for pred in principals: + # 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) -> typing.Optional[str]: + try: + return os.path.basename(path) + except Exception: # pylint: disable=broad-except # we explicitly want to catch everything + # some error, skip + # FIXME: some kind of error reporting (e.g. logging)? + # Options: (a) Fail silently (current); (b) Skip and report to log; + # (c) Raise ExtractorError (aborts extraction); (d) separate content type + # checks from basename errors (report content type errors, skip basename + # errors) + return None + +## EOF ## |