-import itertools, StringIO
-from wrw import dispatch
-import cons
+import itertools, io
+from .. import dispatch
+from . import cons
def findnsnames(el):
names = {}
def proc(el):
if isinstance(el, cons.element):
if el.ns not in names:
- names[el.ns] = u"n" + unicode(nid[0])
+ names[el.ns] = "n" + str(nid[0])
nid[:] = [nid[0] + 1]
for ch in el.children:
proc(ch)
self.buf.extend(text.encode(self.charset))
def quotewrite(self, buf):
- buf = buf.replace(u'&', u"&")
- buf = buf.replace(u'<', u"<")
- buf = buf.replace(u'>', u">")
+ buf = buf.replace('&', "&")
+ buf = buf.replace('<', "<")
+ buf = buf.replace('>', ">")
self.write(buf)
def __iter__(self):
if ns is None:
return el.name
else:
- return ns + u":" + el.name
+ return ns + ":" + el.name
def attrval(self, v):
- qc, qt = (u"'", u"'") if u'"' in v else (u'"', u""")
+ qc, qt = ("'", "'") if '"' in v else ('"', """)
self.write(qc)
- v = v.replace(u'&', u"&")
- v = v.replace(u'<', u"<")
- v = v.replace(u'>', u">")
+ v = v.replace('&', "&")
+ v = v.replace('<', "<")
+ v = v.replace('>', ">")
v = v.replace(qc, qt)
self.write(v)
self.write(qc)
def attr(self, k, v):
self.write(k)
- self.write(u"=")
+ self.write("=")
self.attrval(v)
def attrs(self, attrs):
for k, v in attrs:
- self.write(u" ")
+ self.write(" ")
self.attr(k, v)
def inittag(self, el):
- self.write(u"<" + self.elname(el))
- attrs = el.attrs.iteritems()
+ self.write("<" + self.elname(el))
+ attrs = el.attrs.items()
if self.first:
nsnames = []
- for ns, name in self.nsnames.iteritems():
+ for ns, name in self.nsnames.items():
if ns is None:
if name is not None:
raise Exception("null namespace must have null name, not" + name)
continue
- nsnames.append((u"xmlns" if name is None else (u"xmlns:" + name), ns))
+ nsnames.append(("xmlns" if name is None else ("xmlns:" + name), ns))
attrs = itertools.chain(attrs, iter(nsnames))
self.first = False
self.attrs(attrs)
def starttag(self, el):
self.inittag(el)
- self.write(u">")
+ self.write(">")
def shorttag(self, el):
self.inittag(el)
- self.write(u" />")
+ self.write(" />")
def endtag(self, el):
- self.write(u"</" + self.elname(el) + u">")
+ self.write("</" + self.elname(el) + ">")
def text(self, el):
self.quotewrite(el)
self.write(el)
def start(self, el):
- self.write(u'<?xml version="1.0" encoding="' + self.charset + u'" ?>\n')
+ self.write('<?xml version="1.0" encoding="' + self.charset + '" ?>\n')
if isinstance(el, cons.doctype):
- self.write(u'<!DOCTYPE %s PUBLIC "%s" "%s">\n' % (el.rootname,
+ self.write('<!DOCTYPE %s PUBLIC "%s" "%s">\n' % (el.rootname,
el.pubid,
el.dtdid))
self.first = True
def end(self, el):
pass
- def next(self):
+ def __next__(self):
if self.src is None:
raise StopIteration()
try:
self.start(el)
elif ev == "$":
self.end(el)
- ret = str(self.buf)
- self.buf[:] = ""
+ ret = bytes(self.buf)
+ self.buf[:] = b""
return ret
def nsname(self, el):
return ret
if el.ns is None:
return None
- ret = u"n" + unicode(self.nextns)
+ ret = "n" + str(self.nextns)
self.nextns += 1
return ret
@classmethod
def format(cls, root, **kw):
- buf = StringIO.StringIO()
+ buf = io.BytesIO()
cls.output(buf, root, **kw)
return buf.getvalue()
class indenter(formatter):
- def __init__(self, indent=u" ", *args, **kw):
- super(indenter, self).__init__(*args, **kw)
+ def __init__(self, indent=" ", *args, **kw):
+ super().__init__(*args, **kw)
self.indent = indent
self.col = 0
- self.curind = u""
+ self.curind = ""
self.atbreak = True
self.inline = False
self.stack = []
def write(self, text):
- lines = text.split(u"\n")
+ lines = text.split("\n")
if len(lines) > 1:
for ln in lines[:-1]:
self.buf.extend(ln.encode(self.charset))
- self.buf.extend("\n")
+ self.buf.extend(b"\n")
self.col = 0
self.buf.extend(lines[-1].encode(self.charset))
self.col += len(lines[-1])
def br(self):
if not self.atbreak:
- self.buf.extend((u"\n" + self.curind).encode(self.charset))
+ self.buf.extend(("\n" + self.curind).encode(self.charset))
self.col = 0
self.atbreak = True
self.push(el)
self.inline = self.inline or self.inlinep(el)
self.curind += self.indent
- super(indenter, self).starttag(el)
+ super().starttag(el)
def shorttag(self, el):
if not self.inline:
self.br()
- super(indenter, self).shorttag(el)
+ super().shorttag(el)
def endtag(self, el):
il = self.inline
self.pop()
if not il:
self.br()
- super(indenter, self).endtag(el)
+ super().endtag(el)
def start(self, el):
- super(indenter, self).start(el)
+ super().start(el)
self.atbreak = True
def end(self, el):
maxcol = 70
def text(self, el):
- left = unicode(el)
+ left = str(el)
while True:
if len(left) + self.col > self.maxcol:
bp = max(self.maxcol - self.col, 0)
- for i in xrange(bp, -1, -1):
+ for i in range(bp, -1, -1):
if left[i].isspace():
while i > 0 and left[i - 1].isspace(): i -= 1
break
else:
- for i in xrange(bp + 1, len(left)):
+ for i in range(bp + 1, len(left)):
if left[i].isspace():
break
else:
formatter = indenter
def __init__(self, root):
- super(response, self).__init__()
+ super().__init__()
self.root = root
@property