Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / grit / grit / format / policy_templates / writers / json_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 import json
7
8 from textwrap import TextWrapper
9 from grit.format.policy_templates.writers import template_writer
10
11
12 TEMPLATE_HEADER="""\
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!
17 {"""
18
19
20 HEADER_DELIMETER="""\
21   //-------------------------------------------------------------------------"""
22
23
24 def GetWriter(config):
25   '''Factory method for creating JsonWriter objects.
26   See the constructor of TemplateWriter for description of
27   arguments.
28   '''
29   return JsonWriter(['linux'], config)
30
31
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
36   files.
37   '''
38
39   def PreprocessPolicies(self, policy_list):
40     return self.FlattenGroupsAndSortPolicies(policy_list)
41
42   def WriteComment(self, comment):
43     self._out.append('// ' + comment)
44
45   def WritePolicy(self, policy):
46     if policy['type'] == 'external':
47       # This type can only be set through cloud policy.
48       return
49     example_value_str = json.dumps(policy['example_value'], sort_keys=True)
50
51     # Add comma to the end of the previous line.
52     if not self._first_written:
53       self._out[-2] += ','
54
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)
61
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)
68     self._out.append('')
69     self._out.append(line)
70     self._out.append('')
71
72     self._first_written = False
73
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)
79
80   def EndTemplate(self):
81     self._out.append('}')
82
83   def Init(self):
84     self._out = []
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,
92         width = 80)
93
94   def GetTemplateText(self):
95     return '\n'.join(self._out)