o
    "ì¤gé  ã                   @   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 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	 |S | ¡ }t | || ¡ ¡r:	 |  d||¡}|dkr,n|}| ||d … dkr9nq |d7 }t | | ¡ ¡}|rN| ¡ d pQt| ƒ }}| 	||f¡ q)	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   TNÚ
éÿÿÿÿé   z
     $é   )
Úroutine_start_reÚsearchÚstartÚfunction_start_reÚmatchÚendÚrfindÚroutine_end_reÚlenÚappend)ÚastrÚspanlistÚindÚmr
   Úir   © r   ú_/var/www/html/ecg_monitoring/venv/lib/python3.10/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 ]}|d  ¡ pt|ƒ}|d  dd¡}t|ƒ}|||< q	|S )Nr   r   ú\,ú@comma@)Únamed_reÚfindallÚstripÚ
unique_keyÚreplaceÚconv)r   Ú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 ]}t || ¡}|r*t| d¡ƒ}|| ||< qd |¡S )Nú,c                 S   s   g | ]}|  ¡ ‘qS r   )r   )Ú.0Úxr   r   r   Ú
<listcomp>k   s    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}|sd| }||v r|d7 }nd}|r|S )z( Obtain a unique key given a dictionary.Fr   z__l%sT)ÚlistÚkeys)ÚadictÚallkeysÚdoneÚnÚnewkeyr   r   r   r   s   s   
ûr   z\A\s*(\w[\w\d]*)\s*\Zc                    sh  |   dd¡} |   dd¡} t| ƒ‰t d| ¡} ‡fdd„}t || ¡} d }d }i ‰t | ¡D ]Z}|ˆvr‡ˆ || |d ¡¡}|d u rGtd| ƒ‚||vrT| 	d	¡sT|||< d
d„ | 
d¡D ƒ}t|ƒ}|d u ro|}|ˆ|< |}q-||krx|ˆ|< q-td|d ˆ| ¡||f ƒ q-ˆsŒ| S ‡ ‡fdd„}	d}
t|ƒD ]‰ |
t |	| ¡d 7 }
q™|
  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r$|}q|d u r1tˆ ƒ}|ˆ |< d| S )Nr   r   r   z<%s>)r!   r6   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   r0   ¢   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   )r6   Úget)rC   r%   )ÚkÚrulesr   r   Únamerepl²   s   
zexpand_sub.<locals>.namereplr)   z

ú>ú<)r    r(   r   ÚsubÚlist_reÚtemplate_rer   rH   Ú
ValueErrorÚ
startswithr2   r   Úprintr7   r3   )Úsubstrr#   rF   ÚnumsubsÚ	base_ruleÚrr'   ÚruleÚnumrK   Únewstrr   )rI   rE   rJ   r   Ú
expand_sub‚   sJ   
þ€r[   c           	      C   s   | }d}t |ƒ}d}i }| t¡ |D ]*}t|||d … ƒ\}}||7 }| |¡ |t||d |d … |ƒ7 }|d }q|||d … 7 }|S )Nr)   r   r   )r   ÚupdateÚ_special_namesr,   r[   )	ÚallstrrZ   ÚwritestrÚstructÚoldendr#   rN   Ú
cleanedstrÚdefsr   r   r   r   ¾   s   


z8(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+\.src)['\"]c                 C   s¬   t j | ¡}t| ƒB}g }|D ]4}t |¡}|r>| d¡}t j |¡s*t j ||¡}t j 	|¡r8| 
t|ƒ¡ q| |¡ q| |¡ qW d   ƒ |S 1 sOw   Y  |S )Nr%   )ÚosÚpathÚdirnameÚopenÚinclude_src_rer   r6   Úisabsr7   ÚisfileÚextendÚresolve_includesr   )ÚsourceÚdÚfidÚlinesÚliner   Úfnr   r   r   rl   Ó   s$   


õ
þòrl   c                 C   s   t | ƒ}td |¡ƒS )Nr)   )rl   r   r7   )rm   rp   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w t| dƒ}tj | ¡\}}|}t|dƒ}| 	¡ }t
|ƒ}| |¡ d S )Nr   rW   Úw)ÚsysÚargvÚ
IndexErrorÚstdinÚstdoutrg   rd   re   ÚsplitextÚreadr   Úwrite)Úfilero   ÚoutfileÚbaseÚextÚnewnamer^   r_   r   r   r   Úmainó   s   
þ

r   Ú__main__)Ú__doc__Ú__all__rd   rt   r*   ÚcompileÚIr   r   r   r   rP   r   rO   r(   r,   r4   r!   r   rB   r[   r   rh   rl   r   r]   r   Ú__name__r   r   r   r   Ú<module>   s8   .






<

ÿ