|
23 | 23 |
|
24 | 24 | import time |
25 | 25 | import multiprocessing |
| 26 | +from .decorators import getsource |
| 27 | +import functools |
26 | 28 |
|
27 | 29 | # DEV GROUP |
28 | 30 |
|
@@ -107,3 +109,57 @@ def reset(self, group_silent=False, silent_dev=True, ignore=[], include=[]): |
107 | 109 | if not group_silent: |
108 | 110 | print('Rebooting {}'.format(dev)) |
109 | 111 | self.devs[dev].reset(silent=silent_dev) |
| 112 | + |
| 113 | + |
| 114 | +class DeviceGroup(DEVGROUP): |
| 115 | + |
| 116 | + def __init__(self, *args, **kargs): |
| 117 | + super().__init__(*args, **kargs) |
| 118 | + |
| 119 | + def code(self, func): |
| 120 | + str_func = '\n'.join(getsource(func).split('\n')[1:]) |
| 121 | + for dev in self.devs.keys(): |
| 122 | + self.devs[dev].paste_buff(str_func) |
| 123 | + self.devs[dev].cmd('\x04', silent=True) |
| 124 | + |
| 125 | + @functools.wraps(func) |
| 126 | + def wrapper_cmd(*args, **kwargs): |
| 127 | + flags = ['>', '<', 'object', 'at', '0x'] |
| 128 | + args_repr = [repr(a) for a in args if any( |
| 129 | + f not in repr(a) for f in flags)] |
| 130 | + kwargs_repr = [f"{k}={v!r}" if not callable( |
| 131 | + v) else f"{k}={v.__name__}" for k, v in kwargs.items()] |
| 132 | + signature = ", ".join(args_repr + kwargs_repr) |
| 133 | + cmd_ = f"{func.__name__}({signature})" |
| 134 | + self.output = {} |
| 135 | + for dev in self.devs.keys(): |
| 136 | + self.devs[dev].wr_cmd(cmd_, rtn=True) |
| 137 | + if self.devs[dev].output: |
| 138 | + self.output[dev] = self.devs[dev].output |
| 139 | + if self.output: |
| 140 | + return self.output |
| 141 | + return wrapper_cmd |
| 142 | + |
| 143 | + def code_follow(self, func): |
| 144 | + str_func = '\n'.join(getsource(func).split('\n')[1:]) |
| 145 | + for dev in self.devs.keys(): |
| 146 | + self.devs[dev].paste_buff(str_func) |
| 147 | + self.devs[dev].cmd('\x04', silent=True) |
| 148 | + |
| 149 | + @functools.wraps(func) |
| 150 | + def wrapper_cmd(*args, **kwargs): |
| 151 | + flags = ['>', '<', 'object', 'at', '0x'] |
| 152 | + args_repr = [repr(a) for a in args if any( |
| 153 | + f not in repr(a) for f in flags)] |
| 154 | + kwargs_repr = [f"{k}={v!r}" if not callable( |
| 155 | + v) else f"{k}={v.__name__}" for k, v in kwargs.items()] |
| 156 | + signature = ", ".join(args_repr + kwargs_repr) |
| 157 | + cmd_ = f"{func.__name__}({signature})" |
| 158 | + self.output = {} |
| 159 | + for dev in self.devs.keys(): |
| 160 | + self.devs[dev].wr_cmd(cmd_, rtn=True, follow=True) |
| 161 | + if self.devs[dev].output: |
| 162 | + self.output[dev] = self.devs[dev].output |
| 163 | + if self.output: |
| 164 | + return self.output |
| 165 | + return wrapper_cmd |
0 commit comments