summaryrefslogtreecommitdiff
path: root/tools/glib-errors-str-gen.py
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.co.uk>2012-07-02 15:37:05 +0200
committerXavier Claessens <xavier.claessens@collabora.co.uk>2012-07-03 12:09:56 +0200
commit4b7a97be0f8250af48c5b9f97cace813bc64c0f8 (patch)
treed438c36ea84e7b08d2d1c4abf0b197f1b4c81275 /tools/glib-errors-str-gen.py
parentb1046b3ee4cdd00323a4267a994dc67865a170b0 (diff)
downloadtelepathy-logger-4b7a97be0f8250af48c5b9f97cace813bc64c0f8.tar.gz
Update tools/ copy from telepathy-glib
This avoid single include from generated code
Diffstat (limited to 'tools/glib-errors-str-gen.py')
-rw-r--r--tools/glib-errors-str-gen.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/tools/glib-errors-str-gen.py b/tools/glib-errors-str-gen.py
new file mode 100644
index 0000000..b2cf520
--- /dev/null
+++ b/tools/glib-errors-str-gen.py
@@ -0,0 +1,83 @@
+#!/usr/bin/python
+
+import sys
+import xml.dom.minidom
+
+from libtpcodegen import file_set_contents
+from libglibcodegen import NS_TP, get_docstring, xml_escape
+
+class Generator(object):
+ def __init__(self, dom, basename):
+ self.dom = dom
+ self.errors = self.dom.getElementsByTagNameNS(NS_TP, 'errors')[0]
+ self.basename = basename
+
+ self.__header = []
+ self.__body = []
+ self.__docs = []
+
+ def h(self, s):
+ if isinstance(s, unicode):
+ s = s.encode('utf-8')
+ self.__header.append(s)
+
+ def b(self, s):
+ if isinstance(s, unicode):
+ s = s.encode('utf-8')
+ self.__body.append(s)
+
+ def d(self, s):
+ if isinstance(s, unicode):
+ s = s.encode('utf-8')
+ self.__docs.append(s)
+
+ def __call__(self):
+ errors = self.errors.getElementsByTagNameNS(NS_TP, 'error')
+
+ self.b('#include <telepathy-glib/errors.h>')
+ self.b('')
+ self.b('const gchar *')
+ self.b('tp_error_get_dbus_name (TpError error)')
+ self.b('{')
+ self.b(' switch (error)')
+ self.b(' {')
+
+ for error in errors:
+ ns = error.parentNode.getAttribute('namespace')
+ nick = error.getAttribute('name').replace(' ', '')
+ uc_nick = error.getAttribute('name').replace(' ', '_').replace('.', '_').upper()
+ name = 'TP_ERROR_STR_' + uc_nick
+ error_name = '%s.%s' % (ns, nick)
+
+ self.d('/**')
+ self.d(' * %s:' % name)
+ self.d(' *')
+ self.d(' * The D-Bus error name %s' % error_name)
+ self.d(' *')
+ self.d(' * %s' % xml_escape(get_docstring(error)))
+ self.d(' */')
+ self.d('')
+
+ self.h('#define %s "%s"' % (name, error_name))
+
+ self.b(' case TP_ERROR_%s:' % uc_nick)
+ self.b(' return %s;' % name)
+
+ self.b(' default:')
+ self.b(' g_return_val_if_reached (NULL);')
+ self.b(' }')
+ self.b('}')
+
+ # make both files end with a newline
+ self.h('')
+ self.b('')
+
+ file_set_contents(self.basename + '.h', '\n'.join(self.__header))
+ file_set_contents(self.basename + '.c', '\n'.join(self.__body))
+ file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
+
+if __name__ == '__main__':
+ argv = sys.argv[1:]
+ basename = argv[0]
+
+ Generator(xml.dom.minidom.parse(argv[1]), basename)()