OLD | NEW |
---|---|
(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 | |
14 while 1: | |
15 i = json_string_literal.find('\\', i) | |
16 if i == -1: break | |
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 _GenerateInt(field_info, content, lines): | |
24 """Generates an int to be included in a static structure initializer. If | |
25 content is not specified, generates the default int for this field. | |
26 """ | |
27 if content is None: | |
28 content = field_info['default'] | |
29 lines.append(' %s,' % content) | |
30 | |
31 def _GenerateString(field_info, content, lines): | |
32 """Generates an UTF-8 string to be included in a static structure initializer. | |
33 If content is not specified, uses this field's default string or NULL if it | |
34 doesn't have any default. | |
35 """ | |
36 try: | |
37 if content is None: | |
38 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
| |
39 # json.dumps quotes the string and escape characters as required. | |
40 lines.append(' %s,' % json.dumps(content)) | |
41 except KeyError: | |
42 lines.append(' NULL,') | |
43 | |
44 def _GenerateString16(field_info, content, lines): | |
45 """Generates an UTF-16 string to be included in a static structure | |
46 initializer. If content is not specified, uses this field's default string or | |
47 NULL if it doesn't have any default. | |
48 """ | |
49 try: | |
50 if content is None: | |
51 content = field_info['default'] | |
52 # json.dumps quotes the string and escape characters as required. | |
53 lines.append(' L%s,' % _JSONToCString16(json.dumps(content))) | |
54 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.
| |
55 lines.append(' NULL,') | |
56 | |
57 def _GenerateEnum(field_info, content, lines): | |
58 """Generates an enum to be included in a static structure initializer. If | |
59 content is not specified, generates the default enum for this field. | |
60 """ | |
61 if content is None: | |
62 content = field_info['default'] | |
63 lines.append(' %s,' % content) | |
64 | |
65 def _GenerateArray(field_info, content, lines): | |
66 """Generates an array to be included in a static structure initializer. If | |
67 content is not specified, uses NULL. The array is assigned to a temporary | |
68 variable which is initilized before the structure. | |
69 """ | |
70 if content is None: | |
71 lines.append(' NULL,') | |
72 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.
| |
73 else: | |
74 # Create a new array variable and use it in the structure initializer. | |
75 # This prohibits nested arrays. Add a clash detection and renaming mechanism | |
76 # to solve the problem. | |
77 var = 'array_%s' % field_info['field']; | |
78 lines.append(' %s,' % var) | |
79 lines.append(' %s,' % len(content)) # Size of the array. | |
80 # Generate the array content. | |
81 array_lines = [] | |
82 field_info['contents']['field'] = var; | |
83 array_lines.append(struct_generator.GenerateField( | |
84 field_info['contents']) + '[] = {') | |
85 for subcontent in content: | |
86 GenerateFieldContent(field_info['contents'], subcontent, array_lines) | |
87 array_lines.append('};') | |
88 # Prepend the generated array so it is initialized before the structure. | |
89 lines.reverse() | |
90 array_lines.reverse() | |
91 lines.extend(array_lines) | |
92 lines.reverse() | |
93 | |
94 # Map of supported types to generator functions. | |
95 _content_generators = { | |
96 "int": _GenerateInt, | |
97 "string": _GenerateString, | |
98 "string16": _GenerateString16, | |
99 "enum": _GenerateEnum, | |
100 "array": _GenerateArray, | |
101 } | |
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.
| |
102 | |
103 def GenerateFieldContent(field_info, content, lines): | |
104 """Generate the content of a field to be included in the static structure | |
105 initializer. | |
106 """ | |
107 return _content_generators[field_info['type']](field_info, content, lines) | |
108 | |
109 def GenerateElement(type_name, schema, element_name, element): | |
110 """Generate the static structure initializer for one element. | |
111 """ | |
112 lines = []; | |
113 lines.append('const %s %s = {' % (type_name, element_name)); | |
114 for field_info in schema: | |
115 content = element.get(field_info['field'], None) | |
116 if (content == None and not field_info.get('optional', False)): | |
117 raise RuntimeError('Mandatory field "%s" omitted in element "%s".' % | |
118 (field_info['field'], element_name)) | |
119 GenerateFieldContent(field_info, content, lines) | |
120 lines.append('};') | |
121 return '\n'.join(lines) | |
122 | |
123 def GenerateElements(type_name, schema, description): | |
124 """Generate the static structure initializer for all the elements in the | |
125 description['elements'] dictionary, as well as for any variables in | |
126 description['int_variables']. | |
127 """ | |
128 result = []; | |
129 try: | |
130 for var_name, value in description['int_variables'].items(): | |
131 result.append('const int %s = %s;' % (var_name, value)) | |
132 result.append('') | |
133 except KeyError: | |
not at google - send to devlin
2012/11/13 20:28:07
keyerror etc
beaudoin
2012/11/13 21:42:04
Done.
| |
134 pass | |
135 for element_name, element in description['elements'].items(): | |
136 result.append(GenerateElement(type_name, schema, element_name, element)) | |
137 result.append('') | |
138 return '\n'.join(result) | |
OLD | NEW |