ó
ØÆ÷Xc           @` 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	 „  Z e j	 d
 ƒ Z e j	 d ƒ Z e j	 d ƒ Z d „  Z e j	 d ƒ Z d „  Z d „  Z e j	 d ƒ Z d „  Z d „  Z e j	 d e j
 ƒ Z d „  Z d „  Z e d ƒ Z e d k rìy e j d Z Wn# e  k
 r‡e j! Z" e j# Z$ n= Xe% e d ƒ Z" e j& j' e ƒ \ Z( Z) e( Z* e% e* d ƒ Z$ e" j+ ƒ  Z, e e, ƒ Z- e$ j. e- ƒ n  d S(   sÆ  

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>

i    (   t   divisiont   absolute_importt   print_functiont   process_strt   process_fileNs2   (\n|\A)((     (\$|\*))|)\s*(subroutine|function)\bs+   \n\s*end\s*(subroutine|function)\b.*(\n|\Z)s   \n     (\$|\*)\s*function\bc         C` s  g  } d } xt  rt j |  | ƒ } | d k r7 Pn  | j ƒ  } t j |  | | j ƒ  ƒ r´ xS t  r° |  j d | | ƒ } | d k rŒ Pn  | } |  | | d !d k ra Pqa qa Wn  | d 7} t	 j |  | j ƒ  ƒ } | rì | j ƒ  d põ t
 |  ƒ } } | j | | f ƒ q W| S(   s‘    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.
    i    s   
iÿÿÿÿi   s   
     $i   N(   t   Truet   routine_start_ret   searcht   Nonet   startt   function_start_ret   matcht   endt   rfindt   routine_end_ret   lent   append(   t   astrt   spanlistt   indt   mR	   t   iR   (    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt   parse_structure<   s(    		
&s   <\s*(\w[\w\d]*)\s*>s   <\s*(\w[\w\d]*)\s*=\s*(.*?)\s*>s   <\s*((.*?))\s*>c         C` sr   t  j |  ƒ } i  } xV | D]N } | d j ƒ  p; t | ƒ } | d j d d ƒ } t | ƒ } | | | <q W| S(   Ni    i   s   \,s   @comma@(   t   named_ret   findallt   stript
   unique_keyt   replacet   conv(   R   t   repst   namest   rept   namet   replt   thelist(    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt   find_repl_patterns[   s    s   \A\\(?P<index>\d+)\Zc         C` s—   |  j  d ƒ } g  | D] } | j ƒ  ^ q } xY t t | ƒ ƒ D]E } t j | | ƒ } | rA t | j d ƒ ƒ } | | | | <qA qA Wd j | ƒ S(   Nt   ,t   index(	   t   splitR   t   rangeR   t   item_reR   t   intt   groupt   join(   R   t   bt   xt   lR   R   t   j(    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyR   f   s    c         C` sX   t  |  j ƒ  ƒ } t } d } x3 | sS d | } | | k rJ | d 7} q! t } q! W| S(   s(    Obtain a unique key given a dictionary.i   s   __l%s(   t   listt   keyst   FalseR   (   t   adictt   allkeyst   donet   nt   newkey(    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyR   p   s    	

s   \A\s*(\w[\w\d]*)\s*\Zc         ` s  |  j  d d ƒ }  |  j  d d ƒ }  t |  ƒ ‰ t j d |  ƒ }  ‡ f d †  } t j | |  ƒ }  d  } d  } i  ‰ x+t j |  ƒ D]} | ˆ k r… ˆ j | | j | d  ƒ ƒ } | d  k rÔ t	 d | ƒ ‚ n  | | k rý | j
 d ƒ rý | | | <n  g  | j d	 ƒ D] } | j  d
 d	 ƒ ^ q} t | ƒ }	 | d  k r\|	 } | ˆ | <| } qŸ|	 | k ru| ˆ | <qŸt d | d	 j ˆ | ƒ | | f ƒ q… q… Wˆ s­|  S‡  ‡ f d †  }
 d } x. t | ƒ D]  ‰  | t j |
 |  ƒ d 7} qÒW| j  d d ƒ } | j  d d ƒ } | S(   Ns   \>s   @rightarrow@s   \<s   @leftarrow@s   <\1>c         ` s›   t  |  j d ƒ j d d ƒ ƒ } t j | ƒ r8 d | Sd  } x- ˆ  j ƒ  D] } ˆ  | | k rK | } qK qK W| d  k r“ t ˆ  ƒ } | ˆ  | <n  d | S(   Ni   s   \,s   @comma@s   <%s>(   R   R*   R   t   template_name_reR   R   R1   R   (   t   mobjR"   R    t   key(   t   lnames(    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt   listrepl…   s    !s   No replicates found for <%s>t   _R$   s   @comma@sH   Mismatch in number of replacements (base <%s=%s>) for <%s=%s>. Ignoring.c         ` s.   |  j  d ƒ } ˆ j | ˆ  d | g ƒ ˆ  S(   Ni   (   R*   t   get(   R9   R    (   t   kt   rules(    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt   namerepl¯   s    t    s   

t   >t   <(   R   R#   R   t   subt   list_reR   t   template_reR   R>   t
   ValueErrort
   startswithR&   R   t   printR+   R'   (   t   substrR   R<   t   numsubst	   base_rulet   rR"   R   t   rulet   numRA   t   newstr(    (   R?   R;   R@   s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt
   expand_sub   sF    .
	(c         C` s³   |  } d } t  | ƒ } d } i  } | j t ƒ xm | D]e } | | | | d !7} | j t | | | d !ƒ ƒ | t | | d | d !| ƒ 7} | d } q8 W| | | 7} | S(   NRB   i    i   (   R   t   updatet   _special_namesR#   RR   (   t   allstrRQ   t   writestrt   structt   oldendR   RE   (    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyR   »   s    "s9   (\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+[.]src)['\"]c         C` sã   t  j j |  ƒ } t |  ƒ } g  } x® | D]¦ } t j | ƒ } | rÄ | j d ƒ } t  j j | ƒ s t  j j | | ƒ } n  t  j j	 | ƒ r´ t
 d | ƒ | j t | ƒ ƒ qÑ | j | ƒ q+ | j | ƒ q+ W| j ƒ  | S(   NR    s   Including file(   t   ost   patht   dirnamet   opent   include_src_reR   R*   t   isabsR+   t   isfileRJ   t   extendt   resolve_includesR   t   close(   t   sourcet   dt   fidt   linest   lineR   t   fn(    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyRa   Ï   s     
c         C` s   t  |  ƒ } t d j | ƒ ƒ S(   NRB   (   Ra   R   R+   (   Rc   Rf   (    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyR   ã   s    sÿ   
<_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>
t   __main__i   RN   t   w(/   t   __doc__t
   __future__R    R   R   t   __all__RY   t   syst   ret   compilet   IR   R   R
   R   RG   R   RF   R#   R(   R   R   R8   RR   R   R]   Ra   R   RT   t   __name__t   argvt   filet
   IndexErrort   stdinRe   t   stdoutt   outfileR\   RZ   t   splitextt   baset   extt   newnamet   readRU   RV   t   write(    (    (    s<   /tmp/pip-build-X4mzal/numpy/numpy/distutils/from_template.pyt   <module>/   sJ   		
	
		<					