348 lines
12 KiB
Python
348 lines
12 KiB
Python
|
|
import inspect
|
|
import sys
|
|
import threading
|
|
|
|
from multiprocessing import Pipe
|
|
from random import randrange
|
|
from typing import Any, Union
|
|
|
|
from core.correctness.vars import VALID_RECIPE_NAME_CHARS, \
|
|
VALID_PATTERN_NAME_CHARS, VALID_RULE_NAME_CHARS, VALID_CHANNELS, \
|
|
get_drt_imp_msg, DEBUG_WARNING, DEBUG_INFO, EVENT_TYPE
|
|
from core.correctness.validation import valid_string, check_type, \
|
|
check_implementation, valid_list, valid_dict, setup_debugging
|
|
from core.functionality import print_debug, wait, generate_id
|
|
|
|
|
|
class BaseRecipe:
|
|
name:str
|
|
recipe:Any
|
|
parameters:dict[str, Any]
|
|
requirements:dict[str, Any]
|
|
def __init__(self, name:str, recipe:Any, parameters:dict[str,Any]={},
|
|
requirements:dict[str,Any]={}):
|
|
check_implementation(type(self)._is_valid_recipe, BaseRecipe)
|
|
check_implementation(type(self)._is_valid_parameters, BaseRecipe)
|
|
check_implementation(type(self)._is_valid_requirements, BaseRecipe)
|
|
self._is_valid_name(name)
|
|
self.name = name
|
|
self._is_valid_recipe(recipe)
|
|
self.recipe = recipe
|
|
self._is_valid_parameters(parameters)
|
|
self.parameters = parameters
|
|
self._is_valid_requirements(requirements)
|
|
self.requirements = requirements
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls is BaseRecipe:
|
|
msg = get_drt_imp_msg(BaseRecipe)
|
|
raise TypeError(msg)
|
|
return object.__new__(cls)
|
|
|
|
def _is_valid_name(self, name:str)->None:
|
|
valid_string(name, VALID_RECIPE_NAME_CHARS)
|
|
|
|
def _is_valid_recipe(self, recipe:Any)->None:
|
|
pass
|
|
|
|
def _is_valid_parameters(self, parameters:Any)->None:
|
|
pass
|
|
|
|
def _is_valid_requirements(self, requirements:Any)->None:
|
|
pass
|
|
|
|
|
|
class BasePattern:
|
|
name:str
|
|
recipe:str
|
|
parameters:dict[str,Any]
|
|
outputs:dict[str,Any]
|
|
def __init__(self, name:str, recipe:str, parameters:dict[str,Any]={},
|
|
outputs:dict[str,Any]={}):
|
|
check_implementation(type(self)._is_valid_recipe, BasePattern)
|
|
check_implementation(type(self)._is_valid_parameters, BasePattern)
|
|
check_implementation(type(self)._is_valid_output, BasePattern)
|
|
self._is_valid_name(name)
|
|
self.name = name
|
|
self._is_valid_recipe(recipe)
|
|
self.recipe = recipe
|
|
self._is_valid_parameters(parameters)
|
|
self.parameters = parameters
|
|
self._is_valid_output(outputs)
|
|
self.outputs = outputs
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls is BasePattern:
|
|
msg = get_drt_imp_msg(BasePattern)
|
|
raise TypeError(msg)
|
|
return object.__new__(cls)
|
|
|
|
def _is_valid_name(self, name:str)->None:
|
|
valid_string(name, VALID_PATTERN_NAME_CHARS)
|
|
|
|
def _is_valid_recipe(self, recipe:Any)->None:
|
|
pass
|
|
|
|
def _is_valid_parameters(self, parameters:Any)->None:
|
|
pass
|
|
|
|
def _is_valid_output(self, outputs:Any)->None:
|
|
pass
|
|
|
|
|
|
class BaseRule:
|
|
name:str
|
|
pattern:BasePattern
|
|
recipe:BaseRecipe
|
|
pattern_type:str=""
|
|
recipe_type:str=""
|
|
def __init__(self, name:str, pattern:BasePattern, recipe:BaseRecipe):
|
|
check_implementation(type(self)._is_valid_pattern, BaseRule)
|
|
check_implementation(type(self)._is_valid_recipe, BaseRule)
|
|
self._is_valid_name(name)
|
|
self.name = name
|
|
self._is_valid_pattern(pattern)
|
|
self.pattern = pattern
|
|
self._is_valid_recipe(recipe)
|
|
self.recipe = recipe
|
|
self.__check_types_set()
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls is BaseRule:
|
|
msg = get_drt_imp_msg(BaseRule)
|
|
raise TypeError(msg)
|
|
return object.__new__(cls)
|
|
|
|
def _is_valid_name(self, name:str)->None:
|
|
valid_string(name, VALID_RULE_NAME_CHARS)
|
|
|
|
def _is_valid_pattern(self, pattern:Any)->None:
|
|
pass
|
|
|
|
def _is_valid_recipe(self, recipe:Any)->None:
|
|
pass
|
|
|
|
def __check_types_set(self)->None:
|
|
if self.pattern_type == "":
|
|
raise AttributeError(f"Rule Class '{self.__class__.__name__}' "
|
|
"does not set a pattern_type.")
|
|
if self.recipe_type == "":
|
|
raise AttributeError(f"Rule Class '{self.__class__.__name__}' "
|
|
"does not set a recipe_type.")
|
|
|
|
|
|
class BaseMonitor:
|
|
rules: dict[str, BaseRule]
|
|
to_runner: VALID_CHANNELS
|
|
def __init__(self, rules:dict[str,BaseRule])->None:
|
|
check_implementation(type(self).start, BaseMonitor)
|
|
check_implementation(type(self).stop, BaseMonitor)
|
|
check_implementation(type(self)._is_valid_rules, BaseMonitor)
|
|
self._is_valid_rules(rules)
|
|
self.rules = rules
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls is BaseMonitor:
|
|
msg = get_drt_imp_msg(BaseMonitor)
|
|
raise TypeError(msg)
|
|
return object.__new__(cls)
|
|
|
|
def _is_valid_rules(self, rules:dict[str,BaseRule])->None:
|
|
pass
|
|
|
|
def start(self)->None:
|
|
pass
|
|
|
|
def stop(self)->None:
|
|
pass
|
|
|
|
|
|
class BaseHandler:
|
|
def __init__(self) -> None:
|
|
check_implementation(type(self).handle, BaseHandler)
|
|
check_implementation(type(self).valid_event_types, BaseHandler)
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls is BaseHandler:
|
|
msg = get_drt_imp_msg(BaseHandler)
|
|
raise TypeError(msg)
|
|
return object.__new__(cls)
|
|
|
|
def valid_event_types(self)->list[str]:
|
|
pass
|
|
|
|
def handle(self, event:Any)->None:
|
|
pass
|
|
|
|
|
|
class MeowRunner:
|
|
monitors:list[BaseMonitor]
|
|
handlers:dict[str:BaseHandler]
|
|
from_monitor: list[VALID_CHANNELS]
|
|
def __init__(self, monitors:Union[BaseMonitor,list[BaseMonitor]],
|
|
handlers:Union[BaseHandler,list[BaseHandler]],
|
|
print:Any=sys.stdout, logging:int=0) -> None:
|
|
self._is_valid_handlers(handlers)
|
|
if not type(handlers) == list:
|
|
handlers = [handlers]
|
|
self.handlers = {}
|
|
for handler in handlers:
|
|
handler_events = handler.valid_event_types()
|
|
for event in handler_events:
|
|
if event in self.handlers.keys():
|
|
self.handlers[event].append(handler)
|
|
else:
|
|
self.handlers[event] = [handler]
|
|
|
|
self._is_valid_monitors(monitors)
|
|
if not type(monitors) == list:
|
|
monitors = [monitors]
|
|
self.monitors = monitors
|
|
self.from_monitors = []
|
|
for monitor in self.monitors:
|
|
monitor_to_runner_reader, monitor_to_runner_writer = Pipe()
|
|
monitor.to_runner = monitor_to_runner_writer
|
|
self.from_monitors.append(monitor_to_runner_reader)
|
|
|
|
self._stop_pipe = Pipe()
|
|
self._worker = None
|
|
self._print_target, self.debug_level = setup_debugging(print, logging)
|
|
|
|
def run(self)->None:
|
|
all_inputs = self.from_monitors + [self._stop_pipe[0]]
|
|
while True:
|
|
ready = wait(all_inputs)
|
|
|
|
if self._stop_pipe[0] in ready:
|
|
return
|
|
else:
|
|
for from_monitor in self.from_monitors:
|
|
if from_monitor in ready:
|
|
message = from_monitor.recv()
|
|
event = message
|
|
if not self.handlers[event[EVENT_TYPE]]:
|
|
print_debug(self._print_target, self.debug_level,
|
|
"Could not process event as no relevent "
|
|
f"handler for '{EVENT_TYPE}'", DEBUG_INFO)
|
|
return
|
|
if len(self.handlers[event[EVENT_TYPE]]) == 1:
|
|
self.handlers[event[EVENT_TYPE]][0].handle(event)
|
|
else:
|
|
self.handlers[event[EVENT_TYPE]][
|
|
randrange(len(self.handlers[event[EVENT_TYPE]]))
|
|
].handle(event)
|
|
|
|
def start(self)->None:
|
|
for monitor in self.monitors:
|
|
monitor.start()
|
|
startable = []
|
|
for handler_list in self.handlers.values():
|
|
for handler in handler_list:
|
|
if hasattr(handler, "start") and handler not in startable:
|
|
startable.append()
|
|
for handler in startable:
|
|
handler.start()
|
|
|
|
if self._worker is None:
|
|
self._worker = threading.Thread(
|
|
target=self.run,
|
|
args=[])
|
|
self._worker.daemon = True
|
|
self._worker.start()
|
|
print_debug(self._print_target, self.debug_level,
|
|
"Starting MeowRunner run...", DEBUG_INFO)
|
|
else:
|
|
msg = "Repeated calls to start have no effect."
|
|
print_debug(self._print_target, self.debug_level,
|
|
msg, DEBUG_WARNING)
|
|
raise RuntimeWarning(msg)
|
|
|
|
|
|
def stop(self)->None:
|
|
for monitor in self.monitors:
|
|
monitor.stop()
|
|
|
|
stopable = []
|
|
for handler_list in self.handlers.values():
|
|
for handler in handler_list:
|
|
if hasattr(handler, "stop") and handler not in stopable:
|
|
stopable.append()
|
|
for handler in stopable:
|
|
handler.stop()
|
|
|
|
if self._worker is None:
|
|
msg = "Cannot stop thread that is not started."
|
|
print_debug(self._print_target, self.debug_level,
|
|
msg, DEBUG_WARNING)
|
|
raise RuntimeWarning(msg)
|
|
else:
|
|
self._stop_pipe[1].send(1)
|
|
self._worker.join()
|
|
print_debug(self._print_target, self.debug_level,
|
|
"Worker thread stopped", DEBUG_INFO)
|
|
|
|
|
|
def _is_valid_monitors(self,
|
|
monitors:Union[BaseMonitor,list[BaseMonitor]])->None:
|
|
check_type(monitors, BaseMonitor, alt_types=[list[BaseMonitor]])
|
|
if type(monitors) == list:
|
|
valid_list(monitors, BaseMonitor, min_length=1)
|
|
|
|
def _is_valid_handlers(self,
|
|
handlers:Union[BaseHandler,list[BaseHandler]])->None:
|
|
check_type(handlers, BaseHandler, alt_types=[list[BaseHandler]])
|
|
if type(handlers) == list:
|
|
valid_list(handlers, BaseHandler, min_length=1)
|
|
|
|
def create_rules(patterns:Union[dict[str,BasePattern],list[BasePattern]],
|
|
recipes:Union[dict[str,BaseRecipe],list[BaseRecipe]],
|
|
new_rules:list[BaseRule]=[])->dict[str,BaseRule]:
|
|
check_type(patterns, dict, alt_types=[list])
|
|
check_type(recipes, dict, alt_types=[list])
|
|
valid_list(new_rules, BaseRule, min_length=0)
|
|
|
|
if isinstance(patterns, list):
|
|
valid_list(patterns, BasePattern, min_length=0)
|
|
patterns = {pattern.name:pattern for pattern in patterns}
|
|
else:
|
|
valid_dict(patterns, str, BasePattern, strict=False, min_length=0)
|
|
for k, v in patterns.items():
|
|
if k != v.name:
|
|
raise KeyError(
|
|
f"Key '{k}' indexes unexpected Pattern '{v.name}' "
|
|
"Pattern dictionaries must be keyed with the name of the "
|
|
"Pattern.")
|
|
|
|
if isinstance(recipes, list):
|
|
valid_list(recipes, BaseRecipe, min_length=0)
|
|
recipes = {recipe.name:recipe for recipe in recipes}
|
|
else:
|
|
valid_dict(recipes, str, BaseRecipe, strict=False, min_length=0)
|
|
for k, v in recipes.items():
|
|
if k != v.name:
|
|
raise KeyError(
|
|
f"Key '{k}' indexes unexpected Recipe '{v.name}' "
|
|
"Recipe dictionaries must be keyed with the name of the "
|
|
"Recipe.")
|
|
|
|
# Imported here to avoid circular imports at top of file
|
|
import rules
|
|
rules = {}
|
|
all_rules ={(r.pattern_type, r.recipe_type):r for r in [r[1] \
|
|
for r in inspect.getmembers(sys.modules["rules"], inspect.isclass) \
|
|
if (issubclass(r[1], BaseRule))]}
|
|
|
|
for pattern in patterns.values():
|
|
if pattern.recipe in recipes:
|
|
key = (type(pattern).__name__,
|
|
type(recipes[pattern.recipe]).__name__)
|
|
if (key) in all_rules:
|
|
rule = all_rules[key](
|
|
generate_id(prefix="Rule_"),
|
|
pattern,
|
|
recipes[pattern.recipe]
|
|
)
|
|
rules[rule.name] = rule
|
|
return rules
|