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

The following are 30 code examples for showing how to use google.appengine.api.memcache.get(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module google.appengine.api.memcache , or try the search function .

Example 1
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def CategoryTest(request):
    """Loads the test frameset for a category."""
    category = re.sub('\/test.*', '', request.path)[1:]
    test_set = all_test_sets.GetTestSet(category)

    testurl = ''
    test_key = request.GET.get('test_key')
    if test_key:
        test = test_set.GetTest(test_key)
        testurl = test.url

    params = {
        'category': test_set.category,
        'page_title': '%s - Tests' % test_set.category_name,
        'continue': request.GET.get('continue', ''),
        'autorun': request.GET.get('autorun', ''),
        'testurl': testurl,
        'test_page': test_set.test_page
    }
    return Render(request, 'test_frameset.html', params) 
Example 2
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def CategoryTestDriver(request):
    """Loads the test driver for a category."""
    category = request.GET.get('category')
    test_set = all_test_sets.GetTestSet(category)
    params = {
        'category': test_set.category,
        'category_name': test_set.category_name,
        'page_title': '%s - Test Driver' % test_set.category_name,
        'continue': request.GET.get('continue', ''),
        'autorun': request.GET.get('autorun', ''),
        'test_page': test_set.test_page,
        'testurl': request.GET.get('testurl', ''),
        'csrf_token': request.session.get('csrf_token'),
        'hide_footer': True
    }
    return Render(request, TEST_DRIVER_TPL, params, category) 
Example 3
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def Home(request):
    """Our Home page.
    This also doubles as a general API request handler with a few specific
    bits for the home page template."""

    # The recent tests table.
    recent_tests = memcache.get(key=RECENT_TESTS_MEMCACHE_KEY)
    if not recent_tests:
        ScheduleRecentTestsUpdate()

    show_evolution = False

    params = {
        'page_title': 'Home',
        'message': request.GET.get('message'),
        'recent_tests': recent_tests,
        'show_evolution': show_evolution,
    }
    return GetResults(request, template='home.html', params=params,
                                        do_sparse_filter=True) 
Example 4
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def GvizTableData(request):
    """Returns a string formatted for consumption by a Google Viz table."""
    #def throw_deadline():
    #    logging.info('MANUAL THROW!! DeadlineExceededError DeadlineExceededError')
    #    raise DeadlineExceededError
    #t = Timer(15.0, throw_deadline)

    test_set = None
    category = request.GET.get('category')
    if not category:
        return http.HttpResponseBadRequest('Must pass category=something')

    test_set = all_test_sets.GetTestSet(category)
    if not test_set:
        return http.HttpResponseBadRequest(
            'No test set was found for category=%s' % category)

    formatted_gviz_table_data = GetStats(request, test_set, 'gviz_table_data')
    return http.HttpResponse(formatted_gviz_table_data) 
Example 5
Project: browserscope   Author: elsigh   File: user_test.py    License: Apache License 2.0 6 votes vote down vote up
def GetTestScoreAndDisplayValue(self, test_key, raw_scores):
    """Get a normalized score (0 to 100) and a value to output to the display.

    Args:
      test_key: a key for a test_set test.
      raw_scores: a dict of raw_scores indexed by test keys.
    Returns:
      score, display_value
          # score is from 0 to 100.
          # display_value is the text for the cell.
    """
    #logging.info('GetTestScoreAndDisplayValue, %s, %s' % (test_key, raw_scores))
    score = 0
    raw_score = raw_scores.get(test_key, None)
    if raw_score is None:
      raw_score = ''

    if raw_score != '':
      test = Test.get_test_from_category(self.category)
      score = test.get_score_from_display(test_key, raw_score)

    #logging.info('score: %s, display: %s', score, raw_score)
    return score, raw_score 
Example 6
Project: browserscope   Author: elsigh   File: test_util.py    License: Apache License 2.0 6 votes vote down vote up
def testBeacon(self):
    csrf_token = self.client.get('/get_csrf').content
    params = {
      'category': self.test_set.category,
      'results': 'apple=1,banana=2,coconut=4',
      'csrf_token': csrf_token
    }
    response = self.client.get('/beacon', params, **mock_data.UNIT_TEST_UA)
    self.assertEqual(204, response.status_code)

    # Did a ResultParent get created?
    query = db.Query(result.ResultParent)
    query.filter('category =', self.test_set.category)
    result_parent = query.get()
    self.assertNotEqual(result_parent, None)

    result_times = result_parent.GetResultTimes()
    self.assertEqual(
        [('apple', 1, False), ('banana', 2, False), ('coconut', 4, False)],
        sorted((x.test, x.score, x.dirty) for x in result_times)) 
Example 7
Project: browserscope   Author: elsigh   File: test_admin.py    License: Apache License 2.0 6 votes vote down vote up
def testCreated(self):
    created_base = datetime.datetime(2009, 9, 9, 9, 9, 0)
    keys = []
    for scores in ((0, 10, 100), (1, 20, 200)):
      ip = '1.2.2.%s' % scores[1]
      result = ResultParent.AddResult(
          self.test_set, ip, mock_data.GetUserAgentString('Firefox 3.5'),
          'apple=%s,banana=%s,coconut=%s' % scores,
          created=created_base + datetime.timedelta(seconds=scores[1]))
      keys.append(str(result.key()))
    params = {
          'model': 'ResultParent',
          'created': created_base + datetime.timedelta(seconds=15),
          }
    response = self.client.get('/admin/data_dump_keys', params)
    self.assertEqual(200, response.status_code)
    response_params = simplejson.loads(response.content)
    self.assertEqual(None, response_params['bookmark'])
    self.assertEqual(keys[1:], response_params['keys']) 
Example 8
Project: browserscope   Author: elsigh   File: test_admin.py    License: Apache License 2.0 6 votes vote down vote up
def testBasic(self):
    self.mox.StubOutWithMock(
        result_stats.CategoryBrowserManager, 'UpdateSummaryBrowsers')
    categories = [ts.category for ts in all_test_sets.GetVisibleTestSets()]
    # Use mox to make sure UpdateSummaryBrowsers gets called.
    result_stats.CategoryBrowserManager.UpdateSummaryBrowsers(categories)
    self.mox.ReplayAll()
    params = {
        'category': 'network',
        'version_level': 0,
        'browsers': 'IE,Firefox',
        }
    response = self.client.get('/admin/upload_category_browsers', params)
    self.assertEqual('Success.', response.content)
    self.assertEqual(200, response.status_code)
    self.assertEqual(['Firefox', 'IE'], self.manager.GetBrowsers('network', 0))
    self.mox.VerifyAll() 
Example 9
Project: browserscope   Author: elsigh   File: memcache_viewer.py    License: Apache License 2.0 6 votes vote down vote up
def _get_memcache_value_and_type(self, key):
    """Fetch value from memcache and detect its type.

    Args:
      key: String

    Returns:
      (value, type), value is a Python object or None if the key was not set in
      the cache, type is a string describing the type of the value.
    """
    try:
      value = memcache.get(key)
    except (pickle.UnpicklingError, AttributeError, EOFError, ImportError,
            IndexError), e:
      # Pickled data could be broken or the user might have stored custom class
      # instances in the cache (which can't be unpickled from here).
      msg = 'Failed to retrieve value from cache: %s' % e
      return msg, 'error' 
Example 10
Project: earthengine   Author: mortcanty   File: appengine.py    License: 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 11
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 6 votes vote down vote up
def __init__(self, scope, **kwargs):
    """Constructor for AppAssertionCredentials

    Args:
      scope: string or iterable of strings, scope(s) of the credentials being
        requested.
      **kwargs: optional keyword args, including:
        service_account_id: service account id of the application. If None or
          unspecified, the default service account for the app is used.
    """
    self.scope = util.scopes_to_string(scope)
    self._kwargs = kwargs
    self.service_account_id = kwargs.get('service_account_id', None)

    # Assertion type is no longer used, but still in the parent class signature.
    super(AppAssertionCredentials, self).__init__(None) 
Example 12
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 6 votes vote down vote up
def locked_get(self):
    """Retrieve Credential from datastore.

    Returns:
      oauth2client.Credentials
    """
    credentials = None
    if self._cache:
      json = self._cache.get(self._key_name)
      if json:
        credentials = Credentials.new_from_json(json)
    if credentials is None:
      entity = self._get_entity()
      if entity is not None:
        credentials = getattr(entity, self._property_name)
        if self._cache:
          self._cache.set(self._key_name, credentials.to_json())

    if credentials and hasattr(credentials, 'set_store'):
      credentials.set_store(self)
    return credentials 
Example 13
Project: earthengine   Author: mortcanty   File: appengine.py    License: MIT License 6 votes vote down vote up
def oauth2decorator_from_clientsecrets(filename, scope,
                                       message=None, cache=None):
  """Creates an OAuth2Decorator populated from a clientsecrets file.

  Args:
    filename: string, File name of client secrets.
    scope: string or list of strings, scope(s) of the credentials being
      requested.
    message: string, A friendly string to display to the user if the
      clientsecrets file is missing or invalid. The message may contain HTML and
      will be presented on the web interface for any method that uses the
      decorator.
    cache: An optional cache service client that implements get() and set()
      methods. See clientsecrets.loadfile() for details.

  Returns: An OAuth2Decorator

  """
  return OAuth2DecoratorFromClientSecrets(filename, scope,
                                          message=message, cache=cache) 
Example 14
Project: Sony-PMCA-RE   Author: ma1co   File: main.py    License: MIT License 6 votes vote down vote up
def updateAppStats(data):
 device = data.get('deviceinfo', {})
 apps = dict((app['name'], app['version']) for app in data.get('applications', []) if 'name' in app and 'version' in app)
 if 'name' in device and 'productcode' in device and 'deviceid' in device:
  id = hashlib.sha1('camera_%s_%s_%s' % (device['name'], device['productcode'], device['deviceid'])).hexdigest()
  camera = Camera.get_by_id(id)
  isNewCamera = not camera
  if not camera:
   camera = Camera(id=id)
  camera.model = device['name']
  camera.apps, installed, updated = diffApps(camera.apps or {}, apps)
  camera.put()
  if isNewCamera:
   CameraModelCounter.increment(camera.model)
  for app in installed:
   AppInstallCounter.increment(app)
  for app in updated:
   AppUpdateCounter.increment(app) 
Example 15
Project: Sony-PMCA-RE   Author: ma1co   File: main.py    License: MIT License 6 votes vote down vote up
def post(self):
  data = self.request.body
  dataDict = marketserver.parsePostData(data)
  taskKey = int(dataDict.get('session', {}).get('correlationid', 0))
  task = ndb.Key(Task, taskKey).get()
  if not task:
   return self.error(404)
  if not task.completed and task.app:
   response = marketserver.getJsonInstallResponse('App', self.uri_for('appSpk', appId = task.app, _full = True))
  else:
   response = marketserver.getJsonResponse()
  task.completed = True
  task.response = data
  task.put()
  updateAppStats(dataDict)
  self.output(marketserver.constants.jsonMimeType, response) 
Example 16
Project: hackernewsbot   Author: phil-r   File: main.py    License: MIT License 5 votes vote down vote up
def story_redirect(short_id):
  """Redirect to story url"""
  try:
    story_id = str(shortener.decode(short_id))
  except:
    return abort(400)
  redirect_url = memcache.get(story_id)
  if not redirect_url:
    story = ndb.Key(StoryPost, story_id).get()
    if not story:
      return make_response('<h1>Service Unavailable</h1><p>Try again later</p>', 503, {'Retry-After': 5})
    story.add_memcache()
    redirect_url = story.url
  return redirect(redirect_url) 
Example 17
Project: hackernewsbot   Author: phil-r   File: main.py    License: MIT License 5 votes vote down vote up
def task(stories):
  def check_story(rpc):
    try:
      result = rpc.get_result()
      story = json.loads(result.content)
      if story and story.get('score') >= 100:
        StoryPost.add(story)
      elif story and story.get('score'):
        # api returned a comment once (issue with id 21447853)
        logging.info('STOP: {id} has low score ({score})'.format(**story))
      elif story:
        logging.info('STOP: {id} has no score'.format(**story))
      else:
        logging.info("STOP: story was probably deleted/flagged")
    except urlfetch.DownloadError as ex:
      logging.exception(ex)
    except ValueError as ex:
      logging.info(result.content)
      logging.exception(ex)


  # stringify ids for use in memcache and convert to set for later
  ids = set(str(story_id) for story_id in stories)
  logging.info('checking stories: {}'.format(ids))
  # get stories that we already posted to reduce the number of requests
  cached_stories = set(memcache.get_multi(ids).keys())
  logging.info('cached stories: {}'.format(cached_stories))
  # remove stories we know about from stories that we need to check
  stories_to_check = ids.difference(cached_stories)
  rpcs = map(lambda id: item_async(id, check_story), stories_to_check)
  for rpc in rpcs:
    rpc.wait() 
Example 18
Project: loaner   Author: google   File: template_model.py    License: Apache License 2.0 5 votes vote down vote up
def get(cls, name):
    """Gets a Template by its name.

    Args:
      name: str, the name of the templatew.

    Returns:
      A Template model entity.
    """
    return cls.get_by_id(name) 
Example 19
Project: loaner   Author: google   File: template_model.py    License: Apache License 2.0 5 votes vote down vote up
def _get_subtemplate(sub_name):
    """Gets a template from memcache or datastore for the Jinja2 environment.

    This gets either a sub-component of a Template entity (title or body).

    Args:
      sub_name: str, the name of the subcomponent of the Template (e.g.,
          'template:title:device_due' for the title property of the device_due
          template. If the sub_name is 'base' it gets the body of the base
          template, which has no title.

    Returns:
      The pre-rendered Jinja2-formatted template string.

    Raises:
      NoTemplateError: if the template with that sub-name does not exist.
    """

    if sub_name.endswith('_base'):
      sub_name = _CACHED_BODY_NAME % sub_name
    Template._cache_all_templates()
    cached_template = memcache.get(sub_name)
    if cached_template:
      return cached_template
    else:
      match = _NAME_RE.match(sub_name)
      template_name = match.group(2)  # Stripped template name.
      stored_template = Template.get_by_id(template_name)
      if not stored_template:
        raise NoTemplateError(
            'Template named {} does not exist.'.format(sub_name))
      Template._cache_template(stored_template)
      return getattr(stored_template, match.group(1))  # 'title' or 'body'. 
Example 20
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_from_datastore(self, test_config):
    config = config_model.Config.get(test_config[0])
    self.assertEqual(config, test_config[1]) 
Example 21
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_from_memcache(self):
    config = 'string_config'
    config_value = 'this should be read.'
    memcache.set(config, config_value)
    reference_datastore_config = config_model.Config.get_by_id(config)
    config_memcache = config_model.Config.get(config)

    self.assertEqual(config_memcache, config_value)
    self.assertEqual(reference_datastore_config.string_value, 'config value 1') 
Example 22
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_from_default(
      self, mock_get_by_id, mock_memcache_get, mock_config_model_set):
    config = 'test_config'
    expected_value = 'test_value'
    config_datastore = config_model.Config.get(config)
    mock_config_model_set.assert_called_once_with(config, expected_value)
    self.assertEqual(config_datastore, expected_value) 
Example 23
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_identifier_without_use_asset(self):
    config_datastore = config_model.Config.get('device_identifier_mode')
    self.assertEqual(config_datastore, 'both_required') 
Example 24
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_nonexistent(self):
    with self.assertRaisesRegexp(KeyError, config_model._CONFIG_NOT_FOUND_MSG):
      config_model.Config.get('does_not_exist') 
Example 25
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_set(self, test_config):
    config_model.Config.set(test_config[0], test_config[1])
    memcache_config = memcache.get(test_config[0])
    config = config_model.Config.get(test_config[0])

    self.assertEqual(memcache_config, test_config[1])
    self.assertEqual(config, test_config[1]) 
Example 26
Project: loaner   Author: google   File: config_model_test.py    License: Apache License 2.0 5 votes vote down vote up
def test_set_no_validation(self):
    fake_key = 'fake_int'
    fake_value = 23
    config_model.Config.set(fake_key, fake_value, False)
    result = config_model.Config.get(fake_key)
    self.assertEqual(result, fake_value) 
Example 27
Project: browserscope   Author: elsigh   File: cron.py    License: Apache License 2.0 5 votes vote down vote up
def UpdateRecentTests(request):
  max_recent_tests = 10
  visible_categories = all_test_sets.GetVisibleTestSets()
  #logging.info('visible_categories %s' % visible_categories)

  prev_recent_tests = memcache.get(util.RECENT_TESTS_MEMCACHE_KEY)
  prev_result_parent_key = None
  if prev_recent_tests:
    prev_result_parent_key = prev_recent_tests[0]['result_parent_key']

  recent_tests = []
  recent_query = db.Query(ResultParent).order('-created').filter('category IN',
      [vis.category for vis in visible_categories])
  for result_parent in recent_query.fetch(max_recent_tests):
    if str(result_parent.key()) == prev_result_parent_key:
      num_needed = max_recent_tests - len(recent_tests)
      if num_needed == max_recent_tests:
        return http.HttpResponse('No update needed.')
      else:
        recent_tests.extend(prev_recent_tests[:num_needed])
        break
    recent_scores = result_parent.GetResults()
    test_set = all_test_sets.GetTestSet(result_parent.category)
    visible_test_keys = [t.key for t in test_set.VisibleTests()]
    recent_stats = test_set.GetStats(visible_test_keys, recent_scores)
    recent_tests.append({
        'result_parent_key': str(result_parent.key()),
        'category': result_parent.category,
        'created': result_parent.created,
        'user_agent_pretty': result_parent.user_agent.pretty(),
        'score': recent_stats['summary_score'],
        'display': recent_stats['summary_display'],
        })
  #logging.info('Setting recent tests: %s' % recent_tests)
  memcache.set(util.RECENT_TESTS_MEMCACHE_KEY, recent_tests,
               time=settings.STATS_MEMCACHE_TIMEOUT)
  #logging.info('Read recent tests: %s' %
  #    memcache.get(key=util.RECENT_TESTS_MEMCACHE_KEY))
  return http.HttpResponse('Done') 
Example 28
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 5 votes vote down vote up
def MultiTestFrameset(request):
    """Multi-Page Test Frameset"""
    category = request.GET.get('category', '')
    params = {
        'page_title': 'Multi-Test Frameset',
        'autorun': request.GET.get('autorun', 1),
        'testurl': request.GET.get('testurl', ''),
        'category': category
    }
    return Render(request, MULTI_TEST_FRAMESET_TPL, params, category) 
Example 29
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 5 votes vote down vote up
def MultiTestDriver(request):
    """Multi-Page Test Driver - runs each of multiple tests in sequence in a
    single category"""
    category = request.GET.get('category', '')
    tests = all_test_sets.GetTestSet(category).tests
    params = {
        'page_title': 'Multi-Test Driver',
        'tests': tests,
        'autorun': request.GET.get('autorun'),
        'testurl': request.GET.get('testurl'),
        'category': category
    }
    return Render(request, MULTI_TEST_DRIVER_TPL, params, category) 
Example 30
Project: browserscope   Author: elsigh   File: util.py    License: Apache License 2.0 5 votes vote down vote up
def GetResultUriString(request, category):
    """Parses out a category result string from the request, w/ goo.gl and hash.
    Args:
        request: The request object.
        category: The category.
    Returns:
        The result string in the request url, expanding goo.gl or hashes.
    """
    results_key = '%s_results' % category
    # allow "results" for user_tests.
    results_uri_string = request.GET.get(results_key,
                                         request.GET.get('results', None))
    # Adding a check for None since bots are sending it.
    if results_uri_string and results_uri_string != 'None':
        memcache_str = memcache.get(key=results_uri_string,
                                    namespace=RESULTS_STRING_MEMCACHE_NS)
        if memcache_str:
            results_uri_string = memcache_str

        # If the results_uri_string is a goo.gl link, expand it.
        elif re.search('goo.gl', results_uri_string):
            googl_req_url = ('https://www.googleapis.com/urlshortener/v1/url?'
                             'key=%s&shortUrl=%s' %
                             (settings.GOOGLE_API_KEY, results_uri_string))
            #logging.info('SENDING GOO.GL REQ: %s' % googl_req_url)
            response = urlfetch.fetch(googl_req_url)
            json = simplejson.loads(response.content)
            qs = urlparse.parse_qs(urlparse.urlparse(json['longUrl']).query)
            results_uri_string = qs[results_key][0]
    #logging.info('GetResultUriString RESULTS_URI_STR: %s' % results_uri_string)
    # Stupid web bot crap.
    if results_uri_string == 'None':
        results_uri_string = None
    return results_uri_string