make-directive-index: apply "ruff format"

This commit is contained in:
Yu Watanabe
2026-05-18 02:44:47 +09:00
parent e0ac581a8b
commit 8b24db05d4
+22 -16
View File
@@ -13,6 +13,7 @@ This index contains {count} entries in {sections} sections,
referring to {pages} individual manual pages. referring to {pages} individual manual pages.
''' '''
def _extract_directives(directive_groups, formatting, page): def _extract_directives(directive_groups, formatting, page):
t = xml_parse(page) t = xml_parse(page)
section = t.find('./refmeta/manvolnum').text section = t.find('./refmeta/manvolnum').text
@@ -21,12 +22,13 @@ def _extract_directives(directive_groups, formatting, page):
storopt = directive_groups['options'] storopt = directive_groups['options']
for variablelist in t.iterfind('.//variablelist'): for variablelist in t.iterfind('.//variablelist'):
klass = variablelist.attrib.get('class') klass = variablelist.attrib.get('class')
searchpath = variablelist.attrib.get('xpath','./varlistentry/term/varname') searchpath = variablelist.attrib.get('xpath', './varlistentry/term/varname')
storvar = directive_groups[klass or 'miscellaneous'] storvar = directive_groups[klass or 'miscellaneous']
# <option>s go in OPTIONS, unless class is specified # <option>s go in OPTIONS, unless class is specified
for xpath, stor in ((searchpath, storvar), for xpath, stor in (
('./varlistentry/term/option', (searchpath, storvar),
storvar if klass else storopt)): ('./varlistentry/term/option', storvar if klass else storopt),
):
for name in variablelist.iterfind(xpath): for name in variablelist.iterfind(xpath):
text = re.sub(r'([= ]).*', r'\1', name.text).rstrip() text = re.sub(r'([= ]).*', r'\1', name.text).rstrip()
if text.startswith('-'): if text.startswith('-'):
@@ -45,14 +47,16 @@ def _extract_directives(directive_groups, formatting, page):
if extra: if extra:
stor[extra].append((pagename, section)) stor[extra].append((pagename, section))
if extra not in formatting: if extra not in formatting:
elt = tree.Element("varname") elt = tree.Element('varname')
elt.text= extra elt.text = extra
formatting[extra] = elt formatting[extra] = elt
storfile = directive_groups['filenames'] storfile = directive_groups['filenames']
for xpath, absolute_only in (('.//refsynopsisdiv//filename', False), for xpath, absolute_only in (
('.//refsynopsisdiv//command', False), ('.//refsynopsisdiv//filename', False),
('.//filename', True)): ('.//refsynopsisdiv//command', False),
('.//filename', True),
):
for name in t.iterfind(xpath): for name in t.iterfind(xpath):
if absolute_only and not (name.text and name.text.startswith('/')): if absolute_only and not (name.text and name.text.startswith('/')):
continue continue
@@ -82,7 +86,7 @@ def _extract_directives(directive_groups, formatting, page):
if name.attrib.get('index') == 'false': if name.attrib.get('index') == 'false':
continue continue
name.tail = '' name.tail = ''
if name.text.startswith('('): # a cast, strip it if name.text.startswith('('): # a cast, strip it
name.text = name.text.partition(' ')[2] name.text = name.text.partition(' ')[2]
klass = name.attrib.get('class') or 'constants' klass = name.attrib.get('class') or 'constants'
storfile = directive_groups[klass] storfile = directive_groups[klass]
@@ -101,6 +105,7 @@ def _extract_directives(directive_groups, formatting, page):
storfile[name.text].append((pagename, section)) storfile[name.text].append((pagename, section))
formatting[name.text] = name formatting[name.text] = name
def _make_section(template, name, directives, formatting): def _make_section(template, name, directives, formatting):
varlist = template.find(f".//*[@id='{name}']") varlist = template.find(f".//*[@id='{name}']")
for varname, manpages in sorted(directives.items()): for varname, manpages in sorted(directives.items()):
@@ -123,6 +128,7 @@ def _make_section(template, name, directives, formatting):
d.text = manvolume d.text = manvolume
entry.tail = '\n\n' entry.tail = '\n\n'
def _make_colophon(template, groups): def _make_colophon(template, groups):
count = 0 count = 0
pages = set() pages = set()
@@ -132,9 +138,8 @@ def _make_colophon(template, groups):
pages |= set(pagelist) pages |= set(pagelist)
para = template.find(".//para[@id='colophon']") para = template.find(".//para[@id='colophon']")
para.text = COLOPHON.format(count=count, para.text = COLOPHON.format(count=count, sections=len(groups), pages=len(pages))
sections=len(groups),
pages=len(pages))
def _make_page(template, directive_groups, formatting): def _make_page(template, directive_groups, formatting):
"""Create an XML tree from directive_groups. """Create an XML tree from directive_groups.
@@ -152,21 +157,22 @@ def _make_page(template, directive_groups, formatting):
return template return template
def make_page(template_path, xml_files): def make_page(template_path, xml_files):
"Extract directives from xml_files and return XML index tree." "Extract directives from xml_files and return XML index tree."
template = xml_parse(template_path) template = xml_parse(template_path)
names = [vl.get('id') for vl in template.iterfind('.//variablelist')] names = [vl.get('id') for vl in template.iterfind('.//variablelist')]
directive_groups = {name:collections.defaultdict(list) directive_groups = {name: collections.defaultdict(list) for name in names}
for name in names}
formatting = {} formatting = {}
for page in xml_files: for page in xml_files:
try: try:
_extract_directives(directive_groups, formatting, page) _extract_directives(directive_groups, formatting, page)
except Exception as e: except Exception as e:
raise ValueError("failed to process " + page) from e raise ValueError('failed to process ' + page) from e
return _make_page(template, directive_groups, formatting) return _make_page(template, directive_groups, formatting)
if __name__ == '__main__': if __name__ == '__main__':
with open(sys.argv[1], 'wb') as f: with open(sys.argv[1], 'wb') as f:
_template_path = sys.argv[2] _template_path = sys.argv[2]