Skip to content

Commit

Permalink
support toml files ( fix #57 )
Browse files Browse the repository at this point in the history
  • Loading branch information
drgarcia1986 committed Mar 7, 2017
1 parent c656a35 commit 731dd7a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Python Simple Settings
A simple way to manage your project settings.

It is inspired by Django's settings system but is generic for any python project.
With simple-settings you just need specify your settings module in ``--settings`` arg of command line (or ``SIMPLE_SETTINGS`` of environment) and all settings will be available in ``simple_settings.settings``.
With simple-settings you just need specify your settings module in ``--simple-settings`` arg of command line (or ``SIMPLE_SETTINGS`` of environment) and all settings will be available in ``simple_settings.settings``.

Installation
------------
Expand All @@ -45,7 +45,7 @@ Usage

.. code-block:: bash
$ python app.py --settings=my_settings
$ python app.py --simple-settings=my_settings
.. code-block:: python
Expand All @@ -57,7 +57,7 @@ Usage
Some features
-------------
* Settings by Python modules, Cfg files, Yaml files or Json files.
* Settings by Python modules, Cfg, Yaml, Toml or Json files.
* Settings inheritance (like a pipeline).
* Special settings.
* Dynamic settings.
Expand Down
7 changes: 6 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ The simple-settings is prepared to play with the following files types:
* cfg files (simple `key=value` files).
* yaml files.
* json files.
* toml files.

> To simple-settings load settings of yaml files is necessary install with extras require _yaml_ ex: `pip install simple-settings[yaml]`
> To simple-settings load settings of yaml files is necessary install with extras require _yaml_ ex: `pip install simple-settings[yaml]`
> For toml files is necessary install with extras require _toml_ ex: `pip install simple-settings[toml]`
## Load multiple settings modules
simple-settings can load more than one setting module without use import approach, just specify yours settings modules separated by comma.
Expand Down Expand Up @@ -335,6 +337,9 @@ assert settings.SOME_SETTING == 'bar'
```

## Changelog
### [NEXT_RELEASE]
* Load settings of _toml_ files.

### [0.11.0] - 2017-02-17
* Autoconfigure python logging with `CONFIGURE_LOGGING` _special setting_.

Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ def read(fname):
long_description = description

YAML_REQUIRES = ['PyYAML==3.11']
TOML_REQUIRES = ['toml==0.9.2']
DYNAMIC_SETTINGS_REQUIRES = ['jsonpickle==0.9.3']
REDIS_REQUIRES = ['redis==2.10.5', 'six==1.10.0'] + DYNAMIC_SETTINGS_REQUIRES
CONSUL_REQUIRES = ['consulate==0.6.0'] + DYNAMIC_SETTINGS_REQUIRES
DATABASE_REQUIRES = ['SQLAlchemy==1.0.13'] + DYNAMIC_SETTINGS_REQUIRES

ALL_REQUIRES = set(
YAML_REQUIRES +
TOML_REQUIRES +
REDIS_REQUIRES +
CONSUL_REQUIRES +
DATABASE_REQUIRES
Expand Down Expand Up @@ -66,5 +68,6 @@ def read(fname):
'database': DATABASE_REQUIRES,
'redis': REDIS_REQUIRES,
'yaml': YAML_REQUIRES,
'toml': TOML_REQUIRES,
}
)
10 changes: 10 additions & 0 deletions simple_settings/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
except ImportError: # pragma: no cover
pass

toml_strategy = None
try:
from .toml_file import SettingsLoadStrategyToml
toml_strategy = SettingsLoadStrategyToml
except ImportError: # pragma: no cover
pass


strategies = (
SettingsLoadStrategyPython,
Expand All @@ -19,3 +26,6 @@

if yaml_strategy:
strategies += (yaml_strategy,)

if toml_strategy:
strategies += (toml_strategy,)
20 changes: 20 additions & 0 deletions simple_settings/strategies/toml_file.py
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())
10 changes: 10 additions & 0 deletions tests/samples/simple_toml_file.toml
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"
33 changes: 33 additions & 0 deletions tests/strategies/test_toml.py
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

0 comments on commit 731dd7a

Please sign in to comment.