27
27
DEBUG = True
28
28
29
29
30
- class ApiDocWriter ( object ) :
30
+ class ApiDocWriter :
31
31
"""Class for automatic detection and parsing of API docs
32
32
to Sphinx-parsable reST format"""
33
33
@@ -185,9 +185,8 @@ def _parse_module(self, uri):
185
185
# nothing that we could handle here.
186
186
return ([], [])
187
187
188
- f = open (filename , 'rt' )
189
- functions , classes = self ._parse_lines (f )
190
- f .close ()
188
+ with open (filename ) as f :
189
+ functions , classes = self ._parse_lines (f )
191
190
return functions , classes
192
191
193
192
def _parse_module_with_import (self , uri ):
@@ -217,14 +216,10 @@ def _parse_module_with_import(self, uri):
217
216
continue
218
217
obj = mod .__dict__ [obj_str ]
219
218
# Check if function / class defined in module
220
- if not self .other_defines and not getmodule (obj ) = = mod :
219
+ if not self .other_defines and getmodule (obj ) ! = mod :
221
220
continue
222
221
# figure out if obj is a function or class
223
- if (
224
- hasattr (obj , 'func_name' )
225
- or isinstance (obj , BuiltinFunctionType )
226
- or isinstance (obj , FunctionType )
227
- ):
222
+ if hasattr (obj , 'func_name' ) or isinstance (obj , (BuiltinFunctionType , FunctionType )):
228
223
functions .append (obj_str )
229
224
else :
230
225
try :
@@ -278,7 +273,7 @@ def generate_api_doc(self, uri):
278
273
279
274
# Make a shorter version of the uri that omits the package name for
280
275
# titles
281
- uri_short = re .sub (r'^%s\.' % self .package_name , '' , uri )
276
+ uri_short = re .sub (rf'^ { self .package_name } \.' , '' , uri )
282
277
283
278
head = '.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n '
284
279
body = ''
@@ -345,20 +340,12 @@ def _survives_exclude(self, matchstr, match_type):
345
340
elif match_type == 'package' :
346
341
patterns = self .package_skip_patterns
347
342
else :
348
- raise ValueError ('Cannot interpret match type "%s"' % match_type )
343
+ raise ValueError (f 'Cannot interpret match type "{ match_type } "' )
349
344
# Match to URI without package name
350
345
L = len (self .package_name )
351
346
if matchstr [:L ] == self .package_name :
352
347
matchstr = matchstr [L :]
353
- for pat in patterns :
354
- try :
355
- pat .search
356
- except AttributeError :
357
- pat = re .compile (pat )
358
- if pat .search (matchstr ):
359
- return False
360
-
361
- return True
348
+ return not any (re .search (pat , matchstr ) for pat in patterns )
362
349
363
350
def discover_modules (self ):
364
351
r"""Return module sequence discovered from ``self.package_name``
@@ -426,7 +413,7 @@ def write_modules_api(self, modules, outdir):
426
413
written_modules = []
427
414
428
415
for ulm , mods in module_by_ulm .items ():
429
- print ('Generating docs for %s:' % ulm )
416
+ print (f 'Generating docs for { ulm } :' )
430
417
document_head = []
431
418
document_body = []
432
419
@@ -438,11 +425,8 @@ def write_modules_api(self, modules, outdir):
438
425
document_body .append (body )
439
426
440
427
out_module = ulm + self .rst_extension
441
- outfile = os .path .join (outdir , out_module )
442
- fileobj = open (outfile , 'wt' )
443
-
444
- fileobj .writelines (document_head + document_body )
445
- fileobj .close ()
428
+ with open (os .path .join (outdir , out_module ), 'w' ) as fileobj :
429
+ fileobj .writelines (document_head + document_body )
446
430
written_modules .append (out_module )
447
431
448
432
self .written_modules = written_modules
@@ -497,14 +481,13 @@ def write_index(self, outdir, froot='gen', relative_to=None):
497
481
relpath = (outdir + os .path .sep ).replace (relative_to + os .path .sep , '' )
498
482
else :
499
483
relpath = outdir
500
- idx = open (path , 'wt' )
501
- w = idx .write
502
- w ('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n ' )
503
-
504
- title = 'API Reference'
505
- w (title + '\n ' )
506
- w ('=' * len (title ) + '\n \n ' )
507
- w ('.. toctree::\n \n ' )
508
- for f in self .written_modules :
509
- w (' %s\n ' % os .path .join (relpath , f ))
510
- idx .close ()
484
+ with open (path , 'w' ) as idx :
485
+ w = idx .write
486
+ w ('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n ' )
487
+
488
+ title = 'API Reference'
489
+ w (title + '\n ' )
490
+ w ('=' * len (title ) + '\n \n ' )
491
+ w ('.. toctree::\n \n ' )
492
+ for f in self .written_modules :
493
+ w (f' { os .path .join (relpath , f )} \n ' )
0 commit comments