initial commit with barebones project structure

This commit is contained in:
PatchOfScotland
2022-11-29 17:15:14 +01:00
parent f940354be4
commit 9dd2d0c209
11 changed files with 287 additions and 0 deletions

View File

View File

@ -0,0 +1,51 @@
def check_input(variable, expected_type, or_none=False):
"""
Checks if a given variable is of the expected type. Raises TypeError or
ValueError as appropriate if any issues are encountered.
:param variable: (any) variable to check type of
:param expected_type: (type) expected type of the provided variable
:param or_none: (optional) boolean of if the variable can be unset.
Default value is False.
:return: No return.
"""
if not or_none:
if not isinstance(variable, expected_type):
raise TypeError(
'Expected type was %s, got %s'
% (expected_type, type(variable))
)
else:
if not isinstance(variable, expected_type) \
and not isinstance(variable, type(None)):
raise TypeError(
'Expected type was %s or None, got %s'
% (expected_type, type(variable))
)
def valid_string(variable, valid_chars):
"""
Checks that all characters in a given string are present in a provided
list of characters. Will raise an ValueError if unexpected character is
encountered.
:param variable: (str) variable to check.
:param valid_chars: (str) collection of valid characters.
:return: No return.
"""
check_input(variable, str)
check_input(valid_chars, str)
for char in variable:
if char not in valid_chars:
raise ValueError(
"Invalid character '%s'. Only valid characters are: "
"%s" % (char, valid_chars)
)

10
core/correctness/vars.py Normal file
View File

@ -0,0 +1,10 @@
CHAR_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
CHAR_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
CHAR_NUMERIC = '0123456789'
VALID_NAME_CHARS = CHAR_UPPERCASE + CHAR_LOWERCASE + CHAR_NUMERIC + "_-"
VALID_RECIPE_NAME_CHARS = VALID_NAME_CHARS
VALID_PATTERN_NAME_CHARS = VALID_NAME_CHARS
VALID_RULE_NAME_CHARS = VALID_NAME_CHARS

96
core/meow.py Normal file
View File

@ -0,0 +1,96 @@
import core.correctness.vars
import core.correctness.validation
from typing import Any
class BaseRecipe:
name: str
recipe: Any
paramaters: dict[str, Any]
def __init__(self, name:str, recipe:Any, parameters:dict[str,Any]={}):
self.__is_valid_name(name)
self.name = name
self.__is_valid_recipe(recipe)
self.recipe = recipe
self.__is_valid_parameters(parameters)
self.paramaters = parameters
def __new__(cls, *args, **kwargs):
if cls is BaseRecipe:
raise TypeError("BaseRecipe may not be instantiated directly")
def __is_valid_name(self, name):
core.correctness.validation.valid_string(
name, core.correctness.vars.VALID_RECIPE_NAME_CHARS)
def __is_valid_recipe(self, recipe):
raise NotImplementedError(
f"Recipe '{self.__class__.__name__}' has not implemented "
"'__is_valid_recipe(self, recipe)' function.")
def __is_valid_parameters(self, parameters):
raise NotImplementedError(
f"Recipe '{self.__class__.__name__}' has not implemented "
"'__is_valid_parameters(self, parameters)' function.")
class BasePattern:
name: str
recipe: BaseRecipe
parameters: dict[str, Any]
outputs: dict[str, Any]
def __init__(self, name:str, recipe:BaseRecipe,
parameters:dict[str,Any]={}, outputs:dict[str,Any]={}):
self.__is_valid_name(name)
self.name = name
self.__is_valid_recipe(recipe)
self.recipe = recipe
self.__is_valid_parameters(parameters)
self.paramaters = parameters
self.__is_valid_output(outputs)
self.outputs = outputs
def __new__(cls, *args, **kwargs):
if cls is BasePattern:
raise TypeError("BasePattern may not be instantiated directly")
def __is_valid_name(self, name):
core.correctness.validation.valid_string(
name, core.correctness.vars.VALID_PATTERN_NAME_CHARS)
def __is_valid_recipe(self, recipe):
raise NotImplementedError(
f"Pattern '{self.__class__.__name__}' has not implemented "
"'__is_valid_recipe(self, recipe)' function.")
def __is_valid_parameters(self, parameters):
raise NotImplementedError(
f"Pattern '{self.__class__.__name__}' has not implemented "
"'__is_valid_parameters(self, parameters)' function.")
def __is_valid_output(self, outputs):
raise NotImplementedError(
f"Pattern '{self.__class__.__name__}' has not implemented "
"'__is_valid_output(self, outputs)' function.")
class BaseRule:
name: str
patterns: list[BasePattern]
def __init__(self, name:str, patterns:list[BasePattern]):
self.__is_valid_name(name)
self.name = name
self.__is_valid_patterns(patterns)
self.patterns = patterns
def __new__(cls, *args, **kwargs):
if cls is BaseRule:
raise TypeError("BaseRule may not be instantiated directly")
def __is_valid_name(self, name):
core.correctness.validation.valid_string(
name, core.correctness.vars.VALID_RULE_NAME_CHARS)
def __is_valid_patterns(self, patterns):
raise NotImplementedError(
f"Rule '{self.__class__.__name__}' has not implemented "
"'__is_valid_patterns(self, patterns)' function.")