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

Side by Side Diff: grit/format/policy_templates/template_formatter.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, 3 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 import os
7 import sys
8 import types
9
10 from grit.format import interface
11 from grit.format.policy_templates import policy_template_generator
12 from grit.format.policy_templates import writer_configuration
13 from grit.node import structure
14 from grit.node import message
15 from grit.node import misc
16
17
18 class TemplateFormatter(interface.ItemFormatter):
19 '''Creates a template file corresponding to an <output> node of the grit
20 tree.
21
22 More precisely, processes the whole grit tree for a given <output> node whose
23 type is 'adm'. TODO(gfeher) add new types here
24 The result of processing is a policy template file with the
25 given type and language of the <output> node. A new instance of this class
26 is created by grit.misc.GritNode for each <output> node. This class does
27 the interfacing with grit, but the actual template-generating work is done in
28 policy_template_generator.PolicyTemplateGenerator.
29 '''
30
31 def __init__(self, writer_name):
32 '''Initializes this formatter to output messages with a given writer.
33
34 Args:
35 writer_name: A string identifying the TemplateWriter subclass used
36 for generating the output. If writer name is 'adm', then the class
37 from module 'writers.adm_writer' will be used.
38 '''
39 super(type(self), self).__init__()
40 writer_module_name = \
41 'grit.format.policy_templates.writers.' + writer_name + '_writer'
42 __import__(writer_module_name)
43 # The module that contains the writer class:
44 self._writer_module = sys.modules[writer_module_name]
45
46 def Format(self, item, lang='en', begin_item=True, output_dir='.'):
47 '''Generates a template corresponding to an <output> node in the grd file.
48
49 Args:
50 item: the <grit> root node of the grit tree.
51 lang: the language of outputted text, e.g.: 'en'
52 begin_item: True or False, depending on if this function was called at
53 the beginning or at the end of the item.
54 output_dir: The output directory, currently unused here.
55
56 Returns:
57 The text of the template file.
58 '''
59 if not begin_item:
60 return ''
61
62 self._lang = lang
63 self._config = writer_configuration.GetConfigurationForBuild(item.defines)
64 self._policy_data = None
65 self._messages = {}
66 self._ParseGritNodes(item)
67 return self._GetOutput()
68
69 def _GetOutput(self):
70 '''Generates a template file using the instance variables initialized
71 in Format() using the writer specified in __init__().
72
73 Returns:
74 The text of the policy template based on the parameters passed
75 to __init__() and Format().
76 '''
77 policy_generator = policy_template_generator.PolicyTemplateGenerator(
78 self._config,
79 self._policy_data)
80 writer = self._writer_module.GetWriter(self._config)
81 str = policy_generator.GetTemplateText(writer)
82 return str
83
84 def _ParseGritNodes(self, item):
85 '''Collects the necessary information from the grit tree:
86 the message strings and the policy definitions.
87
88 Args:
89 item: The grit node parsed currently.
90 '''
91 nodes = []
92 if (isinstance(item, misc.IfNode) and not item.IsConditionSatisfied()):
93 return
94 if (isinstance(item, structure.StructureNode) and
95 item.attrs['type'] == 'policy_template_metafile'):
96 assert self._policy_data == None
97 json_text = item.gatherer.Translate(
98 self._lang,
99 pseudo_if_not_available=item.PseudoIsAllowed(),
100 fallback_to_english=item.ShouldFallbackToEnglish())
101 self._policy_data = eval(json_text)
102 for child in item.children:
103 self._ParseGritNodes(child)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698