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

Unified Diff: tools/json_to_struct/element_generator.py

Issue 11377049: Moving prepopulated search engines to a JSON file. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Unit tests for python and C++. Added build step. Created 8 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_to_struct/element_generator.py
diff --git a/tools/json_to_struct/element_generator.py b/tools/json_to_struct/element_generator.py
new file mode 100644
index 0000000000000000000000000000000000000000..764f19c8b0b65c684c5e9dfaa4998591775bdfb4
--- /dev/null
+++ b/tools/json_to_struct/element_generator.py
@@ -0,0 +1,138 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import json
+import struct_generator
+
+def _JSONToCString16(json_string_literal):
+ """Converts a JSON string literal to a C++ UTF-16 string literal. This is
+ done by converting \\u#### to \\x####.
+ """
+ c_string_literal = json_string_literal
+ i = 0
+ while 1:
+ i = json_string_literal.find('\\', i)
+ if i == -1: break
+ if json_string_literal[i + 1] == 'u':
+ c_string_literal = (json_string_literal[0:i + 1] + 'x' +
+ json_string_literal[i + 2:])
+ i += 2
+ return c_string_literal
+
+def _GenerateInt(field_info, content, lines):
+ """Generates an int to be included in a static structure initializer. If
+ content is not specified, generates the default int for this field.
+ """
+ if content is None:
+ content = field_info['default']
+ lines.append(' %s,' % content)
+
+def _GenerateString(field_info, content, lines):
+ """Generates an UTF-8 string to be included in a static structure initializer.
+ If content is not specified, uses this field's default string or NULL if it
+ doesn't have any default.
+ """
+ try:
+ if content is None:
+ content = field_info['default']
not at google - send to devlin 2012/11/13 20:28:07 content = field_info.get('default', 'NULL') rather
beaudoin 2012/11/13 21:42:04 Thanks. First Python patch so I have likely missed
+ # json.dumps quotes the string and escape characters as required.
+ lines.append(' %s,' % json.dumps(content))
+ except KeyError:
+ lines.append(' NULL,')
+
+def _GenerateString16(field_info, content, lines):
+ """Generates an UTF-16 string to be included in a static structure
+ initializer. If content is not specified, uses this field's default string or
+ NULL if it doesn't have any default.
+ """
+ try:
+ if content is None:
+ content = field_info['default']
+ # json.dumps quotes the string and escape characters as required.
+ lines.append(' L%s,' % _JSONToCString16(json.dumps(content)))
+ except KeyError:
not at google - send to devlin 2012/11/13 20:28:07 etc It would actually be nice not to need to repe
beaudoin 2012/11/13 21:42:04 Done.
+ lines.append(' NULL,')
+
+def _GenerateEnum(field_info, content, lines):
+ """Generates an enum to be included in a static structure initializer. If
+ content is not specified, generates the default enum for this field.
+ """
+ if content is None:
+ content = field_info['default']
+ lines.append(' %s,' % content)
+
+def _GenerateArray(field_info, content, lines):
+ """Generates an array to be included in a static structure initializer. If
+ content is not specified, uses NULL. The array is assigned to a temporary
+ variable which is initilized before the structure.
+ """
+ if content is None:
+ lines.append(' NULL,')
+ lines.append(' 0,') # Size of the array.
not at google - send to devlin 2012/11/13 20:28:07 nit: return, no else.
beaudoin 2012/11/13 21:42:04 Done.
+ else:
+ # Create a new array variable and use it in the structure initializer.
+ # This prohibits nested arrays. Add a clash detection and renaming mechanism
+ # to solve the problem.
+ var = 'array_%s' % field_info['field'];
+ lines.append(' %s,' % var)
+ lines.append(' %s,' % len(content)) # Size of the array.
+ # Generate the array content.
+ array_lines = []
+ field_info['contents']['field'] = var;
+ array_lines.append(struct_generator.GenerateField(
+ field_info['contents']) + '[] = {')
+ for subcontent in content:
+ GenerateFieldContent(field_info['contents'], subcontent, array_lines)
+ array_lines.append('};')
+ # Prepend the generated array so it is initialized before the structure.
+ lines.reverse()
+ array_lines.reverse()
+ lines.extend(array_lines)
+ lines.reverse()
+
+# Map of supported types to generator functions.
+_content_generators = {
+ "int": _GenerateInt,
+ "string": _GenerateString,
+ "string16": _GenerateString16,
+ "enum": _GenerateEnum,
+ "array": _GenerateArray,
+}
not at google - send to devlin 2012/11/13 20:28:07 I think that using this is obfuscating the code in
beaudoin 2012/11/13 21:42:04 Done.
+
+def GenerateFieldContent(field_info, content, lines):
+ """Generate the content of a field to be included in the static structure
+ initializer.
+ """
+ return _content_generators[field_info['type']](field_info, content, lines)
+
+def GenerateElement(type_name, schema, element_name, element):
+ """Generate the static structure initializer for one element.
+ """
+ lines = [];
+ lines.append('const %s %s = {' % (type_name, element_name));
+ for field_info in schema:
+ content = element.get(field_info['field'], None)
+ if (content == None and not field_info.get('optional', False)):
+ raise RuntimeError('Mandatory field "%s" omitted in element "%s".' %
+ (field_info['field'], element_name))
+ GenerateFieldContent(field_info, content, lines)
+ lines.append('};')
+ return '\n'.join(lines)
+
+def GenerateElements(type_name, schema, description):
+ """Generate the static structure initializer for all the elements in the
+ description['elements'] dictionary, as well as for any variables in
+ description['int_variables'].
+ """
+ result = [];
+ try:
+ for var_name, value in description['int_variables'].items():
+ result.append('const int %s = %s;' % (var_name, value))
+ result.append('')
+ except KeyError:
not at google - send to devlin 2012/11/13 20:28:07 keyerror etc
beaudoin 2012/11/13 21:42:04 Done.
+ pass
+ for element_name, element in description['elements'].items():
+ result.append(GenerateElement(type_name, schema, element_name, element))
+ result.append('')
+ return '\n'.join(result)

Powered by Google App Engine
This is Rietveld 408576698