switched to Module class

This commit is contained in:
2021-02-07 19:00:54 +00:00
parent a3f68ec37d
commit 089da79074
3 changed files with 85 additions and 54 deletions

View File

@ -1,31 +1,11 @@
#!/usr/bin/env python3
from time import sleep
from modman import ModuleManager
from executing import Executor
folder = "scripts"
mm = ModuleManager(folder)
mods = mm.modules
with Executor() as ex:
while True:
mm.update()
ex.cleanup()
print("not done:", ex.futures)
for n, f in mods.items():
if n in ex.futures:
print(f"{n} still running... will not start again")
continue
fut = ex.run(n, f)
print(fut, "->", n)
sleep(1)
mm.run()

View File

@ -1,6 +1,7 @@
from time import sleep
from pathlib import Path
from importing import load_module
from module import Module, ModuleLoadError, MissingRunFunctionError
from utils import printable_exception
@ -9,47 +10,48 @@ class ModuleManager:
def __init__(self, folder):
self.folder = Path(folder)
self.modules = {}
self.mtimes = {}
self.update()
def run(self, sleep_time=1):
while True:
print("-" * 20)
self.update()
sleep(sleep_time)
def update(self):
current_names = set()
self._update_cached()
self._update_new()
def _update_cached(self):
for fn, mod in tuple(self.modules.items()):
if not mod.file_exists():
print(f"{fn} does not exist... removing cached module")
del self.modules[fn]
elif mod.has_changed():
print(f"{fn} has changed... removing cached module for reloading")
del self.modules[fn]
def _update_new(self):
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))
if fn in self.modules:
print(f"{fn} already cached... skipping")
continue
mod = Module(fn)
print("loading:", mod)
try:
func = mod.run
except AttributeError:
print(f"missing run function in {name}")
continue
mod.load()
except ModuleLoadError as e:
print(f"loading {fn} raised", printable_exception(e))
except MissingRunFunctionError as e:
print(f"missing run function in {fn}")
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)
self.modules[fn] = mod
@property

49
modman/module.py Normal file
View File

@ -0,0 +1,49 @@
from pathlib import Path
from importing import load_module
from utils import printable_exception
class Module:
def __init__(self, fname):
fname = Path(fname)
self.fname = fname
self.name = fname.stem.replace("-", "_")
self.mtime = self.get_mtime()
def load(self):
try:
self.mod = mod = load_module(self.name, self.fname)
except Exception as e:
raise ModuleLoadError(printable_exception(e)) from e
try:
self.run = mod.run
except AttributeError as e:
raise MissingRunFunctionError from e
def file_exists(self):
return self.fname.is_file()
def has_changed(self):
return self.mtime != self.get_mtime()
def get_mtime(self):
return self.fname.stat().st_mtime
def __str__(self):
return f"{self.fname} -> {self.name} ({self.mtime})"
class ModuleLoadError(Exception):
pass
class MissingRunFunctionError(Exception):
pass