
    jg]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	 	 d dlZdZ G d	 d
e      Zy# e$ r dZY w xY w# e$ r d dlZY &w xY w# e$ r dZY 1w xY w)    N)settings)BaseCommandCommandError)loader)
ModelGraphgenerate_dot)signalcommandTFc                   X     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      }|r`| j                   D ]Q  }|j                  dS      }|dT   j	                  dU      j                  dUdV      }||v s=||   | j                   |   dW<   S t        |   |i | yR)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          m/var/www/html/Stanley/venv/lib/python3.12/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 " .M	' (%,	) ' *D	# " %v c ' . z	' "!3 [T
l 8^T: NN$NN3/	'l11#6>>sCH(*:B;:ODNN8,Y7	 + 	$)&)    c                     |j                  dd       | j                  D ]0  } |j                   |j                  d      i | j                  |    2 y)z/Unpack self.arguments for parser.add_arguments.	app_label*)nargsr=   N)add_argumentr@   rB   )rG   parserrK   s      rO   add_argumentszCommand.add_arguments   sJ    Ks3HF!4Qx8PQ 'rP   c           
      4   |d   }|s5|d   s0t        t        di       j                  d      }|r|}nt        d      |j                  d      xs d}t        j
                  j                  |      \  }}|j                         }g d}|j                         D 	ci c]  \  }}	||v s||	 }
}}	t        |
j                               }|d	kD  r.t        d
dj                  |D cg c]  }d|z  	 c}      z        |d	k(  r!t        d |
j                         D              }n2|sd}n-|dk(  rd}n%|dk(  rd}nt        rd}nt        rd}nt        d      |j                  d      dk7  r|dvrt        d      |dv r|st        d      dj                  t        j                   dd        }t#        |fd|i|}|j%                          |dk(  r$|j'                  d      }| j)                  ||      S |j'                  d       }|d!   }t        j
                  j                  d"d#|d$      }t+        j,                  |      }t/        ||%      }|dk(  r | j0                  |fi |S |dk(  r | j2                  |fi |S | j5                  ||       y c c}	}w c c}w )&NrR   r   r<   
app_labelsz&need one or more arguments for appnamer    )r   r   r   r      zOnly one of %s can be set.z, z--%sc              3   ,   K   | ]  \  }}|s	|  y wN ).0keyvals      rO   	<genexpr>z!Command.handle.<locals>.<genexpr>   s     I.A(#sS#.As   
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_nameskvoutput_optsoutput_opts_countoptoutputrd   rg   
graph_datar   template_namerh   dotdatas                        rO   handlezCommand.handle   s   {#G$67!(>2!F!J!J<!X!)"#KLL [[.4"
GG,,Z8>'--/B(/R11@Q;Qq!tR 2 2 45q ;diiarHsarZ]RUarHs>ttuu!Ik.?.?.AIIF Fv%Fw&F!FF   b  c  c;;y!T)f<Z.ZXYY
 ,,Zrsshhsxx|,!$KKK7K((*V%44T4BJ**:zBB!000?
 %8.%Q^_&&}5zH=\!0400DGDDW+4++G?w??':.i S Its   J(J#Jc                     t        |t              r|j                         }|r't        |d      5 }|j	                  |       ddd       y| j
                  j	                  |       y# 1 sw Y   yxY w)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  sZ    gu%nn&Gk4(L""7+ )( KKg& )(s   A&&A/c                     |r,t        |d      5 }t        j                  ||       ddd       y| j                  j	                  t        j
                  |             y# 1 sw Y   yxY w)z2Write model data to file or stdout in JSON format.r   N)r   r   dumpr   r   dumps)rG   r   r   json_output_fs       rO   ry   zCommand.render_output_json&  sM    k4(M		*m4 )( KKdjj45 )(s   AA'c                    t         st        d      t        j                  j	                  d      }	 t        d |j                  d      D              dk  rBt        j                         }|j                  |       |j                  d       |j                  }t        j                  |      }|j                  |d          |j                  |d	          y
# t        $ r Y Jw xY w)z,Render model data as image using pygraphviz.z,You need to install pygraphviz python modulez-svnc              3   2   K   | ]  }t        |        y wr]   )int)r_   r   s     rO   rb   z3Command.render_output_pygraphviz.<locals>.<genexpr>5  s     8%7SV%7s   .)r   $   r   r   )progr   N)rs   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    MNN((//7	8W]]3%7887B"557g&Q!,, !!'*&*+

6,'(  		s   A&C 	C#"C#c                 p   t         st        d      t        j                  |      }|st        d      t	        |t
        t        f      r2t        |      dkD  rt        j                  j                  d       |d   }|d   }g d}||j                  d      dz   d	 }||v r|nd
}|j                  ||       y	)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   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)rt   r   r   graph_from_dot_datar   listr   lenru   stderrr   rfind)rG   r   rI   r   r   formatsextformat_s           rO   r|   zCommand.render_output_pydotB  s    HII))'2899edE]+5zA~

  !]^!HE\*
 +++C01456#UK0rP   r]   )__name__
__module____qualname__r   can_import_settingsrF   rW   r	   r   r}   ry   r{   r|   __classcell__)rN   s   @rO   r   r      sI     CDj*XR C/ C/J	'6)(1rP   r   )ru   r   rj   r   django.confr   django.core.management.baser   r   django.templater   %django_extensions.management.modelvizr   r   "django_extensions.management.utilsr	   r   rs   ImportError	pydotplusr   rt   r   r^   rP   rO   <module>r      s    
  	    A " J <N! I
{1k {1  N    Is@   A A  A* AA	A'$A* &A''A* *A43A4