root/galaxy-central/eggs/Beaker-1.4-py2.6.egg/beaker/ext/google.py @ 3

リビジョン 3, 3.8 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1from __future__ import absolute_import
2import cPickle
3import logging
4from datetime import datetime
5
6from beaker.container import OpenResourceNamespaceManager, Container
7from beaker.exceptions import InvalidCacheBackendError
8from beaker.synchronization import null_synchronizer
9
10log = logging.getLogger(__name__)
11
12try:
13    from google.appengine.ext import db
14except ImportError:
15    raise InvalidCacheBackendError("Datastore cache backend requires the "
16                                   "'google.appengine.ext' library")
17
18
19class GoogleNamespaceManager(OpenResourceNamespaceManager):
20    tables = {}
21   
22    def __init__(self, namespace, table_name='beaker_cache', **params):
23        """Creates a datastore namespace manager"""
24        OpenResourceNamespaceManager.__init__(self, namespace)
25       
26        def make_cache():
27            table_dict = dict(created=db.DateTimeProperty(),
28                              accessed=db.DateTimeProperty(),
29                              data=db.BlobProperty())
30            table = type(table_name, (db.Model,), table_dict)
31            return table
32        self.table_name = table_name
33        self.cache = GoogleNamespaceManager.tables.setdefault(table_name, make_cache())
34        self.hash = {}
35        self._is_new = False
36        self.loaded = False
37        self.log_debug = logging.DEBUG >= log.getEffectiveLevel()
38       
39        # Google wants namespaces to start with letters, change the namespace
40        # to start with a letter
41        self.namespace = 'p%s' % self.namespace
42   
43    def get_access_lock(self):
44        return null_synchronizer()
45
46    def get_creation_lock(self, key):
47        # this is weird, should probably be present
48        return null_synchronizer()
49
50    def do_open(self, flags):
51        # If we already loaded the data, don't bother loading it again
52        if self.loaded:
53            self.flags = flags
54            return
55       
56        item = self.cache.get_by_key_name(self.namespace)
57       
58        if not item:
59            self._is_new = True
60            self.hash = {}
61        else:
62            self._is_new = False
63            try:
64                self.hash = cPickle.loads(str(item.data))
65            except (IOError, OSError, EOFError, cPickle.PickleError):
66                if self.log_debug:
67                    log.debug("Couln't load pickle data, creating new storage")
68                self.hash = {}
69                self._is_new = True
70        self.flags = flags
71        self.loaded = True
72   
73    def do_close(self):
74        if self.flags is not None and (self.flags == 'c' or self.flags == 'w'):
75            if self._is_new:
76                item = self.cache(key_name=self.namespace)
77                item.data = cPickle.dumps(self.hash)
78                item.created = datetime.now()
79                item.accessed = datetime.now()
80                item.put()
81                self._is_new = False
82            else:
83                item = self.cache.get_by_key_name(self.namespace)
84                item.data = cPickle.dumps(self.hash)
85                item.accessed = datetime.now()
86                item.put()
87        self.flags = None
88   
89    def do_remove(self):
90        item = self.cache.get_by_key_name(self.namespace)
91        item.delete()
92        self.hash = {}
93       
94        # We can retain the fact that we did a load attempt, but since the
95        # file is gone this will be a new namespace should it be saved.
96        self._is_new = True
97
98    def __getitem__(self, key):
99        return self.hash[key]
100
101    def __contains__(self, key):
102        return self.hash.has_key(key)
103       
104    def __setitem__(self, key, value):
105        self.hash[key] = value
106
107    def __delitem__(self, key):
108        del self.hash[key]
109
110    def keys(self):
111        return self.hash.keys()
112       
113
114class GoogleContainer(Container):
115    namespace_class = GoogleNamespaceManager
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。