/
core.py
283 lines (224 loc) · 9.25 KB
/
core.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- encoding: utf-8 -*-
#
# Copyright 2012 Martin Zimmermann <info@posativ.org>. All rights reserved.
# License: BSD Style, 2 clauses -- see LICENSE.
#
# This provide some basic functionality of Acrylamid: caching and re-validating.
import os
import io
import zlib
import types
import pickle
import shutil
import tempfile
from os.path import join, exists, getmtime, getsize, dirname, basename
from acrylamid import log, defaults
from acrylamid.errors import AcrylamidException
from acrylamid.compat import PY2K, iteritems, iterkeys
from acrylamid.utils import (
classproperty, cached_property, Struct, hash, HashableList, find, execfile,
lchop, force_unicode as u
)
if PY2K:
import cPickle as pickle
__all__ = ['Memory', 'cache', 'Environment', 'Configuration']
class Memory(dict):
"""A callable dictionary object described at
:func:`acrylamid.helpers.memoize`."""
def __call__(self, key, value=None):
if value is None:
return self.get(key)
rv = self.get(key)
self[key] = value
return rv != value
class cache(object):
"""A cache that stores all intermediates of an entry zlib-compressed on
file system. Inspired from ``werkzeug.contrib.cache``, but heavily modified
to fit our needs.
Terminology: A cache object is a pickled dictionary into a single file. An
intermediate (object) is a key/value pair that we store into a cache object.
An intermediate is the content of an entry that is the same for a chain of
filters used in different views.
:class:`cache` is designed as global singleton and should not be constructed.
.. attribute:: cache_dir
Location where all cache objects are being stored, defaults to `.cache/`.
The :class:`cache` does no longer maintain used/unused intermediates and cache
objects due performance reasons (and an edge case described in #67)."""
_fs_transaction_suffix = '.__ac_cache'
cache_dir = '.cache/'
mode = 0o600
memoize = Memory()
@classmethod
def init(self, cache_dir=None):
"""Initialize cache object by creating the cache_dir if non-existent,
read all available cache objects and restore memoized key/values.
:param cache_dir: the directory where cache files are stored.
:param mode: the file mode wanted for the cache files, default 0600
"""
if cache_dir:
self.cache_dir = cache_dir
if not exists(self.cache_dir):
try:
os.mkdir(self.cache_dir, 0o700)
except OSError:
raise AcrylamidException("could not create directory '%s'" % self.cache_dir)
# load memorized items
try:
with io.open(join(self.cache_dir, 'info'), 'rb') as fp:
self.memoize.update(pickle.load(fp))
except (IOError, pickle.PickleError):
self.emptyrun = True
else:
self.emptyrun = False
@classmethod
def shutdown(self):
"""Write memoized key-value pairs to disk."""
try:
with io.open(join(self.cache_dir, 'info'), 'wb') as fp:
pickle.dump(self.memoize, fp, pickle.HIGHEST_PROTOCOL)
except (IOError, pickle.PickleError) as e:
log.warn('%s: %s' % (e.__class__.__name__, e))
@classmethod
def remove(self, path):
"""Remove a cache object completely from disk and `objects`."""
try:
os.remove(join(self.cache_dir, path))
except OSError as e:
log.debug('OSError: %s' % e)
@classmethod
def clear(self, directory=None):
"""Wipe current cache objects and reset all stored informations.
:param directory: directory to clean (defaults to `.cache/`"""
if directory is not None:
self.cache_dir = directory
self.memoize = Memory()
shutil.rmtree(self.cache_dir, ignore_errors=True)
@classmethod
def get(self, path, key, default=None):
"""Restore value from obj[key] if mtime has not changed or return
default.
:param path: path of this cache object
:param key: key of this value
:param default: default return value
"""
try:
with io.open(join(self.cache_dir, path), 'rb') as fp:
return zlib.decompress(pickle.load(fp)[key]).decode('utf-8')
except KeyError:
pass
except (IOError, pickle.PickleError, zlib.error):
self.remove(join(self.cache_dir, path))
return default
@classmethod
def set(self, path, key, value):
"""Save a key, value pair into a blob using pickle and moderate zlib
compression (level 6). We simply save a dictionary containing all
different intermediates (from every view) of an entry.
:param path: path of this cache object
:param key: dictionary key where we store the value
:param value: a string we compress with zlib and afterwards save
"""
path = join(self.cache_dir, path)
if exists(path):
try:
with io.open(path, 'rb') as fp:
rv = pickle.load(fp)
except (pickle.PickleError, IOError):
self.remove(path)
rv = {}
try:
with io.open(path, 'wb') as fp:
rv[key] = zlib.compress(value.encode('utf-8'), 6)
pickle.dump(rv, fp, pickle.HIGHEST_PROTOCOL)
except (IOError, pickle.PickleError) as e:
log.warn('%s: %s' % (e.__class__.__name__, e))
else:
try:
fd, tmp = tempfile.mkstemp(suffix=self._fs_transaction_suffix,
dir=self.cache_dir)
with io.open(fd, 'wb') as fp:
pickle.dump({key: zlib.compress(value.encode('utf-8'), 6)}, fp,
pickle.HIGHEST_PROTOCOL)
os.rename(tmp, path)
os.chmod(path, self.mode)
except (IOError, OSError, pickle.PickleError, zlib.error) as e:
log.warn('%s: %s' % (e.__class__.__name__, e))
return value
@classmethod
def getmtime(self, path, default=0.0):
"""Get last modification timestamp from cache object but store it over
the whole compilation process so we have the same value for different
views.
:param path: valid cache object
:param default: default value if an :class:`OSError` occurs
"""
try:
return getmtime(join(self.cache_dir, path))
except OSError:
return default
@classproperty
@classmethod
def size(self):
"""return size of all cacheobjects in bytes"""
try:
res = getsize(join(self.cache_dir, 'info'))
except OSError:
res = 0
for (path, dirs, files) in os.walk(self.cache_dir):
for file in files:
filename = os.path.join(path, file)
res += getsize(filename)
return res
def load(path):
"""Load default configuration, prepare namespace and update configuration
with `conf.py`'s uppercase values and normalizes ambiguous values.
"""
conf = Configuration(defaults.conf)
ns = dict([(k.upper(), v) for k, v in iteritems(defaults.conf)])
os.chdir(dirname(find(basename(path), u(dirname(path) or os.getcwd()))))
if PY2K:
execfile(path, ns)
else:
exec(compile(open(path).read(), path, 'exec'), ns)
conf.update(dict([(k.lower(), ns[k]) for k in ns if k.upper() == k]))
# append trailing slash to *_dir and place certain values into an array
return defaults.normalize(conf)
class Environment(Struct):
"""Use *only* for the environment container. This class hides un-hashable
keys from :class:`Struct` hash function.
.. attribute:: modified
Return whether the Environment has changed between two runs. This
attribute must only be accessed after all modifications to the environment!
"""
blacklist = set(['engine', 'translationsfor', 'options', 'archives', 'webassets'])
@classmethod
def new(self, env):
return Environment({'author': env.author, 'url': env.url,
'options': env.options, 'globals': Struct()})
def keys(self):
return sorted(list(set(super(Environment, self).keys()) - self.blacklist))
def values(self):
for key in self.keys():
yield self[key]
@cached_property
def modified(self):
return hash(self) != cache.memoize(self.__class__.__name__)
class Configuration(Environment):
"""Similar to :class:`Environment` but allows hashing of a literarily
defined dictionary (that's the conf.py)."""
blacklist = set(['if', 'hooks'])
def fetch(self, ns):
return Configuration((lchop(k, ns), v)
for k, v in iteritems(self) if k.startswith(ns))
def values(self):
for key in self.keys():
if isinstance(self[key], types.FunctionType):
continue
if isinstance(self[key], list):
yield HashableList(self[key])
elif isinstance(self[key], dict):
yield Configuration(self[key])
elif isinstance(self[key], type(None)):
yield -1
else:
yield self[key]