-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskcache.py
141 lines (118 loc) · 4.39 KB
/
diskcache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
__version__ = '$Id$'
import random
import config
import os
# http://mail.python.org/pipermail/python-list/2006-March/375280.html
try:
os.SEEK_SET
except AttributeError:
os.SEEK_SET, os.SEEK_CUR, os.SEEK_END = range(3)
## Dictionary like disk caching module
## (c) Copyright 2008 - Bryan Tong Minh / The Pywikipediabot team
## Licensed under the terms of the MIT license
class CachedReadOnlyDictI(object):
"""A cached readonly dict with case insensitive keys."""
def __init__(self, data, prefix = "", max_size = 10, cache_base = 'cache'):
self.max_size = max_size
while True:
self.cache_path = config.datafilepath(cache_base, prefix + ''.join(
[random.choice('abcdefghijklmnopqrstuvwxyz')
for i in xrange(16)]))
if not os.path.exists(self.cache_path): break
self.cache_file = open(self.cache_path, 'wb+')
lookup = [-1] * 36
data.sort(key = lambda i: i[0])
for key, value in data:
if type(key) is unicode:
key = key.encode('utf-8')
elif type(key) != str:
key = str(key)
key = key.lower()
index = key[0]
if not ((index >= 'a' and index <= 'z') or (index >= '0' and index <= '9')) or '\t' in key:
raise RuntimeError('Only alphabetic keys are supported', key)
if index < 'a':
index = ord(index) - 48 + 26 # Numeric
else:
index = ord(index) - 97
if lookup[index] == -1:
lookup[index] = self.cache_file.tell()
if type(value) is unicode:
value = value.encode('utf-8')
elif type(value) != str:
value = str(value)
if len(key) > 0xFF:
raise RuntimeError('Key length must be smaller than %i' % 0xFF)
if len(value) > 0xFFFFFF:
raise RuntimeError('Value length must be smaller than %i' % 0xFFFFFF)
self.cache_file.write('%02x%s%06x%s' % (len(key), key, len(value), value))
self.lookup = lookup
self.cache_file.close()
self.cache_file = open(self.cache_path, 'rb')
self.cache_file.seek(0)
self.cache = []
def delete(self):
"""
Method is called from wikipedia._flush, on Python exit
Some modules might already have been unloaded, and some
objects might already have been freed:
1) We have to reload all modules used
2) We have to dereference the loaded modules after usage
3) Strange errors can be raised here, we don't care.
"""
try:
self.cache_file.close()
except IOError:
pass
try:
try:
import os
os.unlink(self.cache_path)
except OSError:
pass
finally:
os = None
def __getitem__(self, key):
key = key.lower()
if type(key) is unicode:
key = key.encode('utf-8')
try:
index = key[0]
except IndexError:
raise KeyError(key)
if not ((index >= 'a' and index <= 'z') or (index >= '0' and index <= '9')):
raise KeyError(key)
if index < 'a':
i = ord(index) - 48 + 26 # Numeric
else:
i = ord(index) - 97
for k, v in self.cache:
if k == key:
self.cache.remove((k, v))
self.cache.append((k, v))
try:
k, v = self.cache[-1]
if k == key:
return v
except IndexError:
pass
self.cache_file.seek(self.lookup[i])
while True:
length = int(self.read(2, key), 16)
k = self.read(length, key)
if k == key:
length = int(self.read(6, key), 16)
value = self.read(length, key).decode('utf-8')
if len(self.cache) > self.max_size:
del self.cache[0]
self.cache.append((key, value))
return value
elif k[0] != index:
raise KeyError(key)
length = int(self.read(6, key), 16)
self.cache_file.seek(length, os.SEEK_CUR)
def read(self, length, key = ''):
s = self.cache_file.read(length)
if not s: raise KeyError(key)
return s