Skip to content

Commit dbfb4e9

Browse files
jonathanspparthea
authored andcommitted
ENH: Add support for env variable PANDAS_GBQ_CREDENTIALS_FILE (#87)
1 parent 79c9067 commit dbfb4e9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

docs/source/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Changelog
55
------------------
66

77
- :func:`read_gbq` now raises ``QueryTimeout`` if the request exceeds the ``query.timeoutMs`` value specified in the BigQuery configuration. (:issue:`76`)
8+
- Environment variable ``PANDAS_GBQ_CREDENTIALS_FILE`` can now be used to override the default location where the BigQuery user account credentials are stored. (:issue:`86`)
9+
810

911
0.2.0 / 2017-07-24
1012
------------------

pandas_gbq/gbq.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import uuid
66
import time
77
import sys
8+
import os
89

910
import numpy as np
1011

@@ -279,7 +280,7 @@ def load_user_account_credentials(self):
279280
from google.oauth2.credentials import Credentials
280281

281282
try:
282-
with open('bigquery_credentials.dat') as credentials_file:
283+
with open(_get_credentials_file()) as credentials_file:
283284
credentials_json = json.load(credentials_file)
284285
except (IOError, ValueError):
285286
return None
@@ -307,7 +308,7 @@ def save_user_account_credentials(self, credentials):
307308
.. versionadded 0.2.0
308309
"""
309310
try:
310-
with open('bigquery_credentials.dat', 'w') as credentials_file:
311+
with open(_get_credentials_file(), 'w') as credentials_file:
311312
credentials_json = {
312313
'refresh_token': credentials.refresh_token,
313314
'id_token': credentials.id_token,
@@ -790,6 +791,11 @@ def delete_and_recreate_table(self, dataset_id, table_id, table_schema):
790791
sleep(delay)
791792

792793

794+
def _get_credentials_file():
795+
return os.environ.get(
796+
'PANDAS_GBQ_CREDENTIALS_FILE', 'bigquery_credentials.dat')
797+
798+
793799
def _parse_data(schema, rows):
794800
# see:
795801
# http://pandas.pydata.org/pandas-docs/dev/missing_data.html
@@ -875,7 +881,10 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
875881
authentication (eg. jupyter iPython notebook on remote host)
876882
auth_local_webserver : boolean, default False
877883
Use the [local webserver flow] instead of the [console flow] when
878-
getting user credentials.
884+
getting user credentials. A file named bigquery_credentials.dat will
885+
be created in current dir. You can also set PANDAS_GBQ_CREDENTIALS_FILE
886+
environment variable so as to define a specific path to store this
887+
credential (eg. /etc/keys/bigquery.dat).
879888
880889
.. [local webserver flow]
881890
http://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_local_server

pandas_gbq/tests/test_gbq.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ def test_import_google_api_python_client(self):
321321
from googleapiclient.discovery import build # noqa
322322
from googleapiclient.errors import HttpError # noqa
323323

324+
def test_should_return_credentials_path_set_by_env_var(self):
325+
import mock
326+
env = {'PANDAS_GBQ_CREDENTIALS_FILE': '/tmp/dummy.dat'}
327+
with mock.patch.dict('os.environ', env):
328+
assert gbq._get_credentials_file() == '/tmp/dummy.dat'
329+
330+
assert gbq._get_credentials_file() == 'bigquery_credentials.dat'
331+
324332
def test_should_return_bigquery_integers_as_python_ints(self):
325333
result = gbq._parse_entry(1, 'INTEGER')
326334
assert result == int(1)

0 commit comments

Comments
 (0)