| 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 |
| 11 import os | 11 import os |
| 12 import struct | 12 import struct |
| 13 import sys | 13 import sys |
| 14 | 14 |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) | 15 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) |
| 16 from grit.format import interface | 16 from grit.format import interface |
| 17 from grit.node import include | 17 from grit.node import include |
| 18 from grit.node import message | 18 from grit.node import message |
| 19 from grit.node import structure |
| 19 from grit.node import misc | 20 from grit.node import misc |
| 20 | 21 |
| 21 | 22 |
| 22 PACK_FILE_VERSION = 4 | 23 PACK_FILE_VERSION = 4 |
| 23 HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and | 24 HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and |
| 24 # one uint8 (encoding of text resources) | 25 # one uint8 (encoding of text resources) |
| 25 BINARY, UTF8, UTF16 = range(3) | 26 BINARY, UTF8, UTF16 = range(3) |
| 26 | 27 |
| 27 class WrongFileVersion(Exception): | 28 class WrongFileVersion(Exception): |
| 28 pass | 29 pass |
| (...skipping 15 matching lines...) Expand all Loading... |
| 44 data[id] = value | 45 data[id] = value |
| 45 return DataPack.WriteDataPackToString(data, UTF8) | 46 return DataPack.WriteDataPackToString(data, UTF8) |
| 46 | 47 |
| 47 @staticmethod | 48 @staticmethod |
| 48 def GetDataNodes(item): | 49 def GetDataNodes(item): |
| 49 '''Returns a list of nodes that can be packed into the data pack file.''' | 50 '''Returns a list of nodes that can be packed into the data pack file.''' |
| 50 nodes = [] | 51 nodes = [] |
| 51 if (isinstance(item, misc.IfNode) and not item.IsConditionSatisfied()): | 52 if (isinstance(item, misc.IfNode) and not item.IsConditionSatisfied()): |
| 52 return nodes | 53 return nodes |
| 53 if (isinstance(item, include.IncludeNode) or | 54 if (isinstance(item, include.IncludeNode) or |
| 54 isinstance(item, message.MessageNode)): | 55 isinstance(item, message.MessageNode) or |
| 56 isinstance(item, structure.StructureNode)): |
| 55 # Include this node if it wasn't marked as skipped by a whitelist. | 57 # Include this node if it wasn't marked as skipped by a whitelist. |
| 56 if not item.WhitelistMarkedAsSkip(): | 58 if not item.WhitelistMarkedAsSkip(): |
| 57 return [item] | 59 return [item] |
| 58 return nodes | 60 return nodes |
| 59 for child in item.children: | 61 for child in item.children: |
| 60 nodes.extend(DataPack.GetDataNodes(child)) | 62 nodes.extend(DataPack.GetDataNodes(child)) |
| 61 return nodes | 63 return nodes |
| 62 | 64 |
| 63 @staticmethod | 65 @staticmethod |
| 64 def ReadDataPack(input_file): | 66 def ReadDataPack(input_file): |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 else: | 166 else: |
| 165 # Just write a simple file. | 167 # Just write a simple file. |
| 166 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } | 168 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } |
| 167 DataPack.WriteDataPack(data, "datapack1.pak", UTF8) | 169 DataPack.WriteDataPack(data, "datapack1.pak", UTF8) |
| 168 data2 = { 1000: "test", 5: "five" } | 170 data2 = { 1000: "test", 5: "five" } |
| 169 DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) | 171 DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) |
| 170 print "wrote datapack1 and datapack2 to current directory." | 172 print "wrote datapack1 and datapack2 to current directory." |
| 171 | 173 |
| 172 if __name__ == '__main__': | 174 if __name__ == '__main__': |
| 173 main() | 175 main() |
| OLD | NEW |