-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c656a35
commit 731dd7a
Showing
7 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
import codecs | ||
|
||
import toml | ||
|
||
|
||
class SettingsLoadStrategyToml(object): | ||
""" | ||
This is the strategy used to read settings from toml files | ||
""" | ||
name = 'toml' | ||
|
||
@staticmethod | ||
def is_valid_file(file_name): | ||
return file_name.endswith('.toml') | ||
|
||
@staticmethod | ||
def load_settings_file(settings_file): | ||
with codecs.open(settings_file, 'r') as f: | ||
return toml.loads(f.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
SIMPLE_STRING = "simple" | ||
SIMPLE_INTEGER = 1 | ||
|
||
SIMPLE_BOOL = true | ||
|
||
COMPLEX_LIST = ["foo", "bar"] | ||
|
||
[COMPLEX_DICT] | ||
complex = "dict" | ||
foo = "bar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
skip = False | ||
try: | ||
from simple_settings.strategies.toml_file import SettingsLoadStrategyToml | ||
except ImportError: | ||
skip = True | ||
|
||
|
||
@pytest.mark.skipif(skip, reason='Installed without Toml') | ||
class TestTomlStrategy(object): | ||
|
||
@pytest.fixture | ||
def strategy_toml(self): | ||
return SettingsLoadStrategyToml | ||
|
||
def test_should_check_a_valid_toml_file(self, strategy_toml): | ||
assert strategy_toml.is_valid_file('foo.toml') is True | ||
|
||
def test_should_check_a_invalid_toml_file(self, strategy_toml): | ||
assert strategy_toml.is_valid_file('foo.bar') is False | ||
|
||
def test_should_load_dict_with_settings_of_toml_file(self, strategy_toml): | ||
settings = strategy_toml.load_settings_file( | ||
'tests/samples/simple_toml_file.toml' | ||
) | ||
|
||
assert settings['SIMPLE_STRING'] == 'simple' | ||
assert settings['COMPLEX_DICT'] == {'complex': 'dict', 'foo': 'bar'} | ||
assert settings['COMPLEX_LIST'] == ['foo', 'bar'] | ||
assert settings['SIMPLE_INTEGER'] == 1 | ||
assert settings['SIMPLE_BOOL'] is True |