Code

Added a target version field to the extended issue schema
[roundup.git] / roundup / backends / back_anydbm.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: back_anydbm.py,v 1.11 2001-11-21 02:34:18 richard Exp $
20 import anydbm, os, marshal
21 from roundup import hyperdb, date, password
23 #
24 # Now the database
25 #
26 class Database(hyperdb.Database):
27     """A database for storing records containing flexible data types."""
29     def __init__(self, storagelocator, journaltag=None):
30         """Open a hyperdatabase given a specifier to some storage.
32         The meaning of 'storagelocator' depends on the particular
33         implementation of the hyperdatabase.  It could be a file name,
34         a directory path, a socket descriptor for a connection to a
35         database over the network, etc.
37         The 'journaltag' is a token that will be attached to the journal
38         entries for any edits done on the database.  If 'journaltag' is
39         None, the database is opened in read-only mode: the Class.create(),
40         Class.set(), and Class.retire() methods are disabled.
41         """
42         self.dir, self.journaltag = storagelocator, journaltag
43         self.classes = {}
44         self.transactions = []
46     #
47     # Classes
48     #
49     def __getattr__(self, classname):
50         """A convenient way of calling self.getclass(classname)."""
51         return self.classes[classname]
53     def addclass(self, cl):
54         cn = cl.classname
55         if self.classes.has_key(cn):
56             raise ValueError, cn
57         self.classes[cn] = cl
59     def getclasses(self):
60         """Return a list of the names of all existing classes."""
61         l = self.classes.keys()
62         l.sort()
63         return l
65     def getclass(self, classname):
66         """Get the Class object representing a particular class.
68         If 'classname' is not a valid class name, a KeyError is raised.
69         """
70         return self.classes[classname]
72     #
73     # Class DBs
74     #
75     def clear(self):
76         for cn in self.classes.keys():
77             db = os.path.join(self.dir, 'nodes.%s'%cn)
78             anydbm.open(db, 'n')
79             db = os.path.join(self.dir, 'journals.%s'%cn)
80             anydbm.open(db, 'n')
82     def getclassdb(self, classname, mode='r'):
83         ''' grab a connection to the class db that will be used for
84             multiple actions
85         '''
86         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
87         if os.path.exists(path):
88             return anydbm.open(path, mode)
89         else:
90             return anydbm.open(path, 'n')
92     #
93     # Nodes
94     #
95     def addnode(self, classname, nodeid, node):
96         ''' add the specified node to its class's db
97         '''
98         db = self.getclassdb(classname, 'c')
99         # now save the marshalled data
100         db[nodeid] = marshal.dumps(node)
101         db.close()
102     setnode = addnode
104     def getnode(self, classname, nodeid, cldb=None):
105         ''' add the specified node to its class's db
106         '''
107         db = cldb or self.getclassdb(classname)
108         if not db.has_key(nodeid):
109             raise IndexError, nodeid
110         res = marshal.loads(db[nodeid])
111         if not cldb: db.close()
112         return res
114     def hasnode(self, classname, nodeid, cldb=None):
115         ''' add the specified node to its class's db
116         '''
117         db = cldb or self.getclassdb(classname)
118         res = db.has_key(nodeid)
119         if not cldb: db.close()
120         return res
122     def countnodes(self, classname, cldb=None):
123         db = cldb or self.getclassdb(classname)
124         return len(db.keys())
125         if not cldb: db.close()
126         return res
128     def getnodeids(self, classname, cldb=None):
129         db = cldb or self.getclassdb(classname)
130         res = db.keys()
131         if not cldb: db.close()
132         return res
134     #
135     # Journal
136     #
137     def addjournal(self, classname, nodeid, action, params):
138         ''' Journal the Action
139         'action' may be:
141             'create' or 'set' -- 'params' is a dictionary of property values
142             'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
143             'retire' -- 'params' is None
144         '''
145         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
146             params)
147         db = anydbm.open(os.path.join(self.dir, 'journals.%s'%classname), 'c')
148         if db.has_key(nodeid):
149             s = db[nodeid]
150             l = marshal.loads(db[nodeid])
151             l.append(entry)
152         else:
153             l = [entry]
154         db[nodeid] = marshal.dumps(l)
155         db.close()
157     def getjournal(self, classname, nodeid):
158         ''' get the journal for id
159         '''
160         # attempt to open the journal - in some rare cases, the journal may
161         # not exist
162         try:
163             db = anydbm.open(os.path.join(self.dir, 'journals.%s'%classname),
164                 'r')
165         except anydbm.open, error:
166             if error.args[0] != 2: raise
167             return []
168         journal = marshal.loads(db[nodeid])
169         res = []
170         for entry in journal:
171             (nodeid, date_stamp, self.journaltag, action, params) = entry
172             date_obj = date.Date(date_stamp)
173             res.append((nodeid, date_obj, self.journaltag, action, params))
174         db.close()
175         return res
177     def close(self):
178         ''' Close the Database - we must release the circular refs so that
179             we can be del'ed and the underlying anydbm connections closed
180             cleanly.
181         '''
182         self.classes = {}
185     #
186     # Basic transaction support
187     #
188     def commit(self):
189         ''' Commit the current transactions.
190         '''
191         # lock the DB
192         for action, classname, entry in self.transactions:
193             # write the node, figure what's changed for the journal.
194             pass
195         # unlock the DB
197     def rollback(self):
198         ''' Reverse all actions from the current transaction.
199         '''
200         self.transactions = []
203 #$Log: not supported by cvs2svn $
204 #Revision 1.10  2001/10/09 23:58:10  richard
205 #Moved the data stringification up into the hyperdb.Class class' get, set
206 #and create methods. This means that the data is also stringified for the
207 #journal call, and removes duplication of code from the backends. The
208 #backend code now only sees strings.
210 #Revision 1.9  2001/10/09 07:25:59  richard
211 #Added the Password property type. See "pydoc roundup.password" for
212 #implementation details. Have updated some of the documentation too.
214 #Revision 1.8  2001/09/29 13:27:00  richard
215 #CGI interfaces now spit up a top-level index of all the instances they can
216 #serve.
218 #Revision 1.7  2001/08/12 06:32:36  richard
219 #using isinstance(blah, Foo) now instead of isFooType
221 #Revision 1.6  2001/08/07 00:24:42  richard
222 #stupid typo
224 #Revision 1.5  2001/08/07 00:15:51  richard
225 #Added the copyright/license notice to (nearly) all files at request of
226 #Bizar Software.
228 #Revision 1.4  2001/07/30 01:41:36  richard
229 #Makes schema changes mucho easier.
231 #Revision 1.3  2001/07/25 01:23:07  richard
232 #Added the Roundup spec to the new documentation directory.
234 #Revision 1.2  2001/07/23 08:20:44  richard
235 #Moved over to using marshal in the bsddb and anydbm backends.
236 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
237 # retired - mod hyperdb.Class.list() so it lists retired nodes)