OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
| 6 import json |
| 7 |
6 from textwrap import TextWrapper | 8 from textwrap import TextWrapper |
7 from grit.format.policy_templates.writers import template_writer | 9 from grit.format.policy_templates.writers import template_writer |
8 | 10 |
9 | 11 |
10 TEMPLATE_HEADER="""\ | 12 TEMPLATE_HEADER="""\ |
11 // Policy template for Linux. | 13 // Policy template for Linux. |
12 // Uncomment the policies you wish to activate and change their values to | 14 // Uncomment the policies you wish to activate and change their values to |
13 // something useful for your case. The provided values are for reference only | 15 // something useful for your case. The provided values are for reference only |
14 // and do not provide meaningful defaults! | 16 // and do not provide meaningful defaults! |
15 {""" | 17 {""" |
(...skipping 15 matching lines...) Expand all Loading... |
31 '''Class for generating policy files in JSON format (for Linux). The | 33 '''Class for generating policy files in JSON format (for Linux). The |
32 generated files will define all the supported policies with example values | 34 generated files will define all the supported policies with example values |
33 set for them. This class is used by PolicyTemplateGenerator to write .json | 35 set for them. This class is used by PolicyTemplateGenerator to write .json |
34 files. | 36 files. |
35 ''' | 37 ''' |
36 | 38 |
37 def PreprocessPolicies(self, policy_list): | 39 def PreprocessPolicies(self, policy_list): |
38 return self.FlattenGroupsAndSortPolicies(policy_list) | 40 return self.FlattenGroupsAndSortPolicies(policy_list) |
39 | 41 |
40 def WritePolicy(self, policy): | 42 def WritePolicy(self, policy): |
41 example_value = policy['example_value'] | 43 if policy['type'] == 'external': |
42 if policy['type'] == 'string': | |
43 example_value_str = '"' + example_value + '"' | |
44 elif policy['type'] in ('int', 'int-enum', 'dict'): | |
45 example_value_str = str(example_value) | |
46 elif policy['type'] == 'list': | |
47 if example_value == []: | |
48 example_value_str = '[]' | |
49 else: | |
50 example_value_str = '["%s"]' % '", "'.join(example_value) | |
51 elif policy['type'] == 'main': | |
52 if example_value == True: | |
53 example_value_str = 'true' | |
54 else: | |
55 example_value_str = 'false' | |
56 elif policy['type'] == 'string-enum': | |
57 example_value_str = '"%s"' % example_value; | |
58 elif policy['type'] == 'external': | |
59 # This type can only be set through cloud policy. | 44 # This type can only be set through cloud policy. |
60 return | 45 return |
61 else: | 46 example_value_str = json.dumps(policy['example_value'], sort_keys=True) |
62 raise Exception('unknown policy type %s:' % policy['type']) | |
63 | 47 |
64 # Add comma to the end of the previous line. | 48 # Add comma to the end of the previous line. |
65 if not self._first_written: | 49 if not self._first_written: |
66 self._out[-2] += ',' | 50 self._out[-2] += ',' |
67 | 51 |
68 line = ' // %s' % policy['caption'] | 52 line = ' // %s' % policy['caption'] |
69 self._out.append(line) | 53 self._out.append(line) |
70 self._out.append(HEADER_DELIMETER) | 54 self._out.append(HEADER_DELIMETER) |
71 description = self._text_wrapper.wrap(policy['desc']) | 55 description = self._text_wrapper.wrap(policy['desc']) |
72 self._out += description; | 56 self._out += description; |
(...skipping 16 matching lines...) Expand all Loading... |
89 self._first_written = True | 73 self._first_written = True |
90 # Create the TextWrapper object once. | 74 # Create the TextWrapper object once. |
91 self._text_wrapper = TextWrapper( | 75 self._text_wrapper = TextWrapper( |
92 initial_indent = ' // ', | 76 initial_indent = ' // ', |
93 subsequent_indent = ' // ', | 77 subsequent_indent = ' // ', |
94 break_long_words = False, | 78 break_long_words = False, |
95 width = 80) | 79 width = 80) |
96 | 80 |
97 def GetTemplateText(self): | 81 def GetTemplateText(self): |
98 return '\n'.join(self._out) | 82 return '\n'.join(self._out) |
OLD | NEW |