aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/extractor/generic
diff options
context:
space:
mode:
Diffstat (limited to 'bsie/extractor/generic')
-rw-r--r--bsie/extractor/generic/__init__.py16
-rw-r--r--bsie/extractor/generic/constant.py52
-rw-r--r--bsie/extractor/generic/path.py70
-rw-r--r--bsie/extractor/generic/stat.py71
4 files changed, 209 insertions, 0 deletions
diff --git a/bsie/extractor/generic/__init__.py b/bsie/extractor/generic/__init__.py
new file mode 100644
index 0000000..0cb7e7f
--- /dev/null
+++ b/bsie/extractor/generic/__init__.py
@@ -0,0 +1,16 @@
+"""Generic extractors focus on information that is typically available on all
+files. Examples include file system information (file name and size, mime type,
+etc.) and information that is independent of the actual file (constant triples,
+host platform infos, current time, etc.).
+
+Part of the bsie module.
+A copy of the license is provided with the project.
+Author: Matthias Baumgartner, 2022
+"""
+# imports
+import typing
+
+# exports
+__all__: typing.Sequence[str] = []
+
+## EOF ##
diff --git a/bsie/extractor/generic/constant.py b/bsie/extractor/generic/constant.py
new file mode 100644
index 0000000..e243131
--- /dev/null
+++ b/bsie/extractor/generic/constant.py
@@ -0,0 +1,52 @@
+"""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
+
+# inner-module imports
+from bsie.base import extractor
+from bsie.utils.bsfs import URI
+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
+
+ def __init__(
+ self,
+ schema: str,
+ tuples: typing.Iterable[typing.Tuple[URI, typing.Any]],
+ ):
+ self._schema = schema
+ self._tuples = tuples
+ # FIXME: use schema instance for predicate checking
+ #self._tuples = [(pred, value) for pred, value in tuples if pred in schema]
+ # FIXME: use schema instance for value checking
+
+ def schema(self) -> str:
+ return self._schema
+
+ def extract(
+ self,
+ subject: Node,
+ content: None,
+ predicates: typing.Iterable[URI],
+ ) -> typing.Iterator[typing.Tuple[Node, URI, typing.Any]]:
+ for pred, value in self._tuples:
+ if pred in predicates:
+ yield subject, pred, value
+
+## EOF ##
diff --git a/bsie/extractor/generic/path.py b/bsie/extractor/generic/path.py
new file mode 100644
index 0000000..c39bbd2
--- /dev/null
+++ b/bsie/extractor/generic/path.py
@@ -0,0 +1,70 @@
+"""
+
+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 ##
diff --git a/bsie/extractor/generic/stat.py b/bsie/extractor/generic/stat.py
new file mode 100644
index 0000000..d74369c
--- /dev/null
+++ b/bsie/extractor/generic/stat.py
@@ -0,0 +1,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 ##