From 9307671fcebfebd88b41dba72e53e4b5ded9e44e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20K=C3=B6rner?= Date: Thu, 13 Jul 2023 18:53:00 +0200 Subject: [PATCH] Make kosh easier to interactively debug --> kosh.setup() can now be called without it starting to serve() Example (python shell): >>> # still need to set the CLI args (simulate) >>> import sys >>> sys.argv[:] = ["__main__", "--config_file", "kosh.ini", "--log_level", "DEBUG"] >>> # now "start" kosh >>> import kosh.kosh >>> app = kosh.kosh.kosh() >>> app.setup() >>> # now we can manipulate the kosh instance, e.g. >>> from kosh.utility.instance import instance >>> lexicon = instance.lexicons["hoenig"] >>> from kosh.elastic.search import search >>> search.entries(lexicon, "lemma_ksh", "aach", "term", 10) ... # ... --- kosh/kosh.py | 80 +++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/kosh/kosh.py b/kosh/kosh.py index 5c1ed11..a67f9b3 100644 --- a/kosh/kosh.py +++ b/kosh/kosh.py @@ -54,44 +54,9 @@ def main(self) -> None: todo: docs """ try: - instance.config = ConfigParser() - instance.config.read_dict(defaultconfig) - logger().info("Started kosh with pid %s", getpid()) - - root = f"{path.dirname(__file__)}/api" - modules = [i for _, i, _ in iter_modules([root]) if i[0] != ("_")] - logger().info("Loaded API endpoint modules %s", modules) - - instance.modules = [ - import_module(f"kosh.api.{module}").__dict__[module] - for module in modules - ] - - for arg in [i for i in argv if i.startswith("--")]: - try: - module = f"kosh.param.{arg[2:]}" - import_module(module).__dict__[arg[2:]](argv) - except Exception: - exit(f"Invalid parameter or argument to {arg[2:]}") + self.setup() config = dotdictionary(instance.config["data"]) - connections.create_connection(hosts=[config.host]) - logger().info("Connecting to Elasticsearch host %s", config.host) - - instance.lexicons = { - lexicon.uid: lexicon - for lexicon in index.lookup(config.root, config.spec) - } - - instance.query_types = [ - query_type - for query_type in instance.config["query_types"] - if query_type not in instance.config.defaults() - and instance.config.getboolean("query_types", query_type) - ] - - for lexicon in instance.lexicons.values(): - index.update(lexicon) self.serve() self.watch() if int(config.sync) > 0 else pause() @@ -106,6 +71,49 @@ def main(self) -> None: finally: logger().info("Stopped kosh with pid %s", getpid()) + def setup(self) -> None: + """ + todo: docs + """ + instance.config = ConfigParser() + instance.config.read_dict(defaultconfig) + logger().info("Started kosh with pid %s", getpid()) + + root = f"{path.dirname(__file__)}/api" + modules = [i for _, i, _ in iter_modules([root]) if i[0] != ("_")] + logger().info("Loaded API endpoint modules %s", modules) + + instance.modules = [ + import_module(f"kosh.api.{module}").__dict__[module] + for module in modules + ] + + for arg in [i for i in argv if i.startswith("--")]: + try: + module = f"kosh.param.{arg[2:]}" + import_module(module).__dict__[arg[2:]](argv) + except Exception: + exit(f"Invalid parameter or argument to {arg[2:]}") + + config = dotdictionary(instance.config["data"]) + connections.create_connection(hosts=[config.host]) + logger().info("Connecting to Elasticsearch host %s", config.host) + + instance.lexicons = { + lexicon.uid: lexicon + for lexicon in index.lookup(config.root, config.spec) + } + + instance.query_types = [ + query_type + for query_type in instance.config["query_types"] + if query_type not in instance.config.defaults() + and instance.config.getboolean("query_types", query_type) + ] + + for lexicon in instance.lexicons.values(): + index.update(lexicon) + def serve(self) -> None: """ todo: docs