Code

9aa575ac63813004cce38c19c62f53cf26501a31
[roundup.git] / roundup / hyperdb.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: hyperdb.py,v 1.58 2002-02-27 03:23:16 richard Exp $
20 __doc__ = """
21 Hyperdatabase implementation, especially field types.
22 """
24 # standard python modules
25 import re, string, weakref, os
27 # roundup modules
28 import date, password
30 DEBUG = os.environ.get('HYPERDBDEBUG', '')
32 #
33 # Types
34 #
35 class String:
36     """An object designating a String property."""
37     def __repr__(self):
38         return '<%s>'%self.__class__
40 class Password:
41     """An object designating a Password property."""
42     def __repr__(self):
43         return '<%s>'%self.__class__
45 class Date:
46     """An object designating a Date property."""
47     def __repr__(self):
48         return '<%s>'%self.__class__
50 class Interval:
51     """An object designating an Interval property."""
52     def __repr__(self):
53         return '<%s>'%self.__class__
55 class Link:
56     """An object designating a Link property that links to a
57        node in a specified class."""
58     def __init__(self, classname, do_journal='no'):
59         self.classname = classname
60         self.do_journal = do_journal == 'yes'
61     def __repr__(self):
62         return '<%s to "%s">'%(self.__class__, self.classname)
64 class Multilink:
65     """An object designating a Multilink property that links
66        to nodes in a specified class.
68        "classname" indicates the class to link to
70        "do_journal" indicates whether the linked-to nodes should have
71                     'link' and 'unlink' events placed in their journal
72     """
73     def __init__(self, classname, do_journal='no'):
74         self.classname = classname
75         self.do_journal = do_journal == 'yes'
76     def __repr__(self):
77         return '<%s to "%s">'%(self.__class__, self.classname)
79 class DatabaseError(ValueError):
80     pass
83 #
84 # the base Database class
85 #
86 class Database:
87     '''A database for storing records containing flexible data types.
89 This class defines a hyperdatabase storage layer, which the Classes use to
90 store their data.
93 Transactions
94 ------------
95 The Database should support transactions through the commit() and
96 rollback() methods. All other Database methods should be transaction-aware,
97 using data from the current transaction before looking up the database.
99 An implementation must provide an override for the get() method so that the
100 in-database value is returned in preference to the in-transaction value.
101 This is necessary to determine if any values have changed during a
102 transaction.
104 '''
106     # flag to set on retired entries
107     RETIRED_FLAG = '__hyperdb_retired'
109     # XXX deviates from spec: storagelocator is obtained from the config
110     def __init__(self, config, journaltag=None):
111         """Open a hyperdatabase given a specifier to some storage.
113         The 'storagelocator' is obtained from config.DATABASE.
114         The meaning of 'storagelocator' depends on the particular
115         implementation of the hyperdatabase.  It could be a file name,
116         a directory path, a socket descriptor for a connection to a
117         database over the network, etc.
119         The 'journaltag' is a token that will be attached to the journal
120         entries for any edits done on the database.  If 'journaltag' is
121         None, the database is opened in read-only mode: the Class.create(),
122         Class.set(), and Class.retire() methods are disabled.
123         """
124         raise NotImplementedError
126     def __getattr__(self, classname):
127         """A convenient way of calling self.getclass(classname)."""
128         raise NotImplementedError
130     def addclass(self, cl):
131         '''Add a Class to the hyperdatabase.
132         '''
133         raise NotImplementedError
135     def getclasses(self):
136         """Return a list of the names of all existing classes."""
137         raise NotImplementedError
139     def getclass(self, classname):
140         """Get the Class object representing a particular class.
142         If 'classname' is not a valid class name, a KeyError is raised.
143         """
144         raise NotImplementedError
146     def clear(self):
147         '''Delete all database contents.
148         '''
149         raise NotImplementedError
151     def getclassdb(self, classname, mode='r'):
152         '''Obtain a connection to the class db that will be used for
153            multiple actions.
154         '''
155         raise NotImplementedError
157     def addnode(self, classname, nodeid, node):
158         '''Add the specified node to its class's db.
159         '''
160         raise NotImplementedError
162     def setnode(self, classname, nodeid, node):
163         '''Change the specified node.
164         '''
165         raise NotImplementedError
167     def getnode(self, classname, nodeid, db=None, cache=1):
168         '''Get a node from the database.
169         '''
170         raise NotImplementedError
172     def hasnode(self, classname, nodeid, db=None):
173         '''Determine if the database has a given node.
174         '''
175         raise NotImplementedError
177     def countnodes(self, classname, db=None):
178         '''Count the number of nodes that exist for a particular Class.
179         '''
180         raise NotImplementedError
182     def getnodeids(self, classname, db=None):
183         '''Retrieve all the ids of the nodes for a particular Class.
184         '''
185         raise NotImplementedError
187     def storefile(self, classname, nodeid, property, content):
188         '''Store the content of the file in the database.
189         
190            The property may be None, in which case the filename does not
191            indicate which property is being saved.
192         '''
193         raise NotImplementedError
195     def getfile(self, classname, nodeid, property):
196         '''Store the content of the file in the database.
197         '''
198         raise NotImplementedError
200     def addjournal(self, classname, nodeid, action, params):
201         ''' Journal the Action
202         'action' may be:
204             'create' or 'set' -- 'params' is a dictionary of property values
205             'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
206             'retire' -- 'params' is None
207         '''
208         raise NotImplementedError
210     def getjournal(self, classname, nodeid):
211         ''' get the journal for id
212         '''
213         raise NotImplementedError
215     def pack(self, pack_before):
216         ''' pack the database
217         '''
218         raise NotImplementedError
220     def commit(self):
221         ''' Commit the current transactions.
223         Save all data changed since the database was opened or since the
224         last commit() or rollback().
225         '''
226         raise NotImplementedError
228     def rollback(self):
229         ''' Reverse all actions from the current transaction.
231         Undo all the changes made since the database was opened or the last
232         commit() or rollback() was performed.
233         '''
234         raise NotImplementedError
236 _marker = []
238 # The base Class class
240 class Class:
241     """The handle to a particular class of nodes in a hyperdatabase."""
243     def __init__(self, db, classname, **properties):
244         """Create a new class with a given name and property specification.
246         'classname' must not collide with the name of an existing class,
247         or a ValueError is raised.  The keyword arguments in 'properties'
248         must map names to property objects, or a TypeError is raised.
249         """
250         self.classname = classname
251         self.properties = properties
252         self.db = weakref.proxy(db)       # use a weak ref to avoid circularity
253         self.key = ''
255         # do the db-related init stuff
256         db.addclass(self)
258     def __repr__(self):
259         return '<hypderdb.Class "%s">'%self.classname
261     # Editing nodes:
263     def create(self, **propvalues):
264         """Create a new node of this class and return its id.
266         The keyword arguments in 'propvalues' map property names to values.
268         The values of arguments must be acceptable for the types of their
269         corresponding properties or a TypeError is raised.
270         
271         If this class has a key property, it must be present and its value
272         must not collide with other key strings or a ValueError is raised.
273         
274         Any other properties on this class that are missing from the
275         'propvalues' dictionary are set to None.
276         
277         If an id in a link or multilink property does not refer to a valid
278         node, an IndexError is raised.
279         """
280         if propvalues.has_key('id'):
281             raise KeyError, '"id" is reserved'
283         if self.db.journaltag is None:
284             raise DatabaseError, 'Database open read-only'
286         # new node's id
287         newid = str(self.count() + 1)
289         # validate propvalues
290         num_re = re.compile('^\d+$')
291         for key, value in propvalues.items():
292             if key == self.key:
293                 try:
294                     self.lookup(value)
295                 except KeyError:
296                     pass
297                 else:
298                     raise ValueError, 'node with key "%s" exists'%value
300             # try to handle this property
301             try:
302                 prop = self.properties[key]
303             except KeyError:
304                 raise KeyError, '"%s" has no property "%s"'%(self.classname,
305                     key)
307             if isinstance(prop, Link):
308                 if type(value) != type(''):
309                     raise ValueError, 'link value must be String'
310                 link_class = self.properties[key].classname
311                 # if it isn't a number, it's a key
312                 if not num_re.match(value):
313                     try:
314                         value = self.db.classes[link_class].lookup(value)
315                     except (TypeError, KeyError):
316                         raise IndexError, 'new property "%s": %s not a %s'%(
317                             key, value, link_class)
318                 elif not self.db.hasnode(link_class, value):
319                     raise IndexError, '%s has no node %s'%(link_class, value)
321                 # save off the value
322                 propvalues[key] = value
324                 # register the link with the newly linked node
325                 if self.properties[key].do_journal:
326                     self.db.addjournal(link_class, value, 'link',
327                         (self.classname, newid, key))
329             elif isinstance(prop, Multilink):
330                 if type(value) != type([]):
331                     raise TypeError, 'new property "%s" not a list of ids'%key
332                 link_class = self.properties[key].classname
333                 l = []
334                 for entry in value:
335                     if type(entry) != type(''):
336                         raise ValueError, 'link value must be String'
337                     # if it isn't a number, it's a key
338                     if not num_re.match(entry):
339                         try:
340                             entry = self.db.classes[link_class].lookup(entry)
341                         except (TypeError, KeyError):
342                             raise IndexError, 'new property "%s": %s not a %s'%(
343                                 key, entry, self.properties[key].classname)
344                     l.append(entry)
345                 value = l
346                 propvalues[key] = value
348                 # handle additions
349                 for id in value:
350                     if not self.db.hasnode(link_class, id):
351                         raise IndexError, '%s has no node %s'%(link_class, id)
352                     # register the link with the newly linked node
353                     if self.properties[key].do_journal:
354                         self.db.addjournal(link_class, id, 'link',
355                             (self.classname, newid, key))
357             elif isinstance(prop, String):
358                 if type(value) != type(''):
359                     raise TypeError, 'new property "%s" not a string'%key
361             elif isinstance(prop, Password):
362                 if not isinstance(value, password.Password):
363                     raise TypeError, 'new property "%s" not a Password'%key
365             elif isinstance(prop, Date):
366                 if value is not None and not isinstance(value, date.Date):
367                     raise TypeError, 'new property "%s" not a Date'%key
369             elif isinstance(prop, Interval):
370                 if value is not None and not isinstance(value, date.Interval):
371                     raise TypeError, 'new property "%s" not an Interval'%key
373         # make sure there's data where there needs to be
374         for key, prop in self.properties.items():
375             if propvalues.has_key(key):
376                 continue
377             if key == self.key:
378                 raise ValueError, 'key property "%s" is required'%key
379             if isinstance(prop, Multilink):
380                 propvalues[key] = []
381             else:
382                 # TODO: None isn't right here, I think...
383                 propvalues[key] = None
385         # convert all data to strings
386         for key, prop in self.properties.items():
387             if isinstance(prop, Date):
388                 if propvalues[key] is not None:
389                     propvalues[key] = propvalues[key].get_tuple()
390             elif isinstance(prop, Interval):
391                 if propvalues[key] is not None:
392                     propvalues[key] = propvalues[key].get_tuple()
393             elif isinstance(prop, Password):
394                 propvalues[key] = str(propvalues[key])
396         # done
397         self.db.addnode(self.classname, newid, propvalues)
398         self.db.addjournal(self.classname, newid, 'create', propvalues)
399         return newid
401     def get(self, nodeid, propname, default=_marker, cache=1):
402         """Get the value of a property on an existing node of this class.
404         'nodeid' must be the id of an existing node of this class or an
405         IndexError is raised.  'propname' must be the name of a property
406         of this class or a KeyError is raised.
408         'cache' indicates whether the transaction cache should be queried
409         for the node. If the node has been modified and you need to
410         determine what its values prior to modification are, you need to
411         set cache=0.
412         """
413         if propname == 'id':
414             return nodeid
416         # get the property (raises KeyErorr if invalid)
417         prop = self.properties[propname]
419         # get the node's dict
420         d = self.db.getnode(self.classname, nodeid, cache=cache)
422         if not d.has_key(propname):
423             if default is _marker:
424                 if isinstance(prop, Multilink):
425                     return []
426                 else:
427                     # TODO: None isn't right here, I think...
428                     return None
429             else:
430                 return default
432         # possibly convert the marshalled data to instances
433         if isinstance(prop, Date):
434             if d[propname] is None:
435                 return None
436             return date.Date(d[propname])
437         elif isinstance(prop, Interval):
438             if d[propname] is None:
439                 return None
440             return date.Interval(d[propname])
441         elif isinstance(prop, Password):
442             p = password.Password()
443             p.unpack(d[propname])
444             return p
446         return d[propname]
448     # XXX not in spec
449     def getnode(self, nodeid, cache=1):
450         ''' Return a convenience wrapper for the node.
452         'nodeid' must be the id of an existing node of this class or an
453         IndexError is raised.
455         'cache' indicates whether the transaction cache should be queried
456         for the node. If the node has been modified and you need to
457         determine what its values prior to modification are, you need to
458         set cache=0.
459         '''
460         return Node(self, nodeid, cache=cache)
462     def set(self, nodeid, **propvalues):
463         """Modify a property on an existing node of this class.
464         
465         'nodeid' must be the id of an existing node of this class or an
466         IndexError is raised.
468         Each key in 'propvalues' must be the name of a property of this
469         class or a KeyError is raised.
471         All values in 'propvalues' must be acceptable types for their
472         corresponding properties or a TypeError is raised.
474         If the value of the key property is set, it must not collide with
475         other key strings or a ValueError is raised.
477         If the value of a Link or Multilink property contains an invalid
478         node id, a ValueError is raised.
479         """
480         if not propvalues:
481             return
483         if propvalues.has_key('id'):
484             raise KeyError, '"id" is reserved'
486         if self.db.journaltag is None:
487             raise DatabaseError, 'Database open read-only'
489         node = self.db.getnode(self.classname, nodeid)
490         if node.has_key(self.db.RETIRED_FLAG):
491             raise IndexError
492         num_re = re.compile('^\d+$')
493         for key, value in propvalues.items():
494             # check to make sure we're not duplicating an existing key
495             if key == self.key and node[key] != value:
496                 try:
497                     self.lookup(value)
498                 except KeyError:
499                     pass
500                 else:
501                     raise ValueError, 'node with key "%s" exists'%value
503             # this will raise the KeyError if the property isn't valid
504             # ... we don't use getprops() here because we only care about
505             # the writeable properties.
506             prop = self.properties[key]
508             # if the value's the same as the existing value, no sense in
509             # doing anything
510             if node.has_key(key) and value == node[key]:
511                 del propvalues[key]
512                 continue
514             # do stuff based on the prop type
515             if isinstance(prop, Link):
516                 link_class = self.properties[key].classname
517                 # if it isn't a number, it's a key
518                 if type(value) != type(''):
519                     raise ValueError, 'link value must be String'
520                 if not num_re.match(value):
521                     try:
522                         value = self.db.classes[link_class].lookup(value)
523                     except (TypeError, KeyError):
524                         raise IndexError, 'new property "%s": %s not a %s'%(
525                             key, value, self.properties[key].classname)
527                 if not self.db.hasnode(link_class, value):
528                     raise IndexError, '%s has no node %s'%(link_class, value)
530                 if self.properties[key].do_journal:
531                     # register the unlink with the old linked node
532                     if node[key] is not None:
533                         self.db.addjournal(link_class, node[key], 'unlink',
534                             (self.classname, nodeid, key))
536                     # register the link with the newly linked node
537                     if value is not None:
538                         self.db.addjournal(link_class, value, 'link',
539                             (self.classname, nodeid, key))
541             elif isinstance(prop, Multilink):
542                 if type(value) != type([]):
543                     raise TypeError, 'new property "%s" not a list of ids'%key
544                 link_class = self.properties[key].classname
545                 l = []
546                 for entry in value:
547                     # if it isn't a number, it's a key
548                     if type(entry) != type(''):
549                         raise ValueError, 'link value must be String'
550                     if not num_re.match(entry):
551                         try:
552                             entry = self.db.classes[link_class].lookup(entry)
553                         except (TypeError, KeyError):
554                             raise IndexError, 'new property "%s": %s not a %s'%(
555                                 key, entry, self.properties[key].classname)
556                     l.append(entry)
557                 value = l
558                 propvalues[key] = value
560                 # handle removals
561                 if node.has_key(key):
562                     l = node[key]
563                 else:
564                     l = []
565                 for id in l[:]:
566                     if id in value:
567                         continue
568                     # register the unlink with the old linked node
569                     if self.properties[key].do_journal:
570                         self.db.addjournal(link_class, id, 'unlink',
571                             (self.classname, nodeid, key))
572                     l.remove(id)
574                 # handle additions
575                 for id in value:
576                     if not self.db.hasnode(link_class, id):
577                         raise IndexError, '%s has no node %s'%(
578                             link_class, id)
579                     if id in l:
580                         continue
581                     # register the link with the newly linked node
582                     if self.properties[key].do_journal:
583                         self.db.addjournal(link_class, id, 'link',
584                             (self.classname, nodeid, key))
585                     l.append(id)
587             elif isinstance(prop, String):
588                 if value is not None and type(value) != type(''):
589                     raise TypeError, 'new property "%s" not a string'%key
591             elif isinstance(prop, Password):
592                 if not isinstance(value, password.Password):
593                     raise TypeError, 'new property "%s" not a Password'% key
594                 propvalues[key] = value = str(value)
596             elif value is not None and isinstance(prop, Date):
597                 if not isinstance(value, date.Date):
598                     raise TypeError, 'new property "%s" not a Date'% key
599                 propvalues[key] = value = value.get_tuple()
601             elif value is not None and isinstance(prop, Interval):
602                 if not isinstance(value, date.Interval):
603                     raise TypeError, 'new property "%s" not an Interval'% key
604                 propvalues[key] = value = value.get_tuple()
606             node[key] = value
608         # nothing to do?
609         if not propvalues:
610             return
612         # do the set, and journal it
613         self.db.setnode(self.classname, nodeid, node)
614         self.db.addjournal(self.classname, nodeid, 'set', propvalues)
616     def retire(self, nodeid):
617         """Retire a node.
618         
619         The properties on the node remain available from the get() method,
620         and the node's id is never reused.
621         
622         Retired nodes are not returned by the find(), list(), or lookup()
623         methods, and other nodes may reuse the values of their key properties.
624         """
625         if self.db.journaltag is None:
626             raise DatabaseError, 'Database open read-only'
627         node = self.db.getnode(self.classname, nodeid)
628         node[self.db.RETIRED_FLAG] = 1
629         self.db.setnode(self.classname, nodeid, node)
630         self.db.addjournal(self.classname, nodeid, 'retired', None)
632     def history(self, nodeid):
633         """Retrieve the journal of edits on a particular node.
635         'nodeid' must be the id of an existing node of this class or an
636         IndexError is raised.
638         The returned list contains tuples of the form
640             (date, tag, action, params)
642         'date' is a Timestamp object specifying the time of the change and
643         'tag' is the journaltag specified when the database was opened.
644         """
645         return self.db.getjournal(self.classname, nodeid)
647     # Locating nodes:
648     def hasnode(self, nodeid):
649         '''Determine if the given nodeid actually exists
650         '''
651         return self.db.hasnode(self.classname, nodeid)
653     def setkey(self, propname):
654         """Select a String property of this class to be the key property.
656         'propname' must be the name of a String property of this class or
657         None, or a TypeError is raised.  The values of the key property on
658         all existing nodes must be unique or a ValueError is raised.
659         """
660         # TODO: validate that the property is a String!
661         self.key = propname
663     def getkey(self):
664         """Return the name of the key property for this class or None."""
665         return self.key
667     def labelprop(self, default_to_id=0):
668         ''' Return the property name for a label for the given node.
670         This method attempts to generate a consistent label for the node.
671         It tries the following in order:
672             1. key property
673             2. "name" property
674             3. "title" property
675             4. first property from the sorted property name list
676         '''
677         k = self.getkey()
678         if  k:
679             return k
680         props = self.getprops()
681         if props.has_key('name'):
682             return 'name'
683         elif props.has_key('title'):
684             return 'title'
685         if default_to_id:
686             return 'id'
687         props = props.keys()
688         props.sort()
689         return props[0]
691     # TODO: set up a separate index db file for this? profile?
692     def lookup(self, keyvalue):
693         """Locate a particular node by its key property and return its id.
695         If this class has no key property, a TypeError is raised.  If the
696         'keyvalue' matches one of the values for the key property among
697         the nodes in this class, the matching node's id is returned;
698         otherwise a KeyError is raised.
699         """
700         cldb = self.db.getclassdb(self.classname)
701         for nodeid in self.db.getnodeids(self.classname, cldb):
702             node = self.db.getnode(self.classname, nodeid, cldb)
703             if node.has_key(self.db.RETIRED_FLAG):
704                 continue
705             if node[self.key] == keyvalue:
706                 return nodeid
707         raise KeyError, keyvalue
709     # XXX: change from spec - allows multiple props to match
710     def find(self, **propspec):
711         """Get the ids of nodes in this class which link to a given node.
713         'propspec' consists of keyword args propname=nodeid   
714           'propname' must be the name of a property in this class, or a
715             KeyError is raised.  That property must be a Link or Multilink
716             property, or a TypeError is raised.
718           'nodeid' must be the id of an existing node in the class linked
719             to by the given property, or an IndexError is raised.
720         """
721         propspec = propspec.items()
722         for propname, nodeid in propspec:
723             # check the prop is OK
724             prop = self.properties[propname]
725             if not isinstance(prop, Link) and not isinstance(prop, Multilink):
726                 raise TypeError, "'%s' not a Link/Multilink property"%propname
727             if not self.db.hasnode(prop.classname, nodeid):
728                 raise ValueError, '%s has no node %s'%(prop.classname, nodeid)
730         # ok, now do the find
731         cldb = self.db.getclassdb(self.classname)
732         l = []
733         for id in self.db.getnodeids(self.classname, cldb):
734             node = self.db.getnode(self.classname, id, cldb)
735             if node.has_key(self.db.RETIRED_FLAG):
736                 continue
737             for propname, nodeid in propspec:
738                 property = node[propname]
739                 if isinstance(prop, Link) and nodeid == property:
740                     l.append(id)
741                 elif isinstance(prop, Multilink) and nodeid in property:
742                     l.append(id)
743         return l
745     def stringFind(self, **requirements):
746         """Locate a particular node by matching a set of its String
747         properties in a caseless search.
749         If the property is not a String property, a TypeError is raised.
750         
751         The return is a list of the id of all nodes that match.
752         """
753         for propname in requirements.keys():
754             prop = self.properties[propname]
755             if isinstance(not prop, String):
756                 raise TypeError, "'%s' not a String property"%propname
757             requirements[propname] = requirements[propname].lower()
758         l = []
759         cldb = self.db.getclassdb(self.classname)
760         for nodeid in self.db.getnodeids(self.classname, cldb):
761             node = self.db.getnode(self.classname, nodeid, cldb)
762             if node.has_key(self.db.RETIRED_FLAG):
763                 continue
764             for key, value in requirements.items():
765                 if node[key] and node[key].lower() != value:
766                     break
767             else:
768                 l.append(nodeid)
769         return l
771     def list(self):
772         """Return a list of the ids of the active nodes in this class."""
773         l = []
774         cn = self.classname
775         cldb = self.db.getclassdb(cn)
776         for nodeid in self.db.getnodeids(cn, cldb):
777             node = self.db.getnode(cn, nodeid, cldb)
778             if node.has_key(self.db.RETIRED_FLAG):
779                 continue
780             l.append(nodeid)
781         l.sort()
782         return l
784     # XXX not in spec
785     def filter(self, filterspec, sort, group, num_re = re.compile('^\d+$')):
786         ''' Return a list of the ids of the active nodes in this class that
787             match the 'filter' spec, sorted by the group spec and then the
788             sort spec
789         '''
790         cn = self.classname
792         # optimise filterspec
793         l = []
794         props = self.getprops()
795         for k, v in filterspec.items():
796             propclass = props[k]
797             if isinstance(propclass, Link):
798                 if type(v) is not type([]):
799                     v = [v]
800                 # replace key values with node ids
801                 u = []
802                 link_class =  self.db.classes[propclass.classname]
803                 for entry in v:
804                     if entry == '-1': entry = None
805                     elif not num_re.match(entry):
806                         try:
807                             entry = link_class.lookup(entry)
808                         except (TypeError,KeyError):
809                             raise ValueError, 'property "%s": %s not a %s'%(
810                                 k, entry, self.properties[k].classname)
811                     u.append(entry)
813                 l.append((0, k, u))
814             elif isinstance(propclass, Multilink):
815                 if type(v) is not type([]):
816                     v = [v]
817                 # replace key values with node ids
818                 u = []
819                 link_class =  self.db.classes[propclass.classname]
820                 for entry in v:
821                     if not num_re.match(entry):
822                         try:
823                             entry = link_class.lookup(entry)
824                         except (TypeError,KeyError):
825                             raise ValueError, 'new property "%s": %s not a %s'%(
826                                 k, entry, self.properties[k].classname)
827                     u.append(entry)
828                 l.append((1, k, u))
829             elif isinstance(propclass, String):
830                 # simple glob searching
831                 v = re.sub(r'([\|\{\}\\\.\+\[\]\(\)])', r'\\\1', v)
832                 v = v.replace('?', '.')
833                 v = v.replace('*', '.*?')
834                 l.append((2, k, re.compile(v, re.I)))
835             else:
836                 l.append((6, k, v))
837         filterspec = l
839         # now, find all the nodes that are active and pass filtering
840         l = []
841         cldb = self.db.getclassdb(cn)
842         for nodeid in self.db.getnodeids(cn, cldb):
843             node = self.db.getnode(cn, nodeid, cldb)
844             if node.has_key(self.db.RETIRED_FLAG):
845                 continue
846             # apply filter
847             for t, k, v in filterspec:
848                 # this node doesn't have this property, so reject it
849                 if not node.has_key(k): break
851                 if t == 0 and node[k] not in v:
852                     # link - if this node'd property doesn't appear in the
853                     # filterspec's nodeid list, skip it
854                     break
855                 elif t == 1:
856                     # multilink - if any of the nodeids required by the
857                     # filterspec aren't in this node's property, then skip
858                     # it
859                     for value in v:
860                         if value not in node[k]:
861                             break
862                     else:
863                         continue
864                     break
865                 elif t == 2 and (node[k] is None or not v.search(node[k])):
866                     # RE search
867                     break
868                 elif t == 6 and node[k] != v:
869                     # straight value comparison for the other types
870                     break
871             else:
872                 l.append((nodeid, node))
873         l.sort()
875         # optimise sort
876         m = []
877         for entry in sort:
878             if entry[0] != '-':
879                 m.append(('+', entry))
880             else:
881                 m.append((entry[0], entry[1:]))
882         sort = m
884         # optimise group
885         m = []
886         for entry in group:
887             if entry[0] != '-':
888                 m.append(('+', entry))
889             else:
890                 m.append((entry[0], entry[1:]))
891         group = m
892         # now, sort the result
893         def sortfun(a, b, sort=sort, group=group, properties=self.getprops(),
894                 db = self.db, cl=self):
895             a_id, an = a
896             b_id, bn = b
897             # sort by group and then sort
898             for list in group, sort:
899                 for dir, prop in list:
900                     # sorting is class-specific
901                     propclass = properties[prop]
903                     # handle the properties that might be "faked"
904                     # also, handle possible missing properties
905                     try:
906                         if not an.has_key(prop):
907                             an[prop] = cl.get(a_id, prop)
908                         av = an[prop]
909                     except KeyError:
910                         # the node doesn't have a value for this property
911                         if isinstance(propclass, Multilink): av = []
912                         else: av = ''
913                     try:
914                         if not bn.has_key(prop):
915                             bn[prop] = cl.get(b_id, prop)
916                         bv = bn[prop]
917                     except KeyError:
918                         # the node doesn't have a value for this property
919                         if isinstance(propclass, Multilink): bv = []
920                         else: bv = ''
922                     # String and Date values are sorted in the natural way
923                     if isinstance(propclass, String):
924                         # clean up the strings
925                         if av and av[0] in string.uppercase:
926                             av = an[prop] = av.lower()
927                         if bv and bv[0] in string.uppercase:
928                             bv = bn[prop] = bv.lower()
929                     if (isinstance(propclass, String) or
930                             isinstance(propclass, Date)):
931                         # it might be a string that's really an integer
932                         try:
933                             av = int(av)
934                             bv = int(bv)
935                         except:
936                             pass
937                         if dir == '+':
938                             r = cmp(av, bv)
939                             if r != 0: return r
940                         elif dir == '-':
941                             r = cmp(bv, av)
942                             if r != 0: return r
944                     # Link properties are sorted according to the value of
945                     # the "order" property on the linked nodes if it is
946                     # present; or otherwise on the key string of the linked
947                     # nodes; or finally on  the node ids.
948                     elif isinstance(propclass, Link):
949                         link = db.classes[propclass.classname]
950                         if av is None and bv is not None: return -1
951                         if av is not None and bv is None: return 1
952                         if av is None and bv is None: continue
953                         if link.getprops().has_key('order'):
954                             if dir == '+':
955                                 r = cmp(link.get(av, 'order'),
956                                     link.get(bv, 'order'))
957                                 if r != 0: return r
958                             elif dir == '-':
959                                 r = cmp(link.get(bv, 'order'),
960                                     link.get(av, 'order'))
961                                 if r != 0: return r
962                         elif link.getkey():
963                             key = link.getkey()
964                             if dir == '+':
965                                 r = cmp(link.get(av, key), link.get(bv, key))
966                                 if r != 0: return r
967                             elif dir == '-':
968                                 r = cmp(link.get(bv, key), link.get(av, key))
969                                 if r != 0: return r
970                         else:
971                             if dir == '+':
972                                 r = cmp(av, bv)
973                                 if r != 0: return r
974                             elif dir == '-':
975                                 r = cmp(bv, av)
976                                 if r != 0: return r
978                     # Multilink properties are sorted according to how many
979                     # links are present.
980                     elif isinstance(propclass, Multilink):
981                         if dir == '+':
982                             r = cmp(len(av), len(bv))
983                             if r != 0: return r
984                         elif dir == '-':
985                             r = cmp(len(bv), len(av))
986                             if r != 0: return r
987                 # end for dir, prop in list:
988             # end for list in sort, group:
989             # if all else fails, compare the ids
990             return cmp(a[0], b[0])
992         l.sort(sortfun)
993         return [i[0] for i in l]
995     def count(self):
996         """Get the number of nodes in this class.
998         If the returned integer is 'numnodes', the ids of all the nodes
999         in this class run from 1 to numnodes, and numnodes+1 will be the
1000         id of the next node to be created in this class.
1001         """
1002         return self.db.countnodes(self.classname)
1004     # Manipulating properties:
1006     def getprops(self, protected=1):
1007         """Return a dictionary mapping property names to property objects.
1008            If the "protected" flag is true, we include protected properties -
1009            those which may not be modified."""
1010         d = self.properties.copy()
1011         if protected:
1012             d['id'] = String()
1013         return d
1015     def addprop(self, **properties):
1016         """Add properties to this class.
1018         The keyword arguments in 'properties' must map names to property
1019         objects, or a TypeError is raised.  None of the keys in 'properties'
1020         may collide with the names of existing properties, or a ValueError
1021         is raised before any properties have been added.
1022         """
1023         for key in properties.keys():
1024             if self.properties.has_key(key):
1025                 raise ValueError, key
1026         self.properties.update(properties)
1029 # XXX not in spec
1030 class Node:
1031     ''' A convenience wrapper for the given node
1032     '''
1033     def __init__(self, cl, nodeid, cache=1):
1034         self.__dict__['cl'] = cl
1035         self.__dict__['nodeid'] = nodeid
1036         self.__dict__['cache'] = cache
1037     def keys(self, protected=1):
1038         return self.cl.getprops(protected=protected).keys()
1039     def values(self, protected=1):
1040         l = []
1041         for name in self.cl.getprops(protected=protected).keys():
1042             l.append(self.cl.get(self.nodeid, name, cache=self.cache))
1043         return l
1044     def items(self, protected=1):
1045         l = []
1046         for name in self.cl.getprops(protected=protected).keys():
1047             l.append((name, self.cl.get(self.nodeid, name, cache=self.cache)))
1048         return l
1049     def has_key(self, name):
1050         return self.cl.getprops().has_key(name)
1051     def __getattr__(self, name):
1052         if self.__dict__.has_key(name):
1053             return self.__dict__[name]
1054         try:
1055             return self.cl.get(self.nodeid, name, cache=self.cache)
1056         except KeyError, value:
1057             # we trap this but re-raise it as AttributeError - all other
1058             # exceptions should pass through untrapped
1059             pass
1060         # nope, no such attribute
1061         raise AttributeError, str(value)
1062     def __getitem__(self, name):
1063         return self.cl.get(self.nodeid, name, cache=self.cache)
1064     def __setattr__(self, name, value):
1065         try:
1066             return self.cl.set(self.nodeid, **{name: value})
1067         except KeyError, value:
1068             raise AttributeError, str(value)
1069     def __setitem__(self, name, value):
1070         self.cl.set(self.nodeid, **{name: value})
1071     def history(self):
1072         return self.cl.history(self.nodeid)
1073     def retire(self):
1074         return self.cl.retire(self.nodeid)
1077 def Choice(name, db, *options):
1078     '''Quick helper to create a simple class with choices
1079     '''
1080     cl = Class(db, name, name=String(), order=String())
1081     for i in range(len(options)):
1082         cl.create(name=options[i], order=i)
1083     return hyperdb.Link(name)
1086 # $Log: not supported by cvs2svn $
1087 # Revision 1.57  2002/02/20 05:23:24  richard
1088 # Didn't accomodate new values for new properties
1090 # Revision 1.56  2002/02/20 05:05:28  richard
1091 #  . Added simple editing for classes that don't define a templated interface.
1092 #    - access using the admin "class list" interface
1093 #    - limited to admin-only
1094 #    - requires the csv module from object-craft (url given if it's missing)
1096 # Revision 1.55  2002/02/15 07:27:12  richard
1097 # Oops, precedences around the way w0rng.
1099 # Revision 1.54  2002/02/15 07:08:44  richard
1100 #  . Alternate email addresses are now available for users. See the MIGRATION
1101 #    file for info on how to activate the feature.
1103 # Revision 1.53  2002/01/22 07:21:13  richard
1104 # . fixed back_bsddb so it passed the journal tests
1106 # ... it didn't seem happy using the back_anydbm _open method, which is odd.
1107 # Yet another occurrance of whichdb not being able to recognise older bsddb
1108 # databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
1109 # process.
1111 # Revision 1.52  2002/01/21 16:33:19  rochecompaan
1112 # You can now use the roundup-admin tool to pack the database
1114 # Revision 1.51  2002/01/21 03:01:29  richard
1115 # brief docco on the do_journal argument
1117 # Revision 1.50  2002/01/19 13:16:04  rochecompaan
1118 # Journal entries for link and multilink properties can now be switched on
1119 # or off.
1121 # Revision 1.49  2002/01/16 07:02:57  richard
1122 #  . lots of date/interval related changes:
1123 #    - more relaxed date format for input
1125 # Revision 1.48  2002/01/14 06:32:34  richard
1126 #  . #502951 ] adding new properties to old database
1128 # Revision 1.47  2002/01/14 02:20:15  richard
1129 #  . changed all config accesses so they access either the instance or the
1130 #    config attriubute on the db. This means that all config is obtained from
1131 #    instance_config instead of the mish-mash of classes. This will make
1132 #    switching to a ConfigParser setup easier too, I hope.
1134 # At a minimum, this makes migration a _little_ easier (a lot easier in the
1135 # 0.5.0 switch, I hope!)
1137 # Revision 1.46  2002/01/07 10:42:23  richard
1138 # oops
1140 # Revision 1.45  2002/01/02 04:18:17  richard
1141 # hyperdb docstrings
1143 # Revision 1.44  2002/01/02 02:31:38  richard
1144 # Sorry for the huge checkin message - I was only intending to implement #496356
1145 # but I found a number of places where things had been broken by transactions:
1146 #  . modified ROUNDUPDBSENDMAILDEBUG to be SENDMAILDEBUG and hold a filename
1147 #    for _all_ roundup-generated smtp messages to be sent to.
1148 #  . the transaction cache had broken the roundupdb.Class set() reactors
1149 #  . newly-created author users in the mailgw weren't being committed to the db
1151 # Stuff that made it into CHANGES.txt (ie. the stuff I was actually working
1152 # on when I found that stuff :):
1153 #  . #496356 ] Use threading in messages
1154 #  . detectors were being registered multiple times
1155 #  . added tests for mailgw
1156 #  . much better attaching of erroneous messages in the mail gateway
1158 # Revision 1.43  2001/12/20 06:13:24  rochecompaan
1159 # Bugs fixed:
1160 #   . Exception handling in hyperdb for strings-that-look-like numbers got
1161 #     lost somewhere
1162 #   . Internet Explorer submits full path for filename - we now strip away
1163 #     the path
1164 # Features added:
1165 #   . Link and multilink properties are now displayed sorted in the cgi
1166 #     interface
1168 # Revision 1.42  2001/12/16 10:53:37  richard
1169 # take a copy of the node dict so that the subsequent set
1170 # operation doesn't modify the oldvalues structure
1172 # Revision 1.41  2001/12/15 23:47:47  richard
1173 # Cleaned up some bare except statements
1175 # Revision 1.40  2001/12/14 23:42:57  richard
1176 # yuck, a gdbm instance tests false :(
1177 # I've left the debugging code in - it should be removed one day if we're ever
1178 # _really_ anal about performace :)
1180 # Revision 1.39  2001/12/02 05:06:16  richard
1181 # . We now use weakrefs in the Classes to keep the database reference, so
1182 #   the close() method on the database is no longer needed.
1183 #   I bumped the minimum python requirement up to 2.1 accordingly.
1184 # . #487480 ] roundup-server
1185 # . #487476 ] INSTALL.txt
1187 # I also cleaned up the change message / post-edit stuff in the cgi client.
1188 # There's now a clearly marked "TODO: append the change note" where I believe
1189 # the change note should be added there. The "changes" list will obviously
1190 # have to be modified to be a dict of the changes, or somesuch.
1192 # More testing needed.
1194 # Revision 1.38  2001/12/01 07:17:50  richard
1195 # . We now have basic transaction support! Information is only written to
1196 #   the database when the commit() method is called. Only the anydbm
1197 #   backend is modified in this way - neither of the bsddb backends have been.
1198 #   The mail, admin and cgi interfaces all use commit (except the admin tool
1199 #   doesn't have a commit command, so interactive users can't commit...)
1200 # . Fixed login/registration forwarding the user to the right page (or not,
1201 #   on a failure)
1203 # Revision 1.37  2001/11/28 21:55:35  richard
1204 #  . login_action and newuser_action return values were being ignored
1205 #  . Woohoo! Found that bloody re-login bug that was killing the mail
1206 #    gateway.
1207 #  (also a minor cleanup in hyperdb)
1209 # Revision 1.36  2001/11/27 03:16:09  richard
1210 # Another place that wasn't handling missing properties.
1212 # Revision 1.35  2001/11/22 15:46:42  jhermann
1213 # Added module docstrings to all modules.
1215 # Revision 1.34  2001/11/21 04:04:43  richard
1216 # *sigh* more missing value handling
1218 # Revision 1.33  2001/11/21 03:40:54  richard
1219 # more new property handling
1221 # Revision 1.32  2001/11/21 03:11:28  richard
1222 # Better handling of new properties.
1224 # Revision 1.31  2001/11/12 22:01:06  richard
1225 # Fixed issues with nosy reaction and author copies.
1227 # Revision 1.30  2001/11/09 10:11:08  richard
1228 #  . roundup-admin now handles all hyperdb exceptions
1230 # Revision 1.29  2001/10/27 00:17:41  richard
1231 # Made Class.stringFind() do caseless matching.
1233 # Revision 1.28  2001/10/21 04:44:50  richard
1234 # bug #473124: UI inconsistency with Link fields.
1235 #    This also prompted me to fix a fairly long-standing usability issue -
1236 #    that of being able to turn off certain filters.
1238 # Revision 1.27  2001/10/20 23:44:27  richard
1239 # Hyperdatabase sorts strings-that-look-like-numbers as numbers now.
1241 # Revision 1.26  2001/10/16 03:48:01  richard
1242 # admin tool now complains if a "find" is attempted with a non-link property.
1244 # Revision 1.25  2001/10/11 00:17:51  richard
1245 # Reverted a change in hyperdb so the default value for missing property
1246 # values in a create() is None and not '' (the empty string.) This obviously
1247 # breaks CSV import/export - the string 'None' will be created in an
1248 # export/import operation.
1250 # Revision 1.24  2001/10/10 03:54:57  richard
1251 # Added database importing and exporting through CSV files.
1252 # Uses the csv module from object-craft for exporting if it's available.
1253 # Requires the csv module for importing.
1255 # Revision 1.23  2001/10/09 23:58:10  richard
1256 # Moved the data stringification up into the hyperdb.Class class' get, set
1257 # and create methods. This means that the data is also stringified for the
1258 # journal call, and removes duplication of code from the backends. The
1259 # backend code now only sees strings.
1261 # Revision 1.22  2001/10/09 07:25:59  richard
1262 # Added the Password property type. See "pydoc roundup.password" for
1263 # implementation details. Have updated some of the documentation too.
1265 # Revision 1.21  2001/10/05 02:23:24  richard
1266 #  . roundup-admin create now prompts for property info if none is supplied
1267 #    on the command-line.
1268 #  . hyperdb Class getprops() method may now return only the mutable
1269 #    properties.
1270 #  . Login now uses cookies, which makes it a whole lot more flexible. We can
1271 #    now support anonymous user access (read-only, unless there's an
1272 #    "anonymous" user, in which case write access is permitted). Login
1273 #    handling has been moved into cgi_client.Client.main()
1274 #  . The "extended" schema is now the default in roundup init.
1275 #  . The schemas have had their page headings modified to cope with the new
1276 #    login handling. Existing installations should copy the interfaces.py
1277 #    file from the roundup lib directory to their instance home.
1278 #  . Incorrectly had a Bizar Software copyright on the cgitb.py module from
1279 #    Ping - has been removed.
1280 #  . Fixed a whole bunch of places in the CGI interface where we should have
1281 #    been returning Not Found instead of throwing an exception.
1282 #  . Fixed a deviation from the spec: trying to modify the 'id' property of
1283 #    an item now throws an exception.
1285 # Revision 1.20  2001/10/04 02:12:42  richard
1286 # Added nicer command-line item adding: passing no arguments will enter an
1287 # interactive more which asks for each property in turn. While I was at it, I
1288 # fixed an implementation problem WRT the spec - I wasn't raising a
1289 # ValueError if the key property was missing from a create(). Also added a
1290 # protected=boolean argument to getprops() so we can list only the mutable
1291 # properties (defaults to yes, which lists the immutables).
1293 # Revision 1.19  2001/08/29 04:47:18  richard
1294 # Fixed CGI client change messages so they actually include the properties
1295 # changed (again).
1297 # Revision 1.18  2001/08/16 07:34:59  richard
1298 # better CGI text searching - but hidden filter fields are disappearing...
1300 # Revision 1.17  2001/08/16 06:59:58  richard
1301 # all searches use re now - and they're all case insensitive
1303 # Revision 1.16  2001/08/15 23:43:18  richard
1304 # Fixed some isFooTypes that I missed.
1305 # Refactored some code in the CGI code.
1307 # Revision 1.15  2001/08/12 06:32:36  richard
1308 # using isinstance(blah, Foo) now instead of isFooType
1310 # Revision 1.14  2001/08/07 00:24:42  richard
1311 # stupid typo
1313 # Revision 1.13  2001/08/07 00:15:51  richard
1314 # Added the copyright/license notice to (nearly) all files at request of
1315 # Bizar Software.
1317 # Revision 1.12  2001/08/02 06:38:17  richard
1318 # Roundupdb now appends "mailing list" information to its messages which
1319 # include the e-mail address and web interface address. Templates may
1320 # override this in their db classes to include specific information (support
1321 # instructions, etc).
1323 # Revision 1.11  2001/08/01 04:24:21  richard
1324 # mailgw was assuming certain properties existed on the issues being created.
1326 # Revision 1.10  2001/07/30 02:38:31  richard
1327 # get() now has a default arg - for migration only.
1329 # Revision 1.9  2001/07/29 09:28:23  richard
1330 # Fixed sorting by clicking on column headings.
1332 # Revision 1.8  2001/07/29 08:27:40  richard
1333 # Fixed handling of passed-in values in form elements (ie. during a
1334 # drill-down)
1336 # Revision 1.7  2001/07/29 07:01:39  richard
1337 # Added vim command to all source so that we don't get no steenkin' tabs :)
1339 # Revision 1.6  2001/07/29 05:36:14  richard
1340 # Cleanup of the link label generation.
1342 # Revision 1.5  2001/07/29 04:05:37  richard
1343 # Added the fabricated property "id".
1345 # Revision 1.4  2001/07/27 06:25:35  richard
1346 # Fixed some of the exceptions so they're the right type.
1347 # Removed the str()-ification of node ids so we don't mask oopsy errors any
1348 # more.
1350 # Revision 1.3  2001/07/27 05:17:14  richard
1351 # just some comments
1353 # Revision 1.2  2001/07/22 12:09:32  richard
1354 # Final commit of Grande Splite
1356 # Revision 1.1  2001/07/22 11:58:35  richard
1357 # More Grande Splite
1360 # vim: set filetype=python ts=4 sw=4 et si