ó
¸9—^c           @   s6  d  d l  Z  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 e Z Wn e k
 rÇ e Z n Xy: y d  d l Z Wn e k
 rú d  d l Z n Xe Z Wn e k
 re Z n Xd e f d „  ƒ  YZ d S(	   iÿÿÿÿN(   t   settings(   t   BaseCommandt   CommandError(   t   loader(   t
   ModelGrapht   generate_dot(   t   signalcommandt   Commandc           B   s_   e  Z d  Z e Z d „  Z d „  Z e d „  ƒ Z d d „ Z
 d d „ Z d „  Z d „  Z RS(	   sµ   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.c         O   s®  i i d d 6t  d 6d d 6d d 6d 6i d d 6t  d 6d	 d 6d
 d 6d 6i d d 6t  d 6d d 6d d 6d 6i d d 6t  d 6d d 6d d 6d 6i d d 6t  d 6d d 6d d 6d 6i d d 6t  d 6d d 6d d 6d 6i d d 6t  d 6d d 6d d 6d 6i d d 6t  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' d 6d( 6i d d 6t  d 6d) d 6d* d 6d+ 6i d d 6d, d 6d- d 6d. 6i d d 6d/ d 6d0 d 6d1 6i d d 6d2 d 6d3 d 6d4 6i d d 6d5 d 6d6 d 6d7 6i d d 6t d 6d8 d 6d9 d 6d: 6i d; d 6t  d 6d8 d 6d< d 6d= 6i d; d 6t d 6d> d 6d? d 6d@ 6i d; d 6t d 6dA d 6dB d 6dC 6i d d 6t  d 6dD d 6dE d 6dF 6i d d 6d d 6dG d 6dH dI dJ dK dL d dM dN dO dP dQ g dR 6dS d 6dT 6|  _ t t dU dZ ƒ } | r‘xf |  j D]X } | j dV ƒ } | dW j dX ƒ j dX dY ƒ } | | k r2| | |  j | d <q2q2Wn  t	 t
 |  ƒ j | | Ž  dZ S([   sÃ  
        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'].
        t
   store_truet   actiont   defaultt
   pygraphvizt   dests,   Output graph data as image using PyGraphViz.t   helps   --pygraphvizt   pydots-   Output graph data as image using PyDot(Plus).s   --pydott   dotsD   Output graph data as raw DOT (graph description language) text data.s   --dott   jsons   Output graph data as JSONs   --jsont   disable_fieldss#   Do not show the class member fieldss   --disable-fields -dt   disable_abstract_fieldss7   Do not show the class member fields that were inheriteds   --disable-abstract-fieldst   group_modelss5   Group models together respective to their applications   --group-models -gt   all_applicationss:   Automatically include all applications from INSTALLED_APPSs   --all-applications -at   storet
   outputfilesi   Render output file. Type of output dependend on file extensions. Use png or jpg to render graph to image.s   --output -ot   layoutsa   Layout to be used by GraphViz for visualization. Layouts: circo dot fdp neato nop nop1 nop2 twopis   --layout -lt   themet
   django2018s¬   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.s
   --theme -tt   verbose_namess%   Use verbose_name of models and fieldss   --verbose-names -nt   languages3   Specify language used for verbose_name localizations   --language -Lt   exclude_columnssP   Exclude specific column(s) from the graph. Can also load exclude list from file.s   --exclude-columns -xt   exclude_modelssj   Exclude specific model(s) from the graph. Can also load exclude list from file. Wildcards (*) are allowed.s   --exclude-models -Xt   include_modelssB   Restrict the graph to specified models. Wildcards (*) are allowed.s   --include-models -It   inheritances$   Include inheritance arrows (default)s   --inheritance -et   store_falses!   Do not include inheritance arrowss   --no-inheritance -Et   relations_as_fieldss-   Do not show relations as fields in the graph.s   --hide-relations-from-fields -Rt   sort_fieldss   Do not sort fieldss   --disable-sort-fields -St   hide_edge_labelss)   Do not showrelations labels in the graph.s   --hide-edge-labelst   arrow_shapet   boxt   crowt   curvet   icurvet   diamondt   invt   nonet   normalt   teet   veet   choicessˆ   Arrow shape to use for relations. Default is dot. Available shapes: box, crow, curve, icurve, diamond, dot, inv, none, normal, tee, vee.s   --arrow-shapet   GRAPH_MODELSt    i    t   -t   _N(   t   Falset   Truet	   argumentst   getattrR    t   Nonet   splitt   lstript   replacet   superR   t   __init__(   t   selft   argst   kwargst   defaultst   argumentt	   arg_splitt   setting_opt(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyR=   $   sæ    (c         C   sK   | j  d d d ƒx1 |  j D]& } | j  | j d ƒ |  j | Ž  q Wd S(   s/   Unpack self.arguments for parser.add_arguments.t	   app_labelt   nargst   *R1   N(   t   add_argumentR6   R9   (   R>   t   parserRB   (    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyt   add_arguments½   s    c            sµ  | d } | r+ | d r+ t  d ƒ ‚ n  | j d ƒ p= d } t j j | ƒ \ } } | j ƒ  } d d d d	 g ‰  ‡  f d
 †  | j ƒ  Dƒ } t | j ƒ  ƒ } | d k rã t  d d j	 g  ˆ  D] } d | ^ qÃ ƒ ƒ ‚ n  | d k rt
 d „  | j ƒ  Dƒ ƒ }	 nc | sd	 }	 nT | d k r2d	 }	 n? | d k rGd }	 n* t rVd }	 n t red }	 n t  d ƒ ‚ |	 d k r“| r“t  d ƒ ‚ n  d j	 t j d ƒ }
 t | d |
 | } | j ƒ  |	 d k rö| j d t ƒ } |  j | | ƒ S| j d t ƒ } | d } t j j	 d d | d ƒ } t j | ƒ } t | d | ƒ} t j si| j d ƒ } n  |	 d k r…|  j | |  S|	 d k r¡|  j | |  S|  j | | ƒ d  S(   NRE   R   s&   need one or more arguments for appnameR   t    R   R   R   R   c            s+   i  |  ]! \ } } | ˆ  k r | | “ q S(    (    (   t   .0t   kt   v(   t   output_opts_names(    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pys
   <dictcomp>Ï   s   	 i   s   Only one of %s can be set.s   , s   --%sc         s   s!   |  ] \ } } | r | Vq d  S(   N(    (   RL   t   keyt   val(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pys	   <genexpr>Õ   s    s   .dots   .jsons€   Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.sQ   An output file (--output) must be specified when --pydot or --pygraphviz are set.R1   i   t   cli_optionst   as_jsonR   t   django_extensionst   graph_modelss   digraph.dott   templates   utf-8(   R   R   (   R   t   gett   ost   patht   splitextt   lowert   itemst   sumt   valuest   joint   nextt   HAS_PYGRAPHVIZt	   HAS_PYDOTt   syst   argvR   t   generate_graph_datat   get_graph_dataR5   t   render_output_jsonR4   R   t   get_templateR   t   sixt   PY3t   encodet   render_output_pygraphvizt   render_output_pydott   print_output(   R>   R?   t   optionsR   R3   t   outputfile_extt   output_optst   output_opts_countt   optt   outputRR   RU   t
   graph_dataR   t   template_nameRV   t   dotdata(    (   RO   sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyt   handleÃ   sX    
3					

	c         C   sl   t  j r* t | t  j ƒ r* | j ƒ  } n  | rX t | d ƒ  } | j | ƒ Wd QXn |  j j | ƒ d S(   s8   Write model data to file or stdout in DOT (text) format.t   wtN(   Ri   Rj   t
   isinstancet   binary_typet   decodet   opent   writet   stdout(   R>   Rw   t   output_filet   dot_output_f(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyRn     s    c         C   sN   | r1 t  | d ƒ  } t j | | ƒ Wd QXn |  j j t j | ƒ ƒ d S(   s2   Write model data to file or stdout in JSON format.Ry   N(   R}   R   t   dumpR   R~   t   dumps(   R>   Ru   R€   t   json_output_f(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyRg     s    c         K   sÎ   t  s t d ƒ ‚ n  t j j d ƒ } y[ t d „  | j d ƒ Dƒ ƒ d k  r t j ƒ  } | j	 | ƒ | j
 d ƒ | j } n  Wn t k
 r• n Xt j | ƒ } | j d | d ƒ | j | d	 ƒ d
 S(   s,   Render model data as image using pygraphviz.s,   You need to install pygraphviz python modules   -svnc         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   int(   RL   RN   (    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pys	   <genexpr>  s    t   .i    i$   t   progR   R   N(   i    i$   (   Ra   R   R   t   __version__t   rstript   tupleR9   t   tempfilet   NamedTemporaryFileR~   t   seekt   namet
   ValueErrort   AGraphR   t   draw(   R>   Rw   R@   t   versiont   tmpfilet   graph(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyRl     s    %c      )   K   sQ  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 } | | k r4| n d1 } | j
 | d2 | ƒd3 S(4   s'   Render model data as image using pydot.s'   You need to install pydot python modules   pydot returned an errori   s9   Found more then one graph, rendering only the first one.
i    R   t   bmpt   canont   cmapt   cmapxt   cmapx_npR   t   diat   emft   emt   fplust   epst   figt   gdt   gd2t   gift   gvt   imapt   imap_npt   ismapt   jpet   jpegt   jpgt   metafilet   pdft   pict   plains	   plain-extt   pngt   povt   pst   ps2t   svgt   svgzt   tift   tifft   tkt   vmlt   vmlzt   vrmlt   wbmpt   xdotR†   t   rawt   formatN(   Rb   R   R   t   graph_from_dot_dataRz   t   listRŠ   t   lenRc   t   stderrR~   t   rfind(   R>   Rw   R@   R”   R€   t   formatst   extt   format_(    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyRm   +  s$    
!N(   t   __name__t
   __module__R   R5   t   can_import_settingsR=   RJ   R   Rx   R8   Rn   Rg   Rl   Rm   (    (    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyR      s   	™	A	(   Rc   R   RX   R‹   Ri   t   django.confR    t   django.core.management.baseR   R   t   django.templateR   t%   django_extensions.management.modelvizR   R   t"   django_extensions.management.utilsR   R   R5   Ra   t   ImportErrorR4   t	   pydotplusR   Rb   R   (    (    (    sT   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/graph_models.pyt   <module>   s.   



