Changing conf.app.modules and conf.app.root to string representations.

This commit is contained in:
Ryan Petrello
2012-03-05 15:23:08 -05:00
parent 4d18835e9f
commit 7ba057b4d2
8 changed files with 47 additions and 15 deletions

View File

@@ -35,7 +35,7 @@ class Command(paste_command.Command):
def get_package_names(self, config):
if not hasattr(config.app, 'modules'):
return []
return [module.__name__ for module in config.app.modules if hasattr(module, '__name__')]
return config.app.modules
def import_module(self, package, name):
parent = __import__(package, fromlist=[name])

View File

@@ -188,7 +188,8 @@ class Pecan(object):
'''
Creates a Pecan application instance, which is a WSGI application.
:param root: The root controller object.
:param root: A string representing a root controller object (e.g.,
"myapp.controller.root.RootController")
:param default_renderer: The default rendering engine to use. Defaults to mako.
:param template_path: The default relative path to use for templates. Defaults to 'templates'.
:param hooks: A list of Pecan hook objects to use for this application.
@@ -197,12 +198,40 @@ class Pecan(object):
:param force_canonical: A boolean indicating if this project should require canonical URLs.
'''
if isinstance(root, basestring):
root = self.__translate_root__(root)
self.root = root
self.renderers = RendererFactory(custom_renderers, extra_template_vars)
self.default_renderer = default_renderer
self.hooks = hooks
self.template_path = template_path
self.force_canonical = force_canonical
def __translate_root__(self, item):
'''
Creates a root controller instance from a string root, e.g.,
> __translate_root__("myproject.controllers.RootController")
myproject.controllers.RootController()
:param item: The string to the item
'''
if '.' in item:
parts = item.split('.')
name = '.'.join(parts[:-1])
fromlist = parts[-1:]
try:
module = __import__(name, fromlist=fromlist)
kallable = getattr(module, parts[-1])
assert hasattr(kallable, '__call__'), "%s does not represent a callable class or function." % item
return kallable()
except AttributeError, e:
raise ImportError('No item named %s' % item)
raise ImportError('No item named %s' % item)
def route(self, node, path):
'''

View File

@@ -4,7 +4,7 @@ def deploy(config_module_or_path):
set_config(config_module_or_path)
for module in getattr(conf.app, 'modules'):
try:
module_app = import_module('%s.app' % module.__name__)
module_app = import_module('%s.app' % module)
if hasattr(module_app, 'setup_app'):
return module_app.setup_app(conf)
except ImportError:

View File

@@ -1,7 +1,3 @@
from ${package}.controllers.root import RootController
import ${package}
# Server Specific Configurations
server = {
'port' : '8080',
@@ -10,8 +6,8 @@ server = {
# Pecan Application Configurations
app = {
'root' : RootController(),
'modules' : [${package}],
'root' : '${package}.controllers.root.RootController',
'modules' : ['${package}'],
'static_root' : '%(confdir)s/public',
'template_path' : '%(confdir)s/${package}/templates',
'reload' : True,

View File

@@ -11,6 +11,9 @@ from pecan.decorators import accept_noncanonical
import os
class SampleRootController(object): pass
class TestBase(TestCase):
def test_simple_app(self):
@@ -31,6 +34,10 @@ class TestBase(TestCase):
r = app.get('/index.html')
assert r.status_int == 200
assert r.body == 'Hello, World!'
def test_controller_lookup_by_string_path(self):
app = Pecan('pecan.tests.test_base.SampleRootController')
assert app.root and isinstance(app.root, SampleRootController)
def test_object_dispatch(self):
class SubSubController(object):

View File

@@ -1,7 +1,7 @@
import sample_app
app = {
'modules': [sample_app]
'modules': ['sample_app']
}
foo = {

View File

@@ -1,7 +1,7 @@
import sample_app_missing
app = {
'modules': [sample_app_missing]
'modules': ['sample_app_missing']
}
foo = {

View File

@@ -16,7 +16,7 @@ class TestDeploy(TestCase):
def test_module_lookup(self):
"""
1. A config file has:
app { 'modules': [valid_module] }
app { 'modules': ['valid_module'] }
2. The module, `valid_module` has an app.py that defines a `def setup.py`
"""
test_config_file = os.path.join(os.path.dirname(__file__), 'test_config', 'sample_apps', 'sample_app_config.py')
@@ -25,7 +25,7 @@ class TestDeploy(TestCase):
def test_module_lookup_find_best_match(self):
"""
1. A config file has:
app { 'modules': [invalid_module, valid_module] }
app { 'modules': ['invalid_module', 'valid_module'] }
2. The module, `valid_module` has an app.py that defines a `def setup_app`
"""
test_config_file = os.path.join(os.path.dirname(__file__), 'test_config', 'sample_apps', 'sample_app_config.py')
@@ -34,7 +34,7 @@ class TestDeploy(TestCase):
def test_missing_app_file_lookup(self):
"""
1. A config file has:
app { 'modules': [valid_module] }
app { 'modules': ['valid_module'] }
2. The module has no `app.py` file.
"""
test_config_file = os.path.join(os.path.dirname(__file__), 'test_config', 'sample_apps', 'sample_app_config_missing.py')
@@ -47,7 +47,7 @@ class TestDeploy(TestCase):
def test_missing_setup_app(self):
"""
1. A config file has:
app { 'modules': [valid_module] }
app { 'modules': ['valid_module'] }
2. The module, `valid_module` has an `app.py` that contains no `def setup_app`
"""
test_config_file = os.path.join(os.path.dirname(__file__), 'test_config', 'sample_apps', 'sample_app_config_missing_app.py')