
    Ƿi]9                         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 G d	 d
e          ZdS )    N)settings)BaseCommandCommandError)loader)
ModelGraphgenerate_dot)signalcommandTFc                   ^     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 )CommandzCreates a GraphViz dot file for the specified app names. You can pass multiple app names and they will all be combined into a single model. Output is usually directed to a dot file.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.d d/dd0d1d d2dd3d4d d5dd6d7d d8dd9d:d;dd<dd:d=dd<d9d>d?dddd@dAdd<d9dBdCddddDdEddddFg dGdHdIdddJdKdddLdMg dNdOdIdP| _         t          t          dQdR          }|rh| j         D ]`}|                    dS          }|dT                             dU                              dUdV          }||v r||         | j         |         dW<   a t                      j        |i | dRS )Xa  
        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--pygraphviz
store_trueF
pygraphvizz,Output graph data as image using PyGraphViz.)actiondefaultdesthelpz--pydotpydotz-Output graph data as image using PyDot(Plus).z--dotdotzDOutput graph data as raw DOT (graph description language) text data.z--jsonjsonz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--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store
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.z--include-models -Iinclude_modelszBRestrict the graph to specified models. Wildcards (*) are allowed.z--inheritance -eT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diamondr   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 it applicable. The colors are: red (CASCADE), orange (SET_NULL), green (SET_DEFAULT), yellow (SET), blue (PROTECT), grey (DO_NOTHING) and purple (RESTRICT).TBrankdir)r7   BTLRRLzSet direction of graph layout. Supported directions: "TB", "LR", "BT", "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.)z--no-inheritance -Ez--hide-relations-from-fields -Rz--relation-fields-onlyz--disable-sort-fields -Sz--hide-edge-labelsz--arrow-shapez--color-code-deletionsz	--rankdirGRAPH_MODELSN r   -_r   )	argumentsgetattrr   splitlstripreplacesuper__init__)selfargskwargsdefaultsargument	arg_splitsetting_opt	__class__s          j/srv/django_bis/venv311/lib/python3.11/site-packages/django_extensions/management/commands/graph_models.pyrF   zCommand.__init__#   s   T
& $F	 T
 & G	 T
 & ^	 T
& & 3	 'T
2 "& (=	$ $3T
> (& 1Q	* *?T
J  & &O	" "KT
V $& *T	& &WT
b !$ D cT
l !  {	 mT
x !' M	 yT
D !& '?	# #ET
P !"M QT
Z #!)j% %[T
d "!( E$ $eT
n "!(\$ $oT
x &%>	! !yT
F ( %;	$ $ (-G	0 0 " .M	' ' (%,	) ) ' *D	# # " %vvv c  ' . z	' ' "!333  [T
 T
 T
l 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@   rB   )rG   parserrK   s      rO   add_argumentszCommand.add_arguments   sh    Ks333 	R 	RHF!4!4Qx8PQQQQ	R 	RrP   c                    |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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k    r | j        |fi |S |	dk    r | j        |fi |S |                     ||           d S )'NrR   r   r<   
app_labelsz&need one or more arguments for appnamer    )r   r   r   r   c                 $    i | ]\  }}|v 	||S  r\   ).0kvoutput_opts_namess      rO   
<dictcomp>z"Command.handle.<locals>.<dictcomp>   s*    RRR11@Q;Q;Qq!;Q;Q;QrP      zOnly one of %s can be set.z, c                     g | ]}d |z  S )z--%sr\   )r]   opts     rO   
<listcomp>z"Command.handle.<locals>.<listcomp>   s    HsHsHsZ]RUHsHsHsrP   c              3   $   K   | ]\  }}||V  d S Nr\   )r]   keyvals      rO   	<genexpr>z!Command.handle.<locals>.<genexpr>   s+      II(#sSI#IIIIIIrP   r   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.r8   r7   )r   r   r   z7--rankdir 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_jsonFr   django_extensionsgraph_modelszdigraph.dot)template)rA   r   getr   ospathsplitextloweritemssumvaluesjoinnextHAS_PYGRAPHVIZ	HAS_PYDOTsysargvr   generate_graph_dataget_graph_datarender_output_jsonr   get_templater   render_output_pygraphvizrender_output_pydotprint_output)rG   rH   optionsdefault_app_labelsr   r?   outputfile_extoutput_optsoutput_opts_countoutputrl   ro   
graph_datar   template_namerp   dotdatar`   s                    @rO   handlezCommand.handle   sd   {# 	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  ;diiHsHsarHsHsHs>t>ttuuu!!IIk.?.?.A.AIIIIIFF 	c FFv%%FFw&&FF 	c!FF 	cFF   b  c  c  c;;y!!T))f<Z.Z.ZXYYY
 ,,,Z,rssshhsx|,,!$KKKK7KK((***V%44T4BBJ**:zBBB!000??
 %8.%Q^__&}55zH===\!!040DDGDDDW+4+G??w???':.....rP   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)
isinstancebytesdecodeopenwritestdout)rG   r   output_filedot_output_fs       rO   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)rG   r   r   json_output_fs       rO   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 rg   )int)r]   r_   s     rO   rj   z3Command.render_output_pygraphviz.<locals>.<genexpr>5  s(      88SVV888888rP   .)r   $   r   r   )progr   N)r{   r   r   __version__rstriptuplerB   tempfileNamedTemporaryFiler   seekname
ValueErrorAGraphr   draw)rG   r   rI   versiontmpfilegraphs         rO   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 errorrb   z9Found more then one graph, rendering only the first one.
r   r   ))bmpcanoncmapcmapxcmapx_npr   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xdotr   Nraw)format)r|   r   r   graph_from_dot_datar   listr   lenr}   stderrr   rfind)rG   r   rI   r   r   formatsextformat_s           rO   r   zCommand.render_output_pydotB  s     	JHIII)'22 	:8999edE]++ 	5zzA~~
  !]^^^!HE\*
 
 
 +++C0014556##UK00000rP   rg   )__name__
__module____qualname__r   can_import_settingsrF   rW   r	   r   r   r   r   r   __classcell__)rN   s   @rO   r   r      s         CDj* j* j* j* j*XR R R C/ C/ ]C/J	' 	' 	' 	'6 6 6 6) ) )(1 1 1 1 1 1 1rP   r   )r}   r   rr   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\   rP   rO   <module>r      su   



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