• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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
4# found in the LICENSE file.
5
6''' Toolbar postprocessing class. Modifies the previously processed GRD tree
7by creating separate message groups for each of the IDS_COMMAND macros.
8Also adds some identifiers nodes to declare specific ids to be included
9in the generated grh file.
10'''
11
12import postprocess_interface
13from grit import lazy_re
14import grit.node.empty
15from grit.node import misc
16
17class ToolbarPostProcessor(postprocess_interface.PostProcessor):
18  ''' Defines message groups within the grd file for each of the
19  IDS_COMMAND stuff.
20  '''
21
22  _IDS_COMMAND = lazy_re.compile(r'IDS_COMMAND_')
23  _GRAB_PARAMETERS = lazy_re.compile(
24      r'(IDS_COMMAND_[a-zA-Z0-9]+)_([a-zA-z0-9]+)')
25
26  def Process(self, rctext, rcpath, grdnode):
27    ''' Processes the data in rctext and grdnode.
28    Args:
29      rctext: string containing the contents of the RC file being processed.
30      rcpath: the path used to access the file.
31      grdnode: the root node of the grd xml data generated by
32      the rc2grd tool.
33
34    Return:
35      The root node of the processed GRD tree.
36    '''
37
38    release = grdnode.children[2]
39    messages = release.children[2]
40
41    identifiers = grit.node.empty.IdentifiersNode()
42    identifiers.StartParsing('identifiers', release)
43    identifiers.EndParsing()
44    release.AddChild(identifiers)
45
46
47    #
48    # Turn the IDS_COMMAND messages into separate message groups
49    # with ids that are offsetted to the message group's first id
50    #
51    previous_name_attr = ''
52    previous_prefix = ''
53    previous_node = ''
54    new_messages_node = self.ConstructNewMessages(release)
55    for node in messages.children[:]:
56      name_attr = node.attrs['name']
57      if self._IDS_COMMAND.search(name_attr):
58        mo = self._GRAB_PARAMETERS.search(name_attr)
59        mp = self._GRAB_PARAMETERS.search(previous_name_attr)
60        if mo and mp:
61          prefix = mo.group(1)
62          previous_prefix = mp.group(1)
63          new_message_id = mp.group(2)
64          if prefix == previous_prefix:
65            messages.RemoveChild(previous_name_attr)
66            previous_node.attrs['offset'] = 'PCI_' + new_message_id
67            del previous_node.attrs['name']
68            new_messages_node.AddChild(previous_node)
69          else:
70            messages.RemoveChild(previous_name_attr)
71            previous_node.attrs['offset'] = 'PCI_' + new_message_id
72            del previous_node.attrs['name']
73            new_messages_node.AddChild(previous_node)
74            new_messages_node.attrs['first_id'] = previous_prefix
75            new_messages_node = self.ConstructNewMessages(release)
76        else:
77          if self._IDS_COMMAND.search(previous_name_attr):
78            messages.RemoveChild(previous_name_attr)
79            previous_prefix = mp.group(1)
80            new_message_id = mp.group(2)
81            previous_node.attrs['offset'] = 'PCI_' + new_message_id
82            del previous_node.attrs['name']
83            new_messages_node.AddChild(previous_node)
84            new_messages_node.attrs['first_id'] = previous_prefix
85            new_messages_node = self.ConstructNewMessages(release)
86      else:
87        if self._IDS_COMMAND.search(previous_name_attr):
88          messages.RemoveChild(previous_name_attr)
89          mp = self._GRAB_PARAMETERS.search(previous_name_attr)
90          previous_prefix = mp.group(1)
91          new_message_id = mp.group(2)
92          previous_node.attrs['offset'] = 'PCI_' + new_message_id
93          del previous_node.attrs['name']
94          new_messages_node.AddChild(previous_node)
95          new_messages_node.attrs['first_id'] = previous_prefix
96          new_messages_node = self.ConstructNewMessages(release)
97      previous_name_attr = name_attr
98      previous_node = node
99
100
101    self.AddIdentifiers(rctext, identifiers)
102    return grdnode
103
104  def ConstructNewMessages(self, parent):
105    new_node = grit.node.empty.MessagesNode()
106    new_node.StartParsing('messages', parent)
107    new_node.EndParsing()
108    parent.AddChild(new_node)
109    return new_node
110
111  def AddIdentifiers(self, rctext, node):
112    node.AddChild(misc.IdentifierNode.Construct(node, 'IDS_COMMAND_gcFirst', '12000', ''))
113    node.AddChild(misc.IdentifierNode.Construct(node,
114                                                'IDS_COMMAND_PCI_SPACE', '16', ''))
115    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_BUTTON', '0', ''))
116    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_MENU', '1', ''))
117    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP', '2', ''))
118    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_OPTIONS_TEXT', '3', ''))
119    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP_DISABLED', '4', ''))
120    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP_MENU', '5', ''))
121    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP_MENU_DISABLED', '6', ''))
122    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP_OPTIONS', '7', ''))
123    node.AddChild(misc.IdentifierNode.Construct(node, 'PCI_TIP_OPTIONS_DISABLED', '8', ''))
124    node.AddChild(misc.IdentifierNode.Construct(node,
125                                                'PCI_TIP_DISABLED_BY_POLICY', '9', ''))
126
127