Refactor clean various cruft

This commit is contained in:
Jason Paryani
2013-08-26 14:57:48 -07:00
parent 38ba5249c3
commit 48f8d995a6

View File

@@ -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,44 +177,33 @@ 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 int type = self.getType()
cdef public object _parent if type == capnp.TYPE_BOOL:
cdef _init(self, C_DynamicValue.Reader other, object parent): return self.asBool()
self.thisptr = other elif type == capnp.TYPE_INT:
self._parent = parent return self.asInt()
return self elif type == capnp.TYPE_UINT:
return self.asUint()
cpdef int getType(self): elif type == capnp.TYPE_FLOAT:
return self.thisptr.getType() return self.asDouble()
elif type == capnp.TYPE_TEXT:
cpdef toPython(self): return self.asText()[:]
cdef int type = self.getType() elif type == capnp.TYPE_DATA:
if type == capnp.TYPE_BOOL: temp = self.asData()
return self.thisptr.asBool() return (<char*>temp.begin())[:temp.size()]
elif type == capnp.TYPE_INT: elif type == capnp.TYPE_LIST:
return self.thisptr.asInt() return list(_DynamicListReader()._init(self.asList(), parent))
elif type == capnp.TYPE_UINT: elif type == capnp.TYPE_STRUCT:
return self.thisptr.asUint() return _DynamicStructReader()._init(self.asStruct(), parent)
elif type == capnp.TYPE_FLOAT: elif type == capnp.TYPE_ENUM:
return self.thisptr.asDouble() return fixMaybe(self.asEnum().getEnumerant()).getProto().getName().cStr()
elif type == capnp.TYPE_TEXT: elif type == capnp.TYPE_VOID:
return self.thisptr.asText()[:] return None
elif type == capnp.TYPE_DATA: elif type == capnp.TYPE_UNKOWN:
temp = self.thisptr.asData() raise ValueError("Cannot convert type to Python. Type is unknown by capnproto library")
return (<char*>temp.begin())[:temp.size()] else:
elif type == capnp.TYPE_LIST: raise ValueError("Cannot convert type to Python. Type is unhandled by capnproto library")
return list(_DynamicListReader()._init(self.thisptr.asList(), self._parent))
elif type == capnp.TYPE_STRUCT:
return _DynamicStructReader()._init(self.thisptr.asStruct(), self._parent)
elif type == capnp.TYPE_ENUM:
return fixMaybe(self.thisptr.asEnum().getEnumerant()).getProto().getName().cStr()
elif type == capnp.TYPE_VOID:
return None
elif type == capnp.TYPE_UNKOWN:
raise ValueError("Cannot convert type to Python. Type is unknown by capnproto library")
else:
raise ValueError("Cannot convert type to Python. Type is unhandled by capnproto library")
cdef toPython(C_DynamicValue.Builder self, object parent): cdef toPython(C_DynamicValue.Builder self, object parent):
cdef int type = self.getType() cdef int type = self.getType()
@@ -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')