X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsp%2Futil.py;h=ad99e8b0e27a110d2d499ce5b365afb0fb216f44;hb=cc1126892c7148faadc7b6781acce6a4a99ede5c;hp=adad53766348e723ae2e57536ed54ea367c31f50;hpb=ad5b93034c1c28f0f4deab4c1b62cb2d6a869f81;p=wrw.git diff --git a/wrw/sp/util.py b/wrw/sp/util.py index adad537..ad99e8b 100644 --- a/wrw/sp/util.py +++ b/wrw/sp/util.py @@ -1,3 +1,5 @@ +import io +from .. import dispatch from . import cons def findnsnames(el): @@ -98,7 +100,7 @@ class formatter(object): def endtag(self, el): self.write('') - def longtag(self, el): + def longtag(self, el, **extra): self.starttag(el, **extra) for ch in el.children: self.node(ch) @@ -140,6 +142,16 @@ class formatter(object): def output(cls, out, el, *args, **kw): cls(out=out, root=el, *args, **kw).start() + @classmethod + def fragment(cls, out, el, *args, **kw): + cls(out=out, root=el, *args, **kw).node(el) + + @classmethod + def format(cls, el, *args, **kw): + buf = io.BytesIO() + cls.output(buf, el, *args, **kw) + return buf.getvalue() + def update(self, **ch): ret = type(self).__new__(type(self)) ret.__dict__.update(self.__dict__) @@ -208,3 +220,20 @@ class indenter(formatter): def start(self): super(indenter, self).start() self.write('\n') + +class response(dispatch.restart): + charset = "utf-8" + doctype = None + formatter = indenter + + def __init__(self, root): + super().__init__() + self.root = root + + @property + def ctype(self): + raise Exception("a subclass of wrw.sp.util.response must override ctype") + + def handle(self, req): + req.ohead["Content-Type"] = self.ctype + return [self.formatter.format(self.root, doctype=self.doctype, charset=self.charset)]