Add python-eventlet 0.16.1
[packages/trusty/python-eventlet.git] / eventlet / eventlet / green / urllib.py
1 from eventlet import patcher
2 from eventlet.green import socket
3 from eventlet.green import time
4 from eventlet.green import httplib
5 from eventlet.green import ftplib
6
7 to_patch = [('socket', socket), ('httplib', httplib),
8             ('time', time), ('ftplib', ftplib)]
9 try:
10     from eventlet.green import ssl
11     to_patch.append(('ssl', ssl))
12 except ImportError:
13     pass
14
15 patcher.inject('urllib', globals(), *to_patch)
16 try:
17     URLopener
18 except NameError:
19     patcher.inject('urllib.request', globals(), *to_patch)
20
21
22 # patch a bunch of things that have imports inside the
23 # function body; this is lame and hacky but I don't feel
24 # too bad because urllib is a hacky pile of junk that no
25 # one should be using anyhow
26 URLopener.open_http = patcher.patch_function(URLopener.open_http, ('httplib', httplib))
27 if hasattr(URLopener, 'open_https'):
28     URLopener.open_https = patcher.patch_function(URLopener.open_https, ('httplib', httplib))
29
30 URLopener.open_ftp = patcher.patch_function(URLopener.open_ftp, ('ftplib', ftplib))
31 ftpwrapper.init = patcher.patch_function(ftpwrapper.init, ('ftplib', ftplib))
32 ftpwrapper.retrfile = patcher.patch_function(ftpwrapper.retrfile, ('ftplib', ftplib))
33
34 del patcher
35
36 # Run test program when run as a script
37 if __name__ == '__main__':
38     main()