o
    DDiQ!                     @   s   d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ e
d
edZG dd deeZG dd deeeZG dd deeZdS )    N)ABCabstractmethod)Dict	GeneratorListTypeTypeVar)Module)Codemod)CodemodContext)ContextAwareTransformer)AddImportsVisitor)RemoveImportsVisitor_Codemod)boundc                       s   e Zd ZU dZdZeed< edej	ddfddZ
d	ee d
edefddZed
edefddZd
edef fddZ  ZS )CodemodCommanda*  
    A :class:`~libcst.codemod.Codemod` which can be invoked on the command-line
    using the ``libcst.tool codemod`` utility. It behaves like any other codemod
    in that it can be instantiated and run identically to a
    :class:`~libcst.codemod.Codemod`. However, it provides support for providing
    help text and command-line arguments to ``libcst.tool codemod`` as well as
    facilities for automatically running certain common transforms after executing
    your :meth:`~libcst.codemod.Codemod.transform_module_impl`.

    The following list of transforms are automatically run at this time:

     - :class:`~libcst.codemod.visitors.AddImportsVisitor` (adds needed imports to a module).
     - :class:`~libcst.codemod.visitors.RemoveImportsVisitor` (removes unreferenced imports from a module).
    zNo description.DESCRIPTION
arg_parserreturnNc                 C      dS )a  
        Override this to add arguments to the CLI argument parser. These args
        will show up when the user invokes ``libcst.tool codemod`` with
        ``--help``. They will also be presented to your class's ``__init__``
        method. So, if you define a command with an argument 'foo', you should also
        have a corresponding 'foo' positional or keyword argument in your
        class's ``__init__`` method.
        N )r   r   r   P/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/codemod/_command.pyadd_args*   s   zCodemodCommand.add_args	transformtreec                 C   s   || j }||S N)contexttransform_module)selfr   r   instr   r   r   _instantiate_and_run7   s   

z#CodemodCommand._instantiate_and_runc                 C   r   )z
        Override this with your transform. You should take in the tree, optionally
        mutate it and then return the mutated version. The module reference and all
        calculated metadata are available for the lifetime of this function.
        Nr   )r   r   r   r   r   transform_module_impl;   s   z$CodemodCommand.transform_module_implc                    sJ   t  |}tjttjti}| D ]\}}|| jjv r"| ||}q|S r   )	superr   r   CONTEXT_KEYr   itemsr   scratchr    )r   r   supported_transformskeyr   	__class__r   r   r   D   s   zCodemodCommand.transform_module)__name__
__module____qualname____doc__r   str__annotations__staticmethodargparseArgumentParserr   r   r   r	   r    r   r!   r   __classcell__r   r   r(   r   r      s   
 r   c                   @   s   e Zd ZdZdS )VisitorBasedCodemodCommanda[  
    A command that acts identically to a visitor-based transform, but also has
    the support of :meth:`~libcst.codemod.CodemodCommand.add_args` and running
    supported helper transforms after execution. See
    :class:`~libcst.codemod.CodemodCommand` and
    :class:`~libcst.codemod.ContextAwareTransformer` for additional documentation.
    N)r*   r+   r,   r-   r   r   r   r   r4   ]   s    r4   c                       s~   e Zd ZdZdedeeef ddf fddZe	de
ee ddf fdd	Zd
ee defddZdedefddZ  ZS )MagicArgsCodemodCommanda-  
    A "magic" args command, which auto-magically looks up the transforms that
    are yielded from :meth:`~libcst.codemod.MagicArgsCodemodCommand.get_transforms`
    and instantiates them using values out of the context. Visitors yielded in
    :meth:`~libcst.codemod.MagicArgsCodemodCommand.get_transforms` must have
    constructor arguments that match a key in the context
    :attr:`~libcst.codemod.CodemodContext.scratch`. The easiest way to
    guarantee that is to use :meth:`~libcst.codemod.CodemodCommand.add_args`
    to add a command arg that will be parsed for each of the args. However, if
    you wish to chain transforms, adding to the scratch in one transform will make
    the value available to the constructor in subsequent transforms as well as the
    scratch for subsequent transforms.
    r   kwargsr   Nc                    s   t  | | jj| d S r   )r"   __init__r   r%   update)r   r   r6   r(   r   r   r7   x   s   z MagicArgsCodemodCommand.__init__c                 C   r   )ai  
        A generator which yields one or more subclasses of
        :class:`~libcst.codemod.Codemod`. In the general case, you will usually
        yield a series of classes, but it is possible to programmatically decide
        which classes to yield depending on the contents of the context
        :attr:`~libcst.codemod.CodemodContext.scratch`.

        Note that you should yield classes, not instances of classes, as the
        point of :class:`~libcst.codemod.MagicArgsCodemodCommand` is to
        instantiate them for you with the contents of
        :attr:`~libcst.codemod.CodemodContext.scratch`.
        Nr   )r   r   r   r   get_transforms|   s   z&MagicArgsCodemodCommand.get_transformsr   c           
      C   s  t |j}g }i }t|jt|jpd }t|jD ]/\}}|dv r$q|| jjvrA||kr/qt	d|j
 d| dd d d || jj|  q|jpOi }|jD ]*}	|	| jjvrq|	|vrqt	d|j
 d	|	 dd d d | jj|	||	 ||	< qS|| jg|R i |S )
Nr   )r   r   zVisitor z requires positional arg z but z?it is not in our context nor does it have a default! It should z?be provided by an argument returned from the 'add_args' method z:or populated into context.scratch by a previous transform!z requires keyword arg )inspectgetfullargspecr7   lenargsdefaults	enumerater   r%   KeyErrorr*   appendkwonlydefaults
kwonlyargsget)
r   r   argspecr=   r6   last_default_argiargrB   kwargr   r   r   _instantiate   sD   

z$MagicArgsCodemodCommand._instantiater   c                 C   s&   |   D ]}| |}||}q|S r   )r9   rJ   r   )r   r   r   r   r   r   r   r!      s   
z-MagicArgsCodemodCommand.transform_module_impl)r*   r+   r,   r-   r   r   r.   objectr7   r   r   r   r
   r9   r   rJ   r	   r!   r3   r   r   r(   r   r5   i   s    "$r5   )r1   r:   abcr   r   typingr   r   r   r   r   libcstr	   libcst.codemod._codemodr
   libcst.codemod._contextr   libcst.codemod._visitorr   $libcst.codemod.visitors._add_importsr   'libcst.codemod.visitors._remove_importsr   r   r   r4   r5   r   r   r   r   <module>   s   H