1#!/usr/bin/env python 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 4# found in the LICENSE file. 5 6 7import sys 8from functools import partial 9 10from grit.format.policy_templates import policy_template_generator 11from grit.format.policy_templates import writer_configuration 12from grit.node import misc 13from grit.node import structure 14 15 16def GetFormatter(type): 17 return partial(_TemplateFormatter, 18 'grit.format.policy_templates.writers.%s_writer' % type) 19 20 21def _TemplateFormatter(writer_module_name, root, lang, output_dir): 22 '''Creates a template file corresponding to an <output> node of the grit 23 tree. 24 25 More precisely, processes the whole grit tree for a given <output> node whose 26 type is one of adm, plist, plist_strings, admx, adml, doc, json, reg. 27 The result of processing is a policy template file with the given type and 28 language of the <output> node. This function does the interfacing with 29 grit, but the actual template-generating work is done in 30 policy_template_generator.PolicyTemplateGenerator. 31 32 Args: 33 writer_name: A string identifying the TemplateWriter subclass used 34 for generating the output. 35 root: the <grit> root node of the grit tree. 36 lang: the language of outputted text, e.g.: 'en' 37 output_dir: The output directory, currently unused here. 38 39 Yields the text of the template file. 40 ''' 41 __import__(writer_module_name) 42 writer_module = sys.modules[writer_module_name] 43 config = writer_configuration.GetConfigurationForBuild(root.defines) 44 policy_data = _ParseGritNodes(root, lang) 45 policy_generator = \ 46 policy_template_generator.PolicyTemplateGenerator(config, policy_data) 47 writer = writer_module.GetWriter(config) 48 yield policy_generator.GetTemplateText(writer) 49 50 51def _ParseGritNodes(root, lang): 52 '''Collects the necessary information from the grit tree: 53 the message strings and the policy definitions. 54 55 Args: 56 root: The root of the grit tree. 57 lang: the language of outputted text, e.g.: 'en' 58 59 Returns: 60 Policy data. 61 ''' 62 policy_data = None 63 for item in root.ActiveDescendants(): 64 with item: 65 if (isinstance(item, structure.StructureNode) and 66 item.attrs['type'] == 'policy_template_metafile'): 67 assert policy_data is None 68 json_text = item.gatherer.Translate( 69 lang, 70 pseudo_if_not_available=item.PseudoIsAllowed(), 71 fallback_to_english=item.ShouldFallbackToEnglish()) 72 policy_data = eval(json_text) 73 return policy_data 74