o
    g^#                     @   s  d Z ddlZddlZddlZddlZddlmZmZ ddlZddlm	Z	 dZ
dd Zdd	 Zed
ZedZedZdd ZedZedZdd Zdd Zdd Zdd ZdZdZdZdZdee e eee dZeeeddZG dd  d ZG d!d" d"ZG d#d$ d$eZ d%ee	j!e	j"f d&e#fd'd(Z$d)ee	j%e	j&f d&eee	j!e	j"f  fd*d+Z'd)ee	j%e	j&f d&e(fd,d-Z)d.d/ Z*dS )0z9Generic classes/functions for pyreverse core/extensions.     N)OptionalUnion)nodesz.pyreversercc                  C   s   g } t jdd}|r>t j|t}z!t|dd}|  } W d   W | S 1 s,w   Y  W | S  t	y=   Y | S w | S )z,Read config file and return list of options.HOME zutf-8)encodingN)
osenvirongetpathjoinRCFILEopenreadsplitOSError)optionshomercfilefile_handle r   T/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/pyreverse/utils.pyget_default_options"   s    r   c                  C   s*   t  } |   | D ]	}tjd| q	dS )z"insert default options to sys.argv   N)r   reversesysargvinsert)r   argr   r   r   insert_default_options0   s
   r   z^__([^\W_]_*)+__$z^__(_*[^\W_])+_?$z^_\w*$c                 C   s>   t | r	d}|S t| rd}|S t| rd}|S d}|S )zHreturn the visibility from a name: public, protected, private or specialspecialprivate	protectedpublic)SPECIALmatchPRIVATE	PROTECTED)name
visibilityr   r   r   get_visibility>   s   


r*   z^.*Abstract.*z^[^\W\da-z]*$c                 C      t | jS )zWreturn true if the given class node correspond to an abstract class
    definition
    )ABSTRACTr%   r(   noder   r   r   is_abstractP      r/   c                 C   r+   )zTreturn true if the given class/function node correspond to final
    definition
    )FINALr%   r(   r-   r   r   r   is_finalW   r0   r2   c                 C   
   | j dkS )N	interfacetyper-   r   r   r   is_interface^      
r7   c                 C   r3   )N	exceptionr5   r-   r   r   r   is_exceptionc   r8   r:   r            )ALLPUB_ONLYr$   OTHER)r    r"   r!   r#   c                   @       e Zd ZdZdd Zdd ZdS )FilterMixInz6filter nodes according to a mode and nodes' visibilityc                 C   sf   d}| dD ]&}z|t| 7 }W q ty- } ztd| tjd W Y d}~qd}~ww || _dS )zinit filter modesr   +zUnknown filter mode )fileN)r   MODESKeyErrorprintr   stderr_FilterMixIn__mode)selfmoderI   nummodexr   r   r   __init__   s    
zFilterMixIn.__init__c                 C   s    t t|d|}| jt| @  S )z)return true if the node should be treatedr(   )r*   getattrrI   VIS_MOD)rJ   r.   r)   r   r   r   	show_attr   s   zFilterMixIn.show_attrN)__name__
__module____qualname____doc__rN   rQ   r   r   r   r   rB   |   s    
rB   c                   @   s:   e Zd ZdZdd ZdddZdd Zd	d
 Zdd ZdS )	ASTWalkera  a walker visiting a tree in preorder, calling on the handler:

    * 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                 C   s   || _ i | _d S N)handler_cache)rJ   rX   r   r   r   rN      s   
zASTWalker.__init__Nc                 C   s   |du rt  }||v rtt|||jf|| | | | D ]}||us+J | || q#| | |j|us>J dS )<walk on the tree from <node>, getting callbacks from handlerN)	setAssertionErroridparentaddvisitget_childrenwalkleave)rJ   r.   _done
child_noder   r   r   rb      s   


zASTWalker.walkc                 C   s   |j }| j|}|du r:| j}|j }t|d| t|dd}t|d| t|dd}||f| j|< ||fS |\}}||fS )z/get callbacks from handler for the visited nodeNvisit_visit_defaultleave_leave_default)	__class__rY   r
   rX   rR   lowerrO   )rJ   r.   klassmethodsrX   kide_methodl_methodr   r   r   get_callbacks   s   
zASTWalker.get_callbacksc                 C   &   |  |d }|dur|| dS dS )rZ   r   Nrq   rJ   r.   methodr   r   r   r`         zASTWalker.visitc                 C   rr   )rZ   r   Nrs   rt   r   r   r   rc      rv   zASTWalker.leaverW   )	rR   rS   rT   rU   rN   rb   rq   r`   rc   r   r   r   r   rV      s    	
rV   c                   @   rA   )LocalsVisitorz3visit a project by traversing the locals dictionaryc                 C   s   t | |  t | _d S rW   )rV   rN   r[   _visited)rJ   r   r   r   rN      s   zLocalsVisitor.__init__c                 C   sz   || j v rdS | j | | |}|d dur|d | t|dr/| D ]}| | q'|d dur;|d |S dS )z-launch the visit starting from the given nodeNr   localsr   )rx   r_   rq   hasattrvaluesr`   )rJ   r.   rm   
local_noder   r   r   r`      s   


zLocalsVisitor.visitN)rR   rS   rT   rU   rN   r`   r   r   r   r   rw      s    rw   annreturnc                 C   s2   d}t | tjr|  }|S t | tjr| j}|S )Nr   )
isinstancer   	Subscript	as_stringNamer(   )r}   labelr   r   r   get_annotation_label   s   r   r.   c                 C   s   d}t | jtjr| jj}n*t | tjr6| jj}ztt|j|j	j
}|| jjj}W n ty5   Y nw |S z|  ^}}W n tjyL   d}Y nw t|}|rht|dddu rf|dsfd| dn|}|rm||_|S )z return the annotation for `node`Nr   valuer   z	Optional[])r   r^   r   	AnnAssign
annotation
AssignAttrdictzipry   argsannotationsr
   r   r(   AttributeErrorinferastroidInferenceErrorr   rO   
startswith)r.   r}   init_methodr   default_r   r   r   r   get_annotation   s8   
r   c                 C   sf   t | }z|rt|tjr|hW S t| W S t|  W S  tjy2   |r-|h Y S t  Y S 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   r   r[   r   r   r   )r.   r}   r   r   r   
infer_node  s   r   c                   C   s(   t ddu rtd td dS dS )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.dotNzThe requested output format is currently not available.
Please install 'Graphviz' to have other output formats than 'dot' or 'vcg'.    )shutilwhichrG   r   exitr   r   r   r   check_graphviz_availability  s   r   )+rU   r   rer   r   typingr   r   r   r   r   r   r   compiler$   r&   r'   r*   r,   r1   r/   r2   r7   r:   _CONSTRUCTOR_SPECIAL
_PROTECTED_PRIVATErE   rP   rB   rV   rw   r   r   strr   r   
AssignNamer   r[   r   r   r   r   r   r   <module>   s\   
	




;	
#