Passed
Push — dev ( 10ffc2...e6fd15 )
by Konstantinos
01:26
created

green_magic.utils.mediator.Component1.do_b()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from abc import ABC, abstractmethod, ABCMeta
2
3
__all__ = ['GenericMediator', 'BaseComponent']
4
5
6
class Mediator(ABC):
7
    """
8
    The Mediator interface declares a method used by components to notify the
9
    mediator about various events. The Mediator may react to these events and
10
    pass the execution to other components.
11
    """
12
    @abstractmethod
13
    def notify(self, sender: object, event: str) -> None:
14
        """[summary]
15
16
        Args:
17
            sender (object): [description]
18
            event (str): [description]
19
        """
20
        raise NotImplementedError
21
22
23
class GenericMediator(Mediator, metaclass=ABCMeta):
24
    """Abstract Mediator class that automatically configures components received as *args through the constructor.
25
    """
26
    def __new__(cls, *components, **kwargs):
27
        x = super().__new__(cls)
28
        for i, component in enumerate(components):
29
            setattr(x, f'_component{i+1}', component)
30
            getattr(x, f'_component{i+1}').mediator = x
31
        return x
32
33
34
class BaseComponent:
35
    """
36
    The Base Component provides the basic functionality of storing a mediator's
37
    instance inside component objects.
38
    """
39
40
    def __init__(self, mediator: Mediator = None) -> None:
41
        self._mediator = mediator
42
43
    @property
44
    def mediator(self) -> Mediator:
45
        return self._mediator
46
47
    @mediator.setter
48
    def mediator(self, mediator: Mediator) -> None:
49
        self._mediator = mediator
50