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