| 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 an RC file for compilation. | 6 '''Support for formatting an RC file for compilation. |
| 7 ''' | 7 ''' |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 import types | 10 import types |
| 11 import re | 11 import re |
| 12 from functools import partial | 12 from functools import partial |
| 13 | 13 |
| 14 from grit import util | 14 from grit import util |
| 15 from grit.format import rc_header |
| 15 from grit.node import misc | 16 from grit.node import misc |
| 16 | 17 |
| 17 | 18 |
| 18 def Format(root, lang='en', output_dir='.'): | 19 def Format(root, lang='en', output_dir='.'): |
| 19 from grit.node import empty, include, message, structure | 20 from grit.node import empty, include, message, structure |
| 20 | 21 |
| 21 yield _FormatHeader(root, lang, output_dir) | 22 yield _FormatHeader(root, lang, output_dir) |
| 22 | 23 |
| 23 for item in root.ActiveDescendants(): | 24 for item in root.ActiveDescendants(): |
| 24 if isinstance(item, empty.MessagesNode): | 25 if isinstance(item, empty.MessagesNode): |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 filename = item.Process(output_dir) | 442 filename = item.Process(output_dir) |
| 442 elif filename_only: | 443 elif filename_only: |
| 443 filename = os.path.basename(filename) | 444 filename = os.path.basename(filename) |
| 444 elif relative_path: | 445 elif relative_path: |
| 445 filename = util.MakeRelativePath(output_dir, filename) | 446 filename = util.MakeRelativePath(output_dir, filename) |
| 446 | 447 |
| 447 filename = filename.replace('\\', '\\\\') # escape for the RC format | 448 filename = filename.replace('\\', '\\\\') # escape for the RC format |
| 448 | 449 |
| 449 if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc(): | 450 if isinstance(item, structure.StructureNode) and item.IsExcludedFromRc(): |
| 450 return '' | 451 return '' |
| 451 else: | 452 |
| 452 return '%-18s %-18s "%s"\n' % (item.attrs['name'], type, filename) | 453 name = item.attrs['name'] |
| 454 item_id = rc_header.GetIds(item.GetRoot())[name] |
| 455 return '// ID: %d\n%-18s %-18s "%s"\n' % (item_id, name, type, filename) |
| 453 | 456 |
| 454 | 457 |
| 455 def _DoNotFormat(item, lang, output_dir): | 458 def _DoNotFormat(item, lang, output_dir): |
| 456 return '' | 459 return '' |
| 457 | 460 |
| 458 | 461 |
| 459 # Formatter instance to use for each type attribute | 462 # Formatter instance to use for each type attribute |
| 460 # when formatting Structure nodes. | 463 # when formatting Structure nodes. |
| 461 _STRUCTURE_FORMATTERS = { | 464 _STRUCTURE_FORMATTERS = { |
| 462 'accelerators' : _FormatSection, | 465 'accelerators' : _FormatSection, |
| 463 'dialog' : _FormatSection, | 466 'dialog' : _FormatSection, |
| 464 'menu' : _FormatSection, | 467 'menu' : _FormatSection, |
| 465 'rcdata' : _FormatSection, | 468 'rcdata' : _FormatSection, |
| 466 'version' : _FormatSection, | 469 'version' : _FormatSection, |
| 467 'admin_template' : partial(FormatInclude, type='ADM'), | 470 'admin_template' : partial(FormatInclude, type='ADM'), |
| 468 'chrome_html' : partial(FormatInclude, type='BINDATA', | 471 'chrome_html' : partial(FormatInclude, type='BINDATA', |
| 469 process_html=True), | 472 process_html=True), |
| 470 'chrome_scaled_image' : partial(FormatInclude, type='BINDATA'), | 473 'chrome_scaled_image' : partial(FormatInclude, type='BINDATA'), |
| 471 'igoogle' : partial(FormatInclude, type='XML'), | 474 'igoogle' : partial(FormatInclude, type='XML'), |
| 472 'muppet' : partial(FormatInclude, type='XML'), | 475 'muppet' : partial(FormatInclude, type='XML'), |
| 473 'tr_html' : partial(FormatInclude, type='HTML'), | 476 'tr_html' : partial(FormatInclude, type='HTML'), |
| 474 'txt' : partial(FormatInclude, type='TXT'), | 477 'txt' : partial(FormatInclude, type='TXT'), |
| 475 'policy_template_metafile': _DoNotFormat, | 478 'policy_template_metafile': _DoNotFormat, |
| 476 } | 479 } |
| 477 | 480 |
| 478 | 481 |
| 479 def FormatStructure(item, lang, output_dir): | 482 def FormatStructure(item, lang, output_dir): |
| 480 formatter = _STRUCTURE_FORMATTERS[item.attrs['type']] | 483 formatter = _STRUCTURE_FORMATTERS[item.attrs['type']] |
| 481 return formatter(item, lang, output_dir) | 484 return formatter(item, lang, output_dir) |
| OLD | NEW |