7
7
import os .path
8
8
import re
9
9
import time
10
+ from pathlib import Path
10
11
from typing import TYPE_CHECKING , NamedTuple
11
12
from urllib .parse import quote
12
13
from zipfile import ZIP_DEFLATED , ZIP_STORED , ZipFile
19
20
from sphinx .builders .html ._build_info import BuildInfo
20
21
from sphinx .locale import __
21
22
from sphinx .util import logging
23
+ from sphinx .util ._pathlib import _StrPath
22
24
from sphinx .util .display import status_iterator
23
25
from sphinx .util .fileutil import copy_asset_file
24
26
from sphinx .util .osutil import copyfile , ensuredir , relpath
25
27
26
28
if TYPE_CHECKING :
27
- from pathlib import Path
28
29
from typing import Any
29
30
30
31
from docutils .nodes import Element , Node
@@ -158,7 +159,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
158
159
guide_titles = GUIDE_TITLES
159
160
media_types = MEDIA_TYPES
160
161
refuri_re = REFURI_RE
161
- template_dir = ''
162
+ template_dir : _StrPath = _StrPath ()
162
163
doctype = ''
163
164
164
165
def init (self ) -> None :
@@ -417,7 +418,7 @@ def copy_image_files_pil(self) -> None:
417
418
The method tries to read and write the files with Pillow, converting
418
419
the format and resizing the image if necessary/possible.
419
420
"""
420
- ensuredir (os . path . join ( self .outdir , self .imagedir ) )
421
+ ensuredir (self .outdir / self .imagedir )
421
422
for src in status_iterator (
422
423
self .images ,
423
424
__ ('copying images... ' ),
@@ -427,12 +428,12 @@ def copy_image_files_pil(self) -> None:
427
428
):
428
429
dest = self .images [src ]
429
430
try :
430
- img = Image .open (os . path . join ( self .srcdir , src ) )
431
+ img = Image .open (self .srcdir / src )
431
432
except OSError :
432
433
if not self .is_vector_graphics (src ):
433
434
logger .warning (
434
435
__ ('cannot read image file %r: copying it instead' ),
435
- os . path . join ( self .srcdir , src ) ,
436
+ self .srcdir / src ,
436
437
)
437
438
try :
438
439
copyfile (
@@ -443,7 +444,7 @@ def copy_image_files_pil(self) -> None:
443
444
except OSError as err :
444
445
logger .warning (
445
446
__ ('cannot copy image file %r: %s' ),
446
- os . path . join ( self .srcdir , src ) ,
447
+ self .srcdir / src ,
447
448
err ,
448
449
)
449
450
continue
@@ -459,11 +460,11 @@ def copy_image_files_pil(self) -> None:
459
460
nh = round ((height * nw ) / width )
460
461
img = img .resize ((nw , nh ), Image .BICUBIC )
461
462
try :
462
- img .save (os . path . join ( self .outdir , self .imagedir , dest ) )
463
+ img .save (self .outdir / self .imagedir / dest )
463
464
except OSError as err :
464
465
logger .warning (
465
466
__ ('cannot write image file %r: %s' ),
466
- os . path . join ( self .srcdir , src ) ,
467
+ self .srcdir / src ,
467
468
err ,
468
469
)
469
470
@@ -511,7 +512,7 @@ def build_mimetype(self) -> None:
511
512
"""Write the metainfo file mimetype."""
512
513
logger .info (__ ('writing mimetype file...' ))
513
514
copyfile (
514
- os . path . join ( self .template_dir , 'mimetype' ) ,
515
+ self .template_dir / 'mimetype' ,
515
516
self .outdir / 'mimetype' ,
516
517
force = True ,
517
518
)
@@ -522,7 +523,7 @@ def build_container(self, outname: str = 'META-INF/container.xml') -> None:
522
523
outdir = self .outdir / 'META-INF'
523
524
ensuredir (outdir )
524
525
copyfile (
525
- os . path . join ( self .template_dir , 'container.xml' ) ,
526
+ self .template_dir / 'container.xml' ,
526
527
outdir / 'container.xml' ,
527
528
force = True ,
528
529
)
@@ -577,9 +578,10 @@ def build_content(self) -> None:
577
578
if not self .use_index :
578
579
self .ignored_files .append ('genindex' + self .out_suffix )
579
580
for root , dirs , files in os .walk (self .outdir ):
581
+ root_path = Path (root )
580
582
dirs .sort ()
581
583
for fn in sorted (files ):
582
- filename = relpath (os . path . join ( root , fn ) , self .outdir )
584
+ filename = relpath (root_path / fn , self .outdir )
583
585
if filename in self .ignored_files :
584
586
continue
585
587
ext = os .path .splitext (filename )[- 1 ]
@@ -684,7 +686,7 @@ def build_content(self) -> None:
684
686
685
687
# write the project file
686
688
copy_asset_file (
687
- os . path . join ( self .template_dir , 'content.opf.jinja' ) ,
689
+ self .template_dir / 'content.opf.jinja' ,
688
690
self .outdir ,
689
691
context = metadata ,
690
692
force = True ,
@@ -778,7 +780,7 @@ def build_toc(self) -> None:
778
780
level = max (item ['level' ] for item in self .refnodes )
779
781
level = min (level , self .config .epub_tocdepth )
780
782
copy_asset_file (
781
- os . path . join ( self .template_dir , 'toc.ncx.jinja' ) ,
783
+ self .template_dir / 'toc.ncx.jinja' ,
782
784
self .outdir ,
783
785
context = self .toc_metadata (level , navpoints ),
784
786
force = True ,
@@ -792,10 +794,10 @@ def build_epub(self) -> None:
792
794
"""
793
795
outname = self .config .epub_basename + '.epub'
794
796
logger .info (__ ('writing %s file...' ), outname )
795
- epub_filename = os . path . join ( self .outdir , outname )
797
+ epub_filename = self .outdir / outname
796
798
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 )
798
800
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 )
800
802
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