
^Q\$                 @   s  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	 m
 Z
 d  d l m Z y d  d l Z d Z Wn e k
 r d Z Yn Xy; y d  d l Z Wn e k
 r d  d l Z Yn Xd Z Wn e k
 r d Z Yn XGd d	   d	 e  Z d S)
    N)settings)BaseCommandCommandError)
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
   Z	 d d   Z
 d d   Z d d   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  i i d d 6d d 6d d 6d 6i d d 6d d 6d	 d 6d
 6i d d 6d d 6d d 6d 6i d d 6d d 6d d 6d 6i d d 6d d 6d d 6d 6i d d 6d d 6d d 6d 6i d d 6d d 6d d 6d d 6d 6i d d 6d d 6d d 6d 6i d d 6d  d 6d! d 6d" 6i d d 6d# d 6d$ d 6d% 6i d d 6d& d 6d' d 6d( 6i d d 6d) d 6d* d 6d+ 6i d d 6d, d 6d- d 6d. d 6d/ 6i d0 d 6d, d 6d1 d 6d2 6i d0 d 6d3 d 6d- d 6d4 d 6d5 6i d0 d 6d6 d 6d- d 6d7 d 6d8 6i d d 6d9 d 6d: d 6d; 6|  _  t t d< d=  } | rxf |  j  D]X } | j d>  } | d? j d@  j d@ dA  } | | k r#| | |  j  | d <q#q#Wn  t t |   j | |   d= S)BaG  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 '_'.
        
store_trueaction
pygraphvizdestz%Use PyGraphViz to generate the image.helpz--pygraphvizpydotz&Use PyDot(Plus) to generate the image.z--pydotZdisable_fieldsz#Do not show the class member fieldsz--disable-fields -dZgroup_modelsz5Group models together respective to their applicationz--group-models -gall_applicationsz:Automatically include all applications from INSTALLED_APPSz--all-applications -astore
outputfileziRender output file. Type of output dependend on file extensions. Use png or jpg to render graph to image.z--output -olayoutdotdefaultzaLayout to be used by GraphViz for visualization. Layouts: circo dot fdp neato nop nop1 nop2 twopiz--layout -lZverbose_namesz%Use verbose_name of models and fieldsz--verbose-names -nlanguagez3Specify language used for verbose_name localizationz--language -LZexclude_columnszPExclude specific column(s) from the graph. Can also load exclude list from file.z--exclude-columns -xZexclude_modelszjExclude specific model(s) from the graph. Can also load exclude list from file. Wildcards (*) are allowed.z--exclude-models -XZinclude_modelszBRestrict the graph to specified models. Wildcards (*) are allowed.z--include-models -IZinheritanceTz$Include inheritance arrows (default)z--inheritance -estore_falsez!Do not include inheritance arrowsz--no-inheritance -EZrelations_as_fieldsz-Do not show relations as fields in the graph.z--hide-relations-from-fields -RZsort_fieldszDo not sort fieldsz--disable-sort-fields -SjsonzOutput graph data as JSONz--jsonZGRAPH_MODELSN r   -_)		argumentsgetattrr   splitlstripreplacesuperr   __init__)selfargskwargsdefaultsargumentZ	arg_splitZsetting_opt)	__class__ c/var/www/dbchiro/venv/build/django-extensions/django_extensions/management/commands/graph_models.pyr!   !   sv    	zCommand.__init__c             C   sK   | j  d d d x1 |  j D]& } | j  | j d  |  j |   q Wd S)z/Unpack self.arguments for parser.add_arguments.	app_labelnargs*r   N)add_argumentr   r   )r"   parserr&   r(   r(   r)   add_arguments   s    zCommand.add_argumentsc       
      O   s  | d } t  |  d k  r6 | d r6 t d   n  | j d d  } | j d d  } | j d d  } | r | s~ | r t d	   n  d
 j t j d d    } t | d | | } | j   | j d |  } | r |  j	 | |  d  St
 |  }	 t j s|	 j d  }	 n  | d r| rT| rTt rBd } qTt rTd } qTn  | rm|  j |	 |  q| r|  j |	 |  qt d   n |  j |	  d  S)Nr*      r   z&need one or more arguments for appnamer   Fr   r   z2Cannot specify --json with --pydot or --pygraphvizr      cli_optionsZas_jsonzutf-8r   TzENeither pygraphviz nor pydotplus could be found to generate the image)lenr   getjoinsysargvr   Zgenerate_graph_dataZget_graph_datarender_output_jsonr   sixPY3encodeHAS_PYGRAPHVIZ	HAS_PYDOTrender_output_pygraphvizrender_output_pydotprint_output)
r"   r#   optionsZuse_pygraphvizZ	use_pydotZuse_jsonr2   Zgraph_models
graph_datadotdatar(   r(   r)   handle   s<    

	
	zCommand.handlec             C   s8   t  j r* t | t  j  r* | j   } n  t |  d  S)N)r9   r:   
isinstancebinary_typedecodeprint)r"   rC   r(   r(   r)   r@      s    zCommand.print_outputc          
   K   sW   | j  d  } | r@ t | d   } t j | |  Wd  QXn t t j |   d  S)Nr   Zwt)r4   openr   dumprH   dumps)r"   rB   r$   output_fileZjson_output_fr(   r(   r)   r8      s
    zCommand.render_output_jsonc             K   s   t  s t d   n  t j j d  } yj t d d   | j d  D  d k  r d d l } | j   } | j	 |  | j
 d  | j } n  Wn t k
 r Yn Xt j |  } | j d	 | d
  | j | d  d S)z"Renders the image using pygraphvizz,You need to install pygraphviz python modulez-svnc             s   s   |  ] } t  |  Vq d  S)N)int).0vr(   r(   r)   	<genexpr>   s    z3Command.render_output_pygraphviz.<locals>.<genexpr>.r   $   Nprogr   r   )r   rR   )r<   r   r   __version__rstriptupler   tempfileNamedTemporaryFilewriteseekname
ValueErrorZAGraphr   Zdraw)r"   rC   r$   versionrW   Ztmpfilegraphr(   r(   r)   r>      s    (z Command.render_output_pygraphvizc          )   K   sW  t  s t d   n  t j |  } | s9 t d   n  t | t t f  r t |  d k rs t j	 j
 d  n  | d } n  | d } d d d	 d
 d d d d d d d d d d d d d 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 | d1 S)4zRenders the image using pydotz'You need to install pydot python modulezpydot returned an errorr0   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_npZismapZjpeZjpegZjpgZmetafileZpdfZpicplainz	plain-extZpngZpovZpsZps2svgZsvgzZtifZtiffZtkZvmlZvmlzZvrmlZwbmpZxdotrQ   Nrawformat)r=   r   r   Zgraph_from_dot_datarE   listrV   r3   r6   stderrrY   rfind)r"   rC   r$   r^   rL   formatsextrf   r(   r(   r)   r?      s$    
!zCommand.render_output_pydot)__name__
__module____qualname__r   Zcan_import_settingsr!   r/   r   rD   r@   r8   r>   r?   r(   r(   )r'   r)   r      s   `&r   )r6   r   r9   Zdjango.confr   Zdjango.core.management.baser   r   Z%django_extensions.management.modelvizr   r   Z"django_extensions.management.utilsr   r   r<   ImportErrorZ	pydotplusr   r=   r   r(   r(   r(   r)   <module>   s(   

