3
­?d][  ã               @   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j	dej
ƒZej	dej
ƒZej	dej
ƒZd	d
„ Zej	dƒZej	dƒZej	dƒZdd„ Zdd„ Zej	dƒZdd„ Zdd„ Zej	dƒZdd„ Zdd„ Zej	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 j| |ƒ}|dkr P |jƒ }tj| ||jƒ ƒrrx6| jd||ƒ}|dkrTP |}| ||d … dkr<P q<W |d7 }tj| |jƒ ƒ}|rš|jƒ d p t| ƒ }}|j	||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spanlistÚindÚmr   Úir   © r   ú>/tmp/pip-build-op5p2y5_/numpy/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 j| ƒ}i }x@|D ]8}|d jƒ p*t|ƒ}|d jddƒ}t|ƒ}|||< qW |S )Nr   r   z\,z@comma@)Únamed_reÚfindallÚstripÚ
unique_keyÚreplaceÚconv)r   ZrepsÚnamesÚrepÚnameÚreplÚthelistr   r   r   Úfind_repl_patterns[   s    

r'   c             C   s"   t | ƒ}tj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   | j dƒ}dd„ |D ƒ}x@tt|ƒƒD ]0}tj|| ƒ}|r&t|jdƒƒ}|| ||< q&W dj|ƒS )Nú,c             S   s   g | ]}|j ƒ ‘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 | jƒ ƒ}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)ÚadictZallkeysÚdoneÚnZnewkeyr   r   r   r   u   s    
r   z\A\s*(\w[\w\d]*)\s*\Zc                sv  | j ddƒ} | j ddƒ} t| ƒ‰tjd| ƒ} ‡fdd„}tj|| ƒ} d }d }i ‰xÄtj| ƒD ]¶}|ˆkr\ˆj||j|d ƒƒ}|d kr�td| ƒ‚||kr¬|j	d	ƒ r¬|||< d
d„ |j
dƒD ƒ}t|ƒ}|d krâ|}|ˆ|< |}q\||krô|ˆ|< q\td|djˆ| ƒ||f ƒ q\W ˆ�s | S ‡ ‡fdd„}	d}
x&t|ƒD ]‰ |
tj|	| ƒd 7 }
�q<W |
j ddƒ}
|
j ddƒ}
|
S )Nz\>z@rightarrow@z\<z@leftarrow@z<\1>c                sn   t | jdƒjddƒƒ}tj|ƒr(d| S d }x ˆ jƒ D ]}ˆ | |kr6|}q6W |d krftˆ ƒ}|ˆ |< d| S )Nr   z\,z@comma@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 | ]}|j 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$   | j dƒ}ˆj|ˆ 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&   ZruleÚnumrH   Únewstrr   )rF   rB   rG   r   Ú
expand_sub„   sF    
rU   c       	      C   s”   | }d}t |ƒ}d}i }|jtƒ x\|D ]T}t|||d … ƒ\}}||7 }|j|ƒ |t||d |d … |ƒ7 }|d }q(W |||d … 7 }|S )Nr(   r   r   )r   ÚupdateÚ_special_namesr+   rU   )	ÚallstrrT   ÚwritestrÚstructZoldendr"   rK   Z
cleanedstrZdefsr   r   r   r   À   s    


z9(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+[.]src)['\"]c             C   s¤   t jj| ƒ}t| ƒ�†}g }xz|D ]r}tj|ƒ}|rˆ|jdƒ}t jj|ƒsVt jj||ƒ}t jj	|ƒr|t
d|ƒ |jt|ƒƒ q’|j|ƒ q |j|ƒ q W W d Q R X |S )Nr$   zIncluding file)ÚosÚpathÚdirnameÚopenÚinclude_src_rer   r5   Úisabsr6   ÚisfilerP   ÚextendÚresolve_includesr   )ÚsourceÚdÚfidÚlinesÚliner   Úfnr   r   r   rc   Õ   s    




rc   c             C   s   t | ƒ}tdj|ƒƒS )Nr(   )rc   r   r6   )rd   rg   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j| ƒ\}}|}t|dƒ}|j	ƒ }t
|ƒ}|j|ƒ d S )Nr   rR   Úw)ÚsysÚargvÚ
IndexErrorÚstdinÚstdoutr^   r[   r\   ÚsplitextÚreadr   Úwrite)Úfilerf   ÚoutfileÚbaseÚextZnewnamerX   rY   r   r   r   Úmainö   s    

rw   Ú__main__) Ú__doc__Ú
__future__r   r   r   Ú__all__r[   rk   r)   ÚcompileÚIr   r   r   r   rM   r   rL   r'   r+   r3   r!   r   r?   rU   r   r_   rc   r   rW   rw   Ú__name__r   r   r   r   Ú<module>/   s8   






<
