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'''The <output> and <file> elements. 7''' 8 9import os 10 11import grit.format.rc_header 12 13from grit import xtb_reader 14from grit.node import base 15 16 17class FileNode(base.Node): 18 '''A <file> element.''' 19 20 def __init__(self): 21 super(FileNode, self).__init__() 22 self.re = None 23 self.should_load_ = True 24 25 def IsTranslation(self): 26 return True 27 28 def GetLang(self): 29 return self.attrs['lang'] 30 31 def DisableLoading(self): 32 self.should_load_ = False 33 34 def MandatoryAttributes(self): 35 return ['path', 'lang'] 36 37 def RunPostSubstitutionGatherer(self, debug=False): 38 if not self.should_load_: 39 return 40 41 root = self.GetRoot() 42 defs = getattr(root, 'defines', {}) 43 target_platform = getattr(root, 'target_platform', '') 44 45 xtb_file = open(self.ToRealPath(self.GetInputPath())) 46 try: 47 lang = xtb_reader.Parse(xtb_file, 48 self.UberClique().GenerateXtbParserCallback( 49 self.attrs['lang'], debug=debug), 50 defs=defs, 51 target_platform=target_platform) 52 except: 53 print "Exception during parsing of %s" % self.GetInputPath() 54 raise 55 # Translation console uses non-standard language codes 'iw' and 'no' for 56 # Hebrew and Norwegian Bokmal instead of 'he' and 'nb' used in Chrome. 57 # Note that some Chrome's .grd still use 'no' instead of 'nb', but 'nb' is 58 # always used for generated .pak files. 59 ALTERNATIVE_LANG_CODE_MAP = { 'he': 'iw', 'nb': 'no' } 60 assert (lang == self.attrs['lang'] or 61 lang == ALTERNATIVE_LANG_CODE_MAP[self.attrs['lang']]), ( 62 'The XTB file you reference must contain messages in the language ' 63 'specified\nby the \'lang\' attribute.') 64 65 def GetInputPath(self): 66 return os.path.expandvars(self.attrs['path']) 67 68 69class OutputNode(base.Node): 70 '''An <output> element.''' 71 72 def MandatoryAttributes(self): 73 return ['filename', 'type'] 74 75 def DefaultAttributes(self): 76 return { 77 'lang' : '', # empty lang indicates all languages 78 'language_section' : 'neutral', # defines a language neutral section 79 'context' : '', 80 } 81 82 def GetType(self): 83 return self.attrs['type'] 84 85 def GetLanguage(self): 86 '''Returns the language ID, default 'en'.''' 87 return self.attrs['lang'] 88 89 def GetContext(self): 90 return self.attrs['context'] 91 92 def GetFilename(self): 93 return self.attrs['filename'] 94 95 def GetOutputFilename(self): 96 path = None 97 if hasattr(self, 'output_filename'): 98 path = self.output_filename 99 else: 100 path = self.attrs['filename'] 101 return os.path.expandvars(path) 102 103 def _IsValidChild(self, child): 104 return isinstance(child, EmitNode) 105 106class EmitNode(base.ContentNode): 107 ''' An <emit> element.''' 108 109 def DefaultAttributes(self): 110 return { 'emit_type' : 'prepend'} 111 112 def GetEmitType(self): 113 '''Returns the emit_type for this node. Default is 'append'.''' 114 return self.attrs['emit_type'] 115 116