Changing conf.app.modules and conf.app.root to string representations.
This commit is contained in:
@@ -35,7 +35,7 @@ class Command(paste_command.Command):
|
|||||||
def get_package_names(self, config):
|
def get_package_names(self, config):
|
||||||
if not hasattr(config.app, 'modules'):
|
if not hasattr(config.app, 'modules'):
|
||||||
return []
|
return []
|
||||||
return [module.__name__ for module in config.app.modules if hasattr(module, '__name__')]
|
return config.app.modules
|
||||||
|
|
||||||
def import_module(self, package, name):
|
def import_module(self, package, name):
|
||||||
parent = __import__(package, fromlist=[name])
|
parent = __import__(package, fromlist=[name])
|
||||||
|
|||||||
@@ -188,7 +188,8 @@ class Pecan(object):
|
|||||||
'''
|
'''
|
||||||
Creates a Pecan application instance, which is a WSGI application.
|
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 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 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.
|
:param hooks: A list of Pecan hook objects to use for this application.
|
||||||
@@ -197,6 +198,9 @@ class Pecan(object):
|
|||||||
:param force_canonical: A boolean indicating if this project should require canonical URLs.
|
: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.root = root
|
||||||
self.renderers = RendererFactory(custom_renderers, extra_template_vars)
|
self.renderers = RendererFactory(custom_renderers, extra_template_vars)
|
||||||
self.default_renderer = default_renderer
|
self.default_renderer = default_renderer
|
||||||
@@ -204,6 +208,31 @@ class Pecan(object):
|
|||||||
self.template_path = template_path
|
self.template_path = template_path
|
||||||
self.force_canonical = force_canonical
|
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):
|
def route(self, node, path):
|
||||||
'''
|
'''
|
||||||
Looks up a controller from a node based upon the specified path.
|
Looks up a controller from a node based upon the specified path.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ def deploy(config_module_or_path):
|
|||||||
set_config(config_module_or_path)
|
set_config(config_module_or_path)
|
||||||
for module in getattr(conf.app, 'modules'):
|
for module in getattr(conf.app, 'modules'):
|
||||||
try:
|
try:
|
||||||
module_app = import_module('%s.app' % module.__name__)
|
module_app = import_module('%s.app' % module)
|
||||||
if hasattr(module_app, 'setup_app'):
|
if hasattr(module_app, 'setup_app'):
|
||||||
return module_app.setup_app(conf)
|
return module_app.setup_app(conf)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
from ${package}.controllers.root import RootController
|
|
||||||
|
|
||||||
import ${package}
|
|
||||||
|
|
||||||
# Server Specific Configurations
|
# Server Specific Configurations
|
||||||
server = {
|
server = {
|
||||||
'port' : '8080',
|
'port' : '8080',
|
||||||
@@ -10,8 +6,8 @@ server = {
|
|||||||
|
|
||||||
# Pecan Application Configurations
|
# Pecan Application Configurations
|
||||||
app = {
|
app = {
|
||||||
'root' : RootController(),
|
'root' : '${package}.controllers.root.RootController',
|
||||||
'modules' : [${package}],
|
'modules' : ['${package}'],
|
||||||
'static_root' : '%(confdir)s/public',
|
'static_root' : '%(confdir)s/public',
|
||||||
'template_path' : '%(confdir)s/${package}/templates',
|
'template_path' : '%(confdir)s/${package}/templates',
|
||||||
'reload' : True,
|
'reload' : True,
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ from pecan.decorators import accept_noncanonical
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class SampleRootController(object): pass
|
||||||
|
|
||||||
|
|
||||||
class TestBase(TestCase):
|
class TestBase(TestCase):
|
||||||
|
|
||||||
def test_simple_app(self):
|
def test_simple_app(self):
|
||||||
@@ -32,6 +35,10 @@ class TestBase(TestCase):
|
|||||||
assert r.status_int == 200
|
assert r.status_int == 200
|
||||||
assert r.body == 'Hello, World!'
|
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):
|
def test_object_dispatch(self):
|
||||||
class SubSubController(object):
|
class SubSubController(object):
|
||||||
@expose()
|
@expose()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import sample_app
|
import sample_app
|
||||||
|
|
||||||
app = {
|
app = {
|
||||||
'modules': [sample_app]
|
'modules': ['sample_app']
|
||||||
}
|
}
|
||||||
|
|
||||||
foo = {
|
foo = {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import sample_app_missing
|
import sample_app_missing
|
||||||
|
|
||||||
app = {
|
app = {
|
||||||
'modules': [sample_app_missing]
|
'modules': ['sample_app_missing']
|
||||||
}
|
}
|
||||||
|
|
||||||
foo = {
|
foo = {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class TestDeploy(TestCase):
|
|||||||
def test_module_lookup(self):
|
def test_module_lookup(self):
|
||||||
"""
|
"""
|
||||||
1. A config file has:
|
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`
|
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')
|
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):
|
def test_module_lookup_find_best_match(self):
|
||||||
"""
|
"""
|
||||||
1. A config file has:
|
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`
|
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')
|
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):
|
def test_missing_app_file_lookup(self):
|
||||||
"""
|
"""
|
||||||
1. A config file has:
|
1. A config file has:
|
||||||
app { 'modules': [valid_module] }
|
app { 'modules': ['valid_module'] }
|
||||||
2. The module has no `app.py` file.
|
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')
|
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):
|
def test_missing_setup_app(self):
|
||||||
"""
|
"""
|
||||||
1. A config file has:
|
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`
|
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')
|
test_config_file = os.path.join(os.path.dirname(__file__), 'test_config', 'sample_apps', 'sample_app_config_missing_app.py')
|
||||||
|
|||||||
Reference in New Issue
Block a user