1
1
"""Build Apple help books."""
2
2
3
+ from __future__ import annotations
4
+
3
5
import plistlib
4
6
import shlex
5
7
import subprocess
6
- from os import environ
7
- from os import path
8
- from subprocess import CalledProcessError , PIPE , STDOUT
9
- from typing import Any
8
+ from os import environ , path
9
+ from pathlib import Path
10
+ from subprocess import PIPE , STDOUT , CalledProcessError
11
+ from typing import TYPE_CHECKING
10
12
11
13
import sphinx
12
- from sphinx .application import Sphinx
13
14
from sphinx .builders .html import StandaloneHTMLBuilder
14
15
from sphinx .errors import SphinxError
15
16
from sphinx .locale import get_translation
18
19
from sphinx .util .matching import Matcher
19
20
from sphinx .util .osutil import ensuredir , make_filename
20
21
22
+ if TYPE_CHECKING :
23
+ from typing import Any
24
+
25
+ from sphinx .application import Sphinx
26
+
21
27
if sphinx .version_info [:2 ] >= (6 , 1 ):
22
28
from sphinx .util .display import SkipProgressMessage , progress_message
23
29
else :
24
- from sphinx .util import ( # type: ignore[attr-defined,no-redef]
25
- SkipProgressMessage , progress_message
30
+ from sphinx .util import ( # type: ignore[no-redef]
31
+ SkipProgressMessage ,
32
+ progress_message ,
26
33
)
27
34
28
35
__version__ = '1.0.8'
@@ -75,16 +82,17 @@ def init(self) -> None:
75
82
self .link_suffix = '.html'
76
83
77
84
if self .config .applehelp_bundle_id is None :
78
- raise SphinxError (__ ('You must set applehelp_bundle_id before '
79
- 'building Apple Help output' ))
85
+ msg = __ ('You must set applehelp_bundle_id '
86
+ 'before building Apple Help output' )
87
+ raise SphinxError (msg )
80
88
81
89
self .bundle_path = path .join (self .outdir , self .config .applehelp_bundle_name + '.help' )
82
- self .outdir = path . join ( # type: ignore[assignment]
90
+ self .outdir = type ( self . outdir )( Path (
83
91
self .bundle_path ,
84
92
'Contents' ,
85
93
'Resources' ,
86
94
self .config .applehelp_locale + '.lproj' ,
87
- )
95
+ ))
88
96
89
97
def handle_finish (self ) -> None :
90
98
super ().handle_finish ()
@@ -94,7 +102,7 @@ def handle_finish(self) -> None:
94
102
95
103
@progress_message (__ ('copying localized files' ))
96
104
def copy_localized_files (self ) -> None :
97
- source_dir = path .join (self .confdir , self .config .applehelp_locale + '.lproj' ) # type: ignore # NOQA
105
+ source_dir = path .join (self .confdir , self .config .applehelp_locale + '.lproj' )
98
106
target_dir = self .outdir
99
107
100
108
if path .isdir (source_dir ):
@@ -175,14 +183,14 @@ def build_helpindex(self, language_dir: str) -> None:
175
183
self .config .applehelp_indexer_path ,
176
184
'-Cf' ,
177
185
path .join (language_dir , 'search.helpindex' ),
178
- language_dir
186
+ language_dir ,
179
187
]
180
188
181
189
if self .config .applehelp_index_anchors is not None :
182
190
args .append ('-a' )
183
191
184
192
if self .config .applehelp_min_term_length is not None :
185
- args += ['-m' , '%s' % self .config .applehelp_min_term_length ]
193
+ args += ['-m' , f' { self .config .applehelp_min_term_length } ' ]
186
194
187
195
if self .config .applehelp_stopwords is not None :
188
196
args += ['-s' , self .config .applehelp_stopwords ]
@@ -196,18 +204,19 @@ def build_helpindex(self, language_dir: str) -> None:
196
204
else :
197
205
try :
198
206
subprocess .run (args , stdout = PIPE , stderr = STDOUT , check = True )
199
- except OSError :
200
- raise AppleHelpIndexerFailed (__ ('Command not found: %s' ) % args [0 ])
201
- except CalledProcessError as exc :
202
- raise AppleHelpIndexerFailed (exc .stdout )
207
+ except OSError as err :
208
+ msg = __ ('Command not found: %s' ) % args [0 ]
209
+ raise AppleHelpIndexerFailed (msg ) from err
210
+ except CalledProcessError as err :
211
+ raise AppleHelpIndexerFailed (err .stdout ) from err
203
212
204
213
@progress_message (__ ('signing help book' ))
205
214
def do_codesign (self ) -> None :
206
215
"""If we've been asked to, sign the bundle."""
207
216
args = [
208
217
self .config .applehelp_codesign_path ,
209
218
'-s' , self .config .applehelp_codesign_identity ,
210
- '-f'
219
+ '-f' ,
211
220
]
212
221
213
222
args += self .config .applehelp_codesign_flags
@@ -220,10 +229,11 @@ def do_codesign(self) -> None:
220
229
else :
221
230
try :
222
231
subprocess .run (args , stdout = PIPE , stderr = STDOUT , check = True )
223
- except OSError :
224
- raise AppleHelpCodeSigningFailed (__ ('Command not found: %s' ) % args [0 ])
225
- except CalledProcessError as exc :
226
- raise AppleHelpCodeSigningFailed (exc .stdout )
232
+ except OSError as err :
233
+ msg = __ ('Command not found: %s' ) % args [0 ]
234
+ raise AppleHelpCodeSigningFailed (msg ) from err
235
+ except CalledProcessError as err :
236
+ raise AppleHelpCodeSigningFailed (err .stdout ) from err
227
237
228
238
229
239
def setup (app : Sphinx ) -> dict [str , Any ]:
@@ -239,7 +249,7 @@ def setup(app: Sphinx) -> dict[str, Any]:
239
249
app .add_config_value ('applehelp_bundle_version' , '1' , 'applehelp' )
240
250
app .add_config_value ('applehelp_icon' , None , 'applehelp' , [str ])
241
251
app .add_config_value ('applehelp_kb_product' ,
242
- lambda self : '%s-%s' % ( make_filename (self .project ), self .release ) ,
252
+ lambda self : f' { make_filename (self .project )} - { self .release } ' ,
243
253
'applehelp' )
244
254
app .add_config_value ('applehelp_kb_url' , None , 'applehelp' , [str ])
245
255
app .add_config_value ('applehelp_remote_url' , None , 'applehelp' , [str ])
0 commit comments