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 # ----------------------------------------------------------------------------------------------------
32 class DocbookCodeGenerator
:
33 def __init__(self
, ifaces
, docbook
):
35 self
.docbook
= docbook
36 self
.generate_expand_dicts()
38 def print_method_prototype(self
, i
, m
, in_synopsis
):
42 max_method_len
= max(len(_m
.name
), max_method_len
)
44 max_method_len
= max(len(m
.name
), max_method_len
)
50 max_signature_len
= max(len(a
.signature
), max_signature_len
)
52 max_signature_len
= max(len(a
.signature
), max_signature_len
)
55 max_signature_len
= max(len(a
.signature
), max_signature_len
)
57 max_signature_len
= max(len(a
.signature
), max_signature_len
)
60 self
.out
.write('<link linkend="gdbus-method-%s.%s">%s</link>%*s ('
61 %(utils
.dots_to_hyphens(i
.name
), m
.name
, m
.name
, max_method_len
- len(m
.name
), ''))
63 self
.out
.write('%s%*s ('
64 %(m
.name
, max_method_len
- len(m
.name
), ''))
68 self
.out
.write(',\n%*s'%(max_method_len
+ 2, ''))
69 self
.out
.write('IN %s%*s %s'%(a
.signature
, max_signature_len
- len(a
.signature
), '', a
.name
))
73 self
.out
.write(',\n%*s'%(max_method_len
+ 2, ''))
74 self
.out
.write('OUT %s%*s %s'%(a
.signature
, max_signature_len
- len(a
.signature
), '', a
.name
))
76 self
.out
.write(');\n')
78 def print_signal_prototype(self
, i
, s
, in_synopsis
):
82 max_signal_len
= max(len(_s
.name
), max_signal_len
)
84 max_signal_len
= max(len(s
.name
), max_signal_len
)
90 max_signature_len
= max(len(a
.signature
), max_signature_len
)
93 max_signature_len
= max(len(a
.signature
), max_signature_len
)
96 self
.out
.write('<link linkend="gdbus-signal-%s.%s">%s</link>%*s ('
97 %(utils
.dots_to_hyphens(i
.name
), s
.name
, s
.name
, max_signal_len
- len(s
.name
), ''))
99 self
.out
.write('%s%*s ('
100 %(s
.name
, max_signal_len
- len(s
.name
), ''))
104 self
.out
.write(',\n%*s'%(max_signal_len
+ 2, ''))
105 self
.out
.write('%s%*s %s'%(a
.signature
, max_signature_len
- len(a
.signature
), '', a
.name
))
107 self
.out
.write(');\n')
109 def print_property_prototype(self
, i
, p
, in_synopsis
):
112 for _p
in i
.properties
:
113 max_property_len
= max(len(_p
.name
), max_property_len
)
115 max_property_len
= max(len(p
.name
), max_property_len
)
117 max_signature_len
= 0
119 for _p
in i
.properties
:
120 max_signature_len
= max(len(_p
.signature
), max_signature_len
)
122 max_signature_len
= max(len(p
.signature
), max_signature_len
)
125 self
.out
.write('<link linkend="gdbus-property-%s.%s">%s</link>%*s'
126 %(utils
.dots_to_hyphens(i
.name
), p
.name
, p
.name
, max_property_len
- len(p
.name
), ''))
128 self
.out
.write('%s%*s'
129 %(p
.name
, max_property_len
- len(p
.name
), ''))
130 if p
.readable
and p
.writable
:
136 self
.out
.write(' %s %s\n'%(access
, p
.signature
))
139 def print_synopsis_methods(self
, i
):
140 self
.out
.write(' <refsynopsisdiv role="synopsis">\n'%())
141 self
.out
.write(' <title role="synopsis.title">Methods</title>\n'%())
142 self
.out
.write(' <synopsis>\n'%())
144 self
.print_method_prototype(i
, m
, in_synopsis
=True)
145 self
.out
.write('</synopsis>\n'%())
146 self
.out
.write(' </refsynopsisdiv>\n'%())
148 def print_synopsis_signals(self
, i
):
149 self
.out
.write(' <refsect1 role="signal_proto">\n'%())
150 self
.out
.write(' <title role="signal_proto.title">Signals</title>\n'%())
151 self
.out
.write(' <synopsis>\n'%())
153 self
.print_signal_prototype(i
, s
, in_synopsis
=True)
154 self
.out
.write('</synopsis>\n'%())
155 self
.out
.write(' </refsect1>\n'%())
157 def print_synopsis_properties(self
, i
):
158 self
.out
.write(' <refsect1 role="properties">\n'%())
159 self
.out
.write(' <title role="properties.title">Properties</title>\n'%())
160 self
.out
.write(' <synopsis>\n'%())
161 for p
in i
.properties
:
162 self
.print_property_prototype(i
, p
, in_synopsis
=True)
163 self
.out
.write('</synopsis>\n'%())
164 self
.out
.write(' </refsect1>\n'%())
166 def print_method(self
, i
, m
):
167 self
.out
.write('<refsect2 role="method" id="gdbus-method-%s.%s">\n'%(utils
.dots_to_hyphens(i
.name
), m
.name
))
168 self
.out
.write(' <title>The %s() method</title>\n'%(m
.name
))
169 self
.out
.write(' <indexterm zone="gdbus-method-%s.%s"><primary sortas="%s.%s">%s.%s()</primary></indexterm>\n'%(utils
.dots_to_hyphens(i
.name
), m
.name
, i
.name_without_prefix
, m
.name
, i
.name
, m
.name
))
170 self
.out
.write('<programlisting>\n')
171 self
.print_method_prototype(i
, m
, in_synopsis
=False)
172 self
.out
.write('</programlisting>\n')
173 self
.out
.write('%s\n'%(self
.expand_paras(m
.doc_string
, True)))
174 if m
.in_args
or m
.out_args
:
175 self
.out
.write('<variablelist role="params">\n')
177 self
.out
.write('<varlistentry>\n'%())
178 self
.out
.write(' <term><literal>IN %s <parameter>%s</parameter></literal>:</term>\n'%(a
.signature
, a
.name
))
179 self
.out
.write(' <listitem>%s</listitem>\n'%(self
.expand_paras(a
.doc_string
, True)))
180 self
.out
.write('</varlistentry>\n'%())
182 self
.out
.write('<varlistentry>\n'%())
183 self
.out
.write(' <term><literal>OUT %s <parameter>%s</parameter></literal>:</term>\n'%(a
.signature
, a
.name
))
184 self
.out
.write(' <listitem>%s</listitem>\n'%(self
.expand_paras(a
.doc_string
, True)))
185 self
.out
.write('</varlistentry>\n'%())
186 self
.out
.write('</variablelist>\n')
188 self
.out
.write('<para role="since">Since %s</para>\n'%(m
.since
))
190 self
.out
.write('<warning><para>The %s() method is deprecated.</para></warning>'%(m
.name
))
191 self
.out
.write('</refsect2>\n')
193 def print_signal(self
, i
, s
):
194 self
.out
.write('<refsect2 role="signal" id="gdbus-signal-%s.%s">\n'%(utils
.dots_to_hyphens(i
.name
), s
.name
))
195 self
.out
.write(' <title>The "%s" signal</title>\n'%(s
.name
))
196 self
.out
.write(' <indexterm zone="gdbus-signal-%s.%s"><primary sortas="%s::%s">%s::%s</primary></indexterm>\n'%(utils
.dots_to_hyphens(i
.name
), s
.name
, i
.name_without_prefix
, s
.name
, i
.name
, s
.name
))
197 self
.out
.write('<programlisting>\n')
198 self
.print_signal_prototype(i
, s
, in_synopsis
=False)
199 self
.out
.write('</programlisting>\n')
200 self
.out
.write('%s\n'%(self
.expand_paras(s
.doc_string
, True)))
202 self
.out
.write('<variablelist role="params">\n')
204 self
.out
.write('<varlistentry>\n'%())
205 self
.out
.write(' <term><literal>%s <parameter>%s</parameter></literal>:</term>\n'%(a
.signature
, a
.name
))
206 self
.out
.write(' <listitem>%s</listitem>\n'%(self
.expand_paras(a
.doc_string
, True)))
207 self
.out
.write('</varlistentry>\n'%())
208 self
.out
.write('</variablelist>\n')
210 self
.out
.write('<para role="since">Since %s</para>\n'%(s
.since
))
212 self
.out
.write('<warning><para>The "%s" signal is deprecated.</para></warning>'%(s
.name
))
213 self
.out
.write('</refsect2>\n')
215 def print_property(self
, i
, p
):
216 self
.out
.write('<refsect2 role="property" id="gdbus-property-%s.%s">\n'%(utils
.dots_to_hyphens(i
.name
), p
.name
))
217 self
.out
.write(' <title>The "%s" property</title>\n'%(p
.name
))
218 self
.out
.write(' <indexterm zone="gdbus-property-%s.%s"><primary sortas="%s:%s">%s:%s</primary></indexterm>\n'%(utils
.dots_to_hyphens(i
.name
), p
.name
, i
.name_without_prefix
, p
.name
, i
.name
, p
.name
))
219 self
.out
.write('<programlisting>\n')
220 self
.print_property_prototype(i
, p
, in_synopsis
=False)
221 self
.out
.write('</programlisting>\n')
222 self
.out
.write('%s\n'%(self
.expand_paras(p
.doc_string
, True)))
224 self
.out
.write('<para role="since">Since %s</para>\n'%(p
.since
))
226 self
.out
.write('<warning><para>The "%s" property is deprecated.</para></warning>'%(p
.name
))
227 self
.out
.write('</refsect2>\n')
229 def expand(self
, s
, expandParamsAndConstants
):
230 for key
in self
.expand_member_dict_keys
:
231 s
= s
.replace(key
, self
.expand_member_dict
[key
])
232 for key
in self
.expand_iface_dict_keys
:
233 s
= s
.replace(key
, self
.expand_iface_dict
[key
])
234 if expandParamsAndConstants
:
235 # replace @foo with <parameter>foo</parameter>
236 s
= re
.sub('@[a-zA-Z0-9_]*', lambda m
: '<parameter>' + m
.group(0)[1:] + '</parameter>', s
)
237 # replace e.g. %TRUE with <constant>TRUE</constant>
238 s
= re
.sub('%[a-zA-Z0-9_]*', lambda m
: '<constant>' + m
.group(0)[1:] + '</constant>', s
)
241 def expand_paras(self
, s
, expandParamsAndConstants
):
242 s
= self
.expand(s
, expandParamsAndConstants
).strip()
243 if not s
.startswith("<para"):
244 s
= "<para>%s</para>" % s
247 def generate_expand_dicts(self
):
248 self
.expand_member_dict
= {}
249 self
.expand_iface_dict
= {}
250 for i
in self
.ifaces
:
252 value
= '<link linkend="gdbus-interface-%s.top_of_page">%s</link>'%(utils
.dots_to_hyphens(i
.name
), i
.name
)
253 self
.expand_iface_dict
[key
] = value
255 key
= '%s.%s()'%(i
.name
, m
.name
)
256 value
= '<link linkend="gdbus-method-%s.%s">%s()</link>'%(utils
.dots_to_hyphens(i
.name
), m
.name
, m
.name
)
257 self
.expand_member_dict
[key
] = value
259 key
= '#%s::%s'%(i
.name
, s
.name
)
260 value
= '<link linkend="gdbus-signal-%s.%s">"%s"</link>'%(utils
.dots_to_hyphens(i
.name
), s
.name
, s
.name
)
261 self
.expand_member_dict
[key
] = value
262 for p
in i
.properties
:
263 key
= '#%s:%s'%(i
.name
, p
.name
)
264 value
= '<link linkend="gdbus-property-%s.%s">"%s"</link>'%(utils
.dots_to_hyphens(i
.name
), p
.name
, p
.name
)
265 self
.expand_member_dict
[key
] = value
266 # Make sure to expand the keys in reverse order so e.g. #org.foo.Iface:MediaCompat
267 # is evaluated before #org.foo.Iface:Media ...
268 self
.expand_member_dict_keys
= sorted(self
.expand_member_dict
.keys(), reverse
=True)
269 self
.expand_iface_dict_keys
= sorted(self
.expand_iface_dict
.keys(), reverse
=True)
272 for i
in self
.ifaces
:
273 self
.out
= open('%s-%s.xml'%(self
.docbook
, i
.name
), 'w')
274 self
.out
.write(''%())
275 self
.out
.write('<?xml version="1.0" encoding="utf-8"?>\n'%())
276 self
.out
.write('<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"\n'%())
277 self
.out
.write(' "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [\n'%())
278 self
.out
.write(']>\n'%())
279 self
.out
.write('<refentry id="gdbus-%s">\n'%(i
.name
))
280 self
.out
.write(' <refmeta>'%())
281 self
.out
.write(' <refentrytitle role="top_of_page" id="gdbus-interface-%s.top_of_page">%s</refentrytitle>\n'%(utils
.dots_to_hyphens(i
.name
), i
.name
))
282 self
.out
.write(' <indexterm zone="gdbus-interface-%s.top_of_page"><primary sortas="%s">%s</primary></indexterm>\n'%(utils
.dots_to_hyphens(i
.name
), i
.name_without_prefix
, i
.name
))
283 self
.out
.write(' </refmeta>'%())
285 self
.out
.write(' <refnamediv>'%())
286 self
.out
.write(' <refname>%s</refname>'%(i
.name
))
287 self
.out
.write(' <refpurpose>%s</refpurpose>'%(i
.doc_string_brief
))
288 self
.out
.write(' </refnamediv>'%())
290 if len(i
.methods
) > 0:
291 self
.print_synopsis_methods(i
)
292 if len(i
.signals
) > 0:
293 self
.print_synopsis_signals(i
)
294 if len(i
.properties
) > 0:
295 self
.print_synopsis_properties(i
)
297 self
.out
.write('<refsect1 role="desc" id="gdbus-interface-%s">\n'%(utils
.dots_to_hyphens(i
.name
)))
298 self
.out
.write(' <title role="desc.title">Description</title>\n'%())
299 self
.out
.write(' %s\n'%(self
.expand_paras(i
.doc_string
, True)))
301 self
.out
.write(' <para role="since">Since %s</para>\n'%(i
.since
))
303 self
.out
.write('<warning><para>The %s interface is deprecated.</para></warning>'%(i
.name
))
304 self
.out
.write('</refsect1>\n'%())
306 if len(i
.methods
) > 0:
307 self
.out
.write('<refsect1 role="details" id="gdbus-methods-%s">\n'%(i
.name
))
308 self
.out
.write(' <title role="details.title">Method Details</title>\n'%())
310 self
.print_method(i
, m
)
311 self
.out
.write('</refsect1>\n'%())
313 if len(i
.signals
) > 0:
314 self
.out
.write('<refsect1 role="details" id="gdbus-signals-%s">\n'%(i
.name
))
315 self
.out
.write(' <title role="details.title">Signal Details</title>\n'%())
317 self
.print_signal(i
, s
)
318 self
.out
.write('</refsect1>\n'%())
320 if len(i
.properties
) > 0:
321 self
.out
.write('<refsect1 role="details" id="gdbus-properties-%s">\n'%(i
.name
))
322 self
.out
.write(' <title role="details.title">Property Details</title>\n'%())
323 for s
in i
.properties
:
324 self
.print_property(i
, s
)
325 self
.out
.write('</refsect1>\n'%())
327 self
.out
.write('</refentry>\n')