o
    g                     @   s   d Z ddgZddlZddlZddlZedejZedejZedejZ	dd	 Z
ed
ZedZedZdd Zdd ZedZdd Zdd ZedZdd Zdd ZedejZdd Zdd ZedZdd  Zed!kr|e  dS dS )"a  

process_file(filename)

  takes templated file .xxx.src and produces .xxx file where .xxx
  is .pyf .f90 or .f using the following template rules:

  '<..>' denotes a template.

  All function and subroutine blocks in a source file with names that
  contain '<..>' will be replicated according to the rules in '<..>'.

  The number of comma-separated words in '<..>' will determine the number of
  replicates.

  '<..>' may have two different forms, named and short. For example,

  named:
   <p=d,s,z,c> where anywhere inside a block '<p>' will be replaced with
   'd', 's', 'z', and 'c' for each replicate of the block.

   <_c>  is already defined: <_c=s,d,c,z>
   <_t>  is already defined: <_t=real,double precision,complex,double complex>

  short:
   <s,d,c,z>, a short form of the named, useful when no <p> appears inside
   a block.

  In general, '<..>' contains a comma separated list of arbitrary
  expressions. If these expression must contain a comma|leftarrow|rightarrow,
  then prepend the comma|leftarrow|rightarrow with a backslash.

  If an expression matches '\<index>' then it will be replaced
  by <index>-th expression.

  Note that all '<..>' forms in a block must have the same number of
  comma-separated entries.

 Predefined named template rules:
  <prefix=s,d,c,z>
  <ftype=real,double precision,complex,double complex>
  <ftypereal=real,double precision,\0,\1>
  <ctype=float,double,complex_float,complex_double>
  <ctypereal=float,double,\0,\1>

process_strprocess_file    Nz2(\n|\A)((     (\$|\*))|)\s*(subroutine|function)\bz+\n\s*end\s*(subroutine|function)\b.*(\n|\Z)z\n     (\$|\*)\s*function\bc                 C   s   g }d}	 t | |}|du r	 |S | }t| || r:	 | d||}|dkr,n|}| ||d  dkr9nq |d7 }t| | }|rN| d pQt|  }}|	||f q)	z Return a list of tuples for each function or subroutine each
    tuple is the start and end of a subroutine or function to be
    expanded.
    r   TN
   z
     $   )
routine_start_researchstartfunction_start_rematchendrfindroutine_end_relenappend)astrspanlistindmr
   ir    r   W/var/www/html/api-tag/env/lib/python3.10/site-packages/numpy/distutils/from_template.pyparse_structure:   s,   r   z<\s*(\w[\w\d]*)\s*>z<\s*(\w[\w\d]*)\s*=\s*(.*?)\s*>z<\s*((.*?))\s*>c                 C   sP   t | }i }|D ]}|d  pt|}|d dd}t|}|||< q	|S )Nr   r   \,@comma@)named_refindallstrip
unique_keyreplaceconv)r   repsnamesrepnamereplthelistr   r   r   find_repl_patternsY   s   

r(   c                 C   s"   t | }ttd| d } | |fS )N r   )r(   resubnr   )r   r#   r   r   r   find_and_remove_repl_patternsc   s   r,   z\A\\(?P<index>\d+)\Zc                 C   s`   |  d}dd |D }tt|D ]}t|| }|r*t|d}|| ||< qd|S )N,c                 S   s   g | ]}|  qS r   )r   ).0xr   r   r   
<listcomp>k   s    zconv.<locals>.<listcomp>index)splitranger   item_rer   intgroupjoin)r   blr   r   jr   r   r   r!   i   s   

r!   c                 C   s>   t |  }d}d}|sd| }||v r|d7 }nd}|r|S )z( Obtain a unique key given a dictionary.Fr   z__l%sT)listkeys)adictallkeysdonennewkeyr   r   r   r   s   s   
r   z\A\s*(\w[\w\d]*)\s*\Zc                    sh  |  dd} |  dd} t| td| } fdd}t|| } d }d }i t| D ]Z}|vr|||d }|d u rGtd| ||vrT|	d	sT|||< d
d |
dD }t|}|d u ro|}||< |}q-||krx||< q-td|d| ||f  q-s| S  fdd}	d}
t|D ] |
t|	| d 7 }
q|
 dd}
|
 dd}
|
S )Nz\>z@rightarrow@z\<z@leftarrow@z<\1>c                    sj   t | ddd}t|rd| S d }  D ]
} | |kr$|}q|d u r1t }| |< d| S )Nr   r   r   z<%s>)r!   r6   r    template_name_rer   r<   r   )mobjr'   r%   key)lnamesr   r   listrepl   s   
zexpand_sub.<locals>.listreplzNo replicates found for <%s>_c                 S   s   g | ]}| d dqS )r   r-   )r    )r.   r   r   r   r   r0      s    zexpand_sub.<locals>.<listcomp>r-   zHMismatch in number of replacements (base <%s=%s>) for <%s=%s>. Ignoring.c                    s$   |  d}| d |g   S )Nr   )r6   get)rC   r%   )krulesr   r   namerepl   s   
zexpand_sub.<locals>.namereplr)   z

><)r    r(   r   sublist_retemplate_rer   rH   
ValueError
startswithr2   r   printr7   r3   )substrr#   rF   numsubs	base_rulerr'   rulenumrK   newstrr   )rI   rE   rJ   r   
expand_sub   sJ   
r[   c           	      C   s   | }d}t |}d}i }|t |D ]*}t|||d  \}}||7 }|| |t||d |d  |7 }|d }q|||d  7 }|S )Nr)   r   r   )r   update_special_namesr,   r[   )	allstrrZ   writestrstructoldendr#   rN   
cleanedstrdefsr   r   r   r      s   


z8(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+\.src)['\"]c                 C   s   t j| }t| B}g }|D ]4}t|}|r>|d}t j|s*t j||}t j	|r8|
t| q|| q|| qW d    |S 1 sOw   Y  |S )Nr%   )ospathdirnameopeninclude_src_rer   r6   isabsr7   isfileextendresolve_includesr   )sourcedfidlinesliner   fnr   r   r   rl      s$   



rl   c                 C   s   t | }td|S )Nr)   )rl   r   r7   )rm   rp   r   r   r   r      s   z
<_c=s,d,c,z>
<_t=real,double precision,complex,double complex>
<prefix=s,d,c,z>
<ftype=real,double precision,complex,double complex>
<ctype=float,double,complex_float,complex_double>
<ftypereal=real,double precision,\0,\1>
<ctypereal=float,double,\0,\1>
c                  C   st   zt jd } W n ty   t j}t j}Y nw t| d}tj| \}}|}t|d}|	 }t
|}|| d S )Nr   rW   w)sysargv
IndexErrorstdinstdoutrg   rd   re   splitextreadr   write)filero   outfilebaseextnewnamer^   r_   r   r   r   main   s   


r   __main__)__doc____all__rd   rt   r*   compileIr   r   r   r   rP   r   rO   r(   r,   r4   r!   r   rB   r[   r   rh   rl   r   r]   r   __name__r   r   r   r   <module>   s8   .






<

