def decompress(self, body): buf = IO() for chunk in body: buf.write(chunk) buf.seek(0) gzfile = GzipFile(mode='rb', fileobj=buf) data = gzfile.read() gzfile.close() del buf del gzfile return data
def render(self, encoding='ascii'): indentation = 0 text = False stack = [] buf = IO() for k, t in self: if k == 'enter': indent = getattr(t, 'indent', True) stack.append(t) if t.strip: continue if text and indent: buf.write('\n') if indent: buf.write(' ' * indentation) for element in t.enter(): buf.write(element.encode(encoding)) if indent: buf.write('\n') indentation += 1 text = False continue if k == 'exit': indent = getattr(t, 'indent', True) stack.pop() if t.strip: continue if indent: indentation -= 1 if not t.simple: if text and indent: buf.write('\n') if indent: buf.write(' ' * indentation) for element in t.exit(): buf.write(element.encode(encoding)) if not t.simple or t.children: buf.write('\n') text = False continue if k == 'text': indent = getattr(stack[-1], 'indent', True) if not text and indent: buf.write(' ' * indentation) t = t.encode(encoding) buf.write(t.replace('\n', '\n' + ' ' * indentation) if indent else t) text = True if k == 'flush': yield buf.getvalue() del buf buf = IO() yield buf.getvalue()