Index: components/variations/service/generate_ui_string_overrider.py |
diff --git a/chrome/browser/metrics/variations/generate_resources_map.py b/components/variations/service/generate_ui_string_overrider.py |
similarity index 52% |
rename from chrome/browser/metrics/variations/generate_resources_map.py |
rename to components/variations/service/generate_ui_string_overrider.py |
index 6cf535b857883f1fe51edcaf1b393e2374eaa2cb..9d21d9579d6432e2c7f91e16c65d5fea103813ae 100755 |
--- a/chrome/browser/metrics/variations/generate_resources_map.py |
+++ b/components/variations/service/generate_ui_string_overrider.py |
@@ -3,6 +3,7 @@ |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
+import argparse |
import collections |
import hashlib |
import operator |
@@ -10,7 +11,7 @@ import os |
import re |
import sys |
- |
+SCRIPT_NAME = "generate_ui_string_overrider.py" |
RESOURCE_EXTRACT_REGEX = re.compile('^#define (\S*) (\d*)$', re.MULTILINE) |
class Error(Exception): |
@@ -123,15 +124,20 @@ def _GenDataArray( |
'content': '\n'.join(lines)} |
-def _GenerateFileContent(resources_content): |
- """Generates the .cc content from the given generated_resources.h content. |
+def _GenerateSourceFileContent(resources_content, namespace, header_filename): |
+ """Generates the .cc content from the given generated grit headers content. |
Args: |
resources_content: The input string to process, contains lines of the form |
"#define NAME INDEX". |
+ namespace: The namespace in which the generated code should be scoped. If |
+ not defined, then the code will be in the global namespace. |
+ |
+ header_filename: Path to the corresponding .h. |
+ |
Returns: |
- .cc file content defining the kResourceHashes and kResourceIndices arrays. |
+ .cc file content implementing the CreateUIStringOverrider() factory. |
""" |
hashed_tuples = _GetResourceListFromString(resources_content) |
@@ -150,34 +156,119 @@ def _GenerateFileContent(resources_content): |
hashed_tuples, " %s, // %s", 'kResourceIndices', 'int', |
operator.attrgetter('index')) |
+ namespace_prefix = "" |
+ namespace_suffix = "" |
+ if namespace: |
+ namespace_prefix = "namespace %s {\n\n" % namespace |
+ namespace_suffix = "\n} // namespace %s\n" % namespace |
+ |
return ( |
- "// This file was generated by generate_resources_map.py. Do not edit.\n" |
- "\n\n" |
- "#include " |
- "\"chrome/browser/metrics/variations/generated_resources_map.h\"\n\n" |
- "namespace chrome_variations {\n\n" |
+ "// This file was generated by %s. Do not edit.\n" |
+ "\n" |
+ "#include \"%s\"\n\n" |
+ "%s" |
+ "namespace {\n\n" |
"const size_t kNumResources = %i;\n\n" |
"%s" |
"\n" |
"%s" |
"\n" |
- "} // namespace chrome_variations\n") % ( |
- len(hashed_tuples), hashes_array, indices_array) |
+ "} // namespace\n" |
+ "\n" |
+ "variations::UIStringOverrider CreateUIStringOverrider() {\n" |
+ " return variations::UIStringOverrider(\n" |
+ " kResourceHashes, kResourceIndices, kNumResources);\n" |
+ "}\n" |
+ "%s") % ( |
jwd
2015/09/29 15:02:54
Can you use dictionary based string interpolation
sdefresne
2015/09/30 09:38:43
Done.
|
+ SCRIPT_NAME, header_filename, namespace_prefix, len(hashed_tuples), |
+ hashes_array, indices_array, namespace_suffix) |
+ |
+ |
+def _GenerateHeaderFileContent(namespace, header_filename): |
+ """Generates the .h for to the .cc generated by _GenerateSourceFileContent. |
+ |
+ Args: |
+ namespace: The namespace in which the generated code should be scoped. If |
+ not defined, then the code will be in the global namespace. |
+ |
+ header_filename: Path to the corresponding .h. Used to generate the include |
+ guards. |
+ Returns: |
+ .cc file content implementing the CreateUIStringOverrider() factory. |
+ """ |
+ |
+ include_guard = re.sub('[^A-Z]', '_', header_filename.upper()) + '_' |
+ |
+ namespace_prefix = "" |
+ namespace_suffix = "" |
+ if namespace: |
+ namespace_prefix = "namespace %s {\n\n" % namespace |
+ namespace_suffix = "\n} // namespace %s\n" % namespace |
+ |
+ return ( |
+ "// This file was generated by %s. Do not edit.\n" |
+ "\n" |
+ "#ifndef %s\n" |
+ "#define %s\n" |
+ "\n" |
+ "#include \"components/variations/service/ui_string_overrider.h\"\n\n" |
+ "%s" |
+ "// Returns an initialized UIStringOverrider.\n" |
+ "variations::UIStringOverrider CreateUIStringOverrider();\n" |
+ "%s" |
+ "\n" |
+ "#endif // %s\n" |
jwd
2015/09/29 15:02:54
optional nit: use dictionary string interpolation
sdefresne
2015/09/30 09:38:43
Done.
|
+ ) % ( |
+ SCRIPT_NAME, include_guard, include_guard, namespace_prefix, |
+ namespace_suffix, include_guard) |
+ |
+ |
+def main(): |
+ arg_parser = argparse.ArgumentParser( |
+ description="Generate UIStringOverrider factory from resources headers " |
+ "generated by grit.") |
+ arg_parser.add_argument( |
+ "--output_dir", "-o", required=True, |
+ help="Base directory to for generated files.") |
+ arg_parser.add_argument( |
+ "--source_filename", "-S", required=True, |
+ help="File name of the generated source file.") |
+ arg_parser.add_argument( |
+ "--header_filename", "-H", required=True, |
+ help="File name of the generated header file.") |
+ arg_parser.add_argument( |
+ "--namespace", "-N", default="", |
+ help="Namespace of the generated factory function (code will be in " |
+ "the global namespace if this is omitted).") |
+ arg_parser.add_argument( |
+ "--test_support", "-t", action="store_true", default=False, |
+ help="Make internal variables accessible for testing.") |
+ arg_parser.add_argument( |
+ "inputs", metavar="FILENAME", nargs="+", |
+ help="Path to resources header file generated by grit.") |
+ arguments = arg_parser.parse_args() |
-def main(resources_file, map_file): |
generated_resources_h = "" |
- with open(resources_file, "r") as resources: |
- generated_resources_h = resources.read() |
+ for resources_file in arguments.inputs: |
+ with open(resources_file, "r") as resources: |
+ generated_resources_h += resources.read() |
if len(generated_resources_h) == 0: |
raise Error("No content loaded for %s." % (resources_file)) |
- file_content = _GenerateFileContent(generated_resources_h) |
+ source_file_content = _GenerateSourceFileContent( |
+ generated_resources_h, arguments.namespace, arguments.header_filename) |
+ header_file_content = _GenerateHeaderFileContent( |
+ arguments.namespace, arguments.header_filename) |
- with open(map_file, "w") as generated_file: |
- generated_file.write(file_content) |
+ with open(os.path.join( |
+ arguments.output_dir, arguments.source_filename), "w") as generated_file: |
+ generated_file.write(source_file_content) |
+ with open(os.path.join( |
+ arguments.output_dir, arguments.header_filename), "w") as generated_file: |
+ generated_file.write(header_file_content) |
if __name__ == '__main__': |
- sys.exit(main(sys.argv[1], sys.argv[2])) |
+ sys.exit(main()) |