mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 16:35:04 +01:00
Change library to use dynamic API of capnproto.
Currently only get is working on DynamicStructReader, but the mechanics are mostly there.
This commit is contained in:
parent
481841730c
commit
40a16470cf
9 changed files with 217 additions and 56612 deletions
|
@ -41,7 +41,7 @@ cdef extern from "capnp/message.h" namespace "::capnp":
|
|||
T operator[](uint)
|
||||
uint size()
|
||||
|
||||
cdef extern from "schema.capnp.h" namespace "::capnp::schema":
|
||||
cdef extern from "capnp/schema.capnp.h" namespace "::capnp::schema":
|
||||
{%- for node_dict in enum_types %}
|
||||
enum {{node_name|capitalize}}:
|
||||
{%- for member_name, member_dict in node_dict['members'].items() %}
|
||||
|
|
|
@ -251,7 +251,7 @@ env.filters['capitalize'] = capitalize
|
|||
env.filters['upper_and_under'] = upper_and_under
|
||||
nodes = fixNodes(nodes)
|
||||
tmpl = env.get_template('capnp.tmpl.pxd')
|
||||
open(os.path.join(curr_dir, 'capnp_schema.pxd'), 'w').write(tmpl.render(nodes=nodes, namespace='::capnp::schema', built_in_types=built_in_types, enum_types=enum_types, union_types=union_types))
|
||||
open(os.path.join(curr_dir, 'schema_cpp.pxd'), 'w').write(tmpl.render(nodes=nodes, namespace='::capnp::schema', built_in_types=built_in_types, enum_types=enum_types, union_types=union_types))
|
||||
tmpl = env.get_template('capnp.tmpl.pyx')
|
||||
open(os.path.join(curr_dir, 'schema.capnp.cpp.pyx'), 'w').write(tmpl.render(nodes=nodes, namespace='::capnp::schema', primitive_types=primitive_types, built_in_types=built_in_types, list_types=list_types, enum_types=enum_types, union_types=union_types))
|
||||
|
||||
|
|
171
capnp.pyx
Normal file
171
capnp.pyx
Normal file
|
@ -0,0 +1,171 @@
|
|||
# capnp.pyx
|
||||
# distutils: language = c++
|
||||
# distutils: extra_compile_args = --std=c++11
|
||||
# distutils: libraries = capnp
|
||||
|
||||
cimport capnp_cpp as capnp
|
||||
cimport schema_cpp
|
||||
from capnp_cpp cimport SchemaLoader as C_SchemaLoader, Schema as C_Schema, StructSchema as C_StructSchema, DynamicStruct as C_DynamicStruct, DynamicValue as C_DynamicValue
|
||||
from schema_cpp cimport CodeGeneratorRequest as C_CodeGeneratorRequest, Node as C_Node
|
||||
from cython.operator cimport dereference as deref
|
||||
|
||||
from libc.stdint cimport *
|
||||
ctypedef unsigned int uint
|
||||
ctypedef uint8_t UInt8
|
||||
ctypedef uint16_t UInt16
|
||||
ctypedef uint32_t UInt32
|
||||
ctypedef uint64_t UInt64
|
||||
ctypedef int8_t Int8
|
||||
ctypedef int16_t Int16
|
||||
ctypedef int32_t Int32
|
||||
ctypedef int64_t Int64
|
||||
|
||||
ctypedef char * Object
|
||||
ctypedef bint Bool
|
||||
ctypedef float Float32
|
||||
ctypedef double Float64
|
||||
|
||||
cdef extern from "capnp/list.h" namespace "::capnp":
|
||||
cdef cppclass List[T]:
|
||||
cppclass Reader:
|
||||
T operator[](uint) except +ValueError
|
||||
uint size()
|
||||
cppclass Builder:
|
||||
T operator[](uint) except +ValueError
|
||||
uint size()
|
||||
|
||||
cdef class _List_UInt64_Reader:
|
||||
cdef List[UInt64].Reader thisptr
|
||||
cdef init(self, List[UInt64].Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
def __getitem__(self, index):
|
||||
size = self.thisptr.size()
|
||||
if index >= size:
|
||||
raise IndexError('Out of bounds')
|
||||
index = index % size
|
||||
return self.thisptr[index]
|
||||
|
||||
def __len__(self):
|
||||
return self.thisptr.size()
|
||||
|
||||
cdef class _List_Node_Reader:
|
||||
cdef List[C_Node].Reader thisptr
|
||||
cdef init(self, List[C_Node].Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
def __getitem__(self, index):
|
||||
size = self.thisptr.size()
|
||||
if index >= size:
|
||||
raise IndexError('Out of bounds')
|
||||
index = index % size
|
||||
return _NodeReader().init(<C_Node.Reader>self.thisptr[index])
|
||||
|
||||
def __len__(self):
|
||||
return self.thisptr.size()
|
||||
|
||||
cdef class _DynamicValueReader:
|
||||
cdef C_DynamicValue.Reader thisptr
|
||||
cdef init(self, C_DynamicValue.Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
|
||||
|
||||
cdef class _DynamicStructReader:
|
||||
cdef C_DynamicStruct.Reader thisptr
|
||||
cdef init(self, C_DynamicStruct.Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
|
||||
def get(self, field):
|
||||
return _DynamicValueReader().init(self.thisptr.get(field))
|
||||
|
||||
def has(self, field):
|
||||
return self.thisptr.has(field)
|
||||
|
||||
cdef class _CodeGeneratorRequestReader:
|
||||
cdef C_CodeGeneratorRequest.Reader thisptr
|
||||
cdef init(self, C_CodeGeneratorRequest.Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
|
||||
property nodes:
|
||||
def __get__(self):
|
||||
return _List_Node_Reader().init(self.thisptr.getNodes())
|
||||
property requestedFiles:
|
||||
def __get__(self):
|
||||
return _List_UInt64_Reader().init(self.thisptr.getRequestedFiles())
|
||||
|
||||
cdef class _NodeReader:
|
||||
cdef C_Node.Reader thisptr
|
||||
cdef init(self, C_Node.Reader other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
property displayName:
|
||||
def __get__(self):
|
||||
return self.thisptr.getDisplayName().cStr()
|
||||
property scopeId:
|
||||
def __get__(self):
|
||||
return self.thisptr.getScopeId()
|
||||
property id:
|
||||
def __get__(self):
|
||||
return self.thisptr.getId()
|
||||
|
||||
cdef class Schema:
|
||||
cdef C_Schema thisptr
|
||||
cdef init(self, C_Schema other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
|
||||
cpdef asStruct(self):
|
||||
return StructSchema().init(self.thisptr.asStruct())
|
||||
|
||||
cdef class StructSchema:
|
||||
cdef C_StructSchema thisptr
|
||||
cdef init(self, C_StructSchema other):
|
||||
self.thisptr = other
|
||||
return self
|
||||
|
||||
cdef class SchemaLoader:
|
||||
cdef C_SchemaLoader * thisptr
|
||||
def __cinit__(self):
|
||||
self.thisptr = new C_SchemaLoader()
|
||||
|
||||
def __dealloc__(self):
|
||||
del self.thisptr
|
||||
|
||||
cpdef load(self, _NodeReader node):
|
||||
return Schema().init(self.thisptr.load(node.thisptr))
|
||||
|
||||
cdef class MessageBuilder:
|
||||
cdef schema_cpp.MessageBuilder * thisptr
|
||||
def __dealloc__(self):
|
||||
del self.thisptr
|
||||
|
||||
cdef class MallocMessageBuilder(MessageBuilder):
|
||||
def __cinit__(self):
|
||||
self.thisptr = new schema_cpp.MallocMessageBuilder()
|
||||
|
||||
cdef class MessageReader:
|
||||
cdef schema_cpp.MessageReader * thisptr
|
||||
def __dealloc__(self):
|
||||
del self.thisptr
|
||||
cpdef getRootNode(self):
|
||||
return _NodeReader().init(self.thisptr.getRootNode())
|
||||
cpdef getRootCodeGeneratorRequest(self):
|
||||
return _CodeGeneratorRequestReader().init(self.thisptr.getRootCodeGeneratorRequest())
|
||||
cpdef getRootDynamicStruct(self, StructSchema schema):
|
||||
return _DynamicStructReader().init(self.thisptr.getRootDynamicStruct(schema.thisptr))
|
||||
|
||||
cdef class StreamFdMessageReader(MessageReader):
|
||||
def __cinit__(self, int fd):
|
||||
self.thisptr = new schema_cpp.StreamFdMessageReader(fd)
|
||||
|
||||
cdef class PackedFdMessageReader(MessageReader):
|
||||
def __cinit__(self, int fd):
|
||||
self.thisptr = new schema_cpp.PackedFdMessageReader(fd)
|
||||
|
||||
def writeMessageToFd(int fd, MessageBuilder m):
|
||||
schema_cpp.writeMessageToFd(fd, deref(m.thisptr))
|
||||
def writePackedMessageToFd(int fd, MessageBuilder m):
|
||||
schema_cpp.writePackedMessageToFd(fd, deref(m.thisptr))
|
26
capnp_cpp.pxd
Normal file
26
capnp_cpp.pxd
Normal file
|
@ -0,0 +1,26 @@
|
|||
# schema.capnp.cpp.pyx
|
||||
# distutils: language = c++
|
||||
# distutils: extra_compile_args = --std=c++11
|
||||
# distutils: libraries = capnp
|
||||
from schema_cpp cimport Node
|
||||
|
||||
cdef extern from "capnp/dynamic.h" namespace "::capnp":
|
||||
cdef cppclass DynamicValue:
|
||||
cppclass Reader:
|
||||
pass
|
||||
cdef cppclass DynamicStruct:
|
||||
cppclass Reader:
|
||||
DynamicValue.Reader get(char *) except +ValueError
|
||||
bint has(char *)
|
||||
|
||||
cdef extern from "capnp/schema.h" namespace "::capnp":
|
||||
cdef cppclass Schema:
|
||||
Node.Reader getProto()
|
||||
StructSchema asStruct()
|
||||
cdef cppclass StructSchema(Schema):
|
||||
pass
|
||||
|
||||
cdef extern from "capnp/schema-loader.h" namespace "::capnp":
|
||||
cdef cppclass SchemaLoader:
|
||||
SchemaLoader()
|
||||
Schema load(Node.Reader &) except +
|
2510
schema.capnp.c++
2510
schema.capnp.c++
File diff suppressed because it is too large
Load diff
5301
schema.capnp.h
5301
schema.capnp.h
File diff suppressed because it is too large
Load diff
48796
schema.cpp
48796
schema.cpp
File diff suppressed because it is too large
Load diff
|
@ -18,6 +18,20 @@ ctypedef char * Object
|
|||
ctypedef bint Bool
|
||||
ctypedef float Float32
|
||||
ctypedef double Float64
|
||||
|
||||
cdef extern from "capnp/dynamic.h" namespace "::capnp":
|
||||
cdef cppclass DynamicValue:
|
||||
cppclass Reader:
|
||||
pass
|
||||
cdef cppclass DynamicStruct:
|
||||
cppclass Reader:
|
||||
pass
|
||||
|
||||
cdef extern from "capnp/schema.h" namespace "::capnp":
|
||||
cdef cppclass Schema:
|
||||
pass
|
||||
cdef cppclass StructSchema(Schema):
|
||||
pass
|
||||
|
||||
cdef extern from "capnp/blob.h" namespace "::capnp":
|
||||
cdef cppclass Data:
|
||||
|
@ -41,7 +55,7 @@ cdef extern from "capnp/message.h" namespace "::capnp":
|
|||
T operator[](uint)
|
||||
uint size()
|
||||
|
||||
cdef extern from "schema.capnp.h" namespace "::capnp::schema":
|
||||
cdef extern from "capnp/schema.capnp.h" namespace "::capnp::schema":
|
||||
enum :
|
||||
_ElementSize_inlineComposite "::capnp::schema::ElementSize::INLINE_COMPOSITE"
|
||||
_ElementSize_eightBytes "::capnp::schema::ElementSize::EIGHT_BYTES"
|
||||
|
@ -643,6 +657,8 @@ cdef extern from "capnp/message.h" namespace "::capnp":
|
|||
EnumNode.Reader getRootEnumNode'getRoot<::capnp::schema::EnumNode>'()
|
||||
StructNode.Reader getRootStructNode'getRoot<::capnp::schema::StructNode>'()
|
||||
Annotation.Reader getRootAnnotation'getRoot<::capnp::schema::Annotation>'()
|
||||
|
||||
DynamicStruct.Reader getRootDynamicStruct'getRoot<::capnp::DynamicStruct>'(StructSchema)
|
||||
|
||||
cdef cppclass MallocMessageBuilder(MessageBuilder):
|
||||
MallocMessageBuilder()
|
||||
|
|
3
setup.py
3
setup.py
|
@ -1,8 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
from distutils.core import setup
|
||||
from Cython.Build import cythonize
|
||||
|
||||
setup(
|
||||
name = "capnp",
|
||||
ext_modules = cythonize('schema.pyx'),
|
||||
ext_modules = cythonize('capnp.pyx'),
|
||||
)
|
Loading…
Add table
Reference in a new issue