Add some useful _StructSchema, reader, and builder methods
This adds:
- _StructSchema.fieldnames
- _DynamicStructBuilder.as_reader
- _DynamicStruct{Reader,Builder}.schema and __dir__
This makes the reader and builder types much easier to navigate in ipython.
This commit is contained in:
@@ -236,6 +236,7 @@ cdef toPython(C_DynamicValue.Builder self, object parent):
|
|||||||
cdef class _DynamicStructReader:
|
cdef class _DynamicStructReader:
|
||||||
cdef C_DynamicStruct.Reader thisptr
|
cdef C_DynamicStruct.Reader thisptr
|
||||||
cdef public object _parent
|
cdef public object _parent
|
||||||
|
cdef object _obj_to_pin
|
||||||
cdef _init(self, C_DynamicStruct.Reader other, object parent):
|
cdef _init(self, C_DynamicStruct.Reader other, object parent):
|
||||||
self.thisptr = other
|
self.thisptr = other
|
||||||
self._parent = parent
|
self._parent = parent
|
||||||
@@ -250,6 +251,14 @@ cdef class _DynamicStructReader:
|
|||||||
cpdef which(self):
|
cpdef which(self):
|
||||||
return fixMaybe(self.thisptr.which()).getProto().getName().cStr()
|
return fixMaybe(self.thisptr.which()).getProto().getName().cStr()
|
||||||
|
|
||||||
|
property schema:
|
||||||
|
"""A _StructSchema object matching this reader"""
|
||||||
|
def __get__(self):
|
||||||
|
return _StructSchema()._init(self.thisptr.getSchema())
|
||||||
|
|
||||||
|
def __dir__(self):
|
||||||
|
return list(self.schema.fieldnames)
|
||||||
|
|
||||||
cdef class _DynamicStructBuilder:
|
cdef class _DynamicStructBuilder:
|
||||||
cdef C_DynamicStruct.Builder thisptr
|
cdef C_DynamicStruct.Builder thisptr
|
||||||
cdef public object _parent
|
cdef public object _parent
|
||||||
@@ -311,6 +320,21 @@ cdef class _DynamicStructBuilder:
|
|||||||
cpdef which(self):
|
cpdef which(self):
|
||||||
return fixMaybe(self.thisptr.which()).getProto().getName().cStr()
|
return fixMaybe(self.thisptr.which()).getProto().getName().cStr()
|
||||||
|
|
||||||
|
cpdef as_reader(self):
|
||||||
|
cdef _DynamicStructReader reader
|
||||||
|
reader = _DynamicStructReader()._init(self.thisptr.asReader(),
|
||||||
|
self._parent)
|
||||||
|
reader._obj_to_pin = self
|
||||||
|
return reader
|
||||||
|
|
||||||
|
property schema:
|
||||||
|
"""A _StructSchema object matching this reader"""
|
||||||
|
def __get__(self):
|
||||||
|
return _StructSchema()._init(self.thisptr.getSchema())
|
||||||
|
|
||||||
|
def __dir__(self):
|
||||||
|
return list(self.schema.fieldnames)
|
||||||
|
|
||||||
cdef class _DynamicOrphan:
|
cdef class _DynamicOrphan:
|
||||||
cdef C_DynamicOrphan thisptr
|
cdef C_DynamicOrphan thisptr
|
||||||
cdef public object _parent
|
cdef public object _parent
|
||||||
@@ -339,10 +363,23 @@ cdef class _Schema:
|
|||||||
|
|
||||||
cdef class _StructSchema:
|
cdef class _StructSchema:
|
||||||
cdef C_StructSchema thisptr
|
cdef C_StructSchema thisptr
|
||||||
|
cdef object __fieldnames
|
||||||
cdef _init(self, C_StructSchema other):
|
cdef _init(self, C_StructSchema other):
|
||||||
self.thisptr = other
|
self.thisptr = other
|
||||||
|
self.__fieldnames = None
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
property fieldnames:
|
||||||
|
"""A tuple of the field names in the struct."""
|
||||||
|
def __get__(self):
|
||||||
|
if self.__fieldnames is not None:
|
||||||
|
return self.__fieldnames
|
||||||
|
fieldlist = self.thisptr.getFields()
|
||||||
|
nfields = fieldlist.size()
|
||||||
|
self.__fieldnames = tuple(fieldlist[i].getProto().getName().cStr()
|
||||||
|
for i in xrange(nfields))
|
||||||
|
return self.__fieldnames
|
||||||
|
|
||||||
cdef class _ParsedSchema:
|
cdef class _ParsedSchema:
|
||||||
cdef C_ParsedSchema thisptr
|
cdef C_ParsedSchema thisptr
|
||||||
cdef _init(self, C_ParsedSchema other):
|
cdef _init(self, C_ParsedSchema other):
|
||||||
|
|||||||
@@ -33,19 +33,15 @@ cdef extern from "capnp/schema.h" namespace " ::capnp":
|
|||||||
Schema getDependency(uint64_t id) except +
|
Schema getDependency(uint64_t id) except +
|
||||||
#InterfaceSchema asInterface() const;
|
#InterfaceSchema asInterface() const;
|
||||||
|
|
||||||
cdef cppclass MemberForward" ::capnp::StructSchema::Field":
|
|
||||||
pass
|
|
||||||
|
|
||||||
cdef cppclass StructSchema(Schema):
|
cdef cppclass StructSchema(Schema):
|
||||||
cppclass FieldList:
|
|
||||||
uint size()
|
|
||||||
MemberForward operator[](uint index)
|
|
||||||
|
|
||||||
cppclass Field:
|
cppclass Field:
|
||||||
StructNode.Member.Reader getProto()
|
StructNode.Member.Reader getProto()
|
||||||
StructSchema getContainingStruct()
|
StructSchema getContainingStruct()
|
||||||
uint getIndex()
|
uint getIndex()
|
||||||
FieldList getFields()
|
|
||||||
|
cppclass FieldList:
|
||||||
|
uint size()
|
||||||
|
Field operator[](uint index)
|
||||||
|
|
||||||
Node.Reader getProto()
|
Node.Reader getProto()
|
||||||
FieldList getFields()
|
FieldList getFields()
|
||||||
@@ -104,6 +100,7 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
|||||||
DynamicValueForward.Builder init(char *)
|
DynamicValueForward.Builder init(char *)
|
||||||
StructSchema getSchema()
|
StructSchema getSchema()
|
||||||
Maybe[StructSchema.Field] which()
|
Maybe[StructSchema.Field] which()
|
||||||
|
DynamicStruct.Reader asReader()
|
||||||
|
|
||||||
cdef extern from "fixMaybe.h":
|
cdef extern from "fixMaybe.h":
|
||||||
StructSchema.Field fixMaybe(Maybe[StructSchema.Field]) except+
|
StructSchema.Field fixMaybe(Maybe[StructSchema.Field]) except+
|
||||||
|
|||||||
Reference in New Issue
Block a user