1919from musicbrainzngs import mbxml
2020from musicbrainzngs import util
2121from musicbrainzngs import compat
22+ from urllib .parse import urlencode , urlunparse
23+ from urllib .request import HTTPDigestAuthHandler , HTTPHandler , HTTPPasswordMgr , Request , build_opener
2224
2325_version = "0.7.1"
2426_log = logging .getLogger ("musicbrainzngs" )
@@ -421,7 +423,7 @@ def __call__(self, *args, **kwargs):
421423 return self .fun (* args , ** kwargs )
422424
423425# From pymb2
424- class _RedirectPasswordMgr (compat . HTTPPasswordMgr ):
426+ class _RedirectPasswordMgr (HTTPPasswordMgr ):
425427 def __init__ (self ):
426428 self ._realms = { }
427429
@@ -436,13 +438,13 @@ def add_password(self, realm, uri, username, password):
436438 # ignoring the uri parameter intentionally
437439 self ._realms [realm ] = (username , password )
438440
439- class _DigestAuthHandler (compat . HTTPDigestAuthHandler ):
441+ class _DigestAuthHandler (HTTPDigestAuthHandler ):
440442 def get_authorization (self , req , chal ):
441443 qop = chal .get ('qop' , None )
442444 if qop and ',' in qop and 'auth' in qop .split (',' ):
443445 chal ['qop' ] = 'auth'
444446
445- return compat . HTTPDigestAuthHandler .get_authorization (self , req , chal )
447+ return HTTPDigestAuthHandler .get_authorization (self , req , chal )
446448
447449 def _encode_utf8 (self , msg ):
448450 """The MusicBrainz server also accepts UTF-8 encoded passwords."""
@@ -467,10 +469,10 @@ def get_algorithm_impls(self, algorithm):
467469 KD = lambda s , d : H ("%s:%s" % (s , d ))
468470 return H , KD
469471
470- class _MusicbrainzHttpRequest (compat . Request ):
472+ class _MusicbrainzHttpRequest (Request ):
471473 """ A custom request handler that allows DELETE and PUT"""
472474 def __init__ (self , method , url , data = None ):
473- compat . Request .__init__ (self , url , data )
475+ Request .__init__ (self , url , data )
474476 allowed_m = ["GET" , "POST" , "DELETE" , "PUT" ]
475477 if method not in allowed_m :
476478 raise ValueError ("invalid method: %s" % method )
@@ -501,7 +503,7 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
501503 f = opener .open (req )
502504 return f .read ()
503505
504- except compat . HTTPError as exc :
506+ except HTTPError as exc :
505507 if exc .code in (400 , 404 , 411 ):
506508 # Bad request, not found, etc.
507509 raise ResponseError (cause = exc )
@@ -515,13 +517,13 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
515517 # retrying for now.
516518 _log .info ("unknown HTTP error %i" % exc .code )
517519 last_exc = exc
518- except compat . BadStatusLine as exc :
520+ except BadStatusLine as exc :
519521 _log .info ("bad status line" )
520522 last_exc = exc
521- except compat . HTTPException as exc :
523+ except HTTPException as exc :
522524 _log .info ("miscellaneous HTTP exception: %s" % str (exc ))
523525 last_exc = exc
524- except compat . URLError as exc :
526+ except URLError as exc :
525527 if isinstance (exc .reason , socket .error ):
526528 code = exc .reason .errno
527529 if code == 104 : # "Connection reset by peer."
@@ -646,18 +648,18 @@ def _mb_request(path, method='GET', auth_required=AUTH_NO,
646648
647649 # Construct the full URL for the request, including hostname and
648650 # query string.
649- url = compat . urlunparse ((
651+ url = urlunparse ((
650652 'https' if https else 'http' ,
651653 hostname ,
652654 '/ws/2/%s' % path ,
653655 '' ,
654- compat . urlencode (newargs ),
656+ urlencode (newargs ),
655657 ''
656658 ))
657659 _log .debug ("%s request for %s" % (method , url ))
658660
659661 # Set up HTTP request handler and URL opener.
660- httpHandler = compat . HTTPHandler (debuglevel = 0 )
662+ httpHandler = HTTPHandler (debuglevel = 0 )
661663 handlers = [httpHandler ]
662664
663665 # Add credentials if required.
@@ -679,7 +681,7 @@ def _mb_request(path, method='GET', auth_required=AUTH_NO,
679681 authHandler .add_password ("musicbrainz.org" , (), user , password )
680682 handlers .append (authHandler )
681683
682- opener = compat . build_opener (* handlers )
684+ opener = build_opener (* handlers )
683685
684686 # Make request.
685687 req = _MusicbrainzHttpRequest (method , url , data )
0 commit comments