diff --git a/datadotworld/__init__.py b/datadotworld/__init__.py index cfe359d..5af6206 100644 --- a/datadotworld/__init__.py +++ b/datadotworld/__init__.py @@ -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' @@ -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. @@ -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 @@ -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', @@ -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. @@ -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__": diff --git a/datadotworld/config.py b/datadotworld/config.py index a9693a3..c24dec4 100644 --- a/datadotworld/config.py +++ b/datadotworld/config.py @@ -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 diff --git a/datadotworld/datadotworld.py b/datadotworld/datadotworld.py index 2ee47fc..60737c9 100644 --- a/datadotworld/datadotworld.py +++ b/datadotworld/datadotworld.py @@ -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) diff --git a/tests/datadotworld/test_config.py b/tests/datadotworld/test_config.py index f7c1727..645a7bf 100644 --- a/tests/datadotworld/test_config.py +++ b/tests/datadotworld/test_config.py @@ -30,7 +30,7 @@ from six import StringIO from datadotworld.config import FileConfig, EnvConfig, DefaultConfig, \ - ChainedConfig + ChainedConfig, InlineConfig # Shared fixtures @@ -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'})