-
Notifications
You must be signed in to change notification settings - Fork 125
ENH: Save BigQuery account credentials in a hidden user folder #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,7 @@ def __init__(self, project_id, reauth=False, verbose=False, | |
self.private_key = private_key | ||
self.auth_local_webserver = auth_local_webserver | ||
self.dialect = dialect | ||
self.credentials_path = _get_credentials_file() | ||
self.credentials = self.get_credentials() | ||
self.service = self.get_service() | ||
|
||
|
@@ -279,8 +280,21 @@ def load_user_account_credentials(self): | |
from google_auth_httplib2 import Request | ||
from google.oauth2.credentials import Credentials | ||
|
||
# Use the default credentials location under ~/.config and the | ||
# equivalent directory on windows if the user has not specified a | ||
# credentials path. | ||
if not self.credentials_path: | ||
self.credentials_path = self.get_default_credentials_path() | ||
|
||
# Previously, pandas-gbq saved user account credentials in the | ||
# current working directory. If the bigquery_credentials.dat file | ||
# exists in the current working directory, move the credentials to | ||
# the new default location. | ||
if os.path.isfile('bigquery_credentials.dat'): | ||
os.rename('bigquery_credentials.dat', self.credentials_path) | ||
|
||
try: | ||
with open(_get_credentials_file()) as credentials_file: | ||
with open(self.credentials_path) as credentials_file: | ||
credentials_json = json.load(credentials_file) | ||
except (IOError, ValueError): | ||
return None | ||
|
@@ -301,14 +315,41 @@ def load_user_account_credentials(self): | |
|
||
return _try_credentials(self.project_id, credentials) | ||
|
||
def get_default_credentials_path(self): | ||
""" | ||
Gets the default path to the BigQuery credentials | ||
|
||
.. versionadded 0.3.0 | ||
|
||
Returns | ||
------- | ||
Path to the BigQuery credentials | ||
""" | ||
|
||
import os | ||
|
||
if os.name == 'nt': | ||
config_path = os.environ['APPDATA'] | ||
else: | ||
config_path = os.path.join(os.path.expanduser('~'), '.config') | ||
|
||
config_path = os.path.join(config_path, 'pandas_gbq') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You know this better than I do, but is there a case for having the default path be the default gcloud path, rather than anything specific to pandas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great question! I've given this some thought and I don't feel comfortable writing default credentials into the @tswast Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 to not clobbering If folks set up application default credentials it won't hit this code anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍 |
||
|
||
# Create a pandas_gbq directory in an application-specific hidden | ||
# user folder on the operating system. | ||
if not os.path.exists(config_path): | ||
os.makedirs(config_path) | ||
|
||
return os.path.join(config_path, 'bigquery_credentials.dat') | ||
|
||
def save_user_account_credentials(self, credentials): | ||
""" | ||
Saves user account credentials to a local file. | ||
|
||
.. versionadded 0.2.0 | ||
""" | ||
try: | ||
with open(_get_credentials_file(), 'w') as credentials_file: | ||
with open(self.credentials_path, 'w') as credentials_file: | ||
credentials_json = { | ||
'refresh_token': credentials.refresh_token, | ||
'id_token': credentials.id_token, | ||
|
@@ -793,7 +834,7 @@ def delete_and_recreate_table(self, dataset_id, table_id, table_schema): | |
|
||
def _get_credentials_file(): | ||
return os.environ.get( | ||
'PANDAS_GBQ_CREDENTIALS_FILE', 'bigquery_credentials.dat') | ||
'PANDAS_GBQ_CREDENTIALS_FILE') | ||
|
||
|
||
def _parse_data(schema, rows): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should do an incremental release before
0.3.0
if we plan to have that be the one where we swap to the google-cloud-python libraries? Seems like that PR has stalled and I don't have time to rebase it either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done