Skip to content

Commit

Permalink
Specialized Box as Dynabox to allow upper and lower case access
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Jul 4, 2017
1 parent b1ee56b commit 05d0184
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dynaconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import importlib
import logging
from contextlib import contextmanager
from box import Box

import os
import sys
Expand All @@ -17,6 +16,7 @@
from dynaconf.utils.functional import LazyObject, empty
from dynaconf.utils.parse_conf import converters, parse_conf_data
from dynaconf.validator import ValidatorList
from dynaconf.utils.boxing import DynaBox


class LazySettings(LazyObject):
Expand Down Expand Up @@ -423,7 +423,7 @@ def set(self, key, value, loader_identifier=None):
"""Set a value storing references for the loader"""
value = parse_conf_data(value)
if isinstance(value, dict):
value = Box(value, box_it_up=True)
value = DynaBox(value, box_it_up=True)

key = key.strip().upper()
setattr(self, key, value)
Expand Down
29 changes: 29 additions & 0 deletions dynaconf/utils/boxing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8

from box import Box


class DynaBox(Box):
"""Specialized Box for dynaconf
it allows items/attrs to be founf both in upper or lower case"""

def __getattr__(self, item):
try:
return super(DynaBox, self).__getattr__(item)
except (AttributeError, KeyError):
n_item = item.lower() if item.isupper() else item.upper()
return super(DynaBox, self).__getattr__(n_item)

def __getitem__(self, item):
try:
return super(DynaBox, self).__getitem__(item)
except (AttributeError, KeyError):
n_item = item.lower() if item.isupper() else item.upper()
return super(DynaBox, self).__getitem__(n_item)

def get(self, item, default=None):
value = super(DynaBox, self).get(item, default)
if value is None or value == default:
n_item = item.lower() if item.isupper() else item.upper()
value = super(DynaBox, self).get(n_item, default)
return value
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_md_to_rst(file):
include_package_data=True,
zip_safe=False,
platforms='any',
install_requires=['six'],
install_requires=['six', 'python-box'],
tests_require=[
"pytest",
"pytest-cov",
Expand Down
14 changes: 13 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ def settings():
'dynaconf.loaders.env_loader',
'dynaconf.loaders.redis_loader'
],
DYNACONF_NAMESPACE="DYNA%s" % mode
DYNACONF_NAMESPACE="DYNA%s" % mode,
boxed_data={
'HOST': 'server.com',
'port': 8080,
'PARAMS': {
'username': 'admin',
'PASSWORD': 'secret',
'token': {
'TYPE': 1,
'value': 2
}
}
}
)
sets.SIMPLE_BOOL = False
sets.configure()
Expand Down
76 changes: 76 additions & 0 deletions tests/test_dynabox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# coding: utf-8

from dynaconf.utils.boxing import DynaBox


box = DynaBox(
{
'server': {
'HOST': 'server.com',
'port': 8080,
'PARAMS': {
'username': 'admin',
'PASSWORD': 'secret',
'token': {
'TYPE': 1,
'value': 2
}
}
}
},
box_it_up=True
)


def test_datatypes():
assert isinstance(box.server, dict)
assert isinstance(box.server, DynaBox)
assert isinstance(box.server.host, str)
assert isinstance(box.server.PORT, int)


def test_access_lowercase():
assert box.server.host == 'server.com'
assert box.server.port == 8080
assert box.server.params.username == 'admin'
assert box.server.params.password == 'secret'
assert box.server.params.token.type == 1
assert box.server.params.token.value == 2


def test_access_uppercase():
assert box.SERVER.HOST == 'server.com'
assert box.SERVER.PORT == 8080
assert box.SERVER.PARAMS.USERNAME == 'admin'
assert box.SERVER.PARAMS.PASSWORD == 'secret'
assert box.SERVER.PARAMS.TOKEN.TYPE == 1
assert box.SERVER.PARAMS.TOKEN.VALUE == 2


def test_access_items():
assert box['SERVER']['HOST'] == 'server.com'
assert box['SERVER']['PORT'] == 8080
assert box['SERVER']['PARAMS']['USERNAME'] == 'admin'
assert box['SERVER']['PARAMS']['PASSWORD'] == 'secret'
assert box['SERVER']['PARAMS']['TOKEN']['TYPE'] == 1
assert box['SERVER']['PARAMS']['TOKEN']['VALUE'] == 2


def test_access_items_lower():
assert box['server']['HOST'] == 'server.com'
assert box['server']['PORT'] == 8080
assert box['server']['params']['USERNAME'] == 'admin'
assert box['server']['params']['PASSWORD'] == 'secret'
assert box['server']['params']['TOKEN']['TYPE'] == 1
assert box['server']['params']['TOKEN']['VALUE'] == 2


def test_get():
assert box.get('server').get('host') == 'server.com'
assert box.get('server').get('port') == 8080
assert box.get('server').get('params').username == 'admin'
assert box.get('server').get('params').password == 'secret'
assert box.get('server').get('params').token.type == 1
assert box.get('server').get('params').token.value == 2
assert box.get('server').get('blabla') is None
assert box.get('server').get('blabla', 'foo') == 'foo'
18 changes: 18 additions & 0 deletions tests/test_endtoend.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ def test_end_to_end(settings):

with settings.using_namespace('OTHER'):
assert settings.TESTING is True


def test_boxed_data(settings):
assert settings.BOXED_DATA.host == 'server.com'
assert settings.BOXED_DATA.port == 8080
assert settings.BOXED_DATA.params.username == 'admin'
assert settings.BOXED_DATA.params.password == 'secret'
assert settings.BOXED_DATA.params.token.type == 1
assert settings.BOXED_DATA.params.token.value == 2


def test_boxed_data_call(settings):
assert settings('boxed_data').host == 'server.com'
assert settings('boxed_data').port == 8080
assert settings('boxed_data').params.username == 'admin'
assert settings('boxed_data').params.password == 'secret'
assert settings('boxed_data').params.token.type == 1
assert settings('boxed_data').params.token.value == 2

0 comments on commit 05d0184

Please sign in to comment.