Passed
Push — dev ( 67478a...3ac626 )
by Konstantinos
01:30
created

CommandsAccumulator.update()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
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
    def __contains__(self, item):
14
        return item in self.commands
15
16
@attr.s
17
class CommandGetter:
18
    _commands_accumulator = attr.ib(init=True, default=CommandsAccumulator())
19
20
    @property
21
    def accumulator(self):
22
        return self._commands_accumulator
23
24
    def __getattr__(self, item):
25
        if item not in self._commands_accumulator.commands:
26
            raise KeyError(f"Item '{item}' not found in [{', '.join(str(_) for _ in self._commands_accumulator.commands.keys())}]")
27
        return self._commands_accumulator.commands[item]
28
29
30
@attr.s
31
class CommandsManager:
32
    """[summary]
33
34
    Args:
35
        prototypes (dict, optional): initial prototypes to be supplied
36
        command_factory (callable, optional): a callable that returns an instance of Command
37
    """
38
    _commands_getter = attr.ib(init=True, default=CommandGetter())
39
40
    @property
41
    def command(self):
42
        return self._commands_getter
43
44
    @property
45
    def commands_dict(self):
46
        return self._commands_accumulator.commands
47