X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsp%2Futil.py;h=e15b9471e1c5c681d9b6585670c8444884f1d7f6;hb=6bb05fa8ed1a7ea1c369e0cab9a1269eb06a4211;hp=f387f1c09298d948ca115b5fd7c24466b91ee07f;hpb=ee213a9b2c572b9abca9a1babb5c32c9512f0682;p=wrw.git diff --git a/wrw/sp/util.py b/wrw/sp/util.py index f387f1c..e15b947 100644 --- a/wrw/sp/util.py +++ b/wrw/sp/util.py @@ -1,3 +1,5 @@ +import StringIO +from wrw import dispatch import cons def findnsnames(el): @@ -44,6 +46,9 @@ class formatter(object): def text(self, el): self.quotewrite(el) + def rawcode(self, el): + self.write(el) + def attrval(self, buf): qc, qt = (u"'", u"'") if u'"' in buf else (u'"', u""") self.write(qc) @@ -95,7 +100,7 @@ class formatter(object): def endtag(self, el): self.write(u'') - def longtag(self, el): + def longtag(self, el, **extra): self.starttag(el, **extra) for ch in el.children: self.node(ch) @@ -112,6 +117,8 @@ class formatter(object): self.element(el) elif isinstance(el, cons.text): self.text(el) + elif isinstance(el, cons.raw): + self.rawcode(el) else: raise Exception("Unknown object in element tree: " + el) @@ -135,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 = StringIO.StringIO() + cls.output(buf, el, *args, **kw) + return buf.getvalue() + def update(self, **ch): ret = type(self).__new__(type(self)) ret.__dict__.update(self.__dict__) @@ -202,3 +219,22 @@ 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(response, self).__init__() + self.root = root + + @property + def ctype(self): + raise Exception("a subclass of wrw.sp.util.response must override ctype") + + def handle(self, req): + ret = self.formatter.format(self.root, doctype=self.doctype, charset=self.charset) + req.ohead["Content-Type"] = self.ctype + req.ohead["Content-Length"] = len(ret) + return [ret]