Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 """Python API for retrieving API keys. | |
| 7 | |
| 8 Note that this cannot have the exact same semantics (at the moment) as | |
| 9 the C++ API in google_api_keys.h, since it does not have access to gyp | |
| 10 variables or preprocessor defines. | |
| 11 | |
| 12 TODO(joi): Give this have the same semantics as the C++ API. | |
| 13 """ | |
| 14 | |
| 15 import os | |
| 16 import re | |
| 17 import sys | |
| 18 | |
| 19 | |
| 20 def _GetTokenFromOfficialFile(token_name): | |
| 21 """Parses the token from the official file if it exists, else returns None.""" | |
| 22 official_path = os.path.join(sys.path[0], | |
| 23 'internal/google_chrome_api_keys.h') | |
| 24 if not os.path.isfile(official_path): | |
| 25 return None | |
| 26 | |
| 27 line_regexp = '^#define\s*%s\s*"([^"]+)"\s*$' % token_name | |
| 28 line_pattern = re.compile(line_regexp) | |
| 29 def ParseLine(current_line): | |
| 30 result = line_pattern.match(current_line) | |
| 31 if result: | |
| 32 return result.group(1) | |
| 33 else: | |
| 34 return None | |
| 35 | |
| 36 with open(official_path) as f: | |
| 37 current_line = '' | |
| 38 for line in f: | |
| 39 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
| |
| 40 if line.strip().endswith('\\'): | |
|
Jamie
2012/09/17 22:28:55
Duplicate call to split.
Jói
2012/09/17 22:46:38
Done.
| |
| 41 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
| |
| 42 elif current_line: | |
| 43 # Last line in multi-line #define, time to parse it. | |
| 44 current_line += line | |
| 45 token = ParseLine(current_line) | |
| 46 current_line = '' | |
| 47 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.
| |
| 48 current_line = line | |
| 49 token = ParseLine(current_line) | |
| 50 current_line = '' | |
| 51 if token: | |
| 52 return token | |
| 53 return None | |
| 54 | |
| 55 | |
| 56 def _GetToken(token_name): | |
| 57 """Returns the API token with the given name, or dummytoken by default.""" | |
| 58 token = _GetTokenFromOfficialFile(token_name) | |
| 59 if token: | |
| 60 return token | |
| 61 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
| |
| 62 return os.environ[token_name] | |
| 63 else: | |
| 64 return 'dummytoken' | |
| 65 | |
| 66 | |
| 67 def GetAPIKey(): | |
| 68 """Returns the simple API key.""" | |
| 69 return _GetToken('GOOGLE_API_KEY') | |
| 70 | |
| 71 | |
| 72 def GetClientID(client_name): | |
| 73 """Returns the OAuth 2.0 client ID for the client of the given name.""" | |
| 74 return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name) | |
| 75 | |
| 76 | |
| 77 def GetClientSecret(client_name): | |
| 78 """Returns the OAuth 2.0 client secret for the client of the given name.""" | |
| 79 return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name) | |
| 80 | |
| 81 | |
| 82 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
| |
| 83 print 'GOOGLE_API_KEY=%s' % GetAPIKey() | |
| 84 print 'GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN') | |
| 85 print 'GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN') | |
| 86 print 'GOOGLE_CLIENT_ID_CLOUD_PRINT=%s' % GetClientID('CLOUD_PRINT') | |
| 87 print 'GOOGLE_CLIENT_SECRET_CLOUD_PRINT=%s' % GetClientSecret('CLOUD_PRINT') | |
| 88 print 'GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING') | |
| 89 print 'GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING') | |
| OLD | NEW |