roster-window: display loading page when reconnecting accounts
[empathy-mirror.git] / tools / c-constants-gen.py
blob188ab82acd8a6af57ed642dacc634b0bbcf8df70
1 #!/usr/bin/python
3 from sys import argv, stdout, stderr
4 import xml.dom.minidom
6 from libglibcodegen import NS_TP, get_docstring, \
7 get_descendant_text, get_by_path
9 class Generator(object):
10 def __init__(self, prefix, dom, output_base):
11 self.prefix = prefix + '_'
12 self.spec = get_by_path(dom, "spec")[0]
14 self.__header = open(output_base + '.h', 'w')
15 self.__docs = open(output_base + '-gtk-doc.h', 'w')
17 def __call__(self):
18 self.do_header()
19 self.do_body()
20 self.do_footer()
22 def write(self, code):
23 self.__header.write(code.encode('utf-8'))
25 def d(self, code):
26 self.__docs.write(code.encode('utf-8'))
28 # Header
29 def do_header(self):
30 self.write('/* Generated from ')
31 self.write(get_descendant_text(get_by_path(self.spec, 'title')))
32 version = get_by_path(self.spec, "version")
33 if version:
34 self.write(', version ' + get_descendant_text(version))
35 self.write('\n\n')
36 for copyright in get_by_path(self.spec, 'copyright'):
37 self.write(get_descendant_text(copyright))
38 self.write('\n')
39 self.write(get_descendant_text(get_by_path(self.spec, 'license')))
40 self.write('\n')
41 self.write(get_descendant_text(get_by_path(self.spec, 'docstring')))
42 self.write("""
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 \n""")
50 # Body
51 def do_body(self):
52 for elem in self.spec.getElementsByTagNameNS(NS_TP, '*'):
53 if elem.localName == 'flags':
54 self.do_flags(elem)
55 elif elem.localName == 'enum':
56 self.do_enum(elem)
58 def do_flags(self, flags):
59 name = flags.getAttribute('plural') or flags.getAttribute('name')
60 value_prefix = flags.getAttribute('singular') or \
61 flags.getAttribute('value-prefix') or \
62 flags.getAttribute('name')
63 self.d("""\
64 /**
66 %s:
67 """ % (self.prefix + name).replace('_', ''))
68 for flag in get_by_path(flags, 'flag'):
69 self.do_gtkdoc(flag, value_prefix)
70 self.d(' *\n')
71 docstrings = get_by_path(flags, 'docstring')
72 if docstrings:
73 self.d("""\
74 * <![CDATA[%s]]>
76 """ % get_descendant_text(docstrings).replace('\n', ' '))
77 self.d("""\
78 * Bitfield/set of flags generated from the Telepathy specification.
80 """)
82 self.write("typedef enum /*< flags >*/ {\n")
84 for flag in get_by_path(flags, 'flag'):
85 self.do_val(flag, value_prefix)
86 self.write("""\
87 } %s;
89 """ % (self.prefix + name).replace('_', ''))
91 def do_enum(self, enum):
92 name = enum.getAttribute('singular') or enum.getAttribute('name')
93 value_prefix = enum.getAttribute('singular') or \
94 enum.getAttribute('value-prefix') or \
95 enum.getAttribute('name')
96 name_plural = enum.getAttribute('plural') or \
97 enum.getAttribute('name') + 's'
98 self.d("""\
99 /**
102 """ % (self.prefix + name).replace('_', ''))
103 vals = get_by_path(enum, 'enumvalue')
104 for val in vals:
105 self.do_gtkdoc(val, value_prefix)
106 self.d(' *\n')
107 docstrings = get_by_path(enum, 'docstring')
108 if docstrings:
109 self.d("""\
110 * <![CDATA[%s]]>
112 """ % get_descendant_text(docstrings).replace('\n', ' '))
113 self.d("""\
114 * Bitfield/set of flags generated from the Telepathy specification.
116 """)
118 self.write("typedef enum {\n")
120 for val in vals:
121 self.do_val(val, value_prefix)
122 self.write("} %s;\n" % (self.prefix + name).replace('_', ''))
124 self.d("""\
126 * NUM_%(upper-plural)s:
128 * 1 higher than the highest valid value of #%(mixed-name)s.
130 """ % {'mixed-name' : (self.prefix + name).replace('_', ''),
131 'upper-plural' : (self.prefix + name_plural).upper(),
132 'last-val' : vals[-1].getAttribute('value')})
134 self.write("""\
135 #define NUM_%(upper-plural)s (%(last-val)s+1)
137 """ % {'mixed-name' : (self.prefix + name).replace('_', ''),
138 'upper-plural' : (self.prefix + name_plural).upper(),
139 'last-val' : vals[-1].getAttribute('value')})
141 def do_val(self, val, value_prefix):
142 name = val.getAttribute('name')
143 suffix = val.getAttribute('suffix')
144 use_name = (self.prefix + value_prefix + '_' + \
145 (suffix or name)).upper()
146 assert not (name and suffix) or name == suffix, \
147 'Flag/enumvalue name %s != suffix %s' % (name, suffix)
148 self.write(' %s = %s,\n' % (use_name, val.getAttribute('value')))
150 def do_gtkdoc(self, node, value_prefix):
151 self.d(' * @')
152 self.d((self.prefix + value_prefix + '_' +
153 node.getAttribute('suffix')).upper())
154 self.d(': <![CDATA[')
155 docstring = get_by_path(node, 'docstring')
156 self.d(get_descendant_text(docstring).replace('\n', ' '))
157 self.d(']]>\n')
159 # Footer
160 def do_footer(self):
161 self.write("""
162 #ifdef __cplusplus
164 #endif
165 """)
167 if __name__ == '__main__':
168 argv = argv[1:]
169 Generator(argv[0], xml.dom.minidom.parse(argv[1]), argv[2])()