Improve error reporting when trying to quote None. Do not print warnings
[platform/upstream/gobject-introspection.git] / giscanner / xmlwriter.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program 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
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19 #
20
21 from contextlib import contextmanager
22 from cStringIO import StringIO
23 from xml.sax.saxutils import quoteattr
24
25
26 class XMLWriter(object):
27     def __init__(self):
28         self._data = StringIO()
29         self._tag_stack = []
30         self._indent = 0
31         self._indent_unit = 2
32         self._indent_char = ' '
33
34     # Private
35
36     def _calc_attrs_length(self, attributes, indent):
37         if indent == -1:
38             return -1
39         attr_length = 0
40         for attr, value in attributes:
41             if value is None:
42                 raise ValueError(
43                     "value for attribute %r cannot be None" % (attr,))
44             attr_length += 2 + len(attr) + len(quoteattr(value))
45         return attr_length + indent
46
47     def _collect_attributes(self, attributes, extra_indent=-1):
48         if not attributes:
49             return ''
50         extra_indent += len(self._indent_char) * self._indent
51         if self._calc_attrs_length(attributes, extra_indent) > 79:
52             indent_len = extra_indent
53         else:
54             indent_len = 0
55         first = True
56         attr_value = ''
57         for attr, value in attributes:
58             if indent_len and not first:
59                 attr_value += '\n%s' % (self._indent_char * indent_len)
60             if value is None:
61                 raise ValueError(
62                     "value for attribute %r cannot be None" % (attr,))
63             attr_value += ' %s=%s' % (attr, quoteattr(value))
64             if first:
65                 first = False
66         return attr_value
67
68     def _open_tag(self, tag_name, attributes=None):
69         attrs = self._collect_attributes(
70             attributes, len(tag_name) + 1)
71         self.write_line('<%s%s>' % (tag_name, attrs))
72
73     def _close_tag(self, tag_name):
74         self.write_line('</%s>' % (tag_name,))
75
76     # Public API
77
78     def get_xml(self):
79         return self._data.getvalue()
80
81     def write_line(self, line=''):
82         self._data.write('%s%s\n' % (self._indent_char * self._indent, line))
83
84     def write_tag(self, tag_name, attributes, data=None):
85         prefix = '<%s' % (tag_name,)
86         if data is not None:
87             suffix = '>%s</%s>' % (data, tag_name)
88         else:
89             suffix = '/>'
90         attrs = self._collect_attributes(
91             attributes, len(prefix) + len(suffix))
92         self.write_line(prefix + attrs + suffix)
93
94     def push_tag(self, tag_name, attributes=None):
95         self._open_tag(tag_name, attributes)
96         self._tag_stack.append(tag_name)
97         self._indent += self._indent_unit
98
99     def pop_tag(self):
100         self._indent -= self._indent_unit
101         tag_name = self._tag_stack.pop()
102         self._close_tag(tag_name)
103         return tag_name
104
105     @contextmanager
106     def tagcontext(self, tag_name, attributes=None):
107         self.push_tag(tag_name, attributes)
108         try:
109             yield
110         finally:
111             self.pop_tag()
112