Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cookbook/examples/ami_handler.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ via ``POST``. If you're using a custom protocol the data is sent via ``GET``.
params = params.split("&")
p = {"column_display_names": [], "cols": []}
for arg in params:
key, value = map(six.moves.urllib.parse.unquote, arg.split("=", 1))
key, value = map(urllib.parse.unquote, arg.split("=", 1))
if key == "column_display_names" or key == "cols":
p[key].append(value)
else:
Expand Down
3 changes: 0 additions & 3 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,3 @@ Example for a user whose language preference is set to Japanese:
},
...
}

.. note::
If needed, the encoding of the returned localized string can be ensured regardless the Python version using shotgun_api3.lib.six.ensure_text().
2 changes: 1 addition & 1 deletion shotgun_api3/lib/mockgun/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
-----------------------------------------------------------------------------
"""

from ..six.moves import cPickle as pickle
import os
import pickle

from .errors import MockgunError

Expand Down
135 changes: 69 additions & 66 deletions shotgun_api3/shotgun.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,36 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

# Python 2/3 compatibility
from .lib import six
from .lib import sgsix
from .lib import sgutils
from .lib.six import BytesIO # used for attachment upload
from .lib.six.moves import map

from .lib.six.moves import http_cookiejar # used for attachment upload
import base64
import copy
import datetime
import json
import http.client # Used for secure file upload
import http.cookiejar # used for attachment upload
import io # used for attachment upload
import logging
import uuid # used for attachment upload
import os
import re
import copy
import shutil # used for attachment download
import ssl
import stat # used for attachment upload
import sys
import time
import json
from .lib.six.moves import urllib
import shutil # used for attachment download
from .lib.six.moves import http_client # Used for secure file upload.
from .lib.httplib2 import Http, ProxyInfo, socks, ssl_error_classes
from .lib.sgtimezone import SgTimezone
import urllib.error
import urllib.parse
import urllib.request
import uuid # used for attachment upload

# Import Error and ResponseError (even though they're unused in this file) since they need
# to be exposed as part of the API.
from .lib.six.moves.xmlrpc_client import Error, ProtocolError, ResponseError # noqa
from xmlrpc.client import Error, ProtocolError, ResponseError # noqa

if six.PY3:
from base64 import encodebytes as base64encode
else:
from base64 import encodestring as base64encode
# Python 2/3 compatibility
from .lib import six
from .lib import sgsix
from .lib import sgutils
from .lib.httplib2 import Http, ProxyInfo, socks, ssl_error_classes
from .lib.sgtimezone import SgTimezone


LOG = logging.getLogger("shotgun_api3")
Expand Down Expand Up @@ -708,12 +705,12 @@ def __init__(
# and auth header

# Do NOT self._split_url(self.base_url) here, as it contains the lower
# case version of the base_url argument. Doing so would base64encode
# case version of the base_url argument. Doing so would base64.encodebytes
# the lowercase version of the credentials.
auth, self.config.server = self._split_url(base_url)
if auth:
auth = base64encode(
sgutils.ensure_binary(urllib.parse.unquote(auth))
auth = base64.encodebytes(
urllib.parse.unquote(auth).encode("utf-8")
).decode("utf-8")
self.config.authorization = "Basic " + auth.strip()

Expand Down Expand Up @@ -2270,8 +2267,7 @@ def schema_field_update(
"type": entity_type,
"field_name": field_name,
"properties": [
{"property_name": k, "value": v}
for k, v in six.iteritems((properties or {}))
{"property_name": k, "value": v} for k, v in (properties or {}).items()
],
}
params = self._add_project_param(params, project_entity)
Expand Down Expand Up @@ -2966,7 +2962,11 @@ def download_attachment(self, attachment=False, file_path=None, attachment_id=No
url.find("s3.amazonaws.com") != -1
and e.headers["content-type"] == "application/xml"
):
body = [sgutils.ensure_text(line) for line in e.readlines()]
body = [
line.decode("utf-8") if isinstance(line, bytes) else line
for line in e.readlines()
]

if body:
xml = "".join(body)
# Once python 2.4 support is not needed we can think about using
Expand Down Expand Up @@ -3000,8 +3000,8 @@ def get_auth_cookie_handler(self):
This is used internally for downloading attachments from FPTR.
"""
sid = self.get_session_token()
cj = http_cookiejar.LWPCookieJar()
c = http_cookiejar.Cookie(
cj = http.cookiejar.LWPCookieJar()
c = http.cookiejar.Cookie(
"0",
"_session_id",
sid,
Expand Down Expand Up @@ -3328,7 +3328,7 @@ def text_search(self, text, entity_types, project_ids=None, limit=None):
raise ValueError("entity_types parameter must be a dictionary")

api_entity_types = {}
for entity_type, filter_list in six.iteritems(entity_types):
for entity_type, filter_list in entity_types.items():

if isinstance(filter_list, (list, tuple)):
resolved_filters = _translate_filters(filter_list, filter_operator=None)
Expand Down Expand Up @@ -3859,8 +3859,7 @@ def _encode_payload(self, payload):
be in a single byte encoding to go over the wire.
"""

wire = json.dumps(payload, ensure_ascii=False)
return sgutils.ensure_binary(wire)
return json.dumps(payload, ensure_ascii=False).encode("utf-8")

def _make_call(self, verb, path, body, headers):
"""
Expand Down Expand Up @@ -3965,7 +3964,7 @@ def _http_request(self, verb, path, body, headers):
resp, content = conn.request(url, method=verb, body=body, headers=headers)
# http response code is handled else where
http_status = (resp.status, resp.reason)
resp_headers = dict((k.lower(), v) for k, v in six.iteritems(resp))
resp_headers = dict((k.lower(), v) for k, v in resp.items())
resp_body = content

LOG.debug("Response status is %s %s" % http_status)
Expand Down Expand Up @@ -4045,7 +4044,7 @@ def _decode_list(lst):

def _decode_dict(dct):
newdict = {}
for k, v in six.iteritems(dct):
for k, v in dct.items():
if isinstance(k, str):
k = sgutils.ensure_str(k)
if isinstance(v, str):
Expand Down Expand Up @@ -4119,7 +4118,7 @@ def _visit_data(self, data, visitor):
return tuple(recursive(i, visitor) for i in data)

if isinstance(data, dict):
return dict((k, recursive(v, visitor)) for k, v in six.iteritems(data))
return dict((k, recursive(v, visitor)) for k, v in data.items())

return visitor(data)

Expand Down Expand Up @@ -4166,10 +4165,6 @@ def _outbound_visitor(value):
value = _change_tz(value)
return value.strftime("%Y-%m-%dT%H:%M:%SZ")

# ensure return is six.text_type
if isinstance(value, str):
return sgutils.ensure_text(value)

return value

return self._visit_data(data, _outbound_visitor)
Expand Down Expand Up @@ -4288,7 +4283,7 @@ def _parse_records(self, records):
continue

# iterate over each item and check each field for possible injection
for k, v in six.iteritems(rec):
for k, v in rec.items():
if not v:
continue

Expand Down Expand Up @@ -4376,7 +4371,7 @@ def _dict_to_list(
[{'field_name': 'foo', 'value': 'bar', 'thing1': 'value1'}]
"""
ret = []
for k, v in six.iteritems((d or {})):
for k, v in (d or {}).items():
d = {key_name: k, value_name: v}
d.update((extra_data or {}).get(k, {}))
ret.append(d)
Expand All @@ -4389,7 +4384,7 @@ def _dict_to_extra_data(self, d, key_name="value"):

e.g. d {'foo' : 'bar'} changed to {'foo': {"value": 'bar'}]
"""
return dict([(k, {key_name: v}) for (k, v) in six.iteritems((d or {}))])
return dict([(k, {key_name: v}) for (k, v) in (d or {}).items()])

def _upload_file_to_storage(self, path, storage_url):
"""
Expand Down Expand Up @@ -4435,7 +4430,7 @@ def _multipart_upload_file_to_storage(self, path, upload_info):
data_size = len(data)
# keep data as a stream so that we don't need to worry how it was
# encoded.
data = BytesIO(data)
data = io.BytesIO(data)
bytes_read += data_size
part_url = self._get_upload_part_link(
upload_info, filename, part_number
Expand Down Expand Up @@ -4657,18 +4652,21 @@ def _send_form(self, url, params):
else:
raise ShotgunError("Unanticipated error occurred %s" % (e))

return sgutils.ensure_text(result)
if isinstance(result, bytes):
result = result.decode("utf-8")

return result
else:
raise ShotgunError("Max attemps limit reached.")


class CACertsHTTPSConnection(http_client.HTTPConnection):
class CACertsHTTPSConnection(http.client.HTTPConnection):
""" "
This class allows to create an HTTPS connection that uses the custom certificates
passed in.
"""

default_port = http_client.HTTPS_PORT
default_port = http.client.HTTPS_PORT

def __init__(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -4738,9 +4736,8 @@ def http_request(self, request):
else:
params.append((key, value))
if not files:
data = sgutils.ensure_binary(
urllib.parse.urlencode(params, True)
) # sequencing on
data = urllib.parse.urlencode(params, True).encode("utf-8")
# sequencing on
else:
boundary, data = self.encode(params, files)
content_type = "multipart/form-data; boundary=%s" % boundary
Expand All @@ -4761,44 +4758,50 @@ def encode(self, params, files, boundary=None, buffer=None):
# We'll do this across both python 2/3 rather than add more branching.
boundary = uuid.uuid4()
if buffer is None:
buffer = BytesIO()
buffer = io.BytesIO()
for key, value in params:
if not isinstance(value, str):
if isinstance(key, bytes):
key = key.decode("utf-8")

if isinstance(value, bytes):
value = value.decode("utf-8")
elif not isinstance(value, str):
# If value is not a string (e.g. int) cast to text
value = str(value)
value = sgutils.ensure_text(value)
key = sgutils.ensure_text(key)

buffer.write(sgutils.ensure_binary("--%s\r\n" % boundary))
buffer.write(f"--{boundary}\r\n".encode("utf-8"))
buffer.write(
sgutils.ensure_binary('Content-Disposition: form-data; name="%s"' % key)
f'Content-Disposition: form-data; name="{key}"'.encode("utf-8")
)
buffer.write(sgutils.ensure_binary("\r\n\r\n%s\r\n" % value))
buffer.write(f"\r\n\r\n{value}\r\n".encode("utf-8"))
for key, fd in files:
# On Windows, it's possible that we were forced to open a file
# with non-ascii characters as unicode. In that case, we need to
# encode it as a utf-8 string to remove unicode from the equation.
# If we don't, the mix of unicode and strings going into the
# buffer can cause UnicodeEncodeErrors to be raised.
filename = fd.name
filename = sgutils.ensure_text(filename)
filename = (
fd.name.decode("utf-8") if isinstance(fd.name, bytes) else fd.name
)
filename = filename.split("/")[-1]
key = sgutils.ensure_text(key)
if isinstance(key, bytes):
key = key.decode("utf-8")

content_type = mimetypes.guess_type(filename)[0]
content_type = content_type or "application/octet-stream"
file_size = os.fstat(fd.fileno())[stat.ST_SIZE]
buffer.write(sgutils.ensure_binary("--%s\r\n" % boundary))
buffer.write(f"--{boundary}\r\n".encode("utf-8"))
c_dis = 'Content-Disposition: form-data; name="%s"; filename="%s"%s'
content_disposition = c_dis % (key, filename, "\r\n")
buffer.write(sgutils.ensure_binary(content_disposition))
buffer.write(sgutils.ensure_binary("Content-Type: %s\r\n" % content_type))
buffer.write(sgutils.ensure_binary("Content-Length: %s\r\n" % file_size))
buffer.write(content_disposition.encode("utf-8"))
buffer.write(f"Content-Type: {content_type}\r\n".encode("utf-8"))
buffer.write(f"Content-Length: {file_size}\r\n".encode("utf-8"))

buffer.write(sgutils.ensure_binary("\r\n"))
buffer.write(b"\r\n")
fd.seek(0)
shutil.copyfileobj(fd, buffer)
buffer.write(sgutils.ensure_binary("\r\n"))
buffer.write(sgutils.ensure_binary("--%s--\r\n\r\n" % boundary))
buffer.write(b"\r\n")
buffer.write(f"--{boundary}--\r\n\r\n".encode("utf-8"))
buffer = buffer.getvalue()
return boundary, buffer

Expand Down
Loading
Loading