##// END OF EJS Templates
urllibcompat: move some adapters from pycompat to urllibcompat...
Augie Fackler -
r34468:192f7b12 default
parent child Browse files
Show More
@@ -314,150 +314,3 b' else:'
314 314 stringio = cStringIO.StringIO
315 315 maplist = map
316 316 rawinput = raw_input
317
318 class _pycompatstub(object):
319 def __init__(self):
320 self._aliases = {}
321
322 def _registeraliases(self, origin, items):
323 """Add items that will be populated at the first access"""
324 items = map(sysstr, items)
325 self._aliases.update(
326 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
327 for item in items)
328
329 def _registeralias(self, origin, attr, name):
330 """Alias ``origin``.``attr`` as ``name``"""
331 self._aliases[sysstr(name)] = (origin, sysstr(attr))
332
333 def __getattr__(self, name):
334 try:
335 origin, item = self._aliases[name]
336 except KeyError:
337 raise AttributeError(name)
338 self.__dict__[name] = obj = getattr(origin, item)
339 return obj
340
341 httpserver = _pycompatstub()
342 urlreq = _pycompatstub()
343 urlerr = _pycompatstub()
344 if not ispy3:
345 import BaseHTTPServer
346 import CGIHTTPServer
347 import SimpleHTTPServer
348 import urllib2
349 import urllib
350 import urlparse
351 urlreq._registeraliases(urllib, (
352 "addclosehook",
353 "addinfourl",
354 "ftpwrapper",
355 "pathname2url",
356 "quote",
357 "splitattr",
358 "splitpasswd",
359 "splitport",
360 "splituser",
361 "unquote",
362 "url2pathname",
363 "urlencode",
364 ))
365 urlreq._registeraliases(urllib2, (
366 "AbstractHTTPHandler",
367 "BaseHandler",
368 "build_opener",
369 "FileHandler",
370 "FTPHandler",
371 "HTTPBasicAuthHandler",
372 "HTTPDigestAuthHandler",
373 "HTTPHandler",
374 "HTTPPasswordMgrWithDefaultRealm",
375 "HTTPSHandler",
376 "install_opener",
377 "ProxyHandler",
378 "Request",
379 "urlopen",
380 ))
381 urlreq._registeraliases(urlparse, (
382 "urlparse",
383 "urlunparse",
384 ))
385 urlerr._registeraliases(urllib2, (
386 "HTTPError",
387 "URLError",
388 ))
389 httpserver._registeraliases(BaseHTTPServer, (
390 "HTTPServer",
391 "BaseHTTPRequestHandler",
392 ))
393 httpserver._registeraliases(SimpleHTTPServer, (
394 "SimpleHTTPRequestHandler",
395 ))
396 httpserver._registeraliases(CGIHTTPServer, (
397 "CGIHTTPRequestHandler",
398 ))
399
400 else:
401 import urllib.parse
402 urlreq._registeraliases(urllib.parse, (
403 "splitattr",
404 "splitpasswd",
405 "splitport",
406 "splituser",
407 "urlparse",
408 "urlunparse",
409 ))
410 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote")
411 import urllib.request
412 urlreq._registeraliases(urllib.request, (
413 "AbstractHTTPHandler",
414 "BaseHandler",
415 "build_opener",
416 "FileHandler",
417 "FTPHandler",
418 "ftpwrapper",
419 "HTTPHandler",
420 "HTTPSHandler",
421 "install_opener",
422 "pathname2url",
423 "HTTPBasicAuthHandler",
424 "HTTPDigestAuthHandler",
425 "HTTPPasswordMgrWithDefaultRealm",
426 "ProxyHandler",
427 "Request",
428 "url2pathname",
429 "urlopen",
430 ))
431 import urllib.response
432 urlreq._registeraliases(urllib.response, (
433 "addclosehook",
434 "addinfourl",
435 ))
436 import urllib.error
437 urlerr._registeraliases(urllib.error, (
438 "HTTPError",
439 "URLError",
440 ))
441 import http.server
442 httpserver._registeraliases(http.server, (
443 "HTTPServer",
444 "BaseHTTPRequestHandler",
445 "SimpleHTTPRequestHandler",
446 "CGIHTTPRequestHandler",
447 ))
448
449 # urllib.parse.quote() accepts both str and bytes, decodes bytes
450 # (if necessary), and returns str. This is wonky. We provide a custom
451 # implementation that only accepts bytes and emits bytes.
452 def quote(s, safe=r'/'):
453 s = urllib.parse.quote_from_bytes(s, safe=safe)
454 return s.encode('ascii', 'strict')
455
456 # urllib.parse.urlencode() returns str. We use this function to make
457 # sure we return bytes.
458 def urlencode(query, doseq=False):
459 s = urllib.parse.urlencode(query, doseq=doseq)
460 return s.encode('ascii')
461
462 urlreq.quote = quote
463 urlreq.urlencode = urlencode
@@ -8,7 +8,99 b' from __future__ import absolute_import'
8 8
9 9 from . import pycompat
10 10
11 _sysstr = pycompat.sysstr
12
13 class _pycompatstub(object):
14 def __init__(self):
15 self._aliases = {}
16
17 def _registeraliases(self, origin, items):
18 """Add items that will be populated at the first access"""
19 items = map(_sysstr, items)
20 self._aliases.update(
21 (item.replace(_sysstr('_'), _sysstr('')).lower(), (origin, item))
22 for item in items)
23
24 def _registeralias(self, origin, attr, name):
25 """Alias ``origin``.``attr`` as ``name``"""
26 self._aliases[_sysstr(name)] = (origin, _sysstr(attr))
27
28 def __getattr__(self, name):
29 try:
30 origin, item = self._aliases[name]
31 except KeyError:
32 raise AttributeError(name)
33 self.__dict__[name] = obj = getattr(origin, item)
34 return obj
35
36 httpserver = _pycompatstub()
37 urlreq = _pycompatstub()
38 urlerr = _pycompatstub()
39
11 40 if pycompat.ispy3:
41 import urllib.parse
42 urlreq._registeraliases(urllib.parse, (
43 "splitattr",
44 "splitpasswd",
45 "splitport",
46 "splituser",
47 "urlparse",
48 "urlunparse",
49 ))
50 urlreq._registeralias(urllib.parse, "unquote_to_bytes", "unquote")
51 import urllib.request
52 urlreq._registeraliases(urllib.request, (
53 "AbstractHTTPHandler",
54 "BaseHandler",
55 "build_opener",
56 "FileHandler",
57 "FTPHandler",
58 "ftpwrapper",
59 "HTTPHandler",
60 "HTTPSHandler",
61 "install_opener",
62 "pathname2url",
63 "HTTPBasicAuthHandler",
64 "HTTPDigestAuthHandler",
65 "HTTPPasswordMgrWithDefaultRealm",
66 "ProxyHandler",
67 "Request",
68 "url2pathname",
69 "urlopen",
70 ))
71 import urllib.response
72 urlreq._registeraliases(urllib.response, (
73 "addclosehook",
74 "addinfourl",
75 ))
76 import urllib.error
77 urlerr._registeraliases(urllib.error, (
78 "HTTPError",
79 "URLError",
80 ))
81 import http.server
82 httpserver._registeraliases(http.server, (
83 "HTTPServer",
84 "BaseHTTPRequestHandler",
85 "SimpleHTTPRequestHandler",
86 "CGIHTTPRequestHandler",
87 ))
88
89 # urllib.parse.quote() accepts both str and bytes, decodes bytes
90 # (if necessary), and returns str. This is wonky. We provide a custom
91 # implementation that only accepts bytes and emits bytes.
92 def quote(s, safe=r'/'):
93 s = urllib.parse.quote_from_bytes(s, safe=safe)
94 return s.encode('ascii', 'strict')
95
96 # urllib.parse.urlencode() returns str. We use this function to make
97 # sure we return bytes.
98 def urlencode(query, doseq=False):
99 s = urllib.parse.urlencode(query, doseq=doseq)
100 return s.encode('ascii')
101
102 urlreq.quote = quote
103 urlreq.urlencode = urlencode
12 104
13 105 def getfullurl(req):
14 106 return req.full_url
@@ -25,6 +117,60 b' if pycompat.ispy3:'
25 117 def hasdata(req):
26 118 return req.data is not None
27 119 else:
120 import BaseHTTPServer
121 import CGIHTTPServer
122 import SimpleHTTPServer
123 import urllib2
124 import urllib
125 import urlparse
126 urlreq._registeraliases(urllib, (
127 "addclosehook",
128 "addinfourl",
129 "ftpwrapper",
130 "pathname2url",
131 "quote",
132 "splitattr",
133 "splitpasswd",
134 "splitport",
135 "splituser",
136 "unquote",
137 "url2pathname",
138 "urlencode",
139 ))
140 urlreq._registeraliases(urllib2, (
141 "AbstractHTTPHandler",
142 "BaseHandler",
143 "build_opener",
144 "FileHandler",
145 "FTPHandler",
146 "HTTPBasicAuthHandler",
147 "HTTPDigestAuthHandler",
148 "HTTPHandler",
149 "HTTPPasswordMgrWithDefaultRealm",
150 "HTTPSHandler",
151 "install_opener",
152 "ProxyHandler",
153 "Request",
154 "urlopen",
155 ))
156 urlreq._registeraliases(urlparse, (
157 "urlparse",
158 "urlunparse",
159 ))
160 urlerr._registeraliases(urllib2, (
161 "HTTPError",
162 "URLError",
163 ))
164 httpserver._registeraliases(BaseHTTPServer, (
165 "HTTPServer",
166 "BaseHTTPRequestHandler",
167 ))
168 httpserver._registeraliases(SimpleHTTPServer, (
169 "SimpleHTTPRequestHandler",
170 ))
171 httpserver._registeraliases(CGIHTTPServer, (
172 "CGIHTTPRequestHandler",
173 ))
28 174
29 175 def gethost(req):
30 176 return req.get_host()
@@ -50,6 +50,7 b' from . import ('
50 50 i18n,
51 51 policy,
52 52 pycompat,
53 urllibcompat,
53 54 )
54 55
55 56 base85 = policy.importmod(r'base85')
@@ -62,7 +63,6 b' b85encode = base85.b85encode'
62 63 cookielib = pycompat.cookielib
63 64 empty = pycompat.empty
64 65 httplib = pycompat.httplib
65 httpserver = pycompat.httpserver
66 66 pickle = pycompat.pickle
67 67 queue = pycompat.queue
68 68 socketserver = pycompat.socketserver
@@ -70,10 +70,12 b' stderr = pycompat.stderr'
70 70 stdin = pycompat.stdin
71 71 stdout = pycompat.stdout
72 72 stringio = pycompat.stringio
73 urlerr = pycompat.urlerr
74 urlreq = pycompat.urlreq
75 73 xmlrpclib = pycompat.xmlrpclib
76 74
75 httpserver = urllibcompat.httpserver
76 urlerr = urllibcompat.urlerr
77 urlreq = urllibcompat.urlreq
78
77 79 # workaround for win32mbcs
78 80 _filenamebytestr = pycompat.bytestr
79 81
General Comments 0
You need to be logged in to leave comments. Login now