20
20
21
21
"""
22
22
23
+ from __future__ import annotations
24
+
23
25
from argparse import ArgumentParser
24
26
from collections .abc import Sequence
25
27
from contextlib import suppress , contextmanager
@@ -171,19 +173,15 @@ def picker_label(self):
171
173
return f"pre ({ self .name } )"
172
174
return self .name
173
175
174
- def setup_indexsidebar (self , versions , dest_path ):
176
+ def setup_indexsidebar (self , versions : Sequence [ Version ] , dest_path : Path ):
175
177
"""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" )
187
185
188
186
@classmethod
189
187
def from_json (cls , name , values ):
@@ -374,19 +372,17 @@ def setup_switchers(
374
372
languages_map = dict (sorted ((l .tag , l .name ) for l in languages if l .in_prod ))
375
373
versions_map = {v .name : v .picker_label for v in reversed (versions )}
376
374
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"
381
376
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 ),
388
382
)
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" ):
390
386
depth = len (file .relative_to (html_root ).parts ) - 1
391
387
src = f"{ '../' * depth } _static/switchers.js"
392
388
script = f' <script type="text/javascript" src="{ src } "></script>\n '
@@ -411,15 +407,13 @@ def build_robots_txt(
411
407
if not www_root .exists ():
412
408
logging .info ("Skipping robots.txt generation (www root does not even exist)." )
413
409
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 ])
423
417
if not skip_cache_invalidation :
424
418
purge (http , "robots.txt" )
425
419
@@ -431,14 +425,13 @@ def build_sitemap(
431
425
if not www_root .exists ():
432
426
logging .info ("Skipping sitemap generation (www root does not even exist)." )
433
427
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 ])
442
435
443
436
444
437
def build_404 (www_root : Path , group ):
@@ -867,10 +860,7 @@ def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
867
860
[
868
861
"cp" ,
869
862
"-a" ,
870
- * [
871
- str (dist )
872
- for dist in (Path (self .checkout ) / "Doc" / "dist" ).glob ("*" )
873
- ],
863
+ * (self .checkout / "Doc" / "dist" ).glob ("*" ),
874
864
target / "archives" ,
875
865
]
876
866
)
@@ -972,7 +962,7 @@ def symlink(
972
962
directory_path = path / directory
973
963
if not directory_path .exists ():
974
964
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 :
976
966
return # Link is already pointing to right doc.
977
967
if link .exists ():
978
968
link .unlink ()
0 commit comments