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..ecff70c2dfd25f544fe2cc08dc55b38875969626 |
| --- /dev/null |
| +++ b/google_apis/google_api_keys.py |
| @@ -0,0 +1,89 @@ |
| +#!/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 |
| + 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() |
|
Jamie
2012/09/17 22:28:55
I'm not sure what the call to strip accomplishes h
Jói
2012/09/17 22:46:38
strip() only strips whitespace, so this is indeed
Jamie
2012/09/17 22:58:05
My point was that lines ending with "\ " are not c
Jói
2012/09/17 23:12:47
Oh, I see, and I think you're right. The strip wa
|
| + if line.strip().endswith('\\'): |
|
Jamie
2012/09/17 22:28:55
Duplicate call to split.
Jói
2012/09/17 22:46:38
Done.
|
| + current_line += line[:-1] |
|
Jamie
2012/09/17 22:28:56
If you're going to handle multi-line defines, I ti
Jói
2012/09/17 22:46:38
Oh, right, that's true, although in practice this
Jamie
2012/09/17 22:58:05
The problem is that the error will be very hard to
Jói
2012/09/17 23:12:47
Done, although in a slightly different fashion tha
|
| + elif current_line: |
| + # Last line in multi-line #define, time to parse it. |
| + current_line += line |
| + token = ParseLine(current_line) |
| + current_line = '' |
| + else: |
|
Jamie
2012/09/17 22:28:56
Not sure you need this final "else" since current_
Jói
2012/09/17 22:46:38
I do, since it's in a loop; I don't want current_l
Jamie
2012/09/17 22:58:05
I actually meant the entire else clause can go. If
Jói
2012/09/17 23:12:47
Ah, yes, thanks for that.
|
| + 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.""" |
| + token = _GetTokenFromOfficialFile(token_name) |
| + if token: |
| + return token |
| + elif token_name in os.environ: |
|
Jamie
2012/09/17 22:28:56
I think this test would be better if it came befor
Jói
2012/09/17 22:46:38
Changed. I was trying to stay as close to the sem
|
| + return os.environ[token_name] |
| + else: |
| + return 'dummytoken' |
| + |
| + |
| +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__": |
|
Jamie
2012/09/17 22:28:56
Is this test code, or should it stay?
Jói
2012/09/17 22:46:38
It is test code, but it is a useful self test and
Jamie
2012/09/17 22:58:05
Fair enough. It will probably end up out-of-date i
|
| + 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') |