2022-11-15 15:00:52 +00:00
from __future__ import annotations
2022-11-24 15:44:25 +00:00
from dataclasses import dataclass , field
from inspect import ClassFoundException
2022-11-15 15:00:52 +00:00
from typing import Type
2023-01-21 19:01:02 +00:00
from . . core import Metadata
2022-11-15 15:00:52 +00:00
from abc import ABC
2022-11-24 15:44:25 +00:00
# from collections.abc import Iterable
2022-11-15 15:00:52 +00:00
@dataclass
class Step ( ABC ) :
2022-11-24 15:44:25 +00:00
name : str = None
2022-11-15 15:00:52 +00:00
def __init__ ( self , config : dict ) - > None :
2022-11-24 15:44:25 +00:00
# reads the configs into object properties
# self.config = config[self.name]
2023-01-04 16:37:36 +00:00
for k , v in config . get ( self . name , { } ) . items ( ) :
2022-11-24 15:44:25 +00:00
self . __setattr__ ( k , v )
2022-11-15 15:00:52 +00:00
@staticmethod
def configs ( ) - > dict : { }
def init ( name : str , config : dict , child : Type [ Step ] ) - > Step :
"""
2022-12-14 16:11:06 +00:00
looks into direct subclasses of child for name and returns such ab object
TODO : cannot find subclasses of child . subclasses
2022-11-15 15:00:52 +00:00
"""
for sub in child . __subclasses__ ( ) :
if sub . name == name :
2022-11-24 15:44:25 +00:00
return sub ( config )
2023-01-21 19:44:12 +00:00
raise ClassFoundException ( f " Unable to initialize STEP with { name =} , check your configuration file/step names, and make sure you made the step discoverable by putting it into __init__.py " )
2023-01-18 16:16:23 +00:00
def assert_valid_string ( self , prop : str ) - > None :
"""
receives a property name an ensures it exists and is a valid non - empty string , raises an exception if not
"""
assert hasattr ( self , prop ) , f " property { prop } not found "
s = getattr ( self , prop )
assert s is not None and type ( s ) == str and len ( s ) > 0 , f " invalid property { prop } value ' { s } ' , it should be a valid string "