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 | |
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.
| |
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() | |
40 if line.endswith('\\'): | |
41 current_line += line[:-1] | |
42 elif current_line: | |
43 # Last line in multi-line #define, time to parse it. | |
44 # TODO(joi): Handle combining multi-line quoted strings. | |
45 current_line += line | |
46 token = ParseLine(current_line) | |
47 current_line = '' | |
48 else: | |
49 current_line = line | |
50 token = ParseLine(current_line) | |
51 current_line = '' | |
52 if token: | |
53 return token | |
54 return None | |
55 | |
56 | |
57 def _GetToken(token_name): | |
58 """Returns the API token with the given name, or dummytoken by default.""" | |
59 if token_name in os.environ: | |
60 return os.environ[token_name] | |
61 | |
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.
| |
62 token = _GetTokenFromOfficialFile(token_name) | |
63 if token: | |
64 return token | |
65 else: | |
66 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.
| |
67 | |
68 | |
69 def GetAPIKey(): | |
70 """Returns the simple API key.""" | |
71 return _GetToken('GOOGLE_API_KEY') | |
72 | |
73 | |
74 def GetClientID(client_name): | |
75 """Returns the OAuth 2.0 client ID for the client of the given name.""" | |
76 return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name) | |
77 | |
78 | |
79 def GetClientSecret(client_name): | |
80 """Returns the OAuth 2.0 client secret for the client of the given name.""" | |
81 return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name) | |
82 | |
83 | |
84 if __name__ == "__main__": | |
85 print 'GOOGLE_API_KEY=%s' % GetAPIKey() | |
86 print 'GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN') | |
87 print 'GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN') | |
88 print 'GOOGLE_CLIENT_ID_CLOUD_PRINT=%s' % GetClientID('CLOUD_PRINT') | |
89 print 'GOOGLE_CLIENT_SECRET_CLOUD_PRINT=%s' % GetClientSecret('CLOUD_PRINT') | |
90 print 'GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING') | |
91 print 'GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING') | |
OLD | NEW |