Chromium Code Reviews| Index: chrome/tools/build/generate_policy_source.py |
| diff --git a/chrome/tools/build/generate_policy_source.py b/chrome/tools/build/generate_policy_source.py |
| index 47d7ad2f41bbc890eee43374490632908720ecfe..5426c06a17799f2e321a1403714d0e2883fad9dd 100755 |
| --- a/chrome/tools/build/generate_policy_source.py |
| +++ b/chrome/tools/build/generate_policy_source.py |
| @@ -11,6 +11,7 @@ chromium_os_flag should be 1 if this is a Chromium OS build |
| template is the path to a .json policy template file.''' |
| from __future__ import with_statement |
| +from collections import namedtuple |
| from optparse import OptionParser |
| import re |
| import sys |
| @@ -32,6 +33,10 @@ TYPE_MAP = { |
| 'string-enum': 'TYPE_STRING', |
| } |
| +PolicyDetails = namedtuple( |
| + 'PolicyDetails', |
| + 'id name vtype platforms is_deprecated is_device_policy') |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
this is nice!
|
| + |
| def main(): |
| parser = OptionParser(usage=__doc__) |
| @@ -107,8 +112,7 @@ def _RemovePlaceholders(text): |
| return result; |
| -# Returns a tuple with details about the given policy: |
| -# (name, type, list_of_platforms, is_deprecated, is_device_policy) |
| +# Returns a PolicyDetails named tuple with details about the given policy. |
| def _GetPolicyDetails(policy): |
| name = policy['name'] |
| if not TYPE_MAP.has_key(policy['type']): |
| @@ -119,7 +123,10 @@ def _GetPolicyDetails(policy): |
| platforms = [ x.split(':')[0] for x in policy['supported_on'] ] |
| is_deprecated = policy.get('deprecated', False) |
| is_device_policy = policy.get('device_only', False) |
| - return (name, vtype, platforms, is_deprecated, is_device_policy) |
| + return PolicyDetails(name=name, vtype=vtype, platforms=platforms, |
| + is_deprecated=is_deprecated, id=policy['id'], |
| + is_device_policy=is_device_policy) |
| + |
| def _GetPolicyList(template_file_contents): |
| policies = [] |
| @@ -135,21 +142,6 @@ def _GetPolicyList(template_file_contents): |
| return policies |
| -def _GetPolicyNameList(template_file_contents): |
| - return [name for (name, _, _, _, _) in _GetPolicyList(template_file_contents)] |
| - |
| -def _GetChromePolicyList(template_file_contents): |
| - return [(name, platforms, vtype, is_device_policy) |
| - for (name, vtype, platforms, _, is_device_policy) |
| - in _GetPolicyList(template_file_contents)] |
| - |
| - |
| -def _GetDeprecatedPolicyList(template_file_contents): |
| - return [name for (name, _, _, is_deprecated, _) |
| - in _GetPolicyList(template_file_contents) |
| - if is_deprecated] |
| - |
| - |
| def _LoadJSONFile(json_file): |
| with open(json_file, 'r') as f: |
| text = f.read() |
| @@ -186,6 +178,7 @@ def _WritePolicyConstantHeader(template_file_contents, args, opts): |
| ' const char* name;\n' |
| ' base::Value::Type value_type;\n' |
| ' bool device_policy;\n' |
| + ' int id;\n' |
| ' };\n' |
| '\n' |
| ' const Entry* begin;\n' |
| @@ -199,8 +192,8 @@ def _WritePolicyConstantHeader(template_file_contents, args, opts): |
| 'const PolicyDefinitionList* GetChromePolicyDefinitionList();\n\n') |
| f.write('// Key names for the policy settings.\n' |
| 'namespace key {\n\n') |
| - for policy_name in _GetPolicyNameList(template_file_contents): |
| - f.write('extern const char k' + policy_name + '[];\n') |
| + for policy in _GetPolicyList(template_file_contents): |
| + f.write('extern const char k' + policy.name + '[];\n') |
| f.write('\n} // namespace key\n\n' |
| '} // namespace policy\n\n' |
| '#endif // CHROME_COMMON_POLICY_CONSTANTS_H_\n') |
| @@ -229,11 +222,14 @@ def _WritePolicyConstantSource(template_file_contents, args, opts): |
| f.write('namespace {\n\n') |
| f.write('const PolicyDefinitionList::Entry kEntries[] = {\n') |
| - policy_list = _GetChromePolicyList(template_file_contents) |
| - for (name, platforms, vtype, device_policy) in policy_list: |
| - if (platform in platforms) or (platform_wildcard in platforms): |
| - f.write(' { key::k%s, Value::%s, %s },\n' % |
| - (name, vtype, 'true' if device_policy else 'false')) |
| + for policy in _GetPolicyList(template_file_contents): |
| + if (platform in policy.platforms) or \ |
| + (platform_wildcard in policy.platforms): |
| + f.write( |
| + ' {{ key::k{name}, Value::{vtype}, {is_device_policy}, {id} }},\n'. |
|
Mattias Nissler (ping if slow)
2012/09/12 13:06:27
I think the previous code was easier to read.
qfel
2012/09/18 11:30:56
Done.
|
| + format(name=policy.name, vtype=policy.vtype, id=policy.id, |
| + is_device_policy='true' if policy.is_device_policy |
| + else 'false')) |
| f.write('};\n\n') |
| f.write('const PolicyDefinitionList kChromePolicyList = {\n' |
| @@ -243,8 +239,9 @@ def _WritePolicyConstantSource(template_file_contents, args, opts): |
| f.write('// List of deprecated policies.\n' |
| 'const char* kDeprecatedPolicyList[] = {\n') |
| - for name in _GetDeprecatedPolicyList(template_file_contents): |
| - f.write(' key::k%s,\n' % name) |
| + for policy in _GetPolicyList(template_file_contents): |
| + if policy.is_deprecated: |
| + f.write(' key::k%s,\n' % policy.name) |
| f.write('};\n\n') |
| f.write('} // namespace\n\n') |
| @@ -276,8 +273,8 @@ def _WritePolicyConstantSource(template_file_contents, args, opts): |
| '}\n\n') |
| f.write('namespace key {\n\n') |
| - for policy_name in _GetPolicyNameList(template_file_contents): |
| - f.write('const char k%s[] = "%s";\n' % (policy_name, policy_name)) |
| + for policy in _GetPolicyList(template_file_contents): |
| + f.write('const char k{name}[] = "{name}";\n'.format(name=policy.name)) |
| f.write('\n} // namespace key\n\n' |
| '} // namespace policy\n') |