<s> """<STR_LIT>""" <EOL> import re <EOL> import glob <EOL> import inspect <EOL> from os import path <EOL> from six import iteritems <EOL> from six . moves import cPickle as pickle <EOL> import sphinx <EOL> from sphinx . builders import Builder <EOL> from sphinx . util . inspect import safe_getattr <EOL> def write_header ( f , text , char = '<STR_LIT:->' ) : <EOL> f . write ( text + '<STR_LIT:\n>' ) <EOL> f . write ( char * len ( text ) + '<STR_LIT:\n>' ) <EOL> def compile_regex_list ( name , exps , warnfunc ) : <EOL> lst = [ ] <EOL> for exp in exps : <EOL> try : <EOL> lst . append ( re . compile ( exp ) ) <EOL> except Exception : <EOL> warnfunc ( '<STR_LIT>' % ( exp , name ) ) <EOL> return lst <EOL> class CoverageBuilder ( Builder ) : <EOL> name = '<STR_LIT>' <EOL> def init ( self ) : <EOL> self . c_sourcefiles = [ ] <EOL> for pattern in self . config . coverage_c_path : <EOL> pattern = path . join ( self . srcdir , pattern ) <EOL> self . c_sourcefiles . extend ( glob . glob ( pattern ) ) <EOL> self . c_regexes = [ ] <EOL> for ( name , exp ) in self . config . coverage_c_regexes . items ( ) : <EOL> try : <EOL> self . c_regexes . append ( ( name , re . compile ( exp ) ) ) <EOL> except Exception : <EOL> self . warn ( '<STR_LIT>' % exp ) <EOL> self . c_ignorexps = { } <EOL> for ( name , exps ) in iteritems ( self . config . coverage_ignore_c_items ) : <EOL> self . c_ignorexps [ name ] = compile_regex_list ( <EOL> '<STR_LIT>' , exps , self . warn ) <EOL> self . mod_ignorexps = compile_regex_list ( <EOL> '<STR_LIT>' , self . config . coverage_ignore_modules , <EOL> self . warn ) <EOL> self . cls_ignorexps = compile_regex_list ( <EOL> '<STR_LIT>' , self . config . coverage_ignore_classes , <EOL> self . warn ) <EOL> self . fun_ignorexps = compile_regex_list ( <EOL> '<STR_LIT>' , self . config . coverage_ignore_functions , <EOL> self . warn ) <EOL> def get_outdated_docs ( self ) : <EOL> return '<STR_LIT>' <EOL> def write ( self , * ignored ) : <EOL> self . py_undoc = { } <EOL> self . build_py_coverage ( ) <EOL> self . write_py_coverage ( ) <EOL> self . c_undoc = { } <EOL> self . build_c_coverage ( ) <EOL> self . write_c_coverage ( ) <EOL> def build_c_coverage ( self ) : <EOL> c_objects = self . env . domaindata [ '<STR_LIT:c>' ] [ '<STR_LIT>' ] <EOL> for filename in self . c_sourcefiles : <EOL> undoc = set ( ) <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> try : <EOL> for line in f : <EOL> for key , regex in self . c_regexes : <EOL> match = regex . match ( line ) <EOL> if match : <EOL> name = match . groups ( ) [ <NUM_LIT:0> ] <EOL> if name not in c_objects : <EOL> for exp in self . c_ignorexps . get ( key , ( ) ) : <EOL> if exp . match ( name ) : <EOL> break <EOL> else : <EOL> undoc . add ( ( key , name ) ) <EOL> continue <EOL> finally : <EOL> f . close ( ) <EOL> if undoc : <EOL> self . c_undoc [ filename ] = undoc <EOL> def write_c_coverage ( self ) : <EOL> output_file = path . join ( self . outdir , '<STR_LIT>' ) <EOL> op = open ( output_file , '<STR_LIT:w>' ) <EOL> try : <EOL> if self . config . coverage_write_headline : <EOL> write_header ( op , '<STR_LIT>' , '<STR_LIT:=>' ) <EOL> op . write ( '<STR_LIT:\n>' ) <EOL> for filename , undoc in iteritems ( self . c_undoc ) : <EOL> write_header ( op , filename ) <EOL> for typ , name in sorted ( undoc ) : <EOL> op . write ( '<STR_LIT>' % ( name , typ ) ) <EOL> op . write ( '<STR_LIT:\n>' ) <EOL> finally : <EOL> op . close ( ) <EOL> def build_py_coverage ( self ) : <EOL> objects = self . env . domaindata [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> modules = self . env . domaindata [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> skip_undoc = self . config . coverage_skip_undoc_in_source <EOL> for mod_name in modules : <EOL> ignore = False <EOL> for exp in self . mod_ignorexps : <EOL> if exp . match ( mod_name ) : <EOL> ignore = True <EOL> break <EOL> if ignore : <EOL> continue <EOL> try : <EOL> mod = __import__ ( mod_name , fromlist = [ '<STR_LIT:foo>' ] ) <EOL> except ImportError as err : <EOL> self . warn ( '<STR_LIT>' % <EOL> ( mod_name , err ) ) <EOL> self . py_undoc [ mod_name ] = { '<STR_LIT:error>' : err } <EOL> continue <EOL> funcs = [ ] <EOL> classes = { } <EOL> for name , obj in inspect . getmembers ( mod ) : <EOL> if name [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> continue <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> continue <EOL> if obj . __module__ != mod_name : <EOL> continue <EOL> full_name = '<STR_LIT>' % ( mod_name , name ) <EOL> if inspect . isfunction ( obj ) : <EOL> if full_name not in objects : <EOL> for exp in self . fun_ignorexps : <EOL> if exp . match ( name ) : <EOL> break <EOL> else : <EOL> if skip_undoc and not obj . __doc__ : <EOL> continue <EOL> funcs . append ( name ) <EOL> elif inspect . isclass ( obj ) : <EOL> for exp in self . cls_ignorexps : <EOL> if exp . match ( name ) : <EOL> break <EOL> else : <EOL> if full_name not in objects : <EOL> if skip_undoc and not obj . __doc__ : <EOL> continue <EOL> classes [ name ] = [ ] <EOL> continue <EOL> attrs = [ ] <EOL> for attr_name in dir ( obj ) : <EOL> if attr_name not in obj . __dict__ : <EOL> continue <EOL> try : <EOL> attr = safe_getattr ( obj , attr_name ) <EOL> except AttributeError : <EOL> continue <EOL> if not ( inspect . ismethod ( attr ) or <EOL> inspect . isfunction ( attr ) ) : <EOL> continue <EOL> if attr_name [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> continue <EOL> if skip_undoc and not attr . __doc__ : <EOL> continue <EOL> full_attr_name = '<STR_LIT>' % ( full_name , attr_name ) <EOL> if full_attr_name not in objects : <EOL> attrs . append ( attr_name ) <EOL> if attrs : <EOL> classes [ name ] = attrs <EOL> self . py_undoc [ mod_name ] = { '<STR_LIT>' : funcs , '<STR_LIT>' : classes } <EOL> def write_py_coverage ( self ) : <EOL> output_file = path . join ( self . outdir , '<STR_LIT>' ) <EOL> op = open ( output_file , '<STR_LIT:w>' ) <EOL> failed = [ ] <EOL> try : <EOL> if self . config . coverage_write_headline : <EOL> write_header ( op , '<STR_LIT>' , '<STR_LIT:=>' ) <EOL> keys = sorted ( self . py_undoc . keys ( ) ) <EOL> for name in keys : <EOL> undoc = self . py_undoc [ name ] <EOL> if '<STR_LIT:error>' in undoc : <EOL> failed . append ( ( name , undoc [ '<STR_LIT:error>' ] ) ) <EOL> else : <EOL> if not undoc [ '<STR_LIT>' ] and not undoc [ '<STR_LIT>' ] : <EOL> continue <EOL> write_header ( op , name ) <EOL> if undoc [ '<STR_LIT>' ] : <EOL> op . write ( '<STR_LIT>' ) <EOL> op . writelines ( '<STR_LIT>' % x for x in undoc [ '<STR_LIT>' ] ) <EOL> op . write ( '<STR_LIT:\n>' ) <EOL> if undoc [ '<STR_LIT>' ] : <EOL> op . write ( '<STR_LIT>' ) <EOL> for name , methods in sorted ( <EOL> iteritems ( undoc [ '<STR_LIT>' ] ) ) : <EOL> if not methods : <EOL> op . write ( '<STR_LIT>' % name ) <EOL> else : <EOL> op . write ( '<STR_LIT>' % name ) <EOL> op . writelines ( '<STR_LIT>' % x for x in methods ) <EOL> op . write ( '<STR_LIT:\n>' ) <EOL> if failed : <EOL> write_header ( op , '<STR_LIT>' ) <EOL> op . writelines ( '<STR_LIT>' % x for x in failed ) <EOL> finally : <EOL> op . close ( ) <EOL> def finish ( self ) : <EOL> picklepath = path . join ( self . outdir , '<STR_LIT>' ) <EOL> dumpfile = open ( picklepath , '<STR_LIT:wb>' ) <EOL> try : <EOL> pickle . dump ( ( self . py_undoc , self . c_undoc ) , dumpfile ) <EOL> finally : <EOL> dumpfile . close ( ) <EOL> def setup ( app ) : <EOL> app . add_builder ( CoverageBuilder ) <EOL> app . add_config_value ( '<STR_LIT>' , [ ] , False ) <EOL> app . add_config_value ( '<STR_LIT>' , [ ] , False ) <EOL> app . add_config_value ( '<STR_LIT>' , [ ] , False ) <EOL> app . add_config_value ( '<STR_LIT>' , [ ] , False ) <EOL> app . add_config_value ( '<STR_LIT>' , { } , False ) <EOL> app . add_config_value ( '<STR_LIT>' , { } , False ) <EOL> app . add_config_value ( '<STR_LIT>' , True , False ) <EOL> app . add_config_value ( '<STR_LIT>' , False , False ) <EOL> return { '<STR_LIT:version>' : sphinx . __display_version__ , '<STR_LIT>' : True } </s>
