<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> import inspect <EOL> from django . db import models <EOL> RE_STR = r"<STR_LIT>" <EOL> MODELS_MODULE_RE = re . compile ( RE_STR , re . MULTILINE ) <EOL> """<STR_LIT>""" <EOL> def source_read ( app , docname , source ) : <EOL> """<STR_LIT>""" <EOL> def __get_models ( mod_path ) : <EOL> """<STR_LIT>""" <EOL> modnames = [ ] <EOL> if mod_path not in sys . modules : <EOL> __import__ ( mod_path ) <EOL> clsmembers = inspect . getmembers ( sys . modules [ mod_path ] , inspect . isclass ) <EOL> for c in clsmembers : <EOL> if issubclass ( c [ <NUM_LIT:1> ] , models . Model ) : <EOL> modnames . append ( c [ <NUM_LIT:1> ] . __module__ + '<STR_LIT:.>' + c [ <NUM_LIT:1> ] . __name__ ) <EOL> return modnames <EOL> def __get_directives ( class_names ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" . format ( c ) for c in class_names ] <EOL> instring = source [ <NUM_LIT:0> ] <EOL> changes = [ ] <EOL> for match in MODELS_MODULE_RE . finditer ( instring ) : <EOL> if match : <EOL> modname = match . group ( ) . strip ( ) [ <NUM_LIT:16> : ] <EOL> to = instring . find ( '<STR_LIT>' , match . end ( ) ) <EOL> to = to if to != - <NUM_LIT:1> else len ( instring ) <EOL> inserts = __get_directives ( __get_models ( modname ) ) <EOL> if len ( inserts ) > <NUM_LIT:0> : <EOL> changes . append ( ( to , '<STR_LIT>' + '<STR_LIT>' . join ( inserts ) ) ) <EOL> result = [ ] <EOL> last_index = <NUM_LIT:0> <EOL> for index , c in enumerate ( changes ) : <EOL> result . append ( instring [ last_index : c [ <NUM_LIT:0> ] ] ) <EOL> result . append ( c [ <NUM_LIT:1> ] ) <EOL> last_index = c [ <NUM_LIT:0> ] <EOL> result . append ( instring [ last_index : ] ) <EOL> source [ <NUM_LIT:0> ] = '<STR_LIT>' . join ( result ) <EOL> def setup ( app ) : <EOL> app . connect ( '<STR_LIT>' , source_read ) </s>
