3 # GDBus - GLib D-Bus Library
5 # Copyright (C) 2008-2011 Red Hat, Inc.
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.
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.
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/>.
20 # Author: David Zeuthen <davidz@redhat.com>
27 from . import dbustypes
30 from . import codegen_docbook
32 def find_arg(arg_list, arg_name):
34 if a.name == arg_name:
38 def find_method(iface, method):
39 for m in iface.methods:
44 def find_signal(iface, signal):
45 for m in iface.signals:
50 def find_prop(iface, prop):
51 for m in iface.properties:
56 def apply_annotation(iface_list, iface, method, signal, prop, arg, key, value):
64 raise RuntimeError('No interface %s'%iface)
69 method_obj = find_method(iface_obj, method)
70 if method_obj == None:
71 raise RuntimeError('No method %s on interface %s'%(method, iface))
73 arg_obj = find_arg(method_obj.in_args, arg)
75 arg_obj = find_arg(method_obj.out_args, arg)
77 raise RuntimeError('No arg %s on method %s on interface %s'%(arg, method, iface))
80 target_obj = method_obj
82 signal_obj = find_signal(iface_obj, signal)
83 if signal_obj == None:
84 raise RuntimeError('No signal %s on interface %s'%(signal, iface))
86 arg_obj = find_arg(signal_obj.args, arg)
88 raise RuntimeError('No arg %s on signal %s on interface %s'%(arg, signal, iface))
91 target_obj = signal_obj
93 prop_obj = find_prop(iface_obj, prop)
95 raise RuntimeError('No property %s on interface %s'%(prop, iface))
98 target_obj = iface_obj
99 target_obj.annotations.insert(0, dbustypes.Annotation(key, value))
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('::')
109 signal = what[pos + 2:]
110 pos = signal.find('[')
112 arg = signal[pos + 1:]
113 signal = signal[0:pos]
116 apply_annotation(iface_list, iface, None, signal, None, arg, key, value)
118 apply_annotation(iface_list, iface, None, signal, None, None, key, value)
124 prop = what[pos + 1:]
125 apply_annotation(iface_list, iface, None, None, prop, None, key, value)
127 pos = what.find('()')
130 combined = what[0:pos]
131 pos = combined.rfind('.')
132 iface = combined[0:pos]
133 method = combined[pos + 1:]
139 apply_annotation(iface_list, iface, method, None, None, arg, key, value)
141 apply_annotation(iface_list, iface, method, None, None, None, key, value)
143 # must be an interface
145 apply_annotation(iface_list, iface, None, None, None, None, key, value)
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();
167 f = open(fname, 'rb')
170 parsed_ifaces = parser.parse_dbus_xml(xml_data)
171 all_ifaces.extend(parsed_ifaces)
173 if opts.annotate != None:
174 apply_annotations(all_ifaces, opts.annotate)
177 i.post_process(opts.interface_prefix, opts.c_namespace)
179 docbook = opts.generate_docbook
180 docbook_gen = codegen_docbook.DocbookCodeGenerator(all_ifaces, docbook);
182 ret = docbook_gen.generate()
184 c_code = opts.generate_c_code
186 h = open(c_code + '.h', 'w')
187 c = open(c_code + '.c', 'w')
188 gen = codegen.CodeGenerator(all_ifaces,
190 opts.interface_prefix,
191 opts.c_generate_object_manager,
200 if __name__ == "__main__":