def get_value(cls, name):
        """Retrieves the value of a configuration parameter.
    
    Args:
      name: the name of the parameter whose value we are looking for.

    Returns:
      The value of the parameter or None if the parameter is unknown.
    """
        if cls.local_config_cache is None:
            # The local cache is empty, retrieve its content from memcache.
            cache = memcache.get(cls.MEMCACHE_ENTRY)
            if cache is None:
                # Nothing in memcache either, recreate the cache from the datastore.
                cache = dict(cls.DEFAULT_VALUES)
                for parameter in Config.all():
                    cache[parameter.key().name()] = parameter.value
                # Save the full cache in memcache with 1h expiration time.
                memcache.add(cls.MEMCACHE_ENTRY, cache, 60 * 60)
            cls.local_config_cache = cache
        # Retrieve the value from the cache.
        return cls.local_config_cache.get(name)
Example #2
0
  def get_value(cls, name):
    """Retrieves the value of a configuration parameter.
    
    Args:
      name: the name of the parameter whose value we are looking for.

    Returns:
      The value of the parameter or None if the parameter is unknown.
    """
    if cls.local_config_cache is None:
      # The local cache is empty, retrieve its content from memcache.
      cache = memcache.get(cls.MEMCACHE_ENTRY)
      if cache is None:
        # Nothing in memcache either, recreate the cache from the datastore.
        cache = dict(cls.DEFAULT_VALUES)
        for parameter in Config.all():
          cache[parameter.key().name()] = parameter.value
        # Save the full cache in memcache with 1h expiration time.
        memcache.add(cls.MEMCACHE_ENTRY, cache, 60*60)
      cls.local_config_cache = cache
    # Retrieve the value from the cache.
    return cls.local_config_cache.get(name)
Example #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' )

            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 )
Example #4
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)