From 1328b24343c8d450ab930226b7daa7f6d4d9d2fa Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 10 Nov 2010 17:10:34 -0500 Subject: extend asbool to accept None, True, and False, and write explicit tests --- .../pylons_sqla/+package+/__init__.py_tmpl | 22 ++++--------- pyramid/settings.py | 8 ++++- pyramid/tests/test_settings.py | 36 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl b/pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl index 218c2120b..a8df81fd6 100644 --- a/pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl +++ b/pyramid/paster_templates/pylons_sqla/+package+/__init__.py_tmpl @@ -1,6 +1,7 @@ from pyramid.configuration import Configurator +from pyramid.settings import asbool + from pyramid_beaker import session_factory_from_settings -from paste.deploy.converters import asbool def main(global_config, **settings): """ This function returns a Pyramid WSGI application. @@ -15,21 +16,10 @@ def main(global_config, **settings): config.begin() session_factory = session_factory_from_settings(settings) config.set_session_factory(session_factory) - config.add_static_view( - 'static', - '{{package}}:static/' - ) - config.add_handler( - 'main', - '/:action', - '{{package}}.handlers:MyHandler', - ) - config.add_handler( - 'home', - '/', - '{{package}}.handlers:MyHandler', - action='index' - ) + config.add_static_view('static', '{{package}}:static/') + config.add_handler('main', '/:action', '{{package}}.handlers:MyHandler') + config.add_handler('home', '/', '{{package}}.handlers:MyHandler', + action='index') config.add_subscriber('{{package}}.subscribers.add_renderer_globals', 'pyramid.events.BeforeRender') config.end() diff --git a/pyramid/settings.py b/pyramid/settings.py index cdf0b177f..ed2c3a281 100644 --- a/pyramid/settings.py +++ b/pyramid/settings.py @@ -82,7 +82,13 @@ def get_settings(): def asbool(s): """ Return the boolean value ``True`` if the case-lowered value of string input ``s`` is any of ``t``, ``true``, ``y``, ``on``, or ``1``, otherwise - return the boolean value ``False``.""" + return the boolean value ``False``. If ``s`` is the value ``None``, + return ``False``. If ``s`` is already one of the boolean values ``True`` + or ``False``, return it.""" + if s is None: + return False + if s in (True, False): + return s s = str(s).strip() return s.lower() in ('t', 'true', 'y', 'yes', 'on', '1') diff --git a/pyramid/tests/test_settings.py b/pyramid/tests/test_settings.py index 0689a0835..90ed9419b 100644 --- a/pyramid/tests/test_settings.py +++ b/pyramid/tests/test_settings.py @@ -201,3 +201,39 @@ class TestGetSettings(unittest.TestCase): self.config.registry.registerUtility(settings, ISettings) self.assertEqual(self._callFUT(), settings) +class Test_asbool(unittest.TestCase): + def _callFUT(self, s): + from pyramid.settings import asbool + return asbool(s) + + def test_s_is_None(self): + result = self._callFUT(None) + self.assertEqual(result, False) + + def test_s_is_True(self): + result = self._callFUT(True) + self.assertEqual(result, True) + + def test_s_is_False(self): + result = self._callFUT(False) + self.assertEqual(result, False) + + def test_s_is_true(self): + result = self._callFUT('True') + self.assertEqual(result, True) + + def test_s_is_false(self): + result = self._callFUT('False') + self.assertEqual(result, False) + + def test_s_is_yes(self): + result = self._callFUT('yes') + self.assertEqual(result, True) + + def test_s_is_on(self): + result = self._callFUT('on') + self.assertEqual(result, True) + + def test_s_is_1(self): + result = self._callFUT(1) + self.assertEqual(result, True) -- cgit v1.2.3