fix engine to template only if matches
Showing
5 changed files
with
26 additions
and
16 deletions
| ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | 
| 7 | 7 | ||
| 8 | some environment variables: | 8 | some environment variables: | 
| 9 | 9 | ||
| 10 | Hostname: [[{HOSTNAME}]] | 10 | Term: [[{TERM}]] | 
| 11 | Path Variable: [[{PATH}]] | 11 | Path Variable: [[{PATH}]] | 
| 12 | Current Working Directory: [[{PWD}]] | 12 | Current Working Directory: [[{PWD}]] | 
| 13 | 13 | ... | ... | 
| ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | 
| 7 | 7 | ||
| 8 | some environment variables: | 8 | some environment variables: | 
| 9 | 9 | ||
| 10 | Hostname: [[{HOSTNAME}]] | 10 | Term: [[{TERM}]] | 
| 11 | Path Variable: [[{PATH}]] | 11 | Path Variable: [[{PATH}]] | 
| 12 | Current Working Directory: [[{PWD}]] | 12 | Current Working Directory: [[{PWD}]] | 
| 13 | 13 | ... | ... | 
| ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | ... | @@ -7,7 +7,7 @@ otra_variable= [[{VAR}]] | 
| 7 | 7 | ||
| 8 | some environment variables: | 8 | some environment variables: | 
| 9 | 9 | ||
| 10 | Hostname: [[{HOSTNAME}]] | 10 | Term: [[{TERM}]] | 
| 11 | Path Variable: [[{PATH}]] | 11 | Path Variable: [[{PATH}]] | 
| 12 | Current Working Directory: [[{PWD}]] | 12 | Current Working Directory: [[{PWD}]] | 
| 13 | 13 | ... | ... | 
| ... | @@ -4,8 +4,12 @@ var = [[{variable}]] | ... | @@ -4,8 +4,12 @@ var = [[{variable}]] | 
| 4 | 4 | ||
| 5 | otra_variable= [[{VAR}]] | 5 | otra_variable= [[{VAR}]] | 
| 6 | 6 | ||
| 7 | |||
| 7 | some environment variables: | 8 | some environment variables: | 
| 8 | Hostname: [[{HOSTNAME}]] | 9 | |
| 10 | Term: [[{TERM}]] | ||
| 9 | Path Variable: [[{PATH}]] | 11 | Path Variable: [[{PATH}]] | 
| 10 | Current Working Directory: [[{PWD}]] | 12 | Current Working Directory: [[{PWD}]] | 
| 11 | 13 | ||
| 14 | [[{}]] | ||
| 15 | ... | ... | 
| ... | @@ -21,7 +21,7 @@ class MyTemplate(string.Template): | ... | @@ -21,7 +21,7 @@ class MyTemplate(string.Template): | 
| 21 | (?P<escaped>\[\[\{)| | 21 | (?P<escaped>\[\[\{)| | 
| 22 | (?P<named>[_A-Z][_A-Z0-9]*)\}\]\]| | 22 | (?P<named>[_A-Z][_A-Z0-9]*)\}\]\]| | 
| 23 | (?P<braced>[_A-Z][_A-Z0-9]*)\}\]\]| | 23 | (?P<braced>[_A-Z][_A-Z0-9]*)\}\]\]| | 
| 24 | (?P<invalid>) | 24 | (?P<invalid>^$) | 
| 25 | ) | 25 | ) | 
| 26 | ''' | 26 | ''' | 
| 27 | 27 | ||
| ... | @@ -59,18 +59,23 @@ def engine(ifile,ofile,vars): | ... | @@ -59,18 +59,23 @@ def engine(ifile,ofile,vars): | 
| 59 | template= MyTemplate(template_content) | 59 | template= MyTemplate(template_content) | 
| 60 | 60 | ||
| 61 | #print 'MATCHES:', t.pattern.findall(t.template) | 61 | #print 'MATCHES:', t.pattern.findall(t.template) | 
| 62 | outputText = template.safe_substitute(vars) | ||
| 63 | print 'MATCHES:', template.pattern.findall(template.template) | ||
| 64 | |||
| 65 | 62 | ||
| 66 | if printit: | 63 | matches = template.pattern.findall(template.template) | 
| 67 | print "AFTER TEMPLATE:" | 64 | print vars | 
| 68 | #print outputText | 65 | |
| 69 | print outputText.encode('ascii', 'ignore') | 66 | if len(matches): | 
| 70 | else: | 67 | print '[ MATCHES ] - ', matches | 
| 71 | f = open(ofile,'w') | 68 | outputText = template.safe_substitute(vars) | 
| 72 | f.write(outputText.encode('ascii', 'ignore')) | 69 | print "[ TEMPLATING ] - %s "%ifile | 
| 73 | f.close() | 70 | |
| 71 | if printit: | ||
| 72 | print "AFTER TEMPLATE:" | ||
| 73 | #print outputText | ||
| 74 | print outputText.encode('ascii', 'ignore') | ||
| 75 | else: | ||
| 76 | f = open(ofile,'w') | ||
| 77 | f.write(outputText.encode('ascii', 'ignore')) | ||
| 78 | f.close() | ||
| 74 | 79 | ||
| 75 | 80 | ||
| 76 | 81 | ||
| ... | @@ -144,6 +149,7 @@ def main(argv): | ... | @@ -144,6 +149,7 @@ def main(argv): | 
| 144 | for filename in files: | 149 | for filename in files: | 
| 145 | fullpath = os.path.join(path, filename) | 150 | fullpath = os.path.join(path, filename) | 
| 146 | if os.path.isfile(fullpath): | 151 | if os.path.isfile(fullpath): | 
| 152 | |||
| 147 | engine(fullpath, fullpath, vars) | 153 | engine(fullpath, fullpath, vars) | 
| 148 | count+=1 | 154 | count+=1 | 
| 149 | else: | 155 | else: | ... | ... | 
- 
Please register or sign in to post a comment
