Initial mediagoblin structure
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 17 Jul 2010 16:33:08 +0000 (11:33 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 17 Jul 2010 16:33:08 +0000 (11:33 -0500)
.gitignore [new file with mode: 0644]
mediagoblin/__init__.py [new file with mode: 0644]
mediagoblin/app.py [new file with mode: 0644]
mediagoblin/routing.py [new file with mode: 0644]
mediagoblin/util.py [new file with mode: 0644]
mediagoblin/views.py [new file with mode: 0644]
setup.py [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..0ed4802
--- /dev/null
@@ -0,0 +1,9 @@
+dist/
+bin/
+develop-eggs/
+build/
+eggs/
+.installed.cfg
+wsgit.egg-info
+*.pyc
+*.pyo
\ No newline at end of file
diff --git a/mediagoblin/__init__.py b/mediagoblin/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/mediagoblin/app.py b/mediagoblin/app.py
new file mode 100644 (file)
index 0000000..41ab7f2
--- /dev/null
@@ -0,0 +1,64 @@
+import sys
+import urllib
+
+from webob import Request, exc
+import routes
+
+from mediagoblin import routing, util
+
+
+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.
+    """
+    def __init__(self, user_template_path=None):
+        self.template_env = util.get_jinja_env(user_template_path)
+
+    def __call__(self, environ, start_response):
+        request = Request(environ)
+        path_info = request.path_info
+        route_match = routing.mapping.match(path_info)
+
+        # No matching page?
+        if route_match is None:
+            # Try to do see if we have a match with a trailing slash
+            # added and if so, redirect
+            if not path_info.endswith('/') \
+                    and request.method == 'GET' \
+                    and routing.mapping.match(path_info + '/'):
+                new_path_info = path_info + '/'
+                if request.GET:
+                    new_path_info = '%s?%s' % (
+                        new_path_info, urllib.urlencode(request.GET))
+                redirect = exc.HTTPTemporaryRedirect(location=new_path_info)
+                return request.get_response(redirect)(environ, start_response)
+
+            # Okay, no matches.  404 time!
+            return exc.HTTPNotFound()(environ, start_response)
+
+        controller = load_controller(route_match['controller'])
+        request.start_response = start_response
+
+        request.matchdict = route_match
+        request.app = self
+        request.template_env = self.template_env
+        request.urlgen = routes.URLGenerator(routing.mapping, environ)
+
+        return controller(request)(environ, start_response)
+
+
+def paste_app_factory(global_config, **kw):
+    return MediagoblinApp(
+        user_template_path=kw.get('local_templates'))
diff --git a/mediagoblin/routing.py b/mediagoblin/routing.py
new file mode 100644 (file)
index 0000000..fec0837
--- /dev/null
@@ -0,0 +1,7 @@
+from routes import Mapper
+
+mapping = Mapper()
+mapping.minimization = False
+
+mapping.connect(
+    "index", "/", controller="mediagoblin.views:root_view")
diff --git a/mediagoblin/util.py b/mediagoblin/util.py
new file mode 100644 (file)
index 0000000..2af9b38
--- /dev/null
@@ -0,0 +1,11 @@
+import jinja2
+
+def get_jinja_env(user_template_path=None):
+    if user_template_path:
+        loader = jinja2.ChoiceLoader(
+            [jinja2.FileSystemLoader(user_template_path),
+             jinja2.PackageLoader('mediagoblin', 'templates')])
+    else:
+        loader = jinja2.PackageLoader('mediagoblin', 'templates')
+
+    return jinja2.Environment(loader=loader, autoescape=True)
diff --git a/mediagoblin/views.py b/mediagoblin/views.py
new file mode 100644 (file)
index 0000000..1b10945
--- /dev/null
@@ -0,0 +1,4 @@
+from webob import Response, exc
+
+def root_view(request):
+    return Response("This is the root")
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..c19e801
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,17 @@
+from setuptools import setup, find_packages
+
+import sys
+
+setup(
+    name = "mediagoblin",
+    version = "0.0.1",
+    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+    zip_safe=False,
+    license = 'AGPLv3',
+    author = 'Christopher Webber',
+    author_email = 'cwebber@dustycloud.org',
+    entry_points = """\
+      [paste.app_factory]
+      mediagoblin = mediagoblin.app:paste_app_factory
+      """,
+    )