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 '''Gatherer for administrative template files. |
| 7 ''' |
| 8 |
| 9 import re |
| 10 import types |
| 11 |
| 12 from grit.gather import regexp |
| 13 from grit import exception |
| 14 from grit import tclib |
| 15 from grit import util |
| 16 |
| 17 |
| 18 class MalformedAdminTemplateException(exception.Base): |
| 19 '''This file doesn't look like a .adm file to me.''' |
| 20 def __init__(self, msg=''): |
| 21 exception.Base.__init__(self, msg) |
| 22 |
| 23 |
| 24 class AdmGatherer(regexp.RegexpGatherer): |
| 25 '''Gatherer for the translateable portions of an admin template. |
| 26 |
| 27 This gatherer currently makes the following assumptions: |
| 28 - there is only one [strings] section and it is always the last section |
| 29 of the file |
| 30 - translateable strings do not need to be escaped. |
| 31 ''' |
| 32 |
| 33 # Finds the strings section as the group named 'strings' |
| 34 _STRINGS_SECTION = re.compile('(?P<first_part>.+^\[strings\])(?P<strings>.+)\Z
', |
| 35 re.MULTILINE | re.DOTALL) |
| 36 |
| 37 # Finds the translateable sections from within the [strings] section. |
| 38 _TRANSLATEABLES = re.compile('^\s*[A-Za-z0-9_]+\s*=\s*"(?P<text>.+)"\s*$', |
| 39 re.MULTILINE) |
| 40 |
| 41 def __init__(self, text): |
| 42 regexp.RegexpGatherer.__init__(self, text) |
| 43 |
| 44 def Escape(self, text): |
| 45 return text.replace('\n', '\\n') |
| 46 |
| 47 def UnEscape(self, text): |
| 48 return text.replace('\\n', '\n') |
| 49 |
| 50 def Parse(self): |
| 51 if self.have_parsed_: |
| 52 return |
| 53 m = self._STRINGS_SECTION.match(self.text_) |
| 54 if not m: |
| 55 raise MalformedAdminTemplateException() |
| 56 # Add the first part, which is all nontranslateable, to the skeleton |
| 57 self._AddNontranslateableChunk(m.group('first_part')) |
| 58 # Then parse the rest using the _TRANSLATEABLES regexp. |
| 59 self._RegExpParse(self._TRANSLATEABLES, m.group('strings')) |
| 60 |
| 61 # static method |
| 62 def FromFile(adm_file, ext_key=None, encoding='cp1252'): |
| 63 '''Loads the contents of 'adm_file' in encoding 'encoding' and creates |
| 64 an AdmGatherer instance that gathers from those contents. |
| 65 |
| 66 The 'ext_key' parameter is ignored. |
| 67 |
| 68 Args: |
| 69 adm_file: file('bingo.rc') | 'filename.rc' |
| 70 encoding: 'utf-8' |
| 71 |
| 72 Return: |
| 73 AdmGatherer(contents_of_file) |
| 74 ''' |
| 75 if isinstance(adm_file, types.StringTypes): |
| 76 adm_file = util.WrapInputStream(file(adm_file, 'r'), encoding) |
| 77 return AdmGatherer(adm_file.read()) |
| 78 FromFile = staticmethod(FromFile) |
| 79 |
OLD | NEW |