B <ÓFd€ã@sldZddlmZmZmZddgZddlZddlZddlZe  dej ¡Z e  dej ¡Z e  dej ¡Z d d „Ze  d ¡Ze  d ¡Ze  d ¡Zdd„Ze  d¡Zdd„Zdd„Ze  d¡Zdd„Zdd„Ze  dej ¡Zdd„Zdd„ZedƒZedkrhyejdZWn"e k r$ej!Z"ej#Z$Yn*Xe%ed ƒZ"ej& 'e¡\Z(Z)e(Z*e%e*d!ƒZ$e" +¡Z,ee,ƒZ-e$ .e-¡dS)"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: é)Ú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\bcCs¼g}d}x®t ||¡}|dkr P| ¡}t ||| ¡¡rrx6| d||¡}|dkrTP|}|||d…dkrz<\s*(\w[\w\d]*)\s*=\s*(.*?)\s*>z<\s*((.*?))\s*>cCsTt |¡}i}x@|D]8}|d ¡p*t|ƒ}|d dd¡}t|ƒ}|||<qW|S)Nrr z\,z@comma@)Únamed_reÚfindallÚstripÚ unique_keyÚreplaceÚconv)rZrepsÚnamesZrepÚnameÚreplÚthelistrrrÚfind_repl_patternsZs   r%z\A\\(?P\d+)\ZcCsd| d¡}dd„|Dƒ}x@tt|ƒƒD]0}t ||¡}|r&t| d¡ƒ}||||<q&Wd |¡S)Nú,cSsg|] }| ¡‘qSr)r)Ú.0Úxrrrú gszconv..Úindex)ÚsplitÚrangerÚitem_rerÚintÚgroupÚjoin)rÚbÚlrrÚjrrrr es r cCs@t| ¡ƒ}d}d}x&|s:d|}||kr4|d7}qd}qW|S)z( Obtain a unique key given a dictionary.Fr z__l%sT)ÚlistÚkeys)ZadictZallkeysZdoneÚnZnewkeyrrrros  rz\A\s*(\w[\w\d]*)\s*\Zc st| dd¡}| dd¡}t|ƒ‰t d|¡}‡fdd„}t ||¡}d}d}i‰xÂt |¡D]´}|ˆkr\ˆ || |d¡¡}|dkrtd|ƒ‚||krª|  d ¡sª|||<d d „|  d ¡Dƒ}t |ƒ}|dkrà|}|ˆ|<|}q\||krò|ˆ|<q\t d |d   ˆ|¡||fƒq\Wˆs|S‡‡fdd„} d} x&t|ƒD]‰| t | |¡d7} q:W|  dd¡} |  dd¡} | S)Nz\>z @rightarrow@z\csnt| d¡ dd¡ƒ}t |¡r(d|Sd}x ˆ ¡D]}ˆ||kr6|}q6W|dkrftˆƒ}|ˆ|<d|S)Nr z\,z@comma@z<%s>)r r/rÚtemplate_name_rerr5r)Úmobjr$r"Úkey)ÚlnamesrrÚlistrepl„s  zexpand_sub..listreplzNo replicates found for <%s>Ú_cSsg|]}| dd¡‘qS)z@comma@r&)r)r'rrrrr)žszexpand_sub..r&zHMismatch in number of replacements (base <%s=%s>) for <%s=%s>. Ignoring.cs$| d¡}ˆ |ˆd|g¡ˆS)Nr )r/Úget)r8r")ÚkÚrulesrrÚnamerepl®s zexpand_sub..namereplÚz ú>ú<)rr%rÚsubÚlist_reÚ template_rerr=Ú ValueErrorÚ startswithr+rÚprintr0r,) Zsubstrr!r;ZnumsubsZ base_ruleÚrr$ZruleZnumr@Únewstrr)r>r:r?rÚ expand_sub~sF        rLcCs˜|}d}t|ƒ}d}i}| t¡x`|D]X}||||d…7}| t|||d…ƒ¡|t||d|d…|ƒ7}|d}q(W|||d…7}|S)NrArr )rÚupdateÚ_special_namesr%rL)ÚallstrrKÚwritestrÚstructZoldendr!rDrrrrºs   z9(\n|\A)\s*include\s*['\"](?P[\w\d./\\]+[.]src)['\"]cCs tj |¡}t|ƒ}g}xz|D]r}t |¡}|r†| d¡}tj |¡sTtj ||¡}tj  |¡rzt d|ƒ|  t |ƒ¡q|  |¡q|  |¡qW| ¡|S)Nr"zIncluding file)ÚosÚpathÚdirnameÚopenÚinclude_src_rerr/Úisabsr0ÚisfilerIÚextendÚresolve_includesrÚclose)ÚsourceÚdÚfidÚlinesÚlinerÚfnrrrrZÎs         rZcCst|ƒ}td |¡ƒS)NrA)rZrr0)r\r_rrrrâszÿ <_c=s,d,c,z> <_t=real,double precision,complex,double complex> Ú__main__r rJÚw)/Ú__doc__Z __future__rrrÚ__all__rRÚsysÚreÚcompileÚIr rrrrFrrEr%r-r rr7rLrrVrZrrNÚ__name__ÚargvÚfileÚ IndexErrorÚstdinr^ÚstdoutZoutfilerUrSÚsplitextÚbaseZextZnewnameÚreadrOrPÚwriterrrrÚ.sJ      <