2727DEBUG = True
2828
2929
30- class ApiDocWriter ( object ) :
30+ class ApiDocWriter :
3131 """Class for automatic detection and parsing of API docs
3232 to Sphinx-parsable reST format"""
3333
@@ -185,9 +185,8 @@ def _parse_module(self, uri):
185185 # nothing that we could handle here.
186186 return ([], [])
187187
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 )
191190 return functions , classes
192191
193192 def _parse_module_with_import (self , uri ):
@@ -217,14 +216,10 @@ def _parse_module_with_import(self, uri):
217216 continue
218217 obj = mod .__dict__ [obj_str ]
219218 # 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 :
221220 continue
222221 # 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 )):
228223 functions .append (obj_str )
229224 else :
230225 try :
@@ -278,7 +273,7 @@ def generate_api_doc(self, uri):
278273
279274 # Make a shorter version of the uri that omits the package name for
280275 # titles
281- uri_short = re .sub (r'^%s\.' % self .package_name , '' , uri )
276+ uri_short = re .sub (rf'^ { self .package_name } \.' , '' , uri )
282277
283278 head = '.. AUTO-GENERATED FILE -- DO NOT EDIT!\n \n '
284279 body = ''
@@ -345,20 +340,12 @@ def _survives_exclude(self, matchstr, match_type):
345340 elif match_type == 'package' :
346341 patterns = self .package_skip_patterns
347342 else :
348- raise ValueError ('Cannot interpret match type "%s"' % match_type )
343+ raise ValueError (f 'Cannot interpret match type "{ match_type } "' )
349344 # Match to URI without package name
350345 L = len (self .package_name )
351346 if matchstr [:L ] == self .package_name :
352347 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 )
362349
363350 def discover_modules (self ):
364351 r"""Return module sequence discovered from ``self.package_name``
@@ -426,7 +413,7 @@ def write_modules_api(self, modules, outdir):
426413 written_modules = []
427414
428415 for ulm , mods in module_by_ulm .items ():
429- print ('Generating docs for %s:' % ulm )
416+ print (f 'Generating docs for { ulm } :' )
430417 document_head = []
431418 document_body = []
432419
@@ -438,11 +425,8 @@ def write_modules_api(self, modules, outdir):
438425 document_body .append (body )
439426
440427 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 )
446430 written_modules .append (out_module )
447431
448432 self .written_modules = written_modules
@@ -497,14 +481,13 @@ def write_index(self, outdir, froot='gen', relative_to=None):
497481 relpath = (outdir + os .path .sep ).replace (relative_to + os .path .sep , '' )
498482 else :
499483 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