##// END OF EJS Templates
hg-core: dedup LazyAncestors Iterator impl...
hg-core: dedup LazyAncestors Iterator impl … Which has been moved to the vcsgraph crate. AncestorsIterator cannot yet be removed because it is still being used in hg-core. Differential Revision: https://phab.mercurial-scm.org/D11948

File last commit:

r47543:521ac0d7 stable
r49351:791f5d5f default
Show More
urllibcompat.py
238 lines | 5.9 KiB | text/x-python | PythonLexer
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466 # urllibcompat.py - adapters to ease using urllib2 on Py2 and urllib on Py3
#
# Copyright 2017 Google, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
Gregory Szorc
py3: manually import getattr where it is needed...
r43359 from .pycompat import getattr
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466 from . import pycompat
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 _sysstr = pycompat.sysstr
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 class _pycompatstub(object):
def __init__(self):
self._aliases = {}
def _registeraliases(self, origin, items):
"""Add items that will be populated at the first access"""
items = map(_sysstr, items)
self._aliases.update(
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 (item.replace('_', '').lower(), (origin, item)) for item in items
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468
def _registeralias(self, origin, attr, name):
"""Alias ``origin``.``attr`` as ``name``"""
self._aliases[_sysstr(name)] = (origin, _sysstr(attr))
def __getattr__(self, name):
try:
origin, item = self._aliases[name]
except KeyError:
raise AttributeError(name)
self.__dict__[name] = obj = getattr(origin, item)
return obj
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 httpserver = _pycompatstub()
urlreq = _pycompatstub()
urlerr = _pycompatstub()
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466 if pycompat.ispy3:
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import urllib.parse
Augie Fackler
formatting: blacken the codebase...
r43346
urlreq._registeraliases(
urllib.parse,
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"splitattr",
b"splitpasswd",
b"splitport",
b"splituser",
b"urlparse",
b"urlunparse",
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 urlreq._registeralias(urllib.parse, b"parse_qs", b"parseqs")
urlreq._registeralias(urllib.parse, b"parse_qsl", b"parseqsl")
urlreq._registeralias(urllib.parse, b"unquote_to_bytes", b"unquote")
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import urllib.request
Augie Fackler
formatting: blacken the codebase...
r43346
urlreq._registeraliases(
urllib.request,
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"AbstractHTTPHandler",
b"BaseHandler",
b"build_opener",
b"FileHandler",
b"FTPHandler",
b"ftpwrapper",
b"HTTPHandler",
b"HTTPSHandler",
b"install_opener",
b"pathname2url",
b"HTTPBasicAuthHandler",
b"HTTPDigestAuthHandler",
b"HTTPPasswordMgrWithDefaultRealm",
b"ProxyHandler",
b"Request",
b"url2pathname",
b"urlopen",
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import urllib.response
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 urlreq._registeraliases(
urllib.response,
(
b"addclosehook",
b"addinfourl",
),
)
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import urllib.error
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 urlerr._registeraliases(
urllib.error,
(
b"HTTPError",
b"URLError",
),
)
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import http.server
Augie Fackler
formatting: blacken the codebase...
r43346
httpserver._registeraliases(
http.server,
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"HTTPServer",
b"BaseHTTPRequestHandler",
b"SimpleHTTPRequestHandler",
b"CGIHTTPRequestHandler",
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468
# urllib.parse.quote() accepts both str and bytes, decodes bytes
# (if necessary), and returns str. This is wonky. We provide a custom
# implementation that only accepts bytes and emits bytes.
Augie Fackler
cleanup: remove pointless r-prefixes on single-quoted strings...
r43906 def quote(s, safe='/'):
Gregory Szorc
py3: coerce bytestr to bytes to appease urllib.parse.quote_from_bytes()...
r40195 # bytestr has an __iter__ that emits characters. quote_from_bytes()
# does an iteration and expects ints. We coerce to bytes to appease it.
if isinstance(s, pycompat.bytestr):
s = bytes(s)
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 s = urllib.parse.quote_from_bytes(s, safe=safe)
return s.encode('ascii', 'strict')
# urllib.parse.urlencode() returns str. We use this function to make
# sure we return bytes.
def urlencode(query, doseq=False):
Augie Fackler
formatting: blacken the codebase...
r43346 s = urllib.parse.urlencode(query, doseq=doseq)
return s.encode('ascii')
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468
urlreq.quote = quote
urlreq.urlencode = urlencode
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466
def getfullurl(req):
return req.full_url
def gethost(req):
return req.host
def getselector(req):
return req.selector
def getdata(req):
return req.data
def hasdata(req):
return req.data is not None
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466 else:
Matt Harbison
typing: disable import error warnings that are already handled...
r47543 # pytype: disable=import-error
Augie Fackler
urllibcompat: move some adapters from pycompat to urllibcompat...
r34468 import BaseHTTPServer
import CGIHTTPServer
import SimpleHTTPServer
import urllib2
import urllib
import urlparse
Augie Fackler
formatting: blacken the codebase...
r43346
Matt Harbison
typing: disable import error warnings that are already handled...
r47543 # pytype: enable=import-error
Augie Fackler
formatting: blacken the codebase...
r43346 urlreq._registeraliases(
urllib,
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"addclosehook",
b"addinfourl",
b"ftpwrapper",
b"pathname2url",
b"quote",
b"splitattr",
b"splitpasswd",
b"splitport",
b"splituser",
b"unquote",
b"url2pathname",
b"urlencode",
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
urlreq._registeraliases(
urllib2,
(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"AbstractHTTPHandler",
b"BaseHandler",
b"build_opener",
b"FileHandler",
b"FTPHandler",
b"HTTPBasicAuthHandler",
b"HTTPDigestAuthHandler",
b"HTTPHandler",
b"HTTPPasswordMgrWithDefaultRealm",
b"HTTPSHandler",
b"install_opener",
b"ProxyHandler",
b"Request",
b"urlopen",
Augie Fackler
formatting: blacken the codebase...
r43346 ),
)
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 urlreq._registeraliases(
urlparse,
(
b"urlparse",
b"urlunparse",
),
)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 urlreq._registeralias(urlparse, b"parse_qs", b"parseqs")
urlreq._registeralias(urlparse, b"parse_qsl", b"parseqsl")
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 urlerr._registeraliases(
urllib2,
(
b"HTTPError",
b"URLError",
),
)
Augie Fackler
formatting: blacken the codebase...
r43346 httpserver._registeraliases(
Augie Fackler
formating: upgrade to black 20.8b1...
r46554 BaseHTTPServer,
(
b"HTTPServer",
b"BaseHTTPRequestHandler",
),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 httpserver._registeraliases(
SimpleHTTPServer, (b"SimpleHTTPRequestHandler",)
)
httpserver._registeraliases(CGIHTTPServer, (b"CGIHTTPRequestHandler",))
Augie Fackler
urllibcompat: new library to help abstract out some python3 urllib2 stuff...
r34466
def gethost(req):
return req.get_host()
def getselector(req):
return req.get_selector()
def getfullurl(req):
return req.get_full_url()
def getdata(req):
return req.get_data()
def hasdata(req):
return req.has_data()