Skip to content

Commit

Permalink
Merge ca20f13 into e7cfab5
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus89 committed Oct 10, 2019
2 parents e7cfab5 + ca20f13 commit f876bbe
Show file tree
Hide file tree
Showing 17 changed files with 1,215 additions and 64 deletions.
23 changes: 17 additions & 6 deletions cartoframes/data/observatory/catalog.py
Expand Up @@ -5,8 +5,11 @@
from .country import Country
from .geography import Geography
from .dataset import Dataset
from .subscriptions import Subscriptions
from .repository.constants import COUNTRY_FILTER, CATEGORY_FILTER, GEOGRAPHY_FILTER

from ...auth import Credentials, get_default_credentials


class Catalog(object):
"""Data Observatory Catalog"""
Expand Down Expand Up @@ -114,18 +117,26 @@ def clear_filters(self):

self.filters = {}

def purchased_datasets(self, credentials=None):
"""Get all the datasets in the Catalog
def subscriptions(self, credentials=None):
"""Get all the subscriptions in the Catalog
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
A :py:class:`Credentials <cartoframes.auth.Credentials>`
instance can be used in place of a `username`|`base_url` / `api_key` combination.
Only required for the purchased datasets.
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be used.
Returns:
:py:class:`Datasets <cartoframes.data.observatory.Datasets>`
"""

return Dataset.get_all(self.filters, credentials)
_credentials = credentials or get_default_credentials()

if not isinstance(credentials, Credentials):
raise ValueError('`credentials` must be a Credentials class instance')

return Subscriptions(
Dataset.get_all(self.filters, _credentials),
Geography.get_all(self.filters, _credentials)
)
22 changes: 18 additions & 4 deletions cartoframes/data/observatory/dataset.py
Expand Up @@ -4,6 +4,7 @@
from .repository.dataset_repo import get_dataset_repo
from .repository.variable_repo import get_variable_repo
from .repository.variable_group_repo import get_variable_group_repo
from .utils import display_subscription_form


class Dataset(CatalogEntity):
Expand Down Expand Up @@ -82,10 +83,23 @@ def download(self, credentials=None):
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be attempted to be
used.
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be used.
"""

return self._download(credentials)

def subscribe(self, credentials=None):
"""Subscribe to a Dataset.
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be used.
"""

_credentials = self._get_credentials(credentials)

display_subscription_form(self.id, _credentials)
12 changes: 8 additions & 4 deletions cartoframes/data/observatory/entity.py
Expand Up @@ -6,7 +6,7 @@
from carto.exceptions import CartoException

from ..clients.bigquery_client import BigQueryClient
from ...auth import get_default_credentials
from ...auth import Credentials, get_default_credentials

try:
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -62,7 +62,7 @@ def _get_print_id(self):
return self.id

def _download(self, credentials=None):
credentials = _get_credentials(credentials)
credentials = self._get_credentials(credentials)
user_dataset = credentials.get_do_dataset()
bq_client = _get_bigquery_client(_WORKING_PROJECT, credentials)

Expand All @@ -79,9 +79,13 @@ def _download(self, credentials=None):

return file_path

def _get_credentials(self, credentials=None):
_credentials = credentials or get_default_credentials()

def _get_credentials(credentials=None):
return credentials or get_default_credentials()
if not isinstance(credentials, Credentials):
raise ValueError('`credentials` must be a Credentials class instance')

return _credentials


def _get_bigquery_client(project, credentials):
Expand Down
26 changes: 22 additions & 4 deletions cartoframes/data/observatory/geography.py
Expand Up @@ -3,6 +3,7 @@
from .entity import CatalogEntity
from .repository.dataset_repo import get_dataset_repo
from .repository.geography_repo import get_geography_repo
from .utils import display_subscription_form


class Geography(CatalogEntity):
Expand Down Expand Up @@ -53,15 +54,32 @@ def is_public_data(self):
def summary(self):
return self.data['summary_jsonb']

@classmethod
def get_all(cls, filters=None, credentials=None):
return cls.entity_repo.get_all(filters, credentials)

def download(self, credentials=None):
"""Download Geography data.
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will attempted to be
used.
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be used.
"""

return self._download(credentials)

def subscribe(self, credentials=None):
"""Subscribe to a Dataset.
Args:
credentials (:py:class:`Credentials <cartoframes.auth.Credentials>`, optional):
credentials of CARTO user account. If not provided,
a default credentials (if set with :py:meth:`set_default_credentials
<cartoframes.auth.set_default_credentials>`) will be used.
"""

_credentials = self._get_credentials(credentials)

display_subscription_form(self.id, _credentials)
4 changes: 4 additions & 0 deletions cartoframes/data/observatory/repository/geography_repo.py
Expand Up @@ -18,6 +18,10 @@ class GeographyRepository(EntityRepository):
def __init__(self):
super(GeographyRepository, self).__init__(_GEOGRAPHY_ID_FIELD, _ALLOWED_FILTERS, _GEOGRAPHY_SLUG_FIELD)

def get_all(self, filters=None, credentials=None):
self.client.set_user_credentials(credentials)
return self._get_filtered_entities(filters)

def get_by_country(self, iso_code3):
return self._get_filtered_entities({COUNTRY_FILTER: iso_code3})

Expand Down
30 changes: 13 additions & 17 deletions cartoframes/data/observatory/repository/repo_client.py
@@ -1,9 +1,8 @@
from __future__ import absolute_import

from carto.do_datasets import DODatasetManager

from ...clients import SQLClient
from ....auth import Credentials, get_default_credentials
from ....auth import Credentials
from ..utils import get_subscription_ids


class RepoClient(object):
Expand All @@ -16,7 +15,7 @@ def __init__(self):
self.client = SQLClient(self._do_credentials)

def set_user_credentials(self, credentials):
self._user_credentials = credentials or get_default_credentials()
self._user_credentials = credentials

def get_countries(self, filters=None):
query = 'SELECT DISTINCT t.country_id AS id FROM datasets_public t'
Expand Down Expand Up @@ -44,6 +43,13 @@ def get_variables_groups(self, filters=None):

def get_geographies(self, filters=None):
query = 'SELECT t.* FROM geographies_public t'

extra_condition = []
if self._user_credentials is not None:
ids = get_subscription_ids(self._user_credentials)
if len(ids) > 0:
extra_condition.append('t.id IN ({})'.format(ids))

return self._run_query(query, filters)

def get_geographies_joined_datasets(self, filters=None):
Expand All @@ -55,7 +61,9 @@ def get_datasets(self, filters=None):

extra_condition = []
if self._user_credentials is not None:
extra_condition.append('t.id IN ({})'.format(self._get_purchased_dataset_ids()))
ids = get_subscription_ids(self._user_credentials)
if len(ids) > 0:
extra_condition.append('t.id IN ({})'.format(ids))

return self._run_query(query, filters, extra_condition)

Expand All @@ -76,18 +84,6 @@ def _compute_conditions(self, filters, extra_conditions):

return conditions

def _get_purchased_dataset_ids(self):
purchased_datasets = self._fetch_purchased_datasets()
purchased_dataset_ids = list(map(lambda pd: pd.id, purchased_datasets))
return ','.join(["'" + id + "'" for id in purchased_dataset_ids])

def _fetch_purchased_datasets(self):
api_key_auth_client = self._user_credentials.get_api_key_auth_client()
do_manager = DODatasetManager(api_key_auth_client)
if do_manager is not None:
return do_manager.all()
return []

def __new__(cls):
if not RepoClient.__instance:
RepoClient.__instance = object.__new__(cls)
Expand Down
37 changes: 37 additions & 0 deletions cartoframes/data/observatory/subscription_info.py
@@ -0,0 +1,37 @@

class SubscriptionInfo(object):

def __init__(self, raw_data):
self._raw_data = raw_data

@property
def id(self):
return self._raw_data.get('id')

@property
def estimated_delivery_days(self):
return self._raw_data.get('estimated_delivery_days')

@property
def subscription_list_price(self):
return self._raw_data.get('subscription_list_price')

@property
def tos(self):
return self._raw_data.get('tos')

@property
def tos_link(self):
return self._raw_data.get('tos_link')

@property
def licenses(self):
return self._raw_data.get('licenses')

@property
def licenses_link(self):
return self._raw_data.get('licenses_link')

@property
def rights(self):
return self._raw_data.get('rights')
14 changes: 14 additions & 0 deletions cartoframes/data/observatory/subscriptions.py
@@ -0,0 +1,14 @@

class Subscriptions(object):

def __init__(self, datasets, geographies):
self._subscriptions_datasetes = datasets
self._subscriptions_geographies = geographies

@property
def datasets(self):
return self._subscriptions_datasetes

@property
def geographies(self):
return self._subscriptions_geographies

0 comments on commit f876bbe

Please sign in to comment.