Skip to content

Commit b4c3fa9

Browse files
authored
Merge pull request #11 from Imageomics/10-config-file
Allow custom config file path
2 parents 03c3ec6 + 8da63de commit b4c3fa9

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This command will prompt you for your Dataverse URL and API token and create a c
3232
with these two values in your home directory. The config file is named `.dataverse` and is in
3333
JSON format containing keys "url" and "token".
3434

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

3637
## Commands
3738

src/dva/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
CONFIG_FILE = "~/.dataverse"
6+
CONFIG_FILE_PATH_VAR_NAME = "DATAVERSE_CONFIG_PATH"
67
CONFIG_URL = "url"
78
CONFIG_TOKEN = "token"
89

@@ -39,7 +40,9 @@ def __init__(self, url):
3940

4041

4142
def _read_config_file():
42-
path = os.path.expanduser(CONFIG_FILE)
43+
path = os.environ.get(CONFIG_FILE_PATH_VAR_NAME)
44+
if not path:
45+
path = os.path.expanduser(CONFIG_FILE)
4346
if os.path.exists(path):
4447
with open(path, 'r') as infile:
4548
data = json.load(infile)

tests/test_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,23 @@ def test_config_from_file(self, mock_os):
3030
})
3131
with patch("builtins.open", mock_open(read_data=config_file_data)) as mock_file:
3232
config = Config(url=None)
33+
mock_file.assert_called_with(mock_os.path.expanduser.return_value, 'r')
3334
self.assertEqual(config.url, "myurl")
3435
self.assertEqual(config.token, "secret")
3536

37+
@patch('dva.config.os')
38+
def test_config_from_file_with_env_setting(self, mock_os):
39+
mock_os.path.exists.return_value = True # no config file
40+
mock_os.path.exists.return_value = True # no config file
41+
mock_os.environ = { "DATAVERSE_CONFIG_PATH" : "/tmp/.dataverse"}
42+
config_file_data = json.dumps({
43+
"url": "myurl",
44+
"token": "secret",
45+
})
46+
with patch("builtins.open", mock_open(read_data=config_file_data)) as mock_file:
47+
config = Config(url=None)
48+
mock_file.assert_called_with('/tmp/.dataverse', 'r')
49+
3650
@patch('dva.config.os')
3751
def test_config_env_variables(self, mock_os):
3852
mock_os.path.exists.return_value = True # no config file

0 commit comments

Comments
 (0)