diff options
Diffstat (limited to 'bsie/apps')
-rw-r--r-- | bsie/apps/__init__.py | 44 | ||||
-rw-r--r-- | bsie/apps/_loader.py | 47 | ||||
-rw-r--r-- | bsie/apps/default_config.yaml | 19 | ||||
-rw-r--r-- | bsie/apps/index.py | 49 | ||||
-rw-r--r-- | bsie/apps/info.py | 48 |
5 files changed, 121 insertions, 86 deletions
diff --git a/bsie/apps/__init__.py b/bsie/apps/__init__.py index 1c3d0f9..2fe4795 100644 --- a/bsie/apps/__init__.py +++ b/bsie/apps/__init__.py @@ -1,12 +1,13 @@ -""" - -Part of the bsie module. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 +#!/usr/bin/env python3 +"""BSIE tools. """ # standard imports +import argparse import typing +# bsie imports +import bsie + # inner-module imports from .index import main as index from .info import main as info @@ -15,6 +16,39 @@ from .info import main as info __all__: typing.Sequence[str] = ( 'index', 'info', + 'main', ) +# config +apps = { + 'index' : index, + 'info' : info, + } + + +## code ## + +def main(argv=None): + """Black Star File System maintenance tools.""" + parser = argparse.ArgumentParser(description=main.__doc__, prog='bsie') + # version + parser.add_argument('--version', action='version', + version='%(prog)s version {}.{}.{}'.format(*bsie.version_info)) # pylint: disable=C0209 + # application selection + parser.add_argument('app', choices=apps.keys(), + help='Select the application to run.') + # dangling args + parser.add_argument('rest', nargs=argparse.REMAINDER) + # parse + args = parser.parse_args(argv) + # run application + apps[args.app](args.rest) + + +## main ## + +if __name__ == '__main__': + import sys + main(sys.argv[1:]) + ## EOF ## diff --git a/bsie/apps/_loader.py b/bsie/apps/_loader.py new file mode 100644 index 0000000..6411f10 --- /dev/null +++ b/bsie/apps/_loader.py @@ -0,0 +1,47 @@ + +# standard imports +import typing + +# external imports +import yaml + +# bsie imports +from bsie.extractor import ExtractorBuilder +from bsie.lib import PipelineBuilder +from bsie.lib.pipeline import Pipeline +from bsie.reader import ReaderBuilder + +# constants +DEFAULT_CONFIG_FILE = 'default_config.yaml' + +# exports +__all__: typing.Sequence[str] = ( + 'DEFAULT_CONFIG_FILE', + 'load_pipeline', + ) + + +## code ## + +def load_pipeline(path: str) -> Pipeline: + """Load a pipeline according to a config at *path*.""" + # load config file + with open(path, 'rt', encoding='utf-8') as ifile: + cfg = yaml.safe_load(ifile) + + # reader builder + rbuild = ReaderBuilder(cfg['ReaderBuilder']) + # extractor builder + ebuild = ExtractorBuilder(cfg['ExtractorBuilder']) + # pipeline builder + pbuild = PipelineBuilder( + rbuild, + ebuild, + ) + # build pipeline + pipeline = pbuild.build() + + # return pipeline + return pipeline + +## EOF ## diff --git a/bsie/apps/default_config.yaml b/bsie/apps/default_config.yaml new file mode 100644 index 0000000..a59b0f3 --- /dev/null +++ b/bsie/apps/default_config.yaml @@ -0,0 +1,19 @@ + +ReaderBuilder: {} + +ExtractorBuilder: + + - bsie.extractor.preview.Preview: + max_sides: [50, 100, 200,400] + + - bsie.extractor.generic.path.Path: {} + + - bsie.extractor.generic.stat.Stat: {} + + - bsie.extractor.image.colors_spatial.ColorsSpatial: + width: 32 + height: 32 + exp: 4 + + - bsie.extractor.image.photometrics.Exif: {} + diff --git a/bsie/apps/index.py b/bsie/apps/index.py index 8798c49..d64e8c2 100644 --- a/bsie/apps/index.py +++ b/bsie/apps/index.py @@ -1,20 +1,16 @@ -""" -Part of the bsie module. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import argparse import os import typing # bsie imports -from bsie.extractor import ExtractorBuilder -from bsie.lib import BSIE, PipelineBuilder, DefaultNamingPolicy -from bsie.reader import ReaderBuilder +from bsie.lib import BSIE, DefaultNamingPolicy from bsie.utils import bsfs, errors, node as node_ +# inner-module imports +from . import _loader + # exports __all__: typing.Sequence[str] = ( 'main', @@ -26,6 +22,9 @@ __all__: typing.Sequence[str] = ( def main(argv): """Index files or directories into BSFS.""" parser = argparse.ArgumentParser(description=main.__doc__, prog='index') + parser.add_argument('--config', type=str, + default=os.path.join(os.path.dirname(__file__), _loader.DEFAULT_CONFIG_FILE), + help='Path to the config file.') parser.add_argument('--host', type=bsfs.URI, default=bsfs.URI('http://example.com'), help='') parser.add_argument('--user', type=str, default='me', @@ -44,39 +43,8 @@ def main(argv): help='') args = parser.parse_args(argv) - # FIXME: Read reader/extractor configs from a config file - # reader builder - rbuild = ReaderBuilder() - # extractor builder - ebuild = ExtractorBuilder([ - {'bsie.extractor.preview.Preview': { - 'max_sides': [50], - }}, - {'bsie.extractor.generic.path.Path': {}}, - {'bsie.extractor.generic.stat.Stat': {}}, - {'bsie.extractor.generic.constant.Constant': dict( - tuples=[('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I')], - schema=''' - bse:author rdfs:subClassOf bsfs:Predicate ; - rdfs:domain bsfs:Entity ; - rdfs:range xsd:string ; - bsfs:unique "true"^^xsd:boolean . - ''', - )}, - {'bsie.extractor.image.colors_spatial.ColorsSpatial': { - 'width': 2, - 'height': 2, - 'exp': 2, - }}, - ]) - # pipeline builder - pbuild = PipelineBuilder( - rbuild, - ebuild, - ) - # build pipeline - pipeline = pbuild.build() + pipeline = _loader.load_pipeline(args.config) # build the naming policy naming_policy = DefaultNamingPolicy( host=args.host, @@ -127,7 +95,6 @@ def main(argv): return store - ## main ## if __name__ == '__main__': diff --git a/bsie/apps/info.py b/bsie/apps/info.py index 750aedc..e27b70b 100644 --- a/bsie/apps/info.py +++ b/bsie/apps/info.py @@ -1,20 +1,16 @@ -""" -Part of the bsie module. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import argparse +import os import sys import typing # bsie imports -from bsie.extractor import ExtractorBuilder -from bsie.lib import PipelineBuilder -from bsie.reader import ReaderBuilder from bsie.utils import bsfs, errors +# inner-module imports +from . import _loader + # exports __all__: typing.Sequence[str] = ( 'main', @@ -26,43 +22,15 @@ __all__: typing.Sequence[str] = ( def main(argv): """Show information from BSIE.""" parser = argparse.ArgumentParser(description=main.__doc__, prog='info') + parser.add_argument('--config', type=str, + default=os.path.join(os.path.dirname(__file__), _loader.DEFAULT_CONFIG_FILE), + help='Path to the config file.') parser.add_argument('what', choices=('predicates', 'schema'), help='Select what information to show.') args = parser.parse_args(argv) - # FIXME: Read reader/extractor configs from a config file - # reader builder - rbuild = ReaderBuilder() - # extractor builder - ebuild = ExtractorBuilder([ - {'bsie.extractor.preview.Preview': { - 'max_sides': [50, 200], - }}, - {'bsie.extractor.generic.path.Path': {}}, - {'bsie.extractor.generic.stat.Stat': {}}, - {'bsie.extractor.generic.constant.Constant': dict( - tuples=[('http://bsfs.ai/schema/Entity#author', 'Me, myself, and I')], - schema=''' - bse:author rdfs:subClassOf bsfs:Predicate ; - rdfs:domain bsfs:Entity ; - rdfs:range xsd:string ; - bsfs:unique "true"^^xsd:boolean . - ''', - )}, - {'bsie.extractor.image.colors_spatial.ColorsSpatial': { - 'width': 2, - 'height': 2, - 'exp': 2, - }}, - ]) - # pipeline builder - pbuild = PipelineBuilder( - rbuild, - ebuild, - ) - # build pipeline - pipeline = pbuild.build() + pipeline = _loader.load_pipeline(args.config) # show info if args.what == 'predicates': |