doc: genrest.py: Convert to use f-strings
Use f-strings instead of .format() to make the code easier to read, especially for long multiline strings. f-strings were added in Python 3.6, which Zephyr requires now. Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
This commit is contained in:
parent
2172e0d6d0
commit
178d3208da
1 changed files with 55 additions and 68 deletions
|
@ -34,7 +34,7 @@ def rst_link(sc):
|
||||||
if sc.nodes:
|
if sc.nodes:
|
||||||
# The "\ " avoids RST issues for !CONFIG_FOO -- see
|
# The "\ " avoids RST issues for !CONFIG_FOO -- see
|
||||||
# http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#character-level-inline-markup
|
# http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#character-level-inline-markup
|
||||||
return r"\ :option:`{0} <CONFIG_{0}>`".format(sc.name)
|
return fr"\ :option:`{sc.name} <CONFIG_{sc.name}>`"
|
||||||
|
|
||||||
elif isinstance(sc, kconfiglib.Choice):
|
elif isinstance(sc, kconfiglib.Choice):
|
||||||
# Choices appear as dependencies of choice symbols.
|
# Choices appear as dependencies of choice symbols.
|
||||||
|
@ -45,8 +45,7 @@ def rst_link(sc):
|
||||||
#
|
#
|
||||||
# Note that the first pair of <...> is non-syntactic here. We just display
|
# Note that the first pair of <...> is non-syntactic here. We just display
|
||||||
# choices links within <> in the documentation.
|
# choices links within <> in the documentation.
|
||||||
return r"\ :ref:`<{}> <{}>`" \
|
return fr"\ :ref:`<{choice_desc(sc)}> <{choice_id(sc)}>`"
|
||||||
.format(choice_desc(sc), choice_id(sc))
|
|
||||||
|
|
||||||
# Can't turn 'sc' into a link. Use the standard Kconfig format.
|
# Can't turn 'sc' into a link. Use the standard Kconfig format.
|
||||||
return kconfiglib.standard_sc_expr_str(sc)
|
return kconfiglib.standard_sc_expr_str(sc)
|
||||||
|
@ -124,16 +123,15 @@ def init():
|
||||||
elif len(spec_parts) == 4:
|
elif len(spec_parts) == 4:
|
||||||
title, suffix, path_s, desc_path = spec_parts
|
title, suffix, path_s, desc_path = spec_parts
|
||||||
else:
|
else:
|
||||||
sys.exit("error: --modules argument '{}' should have the format "
|
sys.exit(f"error: --modules argument '{module_spec}' should have "
|
||||||
"<title>,<suffix>,<path> or the format "
|
"the format <title>,<suffix>,<path> or the format "
|
||||||
"<title>,<suffix>,<path>,<index description filename>. "
|
"<title>,<suffix>,<path>,<index description filename>. "
|
||||||
"A doubled ',,' in any part is treated as a literal comma."
|
"A doubled ',,' in any part is treated as a literal "
|
||||||
.format(module_spec))
|
"comma.")
|
||||||
|
|
||||||
abspath = pathlib.Path(path_s).resolve()
|
abspath = pathlib.Path(path_s).resolve()
|
||||||
if not abspath.exists():
|
if not abspath.exists():
|
||||||
sys.exit("error: path '{}' in --modules argument does not exist"
|
sys.exit(f"error: path '{abspath}' in --modules argument does not exist")
|
||||||
.format(abspath))
|
|
||||||
|
|
||||||
modules.append((title, suffix, abspath, desc_path))
|
modules.append((title, suffix, abspath, desc_path))
|
||||||
|
|
||||||
|
@ -317,17 +315,17 @@ def write_module_index_pages():
|
||||||
rst += sym_table_rst("Configuration Options",
|
rst += sym_table_rst("Configuration Options",
|
||||||
module2syms[title])
|
module2syms[title])
|
||||||
|
|
||||||
write_if_updated("index-{}.rst".format(suffix), rst)
|
write_if_updated(f"index-{suffix}.rst", rst)
|
||||||
|
|
||||||
|
|
||||||
def sym_table_rst(title, syms):
|
def sym_table_rst(title, syms):
|
||||||
# Returns RST for the list of symbols on index pages. 'title' is the
|
# Returns RST for the list of symbols on index pages. 'title' is the
|
||||||
# heading to use.
|
# heading to use.
|
||||||
|
|
||||||
rst = """
|
rst = f"""
|
||||||
|
|
||||||
{}
|
{title}
|
||||||
{}
|
{len(title)*'*'}
|
||||||
|
|
||||||
.. list-table::
|
.. list-table::
|
||||||
:header-rows: 1
|
:header-rows: 1
|
||||||
|
@ -335,13 +333,13 @@ def sym_table_rst(title, syms):
|
||||||
|
|
||||||
* - Symbol name
|
* - Symbol name
|
||||||
- Help/prompt
|
- Help/prompt
|
||||||
""".format(title, len(title)*"*")
|
"""
|
||||||
|
|
||||||
for sym in sorted(syms, key=attrgetter("name")):
|
for sym in sorted(syms, key=attrgetter("name")):
|
||||||
rst += """\
|
rst += f"""\
|
||||||
* - :option:`CONFIG_{}`
|
* - :option:`CONFIG_{sym.name}`
|
||||||
- {}
|
- {sym_index_desc(sym)}
|
||||||
""".format(sym.name, sym_index_desc(sym))
|
"""
|
||||||
|
|
||||||
return rst
|
return rst
|
||||||
|
|
||||||
|
@ -383,23 +381,20 @@ def index_header(title, link, desc_path):
|
||||||
with open(desc_path, encoding="utf-8") as f:
|
with open(desc_path, encoding="utf-8") as f:
|
||||||
desc = f.read()
|
desc = f.read()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
sys.exit("error: failed to open index description file '{}': {}"
|
sys.exit("error: failed to open index description file "
|
||||||
.format(desc_path, e))
|
f"'{desc_path}': {e}")
|
||||||
|
|
||||||
return """\
|
return f"""\
|
||||||
.. _{}:
|
.. _{link}:
|
||||||
|
|
||||||
{}
|
{title}
|
||||||
{}
|
{len(title)*'='}
|
||||||
|
|
||||||
{}
|
{desc}
|
||||||
|
|
||||||
This documentation is generated automatically from the :file:`Kconfig` files by
|
This documentation is generated automatically from the :file:`Kconfig` files by
|
||||||
the :file:`{}` script. Click on symbols for more information.""".format(
|
the :file:`{os.path.basename(__file__)}` script. Click on symbols for more
|
||||||
link,
|
information."""
|
||||||
title, len(title)*"=",
|
|
||||||
desc,
|
|
||||||
os.path.basename(__file__))
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_INDEX_DESC = """\
|
DEFAULT_INDEX_DESC = """\
|
||||||
|
@ -429,7 +424,7 @@ def write_dummy_syms_page():
|
||||||
|
|
||||||
rst = ":orphan:\n\nDummy symbols page for turbo mode.\n\n"
|
rst = ":orphan:\n\nDummy symbols page for turbo mode.\n\n"
|
||||||
for sym in kconf.unique_defined_syms:
|
for sym in kconf.unique_defined_syms:
|
||||||
rst += ".. option:: CONFIG_{}\n".format(sym.name)
|
rst += f".. option:: CONFIG_{sym.name}\n"
|
||||||
|
|
||||||
write_if_updated("dummy-syms.rst", rst)
|
write_if_updated("dummy-syms.rst", rst)
|
||||||
|
|
||||||
|
@ -437,7 +432,7 @@ def write_dummy_syms_page():
|
||||||
def write_sym_page(sym):
|
def write_sym_page(sym):
|
||||||
# Writes documentation for 'sym' to <out_dir>/CONFIG_<sym.name>.rst
|
# Writes documentation for 'sym' to <out_dir>/CONFIG_<sym.name>.rst
|
||||||
|
|
||||||
write_if_updated("CONFIG_{}.rst".format(sym.name),
|
write_if_updated(f"CONFIG_{sym.name}.rst",
|
||||||
sym_header_rst(sym) +
|
sym_header_rst(sym) +
|
||||||
help_rst(sym) +
|
help_rst(sym) +
|
||||||
direct_deps_rst(sym) +
|
direct_deps_rst(sym) +
|
||||||
|
@ -470,31 +465,27 @@ def sym_header_rst(sym):
|
||||||
# - '.. title::' sets the title of the document (e.g. <title>). This seems
|
# - '.. title::' sets the title of the document (e.g. <title>). This seems
|
||||||
# to be poorly documented at the moment.
|
# to be poorly documented at the moment.
|
||||||
return ":orphan:\n\n" \
|
return ":orphan:\n\n" \
|
||||||
".. title:: {0}\n\n" \
|
f".. title:: {sym.name}\n\n" \
|
||||||
".. option:: CONFIG_{0}\n\n" \
|
f".. option:: CONFIG_{sym.name}\n\n" \
|
||||||
"{1}\n\n" \
|
f"{prompt_rst(sym)}\n\n" \
|
||||||
"Type: ``{2}``\n\n" \
|
f"Type: ``{kconfiglib.TYPE_TO_STR[sym.type]}``\n\n"
|
||||||
.format(sym.name, prompt_rst(sym),
|
|
||||||
kconfiglib.TYPE_TO_STR[sym.type])
|
|
||||||
|
|
||||||
|
|
||||||
def choice_header_rst(choice):
|
def choice_header_rst(choice):
|
||||||
# Returns RST that appears at the top of choice reference pages
|
# Returns RST that appears at the top of choice reference pages
|
||||||
|
|
||||||
return ":orphan:\n\n" \
|
return ":orphan:\n\n" \
|
||||||
".. title:: {0}\n\n" \
|
f".. title:: {choice_desc(choice)}\n\n" \
|
||||||
".. _{1}:\n\n" \
|
f".. _{choice_id(choice)}:\n\n" \
|
||||||
".. describe:: {0}\n\n" \
|
f".. describe:: {choice_desc(choice)}\n\n" \
|
||||||
"{2}\n\n" \
|
f"{prompt_rst(choice)}\n\n" \
|
||||||
"Type: ``{3}``\n\n" \
|
f"Type: ``{kconfiglib.TYPE_TO_STR[choice.type]}``\n\n"
|
||||||
.format(choice_desc(choice), choice_id(choice),
|
|
||||||
prompt_rst(choice), kconfiglib.TYPE_TO_STR[choice.type])
|
|
||||||
|
|
||||||
|
|
||||||
def prompt_rst(sc):
|
def prompt_rst(sc):
|
||||||
# Returns RST that lists the prompts of 'sc' (symbol or choice)
|
# Returns RST that lists the prompts of 'sc' (symbol or choice)
|
||||||
|
|
||||||
return "\n\n".join("*{}*".format(node.prompt[0])
|
return "\n\n".join(f"*{node.prompt[0]}*"
|
||||||
for node in sc.nodes if node.prompt) \
|
for node in sc.nodes if node.prompt) \
|
||||||
or "*(No prompt -- not directly user assignable.)*"
|
or "*(No prompt -- not directly user assignable.)*"
|
||||||
|
|
||||||
|
@ -510,8 +501,7 @@ def help_rst(sc):
|
||||||
if node.help is not None:
|
if node.help is not None:
|
||||||
rst += "Help\n" \
|
rst += "Help\n" \
|
||||||
"====\n\n" \
|
"====\n\n" \
|
||||||
"{}\n\n" \
|
f"{node.help}\n\n"
|
||||||
.format(node.help)
|
|
||||||
|
|
||||||
return rst
|
return rst
|
||||||
|
|
||||||
|
@ -524,9 +514,8 @@ def direct_deps_rst(sc):
|
||||||
|
|
||||||
return "Direct dependencies\n" \
|
return "Direct dependencies\n" \
|
||||||
"===================\n\n" \
|
"===================\n\n" \
|
||||||
"{}\n\n" \
|
f"{expr_str(sc.direct_dep)}\n\n" \
|
||||||
"*(Includes any dependencies from ifs and menus.)*\n\n" \
|
"*(Includes any dependencies from ifs and menus.)*\n\n"
|
||||||
.format(expr_str(sc.direct_dep))
|
|
||||||
|
|
||||||
|
|
||||||
def defaults_rst(sc):
|
def defaults_rst(sc):
|
||||||
|
@ -541,7 +530,7 @@ def defaults_rst(sc):
|
||||||
heading = "Default"
|
heading = "Default"
|
||||||
if len(sc.defaults) != 1:
|
if len(sc.defaults) != 1:
|
||||||
heading += "s"
|
heading += "s"
|
||||||
rst = "{}\n{}\n\n".format(heading, len(heading)*"=")
|
rst = f"{heading}\n{len(heading)*'='}\n\n"
|
||||||
|
|
||||||
if sc.defaults:
|
if sc.defaults:
|
||||||
for value, cond in sc.orig_defaults:
|
for value, cond in sc.orig_defaults:
|
||||||
|
@ -574,7 +563,7 @@ def choice_syms_rst(choice):
|
||||||
|
|
||||||
for sym in choice.syms:
|
for sym in choice.syms:
|
||||||
# Generates a link
|
# Generates a link
|
||||||
rst += "- {}\n".format(expr_str(sym))
|
rst += f"- {expr_str(sym)}\n"
|
||||||
|
|
||||||
return rst + "\n"
|
return rst + "\n"
|
||||||
|
|
||||||
|
@ -592,8 +581,8 @@ def select_imply_rst(sym):
|
||||||
nonlocal rst
|
nonlocal rst
|
||||||
|
|
||||||
if lst:
|
if lst:
|
||||||
heading = "Symbols {} by this symbol".format(type_str)
|
heading = f"Symbols {type_str} by this symbol"
|
||||||
rst += "{}\n{}\n\n".format(heading, len(heading)*"=")
|
rst += f"{heading}\n{len(heading)*'='}\n\n"
|
||||||
|
|
||||||
for select, cond in lst:
|
for select, cond in lst:
|
||||||
rst += "- " + rst_link(select)
|
rst += "- " + rst_link(select)
|
||||||
|
@ -625,8 +614,8 @@ def selecting_implying_rst(sym):
|
||||||
nonlocal rst
|
nonlocal rst
|
||||||
|
|
||||||
if expr is not sym.kconfig.n:
|
if expr is not sym.kconfig.n:
|
||||||
heading = "Symbols that {} this symbol".format(type_str)
|
heading = f"Symbols that {type_str} this symbol"
|
||||||
rst += "{}\n{}\n\n".format(heading, len(heading)*"=")
|
rst += f"{heading}\n{len(heading)*'='}\n\n"
|
||||||
|
|
||||||
# The reverse dependencies from each select/imply are ORed together
|
# The reverse dependencies from each select/imply are ORed together
|
||||||
for select in kconfiglib.split_expr(expr, kconfiglib.OR):
|
for select in kconfiglib.split_expr(expr, kconfiglib.OR):
|
||||||
|
@ -661,7 +650,7 @@ def kconfig_definition_rst(sc):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
return "Included via {}\n\n".format(
|
return "Included via {}\n\n".format(
|
||||||
arrow.join("``{}:{}``".format(strip_module_path(filename), linenr)
|
arrow.join(f"``{strip_module_path(filename)}:{linenr}``"
|
||||||
for filename, linenr in node.include_path))
|
for filename, linenr in node.include_path))
|
||||||
|
|
||||||
def menu_path(node):
|
def menu_path(node):
|
||||||
|
@ -683,19 +672,17 @@ def kconfig_definition_rst(sc):
|
||||||
|
|
||||||
heading = "Kconfig definition"
|
heading = "Kconfig definition"
|
||||||
if len(sc.nodes) > 1: heading += "s"
|
if len(sc.nodes) > 1: heading += "s"
|
||||||
rst = "{}\n{}\n\n".format(heading, len(heading)*"=")
|
rst = f"{heading}\n{len(heading)*'='}\n\n"
|
||||||
|
|
||||||
rst += ".. highlight:: kconfig"
|
rst += ".. highlight:: kconfig"
|
||||||
|
|
||||||
for node in sc.nodes:
|
for node in sc.nodes:
|
||||||
rst += "\n\n" \
|
rst += "\n\n" \
|
||||||
"At ``{}:{}``\n\n" \
|
f"At ``{strip_module_path(node.filename)}:{node.linenr}``\n\n" \
|
||||||
"{}" \
|
f"{include_path(node)}" \
|
||||||
"Menu path: {}\n\n" \
|
f"Menu path: {menu_path(node)}\n\n" \
|
||||||
".. parsed-literal::\n\n{}" \
|
".. parsed-literal::\n\n" \
|
||||||
.format(strip_module_path(node.filename), node.linenr,
|
f"{textwrap.indent(node.custom_str(rst_link), 4*' ')}"
|
||||||
include_path(node), menu_path(node),
|
|
||||||
textwrap.indent(node.custom_str(rst_link), 4*" "))
|
|
||||||
|
|
||||||
# Not the last node?
|
# Not the last node?
|
||||||
if node is not sc.nodes[-1]:
|
if node is not sc.nodes[-1]:
|
||||||
|
@ -718,7 +705,7 @@ def choice_id(choice):
|
||||||
# we can't use that, and the prompt isn't guaranteed to be unique.
|
# we can't use that, and the prompt isn't guaranteed to be unique.
|
||||||
|
|
||||||
# Pretty slow, but fast enough
|
# Pretty slow, but fast enough
|
||||||
return "choice_{}".format(choice.kconfig.unique_choices.index(choice))
|
return f"choice_{choice.kconfig.unique_choices.index(choice)}"
|
||||||
|
|
||||||
|
|
||||||
def choice_desc(choice):
|
def choice_desc(choice):
|
||||||
|
@ -775,7 +762,7 @@ def strip_module_path(path):
|
||||||
# Not within the module
|
# Not within the module
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return "<{}>{}{}".format(title, os.path.sep, relpath)
|
return f"<{title}>{os.path.sep}{relpath}"
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue