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 string
20 import cPickle
21 import shelve
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
66
68 """This creates an in-memory index file.
69
70 """
71
72
73
74
75
76 __version = 3
77 __version_key = '__version'
78
79 - def __init__(self, indexname, truncate=None):
102
104 self.__changed = 1
105 dict.update(self, dict)
113 self.__changed = 1
114 dict.clear(self)
115
124
126
127
128
129
130
131
132
133 s = cPickle.dumps(obj)
134 intlist = array.array('b', s)
135 strlist = map(str, intlist)
136 return string.join(strlist, ',')
137
139 intlist = map(int, string.split(str, ','))
140 intlist = array.array('b', intlist)
141 strlist = map(chr, intlist)
142 return cPickle.loads(string.join(strlist, ''))
143
144 Index = _InMemoryIndex
145