1 # -*- coding: utf-8 -*-
4 import xml.etree.ElementTree as tree
7 <refentry id="systemd.directives">
10 <title>systemd.directives</title>
11 <productname>systemd</productname>
15 <contrib>Developer</contrib>
16 <firstname>Zbigniew</firstname>
17 <surname>Jędrzejewski-Szmek</surname>
18 <email>zbyszek@in.waw.pl</email>
24 <refentrytitle>systemd.directives</refentrytitle>
25 <manvolnum>5</manvolnum>
29 <refname>systemd.directives</refname>
30 <refpurpose>Index of configuration directives</refpurpose>
34 <title>Unit directives</title>
36 <para>Directives for configuring units, used in unit
39 <variablelist id='unit-directives' />
43 <title>System manager directives</title>
45 <para>Directives for configuring the behaviour of the
46 systemd process.</para>
48 <variablelist id='systemd-directives' />
52 <title>UDEV directives</title>
54 <para>Directives for configuring systemd units through the
57 <variablelist id='udev-directives' />
62 def _extract_directives(directive_groups, page):
64 section = t.find('./refmeta/manvolnum').text
65 pagename = t.find('./refmeta/refentrytitle').text
66 for variablelist in t.iterfind('.//variablelist'):
67 klass = variablelist.attrib.get('class') or 'unit-directives'
68 stor = directive_groups[klass]
69 for varname in variablelist.iterfind('./varlistentry/term/varname'):
70 text = ''.join(varname.text.partition('=')[:2])
71 stor[text].append((pagename, section))
73 def _make_section(refentry, name, directives):
74 varlist = refentry.find(".//*[@id='{}']".format(name))
75 for varname, manpages in sorted(directives.items()):
76 entry = tree.SubElement(varlist, 'varlistentry')
77 a = tree.SubElement(tree.SubElement(entry, 'term'), 'varname')
79 para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para')
82 for manpage, manvolume in sorted(manpages):
85 b = tree.SubElement(para, 'citerefentry')
86 c = tree.SubElement(b, 'refentrytitle')
88 d = tree.SubElement(b, 'manvolnum')
92 def _make_page(directive_groups):
93 """Create an XML tree from directive_groups.
96 'class': {'variable': [('manpage', 'manvolume'), ...],
101 refentry = tree.fromstring(TEMPLATE)
103 for name, directives in directive_groups.items():
104 _make_section(refentry, name, directives)
108 def make_page(xml_files):
109 "Extract directives from xml_files and return XML index tree."
110 directive_groups = {name:collections.defaultdict(list)
111 for name in ['unit-directives',
113 'systemd-directives',
115 for page in xml_files:
116 _extract_directives(directive_groups, page)
118 return _make_page(directive_groups)
120 if __name__ == '__main__':
121 tree.dump(make_page(sys.argv[1:]))