def __init__( self, parent, left=0, top=0, bottom=0, width=72, padding_top=0, padding_bottom=0, replacements=None, buffered=False, vars=None, charfilter=None, charmap=None, xform=None, ): self.parent = parent self.left = left self.top = top self.bottom = bottom self.width = width self.padding_top = padding_top self.padding_bottom = padding_bottom self.replacements = replacements # Supplying replacements implies buffering the output self.buffered = buffered or bool(replacements) self.vars = vars self.charmap = charmap self.xform = xform if charfilter is None: if self.parent: charfilter = self.parent.charfilter else: charfilter = True self.charfilter = charfilter if self.buffered: self.buffer = StringIO() else: self.buffer = None
def dumps_tree(block): from bookish.compat import StringIO sio = StringIO() dump_tree(block, sio) return sio.getvalue()
class TextFrame(object): display_vars = "left top bottom width replacements buffered vars".split() def __init__( self, parent, left=0, top=0, bottom=0, width=72, padding_top=0, padding_bottom=0, replacements=None, buffered=False, vars=None, charfilter=None, charmap=None, xform=None, ): self.parent = parent self.left = left self.top = top self.bottom = bottom self.width = width self.padding_top = padding_top self.padding_bottom = padding_bottom self.replacements = replacements # Supplying replacements implies buffering the output self.buffered = buffered or bool(replacements) self.vars = vars self.charmap = charmap self.xform = xform if charfilter is None: if self.parent: charfilter = self.parent.charfilter else: charfilter = True self.charfilter = charfilter if self.buffered: self.buffer = StringIO() else: self.buffer = None def __repr__(self): t = "%s(" % type(self).__name__ for i, n in enumerate(self.display_vars): if i > 0: t += " " t += "%s=%r" % (n, self.__dict__[n]) return t def emit(self, string, indent=0, wrap=True, first="", rest="", upper=False, lower=False, normalize=True): left = self.left + indent string = format_block( string, left, width=self.width, wrap=wrap, first=first, rest=rest, upper=upper, lower=lower, xform=self.xform, charfilter=self.charfilter, charmap=self.charmap, normalize=normalize, ) assert isinstance(string, text_type) self.write(string) def get(self, name): if self.vars and name in self.vars: return self.vars.get(name) elif self.parent: return self.parent.get(name) else: return None def inc(self, name): if self.vars and name in self.vars: v = self.vars[name] self.vars[name] += 1 return v elif self.parent: return self.parent.inc(name) else: raise KeyError def write(self, string): # String must be Unicode assert isinstance(string, text_type), repr(string) if self.buffered: self.buffer.write(string) else: self.parent.write(string) def getvalue(self): assert self.buffered output = self.buffer.getvalue() if self.replacements: for target, replacement in self.replacements: output = output.replace(target, replacement) return output def finish(self): if self.buffered: output = self.getvalue() assert isinstance(output, text_type) self.parent.write(output)