made name of entry function configurable (defaults to run); adjusted MissingRunFunctionError -> MissingEntryFunctionError
This commit is contained in:
+8
-7
@@ -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):
|
||||
|
||||
+6
-5
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user