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

Side by Side Diff: components/variations/service/generate_ui_string_overrider.py

Issue 1969243002: Fix regex to parse Variations UI override strings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed rkaplow and alexei comments Created 4 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
« no previous file with comments | « no previous file | no next file » | 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/python 1 #!/usr/bin/python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 2 # Copyright 2014 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 argparse 6 import argparse
7 import collections 7 import collections
8 import hashlib 8 import hashlib
9 import operator 9 import operator
10 import os 10 import os
11 import re 11 import re
12 import sys 12 import sys
13 13
14 SCRIPT_NAME = "generate_ui_string_overrider.py" 14 SCRIPT_NAME = "generate_ui_string_overrider.py"
15
16
17 # Regular expressions for parsing the #define macro format. Separate regular
18 # expressions are used for parsing lines with pragma (for builds with
19 # enable_resource_whitelist_generation flag) in windows and non-windows, and for
20 # lines without pragma, For example,
21 # Without generate whitelist flag:
22 # #define IDS_FOO_MESSAGE 1234
23 # With generate whitelist flag in non-windows:
24 # #define IDS_FOO_MESSAGE _Pragma("whitelisted_resource_1234") 1234
25 # With generate whitelist flag in windows:
26 # #define IDS_FOO_MESSAGE __pragma(message("whitelisted_resource_1234")) 1234
15 RESOURCE_EXTRACT_REGEX = re.compile('^#define (\S*) (\d*)$', re.MULTILINE) 27 RESOURCE_EXTRACT_REGEX = re.compile('^#define (\S*) (\d*)$', re.MULTILINE)
28 RESOURCE_EXTRACT_REGEX_PRAGMA = re.compile(
29 '^#define (\S*) _Pragma\("whitelisted_resource_\d*"\) (\d*)$',
30 re.MULTILINE)
31 RESOURCE_EXTRACT_REGEX_PRAGMA_WINDOWS = re.compile(
32 '^#define (\S*) __pragma\(message\("whitelisted_resource_\d*"\)\) (\d*)$',
33 re.MULTILINE)
16 34
17 class Error(Exception): 35 class Error(Exception):
18 """Base error class for all exceptions in generated_resources_map.""" 36 """Base error class for all exceptions in generated_resources_map."""
19 37
20 38
21 class HashCollisionError(Error): 39 class HashCollisionError(Error):
22 """Multiple resource names hash to the same value.""" 40 """Multiple resource names hash to the same value."""
23 41
24 42
25 Resource = collections.namedtuple("Resource", ['hash', 'name', 'index']) 43 Resource = collections.namedtuple("Resource", ['hash', 'name', 'index'])
(...skipping 20 matching lines...) Expand all
46 an iterator over all matching (NAME, INDEX) pairs. 64 an iterator over all matching (NAME, INDEX) pairs.
47 65
48 Args: 66 Args:
49 string_to_scan: The input string to scan. 67 string_to_scan: The input string to scan.
50 68
51 Yields: 69 Yields:
52 A tuple of name and index. 70 A tuple of name and index.
53 """ 71 """
54 for match in RESOURCE_EXTRACT_REGEX.finditer(string_to_scan): 72 for match in RESOURCE_EXTRACT_REGEX.finditer(string_to_scan):
55 yield match.group(1, 2) 73 yield match.group(1, 2)
74 for match in RESOURCE_EXTRACT_REGEX_PRAGMA.finditer(string_to_scan):
75 yield match.group(1, 2)
76 for match in RESOURCE_EXTRACT_REGEX_PRAGMA_WINDOWS.finditer(string_to_scan):
77 yield match.group(1, 2)
56 78
57 79
58 def _GetResourceListFromString(resources_content): 80 def _GetResourceListFromString(resources_content):
59 """Produces a list of |Resource| objects from a string. 81 """Produces a list of |Resource| objects from a string.
60 82
61 The input string contains lines of the form "#define NAME INDEX". The returned 83 The input string contains lines of the form "#define NAME INDEX". The returned
62 list is sorted primarily by hash, then name, and then index. 84 list is sorted primarily by hash, then name, and then index.
63 85
64 Args: 86 Args:
65 resources_content: The input string to process, contains lines of the form 87 resources_content: The input string to process, contains lines of the form
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 with open(os.path.join( 310 with open(os.path.join(
289 arguments.output_dir, arguments.source_filename), "w") as generated_file: 311 arguments.output_dir, arguments.source_filename), "w") as generated_file:
290 generated_file.write(source_file_content) 312 generated_file.write(source_file_content)
291 with open(os.path.join( 313 with open(os.path.join(
292 arguments.output_dir, arguments.header_filename), "w") as generated_file: 314 arguments.output_dir, arguments.header_filename), "w") as generated_file:
293 generated_file.write(header_file_content) 315 generated_file.write(header_file_content)
294 316
295 317
296 if __name__ == '__main__': 318 if __name__ == '__main__':
297 sys.exit(main()) 319 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698