summaryrefslogtreecommitdiff
path: root/www/py-restclient/files/patch-2to3
blob: f338282ed5ca5520cc52300d89c806d112a30b87 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
--- restclient/__init__.py.orig	2012-11-17 20:17:26 UTC
+++ restclient/__init__.py
@@ -84,10 +84,10 @@ CHANGESET:
 
 import httplib2
 import mimetypes
-import thread
+import _thread
 import types
-import urllib
-import urllib2
+import urllib.request, urllib.parse, urllib.error
+import urllib.request, urllib.error, urllib.parse
 try:
     import json
 except ImportError:
@@ -168,7 +168,7 @@ def get_content_type(filename):
     return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
 
 
-def GET(url, params=None, files=None, accept=[], headers=None, async=False,
+def GET(url, params=None, files=None, accept=[], headers=None, asynchronous=False,
         resp=False, credentials=None, httplib_params=None):
     """ make an HTTP GET request.
 
@@ -178,20 +178,20 @@ def GET(url, params=None, files=None, accept=[], heade
     in addition, parameters and headers can be specified (as dicts). a
     list of mimetypes to accept may be specified.
 
-    if async=True is passed in, it will perform the request in a new
+    if asynchronous=True is passed in, it will perform the request in a new
     thread and immediately return nothing.
 
     if resp=True is passed in, it will return a tuple of an httplib2
     response object and the content instead of just the content.
     """
-    return rest_invoke(url=url, method=u"GET", params=params,
+    return rest_invoke(url=url, method="GET", params=params,
                        files=files, accept=accept, headers=headers,
-                       async=async, resp=resp, credentials=credentials,
+                       asynchronous=asynchronous, resp=resp, credentials=credentials,
                        httplib_params=httplib_params)
 
 
 def POST(url, params=None, files=None, accept=[], headers=None,
-         async=True, resp=False, credentials=None, httplib_params=None):
+         asynchronous=True, resp=False, credentials=None, httplib_params=None):
     """ make an HTTP POST request.
 
     performs a POST request to the specified URL.
@@ -209,19 +209,19 @@ def POST(url, params=None, files=None, accept=[], head
     (nothing) immediately.
 
     To wait for the response and have it return the body of the
-    response, specify async=False.
+    response, specify asynchronous=False.
 
     if resp=True is passed in, it will return a tuple of an httplib2
     response object and the content instead of just the content.
     """
-    return rest_invoke(url=url, method=u"POST", params=params,
+    return rest_invoke(url=url, method="POST", params=params,
                        files=files, accept=accept, headers=headers,
-                       async=async, resp=resp, credentials=credentials,
+                       asynchronous=asynchronous, resp=resp, credentials=credentials,
                        httplib_params=httplib_params)
 
 
 def PUT(url, params=None, files=None, accept=[], headers=None,
-        async=True, resp=False, credentials=None, httplib_params=None):
+        asynchronous=True, resp=False, credentials=None, httplib_params=None):
     """ make an HTTP PUT request.
 
     performs a PUT request to the specified URL.
@@ -239,20 +239,20 @@ def PUT(url, params=None, files=None, accept=[], heade
     (nothing) immediately.
 
     To wait for the response and have it return the body of the
-    response, specify async=False.
+    response, specify asynchronous=False.
 
     if resp=True is passed in, it will return a tuple of an httplib2
     response object and the content instead of just the content.
     """
 
-    return rest_invoke(url=url, method=u"PUT", params=params,
+    return rest_invoke(url=url, method="PUT", params=params,
                        files=files, accept=accept, headers=headers,
-                       async=async, resp=resp, credentials=credentials,
+                       asynchronous=asynchronous, resp=resp, credentials=credentials,
                        httplib_params=httplib_params)
 
 
 def DELETE(url, params=None, files=None, accept=[], headers=None,
-           async=True, resp=False, credentials=None,
+           asynchronous=True, resp=False, credentials=None,
            httplib_params=None):
     """ make an HTTP DELETE request.
 
@@ -265,20 +265,20 @@ def DELETE(url, params=None, files=None, accept=[], he
     returns (nothing) immediately.
 
     To wait for the response and have it return the body of the
-    response, specify async=False.
+    response, specify asynchronous=False.
 
     if resp=True is passed in, it will return a tuple of an httplib2
     response object and the content instead of just the content.
     """
 
-    return rest_invoke(url=url, method=u"DELETE", params=params,
+    return rest_invoke(url=url, method="DELETE", params=params,
                        files=files, accept=accept, headers=headers,
-                       async=async, resp=resp, credentials=credentials,
+                       asynchronous=asynchronous, resp=resp, credentials=credentials,
                        httplib_params=httplib_params)
 
 
-def rest_invoke(url, method=u"GET", params=None, files=None,
-                accept=[], headers=None, async=False, resp=False,
+def rest_invoke(url, method="GET", params=None, files=None,
+                accept=[], headers=None, asynchronous=False, resp=False,
                 httpcallback=None, credentials=None,
                 httplib_params=None):
     """ make an HTTP request with all the trimmings.
@@ -315,7 +315,7 @@ def rest_invoke(url, method=u"GET", params=None, files
     accept: list of mimetypes to accept in order of
             preference. defaults to '*/*'
     headers: dictionary of additional headers to send to the server
-    async: Boolean. if true, does request in new thread and nothing is
+    asynchronous: Boolean. if true, does request in new thread and nothing is
            returned
     resp: Boolean. if true, returns a tuple of response,
           content. otherwise returns just content
@@ -325,8 +325,8 @@ def rest_invoke(url, method=u"GET", params=None, files
     httplib_params: dict of parameters supplied to httplib2 - for
                     example ca_certs='/etc/ssl/certs/ca-certificates.crt'
     """
-    if async:
-        thread.start_new_thread(_rest_invoke,
+    if asynchronous:
+        _thread.start_new_thread(_rest_invoke,
                                 (url, method, params, files, accept,
                                  headers, resp, httpcallback, credentials,
                                  httplib_params))
@@ -335,7 +335,7 @@ def rest_invoke(url, method=u"GET", params=None, files
                             resp, httpcallback, credentials, httplib_params)
 
 
-def _rest_invoke(url, method=u"GET", params=None, files=None, accept=None,
+def _rest_invoke(url, method="GET", params=None, files=None, accept=None,
                  headers=None, resp=False, httpcallback=None,
                  credentials=None, httplib_params=None):
     if params is None:
@@ -363,7 +363,7 @@ def _rest_invoke(url, method=u"GET", params=None, file
             headers[k] = v
 
         if httpcallback.username or httpcallback.password:
-            print "warning: restclient can't handle HTTP auth yet"
+            print("warning: restclient can't handle HTTP auth yet")
         if httpcallback.redirections != 5:
             print ("warning: restclient doesn't support "
                    "HTTPCallback's restrictions yet")
@@ -371,18 +371,18 @@ def _rest_invoke(url, method=u"GET", params=None, file
             print ("warning: restclient doesn't support "
                    "HTTPCallback's follow_all_redirects_yet")
         if httpcallback.body != "":
-            print "warning: restclient doesn't support HTTPCallback's body yet"
+            print("warning: restclient doesn't support HTTPCallback's body yet")
 
     headers = add_accepts(accept, headers)
     if method in ['POST', 'PUT'] and 'Content-Type' not in headers:
         headers['Content-Type'] = 'application/x-www-form-urlencoded'
-        params = urllib.urlencode(fix_params(params))
+        params = urllib.parse.urlencode(fix_params(params))
     elif (method in ['POST', 'PUT'] and
           headers['Content-Type'] == 'application/json'):
         params = json.dumps(params)
     else:
         # GET and DELETE requests
-        params = urllib.urlencode(fix_params(params))
+        params = urllib.parse.urlencode(fix_params(params))
 
     if files:
         return post_multipart(extract_host(url), extract_path(url),
@@ -463,11 +463,11 @@ def my_urlparse(url):
 
 
 def unpack_params(params):
-    return [(k, params[k]) for k in params.keys()]
+    return [(k, params[k]) for k in list(params.keys())]
 
 
 def unpack_files(files):
-    return [(k, files[k]['filename'], files[k]['file']) for k in files.keys()]
+    return [(k, files[k]['filename'], files[k]['file']) for k in list(files.keys())]
 
 
 def add_accepts(accept=None, headers=None):
@@ -485,8 +485,8 @@ def add_accepts(accept=None, headers=None):
 def fix_params(params=None):
     if params is None:
         params = {}
-    for k in params.keys():
-        if type(k) not in types.StringTypes:
+    for k in list(params.keys()):
+        if type(k) not in (str,):
             new_k = str(k)
             params[new_k] = params[k]
             del params[k]
@@ -500,8 +500,8 @@ def fix_params(params=None):
             except UnicodeDecodeError:
                 pass
 
-    for k in params.keys():
-        if type(params[k]) not in types.StringTypes:
+    for k in list(params.keys()):
+        if type(params[k]) not in (str,):
             params[k] = str(params[k])
         try:
             params[k].encode('ascii')
@@ -517,12 +517,12 @@ def fix_params(params=None):
 def fix_headers(headers=None):
     if headers is None:
         headers = {}
-    for k in headers.keys():
-        if type(k) not in types.StringTypes:
+    for k in list(headers.keys()):
+        if type(k) not in (str,):
             new_k = str(k)
             headers[new_k] = headers[k]
             del headers[k]
-        if type(headers[k]) not in types.StringTypes:
+        if type(headers[k]) not in (str,):
             headers[k] = str(headers[k])
         try:
             headers[k].encode('ascii')
@@ -539,8 +539,8 @@ def fix_files(files=None):
     if files is None:
         files = {}
     # fix keys in files
-    for k in files.keys():
-        if type(k) not in types.StringTypes:
+    for k in list(files.keys()):
+        if type(k) not in (str,):
             new_k = str(k)
             files[new_k] = files[k]
             del files[k]
@@ -551,7 +551,7 @@ def fix_files(files=None):
             files[new_k] = files[k]
             del files[k]
     # second pass to fix filenames
-    for k in files.keys():
+    for k in list(files.keys()):
         try:
             files[k]['filename'].encode('ascii')
         except UnicodeEncodeError:
@@ -560,15 +560,15 @@ def fix_files(files=None):
 
 
 if __name__ == "__main__":
-    print rest_invoke("http://localhost:9090/",
+    print(rest_invoke("http://localhost:9090/",
                       method="POST", params={'value': 'store this'},
-                      accept=["text/plain", "text/html"], async=False)
+                      accept=["text/plain", "text/html"], asynchronous=False))
     image = open('sample.jpg').read()
     r = rest_invoke("http://resizer.ccnmtl.columbia.edu/resize",
                     method="POST",
                     files={'image': {'file': image,
                                      'filename': 'sample.jpg'}},
-                    async=False)
+                    asynchronous=False)
     out = open("thumb.jpg", "w")
     out.write(r)
     out.close()
@@ -576,13 +576,13 @@ if __name__ == "__main__":
     r = POST("http://resizer.ccnmtl.columbia.edu/resize",
              files={'image': {'file': image,
                               'filename': 'sample.jpg'}},
-             async=False)
+             asynchronous=False)
     # evil unicode tests
-    print rest_invoke(u"http://localhost:9090/foo/",
-                      params={u'foo\u2012': u'\u2012'},
-                      headers={u"foo\u2012": u"foo\u2012"})
+    print(rest_invoke("http://localhost:9090/foo/",
+                      params={'foo\u2012': '\u2012'},
+                      headers={"foo\u2012": "foo\u2012"}))
 
-    r = rest_invoke(u"http://localhost:9090/resize", method="POST",
-                    files={u'image\u2012': {'file': image,
-                                            'filename': u'samp\u2012le.jpg'}},
-                    async=False)
+    r = rest_invoke("http://localhost:9090/resize", method="POST",
+                    files={'image\u2012': {'file': image,
+                                            'filename': 'samp\u2012le.jpg'}},
+                    asynchronous=False)
--- restclient/test/test_everything.py.orig	2012-11-17 20:17:26 UTC
+++ restclient/test/test_everything.py
@@ -39,7 +39,7 @@ on each request it handles, please submit a patch. 
 
 from restclient import *
 import threading, os
-import BaseHTTPServer
+import http.server
 import cgi
 
 port_num = int(os.environ.get('RESTCLIENT_TEST_PORT',11123))
@@ -47,12 +47,12 @@ hostname = "http://localhost:%d/" % port_num
 image = open('sample.jpg').read()
 
 def start_server(callback):
-    class LoopbackHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+    class LoopbackHandler(http.server.BaseHTTPRequestHandler):
         """ a simple http server that will basically echo back the request
         that was made to it """
         def respond(self):
-            s = self.requestline + u"\n" \
-                + str(self.headers) + u"\n\n" \
+            s = self.requestline + "\n" \
+                + str(self.headers) + "\n\n" \
                 + self.body()
 
             response = s.encode('utf-8')
@@ -82,7 +82,7 @@ def start_server(callback):
 
     def run():
         """ start the server for a single request """
-        server_class=BaseHTTPServer.HTTPServer
+        server_class=http.server.HTTPServer
         handler_class=LoopbackHandler
         server_address = ('', port_num)
         httpd = server_class(server_address, handler_class)
@@ -116,7 +116,7 @@ def test_get():
 @servify
 def test_post():
     expected = "POST\nvalue: store this\nDONE\n"
-    r = POST(hostname, params={'value' : 'store this'}, accept=["text/plain","text/html"], async=False)
+    r = POST(hostname, params={'value' : 'store this'}, accept=["text/plain","text/html"], asynchronous=False)
     assert r.startswith('POST /')
     assert "value=store+this" in r
     assert "accept: text/plain,text/html" in r
@@ -124,25 +124,25 @@ def test_post():
 @servify
 def test_post_image():
     result = POST(hostname + "resize", files={'image' : {'file' : image, 'filename' : 'sample.jpg'}},
-                  async=False)
+                  asynchronous=False)
     assert result.startswith('POST /resize')
     assert "multipart" in result
 
 @servify
 def test_get_unicode():
-    expected = u"GET\nfoo\u2012: \u2012\nDONE\n".encode('utf-8')
-    r = GET(unicode(hostname + "foo/"),params={u'foo\u2012' : u'\u2012'},
-            headers={u"foo\u2012" : u"foo\u2012"})
+    expected = "GET\nfoo\u2012: \u2012\nDONE\n".encode('utf-8')
+    r = GET(str(hostname + "foo/"),params={'foo\u2012' : '\u2012'},
+            headers={"foo\u2012" : "foo\u2012"})
     # unicode in params gets urlencoded
     assert r.startswith('GET /foo/?foo%E2%80%92=%E2%80%92')
     # unicode in headers gets stripped out. they can only contain ascii.
-    assert u"foo: foo" in r
+    assert "foo: foo" in r
 
 @servify
 def test_post_unicode():
-    result = POST(unicode(hostname + "foo/"), 
-                  params={u'foo\u2012' : u'\u2012'},
-                  async=False)
+    result = POST(str(hostname + "foo/"), 
+                  params={'foo\u2012' : '\u2012'},
+                  asynchronous=False)
     assert result.startswith('POST /foo/')
     expected = "foo%E2%80%92=%E2%80%92" # urlencoded 
     assert expected in result