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'''Gatherer for administrative template files. 7''' 8 9import re 10 11from grit.gather import regexp 12from grit import exception 13from grit import lazy_re 14 15 16class MalformedAdminTemplateException(exception.Base): 17 '''This file doesn't look like a .adm file to me.''' 18 pass 19 20 21class AdmGatherer(regexp.RegexpGatherer): 22 '''Gatherer for the translateable portions of an admin template. 23 24 This gatherer currently makes the following assumptions: 25 - there is only one [strings] section and it is always the last section 26 of the file 27 - translateable strings do not need to be escaped. 28 ''' 29 30 # Finds the strings section as the group named 'strings' 31 _STRINGS_SECTION = lazy_re.compile( 32 '(?P<first_part>.+^\[strings\])(?P<strings>.+)\Z', 33 re.MULTILINE | re.DOTALL) 34 35 # Finds the translateable sections from within the [strings] section. 36 _TRANSLATEABLES = lazy_re.compile( 37 '^\s*[A-Za-z0-9_]+\s*=\s*"(?P<text>.+)"\s*$', 38 re.MULTILINE) 39 40 def Escape(self, text): 41 return text.replace('\n', '\\n') 42 43 def UnEscape(self, text): 44 return text.replace('\\n', '\n') 45 46 def Parse(self): 47 if self.have_parsed_: 48 return 49 self.have_parsed_ = True 50 51 self.text_ = self._LoadInputFile().strip() 52 m = self._STRINGS_SECTION.match(self.text_) 53 if not m: 54 raise MalformedAdminTemplateException() 55 # Add the first part, which is all nontranslateable, to the skeleton 56 self._AddNontranslateableChunk(m.group('first_part')) 57 # Then parse the rest using the _TRANSLATEABLES regexp. 58 self._RegExpParse(self._TRANSLATEABLES, m.group('strings')) 59 60 def GetTextualIds(self): 61 return [self.extkey] 62