]> git.0d.be Git - empathy.git/blob - tools/c-constants-gen.py
check-whitespace.sh: check trailing tabs
[empathy.git] / tools / c-constants-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, camelcase_to_upper, get_docstring, \
7         get_descendant_text, get_by_path
8
9 class Generator(object):
10     def __init__(self, prefix, dom):
11         self.prefix = prefix + '_'
12         self.spec = get_by_path(dom, "spec")[0]
13
14     def __call__(self):
15         self.do_header()
16         self.do_body()
17         self.do_footer()
18
19     # Header
20     def do_header(self):
21         stdout.write('/* Generated from ')
22         stdout.write(get_descendant_text(get_by_path(self.spec, 'title')))
23         version = get_by_path(self.spec, "version")
24         if version:
25             stdout.write(', version ' + get_descendant_text(version))
26         stdout.write('\n\n')
27         for copyright in get_by_path(self.spec, 'copyright'):
28             stdout.write(get_descendant_text(copyright))
29             stdout.write('\n')
30         stdout.write(get_descendant_text(get_by_path(self.spec, 'license')))
31         stdout.write('\n')
32         stdout.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
33         stdout.write("""
34  */
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 \n""")
40
41     # Body
42     def do_body(self):
43         for elem in self.spec.getElementsByTagNameNS(NS_TP, '*'):
44             if elem.localName == 'flags':
45                 self.do_flags(elem)
46             elif elem.localName == 'enum':
47                 self.do_enum(elem)
48
49     def do_flags(self, flags):
50         name = flags.getAttribute('plural') or flags.getAttribute('name')
51         value_prefix = flags.getAttribute('singular') or \
52                        flags.getAttribute('value-prefix') or \
53                        flags.getAttribute('name')
54         stdout.write("""\
55 /**
56  *
57 %s:
58 """ % (self.prefix + name).replace('_', ''))
59         for flag in get_by_path(flags, 'flag'):
60             self.do_gtkdoc(flag, value_prefix)
61         stdout.write(' *\n')
62         docstrings = get_by_path(flags, 'docstring')
63         if docstrings:
64             stdout.write("""\
65  * <![CDATA[%s]]>
66  *
67 """ % get_descendant_text(docstrings).replace('\n', ' '))
68         stdout.write("""\
69  * Bitfield/set of flags generated from the Telepathy specification.
70  */
71 typedef enum {
72 """)
73         for flag in get_by_path(flags, 'flag'):
74             self.do_val(flag, value_prefix)
75         stdout.write("""\
76 } %s;
77
78 """ % (self.prefix + name).replace('_', ''))
79
80     def do_enum(self, enum):
81         name = enum.getAttribute('singular') or enum.getAttribute('name')
82         value_prefix = enum.getAttribute('singular') or \
83                        enum.getAttribute('value-prefix') or \
84                        enum.getAttribute('name')
85         name_plural = enum.getAttribute('plural') or \
86                       enum.getAttribute('name') + 's'
87         stdout.write("""\
88 /**
89  *
90 %s:
91 """ % (self.prefix + name).replace('_', ''))
92         vals = get_by_path(enum, 'enumvalue')
93         for val in vals:
94             self.do_gtkdoc(val, value_prefix)
95         stdout.write(' *\n')
96         docstrings = get_by_path(enum, 'docstring')
97         if docstrings:
98             stdout.write("""\
99  * <![CDATA[%s]]>
100  *
101 """ % get_descendant_text(docstrings).replace('\n', ' '))
102         stdout.write("""\
103  * Bitfield/set of flags generated from the Telepathy specification.
104  */
105 typedef enum {
106 """)
107         for val in vals:
108             self.do_val(val, value_prefix)
109         stdout.write("""\
110 } %(mixed-name)s;
111
112 /**
113  * NUM_%(upper-plural)s:
114  *
115  * 1 higher than the highest valid value of #%(mixed-name)s.
116  */
117 #define NUM_%(upper-plural)s (%(last-val)s+1)
118
119 """ % {'mixed-name' : (self.prefix + name).replace('_', ''),
120        'upper-plural' : (self.prefix + name_plural).upper(),
121        'last-val' : vals[-1].getAttribute('value')})
122
123     def do_val(self, val, value_prefix):
124         name = val.getAttribute('name')
125         suffix = val.getAttribute('suffix')
126         use_name = (self.prefix + value_prefix + '_' + \
127                 (suffix or name)).upper()
128         assert not (name and suffix) or name == suffix, \
129                 'Flag/enumvalue name %s != suffix %s' % (name, suffix)
130         stdout.write('    %s = %s,\n' % (use_name, val.getAttribute('value')))
131
132     def do_gtkdoc(self, node, value_prefix):
133         stdout.write(' * @')
134         stdout.write((self.prefix + value_prefix + '_' +
135             node.getAttribute('suffix')).upper())
136         stdout.write(': <![CDATA[')
137         docstring = get_by_path(node, 'docstring')
138         stdout.write(get_descendant_text(docstring).replace('\n', ' '))
139         stdout.write(']]>\n')
140
141     # Footer
142     def do_footer(self):
143         stdout.write("""
144 #ifdef __cplusplus
145 }
146 #endif
147 """)
148
149 if __name__ == '__main__':
150     argv = argv[1:]
151     Generator(argv[0], xml.dom.minidom.parse(argv[1]))()