Skip to content

Commit

Permalink
Merge pull request #11 from Imageomics/10-config-file
Browse files Browse the repository at this point in the history
Allow custom config file path
  • Loading branch information
johnbradley authored Oct 10, 2022
2 parents 03c3ec6 + 8da63de commit b4c3fa9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This command will prompt you for your Dataverse URL and API token and create a c
with these two values in your home directory. The config file is named `.dataverse` and is in
JSON format containing keys "url" and "token".

To use an alternate location for the config file set the `DATAVERSE_CONFIG_PATH` environment variable.

## Commands

Expand Down
5 changes: 4 additions & 1 deletion src/dva/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


CONFIG_FILE = "~/.dataverse"
CONFIG_FILE_PATH_VAR_NAME = "DATAVERSE_CONFIG_PATH"
CONFIG_URL = "url"
CONFIG_TOKEN = "token"

Expand Down Expand Up @@ -39,7 +40,9 @@ def __init__(self, url):


def _read_config_file():
path = os.path.expanduser(CONFIG_FILE)
path = os.environ.get(CONFIG_FILE_PATH_VAR_NAME)
if not path:
path = os.path.expanduser(CONFIG_FILE)
if os.path.exists(path):
with open(path, 'r') as infile:
data = json.load(infile)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,23 @@ def test_config_from_file(self, mock_os):
})
with patch("builtins.open", mock_open(read_data=config_file_data)) as mock_file:
config = Config(url=None)
mock_file.assert_called_with(mock_os.path.expanduser.return_value, 'r')
self.assertEqual(config.url, "myurl")
self.assertEqual(config.token, "secret")

@patch('dva.config.os')
def test_config_from_file_with_env_setting(self, mock_os):
mock_os.path.exists.return_value = True # no config file
mock_os.path.exists.return_value = True # no config file
mock_os.environ = { "DATAVERSE_CONFIG_PATH" : "/tmp/.dataverse"}
config_file_data = json.dumps({
"url": "myurl",
"token": "secret",
})
with patch("builtins.open", mock_open(read_data=config_file_data)) as mock_file:
config = Config(url=None)
mock_file.assert_called_with('/tmp/.dataverse', 'r')

@patch('dva.config.os')
def test_config_env_variables(self, mock_os):
mock_os.path.exists.return_value = True # no config file
Expand Down

0 comments on commit b4c3fa9

Please sign in to comment.