a
    !f<                     @   s  d dl Z d dlZzd dlZW n ey6   edY n0 d dlZd dlZd dlmZm	Z	m
Z
mZmZmZ e	egef ee eee ee f dddZG dd dejZe d	ejejd
ddZedkre jddZejdddddd ejdddddd e ZeejZeejZe sNede dejd ed e sxede dejd ed e erede dejd ed eee dS )     Nz@Run `python -m pip install "libcst >= 0.2.5"` to install libcst.)AnyCallableDictListSequenceTuple)	predicateiteratorreturnc                 C   s8   g g f}|D ]}|t | | | q|d |d fS )z!A stable, out-of-place partition.   r   )intappend)r   r	   resultsi r   o/var/www/html/python-backend/venv/lib/python3.9/site-packages/../../../bin/fixup_firestore_admin_v1_keywords.py	partition   s    r   c                   @   sh   e Zd ZU dZee ed< ddddddddddd	d
dZeeee f ed< e	j
e	j
e	jdddZdS )firestore_adminCallTransformer)retrytimeoutmetadataCTRL_PARAMS)parentindex)name)r   collection_idsZoutput_uri_prefix)r   r   Zinput_uri_prefix)r   )r   filterZ	page_sizeZ
page_token)Zdatabaseupdate_mask)fieldr   )Zcreate_indexZdelete_indexZexport_documentsZget_database	get_fieldZ	get_indexZimport_documentsZlist_databasesZlist_fieldsZlist_indexesZupdate_databaseZupdate_fieldMETHOD_TO_PARAMS)originalupdatedr
   c           
   	      s   z|j jj} j| }W n ttfy2   | Y S 0 tdd |j\}}tdd |D r^|S t fdd|\}}|d t	| |t	|d   }}|
dd t| jD  tjtdd t||| D td	d
}	|j|	g| dS )Nc                 S   s   t | j S N)boolkeywordar   r   r   <lambda>H       z;firestore_adminCallTransformer.leave_Call.<locals>.<lambda>c                 s   s   | ]}|j jd kV  qdS )requestN)r%   value).0kr   r   r   	<genexpr>I   r)   z<firestore_adminCallTransformer.leave_Call.<locals>.<genexpr>c                    s   | j j jvS r#   )r%   r+   r   r&   selfr   r   r(   N   r)   c                 s   s*   | ]"\}}t j|jt j|d dV  qdS )r+   r+   r%   N)cstArgr+   Name)r,   r'   Zctrlr   r   r   r.   S   s   c              	   S   s2   g | ]*\}}t t d |t j|jdqS )z'{}'r1   )r3   ZDictElementZSimpleStringformatZElementr+   )r,   r   argr   r   r   
<listcomp>W   s
   z=firestore_adminCallTransformer.leave_Call.<locals>.<listcomp>r*   r2   )args)funcattrr+   r    AttributeErrorKeyErrorr   r9   anylenextendzipr   r3   r4   r   r5   Zwith_changes)
r0   r!   r"   keyZkword_paramsr9   kwargsZctrl_kwargsZ	ctrl_argsZrequest_argr   r/   r   
leave_Call>   s2    


"


	z)firestore_adminCallTransformer.leave_CallN)__name__
__module____qualname__r   r   str__annotations__r    r   r3   CallZCSTNoderD   r   r   r   r   r   -   s   
r   )transformer)in_dirout_dirc          
   	   C   s   dd t | D }|D ]}t|d}| }W d   n1 sD0    Y  t|}||}||| }	|	j	j
ddd t|	d}||j W d   q1 s0    Y  qdS )zDuplicate the input dir to the output dir, fixing file method calls.

    Preconditions:
    * in_dir is a real directory
    * out_dir is a real, empty directory
    c                 s   sF   | ]>\}}}|D ].}t j|d  dkrtt j||V  qqdS )r   z.pyN)ospathsplitextpathlibPathjoin)r,   root_filesfr   r   r   r.   t   s   zfix_files.<locals>.<genexpr>rNT)parentsexist_okw)rN   walkopenreadr3   Zparse_modulevisitjoinpathrelative_tor   mkdirwritecode)
rL   rM   rK   Z
pyfile_genZfpathrW   srctreer"   Zupdated_pathr   r   r   	fix_filesh   s    &

rg   __main__a  Fix up source that uses the firestore_admin client library.

The existing sources are NOT overwritten but are copied to output_dir with changes made.

Note: This tool operates at a best-effort level at converting positional
      parameters in client method calls to keyword based parameters.
      Cases where it WILL FAIL include
      A) * or ** expansion in a method call.
      B) Calls via function or method alias (includes free function calls)
      C) Indirect or dispatched calls (e.g. the method is looked up dynamically)

      These all constitute false negatives. The tool will also detect false
      positives when an API method shares a name with another method.
)descriptionz-dz--input-directoryT	input_dirz6the input directory to walk for python files to fix up)requireddesthelpz-oz--output-directory
output_dirz5the directory to output files fixed via un-flatteningzinput directory 'z&' does not exist or is not a directory)filezoutput directory 'z' is not empty)!argparserN   Zlibcstr3   ImportErrorrQ   systypingr   r   r   r   r   r   r$   r   ZCSTTransformerr   rR   rg   rE   ArgumentParserparseradd_argument
parse_argsr9   rj   rn   is_dirprintstderrexitlistdirr   r   r   r   <module>   st    ?#








