aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-03-01 12:49:47 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-03-01 12:49:47 +0100
commitd822df3dad0525f35dbacda9d5a66f4756f079ff (patch)
tree1387c5a7e8fd669c11f61d04a4ab71607867ecf8
parentf9eec185bf3d857c220e5d78de75ec6713437330 (diff)
downloadbsfs-d822df3dad0525f35dbacda9d5a66f4756f079ff.tar.gz
bsfs-d822df3dad0525f35dbacda9d5a66f4756f079ff.tar.bz2
bsfs-d822df3dad0525f35dbacda9d5a66f4756f079ff.zip
Integrate main app into package
-rwxr-xr-xbsfs.app52
-rw-r--r--bsfs/apps/__init__.py38
-rw-r--r--bsfs/apps/init.py6
-rw-r--r--bsfs/apps/migrate.py1
-rw-r--r--test/apps/test_main.py42
5 files changed, 88 insertions, 51 deletions
diff --git a/bsfs.app b/bsfs.app
index babacbb..c837ca0 100755
--- a/bsfs.app
+++ b/bsfs.app
@@ -1,52 +1,6 @@
-"""BSFS tools.
-
-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 typing
-
-# module imports
-import bsfs
-import bsfs.apps
-
-# exports
-__all__: typing.Sequence[str] = (
- 'main',
- )
-
-# config
-apps = {
- 'init' : bsfs.apps.init,
- 'migrate' : bsfs.apps.migrate,
- }
-
-
-## code ##
-
-def main(argv):
- """Black Star File System maintenance tools."""
- parser = argparse.ArgumentParser(description=main.__doc__, prog='bsfs')
- # version
- parser.add_argument('--version', action='version',
- version='%(prog)s version {}.{}.{}'.format(*bsfs.version_info))
- # 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()
- # run application
- apps[args.app](args.rest)
-
-
-## main ##
-
+#!/usr/bin/env python3
if __name__ == '__main__':
+ import bsfs.apps
import sys
- main(sys.argv[1:])
+ bsfs.apps.main(sys.argv[1:])
-## EOF ##
diff --git a/bsfs/apps/__init__.py b/bsfs/apps/__init__.py
index 7efaa87..3dec9ad 100644
--- a/bsfs/apps/__init__.py
+++ b/bsfs/apps/__init__.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
"""
Part of the BlackStar filesystem (bsfs) module.
@@ -5,16 +6,53 @@ A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2022
"""
# imports
+import argparse
import typing
+# bsfs imports
+import bsfs
+
# inner-module imports
from .init import main as init
from .migrate import main as migrate
# exports
__all__: typing.Sequence[str] = (
+ 'main',
'init',
'migrate',
)
+# config
+apps = {
+ 'init' : init,
+ 'migrate' : migrate,
+ }
+
+
+## code ##
+
+def main(argv=None):
+ """Black Star File System maintenance tools."""
+ parser = argparse.ArgumentParser(description=main.__doc__, prog='bsfs')
+ # version
+ parser.add_argument('--version', action='version',
+ version='%(prog)s version {}.{}.{}'.format(*bsfs.version_info))
+ # 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/bsfs/apps/init.py b/bsfs/apps/init.py
index 3e2ef37..ec48525 100644
--- a/bsfs/apps/init.py
+++ b/bsfs/apps/init.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
"""
Part of the BlackStar filesystem (bsfs) module.
@@ -60,9 +61,10 @@ def main(argv):
# print config
if args.output is not None:
with open(args.output, mode='wt', encoding='UTF-8') as ofile:
- json.dump(config, ofile)
+ json.dump(config, ofile, indent=4)
else:
- json.dump(config, sys.stdout)
+ json.dump(config, sys.stdout, indent=4)
+ print('')
## main ##
diff --git a/bsfs/apps/migrate.py b/bsfs/apps/migrate.py
index b9d019f..cb62542 100644
--- a/bsfs/apps/migrate.py
+++ b/bsfs/apps/migrate.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python3
"""
Part of the BlackStar filesystem (bsfs) module.
diff --git a/test/apps/test_main.py b/test/apps/test_main.py
new file mode 100644
index 0000000..ae19b5e
--- /dev/null
+++ b/test/apps/test_main.py
@@ -0,0 +1,42 @@
+"""
+
+Part of the bsfs test suite.
+A copy of the license is provided with the project.
+Author: Matthias Baumgartner, 2022
+"""
+# imports
+import contextlib
+import io
+import json
+import unittest
+
+# objects to test
+from bsfs.apps import main
+
+
+## code ##
+
+class TestMain(unittest.TestCase):
+ def test_main(self):
+ # must at least pass an app
+ with contextlib.redirect_stderr(io.StringIO()):
+ self.assertRaises(SystemExit, main, [])
+ # app takes over
+ with contextlib.redirect_stderr(io.StringIO()):
+ self.assertRaises(SystemExit, main, ['init'])
+ outbuf = io.StringIO()
+ with contextlib.redirect_stdout(outbuf):
+ main(['init', 'sparql'])
+ self.assertEqual(json.loads(outbuf.getvalue()), {
+ 'Graph': {
+ 'user': 'http://example.com/me',
+ 'backend': {
+ 'SparqlStore': {}}}})
+
+
+## main ##
+
+if __name__ == '__main__':
+ unittest.main()
+
+## EOF ##