aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs/apps/init.py
blob: 3e2ef37549b7726931655db4abf98f044cab4f76 (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
72
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 ##