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 import copy |
| 7 |
| 8 |
| 9 class PolicyTemplateGenerator: |
| 10 '''Generates template text for a particular platform. |
| 11 |
| 12 This class is used to traverse a JSON structure from a .json template |
| 13 definition metafile and merge GUI message string definitions that come |
| 14 from a .grd resource tree onto it. After this, it can be used to output |
| 15 this data to policy template files using TemplateWriter objects. |
| 16 ''' |
| 17 |
| 18 def _ImportMessage(self, msg_txt): |
| 19 msg_txt = msg_txt.decode('utf-8') |
| 20 # Replace the placeholder of app name. |
| 21 msg_txt = msg_txt.replace('$1', self._config['app_name']) |
| 22 msg_txt = msg_txt.replace('$2', self._config['os_name']) |
| 23 msg_txt = msg_txt.replace('$3', self._config['frame_name']) |
| 24 # Strip spaces and escape newlines. |
| 25 lines = msg_txt.split('\n') |
| 26 lines = [line.strip() for line in lines] |
| 27 return "\n".join(lines) |
| 28 |
| 29 def __init__(self, config, policy_data): |
| 30 '''Initializes this object with all the data necessary to output a |
| 31 policy template. |
| 32 |
| 33 Args: |
| 34 messages: An identifier to string dictionary of all the localized |
| 35 messages that might appear in the policy template. |
| 36 policy_definitions: The list of defined policies and groups, as |
| 37 parsed from the policy metafile. Note that this list is passed by |
| 38 reference and its contents are modified. |
| 39 See chrome/app/policy.policy_templates.json for description and |
| 40 content. |
| 41 ''' |
| 42 # List of all the policies: |
| 43 self._policy_data = copy.deepcopy(policy_data) |
| 44 # Localized messages to be inserted to the policy_definitions structure: |
| 45 self._messages = self._policy_data['messages'] |
| 46 self._config = config |
| 47 for key in self._messages.keys(): |
| 48 self._messages[key]['text'] = self._ImportMessage( |
| 49 self._messages[key]['text']) |
| 50 self._policy_definitions = self._policy_data['policy_definitions'] |
| 51 self._ProcessPolicyList(self._policy_definitions) |
| 52 |
| 53 def _ProcessSupportedOn(self, supported_on): |
| 54 '''Parses and converts the string items of the list of supported platforms |
| 55 into dictionaries. |
| 56 |
| 57 Args: |
| 58 supported_on: The list of supported platforms. E.g.: |
| 59 ['chrome.win:8-10', 'chrome_frame:10-'] |
| 60 |
| 61 Returns: |
| 62 supported_on: The list with its items converted to dictionaries. E.g.: |
| 63 [{ |
| 64 'product': 'chrome', |
| 65 'platform': 'win', |
| 66 'since_version': '8', |
| 67 'until_version': '10' |
| 68 }, { |
| 69 'product': 'chrome_frame', |
| 70 'platform': 'win', |
| 71 'since_version': '10', |
| 72 'until_version': '' |
| 73 }] |
| 74 ''' |
| 75 result = [] |
| 76 for supported_on_item in supported_on: |
| 77 product_platform_part, version_part = supported_on_item.split(':') |
| 78 if '.' in product_platform_part: |
| 79 product, platform = product_platform_part.split('.') |
| 80 if platform == '*': |
| 81 # e.g.: 'chrome.*:8-10' |
| 82 platforms = ['linux', 'mac', 'win'] |
| 83 else: |
| 84 # e.g.: 'chrome.win:-10' |
| 85 platforms = [platform] |
| 86 else: |
| 87 # e.g.: 'chrome_frame:7-' |
| 88 product = product_platform_part |
| 89 platform = { |
| 90 'chrome_os': 'chrome_os', |
| 91 'chrome_frame': 'win' |
| 92 }[product] |
| 93 platforms = [platform] |
| 94 since_version, until_version = version_part.split('-') |
| 95 result.append({ |
| 96 'product': product, |
| 97 'platforms': platforms, |
| 98 'since_version': since_version, |
| 99 'until_version': until_version |
| 100 }) |
| 101 return result |
| 102 |
| 103 def _ProcessPolicy(self, policy): |
| 104 '''Processes localized message strings in a policy or a group. |
| 105 Also breaks up the content of 'supported_on' attribute into a list. |
| 106 |
| 107 Args: |
| 108 policy: The data structure of the policy or group, that will get message |
| 109 strings here. |
| 110 ''' |
| 111 policy['desc'] = self._ImportMessage(policy['desc']) |
| 112 policy['caption'] = self._ImportMessage(policy['caption']) |
| 113 if 'label' in policy: |
| 114 policy['label'] = self._ImportMessage(policy['label']) |
| 115 |
| 116 if policy['type'] == 'group': |
| 117 self._ProcessPolicyList(policy['policies']) |
| 118 elif policy['type'] in ('string-enum', 'int-enum'): |
| 119 # Iterate through all the items of an enum-type policy, and add captions. |
| 120 for item in policy['items']: |
| 121 item['caption'] = self._ImportMessage(item['caption']) |
| 122 if policy['type'] != 'group': |
| 123 if not 'label' in policy: |
| 124 # If 'label' is not specified, then it defaults to 'caption': |
| 125 policy['label'] = policy['caption'] |
| 126 policy['supported_on'] = self._ProcessSupportedOn( |
| 127 policy['supported_on']) |
| 128 |
| 129 def _ProcessPolicyList(self, policy_list): |
| 130 '''Adds localized message strings to each item in a list of policies and |
| 131 groups. Also breaks up the content of 'supported_on' attributes into lists |
| 132 of dictionaries. |
| 133 |
| 134 Args: |
| 135 policy_list: A list of policies and groups. Message strings will be added |
| 136 for each item and to their child items, recursively. |
| 137 ''' |
| 138 for policy in policy_list: |
| 139 self._ProcessPolicy(policy) |
| 140 |
| 141 def GetTemplateText(self, template_writer): |
| 142 '''Generates the text of the template from the arguments given |
| 143 to the constructor, using a given TemplateWriter. |
| 144 |
| 145 Args: |
| 146 template_writer: An object implementing TemplateWriter. Its methods |
| 147 are called here for each item of self._policy_groups. |
| 148 |
| 149 Returns: |
| 150 The text of the generated template. |
| 151 ''' |
| 152 return template_writer.WriteTemplate(self._policy_data) |
OLD | NEW |