Skip to content

Commit

Permalink
2.6.2 - JSON message target resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Sep 8, 2023
1 parent ca89a29 commit 3dea6db
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=runtimepy version=2.6.1
repo=runtimepy version=2.6.2
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.3
hash=235f889fe10d86a261fbe780916da99b
hash=a3111c8378903228c0a4d8a5626629ec
=====================================
-->

# runtimepy ([2.6.1](https://pypi.org/project/runtimepy/))
# runtimepy ([2.6.2](https://pypi.org/project/runtimepy/))

[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
![Build Status](https://github.com/vkottler/runtimepy/workflows/Python%20Package/badge.svg)
Expand Down Expand Up @@ -76,13 +76,14 @@ commands:
```
$ ./venv3.11/bin/runtimepy arbiter -h
usage: runtimepy arbiter [-h] configs [configs ...]
usage: runtimepy arbiter [-h] [--init_only] configs [configs ...]
positional arguments:
configs the configuration to load
configs the configuration to load
options:
-h, --help show this help message and exit
-h, --help show this help message and exit
--init_only exit after completing initialization
```

Expand Down
2 changes: 1 addition & 1 deletion config
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 2
minor: 6
patch: 1
patch: 2
entry: runtimepy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "runtimepy"
version = "2.6.1"
version = "2.6.2"
description = "A framework for implementing Python services."
readme = "README.md"
requires-python = ">=3.8"
Expand Down
4 changes: 2 additions & 2 deletions runtimepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.3
# hash=9236c2428b54a8a3232c1f65708a46e5
# hash=f1035fea70e8a5cff741d00f44d92cb5
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "2.6.1"
VERSION = "2.6.2"
65 changes: 30 additions & 35 deletions runtimepy/net/stream/json/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# third-party
from vcorelib.dict.codec import JsonCodec
from vcorelib.target.resolver import TargetResolver

# internal
from runtimepy import PKG_NAME, VERSION
Expand All @@ -26,7 +27,6 @@
RESERVED_KEYS,
JsonMessage,
MessageHandler,
MessageHandlers,
T,
TypedHandler,
)
Expand All @@ -51,10 +51,7 @@ def init(self) -> None:

super().init()

self.handlers: MessageHandlers = {}
self.typed_handlers: Dict[
str, Tuple[Type[JsonCodec], TypedHandler[Any]]
] = {}
self.targets = TargetResolver()

self.meta = {
"package": PKG_NAME,
Expand All @@ -76,9 +73,9 @@ def init(self) -> None:

self._register_handlers()

self.meta["handlers"] = list( # type: ignore
set(self.handlers.keys()) | set(self.typed_handlers.keys())
)
self.meta["handlers"] = list(self.targets.literals) + [ # type: ignore
x.data for x in self.targets.dynamic
]

self.logger.info(
"metadata: package=%s, version=%s, kind=%s, handlers=%s",
Expand All @@ -88,34 +85,19 @@ def init(self) -> None:
self.meta["handlers"],
)

def _validate_key(self, key: str) -> str:
"""Validate a handler key."""

assert self._valid_new_key(key), key
return key

def _valid_new_key(self, key: str) -> bool:
"""Determine if a key is valid."""

return (
key not in self.handlers
and key not in self.typed_handlers
and key not in RESERVED_KEYS
)

def basic_handler(
self, key: str, handler: MessageHandler = loopback_handler
) -> None:
"""Register a basic handler."""

self.handlers[self._validate_key(key)] = handler
assert self.targets.register(key, (key, handler, None))

def typed_handler(
self, key: str, kind: Type[T], handler: TypedHandler[T]
) -> None:
"""Register a typed handler."""

self.typed_handlers[self._validate_key(key)] = (kind, handler)
assert self.targets.register(key, (key, handler, kind))

def send_json(
self, data: Union[JsonMessage, JsonCodec], addr: Tuple[str, int] = None
Expand Down Expand Up @@ -290,18 +272,31 @@ async def process_json(
sub_responses: JsonMessage = {}

for key, item in data.items():
if self._valid_new_key(key):
keys_ignored.append(key)
continue

sub_response: JsonMessage = {}

# Prepare handler. Each sets its own response data.
if key in self.handlers:
tasks.append(self.handlers[key](sub_response, item))
elif key in self.typed_handlers:
kind, handler = self.typed_handlers[key]
tasks.append(handler(sub_response, kind.create(item)))
target = self.targets.evaluate(key)
if target:
assert target.data is not None
key, handler, kind = target.data

# Use target resolution data (if any) as a base.
with_sub_data = copy(
target.result.substitutions
if target.result.substitutions
else {}
)
with_sub_data.update(item)

if kind is None:
tasks.append(handler(sub_response, with_sub_data))
else:
tasks.append(
handler(sub_response, kind.create(with_sub_data))
)

elif key not in RESERVED_KEYS:
keys_ignored.append(key)
continue

sub_responses[key] = sub_response

Expand Down

0 comments on commit 3dea6db

Please sign in to comment.