From 38e4c7d6b0a51a92747e6c928599a7d651362c6c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 27 Nov 2011 00:00:55 -0500 Subject: add get_appsettings API to paster --- CHANGES.txt | 4 ++++ docs/api/paster.rst | 10 +++------ pyramid/paster.py | 30 +++++++++++++++++++++----- pyramid/tests/test_paster.py | 51 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index effce62b9..3e8a157c8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,10 @@ Features - ``bpython`` interpreter compatibility in ``pshell``. See the "Command-Line Pyramid" narrative docs chapter for more information. +- Added ``get_appconfig`` function to ``pyramid.paster`` API. This function + returns the settings defined within an ``[app:...]`` section in a + PasteDeploy ini file. + Bug Fixes --------- diff --git a/docs/api/paster.rst b/docs/api/paster.rst index 2a32e07e9..5cf8bbe2c 100644 --- a/docs/api/paster.rst +++ b/docs/api/paster.rst @@ -5,13 +5,9 @@ .. automodule:: pyramid.paster - .. function:: get_app(config_uri, name=None) + .. autofunction:: bootstrap - Return the WSGI application named ``name`` in the PasteDeploy - config file specified by ``config_uri``. + .. autofunction:: get_app(config_uri, name=None) - If the ``name`` is None, this will attempt to parse the name from - the ``config_uri`` string expecting the format ``inifile#name``. - If no name is found, the name will default to "main". + .. autofunction:: get_appsettings(config_uri, name=None) - .. autofunction:: bootstrap diff --git a/pyramid/paster.py b/pyramid/paster.py index 3fec6c556..2abb3876c 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -1,7 +1,11 @@ import os import zope.deprecation -from paste.deploy import loadapp + +from paste.deploy import ( + loadapp, + appconfig, + ) from pyramid.scripting import prepare @@ -20,16 +24,32 @@ def get_app(config_uri, name=None, loadapp=loadapp): If the ``name`` is None, this will attempt to parse the name from the ``config_uri`` string expecting the format ``inifile#name``. If no name is found, the name will default to "main".""" + path, section = _getpathsec(config_uri, name) + config_name = 'config:%s' % path + here_dir = os.getcwd() + app = loadapp(config_name, name=section, relative_to=here_dir) + return app + +def get_appsettings(config_uri, name=None, appconfig=appconfig): + """ Return a dictionary representing the key/value pairs in an ``app` + section within the file represented by ``config_uri``. + + If the ``name`` is None, this will attempt to parse the name from + the ``config_uri`` string expecting the format ``inifile#name``. + If no name is found, the name will default to "main".""" + path, section = _getpathsec(config_uri, name) + config_name = 'config:%s' % path + here_dir = os.getcwd() + return appconfig(config_name, name=section, relative_to=here_dir) + +def _getpathsec(config_uri, name): if '#' in config_uri: path, section = config_uri.split('#', 1) else: path, section = config_uri, 'main' if name: section = name - config_name = 'config:%s' % path - here_dir = os.getcwd() - app = loadapp(config_name, name=section, relative_to=here_dir) - return app + return path, section def bootstrap(config_uri, request=None): """ Load a WSGI application from the PasteDeploy config file specified diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py index d23c156c2..dc7e4f21f 100644 --- a/pyramid/tests/test_paster.py +++ b/pyramid/tests/test_paster.py @@ -1,6 +1,6 @@ import unittest -class TestGetApp(unittest.TestCase): +class Test_get_app(unittest.TestCase): def _callFUT(self, config_file, section_name, loadapp): from pyramid.paster import get_app return get_app(config_file, section_name, loadapp) @@ -8,7 +8,7 @@ class TestGetApp(unittest.TestCase): def test_it(self): import os app = DummyApp() - loadapp = DummyLoadApp(app) + loadapp = DummyLoadWSGI(app) result = self._callFUT('/foo/bar/myapp.ini', 'myapp', loadapp) self.assertEqual(loadapp.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(loadapp.section_name, 'myapp') @@ -18,7 +18,7 @@ class TestGetApp(unittest.TestCase): def test_it_with_hash(self): import os app = DummyApp() - loadapp = DummyLoadApp(app) + loadapp = DummyLoadWSGI(app) result = self._callFUT('/foo/bar/myapp.ini#myapp', None, loadapp) self.assertEqual(loadapp.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(loadapp.section_name, 'myapp') @@ -28,13 +28,48 @@ class TestGetApp(unittest.TestCase): def test_it_with_hash_and_name_override(self): import os app = DummyApp() - loadapp = DummyLoadApp(app) + loadapp = DummyLoadWSGI(app) result = self._callFUT('/foo/bar/myapp.ini#myapp', 'yourapp', loadapp) self.assertEqual(loadapp.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(loadapp.section_name, 'yourapp') self.assertEqual(loadapp.relative_to, os.getcwd()) self.assertEqual(result, app) +class Test_get_appsettings(unittest.TestCase): + def _callFUT(self, config_file, section_name, appconfig): + from pyramid.paster import get_appsettings + return get_appsettings(config_file, section_name, appconfig) + + def test_it(self): + import os + values = {'a':1} + appconfig = DummyLoadWSGI(values) + result = self._callFUT('/foo/bar/myapp.ini', 'myapp', appconfig) + self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') + self.assertEqual(appconfig.section_name, 'myapp') + self.assertEqual(appconfig.relative_to, os.getcwd()) + self.assertEqual(result, values) + + def test_it_with_hash(self): + import os + values = {'a':1} + appconfig = DummyLoadWSGI(values) + result = self._callFUT('/foo/bar/myapp.ini#myapp', None, appconfig) + self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') + self.assertEqual(appconfig.section_name, 'myapp') + self.assertEqual(appconfig.relative_to, os.getcwd()) + self.assertEqual(result, values) + + def test_it_with_hash_and_name_override(self): + import os + values = {'a':1} + appconfig = DummyLoadWSGI(values) + result = self._callFUT('/foo/bar/myapp.ini#myapp', 'yourapp', appconfig) + self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') + self.assertEqual(appconfig.section_name, 'yourapp') + self.assertEqual(appconfig.relative_to, os.getcwd()) + self.assertEqual(result, values) + class TestBootstrap(unittest.TestCase): def _callFUT(self, config_uri, request=None): from pyramid.paster import bootstrap @@ -82,15 +117,15 @@ class DummyRegistry(object): dummy_registry = DummyRegistry() -class DummyLoadApp: - def __init__(self, app): - self.app = app +class DummyLoadWSGI: + def __init__(self, result): + self.result = result def __call__(self, config_name, name=None, relative_to=None): self.config_name = config_name self.section_name = name self.relative_to = relative_to - return self.app + return self.result class DummyApp: def __init__(self): -- cgit v1.2.3