From aa2bf4134ee9d1c5351d449bff8592785eb1df47 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Tue, 31 May 2016 20:12:50 +0200 Subject: [PATCH] introduced _compact from pydal --- gluon/_compat.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 gluon/_compat.py diff --git a/gluon/_compat.py b/gluon/_compat.py new file mode 100644 index 00000000..aee1f809 --- /dev/null +++ b/gluon/_compat.py @@ -0,0 +1,106 @@ +import sys +import hashlib +import os + +PY2 = sys.version_info[0] == 2 + +_identity = lambda x: x + +if PY2: + import cPickle as pickle + from cStringIO import StringIO + import copy_reg as copyreg + reduce = reduce + hashlib_md5 = hashlib.md5 + iterkeys = lambda d: d.iterkeys() + itervalues = lambda d: d.itervalues() + iteritems = lambda d: d.iteritems() + integer_types = (int, long) + string_types = (str, unicode) + text_type = unicode + basestring = basestring + xrange = xrange + + def implements_iterator(cls): + cls.next = cls.__next__ + del cls.__next__ + return cls + + def implements_bool(cls): + cls.__nonzero__ = cls.__bool__ + del cls.__bool__ + return cls + + def to_bytes(obj, charset='utf-8', errors='strict'): + if obj is None: + return None + if isinstance(obj, (bytes, bytearray, buffer)): + return bytes(obj) + if isinstance(obj, unicode): + return obj.encode(charset, errors) + raise TypeError('Expected bytes') + + def to_native(obj, charset='utf8', errors='strict'): + if obj is None or isinstance(obj, str): + return obj + return obj.encode(charset, errors) +else: + import pickle + from io import StringIO + import copyreg + from functools import reduce + hashlib_md5 = lambda s: hashlib.md5(bytes(s, 'utf8')) + iterkeys = lambda d: iter(d.keys()) + itervalues = lambda d: iter(d.values()) + iteritems = lambda d: iter(d.items()) + integer_types = (int,) + string_types = (str,) + text_type = str + basestring = str + xrange = range + + implements_iterator = _identity + implements_bool = _identity + + def to_bytes(obj, charset='utf-8', errors='strict'): + if obj is None: + return None + if isinstance(obj, (bytes, bytearray, memoryview)): + return bytes(obj) + if isinstance(obj, str): + return obj.encode(charset, errors) + raise TypeError('Expected bytes') + + def to_native(obj, charset='utf8', errors='strict'): + if obj is None or isinstance(obj, str): + return obj + return obj.decode(charset, errors) + + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + # This requires a bit of explanation: the basic idea is to make a dummy + # metaclass for one level of class instantiation that replaces itself with + # the actual metaclass. + class metaclass(meta): + __call__ = type.__call__ + __init__ = type.__init__ + + def __new__(cls, name, this_bases, d): + if this_bases is None: + return type.__new__(cls, name, (), d) + return meta(name, bases, d) + return metaclass('temporary_class', None, {}) + + +def to_unicode(obj, charset='utf-8', errors='strict'): + if obj is None: + return None + if not isinstance(obj, bytes): + return text_type(obj) + return obj.decode(charset, errors) + + +# shortcuts +pjoin = os.path.join +exists = os.path.exists