<s> """<STR_LIT>""" <EOL> import datetime <EOL> import os <EOL> import re <EOL> import six <EOL> import django <EOL> from django . db . models . fields . related import ( <EOL> ForeignKey , ManyToManyField , OneToOneField , RelatedField , <EOL> ) <EOL> from django . template import Context , Template , loader <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import activate as activate_language <EOL> try : <EOL> from django . utils . encoding import force_bytes <EOL> except ImportError : <EOL> from django . utils . encoding import smart_str as force_bytes <EOL> try : <EOL> from django . contrib . contenttypes . fields import GenericRelation <EOL> except ImportError : <EOL> from django . contrib . contenttypes . generic import GenericRelation <EOL> from django_extensions . compat import get_app , get_models_compat , list_app_labels , get_model <EOL> __version__ = "<STR_LIT:1.0>" <EOL> __license__ = "<STR_LIT>" <EOL> __author__ = "<STR_LIT>" , <EOL> __contributors__ = [ <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> def parse_file_or_list ( arg ) : <EOL> if not arg : <EOL> return [ ] <EOL> if isinstance ( arg , ( list , tuple , set ) ) : <EOL> return arg <EOL> if '<STR_LIT:U+002C>' not in arg and os . path . isfile ( arg ) : <EOL> return [ e . strip ( ) for e in open ( arg ) . readlines ( ) ] <EOL> return [ e . strip ( ) for e in arg . split ( '<STR_LIT:U+002C>' ) ] <EOL> def use_model ( model_name , include_models , exclude_models ) : <EOL> """<STR_LIT>""" <EOL> if exclude_models : <EOL> for model_pattern in exclude_models : <EOL> model_pattern = '<STR_LIT>' % model_pattern . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if re . search ( model_pattern , model_name ) : <EOL> return False <EOL> elif include_models : <EOL> for model_pattern in include_models : <EOL> model_pattern = '<STR_LIT>' % model_pattern . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if re . search ( model_pattern , model_name ) : <EOL> return True <EOL> return not include_models <EOL> def generate_graph_data ( app_labels , ** kwargs ) : <EOL> cli_options = kwargs . get ( '<STR_LIT>' , None ) <EOL> disable_fields = kwargs . get ( '<STR_LIT>' , False ) <EOL> include_models = parse_file_or_list ( kwargs . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> all_applications = kwargs . get ( '<STR_LIT>' , False ) <EOL> use_subgraph = kwargs . get ( '<STR_LIT>' , False ) <EOL> verbose_names = kwargs . get ( '<STR_LIT>' , False ) <EOL> inheritance = kwargs . get ( '<STR_LIT>' , True ) <EOL> relations_as_fields = kwargs . get ( "<STR_LIT>" , True ) <EOL> sort_fields = kwargs . get ( "<STR_LIT>" , True ) <EOL> language = kwargs . get ( '<STR_LIT>' , None ) <EOL> if language is not None : <EOL> activate_language ( language ) <EOL> exclude_columns = parse_file_or_list ( kwargs . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> exclude_models = parse_file_or_list ( kwargs . get ( '<STR_LIT>' , "<STR_LIT>" ) ) <EOL> def skip_field ( field ) : <EOL> if exclude_columns : <EOL> if verbose_names and field . verbose_name : <EOL> if field . verbose_name in exclude_columns : <EOL> return True <EOL> if field . name in exclude_columns : <EOL> return True <EOL> return False <EOL> if all_applications : <EOL> app_labels = list_app_labels ( ) <EOL> graphs = [ ] <EOL> for app_label in app_labels : <EOL> app = get_app ( app_label ) <EOL> if not app : <EOL> continue <EOL> graph = Context ( { <EOL> '<STR_LIT:name>' : '<STR_LIT>' % app . __name__ , <EOL> '<STR_LIT>' : "<STR_LIT:%s>" % '<STR_LIT:.>' . join ( app . __name__ . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : "<STR_LIT>" % app . __name__ . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) , <EOL> '<STR_LIT>' : [ ] <EOL> } ) <EOL> appmodels = list ( get_models_compat ( app_label ) ) <EOL> abstract_models = [ ] <EOL> for appmodel in appmodels : <EOL> abstract_models = abstract_models + [ abstract_model for abstract_model in appmodel . __bases__ if hasattr ( abstract_model , '<STR_LIT>' ) and abstract_model . _meta . abstract ] <EOL> abstract_models = list ( set ( abstract_models ) ) <EOL> appmodels = abstract_models + appmodels <EOL> for appmodel in appmodels : <EOL> appmodel_abstracts = [ abstract_model . __name__ for abstract_model in appmodel . __bases__ if hasattr ( abstract_model , '<STR_LIT>' ) and abstract_model . _meta . abstract ] <EOL> def getBasesAbstractFields ( c ) : <EOL> _abstract_fields = [ ] <EOL> for e in c . __bases__ : <EOL> if hasattr ( e , '<STR_LIT>' ) and e . _meta . abstract : <EOL> _abstract_fields . extend ( e . _meta . fields ) <EOL> _abstract_fields . extend ( getBasesAbstractFields ( e ) ) <EOL> return _abstract_fields <EOL> abstract_fields = getBasesAbstractFields ( appmodel ) <EOL> model = { <EOL> '<STR_LIT>' : appmodel . __module__ . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) , <EOL> '<STR_LIT:name>' : appmodel . __name__ , <EOL> '<STR_LIT>' : appmodel_abstracts , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> if not use_model ( <EOL> appmodel . _meta . object_name , <EOL> include_models , <EOL> exclude_models <EOL> ) : <EOL> continue <EOL> if verbose_names and appmodel . _meta . verbose_name : <EOL> model [ '<STR_LIT:label>' ] = force_bytes ( appmodel . _meta . verbose_name ) <EOL> else : <EOL> model [ '<STR_LIT:label>' ] = model [ '<STR_LIT:name>' ] <EOL> def add_attributes ( field ) : <EOL> if verbose_names and field . verbose_name : <EOL> label = force_bytes ( field . verbose_name ) <EOL> if label . islower ( ) : <EOL> label = label . capitalize ( ) <EOL> else : <EOL> label = field . name <EOL> t = type ( field ) . __name__ <EOL> if isinstance ( field , ( OneToOneField , ForeignKey ) ) : <EOL> t += "<STR_LIT>" . format ( field . rel . field_name ) <EOL> model [ '<STR_LIT>' ] . append ( { <EOL> '<STR_LIT:name>' : field . name , <EOL> '<STR_LIT:label>' : label , <EOL> '<STR_LIT:type>' : t , <EOL> '<STR_LIT:blank>' : field . blank , <EOL> '<STR_LIT>' : field in abstract_fields , <EOL> '<STR_LIT>' : isinstance ( field , RelatedField ) , <EOL> '<STR_LIT:primary_key>' : field . primary_key , <EOL> } ) <EOL> attributes = [ field for field in appmodel . _meta . local_fields ] <EOL> if not relations_as_fields : <EOL> attributes = [ field for field in attributes if not isinstance ( field , RelatedField ) ] <EOL> pk = appmodel . _meta . pk <EOL> if pk and not appmodel . _meta . abstract and pk in attributes : <EOL> add_attributes ( pk ) <EOL> for field in attributes : <EOL> if skip_field ( field ) : <EOL> continue <EOL> if pk and field == pk : <EOL> continue <EOL> add_attributes ( field ) <EOL> if sort_fields : <EOL> model [ '<STR_LIT>' ] = sorted ( model [ '<STR_LIT>' ] , key = lambda field : ( not field [ '<STR_LIT:primary_key>' ] , not field [ '<STR_LIT>' ] , field [ '<STR_LIT:label>' ] ) ) <EOL> def add_relation ( field , extras = "<STR_LIT>" ) : <EOL> if verbose_names and field . verbose_name : <EOL> label = force_bytes ( field . verbose_name ) <EOL> if label . islower ( ) : <EOL> label = label . capitalize ( ) <EOL> else : <EOL> label = field . name <EOL> if hasattr ( field , '<STR_LIT>' ) : <EOL> related_query_name = field . related_query_name ( ) <EOL> if verbose_names and related_query_name . islower ( ) : <EOL> related_query_name = related_query_name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) . capitalize ( ) <EOL> label = '<STR_LIT>' . format ( label , force_bytes ( related_query_name ) ) <EOL> if isinstance ( field . rel . to , six . string_types ) : <EOL> if field . rel . to == '<STR_LIT>' : <EOL> target_model = field . model <EOL> else : <EOL> target_model = get_model ( field . rel . to ) <EOL> else : <EOL> target_model = field . rel . to <EOL> _rel = { <EOL> '<STR_LIT>' : target_model . __module__ . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) , <EOL> '<STR_LIT:target>' : target_model . __name__ , <EOL> '<STR_LIT:type>' : type ( field ) . __name__ , <EOL> '<STR_LIT:name>' : field . name , <EOL> '<STR_LIT:label>' : label , <EOL> '<STR_LIT>' : extras , <EOL> '<STR_LIT>' : True <EOL> } <EOL> if _rel not in model [ '<STR_LIT>' ] and use_model ( <EOL> _rel [ '<STR_LIT:target>' ] , <EOL> include_models , <EOL> exclude_models <EOL> ) : <EOL> model [ '<STR_LIT>' ] . append ( _rel ) <EOL> for field in appmodel . _meta . local_fields : <EOL> if field . attname . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> if field in abstract_fields : <EOL> continue <EOL> if skip_field ( field ) : <EOL> continue <EOL> if isinstance ( field , OneToOneField ) : <EOL> add_relation ( field , '<STR_LIT>' ) <EOL> elif isinstance ( field , ForeignKey ) : <EOL> add_relation ( field , '<STR_LIT>' ) <EOL> for field in appmodel . _meta . local_many_to_many : <EOL> if skip_field ( field ) : <EOL> continue <EOL> if isinstance ( field , ManyToManyField ) : <EOL> if ( getattr ( field , '<STR_LIT>' , False ) or <EOL> ( hasattr ( field . rel . through , '<STR_LIT>' ) and field . rel . through . _meta . auto_created ) ) : <EOL> add_relation ( field , '<STR_LIT>' ) <EOL> elif isinstance ( field , GenericRelation ) : <EOL> add_relation ( field , mark_safe ( '<STR_LIT>' ) ) <EOL> if inheritance : <EOL> for parent in appmodel . __bases__ : <EOL> if hasattr ( parent , "<STR_LIT>" ) : <EOL> l = "<STR_LIT>" <EOL> if parent . _meta . abstract : <EOL> l = "<STR_LIT>" <EOL> if appmodel . _meta . proxy : <EOL> l = "<STR_LIT>" <EOL> l += r"<STR_LIT>" <EOL> _rel = { <EOL> '<STR_LIT>' : parent . __module__ . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) , <EOL> '<STR_LIT:target>' : parent . __name__ , <EOL> '<STR_LIT:type>' : "<STR_LIT>" , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:label>' : l , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> if _rel not in model [ '<STR_LIT>' ] and use_model ( <EOL> _rel [ '<STR_LIT:target>' ] , <EOL> include_models , <EOL> exclude_columns <EOL> ) : <EOL> model [ '<STR_LIT>' ] . append ( _rel ) <EOL> graph [ '<STR_LIT>' ] . append ( model ) <EOL> if graph [ '<STR_LIT>' ] : <EOL> graphs . append ( graph ) <EOL> nodes = [ ] <EOL> for graph in graphs : <EOL> nodes . extend ( [ e [ '<STR_LIT:name>' ] for e in graph [ '<STR_LIT>' ] ] ) <EOL> for graph in graphs : <EOL> for model in graph [ '<STR_LIT>' ] : <EOL> for relation in model [ '<STR_LIT>' ] : <EOL> if relation [ '<STR_LIT:target>' ] in nodes : <EOL> relation [ '<STR_LIT>' ] = False <EOL> now = datetime . datetime . now ( ) <EOL> graph_data = { <EOL> '<STR_LIT>' : now . strftime ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : cli_options , <EOL> '<STR_LIT>' : disable_fields , <EOL> '<STR_LIT>' : use_subgraph , <EOL> '<STR_LIT>' : graphs , <EOL> } <EOL> return graph_data <EOL> def generate_dot ( graph_data ) : <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> if not isinstance ( t , Template ) and not ( hasattr ( t , '<STR_LIT>' ) and isinstance ( t . template , Template ) ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> c = Context ( graph_data ) <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> c = c . flatten ( ) <EOL> dot = t . render ( c ) <EOL> return dot </s>
