Compare commits
No commits in common. "eec240a70a6fea5412f970a22c6b91fc3472410c" and "0dcaf5f3b2c878c1c1417dba4efc6488a4ea5152" have entirely different histories.
eec240a70a
...
0dcaf5f3b2
|
@ -40,7 +40,7 @@ from tractor._state import current_actor
|
|||
from tractor.log import get_logger
|
||||
from tractor.msg import (
|
||||
Error,
|
||||
MsgType,
|
||||
Msg,
|
||||
Stop,
|
||||
Yield,
|
||||
pretty_struct,
|
||||
|
@ -130,10 +130,7 @@ def pformat_boxed_tb(
|
|||
tb_str: str,
|
||||
fields_str: str|None = None,
|
||||
field_prefix: str = ' |_',
|
||||
|
||||
tb_box_indent: int|None = None,
|
||||
tb_body_indent: int = 1,
|
||||
|
||||
indent: str = ' '*2
|
||||
) -> str:
|
||||
if (
|
||||
fields_str
|
||||
|
@ -142,19 +139,15 @@ def pformat_boxed_tb(
|
|||
):
|
||||
fields: str = textwrap.indent(
|
||||
fields_str,
|
||||
# prefix=' '*2,
|
||||
# prefix=' |_',
|
||||
prefix=field_prefix,
|
||||
)
|
||||
else:
|
||||
fields = fields_str or ''
|
||||
|
||||
tb_body = tb_str
|
||||
if tb_body_indent:
|
||||
tb_body: str = textwrap.indent(
|
||||
tb_str,
|
||||
prefix=tb_body_indent * ' ',
|
||||
)
|
||||
|
||||
tb_box: str = (
|
||||
# body_indent: str = len(field_prefix) * ' '
|
||||
body: str = (
|
||||
|
||||
# orig
|
||||
# f' |\n'
|
||||
|
@ -165,29 +158,21 @@ def pformat_boxed_tb(
|
|||
|
||||
f'|\n'
|
||||
f' ------ - ------\n\n'
|
||||
# f'{tb_str}\n'
|
||||
f'{tb_body}'
|
||||
f'{tb_str}\n'
|
||||
f' ------ - ------\n'
|
||||
f'_|\n'
|
||||
)
|
||||
tb_box_indent: str = (
|
||||
tb_box_indent
|
||||
or
|
||||
1
|
||||
|
||||
# (len(field_prefix))
|
||||
# ? ^-TODO-^ ? if you wanted another indent level
|
||||
)
|
||||
if tb_box_indent > 0:
|
||||
tb_box: str = textwrap.indent(
|
||||
tb_box,
|
||||
prefix=tb_box_indent * ' ',
|
||||
if len(indent):
|
||||
body: str = textwrap.indent(
|
||||
body,
|
||||
# prefix=body_indent,
|
||||
prefix=indent,
|
||||
)
|
||||
|
||||
return (
|
||||
fields
|
||||
+
|
||||
tb_box
|
||||
body
|
||||
)
|
||||
|
||||
|
||||
|
@ -331,7 +316,7 @@ class RemoteActorError(Exception):
|
|||
if self._ipc_msg is None:
|
||||
return None
|
||||
|
||||
msg_type: MsgType = type(self._ipc_msg)
|
||||
msg_type: Msg = type(self._ipc_msg)
|
||||
fields: dict[str, Any] = {
|
||||
k: v for _, k, v in
|
||||
pretty_struct.iter_fields(self._ipc_msg)
|
||||
|
@ -508,10 +493,7 @@ class RemoteActorError(Exception):
|
|||
tb_str=self.tb_str,
|
||||
fields_str=fields,
|
||||
field_prefix=' |_',
|
||||
# ^- is so that it's placed like so,
|
||||
# just after <Type(
|
||||
# |___ ..
|
||||
tb_body_indent=1,
|
||||
indent=' ', # no indent?
|
||||
)
|
||||
return (
|
||||
f'<{type(self).__name__}(\n'
|
||||
|
@ -641,7 +623,7 @@ class MsgTypeError(
|
|||
|
||||
'''
|
||||
reprol_fields: list[str] = [
|
||||
'payload_msg',
|
||||
'ipc_msg',
|
||||
]
|
||||
extra_body_fields: list[str] = [
|
||||
'cid',
|
||||
|
@ -651,7 +633,7 @@ class MsgTypeError(
|
|||
@property
|
||||
def msg_dict(self) -> dict[str, Any]:
|
||||
'''
|
||||
If the underlying IPC `MsgType` was received from a remote
|
||||
If the underlying IPC `Msg` was received from a remote
|
||||
actor but was unable to be decoded to a native
|
||||
`Yield`|`Started`|`Return` struct, the interchange backend
|
||||
native format decoder can be used to stash a `dict`
|
||||
|
@ -661,21 +643,22 @@ class MsgTypeError(
|
|||
return self.msgdata.get('_msg_dict')
|
||||
|
||||
@property
|
||||
def payload_msg(
|
||||
self,
|
||||
) -> MsgType|None:
|
||||
def payload_msg(self) -> Msg|None:
|
||||
'''
|
||||
Attempt to construct what would have been the original
|
||||
`MsgType`-with-payload subtype (i.e. an instance from the set
|
||||
`Msg`-with-payload subtype (i.e. an instance from the set
|
||||
of msgs in `.msg.types._payload_msgs`) which failed
|
||||
validation.
|
||||
|
||||
'''
|
||||
if msg_dict := self.msg_dict.copy():
|
||||
return msgtypes.from_dict_msg(
|
||||
dict_msg=msg_dict,
|
||||
)
|
||||
return None
|
||||
msg_dict: dict = self.msg_dict.copy()
|
||||
name: str = msg_dict.pop('msg_type')
|
||||
msg_type: Msg = getattr(
|
||||
msgtypes,
|
||||
name,
|
||||
Msg,
|
||||
)
|
||||
return msg_type(**msg_dict)
|
||||
|
||||
@property
|
||||
def cid(self) -> str:
|
||||
|
@ -925,7 +908,7 @@ def is_multi_cancelled(exc: BaseException) -> bool:
|
|||
|
||||
def _raise_from_no_key_in_msg(
|
||||
ctx: Context,
|
||||
msg: MsgType,
|
||||
msg: Msg,
|
||||
src_err: KeyError,
|
||||
log: StackLevelAdapter, # caller specific `log` obj
|
||||
|
||||
|
|
|
@ -53,9 +53,6 @@ from .types import (
|
|||
|
||||
Error as Error,
|
||||
|
||||
# type-var for `.pld` field
|
||||
PayloadT as PayloadT,
|
||||
|
||||
# full msg class set from above as list
|
||||
__msg_types__ as __msg_types__,
|
||||
|
||||
|
|
|
@ -37,7 +37,6 @@ from contextlib import (
|
|||
# ContextVar,
|
||||
# Token,
|
||||
# )
|
||||
import textwrap
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
|
@ -60,9 +59,7 @@ from tractor.msg.types import (
|
|||
mk_msg_spec,
|
||||
MsgType,
|
||||
)
|
||||
from tractor.log import get_logger
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
# TODO: overall IPC msg-spec features (i.e. in this mod)!
|
||||
#
|
||||
|
@ -90,27 +87,6 @@ class MsgCodec(Struct):
|
|||
|
||||
pld_spec: Union[Type[Struct]]|None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
speclines: str = textwrap.indent(
|
||||
self.pformat_msg_spec(),
|
||||
prefix=' '*3,
|
||||
)
|
||||
body: str = textwrap.indent(
|
||||
f'|_lib = {self.lib.__name__!r}\n'
|
||||
f'|_enc_hook: {self.enc.enc_hook}\n'
|
||||
f'|_dec_hook: {self.dec.dec_hook}\n'
|
||||
f'|_pld_spec: {self.pld_spec_str}\n'
|
||||
# f'|\n'
|
||||
f'|__msg_spec__:\n'
|
||||
f'{speclines}\n',
|
||||
prefix=' '*2,
|
||||
)
|
||||
return (
|
||||
f'<{type(self).__name__}(\n'
|
||||
f'{body}'
|
||||
')>'
|
||||
)
|
||||
|
||||
@property
|
||||
def pld_spec_str(self) -> str:
|
||||
spec: Union[Type]|Type = self.pld_spec
|
||||
|
@ -187,8 +163,8 @@ class MsgCodec(Struct):
|
|||
|
||||
) -> bytes:
|
||||
'''
|
||||
Encode input python objects to `msgpack` bytes for
|
||||
transfer on a tranport protocol connection.
|
||||
Encode input python objects to `msgpack` bytes for transfer
|
||||
on a tranport protocol connection.
|
||||
|
||||
'''
|
||||
return self._enc.encode(py_obj)
|
||||
|
@ -349,9 +325,15 @@ class MsgCodec(Struct):
|
|||
|
||||
|
||||
def mk_codec(
|
||||
ipc_msg_spec: Union[Type[Struct]]|Any|None = None,
|
||||
#
|
||||
# ^TODO^: in the long run, do we want to allow using a diff IPC `Msg`-set?
|
||||
# it would break the runtime, but maybe say if you wanted
|
||||
# to add some kinda field-specific or wholesale `.pld` ecryption?
|
||||
|
||||
# struct type unions set for `Decoder`
|
||||
# https://jcristharif.com/msgspec/structs.html#tagged-unions
|
||||
ipc_pld_spec: Union[Type[Struct]]|Any = Any,
|
||||
ipc_pld_spec: Union[Type[Struct]]|Any|None = None,
|
||||
|
||||
# TODO: offering a per-msg(-field) type-spec such that
|
||||
# the fields can be dynamically NOT decoded and left as `Raw`
|
||||
|
@ -370,6 +352,7 @@ def mk_codec(
|
|||
dec_hook: Callable|None = None,
|
||||
enc_hook: Callable|None = None,
|
||||
# ------ - ------
|
||||
**kwargs,
|
||||
#
|
||||
# Encoder:
|
||||
# write_buffer_size=write_buffer_size,
|
||||
|
@ -384,19 +367,44 @@ def mk_codec(
|
|||
`msgspec` ;).
|
||||
|
||||
'''
|
||||
# (manually) generate a msg-payload-spec for all relevant
|
||||
# god-boxing-msg subtypes, parameterizing the `Msg.pld: PayloadT`
|
||||
# for the decoder such that all sub-type msgs in our SCIPP
|
||||
# will automatically decode to a type-"limited" payload (`Struct`)
|
||||
# object (set).
|
||||
(
|
||||
ipc_msg_spec,
|
||||
msg_types,
|
||||
) = mk_msg_spec(
|
||||
payload_type_union=ipc_pld_spec,
|
||||
)
|
||||
assert len(ipc_msg_spec.__args__) == len(msg_types)
|
||||
assert ipc_msg_spec
|
||||
if (
|
||||
ipc_msg_spec is not None
|
||||
and ipc_pld_spec
|
||||
):
|
||||
raise RuntimeError(
|
||||
f'If a payload spec is provided,\n'
|
||||
"the builtin SC-shuttle-protocol's msg set\n"
|
||||
f'(i.e. a `{MsgType}`) MUST be used!\n\n'
|
||||
f'However both values were passed as => mk_codec(\n'
|
||||
f' ipc_msg_spec={ipc_msg_spec}`\n'
|
||||
f' ipc_pld_spec={ipc_pld_spec}`\n)\n'
|
||||
)
|
||||
|
||||
elif (
|
||||
ipc_pld_spec
|
||||
and
|
||||
|
||||
# XXX required for now (or maybe forever?) until
|
||||
# we can dream up a way to allow parameterizing and/or
|
||||
# custom overrides to the `Msg`-spec protocol itself?
|
||||
ipc_msg_spec is None
|
||||
):
|
||||
# (manually) generate a msg-payload-spec for all relevant
|
||||
# god-boxing-msg subtypes, parameterizing the `Msg.pld: PayloadT`
|
||||
# for the decoder such that all sub-type msgs in our SCIPP
|
||||
# will automatically decode to a type-"limited" payload (`Struct`)
|
||||
# object (set).
|
||||
(
|
||||
ipc_msg_spec,
|
||||
msg_types,
|
||||
) = mk_msg_spec(
|
||||
payload_type_union=ipc_pld_spec,
|
||||
)
|
||||
assert len(ipc_msg_spec.__args__) == len(msg_types)
|
||||
assert ipc_msg_spec
|
||||
|
||||
else:
|
||||
ipc_msg_spec = ipc_msg_spec or Any
|
||||
|
||||
enc = msgpack.Encoder(
|
||||
enc_hook=enc_hook,
|
||||
|
@ -410,6 +418,8 @@ def mk_codec(
|
|||
_enc=enc,
|
||||
_dec=dec,
|
||||
pld_spec=ipc_pld_spec,
|
||||
# payload_msg_specs=payload_msg_specs,
|
||||
# **kwargs,
|
||||
)
|
||||
|
||||
# sanity on expected backend support
|
||||
|
@ -490,16 +500,8 @@ def apply_codec(
|
|||
- https://github.com/oremanj/tricycle/blob/master/tricycle/_tests/test_tree_var.py
|
||||
|
||||
'''
|
||||
__tracebackhide__: bool = True
|
||||
orig: MsgCodec = _ctxvar_MsgCodec.get()
|
||||
assert orig is not codec
|
||||
if codec.pld_spec is None:
|
||||
breakpoint()
|
||||
|
||||
log.info(
|
||||
'Applying new msg-spec codec\n\n'
|
||||
f'{codec}\n'
|
||||
)
|
||||
token: RunVarToken = _ctxvar_MsgCodec.set(codec)
|
||||
|
||||
# TODO: for TreeVar approach, see docs for @cm `.being()` API:
|
||||
|
@ -516,10 +518,7 @@ def apply_codec(
|
|||
_ctxvar_MsgCodec.reset(token)
|
||||
|
||||
assert _ctxvar_MsgCodec.get() is orig
|
||||
log.info(
|
||||
'Reverted to last msg-spec codec\n\n'
|
||||
f'{orig}\n'
|
||||
)
|
||||
|
||||
|
||||
def current_codec() -> MsgCodec:
|
||||
'''
|
||||
|
@ -533,15 +532,14 @@ def current_codec() -> MsgCodec:
|
|||
|
||||
@cm
|
||||
def limit_msg_spec(
|
||||
payload_spec: Union[Type[Struct]],
|
||||
payload_types: Union[Type[Struct]],
|
||||
|
||||
# TODO: don't need this approach right?
|
||||
# -> related to the `MsgCodec._payload_decs` stuff above..
|
||||
# tagged_structs: list[Struct]|None = None,
|
||||
|
||||
**codec_kwargs,
|
||||
|
||||
) -> MsgCodec:
|
||||
):
|
||||
'''
|
||||
Apply a `MsgCodec` that will natively decode the SC-msg set's
|
||||
`Msg.pld: Union[Type[Struct]]` payload fields using
|
||||
|
@ -549,37 +547,10 @@ def limit_msg_spec(
|
|||
for all IPC contexts in use by the current `trio.Task`.
|
||||
|
||||
'''
|
||||
__tracebackhide__: bool = True
|
||||
curr_codec = current_codec()
|
||||
msgspec_codec: MsgCodec = mk_codec(
|
||||
ipc_pld_spec=payload_spec,
|
||||
payload_types=payload_types,
|
||||
**codec_kwargs,
|
||||
)
|
||||
with apply_codec(msgspec_codec) as applied_codec:
|
||||
assert applied_codec is msgspec_codec
|
||||
yield msgspec_codec
|
||||
|
||||
assert curr_codec is current_codec()
|
||||
|
||||
|
||||
# XXX: msgspec won't allow this with non-struct custom types
|
||||
# like `NamespacePath`!@!
|
||||
# @cm
|
||||
# def extend_msg_spec(
|
||||
# payload_spec: Union[Type[Struct]],
|
||||
|
||||
# ) -> MsgCodec:
|
||||
# '''
|
||||
# Extend the current `MsgCodec.pld_spec` (type set) by extending
|
||||
# the payload spec to **include** the types specified by
|
||||
# `payload_spec`.
|
||||
|
||||
# '''
|
||||
# codec: MsgCodec = current_codec()
|
||||
# pld_spec: Union[Type] = codec.pld_spec
|
||||
# extended_spec: Union[Type] = pld_spec|payload_spec
|
||||
|
||||
# with limit_msg_spec(payload_types=extended_spec) as ext_codec:
|
||||
# # import pdbp; pdbp.set_trace()
|
||||
# assert ext_codec.pld_spec == extended_spec
|
||||
# yield ext_codec
|
||||
|
|
|
@ -140,7 +140,6 @@ class Struct(
|
|||
|
||||
return sin_props
|
||||
|
||||
# TODO: make thisi a mod-func!
|
||||
def pformat(
|
||||
self,
|
||||
field_indent: int = 2,
|
||||
|
|
|
@ -447,29 +447,6 @@ class Error(
|
|||
_msg_dict: dict|None = None
|
||||
|
||||
|
||||
def from_dict_msg(
|
||||
dict_msg: dict,
|
||||
|
||||
msgT: MsgType|None = None,
|
||||
tag_field: str = 'msg_type'
|
||||
|
||||
) -> MsgType:
|
||||
'''
|
||||
Helper to build a specific `MsgType` struct from
|
||||
a "vanilla" decoded `dict`-ified equivalent of the
|
||||
msg: i.e. if the `msgpack.Decoder.type == Any`.
|
||||
|
||||
'''
|
||||
msg_type_tag_field: str = (
|
||||
msgT.__struct_config__.tag_field
|
||||
if msgT is not None
|
||||
else tag_field
|
||||
)
|
||||
# XXX ensure tag field is removed
|
||||
msgT_name: str = dict_msg.pop(msg_type_tag_field)
|
||||
msgT: MsgType = _msg_table[msgT_name]
|
||||
return msgT(**dict_msg)
|
||||
|
||||
# TODO: should be make a msg version of `ContextCancelled?`
|
||||
# and/or with a scope field or a full `ActorCancelled`?
|
||||
# class Cancelled(Msg):
|
||||
|
@ -521,18 +498,12 @@ _payload_msgs: list[Msg] = [
|
|||
|
||||
# built-in SC shuttle protocol msg type set in
|
||||
# approx order of the IPC txn-state spaces.
|
||||
__msg_types__: list[MsgType] = (
|
||||
__msg_types__: list[Msg] = (
|
||||
_runtime_msgs
|
||||
+
|
||||
_payload_msgs
|
||||
)
|
||||
|
||||
|
||||
_msg_table: dict[str, MsgType] = {
|
||||
msgT.__name__: msgT
|
||||
for msgT in __msg_types__
|
||||
}
|
||||
|
||||
# TODO: use new type declaration syntax for msg-type-spec
|
||||
# https://docs.python.org/3/library/typing.html#type-aliases
|
||||
# https://docs.python.org/3/reference/simple_stmts.html#type
|
||||
|
@ -689,11 +660,6 @@ def mk_msg_spec(
|
|||
'Generating new IPC msg-spec\n'
|
||||
f'{ipc_spec}\n'
|
||||
)
|
||||
assert (
|
||||
ipc_spec
|
||||
and
|
||||
ipc_spec is not Any
|
||||
)
|
||||
return (
|
||||
ipc_spec,
|
||||
msgtypes_table[spec_build_method] + ipc_msg_types,
|
||||
|
@ -703,9 +669,9 @@ def mk_msg_spec(
|
|||
# TODO: make something similar to this inside `._codec` such that
|
||||
# user can just pass a type table of some sort?
|
||||
# -[ ] we would need to decode all msgs to `pretty_struct.Struct`
|
||||
# and then call `.to_dict()` on them?
|
||||
# and then call `.to_dict()` on them?
|
||||
# -[ ] we're going to need to re-impl all the stuff changed in the
|
||||
# runtime port such that it can handle dicts or `Msg`s?
|
||||
# runtime port such that it can handle dicts or `Msg`s?
|
||||
#
|
||||
# def mk_dict_msg_codec_hooks() -> tuple[Callable, Callable]:
|
||||
# '''
|
||||
|
@ -713,15 +679,88 @@ def mk_msg_spec(
|
|||
# manual convertion from our above native `Msg` set
|
||||
# to `dict` equivalent (wire msgs) in order to keep legacy compat
|
||||
# with the original runtime implementation.
|
||||
#
|
||||
|
||||
# Note: this is is/was primarly used while moving the core
|
||||
# runtime over to using native `Msg`-struct types wherein we
|
||||
# start with the send side emitting without loading
|
||||
# a typed-decoder and then later flipping the switch over to
|
||||
# load to the native struct types once all runtime usage has
|
||||
# been adjusted appropriately.
|
||||
#
|
||||
|
||||
# '''
|
||||
# def enc_to_dict(msg: Any) -> Any:
|
||||
# '''
|
||||
# Encode `Msg`-structs to `dict` msgs instead
|
||||
# of using `msgspec.msgpack.Decoder.type`-ed
|
||||
# features.
|
||||
|
||||
# '''
|
||||
# match msg:
|
||||
# case Start():
|
||||
# dctmsg: dict = pretty_struct.Struct.to_dict(
|
||||
# msg
|
||||
# )['pld']
|
||||
|
||||
# case Error():
|
||||
# dctmsg: dict = pretty_struct.Struct.to_dict(
|
||||
# msg
|
||||
# )['pld']
|
||||
# return {'error': dctmsg}
|
||||
|
||||
|
||||
# def dec_from_dict(
|
||||
# type: Type,
|
||||
# obj: Any,
|
||||
# ) -> Any:
|
||||
# '''
|
||||
# Decode to `Msg`-structs from `dict` msgs instead
|
||||
# of using `msgspec.msgpack.Decoder.type`-ed
|
||||
# features.
|
||||
|
||||
# '''
|
||||
# cid: str = obj.get('cid')
|
||||
# match obj:
|
||||
# case {'cmd': pld}:
|
||||
# return Start(
|
||||
# cid=cid,
|
||||
# pld=pld,
|
||||
# )
|
||||
# case {'functype': pld}:
|
||||
# return StartAck(
|
||||
# cid=cid,
|
||||
# functype=pld,
|
||||
# # pld=IpcCtxSpec(
|
||||
# # functype=pld,
|
||||
# # ),
|
||||
# )
|
||||
# case {'started': pld}:
|
||||
# return Started(
|
||||
# cid=cid,
|
||||
# pld=pld,
|
||||
# )
|
||||
# case {'yield': pld}:
|
||||
# return Yield(
|
||||
# cid=obj['cid'],
|
||||
# pld=pld,
|
||||
# )
|
||||
# case {'stop': pld}:
|
||||
# return Stop(
|
||||
# cid=cid,
|
||||
# )
|
||||
# case {'return': pld}:
|
||||
# return Return(
|
||||
# cid=cid,
|
||||
# pld=pld,
|
||||
# )
|
||||
|
||||
# case {'error': pld}:
|
||||
# return Error(
|
||||
# cid=cid,
|
||||
# pld=ErrorData(
|
||||
# **pld
|
||||
# ),
|
||||
# )
|
||||
|
||||
# return (
|
||||
# # enc_to_dict,
|
||||
# dec_from_dict,
|
||||
|
|
Loading…
Reference in New Issue