77import os .path
88import re
99import time
10+ from pathlib import Path
1011from typing import TYPE_CHECKING , NamedTuple
1112from urllib .parse import quote
1213from zipfile import ZIP_DEFLATED , ZIP_STORED , ZipFile
1920from sphinx .builders .html ._build_info import BuildInfo
2021from sphinx .locale import __
2122from sphinx .util import logging
23+ from sphinx .util ._pathlib import _StrPath
2224from sphinx .util .display import status_iterator
2325from sphinx .util .fileutil import copy_asset_file
2426from sphinx .util .osutil import copyfile , ensuredir , relpath
2527
2628if TYPE_CHECKING :
27- from pathlib import Path
2829 from typing import Any
2930
3031 from docutils .nodes import Element , Node
@@ -158,7 +159,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
158159 guide_titles = GUIDE_TITLES
159160 media_types = MEDIA_TYPES
160161 refuri_re = REFURI_RE
161- template_dir = ''
162+ template_dir : _StrPath = _StrPath ()
162163 doctype = ''
163164
164165 def init (self ) -> None :
@@ -417,7 +418,7 @@ def copy_image_files_pil(self) -> None:
417418 The method tries to read and write the files with Pillow, converting
418419 the format and resizing the image if necessary/possible.
419420 """
420- ensuredir (os . path . join ( self .outdir , self .imagedir ) )
421+ ensuredir (self .outdir / self .imagedir )
421422 for src in status_iterator (
422423 self .images ,
423424 __ ('copying images... ' ),
@@ -427,12 +428,12 @@ def copy_image_files_pil(self) -> None:
427428 ):
428429 dest = self .images [src ]
429430 try :
430- img = Image .open (os . path . join ( self .srcdir , src ) )
431+ img = Image .open (self .srcdir / src )
431432 except OSError :
432433 if not self .is_vector_graphics (src ):
433434 logger .warning (
434435 __ ('cannot read image file %r: copying it instead' ),
435- os . path . join ( self .srcdir , src ) ,
436+ self .srcdir / src ,
436437 )
437438 try :
438439 copyfile (
@@ -443,7 +444,7 @@ def copy_image_files_pil(self) -> None:
443444 except OSError as err :
444445 logger .warning (
445446 __ ('cannot copy image file %r: %s' ),
446- os . path . join ( self .srcdir , src ) ,
447+ self .srcdir / src ,
447448 err ,
448449 )
449450 continue
@@ -459,11 +460,11 @@ def copy_image_files_pil(self) -> None:
459460 nh = round ((height * nw ) / width )
460461 img = img .resize ((nw , nh ), Image .BICUBIC )
461462 try :
462- img .save (os . path . join ( self .outdir , self .imagedir , dest ) )
463+ img .save (self .outdir / self .imagedir / dest )
463464 except OSError as err :
464465 logger .warning (
465466 __ ('cannot write image file %r: %s' ),
466- os . path . join ( self .srcdir , src ) ,
467+ self .srcdir / src ,
467468 err ,
468469 )
469470
@@ -511,7 +512,7 @@ def build_mimetype(self) -> None:
511512 """Write the metainfo file mimetype."""
512513 logger .info (__ ('writing mimetype file...' ))
513514 copyfile (
514- os . path . join ( self .template_dir , 'mimetype' ) ,
515+ self .template_dir / 'mimetype' ,
515516 self .outdir / 'mimetype' ,
516517 force = True ,
517518 )
@@ -522,7 +523,7 @@ def build_container(self, outname: str = 'META-INF/container.xml') -> None:
522523 outdir = self .outdir / 'META-INF'
523524 ensuredir (outdir )
524525 copyfile (
525- os . path . join ( self .template_dir , 'container.xml' ) ,
526+ self .template_dir / 'container.xml' ,
526527 outdir / 'container.xml' ,
527528 force = True ,
528529 )
@@ -577,9 +578,10 @@ def build_content(self) -> None:
577578 if not self .use_index :
578579 self .ignored_files .append ('genindex' + self .out_suffix )
579580 for root , dirs , files in os .walk (self .outdir ):
581+ root_path = Path (root )
580582 dirs .sort ()
581583 for fn in sorted (files ):
582- filename = relpath (os . path . join ( root , fn ) , self .outdir )
584+ filename = relpath (root_path / fn , self .outdir )
583585 if filename in self .ignored_files :
584586 continue
585587 ext = os .path .splitext (filename )[- 1 ]
@@ -684,7 +686,7 @@ def build_content(self) -> None:
684686
685687 # write the project file
686688 copy_asset_file (
687- os . path . join ( self .template_dir , 'content.opf.jinja' ) ,
689+ self .template_dir / 'content.opf.jinja' ,
688690 self .outdir ,
689691 context = metadata ,
690692 force = True ,
@@ -778,7 +780,7 @@ def build_toc(self) -> None:
778780 level = max (item ['level' ] for item in self .refnodes )
779781 level = min (level , self .config .epub_tocdepth )
780782 copy_asset_file (
781- os . path . join ( self .template_dir , 'toc.ncx.jinja' ) ,
783+ self .template_dir / 'toc.ncx.jinja' ,
782784 self .outdir ,
783785 context = self .toc_metadata (level , navpoints ),
784786 force = True ,
@@ -792,10 +794,10 @@ def build_epub(self) -> None:
792794 """
793795 outname = self .config .epub_basename + '.epub'
794796 logger .info (__ ('writing %s file...' ), outname )
795- epub_filename = os . path . join ( self .outdir , outname )
797+ epub_filename = self .outdir / outname
796798 with ZipFile (epub_filename , 'w' , ZIP_DEFLATED ) as epub :
797- epub .write (os . path . join ( self .outdir , 'mimetype' ) , 'mimetype' , ZIP_STORED )
799+ epub .write (self .outdir / 'mimetype' , 'mimetype' , ZIP_STORED )
798800 for filename in ('META-INF/container.xml' , 'content.opf' , 'toc.ncx' ):
799- epub .write (os . path . join ( self .outdir , filename ) , filename , ZIP_DEFLATED )
801+ epub .write (self .outdir / filename , filename , ZIP_DEFLATED )
800802 for filename in self .files :
801- epub .write (os . path . join ( self .outdir , filename ) , filename , ZIP_DEFLATED )
803+ epub .write (self .outdir / filename , filename , ZIP_DEFLATED )
0 commit comments