Skip to content

Commit 517f65f

Browse files
SG-38306 Python2 Removal - Part 3 - Cleanup imports (#400)
* six.moves imports * Cleanup BytesIO import from six * simple json * Cleanup Py2-3 compat with ImportError * Simplify Base64 --------- Co-authored-by: Eduardo Chauca <[email protected]>
1 parent d3809cd commit 517f65f

File tree

7 files changed

+42
-63
lines changed

7 files changed

+42
-63
lines changed

docs/cookbook/examples/ami_handler.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ via ``POST``. If you're using a custom protocol the data is sent via ``GET``.
218218
params = params.split("&")
219219
p = {"column_display_names": [], "cols": []}
220220
for arg in params:
221-
key, value = map(six.moves.urllib.parse.unquote, arg.split("=", 1))
221+
key, value = map(urllib.parse.unquote, arg.split("=", 1))
222222
if key == "column_display_names" or key == "cols":
223223
p[key].append(value)
224224
else:

shotgun_api3/lib/mockgun/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
-----------------------------------------------------------------------------
3131
"""
3232

33-
from ..six.moves import cPickle as pickle
3433
import os
34+
import pickle
3535

3636
from .errors import MockgunError
3737

shotgun_api3/shotgun.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
"""
3131

32+
import base64
3233
import copy
3334
import datetime
3435
import json
36+
import http.client # Used for secure file upload
37+
import http.cookiejar # used for attachment upload
38+
import io # used for attachment upload
3539
import logging
3640
import os
3741
import re
@@ -40,29 +44,22 @@
4044
import stat # used for attachment upload
4145
import sys
4246
import time
47+
import urllib.error
48+
import urllib.parse
49+
import urllib.request
4350
import uuid # used for attachment upload
4451

52+
# Import Error and ResponseError (even though they're unused in this file) since they need
53+
# to be exposed as part of the API.
54+
from xmlrpc.client import Error, ProtocolError, ResponseError # noqa
55+
4556
# Python 2/3 compatibility
4657
from .lib import six
4758
from .lib import sgsix
4859
from .lib import sgutils
49-
from .lib.six import BytesIO # used for attachment upload
50-
from .lib.six.moves import map
51-
from .lib.six.moves import http_cookiejar # used for attachment upload
52-
from .lib.six.moves import urllib
53-
from .lib.six.moves import http_client # Used for secure file upload.
5460
from .lib.httplib2 import Http, ProxyInfo, socks, ssl_error_classes
5561
from .lib.sgtimezone import SgTimezone
5662

57-
# Import Error and ResponseError (even though they're unused in this file) since they need
58-
# to be exposed as part of the API.
59-
from .lib.six.moves.xmlrpc_client import Error, ProtocolError, ResponseError # noqa
60-
61-
if six.PY3:
62-
from base64 import encodebytes as base64encode
63-
else:
64-
from base64 import encodestring as base64encode
65-
6663

6764
LOG = logging.getLogger("shotgun_api3")
6865
"""
@@ -708,13 +705,13 @@ def __init__(
708705
# and auth header
709706

710707
# Do NOT self._split_url(self.base_url) here, as it contains the lower
711-
# case version of the base_url argument. Doing so would base64encode
708+
# case version of the base_url argument. Doing so would base64.encodebytes
712709
# the lowercase version of the credentials.
713710
auth, self.config.server = self._split_url(base_url)
714711
if auth:
715-
auth = base64encode(urllib.parse.unquote(auth).encode("utf-8")).decode(
716-
"utf-8"
717-
)
712+
auth = base64.encodebytes(
713+
urllib.parse.unquote(auth).encode("utf-8")
714+
).decode("utf-8")
718715
self.config.authorization = "Basic " + auth.strip()
719716

720717
# foo:[email protected]:3456
@@ -3003,8 +3000,8 @@ def get_auth_cookie_handler(self):
30033000
This is used internally for downloading attachments from FPTR.
30043001
"""
30053002
sid = self.get_session_token()
3006-
cj = http_cookiejar.LWPCookieJar()
3007-
c = http_cookiejar.Cookie(
3003+
cj = http.cookiejar.LWPCookieJar()
3004+
c = http.cookiejar.Cookie(
30083005
"0",
30093006
"_session_id",
30103007
sid,
@@ -4432,7 +4429,7 @@ def _multipart_upload_file_to_storage(self, path, upload_info):
44324429
data_size = len(data)
44334430
# keep data as a stream so that we don't need to worry how it was
44344431
# encoded.
4435-
data = BytesIO(data)
4432+
data = io.BytesIO(data)
44364433
bytes_read += data_size
44374434
part_url = self._get_upload_part_link(
44384435
upload_info, filename, part_number
@@ -4662,13 +4659,13 @@ def _send_form(self, url, params):
46624659
raise ShotgunError("Max attemps limit reached.")
46634660

46644661

4665-
class CACertsHTTPSConnection(http_client.HTTPConnection):
4662+
class CACertsHTTPSConnection(http.client.HTTPConnection):
46664663
""" "
46674664
This class allows to create an HTTPS connection that uses the custom certificates
46684665
passed in.
46694666
"""
46704667

4671-
default_port = http_client.HTTPS_PORT
4668+
default_port = http.client.HTTPS_PORT
46724669

46734670
def __init__(self, *args, **kwargs):
46744671
"""
@@ -4760,7 +4757,7 @@ def encode(self, params, files, boundary=None, buffer=None):
47604757
# We'll do this across both python 2/3 rather than add more branching.
47614758
boundary = uuid.uuid4()
47624759
if buffer is None:
4763-
buffer = BytesIO()
4760+
buffer = io.BytesIO()
47644761
for key, value in params:
47654762
if isinstance(key, bytes):
47664763
key = key.decode("utf-8")

tests/base.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
11
"""Base class for Flow Production Tracking API tests."""
22

3+
import configparser
34
import contextlib
5+
import json
46
import os
57
import random
68
import re
79
import time
810
import unittest
11+
import urllib.error
912

1013
from . import mock
1114

1215
import shotgun_api3 as api
13-
from shotgun_api3.shotgun import json
1416
from shotgun_api3.shotgun import ServerCapabilities
1517
from shotgun_api3.lib import six
16-
from shotgun_api3.lib.six.moves import urllib
17-
from shotgun_api3.lib.six.moves.configparser import ConfigParser
18-
19-
try:
20-
# Attempt to import skip from unittest. Since this was added in Python 2.7
21-
# in the case that we're running on Python 2.6 we'll need a decorator to
22-
# provide some equivalent functionality.
23-
from unittest import skip
24-
except ImportError:
25-
# On Python 2.6 we'll just have to ignore tests that are skipped -- we won't
26-
# mark them as skipped, but we will not fail on them.
27-
def skip(f):
28-
return lambda self: None
2918

3019

3120
THUMBNAIL_MAX_ATTEMPTS = 30
@@ -456,7 +445,7 @@ def config_keys(self):
456445
]
457446

458447
def read_config(self, config_path):
459-
config_parser = ConfigParser()
448+
config_parser = configparser.ConfigParser()
460449
config_parser.read(config_path)
461450
for section in config_parser.sections():
462451
for option in config_parser.options(section):

tests/test_api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import time
2323
import types
2424
import unittest
25+
import urllib.parse
26+
import urllib.request
27+
import urllib.error
2528
import uuid
2629
import warnings
2730

@@ -33,8 +36,6 @@
3336
# class for the current Python version.
3437
from shotgun_api3.lib.sgsix import ShotgunSSLError
3538

36-
from shotgun_api3.lib.six.moves import range, urllib
37-
3839
import shotgun_api3
3940

4041
from . import base
@@ -629,7 +630,7 @@ def test_linked_thumbnail_url(self):
629630

630631
# For now skip tests that are erroneously failling on some sites to
631632
# allow CI to pass until the known issue causing this is resolved.
632-
@base.skip("Skipping test that erroneously fails on some sites.")
633+
@unittest.skip("Skipping test that erroneously fails on some sites.")
633634
def test_share_thumbnail(self):
634635
"""share thumbnail between two entities"""
635636

@@ -2899,7 +2900,7 @@ def _check_attachment(self, data, attachment_id, additional_fields):
28992900

29002901
# For now skip tests that are erroneously failling on some sites to
29012902
# allow CI to pass until the known issue causing this is resolved.
2902-
@base.skip("Skipping test that erroneously fails on some sites.")
2903+
@unittest.skip("Skipping test that erroneously fails on some sites.")
29032904
def test_simple(self):
29042905
"""
29052906
Test note reply thread API call
@@ -2978,7 +2979,7 @@ def test_simple(self):
29782979

29792980
# For now skip tests that are erroneously failling on some sites to
29802981
# allow CI to pass until the known issue causing this is resolved.
2981-
@base.skip("Skipping test that erroneously fails on some sites.")
2982+
@unittest.skip("Skipping test that erroneously fails on some sites.")
29822983
def test_complex(self):
29832984
"""
29842985
Test note reply thread API call with additional params

tests/test_client.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,34 @@
1212
CRUD functions. These tests always use a mock http connection so not not
1313
need a live server to run against."""
1414

15+
import configparser
16+
import base64
1517
import datetime
18+
import json
1619
import os
1720
import platform
1821
import re
1922
import sys
2023
import time
2124
import unittest
25+
import urllib.parse
26+
import urllib.error
2227

23-
from shotgun_api3.lib.six.moves import urllib
2428
from shotgun_api3.lib import six, sgutils
2529

26-
try:
27-
import simplejson as json
28-
except ImportError:
29-
try:
30-
import json as json
31-
except ImportError:
32-
import shotgun_api3.lib.simplejson as json
33-
3430
from . import mock
3531

3632
import shotgun_api3.lib.httplib2 as httplib2
3733
import shotgun_api3 as api
3834
from shotgun_api3.shotgun import ServerCapabilities, SG_TIMEZONE
3935
from . import base
4036

41-
if six.PY3:
42-
from base64 import encodebytes as base64encode
43-
else:
44-
from base64 import encodestring as base64encode
45-
4637

4738
def b64encode(val):
4839
if isinstance(val, str):
4940
val = val.encode("utf-8")
5041

51-
return base64encode(val).decode("utf-8")
42+
return base64.encodebytes(val).decode("utf-8")
5243

5344

5445
class TestShotgunClient(base.MockTestBase):
@@ -190,7 +181,7 @@ def test_read_config(self):
190181
"""Validate that config values are properly coerced."""
191182
this_dir = os.path.dirname(os.path.realpath(__file__))
192183
config_path = os.path.join(this_dir, "test_config_file")
193-
config = base.ConfigParser()
184+
config = configparser.ConfigParser()
194185
config.read(config_path)
195186
result = config.get("SERVER_INFO", "api_key")
196187
expected = "%abce"

tests/test_unit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
import os
1414
import unittest
1515
from unittest import mock
16+
import urllib.request
17+
import urllib.error
1618

1719
from .mock import patch
1820
import shotgun_api3 as api
1921
from shotgun_api3.shotgun import _is_mimetypes_broken
20-
from shotgun_api3.lib.six.moves import range, urllib
2122
from shotgun_api3.lib.httplib2 import Http, ssl_error_classes
2223

2324

0 commit comments

Comments
 (0)