Inline NetLog IPv6 reachability events.
[chromium-blink-merge.git] / components / html_viewer / generate_blink_resource_map.py
blobe60be141b951fd54a45c0cecd1a9d202e461ffeb
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
3 # found in the LICENSE file.
5 from string import Template
7 import optparse
8 import os
9 import sys
11 try:
12 grit_module_path = os.path.join(
13 os.path.dirname(__file__), '..', '..', 'tools', 'grit')
14 sys.path.insert(0, grit_module_path)
15 from grit.format import data_pack as DataPack
16 except ImportError, e:
17 print 'ImportError: ', e
18 sys.exit(-1)
20 header_template = \
21 """// Copyright 2015 The Chromium Authors. All rights reserved.
22 // Use of this source code is governed by a BSD-style license that can be
23 // found in the LICENSE file.
25 #ifndef COMPONENTS_HTML_VIEWER_BLINK_RESOURCE_MAP_H_
26 #define COMPONENTS_HTML_VIEWER_BLINK_RESOURCE_MAP_H_
28 #include <map>
30 namespace html_viewer {
32 class BlinkResourceMap {
33 public:
34 BlinkResourceMap();
35 const unsigned char* GetResource(int id, int* length);
37 private:
38 struct ResourceEntry {
39 const unsigned char* data;
40 int length;
42 ResourceEntry()
43 : data(nullptr)
44 , length(0) {
47 ResourceEntry(const unsigned char* data, int length)
48 : data(data)
49 , length(length) {
52 typedef std::map<int, ResourceEntry> ResourceMap;
53 ResourceMap resources_;
56 } // namespace html_viewer
57 #endif // COMPONENTS_HTML_VIEWER_BLINK_RESOURCE_MAP_H_"""
59 cpp_template = \
60 """// Copyright 2015 The Chromium Authors. All rights reserved.
61 // Use of this source code is governed by a BSD-style license that can be
62 // found in the LICENSE file.
64 #include "$header_file_name"
66 #include "base/macros.h"
68 namespace html_viewer {
70 $definitions
72 BlinkResourceMap::BlinkResourceMap()
74 $map_initializer
77 const unsigned char* BlinkResourceMap::GetResource(int id, int* length)
79 ResourceMap::iterator it = resources_.find(id);
80 if (it == resources_.end()) {
81 *length = 0;
82 return nullptr;
84 *length = it->second.length;
85 return it->second.data;
88 } // namespace html_viewer"""
90 def main():
91 parser = optparse.OptionParser(
92 usage='Usage: %prog --pak-file PAK_FILE --header HEADER --cpp CPP\n')
93 parser.add_option('-i', '--pak-file', action='store', dest='pak_file',
94 help='The .pak file to be extracted.')
95 parser.add_option('', '--header', action='store', dest='header_file',
96 help='Header file to be generated.')
97 parser.add_option('', '--cpp', action='store', dest='cpp_file',
98 help='C++ file to be generated.')
100 (options, _) = parser.parse_args()
101 if (not options.pak_file or not options.header_file or not options.cpp_file):
102 parser.print_help()
103 sys.exit(-1)
105 header_file = open(options.header_file, 'w+')
106 cpp_file = open(options.cpp_file, 'w+')
108 pak_contents = DataPack.ReadDataPack(options.pak_file)
109 resourceIds = []
111 header_contents = dict()
112 cpp_contents = dict()
114 definitions = []
116 for (resId, data) in pak_contents.resources.iteritems():
117 resourceIds.append(resId)
118 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)]
120 hex_values_string = ',\n '.join(', '.join(x) for x in f(hex_values))
121 cpp_definition = \
122 'const unsigned char kResource%s[%d] = {\n %s \n};' % \
123 (str(resId), len(hex_values), hex_values_string)
124 definitions.append(cpp_definition)
126 header_file_contents = Template(header_template).substitute(header_contents)
127 header_file.write(header_file_contents)
128 header_file.close()
130 map_initializer = []
131 for resId in resourceIds:
132 insert_statement = \
133 'resources_.insert(std::pair<int, ResourceEntry>(\n' \
134 ' %s, ResourceEntry(kResource%s, arraysize(kResource%s))));'
135 map_initializer.append( \
136 insert_statement % (str(resId), str(resId), str(resId)))
138 cpp_contents['definitions']= '\n'.join(definitions)
139 cpp_contents['header_file_name'] = os.path.basename(options.header_file)
140 cpp_contents['map_initializer'] = '\n '.join(map_initializer)
141 cpp_file_contents = Template(cpp_template).substitute(cpp_contents)
142 cpp_file.write(cpp_file_contents)
143 cpp_file.close()
145 if __name__ == '__main__':
146 main()