aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/extractor/generic/stat.py
blob: d74369c664f8f1ba360d7c1e33bb8ad688cde979 (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
60
61
62
63
64
65
66
67
68
69
70
71
"""Extract information from the file system, such as filesize.

Part of the bsie module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
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.stat


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


## code ##

class Stat(extractor.Extractor):
    """Extract information from the file system."""

    CONTENT_READER = bsie.reader.stat.Stat

    def __init__(self):
        self.__callmap = {
            ns.bse.filesize:    self.__filesize,
            }

    def schema(self) -> str:
        return '''
            bse:filesize a bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:integer ;
                rdf:label "File size"^^xsd:string ;
                schema:description "File size 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 __filesize(self, content: CONTENT_READER.CONTENT_TYPE) -> int:
        """Return the file size."""
        try:
            return content.st_size
        except Exception:
            # FIXME: some kind of error reporting (e.g. logging)
            return None

## EOF ##