]> git.phdru.name Git - m_lib.git/blob - m_lib/hash/__init__.py
Remove wrong copyright lines, fix module docstrings
[m_lib.git] / m_lib / hash / __init__.py
1 """Extended disk hashes package.
2
3 It extends anydbm/whichdb with ZODB and MetaKit-based hashes."""
4
5
6 __all__ = ["zshelve", "ZODBhash", "MKhash"]
7
8
9 import anydbm
10 anydbm._names.insert(len(anydbm._names)-1, ['ZODBhash', 'MKhash'])
11    # Insert before dumbdbm
12
13
14 import whichdb
15 _orig_module = whichdb
16 _orig_whichdb = _orig_module.whichdb
17
18 def whichdb(filename):
19     result = _orig_whichdb(filename)
20     if result:
21        return result
22
23     try:
24         f = open(filename, "rb")
25     except IOError:
26         return None
27
28     # Read the start of the file -- the magic number
29     s = f.read(4)
30     f.close()
31
32     # Return "" if not at least 4 bytes
33     if len(s) != 4:
34         return ""
35
36     # Check for MetaKit
37     if s == "JL\x1A\0":
38         return "MKhash"
39
40     # Check for ZODB
41     if s == "FS21":
42         return "ZODBhash"
43
44     # Unknown
45     return ""
46
47 _orig_module.whichdb = whichdb # Now install our extended replacement
48 whichdb.__doc__ = _orig_whichdb.__doc__