Commit 00dadd57 authored by Michael Büsch's avatar Michael Büsch

xmlfactory: Add support for comments

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent 2d057f7b
...@@ -103,12 +103,14 @@ class XmlFactory(object): ...@@ -103,12 +103,14 @@ class XmlFactory(object):
def __init__(self, name, attrs = None, def __init__(self, name, attrs = None,
tags = None, data = None, tags = None, data = None,
comment = None,
emitEmptyAttrs = False, emitEmptyAttrs = False,
emitEmptyTag = False): emitEmptyTag = False):
self.name = name # Tag name self.name = name or "" # Tag name
self.attrs = attrs or {} # Tag attributes self.attrs = attrs or {} # Tag attributes
self.tags = tags or [] # Child tags self.tags = tags or [] # Child tags
self.data = data or "" # Tag data self.data = data or "" # Tag data
self.commentData = comment or "" # Comment data
self.emitEmptyAttrs = emitEmptyAttrs # Emit attributes with empty data? self.emitEmptyAttrs = emitEmptyAttrs # Emit attributes with empty data?
self.emitEmptyTag = emitEmptyTag # Emit tag, if it is completely empty? self.emitEmptyTag = emitEmptyTag # Emit tag, if it is completely empty?
...@@ -144,6 +146,11 @@ class XmlFactory(object): ...@@ -144,6 +146,11 @@ class XmlFactory(object):
return bool(self.getAttrInt(name, return bool(self.getAttrInt(name,
default if default is self.NoDefault else int(bool(default)))) default if default is self.NoDefault else int(bool(default))))
class Comment(Tag):
def __init__(self, text):
super(XmlFactory.Comment, self).__init__(name=None,
comment=text)
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.__kwargs = kwargs self.__kwargs = kwargs
self.builder = None self.builder = None
...@@ -205,27 +212,43 @@ class XmlFactory(object): ...@@ -205,27 +212,43 @@ class XmlFactory(object):
childTags = self.__tags2text(tag.tags, indent + 1) childTags = self.__tags2text(tag.tags, indent + 1)
else: else:
childTags = [] childTags = []
# Add comment, if any.
def addComment(commentData):
if commentData.startswith("\n"):
prefix = "\n"
commentData = commentData[1:]
else:
prefix = ""
ret.append("%s%s<!-- %s -->" % (
prefix, ind,
commentData.replace("-->", " ->")))
# Convert tags to XML # Convert tags to XML
if tag.data or childTags: if tag.data or childTags:
ret.append( if tag.commentData:
"%s<%s%s>%s" % ( addComment(tag.commentData)
ind, if tag.name:
tag.name, ret.append(
attrText, "%s<%s%s>%s" % (
saxutils.escape(tag.data or "")) ind,
) tag.name,
ret.extend(childTags) attrText,
ret.append("%s</%s>" % ( saxutils.escape(tag.data or ""))
ind, )
tag.name) ret.extend(childTags)
) ret.append("%s</%s>" % (
ind,
tag.name)
)
elif attrs or tag.emitEmptyTag: elif attrs or tag.emitEmptyTag:
ret.append( if tag.commentData:
"%s<%s%s />" % ( addComment(tag.commentData)
ind, if tag.name:
tag.name, ret.append(
attrText) "%s<%s%s />" % (
) ind,
tag.name,
attrText)
)
return ret return ret
def compose(self, genXmlHeader=True, baseIndent=0, lineBreakStr="\n", attrLineBreak=False): def compose(self, genXmlHeader=True, baseIndent=0, lineBreakStr="\n", attrLineBreak=False):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment