ó
ÐEe]c           @` sq  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 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 d „  Z e d k rm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<   lib/python2.7/site-packages/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<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt   find_repl_patterns[   s    c         C` s/   t  |  ƒ } t j t d |  ƒ d }  |  | f S(   Nt    i    (   R#   t   ret   subnR   (   R   R   (    (    s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt   find_and_remove_repl_patternse   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<   lib/python2.7/site-packages/numpy/distutils/from_template.pyR   k   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<   lib/python2.7/site-packages/numpy/distutils/from_template.pyR   u   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   R5   R   (   t   mobjR"   R    t   key(   t   lnames(    s<   lib/python2.7/site-packages/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(   R=   R    (   t   kt   rules(    s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt   namerepl´   s    R$   s   

t   >t   <(   R   R#   R   t   subt   list_reR   t   template_reR   RB   t
   ValueErrort
   startswithR*   R   t   printR/   R+   (   t   substrR   R@   t   numsubst	   base_rulet   rR"   R   t   rulet   numRE   t   newstr(    (   RC   R?   RD   s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt
   expand_sub„   sF    .
	(c   	      C` s´   |  } d } t  | ƒ } d } i  } | j t ƒ xn | D]f } t | | | d !ƒ \ } } | | 7} | j | ƒ | t | | d | d !| ƒ 7} | d } q8 W| | | 7} | S(   NR$   i    i   (   R   t   updatet   _special_namesR'   RU   (	   t   allstrRT   t   writestrt   structt   oldendR   RH   t
   cleanedstrt   defs(    (    s<   lib/python2.7/site-packages/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   isfileRM   t   extendt   resolve_includesR   t   close(   t   sourcet   dt   fidt   linest   lineR   t   fn(    (    s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyRf   Õ   s     
c         C` s   t  |  ƒ } t d j | ƒ ƒ S(   NR$   (   Rf   R   R/   (   Rh   Rk   (    (    s<   lib/python2.7/site-packages/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>
c          C` sœ   y t  j d }  Wn# t k
 r6 t  j } t  j } n= Xt |  d ƒ } t j j |  ƒ \ } } | } t | d ƒ } | j	 ƒ  } t
 | ƒ } | j | ƒ d  S(   Ni   RQ   t   w(   t   syst   argvt
   IndexErrort   stdint   stdoutRa   R^   R_   t   splitextt   readR   t   write(   t   fileRj   t   outfilet   baset   extt   newnameRX   RY   (    (    s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt   main÷   s    	t   __main__(    t   __doc__t
   __future__R    R   R   t   __all__R^   Ro   R%   t   compilet   IR   R   R
   R   RJ   R   RI   R#   R'   R,   R   R   R<   RU   R   Rb   Rf   R   RW   R|   t   __name__(    (    (    s<   lib/python2.7/site-packages/numpy/distutils/from_template.pyt   <module>/   s8   		
		
		<					