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
23 """An index file wrapped around shelve.
24
25 """
26
27
28
29
30
31 __version = 2
32 __version_key = '__version'
33
34 - def __init__(self, indexname, truncate=None):
35 dict.__init__(self)
36 try:
37 if truncate:
38
39
40 files = [indexname + '.dir',
41 indexname + '.dat',
42 indexname + '.bak'
43 ]
44 for file in files:
45 if os.path.exists(file):
46 os.unlink(file)
47 raise Exception("open a new shelf")
48 self.data = shelve.open(indexname, flag='r')
49 except:
50
51 self.data = shelve.open(indexname, flag='n')
52 self.data[self.__version_key] = self.__version
53 else:
54
55 version = self.data.get(self.__version_key, None)
56 if version is None:
57 raise IOError("Unrecognized index format")
58 elif version != self.__version:
59 raise IOError("Version %s doesn't match my version %s" \
60 % (version, self.__version))
61
65
67 """This creates an in-memory index file.
68
69 """
70
71
72
73
74
75 __version = 3
76 __version_key = '__version'
77
78 - def __init__(self, indexname, truncate=None):
100
102 self.__changed = 1
103 dict.update(self, dict)
111 self.__changed = 1
112 dict.clear(self)
113
122
124
125
126
127
128
129
130
131 s = cPickle.dumps(obj)
132 intlist = array.array('b', s)
133 strlist = map(str, intlist)
134 return ','.join(strlist)
135
137 intlist = map(int, str.split(','))
138 intlist = array.array('b', intlist)
139 strlist = map(chr, intlist)
140 return cPickle.loads(''.join(strlist))
141
142 Index = _InMemoryIndex
143