o
    wZh*                     @   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   outputZnew_line_with_indentiline r   Q/var/www/auris/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   Ztemplate_pathZ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_filesZpython_filesZfilter_filesr   )r+   r#   r   find_file_paths%   s   

r2   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})".(")r3   z")(')r4   z')z4Unable to find appropriate method name within line:
)RuntimeErrorfindlenr   Zstart_tokenZ	end_tokenstartendr   r   r   extract_method_name6   s   

r;   c                 C   s2   d}d}|  |t| |  |}}| || S )zVExtract class name from class definition in the form of "class {CLASS_NAME}({Type}):".class ()r6   r7   r8   r   r   r   extract_class_nameD   s   r>   	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_datapiper<   zdef __init__(zdef __new__(r=   N)z7open parenthesis count < 0. This shouldn't be possible.r	   r
   )r-   r   listr   countappendr;   r>   addr6   r7   rfindprocess_signaturer5   strip)r?   method_to_signaturemethod_to_class_nameZspecial_output_typedoc_string_dictr   Zopen_paren_countmethod_name
class_name	signatureskipr   r9   r:   r   r   r   parse_datapipe_fileL   s^   



'rQ   
file_pathsc           
      C   sd   i i t  }}}i }| D ]}t|\}}}}	|| || || ||	 q||||fS )N)r-   rQ   r/   )
rR   methods_and_signaturesmethods_and_class_namesZ!methods_with_special_output_typesmethods_and_doc_stringsr   rJ   rK   Z$methods_needing_special_output_typesrL   r   r   r   parse_datapipe_files   s,   



rV   ,	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   [rA   ])rE   )r   rX   Zbracket_countZ
curr_tokenrescharr   r   r   split_outside_bracket   s   




r]   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   rA   *r   z
Callable ==r@   z= ...c                 S   s   g | ]}|d kr|qS r   r   )r&   tr   r   r   
<listcomp>   s    z%process_signature.<locals>.<listcomp>z, )r]   r   rI   rsplitr   )r   tokensr   tokenheadZ_default_argr   r   r   rH      s   
(

rH   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   rl   r   r   rd      r,   z*get_method_definitions.<locals>.<listcomp>r)   z    ...
z# Functional form of 'z'
def r=   z) -> z:
c                 S   s   |  dd S )Nr	   rA   )split)sr   r   r   <lambda>   s    z(get_method_definitions.<locals>.<lambda>)key)strpathlibPath__file__parentresolve
isinstancer2   unionrV   rF   itemsr   rE   sort)r?   r#   ri   rj   rk   rl   rR   rS   rT   Zmethods_w_special_output_typesrU   fn_nameZmethod_definitionsrM   	argumentsrN   output_typeZ
doc_stringr   rm   r   get_method_definitions   sR   


r   iteriterDP_file_pathz__init__.pyzutils.pyiterDP_files_to_excludeiterDP_deprecated_fileszList[IterDataPipe])Z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   ZMapDataPipez${IterDataPipeMethods}   z${MapDataPipeMethods}zdatapipe.pyi.inzdatapipe.pyi)r   r   r   r   N)r   r   r   r   r   r   r   r   r   rs   rt   ru   rv   rw   r!   rr   )Ziter_method_definitionsZmap_method_definitionsr   r   r   r   r   main  s0   
r   __main__)rW   rb   )r   N)"r   rs   collectionsr   typingr   r   rC   rr   intr   tupler!   r-   r2   r;   r>   dictrQ   rV   r]   rH   r   r   __annotations__r   r   r   r   r   r   r   r   __name__r   r   r   r   <module>   sv   


".
4.


8

$
