Ejemplo n.º 1
0
def dispatch_gist_it( dispatch, location ):
        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:
            slice_option = dispatch.request.get( 'slice' )
            footer_option = dispatch.request.get( 'footer' )

            gist = gist_it.Gist.parse( location, slice_option = slice_option, footer_option = footer_option )
            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_:
                base = dispatch.url_for()
                # 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 )
                    gist_html = str( render_gist_html( base, gist, gist_content, footer = gist.footer ) ).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
                    test = dispatch.request.get( 'test' )
                    if test:
                        if test == 'json':
                            dispatch.response.headers['Content-Type'] = 'text/plain';
                            dispatch.response.out.write(simplejson.dumps({
                                'gist': gist.value(),
                                'content': gist_content,
                                'html': gist_html,
                            }))
                        elif False and test == 'example':
                            pass
                        else:
                            dispatch.response.headers['Content-Type'] = 'text/plain' 
                            dispatch.response.out.write( gist_html )
                        return
                    if _CACHE_:
                        memcache.add( memcache_key, data, 60 * 60 * 24 )

            dispatch.response.headers['Content-Type'] = 'text/javascript'
            dispatch.response.out.write( data )
Ejemplo n.º 2
0
def dispatch_gist_it( dispatch, location ):
    location = urllib.unquote( location )
    match = gist_it.Gist.match( location )
    dispatch.set_header('Content-Type', 'text/plain'); 
    if not match:
        dispatch.set_status( 404 )
        dispatch.write( 'Not Found' )
        dispatch.write( "\n" )
        return

    else:
        slice_option = dispatch.get_param( 'slice' )
        footer_option = dispatch.get_param( 'footer' )
        style_option = dispatch.get_param( 'style' )
        highlight_option = dispatch.get_param( 'highlight' )
        test = dispatch.get_param( 'test' )

        gist = gist_it.Gist.parse( location, slice_option = slice_option, footer_option = footer_option, style_option = style_option, highlight_option = highlight_option )
        if not gist:
            dispatch.set_status( 500 )
            dispatch.write( "Unable to parse \"%s\": Not a valid repository path?" % ( location ) )
            dispatch.write( "\n" )
            return
            
        if _CACHE_ and dispatch.get_param( 'flush' ):
            dispatch.write( memcache.delete( memcache_key ) )
            return

        memcache_key = gist.raw_url.encode('UTF-8')
        data = memcache.get( memcache_key )
        if data is None or not _CACHE_:
            base = dispatch.url_for()
            # For below, see: http://stackoverflow.com/questions/2826238/does-google-appengine-cache-external-requests
            response = fetch( gist.raw_url, headers = { 'Cache-Control': 'max-age=300' } )
            if response.status_code != 200:
                if response.status_code == 403:
                    dispatch.set_status( response.status_code )
                elif response.status_code == 404:
                    dispatch.set_status( response.status_code )
                else:
                    dispatch.set_status( 500 )
                dispatch.write( "Unable to fetch \"%s\": (%i)" % ( gist.raw_url, response.status_code ) )
                return
            else:
                # I believe GitHub always returns a utf-8 encoding, so this should be safe
                response_content = response.content.decode('utf-8')

                gist_content = take_slice( response_content, gist.start_line, gist.end_line )
                gist_html = str( render_gist_html( base, gist, gist_content ) ).strip()
                callback = dispatch.get_param( 'callback' );
                if callback != None and 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 test:
                    if test == 'json':
                        dispatch.set_header('Content-Type', 'application/json');
                        # "{ 'gist': '%s', 'content': '%s', 'html': '%s' }"%(gist.value(), gist_content, gist_html)
                        dispatch.write("{ 'gist': '%s', 'content': '%s', 'html': '%s' }"%(gist.value(), gist_content, gist_html))
                    elif False and test == 'example':
                        pass
                    else:
                        dispatch.set_header('Content-Type', 'text/plain' )
                        dispatch.write( gist_html )
                    return
                if _CACHE_:
                    memcache.add( memcache_key, data, 60 * 60 * 24 )

        dispatch.set_header('Content-Type', 'text/javascript')
        dispatch.write( data.decode('UTF-8') )
Ejemplo n.º 3
0
def dispatch_gist_it(dispatch, location):
    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:
        slice_option = dispatch.request.get('slice')
        footer_option = dispatch.request.get('footer')
        style_option = dispatch.request.get('style')
        highlight_option = dispatch.request.get('highlight')
        test = dispatch.request.get('test')

        gist = gist_it.Gist.parse(location,
                                  slice_option=slice_option,
                                  footer_option=footer_option,
                                  style_option=style_option,
                                  highlight_option=highlight_option)
        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_:
            base = dispatch.url_for()
            # 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:
                # I believe GitHub always returns a utf-8 encoding, so this should be safe
                response_content = response.content.decode('utf-8')

                gist_content = take_slice(response_content, gist.start_line,
                                          gist.end_line)
                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 test:
                    if test == 'json':
                        dispatch.response.headers[
                            'Content-Type'] = 'application/json'
                        dispatch.response.out.write(
                            simplejson.dumps({
                                'gist': gist.value(),
                                'content': gist_content,
                                'html': gist_html,
                            }))
                    elif False and test == 'example':
                        pass
                    else:
                        dispatch.response.headers[
                            'Content-Type'] = 'text/plain'
                        dispatch.response.out.write(gist_html)
                    return
                if _CACHE_:
                    memcache.add(memcache_key, data, 60 * 60 * 24)

        dispatch.response.headers['Content-Type'] = 'text/javascript'
        dispatch.response.out.write(data)
Ejemplo n.º 4
0
    def runTest( self ):
        slice_ = gist_it.parse_slice( '' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 0 )
        self.assertEqual( slice_[1], 0 )

        slice_ = gist_it.parse_slice( '1' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 1 )
        self.assertEqual( slice_[1], None )

        slice_ = gist_it.parse_slice( '1:' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 1 )
        self.assertEqual( slice_[1], 0 )

        slice_ = gist_it.parse_slice( '1:0' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 1 )
        self.assertEqual( slice_[1], 0 )

        slice_ = gist_it.parse_slice( ':1' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 0 )
        self.assertEqual( slice_[1], 1 )

        slice_ = gist_it.parse_slice( '0:1' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 0 )
        self.assertEqual( slice_[1], 1 )

        slice_ = gist_it.parse_slice( '1:1' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 1 )
        self.assertEqual( slice_[1], 1 )

        slice_ = gist_it.parse_slice( ':' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 0 )
        self.assertEqual( slice_[1], 0 )

        slice_ = gist_it.parse_slice( '-0:-0' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], 0 )
        self.assertEqual( slice_[1], 0 )

        slice_ = gist_it.parse_slice( '-1:' )
        self.assertEqual( len( slice_ ), 2 )
        self.assertEqual( slice_[0], -1 )
        self.assertEqual( slice_[1], 0 )

        content = """
Line 2
Line 3
Line 4
Line 5
Line 6

Line 8
"""
        self.assertEqual( gist_it.take_slice( content, 0, 0 ), content )
        self.assertEqual( gist_it.take_slice( content, 1, 2 ), "Line 2\nLine 3" )
        self.assertEqual( gist_it.take_slice( content, 1, None ), "Line 2" )
        self.assertEqual( gist_it.take_slice( content, 0, 2 ), "\nLine 2\nLine 3" )
        self.assertEqual( gist_it.take_slice( content, 0, -1 ), """
Line 2
Line 3
Line 4
Line 5
Line 6
""" )
        self.assertEqual( gist_it.take_slice( content, -1, 0 ), "Line 8" )
Ejemplo n.º 5
0
    def runTest(self):
        slice_ = gist_it.parse_slice('')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 0)
        self.assertEqual(slice_[1], 0)

        slice_ = gist_it.parse_slice('1')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 1)
        self.assertEqual(slice_[1], None)

        slice_ = gist_it.parse_slice('1:')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 1)
        self.assertEqual(slice_[1], 0)

        slice_ = gist_it.parse_slice('1:0')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 1)
        self.assertEqual(slice_[1], 0)

        slice_ = gist_it.parse_slice(':1')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 0)
        self.assertEqual(slice_[1], 1)

        slice_ = gist_it.parse_slice('0:1')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 0)
        self.assertEqual(slice_[1], 1)

        slice_ = gist_it.parse_slice('1:1')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 1)
        self.assertEqual(slice_[1], 1)

        slice_ = gist_it.parse_slice(':')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 0)
        self.assertEqual(slice_[1], 0)

        slice_ = gist_it.parse_slice('-0:-0')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], 0)
        self.assertEqual(slice_[1], 0)

        slice_ = gist_it.parse_slice('-1:')
        self.assertEqual(len(slice_), 2)
        self.assertEqual(slice_[0], -1)
        self.assertEqual(slice_[1], 0)

        content = """
Line 2
Line 3
Line 4
Line 5
Line 6

Line 8
"""
        self.assertEqual(gist_it.take_slice(content, 0, 0), content)
        self.assertEqual(gist_it.take_slice(content, 1, 2), "Line 2\nLine 3")
        self.assertEqual(gist_it.take_slice(content, 1, None), "Line 2")
        self.assertEqual(gist_it.take_slice(content, 0, 2), "\nLine 2\nLine 3")
        self.assertEqual(gist_it.take_slice(content, 0, -1), """
Line 2
Line 3
Line 4
Line 5
Line 6
""")
        self.assertEqual(gist_it.take_slice(content, -1, 0), "Line 8")