
    e                        d Z ddlmZ 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	m
Z
mZmZmZmZ ddlZddlmZ ddlmZ e	r5ddlmZmZ eej,                  geee   eeef   df   f   Zeee   ee   f   ZdZdd	Zdd
Z ej8                  d      Z ej8                  d      Z ej8                  d      Zd dZ d!dZ!dZ"dZ#dZ$de"e#z   e$z   e"e#e$z   dZ%e"e#e$ddZ& G d d      Z' G d d      Z(d"dZ)	 	 	 	 d#dZ*d$dZ+ddZ,d%dZ-y)&z8Generic classes/functions for pyreverse core/extensions.    )annotationsN)TYPE_CHECKINGAnyCallableOptionalTupleUnion)nodes)InferenceResult)ClassDiagramPackageDiagramz.pyreversercc                 >   g } t         j                  j                  dd      }|r[t         j                  j	                  |t
              }	 t        |d      5 }|j                         j                         } ddd       | S | S # 1 sw Y   | S xY w# t        $ r Y | S w xY w)z,Read config file and return list of options.HOME utf-8)encodingN)
osenvirongetpathjoinRCFILEopenreadsplitOSError)optionshomercfilefile_handles       8/usr/lib/python3/dist-packages/pylint/pyreverse/utils.pyget_default_optionsr"   !   s    G::>>&"%DdF+	fw/ 5;%**,2245 N7N	5 N  	N	s0   
B B6B BB B 	BBc                     t               } | j                          | D ]"  }t        j                  j	                  d|       $ y)z#Insert default options to sys.argv.   N)r"   reversesysargvinsert)r   args     r!   insert_default_optionsr*   /   s5    !#GOO  3     z^__([^\W_]_*)+__$z^__(_*[^\W_])+_?$z^_\w*$c                    t         j                  |       rd}|S t        j                  |       rd}|S t        j                  |       rd}|S d}|S )zIReturn the visibility from a name: public, protected, private or special.specialprivate	protectedpublic)SPECIALmatchPRIVATE	PROTECTED)name
visibilitys     r!   get_visibilityr7   =   s^    }}T
  
t	
  
	 
  
r+   c                     | j                   dk(  S )N	exception)type)nodes    r!   is_exceptionr<   K   s    99##r+            )ALLPUB_ONLYr1   OTHER)r-   r/   r.   r0   c                       e Zd ZdZddZddZy)FilterMixInz7Filter nodes according to a mode and nodes' visibility.c                    d}|j                  d      D ]  }	 |t        |   z  } || _        y# t        $ r(}t        d| t        j
                         Y d}~Fd}~ww xY w)zInit filter modes.r   +zUnknown filter mode )fileN)r   MODESKeyErrorprintr&   stderr_FilterMixIn__mode)selfmoderL   nummodexs        r!   __init__zFilterMixIn.__init__f   sj    jjo 	DFD%-'	D
   D,RD1

CCDs   .	AAAc                \    t        t        |d|            }| j                  t        |   z   S )z*Return true if the node should be treated.r5   )r7   getattrrL   VIS_MOD)rM   r;   r6   s      r!   	show_attrzFilterMixIn.show_attrp   s,    #GD&$$?@
;;!4444r+   N)rN   strreturnNone)r;   znodes.NodeNG | strrW   bool)__name__
__module____qualname____doc__rQ   rU    r+   r!   rD   rD   c   s    A5r+   rD   c                  (    e Zd ZdZddZddZddZy)	LocalsVisitora  Visit a project by traversing the locals dictionary.

    * visit_<class name> on entering a node, where class name is the class of
    the node in lower case

    * leave_<class name> on leaving a node, where class name is the class of
    the node in lower case
    c                0    i | _         t               | _        y N)_cacheset_visited)rM   s    r!   rQ   zLocalsVisitor.__init__   s    AC+.5r+   c           	     0   |j                   }| j                  j                  |      }|e|j                  j	                         }t        | d| t        | dd            }t        | d| t        | dd            }||f| j                  |<   ||fS |\  }}||fS )z0Get callbacks from handler for the visited node.Nvisit_visit_defaultleave_leave_default)	__class__rc   r   rZ   lowerrS   )rM   r;   klassmethodskide_methodl_methods          r!   get_callbackszLocalsVisitor.get_callbacks   s    ++//%(?..&&(CsengdOT&JH sengdOT&JH #+H!5DKK !! ")Hh!!r+   c                   || j                   v ry| j                   j                  |       | j                  |      }|d    |d   |       t        |d      r&|j	                         D ]  }| j                  |        |d    |d   |      S y)z.Launch the visit starting from the given node.Nr   localsr$   )re   addrr   hasattrvaluesvisit)rM   r;   rn   
local_nodes       r!   rx   zLocalsVisitor.visit   s    4== $$$T*1:!GAJt4""kkm '


:&'1:!71:d##r+   NrW   rX   )r;   nodes.NodeNGrW   _CallbackTupleT)r;   r{   rW   r   )rZ   r[   r\   r]   rQ   rr   rx   r^   r+   r!   r`   r`   v   s    1""r+   r`   c                    t        | t        j                        r| j                  | j                  S t        | t        j                        r| j                         S y)Nr   )
isinstancer
   Namer5   NodeNG	as_string)anns    r!   get_annotation_labelr      s?    #uzz"sxx';xx#u||$}}r+   c                   d}t        | j                  t        j                        r| j                  j                  }nt        | t        j
                        r{| j                  j                  }	 t        t        |j                  |j                  j                              } |j                  | j                  j                  j                        }n|S 	 | j                         ^}}t%        |      }|r^t'        |dd      Q|j)                  d      s@t        |t        j*                        r t-        d |j/                         D              sd| d}|r	|r||_        |S # t        $ r Y w xY w# t         j"                  $ r d}Y w xY w)z!Return the annotation for `node`.Nr   valuer   c              3  p   K   | ].  }t        |t        j                        xr |j                  d u  0 y wrb   )r~   r
   Constr   ).0childs     r!   	<genexpr>z!get_annotation.<locals>.<genexpr>   s4       5%++.F5;;$3FFs   46z	Optional[])r~   parentr
   	AnnAssign
annotation
AssignAttrdictziprt   argsr   r   r   r5   AttributeErrorinferastroidInferenceErrorr   rS   
startswithBinOpanyget_children)r;   r   init_methodr   default_labels          r!   get_annotationr      sV    C$++u/kk$$	D%**	+kk((	s;#5#5{7G7G7S7STUK!+//$++"3"3"8"89C 
jjl! !%E 	GWg.6  ,3,  --/  UG1%J9  		 !! s%   /A#E  E/  	E,+E,/FFc                b   t        |       }	 |r_t        |t        j                        s)t        |t        j                        r|j
                  dk(  r|hS t        |j                               S t        | j                               S # t        j                  $ r |r|hcY S t               cY S w xY w)zReturn a set containing the node annotation if it exists
    otherwise return a set of the inferred types using the NodeNG.infer method.
    |)
r   r~   r
   	Subscriptr   oprd   r   r   r   )r;   r   s     r!   
infer_noder      s    
 
C	'#u/3,3usyy{##4::<  !! 'u&&'s$   AB B .B B.!
B.-B.c                 p    t        j                  d      !t        d       t        j                  d       yy)zCheck if the ``dot`` command is available on the machine.

    This is needed if image output is desired and ``dot`` is used to convert
    from *.dot or *.gv into the final output format.
    dotNz?'Graphviz' needs to be installed for your chosen output format.    )shutilwhichrJ   r&   exitr^   r+   r!   check_graphviz_availabilityr      s,     ||E"OP #r+   c                H   t        j                  ddgddd      }t        j                  d|j                  j                               }|st        d	       y
|j                  d      }| |j                         vr't        d|  d|        t        j                  d       y
y
)zCheck if the ``dot`` command supports the requested output format.

    This is needed if image output is desired and ``dot`` is used to convert
    from *.gv into the final output format.
    r   z-T?TFr   )capture_outputcheckr   z$.*Use one of: (?P<formats>(\S*\s?)+))patternstringzUnable to determine Graphviz supported output formats. Pyreverse will continue, but subsequent error messages regarding the output format may come from Graphviz directly.NformatszFormat z, is not supported by Graphviz. It supports: r   )
subprocessrunrer2   rK   striprJ   groupr   r&   r   )output_format
dot_outputr2   supported_formatss       r!   !check_if_graphviz_supports_formatr      s     	t57J HH7  &&(E K	

 	I.-3355m_$PQbPcd	
 		 6r+   )rW   z	list[str]rz   )r5   rV   rW   rV   )r;   znodes.ClassDefrW   rY   )r   znodes.Name | nodes.NodeNGrW   rV   )r;   #nodes.AssignAttr | nodes.AssignNamerW   z#nodes.Name | nodes.Subscript | None)r;   r   rW   zset[InferenceResult])r   rV   rW   rX   ).r]   
__future__r   r   r   r   r   r&   typingr   r   r   r   r   r	   r   r
   astroid.typingr   pylint.pyreverse.diagramsr   r   r   
_CallbackTr|   r   r"   r*   compiler1   r3   r4   r7   r<   _SPECIAL
_PROTECTED_PRIVATErH   rT   rD   r`   r   r   r   r   r   r^   r+   r!   <module>r      sY  
 ? " 	 	   
 G G   *F	eL!5)E#FLM	OJ HZ0(:2FFGO 
  "**)
*
"**)
*BJJy!	$ 
:%0("		 	5 5&- -`(
-(((V'$r+   