Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: grit/format/policy_templates/writers/json_writer.py

Issue 7994004: Initial source commit to grit-i18n project. (Closed) Base URL: http://grit-i18n.googlecode.com/svn/trunk/
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698