From 1f270160a65860edb48e4c061daff343c7cb7f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20Crespo?= Date: Thu, 11 Jul 2024 18:59:49 +0000 Subject: [PATCH] Load command handlers dynamically --- internal/runner/main.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/internal/runner/main.py b/internal/runner/main.py index 9980b47..4f3cb1b 100644 --- a/internal/runner/main.py +++ b/internal/runner/main.py @@ -13,11 +13,18 @@ class Command: args: Dict[str, Any] = field(default_factory=dict) +class UnknownCommandError(Exception): + pass + + class ExitError(Exception): pass class Runner: + ALLOWED_COMMANDS = ("validate", "make", "exit") + ALLOWED_COMMANDS_LIST = ", ".join(ALLOWED_COMMANDS) + def __init__(self, cmd, stdout): self.cmd = cmd self.stdout = stdout @@ -28,14 +35,8 @@ def run(self): resp = {} try: - if name == "validate": - resp = self.validate(args) - elif name == "make": - resp = self.make(args) - elif name == "exit": - self.exit(args) - else: - raise Exception("Unknown command") + ret = self.get_handler(name)(args) + resp.update(ret) except ExitError: raise except BaseException as err: @@ -44,17 +45,27 @@ def run(self): self.write(self.stdout, resp) - def validate(self, args): + def get_handler(self, name): + if name not in self.ALLOWED_COMMANDS: + raise UnknownCommandError( + f"'{name}' is not a valid command, use: {self.ALLOWED_COMMANDS_LIST}" + ) + handler = getattr(self, f"{name}_handler") + if handler is None: + raise UnknownCommandError(f"'{name}' does not have a handler") + return handler + + def validate_handler(self, args): bag = Bag(args.get("path")) bag.validate(processes=multiprocessing.cpu_count()) return {"valid": True} - def make(self, args): + def make_handler(self, args): bag_dir = args.pop("path") bag = make_bag(bag_dir, **args) return {"version": bag.version} - def exit(self, args): + def exit_handler(self, args): raise ExitError @staticmethod