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    # We special case 'he' and 'iw' because the translation console uses 'iw'
56    # and we use 'he'.
57    assert (lang == self.attrs['lang'] or
58            (lang == 'iw' and self.attrs['lang'] == 'he')), ('The XTB file you '
59            'reference must contain messages in the language specified\n'
60            'by the \'lang\' attribute.')
61
62  def GetInputPath(self):
63    return os.path.expandvars(self.attrs['path'])
64
65
66class OutputNode(base.Node):
67  '''An <output> element.'''
68
69  def MandatoryAttributes(self):
70    return ['filename', 'type']
71
72  def DefaultAttributes(self):
73    return {
74      'lang' : '', # empty lang indicates all languages
75      'language_section' : 'neutral', # defines a language neutral section
76      'context' : '',
77    }
78
79  def GetType(self):
80    return self.attrs['type']
81
82  def GetLanguage(self):
83    '''Returns the language ID, default 'en'.'''
84    return self.attrs['lang']
85
86  def GetContext(self):
87    return self.attrs['context']
88
89  def GetFilename(self):
90    return self.attrs['filename']
91
92  def GetOutputFilename(self):
93    path = None
94    if hasattr(self, 'output_filename'):
95      path = self.output_filename
96    else:
97      path = self.attrs['filename']
98    return os.path.expandvars(path)
99
100  def _IsValidChild(self, child):
101    return isinstance(child, EmitNode)
102
103class EmitNode(base.ContentNode):
104  ''' An <emit> element.'''
105
106  def DefaultAttributes(self):
107    return { 'emit_type' : 'prepend'}
108
109  def GetEmitType(self):
110    '''Returns the emit_type for this node. Default is 'append'.'''
111    return self.attrs['emit_type']
112
113