1 | import sys |
---|
2 | import hashlib |
---|
3 | import os |
---|
4 | |
---|
5 | PY2 = sys.version_info[0] == 2 |
---|
6 | |
---|
7 | _identity = lambda x: x |
---|
8 | |
---|
9 | if PY2: |
---|
10 | import cPickle as pickle |
---|
11 | from cStringIO import StringIO |
---|
12 | import copy_reg as copyreg |
---|
13 | from urllib import unquote |
---|
14 | from HTMLParser import HTMLParser |
---|
15 | import urlparse |
---|
16 | from htmlentitydefs import entitydefs, name2codepoint |
---|
17 | import __builtin__ as builtin |
---|
18 | import thread |
---|
19 | import Cookie |
---|
20 | import urllib2 |
---|
21 | import Queue |
---|
22 | import ConfigParser as configparser |
---|
23 | from .contrib import ipaddress |
---|
24 | from email.MIMEBase import MIMEBase |
---|
25 | from email.Header import Header |
---|
26 | from email import Encoders, Charset |
---|
27 | from email.MIMEMultipart import MIMEMultipart |
---|
28 | from email.MIMEText import MIMEText |
---|
29 | from email.Charset import add_charset, QP as charset_QP |
---|
30 | from urllib import FancyURLopener, urlencode |
---|
31 | from urllib import ( |
---|
32 | quote as urllib_quote, |
---|
33 | unquote as urllib_unquote, |
---|
34 | quote_plus as urllib_quote_plus, |
---|
35 | ) |
---|
36 | from urllib2 import urlopen |
---|
37 | from string import maketrans |
---|
38 | from types import ClassType |
---|
39 | import cookielib |
---|
40 | from xmlrpclib import ProtocolError |
---|
41 | |
---|
42 | BytesIO = StringIO |
---|
43 | reduce = reduce |
---|
44 | reload = reload |
---|
45 | hashlib_md5 = hashlib.md5 |
---|
46 | iterkeys = lambda d: d.iterkeys() |
---|
47 | itervalues = lambda d: d.itervalues() |
---|
48 | iteritems = lambda d: d.iteritems() |
---|
49 | integer_types = (int, long) |
---|
50 | string_types = (str, unicode) |
---|
51 | text_type = unicode |
---|
52 | basestring = basestring |
---|
53 | xrange = xrange |
---|
54 | long = long |
---|
55 | unichr = unichr |
---|
56 | unicodeT = unicode |
---|
57 | |
---|
58 | def implements_bool(cls): |
---|
59 | cls.__nonzero__ = cls.__bool__ |
---|
60 | del cls.__bool__ |
---|
61 | return cls |
---|
62 | |
---|
63 | def implements_iterator(cls): |
---|
64 | cls.next = cls.__next__ |
---|
65 | del cls.__next__ |
---|
66 | return cls |
---|
67 | |
---|
68 | def to_bytes(obj, charset="utf-8", errors="strict"): |
---|
69 | if obj is None: |
---|
70 | return None |
---|
71 | if isinstance(obj, (bytes, bytearray, buffer)): |
---|
72 | return bytes(obj) |
---|
73 | if isinstance(obj, unicode): |
---|
74 | return obj.encode(charset, errors) |
---|
75 | raise TypeError("Expected bytes") |
---|
76 | |
---|
77 | def to_native(obj, charset="utf8", errors="strict"): |
---|
78 | if obj is None or isinstance(obj, str): |
---|
79 | return obj |
---|
80 | return obj.encode(charset, errors) |
---|
81 | |
---|
82 | |
---|
83 | else: |
---|
84 | import pickle |
---|
85 | from io import StringIO, BytesIO |
---|
86 | import copyreg |
---|
87 | from importlib import reload |
---|
88 | from functools import reduce |
---|
89 | from urllib.parse import unquote |
---|
90 | from html.parser import HTMLParser |
---|
91 | from http import cookies as Cookie |
---|
92 | from urllib import parse as urlparse |
---|
93 | from urllib import request as urllib2 |
---|
94 | from html.entities import entitydefs, name2codepoint |
---|
95 | import builtins as builtin |
---|
96 | import _thread as thread |
---|
97 | import configparser |
---|
98 | import queue as Queue |
---|
99 | import ipaddress |
---|
100 | from email.mime.base import MIMEBase |
---|
101 | from email.mime.multipart import MIMEMultipart |
---|
102 | from email.mime.text import MIMEText |
---|
103 | from email import encoders as Encoders |
---|
104 | from email.header import Header |
---|
105 | from email.charset import Charset, add_charset, QP as charset_QP |
---|
106 | from urllib.request import FancyURLopener, urlopen |
---|
107 | from urllib.parse import ( |
---|
108 | quote as urllib_quote, |
---|
109 | unquote as urllib_unquote, |
---|
110 | urlencode, |
---|
111 | quote_plus as urllib_quote_plus, |
---|
112 | ) |
---|
113 | from http import cookiejar as cookielib |
---|
114 | from xmlrpc.client import ProtocolError |
---|
115 | import html # warning, this is the python3 module and not the web2py html module |
---|
116 | |
---|
117 | hashlib_md5 = lambda s: hashlib.md5(bytes(s, "utf8")) |
---|
118 | iterkeys = lambda d: iter(d.keys()) |
---|
119 | itervalues = lambda d: iter(d.values()) |
---|
120 | iteritems = lambda d: iter(d.items()) |
---|
121 | integer_types = (int,) |
---|
122 | string_types = (str,) |
---|
123 | text_type = str |
---|
124 | basestring = str |
---|
125 | xrange = range |
---|
126 | long = int |
---|
127 | unichr = chr |
---|
128 | unicodeT = str |
---|
129 | maketrans = str.maketrans |
---|
130 | ClassType = type |
---|
131 | |
---|
132 | implements_iterator = _identity |
---|
133 | implements_bool = _identity |
---|
134 | |
---|
135 | def to_bytes(obj, charset="utf-8", errors="strict"): |
---|
136 | if obj is None: |
---|
137 | return None |
---|
138 | if isinstance(obj, (bytes, bytearray, memoryview)): |
---|
139 | return bytes(obj) |
---|
140 | if isinstance(obj, str): |
---|
141 | return obj.encode(charset, errors) |
---|
142 | raise TypeError("Expected bytes") |
---|
143 | |
---|
144 | def to_native(obj, charset="utf8", errors="strict"): |
---|
145 | if obj is None or isinstance(obj, str): |
---|
146 | return obj |
---|
147 | return obj.decode(charset, errors) |
---|
148 | |
---|
149 | |
---|
150 | def with_metaclass(meta, *bases): |
---|
151 | """Create a base class with a metaclass.""" |
---|
152 | # This requires a bit of explanation: the basic idea is to make a dummy |
---|
153 | # metaclass for one level of class instantiation that replaces itself with |
---|
154 | # the actual metaclass. |
---|
155 | class metaclass(meta): |
---|
156 | __call__ = type.__call__ |
---|
157 | __init__ = type.__init__ |
---|
158 | |
---|
159 | def __new__(cls, name, this_bases, d): |
---|
160 | if this_bases is None: |
---|
161 | return type.__new__(cls, name, (), d) |
---|
162 | return meta(name, bases, d) |
---|
163 | |
---|
164 | return metaclass("temporary_class", None, {}) |
---|
165 | |
---|
166 | |
---|
167 | def to_unicode(obj, charset="utf-8", errors="strict"): |
---|
168 | if obj is None: |
---|
169 | return None |
---|
170 | if not hasattr(obj, "decode") or not callable(obj.decode): |
---|
171 | return text_type(obj) |
---|
172 | return obj.decode(charset, errors) |
---|
173 | |
---|
174 | |
---|
175 | # shortcuts |
---|
176 | pjoin = os.path.join |
---|
177 | exists = os.path.exists |
---|