forked from dpgaspar/Flask-AppBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_compat.py
64 lines (51 loc) · 1.92 KB
/
_compat.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
# -*- coding: utf-8 -*-
"""
Some py2/py3 compatibility support based on a stripped down
version of six so we don't have to depend on a specific version
of it.
:copyright: (c) 2013 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import sys
PY2 = sys.version_info[0] == 2
VER = sys.version_info
if not PY2:
text_type = str
string_types = (str,)
integer_types = (int,)
iterkeys = lambda d: iter(d.keys()) # noqa
itervalues = lambda d: iter(d.values()) # noqa
iteritems = lambda d: iter(d.items()) # noqa
def as_unicode(s):
if isinstance(s, bytes):
return s.decode("utf-8")
return str(s)
else:
text_type = unicode # noqa
string_types = (str, unicode) # noqa
integer_types = (int, long) # noqa
iterkeys = lambda d: d.iterkeys() # noqa
itervalues = lambda d: d.itervalues() # noqa
iteritems = lambda d: d.iteritems() # noqa
def as_unicode(s):
if isinstance(s, str):
return s.decode("utf-8")
return unicode(s) # noqa
def with_metaclass(meta, *bases):
# 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. Because of internal type checks
# we also need to make sure that we downgrade the custom metaclass
# for one level to something closer to type (that's why __call__ and
# __init__ comes back from type etc.).
#
# This has the advantage over six.with_metaclass in that it does not
# introduce dummy classes into the final MRO.
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, {})