
    kh	F                        d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ 	 d dlZdZn# e$ r dZY nw xY w	 	 d dlZn# e$ r d dlZY nw xY wdZn# e$ r dZY nw xY wi fd	Z G d
 de          ZdS )    N)settings)BaseCommandCommandError)loader)
ModelGraphgenerate_dot)signalcommandTFc                    t          |t                    rt          j                            |          rq	 t          |d          5 }t          j        |          }d d d            n# 1 swxY w Y   n5# t          $ r&}t          d|            t          |          d }~ww xY w| S | d         D ]-}|D ](}d|v r"|d         D ]}|d         }||v r||         |d<   ).| S )NrtzInvalid app style file graphsnamemodelsapp_namestyle)

isinstancestrospathexistsopenjsonload	Exceptionprint)
graph_data	app_stylefegcgmr   s           i/var/www/histauto/venv/lib/python3.11/site-packages/django_extensions/management/commands/graph_models.pyrethemer#      sY   )S!! 	7>>)$$ 	#)T** -a $	!I- - - - - - - - - - - - - - - # # #;	;;<<<ll"# " 9 9 	9 	9A{{8 9 9A }H9,,%.x%8'
	9 s;   A3 A'A3 'A++A3 .A+/A3 3
B#=!BB#c                   b     e Zd ZdZ	 	 dZ fdZd Zed             ZddZ	ddZ
d	 Zd
 Z xZS )Commandz8Creates a GraphViz dot file for the specified app names.Tc                    i ddddddddd	d
ddddd	dddddd	dddddd	dddddd	dddddd	dddddd	dddddd	d d!dd"dd	d#d$dd%dd&d'd(d)dd*dd+d,d-dd.d/d0d,d1dd	d2d3dd4dd5d6d(d7dd8d9d(d:dd;d<d(dd=d>d(dd?d@dAddBd	d@dCddBd?dDdEddd	dFdGddBd?dHdIddd	dJdKddddLg dMdNdOdd	dPdQdddRdSg dTdUdOddVdWdXdYgdZdOd[| _         t          t          d\dV          }|rh| j         D ]`}|                    d]          }|d^                             d_                              d_d`          }||v r||         | j         |         da<   a t                      j        |i | dVS )ba  
        Allow defaults for arguments to be set in settings.GRAPH_MODELS.

        Each argument in self.arguments is a dict where the key is the
        space-separated args and the value is our kwarg dict.

        The default from settings is keyed as the long arg name with '--'
        removed and any '-' replaced by '_'. For example, the default value for
        --disable-fields can be set in settings.GRAPH_MODELS['disable_fields'].
        z--app-stylestorez1Path to style json to configure the style per app	app-stylez.app-style.json)actionhelpdestdefaultz--pygraphviz
store_trueF
pygraphvizz,Output graph data as image using PyGraphViz.)r)   r,   r+   r*   z--pydotpydotz-Output graph data as image using PyDot(Plus).z--dotdotzDOutput graph data as raw DOT (graph description language) text data.z--jsonr   zOutput graph data as JSONz--disable-fields -ddisable_fieldsz#Do not show the class member fieldsz--disable-abstract-fieldsdisable_abstract_fieldsz7Do not show the class member fields that were inheritedz--display-field-choicesdisplay_field_choicesz%Display choices instead of field typez--group-models -ggroup_modelsz5Group models together respective to their applicationz--all-applications -aall_applicationsz:Automatically include all applications from INSTALLED_APPSz--output -o
outputfileziRender output file. Type of output dependend on file extensions. Use png or jpg to render graph to image.)r)   r+   r*   z--layout -llayoutzaLayout to be used by GraphViz for visualization. Layouts: circo dot fdp neato nop nop1 nop2 twopi)r)   r+   r,   r*   z
--theme -ttheme
django2018zTheme to use. Supplied are 'original' and 'django2018'. You can create your own by creating dot templates in 'django_extentions/graph_models/themename/' template directory.z--verbose-names -nverbose_namesz%Use verbose_name of models and fieldsz--language -Llanguagez3Specify language used for verbose_name localizationz--exclude-columns -xexclude_columnszPExclude specific column(s) from the graph. Can also load exclude list from file.z--exclude-models -Xexclude_modelszjExclude specific model(s) from the graph. Can also load exclude list from file. Wildcards (*) are allowed.include_modelszBRestrict the graph to specified models. Wildcards (*) are allowed.Tinheritancez$Include inheritance arrows (default)store_falsez!Do not include inheritance arrowsrelations_as_fieldsz-Do not show relations as fields in the graph.relation_fields_onlyz3Only display fields that are relevant for relationssort_fieldszDo not sort fieldshide_edge_labelsz*Do not show relations labels in the graph.arrow_shape)boxcrowcurveicurvediamondr0   invnonenormalteeveezArrow shape to use for relations. Default is dot. Available shapes: box, crow, curve, icurve, diamond, dot, inv, none, normal, tee, vee.)r)   r,   r+   choicesr*   color_code_deletionszColor the relations according to their on_delete setting, where it is applicable. The colors are: red (CASCADE), orange (SET_NULL), green (SET_DEFAULT), yellow (SET), blue (PROTECT), grey (DO_NOTHING), and purple (RESTRICT).TBrankdir)rR   BTLRRLzSet direction of graph layout. Supported directions: TB, LR, BT and RL. Corresponding to directed graphs drawn from top to bottom, from left to right, from bottom to top, and from right to left, respectively. Default is TB.NorderinginoutzControls how the edges are arranged. Supported orderings: "in" (incoming relations first), "out" (outgoing relations first). Default is None.)z--include-models -Iz--inheritance -ez--no-inheritance -Ez--hide-relations-from-fields -Rz--relation-fields-onlyz--disable-sort-fields -Sz--hide-edge-labelsz--arrow-shapez--color-code-deletionsz	--rankdirz
--orderingGRAPH_MODELS r   -_r,   )	argumentsgetattrr   splitlstripreplacesuper__init__)selfargskwargsdefaultsargument	arg_splitsetting_opt	__class__s          r"   rd   zCommand.__init__<   sE   I
!K#,	 I
 & $F	 I
 & G	 I
& & ! 'I
8 & 3	 9I
D "& (=	$ $EI
P (& 1Q	* *QI
\ && /?	( (]I
h  & &O	" "iI
t $& *T	& &uI
@ !$?	 AI
P !  :	 QI
^ !'R	 _I
n !& '?	# #oI
z !"M {I
D #!)8% %EI
P "!(E$ $QI
^ "(-$ $ '%>	! ! ( %;	$ $ (-G	0 0 " .M	' ' (%,	) ) ' *D	# # " %  *# , ' .L	' ' "!333>	 	 "" %=# AI
 I
 I
V 8^T:: 	P N P P$NN3//	'l11#66>>sCHH(**:B;:ODN8,Y7$)&)))))    c                     |                     dd           | j        D ]-} |j         |                    d          i | j        |          .dS )z/Unpack self.arguments for parser.add_arguments.	app_label*)nargsr[   N)add_argumentr^   r`   )re   parserri   s      r"   add_argumentszCommand.add_arguments  sh    Ks333 	R 	RHF!4!4Qx8PQQQQ	R 	Rrm   c                 J   |d         }|sE|d         s=t          t          di                               d          }|r|}nt          d          |                    d          pd}t          j                            |          \  }}|                                }g dfd	|                                D             }t          |
                                          }|d
k    r/t          dd                    d D                       z            |d
k    r,t          d |                                D                       }	n:|sd}	n5|dk    rd}	n,|dk    rd}	n#t          rd}	nt          rd}	nt          d          |                    d          dk    r|	dvrt          d          |                    d          r|	dvrt          d          |	dv r|st          d          d                    t          j        dd                    }
t#          |fd |
i|}|                                 |	dk    r,|                    d!"          }|                     ||          S |                    d#"          }|d$         }t          j                            d%d&|d'          }t+          j        |          }t/          ||d(         )          }t1          ||*          }|	dk    r | j        |fi |S |	dk    r | j        |fi |S |                     ||           d S )+Nro   r5   rZ   
app_labelsz&need one or more arguments for appnamer6    )r/   r.   r   r0   c                 $    i | ]\  }}|v 	||S  ry   ).0kvoutput_opts_namess      r"   
<dictcomp>z"Command.handle.<locals>.<dictcomp>3  s*    RRR11@Q;Q;Qq!;Q;Q;Qrm      zOnly one of %s can be set.z, c                     g | ]}d |z  S )z--%sry   )rz   opts     r"   
<listcomp>z"Command.handle.<locals>.<listcomp>8  s    GGGcVc\GGGrm   c              3   $   K   | ]\  }}||V  d S Nry   )rz   keyvals      r"   	<genexpr>z!Command.handle.<locals>.<genexpr><  s+      II(#sSI#IIIIIIrm   r0   z.dotz.jsonr   r.   r/   zNeither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.rS   rR   )r/   r.   r0   z7--rankdir is not supported for the chosen output formatrW   z8--ordering is not supported for the chosen output format)r/   r.   zQAn output file (--output) must be specified when --pydot or --pygraphviz are set.r[      cli_optionsT)as_jsonFr8   django_extensionsgraph_modelszdigraph.dotr(   )r   )template)r_   r   getr   r   r   splitextloweritemssumvaluesjoinnextHAS_PYGRAPHVIZ	HAS_PYDOTsysargvr   generate_graph_dataget_graph_datarender_output_jsonr   get_templater#   r   render_output_pygraphvizrender_output_pydotprint_output)re   rf   optionsdefault_app_labelsr6   r]   outputfile_extoutput_optsoutput_opts_countoutputr   r   r   r8   template_namer   dotdatar}   s                    @r"   handlezCommand.handle#  s   {# 	MG$67 	M!(>2!F!F!J!J<!X!X! M)"#KLLL [[..4"
G,,Z88>'--//BBBRRRRRRR 2 2 4 455q  ,))GG5FGGGHHI  
 !!IIk.?.?.A.AIIIIIFF 	 FFv%%FFw&&FF 	!FF 	FFM  
 ;;y!!T))f =
 /
 /

 I   ;;z"" 	v5S'S'SJ   ,,,Z,(  
 hhsx|,,!$KKKK7KK((***V%44T4BBJ**:zBBB!000??
 
 
 &}55Z7;3GHHH
zH===\!!040DDGDDDW+4+G??w???':.....rm   Nc                    t          |t                    r|                                }|r@t          |d          5 }|                    |           ddd           dS # 1 swxY w Y   dS | j                            |           dS )z8Write model data to file or stdout in DOT (text) format.wtN)r   bytesdecoder   writestdout)re   r   output_filedot_output_fs       r"   r   zCommand.print_output  s    gu%% 	'nn&&G 	'k4(( ,L""7+++, , , , , , , , , , , , , , , , , , Kg&&&&&s   AA#&A#c                     |r@t          |d          5 }t          j        ||           ddd           dS # 1 swxY w Y   dS | j                            t          j        |                     dS )z2Write model data to file or stdout in JSON format.r   N)r   r   dumpr   r   dumps)re   r   r   json_output_fs       r"   r   zCommand.render_output_json  s     	6k4(( 5M	*m4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Kdj4455555s   6::c                    t           st          d          t          j                            d          }	 t          d |                    d          D                       dk     rDt          j                    }|	                    |           |
                    d           |j        }n# t          $ r Y nw xY wt          j        |          }|                    |d                    |                    |d	                    d
S )z,Render model data as image using pygraphviz.z,You need to install pygraphviz python modulez-svnc              3   4   K   | ]}t          |          V  d S r   )int)rz   r|   s     r"   r   z3Command.render_output_pygraphviz.<locals>.<genexpr>  s(      88SVV888888rm   .)r   $   r   r7   )progr6   N)r   r   r.   __version__rstriptupler`   tempfileNamedTemporaryFiler   seekr   
ValueErrorAGraphr7   draw)re   r   rg   versiontmpfilegraphs         r"   r   z Command.render_output_pygraphviz  s    	OMNNN(//77		88W]]3%7%7888887BB #577g&&&Q!, 	 	 	D	 !'**&*+++

6,'(((((s   A4B, ,
B98B9c                    t           st          d          t          j        |          }|st          d          t	          |t
          t          f          r:t          |          dk    rt          j	        
                    d           |d         }|d         }g d}||                    d          dz   d	         }||v r|nd
}|
                    ||           d	S )z'Render model data as image using pydot.z'You need to install pydot python modulezpydot returned an errorr   z9Found more then one graph, rendering only the first one.
r   r6   )*bmpcanoncmapcmapxcmapx_npr0   diaemfemfplusepsfiggdgd2gifgvimapimap_npismapjpejpegjpgmetafilepdfpicplainz	plain-extpngpovpsps2svgsvgztiftifftkvmlvmlzvrmlwbmpwebpxdotr   Nraw)format)r   r   r/   graph_from_dot_datar   listr   lenr   stderrr   rfind)re   r   rg   r   r   formatsextformat_s           r"   r   zCommand.render_output_pydot  s     	JHIII)'22 	:8999edE]++ 	5zzA~~
  P   !HE\*+
 +
 +
X +++C0014667##UK00000rm   r   )__name__
__module____qualname__r*   can_import_settingsrd   rt   r	   r   r   r   r   r   __classcell__)rl   s   @r"   r%   r%   5   s        EDK>_* _* _* _* _*BR R R Z/ Z/ ]Z/x	' 	' 	' 	'6 6 6 6) ) )*>1 >1 >1 >1 >1 >1 >1rm   r%   )r   r   r   r   django.confr   django.core.management.baser   r   django.templater   %django_extensions.management.modelvizr   r   "django_extensions.management.utilsr	   r.   r   ImportError	pydotplusr/   r   r#   r%   ry   rm   r"   <module>r     s   



  				              A A A A A A A A " " " " " " J J J J J J J J < < < < < <NN   NNN!!!!!   II   III #%    ,q1 q1 q1 q1 q1k q1 q1 q1 q1 q1s?   ; AA
A A! 	AA! AA! !A+*A+