Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(602)

Side by Side Diff: native_client_sdk/src/build_tools/easy_template.py

Issue 250773010: [NaCl SDK] easy_template: fix bug when using % in an expression template. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix % after {{}} bug Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | native_client_sdk/src/build_tools/tests/easy_template_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import copy 6 import copy
7 import cStringIO 7 import cStringIO
8 import optparse 8 import optparse
9 import os 9 import os
10 import re 10 import re
(...skipping 14 matching lines...) Expand all
25 indent_string = indent_re.match(statement).group() 25 indent_string = indent_re.match(statement).group()
26 if statement.rstrip()[-1:] == ':': 26 if statement.rstrip()[-1:] == ':':
27 indent_string += ' ' 27 indent_string += ' '
28 output.write(statement + '\n') 28 output.write(statement + '\n')
29 else: 29 else:
30 line_ending = '' 30 line_ending = ''
31 while line and line[-1] in '\\"\n\r': 31 while line and line[-1] in '\\"\n\r':
32 line_ending = line[-1] + line_ending 32 line_ending = line[-1] + line_ending
33 line = line[:-1] 33 line = line[:-1]
34 34
35 m = expr_re.search(line) 35 ms = list(expr_re.finditer(line))
36 if m: 36 if ms:
37 line = line.replace('%', '%%') 37 # Only replace % with %% outside of the expr matches.
38 new_line = ''
39 start = 0
40 for m in ms:
41 new_line += line[start:m.start()].replace('%', '%%')
42 new_line += line[m.start():m.end()]
43 start = m.end()
44 new_line += line[start:].replace('%', '%%')
45 line = new_line
46
38 subst_line = r'r"""%s""" %% (%s,)' % ( 47 subst_line = r'r"""%s""" %% (%s,)' % (
39 re.sub(expr_re, '%s', line), 48 re.sub(expr_re, '%s', line),
40 ', '.join(re.findall(expr_re, line))) 49 ', '.join(re.findall(expr_re, line)))
41 else: 50 else:
42 subst_line = r'r"""%s"""' % line 51 subst_line = r'r"""%s"""' % line
43 52
44 out_string = r'%s__outfile__.write(%s + %s)' % ( 53 out_string = r'%s__outfile__.write(%s + %s)' % (
45 indent_string, 54 indent_string,
46 subst_line, 55 subst_line,
47 repr(line_ending)) 56 repr(line_ending))
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 _, args = parser.parse_args(args) 102 _, args = parser.parse_args(args)
94 if not args: 103 if not args:
95 return 104 return
96 105
97 with open(args[0]) as f: 106 with open(args[0]) as f:
98 print TemplateToPython( 107 print TemplateToPython(
99 f.read(), re.compile(STATEMENT_RE), re.compile(EXPR_RE)) 108 f.read(), re.compile(STATEMENT_RE), re.compile(EXPR_RE))
100 109
101 if __name__ == '__main__': 110 if __name__ == '__main__':
102 sys.exit(main(sys.argv[1:])) 111 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « no previous file | native_client_sdk/src/build_tools/tests/easy_template_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698