Skip to content

Commit b558a4a

Browse files
committed
Merge pull request #35 from NYCPython/validate-after-all-settings
Change how required settings are handled
2 parents 8883fc6 + 1aee34d commit b558a4a

File tree

10 files changed

+78
-14
lines changed

10 files changed

+78
-14
lines changed

.coveragerc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[run]
2+
branch = True
3+
4+
[report]
5+
exclude_lines =
6+
if __name__ == '__main__:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Local/private stuffs
2+
instance
13
settings.cfg
24

35
# Frontend preprocessors

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ A list of issues can be found on GitHub_. Issues are categorized as graphics
9696

9797
Pick one and start hacking away!
9898

99+
Testing
100+
-------
101+
102+
Before you push your changes back to GitHub and submit a pull request, you
103+
probably want to run the test suite. To prepare for running the tests, install
104+
the testing requirements::
105+
106+
$ pip install -r tests/requirements.txt
107+
108+
The test suite can be run with tox_::
109+
110+
$ tox
111+
99112
After you're done, be sure to add your name and GitHub profile to AUTHORS.rst.
100113

101114
.. _GitHub: https://github.com/NYCPython/nycpython.com/issues
115+
.. _tox: http://tox.rtfd.org

nycpython/factory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from flask import Flask
44

5-
from nycpython.utils import register_blueprints
5+
from nycpython.utils import check_required_settings, register_blueprints
66

77
__all__ = 'create_app',
88

@@ -25,6 +25,8 @@ def create_app(package_name, package_path, settings_override=None,
2525
app.config.from_pyfile('settings.cfg', silent=True)
2626
app.config.from_object(settings_override)
2727

28+
check_required_settings(app.config)
29+
2830
register_blueprints(app, package_name, package_path)
2931

3032
return app

nycpython/settings.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@
22

33
from os import environ as env
44

5+
from nycpython.utils import DOES_NOT_EXIST
6+
57

68
def bool_(key, default):
79
"""Return an environment setting represented as a boolean."""
810
return env.get(key, str(default)).lower() == 'true'
911

1012

11-
def required(key):
12-
"""Return the value for a required environment setting."""
13-
try:
14-
return env[key]
15-
except KeyError:
16-
message = 'The {} environment setting is required.'.format(key)
17-
raise RuntimeError(message)
18-
19-
2013
DEBUG = bool_('DEBUG', False)
21-
SECRET_KEY = required('SECRET_KEY')
14+
SECRET_KEY = env.get('SECRET_KEY', DOES_NOT_EXIST)
2215

2316
# Flask-Assets
24-
ASSESTS_BUDEG = bool_('ASSETS_DEBUG', False)
17+
ASSESTS_DEBUG = bool_('ASSETS_DEBUG', False)
2518

2619
# Flask-Cache
2720
CACHE_REDIS_URL = env.get('CACHE_REDIS_URL')
@@ -33,4 +26,4 @@ def required(key):
3326

3427
del bool_
3528
del env
36-
del required
29+
del DOES_NOT_EXIST

nycpython/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55

66
from flask import Blueprint
77

8-
__all__ = 'register_blueprints',
8+
__all__ = 'check_required_settings', 'register_blueprints',
9+
10+
DOES_NOT_EXIST = '!@DNE@!' # Placeholder value to use for missing settings.
11+
12+
13+
def check_required_settings(config, keys=('SECRET_KEY',)):
14+
"""Validate the presence of required settings."""
15+
for key in keys:
16+
if config.get(key, DOES_NOT_EXIST) == DOES_NOT_EXIST:
17+
message = 'The {} configuration settings is required.'.format(key)
18+
raise RuntimeError(message)
919

1020

1121
def register_blueprints(app, package_name, package_path):

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from setuptools import setup
2+
3+
setup(name='nycpython', version='1.0.0')

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tox

tests/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from nycpython import utils
4+
5+
6+
@pytest.fixture(scope='module')
7+
def config():
8+
return {
9+
'KEY1': 1,
10+
'KEY2': 2,
11+
}
12+
13+
14+
def test_check_required_settings(config):
15+
assert utils.check_required_settings(config, ('KEY1', 'KEY2')) is None
16+
17+
18+
def test_check_required_settings_missing_key(config):
19+
with pytest.raises(RuntimeError) as e:
20+
utils.check_required_settings(config, ('KEY1', 'KEY3'))
21+
assert 'KEY3' in e.value.args[0]

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[tox]
2+
envlist = py33
3+
4+
[testenv]
5+
deps =
6+
-r{toxinidir}/requirements.txt
7+
coverage
8+
factory_boy
9+
pytest
10+
commands =
11+
coverage run --source nycpython -m pytest tests
12+
coverage report -m

0 commit comments

Comments
 (0)