o
    DDi^                     @   s|   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ G dd	 d	ee
ZG d
d deeZdS )    )MappingN)MetadataDependent)Codemod)CodemodContext)MatcherDecoratableTransformerMatcherDecoratableVisitor)	ProviderTc                   @   s8   e Zd ZdZdeddfddZdejdejfdd	ZdS )
ContextAwareTransformera  
    A transformer which visits using LibCST. Allows visitor-based mutation of a tree.
    Classes wishing to do arbitrary non-visitor-based mutation on a tree should
    instead subclass from :class:`Codemod` and implement
    :meth:`~Codemod.transform_module_impl`. This is a subclass of
    :class:`~libcst.matchers.MatcherDecoratableTransformer` so all features of matchers
    as well as :class:`~libcst.CSTTransformer` are available to subclasses of this
    class.
    contextreturnNc                 C   s   t | | t|  d S N)r   __init__r   )selfr
    r   P/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/codemod/_visitor.pyr      s   z ContextAwareTransformer.__init__treec                 C   s
   | | S r   )visit)r   r   r   r   r   transform_module_impl   s   
z-ContextAwareTransformer.transform_module_impl)	__name__
__module____qualname____doc__r   r   cstModuler   r   r   r   r   r	      s    
r	   c                   @   sH   e Zd ZdZdeddfddZdeddfdd	Zede	j
fd
dZdS )ContextAwareVisitora  
    A visitor which visits using LibCST. Allows visitor-based collecting of info
    on a tree. All codemods which wish to implement an information collector should
    subclass from this instead of directly from
    :class:`~libcst.matchers.MatcherDecoratableVisitor` or :class:`~libcst.CSTVisitor`
    since this provides access to the current codemod context. As a result, this
    class allows access to metadata which was calculated in a parent
    :class:`~libcst.codemod.Codemod` through the
    :meth:`~libcst.MetadataDependent.get_metadata` method.

    Note that you cannot directly run a :class:`~libcst.codemod.ContextAwareVisitor`
    using :func:`~libcst.codemod.transform_module` because visitors by definition
    do not transform trees. However, you can instantiate a
    :class:`~libcst.codemod.ContextAwareVisitor` inside a codemod and pass it to the
    :class:`~libcst.CSTNode.visit` method on any node in order to run information
    gathering with metadata and context support.

    Remember that a :class:`~libcst.codemod.ContextAwareVisitor` is a subclass of
    :class:`~libcst.MetadataDependent`, meaning that you still need to declare
    your metadata dependencies with
    :attr:`~libcst.MetadataDependent.METADATA_DEPENDENCIES` before you can retrieve
    metadata using :meth:`~libcst.MetadataDependent.get_metadata`, even if the parent
    codemod has listed its own metadata dependencies. Note also that the dependencies
    listed on this class must be a strict subset of the dependencies listed in the
    parent codemod.
    r
   r   Nc                    s   t |  t|  || _|  }|rZ| jj  d u r)td| jj dd d |D ]"}| j	vrMtd|j dd d d	| jj d
 |j d q+ fdd|D | _
d S d S )NzAttempting to instantiate z outside of z:an active transform. This means that metadata hasn't been z:calculated and we cannot successfully create this visitor.zAttempting to access metadata z that was not a z:declared dependency of parent transform! This means it is z;not possible to compute this value. Please ensure that all zparent transforms of z	 declare z as a metadata dependency.c                    s   i | ]}| j | qS r   )	_metadata).0depwrapperr   r   
<dictcomp>V   s    z0ContextAwareVisitor.__init__.<locals>.<dictcomp>)r   r   r   r
   get_inherited_dependenciesr   	Exception	__class__r   r   metadata)r   r
   dependenciesr   r   r   r   r   ?   s@   




zContextAwareVisitor.__init__warningc                 C   s   | j j| dS )z\
        Emit a warning that is displayed to the user who has invoked this codemod.
        N)r
   warningsappend)r   r&   r   r   r   warnZ   s   zContextAwareVisitor.warnc                 C   s,   | j j}|du rtd| jj dd |S )z
        Reference to the currently-traversed module. Note that this is only available
        during a transform itself.
        NzAttempted access of z.module outside of ztransform_module().)r
   moduler"   r#   r   )r   r*   r   r   r   r*   `   s   zContextAwareVisitor.module)r   r   r   r   r   r   strr)   propertyr   r   r*   r   r   r   r   r   #   s    r   )typingr   libcstr   r   libcst.codemod._codemodr   libcst.codemod._contextr   libcst.matchersr   r   libcst.metadatar   r	   r   r   r   r   r   <module>   s   