Skip to content

ENH: avoid unnecessary ga mgmt calls if given profile_id upfront #4529

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ci/print_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,22 @@
except:
print("html5lib: Not installed")

try:
import apiclient
print("apiclient: %s" % apiclient.__version__)
except:
print("apiclient: Not installed")

try:
import oauth2client
print("oauth2client: %s" % oauth2client.__version__)
except:
print("oauth2client: Not installed")

try:
import httplib2
print("httplib2: %s" % httplib2.__version__)
except:
print("httplib2: Not installed")

print("\n")
2 changes: 2 additions & 0 deletions ci/requirements-2.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ python-dateutil==1.5
pytz==2013b
http://www.crummy.com/software/BeautifulSoup/bs4/download/4.2/beautifulsoup4-4.2.0.tar.gz
html5lib==1.0b2
google-api-python-client==1.2
httplib2==0.8
3 changes: 3 additions & 0 deletions ci/requirements-2.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ scikits.timeseries==0.91.3
MySQL-python==1.2.4
scipy==0.10.0
beautifulsoup4==4.2.1
google-api-python-client==1.2
httplib2==0.8
python-gflags==2.0
1 change: 1 addition & 0 deletions ci/requirements-2.7_LOCALE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ html5lib==1.0b2
lxml==3.2.1
scipy==0.10.0
beautifulsoup4==4.2.1
google-api-python-client==1.2
4 changes: 4 additions & 0 deletions doc/source/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Optional Dependencies
to get the necessary dependencies for installation of `lxml`_. This
will prevent further headaches down the line.

* `google-api-python-client `<https://developers.google.com/
api-client-library/python/start/installation>`__
* Needed for :mod:`pandas.io.ga`


.. _html5lib: https://github.com/html5lib/html5lib-python
.. _BeautifulSoup4: http://www.crummy.com/software/BeautifulSoup
Expand Down
34 changes: 26 additions & 8 deletions pandas/io/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@
import pandas.io.auth as auth
from pandas.util.decorators import Appender, Substitution

from apiclient.errors import HttpError
from oauth2client.client import AccessTokenRefreshError
try:
from apiclient.errors import HttpError
_APICLIENT_INSTALLED = True
except:
_APICLIENT_INSTALLED = False

try:
from oauth2client.client import AccessTokenRefreshError
_OAUTH2_INSTALLED = True
except:
_OAUTH2_INSTALLED = False

from pandas.compat import zip, u

TYPE_MAP = {u('INTEGER'): int, u('FLOAT'): float, u('TIME'): int}
Expand Down Expand Up @@ -97,6 +107,12 @@ def reset_token_store():
@Substitution(extras=_AUTH_PARAMS)
@Appender(_GA_READER_DOC)
def read_ga(metrics, dimensions, start_date, **kwargs):

if not _OAUTH2_INSTALLED:
raise ImportError('Could not import OAuth2.0 Client.')
elif not _APICLIENT_INSTALLED:
raise ImportError('Could not import Google Python API.')

lst = ['secrets', 'scope', 'token_file_name', 'redirect']
reader_kwds = dict((p, kwargs.pop(p)) for p in lst if p in kwargs)
reader = GAnalytics(**reader_kwds)
Expand Down Expand Up @@ -254,13 +270,15 @@ def get_data(self, metrics, start_date, end_date=None,
raise ValueError('Google API returns maximum of 10,000 rows, '
'please set chunksize')

account = self.get_account(account_name, account_id)
web_property = self.get_web_property(account.get('id'), property_name,
property_id)
profile = self.get_profile(account.get('id'), web_property.get('id'),
profile_name, profile_id)
if not profile_id:
account = self.get_account(account_name, account_id)
web_property = self.get_web_property(account.get('id'),
property_name, property_id)
profile = self.get_profile(account.get('id'),
web_property.get('id'), profile_name,
profile_id)

profile_id = profile.get('id')
profile_id = profile.get('id')

if index_col is None and dimensions is not None:
if isinstance(dimensions, compat.string_types):
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/tests/test_ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from pandas.io.ga import GAnalytics, read_ga
from pandas.io.auth import AuthenticationConfigError, reset_token_store
from pandas.io import auth
except ImportError:
raise nose.SkipTest
except (Exception) as detail:
raise nose.SkipTest(detail)

class TestGoogle(unittest.TestCase):

Expand Down