diff --git a/capnp/lib/capnp.pxd b/capnp/lib/capnp.pxd index 179c89a..3f78126 100644 --- a/capnp/lib/capnp.pxd +++ b/capnp/lib/capnp.pxd @@ -1,6 +1,6 @@ from .capnp.includes cimport capnp_cpp as capnp from .capnp.includes cimport schema_cpp -from .capnp.includes.capnp_cpp cimport Schema as C_Schema, StructSchema as C_StructSchema, InterfaceSchema as C_InterfaceSchema, DynamicStruct as C_DynamicStruct, DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, String, StringTree, DynamicOrphan as C_DynamicOrphan, AnyPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, Request, Response, RemotePromise, PyPromise, VoidPromise, CallContext, PyRestorer, RpcSystem, makeRpcServer, makeRpcClient, Capability as C_Capability, TwoPartyVatNetwork as C_TwoPartyVatNetwork, Side, AsyncIoStream, Own, makeTwoPartyVatNetwork, PromiseFulfillerPair as C_PromiseFulfillerPair, copyPromiseFulfillerPair, newPromiseAndFulfiller, PyArray, DynamicStruct_Builder +from .capnp.includes.capnp_cpp cimport Schema as C_Schema, StructSchema as C_StructSchema, InterfaceSchema as C_InterfaceSchema, EnumSchema as C_EnumSchema, DynamicStruct as C_DynamicStruct, DynamicValue as C_DynamicValue, Type as C_Type, DynamicList as C_DynamicList, SchemaParser as C_SchemaParser, ParsedSchema as C_ParsedSchema, VOID, ArrayPtr, StringPtr, String, StringTree, DynamicOrphan as C_DynamicOrphan, AnyPointer as C_DynamicObject, DynamicCapability as C_DynamicCapability, Request, Response, RemotePromise, PyPromise, VoidPromise, CallContext, PyRestorer, RpcSystem, makeRpcServer, makeRpcClient, Capability as C_Capability, TwoPartyVatNetwork as C_TwoPartyVatNetwork, Side, AsyncIoStream, Own, makeTwoPartyVatNetwork, PromiseFulfillerPair as C_PromiseFulfillerPair, copyPromiseFulfillerPair, newPromiseAndFulfiller, PyArray, DynamicStruct_Builder from .capnp.includes.schema_cpp cimport Node as C_Node, EnumNode as C_EnumNode from .capnp.includes.types cimport * from .capnp.helpers.non_circular cimport reraise_kj_exception diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 76d2792..dd536d8 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -343,6 +343,9 @@ cdef class _NodeReader: property isInterface: def __get__(self): return self.thisptr.isInterface() + property isEnum: + def __get__(self): + return self.thisptr.isEnum() cdef class _NestedNodeReader: cdef C_Node.NestedNode.Reader thisptr @@ -1963,6 +1966,9 @@ cdef class _Schema: cpdef as_interface(self): return _InterfaceSchema()._init(self.thisptr.asInterface()) + cpdef as_enum(self): + return _EnumSchema()._init(self.thisptr.asEnum()) + cpdef get_dependency(self, id): return _Schema()._init(self.thisptr.getDependency(id)) @@ -2066,6 +2072,29 @@ cdef class _InterfaceSchema: def __repr__(self): return '' % self.node.displayName +cdef class _EnumSchema: + cdef C_EnumSchema thisptr + + cdef _init(self, C_EnumSchema other): + self.thisptr = other + return self + + property enumerants: + """The list of enumerants as a dictionary""" + def __get__(self): + ret = {} + enumerants = self.thisptr.getEnumerants() + for i in range(enumerants.size()): + enumerant = enumerants[i] + ret[enumerant.getProto().getName().cStr()] = enumerant.getOrdinal() + + return ret + + property node: + """The raw schema node""" + def __get__(self): + return _DynamicStructReader()._init(self.thisptr.getProto(), self) + cdef class _ParsedSchema(_Schema): cdef C_ParsedSchema thisptr_child cdef _init_child(self, C_ParsedSchema other): @@ -2229,6 +2258,14 @@ class _InterfaceModule(object): def _new_server(self, server): return _DynamicCapabilityServer(self.schema, server) +class _EnumModule(object): + def __init__(self, schema, name): + def server_init(server_self): + pass + self.schema = schema + for name, val in schema.enumerants.items(): + setattr(self, name, val) + cdef class SchemaParser: """A class for loading Cap'n Proto schema files. @@ -2325,6 +2362,10 @@ cdef class SchemaParser: elif proto.isInterface: local_module = _InterfaceModule(schema.as_interface(), node.name) + module.__dict__[node.name] = local_module + elif proto.isEnum: + local_module = _EnumModule(schema.as_enum(), node.name) + module.__dict__[node.name] = local_module _load(schema, local_module)