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