Skip to content

Commit 8c93ad1

Browse files
authored
Use pathlib more (#197)
1 parent 04cc37b commit 8c93ad1

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

build_docs.py

+35-45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
2121
"""
2222

23+
from __future__ import annotations
24+
2325
from argparse import ArgumentParser
2426
from collections.abc import Sequence
2527
from contextlib import suppress, contextmanager
@@ -171,19 +173,15 @@ def picker_label(self):
171173
return f"pre ({self.name})"
172174
return self.name
173175

174-
def setup_indexsidebar(self, versions, dest_path):
176+
def setup_indexsidebar(self, versions: Sequence[Version], dest_path: Path):
175177
"""Build indexsidebar.html for Sphinx."""
176-
with open(
177-
HERE / "templates" / "indexsidebar.html", encoding="UTF-8"
178-
) as sidebar_template_file:
179-
sidebar_template = jinja2.Template(sidebar_template_file.read())
180-
with open(dest_path, "w", encoding="UTF-8") as sidebar_file:
181-
sidebar_file.write(
182-
sidebar_template.render(
183-
current_version=self,
184-
versions=versions[::-1],
185-
)
186-
)
178+
template_path = HERE / "templates" / "indexsidebar.html"
179+
template = jinja2.Template(template_path.read_text(encoding="UTF-8"))
180+
rendered_template = template.render(
181+
current_version=self,
182+
versions=versions[::-1],
183+
)
184+
dest_path.write_text(rendered_template, encoding="UTF-8")
187185

188186
@classmethod
189187
def from_json(cls, name, values):
@@ -374,19 +372,17 @@ def setup_switchers(
374372
languages_map = dict(sorted((l.tag, l.name) for l in languages if l.in_prod))
375373
versions_map = {v.name: v.picker_label for v in reversed(versions)}
376374

377-
with open(
378-
HERE / "templates" / "switchers.js", encoding="UTF-8"
379-
) as switchers_template_file:
380-
template = Template(switchers_template_file.read())
375+
switchers_template_file = HERE / "templates" / "switchers.js"
381376
switchers_path = html_root / "_static" / "switchers.js"
382-
switchers_path.write_text(
383-
template.safe_substitute(
384-
LANGUAGES=json.dumps(languages_map),
385-
VERSIONS=json.dumps(versions_map),
386-
),
387-
encoding="UTF-8",
377+
378+
template = Template(switchers_template_file.read_text(encoding="UTF-8"))
379+
rendered_template = template.safe_substitute(
380+
LANGUAGES=json.dumps(languages_map),
381+
VERSIONS=json.dumps(versions_map),
388382
)
389-
for file in Path(html_root).glob("**/*.html"):
383+
switchers_path.write_text(rendered_template, encoding="UTF-8")
384+
385+
for file in html_root.glob("**/*.html"):
390386
depth = len(file.relative_to(html_root).parts) - 1
391387
src = f"{'../' * depth}_static/switchers.js"
392388
script = f' <script type="text/javascript" src="{src}"></script>\n'
@@ -411,15 +407,13 @@ def build_robots_txt(
411407
if not www_root.exists():
412408
logging.info("Skipping robots.txt generation (www root does not even exist).")
413409
return
414-
robots_file = www_root / "robots.txt"
415-
with open(HERE / "templates" / "robots.txt", encoding="UTF-8") as template_file:
416-
template = jinja2.Template(template_file.read())
417-
with open(robots_file, "w", encoding="UTF-8") as robots_txt_file:
418-
robots_txt_file.write(
419-
template.render(languages=languages, versions=versions) + "\n"
420-
)
421-
robots_file.chmod(0o775)
422-
run(["chgrp", group, robots_file])
410+
template_path = HERE / "templates" / "robots.txt"
411+
template = jinja2.Template(template_path.read_text(encoding="UTF-8"))
412+
rendered_template = template.render(languages=languages, versions=versions)
413+
robots_path = www_root / "robots.txt"
414+
robots_path.write_text(rendered_template + "\n", encoding="UTF-8")
415+
robots_path.chmod(0o775)
416+
run(["chgrp", group, robots_path])
423417
if not skip_cache_invalidation:
424418
purge(http, "robots.txt")
425419

@@ -431,14 +425,13 @@ def build_sitemap(
431425
if not www_root.exists():
432426
logging.info("Skipping sitemap generation (www root does not even exist).")
433427
return
434-
with open(HERE / "templates" / "sitemap.xml", encoding="UTF-8") as template_file:
435-
template = jinja2.Template(template_file.read())
436-
sitemap_file = www_root / "sitemap.xml"
437-
sitemap_file.write_text(
438-
template.render(languages=languages, versions=versions) + "\n", encoding="UTF-8"
439-
)
440-
sitemap_file.chmod(0o664)
441-
run(["chgrp", group, sitemap_file])
428+
template_path = HERE / "templates" / "sitemap.xml"
429+
template = jinja2.Template(template_path.read_text(encoding="UTF-8"))
430+
rendered_template = template.render(languages=languages, versions=versions)
431+
sitemap_path = www_root / "sitemap.xml"
432+
sitemap_path.write_text(rendered_template + "\n", encoding="UTF-8")
433+
sitemap_path.chmod(0o664)
434+
run(["chgrp", group, sitemap_path])
442435

443436

444437
def build_404(www_root: Path, group):
@@ -867,10 +860,7 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
867860
[
868861
"cp",
869862
"-a",
870-
*[
871-
str(dist)
872-
for dist in (Path(self.checkout) / "Doc" / "dist").glob("*")
873-
],
863+
*(self.checkout / "Doc" / "dist").glob("*"),
874864
target / "archives",
875865
]
876866
)
@@ -972,7 +962,7 @@ def symlink(
972962
directory_path = path / directory
973963
if not directory_path.exists():
974964
return # No touching link, dest doc not built yet.
975-
if link.exists() and readlink(str(link)) == directory:
965+
if link.exists() and readlink(link) == directory:
976966
return # Link is already pointing to right doc.
977967
if link.exists():
978968
link.unlink()

0 commit comments

Comments
 (0)