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
25 from contextlib import contextmanager
26 from cStringIO import StringIO
27 from xml.sax.saxutils import escape
29 from .libtoolimporter import LibtoolImporter
32 with LibtoolImporter(None, None):
33 if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
34 from _giscanner import collect_attributes
36 from giscanner._giscanner import collect_attributes
39 def build_xml_tag(tag_name, attributes=None, data=None, self_indent=0,
40 self_indent_char=' '):
41 if attributes is None:
43 prefix = u'<%s' % (tag_name, )
45 if isinstance(data, str):
46 data = data.decode('UTF-8')
47 suffix = u'>%s</%s>' % (escape(data), tag_name)
50 attrs = collect_attributes(
54 len(prefix) + len(suffix))
55 return prefix + attrs + suffix
58 class XMLWriter(object):
61 self._data = StringIO()
62 self._data.write('<?xml version="1.0"?>\n')
66 self.enable_whitespace()
70 def _open_tag(self, tag_name, attributes=None):
71 if attributes is None:
73 attrs = collect_attributes(tag_name, attributes,
74 self._indent, self._indent_char, len(tag_name) + 2)
75 self.write_line(u'<%s%s>' % (tag_name, attrs))
77 def _close_tag(self, tag_name):
78 self.write_line(u'</%s>' % (tag_name, ))
82 def enable_whitespace(self):
83 self._indent_char = ' '
84 self._newline_char = '\n'
86 def disable_whitespace(self):
87 self._indent_char = ''
88 self._newline_char = ''
91 return self._data.getvalue()
93 def write_line(self, line=u'', indent=True, do_escape=False):
94 if isinstance(line, str):
95 line = line.decode('utf-8')
96 assert isinstance(line, unicode)
100 self._data.write('%s%s%s' % (self._indent_char * self._indent,
101 line.encode('utf-8'),
104 self._data.write('%s%s' % (line.encode('utf-8'), self._newline_char))
106 def write_comment(self, text):
107 self.write_line('<!-- %s -->' % (text, ))
109 def write_tag(self, tag_name, attributes, data=None):
110 self.write_line(build_xml_tag(tag_name, attributes, data,
111 self._indent, self._indent_char))
113 def push_tag(self, tag_name, attributes=None):
114 if attributes is None:
116 self._open_tag(tag_name, attributes)
117 self._tag_stack.append(tag_name)
118 self._indent += self._indent_unit
121 self._indent -= self._indent_unit
122 tag_name = self._tag_stack.pop()
123 self._close_tag(tag_name)
127 def tagcontext(self, tag_name, attributes=None):
128 self.push_tag(tag_name, attributes)
137 w.push_tag('repository')
138 w.push_tag('namespace')
139 w.push_tag('enumeration')
143 ('c:identifier', 'GTK_ANCHOR_WEST'),
144 ('glib:nick', 'west')])
150 lines = x.split('\n')
153 assert len(lines[3]) < 80, len(lines[3])
155 if __name__ == '__main__':