1 """Guess which db package to use to open a db file."""
6 """Guess which db package to use to open a db file.
10 - None if the database file can't be read;
11 - empty string if the file can be read but can't be recognized
12 - the module name (e.g. "dbm" or "gdbm") if recognized.
14 Importing the given module may still fail, and opening the
15 database using that module may still fail.
18 # Check for dbm first -- this has a .pag and a .dir file
20 f
= open(filename
+ ".pag", "rb")
22 f
= open(filename
+ ".dir", "rb")
28 # Check for dumbdbm next -- this has a .dir and and a .dat file
30 f
= open(filename
+ ".dat", "rb")
32 f
= open(filename
+ ".dir", "rb")
34 if f
.read(1) in ["'", '"']:
41 # See if the file exists, return None if not
43 f
= open(filename
, "rb")
47 # Read the start of the file -- the magic number
52 # Return "" if not at least 4 bytes
56 # Convert to 4-byte int in native byte order -- return "" if impossible
58 (magic
,) = struct
.unpack("=l", s
)
63 if magic
== 0x13579ace:
67 if magic
in (0x00061561, 0x61150600):
70 # BSD hash v2 has a 12-byte NULL pad in front of the file type
72 (magic
,) = struct
.unpack("=l", s16
[-4:])
77 if magic
in (0x00061561, 0x61150600):