Created
May 1, 2010 02:21
-
-
Save dound/385979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a simple example showing how to use LazyControllerLoader.py | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp.util import run_wsgi_app | |
from LazyControllerLoader import url_list | |
url_mappings = [ | |
('/', 'controllers.Home.Home'), | |
('/.*', 'controllers.PageNotFound.PageNotFound'), | |
] | |
app = webapp.WSGIApplication(url_list(url_mappings), debug=True) | |
def main(): | |
run_wsgi_app(app) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from http://appengine-cookbook.appspot.com/recipe/lazy-loading-webapp-handler/ | |
def _istring(import_name): | |
"""Imports an object based on a string. | |
@param import_name the dotted name for the object to import. | |
@return imported object | |
""" | |
module, obj = import_name.rsplit('.', 1) | |
# __import__ can't handle unicode strings in fromlist if module is a package | |
if isinstance(obj, unicode): | |
obj = obj.encode('utf-8') | |
return getattr(__import__(module, None, None, [obj]), obj) | |
class _lazy(object): | |
"""Handles lazily importing and instantiating a class.""" | |
def __init__(self, path): | |
"""Specify the path to the class to lazily import and instantiate.""" | |
self.path = path | |
self.name = path.split(".")[-1] | |
self.cls = None | |
def __call__(self): | |
"""Import the specified class and return a new instantiation of it.""" | |
if not self.cls: | |
self.cls = _istring(self.path) | |
return self.cls() | |
def url_list(mappings): | |
"""Creates a webapp-compatible list of URL mappings from mappings from URL | |
patterns (like the default) -> string containing the path to the request | |
handler object. The values in the returned list are wrappers around the | |
handler objects which only perform the import of the handler when actually | |
called. | |
""" | |
return [(m[0], _lazy(m[1])) for m in mappings] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment