o 6a@%@sdZddgZddlZddlZddlZiZdZddZdd Ze d Z e d Z d d Z e dZ e dZe dZe dZddZe dZddZddZe dejZddZddZddZddZed krqedSdS)!aX takes templated file .xxx.src and produces .xxx file where .xxx is .i or .c or .h, using the following template rules /**begin repeat -- on a line by itself marks the start of a repeated code segment /**end repeat**/ -- on a line by itself marks it's end After the /**begin repeat and before the */, all the named templates are placed these should all have the same number of replacements Repeat blocks can be nested, with each nested block labeled with its depth, i.e. /**begin repeat1 *.... */ /**end repeat1**/ When using nested loops, you can optionally exclude particular combinations of the variables using (inside the comment portion of the inner loop): :exclude: var1=value1, var2=value2, ... This will exclude the pattern where var1 is value1 and var2 is value2 when the result is being generated. In the main body each replace will use one entry from the list of named replacements Note that all #..# forms in a block must have the same number of comma-separated entries. Example: An input file containing /**begin repeat * #a = 1,2,3# * #b = 1,2,3# */ /**begin repeat1 * #c = ted, jim# */ @a@, @b@, @c@ /**end repeat1**/ /**end repeat**/ produces line 1 "template.c.src" /* ********************************************************************* ** This file was autogenerated from a template DO NOT EDIT!!** ** Changes should be made to the original source (.src) file ** ********************************************************************* */ #line 9 1, 1, ted #line 9 1, 1, jim #line 9 2, 2, ted #line 9 2, 2, jim #line 9 3, 3, ted #line 9 3, 3, jim process_str process_fileNaE /* ***************************************************************************** ** This file was autogenerated from a template DO NOT EDIT!!!! ** ** Changes should be made to the original source (.src) file ** ***************************************************************************** */ c Cs|dkr d}d}nd|}d|}d}d}g} |||}|dkr#n?|d|}|d |}|||} |d | } ||d ||d 7}|||d | | d |f||d |d | 7}| }q||S) z The returned line number is from the beginning of the string, starting at zero. Returns an empty list if no loops found. rz/**begin repeatz/**end repeat**/z/**begin repeat%dz/**end repeat%d**/Tz*/ )findcountappendsort) astrlevelloopbegloopendindlinespanliststartstart2fini1fini2r?/usr/lib/python3/dist-packages/numpy/distutils/conv_template.pyparse_structuregs.      rcCs(|d}|d}d|gt|S)Nr,)groupjoinint)objtorepnumreprrr paren_repls  r!z\(([^)]*)\)\*(\d+)z([^*]+)\*(\d+)cCs0tt|}ddd|dD}|dS)NrcSsg|] }tt|qSr)plainrepsubr!strip.0xrrr sz parse_values..)parenrepr#r!rsplit)r rrr parse_valuess   r+z\n\s*\*?z#\s*(\w*)\s*=([^#]*)#z (\w*)=(\w*)z :exclude:cstd|}g}t|}d}|D].}|d}t|d}t|}|dur(|}n||kr8d}t|||||f|||fqg} t |D]"} | } | d| d} || d| } t | }| t |qGg}|durttdt|D]fdd |D}||qx|S) aFind all named replacements in the header Returns a list of dictionaries, one for each loop iteration, where each key is a name to be substituted and the corresponding value is the replacement string. Also return a list of exclusions. The exclusions are dictionaries of key value pairs. There can be more than one exclusion. [{'var1':'value1', 'var2', 'value2'[,...]}, ...] Nrrz.Mismatch in number of values, %d != %d %s = %srzNo substitution variables foundcsi|] \}}||qSrr)r&namevalsirr sz%parse_loop_header..)stripastr#named_refindallr+len ValueErrorr exclude_refinditerspanrexclude_vars_redictrange)loopheadnamesrepsnsubrepr-r.sizemsgexcludesrr9endlinesubstrex_namesdlisttmprr/rparse_loop_headers8      rJz@(\w+)@c sJd}fdd}|g}t||}|rd}|d} |D]d} ||| d} || d| d} || d| d} | d}| d}|t|| zt| }Wntyj}z d ||f}t|d}~ww|D]}|t| || |}||qmq||d}|t||n |t|||d d |S) Nz #line %d cs>|d}z|}W|Styd|f}t|dw)Nrz"line %d: no definition of key "%s")rKeyErrorr6)matchr-valrCenvrrrreplaces    zparse_string..replacerrrz line %d: %srr,) rr replace_rer#rJr6update parse_stringextendr)r rOr rlinenorPcodestructoldendnewlevelr#prefheadtextnewlineenvlisterCnewenvnewcodesuffrrNrrUs>         rUcCs$tg}|t|tddd|S)Nrrr,)headerrVrU global_namesr)r rXrrrrs z7(\n|\A)#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_rerLrisabsrisfilerVresolve_includesr )sourcedfidlinesrmfnrrrrns$         rnc Csft|}tj|dd}z td|}Wnty,}z td||fdd}~wwd||fS)N\z\\r,zIn "%s" loop at %sz#line 1 "%s" %s)rnrgrhnormcaserPrrr6)rorr sourcefilerXrarrrrs csNt|}d}d|s%dfdd|D}||vr!d7nd}|r |S)NFrr,csg|]}|dqS)Nrr%nrrr(,szunique_key..T)listkeysr)adictallkeysdonenewkeyrrxr unique_key#s  rc Csztjd}Wntytj}tj}Ynwt|d}tj|\}}|}t|d}| }zt |}Wnt yJ}z t d||fdd}~ww| |dS)NrrwzIn %s loop at %s) sysargv IndexErrorstdinstdoutrjrgrhsplitextreadrr6write) filerqoutfilebaseextnewnameallstrwritestrrarrrmain4s$     r__main__)__doc____all__rgrrerfrerr!compiler)r"r+r2r3r:r7rJrSrUrIrkrnrrr__name__rrrrs>P       4+