Files
morbidissimo/modman/modman.py
T
2021-02-06 18:33:08 +00:00

62 lines
1.5 KiB
Python

from pathlib import Path
from importing import load_module
from utils import printable_exception
class ModuleManager:
def __init__(self, folder):
self.folder = Path(folder)
self.modules = {}
self.mtimes = {}
self.update()
def update(self):
current_names = set()
for fn in self.fnames:
name = fn.stem
mtime = fn.stat().st_mtime
print(fn, name, mtime)
if name in self.mtimes:
if self.mtimes[name] == mtime:
print(f"{name} unchanged... will not reload")
current_names.add(name)
continue
else:
print(f"{name} changed... reloading")
self.mtimes[name] = mtime
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
current_names.add(name)
deleted_names = self.modules.keys() - current_names
for n in deleted_names:
print(f"delete cached {n}")
self.modules.pop(n, None)
self.mtimes.pop(n, None)
@property
def fnames(self):
fns = self.folder.glob("*.py")
return sorted(fns)