Skip to content
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

Set token in inline config #83

Merged
merged 4 commits into from
Nov 20, 2017
Merged
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
43 changes: 27 additions & 16 deletions datadotworld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@

import weakref

from datadotworld.config import FileConfig, ChainedConfig
from datadotworld.config import (
FileConfig,
ChainedConfig,
InlineConfig,
EnvConfig
)
from datadotworld.datadotworld import DataDotWorld, UriParam # noqa: F401

__version__ = '1.4.3'
Expand All @@ -37,24 +42,28 @@
__instances = weakref.WeakValueDictionary()


def _get_instance(profile):
def _get_instance(profile, **kwargs):
"""

:param profile:

"""
instance = __instances.get(profile)

if instance is None:
config_param = (ChainedConfig()
if profile == 'default'
else FileConfig(profile=profile))
config_param = (ChainedConfig(config_chain=[
InlineConfig(kwargs.get('auth_token')),
EnvConfig(),
FileConfig()])
if profile == 'default'
else FileConfig(profile=profile))
instance = DataDotWorld(config=config_param)
__instances[profile] = instance
return instance


def load_dataset(dataset_key, force_update=False, auto_update=False,
profile='default'):
profile='default', **kwargs):
"""Load a dataset from the local filesystem, downloading it from data.world
first, if necessary.

Expand Down Expand Up @@ -86,13 +95,14 @@ def load_dataset(dataset_key, force_update=False, auto_update=False,
>>> list(dataset.dataframes)
['changelog', 'datadotworldbballstats', 'datadotworldbballteam']
"""
return _get_instance(profile).load_dataset(dataset_key,
force_update=force_update,
auto_update=auto_update)
return _get_instance(profile, **kwargs). \
load_dataset(dataset_key,
force_update=force_update,
auto_update=auto_update)


def query(dataset_key, query, query_type='sql', profile='default',
parameters=None):
parameters=None, **kwargs):
"""Query an existing dataset

:param dataset_key: Dataset identifier, in the form of owner/id or of a url
Expand Down Expand Up @@ -127,9 +137,10 @@ def query(dataset_key, query, query_type='sql', profile='default',
>>> df.shape
(8, 6)
"""
return _get_instance(profile).query(dataset_key, query,
query_type=query_type,
parameters=parameters)
return _get_instance(profile, **kwargs).query(dataset_key, query,
query_type=query_type,
parameters=parameters,
**kwargs)


def open_remote_file(dataset_key, file_name, profile='default',
Expand Down Expand Up @@ -210,12 +221,12 @@ def open_remote_file(dataset_key, file_name, profile='default',
... 'test', mode='rb') as r:
... bytes = r.read()
"""
return _get_instance(profile).open_remote_file(
return _get_instance(profile, **kwargs).open_remote_file(
dataset_key, file_name,
mode=mode, **kwargs)


def api_client(profile='default'):
def api_client(profile='default', **kwargs):
"""Return API client for access to data.world's REST API

:param profile: Configuration profile (account) to use.
Expand All @@ -232,7 +243,7 @@ def api_client(profile='default'):
... 'jonloyens/an-intro-to-dataworld-dataset').get('title')
'An Intro to data.world Dataset'
"""
return _get_instance(profile).api_client
return _get_instance(profile, **kwargs).api_client


if __name__ == "__main__":
Expand Down
6 changes: 6 additions & 0 deletions datadotworld/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,9 @@ def _first_not_none(seq, supplier_func):
return obj

return None


class InlineConfig(DefaultConfig):
def __init__(self, token):
super(InlineConfig, self).__init__()
self._auth_token = token
1 change: 0 additions & 1 deletion datadotworld/datadotworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def __init__(self, config=None):
self._protocol = 'https'
self._query_host = 'query.data.world'
self._download_host = 'download.data.world'

self._config = config or ChainedConfig()
self.api_client = RestApiClient(self._config)

Expand Down
18 changes: 17 additions & 1 deletion tests/datadotworld/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from six import StringIO

from datadotworld.config import FileConfig, EnvConfig, DefaultConfig, \
ChainedConfig
ChainedConfig, InlineConfig


# Shared fixtures
Expand Down Expand Up @@ -67,6 +67,22 @@ def test_tmp_dir(self):
equal_to(path.expanduser(tempfile.gettempdir())))


class TestInlineConfig:
def test_auth_token(self):
config = InlineConfig('inline_token')
assert_that(config.auth_token, equal_to('inline_token'))

def test_cache_dir(self):
config = InlineConfig('inline_token')
assert_that(config.cache_dir,
equal_to(path.expanduser('~/.dw/cache')))

def test_tmp_dir(self):
config = InlineConfig('inline_token')
assert_that(config.tmp_dir,
equal_to(path.expanduser(tempfile.gettempdir())))


class TestEnvConfig:
def test_auth_token(self, monkeypatch):
monkeypatch.setattr(os, 'environ', {'DW_AUTH_TOKEN': 'env_token'})
Expand Down