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 os |
| 11 import sys |
| 12 |
| 13 import rjsmin |
| 14 |
10 import collections | 15 import collections |
11 import exceptions | 16 import exceptions |
12 import os | 17 import os |
13 import struct | 18 import struct |
14 import sys | 19 import sys |
15 if __name__ == '__main__': | 20 if __name__ == '__main__': |
16 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | 21 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) |
17 | 22 |
18 from grit import util | 23 from grit import util |
19 from grit.node import include | 24 from grit.node import include |
(...skipping 16 matching lines...) Expand all Loading... |
36 | 41 |
37 | 42 |
38 def Format(root, lang='en', output_dir='.'): | 43 def Format(root, lang='en', output_dir='.'): |
39 """Writes out the data pack file format (platform agnostic resource file).""" | 44 """Writes out the data pack file format (platform agnostic resource file).""" |
40 data = {} | 45 data = {} |
41 for node in root.ActiveDescendants(): | 46 for node in root.ActiveDescendants(): |
42 with node: | 47 with node: |
43 if isinstance(node, (include.IncludeNode, message.MessageNode, | 48 if isinstance(node, (include.IncludeNode, message.MessageNode, |
44 structure.StructureNode)): | 49 structure.StructureNode)): |
45 id, value = node.GetDataPackPair(lang, UTF8) | 50 id, value = node.GetDataPackPair(lang, UTF8) |
| 51 txt_id = node.GetTextualIds()[0] |
| 52 |
| 53 # Use rjsmin to compress JavaScript resources. Closure would |
| 54 # be even better but is tricky to integrate into the build |
| 55 # system since it is Java. |
| 56 if "_JS" in txt_id[-3:]: |
| 57 size = len(value) |
| 58 packed_value = rjsmin.jsmin(value) |
| 59 packed_size = len(packed_value) |
| 60 if packed_size < size: |
| 61 value = packed_value |
46 if value is not None: | 62 if value is not None: |
47 data[id] = value | 63 data[id] = value |
48 return WriteDataPackToString(data, UTF8) | 64 return WriteDataPackToString(data, UTF8) |
49 | 65 |
50 | 66 |
51 def ReadDataPack(input_file): | 67 def ReadDataPack(input_file): |
52 """Reads a data pack file and returns a dictionary.""" | 68 """Reads a data pack file and returns a dictionary.""" |
53 data = util.ReadFile(input_file, util.BINARY) | 69 data = util.ReadFile(input_file, util.BINARY) |
54 original_data = data | 70 original_data = data |
55 | 71 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 # Just write a simple file. | 219 # Just write a simple file. |
204 data = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} | 220 data = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} |
205 WriteDataPack(data, 'datapack1.pak', UTF8) | 221 WriteDataPack(data, 'datapack1.pak', UTF8) |
206 data2 = {1000: 'test', 5: 'five'} | 222 data2 = {1000: 'test', 5: 'five'} |
207 WriteDataPack(data2, 'datapack2.pak', UTF8) | 223 WriteDataPack(data2, 'datapack2.pak', UTF8) |
208 print 'wrote datapack1 and datapack2 to current directory.' | 224 print 'wrote datapack1 and datapack2 to current directory.' |
209 | 225 |
210 | 226 |
211 if __name__ == '__main__': | 227 if __name__ == '__main__': |
212 main() | 228 main() |
OLD | NEW |