
    `i*.                        U d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 	 d dl
mZ d dlmZ n# e$ r d dlZ ee                                          j        d         Zej                            d  ee                     d dl
mZ d dlmZ  eej                  d k    r"ej        d           ee          k    rej        d = Y nw xY wg Zee         ed	<   d
ee         fdZ e	de          dee         ded
efd            Z e	de          dedededeeeeef                  fd            Z dee         de!e         d
e!e         fdZ"ded
efdZ#ded
efdZ$ded
ee%eee         f         e%eef         e!e         e%eee         f         f         fdZ&de!e         d
ee%eee         f         e%eef         e!e         e%eee         f         f         fd Z'd=ded"ed
ee         fd#Z(ded
ee         f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*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Z0e!e         ed6<    e!            Z1e!e         ed7<   d8d9iZ2e%eef         ed:<   d?d;Z3e4d<k    r e3             dS dS )@    N)defaultdict)Path)AnyUnion)
deprecated)format_function_signature)FileManager   __all__returnc                      g S N r       v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/utils/data/datapipes/gen_pyi.py__dir__r      s    Ir   zg`torch.utils.data.datapipes.gen_pyi.materialize_lines` is deprecated and will be removed in the future.)categorylinesindentationc                     d}dd|z  z   }t          |           D ])\  }}|dk    r||z  }||                    d|          z  }*|S )N 
 r   )	enumeratereplace)r   r   outputnew_line_with_indentilines         r   materialize_linesr        sh    
 F#"33U## ; ;466**F$,,t%9:::Mr   zg`torch.utils.data.datapipes.gen_pyi.gen_from_template` is deprecated and will be removed in the future.dirtemplate_nameoutput_namereplacementsc           	         t           j                            | |          }t           j                            | |          }t          |d          5 }|                                }d d d            n# 1 swxY w Y   |D ]i\  }}	}
t          |dd          5 }|                    |t          |	|
                    }|                    |           d d d            n# 1 swxY w Y   jd S )Nutf-8encodingw)ospathjoinopenreadr   r    write)r!   r"   r#   r$   template_pathoutput_pathfcontentplaceholderr   r   s              r   gen_from_templater5   .   s_    GLLm44M',,sK00K	mg	.	.	. !&&((              +7  'UK+sW555 	oo.ukBB G GGG		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s$   A33A7:A7:CC"	%C"		dir_pathsfiles_to_excludec                     t                      }| D ]Qt          j                  }d |D             }fd|D             }|                    fd|D                        R|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                 .    h | ]}d |dd         k    |S )z.pyNr   ).0fnames     r   	<setcomp>z"find_file_paths.<locals>.<setcomp>N   s*    LLL%rss8K8K8K8K8Kr   c                     h | ]}|v|	S r   r   )r;   r<   r7   s     r   r=   z"find_file_paths.<locals>.<setcomp>O   s+     
 
 
u<L/L/LE/L/L/Lr   c                 P    h | ]"}t           j                            |          #S r   r*   r+   r,   )r;   r<   dir_paths     r   r=   z"find_file_paths.<locals>.<setcomp>R   s)    NNNbgll8U33NNNr   )setr*   listdirupdate)r6   r7   paths	all_filespython_filesfilter_filesrA   s    `    @r   find_file_pathsrI   E   s     eeE P PJx((	LL9LLL
 
 
 
+
 
 
 	NNNNNNNOOOOLr   r   c                     d| v rd\  }}nd| v rd\  }}nt          d|            |                     |          t          |          z   |                     |          }}| ||         S )zXExtract method name from decorator in the form of "@functional_datapipe({method_name})".(")rK   z")(')rL   z')z4Unable to find appropriate method name within line:
)RuntimeErrorfindlenr   start_token	end_tokenstartends        r   extract_method_namerU   V   s    t||!+YY	!+YYJDJJ
 
 	
 ;''#k*:*::DIIi<P<P3Ec	?r   c                     d}d}|                      |          t          |          z   |                      |          }}| ||         S )zVExtract class name from class definition in the form of "class {CLASS_NAME}({Type}):".class ()rN   rO   rP   s        r   extract_class_namerY   d   sH    KI;''#k*:*::DIIi<P<P3Ec	?r   	file_pathc                    i i t                      }}}t          t                    }t          | d          5 }d}d\  }}}	d}
|D ]s}|                    d          dz  dk    r|
 }
|
sd|v r||                             |           Dd	|v rt          |          }g ||<   ]|rd
|v rt          |          }s|rUd|v sd|v rMd|v r|                    |           |dz  }|	                    d          t          d          z   }||d         }|dk    r||                    d          z  }||                    d          z  }|dk    rA|                    d          }|	|d|         z  }	t          |	          ||<   |||<   d\  }}}	G|dk     rt          d          |	|                                z  }	u	 ddd           n# 1 swxY w 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   r   r   Fz"""      z@functional_datapiperW   zdef __init__(zdef __new__(rX   N)z7open parenthesis count < 0. This shouldn't be possible.)rB   r   listr-   countappendrU   rY   addrN   rO   rfindprocess_signaturerM   strip)rZ   method_to_signaturemethod_to_class_namespecial_output_typedoc_string_dictr2   open_paren_countmethod_name
class_name	signatureskipr   rS   rT   s                 r   parse_datapipe_filero   l   s    FHSUU/B-!$''O	i'	*	*	* %.a-7*Z !	. !	.Dzz%  1$))x u}},33D999%--1$77/1, x4///55
 $4 7 7>T;Q;Q!T))'++K888 A% 		#S1EFF|!## DJJsOO3  DJJsOO3 #q((**S//Cdsd+I7H7S7S'48B(59C6KYY%))&Q   -IC!	.	%. %. %. %. %. %. %. %. %. %. %. %. %. %. %.N 		 s   FGGG
file_pathsc                    i }i }t                      }i }| D ]j}t          |          \  }}}}	|                    |           |                    |           |                    |           |                    |	           k||||fS r   )rB   ro   rD   )
rp   methods_and_signaturesmethods_and_class_names!methods_with_special_output_typesmethods_and_doc_stringsr+   rf   rg   $methods_needing_special_output_typesri   s
             r   parse_datapipe_filesrw      s       (+%  
8 
8  %%	
 0%%&9:::&&';<<<)001UVVV&&7777)	 r   ,	delimiterc                     d}d}g }| D ]C}|dk    r|dz  }n0|dk    r|dz  }n$||k    r|dk    r|                     |           d}>||z  }D|                     |           |S )zRGiven a line of text, split it on comma unless the comma is within a bracket '[]'.r   r   [r]   ])ra   )r   ry   bracket_count
curr_tokenreschars         r   split_outside_bracketr      s    MJ
C 	 	3;;QMMS[[QMMY=A#5#5JJz"""Jd

JJzJr   c                 |   t          |           }t          |          D ]\  }}|                    d          ||<   |dk    rd||<   )|dk    r'd||dz
           k    r||         d         dk    rd||<   Vd|v r6|                    d	          d         }|                    d          d
z   ||<   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   r]   *r   z
Callable ==z = ...c                     g | ]
}|d k    |S r   r   )r;   ts     r   
<listcomp>z%process_signature.<locals>.<listcomp>   s    +++A177a777r   )r   r   re   
rpartition)r   tokensr   tokenheads        r   rd   rd      s     .d33Ff%% 	3 	35KK$$q	E>>F1IIUU&Q-//fQilc6I6IF1IIU""##C((+D

3(2F1I+++++FMr   r   deprecated_filesdefault_output_typemethod_to_special_output_typerootc                    dk    r8t          t          t                    j                                                  t          | t                     r| gn| } fd| D             } t          | |                    |                    }t          |          \  }}}	}
|D ]}||	vr|		                    |           g }|
                                D ]\  }}||         }||	v r	||         }n|}d                    |
|                   }|dk    rd}nd|z   }t          |||          }|                    d| d|                    d                                          z   |z              |                    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                 P    g | ]"}t           j                            |          #S r   r@   )r;   r+   r   s     r   r   z*get_method_definitions.<locals>.<listcomp>   s)    @@@ddD))@@@r   )r7   z ...r   z# Functional form of 'z'
z...c                 8    |                      d          d         S )Nr   r]   )split)ss    r   <lambda>z(get_method_definitions.<locals>.<lambda>  s    aggdmmA& r   )key)strr   __file__parentresolve
isinstancerI   unionrw   rb   itemsr,   r   ra   removesuffixrstripsort)rZ   r7   r   r   r   r   rp   rr   rs   methods_w_special_output_typesru   fn_namemethod_definitionsrk   	argumentsrl   output_type
doc_string
definitions        `             r   get_method_definitionsr      s    rzz4>>(002233))S99HyI@@@@i@@@I $4$:$:;K$L$L  J 	Z((& 1 8 8888*..w777"8">">"@"@ 
 
Y,[9
8887DKK-KWW4[ABB
JJ
*J.{I{SS
!!4Z444%%e,,33556	
 	
 	
 	

 &&     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                  V   t          t          t          t          dt                    t          t
          t          t          dt                    t          t                                                    j        } t          | | d          }|                    ddf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   MapDataPipeF)install_dirtemplate_dirdry_runzdatapipe.pyizdatapipe.pyi.inc                       dS )N)IterDataPipeMethodsMapDataPipeMethodsr   )iter_method_definitionsmap_method_definitionss   r   r   zmain.<locals>.<lambda>H  s    #:"8
 
 r   N)r   r   r   r   r   r   r   r   r   r   r   absoluter   r	   write_with_template)r+   fmr   r   s     @@r   mainr   ,  s     5,  4+  >>""$$+D	D%	H	H	HB	
 	
 	
 	
 	
    r   __main__)rx   r   )r   N)5r*   collectionsr   pathlibr   typingr   r   typing_extensionsr   torchgen.api.pythonr   torchgen.utilsr	   ImportErrorsysr   r   parents	REPO_ROOTr+   insertr   rO   r   r_   __annotations__r   FutureWarningintr    tupler5   rB   rI   rU   rY   dictro   rw   r   rd   r   r   r   r   r   r   r   r   r   r   __name__r   r   r   <module>r      sY   					 # # # # # #               ( ( ( ( ( (======9999999 
 
 
JJJX''))1!4IHOOAss9~~&&&======******
s38}}qSXa[CC	NN::HQK
 c   c     m  T#Y S S   	  m  	  uS#s]+,	  	 &tCy CH S    "c c    S S    11
4T#Yc3hS4T#Y;OOP1 1 1 1hC
4T#Yc3hS4T#Y;OOP   4   d3i    &C DI    8 7 7S$s)^$7#h7 #h7 	7
 $(S>7 7 
#Y7 7 7 7v  #   %2J$? S ? ? ?$'CEE S ) ) )! 8 8 $d38n   
    $1:#> C > > >#&355 C ( ( (7@.6Q #T#s(^ Q Q Q       F zDFFFFF s   . BCC