<s> """<STR_LIT>""" <EOL> import inspect <EOL> import os <EOL> import re <EOL> import sys <EOL> from . utils import find_mod_objs <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> text_type = str <EOL> else : <EOL> text_type = unicode <EOL> automod_templ_modheader = """<STR_LIT>""" <EOL> automod_templ_classes = """<STR_LIT>""" <EOL> automod_templ_funcs = """<STR_LIT>""" <EOL> automod_templ_inh = """<STR_LIT>""" <EOL> _automodapirex = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' , <EOL> flags = re . MULTILINE ) <EOL> _automodapiargsrex = re . compile ( r'<STR_LIT>' , flags = re . MULTILINE ) <EOL> def automodapi_replace ( sourcestr , app , dotoctree = True , docname = None , <EOL> warnings = True ) : <EOL> """<STR_LIT>""" <EOL> spl = _automodapirex . split ( sourcestr ) <EOL> if len ( spl ) > <NUM_LIT:1> : <EOL> if dotoctree : <EOL> toctreestr = '<STR_LIT>' <EOL> dirnm = app . config . automodapi_toctreedirnm <EOL> if not dirnm . endswith ( "<STR_LIT:/>" ) : <EOL> dirnm += "<STR_LIT:/>" <EOL> if docname is not None : <EOL> toctreestr += '<STR_LIT>' * docname . count ( '<STR_LIT:/>' ) + dirnm <EOL> else : <EOL> toctreestr += dirnm <EOL> else : <EOL> toctreestr = '<STR_LIT>' <EOL> newstrs = [ spl [ <NUM_LIT:0> ] ] <EOL> for grp in range ( len ( spl ) // <NUM_LIT:3> ) : <EOL> modnm = spl [ grp * <NUM_LIT:3> + <NUM_LIT:1> ] <EOL> if docname is None : <EOL> location = None <EOL> else : <EOL> location = ( docname , spl [ <NUM_LIT:0> ] . count ( '<STR_LIT:\n>' ) ) <EOL> toskip = [ ] <EOL> inhdiag = maindocstr = top_head = True <EOL> hds = '<STR_LIT>' <EOL> allowedpkgnms = [ ] <EOL> unknownops = [ ] <EOL> for opname , args in _automodapiargsrex . findall ( spl [ grp * <NUM_LIT:3> + <NUM_LIT:2> ] ) : <EOL> if opname == '<STR_LIT>' : <EOL> toskip . append ( args . strip ( ) ) <EOL> elif opname == '<STR_LIT>' : <EOL> inhdiag = False <EOL> elif opname == '<STR_LIT>' : <EOL> maindocstr = False <EOL> elif opname == '<STR_LIT>' : <EOL> hds = args <EOL> elif opname == '<STR_LIT>' : <EOL> top_head = False <EOL> elif opname == '<STR_LIT>' : <EOL> allowedpkgnms . append ( args . strip ( ) ) <EOL> else : <EOL> unknownops . append ( opname ) <EOL> if len ( allowedpkgnms ) == <NUM_LIT:0> : <EOL> allowedpkgnms = '<STR_LIT>' <EOL> onlylocals = True <EOL> else : <EOL> allowedpkgnms = '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( allowedpkgnms ) <EOL> onlylocals = allowedpkgnms <EOL> if len ( hds ) < <NUM_LIT:2> : <EOL> msg = '<STR_LIT>' <EOL> if warnings : <EOL> app . warn ( msg . format ( len ( hds ) ) , location ) <EOL> hds = '<STR_LIT>' <EOL> h1 , h2 = hds . lstrip ( ) [ : <NUM_LIT:2> ] <EOL> if len ( unknownops ) > <NUM_LIT:0> and app is not None : <EOL> opsstrs = '<STR_LIT:U+002C>' . join ( unknownops ) <EOL> msg = '<STR_LIT>' + opsstrs + '<STR_LIT>' <EOL> if warnings : <EOL> app . warn ( msg , location ) <EOL> ispkg , hascls , hasfuncs = _mod_info ( modnm , toskip , onlylocals = onlylocals ) <EOL> if maindocstr : <EOL> automodline = '<STR_LIT>' . format ( modname = modnm ) <EOL> else : <EOL> automodline = '<STR_LIT>' <EOL> if top_head : <EOL> newstrs . append ( automod_templ_modheader . format ( modname = modnm , <EOL> modhds = h1 * len ( modnm ) , <EOL> pkgormod = '<STR_LIT>' if ispkg else '<STR_LIT>' , <EOL> pkgormodhds = h1 * ( <NUM_LIT:8> if ispkg else <NUM_LIT:7> ) , <EOL> automoduleline = automodline ) ) <EOL> else : <EOL> newstrs . append ( automod_templ_modheader . format ( <EOL> modname = '<STR_LIT>' , <EOL> modhds = '<STR_LIT>' , <EOL> pkgormod = '<STR_LIT>' , <EOL> pkgormodhds = '<STR_LIT>' , <EOL> automoduleline = automodline ) ) <EOL> clsfuncoptions = [ ] <EOL> if toctreestr : <EOL> clsfuncoptions . append ( toctreestr ) <EOL> if toskip : <EOL> clsfuncoptions . append ( '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( toskip ) ) <EOL> if allowedpkgnms : <EOL> clsfuncoptions . append ( allowedpkgnms ) <EOL> clsfuncoptionstr = '<STR_LIT>' . join ( clsfuncoptions ) <EOL> if hasfuncs : <EOL> newstrs . append ( automod_templ_funcs . format ( <EOL> modname = modnm , <EOL> funchds = h2 * <NUM_LIT:9> , <EOL> clsfuncoptions = clsfuncoptionstr ) ) <EOL> if hascls : <EOL> newstrs . append ( automod_templ_classes . format ( <EOL> modname = modnm , <EOL> clshds = h2 * <NUM_LIT:7> , <EOL> clsfuncoptions = clsfuncoptionstr ) ) <EOL> if inhdiag and hascls : <EOL> newstrs . append ( automod_templ_inh . format ( <EOL> modname = modnm , <EOL> clsinhsechds = h2 * <NUM_LIT> , <EOL> allowedpkgnms = allowedpkgnms ) ) <EOL> newstrs . append ( spl [ grp * <NUM_LIT:3> + <NUM_LIT:3> ] ) <EOL> newsourcestr = '<STR_LIT>' . join ( newstrs ) <EOL> if app . config . automodapi_writereprocessed : <EOL> if isinstance ( newsourcestr , text_type ) : <EOL> ustr = newsourcestr <EOL> else : <EOL> ustr = newsourcestr . decode ( app . config . source_encoding ) <EOL> if docname is None : <EOL> with open ( os . path . join ( app . srcdir , '<STR_LIT>' ) , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( ustr ) <EOL> else : <EOL> env = app . builder . env <EOL> filename = docname + os . path . splitext ( env . doc2path ( docname ) ) [ <NUM_LIT:1> ] <EOL> filename += '<STR_LIT>' <EOL> with open ( os . path . join ( app . srcdir , filename ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( ustr ) <EOL> return newsourcestr <EOL> else : <EOL> return sourcestr <EOL> def _mod_info ( modname , toskip = [ ] , onlylocals = True ) : <EOL> """<STR_LIT>""" <EOL> hascls = hasfunc = False <EOL> for localnm , fqnm , obj in zip ( * find_mod_objs ( modname , onlylocals = onlylocals ) ) : <EOL> if localnm not in toskip : <EOL> hascls = hascls or inspect . isclass ( obj ) <EOL> hasfunc = hasfunc or inspect . isroutine ( obj ) <EOL> if hascls and hasfunc : <EOL> break <EOL> pkg = sys . modules [ modname ] <EOL> ispkg = ( hasattr ( pkg , '<STR_LIT>' ) and isinstance ( pkg . __file__ , str ) and <EOL> os . path . split ( pkg . __file__ ) [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) ) <EOL> return ispkg , hascls , hasfunc <EOL> def process_automodapi ( app , docname , source ) : <EOL> source [ <NUM_LIT:0> ] = automodapi_replace ( source [ <NUM_LIT:0> ] , app , True , docname ) <EOL> def setup ( app ) : <EOL> app . setup_extension ( '<STR_LIT>' ) <EOL> app . connect ( '<STR_LIT>' , process_automodapi ) <EOL> app . add_config_value ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> app . add_config_value ( '<STR_LIT>' , False , True ) </s>
