
    +jT                     ,   d Z ddlmZm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Z e            Ze	j                                        D ]&\  ZZ eee          re                    e           'dD ]Ze                    e           e
j        Ze
j        Z eej                  Z ee          Zd Zd Zd Zd Zd	 Z  G d
 d          Z! G d d          Z"d Z#d Z$d Z%d Z& ej'        d          Z( ej'        d          Z) ej'        d          Z* ej'        d          Z+ ej'        d          Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d  Z6d! Z7ej8        fd"Z9e:d#k    r$ e9ej8                  Z;e; ej<        e;           dS dS dS )$a  
This program will generate .pyi files for all the VTK modules
in the "vtkmodules" package (or whichever package you specify).
These files are used for type checking and autocompletion in
some Python IDEs.

The VTK modules must be in Python's path when you run this script.
Options are as follows:

 -p PACKAGE    The package to generate .pyi files for [vtkmodules]
 -o OUTPUT     The output directory [default is the package directory]
 -e EXT        The file suffix [.pyi]
 -i IMPORTER   The static module importer (for static builds only)
 -h HELP

With no arguments, the script runs with the defaults (the .pyi files
are put inside the existing vtkmodules package).  This is equivalent
to the following:

    python -m vtkmodules.generate_pyi -p vtkmodules

To put the pyi files somewhere else, perhaps with a different suffix:

    python -m vtkmodules.generate_pyi -o /path/to/vtkmodules -e .pyi

To generate pyi files for just one or two modules:

    python -m vtkmodules.generate_pyi -p vtkmodules vtkCommonCore vtkCommonDataModel

To generate pyi files for your own modules in your own package:

    python -m vtkmodules.generate_pyi -p mypackage mymodule [mymodule2 ...]

    )	vtkObjectvtkSOADataArrayTemplateN)AnyBufferCallbackNonePointerTemplateUnionc                 2    t          |           t          k    S )z(Check for VTK's custom method descriptor)type	vtkmethodms    Q/DATA/AppData/hermes/venv/lib/python3.11/site-packages/vtkmodules/generate_pyi.pyisvtkmethodr   D   s    GGy !    c                 B    t          t          |                     dk    S )z$Check for namespaces within a modulez,<class 'vtkmodules.vtkCommonCore.namespace'>)strr   r   s    r   isnamespacer   H   s     QLLJJKr   c                 J    t          |           ot          | t                    S )z,Check for enums (currently derived from int))isclass
issubclassintr   s    r   isenumr   M   s    1::,*Q,,-r   c                 f    | dS t          |           t          k    rdS t          |           j        S )z4Generate a typename that can be used for annotation.Nr   r
   )r   template__name__)os    r   typenamer    Q   s1    yv	aH		zAwwr   c                 F    t          |           }|t          vrd|z   dz   }|S )z:Generate a typename, or if necessary, a forward reference.')r    types)r   names     r   typename_forwardr%   Z   s+    A;;D5d{T!Kr   c                   $    e Zd ZdZd Zd Zd ZdS )Graphz A graph for topological sorting.c                     i | _         d S Nnodes)selfs    r   __init__zGraph.__init__g   s    


r   c                     | j         |         S r)   r*   )r,   r$   s     r   __getitem__zGraph.__getitem__i   s    z$r   c                     || j         |<   d S r)   r*   )r,   r$   nodes      r   __setitem__zGraph.__setitem__k   s    
4r   N)r   
__module____qualname____doc__r-   r/   r2    r   r   r'   r'   e   sG        **                r   r'   c                       e Zd ZdZd ZdS )NodezA node for the graph.c                 "    || _         || _        d S r)   )objdeps)r,   r   ds      r   r-   zNode.__init__p   s    			r   N)r   r3   r4   r5   r-   r6   r   r   r8   r8   n   s)            r   r8   c                    t                      }t          |                                           }|D ]\  }}t          |          rJ||j        k    r%d |j        D             }t          ||          ||<   Dt          ||j        g          ||<   ^t          |          rt          |g           ||<   t          |t          |          g          ||<   |S )z)Build a graph from a module's dictionary.c                     g | ]	}|j         
S r6   )r   ).0bs     r   
<listcomp>zbuild_graph.<locals>.<listcomp>|   s    999999r   )	r'   sorteditemsr   r   	__bases__r8   ismethodr    )r<   graphrC   r   r   basess         r   build_graphrH   t   s    GGE17799E . .!1:: 	.AJ99Q[9995>>a  AJ<00aa[[ 	.Ar{{E!HHA}--E!HHLr   c                     |                     |           	 | |         }n# t          $ r Y dS w xY w|j        D ]}||vrt          | |||           |                    ||j        f           dS )zHelper for topological sorting.N)addKeyErrorr;   sorted_graph_helperappendr:   )rF   r   visitedrC   r1   deps         r   rL   rL      s    KKNNNQx   y < <gsGU;;;	LL!TXs     
..c                 f    g }t                      }| j        D ]}||vrt          | |||           |S )z)Sort a graph and return the sorted items.)setr+   rL   )rF   rC   rN   r   s       r   sorted_graphrR      sF    EeeG[ ; ;G 7E:::Lr   c                 :    t          t          |                     S )zBReturn the items from a module's dictionary, topologically sorted.)rR   rH   )r<   s    r   topologically_sorted_itemsrT      s    A'''r   z!("([^\\"]|\\.)*"|'([^\\']|\\.)*')z'([A-Za-z_]([A-Za-z0-9_]|[.][A-Za-z_])*)z[ \t]+(?=\S)z[(]self[,)]z[\'\"{}\[\]()\n]c                 J   |                     d|          }|dk    rt          |          }t          j                            d| z   dz              t          j                            |||         dz              t          j                            d||z
  z  dz              dS )z.Print a parse error, syntax or otherwise.
    
zError: z:
-z^
N)findlensysstderrwrite)messagetextbeginposends        r   parse_errorrc      s     ))D#

C
byy$iiJY(50111JT%)_t+,,,JSC%K(5011111r   c                 >   t          | t          j                  r| j        }|t          vrd|z   dz   }nt          | t          j        t          j        f          rt          | j                  }| j        d         }| j	        }||j	        |z
  |j
        |z
           }t          |||          }|rCt          | t          j                  r dd                    |g|z            z   dz   }n4d|z   dz   }n+t          | t          j                  r	d|z   dz   }nd|z   dz   }|S )	z6Return the new text to be used for an annotation.
    r"   r   zTuple[, ]zList[z	Sequence[zMutableSequence[)
isinstanceastNameidr#   TupleListrZ   elts
col_offsetend_col_offsetannotation_textjoin)ar_   	is_returnr$   sizeeoffsetold_names           r   rp   rp      s,    !SX 7tu$;%D	A	38,	-	- 716{{F1Iv-a.>.GGHq(I66 	7!SY'' ,$))TF4K"8"883>~+ !SY'' 7"T)C/)D036Kr   c                    t          j        |           j        d         }d |j        j        D             }t	          |          }|                    |j                   g }t          |          D ]X\  }}|Q| |j        |j	                 }t          ||||k              }||k    r"|                    |j        |j	        |f           Y|r0d}	d}
|D ]\  }}}|	| |
|         z  }	|	|z  }	|}
|	| |
d         z  }	|	} | S )zuFix the annotations in a method definition.
    The signature must be a single-line function def, no decorators.
    r   c                     g | ]	}|j         
S r6   )
annotation)r?   args     r   rA   z#fix_annotations.<locals>.<listcomp>   s    BBBc3>BBBr   N )rh   parsebodyargsrZ   rM   returns	enumeratern   ro   rp   )	signature
definitionannotationsreturn_ichangesirr   old_textr_   newsiglastposr`   rb   s                r   fix_annotationsr      s=   
 9%%*1-JBBZ_-ABBBK;Hz)*** G%% H H!= a.>!>?H"1hh@@Dxa.>FGGG  % 	 	NE#di..FdNFGG)GHH%%	r   c                 N   t          j        dd|          }t          j        dd|          }|                    d          rd
S |                    | j        dz             rBd|z   dz   }t          j        d	k    rt          |          }||vr|                    |           d
S d
S d
S )z7Process a method signature and add it to the list.
    z\s+ z([({\[:=]) z\1zC++:(def z: ...  N)resub
startswithr   r[   
hexversionr   rM   )r   lr   s      r   push_signaturer      s     vsI..I~ui88IF## 
 			aj3.	/	/  Y&0	>Y&&'	22IAHHY    r   c                    | j         }g }||S d}d}g }|t          |          k     rt                              ||          }|r&|                                \  }}|                                }|dv rJt                              ||          }	|	r|	                                \  }}nt          d|||           n|dv r |	                    dddd	|                    n|d
v r.|r||
                                k    rt          d|||           nn|dk    rP|sNt                              ||          s3|||                                         }
|
rt          | ||
           |}nVnknTt          |          }|s0|||                                         }
|
rt          | ||
           nt          d|||           n|}|t          |          k     |S )z?Return a list of method signatures found in the docstring.
    Nr   z"'zUnterminated stringz{[(}rf   )){[r   z}])zUnmatched bracketrV   )r5   rZ   keycharsearchspangroupstringmatchrc   rM   popindentstripr   )r   doc
signaturesr`   ra   delim_stackr   rb   cr   r   s              r   get_signaturesr     s    )CJ
{ E
CK C..sC(( (	jjllGCAF{{LLc** ffhhGC 5sE3GGGe""#<#<Q#?@@@@e" a;??+<+<&<&< 3S%EEE '= dK6<<S3I3I  c	N0022	 "1j)<<<EE  c((C c	N0022	 ="1j)<<</eSAAA _ C..b r   c           	         g }| j         }| j        }|r|                    |dz             s|S t          |           }|D ]}|                    d|z   dz             rt	          j        d|z   dz   d|          }|                    d|z   dz             r.|                    t	          j        |dz   d|d	                     |                    t	          j        |dz   d
|d	                     |S )z3Get constructors from the class documentation.
    r   r   z-> '?z'?z-> Nonez()z\(z__init__(self   z__init__(self, )r   r5   r   r   r   r   rM   )r   constructorsr$   r   r   r   s         r   get_constructorsr   J  s    L:D
)C cnnTCZ00 ""J [ [	 344 	[x$6	9MMI##FTMD$899 [##BF4%<)UV$W$WXXXX##BF4%<9JIWX$Y$YZZZr   c                 b    t          |           rt                              |          sd|z   S |S )z/If method has no "self", add @static decorator.z@staticmethod
)r   has_selfr   )r   r   s     r   handle_staticr   ]  s4    1~~ hooi88  9,,r   c                 :    |t          j        dd|z   |           z   S )z:Add the given indent before every line in the string.
    z\n(?=([^\n]))rV   )r   r   )sr   s     r   
add_indentr   d  s"     BF+TF]A>>>>r   c                 X   d}|j         dk    rd|z   }d| j         z   dz   |z   dz   }d}t          | j                  }g }|D ]\\  }}t          |          r1||j         k    r&|t	          t          |          d          z  }|d	z  }E|                    ||f           ]|}g }|D ]s\  }}|                    d
          sBt          |          s3t          |          s$|d|z   dz   t          |          z   dz   z  }|d	z  }\|                    ||f           t|dk    r|dd         dz   }|S )z0Fake a namespace by creating a dummy class.
    	namespacevtkmodules.vtkCommonCorezvtkmodules.vtkCommonCore.class r   ):
r       r   __:rV   rW    ...
)r   rT   __dict__r   r   	class_pyirM   r   rE   r   r%   )	r   modbaseoutcountrC   othersr   r   s	            r   namespace_pyir   i  sw    D
|111*T1
QZ
#
%
,v
5CE 'qz22EF " "!!99 	"aj:illF333CQJEEMM1a&!!!! EF ! !!||D!! 	!(1++ 	!gajj 	!6A:#&6q&9&99D@@CQJEEMM1Q%    zz!B$i("Jr   c                    g }| j         D ]R}|j        | j        dfv r|                    |j                   -|                    |j        dz   |j        z              Sd| j        z   dz   d                    |          z   dz   }d}t          | j                  }g }|D ]\\  }}t          |          r1||j        k    r&|t          t          |          d          z  }|d	z  }E|                    ||f           ]|}g }|D ]s\  }}|
                    d
          sBt          |          s3t          |          s$|d|z   dz   t          |          z   dz   z  }|d	z  }\|                    ||f           tt          |           }	t          |	          dk    rnS|d	z  }t          |	          d	k    r|t          |	d         d          dz   z  }n|	D ]}
|t          d|
z   d          dz   z  }|}g }|D ]\  }}t          |          rt          |          }t          |          dk    r7|d	z  }t          |          d	k    r-t!          ||d                   }|t          |d          dz   z  }||D ]+}
t!          ||
          }|t          d|z   d          dz   z  },|                    ||f           |dk    r|dd         dz   }|S )z/Generate all the method stubs for a class.
    builtins.r   r   re   r   r   r   r   r   r   rV   z
@overload
rW   r   )rD   r3   rM   r   rq   rT   r   r   r   r   r   rE   r%   r   rZ   r   r   )r   rG   r@   r   r   rC   r   r   r   r   overloadr   r   s                r   r   r     sU    E[ : :<AL*555LL$$$$LL+aj89999
QZ
#
%		%(8(8
86
ACE 'qz22EF " "!1:: 	"!qz//:illF333CQJEEMM1a&!!!! EF ! !!||D!! 	!(1++ 	!gajj 	!6A:#&6q&9&99D@@CQJEEMM1Q%     $A&&L
<A 	
|!!:l1ov66==CC( K Kz-(":FCCdJJ EF " "!A;; 	"'**J:!##QJE:!##*1jm<<
9f55<<& M M*1h77
=9#<fEELLM MM1a&!!!!zz!B$i("Jr   c                 V	   |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           |                     d           | j        dk    r*|                     d	           |                     d           t          dg          }| j                                        D ]C\  }}t          |          r/||j        k    r$|j        D ]}|                    |j                   D|	                    | j                   |	                    d
           t          |          D ]}|                     d|z   dz              |r|                     d           t          | j                  }g }|D ]o\  }}t          |          rD||j        k    r9|                     t          ||                      |                     d           X|                    ||f           p|}g }|D ]n\  }}t          |          rC||j        k    r8|                     t!          |                     |                     d           W|                    ||f           o|}g }|D ]Z\  }}t          |          r/||j        k    r$|                     |dz   |j        z   dz              C|                    ||f           [|}g }|D ]{\  }}|                    d          sJt%          |          s;t          |          s,|                     |dz   t'          |          z   dz              d|                    ||f           |t)          |          t)          |          k    r|                     d           |}g }|D ]n\  }}t          |          rC||j        k    r8|                     t!          |                     |                     d           W|                    ||f           o|}g }|D ]Z\  }}t          |          r/||j        k    r$|                     |dz   |j        z   dz              C|                    ||f           [dS )z;Generate the contents of a .pyi file for a VTK module.
    z;from typing import overload, Any, Callable, TypeVar, Union
z:from typing import Tuple, List, Sequence, MutableSequence
rV   z,Callback = Union[Callable[..., None], None]
zBuffer = TypeVar('Buffer')
zPointer = TypeVar('Pointer')
zTemplate = TypeVar('Template')
r   zclass namespace: pass
r   zimport z = r   r   N)r]   r   rQ   r   rC   r   rD   rJ   r3   discardrB   rT   r   r   rM   r   r   r   rE   r%   rZ   )	r   outputdependsr   r   r   dependrC   r   s	            r   
module_pyir     s    LLOPPP
LLNOOO
LL
LL@AAA
LL/000
LL1222
LL3444
LL
|111.///T -.//G|!!## - -!1:: 	-!qz// - -DO,,,,OOCL!!!OOJ// 0 0Y'$.//// T 's|44E F " "!q>> 	"a1:ooLLq#..///LLMM1a&!!!! EF " "!!99 	"ajLL1&&&LLMM1a&!!!! EF " "!!99 	"ajLLUQZ/$67777MM1a&!!!! EF ! !!||D!! 	!(1++ 	!gajj 	!LLS#3A#6#66=>>>>MM1Q%    
5zzCKK||D EF " "!1:: 	"!qz//LL1&&&LLMM1a&!!!! EF " "!1:: 	"!qz//LLUQZ/$67777MM1a&!!!!	" "r   c                    t           j                            | d                   }t          j        | d         d|z   dz   d          }|                    ddt          dd	
           |                    ddt          d           |                    ddt          d           |                    ddt          dd
           |                    dddd           |                    dt          dd           |                    | dd                    }|j        }|j	        }|j
        }|j        }|j        rt          |          dk    r't          j                            |dz   d z              dS |d!z   |d         z                       d!d"          }|t          j        vrt'          j        |j                   |t          |          dk    rt'          j        |          }	|t+          |	d#d           }
|
#t           j                            |
          d$k    r*t          j                            |d%z   |z   d&z              dS t           j                            |
          }t          |          dk    r|	j        D ]}	 t&          j                            |d!z   |z             }n># t4          $ r1 d }|s*d'}t          j                            |d(z   |z   d)z              Y nw xY w|jt7          |j        t&          j        j                  s|                    |           d*}|D ]}t           j                             |||z             }|j!        rkt          j"        d+k    rtF          j$        nd}tK          |d,          5 }tM          |'                                |d-|           d d d            n# 1 swxY w Y   t'          j        |d!z   |z             }	tK          |d.          5 }tQ          |	|           d d d            n# 1 swxY w Y   tK          t           j                             |d/          d.          5 }	 d d d            d S # 1 swxY w Y   d S )0Nr   zpython z [-p package] [-o output_dir]z-A .pyi generator for the VTK python wrappers.)progusagedescriptionz-pz	--package
vtkmoduleszPackage name [vtkmodules].)r   defaulthelpz-iz
--importerzStatic module importer [].)r   r   z-oz--outputz%Output directory [package directory].z-ez--extz.pyizOutput file suffix [.pyi].z--testr   z)Test .pyi files instead of creating them.)actionr   r   modules*zModules to process [all].)r   nargsr   r   z: when using '-i', all modules z3in the package must be listed on the command line.
r   ___file__z__init__.pyz: z has no __init__.py
Tz: couldn't get loader for rV   Fr   rexecwzpy.typed))ospathbasenameargparseArgumentParseradd_argumentr   
parse_argspackager   r   extimporterrZ   r[   r\   r]   replacebuiltin_module_names	importlibimport_modulegetattrdirname__all__util	find_spec
ValueErrorrg   loader	machineryExtensionFileLoaderrM   rq   testr   rh   PyCF_TYPE_COMMENTSopencompilereadr   )argvprognameparserr   packagenamer   basedirr   module_exemplarr   filenamemodnamespecerrflagpyifileflagsfs                    r   mainr  /  sn   wQ((H $!W("%DDCE E EF k\9  ; ; ;
l9  ; ; ;
jsD  F F F
gC9  ; ; ;
!H  J J J
	38  : : :T!""X&&D ,KlGkG
(C } 	3w<<1JX(IIFG H H H1 ',wqz9BB3LL#":::#DM222 #g,,!++%k223
D11rw//99]JJJX_{:=TTUUU1'//(++
7||q{ 	$ 	$G_ ~//c0AG0KLL _ _ _ _"GJ$$X0L%Lw%VY]%]^^^	_
 |dk9+>+RSS NN7#### G # #',,w#669 		#.1n	.I.IC**qEgs## :q'65999: : : : : : : : : : : : : : : )+*;g*EFFCgs## #q3"""# # # # # # # # # # # # # # # 
bgll7J//	5	5                  sH   %J**8K%$K%%N55N9	<N9	+PP	P	QQQ__main__)=r5   vtkmodules.vtkCommonCorer   r   r[   r   r   rh   r   r   inspectimportlib.utilr   rQ   r#   r   rC   r   r   rg   r   rJ   	isroutinerE   r   IsAr   r   r   r   r   r    r%   r'   r8   rH   rL   rR   rT   r   r   
identifierr   r   r   rc   rp   r   r   r   r   r   r   r   r   r   r   r  r   resultexitr6   r   r   <module>r     sn  ! !F H G G G G G G G 



 				 				 



        	""$$  CAaz!T 		!	N  A	IIaLLLL 
/ D	4'((" " "L L L
. . .                               &
  
  
   ( ( ( 
<	=	=RZFGG
	O	$	$2:n%% "*(
)
)2 2 2  <  @     (@ @ @D  &  ? ? ?
     DF F FPZ" Z" Z"x h T T T Tl zT#(^^F r   