-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specialized Box as Dynabox to allow upper and lower case access
- Loading branch information
1 parent
b1ee56b
commit 05d0184
Showing
6 changed files
with
139 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
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 |
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,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' |
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