OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 |
| 6 from xml.dom import minidom |
| 7 from grit.format.policy_templates.writers import template_writer |
| 8 |
| 9 |
| 10 def GetWriter(config): |
| 11 '''Factory method for creating JsonWriter objects. |
| 12 See the constructor of TemplateWriter for description of |
| 13 arguments. |
| 14 ''' |
| 15 return JsonWriter(['linux'], config) |
| 16 |
| 17 |
| 18 class JsonWriter(template_writer.TemplateWriter): |
| 19 '''Class for generating policy files in JSON format (for Linux). The |
| 20 generated files will define all the supported policies with example values |
| 21 set for them. This class is used by PolicyTemplateGenerator to write .json |
| 22 files. |
| 23 ''' |
| 24 |
| 25 def PreprocessPolicies(self, policy_list): |
| 26 return self.FlattenGroupsAndSortPolicies(policy_list) |
| 27 |
| 28 def WritePolicy(self, policy): |
| 29 example_value = policy['example_value'] |
| 30 if policy['type'] == 'string': |
| 31 example_value_str = '"' + example_value + '"' |
| 32 elif policy['type'] == 'int': |
| 33 example_value_str = str(example_value) |
| 34 elif policy['type'] == 'list': |
| 35 if example_value == []: |
| 36 example_value_str = '[]' |
| 37 else: |
| 38 example_value_str = '["%s"]' % '", "'.join(example_value) |
| 39 elif policy['type'] == 'main': |
| 40 if example_value == True: |
| 41 example_value_str = 'true' |
| 42 else: |
| 43 example_value_str = 'false' |
| 44 elif policy['type'] == 'string-enum': |
| 45 example_value_str = '"%s"' % example_value; |
| 46 elif policy['type'] == 'int-enum': |
| 47 example_value_str = str(example_value) |
| 48 else: |
| 49 raise Exception('unknown policy type %s:' % policy['type']) |
| 50 |
| 51 # Add comme to the end of the previous line. |
| 52 if not self._first_written: |
| 53 self._out[-1] += ',' |
| 54 |
| 55 line = ' "%s": %s' % (policy['name'], example_value_str) |
| 56 self._out.append(line) |
| 57 |
| 58 self._first_written = False |
| 59 |
| 60 def BeginTemplate(self): |
| 61 self._out.append('{') |
| 62 |
| 63 def EndTemplate(self): |
| 64 self._out.append('}') |
| 65 |
| 66 def Init(self): |
| 67 self._out = [] |
| 68 # The following boolean member is true until the first policy is written. |
| 69 self._first_written = True |
| 70 |
| 71 def GetTemplateText(self): |
| 72 return '\n'.join(self._out) |
OLD | NEW |