From e94368c75468e3e94382b12705e55d396249eaca Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Sun, 18 Dec 2022 14:20:25 +0100 Subject: bsfs applications --- bsfs/apps/__init__.py | 20 ++++++++++++++ bsfs/apps/init.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ bsfs/apps/migrate.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ bsfs/utils/errors.py | 3 +++ 4 files changed, 163 insertions(+) create mode 100644 bsfs/apps/__init__.py create mode 100644 bsfs/apps/init.py create mode 100644 bsfs/apps/migrate.py (limited to 'bsfs') diff --git a/bsfs/apps/__init__.py b/bsfs/apps/__init__.py new file mode 100644 index 0000000..7efaa87 --- /dev/null +++ b/bsfs/apps/__init__.py @@ -0,0 +1,20 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import typing + +# inner-module imports +from .init import main as init +from .migrate import main as migrate + +# exports +__all__: typing.Sequence[str] = ( + 'init', + 'migrate', + ) + +## EOF ## diff --git a/bsfs/apps/init.py b/bsfs/apps/init.py new file mode 100644 index 0000000..3e2ef37 --- /dev/null +++ b/bsfs/apps/init.py @@ -0,0 +1,73 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import argparse +import json +import sys +import typing + +# bsfs imports +from bsfs.utils import errors + +# exports +__all__: typing.Sequence[str] = ( + 'main', + ) + +## code ## + +def init_sparql_store(user) -> typing.Any: + """Initialize a SparqlStore backend. Returns a configuration to load it.""" + # nothing to do for non-persistent store + # return config to storage + return { + 'Graph': { + 'user': user, + 'backend': { + 'SparqlStore': {}, + }, + } + } + + +def main(argv): + """Create a new bsfs storage structure.""" + parser = argparse.ArgumentParser(description=main.__doc__, prog='init') + # global arguments + parser.add_argument('--user', type=str, default='http://example.com/me', + help='Default user.') + parser.add_argument('--output', type=str, default=None, + help='Write the config to a file instead of standard output.') + #parser.add_argument('--schema', type=str, default=None, + # help='Initial schema.') + # storage selection + parser.add_argument('store', choices=('sparql', ), + help='Which storage to initialize.') + # storage args + # parse args + args = parser.parse_args(argv) + + # initialize selected storage + if args.store == 'sparql': + config = init_sparql_store(args.user) + else: + raise errors.UnreachableError() + + # print config + if args.output is not None: + with open(args.output, mode='wt', encoding='UTF-8') as ofile: + json.dump(config, ofile) + else: + json.dump(config, sys.stdout) + + +## main ## + +if __name__ == '__main__': + main(sys.argv[1:]) + +## EOF ## diff --git a/bsfs/apps/migrate.py b/bsfs/apps/migrate.py new file mode 100644 index 0000000..91c1661 --- /dev/null +++ b/bsfs/apps/migrate.py @@ -0,0 +1,67 @@ +""" + +Part of the BlackStar filesystem (bsfs) module. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# imports +import argparse +import json +import logging +import sys +import typing + +# bsfs imports +import bsfs + +# exports +__all__: typing.Sequence[str] = ( + 'main', + ) + + +## code ## + +logger = logging.getLogger(__name__) + +def main(argv): + """Migrate a storage structure to a modified schema.""" + parser = argparse.ArgumentParser(description=main.__doc__, prog='migrate') + parser.add_argument('--remove', action='store_true', default=False, + help='Remove classes that are not specified in the provided schema.') + parser.add_argument('config', type=str, default=None, + help='Path to the storage config file.') + parser.add_argument('schema', nargs=argparse.REMAINDER, + help='Paths to schema files. Reads from standard input if no file is supplied.') + args = parser.parse_args(argv) + + # load storage config + with open(args.config, mode='rt', encoding='UTF-8') as ifile: + config = json.load(ifile) + # open bsfs storage + graph = bsfs.Open(config) + + # initialize schema + schema = bsfs.schema.Schema.Empty() + if len(args.schema) == 0: + # assemble schema from standard input + schema = schema + bsfs.schema.Schema.from_string(sys.stdin.read()) + else: + # assemble schema from input files + for pth in args.schema: + with open(pth, mode='rt', encoding='UTF-8') as ifile: + schema = schema + bsfs.schema.Schema.from_string(ifile.read()) + + # migrate schema + graph.migrate(schema, not args.remove) + + # return the migrated storage + return graph + + +## main ## + +if __name__ == '__main__': + main(sys.argv[1:]) + +## EOF ## diff --git a/bsfs/utils/errors.py b/bsfs/utils/errors.py index 04561a2..c5e8e16 100644 --- a/bsfs/utils/errors.py +++ b/bsfs/utils/errors.py @@ -35,4 +35,7 @@ class ProgrammingError(_BSFSError): class UnreachableError(ProgrammingError): """Bravo, you've reached a point in code that should logically not be reachable.""" +class ConfigError(_BSFSError): + """User config issue.""" + ## EOF ## -- cgit v1.2.3