<s> """<STR_LIT>""" <EOL> import inspect <EOL> import docutils <EOL> from sphinx . locale import _ <EOL> from sphinx . util . compat import Directive <EOL> from django . utils . html import strip_tags <EOL> from django . utils . encoding import force_unicode <EOL> from django . db import models , connection <EOL> class djangomodellist ( docutils . nodes . General , docutils . nodes . Element ) : <EOL> pass <EOL> class DjangoModellistDirective ( Directive ) : <EOL> def run ( self ) : <EOL> return [ djangomodellist ( '<STR_LIT>' ) ] <EOL> def purge_nodes ( app , env , docname ) : <EOL> if not hasattr ( env , '<STR_LIT>' ) : <EOL> return <EOL> env . djmex_all_djangomodels = [ m for m in env . djmex_all_djangomodels <EOL> if m [ '<STR_LIT>' ] != docname ] <EOL> def process_nodes ( app , doctree , fromdocname ) : <EOL> """<STR_LIT>""" <EOL> env = app . builder . env <EOL> content = [ ] <EOL> if app . config . djmex_include_djangomodellists : <EOL> for info in env . djmex_all_djangomodels : <EOL> block = docutils . nodes . line_block ( ) <EOL> block . append ( docutils . nodes . Text ( '<STR_LIT>' ) ) <EOL> block . append ( docutils . nodes . strong ( <EOL> text = info [ '<STR_LIT:target>' ] . _meta . db_table ) ) <EOL> block . append ( docutils . nodes . emphasis ( <EOL> text = '<STR_LIT>' . format ( info [ '<STR_LIT:name>' ] ) ) ) <EOL> content . append ( block ) <EOL> block = docutils . nodes . line_block ( ) <EOL> for l in info [ '<STR_LIT>' ] : <EOL> para = docutils . nodes . paragraph ( ) <EOL> para . append ( docutils . nodes . Text ( l ) ) <EOL> block . append ( para ) <EOL> content . append ( block ) <EOL> field_list = docutils . nodes . field_list ( ) <EOL> field = docutils . nodes . field ( ) <EOL> field . append ( docutils . nodes . field_name ( text = _ ( '<STR_LIT>' ) ) ) <EOL> body = docutils . nodes . field_body ( ) <EOL> blist = docutils . nodes . bullet_list ( ) <EOL> body . append ( blist ) <EOL> field . append ( body ) <EOL> field_list . append ( field ) <EOL> fields = info [ '<STR_LIT:target>' ] . _meta . _fields ( ) <EOL> for field in fields : <EOL> db_column = field . db_column or field . name <EOL> help_text = field . help_text or field . verbose_name <EOL> help_text = '<STR_LIT:U+0020>' + strip_tags ( force_unicode ( help_text ) ) <EOL> try : <EOL> db_type = field . db_type ( connection ) <EOL> except : <EOL> db_type = '<STR_LIT>' <EOL> if app . config . djmex_show_django_field : <EOL> ftype = '<STR_LIT:U+002CU+0020>' . join ( [ db_type , field . get_internal_type ( ) ] ) <EOL> else : <EOL> ftype = db_type <EOL> db_index = '<STR_LIT>' if field . db_index else '<STR_LIT>' <EOL> is_pk = '<STR_LIT>' if field . primary_key else '<STR_LIT>' <EOL> para = docutils . nodes . paragraph ( ) <EOL> para += docutils . nodes . strong ( text = db_column ) <EOL> para += docutils . nodes . emphasis ( text = '<STR_LIT>' . format ( ftype ) ) <EOL> details = [ help_text , is_pk , db_index ] <EOL> for i , d in enumerate ( details ) : <EOL> if d not in [ "<STR_LIT>" , None ] : <EOL> if i > <NUM_LIT:0> : <EOL> para += docutils . nodes . Text ( '<STR_LIT:U+002CU+0020>' , '<STR_LIT:U+002CU+0020>' ) <EOL> para += docutils . nodes . Text ( d , d ) <EOL> li = docutils . nodes . list_item ( ) <EOL> li . append ( para ) <EOL> blist . append ( li ) <EOL> content . append ( field_list ) <EOL> for node in doctree . traverse ( djangomodellist ) : <EOL> node . replace_self ( content ) <EOL> def add_djangomodel_node ( app , what , name , obj , options , lines ) : <EOL> """<STR_LIT>""" <EOL> env = app . builder . env <EOL> if not hasattr ( env , '<STR_LIT>' ) : <EOL> env . djmex_all_djangomodels = [ ] <EOL> env . djmex_all_djangomodels . append ( { <EOL> '<STR_LIT>' : env . docname , <EOL> '<STR_LIT:target>' : obj , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : [ l for l in lines ] , <EOL> } ) <EOL> def enrich_docstring ( obj , lines ) : <EOL> """<STR_LIT>""" <EOL> fields = obj . _meta . _fields ( ) <EOL> for field in fields : <EOL> help_text = strip_tags ( force_unicode ( field . help_text ) ) <EOL> verbose_name = force_unicode ( field . verbose_name ) . capitalize ( ) <EOL> if help_text : <EOL> lines . append ( u'<STR_LIT>' % ( field . attname , help_text ) ) <EOL> else : <EOL> lines . append ( u'<STR_LIT>' % ( field . attname , verbose_name ) ) <EOL> lines . append ( u'<STR_LIT>' % ( field . attname , type ( field ) . __name__ ) ) <EOL> def process_docstring ( app , what , name , obj , options , lines ) : <EOL> """<STR_LIT>""" <EOL> if inspect . isclass ( obj ) and issubclass ( obj , models . Model ) : <EOL> add_djangomodel_node ( app , what , name , obj , options , lines ) <EOL> if app . config . djmex_pretty_model : <EOL> lines = enrich_docstring ( obj , lines ) <EOL> return lines <EOL> def setup ( app ) : <EOL> app . connect ( '<STR_LIT>' , process_docstring ) <EOL> app . add_config_value ( '<STR_LIT>' , False , False ) <EOL> app . add_config_value ( '<STR_LIT>' , False , False ) <EOL> app . add_config_value ( '<STR_LIT>' , True , False ) <EOL> app . add_node ( djangomodellist ) <EOL> app . add_directive ( '<STR_LIT>' , DjangoModellistDirective ) <EOL> app . connect ( '<STR_LIT>' , process_nodes ) <EOL> app . connect ( '<STR_LIT>' , purge_nodes ) </s>
