o
    0h*                     @   sf  U d dl Z d dlZd dlmZ d dlmZmZ dee de	defddZ
d	ed
ededeeeee	f  fddZdee dee dee fddZdedefddZdedefddZdedeeeef eeef ee eeee f f fddZdee deeeef eeef ee eeee f f fddZd>dededee fd d!Zdedefd"d#Z	$d?deeee f dee d%ee d&ed'eeef d(edee fd)d*Zd+Zeed,< d-d.hZee ed/< e Zee ed0< d1d1d2Zeeef ed3< d4Zeed5< d-d.hZee ed6< e Zee ed7< d8d9iZeeef ed:< d@d;d<Z e!d=kr1e   dS dS )A    N)defaultdict)AnyUnionlinesindentationreturnc                 C   sF   d}dd|  }t | D ]\}}|dkr||7 }||d|7 }q|S )N 
 r   )	enumeratereplace)r   r   outputnew_line_with_indentiline r   V/var/www/vscode/kcb/lib/python3.10/site-packages/torch/utils/data/datapipes/gen_pyi.pymaterialize_lines   s   r   dirtemplate_nameoutput_namereplacementsc              	   C   s   t j| |}t j| |}t|}| }W d    n1 s!w   Y  |D ](\}}	}
t|d}||t|	|
}|| W d    n1 sKw   Y  q(d S )Nw)ospathjoinopenreadr   r   write)r   r   r   r   template_pathoutput_pathfcontentplaceholderr   r   r   r   r   gen_from_template   s   


r$   	dir_pathsfiles_to_excludec                    sV   t  }| D ]# t }dd |D }fdd|D }| fdd|D  q|S )z
    When given a path to a directory, returns the paths to the relevant files within it.

    This function does NOT recursive traverse to subdirectories.
    c                 S   s    h | ]}d |dd kr|qS )z.pyNr   .0fnamer   r   r   	<setcomp>.   s     z"find_file_paths.<locals>.<setcomp>c                    s   h | ]}| vr|qS r   r   r(   r&   r   r   r+   /   s    c                    s   h | ]	}t j |qS r   r   r   r   r(   )dir_pathr   r   r+   2       )setr   listdirupdate)r%   r&   paths	all_filespython_filesfilter_filesr   )r.   r&   r   find_file_paths%   s   

r7   r   c                 C   s\   d| v r	d\}}nd| v rd\}}nt d|  | |t| | |}}| || S )zXExtract method name from decorator in the form of "@functional_datapipe({method_name})".(")r8   z")(')r9   z')z4Unable to find appropriate method name within line:
)RuntimeErrorfindlenr   start_token	end_tokenstartendr   r   r   extract_method_name6   s   

rB   c                 C   s2   d}d}|  |t| |  |}}| || S )zVExtract class name from class definition in the form of "class {CLASS_NAME}({Type}):".class ()r;   r<   r=   r   r   r   extract_class_nameD   s   rE   	file_pathc                 C   s  i i t  }}}tt}t| }d}d\}}}	d}
|D ]}|dd dkr+|
 }
|
s1d|v r9|| | qd|v rFt|}g ||< q|rQd|v rQt|}q|rwd	|v s[d
|v rwd
|v rd|| |d7 }|	dt
d }||d }|dkr||d7 }||d8 }|dkr|d}|	|d| 7 }	t|	||< |||< d\}}}	q|dk rtd|	|dd7 }	qW d   n1 sw   Y  ||||fS )zfGiven a path to file, parses the file and returns a dictionary of method names to function signatures.r   )r   r   r   Fz"""      z@functional_datapiperC   zdef __init__(zdef __new__(rD   N)z7open parenthesis count < 0. This shouldn't be possible.r	   r
   )r0   r   listr   countappendrB   rE   addr;   r<   rfindprocess_signaturer:   strip)rF   method_to_signaturemethod_to_class_namespecial_output_typedoc_string_dictr!   open_paren_countmethod_name
class_name	signatureskipr   r@   rA   r   r   r   parse_datapipe_fileL   s^   



'rZ   
file_pathsc           
      C   sd   i i t  }}}i }| D ]}t|\}}}}	|| || || ||	 q||||fS )N)r0   rZ   r2   )
r[   methods_and_signaturesmethods_and_class_names!methods_with_special_output_typesmethods_and_doc_stringsr   rQ   rR   $methods_needing_special_output_typesrT   r   r   r   parse_datapipe_files   s,   



ra   ,	delimiterc                 C   sp   d}d}g }| D ](}|dkr|d7 }n|dkr|d8 }n||kr,|dkr,| | d}q||7 }q| | |S )zRGiven a line of text, split it on comma unless the comma is within a bracket '[]'.r   r   [rH   ])rL   )r   rc   bracket_count
curr_tokenrescharr   r   r   split_outside_bracket   s   




rj   c                 C   s   t | }t|D ]B\}}|d||< |dkrd||< q|dkr5d||d  kr5|| d dkr5d||< qd|v rJ|d	d
\}}|dd ||< qdd |D }d|} | S )z
    Clean up a given raw function signature.

    This includes removing the self-referential datapipe argument, default
    arguments of input functions, newlines, and spaces.
    r
   clsselfr   rH   *r   z
Callable ==rG   z= ...c                 S   s   g | ]}|d kr|qS r   r   )r)   tr   r   r   
<listcomp>   s    z%process_signature.<locals>.<listcomp>z, )rj   r   rP   rsplitr   )r   tokensr   tokenhead_default_argr   r   r   rO      s   
(

rO   r   deprecated_filesdefault_output_typemethod_to_special_output_typerootc                    s
   dkrt ttj  t| t r| gn| }  fdd| D } t| ||d}t	|\}}}	}
|D ]}||	vr?|	
| q4g }| D ]4\}}|| }||	v rW|| }n|}d|
| }|dkrfd}|d| d| d| d	| d
| 
 qF|jdd d |S )a  
    #.pyi generation for functional DataPipes Process.

    # 1. Find files that we want to process (exclude the ones who don't)
    # 2. Parse method name and signature
    # 3. Remove first argument after self (unless it is "*datapipes"), default args, and spaces
    r   c                    s   g | ]	}t j |qS r   r-   )r)   r   rz   r   r   rq      r/   z*get_method_definitions.<locals>.<listcomp>r,   z    ...
z# Functional form of 'z'
def rD   z) -> z:
c                 S   s   |  dd S )Nr	   rH   )split)sr   r   r   <lambda>   s    z(get_method_definitions.<locals>.<lambda>)key)strpathlibPath__file__parentresolve
isinstancer7   unionra   rM   itemsr   rL   sort)rF   r&   rw   rx   ry   rz   r[   r\   r]   methods_w_special_output_typesr_   fn_namemethod_definitionsrV   	argumentsrW   output_type
doc_stringr   r{   r   get_method_definitions   sR   


r   iteriterDP_file_pathz__init__.pyzutils.pyiterDP_files_to_excludeiterDP_deprecated_fileszList[IterDataPipe])demuxfork$iterDP_method_to_special_output_typemapmapDP_file_pathmapDP_files_to_excludemapDP_deprecated_filesshuffleIterDataPipe#mapDP_method_to_special_output_typec                  C   s\   t tttdt} t tttdt}t	
tj }d| dfd|dfg}tt|dd|d d	S )
a  
    # Inject file into template datapipe.pyi.in.

    TODO: The current implementation of this script only generates interfaces for built-in methods. To generate
          interface for user-defined DataPipes, consider changing `IterDataPipe.register_datapipe_as_function`.
    r   MapDataPipez${IterDataPipeMethods}   z${MapDataPipeMethods}zdatapipe.pyi.inzdatapipe.pyi)r   r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   )iter_method_definitionsmap_method_definitionsr   r   r   r   r   main  s0   
r   __main__)rb   ro   )r   N)"r   r   collectionsr   typingr   r   rJ   r   intr   tupler$   r0   r7   rB   rE   dictrZ   ra   rj   rO   r   r   __annotations__r   r   r   r   r   r   r   r   __name__r   r   r   r   <module>   sv   


".
4.


8

$
