4 from webob
import Request
, exc
8 from mediagoblin
import routing
, util
11 class Error(Exception): pass
12 class ImproperlyConfigured(Error
): pass
15 def load_controller(string
):
16 module_name
, func_name
= string
.split(':', 1)
17 __import__(module_name
)
18 module
= sys
.modules
[module_name
]
19 func
= getattr(module
, func_name
)
23 class MediagoblinApp(object):
25 Really basic wsgi app using routes and WebOb.
27 def __init__(self
, database
, user_template_path
=None):
28 self
.template_env
= util
.get_jinja_env(user_template_path
)
30 self
.routing
= routing
.get_mapper()
32 def __call__(self
, environ
, start_response
):
33 request
= Request(environ
)
34 path_info
= request
.path_info
35 route_match
= self
.routing
.match(path_info
)
38 if route_match
is None:
39 # Try to do see if we have a match with a trailing slash
40 # added and if so, redirect
41 if not path_info
.endswith('/') \
42 and request
.method
== 'GET' \
43 and self
.routing
.match(path_info
+ '/'):
44 new_path_info
= path_info
+ '/'
46 new_path_info
= '%s?%s' % (
47 new_path_info
, urllib
.urlencode(request
.GET
))
48 redirect
= exc
.HTTPTemporaryRedirect(location
=new_path_info
)
49 return request
.get_response(redirect
)(environ
, start_response
)
51 # Okay, no matches. 404 time!
52 return exc
.HTTPNotFound()(environ
, start_response
)
54 controller
= load_controller(route_match
['controller'])
55 request
.start_response
= start_response
57 request
.matchdict
= route_match
59 request
.template_env
= self
.template_env
60 request
.urlgen
= routes
.URLGenerator(self
.routing
, environ
)
62 return controller(request
)(environ
, start_response
)
65 def paste_app_factory(global_config
, **kw
):
66 connection
= pymongo
.Connection()
67 db
= connection
[kw
.get('db_name', 'mediagoblin')]
69 return MediagoblinApp(
71 user_template_path
=kw
.get('local_templates'))