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
7from grit.format.policy_templates.writers import plist_helper
8from grit.format.policy_templates.writers import template_writer
9
10
11def 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
19class 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 _AddToStringTable(self, item_name, caption, desc):
26    '''Add a title and a description of an item to the string table.
27
28    Args:
29      item_name: The name of the item that will get the title and the
30        description.
31      title: The text of the title to add.
32      desc: The text of the description to add.
33    '''
34    caption = caption.replace('"', '\\"')
35    caption = caption.replace('\n', '\\n')
36    desc = desc.replace('"', '\\"')
37    desc = desc.replace('\n', '\\n')
38    self._out.append('%s.pfm_title = \"%s\";' % (item_name, caption))
39    self._out.append('%s.pfm_description = \"%s\";' % (item_name, desc))
40
41  def PreprocessPolicies(self, policy_list):
42    return self.FlattenGroupsAndSortPolicies(policy_list)
43
44  def WritePolicy(self, policy):
45    '''Add strings to the stringtable corresponding a given policy.
46
47    Args:
48      policy: The policy for which the strings will be added to the
49        string table.
50    '''
51    desc = policy['desc']
52    if policy['type'] == 'external':
53      # This type can only be set through cloud policy.
54      return
55    elif policy['type'] in ('int-enum','string-enum', 'string-enum-list'):
56      # Append the captions of enum items to the description string.
57      item_descs = []
58      for item in policy['items']:
59        item_descs.append(str(item['value']) + ' - ' + item['caption'])
60      desc = '\n'.join(item_descs) + '\n' + desc
61
62    self._AddToStringTable(policy['name'], policy['label'], desc)
63
64  def BeginTemplate(self):
65    app_name = plist_helper.GetPlistFriendlyName(self.config['app_name'])
66    self._AddToStringTable(
67        app_name,
68        self.config['app_name'],
69        self.messages['mac_chrome_preferences']['text'])
70
71  def Init(self):
72    # A buffer for the lines of the string table being generated.
73    self._out = []
74
75  def GetTemplateText(self):
76    return '\n'.join(self._out)
77