
    Ph*                        U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
 dee   dedefdZ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f   eeef   ee   eeee   f   f   f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Zd1dededee   fdZdedefdZ	 d2d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d$<   d%d%d&Zeeef   ed'<   d(Zeed)<   d!d"hZee   ed*<    e       Zee   ed+<   d,d-iZ eeef   ed.<   d3d/Z!e"d0k(  r e!        yy)4    N)defaultdict)AnyDictListSetTupleUnionlinesindentationreturnc                 ~    d}dd|z  z   }t        |       D ]$  \  }}|dk7  r||z  }||j                  d|      z  }& |S )N 
 r   )	enumeratereplace)r
   r   outputnew_line_with_indentilines         mC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/utils/data/datapipes/gen_pyi.pymaterialize_linesr      sY    F#"33U#46**F$,,t%9:: $ M    dirtemplate_nameoutput_namereplacementsc           	         t         j                  j                  | |      }t         j                  j                  | |      }t        |      5 }|j	                         }d d d        |D ]H  \  }}	}
t        |d      5 }j                  |t        |	|
            }|j                  |       d d d        J y # 1 sw Y   WxY w# 1 sw Y   axY w)Nw)ospathjoinopenreadr   r   write)r   r   r   r   template_pathoutput_pathfcontentplaceholderr
   r   s              r   gen_from_templater+      s    GGLLm4M'',,sK0K	m	&&( 
+7'UK+s#qook3DUK3XYGGGG $# ,8 
	 $#s   B3:.B?3B<?C		dir_pathsfiles_to_excludec           
      B   t               }| D ]  }t        j                  |      }|D ch c]  }d|dd k(  s| }}|D ch c]	  }||vs| }}|j                  |D ch c]"  }t        j                  j                  ||      $ c}        |S c c}w c c}w c c}w )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.
    z.pyN)setr    listdirupdater!   r"   )r,   r-   pathsdir_path	all_filesfnamepython_filesfilter_filess           r   find_file_pathsr9      s     eEJJx(	+4L9%rs8K9L+7Y<%5HX;X<YNbggll8U3NO	 
 L MYNs   BB	BB 'B
r   c                     d| v rd\  }}nd| v rd\  }}nt        d|        | j                  |      t        |      z   | j                  |      }}| || S )zXExtract method name from decorator in the form of "@functional_datapipe({method_name})".(")r;   z")(')r<   z')z4Unable to find appropriate method name within line:
)RuntimeErrorfindlenr   start_token	end_tokenstartends        r   extract_method_namerE   -   sj    }!-Y	$!-YRSWRXYZZ;'#k*::DIIi<P3Ec?r   c                 p    d}d}| j                  |      t        |      z   | j                  |      }}| || S )zVExtract class name from class definition in the form of "class {CLASS_NAME}({Type}):".class ()r>   r?   r@   s        r   extract_class_namerI   9   s=    KI;'#k*::DIIi<P3Ec?r   	file_pathc                 D   i i t               }}}t        t              }t        |       5 }d}d\  }}}	d}
|j	                         D ]?  }|j                  d      dz  dk(  r|
 }
|
sd|v r||   j                  |       9d|v rt        |      }g ||<   N|rd|v rt        |      }`|rDd	|v sd
|v r<d
|v r|j                  |       |dz  }|j                  d      t        d      z   }||d }|dkD  s||j                  d      z  }||j                  d      z  }|dk(  r4|j                  d      }|	|d| z  }	t        |	      ||<   |||<   d\  }}}	|dk  rt        d      |	|j                  d      j                  d      z  }	B 	 ddd       ||||fS # 1 sw Y   xY w)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_datapiperG   zdef __init__(zdef __new__(rH   N)z7open parenthesis count < 0. This shouldn't be possible.r   r   )r0   r   listr#   	readlinescountappendrE   rI   addr>   r?   rfindprocess_signaturer=   strip)rJ   method_to_signaturemethod_to_class_namespecial_output_typedoc_string_dictr(   open_paren_countmethod_name
class_name	signatureskipr   rC   rD   s                 r   parse_datapipe_filer`   A   s   EGSU/B-!$'O	iA-7*ZKKMDzz(#a'1,xx4',33D9%-1$7/1,x4//5
4 7>T;Q!T)'++K8 A% 		#S1EF|!# DJJsO3  DJJsO3 #q(**S/Cds+I7H7S'48B(59C6KY%)&'`aaD!1!7!7!<<I? "	 
H  46I?ZZI 
s   CF0BFF
file_pathsc                     i i t               }}}i }| D ]V  }t        |      \  }}}}	|j                  |       |j                  |       |j                  |       |j                  |	       X ||||fS )N)r0   r`   r2   )
ra   methods_and_signaturesmethods_and_class_names!methods_with_special_output_typesmethods_and_doc_stringsr!   rW   rX   $methods_needing_special_output_typesrZ   s
             r   parse_datapipe_filesrh   l   s    Y[]_adaf5V3   %	
 0%%&9:&&';<)001UV&&7  "#:<]_vvvr   	delimiterc                     d}d}g }| D ];  }|dk(  r|dz  }n)|dk(  r|dz  }n||k(  r|dk(  r|j                  |       d}7||z  }= |j                  |       |S )zRGiven a line of text, split it on comma unless the comma is within a bracket '[]'.r   r   [rM   ])rR   )r   ri   bracket_count
curr_tokenreschars         r   split_outside_bracketrq   }   s}    MJ
C3;QMS[QMY=A#5JJz"Jd
  JJzJr   c                 z   t        |       }t        |      D ]v  \  }}|j                  d      ||<   |dk(  rd||<   %|dkD  rd||dz
     k(  r||   d   dk7  rd||<   Fd|v sK|j                  d	d
      \  }}|j                  d      dz   ||<   x |D cg c]
  }|dk7  s	| }}dj	                  |      } | S c c}w )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   rM   *r   z
Callable ==rL   z= ...z, )rq   r   rV   rsplitr"   )r   tokensr   tokenheaddefault_argts          r   rU   rU      s     .d3Ff%5KK$q	E>F1IU&Q-/fQilc6IF1IU" %S! 4D+

3'1F1I &  +A17aF+99VDK ,s   
B8B8deprecated_filesdefault_output_typemethod_to_special_output_typerootc                    |dk(  r:t        t        j                  t              j                  j                               }t        | t               r| gn| } | D cg c]"  }t        j                  j                  ||      $ } }t        | |j                  |            }t        |      \  }}	}
}|D ]  }||
vs|
j                  |        g }|j                         D ]Q  \  }}|	|   }||
v r||   }n|}dj                  ||         }|dk(  rd}|j                  d| d| d| d| d| 
       S |j!                  d	 
       |S c c}w )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   )r-   z    ...
z# Functional form of 'z'
def rH   z) -> z:
c                 *    | j                  d      d   S )Nr   rM   )split)ss    r   <lambda>z(get_method_definitions.<locals>.<lambda>   s    !''$-*:r   )key)strpathlibPath__file__parentresolve
isinstancer    r!   r"   r9   unionrh   rS   itemsrR   sort)rJ   r-   r}   r~   r   r   r!   ra   rc   rd   methods_w_special_output_typesrf   fn_namemethod_definitionsr\   	argumentsr]   output_type
doc_strings                      r   get_method_definitionsr      sz    rz7<<)0088:;))S9yI6?@iddD)iI@ 2B2H2HIY2Z\J 	Z( m35SUl 188*..w7 1 "8">">"@Y,[9
887DK-KWW4[AB
$J!!$::, G))4Qyk{mSV%/L#2 	3 #A  :;3 As   'E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                  &   t        t        t        t        dt              } t        t
        t        t        dt              }t        j                  t              j                  j                         }d| dfd|dfg}t        t        |      dd|       y	)
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   s       r   mainr      s     55EG^`w5CEik 4OE[]s4ACfh <<!((002D-/FJ,.DaHJL#d)$5"0#/1r   __main__),)r   )r   N)#r    r   collectionsr   typingr   r   r   r   r   r	   r   intr   r+   r9   rE   rI   r`   rh   rq   rU   r   r   __annotations__r   r0   r   r   r   r   r   r   r   __name__ r   r   <module>r      s   	  # 5 5T#Y S S 
3 
s 
 
TXY^_bdgil_lYmTn 
tCy CH S 	c 	c 	S S ([3 ([5c3hc3hQTUXQY[_`ceijmen`n[o1o+p ([VwSX w%S#XSRUXX[\_X`bfgjlpqtlugubv8v2w w"  d3i &C C 8 (*)eCcN&; )-0X)-0X) 14) ;?sCx.	)
 "%)
 /33i)Z  # %2J$? S ?$'E S )AU_s7t $d38n t $1:#> C >#&5 C (7@.6Q #T#s(^ Q1, zF r   