Permalink
Browse files

Refactored dispatching into gist_it/appengine.py

  • Loading branch information...
1 parent afcff53 commit 4c383900f98be6f6d6ed88614f7bdc83cc952178 @robertkrimen robertkrimen committed Aug 25, 2011
Showing with 96 additions and 71 deletions.
  1. +3 −71 app.py
  2. 0 gist_it.py → gist_it/__init__.py
  3. +93 −0 gist_it/appengine.py
View
74 app.py
@@ -25,24 +25,11 @@
sys.path.append( 'pyl' )
-from versioned_memcache import memcache
-
import jinja2 as jinja2_
jinja2 = jinja2_.Environment( loader=jinja2_.FileSystemLoader( 'jinja2-assets' ) )
-import gist_it
-from gist_it import take_slice
-
-def render_gist_html( base, gist, document ):
- result = jinja2.get_template( 'gist.jinja.html' ).render( cgi = cgi, base = base, gist = gist, document = document )
- return result
-
-def render_gist_js( base, gist, gist_html ):
- result = jinja2.get_template( 'gist.jinja.js' ).render( base = base, gist = gist, gist_html = gist_html )
- return result
-
-def render_gist_js_callback( callback, gist, gist_html ):
- return "%s( '%s', '%s' );" % ( callback, gist_html.encode( 'string_escape' ), gist.raw_path )
+from gist_it import appengine as gist_it_appengine
+gist_it_appengine.jinja2 = jinja2
class RequestHandler( webapp.RequestHandler ):
@@ -62,62 +49,7 @@ def get( self ):
class dispatch_gist_it( RequestHandler ):
def get( self, location ):
- base = self.url_for()
- location = urllib.unquote( location )
- match = gist_it.Gist.match( location )
- self.response.headers['Content-Type'] = 'text/plain';
- if not match:
- self.response.set_status( 404 )
- self.response.out.write( self.response.http_status_message( 404 ) )
- self.response.out.write( "\n" )
- return
-
- else:
- gist = gist_it.Gist.parse( location, slice_ = self.request.get( 'slice' ) )
- if not gist:
- self.response.set_status( 500 )
- self.response.out.write( "Unable to parse \"%s\": Not a valid repository path?" % ( location ) )
- self.response.out.write( "\n" )
- return
-
- if _CACHE_ and self.request.get( 'flush' ):
- self.response.out.write( memcache.delete( memcache_key ) )
- return
-
- memcache_key = gist.raw_url
- data = memcache.get( memcache_key )
- if data is None or not _CACHE_:
- # For below, see: http://stackoverflow.com/questions/2826238/does-google-appengine-cache-external-requests
- response = urlfetch.fetch( gist.raw_url, headers = { 'Cache-Control': 'max-age=300' } )
- if response.status_code != 200:
- if response.status_code == 403:
- self.response.set_status( response.status_code )
- elif response.status_code == 404:
- self.response.set_status( response.status_code )
- else:
- self.response.set_status( 500 )
- self.response.out.write( "Unable to fetch \"%s\": (%i)" % ( gist.raw_url, response.status_code ) )
- return
- else:
- gist_content = take_slice( response.content, gist.start_line, gist.end_line )
- if self.request.get( 'test' ):
- self.response.headers['Content-Type'] = 'text/plain';
- self.response.out.write( gist_content )
- return
- else:
- gist_html = str( render_gist_html( base, gist, gist_content ) ).strip()
- callback = self.request.get( 'callback' );
- if callback != '':
- result = render_gist_js_callback( callback, gist, gist_html )
- else:
- result = render_gist_js( base, gist, gist_html )
- result = str( result ).strip()
- data = result
- if _CACHE_:
- memcache.add( memcache_key, data, 60 * 60 * 24 )
-
- self.response.headers['Content-Type'] = 'text/javascript';
- self.response.out.write( data )
+ return gist_it_appengine.dispatch_gist_it( self, location )
wsgi_application = webapp.WSGIApplication( [
( r'/', dispatch_index ),
File renamed without changes.
View
@@ -0,0 +1,93 @@
+#!/usr/bin/python
+import logging
+import os
+import cgi
+import sys
+import urllib
+
+jinja2 = None
+
+_LOCAL_ = os.environ[ 'SERVER_SOFTWARE' ].startswith( 'Development' )
+_DEBUG_ = True
+_CACHE_ = False
+
+from google.appengine.api import urlfetch
+from google.appengine.ext import webapp
+from versioned_memcache import memcache
+
+import gist_it
+from gist_it import take_slice
+
+def render_gist_html( base, gist, document ):
+ if jinja2 is None:
+ return
+ result = jinja2.get_template( 'gist.jinja.html' ).render( cgi = cgi, base = base, gist = gist, document = document )
+ return result
+
+def render_gist_js( base, gist, gist_html ):
+ if jinja2 is None:
+ return
+ result = jinja2.get_template( 'gist.jinja.js' ).render( base = base, gist = gist, gist_html = gist_html )
+ return result
+
+def render_gist_js_callback( callback, gist, gist_html ):
+ return "%s( '%s', '%s' );" % ( callback, gist_html.encode( 'string_escape' ), gist.raw_path )
+
+# dispatch == RequestHandler
+def dispatch_gist_it( dispatch, location ):
+ base = dispatch.url_for()
+ location = urllib.unquote( location )
+ match = gist_it.Gist.match( location )
+ dispatch.response.headers['Content-Type'] = 'text/plain';
+ if not match:
+ dispatch.response.set_status( 404 )
+ dispatch.response.out.write( dispatch.response.http_status_message( 404 ) )
+ dispatch.response.out.write( "\n" )
+ return
+
+ else:
+ gist = gist_it.Gist.parse( location, slice_ = dispatch.request.get( 'slice' ) )
+ if not gist:
+ dispatch.response.set_status( 500 )
+ dispatch.response.out.write( "Unable to parse \"%s\": Not a valid repository path?" % ( location ) )
+ dispatch.response.out.write( "\n" )
+ return
+
+ if _CACHE_ and dispatch.request.get( 'flush' ):
+ dispatch.response.out.write( memcache.delete( memcache_key ) )
+ return
+
+ memcache_key = gist.raw_url
+ data = memcache.get( memcache_key )
+ if data is None or not _CACHE_:
+ # For below, see: http://stackoverflow.com/questions/2826238/does-google-appengine-cache-external-requests
+ response = urlfetch.fetch( gist.raw_url, headers = { 'Cache-Control': 'max-age=300' } )
+ if response.status_code != 200:
+ if response.status_code == 403:
+ dispatch.response.set_status( response.status_code )
+ elif response.status_code == 404:
+ dispatch.response.set_status( response.status_code )
+ else:
+ dispatch.response.set_status( 500 )
+ dispatch.response.out.write( "Unable to fetch \"%s\": (%i)" % ( gist.raw_url, response.status_code ) )
+ return
+ else:
+ gist_content = take_slice( response.content, gist.start_line, gist.end_line )
+ if dispatch.request.get( 'test' ):
+ dispatch.response.headers['Content-Type'] = 'text/plain';
+ dispatch.response.out.write( gist_content )
+ return
+ gist_html = str( render_gist_html( base, gist, gist_content ) ).strip()
+ callback = dispatch.request.get( 'callback' );
+ if callback != '':
+ result = render_gist_js_callback( callback, gist, gist_html )
+ else:
+ result = render_gist_js( base, gist, gist_html )
+ result = str( result ).strip()
+ data = result
+ if _CACHE_:
+ memcache.add( memcache_key, data, 60 * 60 * 24 )
+
+ dispatch.response.headers['Content-Type'] = 'text/javascript';
+ dispatch.response.out.write( data )
+

0 comments on commit 4c38390

Please sign in to comment.