Skip to content

Commit 5d93b60

Browse files
committed
chore: Further tear out the compat module
1 parent 674314f commit 5d93b60

File tree

8 files changed

+40
-35
lines changed

8 files changed

+40
-35
lines changed

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
]
1111

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

1416
from musicbrainzngs import compat, musicbrainz
1517
from musicbrainzngs.util import _unicode
@@ -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,7 +65,7 @@ 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

6971
opener = compat.build_opener(*handlers)

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: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from musicbrainzngs import mbxml
2020
from musicbrainzngs import util
2121
from musicbrainzngs import compat
22+
from http.client import BadStatusLine, HTTPException
23+
from urllib.error import HTTPError, URLError
24+
from urllib.parse import urlencode, urlunparse
25+
from urllib.request import HTTPDigestAuthHandler, HTTPHandler, HTTPPasswordMgr, Request
2226

2327
_version = "0.7.1"
2428
_log = logging.getLogger("musicbrainzngs")
@@ -421,7 +425,7 @@ def __call__(self, *args, **kwargs):
421425
return self.fun(*args, **kwargs)
422426

423427
# From pymb2
424-
class _RedirectPasswordMgr(compat.HTTPPasswordMgr):
428+
class _RedirectPasswordMgr(HTTPPasswordMgr):
425429
def __init__(self):
426430
self._realms = { }
427431

@@ -436,13 +440,13 @@ def add_password(self, realm, uri, username, password):
436440
# ignoring the uri parameter intentionally
437441
self._realms[realm] = (username, password)
438442

439-
class _DigestAuthHandler(compat.HTTPDigestAuthHandler):
443+
class _DigestAuthHandler(HTTPDigestAuthHandler):
440444
def get_authorization (self, req, chal):
441445
qop = chal.get ('qop', None)
442446
if qop and ',' in qop and 'auth' in qop.split (','):
443447
chal['qop'] = 'auth'
444448

445-
return compat.HTTPDigestAuthHandler.get_authorization (self, req, chal)
449+
return HTTPDigestAuthHandler.get_authorization (self, req, chal)
446450

447451
def _encode_utf8(self, msg):
448452
"""The MusicBrainz server also accepts UTF-8 encoded passwords."""
@@ -467,10 +471,10 @@ def get_algorithm_impls(self, algorithm):
467471
KD = lambda s, d: H("%s:%s" % (s, d))
468472
return H, KD
469473

470-
class _MusicbrainzHttpRequest(compat.Request):
474+
class _MusicbrainzHttpRequest(Request):
471475
""" A custom request handler that allows DELETE and PUT"""
472476
def __init__(self, method, url, data=None):
473-
compat.Request.__init__(self, url, data)
477+
Request.__init__(self, url, data)
474478
allowed_m = ["GET", "POST", "DELETE", "PUT"]
475479
if method not in allowed_m:
476480
raise ValueError("invalid method: %s" % method)
@@ -501,7 +505,7 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
501505
f = opener.open(req)
502506
return f.read()
503507

504-
except compat.HTTPError as exc:
508+
except HTTPError as exc:
505509
if exc.code in (400, 404, 411):
506510
# Bad request, not found, etc.
507511
raise ResponseError(cause=exc)
@@ -515,13 +519,13 @@ def _safe_read(opener, req, body=None, max_retries=_max_retries, retry_delay_del
515519
# retrying for now.
516520
_log.info("unknown HTTP error %i" % exc.code)
517521
last_exc = exc
518-
except compat.BadStatusLine as exc:
522+
except BadStatusLine as exc:
519523
_log.info("bad status line")
520524
last_exc = exc
521-
except compat.HTTPException as exc:
525+
except HTTPException as exc:
522526
_log.info("miscellaneous HTTP exception: %s" % str(exc))
523527
last_exc = exc
524-
except compat.URLError as exc:
528+
except URLError as exc:
525529
if isinstance(exc.reason, socket.error):
526530
code = exc.reason.errno
527531
if code == 104: # "Connection reset by peer."
@@ -646,18 +650,18 @@ def _mb_request(path, method='GET', auth_required=AUTH_NO,
646650

647651
# Construct the full URL for the request, including hostname and
648652
# query string.
649-
url = compat.urlunparse((
653+
url = urlunparse((
650654
'https' if https else 'http',
651655
hostname,
652656
'/ws/2/%s' % path,
653657
'',
654-
compat.urlencode(newargs),
658+
urlencode(newargs),
655659
''
656660
))
657661
_log.debug("%s request for %s" % (method, url))
658662

659663
# Set up HTTP request handler and URL opener.
660-
httpHandler = compat.HTTPHandler(debuglevel=0)
664+
httpHandler = HTTPHandler(debuglevel=0)
661665
handlers = [httpHandler]
662666

663667
# Add credentials if required.

musicbrainzngs/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import locale
77
import sys
88
import xml.etree.ElementTree as ET
9+
from io import StringIO
910

1011
from . import compat
1112

@@ -39,6 +40,6 @@ def bytes_to_elementtree(bytes_or_file):
3940

4041
s = _unicode(s, "utf-8")
4142

42-
f = compat.StringIO(s)
43+
f = StringIO(s)
4344
tree = ET.ElementTree(file=f)
4445
return tree

test/test_caa.py

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

34
import musicbrainzngs
45
from musicbrainzngs import caa, compat
@@ -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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from urllib.error import HTTPError
23

34
import musicbrainzngs
45
from musicbrainzngs import compat
@@ -79,7 +80,7 @@ def local_mb_request(path, method='GET',
7980
def test_no_collection(self):
8081
""" If a collection doesn't exist, you get a 404 """
8182

82-
exc = compat.HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
83+
exc = HTTPError("", 404, "", "", _common.StringIO.StringIO(""))
8384
self.opener = _common.FakeOpener(exception=musicbrainzngs.ResponseError(cause=exc))
8485
musicbrainzngs.compat.build_opener = lambda *args: self.opener
8586
try:
@@ -92,7 +93,7 @@ def test_private_collection(self):
9293
""" If you ask for a collection that is private, you should
9394
get a 401"""
9495

95-
exc = compat.HTTPError("", 401, "", "", _common.StringIO.StringIO(""))
96+
exc = HTTPError("", 401, "", "", _common.StringIO.StringIO(""))
9697
self.opener = _common.FakeOpener(exception=musicbrainzngs.AuthenticationError(cause=exc))
9798
musicbrainzngs.compat.build_opener = lambda *args: self.opener
9899
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)