|
1
|
|
|
import attr |
|
2
|
|
|
from so_magic.utils import ObjectRegistry, Observer |
|
3
|
|
|
from .commands_manager import CommandsManager |
|
4
|
|
|
from .encoding import MagicEncoderFactory |
|
5
|
|
|
from .filling import MagicFillerFactory |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@attr.s |
|
9
|
|
|
class Phis(Observer): |
|
10
|
|
|
registry: ObjectRegistry = attr.ib(default=attr.Factory(ObjectRegistry)) |
|
11
|
|
|
|
|
12
|
|
|
def __getattr__(self, item): |
|
13
|
|
|
return getattr(self.registry, item) |
|
14
|
|
|
|
|
15
|
|
|
def __contains__(self, item): |
|
16
|
|
|
return item in self.registry |
|
17
|
|
|
|
|
18
|
|
|
def update(self, subject, *args, **kwargs): |
|
19
|
|
|
self.registry.add(subject.name, subject.state) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def encoder_fct_callback(self, method): |
|
23
|
|
|
def _callback(*args, **kwargs): |
|
24
|
|
|
print('----- encoder_fct_callback', [type(x) for x in args], [(k, type(v)) for k, v in kwargs.items()]) |
|
25
|
|
|
return method(self.datapoints, *args, **kwargs) |
|
26
|
|
|
return _callback |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
@attr.s |
|
30
|
|
|
class DataManager: |
|
31
|
|
|
engine = attr.ib(init=True) |
|
32
|
|
|
_phi_function_class = attr.ib(init=True) |
|
33
|
|
|
feature_manager = attr.ib(init=True) |
|
34
|
|
|
|
|
35
|
|
|
commands_manager = attr.ib(init=True, default=CommandsManager()) |
|
36
|
|
|
# mediator = attr.ib(init=False, default=attr.Factory( |
|
37
|
|
|
# lambda self: DataMediator(self.commands_manager, self.backend), takes_self=True)) |
|
38
|
|
|
built_phis = attr.ib(init=False, default=Phis()) |
|
39
|
|
|
_factories = attr.ib(init=False, default=attr.Factory(lambda: { |
|
40
|
|
|
'encoder': MagicEncoderFactory(), |
|
41
|
|
|
'filler': MagicFillerFactory(), |
|
42
|
|
|
})) |
|
43
|
|
|
_fct_callbacks = attr.ib(init=False, default=attr.Factory(lambda self: { |
|
44
|
|
|
'encoder': encoder_fct_callback, |
|
45
|
|
|
'filler': encoder_fct_callback, |
|
46
|
|
|
}, takes_self=True)) |
|
47
|
|
|
|
|
48
|
|
|
def __attrs_post_init__(self): |
|
49
|
|
|
self.engine.backend.datapoints_factory.subject.attach(self.engine.datapoints_manager) |
|
50
|
|
|
self.engine.backend.command_factory.subject.attach(self.commands_manager.command.accumulator) |
|
51
|
|
|
self._phi_function_class.subject.attach(self.built_phis) |
|
52
|
|
|
|
|
53
|
|
|
@property |
|
54
|
|
|
def phis(self): |
|
55
|
|
|
return self.built_phis |
|
56
|
|
|
|
|
57
|
|
|
@property |
|
58
|
|
|
def phi_class(self): |
|
59
|
|
|
return self._phi_function_class |
|
60
|
|
|
|
|
61
|
|
|
@property |
|
62
|
|
|
def commands(self): |
|
63
|
|
|
return self.commands_manager.commands_dict |
|
64
|
|
|
|
|
65
|
|
|
@property |
|
66
|
|
|
def command(self): |
|
67
|
|
|
return self.commands_manager.command |
|
68
|
|
|
|
|
69
|
|
|
@property |
|
70
|
|
|
def datapoints(self): |
|
71
|
|
|
return self.engine.datapoints_manager.datapoints |
|
72
|
|
|
|
|
73
|
|
|
def create(self, factory_type: str, *args, **kwargs): |
|
74
|
|
|
print('----- DATA MANAGER', type(self), type(factory_type), [type(x) for x in args], [(k, type(v)) for k, v in kwargs.items()]) |
|
75
|
|
|
fct_method = self._fct_callbacks[factory_type] |
|
76
|
|
|
return fct_method(self, self._factories[factory_type].create)(*args, **kwargs) |
|
77
|
|
|
|