a
    ܌xd84                     @   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 )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d3g d4d5d6d7| _ ttd8d9}|rh| j D ]B}|d:}|d; d<d<d=}||v r$|| | j | d>< q$t j|i | d9S )?a  
        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sort_fieldszDo not sort fieldsZhide_edge_labelsz)Do not showrelations labels in the graph.Zarrow_shape)boxZcrowZcurveZicurveZdiamondr   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--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--disable-sort-fields -Sz--hide-edge-labelsz--arrow-shapeGRAPH_MODELSN r   -_r   )	argumentsgetattrr   splitlstripreplacesuper__init__)selfargskwargsdefaultsargumentZ	arg_splitZsetting_opt	__class__ j/var/www/html/Ranjet/env/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+   parserr/   r2   r2   r3   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rtdddd  D  |dkrtdd | D }	nD|sd}	n:|dkrd}	n,|dkrd}	ntrd}	ntrd}	ntd|	dv r(|s(tddtjdd  }
t|fd|
i|}|  |	dkrz|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 )&Nr4   r   r    Z
app_labelsz&need one or more arguments for appnamer    )r   r   r   r   c                    s   i | ]\}}| v r||qS r2   r2   ).0kvZoutput_opts_namesr2   r3   
<dictcomp>       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>   r@   z"Command.handle.<locals>.<listcomp>c                 s   s   | ]\}}|r|V  qd S Nr2   )r;   keyvalr2   r2   r3   	<genexpr>   r@   z!Command.handle.<locals>.<genexpr>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.)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outputrI   rJ   
graph_datar   Ztemplate_namerK   dotdatar2   r>   r3   handle   sZ    



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+   ra   output_fileZdot_output_fr2   r2   r3   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.rc   N)rg   r   dumpri   rh   dumps)r+   r`   rj   Zjson_output_fr2   r2   r3   rZ     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 rD   )int)r;   r=   r2   r2   r3   rG     r@   z3Command.render_output_pygraphviz.<locals>.<genexpr>.)r   $   r   r   )progr   N)rV   r   r   __version__rstriptupler&   tempfileNamedTemporaryFilerh   seekname
ValueErrorZAGraphr   Zdraw)r+   ra   r-   versionZtmpfilegraphr2   r2   r3   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 errorrA   z9Found more then one graph, rendering only the first one.
r   r   ))ZbmpZcanonZcmapZcmapxZcmapx_npr   ZdiaZemfZemZfplusZepsZfiggdZgd2ZgifgvimapZimap_npZismapZjpeZjpegZjpgZmetafileZpdfZpicplainz	plain-extZpngpovZpsZps2svgZsvgzZtifZtiffZtkZvmlZvmlzZvrmlZwbmpZxdotrn   Nraw)format)rW   r   r   Zgraph_from_dot_datard   listrs   lenrX   stderrrh   rfind)r+   ra   r-   rz   rj   formatsextZformat_r2   r2   r3   r\   ,  s    
zCommand.render_output_pydot)N)N)__name__
__module____qualname__r   Zcan_import_settingsr*   r9   r   rb   r]   rZ   r[   r\   __classcell__r2   r2   r0   r3   r	      s    
B

r	   )rX   r   rM   rt   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.templater   Z%django_extensions.management.modelvizr   r   Z"django_extensions.management.utilsr   r   rV   ImportErrorZ	pydotplusr   rW   r	   r2   r2   r2   r3   <module>   s,   

