a
    p=icé  ã                   @   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 )"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q°| ¡ }t | || ¡ ¡rl|  d||¡}|dkrPql|}| ||d … dkr8qlq8|d7 }t | | ¡ ¡}|r”| ¡ d pšt| ƒ }}| 	||f¡ q|S )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   NÚ
éÿÿÿÿé   z
     $é   )
Úroutine_start_reÚsearchÚstartÚfunction_start_reÚmatchÚendÚrfindÚroutine_end_reÚlenÚappend)ÚastrZspanlistÚindÚmr
   Úir   © r   ún/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/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 ]8}|d  ¡ p(t|ƒ}|d  dd¡}t|ƒ}|||< q|S )Nr   r   ú\,ú@comma@)Únamed_reÚfindallÚstripÚ
unique_keyÚreplaceÚconv)r   Z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 ]0}t || ¡}|r$t| d¡ƒ}|| ||< q$d |¡S )Nú,c                 S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0Úxr   r   r   Ú
<listcomp>k   ó    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}|s8d| }||v r2|d7 }qd}q|S )z( Obtain a unique key given a dictionary.Fr   z__l%sT)ÚlistÚkeys)ÚadictZallkeysÚdoneÚnZnewkeyr   r   r   r   s   s    
r   z\A\s*(\w[\w\d]*)\s*\Zc                    sl  |   dd¡} |   dd¡} t| ƒ‰t d| ¡} ‡fdd„}t || ¡} d }d }i ‰t | ¡D ]´}|ˆvrZˆ || |d ¡¡}|d u rŽtd| ƒ‚||vr¨| 	d	¡s¨|||< d
d„ | 
d¡D ƒ}t|ƒ}|d u rÞ|}|ˆ|< |}qZ||krð|ˆ|< qZtd|d ˆ| ¡||f ƒ qZˆs| S ‡ ‡fdd„}	d}
t|ƒD ]‰ |
t |	| ¡d 7 }
q4|
  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r4|}q4|d u rbtˆ ƒ}|ˆ |< d| S )Nr   r   r   z<%s>)r    r5   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   r.   ¢   r/   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   )r5   Úget)r@   r#   )ÚkÚrulesr   r   Únamerepl²   s    
zexpand_sub.<locals>.namereplr'   z

ú>ú<)r   r&   r   ÚsubÚlist_reÚtemplate_rer   rE   Ú
ValueErrorÚ
startswithr1   r   Úprintr6   r2   )Úsubstrr!   rC   ZnumsubsZ	base_ruleÚrr%   ÚruleÚnumrH   Únewstrr   )rF   rB   rG   r   Ú
expand_sub‚   sH    
þrV   c           	      C   s   | }d}t |ƒ}d}i }| t¡ |D ]T}t|||d … ƒ\}}||7 }| |¡ |t||d |d … |ƒ7 }|d }q&|||d … 7 }|S )Nr'   r   r   )r   ÚupdateÚ_special_namesr*   rV   )	ÚallstrrU   ÚwritestrÚstructZoldendr!   rK   Z
cleanedstrZdefsr   r   r   r   ¾   s    


z8(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+\.src)['\"]c                 C   sª   t j | ¡}t| ƒ‚}g }|D ]h}t |¡}|r|| d¡}t j |¡sTt j ||¡}t j 	|¡rp| 
t|ƒ¡ q†| |¡ q| |¡ qW d   ƒ n1 sœ0    Y  |S )Nr#   )ÚosÚpathÚdirnameÚopenÚinclude_src_rer   r5   Úisabsr6   ÚisfileÚextendÚresolve_includesr   )ÚsourceÚdÚfidÚlinesÚliner   Úfnr   r   r   rd   Ó   s    


*rd   c                 C   s   t | ƒ}td |¡ƒS )Nr'   )rd   r   r6   )re   rh   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*0 t| dƒ}tj | ¡\}}|}t|dƒ}| 	¡ }t
|ƒ}| |¡ d S )Nr   rR   Úw)ÚsysÚargvÚ
IndexErrorÚstdinÚstdoutr_   r\   r]   ÚsplitextÚreadr   Úwrite)Úfilerg   ÚoutfileÚbaseÚextZnewnamerY   rZ   r   r   r   Úmainó   s    

rx   Ú__main__)Ú__doc__Ú__all__r\   rl   r(   ÚcompileÚIr   r   r   r   rM   r   rL   r&   r*   r3   r    r   r?   rV   r   r`   rd   r   rX   rx   Ú__name__r   r   r   r   Ú<module>   s4   .






<
