Skip to content

Commit 25e841a

Browse files
committed
chore: Organize imports and remove some Python 2 kludges
1 parent 15ac0a6 commit 25e841a

29 files changed

Lines changed: 65 additions & 108 deletions

docs/conf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
#
31
# musicbrainzngs documentation build configuration file, created by
42
# sphinx-quickstart2 on Thu Apr 26 15:56:46 2012.
53
#
@@ -11,7 +9,8 @@
119
# All configuration values have a default; values that are commented out
1210
# serve to show the default.
1311

14-
import sys, os
12+
import os
13+
import sys
1514

1615
# If extensions (or modules to document with autodoc) are in another directory,
1716
# add these directories to sys.path here. If the directory is relative to the

examples/collection.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323
$ ./collection.py USERNAME 4137a646-a104-4031-b549-da4e1f36a463
2424
--remove 0d432d8b-8865-4ae9-8479-3a197620a37b
2525
"""
26-
from __future__ import print_function
27-
from __future__ import unicode_literals
28-
import musicbrainzngs
2926
import getpass
30-
from optparse import OptionParser
3127
import sys
28+
from optparse import OptionParser
29+
30+
import musicbrainzngs
3231

33-
try:
34-
user_input = raw_input
35-
except NameError:
36-
user_input = input
32+
user_input = input
3733

3834
musicbrainzngs.set_useragent(
3935
"python-musicbrainzngs-example",

examples/find_disc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
...
1313
"""
1414

15-
from __future__ import unicode_literals
16-
import musicbrainzngs
1715
import sys
1816

17+
import musicbrainzngs
18+
1919
musicbrainzngs.set_useragent(
2020
"python-musicbrainzngs-example",
2121
"0.1",

examples/releasesearch.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
Released 1966-08-08 (Official)
88
MusicBrainz ID: b4b04cbf-118a-3944-9545-38a0a88ff1a2
99
"""
10-
from __future__ import print_function
11-
from __future__ import unicode_literals
12-
import musicbrainzngs
1310
import sys
1411

12+
import musicbrainzngs
13+
1514
musicbrainzngs.set_useragent(
1615
"python-musicbrainzngs-example",
1716
"0.1",

musicbrainzngs/caa.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
import json
1313

14-
from musicbrainzngs import compat
15-
from musicbrainzngs import musicbrainz
14+
from musicbrainzngs import compat, musicbrainz
1615
from musicbrainzngs.util import _unicode
1716

1817
hostname = "coverartarchive.org"

musicbrainzngs/compat.py

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# Copyright (c) 2012 Kenneth Reitz.
32

43
# Permission to use, copy, modify, and/or distribute this software for any
@@ -13,49 +12,14 @@
1312
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1413
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1514

16-
"""
17-
pythoncompat
18-
"""
19-
20-
15+
"""pythoncompat"""
2116
import sys
22-
23-
# -------
24-
# Pythons
25-
# -------
26-
27-
# Syntax sugar.
28-
_ver = sys.version_info
29-
30-
#: Python 2.x?
31-
is_py2 = (_ver[0] == 2)
32-
33-
#: Python 3.x?
34-
is_py3 = (_ver[0] == 3)
35-
36-
# ---------
37-
# Specifics
38-
# ---------
39-
40-
if is_py2:
41-
from StringIO import StringIO
42-
from urllib2 import HTTPPasswordMgr, HTTPDigestAuthHandler, Request,\
43-
HTTPHandler, build_opener, HTTPError, URLError
44-
from httplib import BadStatusLine, HTTPException
45-
from urlparse import urlunparse
46-
from urllib import urlencode, quote_plus
47-
48-
bytes = str
49-
unicode = unicode
50-
basestring = basestring
51-
elif is_py3:
52-
from io import StringIO
53-
from urllib.request import HTTPPasswordMgr, HTTPDigestAuthHandler, Request,\
54-
HTTPHandler, build_opener
55-
from urllib.error import HTTPError, URLError
56-
from http.client import HTTPException, BadStatusLine
57-
from urllib.parse import urlunparse, urlencode, quote_plus
58-
59-
unicode = str
60-
bytes = bytes
61-
basestring = (str,bytes)
17+
from http.client import BadStatusLine, HTTPException
18+
from io import StringIO
19+
from urllib.error import HTTPError, URLError
20+
from urllib.parse import quote_plus, urlencode, urlunparse
21+
from urllib.request import HTTPDigestAuthHandler, HTTPHandler, HTTPPasswordMgr, Request, build_opener
22+
23+
unicode = str
24+
bytes = bytes
25+
basestring = (str,bytes)

musicbrainzngs/mbxml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# This file is distributed under a BSD-2-Clause type license.
44
# See the COPYING file for more information.
55

6+
import logging
67
import re
78
import xml.etree.ElementTree as ET
8-
import logging
99

1010
from . import util
1111

musicbrainzngs/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# This file is distributed under a BSD-2-Clause type license.
44
# See the COPYING file for more information.
55

6-
import sys
76
import locale
7+
import sys
88
import xml.etree.ElementTree as ET
99

1010
from . import compat
1111

12+
1213
def _unicode(string, encoding=None):
1314
"""Try to decode byte strings to unicode.
1415
This can only be a guess, but this might be better than failing.
@@ -36,8 +37,7 @@ def bytes_to_elementtree(bytes_or_file):
3637
else:
3738
s = bytes_or_file.read()
3839

39-
if compat.is_py3:
40-
s = _unicode(s, "utf-8")
40+
s = _unicode(s, "utf-8")
4141

4242
f = compat.StringIO(s)
4343
tree = ET.ElementTree(file=f)

query.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import sys
2-
31
import musicbrainzngs as m
42

5-
def main():
3+
4+
def main() -> None:
65
m.set_useragent("application", "0.01", "http://example.com")
76
print(m.get_artist_by_id("952a4205-023d-4235-897c-6fdb6f58dfaa", []))
87
#print m.get_label_by_id("aab2e720-bdd2-4565-afc2-460743585f16")

test/_common.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
"""Common support for the test cases."""
2+
import io as StringIO
23
import time
4+
from io import BytesIO
5+
from os.path import join
6+
from urllib.request import OpenerDirector
37

48
import musicbrainzngs
59
from musicbrainzngs import compat
6-
from os.path import join
7-
8-
try:
9-
from urllib2 import OpenerDirector
10-
except ImportError:
11-
from urllib.request import OpenerDirector
12-
13-
from io import BytesIO
1410

15-
try:
16-
import StringIO
17-
except ImportError:
18-
import io as StringIO
1911

2012
class FakeOpener(OpenerDirector):
2113
""" A URL Opener that saves the URL requested and

0 commit comments

Comments
 (0)