a
    ÆZEg]9  ã                   @   sè   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 zd dlZdZW n eyŠ   dZY n0 z0zd dlZW n ey´   d dlZY n0 dZW n eyÒ   dZY n0 G d	d
„ d
eƒZdS )é    N)Úsettings)ÚBaseCommandÚCommandError)Úloader)Ú
ModelGraphÚgenerate_dot)ÚsignalcommandTFc                       s\   e Zd ZdZdZ‡ fdd„Zdd„ Zedd„ ƒZdd
d„Z	ddd„Z
dd„ Zdd„ Z‡  ZS )ÚCommandzµCreates 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                    s¨  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)d*dœd+dd)d,dœd+d(d-d.dœddd/d0dœd+d(d1d2dœddd3d4dœddd5g d6¢d7d8œddd9d:dœdd;d<g d=¢d>d8œd?œ| _ ttd@dAƒ}|�r’| j D ]B}| dB¡}|dC  dD¡ dDdE¡}||v �rN|| | j | dF< �qNtƒ j|i |¤Ž dAS )GaÃ  
        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'].
        Ú
store_trueFÚ
pygraphvizz,Output graph data as image using PyGraphViz.)ÚactionÚdefaultÚdestÚhelpÚpydotz-Output graph data as image using PyDot(Plus).ÚdotzDOutput graph data as raw DOT (graph description language) text data.ÚjsonzOutput graph data as JSONZdisable_fieldsz#Do not show the class member fieldsZdisable_abstract_fieldsz7Do not show the class member fields that were inheritedZgroup_modelsz5Group models together respective to their applicationÚall_applicationsz:Automatically include all applications from INSTALLED_APPSÚstoreÚ
outputfileziRender output file. Type of output dependend on file extensions. Use png or jpg to render graph to image.)r   r   r   ÚlayoutzaLayout to be used by GraphViz for visualization. Layouts: circo dot fdp neato nop nop1 nop2 twopi)r   r   r   r   ÚthemeZ
django2018z¬Theme 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_namesz%Use verbose_name of models and fieldsÚlanguagez3Specify language used for verbose_name localizationZexclude_columnszPExclude specific column(s) from the graph. Can also load exclude list from file.Zexclude_modelszjExclude specific model(s) from the graph. Can also load exclude list from file. Wildcards (*) are allowed.Zinclude_modelszBRestrict the graph to specified models. Wildcards (*) are allowed.TZinheritancez$Include inheritance arrows (default)Ústore_falsez!Do not include inheritance arrowsZrelations_as_fieldsz-Do not show relations as fields in the graph.Zrelation_fields_onlyz3Only display fields that are relevant for relationsZsort_fieldszDo not sort fieldsZhide_edge_labelsz*Do not show relations labels in the graph.Zarrow_shape)ZboxZcrowZcurveZicurveZdiamondr   ÚinvÚnoneÚnormalÚteeZveezˆArrow 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   Zcolor_code_deletionszßColor 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)r   ZBTZLRZRLzäSet 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--pygraphvizz--pydotz--dotz--jsonz--disable-fields -dz--disable-abstract-fieldsz--group-models -gz--all-applications -az--output -oz--layout -lz
--theme -tz--verbose-names -nz--language -Lz--exclude-columns -xz--exclude-models -Xz--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	--rankdirÚGRAPH_MODELSNú r   ú-Ú_r   )Ú	argumentsÚgetattrr   ÚsplitÚlstripÚreplaceÚsuperÚ__init__)ÚselfÚargsÚkwargsÚdefaultsÚargumentZ	arg_splitZsetting_opt©Ú	__class__© úk/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/management/commands/graph_models.pyr+   #   s
   üüüüüüüüýüüüýýýýüüüüüüûüû€ ó 


zCommand.__init__c                 C   s:   |j ddd� | jD ] }|j | d¡i | j| ¤Ž qdS )z/Unpack self.arguments for parser.add_arguments.Ú	app_labelÚ*)Únargsr"   N)Úadd_argumentr%   r'   )r,   Úparserr0   r3   r3   r4   Úadd_argumentsÏ   s    
zCommand.add_argumentsc                    s   |d }|s8|d s8t tdi ƒ d¡}|r0|}ntdƒ‚| d¡pDd}tj |¡\}}| ¡ }g d¢‰ ‡ fd	d
„| ¡ D ƒ}t	| 
¡ ƒ}|dkr¬tdd dd„ ˆ D ƒ¡ ƒ‚|dkrÌtdd„ | ¡ D ƒƒ}	nD|sÖd}	n:|dkräd}	n,|dkròd}	ntrüd}	nt�rd}	ntdƒ‚| d¡dk�r2|	dv�r2tdƒ‚|	dv �rJ|�sJtdƒ‚d tjd d … ¡}
t|fd!|
i|¤Ž}| ¡  |	dk�rœ|jd"d#�}|  ||¡S |jd$d#�}|d% }tj d&d'|d(¡}t |¡}t||d)�}|	dk�rô| j|fi |¤ŽS |	dk�r| j|fi |¤ŽS |  ||¡ d S )*Nr5   r   r!   Z
app_labelsz&need one or more arguments for appnamer   Ú )r   r   r   r   c                    s   i | ]\}}|ˆ v r||“qS r3   r3   )Ú.0ÚkÚv©Zoutput_opts_namesr3   r4   Ú
<dictcomp>å   ó    z"Command.handle.<locals>.<dictcomp>é   zOnly one of %s can be set.z, c                 S   s   g | ]}d | ‘qS )z--%sr3   )r<   Úoptr3   r3   r4   Ú
<listcomp>è   rA   z"Command.handle.<locals>.<listcomp>c                 s   s   | ]\}}|r|V  qd S ©Nr3   )r<   ÚkeyÚvalr3   r3   r4   Ú	<genexpr>ë   rA   z!Command.handle.<locals>.<genexpr>r   z.dotz.jsonr   r   r   z€Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.r    r   )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)Zas_jsonFr   Zdjango_extensionsÚgraph_modelszdigraph.dot)Útemplate)r&   r   Úgetr   ÚosÚpathÚsplitextÚlowerÚitemsÚsumÚvaluesÚjoinÚnextÚHAS_PYGRAPHVIZÚ	HAS_PYDOTÚsysÚargvr   Zgenerate_graph_dataZget_graph_dataÚrender_output_jsonr   Zget_templater   Úrender_output_pygraphvizÚrender_output_pydotÚprint_output)r,   r-   ÚoptionsZdefault_app_labelsr   r$   Zoutputfile_extZoutput_optsZoutput_opts_countÚoutputrJ   rK   Ú
graph_datar   Ztemplate_namerL   Údotdatar3   r?   r4   ÚhandleÕ   s^    



zCommand.handleNc                 C   s\   t |tƒr| ¡ }|rLt|dƒ�}| |¡ W d  ƒ qX1 s@0    Y  n| j |¡ dS )z8Write model data to file or stdout in DOT (text) format.ÚwtN)Ú
isinstanceÚbytesÚdecodeÚopenÚwriteÚstdout)r,   rb   Úoutput_fileZdot_output_fr3   r3   r4   r^     s    
*zCommand.print_outputc                 C   sR   |r<t |dƒ�}t ||¡ W d  ƒ qN1 s00    Y  n| j t |¡¡ dS )z2Write model data to file or stdout in JSON format.rd   N)rh   r   Údumprj   ri   Údumps)r,   ra   rk   Zjson_output_fr3   r3   r4   r[   &  s    ,zCommand.render_output_jsonc                 K   sš   t stdƒ‚tj d¡}zBtdd„ | d¡D ƒƒdk rXt ¡ }| 	|¡ | 
d¡ |j}W n tyl   Y n0 t |¡}|j|d d	� | |d
 ¡ dS )z,Render model data as image using pygraphviz.z,You need to install pygraphviz python modulez-svnc                 s   s   | ]}t |ƒV  qd S rE   )Úint)r<   r>   r3   r3   r4   rH   5  rA   z3Command.render_output_pygraphviz.<locals>.<genexpr>Ú.)r   é$   r   r   )Úprogr   N)rW   r   r   Ú__version__ÚrstripÚtupler'   ÚtempfileÚNamedTemporaryFileri   ÚseekÚnameÚ
ValueErrorZAGraphr   Zdraw)r,   rb   r.   ÚversionZtmpfileÚgraphr3   r3   r4   r\   .  s    



z Command.render_output_pygraphvizc                 K   s˜   t stdƒ‚t |¡}|s"tdƒ‚t|ttfƒrPt|ƒdkrHtj	 
d¡ |d }|d }g d¢}|| d¡d d	… }||v r‚|nd
}|j
||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   ))ZbmpZcanonZcmapZcmapxZcmapx_npr   ZdiaZemfÚemZfplusZepsZfigÚgdZgd2ZgifÚgvÚimapZimap_npÚismapZjpeZjpegZjpgZmetafileZpdfZpicÚplainz	plain-extZpngZpovZpsZps2ÚsvgZsvgzZtifZtiffZtkZvmlZvmlzZvrmlZwbmpZxdotro   NÚraw)Úformat)rX   r   r   Zgraph_from_dot_datare   Úlistrt   ÚlenrY   Ústderrri   Úrfind)r,   rb   r.   r{   rk   ÚformatsÚextZformat_r3   r3   r4   r]   B  s    
zCommand.render_output_pydot)N)N)Ú__name__Ú
__module__Ú__qualname__r   Zcan_import_settingsr+   r:   r   rc   r^   r[   r\   r]   Ú__classcell__r3   r3   r1   r4   r	      s    -
E

r	   )rY   r   rN   ru   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.templater   Z%django_extensions.management.modelvizr   r   Z"django_extensions.management.utilsr   r   rW   ÚImportErrorZ	pydotplusr   rX   r	   r3   r3   r3   r4   Ú<module>   s,   

