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 json 8 9from grit.format.policy_templates.writers import template_writer 10 11 12def GetWriter(config): 13 '''Factory method for creating RegWriter objects. 14 See the constructor of TemplateWriter for description of 15 arguments. 16 ''' 17 return RegWriter(['win'], config) 18 19 20class RegWriter(template_writer.TemplateWriter): 21 '''Class for generating policy example files in .reg format (for Windows). 22 The generated files will define all the supported policies with example 23 values set for them. This class is used by PolicyTemplateGenerator to 24 write .reg files. 25 ''' 26 27 NEWLINE = '\r\n' 28 29 def _EscapeRegString(self, string): 30 return string.replace('\\', '\\\\').replace('\"', '\\\"') 31 32 def _StartBlock(self, key, suffix, list): 33 key = 'HKEY_LOCAL_MACHINE\\' + key 34 if suffix: 35 key = key + '\\' + suffix 36 if key != self._last_key.get(id(list), None): 37 list.append('') 38 list.append('[%s]' % key) 39 self._last_key[id(list)] = key 40 41 def PreprocessPolicies(self, policy_list): 42 return self.FlattenGroupsAndSortPolicies(policy_list, 43 self.GetPolicySortingKey) 44 45 def GetPolicySortingKey(self, policy): 46 '''Extracts a sorting key from a policy. These keys can be used for 47 list.sort() methods to sort policies. 48 See TemplateWriter.SortPoliciesGroupsFirst for usage. 49 ''' 50 is_list = policy['type'] in ('list', 'string-enum-list') 51 # Lists come after regular policies. 52 return (is_list, policy['name']) 53 54 def _WritePolicy(self, policy, key, list): 55 example_value = policy['example_value'] 56 57 if policy['type'] == 'external': 58 # This type can only be set through cloud policy. 59 return 60 elif policy['type'] in ('list', 'string-enum-list'): 61 self._StartBlock(key, policy['name'], list) 62 i = 1 63 for item in example_value: 64 escaped_str = self._EscapeRegString(item) 65 list.append('"%d"="%s"' % (i, escaped_str)) 66 i = i + 1 67 else: 68 self._StartBlock(key, None, list) 69 if policy['type'] in ('string', 'string-enum', 'dict'): 70 example_value_str = json.dumps(example_value, sort_keys=True) 71 if policy['type'] == 'dict': 72 example_value_str = '"%s"' % example_value_str 73 elif policy['type'] == 'main': 74 if example_value == True: 75 example_value_str = 'dword:00000001' 76 else: 77 example_value_str = 'dword:00000000' 78 elif policy['type'] in ('int', 'int-enum'): 79 example_value_str = 'dword:%08x' % example_value 80 else: 81 raise Exception('unknown policy type %s:' % policy['type']) 82 83 list.append('"%s"=%s' % (policy['name'], example_value_str)) 84 85 def WritePolicy(self, policy): 86 if self.CanBeMandatory(policy): 87 self._WritePolicy(policy, 88 self.config['win_reg_mandatory_key_name'], 89 self._mandatory) 90 91 def WriteRecommendedPolicy(self, policy): 92 self._WritePolicy(policy, 93 self.config['win_reg_recommended_key_name'], 94 self._recommended) 95 96 def BeginTemplate(self): 97 pass 98 99 def EndTemplate(self): 100 pass 101 102 def Init(self): 103 self._mandatory = [] 104 self._recommended = [] 105 self._last_key = {} 106 107 def GetTemplateText(self): 108 prefix = ['Windows Registry Editor Version 5.00'] 109 all = prefix + self._mandatory + self._recommended 110 return self.NEWLINE.join(all) 111