Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 # Generates a Java file with API keys. | |
| 8 | |
| 9 import os | |
| 10 from string import Template | |
|
agrieve
2015/10/30 00:55:13
style guide is against importing classes (here and
dvh
2015/10/30 20:56:20
Done.
agrieve
2015/10/31 02:09:37
Please fix all imports that do this (google_apk_ke
| |
| 11 import sys | |
| 12 | |
| 13 sys.path.append( | |
| 14 os.path.abspath(os.path.join(sys.path[0], '../../../google_apis'))) | |
| 15 from google_api_keys import (GetAPIKey, GetAPIKeyRemoting, GetClientID, | |
| 16 GetClientSecret) | |
| 17 | |
|
agrieve
2015/10/30 00:55:13
two lines between top-level declarations
dvh
2015/10/30 20:56:20
Done.
| |
| 18 def GetScriptName(): | |
|
agrieve
2015/10/30 00:55:13
I think this function would be better to use:
o
agrieve
2015/10/30 00:55:13
Make this private (_GetScriptName)
dvh
2015/10/30 20:56:20
It's exported for the unit test.
dvh
2015/10/30 20:56:20
constants didn't work. I tried to import it. It ha
agrieve
2015/10/31 02:09:37
You need to tweak the sys.path (not a great practi
| |
| 19 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) | |
| 20 build_index = script_components.index('build') | |
| 21 return os.sep.join(script_components[build_index:]) | |
| 22 | |
| 23 def GenerateOutput(constant_definitions): | |
| 24 template = Template(""" | |
| 25 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 26 // Use of this source code is governed by a BSD-style license that can be | |
| 27 // found in the LICENSE file. | |
| 28 | |
| 29 // This file is autogenerated by | |
| 30 // ${SCRIPT_NAME} | |
| 31 // From | |
| 32 // ${SOURCE_PATH} | |
| 33 | |
| 34 package ${PACKAGE}; | |
| 35 | |
| 36 public class ${CLASS_NAME} { | |
| 37 ${CONSTANT_ENTRIES} | |
| 38 } | |
| 39 """) | |
| 40 | |
| 41 constant_template = Template( | |
| 42 ' public static final String ${NAME} = "${VALUE}";') | |
| 43 constant_entries_string = [] | |
|
agrieve
2015/10/30 00:55:13
nit: don't call this _string since it holds a list
dvh
2015/10/30 20:56:20
Done.
| |
| 44 for constant_name, constant_value in constant_definitions.iteritems(): | |
| 45 values = { | |
| 46 'NAME': constant_name, | |
| 47 'VALUE': constant_value, | |
| 48 } | |
| 49 constant_entries_string.append(constant_template.substitute(values)) | |
| 50 constant_entries_string = '\n'.join(constant_entries_string) | |
| 51 | |
| 52 values = { | |
| 53 'CLASS_NAME': 'GoogleAPIKeys', | |
| 54 'CONSTANT_ENTRIES': constant_entries_string, | |
| 55 'PACKAGE': 'org.chromium.chrome', | |
| 56 'SCRIPT_NAME': GetScriptName(), | |
| 57 'SOURCE_PATH': 'google_api_keys/google_api_keys.h', | |
| 58 } | |
| 59 return template.substitute(values) | |
| 60 | |
| 61 def DoWriteOutput(output_path, constant_definition): | |
|
agrieve
2015/10/30 00:55:13
nit: make private
dvh
2015/10/30 20:56:20
Done.
| |
| 62 with open(output_path + os.path.sep + 'GoogleAPIKeys.java', 'w') as out_file: | |
|
agrieve
2015/10/30 00:55:13
use os.path.join() rather than os.path.sep
dvh
2015/10/30 20:56:20
Done.
| |
| 63 out_file.write(GenerateOutput(constant_definition)) | |
| 64 | |
| 65 def DoMain(argv): | |
| 66 values = {} | |
| 67 values['GOOGLE_API_KEY'] = GetAPIKey() | |
| 68 values['GOOGLE_API_KEY_REMOTING'] = GetAPIKeyRemoting() | |
| 69 values['GOOGLE_CLIENT_ID_MAIN'] = GetClientID('MAIN') | |
| 70 values['GOOGLE_CLIENT_SECRET_MAIN'] = GetClientSecret('MAIN') | |
| 71 values['GOOGLE_CLIENT_ID_CLOUD_PRINT'] = GetClientID('CLOUD_PRINT') | |
| 72 values['GOOGLE_CLIENT_SECRET_CLOUD_PRINT'] = GetClientSecret('CLOUD_PRINT') | |
| 73 values['GOOGLE_CLIENT_ID_REMOTING'] = GetClientID('REMOTING') | |
| 74 values['GOOGLE_CLIENT_SECRET_REMOTING'] = GetClientSecret('REMOTING') | |
| 75 values['GOOGLE_CLIENT_ID_REMOTING_HOST'] = GetClientID('REMOTING_HOST') | |
| 76 values['GOOGLE_CLIENT_SECRET_REMOTING_HOST'] = GetClientSecret( | |
| 77 'REMOTING_HOST') | |
| 78 values['GOOGLE_CLIENT_ID_REMOTING_IDENTITY_API'] = GetClientID( | |
| 79 'REMOTING_IDENTITY_API') | |
| 80 DoWriteOutput(argv[0], values) | |
|
agrieve
2015/10/30 00:55:13
pass this is as a --flag and use argparse so that
dvh
2015/10/30 20:56:20
Done.
| |
| 81 | |
| 82 if __name__ == '__main__': | |
| 83 DoMain(sys.argv[1:]) | |
| 84 | |
| OLD | NEW |