aboutsummaryrefslogtreecommitdiff
path: root/grit/format/policy_templates/writers/plist_strings_writer.py
blob: 08fcddd5ac701916f081baa851edb1d4bb8bda23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


from grit.format.policy_templates.writers import plist_helper
from grit.format.policy_templates.writers import template_writer


def GetWriter(config):
  '''Factory method for creating PListStringsWriter objects.
  See the constructor of TemplateWriter for description of
  arguments.
  '''
  return PListStringsWriter(['mac'], config)


class PListStringsWriter(template_writer.TemplateWriter):
  '''Outputs localized string table files for the Mac policy file.
  These files are named Localizable.strings and they are in the
  [lang].lproj subdirectories of the manifest bundle.
  '''

  def _AddToStringTable(self, item_name, caption, desc):
    '''Add a title and a description of an item to the string table.

    Args:
      item_name: The name of the item that will get the title and the
        description.
      title: The text of the title to add.
      desc: The text of the description to add.
    '''
    caption = caption.replace('"', '\\"')
    caption = caption.replace('\n', '\\n')
    desc = desc.replace('"', '\\"')
    desc = desc.replace('\n', '\\n')
    self._out.append('%s.pfm_title = \"%s\";' % (item_name, caption))
    self._out.append('%s.pfm_description = \"%s\";' % (item_name, desc))

  def PreprocessPolicies(self, policy_list):
    return self.FlattenGroupsAndSortPolicies(policy_list)

  def WritePolicy(self, policy):
    '''Add strings to the stringtable corresponding a given policy.

    Args:
      policy: The policy for which the strings will be added to the
        string table.
    '''
    desc = policy['desc']
    if policy['type'] == 'external':
      # This type can only be set through cloud policy.
      return
    elif policy['type'] in ('int-enum','string-enum'):
      # Append the captions of enum items to the description string.
      item_descs = []
      for item in policy['items']:
        item_descs.append(str(item['value']) + ' - ' + item['caption'])
      desc = '\n'.join(item_descs) + '\n' + desc

    self._AddToStringTable(policy['name'], policy['label'], desc)

  def BeginTemplate(self):
    app_name = plist_helper.GetPlistFriendlyName(self.config['app_name'])
    self._AddToStringTable(
        app_name,
        self.config['app_name'],
        self.messages['mac_chrome_preferences']['text'])

  def Init(self):
    # A buffer for the lines of the string table being generated.
    self._out = []

  def GetTemplateText(self):
    return '\n'.join(self._out)