2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 Johan Dahlin
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
21 from __future__ import with_statement
23 from contextlib import contextmanager
24 from cStringIO import StringIO
25 from xml.sax.saxutils import quoteattr
27 from .libtoolimporter import LibtoolImporter
30 def _calc_attrs_length(attributes, indent, self_indent):
34 for attr, value in attributes:
35 # FIXME: actually, if we have attributes with None as value this
36 # should be considered a bug and raise an error. We are just
37 # ignoring them here while we fix GIRParser to create the right
38 # ast with the correct attributes.
41 attr_length += 2 + len(attr) + len(quoteattr(value))
42 return attr_length + indent + self_indent
45 def collect_attributes(tag_name, attributes, self_indent,
46 self_indent_char, indent=-1):
49 if _calc_attrs_length(attributes, indent, self_indent) > 79:
50 indent_len = self_indent + len(tag_name) + 1
55 for attr, value in attributes:
56 # FIXME: actually, if we have attributes with None as value this
57 # should be considered a bug and raise an error. We are just
58 # ignoring them here while we fix GIRParser to create the right
59 # ast with the correct attributes.
62 if indent_len and not first:
63 attr_value += '\n%s' % (self_indent_char * indent_len)
64 attr_value += ' %s=%s' % (attr, quoteattr(value))
71 from giscanner._giscanner import collect_attributes
74 class XMLWriter(object):
77 self._data = StringIO()
78 self._data.write('<?xml version="1.0"?>\n')
82 self._indent_char = ' '
86 def _open_tag(self, tag_name, attributes=None):
87 attrs = collect_attributes(
88 tag_name, attributes, self._indent,
91 self.write_line('<%s%s>' % (tag_name, attrs))
93 def _close_tag(self, tag_name):
94 self.write_line('</%s>' % (tag_name, ))
99 return self._data.getvalue()
101 def write_line(self, line=''):
102 self._data.write('%s%s\n' % (self._indent_char * self._indent, line))
104 def write_comment(self, text):
105 self.write_line('<!-- %s -->' % (text, ))
107 def write_tag(self, tag_name, attributes, data=None):
108 if attributes is None:
110 prefix = '<%s' % (tag_name, )
112 suffix = '>%s</%s>' % (data, tag_name)
115 attrs = collect_attributes(
116 tag_name, attributes,
119 len(prefix) + len(suffix))
120 self.write_line(prefix + attrs + suffix)
122 def push_tag(self, tag_name, attributes=None):
123 self._open_tag(tag_name, attributes)
124 self._tag_stack.append(tag_name)
125 self._indent += self._indent_unit
128 self._indent -= self._indent_unit
129 tag_name = self._tag_stack.pop()
130 self._close_tag(tag_name)
134 def tagcontext(self, tag_name, attributes=None):
135 self.push_tag(tag_name, attributes)
144 w.push_tag('repository')
145 w.push_tag('namespace')
146 w.push_tag('enumeration')
150 ('c:identifier', 'GTK_ANCHOR_WEST'),
151 ('glib:nick', 'west')])
157 lines = x.split('\n')
160 assert len(lines[3]) < 80, len(lines[3])
162 if __name__ == '__main__':