Skip to content

Commit

Permalink
feat(settings): Add settings.load to read settings from a configura…
Browse files Browse the repository at this point in the history
…tion file

It used to be in the `write_settings` method but does not provide an easy way for third party scripts to load settings from an arbitray file
  • Loading branch information
YuukanOO committed May 4, 2019
1 parent 688408d commit a9be0f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 12 additions & 3 deletions pytlas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def func(**kwargs):

# And read it if it exists
if conf and os.path.isfile(conf):
global config_loaded_from_path
config_loaded_from_path = os.path.abspath(conf)
config.read(config_loaded_from_path)
load(conf)

# And then, for each argument, write its value in the config object
for (k, v) in kwargs.items():
Expand Down Expand Up @@ -120,6 +118,17 @@ def stringify(value):

return str(value)

def load(filepath):
"""Read settings from the given filepath.
Args:
filepath (str): Filepath to be loaded
"""

global config_loaded_from_path
config_loaded_from_path = os.path.abspath(filepath)
config.read(config_loaded_from_path)

def set(setting, value, section=DEFAULT_SECTION):
"""Sets a setting value in the inner config.
Expand Down
19 changes: 17 additions & 2 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
from sure import expect
from unittest.mock import patch, mock_open
from configparser import NoOptionError
from pytlas.settings import write_to_settings, config, DEFAULT_SECTION, SETTING_DEFAULT_REPO_URL, \
SETTING_SKILLS, DEFAULT_SETTING_SKILLS, DEFAULT_SETTING_DEFAULT_REPO_URL, get, set as set_setting, \
getbool, getint, getfloat, getlist, getpath
load, getbool, getint, getfloat, getlist, getpath
import os

class TestSettings:

def test_it_should_load_settings_from_a_path(self):
conf = """
[some_section]
some_key=some_value
"""
conf_path = '/a/path/to/a/file.conf'

with patch('builtins.open', mock_open(read_data=conf)):
expect(get('some_key', section='some_section')).to.be.none

load(conf_path)

expect(get('some_key', section='some_section')).to.equal('some_value')

def test_it_should_set_the_setting_even_if_the_section_does_not_exists_yet(self):
set_setting('my_key', 'a value', section='my_section')

Expand Down Expand Up @@ -137,4 +152,4 @@ def test_it_should_returns_an_absolute_path_when_asked_to(self):

expect(getpath('a key', section='paths', additional_lookup={
'PATHS_A_KEY': 'var/data',
})).to.equal(os.path.abspath('var/data'))
})).to.equal(os.path.abspath('var/data'))

0 comments on commit a9be0f5

Please sign in to comment.