Fix emulator build error
[platform/framework/web/chromium-efl.git] / google_apis / google_api_keys.py
1 #!/usr/bin/env python
2 # Copyright 2012 The Chromium Authors
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 does not have the exact same semantics as the C++ API
9 in google_api_keys.h, since it does not have access to gyp variables
10 or preprocessor defines.
11 """
12
13 from __future__ import print_function
14
15 import os
16 import re
17 import sys
18
19
20 # The token returned when an API key is unset.
21 DUMMY_TOKEN = 'dummytoken'
22
23
24 def _GetTokenFromOfficialFile(token_name):
25   """Parses the token from the official file if it exists, else returns None."""
26   official_path = os.path.join(os.path.dirname(__file__),
27                                'internal/google_chrome_api_keys.h')
28   if not os.path.isfile(official_path):
29     return None
30
31   line_regexp = '^#define\s*%s\s*"([^"]+)"' % token_name
32   line_pattern = re.compile(line_regexp)
33   def ParseLine(current_line):
34     result = line_pattern.match(current_line)
35     if result:
36       return result.group(1)
37     else:
38       return None
39
40   with open(official_path) as f:
41     current_line = ''
42     for line in f:
43       if line.endswith('\\\n'):
44         current_line += line[:-2]
45       else:
46         # Last line in multi-line #define, or a line that is not a
47         # continuation line.
48         current_line += line
49         token = ParseLine(current_line)
50         if token:
51           if current_line.count('"') != 2:
52             raise Exception(
53               'Embedded quotes and multi-line strings are not supported.')
54           return token
55         current_line = ''
56     return None
57
58
59 def _GetToken(token_name):
60   """Returns the API token with the given name, or DUMMY_TOKEN by default."""
61   if token_name in os.environ:
62     return os.environ[token_name]
63   token = _GetTokenFromOfficialFile(token_name)
64   if token:
65     return token
66   else:
67     return DUMMY_TOKEN
68
69
70 def GetAPIKey():
71   """Returns the simple API key."""
72   return _GetToken('GOOGLE_API_KEY')
73
74
75 def GetAPIKeyAndroidNonStable():
76   """Returns the API key for android non-stable channel."""
77   return _GetToken('GOOGLE_API_KEY_ANDROID_NON_STABLE')
78
79
80 def GetClientID(client_name):
81   """Returns the OAuth 2.0 client ID for the client of the given name."""
82   return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name)
83
84
85 def GetClientSecret(client_name):
86   """Returns the OAuth 2.0 client secret for the client of the given name."""
87   return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name)
88
89
90 if __name__ == "__main__":
91   print('GOOGLE_API_KEY=%s' % GetAPIKey())
92   print('GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN'))
93   print('GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN'))
94   print('GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING'))
95   print('GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING'))
96   print('GOOGLE_CLIENT_ID_REMOTING_HOST=%s' % GetClientID('REMOTING_HOST'))
97   print('GOOGLE_CLIENT_SECRET_REMOTING_HOST=%s' % GetClientSecret(
98       'REMOTING_HOST'))