Add EnumSchema and corresponding module to load

This commit is contained in:
Jason Paryani
2014-02-05 15:32:14 -08:00
parent c9130ac44a
commit 345b8a6911
2 changed files with 42 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
from .capnp.includes cimport capnp_cpp as capnp from .capnp.includes cimport capnp_cpp as capnp
from .capnp.includes cimport schema_cpp 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.schema_cpp cimport Node as C_Node, EnumNode as C_EnumNode
from .capnp.includes.types cimport * from .capnp.includes.types cimport *
from .capnp.helpers.non_circular cimport reraise_kj_exception from .capnp.helpers.non_circular cimport reraise_kj_exception

View File

@@ -343,6 +343,9 @@ cdef class _NodeReader:
property isInterface: property isInterface:
def __get__(self): def __get__(self):
return self.thisptr.isInterface() return self.thisptr.isInterface()
property isEnum:
def __get__(self):
return self.thisptr.isEnum()
cdef class _NestedNodeReader: cdef class _NestedNodeReader:
cdef C_Node.NestedNode.Reader thisptr cdef C_Node.NestedNode.Reader thisptr
@@ -1963,6 +1966,9 @@ cdef class _Schema:
cpdef as_interface(self): cpdef as_interface(self):
return _InterfaceSchema()._init(self.thisptr.asInterface()) return _InterfaceSchema()._init(self.thisptr.asInterface())
cpdef as_enum(self):
return _EnumSchema()._init(self.thisptr.asEnum())
cpdef get_dependency(self, id): cpdef get_dependency(self, id):
return _Schema()._init(self.thisptr.getDependency(id)) return _Schema()._init(self.thisptr.getDependency(id))
@@ -2066,6 +2072,29 @@ cdef class _InterfaceSchema:
def __repr__(self): def __repr__(self):
return '<schema for %s>' % self.node.displayName return '<schema for %s>' % 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[<char *>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 class _ParsedSchema(_Schema):
cdef C_ParsedSchema thisptr_child cdef C_ParsedSchema thisptr_child
cdef _init_child(self, C_ParsedSchema other): cdef _init_child(self, C_ParsedSchema other):
@@ -2229,6 +2258,14 @@ class _InterfaceModule(object):
def _new_server(self, server): def _new_server(self, server):
return _DynamicCapabilityServer(self.schema, 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: cdef class SchemaParser:
"""A class for loading Cap'n Proto schema files. """A class for loading Cap'n Proto schema files.
@@ -2325,6 +2362,10 @@ cdef class SchemaParser:
elif proto.isInterface: elif proto.isInterface:
local_module = _InterfaceModule(schema.as_interface(), node.name) 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 module.__dict__[node.name] = local_module
_load(schema, local_module) _load(schema, local_module)