summaryrefslogtreecommitdiff
path: root/giscanner/xmlwriter.py
diff options
context:
space:
mode:
authorJohan Dahlin <johan@src.gnome.org>2008-04-28 00:54:43 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-04-28 00:54:43 +0000
commit1fdc97d62fd2c523db8a8053f7b6e9af598cde45 (patch)
tree223ad704e737c5d6f02eeef337f7142e4b8bd7e3 /giscanner/xmlwriter.py
parentffdd14a60438db558819ab12134e7ea7d8472566 (diff)
downloadgobject-introspection-1fdc97d62fd2c523db8a8053f7b6e9af598cde45.tar.gz
Refactor pretty attribute indentation to be more generic and always use it
svn path=/trunk/; revision=236
Diffstat (limited to 'giscanner/xmlwriter.py')
-rw-r--r--giscanner/xmlwriter.py69
1 files changed, 30 insertions, 39 deletions
diff --git a/giscanner/xmlwriter.py b/giscanner/xmlwriter.py
index c80c90d4..53926d7c 100644
--- a/giscanner/xmlwriter.py
+++ b/giscanner/xmlwriter.py
@@ -18,7 +18,6 @@
#
from contextlib import contextmanager
-
from cStringIO import StringIO
from xml.sax.saxutils import quoteattr
@@ -28,77 +27,69 @@ class XMLWriter(object):
self._data = StringIO()
self._tag_stack = []
self._indent = 0
+ self._indent_unit = 2
+ self._indent_char = ' '
# Private
- def _collect_attributes(self, attributes):
- attr_value = ''
- if attributes:
- for attr, value in attributes:
- assert value is not None, attr
- attr_value += ' %s=%s' % (attr, quoteattr(value))
+ def _calc_attrs_length(self, attributes):
+ attr_length = 0
+ for attr, value in attributes:
+ attr_length += 1 + len(attr) + len(quoteattr(value))
+ return attr_length
- return attr_value
+ def _collect_attributes(self, attributes, indent=-1):
+ if not attributes:
+ return ''
- def _collect_attributes_wrapped(self, tag_name, attributes):
- assert attributes
+ if indent != -1 and self._calc_attrs_length(attributes) > 79:
+ indent_len = self._indent + indent
+ else:
+ indent_len = 0
+ first = True
attr_value = ''
- indent_len = 0
for attr, value in attributes:
- if indent_len:
- attr_value += '\n%s' % (' ' * indent_len)
+ if indent_len and not first:
+ attr_value += '\n%s' % (self._indent_char * indent_len)
assert value is not None, attr
attr_value += ' %s=%s' % (attr, quoteattr(value))
- if not indent_len:
- indent_len = (self._indent +
- len(tag_name) + 1)
-
+ if first:
+ first = False
return attr_value
def _open_tag(self, tag_name, attributes=None):
- attrs = self._collect_attributes(attributes)
- if (len(attrs) + len(tag_name) + 2) > 79:
- attrs = self._collect_attributes_wrapped(tag_name, attributes)
+ attrs = self._collect_attributes(
+ attributes, len(tag_name) + 1)
self.write_line('<%s%s>' % (tag_name, attrs))
def _close_tag(self, tag_name):
self.write_line('</%s>' % (tag_name,))
- def _rewrap_line(self, line, attrs):
- # assume we have an xml tag here
- assert line[0] == '<' and line[-1] == '>'
- if not line.endswith('/>'):
- tagname = line[1:].split(None, 1)[0]
- line += "</%s>" % (tagname,)
- from xml.etree.ElementTree import parse
- doc = parseString(line)
- print doc
- return line
-
# Public API
def get_xml(self):
return self._data.getvalue()
def write_line(self, line=''):
- self._data.write('%s%s\n' % (' ' * self._indent, line))
+ self._data.write('%s%s\n' % (self._indent_char * self._indent, line))
def write_tag(self, tag_name, attributes, data=None):
- attrs = self._collect_attributes(attributes)
- output = '<%s%s' % (tag_name, attrs)
+ prefix = '<%s' % (tag_name,)
if data:
- output = '>%s</%s>' % (data, tag_name)
+ suffix = '>%s</%s>' % (data, tag_name)
else:
- output += '/>'
- self.write_line(output)
+ suffix = '/>'
+ attrs = self._collect_attributes(
+ attributes, len(prefix) + len(suffix))
+ self.write_line(prefix + attrs + suffix)
def push_tag(self, tag_name, attributes=None):
self._open_tag(tag_name, attributes)
self._tag_stack.append(tag_name)
- self._indent += 2
+ self._indent += self._indent_unit
def pop_tag(self):
- self._indent -= 2
+ self._indent -= self._indent_unit
tag_name = self._tag_stack.pop()
self._close_tag(tag_name)
return tag_name