v0.8.0! Let's get this party started!
[larjonas-mediagoblin.git] / mediagoblin / _compat.py
blob9164d5fcebb78e132675edfb3eea6723fe073659
1 import functools
2 import warnings
4 import six
6 if six.PY3:
7 from email.mime.text import MIMEText
8 else:
9 from email.MIMEText import MIMEText
12 def encode_to_utf8(method):
13 def wrapper(self):
14 if six.PY2 and isinstance(method(self), six.text_type):
15 return method(self).encode('utf-8')
16 return method(self)
17 functools.update_wrapper(wrapper, method, ['__name__', '__doc__'])
18 return wrapper
21 # based on django.utils.encoding.python_2_unicode_compatible
22 def py2_unicode(klass):
23 if six.PY2:
24 if '__str__' not in klass.__dict__:
25 warnings.warn("@py2_unicode cannot be applied "
26 "to %s because it doesn't define __str__()." %
27 klass.__name__)
28 klass.__unicode__ = klass.__str__
29 klass.__str__ = encode_to_utf8(klass.__unicode__)
30 if '__repr__' in klass.__dict__:
31 klass.__repr__ = encode_to_utf8(klass.__repr__)
32 return klass