From 2c428e52ccc512b482040d5df68856ea21a56b41 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Mon, 15 Feb 2021 11:12:23 +0000 Subject: [PATCH] made name of entry function configurable (defaults to run); adjusted MissingRunFunctionError -> MissingEntryFunctionError --- modman/modman.py | 15 ++++++++------- modman/module.py | 11 ++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/modman/modman.py b/modman/modman.py index 2ef54c6..076593b 100644 --- a/modman/modman.py +++ b/modman/modman.py @@ -1,14 +1,15 @@ from time import sleep from pathlib import Path -from module import Module, ModuleLoadError, MissingRunFunctionError +from module import Module, ModuleLoadError, MissingEntryFunctionError from utils import printable_exception class ModuleManager: - def __init__(self, folder): + def __init__(self, folder, entry="run"): #TODO "run" as function name? or is "main" better? self.folder = Path(folder) + self.entry = entry self.modules = {} self.update() @@ -47,15 +48,15 @@ class ModuleManager: print(f"{fn} already cached... skipping load") continue - mod = Module(fn) + mod = Module(fn, self.entry) print("loading:", mod) try: mod.load() except ModuleLoadError as e: print(f"loading {fn} raised", printable_exception(e)) - except MissingRunFunctionError: - print(f"missing run function in {fn}") + except MissingEntryFunctionError: + print(f"missing function {self.entry}() in {fn}") self.modules[fn] = mod @@ -69,8 +70,8 @@ class ModuleManager: print(f"{mod.name} starting") try: mod.start() - except MissingRunFunctionError: - print(f"{mod.name} has no function to run") + except MissingEntryFunctionError: + print(f"{mod.name} has no function {self.entry}() to run") def get_fnames(self): diff --git a/modman/module.py b/modman/module.py index 34f269a..90a58cc 100644 --- a/modman/module.py +++ b/modman/module.py @@ -7,9 +7,10 @@ from utils import printable_exception class Module: - def __init__(self, fname): + def __init__(self, fname, entry): fname = Path(fname) self.fname = fname + self.entry = entry self.name = fname.stem.replace("-", "_") self.mtime = self.get_mtime() self.mod = self.func = self.task = None @@ -17,7 +18,7 @@ class Module: def start(self): if not self.func: - raise MissingRunFunctionError + raise MissingEntryFunctionError self.task = task = Task(self.func, self.name) task.start() @@ -41,9 +42,9 @@ class Module: raise ModuleLoadError(printable_exception(e)) from e try: - self.func = mod.run #TODO this function name? is "main" better? + self.func = getattr(mod, self.entry) except AttributeError as e: - raise MissingRunFunctionError from e + raise MissingEntryFunctionError from e def file_exists(self): @@ -66,7 +67,7 @@ class Module: class ModuleLoadError(Exception): pass -class MissingRunFunctionError(Exception): +class MissingEntryFunctionError(Exception): pass