o
    DDis                     @   s   d dl Z d dlm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mZ d dlmZmZ d dlmZ d dlmZmZmZ G dd	 d	eee  ZG d
d dejZG dd deee  ZG dd dejZdS )    N)Path)Any
CollectionListMappingOptionalUnion)	LazyValueMetadataDependent)calculate_module_and_packageModuleNameAndPackage)BatchableMetadataProvider)QualifiedNameQualifiedNameSourceScopeProviderc                	   @   sV   e Zd ZdZefZdejdee	 fddZ
ededejdeeef de	fdd	Zd
S )QualifiedNameProvideran  
    Compute possible qualified names of a variable CSTNode
    (extends `PEP-3155 <https://www.python.org/dev/peps/pep-3155/>`_).
    It uses the
    :func:`~libcst.metadata.Scope.get_qualified_names_for` underlying to get qualified names.
    Multiple qualified names may be returned, such as when we have conditional imports or an
    import shadows another. E.g., the provider finds ``a.b``, ``d.e`` and
    ``f.g`` as possible qualified names of ``c``::

        >>> wrapper = MetadataWrapper(
        >>>     cst.parse_module(dedent(
        >>>     '''
        >>>         if something:
        >>>             from a import b as c
        >>>         elif otherthing:
        >>>             from d import e as c
        >>>         else:
        >>>             from f import g as c
        >>>         c()
        >>>     '''
        >>>     ))
        >>> )
        >>> call = wrapper.module.body[1].body[0].value
        >>> wrapper.resolve(QualifiedNameProvider)[call],
        {
            QualifiedName(name="a.b", source=QualifiedNameSource.IMPORT),
            QualifiedName(name="d.e", source=QualifiedNameSource.IMPORT),
            QualifiedName(name="f.g", source=QualifiedNameSource.IMPORT),
        }

    For qualified name of a variable in a function or a comprehension, please refer
    :func:`~libcst.metadata.Scope.get_qualified_names_for` for more detail.
    nodereturnc                 C   s   t | }|| d S N)QualifiedNameVisitorvisitselfr   visitor r   V/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/metadata/name_provider.pyvisit_Module:   s   z"QualifiedNameProvider.visit_Moduler   namec                    sF   |  t|t }t trt fdd|D S t fdd|D S )z`Check if any of qualified name has the str name or :class:`~libcst.metadata.QualifiedName` name.c                 3   s    | ]}|j  kV  qd S r   r   .0qnr   r   r   	<genexpr>E   s    z1QualifiedNameProvider.has_name.<locals>.<genexpr>c                 3   s    | ]}| kV  qd S r   r   r   r   r   r   r"   G   s    )get_metadatar   set
isinstancestrany)r   r   r   qualified_namesr   r   r   has_name>   s   
zQualifiedNameProvider.has_nameN)__name__
__module____qualname____doc__r   METADATA_DEPENDENCIEScstModuler   boolr   staticmethodr
   CSTNoder   r&   r   r)   r   r   r   r   r      s    "
r   c                       s2   e Zd Zd
ddZdejdef fdd	Z  ZS )r   providerr   r   Nc                 C   s
   || _ d S r   )r4   )r   r4   r   r   r   __init__K   s   
zQualifiedNameVisitor.__init__r   c                    sR   | j t d r| j  t fdd n| j  t  t   dS )Nc                      s
     S r   )get_qualified_names_forr   r   scoper   r   <lambda>R   s   
 z/QualifiedNameVisitor.on_visit.<locals>.<lambda>T)r4   r#   r   set_metadatar	   r$   superon_visit)r   r   	__class__r7   r   r<   N   s   zQualifiedNameVisitor.on_visit)r4   r   r   N)	r*   r+   r,   r5   r/   r3   r1   r<   __classcell__r   r   r=   r   r   J   s    
 r   c                       sx   e Zd ZdZefZedddedee	 de
dedee	ef f
d	d
Zdeddf fddZdejde
fddZ  ZS )FullyQualifiedNameProvidera  
    Provide fully qualified names for CST nodes. Like :class:`QualifiedNameProvider`,
    but the provided :class:`QualifiedName` instances have absolute identifier names
    instead of local to the current module.

    This provider is initialized with the current module's fully qualified name, and can
    be used with :class:`~libcst.metadata.FullRepoManager`. The module's fully qualified
    name itself is stored as a metadata of the :class:`~libcst.Module` node. Compared to
    :class:`QualifiedNameProvider`, it also resolves relative imports.

    Example usage::

        >>> mgr = FullRepoManager(".", {"dir/a.py"}, {FullyQualifiedNameProvider})
        >>> wrapper = mgr.get_metadata_wrapper_for_path("dir/a.py")
        >>> fqnames = wrapper.resolve(FullyQualifiedNameProvider)
        >>> {type(k): v for (k, v) in fqnames.items()}
        {<class 'libcst._nodes.module.Module'>: {QualifiedName(name='dir.a', source=<QualifiedNameSource.LOCAL: 3>)}}

    Fuse_pyproject_toml	root_pathpathsrB   kwargsr   c                   s    fdd|D }|S )Nc                    s   i | ]
}|t  |d qS )rA   )r   )r    pathrC   rB   r   r   
<dictcomp>z   s    z8FullyQualifiedNameProvider.gen_cache.<locals>.<dictcomp>r   )clsrC   rD   rB   rE   cacher   rG   r   	gen_cacheq   s   	z$FullyQualifiedNameProvider.gen_cacherJ   Nc                    s    t  | |j| _|j| _d S r   )r;   r5   r   module_namepackagepackage_name)r   rJ   r=   r   r   r5      s   z#FullyQualifiedNameProvider.__init__r   c                 C   s8   t | | j| j}|| | |t| jtjdh dS )N)r   sourceT)FullyQualifiedNameVisitorrL   rN   r   r:   r   r   LOCALr   r   r   r   r      s   
z'FullyQualifiedNameProvider.visit_Module)r*   r+   r,   r-   r   r.   classmethodr   r   r&   r1   r   r   r   rK   r5   r/   r0   r   r?   r   r   r=   r   r@   Z   s$    
r@   c                	   @   sv   e Zd ZededededefddZededededefdd	Zd
edededdfddZ	de
jdefddZdS )rP   rL   rN   r   r   c                 C   s`   | d}t|t| }|dkr)|}|d|d }t||k r%td|d } |  d| S )N.r      z2attempted relative import beyond top-level package)lstriplenrsplitImportError)rL   rN   r   abs_namenum_dotsbitsr   r   r   _fully_qualify_local   s   
z.FullyQualifiedNameVisitor._fully_qualify_localqnamec                 C   sN   |j tjkr|S |j}|j tjkr|ds|S t| ||j}tj	||dS )NrS   r   )
rO   r   BUILTINr   IMPORT
startswithrP   r\   dataclassesreplace)rL   rN   r]   r   new_namer   r   r   _fully_qualify   s   z(FullyQualifiedNameVisitor._fully_qualifyr4   Nc                 C   s   || _ || _|| _d S r   )rL   rN   r4   )r   r4   rL   rN   r   r   r   r5      s   
z"FullyQualifiedNameVisitor.__init__r   c                    s6    j t|}|d ur j | fdd|D  dS )Nc                    s   h | ]}t  j j|qS r   )rP   rd   rL   rN   )r    r]   r   r   r   	<setcomp>   s    
z5FullyQualifiedNameVisitor.on_visit.<locals>.<setcomp>T)r4   r#   r   r:   )r   r   qnamesr   re   r   r<      s   
	z"FullyQualifiedNameVisitor.on_visit)r*   r+   r,   r2   r&   r\   r   rd   r@   r5   r/   r3   r1   r<   r   r   r   r   rP      s.    
rP   )ra   pathlibr   typingr   r   r   r   r   r   libcstr/   libcst._metadata_dependentr	   r
   libcst.helpers.moduler   r   libcst.metadata.base_providerr   libcst.metadata.scope_providerr   r   r   r   
CSTVisitorr   r@   rP   r   r   r   r   <module>   s    57