3.12.12
[nijm-empathy.git] / tools / glib-errors-str-gen.py
blobddb1e16b77f7b2ed34086f0fd1bed92e74d34f39
1 #!/usr/bin/python
3 import sys
4 import xml.dom.minidom
6 from libtpcodegen import file_set_contents, u
7 from libglibcodegen import NS_TP, get_docstring, xml_escape
9 class Generator(object):
10 def __init__(self, dom, basename):
11 self.dom = dom
12 self.errors = self.dom.getElementsByTagNameNS(NS_TP, 'errors')[0]
13 self.basename = basename
15 self.__header = []
16 self.__body = []
17 self.__docs = []
19 def h(self, s):
20 self.__header.append(s)
22 def b(self, s):
23 self.__body.append(s)
25 def d(self, s):
26 self.__docs.append(s)
28 def __call__(self):
29 errors = self.errors.getElementsByTagNameNS(NS_TP, 'error')
31 self.b('#include <telepathy-glib/errors.h>')
32 self.b('')
33 self.b('const gchar *')
34 self.b('tp_error_get_dbus_name (TpError error)')
35 self.b('{')
36 self.b(' switch (error)')
37 self.b(' {')
39 for error in errors:
40 ns = error.parentNode.getAttribute('namespace')
41 nick = error.getAttribute('name').replace(' ', '')
42 uc_nick = error.getAttribute('name').replace(' ', '_').replace('.', '_').upper()
43 name = 'TP_ERROR_STR_' + uc_nick
44 error_name = '%s.%s' % (ns, nick)
46 self.d('/**')
47 self.d(' * %s:' % name)
48 self.d(' *')
49 self.d(' * The D-Bus error name %s' % error_name)
50 self.d(' *')
51 self.d(' * %s' % xml_escape(get_docstring(error)))
52 self.d(' */')
53 self.d('')
55 self.h('#define %s "%s"' % (name, error_name))
57 self.b(' case TP_ERROR_%s:' % uc_nick)
58 self.b(' return %s;' % name)
60 self.b(' default:')
61 self.b(' g_return_val_if_reached (NULL);')
62 self.b(' }')
63 self.b('}')
65 # make both files end with a newline
66 self.h('')
67 self.b('')
69 file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
70 file_set_contents(self.basename + '.c', u('\n').join(self.__body).encode('utf-8'))
71 file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
73 if __name__ == '__main__':
74 argv = sys.argv[1:]
75 basename = argv[0]
77 Generator(xml.dom.minidom.parse(argv[1]), basename)()