diff options
-rw-r--r-- | mediagoblin/app.py | 16 | ||||
-rw-r--r-- | mediagoblin/tests/test_util.py | 30 | ||||
-rw-r--r-- | mediagoblin/util.py | 18 |
3 files changed, 53 insertions, 11 deletions
diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 195d4792..0316b43c 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -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')) diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py new file mode 100644 index 00000000..0e7a2967 --- /dev/null +++ b/mediagoblin/tests/test_util.py @@ -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 diff --git a/mediagoblin/util.py b/mediagoblin/util.py index bd509256..cf03a93e 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -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 |