commit 791bb6e1bbb53dd3da7ec130f1b79babc579d551
parent 7c62a86cd6e5c62acb96f1628bedaebb8bc515cd
Author: lash <dev@holbrook.no>
Date: Mon, 13 Feb 2023 14:37:19 +0000
Add chainlib module finder tool
Diffstat:
5 files changed, 102 insertions(+), 1 deletion(-)
diff --git a/ROADMAP b/ROADMAP
diff --git a/chainlib/cli/find.py b/chainlib/cli/find.py
@@ -0,0 +1,45 @@
+# standard imports
+import sys
+import os
+import logging
+from importlib import import_module
+
+logg = logging.getLogger(__name__)
+
+
+__mf = {}
+
+def execute_for_path(d, fltr=None, cmd=None, args=None, handler=None):
+ logg.debug('scanning package directory {}'.format(d))
+ fp = os.path.join(d, 'data', '.chainlib')
+ if not os.path.exists(fp):
+ return None
+ s = os.path.basename(d)
+ if fltr != None and s not in fltr:
+ logg.info('skipped chainlib module "{}" not matching search filter'.format(s))
+ return None
+ pm = __mf.get(s)
+ if pm != None:
+ logg.info('skipped chainlib module "{}" already exxcuted from {}'.format(s, pm))
+ return None
+ __mf[s] = d
+ logg.info('found chainlib module {} in {}'.format(s, d))
+ m = import_module(s + '._clf')
+ handler(m, cmd, args)
+
+
+def find_chainlib_modules(fltr=None, cmd=None, args=None, handler=None):
+ m = []
+ for p in sys.path:
+ logg.debug('scanning path is {}'.format(p))
+ (n, x) = os.path.splitext(p)
+ if x:
+ continue
+ for d in os.listdir(p):
+ dp = os.path.join(p, d)
+ if not os.path.isdir(dp):
+ continue
+ r = execute_for_path(dp, fltr=fltr, cmd=cmd, args=args, handler=handler)
+ if r != None:
+ m.append(r)
+ return m
diff --git a/chainlib/cli/log.py b/chainlib/cli/log.py
@@ -0,0 +1,13 @@
+# standard imports
+import logging
+
+logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s')
+
+
+def process_log(args, logger):
+ if args.vv:
+ logger.setLevel(logging.DEBUG)
+ elif args.v:
+ logger.setLevel(logging.INFO)
+
+ return logger
diff --git a/chainlib/runnable/find.py b/chainlib/runnable/find.py
@@ -0,0 +1,43 @@
+# standard imports
+import argparse
+import sys
+import os
+import logging
+
+# local imports
+from chainlib.cli.find import find_chainlib_modules
+
+logging.basicConfig(level=logging.WARNING)
+logg = logging.getLogger()
+
+
+ap = argparse.ArgumentParser()
+ap.add_argument('-m', '--module', type=str, action='append', help='module name to add to filter (default all modules match)')
+ap.add_argument('--arg', type=str, action='append', help='argument for command to execute')
+ap.add_argument('-v', action='store_true', help='verbose logging')
+ap.add_argument('-vv', action='store_true', help='very verbose logging')
+ap.add_argument('command', type=str, nargs='?', help='command to pass to module execution handler')
+args = ap.parse_args(sys.argv[1:])
+
+if args.vv:
+ logg.setLevel(logging.DEBUG)
+elif args.v:
+ logg.setLevel(logging.INFO)
+
+
+def default_handler(m, cmd, args):
+ r = None
+ if cmd == None:
+ r = m.default(args)
+ else:
+ fn = getattr(m, cmd)
+ r = fn(args)
+ print(r)
+
+
+def main():
+ find_chainlib_modules(fltr=args.module, cmd=args.command, args=args.arg, handler=default_handler)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/setup.cfg b/setup.cfg
@@ -3,7 +3,7 @@ name=chainlib
license=AGPLv3+
author_email=dev@holbrook.no
description=Generic blockchain access library and tooling
-version=0.4.7
+version=0.4.8
url=https://git.defalsify.org/chainlib
author=Louis Holbrook