o
    DDib                  	   @   s  d dl Z d dlZd dlZd dl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mZmZmZmZmZmZ d dlZd dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZ d dl m!Z!m"Z" d dl#m$Z$ d	e%d
ee% de&fddZ'dee%ef fddZ(dZ)d	e%dee%ef fddZ*d	e%d
ee% de&fddZ+G dd de	Z,G dd de,Z-G dd de,Z.d	e%d
ee% de&fddZ/de%de%deee%e0f  fdd Z1d	e%d
ee% de&fd!d"Z2d	e%d#ee% de&fd$d%Z3e4d&kre5e3ej67d'd(ej8d)d  dS dS )*    N)ABCabstractmethod)AnyCallableDictListTupleType)LIBCST_VERSIONparse_modulePartialParserConfig)parse_version_string)CodemodCommandCodemodContext	diff_codeexec_transform_with_prettyprintgather_files(parallel_exec_transform_with_prettyprint)dumpdump_graphviz)_DEFAULT_INDENT	proc_namecommand_argsreturnc                 C   sT  t jd|  ddd}|jdddtd |jd	d
dd |jdd
dd |jdd
dd |jdd
dd |jdtdtd |jddddtd d ||}|j}|dkr\tj	 }nt
|d}|	 }W d    n1 spw   Y  t||jd urt|jdnt d}|jstt||j|j|j|jd d!S tt||j|j|jd  d!S )"Nz/Print the LibCST tree representation of a file.z print@descriptionprogfromfile_prefix_charsinfileINFILEz)File to print tree for. Use "-" for stdin)metavarhelptypez--show-whitespace
store_truez%Show whitespace nodes in printed treeactionr"   z--show-defaultsz/Show values that are unchanged from the defaultz--show-syntaxzAShow values that exist only for syntax, like commas or semicolonsz
--graphvizz;Displays the graph in .dot format, compatible with Graphvizz--indent-stringz0String to use for indenting levels, defaults to )defaultr"   -p--python-versionVERSIONzOverride the version string used for parsing Python source files. Defaults to the version of python used to run this tool.r!   r"   r#   r'   -rb)python_version)config)indentshow_defaultsshow_syntaxshow_whitespace)r2   r3   r4   r   )argparseArgumentParseradd_argumentstrr   
parse_argsr   sysstdinreadopenr   r/   r   graphvizprintr   indent_stringr2   r3   r4   r   )r   r   parserargsr   codefptree rF   D/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/tool.py_print_tree_impl&   s   


rH   c                   C   s   dd dddgg dgddS )	Nz@gen eratedblackr-   zlibcst.codemod.commands.generated_code_marker	formatterblacklist_patternsmodules	repo_rootrF   rF   rF   rF   rG   _default_config~   s   
rS   z.libcst.codemod.yamlc                 C   s  t  }tjt }d }d}||krtj|t}tj|rt|d}t	
| }W d    n1 s7w   Y  t|trdD ]}||v rVt|| trV|| ||< qCdD ]}	|	|v rwt||	 trwtdd ||	 D rw||	 ||	< qYdD ]}
|
|v rt||
 trtjtj|||
 ||
< qzd}n|}tjtj|tj}||ksttjd	d
}|r|stdt dd d d|  d |d rt|d d p|d d }tj|g|d dd  |d< |S )NFr)rN   )rO   rP   rQ   c                 s   s    | ]}t |tV  qd S N)
isinstancer8   ).0srF   rF   rG   	<genexpr>       

z(_find_and_load_config.<locals>.<genexpr>)rR   TLIBCST_TOOL_REQUIRE_CONFIGrI   zDid not find a z in current directory or any z?parent directory! Perhaps you meant to run this command from a zAconfigured subdirectory, or you need to initialize a new project zusing "z initialize"?rO   r      )rS   ospathabspathgetcwdjoinCONFIG_FILE_NAMEisfiler=   yaml	safe_loadr<   rV   dictr8   listallpardirboolenvironget	Exceptionshutilwhich)r   r0   current_dirprevious_dirfound_configconfig_filerD   possible_configstr_settinglist_settingpath_settingrequires_configexerF   rF   rG   _find_and_load_config   sn   


-

"rz   c                 C   s  t | }tjddd}|jddtdd d |jdd	d
ddd}||\}}|jd ur|jd\}}}|r8|sEt|j dt	j
d dS |jrQtt||}	n>d }	|d D ]#}
ztt|
 d| |}	W  n tyr   Y qW tyz   Y qWw |	d u rtd| dt	j
d dS nt}	tj|	tu rdn|	j|  ddd}|| |jddtdd |jddddd |jddd d!td d" |jd#d$d%d&td d" |jd'd(d)d*tdd d+d, |jd-d
d.d/ |jd0d
d1d/ |jd2d
d3d/ |jd4d
d5d/ |jd6d
d7d/ |jd8d
d9d/ |jd:d
d;d/ |	| ||}d<d= t| D }t|d> r[tj|d> d? d@v r[t|j}|d> d? dAdB|j |j  g|d> dd   |d>< tdCdD |jD rt!|jdkrrt"dEtdFt	j
d t	j#$ }t%|	t& fi |||j'|dG |j( |d> |jdH}|stdIt	j
d dS |j)rtt*|||j)dJdK d?S t| d?S t+|j|j,dL}z(t-|	||j.|j)|j'|dG |j( |d> |j/|j0|j1|j2|dM |j|dN |dO}W n t3y   tdPt	j
d Y dQS w tdR|j4|j5 |j6  dSt	j
d tdT|j4 dUt	j
d tdV|j5 dWt	j
d tdX|j6 dWt	j
d tdY|j7 dZt	j
d |j6d?krHdS d?S )[NFr   )add_helpr   commandCOMMAND?)r!   r#   nargsr'   z-xz
--externalr$   z4Interpret `command` as just a module/class specifier)r&   r'   r"   rL   z is not a valid codemod commandfiler\   rQ   zCould not find z in any configured modulesz,Execute a codemod against a series of files.z codemodr   zThe name of the file (minus the path and extension) and class joined with a '.' that defines your command (e.g. strip_strings_from_types.StripStringsCommand)r!   r#   r"   r^   PATH+zyPath to codemod. Can be a directory, file, or multiple of either. To instead read from stdin and write to stdout, use "-")r!   r   r"   z-jz--jobsJOBSzHNumber of jobs to use when processing files. Defaults to number of coresr,   r(   r)   r*   r+   z-uz--unified-diffCONTEXTzEOutput unified diff instead of contents. Implies outputting to stdout   )r!   r"   r#   r   r'   constz--include-generatedzCodemod generated files.r%   z--include-stubszCodemod typing stub files.z--no-formatz9Don't format resulting codemod with configured formatter.z--show-successesz5Print files successfully codemodded with no warnings.z--hide-generated-warningsz<Do not print files that are skipped for being autogenerated.z--hide-blacklisted-warningsz:Do not print files that are skipped for being blacklisted.z--hide-progresszADo not print progress indicator. Useful if calling from a script.c                 S   s   i | ]\}}|d vr||qS )>   jobsr^   r|   external	no_formatunified_diffhide_progressinclude_stubsr/   show_successesinclude_generatedhide_generated_warningshide_blacklisted_warningsrF   )rW   kvrF   rF   rG   
<dictcomp>e  s    z!_codemod_impl.<locals>.<dictcomp>rO   r   )rK   z	black.exez--target-versionpyc                 s   s    | ]}|d kV  qdS )r-   NrF   )rW   prF   rF   rG   rY     s    z _codemod_impl.<locals>.<genexpr>z6Cannot specify multiple paths when reading from stdin!zCodemodding from stdinrN   )r   rN   format_codeformatter_argsr/   zFailed to codemod from stdinr;   )filename)r   rP   rR   )r   r   r   rN   r   r   r   hide_generatedhide_blacklistedr   rP   r/   rR   codemod_argszInterrupted!   zFinished codemodding z files!z - Transformed z files successfully.z - Skipped z files.z - Failed to codemod  - z warnings were generated.)8rz   r5   r6   r7   r8   parse_known_argsr|   
rpartitionr?   r:   stderrr   getattr	importlibimport_moduleAttributeErrorModuleNotFoundErrorr   DESCRIPTION_add_actionintadd_argsr9   varsitemsanyr]   r^   basenamer   r/   majorminorlenrm   r;   r<   r   r   r   r   r   r   r   r   r   r   r   r   r   r   KeyboardInterrupt	successesskipsfailureswarnings)r   r   r0   rA   
ext_actionrB   _command_module_namecommand_class_namecommand_classmoduler   parsed_versionoldcodenewcodefilesresultrF   rF   rG   _codemod_impl   sv  


			



(


	
r   c                   @   sN   e Zd ZdeddfddZdededefdd	Zedededefd
dZdS )_SerializerBasecommentr   Nc                 C   s
   || _ d S rU   )r   )selfr   rF   rF   rG   __init__  s   
z_SerializerBase.__init__keyvaluec                 C   s>   t jdd t| jD }| t j | || t j S )Nc                 s   s    | ]}d | V  qdS )z# NrF   )rW   r   rF   rF   rG   rY     rZ   z,_SerializerBase.serialize.<locals>.<genexpr>)r]   linesepra   textwrapwrapr   _serialize_impl)r   r   r   commentsrF   rF   rG   	serialize  s   
 z_SerializerBase.serializec                 C   s   d S rU   rF   r   r   r   rF   rF   rG   r     s   z_SerializerBase._serialize_impl)	__name__
__module____qualname__r8   r   objectr   r   r   rF   rF   rF   rG   r     s
    r   c                   @   s"   e Zd ZdededefddZdS )_StrSerializerr   r   r   c                 C   s   | d|S )Nz: rF   r   rF   rF   rG   r     s   z_StrSerializer._serialize_implN)r   r   r   r8   r   r   rF   rF   rF   rG   r     s    r   c                       sF   e Zd Zdddededdf fddZd	ed
edefddZ  ZS )_ListSerializerFnewlinesr   r   r   Nc                   s   t  | || _d S rU   )superr   r   )r   r   r   	__class__rF   rG   r     s   
z_ListSerializer.__init__r   r   c                 C   sf   t |ts	td| jr!dd |D }| dtj tj| S dd |D }| dd| dS )	NzCan only serialize lists!c                 S   s   g | ]}d |qS )z- rF   rW   r   rF   rF   rG   
<listcomp>  s    z3_ListSerializer._serialize_impl.<locals>.<listcomp>:c                 S   s   g | ]}t |qS rF   )reprr   rF   rF   rG   r     s    z: [z, ])rV   rg   rm   r   r]   r   ra   )r   r   r   valuesrF   rF   rG   r     s   
z_ListSerializer._serialize_impl)	r   r   r   r8   rj   r   r   r   __classcell__rF   rF   r   rG   r     s     r   c           	         s   t jd|  ddd}|jddtdd ||}t }td	td
tdtdddtdd d fdd|	 D }t
|}||krKtdtjtj|jt}t|d}|| W d    n1 slw   Y  td|  dS )Nz@Initialize a directory by writing a default LibCST config to it.z initializer   r   r^   r   z>Path to initialize with a default LibCST codemod configurationr   z]String that LibCST should look for in code which indicates that the module is generated code.zCommand line and arguments for invoking a code formatter. Anything specified here must be capable of taking code via stdin and returning formatted code via stdout.zqList of regex patterns which LibCST will evaluate against filenames to determine if the module should be touched.z5List of modules that contain codemods inside of them.Tr   zAbsolute or relative path of the repository root, used for providing full-repo metadata. Relative paths should be specified with this file location as the base.rM   rI   c                 3   s$    | ]\}} |  ||V  qd S rU   )r   )rW   r   valserializersrF   rG   rY     s    
z#_initialize_impl.<locals>.<genexpr>z&Logic error, serialization is invalid!wz*Successfully wrote default config file to r   )r5   r6   r7   r8   r9   rS   r   r   ra   r   rd   re   rm   r]   r^   r_   rb   r=   writer?   )	r   r   rA   rB   default_config
config_stractual_configrs   rD   rF   r   rG   _initialize_impl  sR   

r   base_dirbase_modulec           	   	   C   s   g }t | D ][}t j| |}t j|r4t|| d| }|D ]\}}|| d| |f q"qt j|r?|ds@qz|dd }t	
| d| }|||f W q tyb   Y qw |S )z
    Given a base directory and a base module, recursively walk the directory looking
    for importable python modules, returning them and their relative module name
    based off of the base_module.
    rL   z.pyN)r]   listdirr^   ra   isdir_recursive_findappendrc   endswithr   r   rm   )	r   r   rQ   r^   	full_path	additionsmodule_namemodule_objectpotential_codemodrF   rF   rG   r   (  s$   r   c                 C   st  t | }tjd|  ddd}||}i }|d D ]}zt|}W n ty.   d }Y nw |s=td| dtj	d qt
jt
j|j}t||D ]W\}	}t|D ]N}
zCt||
}t|tsdW qUt|rkW qUtd	d
 t|gD rzW qU|	 d|j }||v rt|t|| k r|||< n|||< W qU ty   Y qUw qMqdd | D }tdt| dS )Nz#List all codemods available to run.z listr   r   rQ   zCould not import z , cannot list codemods inside itr   c                 s   s    | ]	}|d  t u V  qdS )r   N)r   )rW   clsrF   rF   rG   rY   q  s    z_list_impl.<locals>.<genexpr>rL   c                 S   s    g | ]\}}| d |j  qS )r   )r   )rW   objnamerF   rF   rG   r   ~  s    z_list_impl.<locals>.<listcomp>
r   )rz   r5   r6   r9   r   r   rm   r?   r:   r   r]   r^   dirnamer_   __file__r   dirr   
issubclassr   inspect
isabstractr   getclasstreer   r   	TypeErrorr   ra   sorted)r   r   r0   rA   r   codemodsr   imported_moduler^   r   objnamer   fullnameprintable_codemodsrF   rF   rG   
_list_implG  s`   




r  cli_argsc                    s   |r|d nd}|dv }t jd|| dd  jddd	d
t d  jddg dd  |\}}dtdtt dtf fdd}tt	t
td}||jpNd || |S )Nr   --help>   -hr  z.Collection of utilities that ship with LibCST.r   )r   r{   r   r   z	--versionz(Print current version of LibCST toolset.versionzLibCST version )r"   r&   r  r&   zHAction to take. Valid options include: print, codemod, list, initialize.)r?   codemodrg   
initialize)r"   choicesr   r   r   c                    s   t dtjd  tj dS )NzPlease specify a command!
r   r\   )r?   r:   r   
print_help)r   r   rA   rF   rG   _invalid_command  s   zmain.<locals>._invalid_command)r?   r  r  rg   )r5   r6   r7   r
   r   r8   r   r   rH   r   r   r  rl   r&   )r   r  	first_argr{   rB   r   r  lookuprF   r  rG   main  s6   r  __main__LIBCST_TOOL_COMMAND_NAMEzlibcst.toolr\   )9r5   r   r  r]   os.pathrn   r:   r   abcr   r   typingr   r   r   r   r   r	   rd   libcstr
   r   r   libcst._parser.parso.utilsr   libcst.codemodr   r   r   r   r   r   libcst.displayr   r   libcst.display.textr   r8   r   rH   rS   rb   rz   r   r   r   r   r   r   r   r  r  r   exitrk   rl   argvrF   rF   rF   rG   <module>   sD   
  X
F w">>
+