summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/datastore_models.py
blob: c05a0b9151857cde76b48e5bf29534ec13f686a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import cPickle
import traceback

from appengine_wrappers import db

# A collection of the data store models used throughout the server.
# These values are global within datastore.

class PersistentObjectStoreItem(db.Model):
  pickled_value = db.BlobProperty()

  @classmethod
  def CreateKey(cls, namespace, key):
    path = '%s/%s' % (namespace, key)
    try:
      return db.Key.from_path(cls.__name__, path)
    except Exception:
      # Probably AppEngine's BadValueError for the name being too long, but
      # it's not documented which errors can actually be thrown here, so catch
      # 'em all.
      raise ValueError(
          'Exception thrown when trying to create db.Key from path %s: %s' % (
              path, traceback.format_exc()))

  @classmethod
  def CreateItem(cls, namespace, key, value):
    return PersistentObjectStoreItem(key=cls.CreateKey(namespace, key),
                                     pickled_value=cPickle.dumps(value))

  def GetValue(self):
    return cPickle.loads(self.pickled_value)