
    t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                  j!                         D ]   \  ZZ eee      sej+                  e       " dD ]  Zej+                  e        e
j,                  Ze
j0                  Z eej2                        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jN                  d      Z( ejN                  d      Z) ejN                  d      Z* ejN                  d      Z+ ejN                  d      Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d  Z6d! Z7ejp                  fd"Z9e:d#k(  r( e9ejp                        Z;e; ejx                  e;       yyy)$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                 &    t        |       t        k(  S )z(Check for VTK's custom method descriptor)type	vtkmethodms    D/DATA/.local/lib/python3.12/site-packages/vtkmodules/generate_pyi.pyisvtkmethodr   D   s    Gy !    c                 0    t        t        |             dk(  S )z$Check for namespaces within a modulez,<class 'vtkmodules.vtkCommonCore.namespace'>)strr   r   s    r   isnamespacer   H   s     QLJJKr   c                 <    t        |       xr t        | t              S )z,Check for enums (currently derived from int))isclass
issubclassintr   s    r   isenumr   M   s    1:,*Q,-r   c                 X    | yt        |       t        k(  ryt        |       j                  S )z4Generate a typename that can be used for annotation.r   r
   )r   template__name__)os    r   typenamer    Q   s)    y	aH	Awr   c                 <    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y)Graphz A graph for topological sorting.c                     i | _         y 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    zz$r   c                 "    || j                   |<   y 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   s    *  r   r'   c                       e Zd ZdZd Zy)NodezA node for the graph.c                      || _         || _        y 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        | j                               }|D ]  \  }}t        |      r]||j                  k(  r3|j
                  D cg c]  }|j                   }}t        ||      ||<   St        ||j                  g      ||<   nt        |      rt        |g       ||<   t        |t        |      g      ||<    |S c c}w )z)Build a graph from a module's dictionary.)	r'   sorteditemsr   r   	__bases__r8   ismethodr    )r<   graphr?   r   r   bbasess          r   build_graphrE   t   s    GE1779E!1:AJJ-.[[9[[95>a  AJJ<0aa[Ar{E!HA}-E!H  L :s   Cc                     |j                  |       	 | |   }|j                  D ]  }||vst        | |||        |j	                  ||j
                  f       y# t        $ r Y yw xY w)zHelper for topological sorting.N)addKeyErrorr;   sorted_graph_helperappendr:   )rB   r   visitedr?   r1   deps         r   rI   rI      sk    KKNQx yygsGU;  
LL!TXX  s   A 	A&%A&c                 f    g }t               }| j                  D ]  }||vst        | |||        |S )z)Sort a graph and return the sorted items.)setr+   rI   )rB   r?   rK   r   s       r   sorted_graphrO      s9    EeG[[G 7E:  Lr   c                 *    t        t        |             S )zBReturn the items from a module's dictionary, topologically sorted.)rO   rE   )r<   s    r   topologically_sorted_itemsrQ      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                  j	                  d| z   dz          t        j                  j	                  ||| dz          t        j                  j	                  d||z
  z  dz          y)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_errorr`      s}     ))D#
C
by$iJJY(501JJT%_t+,JJSC%K(501r   c                 `   t        | t        j                        r| j                  }|t        vrd|z   dz   }|S t        | t        j
                  t        j                  f      rt        | j                        }| j                  d   }| j                  }||j                  |z
  |j                  |z
   }t        |||      }|rAt        | t        j
                        rddj                  |g|z        z   dz   }|S d|z   dz   }|S t        | t        j
                        r
d|z   dz   }|S 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ListrW   elts
col_offsetend_col_offsetannotation_textjoin)ar\   	is_returnr$   sizeeoffsetold_names           r   rm   rm      s+    !SXXttu$;%D* K) 
A		388,	-166{FF1Iv-a.>.>.GHq(I6!SYY'$))TF4K"883> K ~+ K !SYY'"T)C/ K *D036Kr   c                 ,   t        j                  |       j                  d   }|j                  j                  D cg c]  }|j                   }}t        |      }|j                  |j                         g }t        |      D ]_  \  }}|	| |j                  |j                   }t        ||||k(        }	|	|k7  s8|j                  |j                  |j                  |	f       a |r(d}
d}|D ]  \  }}}	|
| || z  }
|
|	z  }
|} |
| |d z  }
|
} | S c c}w )zuFix the annotations in a method definition.
    The signature must be a single-line function def, no decorators.
    r   N )re   parsebodyargs
annotationrW   rJ   returns	enumeraterk   rl   rm   )	signature
definitionargannotationsreturn_ichangesiro   old_textr\   newsiglastposr]   r_   s                 r   fix_annotationsr      s-   
 9%**1-J-7__-A-AB-Ac3>>-AKB;Hz))* G%!= a.>.>?H"1hh@Dxa.>.>FG & %NE#di..FdNFG & 	)GH%%	1 Cs   Dc                 :   t        j                  dd|      }t        j                  dd|      }|j                  d      ry
|j                  | j                  dz         r=d|z   dz   }t        j
                  d	k\  rt        |      }||vr|j                  |       y
y
y
)z7Process a method signature and add it to the list.
    z\s+ z([({\[:=]) z\1zC++:(def z: ...  N)resub
startswithr   rX   
hexversionr   rJ   )r   lr}   s      r   push_signaturer      s     vsI.I~ui8IF#			ajj3.	/Y&0	>>Y&'	2IAHHY  
0r   c                 
   | j                   }g }||S d}d}g }|t        |      k  r[t        j                  ||      }|r|j	                         \  }}|j                         }|dv r=t        j                  ||      }	|	r|	j	                         \  }}nt        d|||       	 |S |dv r|j                  dddd|          n|d	v r&|r||j                         k7  rt        d
|||       	 |S |dk(  r|st        j                  ||      si||| j                         }
|
rt        | ||
       |}nD	 |S t        |      }|s#||| j                         }
|
rt        | ||
       nt        d
|||       	 |S |}|t        |      k  r[|S )z?Return a list of method signatures found in the docstring.
    r   z"'zUnterminated stringz{[(}rc   )){[r   z}])zUnmatched bracketrS   )r5   rW   keycharsearchspangroupstringmatchr`   rJ   popindentstripr   )r   doc
signaturesr]   r^   delim_stackr   r_   cr   r}   s              r   get_signaturesr     s    ))CJ
{ E
CK C.sC(jjlGCAF{LLc*ffhGC 5sE3GB A e""#<Q#?@e"a;??+<&< 3S%E2 1 dK6<<S3I  cN002	"1j)<E   c(CcN002	"1j)</eSA
  _ C.b r   c           	         g }| j                   }| j                  }|r|j                  |dz         s|S t        |       }|D ]  }|j                  d|z   dz         st	        j
                  d|z   dz   d|      }|j                  d|z   dz         r+|j                  t	        j
                  |dz   d|d	             z|j                  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   rJ   )r   constructorsr$   r   r   r}   s         r   get_constructorsr   J  s     L::D
))CcnnTCZ0"J	 34x$6	9MI##FTMD$89##BFF4%<)UV$WX##BFF4%<9JIWX$YZ   r   c                 P    t        |       rt        j                  |      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   ]  s%    1~hooi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]))rS   )r   r   )sr   s     r   
add_indentr   d  s!     BFF+TF]A>>>r   c                 (   d}|j                   dk7  rd|z   }d| j                   z   dz   |z   dz   }d}t        | j                        }g }|D ]P  \  }}t        |      r-||j                   k(  r|t	        t        |      d      z  }|d	z  }>|j                  ||f       R |}g }|D ]_  \  }}|j                  d
      s6t        |      s+t        |      s |d|z   dz   t        |      z   dz   z  }|d	z  }M|j                  ||f       a |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   __:rS   rT    ...
)r   rQ   __dict__r   r   	class_pyirJ   r   rA   r   r%   )	r   modbaseoutcountr?   othersr   r   s	            r   namespace_pyir   i  s0    D
||11*T1
QZZ
#
%
,v
5CE 'qzz2EF!!9ajj:ilF33CQJEMM1a&!  EF!||D!(1+gaj6A:#&6q&99D@@CQJEMM1Q%   z!Bi("Jr   c                    g }| j                   D ]c  }|j                  | j                  dfv r|j                  |j                         9|j                  |j                  dz   |j                  z          e d| j                  z   dz   dj	                  |      z   dz   }d}t        | j                        }g }|D ]P  \  }}t        |      r-||j                  k(  r|t        t        |      d      z  }|d	z  }>|j                  ||f       R |}g }|D ]_  \  }}|j                  d
      s6t        |      s+t        |      s |d|z   dz   t        |      z   dz   z  }|d	z  }M|j                  ||f       a t        |       }	t        |	      dk(  rnE|d	z  }t        |	      d	k(  r|t        |	d   d      dz   z  }n|	D ]  }
|t        d|
z   d      dz   z  } |}g }|D ]  \  }}t        |      rxt        |      }t        |      dk(  r+|d	z  }t        |      d	k(  r"t!        ||d         }|t        |d      dz   z  }`|D ]#  }
t!        ||
      }|t        d|z   d      dz   z  }% |j                  ||f        |dk(  r|dd dz   }|S )z/Generate all the method stubs for a class.
    builtins.r   r   rb   r   r   r   r   r   r   rS   z
@overload
rT   r   )r@   r3   rJ   r   rn   rQ   r   r   r   r   r   rA   r%   r   rW   r   r   )r   rD   rC   r   r   r?   r   r   r   r   overloadr   r}   s                r   r   r     s    E[[<<ALL*55LL$LL+ajj89	  QZZ
#
%		%(8
86
ACE 'qzz2EF!1:!qzz/:ilF33CQJEMM1a&!  EF!||D!(1+gaj6A:#&6q&99D@@CQJEMM1Q%   $A&L
<A 	
|!:l1ov6==C(z-(":FCdJJ ) EF!A;'*J:!#QJE:!#*1jm<
9f5<<&*1h7
=9#<fELL ' MM1a&!   z!Bi("Jr   c                 h   |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       | j                  dk(  r"|j                  d	       |j                  d       t        dg      }| j                  j	                         D ]M  \  }}t        |      s||j                  k(  s"|j                  D ]  }|j                  |j                          O |j                  | j                         |j                  d
       t        |      D ]  }|j                  d|z   dz           |r|j                  d       t        | j                        }g }|D ]_  \  }}t        |      r<||j                  k(  r-|j                  t        ||              |j                  d       M|j                  ||f       a |}g }|D ]^  \  }}t        |      r;||j                  k(  r,|j                  t!        |             |j                  d       L|j                  ||f       ` |}g }|D ]W  \  }}t        |      r4||j                  k7  r%|j                  |dz   |j                  z   dz          E|j                  ||f       Y |}g }|D ]c  \  }}|j#                  d      s:t%        |      s/t        |      s$|j                  |dz   t'        |      z   dz          Q|j                  ||f       e t)        |      t)        |      kD  r|j                  d       |}g }|D ]^  \  }}t        |      r;||j                  k(  r,|j                  t!        |             |j                  d       L|j                  ||f       ` |}g }|D ]W  \  }}t        |      r4||j                  k7  r%|j                  |dz   |j                  z   dz          E|j                  ||f       Y y)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
rS   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)rZ   r   rN   r   r?   r   r@   rG   r3   discardr>   rQ   r   r   rJ   r   r   r   rA   r%   rW   )	r   outputdependsr   r   r   dependr?   r   s	            r   
module_pyir     s    LLOP
LLNO
LL
LL@A
LL/0
LL12
LL34
LL
||11./T -./G||!!#!1:!qzz/DOO, $ $ OOCLL!OOJ/Y'$./ "T 's||4E F!q>a1::oLLq#./LLMM1a&!  EF!!9ajjLL1&LLMM1a&!  EF!!9ajjLLUQZZ/$67MM1a&!	  EF!||D!(1+gajLLS#3A#66=>MM1Q% 	 
 5zCK||D EF!1:!qzz/LL1&LLMM1a&!  EF!1:!qzz/LLUQZZ/$67MM1a&!	 r   c                 h   t         j                  j                  | d         }t        j                  | d   d|z   dz   d      }|j                  ddt        dd	
       |j                  ddt        d       |j                  ddt        d       |j                  ddt        dd
       |j                  dddd       |j                  dt        dd       |j                  | dd        }|j                  }|j                  }|j                  }|j                  }|j                  rt        |      dk(  r&t        j                  j!                  |dz   d z          y|d!z   |d   z   j#                  d!d"      }|t        j$                  vrt'        j(                  |j                         |t        |      dk(  rt'        j(                  |      }	|yt+        	d#d       }
|
"t         j                  j                  |
      d$k7  r)t        j                  j!                  |d%z   |z   d&z          yt         j                  j-                  |
      }t        |      dk(  rz	j.                  D ]k  }	 t&        j0                  j3                  |d!z   |z         }|,t7        |j8                  t&        j:                  j<                        s[|j?                  |       m d*}|D ]  }t         j                  jA                  |||z         }|jB                  rWt        jD                  d+k\  rtF        jH                  nd}tK        |d,      5 }tM        |jO                         |d-|       d d d        t'        j(                  |d!z   |z         }	tK        |d.      5 }tQ        |	|       d d d         tK        t         j                  jA                  |d/      d.      5 }	 d d d        y # t4        $ r2 d }s*d'}t        j                  j!                  |d(z   |z   d)z          Y w xY w# 1 sw Y   DxY w# 1 sw Y   QxY w# 1 sw Y   y xY w)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 rS   Fr   rexecwzpy.typed))ospathbasenameargparseArgumentParseradd_argumentr   
parse_argspackager   r   extimporterrW   rX   rY   rZ   replacebuiltin_module_names	importlibimport_modulegetattrdirname__all__util	find_spec
ValueErrorrd   loader	machineryExtensionFileLoaderrJ   rn   testr   re   PyCF_TYPE_COMMENTSopencompilereadr   )argvprognameparserry   packagenamer   basedirr   module_exemplarr   filenamemodnamespecerrflagpyifileflagsfs                    r   mainr  /  s   wwQ(H $$!W("%DDCEF k\9  ;
l9  ;
jsD  F
gC9  ;
!H  J
	38  :T!"X&D ,,KllGkkG
((C }}w<1JJX(IIFG H ',wqz9BB3L#":"::##DMM2 #g,!+%%k23
D1rww//9]JJJX_{:=TTU''//(+
7|q{{G_ ~~//c0AG0KL |dkk9+>+>+R+RSNN7# #" G'',,w#699.1nn	.IC**qEgs#q'659 $# ))+*;g*EFCgs#q3" $#  
bggll7J/	5 
6	59  _"GJJ$$X0L%Lw%VY]%]^	_& $#
 $# 
6	5s6   &%O7PPP(7P
PP	P%	(P1__main__)=r5   vtkmodules.vtkCommonCorer   r   rX   r   r   re   r   r   inspectimportlib.utilr   rN   r#   r   r?   r   r   rd   r   rG   	isroutinerA   r   IsAr   r   r   r   r   r    r%   r'   r8   rE   rI   rO   rQ   r   r   
identifierr   r   r   r`   rm   r   r   r   r   r   r   r   r   r   r   r  r   resultexitr6   r   r   <module>r     s  !F H 
 	 	 
     	""$CAa!T		! % 
OA	IIaL 
O 
// 	'("L
.     &
 ( 
<	=RZZFG
	O	$2::n% "**(
)2<@ (@D&?
 DFPZ"x hh Tl z#((^F  r   