OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Support for formatting a data pack file used for platform agnostic resource | 6 '''Support for formatting a data pack file used for platform agnostic resource |
7 files. | 7 files. |
8 ''' | 8 ''' |
9 | 9 |
10 import exceptions | 10 import exceptions |
(...skipping 16 matching lines...) Expand all Loading... |
27 class WrongFileVersion(Exception): | 27 class WrongFileVersion(Exception): |
28 pass | 28 pass |
29 | 29 |
30 class DataPackContents: | 30 class DataPackContents: |
31 def __init__(self, resources, encoding): | 31 def __init__(self, resources, encoding): |
32 self.resources = resources | 32 self.resources = resources |
33 self.encoding = encoding | 33 self.encoding = encoding |
34 | 34 |
35 class DataPack(interface.ItemFormatter): | 35 class DataPack(interface.ItemFormatter): |
36 '''Writes out the data pack file format (platform agnostic resource file).''' | 36 '''Writes out the data pack file format (platform agnostic resource file).''' |
37 def Format(self, item, lang='en', begin_item=True, output_dir='.'): | 37 def Format(self, item, lang='en', output_dir='.'): |
38 if not begin_item: | |
39 return '' | |
40 | |
41 assert isinstance(item, misc.ReleaseNode) | 38 assert isinstance(item, misc.ReleaseNode) |
42 | 39 |
43 nodes = DataPack.GetDataNodes(item) | 40 nodes = DataPack.GetDataNodes(item) |
44 data = {} | 41 data = {} |
45 for node in nodes: | 42 for node in nodes: |
46 id, value = node.GetDataPackPair(lang, UTF8) | 43 id, value = node.GetDataPackPair(lang, UTF8) |
47 data[id] = value | 44 data[id] = value |
48 return DataPack.WriteDataPackToString(data, UTF8) | 45 return DataPack.WriteDataPackToString(data, UTF8) |
49 | 46 |
50 @staticmethod | 47 @staticmethod |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 str(new_content.encoding)) | 147 str(new_content.encoding)) |
151 | 148 |
152 resources.update(new_content.resources) | 149 resources.update(new_content.resources) |
153 | 150 |
154 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16 | 151 # Encoding is 0 for BINARY, 1 for UTF8 and 2 for UTF16 |
155 if encoding is None: | 152 if encoding is None: |
156 encoding = BINARY | 153 encoding = BINARY |
157 DataPack.WriteDataPack(resources, output_file, encoding) | 154 DataPack.WriteDataPack(resources, output_file, encoding) |
158 | 155 |
159 def main(): | 156 def main(): |
160 # Just write a simple file. | 157 if len(sys.argv): |
161 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } | 158 # When an argument is given, read and explode the file to text |
162 DataPack.WriteDataPack(data, "datapack1.pak", UTF8) | 159 # format, for easier diffing. |
163 data2 = { 1000: "test", 5: "five" } | 160 data = DataPack.ReadDataPack(sys.argv[1]) |
164 DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) | 161 print data.encoding |
165 print "wrote datapack1 and datapack2 to current directory." | 162 for (resource_id, text) in data.resources.iteritems(): |
| 163 print "%s: %s" % (resource_id, text) |
| 164 else: |
| 165 # Just write a simple file. |
| 166 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } |
| 167 DataPack.WriteDataPack(data, "datapack1.pak", UTF8) |
| 168 data2 = { 1000: "test", 5: "five" } |
| 169 DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) |
| 170 print "wrote datapack1 and datapack2 to current directory." |
166 | 171 |
167 if __name__ == '__main__': | 172 if __name__ == '__main__': |
168 main() | 173 main() |
OLD | NEW |