Skip to content

Commit

Permalink
Merge pull request #90 from vkottler/dev/2.1.3
Browse files Browse the repository at this point in the history
Remove constraint on enum value in schema
  • Loading branch information
vkottler authored Aug 30, 2023
2 parents 3aa4c6d + 56126fd commit 230873f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 13 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.1.2
repo=runtimepy version=2.1.3
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[DESIGN]
max-args=7
max-args=8
max-attributes=12
max-parents=8

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.2
hash=64a758df8095e218dce93935454221a7
hash=6a75e29b42b1d2fa20b1373c2a8adc01
=====================================
-->

# runtimepy ([2.1.2](https://pypi.org/project/runtimepy/))
# runtimepy ([2.1.3](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
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: 1
patch: 2
patch: 3
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.1.2"
version = "2.1.3"
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.2
# hash=f83181278a252952c4fdf15493634816
# hash=933cfe7057a94bf7e43e8b9938cb8159
# =====================================

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

DESCRIPTION = "A framework for implementing Python services."
PKG_NAME = "runtimepy"
VERSION = "2.1.2"
VERSION = "2.1.3"
23 changes: 22 additions & 1 deletion runtimepy/codec/protocol/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from runtimepy.primitives.byte_order import ByteOrder as _ByteOrder
from runtimepy.primitives.field.fields import BitFields as _BitFields
from runtimepy.primitives.field.manager import BitFieldsManager
from runtimepy.primitives.serializable import Serializable, SerializableMap
from runtimepy.registry.name import NameRegistry as _NameRegistry
from runtimepy.registry.name import RegistryKey as _RegistryKey

Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(
build: _List[_Union[int, FieldSpec]] = None,
identifier: int = 1,
byte_order: _Union[_ByteOrder, _RegistryKey] = _DEFAULT_BYTE_ORDER,
serializables: SerializableMap = None,
) -> None:
"""Initialize this protocol."""

Expand Down Expand Up @@ -110,6 +112,12 @@ def __init__(
item.name, item.kind, enum=item.enum, track=False
)

# Keep track of named serializables.
self.serializables: SerializableMap = {}
if serializables is not None:
for name, serializable in serializables.items():
self.add_field(name, serializable=serializable)

def __copy__(self: T) -> T:
"""Create another protocol instance from this one."""

Expand All @@ -119,13 +127,15 @@ def __copy__(self: T) -> T:
fields=_copy(self._fields),
build=self._build,
byte_order=self.array.byte_order,
serializables=self.serializables,
)

def add_field(
self,
name: str,
kind: _Primitivelike = None,
enum: _RegistryKey = None,
serializable: Serializable = None,
track: bool = True,
) -> None:
"""Add a new field to the protocol."""
Expand All @@ -134,10 +144,21 @@ def add_field(
ident = self._names.register_name(name)
assert ident is not None, f"Couldn't register field '{name}'!"

# Add the serializable to the end of this protocol.
if serializable is not None:
assert kind is None and enum is None
self.serializables[name] = serializable
self.array.add_to_end(serializable)
return

if enum is not None:
runtime_enum = self._enum_registry[enum]
self._enum_fields[name] = runtime_enum
kind = runtime_enum.primitive

# Allow the primitive type to be overridden when passed as a
# method argument.
if kind is None:
kind = runtime_enum.primitive

assert kind is not None
new = _create(kind)
Expand Down
1 change: 0 additions & 1 deletion runtimepy/data/schemas/RuntimeEnum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ properties:
"^\\w+$":
oneOf:
- type: integer
minimum: 0
- type: boolean
4 changes: 2 additions & 2 deletions runtimepy/primitives/array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(
*primitives: _AnyPrimitive,
byte_order: _ByteOrder = _DEFAULT_BYTE_ORDER,
fragments: _List[ArrayFragmentSpec] = None,
next_array: "PrimitiveArray" = None,
chain: Serializable = None,
) -> None:
"""Initialize this primitive array."""

Expand All @@ -59,7 +59,7 @@ def __init__(
for item in primitives:
self.add(item)

super().__init__(byte_order=self.byte_order, chain=next_array)
super().__init__(byte_order=self.byte_order, chain=chain)

self._fragments: _List["PrimitiveArray"] = []
self._fragment_specs: _List[ArrayFragmentSpec] = []
Expand Down
7 changes: 6 additions & 1 deletion runtimepy/primitives/serializable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
A module defining an interface for serializable objects.
"""

# built-in
from typing import Dict

# internal
from runtimepy.primitives.serializable.base import Serializable
from runtimepy.primitives.serializable.fixed import FixedChunk
from runtimepy.primitives.serializable.prefixed import PrefixedChunk

__all__ = ["Serializable", "FixedChunk"]
SerializableMap = Dict[str, Serializable]
__all__ = ["Serializable", "SerializableMap", "FixedChunk", "PrefixedChunk"]
14 changes: 14 additions & 0 deletions tests/codec/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from runtimepy.codec.protocol import Protocol
from runtimepy.codec.protocol.base import FieldSpec
from runtimepy.enum.registry import EnumRegistry
from runtimepy.primitives.serializable import PrefixedChunk

# internal
from tests.resources import resource
Expand Down Expand Up @@ -62,3 +63,16 @@ def test_protocol_basic():
assert Protocol.import_json(load(stream))

assert str(proto)

# Create a string serializable.
string = PrefixedChunk.create()
assert string.length() == 2
assert string.update_str("abc") == 5

# Add the string to the end and confirm the size update.
curr = proto.size
proto.add_field("string", serializable=string)
assert proto.size == curr + 5

# Should be the same size.
assert copy(proto).size == proto.size

0 comments on commit 230873f

Please sign in to comment.