B
    ôàn\+  ã               @   s  d Z ddlmZmZm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>

é    )ÚdivisionÚabsolute_importÚprint_functionÚprocess_strÚprocess_fileNz2(\n|\A)((     (\$|\*))|)\s*(subroutine|function)\bz+\n\s*end\s*(subroutine|function)\b.*(\n|\Z)z\n     (\$|\*)\s*function\bc             C   s¼   g }d}x®t  | |¡}|dkr P | ¡ }t | || ¡ ¡rrx6|  d||¡}|dkrTP |}| ||d … dkr<P q<W |d7 }t | | ¡ ¡}|rš| ¡ d p t| ƒ }}| 	||f¡ q
W |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spanlistZindÚmr   Úir   © r   ú<lib/python3.7/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   sT   t  | ¡}i }x@|D ]8}|d  ¡ p*t|ƒ}|d  dd¡}t|ƒ}|||< qW |S )Nr   r
   z\,z@comma@)Únamed_reÚfindallÚstripÚ
unique_keyÚreplaceÚconv)r   ZrepsÚnamesZrepÚnameÚreplÚthelistr   r   r   Úfind_repl_patterns[   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_patternse   s    r)   z\A\\(?P<index>\d+)\Zc             C   sd   |   d¡}dd„ |D ƒ}x@tt|ƒƒD ]0}t || ¡}|r&t| d¡ƒ}|| ||< q&W d |¡S )Nú,c             S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0Úxr   r   r   ú
<listcomp>m   s    zconv.<locals>.<listcomp>Úindex)ÚsplitÚranger   Úitem_rer   ÚintÚgroupÚjoin)r   ÚbÚlr   r   Újr   r   r   r    k   s    
r    c             C   s@   t |  ¡ ƒ}d}d}x&|s:d| }||kr4|d7 }qd}qW |S )z( Obtain a unique key given a dictionary.Fr
   z__l%sT)ÚlistÚkeys)ZadictZallkeysZdoneÚnZnewkeyr   r   r   r   u   s    
r   z\A\s*(\w[\w\d]*)\s*\Zc                st  |   dd¡} |   dd¡} t| ƒ‰t d| ¡} ‡fdd„}t || ¡} d }d }i ‰xÂt | ¡D ]´}|ˆkr\ˆ || |d ¡¡}|d krtd| ƒ‚||krª| 	d	¡sª|||< d
d„ | 
d¡D ƒ}t|ƒ}|d krà|}|ˆ|< |}q\||krò|ˆ|< q\td|d ˆ| ¡||f ƒ q\W ˆs| S ‡ ‡fdd„}	d}
x&t|ƒD ]‰ |
t |	| ¡d 7 }
q:W |
  dd¡}
|
  dd¡}
|
S )Nz\>z@rightarrow@z\<z@leftarrow@z<\1>c                sn   t |  d¡ dd¡ƒ}t |¡r(d| S d }x ˆ  ¡ D ]}ˆ | |kr6|}q6W |d krftˆ ƒ}|ˆ |< d| S )Nr
   z\,z@comma@z<%s>)r    r3   r   Útemplate_name_rer   r9   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 )z@comma@r*   )r   )r+   r   r   r   r   r-   ¤   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
   )r3   Úget)r<   r"   )ÚkÚrulesr   r   Únamerepl´   s    
zexpand_sub.<locals>.namereplr&   z

ú>ú<)r   r%   r   ÚsubÚlist_reÚtemplate_rer   rA   Ú
ValueErrorÚ
startswithr/   r   Úprintr4   r0   )Zsubstrr!   r?   ZnumsubsZ	base_ruleÚrr$   ZruleZnumrD   Únewstrr   )rB   r>   rC   r   Ú
expand_sub„   sF    
rO   c       	      C   s”   | }d}t |ƒ}d}i }| t¡ x\|D ]T}t|||d … ƒ\}}||7 }| |¡ |t||d |d … |ƒ7 }|d }q(W |||d … 7 }|S )Nr&   r   r
   )r   ÚupdateÚ_special_namesr)   rO   )	ÚallstrrN   ÚwritestrZstructZoldendr!   rG   Z
cleanedstrZdefsr   r   r   r   À   s    


z9(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+[.]src)['\"]c             C   s    t j | ¡}t| ƒ}g }xz|D ]r}t |¡}|r†| d¡}t j |¡sTt j ||¡}t j 	|¡rzt
d|ƒ | t|ƒ¡ q| |¡ q| |¡ qW | ¡  |S )Nr"   zIncluding file)ÚosÚpathÚdirnameÚopenÚinclude_src_rer   r3   Úisabsr4   ÚisfilerL   ÚextendÚresolve_includesr   Úclose)ÚsourceÚdÚfidÚlinesÚliner   Úfnr   r   r   r\   Õ   s     



r\   c             C   s   t | ƒ}td |¡ƒS )Nr&   )r\   r   r4   )r^   ra   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   sv   yt jd } W n  tk
r.   t j}t j}Y n*X t| dƒ}tj | ¡\}}|}t|dƒ}| 	¡ }t
|ƒ}| |¡ d S )Nr
   rM   Úw)ÚsysÚargvÚ
IndexErrorÚstdinÚstdoutrW   rT   rU   ÚsplitextÚreadr   Úwrite)Úfiler`   ZoutfileÚbaseZextZnewnamerR   rS   r   r   r   Úmain÷   s    

ro   Ú__main__) Ú__doc__Z
__future__r   r   r   Ú__all__rT   re   r'   ÚcompileÚIr   r   r   r   rI   r   rH   r%   r)   r1   r    r   r;   rO   r   rX   r\   r   rQ   ro   Ú__name__r   r   r   r   Ú<module>/   s8   






<
