Skip to content

Commit fa3cae3

Browse files
committed
Add _compatimports.py to simplify handling py2 vs py3
Groups all the annoying module naming differences Signed-off-by: Cole Robinson <[email protected]>
1 parent b633eaa commit fa3cae3

7 files changed

Lines changed: 39 additions & 52 deletions

File tree

bugzilla/_authfiles.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22
# See the COPYING file in the top-level directory.
33

44
import os
5-
import sys
65
from logging import getLogger
76

8-
# pylint: disable=import-error,no-name-in-module,ungrouped-imports
9-
if sys.version_info[0] >= 3:
10-
from configparser import ConfigParser
11-
from http.cookiejar import LoadError, MozillaCookieJar
12-
from urllib.parse import urlparse # pylint: disable=no-name-in-module
13-
else:
14-
from ConfigParser import SafeConfigParser as ConfigParser
15-
from cookielib import LoadError, MozillaCookieJar
16-
from urlparse import urlparse
17-
# pylint: enable=import-error,no-name-in-module,ungrouped-imports
18-
7+
from ._compatimports import (ConfigParser, LoadError,
8+
MozillaCookieJar, urlparse)
199
from .exceptions import BugzillaError
2010
from ._util import listify
2111

bugzilla/_backendxmlrpc.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
from logging import getLogger
55
import sys
66

7-
# pylint: disable=import-error
8-
if sys.version_info[0] >= 3:
9-
from xmlrpc.client import (Binary, Fault, ProtocolError,
10-
ServerProxy, Transport)
11-
else:
12-
from xmlrpclib import Binary, Fault, ProtocolError, ServerProxy, Transport
13-
# pylint: enable=import-error
14-
157
from requests import RequestException
168

179
from ._backendbase import _BackendBase
10+
from ._compatimports import (Binary, Fault, ProtocolError,
11+
ServerProxy, Transport)
1812
from .exceptions import BugzillaError
1913
from ._util import listify
2014

bugzilla/_cli.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,11 @@
2121
import sys
2222
import tempfile
2323

24-
# pylint: disable=import-error
25-
if sys.version_info[0] >= 3:
26-
# pylint: disable=no-name-in-module,redefined-builtin
27-
from xmlrpc.client import Fault, ProtocolError
28-
from urllib.parse import urlparse
29-
basestring = (str, bytes)
30-
else:
31-
from xmlrpclib import Fault, ProtocolError
32-
from urlparse import urlparse
33-
# pylint: enable=import-error
34-
3524
import requests.exceptions
3625

3726
import bugzilla
27+
from bugzilla._compatimports import Fault, ProtocolError, urlparse, IS_PY3
28+
3829

3930
DEFAULT_BZ = 'https://bugzilla.redhat.com'
4031

@@ -57,12 +48,14 @@ def _is_unittest_debug():
5748

5849
def to_encoding(ustring):
5950
string = ''
51+
if IS_PY3:
52+
basestring = (str, bytes)
6053
if isinstance(ustring, basestring):
6154
string = ustring
6255
elif ustring is not None:
6356
string = str(ustring)
6457

65-
if sys.version_info[0] >= 3:
58+
if IS_PY3:
6659
return string
6760

6861
preferred = locale.getpreferredencoding()

bugzilla/_compatimports.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This work is licensed under the GNU GPLv2 or later.
2+
# See the COPYING file in the top-level directory.
3+
4+
import sys
5+
6+
IS_PY3 = sys.version_info[0] >= 3
7+
8+
# pylint: disable=import-error,unused-import,ungrouped-imports
9+
# pylint: disable=no-name-in-module
10+
if IS_PY3:
11+
from collections.abc import Mapping
12+
from configparser import ConfigParser
13+
from http.cookiejar import LoadError, MozillaCookieJar
14+
from urllib.parse import urlparse, urlunparse, parse_qsl
15+
from xmlrpc.client import (Binary, Fault, ProtocolError,
16+
ServerProxy, Transport)
17+
else: # pragma: no cover
18+
from collections import Mapping
19+
from ConfigParser import SafeConfigParser as ConfigParser
20+
from cookielib import LoadError, MozillaCookieJar
21+
from urlparse import urlparse
22+
from xmlrpclib import Binary, Fault, ProtocolError, ServerProxy, Transport
23+
from urlparse import urlparse, urlunparse, parse_qsl

bugzilla/_session.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33

44
import base64
55
from logging import getLogger
6-
import sys
7-
8-
# pylint: disable=import-error
9-
if sys.version_info[0] >= 3:
10-
from urllib.parse import urlparse # pylint: disable=no-name-in-module
11-
else:
12-
from urlparse import urlparse
13-
# pylint: enable=import-error
146

157
import requests
168

179
from ._authfiles import _BugzillaTokenCache
10+
from ._compatimports import urlparse
1811

1912

2013
log = getLogger(__name__)

bugzilla/base.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@
1515

1616
from io import BytesIO
1717

18-
# pylint: disable=import-error,no-name-in-module,ungrouped-imports
19-
if sys.version_info[0] >= 3:
20-
from collections.abc import Mapping
21-
from urllib.parse import urlparse, urlunparse, parse_qsl
22-
else:
23-
from collections import Mapping
24-
from urlparse import urlparse, urlunparse, parse_qsl
25-
# pylint: enable=import-error,no-name-in-module,ungrouped-imports
26-
27-
2818
from ._authfiles import (DEFAULT_CONFIGPATHS, open_bugzillarc,
2919
_build_cookiejar, _default_cache_location,
3020
_parse_hostname, _save_api_key)
3121
from .apiversion import __version__
3222
from ._backendxmlrpc import _BackendXMLRPC
23+
from ._compatimports import Mapping, urlparse, urlunparse, parse_qsl
3324
from .bug import Bug, User
3425
from .exceptions import BugzillaError
3526
from ._session import _BugzillaSession

bugzilla/bug.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
# See the COPYING file in the top-level directory.
88

99
from __future__ import unicode_literals
10+
1011
import locale
1112
from logging import getLogger
12-
import sys
13+
14+
from ._compatimports import IS_PY3
15+
1316

1417
log = getLogger(__name__)
1518

@@ -49,7 +52,7 @@ def __str__(self):
4952
'print(bug)' is not recommended because of potential encoding issues.
5053
Please use unicode(bug) where possible.
5154
"""
52-
if sys.version_info[0] >= 3:
55+
if IS_PY3:
5356
return self.__unicode__()
5457
else:
5558
return self.__unicode__().encode(

0 commit comments

Comments
 (0)