# -*- coding: utf-8 -*-
from __future__ import annotations
from abc import ABCMeta
from inspect import isclass
from typing import TYPE_CHECKING, Dict, Tuple, Type
if TYPE_CHECKING:
from pygerber.drawing_state import DrawingState
import re
from pygerber.validators.validator import Validator
[docs]class Dispatcher(metaclass=DispatcherMeta):
"""Base class for all dispatcher objects. Includes
tokens and some of the fields.
"""
__validators__: Dict[str, Validator]
def __init__(self, match_object: re.Match, state: DrawingState) -> None:
self.re_match = match_object
group_dict = self.re_match.groupdict()
for name, validator in self.__validators__:
cleaned_value = validator(self, state, group_dict.get(name))
setattr(self, name, cleaned_value)
[docs]def getvalidators(mesh_factory: Dispatcher) -> dict:
"""Returns validators specified for given Dispatcher.
:param mesh_factory: Object to fetch validators from.
:type mesh_factory: Dispatcher
:return: dictionary of factory fields.
:rtype: dict
"""
if isclass(mesh_factory):
return mesh_factory.__dict__["__validators__"]
else:
return mesh_factory.__class__.__dict__["__validators__"]