From 2d22713806db49afdf12332fce013d2ba6fab64d Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sun, 14 Apr 2024 16:29:21 -0400 Subject: [PATCH] Add `from_dict_msg(user_pretty: bool)` flag Allows for optionally (and dynamically) constructing the "expected" `MsgType` from a `dict` into a `pretty_struct.Struct`, mostly for logging usage. --- tractor/msg/types.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tractor/msg/types.py b/tractor/msg/types.py index 9787504..f7654f6 100644 --- a/tractor/msg/types.py +++ b/tractor/msg/types.py @@ -451,7 +451,8 @@ def from_dict_msg( dict_msg: dict, msgT: MsgType|None = None, - tag_field: str = 'msg_type' + tag_field: str = 'msg_type', + use_pretty: bool = False, ) -> MsgType: ''' @@ -468,6 +469,19 @@ def from_dict_msg( # XXX ensure tag field is removed msgT_name: str = dict_msg.pop(msg_type_tag_field) msgT: MsgType = _msg_table[msgT_name] + if use_pretty: + msgT = defstruct( + name=msgT_name, + fields=[ + (key, fi.type) + for fi, key, _ + in pretty_struct.iter_fields(msgT) + ], + bases=( + pretty_struct.Struct, + msgT, + ), + ) return msgT(**dict_msg) # TODO: should be make a msg version of `ContextCancelled?`