Chromium Code Reviews| Index: google_apis/google_api_keys.py |
| diff --git a/google_apis/google_api_keys.py b/google_apis/google_api_keys.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..e3fd94c059fc05c3cfc5b429294eddc94e9121bd |
| --- /dev/null |
| +++ b/google_apis/google_api_keys.py |
| @@ -0,0 +1,91 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Python API for retrieving API keys. |
| + |
| +Note that this cannot have the exact same semantics (at the moment) as |
| +the C++ API in google_api_keys.h, since it does not have access to gyp |
| +variables or preprocessor defines. |
| + |
| +TODO(joi): Give this have the same semantics as the C++ API. |
| +""" |
| + |
| +import os |
| +import re |
| +import sys |
| + |
| + |
| +def _GetTokenFromOfficialFile(token_name): |
| + """Parses the token from the official file if it exists, else returns None.""" |
| + official_path = os.path.join(sys.path[0], |
| + 'internal/google_chrome_api_keys.h') |
| + if not os.path.isfile(official_path): |
| + return None |
| + |
| + line_regexp = '^#define\s*%s\s*"([^"]+)"\s*$' % token_name |
|
Jamie
2012/09/17 22:58:05
FYI (I don't think it matters), but if the string
Jói
2012/09/17 23:21:52
Fixed.
|
| + line_pattern = re.compile(line_regexp) |
| + def ParseLine(current_line): |
| + result = line_pattern.match(current_line) |
| + if result: |
| + return result.group(1) |
| + else: |
| + return None |
| + |
| + with open(official_path) as f: |
| + current_line = '' |
| + for line in f: |
| + line = line.strip() |
| + if line.endswith('\\'): |
| + current_line += line[:-1] |
| + elif current_line: |
| + # Last line in multi-line #define, time to parse it. |
| + # TODO(joi): Handle combining multi-line quoted strings. |
| + current_line += line |
| + token = ParseLine(current_line) |
| + current_line = '' |
| + else: |
| + current_line = line |
| + token = ParseLine(current_line) |
| + current_line = '' |
| + if token: |
| + return token |
| + return None |
| + |
| + |
| +def _GetToken(token_name): |
| + """Returns the API token with the given name, or dummytoken by default.""" |
| + if token_name in os.environ: |
| + return os.environ[token_name] |
| + |
|
Jamie
2012/09/17 22:58:05
Nit: I don't think this blank line adds to readabi
Jói
2012/09/17 23:21:52
Done.
|
| + token = _GetTokenFromOfficialFile(token_name) |
| + if token: |
| + return token |
| + else: |
| + return 'dummytoken' |
|
Jamie
2012/09/17 23:06:41
Could this string be exposed via a constant so tha
Jói
2012/09/17 23:21:52
Done.
|
| + |
| + |
| +def GetAPIKey(): |
| + """Returns the simple API key.""" |
| + return _GetToken('GOOGLE_API_KEY') |
| + |
| + |
| +def GetClientID(client_name): |
| + """Returns the OAuth 2.0 client ID for the client of the given name.""" |
| + return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name) |
| + |
| + |
| +def GetClientSecret(client_name): |
| + """Returns the OAuth 2.0 client secret for the client of the given name.""" |
| + return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name) |
| + |
| + |
| +if __name__ == "__main__": |
| + print 'GOOGLE_API_KEY=%s' % GetAPIKey() |
| + print 'GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN') |
| + print 'GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN') |
| + print 'GOOGLE_CLIENT_ID_CLOUD_PRINT=%s' % GetClientID('CLOUD_PRINT') |
| + print 'GOOGLE_CLIENT_SECRET_CLOUD_PRINT=%s' % GetClientSecret('CLOUD_PRINT') |
| + print 'GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING') |
| + print 'GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING') |