|
| 1 | +# tractor: structured concurrent "actors". |
| 2 | +# Copyright 2018-eternity Tyler Goodlet. |
| 3 | + |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU Affero General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | + |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU Affero General Public License for more details. |
| 13 | + |
| 14 | +# You should have received a copy of the GNU Affero General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +''' |
| 18 | +Capability-based messaging specifications: or colloquially as "msgspecs". |
| 19 | +
|
| 20 | +Includes our SCIPP (structured-con-inter-process-protocol) message type defs |
| 21 | +and APIs for applying custom msgspec-sets for implementing un-protocol state machines. |
| 22 | +
|
| 23 | +''' |
| 24 | + |
| 25 | +# TODO: integration with our ``enable_modules: list[str]`` caps sys. |
| 26 | + |
| 27 | +# ``pkgutil.resolve_name()`` internally uses |
| 28 | +# ``importlib.import_module()`` which can be filtered by inserting |
| 29 | +# a ``MetaPathFinder`` into ``sys.meta_path`` (which we could do before |
| 30 | +# entering the ``Actor._process_messages()`` loop). |
| 31 | +# https://github.com/python/cpython/blob/main/Lib/pkgutil.py#L645 |
| 32 | +# https://stackoverflow.com/questions/1350466/preventing-python-code-from-importing-certain-modules |
| 33 | +# - https://stackoverflow.com/a/63320902 |
| 34 | +# - https://docs.python.org/3/library/sys.html#sys.meta_path |
| 35 | + |
| 36 | +# the new "Implicit Namespace Packages" might be relevant? |
| 37 | +# - https://www.python.org/dev/peps/pep-0420/ |
| 38 | + |
| 39 | +# add implicit serialized message type support so that paths can be |
| 40 | +# handed directly to IPC primitives such as streams and `Portal.run()` |
| 41 | +# calls: |
| 42 | +# - via ``msgspec``: |
| 43 | +# - https://jcristharif.com/msgspec/api.html#struct |
| 44 | +# - https://jcristharif.com/msgspec/extending.html |
| 45 | +# via ``msgpack-python``: |
| 46 | +# https://github.com/msgpack/msgpack-python#packingunpacking-of-custom-data-type |
| 47 | + |
| 48 | +from __future__ import annotations |
| 49 | +from contextlib import contextmanager as cm |
| 50 | +from typing import ( |
| 51 | + Union, |
| 52 | + Any, |
| 53 | +) |
| 54 | + |
| 55 | +from msgspec import Struct |
| 56 | +from msgspec.msgpack import ( |
| 57 | + Encoder, |
| 58 | + Decoder, |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +# LIFO codec stack that is appended when the user opens the |
| 63 | +# ``configure_native_msgs()`` cm below to configure a new codec set |
| 64 | +# which will be applied to all new (msgspec relevant) IPC transports |
| 65 | +# that are spawned **after** the configure call is made. |
| 66 | +_lifo_codecs: list[ |
| 67 | + tuple[ |
| 68 | + Encoder, |
| 69 | + Decoder, |
| 70 | + ], |
| 71 | +] = [(Encoder(), Decoder())] |
| 72 | + |
| 73 | + |
| 74 | +def get_msg_codecs() -> tuple[ |
| 75 | + Encoder, |
| 76 | + Decoder, |
| 77 | +]: |
| 78 | + ''' |
| 79 | + Return the currently configured ``msgspec`` codec set. |
| 80 | +
|
| 81 | + The defaults are defined above. |
| 82 | +
|
| 83 | + ''' |
| 84 | + global _lifo_codecs |
| 85 | + return _lifo_codecs[-1] |
| 86 | + |
| 87 | + |
| 88 | +@cm |
| 89 | +def configure_native_msgs( |
| 90 | + tagged_structs: list[Struct], |
| 91 | +): |
| 92 | + ''' |
| 93 | + Push a codec set that will natively decode |
| 94 | + tagged structs provied in ``tagged_structs`` |
| 95 | + in all IPC transports and pop the codec on exit. |
| 96 | +
|
| 97 | + ''' |
| 98 | + global _lifo_codecs |
| 99 | + |
| 100 | + # See "tagged unions" docs: |
| 101 | + # https://jcristharif.com/msgspec/structs.html#tagged-unions |
| 102 | + |
| 103 | + # "The quickest way to enable tagged unions is to set tag=True when |
| 104 | + # defining every struct type in the union. In this case tag_field |
| 105 | + # defaults to "type", and tag defaults to the struct class name |
| 106 | + # (e.g. "Get")." |
| 107 | + enc = Encoder() |
| 108 | + |
| 109 | + types_union = Union[tagged_structs[0]] | Any |
| 110 | + for struct in tagged_structs[1:]: |
| 111 | + types_union |= struct |
| 112 | + |
| 113 | + dec = Decoder(types_union) |
| 114 | + |
| 115 | + _lifo_codecs.append((enc, dec)) |
| 116 | + try: |
| 117 | + print("YOYOYOOYOYOYOY") |
| 118 | + yield enc, dec |
| 119 | + finally: |
| 120 | + print("NONONONONON") |
| 121 | + _lifo_codecs.pop() |
0 commit comments