| 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 | 
| 29 | 30 | 
| 30 class DataPackContents: | 31 class DataPackContents: | 
| 31   def __init__(self, resources, encoding): | 32   def __init__(self, resources, encoding): | 
| 32     self.resources = resources | 33     self.resources = resources | 
| 33     self.encoding = encoding | 34     self.encoding = encoding | 
| 34 | 35 | 
| 35 class DataPack(interface.ItemFormatter): | 36 class DataPack(interface.ItemFormatter): | 
| 36   '''Writes out the data pack file format (platform agnostic resource file).''' | 37   '''Writes out the data pack file format (platform agnostic resource file).''' | 
| 37   def Format(self, item, lang='en', output_dir='.'): | 38   def Format(self, item, lang='en', output_dir='.'): | 
| 38     assert isinstance(item, misc.ReleaseNode) | 39     assert isinstance(item, misc.ReleaseNode) | 
| 39 | 40 | 
| 40     nodes = DataPack.GetDataNodes(item) | 41     nodes = DataPack.GetDataNodes(item) | 
| 41     data = {} | 42     data = {} | 
| 42     for node in nodes: | 43     for node in nodes: | 
| 43       id, value = node.GetDataPackPair(lang, UTF8) | 44       id, value = node.GetDataPackPair(lang, UTF8) | 
| 44       data[id] = value | 45       if value is not None: | 
|  | 46         data[id] = value | 
| 45     return DataPack.WriteDataPackToString(data, UTF8) | 47     return DataPack.WriteDataPackToString(data, UTF8) | 
| 46 | 48 | 
| 47   @staticmethod | 49   @staticmethod | 
| 48   def GetDataNodes(item): | 50   def GetDataNodes(item): | 
| 49     '''Returns a list of nodes that can be packed into the data pack file.''' | 51     '''Returns a list of nodes that can be packed into the data pack file.''' | 
| 50     nodes = [] | 52     nodes = [] | 
| 51     if (isinstance(item, misc.IfNode) and not item.IsConditionSatisfied()): | 53     if (isinstance(item, misc.IfNode) and not item.IsConditionSatisfied()): | 
| 52       return nodes | 54       return nodes | 
| 53     if (isinstance(item, include.IncludeNode) or | 55     if (isinstance(item, include.IncludeNode) or | 
| 54         isinstance(item, message.MessageNode)): | 56         isinstance(item, message.MessageNode) or | 
|  | 57         isinstance(item, structure.StructureNode)): | 
| 55       # Include this node if it wasn't marked as skipped by a whitelist. | 58       # Include this node if it wasn't marked as skipped by a whitelist. | 
| 56       if not item.WhitelistMarkedAsSkip(): | 59       if not item.WhitelistMarkedAsSkip(): | 
| 57         return [item] | 60         return [item] | 
| 58       return nodes | 61       return nodes | 
| 59     for child in item.children: | 62     for child in item.children: | 
| 60       nodes.extend(DataPack.GetDataNodes(child)) | 63       nodes.extend(DataPack.GetDataNodes(child)) | 
| 61     return nodes | 64     return nodes | 
| 62 | 65 | 
| 63   @staticmethod | 66   @staticmethod | 
| 64   def ReadDataPack(input_file): | 67   def ReadDataPack(input_file): | 
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 164   else: | 167   else: | 
| 165     # Just write a simple file. | 168     # Just write a simple file. | 
| 166     data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } | 169     data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } | 
| 167     DataPack.WriteDataPack(data, "datapack1.pak", UTF8) | 170     DataPack.WriteDataPack(data, "datapack1.pak", UTF8) | 
| 168     data2 = { 1000: "test", 5: "five" } | 171     data2 = { 1000: "test", 5: "five" } | 
| 169     DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) | 172     DataPack.WriteDataPack(data2, "datapack2.pak", UTF8) | 
| 170     print "wrote datapack1 and datapack2 to current directory." | 173     print "wrote datapack1 and datapack2 to current directory." | 
| 171 | 174 | 
| 172 if __name__ == '__main__': | 175 if __name__ == '__main__': | 
| 173   main() | 176   main() | 
| OLD | NEW | 
|---|