]> git.0d.be Git - empathy.git/blob - tools/glib-interfaces-gen.py
account-settings: allow to change the service
[empathy.git] / tools / glib-interfaces-gen.py
1 #!/usr/bin/python
2
3 from sys import argv, stdout, stderr
4 import xml.dom.minidom
5
6 from libglibcodegen import NS_TP, get_docstring, \
7         get_descendant_text, get_by_path
8
9 class Generator(object):
10     def __init__(self, prefix, implfile, declfile, dom):
11         self.prefix = prefix + '_'
12
13         assert declfile.endswith('.h')
14         docfile = declfile[:-2] + '-gtk-doc.h'
15
16         self.impls = open(implfile, 'w')
17         self.decls = open(declfile, 'w')
18         self.docs = open(docfile, 'w')
19         self.spec = get_by_path(dom, "spec")[0]
20
21     def h(self, code):
22         self.decls.write(code.encode('utf-8'))
23
24     def c(self, code):
25         self.impls.write(code.encode('utf-8'))
26
27     def d(self, code):
28         self.docs.write(code.encode('utf-8'))
29
30     def __call__(self):
31         for f in self.h, self.c:
32             self.do_header(f)
33         self.do_body()
34
35     # Header
36     def do_header(self, f):
37         f('/* Generated from: ')
38         f(get_descendant_text(get_by_path(self.spec, 'title')))
39         version = get_by_path(self.spec, "version")
40         if version:
41             f(' version ' + get_descendant_text(version))
42         f('\n\n')
43         for copyright in get_by_path(self.spec, 'copyright'):
44             f(get_descendant_text(copyright))
45             f('\n')
46         f('\n')
47         f(get_descendant_text(get_by_path(self.spec, 'license')))
48         f(get_descendant_text(get_by_path(self.spec, 'docstring')))
49         f("""
50  */
51
52 """)
53
54     # Body
55     def do_body(self):
56         for iface in self.spec.getElementsByTagName('interface'):
57             self.do_iface(iface)
58
59     def do_iface(self, iface):
60         parent_name = get_by_path(iface, '../@name')
61         self.d("""\
62 /**
63  * %(IFACE_DEFINE)s:
64  *
65  * The interface name "%(name)s"
66  */
67 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
68             parent_name).upper().replace('/', ''),
69        'name' : iface.getAttribute('name')})
70
71         self.h("""
72 #define %(IFACE_DEFINE)s \\
73 "%(name)s"
74 """ % {'IFACE_DEFINE' : (self.prefix + 'IFACE_' + \
75             parent_name).upper().replace('/', ''),
76        'name' : iface.getAttribute('name')})
77
78         self.d("""
79 /**
80  * %(IFACE_QUARK_DEFINE)s:
81  *
82  * Expands to a call to a function that returns a quark for the interface \
83 name "%(name)s"
84  */
85 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
86             parent_name).upper().replace('/', ''),
87        'iface_quark_func' : (self.prefix + 'iface_quark_' + \
88             parent_name).lower().replace('/', ''),
89        'name' : iface.getAttribute('name')})
90
91         self.h("""
92 #define %(IFACE_QUARK_DEFINE)s \\
93   (%(iface_quark_func)s ())
94
95 GQuark %(iface_quark_func)s (void);
96
97 """ % {'IFACE_QUARK_DEFINE' : (self.prefix + 'IFACE_QUARK_' + \
98             parent_name).upper().replace('/', ''),
99        'iface_quark_func' : (self.prefix + 'iface_quark_' + \
100             parent_name).lower().replace('/', ''),
101        'name' : iface.getAttribute('name')})
102
103         self.c("""\
104 GQuark
105 %(iface_quark_func)s (void)
106 {
107   static GQuark quark = 0;
108
109   if (G_UNLIKELY (quark == 0))
110     {
111       quark = g_quark_from_static_string ("%(name)s");
112     }
113
114   return quark;
115 }
116
117 """ % {'iface_quark_func' : (self.prefix + 'iface_quark_' + \
118             parent_name).lower().replace('/', ''),
119        'name' : iface.getAttribute('name')})
120
121         for prop in iface.getElementsByTagNameNS(None, 'property'):
122             self.d("""
123 /**
124  * %(IFACE_PREFIX)s_%(PROP_UC)s:
125  *
126  * The fully-qualified property name "%(name)s.%(prop)s"
127  */
128 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
129                 parent_name).upper().replace('/', ''),
130            'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
131            'name' : iface.getAttribute('name'),
132            'prop' : prop.getAttribute('name'),
133            })
134
135             self.h("""
136 #define %(IFACE_PREFIX)s_%(PROP_UC)s \\
137 "%(name)s.%(prop)s"
138 """ % {'IFACE_PREFIX' : (self.prefix + 'PROP_' + \
139                 parent_name).upper().replace('/', ''),
140            'PROP_UC': prop.getAttributeNS(NS_TP, "name-for-bindings").upper(),
141            'name' : iface.getAttribute('name'),
142            'prop' : prop.getAttribute('name'),
143            })
144
145
146         for prop in iface.getElementsByTagNameNS(NS_TP, 'contact-attribute'):
147             self.d("""
148 /**
149  * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
150  *
151  * The fully-qualified contact attribute token name "%(name)s/%(prop)s"
152  */
153 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
154                 parent_name).upper().replace('/', ''),
155            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
156            'name' : iface.getAttribute('name'),
157            'prop' : prop.getAttribute('name'),
158            })
159
160             self.h("""
161 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
162 "%(name)s/%(prop)s"
163 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
164                 parent_name).upper().replace('/', ''),
165            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
166            'name' : iface.getAttribute('name'),
167            'prop' : prop.getAttribute('name'),
168            })
169
170         for prop in iface.getElementsByTagNameNS(NS_TP, 'hct'):
171             if (prop.getAttribute('is-family') != "yes"):
172                 self.d("""
173 /**
174  * %(TOKEN_PREFIX)s_%(TOKEN_UC)s:
175  *
176  * The fully-qualified capability token name "%(name)s/%(prop)s"
177  */
178 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
179                 parent_name).upper().replace('/', ''),
180            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
181            'name' : iface.getAttribute('name'),
182            'prop' : prop.getAttribute('name'),
183            })
184
185                 self.h("""
186 #define %(TOKEN_PREFIX)s_%(TOKEN_UC)s \\
187 "%(name)s/%(prop)s"
188 """ % {'TOKEN_PREFIX' : (self.prefix + 'TOKEN_' + \
189                 parent_name).upper().replace('/', ''),
190            'TOKEN_UC': prop.getAttributeNS(None, "name").upper().replace("-", "_").replace(".", "_"),
191            'name' : iface.getAttribute('name'),
192            'prop' : prop.getAttribute('name'),
193            })
194
195 if __name__ == '__main__':
196     argv = argv[1:]
197     Generator(argv[0], argv[1], argv[2], xml.dom.minidom.parse(argv[3]))()