1
2
3
4
5
6 """Index.py
7
8 This module provides a way to create indexes to text files.
9
10 Classes:
11 Index Dictionary-like class used to store index information.
12
13 _ShelveIndex An Index class based on the shelve module.
14 _InMemoryIndex An in-memory Index class.
15
16 """
17 import os
18 import array
19 import cPickle
20 import shelve
21
22
24 """An index file wrapped around shelve.
25
26 """
27
28
29
30
31
32 __version = 2
33 __version_key = '__version'
34
35 - def __init__(self, indexname, truncate=None):
36 dict.__init__(self)
37 try:
38 if truncate:
39
40
41 files = [indexname + '.dir',
42 indexname + '.dat',
43 indexname + '.bak'
44 ]
45 for file in files:
46 if os.path.exists(file):
47 os.unlink(file)
48 raise Exception("open a new shelf")
49 self.data = shelve.open(indexname, flag='r')
50 except:
51
52 self.data = shelve.open(indexname, flag='n')
53 self.data[self.__version_key] = self.__version
54 else:
55
56 version = self.data.get(self.__version_key, None)
57 if version is None:
58 raise IOError("Unrecognized index format")
59 elif version != self.__version:
60 raise IOError("Version %s doesn't match my version %s"
61 % (version, self.__version))
62
64 if 'data' in self.__dict__:
65 self.data.close()
66
67
69 """This creates an in-memory index file.
70
71 """
72
73
74
75
76
77 __version = 3
78 __version_key = '__version'
79
80 - def __init__(self, indexname, truncate=None):
102
104 self.__changed = 1
105 dict.update(self, dict)
106
110
114
116 self.__changed = 1
117 dict.clear(self)
118
127
129
130
131
132
133
134
135
136 s = cPickle.dumps(obj)
137 intlist = array.array('b', s)
138 strlist = map(str, intlist)
139 return ','.join(strlist)
140
142 intlist = map(int, str.split(','))
143 intlist = array.array('b', intlist)
144 strlist = map(chr, intlist)
145 return cPickle.loads(''.join(strlist))
146
147 Index = _InMemoryIndex
148