1
+ import argparse
1
2
import importlib
2
3
import inspect
3
- import optparse
4
4
import os
5
5
import pkgutil
6
6
import re
7
7
import shutil
8
8
import textwrap
9
- import sys
10
9
11
10
pj = os .path .join
12
11
@@ -285,138 +284,132 @@ def main():
285
284
"""
286
285
)
287
286
288
- parser = optparse .OptionParser (
289
- usage = textwrap .dedent (
290
- """\
291
- usage: %prog [options] <package>
287
+ parser = argparse .ArgumentParser ()
292
288
293
- Arguments:
294
- package : The name of the package to walk (e.g 'scipy')
295
- """
296
- )
289
+ parser .add_argument (
290
+ "package" , help = "The name of the package to walk (e.g. 'scipy')"
297
291
)
298
292
299
- parser .add_option (
293
+ parser .add_argument (
300
294
"-s" ,
301
295
"--source" ,
302
296
action = "store" ,
303
- help = "sphinx source dir below which all rst files will \
304
- be written [%default] " ,
297
+ help = """ sphinx source dir below which all rst files will
298
+ be written [%( default)s]"" " ,
305
299
default = "source" ,
306
300
)
307
- parser .add_option (
301
+ parser .add_argument (
308
302
"-a" ,
309
303
"--apipath" ,
310
304
action = "store" ,
311
305
help = """dir (relative to SOURCE) for generated API rst
312
- files, written by default, may be
313
- turned off by --no-write-api [%default]""" ,
306
+ files, written by default, may be
307
+ turned off by --no-write-api [%( default)s ]""" ,
314
308
default = "generated/api" ,
315
309
)
316
- parser .add_option (
310
+ parser .add_argument (
317
311
"-d" ,
318
312
"--docpath" ,
319
313
action = "store" ,
320
314
help = """dir (relative to SOURCE) for extra generated rst
321
- files for doc strings pulled from modules, use with
322
- --write-doc, off by default [%default]""" ,
315
+ files for doc strings pulled from modules, use with
316
+ --write-doc, off by default [%( default)s ]""" ,
323
317
default = "generated/doc" ,
324
318
)
325
- parser .add_option (
319
+ parser .add_argument (
326
320
"-w" ,
327
321
"--writtenpath" ,
328
322
action = "store" ,
329
- help = """dir (relative to SOURCE) for hand written rst
330
- files, an index.rst file must exist there, only needed
331
- with --write-index [%default]""" ,
323
+ help = """dir (relative to SOURCE) with hand written rst
324
+ files, an index.rst file must exist there, this will be added
325
+ to SOURCE/index.rst if found, only needed
326
+ with --write-index [%(default)s]""" ,
332
327
default = "written" ,
333
328
)
334
- parser .add_option (
329
+ parser .add_argument (
335
330
"-i" ,
336
331
"--write-index" ,
337
332
action = "store_true" ,
338
- help = """(over)write SOURCE/index.rst, not written by
339
- default""" ,
333
+ help = """(over)write SOURCE/index.rst (a backup is made) , not written by
334
+ default""" ,
340
335
default = False ,
341
336
)
342
- parser .add_option (
343
- "" ,
337
+ parser .add_argument (
344
338
"--write-doc" ,
345
339
action = "store_true" ,
346
340
help = """(over)write SOURCE/DOCPATH""" ,
347
341
default = False ,
348
342
)
349
- parser .add_option (
350
- "" ,
343
+ parser .add_argument (
351
344
"--no-write-api" ,
352
345
action = "store_false" ,
353
346
dest = "write_api" ,
354
347
help = """don't (over)write SOURCE/APIPATH""" ,
355
348
default = True ,
356
349
)
357
- parser .add_option (
350
+ parser .add_argument (
358
351
"-X" ,
359
352
"--exclude" ,
360
353
help = """regex for excluding modules, applied to the full
361
- module name [%default]""" ,
354
+ module name [%( default)s ]""" ,
362
355
default = None ,
363
356
)
364
357
365
- ( opts , args ) = parser .parse_args (sys . argv [ 1 :] )
358
+ args = parser .parse_args ()
366
359
367
- package_name = args [ 0 ]
360
+ package_name = args . package
368
361
bar = "=" * len (package_name )
369
362
370
- print ("processing package: %s" % package_name )
363
+ print (f "processing package: { package_name } " )
371
364
package = importlib .import_module (package_name )
372
365
mods = [
373
366
Module (
374
367
name ,
375
- source = opts .source ,
376
- apipath = opts .apipath ,
377
- docpath = opts .docpath ,
368
+ source = args .source ,
369
+ apipath = args .apipath ,
370
+ docpath = args .docpath ,
378
371
)
379
372
for name in walk_package (package )
380
373
]
381
374
382
- if opts .exclude is not None :
383
- rex = re .compile (opts .exclude )
375
+ if args .exclude is not None :
376
+ rex = re .compile (args .exclude )
384
377
mods = [mod for mod in mods if rex .search (mod .name ) is None ]
385
378
386
379
modules_api = ""
387
380
modules_doc = ""
388
381
389
- if opts .write_api :
390
- os .makedirs (pj (opts .source , opts .apipath ), exist_ok = True )
391
- if opts .write_doc :
392
- os .makedirs (pj (opts .source , opts .docpath ), exist_ok = True )
382
+ if args .write_api :
383
+ os .makedirs (pj (args .source , args .apipath ), exist_ok = True )
384
+ if args .write_doc :
385
+ os .makedirs (pj (args .source , args .docpath ), exist_ok = True )
393
386
394
387
print ("modules:" )
395
388
for mod in mods :
396
389
print (" %s" % mod .name )
397
- if opts .write_api :
390
+ if args .write_api :
398
391
mod .write_api ()
399
392
modules_api += format_name (mod .fullbasename ) + "\n "
400
- if opts .write_doc and mod .has_doc :
393
+ if args .write_doc and mod .has_doc :
401
394
mod .write_doc ()
402
395
modules_doc += format_name (mod .fullbasename ) + "\n "
403
396
404
- if opts .write_api :
397
+ if args .write_api :
405
398
txt = api_index_templ .format (modules_api = modules_api )
406
- file_write (pj (opts .source , opts .apipath , "index.rst" ), txt )
399
+ file_write (pj (args .source , args .apipath , "index.rst" ), txt )
407
400
408
- if opts .write_doc :
401
+ if args .write_doc :
409
402
txt = doc_index_templ .format (modules_doc = modules_doc )
410
- file_write (pj (opts .source , opts .docpath , "index.rst" ), txt )
403
+ file_write (pj (args .source , args .docpath , "index.rst" ), txt )
411
404
index_templ = re .sub (
412
405
"__docpath_index_entry__" , "{docpath}/index" , index_templ
413
406
)
414
407
else :
415
408
index_templ = re .sub ("__docpath_index_entry__" , "" , index_templ )
416
409
417
- if opts .write_index :
418
- index_fn = pj (opts .source , "index.rst" )
419
- w_pth = pj (opts .source , opts .writtenpath )
410
+ if args .write_index :
411
+ index_fn = pj (args .source , "index.rst" )
412
+ w_pth = pj (args .source , args .writtenpath )
420
413
w_index_fn = pj (w_pth , "index.rst" )
421
414
print ("overwriting main index: %s" % index_fn )
422
415
if os .path .exists (w_pth ):
@@ -426,9 +419,9 @@ def main():
426
419
)
427
420
index_templ += written_index_templ
428
421
txt = index_templ .format (
429
- apipath = opts .apipath ,
430
- docpath = opts .docpath ,
431
- writtenpath = opts .writtenpath ,
422
+ apipath = args .apipath ,
423
+ docpath = args .docpath ,
424
+ writtenpath = args .writtenpath ,
432
425
package_name = package_name ,
433
426
bar = bar ,
434
427
)
0 commit comments