| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from string import Template | 5 from string import Template |
| 6 | 6 |
| 7 import optparse | 7 import optparse |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 | 10 |
| 11 try: | 11 try: |
| 12 grit_module_path = os.path.join( | 12 grit_module_path = os.path.join( |
| 13 os.path.dirname(__file__), '..', '..', '..', 'tools', 'grit') | 13 os.path.dirname(__file__), '..', '..', '..', 'tools', 'grit') |
| 14 sys.path.insert(0, grit_module_path) | 14 sys.path.insert(0, grit_module_path) |
| 15 from grit.format import data_pack as DataPack | 15 from grit.format import data_pack as DataPack |
| 16 except ImportError, e: | 16 except ImportError, e: |
| 17 print 'ImportError: ', e | 17 print 'ImportError: ', e |
| 18 sys.exit(-1) | 18 sys.exit(-1) |
| 19 | 19 |
| 20 def is_ascii(s): |
| 21 return all(ord(c) < 128 for c in s) |
| 22 |
| 20 header_template = \ | 23 header_template = \ |
| 21 """// Copyright 2015 The Chromium Authors. All rights reserved. | 24 """// Copyright 2015 The Chromium Authors. All rights reserved. |
| 22 // Use of this source code is governed by a BSD-style license that can be | 25 // Use of this source code is governed by a BSD-style license that can be |
| 23 // found in the LICENSE file. | 26 // found in the LICENSE file. |
| 24 | 27 |
| 25 #ifndef MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ | 28 #ifndef MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ |
| 26 #define MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ | 29 #define MOJO_SERVICES_HTML_VIEWER_BLINK_RESOURCE_MAP_H_ |
| 27 | 30 |
| 28 #include <map> | 31 #include <map> |
| 29 | 32 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 110 |
| 108 pak_contents = DataPack.ReadDataPack(options.pak_file) | 111 pak_contents = DataPack.ReadDataPack(options.pak_file) |
| 109 resourceIds = [] | 112 resourceIds = [] |
| 110 | 113 |
| 111 header_contents = dict() | 114 header_contents = dict() |
| 112 cpp_contents = dict() | 115 cpp_contents = dict() |
| 113 | 116 |
| 114 definitions = [] | 117 definitions = [] |
| 115 | 118 |
| 116 for (resId, data) in pak_contents.resources.iteritems(): | 119 for (resId, data) in pak_contents.resources.iteritems(): |
| 120 if not is_ascii(data): |
| 121 continue |
| 117 resourceIds.append(resId) | 122 resourceIds.append(resId) |
| 118 hex_values = ['0x{0:02x}'.format(ord(char)) for char in data] | 123 hex_values = ['0x{0:02x}'.format(ord(char)) for char in data] |
| 119 f = lambda A, n=12: [A[i:i+n] for i in range(0, len(A), n)] | 124 f = lambda A, n=12: [A[i:i+n] for i in range(0, len(A), n)] |
| 120 hex_values_string = ',\n '.join(', '.join(x) for x in f(hex_values)) | 125 hex_values_string = ',\n '.join(', '.join(x) for x in f(hex_values)) |
| 121 cpp_definition = \ | 126 cpp_definition = \ |
| 122 'const char kResource%s[%d] = {\n %s \n};' % \ | 127 'const char kResource%s[%d] = {\n %s \n};' % \ |
| 123 (str(resId), len(hex_values), hex_values_string) | 128 (str(resId), len(hex_values), hex_values_string) |
| 124 definitions.append(cpp_definition) | 129 definitions.append(cpp_definition) |
| 125 | 130 |
| 126 header_file_contents = Template(header_template).substitute(header_contents) | 131 header_file_contents = Template(header_template).substitute(header_contents) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 137 | 142 |
| 138 cpp_contents['definitions']= '\n'.join(definitions) | 143 cpp_contents['definitions']= '\n'.join(definitions) |
| 139 cpp_contents['header_file_name'] = os.path.basename(options.header_file) | 144 cpp_contents['header_file_name'] = os.path.basename(options.header_file) |
| 140 cpp_contents['map_initializer'] = '\n '.join(map_initializer) | 145 cpp_contents['map_initializer'] = '\n '.join(map_initializer) |
| 141 cpp_file_contents = Template(cpp_template).substitute(cpp_contents) | 146 cpp_file_contents = Template(cpp_template).substitute(cpp_contents) |
| 142 cpp_file.write(cpp_file_contents) | 147 cpp_file.write(cpp_file_contents) |
| 143 cpp_file.close() | 148 cpp_file.close() |
| 144 | 149 |
| 145 if __name__ == '__main__': | 150 if __name__ == '__main__': |
| 146 main() | 151 main() |
| OLD | NEW |