OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 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, messages): |
| 11 '''Factory method for creating JsonWriter objects. |
| 12 See the constructor of TemplateWriter for description of |
| 13 arguments. |
| 14 ''' |
| 15 return JsonWriter(['linux'], config, messages) |
| 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 WritePolicy(self, policy): |
| 26 example_value = policy['annotations']['example_value'] |
| 27 if policy['type'] == 'string': |
| 28 example_value_str = '"' + example_value + '"' |
| 29 elif policy['type'] == 'list': |
| 30 if example_value == []: |
| 31 example_value_str = '[]' |
| 32 else: |
| 33 example_value_str = '["%s"]' % '", "'.join(example_value) |
| 34 elif policy['type'] == 'main': |
| 35 if example_value == True: |
| 36 example_value_str = 'true' |
| 37 else: |
| 38 example_value_str = 'false' |
| 39 elif policy['type'] == 'enum': |
| 40 example_value_str = example_value |
| 41 else: |
| 42 raise Exception('unknown policy type %s:' % policy['type']) |
| 43 |
| 44 # Add comme to the end of the previous line. |
| 45 if not self._first_written: |
| 46 self._out[-1] += ',' |
| 47 |
| 48 line = ' "%s": %s' % (policy['name'], example_value_str) |
| 49 self._out.append(line) |
| 50 |
| 51 self._first_written = False |
| 52 |
| 53 def BeginTemplate(self): |
| 54 self._out.append('{') |
| 55 |
| 56 def EndTemplate(self): |
| 57 self._out.append('}') |
| 58 |
| 59 def Init(self): |
| 60 self._out = [] |
| 61 # The following boolean member is true until the first policy is written. |
| 62 self._first_written = True |
| 63 |
| 64 def GetTemplateText(self): |
| 65 return '\n'.join(self._out) |
OLD | NEW |