added support for multi-type waiting plus some cleanup

This commit is contained in:
PatchOfScotland
2022-12-15 11:31:51 +01:00
parent 380f7066e1
commit ea9a689b26
12 changed files with 516 additions and 173 deletions

View File

@ -1,13 +1,15 @@
import nbformat
import threading
from multiprocessing import Pipe
from typing import Any
from core.correctness.validation import check_input, valid_string, \
valid_dict, valid_path
from core.correctness.vars import VALID_JUPYTER_NOTEBOOK_FILENAME_CHARS, \
VALID_JUPYTER_NOTEBOOK_EXTENSIONS, VALID_VARIABLE_NAME_CHARS
from core.meow import BaseRecipe
from core.correctness.validation import check_type, valid_string, \
valid_dict, valid_path, valid_list
from core.correctness.vars import VALID_VARIABLE_NAME_CHARS, VALID_CHANNELS
from core.functionality import wait
from core.meow import BaseRecipe, BaseHandler
class JupyterNotebookRecipe(BaseRecipe):
source:str
@ -18,21 +20,11 @@ class JupyterNotebookRecipe(BaseRecipe):
self.source = source
def _is_valid_source(self, source:str)->None:
valid_path(source, extension=".ipynb", min_length=0)
if not source:
return
matched = False
for i in VALID_JUPYTER_NOTEBOOK_EXTENSIONS:
if source.endswith(i):
matched = True
if not matched:
raise ValueError(f"source '{source}' does not end with a valid "
"jupyter notebook extension.")
if source:
valid_path(source, extension=".ipynb", min_length=0)
def _is_valid_recipe(self, recipe:dict[str,Any])->None:
check_input(recipe, dict)
check_type(recipe, dict)
nbformat.validate(recipe)
def _is_valid_parameters(self, parameters:dict[str,Any])->None:
@ -44,3 +36,49 @@ class JupyterNotebookRecipe(BaseRecipe):
valid_dict(requirements, str, Any, strict=False, min_length=0)
for k in requirements.keys():
valid_string(k, VALID_VARIABLE_NAME_CHARS)
class PapermillHandler(BaseHandler):
_worker:threading.Thread
_stop_pipe:Pipe
def __init__(self, inputs:list[VALID_CHANNELS])->None:
super().__init__(inputs)
self._worker = None
self._stop_pipe = Pipe()
def run(self)->None:
all_inputs = self.inputs + [self._stop_pipe[0]]
while True:
ready = wait(all_inputs)
if self._stop_pipe[0] in ready:
return
else:
for input in self.inputs:
if input in ready:
message = input.recv()
event, rule = message
self.handle(event, rule)
def start(self)->None:
if self._worker is None:
self._worker = threading.Thread(
target=self.run,
args=[])
self._worker.daemon = True
self._worker.start()
else:
raise RuntimeWarning("Repeated calls to start have no effect.")
def stop(self)->None:
if self._worker is None:
raise RuntimeWarning("Cannot stop thread that is not started.")
else:
self._stop_pipe[1].send(1)
self._worker.join()
def handle(self, event, rule)->None:
# TODO finish implementation and test
pass
def _is_valid_inputs(self, inputs:list[VALID_CHANNELS])->None:
valid_list(inputs, VALID_CHANNELS)