| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | """Generic interface to all dbm clones.
 | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Instead of | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |         import dbm | 
					
						
							|  |  |  |         d = dbm.open(file, 'w', 0666) | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | use | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |         import anydbm | 
					
						
							|  |  |  |         d = anydbm.open(file, 'w') | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | The returned object is a dbhash, gdbm, dbm or dumbdbm object, | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | dependent on the type of database being opened (determined by whichdb | 
					
						
							|  |  |  | module) in the case of an existing dbm. If the dbm does not exist and | 
					
						
							|  |  |  | the create or new flag ('c' or 'n') was specified, the dbm type will | 
					
						
							|  |  |  | be determined by the availability of the modules (tested in the above | 
					
						
							|  |  |  | order). | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | It has the following interface (key and data are strings): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |         d[key] = data   # store data at key (may override data at | 
					
						
							|  |  |  |                         # existing key) | 
					
						
							|  |  |  |         data = d[key]   # retrieve data at key (raise KeyError if no | 
					
						
							|  |  |  |                         # such key) | 
					
						
							|  |  |  |         del d[key]      # delete data stored at key (raises KeyError | 
					
						
							|  |  |  |                         # if no such key) | 
					
						
							| 
									
										
										
										
											2002-06-01 14:18:47 +00:00
										 |  |  |         flag = key in d   # true if the key exists | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |         list = d.keys() # return a list of all existing keys (slow!) | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Future versions may change the order in which implementations are | 
					
						
							| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | tested for existence, add interfaces to other dbm-like | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | implementations. | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | The open function has an optional second argument.  This can be 'r', | 
					
						
							|  |  |  | for read-only access, 'w', for read-write access of an existing | 
					
						
							| 
									
										
										
										
											1998-04-28 15:23:09 +00:00
										 |  |  | database, 'c' for read-write access to a new or existing database, and | 
					
						
							|  |  |  | 'n' for read-write access to a new database.  The default is 'r'. | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-28 15:23:09 +00:00
										 |  |  | Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it | 
					
						
							|  |  |  | only if it doesn't exist; and 'n' always creates a new database. | 
					
						
							| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-02-09 17:18:10 +00:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											1995-01-10 00:31:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-18 03:07:20 +00:00
										 |  |  | class error(Exception): | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | _errors = [error] | 
					
						
							|  |  |  | _defaultmod = None | 
					
						
							| 
									
										
										
										
											1995-08-10 19:24:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | for _name in _names: | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         _mod = __import__(_name) | 
					
						
							|  |  |  |     except ImportError: | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |     if not _defaultmod: | 
					
						
							|  |  |  |         _defaultmod = _mod | 
					
						
							|  |  |  |     _errors.append(_mod.error) | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if not _defaultmod: | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |     raise ImportError, "no dbm clone found; tried %s" % _names | 
					
						
							| 
									
										
										
										
											1996-01-25 18:26:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-28 15:19:34 +00:00
										 |  |  | error = tuple(_errors) | 
					
						
							| 
									
										
										
										
											1996-01-25 18:26:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-08-11 14:18:27 +00:00
										 |  |  | def open(file, flag = 'r', mode = 0666): | 
					
						
							| 
									
										
										
										
											2001-01-14 18:09:23 +00:00
										 |  |  |     # guess the type of an existing database | 
					
						
							|  |  |  |     from whichdb import whichdb | 
					
						
							|  |  |  |     result=whichdb(file) | 
					
						
							|  |  |  |     if result is None: | 
					
						
							|  |  |  |         # db doesn't exist | 
					
						
							|  |  |  |         if 'c' in flag or 'n' in flag: | 
					
						
							|  |  |  |             # file doesn't exist and the new | 
					
						
							|  |  |  |             # flag was used so use default type | 
					
						
							|  |  |  |             mod = _defaultmod | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise error, "need 'c' or 'n' flag to open new db" | 
					
						
							|  |  |  |     elif result == "": | 
					
						
							|  |  |  |         # db type cannot be determined | 
					
						
							|  |  |  |         raise error, "db type could not be determined" | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         mod = __import__(result) | 
					
						
							|  |  |  |     return mod.open(file, flag, mode) |