def _build(self, obj, stream, context): size = self._sizeof(context) stream2 = StringIO() self.subcon._build(obj, stream2, context) data = self.encoder(stream2.getvalue()) assert len(data) == size _write_stream(stream, self._sizeof(context), data)
def build(self, obj): """ Build an object in memory. """ stream = StringIO() self.build_stream(obj, stream) return stream.getvalue()
def parse(self, data): """ Parse an in-memory buffer. Strings, buffers, memoryviews, and other complete buffers can be parsed with this method. """ return self.parse_stream(StringIO(data))
def _build(self, obj, stream, context): if self.include_name: name, obj = obj for sc in self.subcons: if sc.name == name: sc._build(obj, stream, context) return else: for sc in self.subcons: stream2 = StringIO() context2 = context.__copy__() try: sc._build(obj, stream2, context2) except Exception: pass else: context.__update__(context2) stream.write(stream2.getvalue()) return raise SelectError("no subconstruct matched", obj)
def _encode(self, obj, context): stream = StringIO() self.inner_subcon._build(obj, stream, context) return stream.getvalue()
def _decode(self, obj, context): return self.inner_subcon._parse(StringIO(obj), context)
def build(self, obj): """builds an object in a string (in memory)""" stream = StringIO() self.build_stream(obj, stream) return stream.getvalue()
def parse(self, data): """parses data given as a buffer or a string (in-memory)""" return self.parse_stream(StringIO(data))
def _parse(self, stream, context): data = _read_stream(stream, self._sizeof(context)) stream2 = StringIO(self.decoder(data)) return self.subcon._parse(stream2, context)