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

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: Answered review comments. 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 i = 0
not at google - send to devlin 2012/11/13 22:12:05 More descriptive name than "i"? Perhaps escape_ind
beaudoin 2012/11/14 17:34:12 Done.
14 while 1:
not at google - send to devlin 2012/11/13 22:12:05 (True)
beaudoin 2012/11/14 17:34:12 Gone.
15 i = json_string_literal.find('\\', i)
16 if i == -1: break
not at google - send to devlin 2012/11/13 22:12:05 (break on its own line)
beaudoin 2012/11/14 17:34:12 Gone.
17 if json_string_literal[i + 1] == 'u':
18 c_string_literal = (json_string_literal[0:i + 1] + 'x' +
19 json_string_literal[i + 2:])
20 i += 2
21 return c_string_literal
22
23 def _GenerateString(content, lines):
24 """Generates an UTF-8 string to be included in a static structure initializer.
25 If content is not specified, uses NULL.
26 """
27 if content is None:
28 lines.append(' NULL,')
29 else:
30 # json.dumps quotes the string and escape characters as required.
31 lines.append(' %s,' % json.dumps(content))
32
33 def _GenerateString16(content, lines):
34 """Generates an UTF-16 string to be included in a static structure
35 initializer. If content is not specified, uses NULL.
36 """
37 if content is None:
38 lines.append(' NULL,')
39 else:
40 # json.dumps quotes the string and escape characters as required.
41 lines.append(' L%s,' % _JSONToCString16(json.dumps(content)))
42
43 def _GenerateArray(field_info, content, lines):
44 """Generates an array to be included in a static structure initializer. If
45 content is not specified, uses NULL. The array is assigned to a temporary
46 variable which is initialized before the structure.
47 """
48 if content is None:
49 lines.append(' NULL,')
50 lines.append(' 0,') # Size of the array.
51 return
52
53 # Create a new array variable and use it in the structure initializer.
54 # This prohibits nested arrays. Add a clash detection and renaming mechanism
55 # to solve the problem.
56 var = 'array_%s' % field_info['field'];
57 lines.append(' %s,' % var)
58 lines.append(' %s,' % len(content)) # Size of the array.
59 # Generate the array content.
60 array_lines = []
61 field_info['contents']['field'] = var;
62 array_lines.append(struct_generator.GenerateField(
63 field_info['contents']) + '[] = {')
64 for subcontent in content:
65 GenerateFieldContent(field_info['contents'], subcontent, array_lines)
66 array_lines.append('};')
67 # Prepend the generated array so it is initialized before the structure.
68 lines.reverse()
69 array_lines.reverse()
70 lines.extend(array_lines)
71 lines.reverse()
72
73 def GenerateFieldContent(field_info, content, lines):
74 """Generate the content of a field to be included in the static structure
75 initializer. If the field's content is not specified, uses the default value
76 if one exists.
77 """
78 if content is None:
79 content = field_info.get('default', None)
80 type = field_info['type']
81 if type == 'int' or type == 'enum':
82 lines.append(' %s,' % content)
83 elif type == 'string':
84 _GenerateString(content, lines)
85 elif type == 'string16':
86 _GenerateString16(content, lines)
87 elif type == 'array':
88 _GenerateArray(field_info, content, lines)
89 else:
90 raise RuntimeError('Unknown field type "%s"' % type)
91
92 def GenerateElement(type_name, schema, element_name, element):
93 """Generate the static structure initializer for one element.
94 """
95 lines = [];
96 lines.append('const %s %s = {' % (type_name, element_name));
97 for field_info in schema:
98 content = element.get(field_info['field'], None)
99 if (content == None and not field_info.get('optional', False)):
100 raise RuntimeError('Mandatory field "%s" omitted in element "%s".' %
101 (field_info['field'], element_name))
102 GenerateFieldContent(field_info, content, lines)
103 lines.append('};')
104 return '\n'.join(lines)
105
106 def GenerateElements(type_name, schema, description):
107 """Generate the static structure initializer for all the elements in the
108 description['elements'] dictionary, as well as for any variables in
109 description['int_variables'].
110 """
111 result = [];
112 for var_name, value in description.get('int_variables', {}).items():
113 result.append('const int %s = %s;' % (var_name, value))
114 result.append('')
115
116 for element_name, element in description.get('elements', {}).items():
117 result.append(GenerateElement(type_name, schema, element_name, element))
118 result.append('')
119 return '\n'.join(result)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698