Skip to content

Commit

Permalink
Change how required settings are handled
Browse files Browse the repository at this point in the history
Certain settings are going to be required for the application to run.
Currently `SECRET_KEY` is the only one, but the list will most likely
grow over time. The application factory allows for the settings in
`settings.py` to be overridden a couple of different ways. Required
settings should be checked after all of the overrides have been
processed.

This is being implemented in the form of a helper function that checks
for the required keys. Because `settings.py` will add the key to the
configuration, `DOES_NOT_EXIST` is a value that can be assigned to a key
that is intended to be required. `check_required_settings` will respect
both the absence of the key and the value `DOES_NOT_EXIST`.

Along with this change comes some tests. These two tests are the
groundwork for the eventual suite of tests. `tox` can be used to run the
tests. `tox` requires a `setup.py` in order to install the package into
the virtualenv, so a minimal one is being added for that purpose.

Fixes #32
  • Loading branch information
dirn committed Feb 26, 2014
1 parent 8883fc6 commit c2ea6ab
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
branch = True

[report]
exclude_lines =
if __name__ == '__main__:
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Local/private stuffs
instance
settings.cfg

# Frontend preprocessors
Expand Down
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ A list of issues can be found on GitHub_. Issues are categorized as graphics

Pick one and start hacking away!

Testing
-------

Before you push your changes back to GitHub and submit a pull request, you
probably want to run the test suite. To prepare for running the tests, install
the testing requirements::

$ pip install -r tests/requirements.txt

The test suite can be run with tox_::

$ tox

After you're done, be sure to add your name and GitHub profile to AUTHORS.rst.

.. _GitHub: https://github.com/NYCPython/nycpython.com/issues
.. _tox: http://tox.rtfd.org
4 changes: 3 additions & 1 deletion nycpython/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import Flask

from nycpython.utils import register_blueprints
from nycpython.utils import check_required_settings, register_blueprints

__all__ = 'create_app',

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

check_required_settings(app.config)

register_blueprints(app, package_name, package_path)

return app
17 changes: 5 additions & 12 deletions nycpython/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@

from os import environ as env

from nycpython.utils import DOES_NOT_EXIST


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


def required(key):
"""Return the value for a required environment setting."""
try:
return env[key]
except KeyError:
message = 'The {} environment setting is required.'.format(key)
raise RuntimeError(message)


DEBUG = bool_('DEBUG', False)
SECRET_KEY = required('SECRET_KEY')
SECRET_KEY = env.get('SECRET_KEY', DOES_NOT_EXIST)

# Flask-Assets
ASSESTS_BUDEG = bool_('ASSETS_DEBUG', False)
ASSESTS_DEBUG = bool_('ASSETS_DEBUG', False)

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

del bool_
del env
del required
del DOES_NOT_EXIST
12 changes: 11 additions & 1 deletion nycpython/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@

from flask import Blueprint

__all__ = 'register_blueprints',
__all__ = 'check_required_settings', 'register_blueprints',

DOES_NOT_EXIST = '!@DNE@!' # Placeholder value to use for missing settings.


def check_required_settings(config, keys=('SECRET_KEY',)):
"""Validate the presence of required settings."""
for key in keys:
if config.get(key, DOES_NOT_EXIST) == DOES_NOT_EXIST:
message = 'The {} configuration settings is required.'.format(key)
raise RuntimeError(message)


def register_blueprints(app, package_name, package_path):
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from setuptools import setup

setup(name='nycpython', version='1.0.0')
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tox
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from nycpython import utils


@pytest.fixture(scope='module')
def config():
return {
'KEY1': 1,
'KEY2': 2,
}


def test_check_required_settings(config):
assert utils.check_required_settings(config, ('KEY1', 'KEY2')) is None


def test_check_required_settings_missing_key(config):
with pytest.raises(RuntimeError) as e:
utils.check_required_settings(config, ('KEY1', 'KEY3'))
assert 'KEY3' in e.value.args[0]
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tox]
envlist = py33

[testenv]
deps =
coverage
factory_boy
pytest
commands =
coverage run --source nycpython -m pytest tests
coverage report -m

0 comments on commit c2ea6ab

Please sign in to comment.