convert function to class

This commit is contained in:
2021-02-06 13:22:48 +00:00
parent 3a4b24f52d
commit 296c091ef7

View File

@ -8,31 +8,39 @@ from importing import load_module
from utils import printable_exception
def load_files(folder):
folder = Path(folder)
fnames = folder.glob("*.py")
fnames = sorted(fnames)
class ModuleManager:
def __init__(self, folder):
self.folder = Path(folder)
self.modules = {}
self.update()
def update(self):
for fn in self.fnames:
print(fn, fn.stat().st_mtime)
name = fn.stem
try:
mod = load_module(name, fn)
except Exception as e:
print(f"loading {name} raised", printable_exception(e))
continue
try:
func = mod.run
except AttributeError:
print(f"missing run function in {name}")
continue
self.modules[name] = func
@property
def fnames(self):
fns = self.folder.glob("*.py")
return sorted(fns)
res = {}
for fn in fnames:
print(fn, fn.stat().st_mtime)
name = fn.stem
try:
mod = load_module(name, fn)
except Exception as e:
print(f"loading {name} raised", printable_exception(e))
continue
try:
func = mod.run
except AttributeError:
print(f"missing run function in {name}")
continue
res[name] = func
return res
folder = "scripts"
mods = load_files(folder)
modman = ModuleManager(folder)
mods = modman.modules
while True:
with ThreadPoolExecutor() as ex: