2
2
# openslide-python - Python bindings for the OpenSlide library
3
3
#
4
4
# Copyright (c) 2014 Carnegie Mellon University
5
+ # Copyright (c) 2024 Benjamin Gilbert
5
6
#
6
7
# This library is free software; you can redistribute it and/or modify it
7
8
# under the terms of version 2.1 of the GNU Lesser General Public License
26
27
from __future__ import annotations
27
28
28
29
import os
30
+ from pathlib import Path
29
31
30
32
from sphinx .application import Sphinx
31
33
from sphinx .util import logging
@@ -49,37 +51,33 @@ def remove_path_underscores(app: Sphinx, exception: Exception | None) -> None:
49
51
logger = logging .getLogger (__name__ )
50
52
logger .info (bold ('fixing pathnames... ' ), nonl = True )
51
53
# Rewrite references in HTML/JS files
52
- for dirpath , _ , filenames in os .walk (app .outdir ):
54
+ outdir = Path (app .outdir )
55
+ for dirpath , _ , filenames in os .walk (outdir ):
53
56
for filename in filenames :
54
- _ , ext = os .path .splitext (filename )
55
- if ext in REWRITE_EXTENSIONS :
56
- path = os .path .join (dirpath , filename )
57
- with open (path , encoding = 'utf-8' ) as fh :
57
+ path = Path (dirpath ) / filename
58
+ if path .suffix in REWRITE_EXTENSIONS :
59
+ with path .open (encoding = 'utf-8' ) as fh :
58
60
contents = fh .read ()
59
61
for old , new in DIRS .items ():
60
62
contents = contents .replace (old + '/' , new + '/' )
61
63
for old , new in FILES .items ():
62
64
contents = contents .replace (old , new )
63
- with open (path , 'w' , encoding = 'utf-8' ) as fh :
65
+ with path . open ('w' , encoding = 'utf-8' ) as fh :
64
66
fh .write (contents )
65
67
# Move directory contents
66
68
for old , new in DIRS .items ():
67
- olddir = os .path .join (app .outdir , old )
68
- newdir = os .path .join (app .outdir , new )
69
- if not os .path .exists (newdir ):
70
- os .mkdir (newdir )
71
- if os .path .isdir (olddir ):
72
- for filename in os .listdir (olddir ):
73
- oldfile = os .path .join (olddir , filename )
74
- newfile = os .path .join (newdir , filename )
75
- os .rename (oldfile , newfile )
76
- os .rmdir (olddir )
69
+ olddir = outdir / old
70
+ newdir = outdir / new
71
+ newdir .mkdir (exist_ok = True )
72
+ if olddir .is_dir ():
73
+ for oldfile in olddir .iterdir ():
74
+ oldfile .rename (newdir / oldfile .name )
75
+ olddir .rmdir ()
77
76
# Move files
78
77
for old , new in FILES .items ():
79
- oldfile = os .path .join (app .outdir , old )
80
- newfile = os .path .join (app .outdir , new )
81
- if os .path .isfile (oldfile ):
82
- os .rename (oldfile , newfile )
78
+ oldfile = outdir / old
79
+ if oldfile .is_file ():
80
+ oldfile .rename (outdir / new )
83
81
logger .info ('done' )
84
82
85
83
0 commit comments