Mercurial > hg
annotate mercurial/urllibcompat.py @ 37870:39c17718f311
tests: make test-check-interfaces.py work on Python 3
# skip-blame just a bunch of bytes prefixes
Differential Revision: https://phab.mercurial-scm.org/D3465
author | Augie Fackler <augie@google.com> |
---|---|
date | Thu, 26 Apr 2018 21:39:35 -0400 |
parents | 5bc7ff103081 |
children | 5774fc623a18 |
rev | line source |
---|---|
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
1 # urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
2 # |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
3 # Copyright 2017 Google, Inc. |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
4 # |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
7 from __future__ import absolute_import |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
8 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
9 from . import pycompat |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
10 |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
11 _sysstr = pycompat.sysstr |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
12 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
13 class _pycompatstub(object): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
14 def __init__(self): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
15 self._aliases = {} |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
16 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
17 def _registeraliases(self, origin, items): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
18 """Add items that will be populated at the first access""" |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
19 items = map(_sysstr, items) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
20 self._aliases.update( |
36835
5bc7ff103081
py3: use r'' instead of sysstr('') to get around code transformer
Yuya Nishihara <yuya@tcha.org>
parents:
36809
diff
changeset
|
21 (item.replace(r'_', r'').lower(), (origin, item)) |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
22 for item in items) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
23 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
24 def _registeralias(self, origin, attr, name): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
25 """Alias ``origin``.``attr`` as ``name``""" |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
26 self._aliases[_sysstr(name)] = (origin, _sysstr(attr)) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
27 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
28 def __getattr__(self, name): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
29 try: |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
30 origin, item = self._aliases[name] |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
31 except KeyError: |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
32 raise AttributeError(name) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
33 self.__dict__[name] = obj = getattr(origin, item) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
34 return obj |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
35 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
36 httpserver = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
37 urlreq = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
38 urlerr = _pycompatstub() |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
39 |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
40 if pycompat.ispy3: |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
41 import urllib.parse |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
42 urlreq._registeraliases(urllib.parse, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
43 "splitattr", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
44 "splitpasswd", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
45 "splitport", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
46 "splituser", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
47 "urlparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
48 "urlunparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
49 )) |
36077
a3d42d1865f1
wireprotoserver: define and use parse_qs from urllib
Gregory Szorc <gregory.szorc@gmail.com>
parents:
34467
diff
changeset
|
50 urlreq._registeralias(urllib.parse, "parse_qs", "parseqs") |
36809
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36077
diff
changeset
|
51 urlreq._registeralias(urllib.parse, "parse_qsl", "parseqsl") |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
52 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote") |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
53 import urllib.request |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
54 urlreq._registeraliases(urllib.request, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
55 "AbstractHTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
56 "BaseHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
57 "build_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
58 "FileHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
59 "FTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
60 "ftpwrapper", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
61 "HTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
62 "HTTPSHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
63 "install_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
64 "pathname2url", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
65 "HTTPBasicAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
66 "HTTPDigestAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
67 "HTTPPasswordMgrWithDefaultRealm", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
68 "ProxyHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
69 "Request", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
70 "url2pathname", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
71 "urlopen", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
72 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
73 import urllib.response |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
74 urlreq._registeraliases(urllib.response, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
75 "addclosehook", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
76 "addinfourl", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
77 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
78 import urllib.error |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
79 urlerr._registeraliases(urllib.error, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
80 "HTTPError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
81 "URLError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
82 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
83 import http.server |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
84 httpserver._registeraliases(http.server, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
85 "HTTPServer", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
86 "BaseHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
87 "SimpleHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
88 "CGIHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
89 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
90 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
91 # urllib.parse.quote() accepts both str and bytes, decodes bytes |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
92 # (if necessary), and returns str. This is wonky. We provide a custom |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
93 # implementation that only accepts bytes and emits bytes. |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
94 def quote(s, safe=r'/'): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
95 s = urllib.parse.quote_from_bytes(s, safe=safe) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
96 return s.encode('ascii', 'strict') |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
97 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
98 # urllib.parse.urlencode() returns str. We use this function to make |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
99 # sure we return bytes. |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
100 def urlencode(query, doseq=False): |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
101 s = urllib.parse.urlencode(query, doseq=doseq) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
102 return s.encode('ascii') |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
103 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
104 urlreq.quote = quote |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
105 urlreq.urlencode = urlencode |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
106 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
107 def getfullurl(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
108 return req.full_url |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
109 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
110 def gethost(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
111 return req.host |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
112 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
113 def getselector(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
114 return req.selector |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
115 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
116 def getdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
117 return req.data |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
118 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
119 def hasdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
120 return req.data is not None |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
121 else: |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
122 import BaseHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
123 import CGIHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
124 import SimpleHTTPServer |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
125 import urllib2 |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
126 import urllib |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
127 import urlparse |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
128 urlreq._registeraliases(urllib, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
129 "addclosehook", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
130 "addinfourl", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
131 "ftpwrapper", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
132 "pathname2url", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
133 "quote", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
134 "splitattr", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
135 "splitpasswd", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
136 "splitport", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
137 "splituser", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
138 "unquote", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
139 "url2pathname", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
140 "urlencode", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
141 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
142 urlreq._registeraliases(urllib2, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
143 "AbstractHTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
144 "BaseHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
145 "build_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
146 "FileHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
147 "FTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
148 "HTTPBasicAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
149 "HTTPDigestAuthHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
150 "HTTPHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
151 "HTTPPasswordMgrWithDefaultRealm", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
152 "HTTPSHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
153 "install_opener", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
154 "ProxyHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
155 "Request", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
156 "urlopen", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
157 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
158 urlreq._registeraliases(urlparse, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
159 "urlparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
160 "urlunparse", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
161 )) |
36077
a3d42d1865f1
wireprotoserver: define and use parse_qs from urllib
Gregory Szorc <gregory.szorc@gmail.com>
parents:
34467
diff
changeset
|
162 urlreq._registeralias(urlparse, "parse_qs", "parseqs") |
36809
3c15b84ab66c
hgweb: teach WSGI parser about query strings
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36077
diff
changeset
|
163 urlreq._registeralias(urlparse, "parse_qsl", "parseqsl") |
34467
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
164 urlerr._registeraliases(urllib2, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
165 "HTTPError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
166 "URLError", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
167 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
168 httpserver._registeraliases(BaseHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
169 "HTTPServer", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
170 "BaseHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
171 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
172 httpserver._registeraliases(SimpleHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
173 "SimpleHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
174 )) |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
175 httpserver._registeraliases(CGIHTTPServer, ( |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
176 "CGIHTTPRequestHandler", |
192f7b126ed2
urllibcompat: move some adapters from pycompat to urllibcompat
Augie Fackler <augie@google.com>
parents:
34465
diff
changeset
|
177 )) |
34465
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
178 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
179 def gethost(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
180 return req.get_host() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
181 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
182 def getselector(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
183 return req.get_selector() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
184 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
185 def getfullurl(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
186 return req.get_full_url() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
187 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
188 def getdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
189 return req.get_data() |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
190 |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
191 def hasdata(req): |
80d4681150b9
urllibcompat: new library to help abstract out some python3 urllib2 stuff
Augie Fackler <augie@google.com>
parents:
diff
changeset
|
192 return req.has_data() |