1
2
3
4
5
6
7
8
9
10
11 """
12 This cache driver creates pickled data as cache in a directory.
13
14 To use this driver, add the following configuration options in your config.py
15
16 py['cacheDriver'] = 'entrypickle'
17 py['cacheConfig'] = '/path/to/a/cache/directory'
18
19 If successful, you will see the cache directory filled up with files that ends
20 with .entryplugin extention in the drectory.
21 """
22
23 __revision__ = "$Revision: 913 $"
24
25 from Pyblosxom import tools
26 from Pyblosxom.cache.base import BlosxomCacheBase
27
28 import cPickle as pickle
29 import os
30 from os import makedirs
31 from os.path import normpath, dirname, exists, abspath
32
34 """
35 This cache stores each entry as a separate pickle file of the
36 entry's contents.
37 """
39 """
40 Takes in a PyBlosxom request object and a configuration string
41 which determines where to store the pickle files.
42 """
43 BlosxomCacheBase.__init__(self, req, config)
44 self._cachefile = ""
45
46 - def load(self, entryid):
47 """
48 Takes an entryid and keeps track of the filename. We only
49 open the file when it's requested with getEntry.
50 """
51 BlosxomCacheBase.load(self, entryid)
52 filename = os.path.join(self._config, entryid.replace('/', '_'))
53 self._cachefile = filename + '.entrypickle'
54
56 """
57 Open the pickle file and return the data therein. If this
58 fails, then we return None.
59 """
60 filep = None
61 try:
62 filep = open(self._cachefile, 'rb')
63 data = pickle.load(filep)
64 filep.close()
65 return data
66 except IOError:
67 return None
68
69 if filep:
70 filep.close()
71
72
74 """
75 Check to see if the file is updated.
76 """
77 return os.path.isfile(self._cachefile) and \
78 os.stat(self._cachefile)[8] >= os.stat(self._entryid)[8]
79
80
81 - def saveEntry(self, entrydata):
82 """
83 Save the data in the entry object to a pickle file.
84 """
85 filep = None
86 try:
87 self.__makepath(self._cachefile)
88 filep = open(self._cachefile, "w+b")
89 entrydata.update({'realfilename': self._entryid})
90 pickle.dump(entrydata, filep, 1)
91 except IOError:
92 pass
93
94 if filep:
95 filep.close()
96
98 """
99 Removes the pickle file for this entry if it exists.
100 """
101 if os.path.isfile(self._cachefile):
102 os.remove(self._cachefile)
103
105 """
106 Returns a list of the keys found in this entrypickle instance.
107 This corresponds to the list of entries that are cached.
108
109 @returns: list of full paths to entries that are cached
110 @rtype: list of strings
111 """
112 import re
113 keys = []
114 cached = []
115 if os.path.isdir(self._config):
116 cached = tools.Walk(self._request,
117 self._config,
118 1,
119 re.compile(r'.*\.entrypickle$'))
120 for cache in cached:
121 cache_data = pickle.load(open(cache))
122 key = cache_data.get('realfilename', '')
123 if not key and os.path.isfile(cache):
124 os.remove(cache)
125 self.load(key)
126 if not self.isCached():
127 self.rmEntry()
128 else:
129 keys.append(key)
130 return keys
131
133 """
134 Creates the directory and all parent directories for a
135 specified path.
136
137 @param path: the path to create
138 @type path: string
139
140 @returns: the normalized absolute path
141 @rtype: string
142 """
143 dpath = normpath(dirname(path))
144 if not exists(dpath):
145 makedirs(dpath)
146 return normpath(abspath(path))
147
148
149