示例#1
0
文件: core.py 项目: crass/construct
 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)
示例#2
0
文件: core.py 项目: crass/construct
    def build(self, obj):
        """
        Build an object in memory.
        """

        stream = StringIO()
        self.build_stream(obj, stream)
        return stream.getvalue()
示例#3
0
    def build(self, obj):
        """
        Build an object in memory.
        """

        stream = StringIO()
        self.build_stream(obj, stream)
        return stream.getvalue()
示例#4
0
    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))
示例#5
0
文件: core.py 项目: crass/construct
 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)
示例#6
0
 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)
示例#7
0
 def _encode(self, obj, context):
     stream = StringIO()
     self.inner_subcon._build(obj, stream, context)
     return stream.getvalue()
示例#8
0
 def _decode(self, obj, context):
     return self.inner_subcon._parse(StringIO(obj), context)
示例#9
0
 def _encode(self, obj, context):
     stream = StringIO()
     self.inner_subcon._build(obj, stream, context)
     return stream.getvalue()
示例#10
0
 def build(self, obj):
     """builds an object in a string (in memory)"""
     stream = StringIO()
     self.build_stream(obj, stream)
     return stream.getvalue()
示例#11
0
 def build(self, obj):
     """builds an object in a string (in memory)"""
     stream = StringIO()
     self.build_stream(obj, stream)
     return stream.getvalue()
示例#12
0
 def parse(self, data):
     """parses data given as a buffer or a string (in-memory)"""
     return self.parse_stream(StringIO(data))
示例#13
0
 def _parse(self, stream, context):
     data = _read_stream(stream, self._sizeof(context))
     stream2 = StringIO(self.decoder(data))
     return self.subcon._parse(stream2, context)