Passed
Push — dev ( 4bfa05...67478a )
by Konstantinos
01:31
created

green_magic.data.commands_manager.MagicCommandFactory.__call__()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
import attr
2
from green_magic.utils import Observer
3
4
5
@attr.s
6
class CommandsAccumulator(Observer):
7
    """"""
8
    commands = attr.ib(init=False, default={})
9
10
    def update(self, subject) -> None:
11
        self.commands[getattr(subject, 'name', str(subject.state))] = subject.state
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable getattr does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
12
13
@attr.s
14
class CommandGetter:
15
    _commands_accumulator = attr.ib(init=True, default=CommandsAccumulator())
16
17
    @property
18
    def accumulator(self):
19
        return self._commands_accumulator
20
21
    def __getattr__(self, item):
22
        if item not in self._commands_accumulator.commands:
23
            raise KeyError(f"Item '{item}' not found in [{', '.join(str(_) for _ in self._commands_accumulator.commands.keys())}]")
24
        return self._commands_accumulator.commands[item]
25
26
27
@attr.s
28
class CommandsManager:
29
    """[summary]
30
31
    Args:
32
        prototypes (dict, optional): initial prototypes to be supplied
33
        command_factory (callable, optional): a callable that returns an instance of Command
34
    """
35
    _commands_getter = attr.ib(init=True, default=CommandGetter())
36
37
    @property
38
    def command(self):
39
        return self._commands_getter
40
41
    @property
42
    def commands_dict(self):
43
        return self._commands_accumulator.commands
44
45
    # def __getattr__(self, item):
46
    #     if item not in self._commands_accumulator.commands:
47
    #         raise KeyError(f"Item '{item}' not found in [{', '.join(str(_) for _ in self._commands_accumulator.commands.keys())}]")
48
    #     return self._commands_accumulator.commands[item]
49