Package buildbot :: Package db :: Module base
[frames] | no frames]

Source Code for Module buildbot.db.base

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  """ 
17  Base classes for database handling 
18  """ 
19   
20 -class DBConnectorComponent(object):
21 """ 22 A fixed component of the DBConnector, handling one particular aspect of the 23 database. Instances of subclasses are assigned to attributes of the 24 DBConnector object, so that they are available at e.g., C{master.db.model} 25 or C{master.db.changes}. This parent class takes care of the necessary 26 backlinks and other housekeeping. 27 """ 28 29 connector = None 30
31 - def __init__(self, connector):
32 self.db = connector 33 "backlink to the DBConnector object" 34 35 # set up caches 36 for method in dir(self.__class__): 37 o = getattr(self, method) 38 if isinstance(o, CachedMethod): 39 setattr(self, method, o.get_cached_method(self))
40
41 -class CachedMethod(object):
42 - def __init__(self, cache_name, method):
43 self.cache_name = cache_name 44 self.method = method
45
46 - def get_cached_method(self, component):
47 meth = self.method 48 49 meth_name = meth.__name__ 50 cache = component.db.master.caches.get_cache(self.cache_name, 51 lambda key : meth(component, key)) 52 def wrap(key, no_cache=0): 53 if no_cache: 54 return meth(component, key) 55 return cache.get(key)
56 wrap.__name__ = meth_name + " (wrapped)" 57 wrap.__module__ = meth.__module__ 58 wrap.__doc__ = meth.__doc__ 59 wrap.cache = cache 60 return wrap
61
62 -def cached(cache_name):
63 """ 64 A decorator for "getter" functions that fetch an object from the database 65 based on a single key. The wrapped method will only be called if the named 66 cache does not contain the key. 67 68 The wrapped function must take one argument (the key); the wrapper will 69 take a key plus an optional C{no_cache} argument which, if true, will cause 70 it to invoke the underlying method even if the key is in the cache. 71 72 The resulting method will have a C{cache} attribute which can be used to 73 access the underlying cache. 74 75 @param cache_name: name of the cache to use 76 """ 77 return lambda method : CachedMethod(cache_name, method)
78