Skip to content

Use JSON standards for parsing #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions flask_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import json


class MetaFlaskEnv(type):
Expand Down Expand Up @@ -29,22 +30,12 @@ def __init__(cls, name, bases, dict):
if not load_all and not hasattr(cls, key):
continue

# If value is "true" or "false", parse as a boolean
# Otherwise, if it contains a "." then try to parse as a float
# Otherwise, try to parse as an integer
# If all else fails, just keep it a string
if value.lower() in ('true', 'false'):
value = True if value.lower() == 'true' else False
elif '.' in value:
try:
value = float(value)
except ValueError:
pass
else:
try:
value = int(value)
except ValueError:
pass
# Parse value according to JSON standards
# If that fails, just keep it a string
try:
value = json.loads(value)
except ValueError:
pass

# Update our config with the value from `os.environ`
setattr(cls, key, value)
24 changes: 20 additions & 4 deletions test_flask_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_parsing_boolean(self):
env = dict(
IS_TRUE='true',
IS_NOT_TRUE='true-ish',
IS_FALSE='FALSE',
IS_FALSE='false',
IS_WACKY_FALSE='FaLSe',
)
with self.with_env(**env):
Expand All @@ -91,7 +91,7 @@ def test_parsing_boolean(self):
self.assertEqual(TestConfiguration.IS_TRUE, True)
self.assertEqual(TestConfiguration.IS_NOT_TRUE, 'true-ish')
self.assertEqual(TestConfiguration.IS_FALSE, False)
self.assertEqual(TestConfiguration.IS_WACKY_FALSE, False)
self.assertEqual(TestConfiguration.IS_WACKY_FALSE, 'FaLSe')

def test_parsing_float(self):
"""A test to ensure that we properly parse floats"""
Expand All @@ -105,8 +105,8 @@ def test_parsing_float(self):
# DEV: Set `env_load_all=True` to keep from having to make default values for each variable
TestConfiguration = self._get_test_configuration(env_load_all=True)
self.assertEqual(TestConfiguration.IS_FLOAT, 12.5)
self.assertEqual(TestConfiguration.TRAILING_DOT, 12.0)
self.assertEqual(TestConfiguration.LEADING_DOT, 0.12)
self.assertEqual(TestConfiguration.TRAILING_DOT, '12.')
self.assertEqual(TestConfiguration.LEADING_DOT, '.12')
self.assertEqual(TestConfiguration.IS_NOT_FLOAT, 'This is 6.5')

def test_parsing_int(self):
Expand All @@ -123,6 +123,22 @@ def test_parsing_int(self):
self.assertEqual(TestConfiguration.IS_ZERO, 0)
self.assertEqual(TestConfiguration.IS_NOT_INT, '12fa')

def test_parsing_dict(self):
"""A test to ensure that we properly parse dicts"""
env = dict(
IS_EMPTY_DICT='{}',
IS_DICT_WITH_LIST='{"a": [1, 2, 3]}',
IS_DICT_WITH_STRING='{"foo": "bar"}',
IS_NOT_DICT='{not a dict}',
)
with self.with_env(**env):
# DEV: Set `env_load_all=True` to keep from having to make default values for each variable
TestConfiguration = self._get_test_configuration(env_load_all=True)
self.assertEqual(TestConfiguration.IS_EMPTY_DICT, {})
self.assertEqual(TestConfiguration.IS_DICT_WITH_LIST, {'a': [1, 2, 3]})
self.assertEqual(TestConfiguration.IS_DICT_WITH_STRING, {'foo': 'bar'})
self.assertEqual(TestConfiguration.IS_NOT_DICT, '{not a dict}')


if __name__ == '__main__':
unittest.main()