13
13
from pandas .tools .merge import concat
14
14
from pandas .core .common import PandasError
15
15
16
- _IMPORTS = False
17
- _GOOGLE_API_CLIENT_INSTALLED = False
18
- _GOOGLE_API_CLIENT_VALID_VERSION = False
19
- _GOOGLE_FLAGS_INSTALLED = False
20
- _GOOGLE_FLAGS_VALID_VERSION = False
21
- _HTTPLIB2_INSTALLED = False
22
- _SETUPTOOLS_INSTALLED = False
23
16
24
- def _importers ():
25
- # import things we need
26
- # but make this done on a first use basis
27
-
28
- global _IMPORTS
29
- if _IMPORTS :
30
- return
31
-
32
- _IMPORTS = True
33
-
34
- if not compat .PY3 :
35
-
36
- global _GOOGLE_API_CLIENT_INSTALLED , _GOOGLE_API_CLIENT_VALID_VERSION , \
37
- _GOOGLE_FLAGS_INSTALLED , _GOOGLE_FLAGS_VALID_VERSION , \
38
- _HTTPLIB2_INSTALLED , _SETUPTOOLS_INSTALLED
39
-
40
- try :
41
- import pkg_resources
42
- _SETUPTOOLS_INSTALLED = True
43
- except ImportError :
44
- _SETUPTOOLS_INSTALLED = False
45
-
46
- if _SETUPTOOLS_INSTALLED :
47
- try :
48
- from apiclient .discovery import build
49
- from apiclient .http import MediaFileUpload
50
- from apiclient .errors import HttpError
51
-
52
- from oauth2client .client import OAuth2WebServerFlow
53
- from oauth2client .client import AccessTokenRefreshError
54
- from oauth2client .client import flow_from_clientsecrets
55
- from oauth2client .file import Storage
56
- from oauth2client .tools import run
57
- _GOOGLE_API_CLIENT_INSTALLED = True
58
- _GOOGLE_API_CLIENT_VERSION = pkg_resources .get_distribution ('google-api-python-client' ).version
59
-
60
- if LooseVersion (_GOOGLE_API_CLIENT_VERSION ) >= '1.2.0' :
61
- _GOOGLE_API_CLIENT_VALID_VERSION = True
62
-
63
- except ImportError :
64
- _GOOGLE_API_CLIENT_INSTALLED = False
65
-
66
-
67
- try :
68
- import gflags as flags
69
- _GOOGLE_FLAGS_INSTALLED = True
70
-
71
- _GOOGLE_FLAGS_VERSION = pkg_resources .get_distribution ('python-gflags' ).version
17
+ def _check_google_client_version ():
18
+ if compat .PY3 :
19
+ raise NotImplementedError ("Google's libraries do not support Python 3 yet" )
72
20
73
- if LooseVersion ( _GOOGLE_FLAGS_VERSION ) >= '2.0' :
74
- _GOOGLE_FLAGS_VALID_VERSION = True
21
+ try :
22
+ import pkg_resources
75
23
76
- except ImportError :
77
- _GOOGLE_FLAGS_INSTALLED = False
24
+ except ImportError :
25
+ raise ImportError ( 'Could not import pkg_resources (setuptools).' )
78
26
79
- try :
80
- import httplib2
81
- _HTTPLIB2_INSTALLED = True
82
- except ImportError :
83
- _HTTPLIB2_INSTALLED = False
27
+ _GOOGLE_API_CLIENT_VERSION = pkg_resources .get_distribution ('google-api-python-client' ).version
84
28
29
+ if LooseVersion (_GOOGLE_API_CLIENT_VERSION ) < '1.2.0' :
30
+ raise ImportError ("pandas requires google-api-python-client >= 1.2.0 for Google "
31
+ "BigQuery support, current version " + _GOOGLE_API_CLIENT_VERSION )
85
32
86
33
logger = logging .getLogger ('pandas.io.gbq' )
87
34
logger .setLevel (logging .ERROR )
@@ -142,6 +89,16 @@ def __init__(self, project_id, reauth=False):
142
89
self .service = self .get_service (self .credentials )
143
90
144
91
def get_credentials (self ):
92
+ try :
93
+ from oauth2client .client import OAuth2WebServerFlow
94
+ from oauth2client .file import Storage
95
+ from oauth2client .tools import run_flow , argparser
96
+
97
+ except ImportError :
98
+ raise ImportError ('Could not import Google API Client.' )
99
+
100
+ _check_google_client_version ()
101
+
145
102
flow = OAuth2WebServerFlow (client_id = '495642085510-k0tmvj2m941jhre2nbqka17vqpjfddtd.apps.googleusercontent.com' ,
146
103
client_secret = 'kOc9wMptUtxkcIFbtZCcrEAc' ,
147
104
scope = 'https://www.googleapis.com/auth/bigquery' ,
@@ -151,18 +108,41 @@ def get_credentials(self):
151
108
credentials = storage .get ()
152
109
153
110
if credentials is None or credentials .invalid or self .reauth :
154
- credentials = run (flow , storage )
111
+ credentials = run_flow (flow , storage , argparser . parse_args ([]) )
155
112
156
113
return credentials
157
114
158
115
def get_service (self , credentials ):
116
+ try :
117
+ import httplib2
118
+
119
+ except ImportError :
120
+ raise ImportError ("pandas requires httplib2 for Google BigQuery support" )
121
+
122
+ try :
123
+ from apiclient .discovery import build
124
+
125
+ except ImportError :
126
+ raise ImportError ('Could not import Google API Client.' )
127
+
128
+ _check_google_client_version ()
129
+
159
130
http = httplib2 .Http ()
160
131
http = credentials .authorize (http )
161
132
bigquery_service = build ('bigquery' , 'v2' , http = http )
162
133
163
134
return bigquery_service
164
135
165
136
def run_query (self , query ):
137
+ try :
138
+ from apiclient .errors import HttpError
139
+ from oauth2client .client import AccessTokenRefreshError
140
+
141
+ except ImportError :
142
+ raise ImportError ('Could not import Google API Client.' )
143
+
144
+ _check_google_client_version ()
145
+
166
146
job_collection = self .service .jobs ()
167
147
job_data = {
168
148
'configuration' : {
@@ -313,38 +293,6 @@ def _parse_entry(field_value, field_type):
313
293
return field_value == 'true'
314
294
return field_value
315
295
316
- def _test_imports ():
317
-
318
- _importers ()
319
- _GOOGLE_API_CLIENT_INSTALLED
320
- _GOOGLE_API_CLIENT_VALID_VERSION
321
- _GOOGLE_FLAGS_INSTALLED
322
- _GOOGLE_FLAGS_VALID_VERSION
323
- _HTTPLIB2_INSTALLED
324
- _SETUPTOOLS_INSTALLED
325
-
326
- if compat .PY3 :
327
- raise NotImplementedError ("Google's libraries do not support Python 3 yet" )
328
-
329
- if not _SETUPTOOLS_INSTALLED :
330
- raise ImportError ('Could not import pkg_resources (setuptools).' )
331
-
332
- if not _GOOGLE_API_CLIENT_INSTALLED :
333
- raise ImportError ('Could not import Google API Client.' )
334
-
335
- if not _GOOGLE_FLAGS_INSTALLED :
336
- raise ImportError ('Could not import Google Command Line Flags Module.' )
337
-
338
- if not _GOOGLE_API_CLIENT_VALID_VERSION :
339
- raise ImportError ("pandas requires google-api-python-client >= 1.2.0 for Google "
340
- "BigQuery support, current version " + _GOOGLE_API_CLIENT_VERSION )
341
-
342
- if not _GOOGLE_FLAGS_VALID_VERSION :
343
- raise ImportError ("pandas requires python-gflags >= 2.0.0 for Google "
344
- "BigQuery support, current version " + _GOOGLE_FLAGS_VERSION )
345
-
346
- if not _HTTPLIB2_INSTALLED :
347
- raise ImportError ("pandas requires httplib2 for Google BigQuery support" )
348
296
349
297
def read_gbq (query , project_id = None , index_col = None , col_order = None , reauth = False ):
350
298
"""Load data from Google BigQuery.
@@ -379,7 +327,6 @@ def read_gbq(query, project_id = None, index_col=None, col_order=None, reauth=Fa
379
327
380
328
"""
381
329
382
- _test_imports ()
383
330
384
331
if not project_id :
385
332
raise TypeError ("Missing required parameter: project_id" )
@@ -450,7 +397,6 @@ def to_gbq(dataframe, destination_table, project_id=None, chunksize=10000,
450
397
if multiple accounts are used.
451
398
452
399
"""
453
- _test_imports ()
454
400
455
401
if not project_id :
456
402
raise TypeError ("Missing required parameter: project_id" )
0 commit comments