Skip to content

Commit 8dd5dae

Browse files
committed
Add basic Python 3 compatiblity fixing imports #280
Signed-off-by: Thomas Druez <[email protected]>
1 parent 5189bda commit 8dd5dae

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/attributecode/api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717
from __future__ import absolute_import
1818
from __future__ import print_function
1919

20-
import httplib
2120
import json
2221
import urllib
23-
import urllib2
24-
2522
from collections import namedtuple
2623

24+
try:
25+
import httplib # Python 2
26+
except ImportError:
27+
import http.client as httplib # Python 3
28+
29+
try:
30+
import urllib2 # Python 2
31+
except ImportError:
32+
import urllib as urllib2 # Python 3
33+
2734
from attributecode import ERROR
2835
from attributecode import Error
2936

src/attributecode/model.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@
3131
import os
3232
import posixpath
3333
import re
34-
import urllib2
3534
import unicodecsv
36-
3735
from collections import OrderedDict
3836
from posixpath import dirname
39-
from urlparse import urljoin, urlparse
37+
38+
try:
39+
import urllib2 # Python 2
40+
except ImportError:
41+
import urllib as urllib2 # Python 3
42+
43+
try:
44+
from urlparse import urljoin, urlparse # Python 2
45+
except ImportError:
46+
from urllib.parse import urljoin, urlparse # Python 3
47+
48+
try:
49+
basestring # Python 2
50+
except NameError:
51+
basestring = str # Python 3
4052

4153
from attributecode import CRITICAL
4254
from attributecode import ERROR
@@ -757,7 +769,7 @@ def all_fields(self, with_absent=True, with_empty=True):
757769
If with_empty, include empty fields.
758770
"""
759771
all_fields = []
760-
for field in self.fields.values() + self.custom_fields.values():
772+
for field in list(self.fields.values()) + list(self.custom_fields.values()):
761773
if field.required:
762774
all_fields.append(field)
763775
# TODO: The following code need refactor

src/attributecode/saneyaml.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
from yaml import SafeLoader
3030
from yaml import SafeDumper
3131

32+
try:
33+
unicode # Python 2
34+
except NameError:
35+
unicode = str # Python 3
36+
3237

3338
"""
3439
Wrapper around PyYAML to provide sane defaults ensuring that dump/load does

src/attributecode/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import codecs
2020
import errno
21-
import httplib
2221
import json
2322
import ntpath
2423
import os
@@ -28,12 +27,16 @@
2827
import string
2928
import sys
3029
import unicodecsv
31-
3230
from collections import OrderedDict
3331
from os.path import abspath
3432
from os.path import dirname
3533
from os.path import join
3634

35+
try:
36+
import httplib # Python 2
37+
except ImportError:
38+
import http.client as httplib # Python 3
39+
3740
from attributecode import CRITICAL, Error
3841

3942

0 commit comments

Comments
 (0)