o 6a@sdZddgZddlZddlZddlZedejZedejZedejZ dd Z ed Z ed Z ed Z d dZddZedZddZddZedZddZddZedejZddZddZedZdd Zed!kr|edSdS)"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: where anywhere inside a block '

' 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: , a short form of the named, useful when no

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 '\' then it will be replaced by -th expression. Note that all '<..>' forms in a block must have the same number of comma-separated entries. Predefined named template rules: process_str process_fileNz2(\n|\A)(( (\$|\*))|)\s*(subroutine|function)\bz+\n\s*end\s*(subroutine|function)\b.*(\n|\Z)z\n (\$|\*)\s*function\bcCsg}d} t||}|dur |S|}t|||r: |d||}|dkr,n|}|||ddkr9nq |d7}t||}|rN|dpQt|}}| ||fq) 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. rTN z $) routine_start_researchstartfunction_start_rematchendrfindroutine_end_relenappend)astrspanlistindmr ir r?/usr/lib/python3/dist-packages/numpy/distutils/from_template.pyparse_structure:s, rz<\s*(\w[\w\d]*)\s*>z<\s*(\w[\w\d]*)\s*=\s*(.*?)\s*>z<\s*((.*?))\s*>cCsPt|}i}|D]}|dpt|}|ddd}t|}|||<q |S)Nrr\,@comma@)named_refindallstrip unique_keyreplaceconv)rrepsnamesrepnamereplthelistrrrfind_repl_patternsYs  r(cCs"t|}ttd|d}||fS)Nr)r(resubnr)rr#rrrfind_and_remove_repl_patternscsr,z\A\\(?P\d+)\ZcCs`|d}dd|D}tt|D]}t||}|r*t|d}||||<qd|S)N,cSsg|]}|qSr)r).0xrrr kszconv..index)splitrangeritem_rer intgroupjoin)rblrrjrrrr!is   r!cCs>t|}d}d}|sd|}||vr|d7}nd}|r |S)z( Obtain a unique key given a dictionary.Frz__l%sT)listkeys)adictallkeysdonennewkeyrrrrss  rz\A\s*(\w[\w\d]*)\s*\Zc sh|dd}|dd}t|td|}fdd}t||}d}d}it|D]Z}|vr|||d}|durGtd|||vrT| d sT|||<d d | d D}t |}|duro|}||<|}q-||krx||<q-t d |d |||fq-s|Sfdd} d} t|D] | t| |d7} q| dd} | dd} | S)Nz\>z @rightarrow@z\csjt|ddd}t|rd|Sd}D] }||kr$|}q|dur1t}||<d|S)Nrrrz<%s>)r!r6r template_name_rer r<r)mobjr'r%key)lnamesrrlistrepls   zexpand_sub..listreplzNo replicates found for <%s>_cSsg|]}|ddqS)rr-)r )r.rrrrr0szexpand_sub..r-zHMismatch in number of replacements (base <%s=%s>) for <%s=%s>. Ignoring.cs$|d}|d|gS)Nr)r6get)rCr%)krulesrrnamerepls zexpand_sub..namereplr)z ><)r r(rsublist_re template_rerrH ValueError startswithr2rprintr7r3) substrr#rFnumsubs base_rulerr'rulenumrKnewstrr)rIrErJr expand_subsJ          r[c Cs|}d}t|}d}i}|t|D]*}t|||d\}}||7}|||t||d|d|7}|d}q|||d7}|S)Nr)rr)rupdate_special_namesr,r[) allstrrZwritestrstructoldendr#rN cleanedstrdefsrrrrs   z8(\n|\A)\s*include\s*['\"](?P[\w\d./\\]+\.src)['\"]cCstj|}t|B}g}|D]4}t|}|r>|d}tj|s*tj||}tj |r8| t |q| |q| |qWd|S1sOwY|S)Nr%) ospathdirnameopeninclude_src_rer r6isabsr7isfileextendresolve_includesr)sourcedfidlineslinerfnrrrrls$         rlcCst|}td|S)Nr))rlrr7)rmrprrrrsz <_c=s,d,c,z> <_t=real,double precision,complex,double complex> cCstztjd}Wntytj}tj}Ynwt|d}tj|\}}|}t|d}| }t |}| |dS)NrrWw) sysargv IndexErrorstdinstdoutrgrdresplitextreadrwrite)filerooutfilebaseextnewnamer^r_rrrmains    r__main__)__doc____all__rdrtr*compileIrrr rrPrrOr(r,r4r!rrBr[rrhrlrr]r__name__rrrrs8.     <