22# openslide-python - Python bindings for the OpenSlide library
33#
44# Copyright (c) 2014 Carnegie Mellon University
5+ # Copyright (c) 2024 Benjamin Gilbert
56#
67# This library is free software; you can redistribute it and/or modify it
78# under the terms of version 2.1 of the GNU Lesser General Public License
2627from __future__ import annotations
2728
2829import os
30+ from pathlib import Path
2931
3032from sphinx .application import Sphinx
3133from sphinx .util import logging
@@ -49,37 +51,33 @@ def remove_path_underscores(app: Sphinx, exception: Exception | None) -> None:
4951 logger = logging .getLogger (__name__ )
5052 logger .info (bold ('fixing pathnames... ' ), nonl = True )
5153 # 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 ):
5356 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 :
5860 contents = fh .read ()
5961 for old , new in DIRS .items ():
6062 contents = contents .replace (old + '/' , new + '/' )
6163 for old , new in FILES .items ():
6264 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 :
6466 fh .write (contents )
6567 # Move directory contents
6668 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 ()
7776 # Move files
7877 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 )
8381 logger .info ('done' )
8482
8583
0 commit comments