Skip to content

STY: fstrings doc/make.py #30060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 5, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 35 additions & 46 deletions doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(
if single_doc and single_doc.endswith(".rst"):
self.single_doc_html = os.path.splitext(single_doc)[0] + ".html"
elif single_doc:
self.single_doc_html = "reference/api/pandas.{}.html".format(single_doc)
self.single_doc_html = f"reference/api/pandas.{single_doc}.html"

def _process_single_doc(self, single_doc):
"""
Expand All @@ -76,25 +76,23 @@ def _process_single_doc(self, single_doc):
if os.path.exists(os.path.join(SOURCE_PATH, single_doc)):
return single_doc
else:
raise FileNotFoundError("File {} not found".format(single_doc))
raise FileNotFoundError(f"File {single_doc} not found")

elif single_doc.startswith("pandas."):
try:
obj = pandas # noqa: F821
for name in single_doc.split("."):
obj = getattr(obj, name)
except AttributeError:
raise ImportError("Could not import {}".format(single_doc))
raise ImportError(f"Could not import {single_doc}")
else:
return single_doc[len("pandas.") :]
else:
raise ValueError(
(
"--single={} not understood. Value should be a "
"valid path to a .rst or .ipynb file, or a "
"valid pandas object (e.g. categorical.rst or "
"pandas.DataFrame.head)"
).format(single_doc)
f"--single={single_doc} not understood. "
f"Value should be a valid path to a .rst or .ipynb file, "
f"or a valid pandas object "
f"(e.g. categorical.rst or pandas.DataFrame.head)"
)

@staticmethod
Expand All @@ -113,7 +111,7 @@ def _run_os(*args):
"""
subprocess.check_call(args, stdout=sys.stdout, stderr=sys.stderr)

def _sphinx_build(self, kind):
def _sphinx_build(self, kind: str):
"""
Call sphinx to build documentation.

Expand All @@ -128,15 +126,15 @@ def _sphinx_build(self, kind):
>>> DocBuilder(num_jobs=4)._sphinx_build('html')
"""
if kind not in ("html", "latex"):
raise ValueError("kind must be html or latex, " "not {}".format(kind))
raise ValueError(f"kind must be html or latex, not {kind}")

cmd = ["sphinx-build", "-b", kind]
if self.num_jobs:
cmd += ["-j", str(self.num_jobs)]
if self.warnings_are_errors:
cmd += ["-W", "--keep-going"]
if self.verbosity:
cmd.append("-{}".format("v" * self.verbosity))
cmd.append(f"-{'v' * self.verbosity}")
cmd += [
"-d",
os.path.join(BUILD_PATH, "doctrees"),
Expand All @@ -156,7 +154,7 @@ def _get_page_title(self, page):
"""
Open the rst file `page` and extract its title.
"""
fname = os.path.join(SOURCE_PATH, "{}.rst".format(page))
fname = os.path.join(SOURCE_PATH, f"{page}.rst")
option_parser = docutils.frontend.OptionParser(
components=(docutils.parsers.rst.Parser,)
)
Expand Down Expand Up @@ -184,18 +182,6 @@ def _add_redirects(self):
Create in the build directory an html file with a redirect,
for every row in REDIRECTS_FILE.
"""
html = """
<html>
<head>
<meta http-equiv="refresh" content="0;URL={url}"/>
</head>
<body>
<p>
The page has been moved to <a href="{url}">{title}</a>
</p>
</body>
<html>
"""
with open(REDIRECTS_FILE) as mapping_fd:
reader = csv.reader(mapping_fd)
for row in reader:
Expand All @@ -214,15 +200,23 @@ def _add_redirects(self):

if os.path.exists(path):
raise RuntimeError(
("Redirection would overwrite an existing file: " "{}").format(
path
)
f"Redirection would overwrite an existing file: {path}"
)

with open(path, "w") as moved_page_fd:
moved_page_fd.write(
html.format(url="{}.html".format(row[1]), title=title)
)
html = f"""\
<html>
<head>
<meta http-equiv="refresh" content="0;URL={row[1]}.html"/>
</head>
<body>
<p>
The page has been moved to <a href="{row[1]}.html">{title}</a>
</p>
</body>
<html>"""

moved_page_fd.write(html)

def html(self):
"""
Expand Down Expand Up @@ -290,15 +284,14 @@ def zip_html(self):
def main():
cmds = [method for method in dir(DocBuilder) if not method.startswith("_")]

joined = ",".join(cmds)
argparser = argparse.ArgumentParser(
description="pandas documentation builder",
epilog="Commands: {}".format(",".join(cmds)),
description="pandas documentation builder", epilog=f"Commands: {joined}",
)

joined = ", ".join(cmds)
argparser.add_argument(
"command",
nargs="?",
default="html",
help="command to run: {}".format(", ".join(cmds)),
"command", nargs="?", default="html", help=f"command to run: {joined}",
)
argparser.add_argument(
"--num-jobs", type=int, default=0, help="number of jobs used by sphinx-build"
Expand All @@ -312,10 +305,9 @@ def main():
type=str,
default=None,
help=(
'filename (relative to the "source" folder)'
" of section or method name to compile, e.g. "
'"development/contributing.rst",'
' "ecosystem.rst", "pandas.DataFrame.join"'
"filename (relative to the 'source' folder) of section or method name to "
"compile, e.g. 'development/contributing.rst', "
"'ecosystem.rst', 'pandas.DataFrame.join'"
),
)
argparser.add_argument(
Expand All @@ -340,11 +332,8 @@ def main():
args = argparser.parse_args()

if args.command not in cmds:
raise ValueError(
"Unknown command {}. Available options: {}".format(
args.command, ", ".join(cmds)
)
)
joined = ", ".join(cmds)
raise ValueError(f"Unknown command {args.command}. Available options: {joined}")

# Below we update both os.environ and sys.path. The former is used by
# external libraries (namely Sphinx) to compile this module and resolve
Expand Down