1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- #$Id: back_anydbm.py,v 1.45 2002-07-14 04:03:14 richard Exp $
18+ #$Id: back_anydbm.py,v 1.46 2002-07-14 06:06:34 richard Exp $
1919'''
2020This module defines a backend that saves the hyperdatabase in a database
2121chosen by anydbm. It is guaranteed to always be available in python
@@ -143,16 +143,10 @@ def getclassdb(self, classname, mode='r'):
143143 print >> hyperdb .DEBUG , 'getclassdb' , (self , classname , mode )
144144 return self ._opendb ('nodes.%s' % classname , mode )
145145
146- def _opendb (self , name , mode ):
147- '''Low-level database opener that gets around anydbm/dbm
148- eccentricities.
146+ def determine_db_type (self , path ):
147+ ''' determine which DB wrote the class file
149148 '''
150- if __debug__ :
151- print >> hyperdb .DEBUG , '_opendb' , (self , name , mode )
152-
153- # determine which DB wrote the class file
154149 db_type = ''
155- path = os .path .join (os .getcwd (), self .dir , name )
156150 if os .path .exists (path ):
157151 db_type = whichdb .whichdb (path )
158152 if not db_type :
@@ -161,6 +155,18 @@ def _opendb(self, name, mode):
161155 # if the path ends in '.db', it's a dbm database, whether
162156 # anydbm says it's dbhash or not!
163157 db_type = 'dbm'
158+ return db_type
159+
160+ def _opendb (self , name , mode ):
161+ '''Low-level database opener that gets around anydbm/dbm
162+ eccentricities.
163+ '''
164+ if __debug__ :
165+ print >> hyperdb .DEBUG , '_opendb' , (self , name , mode )
166+
167+ # figure the class db type
168+ path = os .path .join (os .getcwd (), self .dir , name )
169+ db_type = self .determine_db_type (path )
164170
165171 # new database? let anydbm pick the best dbm
166172 if not db_type :
@@ -428,19 +434,12 @@ def pack(self, pack_before):
428434
429435 classes = self .getclasses ()
430436
431- # TODO: factor this out to method - we're already doing it in
432- # _opendb.
433- db_type = ''
434- path = os .path .join (os .getcwd (), self .dir , classes [0 ])
435- if os .path .exists (path ):
436- db_type = whichdb .whichdb (path )
437- if not db_type :
438- raise hyperdb .DatabaseError , "Couldn't identify database type"
439- elif os .path .exists (path + '.db' ):
440- db_type = 'dbm'
437+ # figure the class db type
441438
442439 for classname in classes :
443440 db_name = 'journals.%s' % classname
441+ path = os .path .join (os .getcwd (), self .dir , classname )
442+ db_type = self .determine_db_type (path )
444443 db = self ._opendb (db_name , 'w' )
445444
446445 for key in db .keys ():
@@ -740,7 +739,6 @@ def create(self, **propvalues):
740739 if isinstance (prop , Multilink ):
741740 propvalues [key ] = []
742741 else :
743- # TODO: None isn't right here, I think...
744742 propvalues [key ] = None
745743
746744 # done
@@ -809,7 +807,6 @@ def get(self, nodeid, propname, default=_marker, cache=1):
809807 if isinstance (prop , Multilink ):
810808 return []
811809 else :
812- # TODO: None isn't right here, I think...
813810 return None
814811 else :
815812 return default
@@ -1059,9 +1056,12 @@ def setkey(self, propname):
10591056
10601057 'propname' must be the name of a String property of this class or
10611058 None, or a TypeError is raised. The values of the key property on
1062- all existing nodes must be unique or a ValueError is raised.
1059+ all existing nodes must be unique or a ValueError is raised. If the
1060+ property doesn't exist, KeyError is raised.
10631061 """
1064- # TODO: validate that the property is a String!
1062+ prop = self .getprops ()[propname ]
1063+ if not isinstance (prop , String ):
1064+ raise TypeError , 'key properties must be String'
10651065 self .key = propname
10661066
10671067 def getkey (self ):
@@ -1615,6 +1615,10 @@ def __init__(self, db, classname, **properties):
16151615
16161616#
16171617#$Log: not supported by cvs2svn $
1618+ #Revision 1.45 2002/07/14 04:03:14 richard
1619+ #Implemented a switch to disable journalling for a Class. CGI session
1620+ #database now uses it.
1621+ #
16181622#Revision 1.44 2002/07/14 02:05:53 richard
16191623#. all storage-specific code (ie. backend) is now implemented by the backends
16201624#
0 commit comments