summaryrefslogtreecommitdiffstats
path: root/google_apis
diff options
context:
space:
mode:
authorjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-17 23:48:43 +0000
committerjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-17 23:48:43 +0000
commit13a533682e3c38ec8063d9b75f2c5f6f5f482d05 (patch)
tree5e334adc056bb85f83ecdc9cb14915ac28f05ad7 /google_apis
parentaaae03bcbd224e25a3bb629ae87def446ae4fb69 (diff)
downloadchromium_src-13a533682e3c38ec8063d9b75f2c5f6f5f482d05.zip
chromium_src-13a533682e3c38ec8063d9b75f2c5f6f5f482d05.tar.gz
chromium_src-13a533682e3c38ec8063d9b75f2c5f6f5f482d05.tar.bz2
Add a preliminary Python API for retrieving Google API keys.
This has nearly identical semantics to the C++ API for builds where the internal key file is available. For builds where that file is not available, it does not yet have the same semantics, since at the moment it does not have access to the gyp variables that would be required (this is a TODO item). Instead, it will simply try to find keys from environment variables, and if they are not found it will use 'dummytoken' as the default token for each key. BUG=145584 Review URL: https://codereview.chromium.org/10933138 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157247 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'google_apis')
-rwxr-xr-xgoogle_apis/google_api_keys.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/google_apis/google_api_keys.py b/google_apis/google_api_keys.py
new file mode 100755
index 0000000..26fd236
--- /dev/null
+++ b/google_apis/google_api_keys.py
@@ -0,0 +1,92 @@
+#!/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
+
+
+# The token returned when an API key is unset.
+DUMMY_TOKEN = 'dummytoken'
+
+
+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*"([^"]+)"' % 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:
+ if line.endswith('\\\n'):
+ current_line += line[:-2]
+ else:
+ # Last line in multi-line #define, or a line that is not a
+ # continuation line.
+ current_line += line
+ token = ParseLine(current_line)
+ if token:
+ if current_line.count('"') != 2:
+ raise Exception(
+ 'Embedded quotes and multi-line strings are not supported.')
+ return token
+ current_line = ''
+ return None
+
+
+def _GetToken(token_name):
+ """Returns the API token with the given name, or DUMMY_TOKEN by default."""
+ if token_name in os.environ:
+ return os.environ[token_name]
+ token = _GetTokenFromOfficialFile(token_name)
+ if token:
+ return token
+ else:
+ return DUMMY_TOKEN
+
+
+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')