1 # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 # This file is part of elogind.
5 # Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
7 # elogind is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
12 # elogind is distributed in the hope that it will be useful, but
13 # 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 Public License
18 # along with elogind; If not, see <http://www.gnu.org/licenses/>.
23 from xml_helper import *
24 from copy import deepcopy
27 <refentry id="elogind.directives" conditional="HAVE_PYTHON">
30 <title>elogind.directives</title>
31 <productname>elogind</productname>
35 <contrib>Developer</contrib>
36 <firstname>Zbigniew</firstname>
37 <surname>Jędrzejewski-Szmek</surname>
38 <email>zbyszek@in.waw.pl</email>
44 <refentrytitle>elogind.directives</refentrytitle>
45 <manvolnum>7</manvolnum>
49 <refname>elogind.directives</refname>
50 <refpurpose>Index of configuration directives</refpurpose>
54 <title>PAM configuration directives</title>
56 <para>Directives for configuring PAM behaviour.</para>
58 <variablelist id='pam-directives' />
62 <title>elogind manager directives</title>
64 <para>Directives for configuring the behaviour of the
65 elogind process.</para>
67 <variablelist id='elogind-directives' />
71 <title>command line options</title>
73 <para>Command-line options accepted by programs in the
76 <variablelist id='options' />
80 <title>Constants</title>
82 <para>Various constant used and/or defined by elogind.</para>
84 <variablelist id='constants' />
88 <title>Miscellaneous options and directives</title>
90 <para>Other configuration elements which don't fit in
91 any of the above groups.</para>
93 <variablelist id='miscellaneous' />
97 <title>Files and directories</title>
99 <para>Paths and file names referred to in the
100 documentation.</para>
102 <variablelist id='filenames' />
106 <title>Colophon</title>
107 <para id='colophon' />
113 This index contains {count} entries in {sections} sections,
114 referring to {pages} individual manual pages.
117 def _extract_directives(directive_groups, formatting, page):
119 section = t.find('./refmeta/manvolnum').text
120 pagename = t.find('./refmeta/refentrytitle').text
122 storopt = directive_groups['options']
123 for variablelist in t.iterfind('.//variablelist'):
124 klass = variablelist.attrib.get('class')
125 storvar = directive_groups[klass or 'miscellaneous']
126 # <option>s go in OPTIONS, unless class is specified
127 for xpath, stor in (('./varlistentry/term/varname', storvar),
128 ('./varlistentry/term/option',
129 storvar if klass else storopt)):
130 for name in variablelist.iterfind(xpath):
131 text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
132 stor[text].append((pagename, section))
133 if text not in formatting:
134 # use element as formatted display
135 if name.text[-1] in '= ':
140 formatting[text] = name
142 storfile = directive_groups['filenames']
143 for xpath, absolute_only in (('.//refsynopsisdiv//filename', False),
144 ('.//refsynopsisdiv//command', False),
145 ('.//filename', True)):
146 for name in t.iterfind(xpath):
147 if absolute_only and not (name.text and name.text.startswith('/')):
149 if name.attrib.get('noindex'):
153 if name.text.endswith('*'):
154 name.text = name.text[:-1]
155 if not name.text.startswith('.'):
156 text = name.text.partition(' ')[0]
157 if text != name.text:
160 if text.endswith('/'):
162 storfile[text].append((pagename, section))
163 if text not in formatting:
164 # use element as formatted display
165 formatting[text] = name
167 text = ' '.join(name.itertext())
168 storfile[text].append((pagename, section))
169 formatting[text] = name
171 storfile = directive_groups['constants']
172 for name in t.iterfind('.//constant'):
173 if name.attrib.get('noindex'):
176 if name.text.startswith('('): # a cast, strip it
177 name.text = name.text.partition(' ')[2]
178 storfile[name.text].append((pagename, section))
179 formatting[name.text] = name
181 def _make_section(template, name, directives, formatting):
182 varlist = template.find(".//*[@id='{}']".format(name))
183 for varname, manpages in sorted(directives.items()):
184 entry = tree.SubElement(varlist, 'varlistentry')
185 term = tree.SubElement(entry, 'term')
186 display = deepcopy(formatting[varname])
189 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
192 for manpage, manvolume in sorted(set(manpages)):
195 b = tree.SubElement(para, 'citerefentry')
196 c = tree.SubElement(b, 'refentrytitle')
198 d = tree.SubElement(b, 'manvolnum')
202 def _make_colophon(template, groups):
207 for pagelist in group.values():
208 pages |= set(pagelist)
210 para = template.find(".//para[@id='colophon']")
211 para.text = COLOPHON.format(count=count,
212 sections=len(groups),
215 def _make_page(template, directive_groups, formatting):
216 """Create an XML tree from directive_groups.
219 'class': {'variable': [('manpage', 'manvolume'), ...],
224 for name, directives in directive_groups.items():
225 _make_section(template, name, directives, formatting)
227 _make_colophon(template, directive_groups.values())
231 def make_page(*xml_files):
232 "Extract directives from xml_files and return XML index tree."
233 template = tree.fromstring(TEMPLATE)
234 names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
235 directive_groups = {name:collections.defaultdict(list)
238 for page in xml_files:
240 _extract_directives(directive_groups, formatting, page)
242 raise ValueError("failed to process " + page)
244 return _make_page(template, directive_groups, formatting)
246 if __name__ == '__main__':
247 with open(sys.argv[1], 'wb') as f:
248 f.write(xml_print(make_page(*sys.argv[2:])))