Back to index

moin  1.9.0~rc2
python_compatibility.py
Go to the documentation of this file.
00001 """
00002     MoinMoin - Support Package
00003 
00004     Stuff for compatibility with older Python versions
00005 
00006     @copyright: 2007 Heinrich Wendel <heinrich.wendel@gmail.com>,
00007                 2009 MoinMoin:ThomasWaldmann
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 min_req_exc = Exception("Minimum requirement for MoinMoin is Python 2.4.")
00012 
00013 try:
00014     import string
00015     rsplit = string.rsplit # Python >= 2.4 needed
00016 except AttributeError:
00017     raise min_req_exc
00018 
00019 try:
00020     sorted = sorted # Python >= 2.4 needed
00021 except NameError:
00022     raise min_req_exc
00023 
00024 try:
00025     set = set # Python >= 2.4 needed
00026     frozenset = frozenset
00027 except NameError:
00028     raise min_req_exc
00029 
00030 
00031 try:
00032     from functools import partial # Python >= 2.5 needed
00033 except (NameError, ImportError):
00034     class partial(object):
00035         def __init__(*args, **kw):
00036             self = args[0]
00037             self.fn, self.args, self.kw = (args[1], args[2:], kw)
00038 
00039         def __call__(self, *args, **kw):
00040             if kw and self.kw:
00041                 d = self.kw.copy()
00042                 d.update(kw)
00043             else:
00044                 d = kw or self.kw
00045             return self.fn(*(self.args + args), **d)
00046 
00047 
00048 try:
00049     import hashlib, hmac # Python >= 2.5 needed
00050     hash_new = hashlib.new
00051     def hmac_new(key, msg, digestmod=hashlib.sha1):
00052         return hmac.new(key, msg, digestmod)
00053 
00054 except (NameError, ImportError):
00055     import sha
00056     def hash_new(name, string=''):
00057         if name in ('SHA1', 'sha1'):
00058             return sha.new(string)
00059         elif name in ('MD5', 'md5'):
00060             import md5
00061             return md5.new(string)
00062         raise ValueError("unsupported hash type")
00063 
00064     def hmac_new(key, msg, digestmod=sha):
00065         import hmac
00066         return hmac.new(key, msg, digestmod)
00067