Moved app.load_controller -> util.import_component and added tests.

This commit is contained in:
Christopher Allan Webber 2011-04-17 09:43:03 -05:00
parent 904f61c298
commit cb8ea0fe3f
3 changed files with 53 additions and 11 deletions

View File

@ -14,10 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import urllib
from beaker.middleware import SessionMiddleware
import routes
import mongokit
from webob import Request, exc
@ -29,14 +27,6 @@ class Error(Exception): pass
class ImproperlyConfigured(Error): pass
def load_controller(string):
module_name, func_name = string.split(':', 1)
__import__(module_name)
module = sys.modules[module_name]
func = getattr(module, func_name)
return func
class MediaGoblinApp(object):
"""
Really basic wsgi app using routes and WebOb.
@ -71,7 +61,7 @@ class MediaGoblinApp(object):
# Okay, no matches. 404 time!
return exc.HTTPNotFound()(environ, start_response)
controller = load_controller(route_match['controller'])
controller = util.import_component(route_match['controller'])
request.start_response = start_response
request.matchdict = route_match
@ -87,9 +77,13 @@ class MediaGoblinApp(object):
def paste_app_factory(global_config, **kw):
# Get the database connection
connection = mongokit.Connection(
kw.get('db_host'), kw.get('db_port'))
# Set up the storage systems.
## TODO: allow for extra storage systems that aren't just
## BasicFileStorage.
mgoblin_app = MediaGoblinApp(
connection, kw.get('db_name', 'mediagoblin'),
user_template_path=kw.get('local_templates'))

View File

@ -0,0 +1,30 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011 Free Software Foundation, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin import util
def _import_component_testing_method(silly_string):
# Just for the sake of testing that our component importer works.
return u"'%s' is the silliest string I've ever seen" % silly_string
def test_import_component():
imported_func = util.import_component(
'mediagoblin.tests.test_util:_import_component_testing_method')
result = imported_func('hooobaladoobala')
expected = u"'hooobaladoobala' is the silliest string I've ever seen"
assert result == expected

View File

@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import jinja2
import mongokit
@ -54,3 +56,19 @@ def setup_user_in_request(request):
request.session.invalidate()
request.user = user
def import_component(import_string):
"""
Import a module component defined by STRING. Probably a method,
class, or global variable.
Args:
- import_string: a string that defines what to import. Written
in the format of "module1.module2:component"
"""
module_name, func_name = import_string.split(':', 1)
__import__(module_name)
module = sys.modules[module_name]
func = getattr(module, func_name)
return func