- add third_party src.
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / data_pack.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 '''Support for formatting a data pack file used for platform agnostic resource
7 files.
8 '''
9
10 import collections
11 import exceptions
12 import os
13 import struct
14 import sys
15 if __name__ == '__main__':
16   sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
17
18 from grit import util
19 from grit.node import include
20 from grit.node import message
21 from grit.node import structure
22 from grit.node import misc
23
24
25 PACK_FILE_VERSION = 4
26 HEADER_LENGTH = 2 * 4 + 1  # Two uint32s. (file version, number of entries) and
27                            # one uint8 (encoding of text resources)
28 BINARY, UTF8, UTF16 = range(3)
29
30
31 class WrongFileVersion(Exception):
32   pass
33
34
35 DataPackContents = collections.namedtuple(
36     'DataPackContents', 'resources encoding')
37
38
39 def Format(root, lang='en', output_dir='.'):
40   '''Writes out the data pack file format (platform agnostic resource file).'''
41   data = {}
42   for node in root.ActiveDescendants():
43     with node:
44       if isinstance(node, (include.IncludeNode, message.MessageNode,
45                            structure.StructureNode)):
46         id, value = node.GetDataPackPair(lang, UTF8)
47         if value is not None:
48           data[id] = value
49   return WriteDataPackToString(data, UTF8)
50
51
52 def ReadDataPack(input_file):
53   """Reads a data pack file and returns a dictionary."""
54   data = util.ReadFile(input_file, util.BINARY)
55   original_data = data
56
57   # Read the header.
58   version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])
59   if version != PACK_FILE_VERSION:
60     print "Wrong file version in ", input_file
61     raise WrongFileVersion
62
63   resources = {}
64   if num_entries == 0:
65     return DataPackContents(resources, encoding)
66
67   # Read the index and data.
68   data = data[HEADER_LENGTH:]
69   kIndexEntrySize = 2 + 4  # Each entry is a uint16 and a uint32.
70   for _ in range(num_entries):
71     id, offset = struct.unpack("<HI", data[:kIndexEntrySize])
72     data = data[kIndexEntrySize:]
73     next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])
74     resources[id] = original_data[offset:next_offset]
75
76   return DataPackContents(resources, encoding)
77
78
79 def WriteDataPackToString(resources, encoding):
80   """Write a map of id=>data into a string in the data pack format and return
81   it."""
82   ids = sorted(resources.keys())
83   ret = []
84
85   # Write file header.
86   ret.append(struct.pack("<IIB", PACK_FILE_VERSION, len(ids), encoding))
87   HEADER_LENGTH = 2 * 4 + 1            # Two uint32s and one uint8.
88
89   # Each entry is a uint16 + a uint32s. We have one extra entry for the last
90   # item.
91   index_length = (len(ids) + 1) * (2 + 4)
92
93   # Write index.
94   data_offset = HEADER_LENGTH + index_length
95   for id in ids:
96     ret.append(struct.pack("<HI", id, data_offset))
97     data_offset += len(resources[id])
98
99   ret.append(struct.pack("<HI", 0, data_offset))
100
101   # Write data.
102   for id in ids:
103     ret.append(resources[id])
104   return ''.join(ret)
105
106
107 def WriteDataPack(resources, output_file, encoding):
108   """Write a map of id=>data into output_file as a data pack."""
109   content = WriteDataPackToString(resources, encoding)
110   with open(output_file, "wb") as file:
111     file.write(content)
112
113
114 def RePack(output_file, input_files):
115   """Write a new data pack to |output_file| based on a list of filenames
116   (|input_files|)"""
117   resources = {}
118   encoding = None
119   for filename in input_files:
120     new_content = ReadDataPack(filename)
121
122     # Make sure we have no dups.
123     duplicate_keys = set(new_content.resources.keys()) & set(resources.keys())
124     if len(duplicate_keys) != 0:
125       raise exceptions.KeyError("Duplicate keys: " + str(list(duplicate_keys)))
126
127     # Make sure encoding is consistent.
128     if encoding in (None, BINARY):
129       encoding = new_content.encoding
130     elif new_content.encoding not in (BINARY, encoding):
131         raise exceptions.KeyError("Inconsistent encodings: " +
132                                   str(encoding) + " vs " +
133                                   str(new_content.encoding))
134
135     resources.update(new_content.resources)
136
137   # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16
138   if encoding is None:
139     encoding = BINARY
140   WriteDataPack(resources, output_file, encoding)
141
142
143 # Temporary hack for external programs that import data_pack.
144 # TODO(benrg): Remove this.
145 class DataPack(object):
146   pass
147 DataPack.ReadDataPack = staticmethod(ReadDataPack)
148 DataPack.WriteDataPackToString = staticmethod(WriteDataPackToString)
149 DataPack.WriteDataPack = staticmethod(WriteDataPack)
150 DataPack.RePack = staticmethod(RePack)
151
152
153 def main():
154   if len(sys.argv) > 1:
155     # When an argument is given, read and explode the file to text
156     # format, for easier diffing.
157     data = ReadDataPack(sys.argv[1])
158     print data.encoding
159     for (resource_id, text) in data.resources.iteritems():
160       print "%s: %s" % (resource_id, text)
161   else:
162     # Just write a simple file.
163     data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
164     WriteDataPack(data, "datapack1.pak", UTF8)
165     data2 = { 1000: "test", 5: "five" }
166     WriteDataPack(data2, "datapack2.pak", UTF8)
167     print "wrote datapack1 and datapack2 to current directory."
168
169
170 if __name__ == '__main__':
171   main()