21
from hashlib import sha1
23
from sha import sha as sha1
18
py3k = getattr(sys, 'py3kwarning', False) or sys.version_info >= (3, 0)
19
py24 = sys.version_info < (2,5)
20
jython = sys.platform.startswith('java')
25
import cPickle as pickle
25
27
from beaker.converters import asbool
28
from base64 import b64encode, b64decode
32
_translation = [chr(_x) for _x in range(256)]
34
# From Python 2.5 base64.py
35
def _translate(s, altchars):
36
translation = _translation[:]
37
for k, v in altchars.items():
38
translation[ord(k)] = v
39
return s.translate(''.join(translation))
41
def b64encode(s, altchars=None):
42
"""Encode a string using Base64.
44
s is the string to encode. Optional altchars must be a string of at least
45
length 2 (additional characters are ignored) which specifies an
46
alternative alphabet for the '+' and '/' characters. This allows an
47
application to e.g. generate url or filesystem safe Base64 strings.
49
The encoded string is returned.
51
# Strip off the trailing newline
52
encoded = binascii.b2a_base64(s)[:-1]
53
if altchars is not None:
54
return _translate(encoded, {'+': altchars[0], '/': altchars[1]})
57
def b64decode(s, altchars=None):
58
"""Decode a Base64 encoded string.
60
s is the string to decode. Optional altchars must be a string of at least
61
length 2 (additional characters are ignored) which specifies the
62
alternative alphabet used instead of the '+' and '/' characters.
64
The decoded string is returned. A TypeError is raised if s were
65
incorrectly padded or if there are non-alphabet characters present in the
68
if altchars is not None:
69
s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
71
return binascii.a2b_base64(s)
72
except binascii.Error, msg:
73
# Transform this exception for consistency
77
from threading import local as _tlocal
80
from dummy_threading import local as _tlocal
82
class _tlocal(object):
84
self.__dict__['_tdict'] = {}
86
def __delattr__(self, key):
88
del self._tdict[(thread.get_ident(), key)]
90
raise AttributeError(key)
92
def __getattr__(self, key):
94
return self._tdict[(thread.get_ident(), key)]
96
raise AttributeError(key)
98
def __setattr__(self, key, value):
99
self._tdict[(thread.get_ident(), key)] = value
28
from threading import local as _tlocal
102
31
__all__ = ["ThreadLocal", "Registry", "WeakValuedRegistry", "SyncDict",
120
def deprecated(func, message):
121
def deprecated_method(*args, **kargs):
122
warnings.warn(message, DeprecationWarning, 2)
123
return func(*args, **kargs)
125
deprecated_method.__name__ = func.__name__
126
except TypeError: # Python < 2.4
128
deprecated_method.__doc__ = "%s\n\n%s" % (message, func.__doc__)
129
return deprecated_method
49
def deprecated(message):
51
def deprecated_method(*args, **kargs):
52
warnings.warn(message, DeprecationWarning, 2)
53
return fn(*args, **kargs)
54
# TODO: use decorator ? functools.wrapper ?
55
deprecated_method.__name__ = fn.__name__
56
deprecated_method.__doc__ = "%s\n\n%s" % (message, fn.__doc__)
57
return deprecated_method
131
60
class ThreadLocal(object):
132
61
"""stores a value on a per-thread basis"""
214
143
self.mutex = _threading.RLock()
215
144
self.dict = weakref.WeakValueDictionary()
218
147
def encoded_path(root, identifiers, extension = ".enc", depth = 3,
219
148
digest_filenames=True):
220
150
"""Generate a unique file-accessible path from the given list of
221
151
identifiers starting at the given root directory."""
222
ident = string.join(identifiers, "_")
152
ident = "_".join(identifiers)
156
from beaker.crypto import sha1
224
158
if digest_filenames:
225
ident = sha1(ident).hexdigest()
160
ident = sha1(ident.encode('utf-8')).hexdigest()
162
ident = sha1(ident).hexdigest()
227
164
ident = os.path.basename(ident)
273
210
('data_dir', (str, types.NoneType), "data_dir must be a string "
274
211
"referring to a directory."),
275
('lock_dir', (str,), "lock_dir must be a string referring to a "
212
('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
277
214
('type', (str, types.NoneType), "Session type must be a string."),
278
215
('cookie_expires', (bool, datetime, timedelta), "Cookie expires was "
299
236
('data_dir', (str, types.NoneType), "data_dir must be a string "
300
237
"referring to a directory."),
301
('lock_dir', (str,), "lock_dir must be a string referring to a "
238
('lock_dir', (str, types.NoneType), "lock_dir must be a string referring to a "
303
240
('type', (str,), "Cache type must be a string."),
304
241
('enabled', (bool, types.NoneType), "enabled must be true/false "