def printout(self, stream, context): obj = Container() if self.show_stream: obj.stream_position = stream.tell() follows = stream.read(self.stream_lookahead) if not follows: obj.following_stream_data = "EOF reached" else: stream.seek(-len(follows), 1) obj.following_stream_data = HexString(follows) print if self.show_context: obj.context = context if self.show_stack: obj.stack = ListContainer() frames = [s[0] for s in inspect.stack()][1:-1] frames.reverse() for f in frames: a = Container() a.__update__(f.f_locals) obj.stack.append(a) print "=" * 80 print "Probe", self.printname print obj print "=" * 80
def _parse(self, stream, context): if "<obj>" in context: obj = context["<obj>"] del context["<obj>"] else: obj = Container() if self.nested: context = Container(_=context) for sc in self.subcons: if sc.conflags & self.FLAG_EMBED: context["<obj>"] = obj sc._parse(stream, context) else: subobj = sc._parse(stream, context) if sc.name is not None: obj[sc.name] = subobj context[sc.name] = subobj return obj
def parse_stream(self, stream): """ Parse a stream. Files, pipes, sockets, and other streaming sources of data are handled by this method. """ return self._parse(stream, Container())
def _build(self, obj, stream, context): if "<unnested>" in context: del context["<unnested>"] elif self.nested: context = Container(_=context) for sc in self.subcons: if sc.conflags & self.FLAG_EMBED: context["<unnested>"] = True subobj = obj elif sc.name is None: subobj = None else: subobj = getattr(obj, sc.name) context[sc.name] = subobj sc._build(subobj, stream, context)
def sizeof(self, context=None): """ Calculate the size of this object, optionally using a context. Some constructs have no fixed size and can only know their size for a given hunk of data; these constructs will raise an error if they are not passed a context. :param ``Container`` context: contextual data :returns: int of the length of this construct :raises SizeofError: the size could not be determined """ if context is None: context = Container() try: return self._sizeof(context) except Exception, e: raise SizeofError(e)
def set_pairs(self, keywords): for operon in self: pairs = Container() operon.pairs = pairs for k in keywords: setattr(operon.pairs, k, {})
def _sizeof(self, context): if self.nested: context = Container(_=context) return sum(sc._sizeof(context) for sc in self.subcons)
def build_stream(self, obj, stream): """ Build an object directly into a stream. """ self._build(obj, stream, Container())