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
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
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_
30 namespace html_viewer {
32 class BlinkResourceMap {
35 const unsigned char* GetResource(int id, int* length);
38 struct ResourceEntry {
39 const unsigned char* data;
47 ResourceEntry(const unsigned char* data, int length)
52 typedef std::map<int, ResourceEntry> ResourceMap;
53 ResourceMap resources_;
56 } // namespace html_viewer
57 #endif // COMPONENTS_HTML_VIEWER_BLINK_RESOURCE_MAP_H_"""
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 {
72 BlinkResourceMap::BlinkResourceMap()
77 const unsigned char* BlinkResourceMap::GetResource(int id, int* length)
79 ResourceMap::iterator it = resources_.find(id);
80 if (it == resources_.end()) {
84 *length = it->second.length;
85 return it->second.data;
88 } // namespace html_viewer"""
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
):
105 header_file
= open(options
.header_file
, 'w+')
106 cpp_file
= open(options
.cpp_file
, 'w+')
108 pak_contents
= DataPack
.ReadDataPack(options
.pak_file
)
111 header_contents
= dict()
112 cpp_contents
= dict()
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
))
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
)
131 for resId
in resourceIds
:
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
)
145 if __name__
== '__main__':