Updated FSF's address
[platform/upstream/glib.git] / gio / gdbus-2.0 / codegen / codegen_main.py
1 # -*- Mode: Python -*-
2
3 # GDBus - GLib D-Bus Library
4 #
5 # Copyright (C) 2008-2011 Red Hat, Inc.
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General
18 # Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: David Zeuthen <davidz@redhat.com>
21
22 import sys
23 import optparse
24
25 from . import config
26 from . import utils
27 from . import dbustypes
28 from . import parser
29 from . import codegen
30 from . import codegen_docbook
31
32 def find_arg(arg_list, arg_name):
33     for a in arg_list:
34         if a.name == arg_name:
35             return a
36     return None
37
38 def find_method(iface, method):
39     for m in iface.methods:
40         if m.name == method:
41             return m
42     return None
43
44 def find_signal(iface, signal):
45     for m in iface.signals:
46         if m.name == signal:
47             return m
48     return None
49
50 def find_prop(iface, prop):
51     for m in iface.properties:
52         if m.name == prop:
53             return m
54     return None
55
56 def apply_annotation(iface_list, iface, method, signal, prop, arg, key, value):
57     iface_obj = None
58     for i in iface_list:
59         if i.name == iface:
60             iface_obj = i
61             break
62
63     if iface_obj == None:
64         raise RuntimeError('No interface %s'%iface)
65
66     target_obj = None
67
68     if method:
69         method_obj = find_method(iface_obj, method)
70         if method_obj == None:
71             raise RuntimeError('No method %s on interface %s'%(method, iface))
72         if arg:
73             arg_obj = find_arg(method_obj.in_args, arg)
74             if (arg_obj == None):
75                 arg_obj = find_arg(method_obj.out_args, arg)
76                 if (arg_obj == None):
77                     raise RuntimeError('No arg %s on method %s on interface %s'%(arg, method, iface))
78             target_obj = arg_obj
79         else:
80             target_obj = method_obj
81     elif signal:
82         signal_obj = find_signal(iface_obj, signal)
83         if signal_obj == None:
84             raise RuntimeError('No signal %s on interface %s'%(signal, iface))
85         if arg:
86             arg_obj = find_arg(signal_obj.args, arg)
87             if (arg_obj == None):
88                 raise RuntimeError('No arg %s on signal %s on interface %s'%(arg, signal, iface))
89             target_obj = arg_obj
90         else:
91             target_obj = signal_obj
92     elif prop:
93         prop_obj = find_prop(iface_obj, prop)
94         if prop_obj == None:
95             raise RuntimeError('No property %s on interface %s'%(prop, iface))
96         target_obj = prop_obj
97     else:
98         target_obj = iface_obj
99     target_obj.annotations.insert(0, dbustypes.Annotation(key, value))
100
101
102 def apply_annotations(iface_list, annotation_list):
103     # apply annotations given on the command line
104     for (what, key, value) in annotation_list:
105         pos = what.find('::')
106         if pos != -1:
107             # signal
108             iface = what[0:pos];
109             signal = what[pos + 2:]
110             pos = signal.find('[')
111             if pos != -1:
112                 arg = signal[pos + 1:]
113                 signal = signal[0:pos]
114                 pos = arg.find(']')
115                 arg = arg[0:pos]
116                 apply_annotation(iface_list, iface, None, signal, None, arg, key, value)
117             else:
118                 apply_annotation(iface_list, iface, None, signal, None, None, key, value)
119         else:
120             pos = what.find(':')
121             if pos != -1:
122                 # property
123                 iface = what[0:pos];
124                 prop = what[pos + 1:]
125                 apply_annotation(iface_list, iface, None, None, prop, None, key, value)
126             else:
127                 pos = what.find('()')
128                 if pos != -1:
129                     # method
130                     combined = what[0:pos]
131                     pos = combined.rfind('.')
132                     iface = combined[0:pos]
133                     method = combined[pos + 1:]
134                     pos = what.find('[')
135                     if pos != -1:
136                         arg = what[pos + 1:]
137                         pos = arg.find(']')
138                         arg = arg[0:pos]
139                         apply_annotation(iface_list, iface, method, None, None, arg, key, value)
140                     else:
141                         apply_annotation(iface_list, iface, method, None, None, None, key, value)
142                 else:
143                     # must be an interface
144                     iface = what
145                     apply_annotation(iface_list, iface, None, None, None, None, key, value)
146
147 def codegen_main():
148     arg_parser = optparse.OptionParser('%prog [options]')
149     arg_parser.add_option('', '--xml-files', metavar='FILE', action='append',
150                           help='D-Bus introspection XML file')
151     arg_parser.add_option('', '--interface-prefix', metavar='PREFIX', default='',
152                             help='String to strip from D-Bus interface names for code and docs')
153     arg_parser.add_option('', '--c-namespace', metavar='NAMESPACE', default='',
154                             help='The namespace to use for generated C code')
155     arg_parser.add_option('', '--c-generate-object-manager', action='store_true',
156                             help='Generate a GDBusObjectManagerClient subclass when generating C code')
157     arg_parser.add_option('', '--generate-c-code', metavar='OUTFILES',
158                           help='Generate C code in OUTFILES.[ch]')
159     arg_parser.add_option('', '--generate-docbook', metavar='OUTFILES',
160                           help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
161     arg_parser.add_option('', '--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
162                           help='Add annotation (may be used several times)')
163     (opts, args) = arg_parser.parse_args();
164
165     all_ifaces = []
166     for fname in args:
167         f = open(fname, 'rb')
168         xml_data = f.read()
169         f.close()
170         parsed_ifaces = parser.parse_dbus_xml(xml_data)
171         all_ifaces.extend(parsed_ifaces)
172
173     if opts.annotate != None:
174         apply_annotations(all_ifaces, opts.annotate)
175
176     for i in all_ifaces:
177         i.post_process(opts.interface_prefix, opts.c_namespace)
178
179     docbook = opts.generate_docbook
180     docbook_gen = codegen_docbook.DocbookCodeGenerator(all_ifaces, docbook);
181     if docbook:
182         ret = docbook_gen.generate()
183
184     c_code = opts.generate_c_code
185     if c_code:
186         h = open(c_code + '.h', 'w')
187         c = open(c_code + '.c', 'w')
188         gen = codegen.CodeGenerator(all_ifaces,
189                                     opts.c_namespace,
190                                     opts.interface_prefix,
191                                     opts.c_generate_object_manager,
192                                     docbook_gen,
193                                     h, c);
194         ret = gen.generate()
195         h.close()
196         c.close()
197
198     sys.exit(0)
199
200 if __name__ == "__main__":
201     codegen_main()