| Total Complexity | 7 |
| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |
||
| 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 |