Skip to content

Commit 9a57e98

Browse files
committed
chore: Further tear out the compat module
1 parent 1605d32 commit 9a57e98

9 files changed

Lines changed: 42 additions & 44 deletions

File tree

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,5 @@ Contributions have been made by:
6666
License
6767
*******
6868

69-
This library is released under the simplified BSD license except for the file
70-
``musicbrainzngs/compat.py`` which is licensed under the ISC license.
69+
This library is released under the simplified BSD license.
7170
See COPYING for details.

musicbrainzngs/caa.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
]
1111

1212
import json
13+
from urllib.parse import urlunparse
14+
from urllib.request import HTTPHandler, build_opener
1315

14-
from musicbrainzngs import compat, musicbrainz
16+
from musicbrainzngs import musicbrainz
1517
from musicbrainzngs.util import _unicode
1618

1719
hostname = "coverartarchive.org"
@@ -52,7 +54,7 @@ def _caa_request(mbid, imageid=None, size=None, entitytype="release"):
5254
path.append("%s-%s" % (imageid, size))
5355
elif imageid:
5456
path.append(imageid)
55-
url = compat.urlunparse((
57+
url = urlunparse((
5658
'https' if https else 'http',
5759
hostname,
5860
'/%s' % '/'.join(path),
@@ -63,10 +65,10 @@ def _caa_request(mbid, imageid=None, size=None, entitytype="release"):
6365
musicbrainz._log.debug("GET request for %s" % (url, ))
6466

6567
# Set up HTTP request handler and URL opener.
66-
httpHandler = compat.HTTPHandler(debuglevel=0)
68+
httpHandler = HTTPHandler(debuglevel=0)
6769
handlers = [httpHandler]
6870

69-
opener = compat.build_opener(*handlers)
71+
opener = build_opener(*handlers)
7072

7173
# Make request.
7274
req = musicbrainz._MusicbrainzHttpRequest("GET", url, None)

musicbrainzngs/compat.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
from http.client import BadStatusLine, HTTPException
2-
from io import StringIO
3-
from urllib.error import HTTPError, URLError
4-
from urllib.parse import quote_plus, urlencode, urlunparse
5-
from urllib.request import HTTPDigestAuthHandler, HTTPHandler, HTTPPasswordMgr, Request, build_opener
1+
from urllib.request import build_opener

musicbrainzngs/musicbrainz.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
from musicbrainzngs import mbxml
2020
from musicbrainzngs import util
21-
from musicbrainzngs import compat
21+
from urllib.parse import urlencode, urlunparse
22+
from urllib.request import HTTPDigestAuthHandler, HTTPHandler, HTTPPasswordMgr, Request, build_opener
2223

2324
_version = "0.7.1"
2425
_log = logging.getLogger("musicbrainzngs")
@@ -421,7 +422,7 @@ def __call__(self, *args, **kwargs):
421422
return self.fun(*args, **kwargs)
422423

423424
# From pymb2
424-
class _RedirectPasswordMgr(compat.HTTPPasswordMgr):
425+
class _RedirectPasswordMgr(HTTPPasswordMgr):
425426
def __init__(self):
426427
self._realms = { }
427428

@@ -436,13 +437,13 @@ def add_password(self, realm, uri, username, password):
436437
# ignoring the uri parameter intentionally
437438
self._realms[realm] = (username, password)
438439

439-
class _DigestAuthHandler(compat.HTTPDigestAuthHandler):
440+
class _DigestAuthHandler(HTTPDigestAuthHandler):
440441
def get_authorization (self, req, chal):
441442
qop = chal.get ('qop', None)
442443
if qop and ',' in qop and 'auth' in qop.split (','):
443444
chal['qop'] = 'auth'
444445

445-
return compat.HTTPDigestAuthHandler.get_authorization (self, req, chal)
446+
return HTTPDigestAuthHandler.get_authorization (self, req, chal)
446447

447448
def _encode_utf8(self, msg):
448449
"""The MusicBrainz server also accepts UTF-8 encoded passwords."""
@@ -467,10 +468,10 @@ def get_algorithm_impls(self, algorithm):
467468
KD = lambda s, d: H("%s:%s" % (s, d))
468469
return H, KD
469470

470-
class _MusicbrainzHttpRequest(compat.Request):
471+
class _MusicbrainzHttpRequest(Request):
471472
""" A custom request handler that allows DELETE and PUT"""
472473
def __init__(self, method, url, data=None):
473-
compat.Request.__init__(self, url, data)
474+
Request.__init__(self, url, data)
474475
allowed_m = ["GET", "POST", "DELETE", "PUT"]
475476
if method not in allowed_m:
476477
raise ValueError("invalid method: %s" % method)
@@ -501,7 +502,7 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
501502
f = opener.open(req)
502503
return f.read()
503504

504-
except compat.HTTPError as exc:
505+
except HTTPError as exc:
505506
if exc.code in (400, 404, 411):
506507
# Bad request, not found, etc.
507508
raise ResponseError(cause=exc)
@@ -515,13 +516,13 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
515516
# retrying for now.
516517
_log.info("unknown HTTP error %i" % exc.code)
517518
last_exc = exc
518-
except compat.BadStatusLine as exc:
519+
except BadStatusLine as exc:
519520
_log.info("bad status line")
520521
last_exc = exc
521-
except compat.HTTPException as exc:
522+
except HTTPException as exc:
522523
_log.info("miscellaneous HTTP exception: %s" % str(exc))
523524
last_exc = exc
524-
except compat.URLError as exc:
525+
except URLError as exc:
525526
if isinstance(exc.reason, socket.error):
526527
code = exc.reason.errno
527528
if code == 104: # "Connection reset by peer."
@@ -646,18 +647,18 @@ def _mb_request(path, method='GET', auth_required=AUTH_NO,
646647

647648
# Construct the full URL for the request, including hostname and
648649
# query string.
649-
url = compat.urlunparse((
650+
url = urlunparse((
650651
'https' if https else 'http',
651652
hostname,
652653
'/ws/2/%s' % path,
653654
'',
654-
compat.urlencode(newargs),
655+
urlencode(newargs),
655656
''
656657
))
657658
_log.debug("%s request for %s" % (method, url))
658659

659660
# Set up HTTP request handler and URL opener.
660-
httpHandler = compat.HTTPHandler(debuglevel=0)
661+
httpHandler = HTTPHandler(debuglevel=0)
661662
handlers = [httpHandler]
662663

663664
# Add credentials if required.
@@ -679,7 +680,7 @@ def _mb_request(path, method='GET', auth_required=AUTH_NO,
679680
authHandler.add_password("musicbrainz.org", (), user, password)
680681
handlers.append(authHandler)
681682

682-
opener = compat.build_opener(*handlers)
683+
opener = build_opener(*handlers)
683684

684685
# Make request.
685686
req = _MusicbrainzHttpRequest(method, url, data)

musicbrainzngs/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import locale
77
import sys
88
import xml.etree.ElementTree as ET
9-
10-
from . import compat
9+
from io import StringIO
1110

1211

1312
def _unicode(string, encoding=None):
@@ -39,6 +38,6 @@ def bytes_to_elementtree(bytes_or_file):
3938

4039
s = _unicode(s, "utf-8")
4140

42-
f = compat.StringIO(s)
41+
f = StringIO(s)
4342
tree = ET.ElementTree(file=f)
4443
return tree

test/_common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from urllib.request import OpenerDirector
77

88
import musicbrainzngs
9-
from musicbrainzngs import compat
109

1110

1211
class FakeOpener(OpenerDirector):

test/test_caa.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
2+
from urllib.error import HTTPError
23

34
import musicbrainzngs
4-
from musicbrainzngs import caa, compat
5+
from musicbrainzngs import caa
56
from musicbrainzngs.musicbrainz import _version
67
from test import _common
78

@@ -32,7 +33,7 @@ def test_get_release_group_list(self):
3233
def test_list_none(self):
3334
""" When CAA gives a 404 error, pass it through."""
3435

35-
exc = compat.HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
36+
exc = HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
3637
self.opener = _common.FakeOpener(exception=musicbrainzngs.ResponseError(cause=exc))
3738
musicbrainzngs.compat.build_opener = lambda *args: self.opener
3839
try:
@@ -42,7 +43,7 @@ def test_list_none(self):
4243
self.assertEqual(e.cause.code, 404)
4344

4445
def test_list_baduuid(self):
45-
exc = compat.HTTPError("", 400, "", "", _common.StringIO.StringIO(""))
46+
exc = HTTPError("", 400, "", "", _common.StringIO.StringIO(""))
4647
self.opener = _common.FakeOpener(exception=musicbrainzngs.ResponseError(cause=exc))
4748
musicbrainzngs.compat.build_opener = lambda *args: self.opener
4849
try:

test/test_collection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
2+
from urllib.error import HTTPError
23

34
import musicbrainzngs
4-
from musicbrainzngs import compat
55
from test import _common
66

77

@@ -79,7 +79,7 @@ def local_mb_request(path, method='GET',
7979
def test_no_collection(self):
8080
""" If a collection doesn't exist, you get a 404 """
8181

82-
exc = compat.HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
82+
exc = HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
8383
self.opener = _common.FakeOpener(exception=musicbrainzngs.ResponseError(cause=exc))
8484
musicbrainzngs.compat.build_opener = lambda *args: self.opener
8585
try:
@@ -92,7 +92,7 @@ def test_private_collection(self):
9292
""" If you ask for a collection that is private, you should
9393
get a 401"""
9494

95-
exc = compat.HTTPError("", 401, "", "", _common.StringIO.StringIO(""))
95+
exc = HTTPError("", 401, "", "", _common.StringIO.StringIO(""))
9696
self.opener = _common.FakeOpener(exception=musicbrainzngs.AuthenticationError(cause=exc))
9797
musicbrainzngs.compat.build_opener = lambda *args: self.opener
9898
try:

test/test_search.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from urllib.parse import quote_plus
23

34
import musicbrainzngs
45
from test import _common
@@ -26,7 +27,7 @@ def test_search_annotations(self):
2627
# TODO: We escape special characters and then urlencode all query parameters, which may
2728
# not be necessary, but MusicBrainz accepts it and appears to return the same value as without
2829
expected_query = r'entity:(bdb24cb5\-404b\-4f60\-bba4\-7b730325ae47)'
29-
expected = 'https://musicbrainz.org/ws/2/annotation/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
30+
expected = 'https://musicbrainz.org/ws/2/annotation/?query=%s' % quote_plus(expected_query)
3031
self.assertEqual(expected, self.opener.get_url())
3132

3233
# Invalid query field
@@ -39,7 +40,7 @@ def test_search_artists(self):
3940

4041
musicbrainzngs.search_artists(artist="Dynamo Go")
4142
expected_query = 'artist:(dynamo go)'
42-
expected = 'https://musicbrainz.org/ws/2/artist/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
43+
expected = 'https://musicbrainz.org/ws/2/artist/?query=%s' % quote_plus(expected_query)
4344
self.assertEqual(expected, self.opener.get_url())
4445

4546
# Invalid query field
@@ -52,7 +53,7 @@ def test_search_events(self):
5253

5354
musicbrainzngs.search_events(event="woodstock")
5455
expected_query = 'event:(woodstock)'
55-
expected = 'https://musicbrainz.org/ws/2/event/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
56+
expected = 'https://musicbrainz.org/ws/2/event/?query=%s' % quote_plus(expected_query)
5657
self.assertEqual(expected, self.opener.get_url())
5758

5859
# Invalid query field
@@ -65,7 +66,7 @@ def test_search_labels(self):
6566

6667
musicbrainzngs.search_labels(label="Waysafe")
6768
expected_query = 'label:(waysafe)'
68-
expected = 'https://musicbrainz.org/ws/2/label/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
69+
expected = 'https://musicbrainz.org/ws/2/label/?query=%s' % quote_plus(expected_query)
6970
self.assertEqual(expected, self.opener.get_url())
7071

7172
# Invalid query field
@@ -78,7 +79,7 @@ def test_search_places(self):
7879

7980
musicbrainzngs.search_places(place="Fillmore")
8081
expected_query = 'place:(fillmore)'
81-
expected = 'https://musicbrainz.org/ws/2/place/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
82+
expected = 'https://musicbrainz.org/ws/2/place/?query=%s' % quote_plus(expected_query)
8283
self.assertEqual(expected, self.opener.get_url())
8384

8485
# Invalid query field
@@ -91,7 +92,7 @@ def test_search_releases(self):
9192

9293
musicbrainzngs.search_releases(release="Affordable Pop Music")
9394
expected_query = 'release:(affordable pop music)'
94-
expected = 'https://musicbrainz.org/ws/2/release/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
95+
expected = 'https://musicbrainz.org/ws/2/release/?query=%s' % quote_plus(expected_query)
9596
self.assertEqual(expected, self.opener.get_url())
9697

9798
# Invalid query field
@@ -104,7 +105,7 @@ def test_search_release_groups(self):
104105

105106
musicbrainzngs.search_release_groups(releasegroup="Affordable Pop Music")
106107
expected_query = 'releasegroup:(affordable pop music)'
107-
expected = 'https://musicbrainz.org/ws/2/release-group/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
108+
expected = 'https://musicbrainz.org/ws/2/release-group/?query=%s' % quote_plus(expected_query)
108109
self.assertEqual(expected, self.opener.get_url())
109110

110111
# Invalid query field
@@ -117,7 +118,7 @@ def test_search_recordings(self):
117118

118119
musicbrainzngs.search_recordings(recording="Thief of Hearts")
119120
expected_query = 'recording:(thief of hearts)'
120-
expected = 'https://musicbrainz.org/ws/2/recording/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
121+
expected = 'https://musicbrainz.org/ws/2/recording/?query=%s' % quote_plus(expected_query)
121122
self.assertEqual(expected, self.opener.get_url())
122123

123124
# Invalid query field
@@ -130,7 +131,7 @@ def test_search_works(self):
130131

131132
musicbrainzngs.search_works(work="Fountain City")
132133
expected_query = 'work:(fountain city)'
133-
expected = 'https://musicbrainz.org/ws/2/work/?query=%s' % musicbrainzngs.compat.quote_plus(expected_query)
134+
expected = 'https://musicbrainz.org/ws/2/work/?query=%s' % quote_plus(expected_query)
134135
self.assertEqual(expected, self.opener.get_url())
135136

136137
# Invalid query field

0 commit comments

Comments
 (0)