3
g]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 yd dlZdZW n ek
r   dZY nX y2yd dlZW n ek
r   d dlZY nX dZW n ek
r   dZY nX 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 )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          $      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d5d6d7d8d9d:dd;d<d=d>d?gd@dAdddBdCdddDdEdDdFdGdHgdIdAdJ| _ ttdKdL}|rxL| j D ]B}|jdM}|dN jdOjdOdP}||krf|| | j | dQ< qfW t j|| dLS )Ra  
        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
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_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_shapeZboxZcrowZcurveZicurveZdiamondinvnonenormalteeZ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   Z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rankdirZBTZLRZ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--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__ p/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/management/commands/graph_models.pyr+   #   s    


zCommand.__init__c             C   s:   |j ddd x&| jD ]}|j |jd| j|  qW dS )z/Unpack self.arguments for parser.add_arguments.	app_label*)nargsr"   N)add_argumentr%   r'   )r,   parserr0   r2   r2   r3   add_arguments   s    zCommand.add_argumentsc                s$  |d }| r<|d  r<t tdi jd}|r4|}ntd|jdpHd}tjj|\}}|j }dd	d
dg  fdd|j D }t	|j
 }|dkrtddjdd  D  |dkrtdd |j D }	nF|sd}	n<|dkrd}	n.|dkrd
}	n trd	}	ntrd}	ntd|jddkr<|	d'kr<td|	d(krV| rVtddjtjdd  }
t|fd|
i|}|j  |	d
kr|jdd }| j||S |jd!d }|d" }tjjd#d$|d%}tj|}t||d&}|	d	kr| j|f|S |	dkr| j|f|S | j|| d S ))Nr4   r   r!   Z
app_labelsz&need one or more arguments for appnamer    r   r   r   r   c                s   i | ]\}}| kr||qS r2   r2   ).0kv)output_opts_namesr2   r3   
<dictcomp>   s    z"Command.handle.<locals>.<dictcomp>   zOnly one of %s can be set.z, c             S   s   g | ]}d | qS )z--%sr2   )r;   optr2   r2   r3   
<listcomp>   s    z"Command.handle.<locals>.<listcomp>c             s   s   | ]\}}|r|V  qd S )Nr2   )r;   keyvalr2   r2   r3   	<genexpr>   s    z!Command.handle.<locals>.<genexpr>z.dotz.jsonzNeither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.r    r   z7--rankdir is not supported for the chosen output formatzQAn 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   r   )r   r   )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outputrG   rH   
graph_datar   Ztemplate_namerI   dotdatar2   )r>   r3   handle   s^    



zCommand.handleNc          	   C   sH   t |tr|j }|r8t|d}|j| W dQ R X n| jj| dS )z8Write model data to file or stdout in DOT (text) format.wtN)
isinstancebytesdecodeopenwritestdout)r,   r_   output_fileZdot_output_fr2   r2   r3   r[     s    
zCommand.print_outputc          
   C   s>   |r(t |d}tj|| W dQ R X n| jjtj| dS )z2Write model data to file or stdout in JSON format.ra   N)re   r   dumprg   rf   dumps)r,   r^   rh   Zjson_output_fr2   r2   r3   rX   &  s    zCommand.render_output_jsonc             K   s   t stdtjjd}yBtdd |jdD dk rXtj }|j	| |j
d |j}W n tk
rn   Y nX tj|}|j|d d	 |j|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 )N)int)r;   r=   r2   r2   r3   rE   5  s    z3Command.render_output_pygraphviz.<locals>.<genexpr>.r   $   r   )progr   N)r   rm   )rT   r   r   __version__rstriptupler'   tempfileNamedTemporaryFilerf   seekname
ValueErrorZAGraphr   Zdraw)r,   r_   r.   versionZtmpfilegraphr2   r2   r3   rY   .  s    



z Command.render_output_pygraphvizc          )   K   s   t stdtj|}|s"tdt|ttfrPt|dkrHtj	j
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/g)}||jd0d d1 }||kr|nd2}|j
||d3 d1S )4z'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   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xdotrl   Nraw)format)rU   r   r   Zgraph_from_dot_datarb   listrq   lenrV   stderrrf   rfind)r,   r_   r.   rx   rh   formatsextZformat_r2   r2   r3   rZ   B  s$    
zCommand.render_output_pydot)N)N)__name__
__module____qualname__r   Zcan_import_settingsr+   r9   r   r`   r[   rX   rY   rZ   __classcell__r2   r2   )r1   r3   r	      s    -F

r	   )rV   r   rK   rr   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.templater   Z%django_extensions.management.modelvizr   r   Z"django_extensions.management.utilsr   r   rT   ImportErrorZ	pydotplusr   rU   r	   r2   r2   r2   r3   <module>   s,   

