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.
8 from textwrap import TextWrapper
9 from grit.format.policy_templates.writers import template_writer
13 // Policy template for Linux.
14 // Uncomment the policies you wish to activate and change their values to
15 // something useful for your case. The provided values are for reference only
16 // and do not provide meaningful defaults!
21 //-------------------------------------------------------------------------"""
24 def GetWriter(config):
25 '''Factory method for creating JsonWriter objects.
26 See the constructor of TemplateWriter for description of
29 return JsonWriter(['linux'], config)
32 class JsonWriter(template_writer.TemplateWriter):
33 '''Class for generating policy files in JSON format (for Linux). The
34 generated files will define all the supported policies with example values
35 set for them. This class is used by PolicyTemplateGenerator to write .json
39 def PreprocessPolicies(self, policy_list):
40 return self.FlattenGroupsAndSortPolicies(policy_list)
42 def WriteComment(self, comment):
43 self._out.append('// ' + comment)
45 def WritePolicy(self, policy):
46 if policy['type'] == 'external':
47 # This type can only be set through cloud policy.
49 example_value_str = json.dumps(policy['example_value'], sort_keys=True)
51 # Add comma to the end of the previous line.
52 if not self._first_written:
55 if not self.CanBeMandatory(policy) and self.CanBeRecommended(policy):
56 line = ' // Note: this policy is supported only in recommended mode.'
57 self._out.append(line)
58 line = ' // The JSON file should be placed in %srecommended.' % \
59 self.config['linux_policy_path']
60 self._out.append(line)
62 line = ' // %s' % policy['caption']
63 self._out.append(line)
64 self._out.append(HEADER_DELIMETER)
65 description = self._text_wrapper.wrap(policy['desc'])
66 self._out += description;
67 line = ' //"%s": %s' % (policy['name'], example_value_str)
69 self._out.append(line)
72 self._first_written = False
74 def BeginTemplate(self):
75 if self._GetChromiumVersionString() is not None:
76 self.WriteComment(self.config['build'] + ''' version: ''' + \
77 self._GetChromiumVersionString())
78 self._out.append(TEMPLATE_HEADER)
80 def EndTemplate(self):
85 # The following boolean member is true until the first policy is written.
86 self._first_written = True
87 # Create the TextWrapper object once.
88 self._text_wrapper = TextWrapper(
89 initial_indent = ' // ',
90 subsequent_indent = ' // ',
91 break_long_words = False,
94 def GetTemplateText(self):
95 return '\n'.join(self._out)