✨ Added previous work to new repo
This commit is contained in:
51
patterns/network_event_pattern.py
Normal file
51
patterns/network_event_pattern.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
from meow_base.functionality.validation import valid_string, valid_dict
|
||||||
|
from meow_base.core.vars import VALID_RECIPE_NAME_CHARS, VALID_VARIABLE_NAME_CHARS
|
||||||
|
from meow_base.core.base_recipe import BaseRecipe
|
||||||
|
from meow_base.core.base_monitor import BaseMonitor
|
||||||
|
from meow_base.core.base_pattern import BasePattern
|
||||||
|
|
||||||
|
|
||||||
|
class NetworkEventPattern(BasePattern):
|
||||||
|
# The port to monitor
|
||||||
|
triggering_port:int
|
||||||
|
|
||||||
|
def __init__(self, name: str, triggering_port:int, recipe: str, parameters: Dict[str, Any] = {}, outputs: Dict[str, Any] = {}, sweep: Dict[str, Any] = {}):
|
||||||
|
super().__init__(name, recipe, parameters, outputs, sweep)
|
||||||
|
self._is_valid_port(triggering_port)
|
||||||
|
self.triggering_port = triggering_port
|
||||||
|
|
||||||
|
def _is_valid_port(self, port:int)->None:
|
||||||
|
if not isinstance(port, int):
|
||||||
|
raise ValueError (
|
||||||
|
f"Port '{port}' is not of type int."
|
||||||
|
)
|
||||||
|
elif not (0 < port < 65536):
|
||||||
|
raise ValueError (
|
||||||
|
f"Port '{port}' is not valid."
|
||||||
|
)
|
||||||
|
|
||||||
|
def _is_valid_recipe(self, recipe:str)->None:
|
||||||
|
"""Validation check for 'recipe' variable from main constructor.
|
||||||
|
Called within parent BasePattern constructor."""
|
||||||
|
valid_string(recipe, VALID_RECIPE_NAME_CHARS)
|
||||||
|
|
||||||
|
def _is_valid_parameters(self, parameters:Dict[str,Any])->None:
|
||||||
|
"""Validation check for 'parameters' variable from main constructor.
|
||||||
|
Called within parent BasePattern constructor."""
|
||||||
|
valid_dict(parameters, str, Any, strict=False, min_length=0)
|
||||||
|
for k in parameters.keys():
|
||||||
|
valid_string(k, VALID_VARIABLE_NAME_CHARS)
|
||||||
|
|
||||||
|
def _is_valid_output(self, outputs:Dict[str,str])->None:
|
||||||
|
"""Validation check for 'output' variable from main constructor.
|
||||||
|
Called within parent BasePattern constructor."""
|
||||||
|
valid_dict(outputs, str, str, strict=False, min_length=0)
|
||||||
|
for k in outputs.keys():
|
||||||
|
valid_string(k, VALID_VARIABLE_NAME_CHARS)
|
||||||
|
|
||||||
|
class NetworkMonitor(BaseMonitor):
|
||||||
|
def __init__(self, patterns: Dict[str, NetworkEventPattern], recipes: Dict[str, BaseRecipe]) -> None:
|
||||||
|
super().__init__(patterns, recipes)
|
||||||
|
self.ports = set(pattern.triggering_port for pattern in patterns.values())
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
@ -15,6 +14,7 @@ from meow_base.functionality.meow import create_rule
|
|||||||
from meow_base.patterns.file_event_pattern import FileEventPattern, \
|
from meow_base.patterns.file_event_pattern import FileEventPattern, \
|
||||||
WatchdogMonitor, _DEFAULT_MASK, WATCHDOG_HASH, WATCHDOG_BASE, \
|
WatchdogMonitor, _DEFAULT_MASK, WATCHDOG_HASH, WATCHDOG_BASE, \
|
||||||
EVENT_TYPE_WATCHDOG, WATCHDOG_EVENT_KEYS, create_watchdog_event
|
EVENT_TYPE_WATCHDOG, WATCHDOG_EVENT_KEYS, create_watchdog_event
|
||||||
|
from patterns.network_event_pattern import NetworkEventPattern
|
||||||
from meow_base.recipes.jupyter_notebook_recipe import JupyterNotebookRecipe
|
from meow_base.recipes.jupyter_notebook_recipe import JupyterNotebookRecipe
|
||||||
from meow_base.recipes.python_recipe import PythonRecipe
|
from meow_base.recipes.python_recipe import PythonRecipe
|
||||||
from shared import BAREBONES_NOTEBOOK, TEST_MONITOR_BASE, \
|
from shared import BAREBONES_NOTEBOOK, TEST_MONITOR_BASE, \
|
||||||
@ -998,3 +998,83 @@ class WatchdogMonitorTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertIsInstance(rules, dict)
|
self.assertIsInstance(rules, dict)
|
||||||
self.assertEqual(len(rules), 2)
|
self.assertEqual(len(rules), 2)
|
||||||
|
|
||||||
|
class NetworkEventPatternTests(unittest.TestCase):
|
||||||
|
def setUp(self)->None:
|
||||||
|
super().setUp()
|
||||||
|
setup()
|
||||||
|
|
||||||
|
def tearDown(self)->None:
|
||||||
|
super().tearDown()
|
||||||
|
teardown()
|
||||||
|
|
||||||
|
# Test NetworkEvent created
|
||||||
|
def testNetworkEventPatternCreationMinimum(self)->None:
|
||||||
|
NetworkEventPattern("name", 9000, "recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with empty name
|
||||||
|
def testNetworkEventPatternCreationEmptyName(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("", 9000, "recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with empty recipe
|
||||||
|
def testNetworkEventPatternCreationEmptyRecipe(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("name", 9000, "")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with invalid name
|
||||||
|
def testNetworkEventPatternCreationInvalidName(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("@name", 9000, "recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with invalid port
|
||||||
|
def testNetworkEventPatternCreationInvalidPort(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("name", "9000", "recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with invalid port
|
||||||
|
def testNetworkEventPatternCreationInvalidPort2(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("name", 0, "recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern not created with invalid recipe
|
||||||
|
def testNetworkEventPatternCreationInvalidRecipe(self)->None:
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
NetworkEventPattern("name", 9000, "@recipe")
|
||||||
|
|
||||||
|
# Test NetworkEventPattern created with valid name
|
||||||
|
def testNetworkEventPatternSetupName(self)->None:
|
||||||
|
name = "name"
|
||||||
|
nep = NetworkEventPattern(name, 9000, "recipe")
|
||||||
|
self.assertEqual(nep.name, name)
|
||||||
|
|
||||||
|
# Test NetworkEventPattern created with valid port
|
||||||
|
def testNetworkEventPatternSetupPort(self)->None:
|
||||||
|
port = 9000
|
||||||
|
nep = NetworkEventPattern("name", port, "recipe")
|
||||||
|
self.assertEqual(nep.triggering_port, port)
|
||||||
|
|
||||||
|
# Test NetworkEventPattern created with valid recipe
|
||||||
|
def testNetworkEventPatternSetupRecipe(self)->None:
|
||||||
|
recipe = "recipe"
|
||||||
|
nep = NetworkEventPattern("name", 9000, recipe)
|
||||||
|
self.assertEqual(nep.recipe, recipe)
|
||||||
|
|
||||||
|
# Test NetworkEventPattern created with valid parameters
|
||||||
|
def testNetworkEventPatternSetupParameters(self)->None:
|
||||||
|
parameters = {
|
||||||
|
"a": 1,
|
||||||
|
"b": True
|
||||||
|
}
|
||||||
|
fep = NetworkEventPattern("name", 9000, "recipe", parameters=parameters)
|
||||||
|
self.assertEqual(fep.parameters, parameters)
|
||||||
|
|
||||||
|
# Test NetworkEventPattern created with valid outputs
|
||||||
|
def testNetworkEventPatternSetupOutputs(self)->None:
|
||||||
|
outputs = {
|
||||||
|
"a": "a",
|
||||||
|
"b": "b"
|
||||||
|
}
|
||||||
|
fep = NetworkEventPattern("name", 9000, "recipe", outputs=outputs)
|
||||||
|
self.assertEqual(fep.outputs, outputs)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user