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

Side by Side 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: Minor clean-up and more robust unit test. 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 unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6 import struct_generator
7
8 def _JSONToCString16(json_string_literal):
9 """Converts a JSON string literal to a C++ UTF-16 string literal. This is
10 done by converting \\u#### to \\x####.
11 """
12 c_string_literal = json_string_literal
13 escape_index = json_string_literal.find('\\')
14 while escape_index > 0:
15 if json_string_literal[escape_index + 1] == 'u':
16 c_string_literal = (json_string_literal[0:escape_index + 1] + 'x' +
17 json_string_literal[escape_index + 2:])
18 escape_index = json_string_literal.find('\\', escape_index + 2)
19 return c_string_literal
20
21 def _GenerateString(content, lines):
22 """Generates an UTF-8 string to be included in a static structure initializer.
23 If content is not specified, uses NULL.
24 """
25 if content is None:
26 lines.append(' NULL,')
27 else:
28 # json.dumps quotes the string and escape characters as required.
29 lines.append(' %s,' % json.dumps(content))
30
31 def _GenerateString16(content, lines):
32 """Generates an UTF-16 string to be included in a static structure
33 initializer. If content is not specified, uses NULL.
34 """
35 if content is None:
36 lines.append(' NULL,')
37 else:
38 # json.dumps quotes the string and escape characters as required.
39 lines.append(' L%s,' % _JSONToCString16(json.dumps(content)))
40
41 def _GenerateArray(field_info, content, lines):
42 """Generates an array to be included in a static structure initializer. If
43 content is not specified, uses NULL. The array is assigned to a temporary
44 variable which is initialized before the structure.
45 """
46 if content is None:
47 lines.append(' NULL,')
48 lines.append(' 0,') # Size of the array.
49 return
50
51 # Create a new array variable and use it in the structure initializer.
52 # This prohibits nested arrays. Add a clash detection and renaming mechanism
53 # to solve the problem.
54 var = 'array_%s' % field_info['field'];
55 lines.append(' %s,' % var)
56 lines.append(' %s,' % len(content)) # Size of the array.
57 # Generate the array content.
58 array_lines = []
59 field_info['contents']['field'] = var;
60 array_lines.append(struct_generator.GenerateField(
61 field_info['contents']) + '[] = {')
62 for subcontent in content:
63 GenerateFieldContent(field_info['contents'], subcontent, array_lines)
64 array_lines.append('};')
65 # Prepend the generated array so it is initialized before the structure.
66 lines.reverse()
67 array_lines.reverse()
68 lines.extend(array_lines)
69 lines.reverse()
70
71 def GenerateFieldContent(field_info, content, lines):
72 """Generate the content of a field to be included in the static structure
73 initializer. If the field's content is not specified, uses the default value
74 if one exists.
75 """
76 if content is None:
77 content = field_info.get('default', None)
78 type = field_info['type']
79 if type == 'int' or type == 'enum':
80 lines.append(' %s,' % content)
81 elif type == 'string':
82 _GenerateString(content, lines)
83 elif type == 'string16':
84 _GenerateString16(content, lines)
85 elif type == 'array':
86 _GenerateArray(field_info, content, lines)
87 else:
88 raise RuntimeError('Unknown field type "%s"' % type)
89
90 def GenerateElement(type_name, schema, element_name, element):
91 """Generate the static structure initializer for one element.
92 """
93 lines = [];
94 lines.append('const %s %s = {' % (type_name, element_name));
95 for field_info in schema:
96 content = element.get(field_info['field'], None)
97 if (content == None and not field_info.get('optional', False)):
98 raise RuntimeError('Mandatory field "%s" omitted in element "%s".' %
99 (field_info['field'], element_name))
100 GenerateFieldContent(field_info, content, lines)
101 lines.append('};')
102 return '\n'.join(lines)
103
104 def GenerateElements(type_name, schema, description):
105 """Generate the static structure initializer for all the elements in the
106 description['elements'] dictionary, as well as for any variables in
107 description['int_variables'].
108 """
109 result = [];
110 for var_name, value in description.get('int_variables', {}).items():
111 result.append('const int %s = %s;' % (var_name, value))
112 result.append('')
113
114 for element_name, element in description.get('elements', {}).items():
115 result.append(GenerateElement(type_name, schema, element_name, element))
116 result.append('')
117 return '\n'.join(result)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698