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'''A baseclass for simple gatherers based on regular expressions.
7'''
8
9import re
10
11from grit.gather import skeleton_gatherer
12
13
14class RegexpGatherer(skeleton_gatherer.SkeletonGatherer):
15  '''Common functionality of gatherers based on parsing using a single
16  regular expression.
17  '''
18
19  DescriptionMapping_ = {
20      'CAPTION' : 'This is a caption for a dialog',
21      'CHECKBOX' : 'This is a label for a checkbox',
22      'CONTROL': 'This is the text on a control',
23      'CTEXT': 'This is a label for a control',
24      'DEFPUSHBUTTON': 'This is a button definition',
25      'GROUPBOX': 'This is a label for a grouping',
26      'ICON': 'This is a label for an icon',
27      'LTEXT': 'This is the text for a label',
28      'PUSHBUTTON': 'This is the text for a button',
29    }
30
31  # Contextualization elements. Used for adding additional information
32  # to the message bundle description string from RC files.
33  def AddDescriptionElement(self, string):
34    if self.DescriptionMapping_.has_key(string):
35      description = self.DescriptionMapping_[string]
36    else:
37      description = string
38    if self.single_message_:
39      self.single_message_.SetDescription(description)
40    else:
41      if (self.translatable_chunk_):
42        message = self.skeleton_[len(self.skeleton_) - 1].GetMessage()
43        message.SetDescription(description)
44
45  def _RegExpParse(self, regexp, text_to_parse):
46    '''An implementation of Parse() that can be used for resource sections that
47    can be parsed using a single multi-line regular expression.
48
49    All translateables must be in named groups that have names starting with
50    'text'.  All textual IDs must be in named groups that have names starting
51    with 'id'. All type definitions that can be included in the description
52    field for contextualization purposes should have a name that starts with
53    'type'.
54
55    Args:
56      regexp: re.compile('...', re.MULTILINE)
57      text_to_parse:
58    '''
59    chunk_start = 0
60    for match in regexp.finditer(text_to_parse):
61      groups = match.groupdict()
62      keys = groups.keys()
63      keys.sort()
64      self.translatable_chunk_ = False
65      for group in keys:
66        if group.startswith('id') and groups[group]:
67          self._AddTextualId(groups[group])
68        elif group.startswith('text') and groups[group]:
69          self._AddNontranslateableChunk(
70            text_to_parse[chunk_start : match.start(group)])
71          chunk_start = match.end(group)  # Next chunk will start after the match
72          self._AddTranslateableChunk(groups[group])
73        elif group.startswith('type') and groups[group]:
74          # Add the description to the skeleton_ list. This works because
75          # we are using a sort set of keys, and because we assume that the
76          # group name used for descriptions (type) will come after the "text"
77          # group in alphabetical order. We also assume that there cannot be
78          # more than one description per regular expression match.
79          self.AddDescriptionElement(groups[group])
80
81    self._AddNontranslateableChunk(text_to_parse[chunk_start:])
82
83    if self.single_message_:
84      self.skeleton_.append(self.uberclique.MakeClique(self.single_message_))
85
86