Tres Seaver
2012-03-19 9c1e6f85bee360d4e53bff46f6df5be5fdc254b7
commit | author | age
9df42f 1 try:
TS 2     STRING_TYPES = (str, unicode)
a37aff 3 except NameError: #pragma NO COVER Python >= 3.0
9df42f 4     STRING_TYPES = (str,)
TS 5
fdc787 6 try:
TS 7     u = unicode
8 except NameError: #pragma NO COVER Python >= 3.0
9     u = str
00c816 10     b = bytes
bf5b5a 11 else: #pragma NO COVER Python < 3.0
TS 12     b = str
fdc787 13
a37aff 14 import base64
TS 15 if 'decodebytes' in base64.__dict__: #pragma NO COVER Python >= 3.0
16     decodebytes = base64.decodebytes
17     encodebytes = base64.encodebytes
513d65 18     def decodestring(value):
TS 19         return base64.decodestring(bytes(value, 'ascii')).decode('ascii')
20     def encodestring(value):
21         return base64.encodestring(bytes(value, 'ascii')).decode('ascii')
bf5b5a 22 else: #pragma NO COVER Python < 3.0
a37aff 23     decodebytes = base64.decodestring
TS 24     encodebytes = base64.encodestring
513d65 25     decodestring = base64.decodestring
TS 26     encodestring = base64.encodestring
a90306 27
adef05 28 try:
9c1e6f 29     from urllib.parse import parse_qs
TS 30 except ImportError: #pragma NO COVER Python < 3.0
31     from cgi import parse_qs
32     from cgi import parse_qsl
33 else: #pragma NO COVER Python >= 3.0
34     from urllib.parse import parse_qsl
35
36 try:
adef05 37     from ConfigParser import ConfigParser
TS 38 except ImportError: #pragma NO COVER Python >= 3.0
39     from configparser import ConfigParser
40     from configparser import ParsingError
bf5b5a 41 else: #pragma NO COVER Python < 3.0
a37aff 42     from ConfigParser import ParsingError
adef05 43
TS 44 try:
45     from Cookie import SimpleCookie
46 except ImportError: #pragma NO COVER Python >= 3.0
47     from http.cookies import SimpleCookie
48     from http.cookies import CookieError
bf5b5a 49 else: #pragma NO COVER Python < 3.0
a37aff 50     from Cookie import CookieError
adef05 51
TS 52 try:
53     from itertools import izip_longest
54 except ImportError: #pragma NO COVER Python >= 3.0
55     from itertools import zip_longest as izip_longest
56
57 try:
58     from StringIO import StringIO
59 except ImportError: #pragma NO COVER Python >= 3.0
60     from io import StringIO
61
62 try:
53987b 63     from urllib import urlencode
adef05 64 except ImportError: #pragma NO COVER Python >= 3.0
53987b 65     from urllib.parse import urlencode
adef05 66     from urllib.parse import quote as url_quote
TS 67     from urllib.parse import unquote as url_unquote
bf5b5a 68 else: #pragma NO COVER Python < 3.0
a37aff 69     from urllib import quote as url_quote
TS 70     from urllib import unquote as url_unquote
adef05 71
53987b 72 try:
TS 73     from urlparse import urlparse
74 except ImportError: #pragma NO COVER Python >= 3.0
75     from urllib.parse import urlparse
76     from urllib.parse import urlunparse
bf5b5a 77 else: #pragma NO COVER Python < 3.0
a37aff 78     from urlparse import urlunparse
53987b 79
adef05 80 import wsgiref.util
TS 81 import wsgiref.headers
82
02a504 83 def REQUEST_METHOD(environ):
AO 84     return environ['REQUEST_METHOD']
85
86 def CONTENT_TYPE(environ):
87     return environ['CONTENT_TYPE']
88
89 def USER_AGENT(environ):
90     return environ.get('HTTP_USER_AGENT')
91
92 def AUTHORIZATION(environ):
93     return environ.get('HTTP_AUTHORIZATION', '')
94
95 def get_cookies(environ):
96     header = environ.get('HTTP_COOKIE', '')
a37aff 97     if 'paste.cookies' in environ:
02a504 98         cookies, check_header = environ['paste.cookies']
AO 99         if check_header == header:
100             return cookies
101     cookies = SimpleCookie()
102     try:
103         cookies.load(header)
a37aff 104     except CookieError: #pragma NO COVER (can't see how to provoke this)
02a504 105         pass
AO 106     environ['paste.cookies'] = (cookies, header)
107     return cookies
108
109 def construct_url(environ):
110     return wsgiref.util.request_uri(environ)
111
112 def header_value(environ, key):
113     headers = wsgiref.headers.Headers(environ)
114     values = headers.get(key)
115     if not values:
116         return ""
bf5b5a 117     if isinstance(values, list): #pragma NO COVER can't be true under Py3k.
02a504 118         return ",".join(values)
AO 119     else:
120         return values
cc04ed 121
TS 122 def must_decode(value):
123     if type(value) is b:
124         try:
125             return value.decode('utf-8')
126         except UnicodeDecodeError:
127             return value.decode('latin1')
128     return value