Python google.appengine.api.memcache.add() Examples

The following are code examples for showing how to use google.appengine.api.memcache.add(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: mimic   Author: googlearchive   File: mimic_test.py    Apache License 2.0 6 votes vote down vote up
def setUp(self):
    test_util.InitAppHostingApi()
    # save os.environ state so we car restore it in tearDown
    self._environ = os.environ.copy()
    os.environ.pop('HTTP_X_APPENGINE_CURRENT_NAMESPACE', None)
    # used by app_identity.get_default_version_hostname()
    os.environ['DEFAULT_VERSION_HOSTNAME'] = 'your-app-id.appspot.com'
    # we set it here to prevent contaimination, may be overridden in tests
    os.environ['HTTP_HOST'] = 'your-app-id.appspot.com'
    # TODO: add tests for app.yaml 'secure: always'
    os.environ['wsgi.url_scheme'] = 'http'
    os.environ['PATH_INFO'] = ''
    os.environ['QUERY_STRING'] = ''
    mimic._dev_appserver_state = {}
    # files that will be part of the tree
    self._files = {}
    # these are filled in from mimic's response during CallMimic()
    self._status = None
    self._headers = {}
    self._body = ''
    self._users_mod = FakeUsersMod()
    os.environ['HTTP_COOKIE'] = 'SID=ghi; SSID=def; HSID=abc;' 
Example 2
Project: splunk-google-drive   Author: LukeMurphey   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 3
Project: sndlatr   Author: Schibum   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 4
Project: experts-app-backend   Author: GoogleDeveloperExperts   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 5
Project: mekong-ssc-gae   Author: KMarkert   File: server.py    GNU General Public License v3.0 6 votes vote down vote up
def GetPolygonTimeSeries(polygon_id):
  """Returns details about the polygon with the passed-in ID."""
  details = memcache.get(polygon_id)

  # If we've cached details for this polygon, return them.
  if details is not None:
    return details

  details = {'wikiUrl': WIKI_URL + polygon_id.replace('-', '%20')}

  try:
    details['timeSeries'] = ComputePolygonTimeSeries(polygon_id)
    # Store the results in memcache.
    memcache.add(polygon_id, json.dumps(details), MEMCACHE_EXPIRATION)
  except ee.EEException as e:
    # Handle exceptions from the EE client library.
    details['error'] = str(e)

  # Send the results to the browser.
  return json.dumps(details) 
Example 6
Project: mekong-ssc-gae   Author: KMarkert   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 7
Project: mekong-ssc-gae   Author: KMarkert   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 8
Project: RSSNewsGAE   Author: liantian-cn   File: gae.py    Apache License 2.0 6 votes vote down vote up
def open_resource(self, name):
        """Opens a resource from the zoneinfo subdir for reading."""
        # Import nested here so we can run setup.py without GAE.
        from google.appengine.api import memcache
        from pytz import OLSON_VERSION

        name_parts = name.lstrip('/').split('/')
        if os.path.pardir in name_parts:
            raise ValueError('Bad path segment: %r' % os.path.pardir)

        cache_key = 'pytz.zoneinfo.%s.%s' % (OLSON_VERSION, name)
        zonedata = memcache.get(cache_key)
        if zonedata is None:
            zonedata = get_zoneinfo().read('zoneinfo/' + '/'.join(name_parts))
            memcache.add(cache_key, zonedata)
            log.info('Added timezone to memcache: %s' % cache_key)
        else:
            log.info('Loaded timezone from memcache: %s' % cache_key)

        return StringIO(zonedata) 
Example 9
Project: syndicate-core   Author: syndicate-storage   File: shard.py    Apache License 2.0 6 votes vote down vote up
def get_modtime( config, name ):
    """Retrieve the value for a given sharded maximum.

    Args:
        name: The name of the counter.

    Returns:
        Integer; the maximum of all sharded maximums for the given
            maximum name.
    """
    mm = memcache.get(name)
    if mm is None:
        all_keys = ShardConfig.all_keys_config( config, name )

        for value in ndb.get_multi(all_keys):
           if value is not None:
              if mm != None:
                 mm = value
              else:
                 mm = modtime_max( mm, value )
                 
        memcache.add(name, mm, 60)
    return (mm.mtime_sec, mm.mtime_nsec) 
Example 10
Project: syndicate-core   Author: syndicate-storage   File: shard.py    Apache License 2.0 6 votes vote down vote up
def get_modtime_from_shards( name, results ):
   mm = None
   for value in results:
      if mm == None:
         mm = value
         continue

      if value == None:
         continue

      mm = modtime_max( mm, value )

   if mm != None:
      memcache.add( name, mm, 60 )
      return mm
   else:
      return None 
Example 11
Project: ecodash   Author: Servir-Mekong   File: server.py    GNU General Public License v3.0 6 votes vote down vote up
def GetPolygonTimeSeries(polygon_id,mypath,ref_start,ref_end,series_start,series_end):
  """Returns details about the polygon with the passed-in ID."""
  #details = memcache.get(polygon_id)

  # If we've cached details for this polygon, return them.
  #if details is not None:
  #  return details

  details = {}

  try:
    details['timeSeries'] = ComputePolygonTimeSeries(polygon_id,mypath,ref_start,ref_end,series_start,series_end)
    # Store the results in memcache.
    #memcache.add(polygon_id, json.dumps(details), MEMCACHE_EXPIRATION)
  except ee.EEException as e:
    # Handle exceptions from the EE client library.
    details['error'] = str(e)

  # Send the results to the browser.
  return json.dumps(details) 
Example 12
Project: ecodash   Author: Servir-Mekong   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 13
Project: ecodash   Author: Servir-Mekong   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 14
Project: geofire-python   Author: ininex   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 15
Project: Niallbot   Author: kafejo   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 16
Project: aqua-monitor   Author: Deltares   File: appengine.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 17
Project: chromabrush   Author: liuyanghejerry   File: image.py    Apache License 2.0 6 votes vote down vote up
def post(self, key=None):
    key = hashlib.sha1(self.request.body).hexdigest()
    file_data = self.request.body
    file_type = self.request.headers['X-File-Type']
    
    if file_type.lower() not in ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']:
      self.response.out.write('invalid type')
      return
      
    memcache.add("file|%s" % key, file_data, 60)
    memcache.add("type|%s" % key, file_type, 60)
    
    url = "%s/image/%s" % (self.request.host_url, key)
    logging.info(self.request.headers)
    logging.info(url)
        
    self.response.out.write(url) 
Example 18
Project: oscars2016   Author: 0x0ece   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 19
Project: that-startpage-rocks   Author: the-duck   File: appengine.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 20
Project: gcp-slack-init   Author: symphco   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 21
Project: deb-python-oauth2client   Author: openstack   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 22
Project: luci-py   Author: luci   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 23
Project: luci-py   Author: luci   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 24
Project: luci-py   Author: luci   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 25
Project: luci-py   Author: luci   File: handlers_endpoints.py    Apache License 2.0 6 votes vote down vote up
def count(self, request):
    """Counts number of tasks in a given state."""
    logging.debug('%s', request)
    if not request.start:
      raise endpoints.BadRequestException('start (as epoch) is required')
    now = utils.utcnow()
    mem_key = self._memcache_key(request, now)
    count = memcache.get(mem_key, namespace='tasks_count')
    if count is not None:
      return swarming_rpcs.TasksCount(count=count, now=now)

    try:
      count = self._query_from_request(request, 'created_ts').count()
      memcache.add(mem_key, count, 24*60*60, namespace='tasks_count')
    except ValueError as e:
      raise endpoints.BadRequestException(
          'Inappropriate filter for tasks/count: %s' % e)
    return swarming_rpcs.TasksCount(count=count, now=now) 
Example 26
Project: luci-py   Author: luci   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 27
Project: node-gn   Author: Shouqun   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 28
Project: alfredToday   Author: jeeftor   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 29
Project: html5-to-dfp   Author: googleads   File: frontend_utils.py    Apache License 2.0 6 votes vote down vote up
def client_secret():
  """Returns the client secret for the site, creates one if it doesn't exist."""
  if env.DEBUG:
    # Try the shortcut used for the development server.
    secret = getattr(env, 'CLIENT_SECRET', None)
    if secret:
      return secret
  secret = memcache.get('client_secret', namespace='frontend_utils#ns')
  if not secret:
    model = SiteClientSecret.get_by_id('site')
    if not model:
      # Administrator will need to enter this from the console, don't cache.
      logger.error('Change client secret from the appengine console.')
      secret = ''
    else:
      secret = model.secret
      memcache.add('client_secret', secret, namespace='frontend_utils#ns')
  return str(secret) 
Example 30
Project: kasir   Author: iColdPlayer   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 31
Project: SurfaceWaterTool   Author: Servir-Mekong   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 32
Project: SurfaceWaterTool   Author: Servir-Mekong   File: appengine.py    GNU General Public License v3.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 33
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: recording.py    Apache License 2.0 6 votes vote down vote up
def start_recording(env=None):
  """Start recording RPC traces.

  This creates a Recorder instance and sets it for the current request
  in the global RequestLocalRecorderProxy 'recorder_proxy'.

  Args:
    env: Optional WSGI environment; defaults to os.environ.
  """
  recorder_proxy.clear_for_current_request()
  if env is None:
    env = os.environ
  if not config.should_record(env):
    return

  if memcache.add(lock_key(), 0,
                  time=config.LOCK_TIMEOUT, namespace=config.KEY_NAMESPACE):
    recorder_proxy.set_for_current_request(Recorder(env))
    if config.DEBUG:
      logging.debug('Set recorder') 
Example 34
Project: cros-info   Author: jay0lee   File: appengine.py    Apache License 2.0 6 votes vote down vote up
def xsrf_secret_key():
    """Return the secret key for use for XSRF protection.

    If the Site entity does not have a secret key, this method will also create
    one and persist it.

    Returns:
        The secret key.
    """
    secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
    if not secret:
        # Load the one and only instance of SiteXsrfSecretKey.
        model = SiteXsrfSecretKey.get_or_insert(key_name='site')
        if not model.secret:
            model.secret = _generate_new_xsrf_secret_key()
            model.put()
        secret = model.secret
        memcache.add(XSRF_MEMCACHE_ID, secret,
                     namespace=OAUTH2CLIENT_NAMESPACE)

    return str(secret) 
Example 35
Project: python-for-android   Author: kuri65536   File: cron.py    Apache License 2.0 6 votes vote down vote up
def __init__(self):
        # Check if any tasks need to be run
        query = _AppEngineUtilities_Cron.all()
        query.filter('next_run <= ', datetime.datetime.now())
        results = query.fetch(1000)
        if len(results) > 0:
            one_second = datetime.timedelta(seconds = 1)
            before  = datetime.datetime.now()
            for r in results:
                if re.search(':' + APPLICATION_PORT, r.url):
                    r.url = re.sub(':' + APPLICATION_PORT, ':' + CRON_PORT, r.url)
                #result = urlfetch.fetch(r.url)
                diff = datetime.datetime.now() - before
                if int(diff.seconds) < 1:
                    if memcache.add(str(r.key), "running"):
                        result = urlfetch.fetch(r.url)
                        r.next_run = self._get_next_run(pickle.loads(r.cron_compiled))
                        r.put()
                        memcache.delete(str(r.key))
                else:
                    break 
Example 36
Project: yumstat   Author: RKD314   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 37
Project: yumstat   Author: RKD314   File: appengine.py    MIT License 6 votes vote down vote up
def xsrf_secret_key():
  """Return the secret key for use for XSRF protection.

  If the Site entity does not have a secret key, this method will also create
  one and persist it.

  Returns:
    The secret key.
  """
  secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
  if not secret:
    # Load the one and only instance of SiteXsrfSecretKey.
    model = SiteXsrfSecretKey.get_or_insert(key_name='site')
    if not model.secret:
      model.secret = _generate_new_xsrf_secret_key()
      model.put()
    secret = model.secret
    memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)

  return str(secret) 
Example 38
Project: mimic   Author: googlearchive   File: mimic_test.py    Apache License 2.0 5 votes vote down vote up
def testNoDefaultContentType(self):
    self._AddFile('app.yaml', _GENERIC_APP_YAML)
    # Script which does not print any HTTP repsonse headers.
    self._AddFile('main.py', r'print "hello"')
    self._CallMimic('/main.py')
    # Mimic should not add a default Content-Type header.
    self._CheckResponse(httplib.OK, None)
    self.assertEquals('hello\n', self._body) 
Example 39
Project: bbq   Author: ocadotechnology   File: backup_process.py    Apache License 2.0 5 votes vote down vote up
def __ensure_dataset_for_backups_exists(self):
        location = self.big_query_table_metadata.get_location()
        target_dataset_name = DatasetIdCreator.create(
            datetime.datetime.utcnow(), location, self.project_id)
        dataset_not_exists_in_cache = memcache.get(target_dataset_name) is None

        if dataset_not_exists_in_cache:
            destination_project_id = configuration.backup_project_id
            self.big_query.create_dataset(destination_project_id,
                                          target_dataset_name, location)
            memcache.add(target_dataset_name, 'exist') 
Example 40
Project: livremarketplace   Author: salomax   File: models.py    Apache License 2.0 5 votes vote down vote up
def get(email):
    """ Get marketplace model by user email.
    """

    # Set memcache key
    memcache_key = 'email.marktplace.%s' % email

    # Verify if user marktplace is cached
    marketplace_cached = memcache.get(memcache_key)

    # and return cached marketplace
    if marketplace_cached is not None:
        logging.debug("Marketplace cached for %s user. Return it.", email)
        return marketplace_cached

    # else, query or add new one...

    # Create user key as parent
    user_key = user.user_key(email)

    # Select user marketplace
    marketplaceModel = MarketplaceModel.query(ancestor=user_key).get()

    # If not present, create new one
    if marketplaceModel is None:
        marketplaceModel = put(email=email, name='Nova Loja', user_key=user_key)
        logging.debug("Marketplace create sucessfully for %s user", email)

    # Add to memcache
    memcache.add(key=memcache_key, value=marketplaceModel, time=3600 * 24)

    logging.debug("Marketplace cached for %s user", email)

    # Return marketplace
    return marketplaceModel 
Example 41
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def cachename(self, filepath):
        #: if lang is provided then add it to cache filename.
        if self.lang:
            return '%s.%s.cache' % (filepath, self.lang)
        #: return cache file name.
        else:
            return filepath + '.cache' 
Example 42
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def to_filename(self, template_name):
        """Convert template short name into filename.
           ex.
             >>> engine = tenjin.Engine(prefix='user_', postfix='.pyhtml')
             >>> engine.to_filename(':list')
             'user_list.pyhtml'
             >>> engine.to_filename('list')
             'list'
        """
        #: if template_name starts with ':', add prefix and postfix to it.
        if template_name[0] == ':' :
            return self.prefix + template_name[1:] + self.postfix
        #: if template_name doesn't start with ':', just return it.
        return template_name 
Example 43
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def include(self, template_name, append_to_buf=True, **kwargs):
        """Evaluate template using current local variables as context.

           template_name:str
             Filename (ex. 'user_list.pyhtml') or short name (ex. ':list') of template.
           append_to_buf:boolean (=True)
             If True then append output into _buf and return None,
             else return stirng output.

           ex.
             <?py include('file.pyhtml') ?>
             #{include('file.pyhtml', False)}
             <?py val = include('file.pyhtml', False) ?>
        """
        #: get local and global vars of caller.
        frame = sys._getframe(1)
        locals  = frame.f_locals
        globals = frame.f_globals
        #: get _context from caller's local vars.
        assert '_context' in locals
        context = locals['_context']
        #: if kwargs specified then add them into context.
        if kwargs:
            context.update(kwargs)
        #: get template object with context data and global vars.
        ## (context and globals are passed to get_template() only for preprocessing.)
        template = self.get_template(template_name, context, globals)
        #: if append_to_buf is true then add output to _buf.
        #: if append_to_buf is false then don't add output to _buf.
        if append_to_buf:  _buf = locals['_buf']
        else:              _buf = None
        #: render template and return output.
        s = template.render(context, globals, _buf=_buf)
        #: kwargs are removed from context data.
        if kwargs:
            for k in kwargs:
                del context[k]
        return s 
Example 44
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 5 votes vote down vote up
def hook_context(self, context):
        #: add engine itself into context data.
        context['_engine'] = self
        #context['render'] = self.render
        #: add include() method into context data.
        context['include'] = self.include


##
## safe template and engine
## 
Example 45
Project: mapotron   Author: earthenv   File: services.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cacheResult(result, key, type='json'):
    logging.info('Caching %s' % key)
    if type == 'json':
        cache.add(key, json.dumps(result), value_type='string')
        memcache.add(key, json.dumps(result))
    else:
        cache.add(key, result, value_type='blob')
        memcache.add(key, result) 
Example 46
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_keyword():
    data = memcache.get(key="keyword")
    if data is None:
        q = KeyWord.query()
        data = ndb.get_multi(q.fetch(keys_only=True))
        memcache.add(key="keyword", value=data, time=86400 * 30)
    return data 
Example 47
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_enable_keyword():
    data = memcache.get(key="enable_keyword")
    if data is None:
        q = KeyWord.query(KeyWord.enable == True)
        data = ndb.get_multi(q.fetch(keys_only=True))
        memcache.add(key="enable_keyword", value=data, time=86400 * 30)
    return data 
Example 48
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_pure_keyword():
    data = memcache.get(key="pure_keyword")
    if data is None:
        data = []
        q = KeyWord.query(KeyWord.enable == True)
        key_words = ndb.get_multi(q.fetch(keys_only=True))
        for key_word in key_words:
            data.append(key_word.word)
        memcache.add(key="pure_keyword", value=data, time=86400 * 30)
    return data 
Example 49
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_feed():
    data = memcache.get(key="feed")
    if data is None:
        q = Feed.query()
        data = ndb.get_multi(q.fetch(keys_only=True))
        memcache.add(key="feed", value=data, time=86400 * 30)
    return data 
Example 50
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_daily_feed():
    data = memcache.get(key="daily_feed")
    if data is None:
        q = Feed.query(Feed.delay == "daily", Feed.enable == True)
        data = ndb.get_multi(q.fetch(keys_only=True))
        memcache.add(key="daily_feed", value=data, time=86400 * 30)
    return data 
Example 51
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_quarterly_feed():
    data = memcache.get(key="quarterly_feed")
    if data is None:
        q = Feed.query(Feed.delay == "quarterly", Feed.enable == True)
        data = ndb.get_multi(q.fetch(keys_only=True))
        memcache.add(key="quarterly_feed", value=data, time=86400 * 30)
    return data 
Example 52
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_latest_news():
    data = memcache.get(key="latest_news")
    if data is None:
        q = NewsEntry.query().order(-NewsEntry.published)
        data = ndb.get_multi(q.fetch(limit=app.config["PER_PAGE"],keys_only=True))
        memcache.add(key="latest_news", value=data, time=app.config["INDEX_CACHE_TIME"])
    return data 
Example 53
Project: RSSNewsGAE   Author: liantian-cn   File: models.py    Apache License 2.0 5 votes vote down vote up
def get_important_news():
    data = memcache.get(key="important_news")
    if data is None:
        q = NewsEntry.query(NewsEntry.important==True).order(-NewsEntry.published)
        data = ndb.get_multi(q.fetch(limit=app.config["PER_PAGE"],keys_only=True))
        memcache.add(key="important_news", value=data, time=app.config["INDEX_CACHE_TIME"])
    return data 
Example 54
Project: ecodash   Author: Servir-Mekong   File: server.py    GNU General Public License v3.0 5 votes vote down vote up
def get(self):
		
		polygon =  unicode(self.request.get('polygon')) 
		refLow = self.request.get('refLow')
		refHigh = self.request.get('refHigh')
		studyLow = self.request.get('studyLow')
		studyHigh = self.request.get('studyHigh')
    
		ref_start = refLow + '-01-01'
		ref_end = refHigh + '-12-31'
		series_start = studyLow + '-01-01'
		series_end = studyHigh + '-12-31'
		
			
		coords = []
				
		for items in eval(polygon):
			coords.append([items[1],items[0]])
		
		mypoly =  ee.FeatureCollection(ee.Geometry.Polygon(coords))
		
		#details = memcache.get(str(counter))
		
		details = ComputePolygonDrawTimeSeries(mypoly,ref_start,ref_end,series_start,series_end)
		
		#memcache.add(str(counter), json.dumps(details), MEMCACHE_EXPIRATION)	
		content = json.dumps(details) # ComputePolygonDrawTimeSeries(mypoly,ref_start,ref_end,series_start,series_end)
		

		self.response.headers['Content-Type'] = 'application/json'   
	
		self.response.out.write(content) 
Example 55
Project: vierchanbot   Author: jackieleng   File: main.py    MIT License 5 votes vote down vote up
def test():
    """Test"""
    logger.info("Got these args %s", request.args)
    # add means only add when key doesn't exist
    #memcache.add(key='key', value=str(request.args), time=3600)

    pt = PickledThing.get_by_id(a_images_key)
    if not pt:
        logger.error("No entry in Datastore found for key: %s", a_images_key)
        return "No images"
    imgs = pt.thing

    return str(imgs)
    # return str(memcache.get('a:images')) 
Example 56
Project: mymovie   Author: manish-pocketgems   File: main.py    MIT License 5 votes vote down vote up
def get_all():
        movies = memcache.get(MEMCACHE_ALL_MOVIES_KEY)
        if movies:
            logging.info('Read successfully from memcache')
        else:
            movies = Movie.query().order(-Movie.average_rating).fetch(80)
            if not memcache.add(MEMCACHE_ALL_MOVIES_KEY, movies,
                                MEMCACHE_TIMEOUT):
                logging.error('Memcache add failed')
        return movies 
Example 57
Project: mymovie   Author: manish-pocketgems   File: main.py    MIT License 5 votes vote down vote up
def get_from_cache(cls, movie_id):
        """Get an entity from cache layer"""
        mc_key = MEMCACHE_KEY_PREFIX + str(movie_id)
        movie = memcache.get(mc_key)
        if movie is None:
            movie = Movie.get_by_id(int(movie_id))
            if movie:
                memcache.add(mc_key, movie, MEMCACHE_TIMEOUT)
        return movie 
Example 58
Project: pledgeservice   Author: Lessig2016   File: main.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def getTeamTotal(self, team):
    self.response.headers['Content-Type'] = 'application/javascript'
    key = "TEAM-TOTAL-%s" % team
    res = memcache.get(key)
    if not res:
      total_pledges, total_amount = 0, 0
      for pledge in model.Pledge.all().filter("team =", team):
        total_pledges += 1
        total_amount += pledge.amountCents
      # doh, we should probably return a json object here instead of just an
      # some ints, but we'd like to be backwards compatible with the previous
      # (non-team) api. so for now, let's make use javascript varargs
      res = '(%d, %d)' % (total_amount, total_pledges)
      memcache.add(key, res, 60)
    self.response.write("%s%s" % (self.request.get('callback'), res)) 
Example 59
Project: pledgeservice   Author: Lessig2016   File: cache.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def SetShardedCounterTotal(name, value):
  memcache.add(_COUNTER_TOTAL.format(name), value, 60) 
Example 60
Project: pledgeservice   Author: Lessig2016   File: cache.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def SetTeamPledgeCount(team, value):
  memcache.add(_TEAM_PLEDGES.format(team), value, 60) 
Example 61
Project: pledgeservice   Author: Lessig2016   File: cache.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def SetTeamTotal(team, value):
  memcache.add(_TEAM_TOTAL.format(team), value, 60) 
Example 62
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    Apache License 2.0 5 votes vote down vote up
def get_greetings(self, guestbook_name):
        """
        get_greetings()
        Checks the cache to see if there are cached greetings.
        If not, call render_greetings and set the cache

        Args:
          guestbook_name: Guestbook entity group key (string).

        Returns:
          A string of HTML containing greetings.
        """
        greetings = memcache.get('{}:greetings'.format(guestbook_name))
        if greetings is None:
            greetings = self.render_greetings(guestbook_name)
            try:
                added = memcache.add(
                    '{}:greetings'.format(guestbook_name), greetings, 10)
                if not added:
                    logging.error('Memcache set failed.')
            except ValueError:
                logging.error('Memcache set failed - data larger than 1MB')
        return greetings
    # [END check_memcache]

    # [START query_datastore] 
Example 63
Project: python-docs-samples   Author: GoogleCloudPlatform   File: migration2.py    Apache License 2.0 5 votes vote down vote up
def get_or_add_person(name, userid):
    person = memcache.get(name)
    if person is None:
        person = Person(name=name, userid=userid)
        memcache.add(name, person)
    else:
        logging.info('Found in cache: ' + name + ', userid: ' + person.userid)
    return person
# [END best-practice-2] 
Example 64
Project: python-docs-samples   Author: GoogleCloudPlatform   File: migration1.py    Apache License 2.0 5 votes vote down vote up
def get_or_add_person(name):
    person = memcache.get(name)
    if person is None:
        person = Person(name=name)
        memcache.add(name, person)
    else:
        logging.info('Found in cache: ' + name)
    return person
# [END best-practice-1] 
Example 65
Project: python-docs-samples   Author: GoogleCloudPlatform   File: failure.py    Apache License 2.0 5 votes vote down vote up
def get(self):
        key = "some-key"
        # [START memcache-read]
        v = memcache.get(key)
        if v is None:
            v = read_from_persistent_store()
            memcache.add(key, v)
        # [END memcache-read]

        self.response.content_type = 'text/html'
        self.response.write(str(v)) 
Example 66
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets.py    Apache License 2.0 5 votes vote down vote up
def get_data():
    data = memcache.get('key')
    if data is not None:
        return data
    else:
        data = query_for_data()
        memcache.add('key', data, 60)
    return data
# [END get_data] 
Example 67
Project: python-docs-samples   Author: GoogleCloudPlatform   File: snippets_test.py    Apache License 2.0 5 votes vote down vote up
def test_get_data_present(query_fn, testbed):
    memcache.add('key', 'data', 9000)
    data = snippets.get_data()
    query_fn.assert_not_called()
    assert data == 'data'
    memcache.delete('key') 
Example 68
Project: luci-py   Author: luci   File: task_to_run.py    Apache License 2.0 5 votes vote down vote up
def set_lookup_cache(to_run_key, is_available_to_schedule):
  """Updates the quick lookup cache to mark an item as available or not.

  This cache is a blacklist of items that are about to be reaped or are already
  reaped, so it is not worth trying to reap it with a DB transaction. This saves
  on DB contention when a high number (>1000) of concurrent bots with similar
  dimension are reaping tasks simultaneously. In this case, there is a high
  likelihood that multiple concurrent HTTP handlers are trying to reap the exact
  same task simultaneously. This blacklist helps reduce the contention by
  telling the other bots to back off.

  Another reason for this negative cache is that the DB index takes some seconds
  to be updated, which means it can return stale items (e.g. already reaped).

  It can be viewed as a lock, except that the 'lock' is never released, it is
  impliclity released after 15 seconds.

  Returns:
    True if the key was updated, False if was trying to reap and the entry was
    already set.
  """
  # Set the expiration time for items in the negative cache as 15 seconds. This
  # copes with significant index inconsistency but do not clog the memcache
  # server with unneeded keys.
  cache_lifetime = 15

  key = _memcache_to_run_key(to_run_key)
  if is_available_to_schedule:
    # The item is now available, so remove it from memcache.
    memcache.delete(key, namespace='task_to_run')
    return True

  # add() returns True if the entry was added, False otherwise. That's perfect.
  return memcache.add(key, True, time=cache_lifetime, namespace='task_to_run') 
Example 69
Project: html5-to-dfp   Author: googleads   File: dfp_utils.py    Apache License 2.0 5 votes vote down vote up
def put(self, id, object):
    return memcache.add('wsdl_%s' % id, object) 
Example 70
Project: html5-to-dfp   Author: googleads   File: frontend_utils.py    Apache License 2.0 5 votes vote down vote up
def session_key():
  """Returns the session key for the site, creates one if it doesn't exist."""
  secret = memcache.get('session_key', namespace='frontend_utils#ns')
  if not secret:
    model = SiteSessionKey.get_or_insert('site')
    if not model.secret:
      model.secret = os.urandom(16).encode('hex')
      model.put()
    secret = model.secret
    memcache.add('session_key', secret, namespace='frontend_utils#ns')
  return str(secret) 
Example 71
Project: oauth-handler   Author: duplicati   File: main.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get(self):
        try:
            fetchtoken = self.request.get('token')

            # self.headers.add('Access-Control-Allow-Origin', '*')

            if fetchtoken is None or fetchtoken == '':
                self.response.write(wrap_json(self, {'error': 'Missing token'}))
                return

            entry = dbmodel.FetchToken.get_by_key_name(fetchtoken)
            if entry is None:
                self.response.write(wrap_json(self, {'error': 'No such entry'}))
                return

            if entry.expires < datetime.datetime.utcnow():
                self.response.write(wrap_json(self, {'error': 'No such entry'}))
                return

            if entry.authid is None or entry.authid == '':
                self.response.write(wrap_json(self, {'wait': 'Not ready'}))
                return

            entry.fetched = True
            entry.put()

            self.response.write(wrap_json(self, {'authid': entry.authid}))
        except:
            logging.exception('handler error')
            self.response.set_status(500, 'Server error')
            self.response.write(wrap_json(self, {'error': 'Server error'})) 
Example 72
Project: oauth-handler   Author: duplicati   File: main.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def get(self):
        if settings.WORKER_URLS is None:
            return

        data = '%030x' % random.randrange(16 ** 32)

        validhosts = []

        for n in settings.WORKER_URLS:
            try:
                url = n[:-len("refresh")] + "isalive?data=" + data
                logging.info('Checking if server is alive: %s', url)

                req = urllib2.Request(url)
                f = urllib2.urlopen(req)
                content = f.read()
                f.close()

                resp = json.loads(content)
                if resp["data"] != data:
                    logging.info('Bad response, was %s, should have been %s', resp['data'], data)
                else:
                    validhosts.append(n)
            except:
                logging.exception('handler error')

        logging.info('Valid hosts are: %s', validhosts)

        memcache.add(key='worker-urls', value=validhosts, time=60 * 60 * 1) 
Example 73
Project: teams   Author: Lessig2016   File: main.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def makeUserAdmin(user_id, team):
  AdminToTeam(user=user_id, team=team).put()
  memcache.add(AdminToTeam.memcacheKey(user_id, team), True, 30) 
Example 74
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: ereporter.py    Apache License 2.0 5 votes vote down vote up
def emit(self, record):
    """Log an error to the datastore, if applicable.

    Args:
      The logging.LogRecord object.
        See http://docs.python.org/library/logging.html#logging.LogRecord
    """
    try:
      if not record.exc_info:

        return

      signature = self.__GetSignature(record.exc_info)

      old_namespace = namespace_manager.get_namespace()
      try:
        namespace_manager.set_namespace('')


        if not memcache.add(signature, None, self.log_interval):
          return


        db.run_in_transaction_custom_retries(1, self.__EmitTx, signature,
                                             record.exc_info)
      finally:
        namespace_manager.set_namespace(old_namespace)
    except Exception:
      self.handleError(record) 
Example 75
Project: mimic   Author: googlearchive   File: mimic_test.py    Apache License 2.0 4 votes vote down vote up
def testMemcacheNamespacing(self):
    """Tests that different projects see different memcaches."""

    put_script = """
from google.appengine.api import memcache
import os
value = os.environ['PATH_INFO'].split('?')[1]
memcache.add(key="my_key", value=value, time=3600)

print 'Content-type: text/plain; charset=utf-8'
print 'Status: 200 OK'
print ''
print 'value: ' + value
"""

    get_script = """
from google.appengine.api import memcache

value = memcache.get(key="my_key")

print 'Content-type: text/plain; charset=utf-8'
print 'Status: 200 OK'
print ''
print 'value: ' + value
"""

    self._AddFile('put.py', put_script)
    self._AddFile('get.py', get_script)
    self._AddFile('app.yaml', _GENERIC_APP_YAML)

    # put John and Jane using the same key
    self._CallMimic('/put.py?JohnDoe',
                    http_host='proj1.your-app-id.appspot.com')
    self.assertEquals('value: JohnDoe\n', self._body)
    self._CallMimic('/put.py?JaneDoe',
                    http_host='proj2.your-app-id.appspot.com')
    self.assertEquals('value: JaneDoe\n', self._body)

    self._CallMimic('/get.py', http_host='proj1.your-app-id.appspot.com')
    self.assertEquals('value: JohnDoe\n', self._body)

    self._CallMimic('/get.py', http_host='proj2.your-app-id.appspot.com')
    self.assertEquals('value: JaneDoe\n', self._body) 
Example 76
Project: gae-bbs   Author: ego008   File: tenjin.py    Apache License 2.0 4 votes vote down vote up
def parse_stmts(self, buf, input):
        if not input: return
        rexp = self.stmt_pattern()
        is_bol = True
        index = 0
        for m in rexp.finditer(input):
            mspace, code, rspace = m.groups()
            #mspace, close, rspace = m.groups()
            #code = input[m.start()+4+len(mspace):m.end()-len(close)-(rspace and len(rspace) or 0)]
            text = input[index:m.start()]
            index = m.end()
            ## detect spaces at beginning of line
            lspace = None
            if text == '':
                if is_bol:
                    lspace = ''
            elif text[-1] == '\n':
                lspace = ''
            else:
                rindex = text.rfind('\n')
                if rindex < 0:
                    if is_bol and text.isspace():
                        lspace, text = text, ''
                else:
                    s = text[rindex+1:]
                    if s.isspace():
                        lspace, text = s, text[:rindex+1]
            #is_bol = rspace is not None
            ## add text, spaces, and statement
            self.parse_exprs(buf, text, is_bol)
            is_bol = rspace is not None
            #if mspace == "\n":
            if mspace and mspace.endswith("\n"):
                code = "\n" + (code or "")
            #if rspace == "\n":
            if rspace and rspace.endswith("\n"):
                code = (code or "") + "\n"
            if code:
                code = self.statement_hook(code)
                m = self._match_to_args_declaration(code)
                if m:
                    self._add_args_declaration(buf, m)
                else:
                    self.add_stmt(buf, code)
        rest = input[index:]
        if rest:
            self.parse_exprs(buf, rest)
        self._arrange_indent(buf) 
Example 77
Project: luci-py   Author: luci   File: api.py    Apache License 2.0 4 votes vote down vote up
def get_config_multi(scope, path, hashes_only):
  """Returns configs at |path| in all config sets.

  scope can be 'projects' or 'refs'.

  Returns empty config list if requester does not have project access.
  """
  assert scope in ('projects', 'refs'), scope
  cache_key = (
    'v2/%s%s:%s' % (scope, ',hashes_only' if hashes_only else '', path))
  configs = memcache.get(cache_key)
  if configs is None:
    config_sets = list(get_config_sets_from_scope(scope))
    cfg_map = storage.get_latest_configs_async(
        config_sets, path, hashes_only=hashes_only).get_result()
    configs = []
    for cs in config_sets:
      rev, rev_url, content_hash, content = cfg_map.get(cs, (None, None, None))
      if not content_hash:
        continue
      configs.append({
        'config_set': cs,
        'revision': rev,
        'content_hash': content_hash,
        'content': content,
        'url': rev_url,
      })
      if not hashes_only and content is None:
        logging.error(
            'Blob %s referenced from %s:%s:%s was not found',
            content_hash, cs, rev, path)
    try:
      memcache.add(cache_key, configs, time=60)
    except ValueError:
      logging.exception('%s:%s configs are too big for memcache', scope, path)

  res = GetConfigMultiResponseMessage()
  can_read = can_read_config_sets([c['config_set'] for c in configs])
  for config in configs:
    if not can_read[config['config_set']]:
      continue
    if not hashes_only and config.get('content') is None:
      continue
    res.configs.append(res.ConfigEntry(
        config_set=config['config_set'],
        revision=config['revision'],
        content_hash=config['content_hash'],
        content=config.get('content'),
        url=config.get('url'),
    ))
  return res