Coverage for .tox/testcoverage/lib/python2.7/site-packages/py/_xmlgen : 26%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" module for generating and serializing xml and html structures by using simple python objects.
(c) holger krekel, holger at merlinux eu. 2009 """
def u(s): return s def unicode(x, errors=None): if hasattr(x, '__unicode__'): return x.__unicode__() return str(x) else:
if self == Namespace: raise ValueError("Namespace class is abstract") tagspec = self.__tagspec__ if tagspec is not None and name not in tagspec: raise AttributeError(name) classattr = {} if self.__stickyname__: classattr['xmlname'] = name cls = type(name, (self.__tagclass__,), classattr) setattr(self, name, cls) return cls
self.__dict__.update(kwargs)
super(Tag, self).__init__(args) self.attr = self.Attr(**kwargs)
return self.unicode(indent=0)
l = [] SimpleUnicodeVisitor(l.append, indent).visit(self) return u("").join(l)
name = self.__class__.__name__ return "<%r tag object %d>" % (name, id(self))
'__tagspec__': None, '__tagclass__': Tag, '__stickyname__': False, })
l = [] HtmlVisitor(l.append, indent, shortempty=False).visit(self) return u("").join(l)
# exported plain html namespace 'a,abbr,acronym,address,applet,area,b,bdo,big,blink,' 'blockquote,body,br,button,caption,center,cite,code,col,' 'colgroup,comment,dd,del,dfn,dir,div,dl,dt,em,embed,' 'fieldset,font,form,frameset,h1,h2,h3,h4,h5,h6,head,html,' 'i,iframe,img,input,ins,kbd,label,legend,li,link,listing,' 'map,marquee,menu,meta,multicol,nobr,noembed,noframes,' 'noscript,object,ol,optgroup,option,p,pre,q,s,script,' 'select,small,span,strike,strong,style,sub,sup,table,' 'tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,xmp,' 'base,basefont,frame,hr,isindex,param,samp,var' ).split(',') if x])
for x, y in kw.items(): x = x.replace('_', '-') setattr(self, x, y)
"""just a box that can contain a unicode string that will be included directly in the output""" self.uniobj = uniobj
""" recursive visitor to write unicode. """ self.write = write self.cache = {} self.visited = {} # for detection of recursion self.indent = indent self.curindent = curindent self.parents = [] self.shortempty = shortempty # short empty tags or not
""" dispatcher on node's class/bases name. """ cls = node.__class__ try: visitmethod = self.cache[cls] except KeyError: for subclass in cls.__mro__: visitmethod = getattr(self, subclass.__name__, None) if visitmethod is not None: break else: visitmethod = self.__object self.cache[cls] = visitmethod visitmethod(node)
# the default fallback handler is marked private # to avoid clashes with the tag name object #self.write(obj) self.write(escape(unicode(obj)))
self.write(obj.uniobj)
assert id(obj) not in self.visited self.visited[id(obj)] = 1 for elem in obj: self.visit(elem)
assert id(tag) not in self.visited try: tag.parent = self.parents[-1] except IndexError: tag.parent = None self.visited[id(tag)] = 1 tagname = getattr(tag, 'xmlname', tag.__class__.__name__) if self.curindent and not self._isinline(tagname): self.write("\n" + u(' ') * self.curindent) if tag: self.curindent += self.indent self.write(u('<%s%s>') % (tagname, self.attributes(tag))) self.parents.append(tag) for x in tag: self.visit(x) self.parents.pop() self.write(u('</%s>') % tagname) self.curindent -= self.indent else: nameattr = tagname+self.attributes(tag) if self._issingleton(tagname): self.write(u('<%s/>') % (nameattr,)) else: self.write(u('<%s></%s>') % (nameattr, tagname))
# serialize attributes attrlist = dir(tag.attr) attrlist.sort() l = [] for name in attrlist: res = self.repr_attribute(tag.attr, name) if res is not None: l.append(res) l.extend(self.getstyle(tag)) return u("").join(l)
if name[:2] != '__': value = getattr(attrs, name) if name.endswith('_'): name = name[:-1] if isinstance(value, raw): insert = value.uniobj else: insert = escape(unicode(value)) return ' %s="%s"' % (name, insert)
""" return attribute list suitable for styling. """ try: styledict = tag.style.__dict__ except AttributeError: return [] else: stylelist = [x+': ' + y for x,y in styledict.items()] return [u(' style="%s"') % u('; ').join(stylelist)]
"""can (and will) be overridden in subclasses""" return self.shortempty
"""can (and will) be overridden in subclasses""" return False
('br,img,area,param,col,hr,meta,link,base,' 'input,frame').split(',')]) ('a abbr acronym b basefont bdo big br cite code dfn em font ' 'i img input kbd label q s samp select small span strike ' 'strong sub sup textarea tt u var'.split(' '))])
if name == 'class_': value = getattr(attrs, name) if value is None: return return super(HtmlVisitor, self).repr_attribute(attrs, name)
return tagname in self.single
return tagname in self.inline
u('"') : u('"'), u('<') : u('<'), u('>') : u('>'), u('&') : u('&'), u("'") : u('''), }
return self.escape[match.group(0)]
""" xml-escape the given unicode string. """ try: ustring = unicode(ustring) except UnicodeDecodeError: ustring = unicode(ustring, 'utf-8', errors='replace') return self.charef_rex.sub(self._replacer, ustring)
|