Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / plist_strings_writer.py
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
7 from grit.format.policy_templates.writers import plist_helper
8 from grit.format.policy_templates.writers import template_writer
9
10
11 def GetWriter(config):
12   '''Factory method for creating PListStringsWriter objects.
13   See the constructor of TemplateWriter for description of
14   arguments.
15   '''
16   return PListStringsWriter(['mac'], config)
17
18
19 class PListStringsWriter(template_writer.TemplateWriter):
20   '''Outputs localized string table files for the Mac policy file.
21   These files are named Localizable.strings and they are in the
22   [lang].lproj subdirectories of the manifest bundle.
23   '''
24
25   def WriteComment(self, comment):
26     self._out.append('/* ' + comment + ' */' )
27
28   def _AddToStringTable(self, item_name, caption, desc):
29     '''Add a title and a description of an item to the string table.
30
31     Args:
32       item_name: The name of the item that will get the title and the
33         description.
34       title: The text of the title to add.
35       desc: The text of the description to add.
36     '''
37     caption = caption.replace('"', '\\"')
38     caption = caption.replace('\n', '\\n')
39     desc = desc.replace('"', '\\"')
40     desc = desc.replace('\n', '\\n')
41     self._out.append('%s.pfm_title = \"%s\";' % (item_name, caption))
42     self._out.append('%s.pfm_description = \"%s\";' % (item_name, desc))
43
44   def PreprocessPolicies(self, policy_list):
45     return self.FlattenGroupsAndSortPolicies(policy_list)
46
47   def WritePolicy(self, policy):
48     '''Add strings to the stringtable corresponding a given policy.
49
50     Args:
51       policy: The policy for which the strings will be added to the
52         string table.
53     '''
54     desc = policy['desc']
55     if policy['type'] == 'external':
56       # This type can only be set through cloud policy.
57       return
58     elif policy['type'] in ('int-enum','string-enum', 'string-enum-list'):
59       # Append the captions of enum items to the description string.
60       item_descs = []
61       for item in policy['items']:
62         item_descs.append(str(item['value']) + ' - ' + item['caption'])
63       desc = '\n'.join(item_descs) + '\n' + desc
64
65     self._AddToStringTable(policy['name'], policy['label'], desc)
66
67   def BeginTemplate(self):
68     app_name = plist_helper.GetPlistFriendlyName(self.config['app_name'])
69     if self._GetChromiumVersionString() is not None:
70       self.WriteComment(self.config['build'] + ''' version: ''' + \
71           self._GetChromiumVersionString())
72     self._AddToStringTable(
73         app_name,
74         self.config['app_name'],
75         self.messages['mac_chrome_preferences']['text'])
76
77   def Init(self):
78     # A buffer for the lines of the string table being generated.
79     self._out = []
80
81   def GetTemplateText(self):
82     return '\n'.join(self._out)