Refactor clean various cruft
This commit is contained in:
@@ -119,15 +119,12 @@ cdef class _DynamicListReader:
|
|||||||
self._parent = parent
|
self._parent = parent
|
||||||
return self
|
return self
|
||||||
|
|
||||||
cpdef _get(self, index):
|
def __getitem__(self, index):
|
||||||
size = self.thisptr.size()
|
size = self.thisptr.size()
|
||||||
if index >= size:
|
if index >= size:
|
||||||
raise IndexError('Out of bounds')
|
raise IndexError('Out of bounds')
|
||||||
index = index % size
|
index = index % size
|
||||||
return _DynamicValueReader()._init(self.thisptr[index], self._parent)
|
return toPythonReader(self.thisptr[index], self._parent)
|
||||||
|
|
||||||
def __getitem__(self, index):
|
|
||||||
return self._get(index).toPython()
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.thisptr.size()
|
return self.thisptr.size()
|
||||||
@@ -140,10 +137,6 @@ cdef class _DynamicListBuilder:
|
|||||||
self._parent = parent
|
self._parent = parent
|
||||||
return self
|
return self
|
||||||
|
|
||||||
#def _init(self, size):
|
|
||||||
# self.thisptr._init(size)
|
|
||||||
# return self
|
|
||||||
|
|
||||||
cpdef _get(self, index) except +ValueError:
|
cpdef _get(self, index) except +ValueError:
|
||||||
return toPython(self.thisptr[index], self._parent)
|
return toPython(self.thisptr[index], self._parent)
|
||||||
|
|
||||||
@@ -168,37 +161,6 @@ cdef class _DynamicListBuilder:
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.thisptr.size()
|
return self.thisptr.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 _List_NestedNode_Reader:
|
cdef class _List_NestedNode_Reader:
|
||||||
cdef List[C_Node.NestedNode].Reader thisptr
|
cdef List[C_Node.NestedNode].Reader thisptr
|
||||||
cdef _init(self, List[C_Node.NestedNode].Reader other):
|
cdef _init(self, List[C_Node.NestedNode].Reader other):
|
||||||
@@ -215,38 +177,27 @@ cdef class _List_NestedNode_Reader:
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.thisptr.size()
|
return self.thisptr.size()
|
||||||
|
|
||||||
cdef class _DynamicValueReader:
|
cdef toPythonReader(C_DynamicValue.Reader self, object parent):
|
||||||
cdef C_DynamicValue.Reader thisptr
|
|
||||||
cdef public object _parent
|
|
||||||
cdef _init(self, C_DynamicValue.Reader other, object parent):
|
|
||||||
self.thisptr = other
|
|
||||||
self._parent = parent
|
|
||||||
return self
|
|
||||||
|
|
||||||
cpdef int getType(self):
|
|
||||||
return self.thisptr.getType()
|
|
||||||
|
|
||||||
cpdef toPython(self):
|
|
||||||
cdef int type = self.getType()
|
cdef int type = self.getType()
|
||||||
if type == capnp.TYPE_BOOL:
|
if type == capnp.TYPE_BOOL:
|
||||||
return self.thisptr.asBool()
|
return self.asBool()
|
||||||
elif type == capnp.TYPE_INT:
|
elif type == capnp.TYPE_INT:
|
||||||
return self.thisptr.asInt()
|
return self.asInt()
|
||||||
elif type == capnp.TYPE_UINT:
|
elif type == capnp.TYPE_UINT:
|
||||||
return self.thisptr.asUint()
|
return self.asUint()
|
||||||
elif type == capnp.TYPE_FLOAT:
|
elif type == capnp.TYPE_FLOAT:
|
||||||
return self.thisptr.asDouble()
|
return self.asDouble()
|
||||||
elif type == capnp.TYPE_TEXT:
|
elif type == capnp.TYPE_TEXT:
|
||||||
return self.thisptr.asText()[:]
|
return self.asText()[:]
|
||||||
elif type == capnp.TYPE_DATA:
|
elif type == capnp.TYPE_DATA:
|
||||||
temp = self.thisptr.asData()
|
temp = self.asData()
|
||||||
return (<char*>temp.begin())[:temp.size()]
|
return (<char*>temp.begin())[:temp.size()]
|
||||||
elif type == capnp.TYPE_LIST:
|
elif type == capnp.TYPE_LIST:
|
||||||
return list(_DynamicListReader()._init(self.thisptr.asList(), self._parent))
|
return list(_DynamicListReader()._init(self.asList(), parent))
|
||||||
elif type == capnp.TYPE_STRUCT:
|
elif type == capnp.TYPE_STRUCT:
|
||||||
return _DynamicStructReader()._init(self.thisptr.asStruct(), self._parent)
|
return _DynamicStructReader()._init(self.asStruct(), parent)
|
||||||
elif type == capnp.TYPE_ENUM:
|
elif type == capnp.TYPE_ENUM:
|
||||||
return fixMaybe(self.thisptr.asEnum().getEnumerant()).getProto().getName().cStr()
|
return fixMaybe(self.asEnum().getEnumerant()).getProto().getName().cStr()
|
||||||
elif type == capnp.TYPE_VOID:
|
elif type == capnp.TYPE_VOID:
|
||||||
return None
|
return None
|
||||||
elif type == capnp.TYPE_UNKOWN:
|
elif type == capnp.TYPE_UNKOWN:
|
||||||
@@ -290,11 +241,8 @@ cdef class _DynamicStructReader:
|
|||||||
self._parent = parent
|
self._parent = parent
|
||||||
return self
|
return self
|
||||||
|
|
||||||
cpdef _get(self, field):
|
|
||||||
return _DynamicValueReader()._init(self.thisptr.get(field), self._parent)
|
|
||||||
|
|
||||||
def __getattr__(self, field):
|
def __getattr__(self, field):
|
||||||
return self._get(field).toPython()
|
return toPythonReader(self.thisptr.get(field), self._parent)
|
||||||
|
|
||||||
def _has(self, field):
|
def _has(self, field):
|
||||||
return self.thisptr.has(field)
|
return self.thisptr.has(field)
|
||||||
@@ -360,13 +308,8 @@ cdef class _DynamicStructBuilder:
|
|||||||
else:
|
else:
|
||||||
return toPython(self.thisptr.init(field, size), self._parent)
|
return toPython(self.thisptr.init(field, size), self._parent)
|
||||||
|
|
||||||
# cpdef which(self):
|
cpdef which(self):
|
||||||
# try:
|
return fixMaybe(self.thisptr.which()).getProto().getName().cStr()
|
||||||
# union = fixMaybeUnion(self.thisptr.getSchema().getUnnamedUnion())
|
|
||||||
# except:
|
|
||||||
# raise TypeError("This struct has no unnamed enums. You cannot call which on it")
|
|
||||||
|
|
||||||
# return getWhichBuilder(self.thisptr.getByUnion(union))
|
|
||||||
|
|
||||||
cdef class _DynamicOrphan:
|
cdef class _DynamicOrphan:
|
||||||
cdef C_DynamicOrphan thisptr
|
cdef C_DynamicOrphan thisptr
|
||||||
@@ -383,7 +326,7 @@ cdef class Schema:
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
cpdef asConstValue(self):
|
cpdef asConstValue(self):
|
||||||
return _DynamicValueReader()._init(<C_DynamicValue.Reader>self.thisptr.asConst(), self).toPython()
|
return toPythonReader(<C_DynamicValue.Reader>self.thisptr.asConst(), self)
|
||||||
|
|
||||||
cpdef asStruct(self):
|
cpdef asStruct(self):
|
||||||
return StructSchema()._init(self.thisptr.asStruct())
|
return StructSchema()._init(self.thisptr.asStruct())
|
||||||
@@ -407,7 +350,7 @@ cdef class ParsedSchema:
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
cpdef asConstValue(self):
|
cpdef asConstValue(self):
|
||||||
return _DynamicValueReader()._init(<C_DynamicValue.Reader>self.thisptr.asConst(), self).toPython()
|
return toPythonReader(<C_DynamicValue.Reader>self.thisptr.asConst(), self)
|
||||||
|
|
||||||
cpdef asStruct(self):
|
cpdef asStruct(self):
|
||||||
return StructSchema()._init(self.thisptr.asStruct())
|
return StructSchema()._init(self.thisptr.asStruct())
|
||||||
@@ -472,6 +415,7 @@ cdef class MessageBuilder:
|
|||||||
else:
|
else:
|
||||||
s = schema
|
s = schema
|
||||||
return _DynamicStructBuilder()._init(self.thisptr.initRootDynamicStruct(s.thisptr), self)
|
return _DynamicStructBuilder()._init(self.thisptr.initRootDynamicStruct(s.thisptr), self)
|
||||||
|
|
||||||
cpdef getRoot(self, schema):
|
cpdef getRoot(self, schema):
|
||||||
cdef StructSchema s
|
cdef StructSchema s
|
||||||
if hasattr(schema, 'Schema'):
|
if hasattr(schema, 'Schema'):
|
||||||
@@ -495,6 +439,7 @@ cdef class MessageReader:
|
|||||||
|
|
||||||
cpdef _getRootNode(self):
|
cpdef _getRootNode(self):
|
||||||
return _NodeReader().init(self.thisptr.getRootNode())
|
return _NodeReader().init(self.thisptr.getRootNode())
|
||||||
|
|
||||||
cpdef getRoot(self, schema):
|
cpdef getRoot(self, schema):
|
||||||
cdef StructSchema s
|
cdef StructSchema s
|
||||||
if hasattr(schema, 'Schema'):
|
if hasattr(schema, 'Schema'):
|
||||||
@@ -525,7 +470,7 @@ def load(file_name, display_name=None, imports=[]):
|
|||||||
|
|
||||||
You will have to load a schema before you can begin doing anything
|
You will have to load a schema before you can begin doing anything
|
||||||
meaningful with this library. Loading a schema is much like Loading
|
meaningful with this library. Loading a schema is much like Loading
|
||||||
a Python module (and load even returns a ModuleType). Once it's been
|
a Python module (and load even returns a `ModuleType`). Once it's been
|
||||||
loaded, you use it much like any other Module::
|
loaded, you use it much like any other Module::
|
||||||
|
|
||||||
addressbook = capnp.load('addressbook.capnp')
|
addressbook = capnp.load('addressbook.capnp')
|
||||||
|
|||||||
Reference in New Issue
Block a user