add attrs to dataclasses

This commit is contained in:
onyx-and-iris 2022-07-28 11:55:05 +01:00
parent 00a97b1d8b
commit ce9bc7e8d6
3 changed files with 12 additions and 3 deletions

View File

@ -21,7 +21,7 @@ class Callback:
for fn in self._callbacks:
if fn.__name__ == f"on_{to_snake_case(event)}":
fn(as_dataclass(event, data.get("eventData")))
fn(as_dataclass(event, data))
def register(self, fns: Union[Iterable, Callable]):
"""registers callback functions"""

View File

@ -57,7 +57,10 @@ class EventClient(object):
self.running = True
while self.running:
self.data = json.loads(self.base_client.ws.recv())
event, data = (self.data["d"].get("eventType"), self.data["d"])
event, data = (
self.data["d"].get("eventType"),
self.data["d"].get("eventData"),
)
self.callback.trigger(event, data)
time.sleep(self.DELAY)

View File

@ -11,10 +11,16 @@ def to_snake_case(s):
def as_dataclass(identifier, data):
def attrs():
return list(data.keys())
return dataclass(
type(
f"{identifier}Dataclass",
(),
{**{to_snake_case(k): v for k, v in data.items()}},
{
"attrs": attrs,
**{to_snake_case(k): v for k, v in data.items()},
},
)
)