Skip to content

Commit

Permalink
python 3 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Seward committed Feb 25, 2020
1 parent 979244d commit 438b6c7
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 45 deletions.
12 changes: 6 additions & 6 deletions ims_lti_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Classes
from tool_config import ToolConfig
from tool_consumer import ToolConsumer
from tool_provider import ToolProvider
from outcome_request import OutcomeRequest
from outcome_response import OutcomeResponse
from .tool_config import ToolConfig
from .tool_consumer import ToolConsumer
from .tool_provider import ToolProvider
from .outcome_request import OutcomeRequest
from .outcome_response import OutcomeResponse

# Exceptions
from utils import InvalidLTIConfigError, InvalidLTIRequestError
from .utils import InvalidLTIConfigError, InvalidLTIRequestError
6 changes: 3 additions & 3 deletions ims_lti_py/launch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if py < (2, 6, 0): bytes=str

def touni(s, enc='utf8', err='strict'):
return s.decode(enc, err) if isinstance(s, bytes) else unicode(s)
return s.decode(enc, err) if isinstance(s, bytes) else str(s)


# List of the standard launch parameters for an LTI launch
Expand Down Expand Up @@ -93,7 +93,7 @@ def roles(self, roles_list):
'''
if roles_list and isinstance(roles_list, list):
self.roles = [].extend(roles_list)
elif roles_list and isinstance(roles_list, basestring):
elif roles_list and isinstance(roles_list, str):
self.roles = [role.lower() for role in roles_list.split(',')]

def process_params(self, params):
Expand All @@ -102,7 +102,7 @@ def process_params(self, params):
the LAUNCH_DATA_PARAMETERS list, or that start with 'custom_' or
'ext_'.
'''
for key, val in params.items():
for key, val in list(params.items()):
if key in LAUNCH_DATA_PARAMETERS and val != 'None':
if key == 'roles':
if isinstance(val, list):
Expand Down
10 changes: 5 additions & 5 deletions ims_lti_py/outcome_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import oauth2

from outcome_response import OutcomeResponse
from utils import InvalidLTIConfigError
from .outcome_response import OutcomeResponse
from .utils import InvalidLTIConfigError

REPLACE_REQUEST = 'replaceResult'
DELETE_REQUEST = 'deleteResult'
Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(self, opts=defaultdict(lambda: None)):
setattr(self, accessor, None)

# Store specified options in our accessors
for (key, val) in opts.iteritems():
for (key, val) in opts.items():
setattr(self, key, val)

@staticmethod
Expand Down Expand Up @@ -144,11 +144,11 @@ def post_outcome_request(self):
normalize = http._normalize_headers

def my_normalize(self, headers):
print("My Normalize", headers)
print(("My Normalize", headers))
ret = normalize(self, headers)
if 'authorization' in ret:
ret['Authorization'] = ret.pop('authorization')
print("My Normalize", ret)
print(("My Normalize", ret))
return ret
http._normalize_headers = my_normalize
monkey_patch_function = normalize
Expand Down
2 changes: 1 addition & 1 deletion ims_lti_py/outcome_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, **kwargs):
setattr(self, opt, None)

# Store specified options in our options member
for (key, val) in kwargs.iteritems():
for (key, val) in kwargs.items():
setattr(self, key, val)

@staticmethod
Expand Down
8 changes: 6 additions & 2 deletions ims_lti_py/request_validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import oauth2
import six


class RequestValidatorMixin(object):
Expand Down Expand Up @@ -36,10 +37,13 @@ def is_valid_request(self, request, parameters={},
headers=headers,
parameters=parameters)

if isinstance(oauth_request['oauth_signature'], six.string_types):
oauth_request['oauth_signature'] = oauth_request['oauth_signature'].encode('utf-8')

self.oauth_server.verify_request(
oauth_request, self.oauth_consumer, {})

except oauth2.MissingSignature, e:
except oauth2.MissingSignature as e:
if handle_error:
return False
else:
Expand Down Expand Up @@ -93,7 +97,7 @@ def parse_request(self, request, parameters, fake_method=None):
return (fake_method or request.method,
request.build_absolute_uri(),
request.META,
(dict(request.POST.iteritems())
(dict(iter(request.POST.items()))
if request.method == 'POST'
else parameters))

Expand Down
8 changes: 4 additions & 4 deletions ims_lti_py/tool_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from lxml import etree, objectify

from utils import InvalidLTIConfigError
from .utils import InvalidLTIConfigError

accessors = [
'title',
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, **kwargs):
else defaultdict(lambda: None)

# Iterate over all provided options and save to class instance members
for (key, val) in kwargs.iteritems():
for (key, val) in kwargs.items():
setattr(self, key, val)

@staticmethod
Expand Down Expand Up @@ -170,12 +170,12 @@ def process_xml(self, xml):
self.set_ext_params(platform, properties)

def recursive_options(self,element,params):
for key, val in params.iteritems():
for key, val in params.items():
if isinstance(val, dict):
options_node = etree.SubElement(element,
'{%s}%s' %(NSMAP['lticm'], 'options'), name =
key)
for key, val in val.iteritems():
for key, val in val.items():
self.recursive_options(options_node,{key:val})
else:
param_node = etree.SubElement(element, '{%s}%s'
Expand Down
8 changes: 4 additions & 4 deletions ims_lti_py/tool_consumer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections import defaultdict
from urllib2 import urlparse, unquote
from urllib.parse import unquote

import oauth2
import time

from launch_params import LaunchParamsMixin
from request_validator import RequestValidatorMixin
from utils import InvalidLTIConfigError, generate_identifier
from .launch_params import LaunchParamsMixin
from .request_validator import RequestValidatorMixin
from .utils import InvalidLTIConfigError, generate_identifier

accessors = [
'consumer_key',
Expand Down
12 changes: 6 additions & 6 deletions ims_lti_py/tool_provider.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from launch_params import LaunchParamsMixin
from request_validator import (
from .launch_params import LaunchParamsMixin
from .request_validator import (
RequestValidatorMixin,
FlaskRequestValidatorMixin,
DjangoRequestValidatorMixin,
WebObRequestValidatorMixin
)
from outcome_request import OutcomeRequest
from .outcome_request import OutcomeRequest
from collections import defaultdict
import re
from urllib import urlencode
from urlparse import urlsplit, urlunsplit
from urllib.parse import urlencode
from urllib.parse import urlsplit, urlunsplit

try:
from urlparse import parse_qsl
from urllib.parse import parse_qsl
except ImportError:
# fall back for Python 2.5
from cgi import parse_qsl # NOQA
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
author_email='[email protected]',
url='https://github.com/tophatmonocle/ims_lti_py',
packages=find_packages(),
install_requires=['lxml', 'oauth2'],
install_requires=['lxml', 'oauth2', 'six'],
license='MIT License',
keywords='lti',
zip_safe=True,
Expand Down
20 changes: 10 additions & 10 deletions tests/launch_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def test_process_params(self):
'''
Should process parameters.
'''
for (key, val) in self.params.iteritems():
for (key, val) in self.params.items():
if not 'custom_' in key\
and not 'ext_' in key\
and not 'roles' in key:
self.assertEquals(getattr(self.tool, key), val)
self.assertEqual(getattr(self.tool, key), val)

# Test roles
self.assertTrue(sorted(self.tool.roles) == sorted(['Learner',\
Expand All @@ -21,20 +21,20 @@ def test_custom_extension_parameters(self):
'''
Should handle custom/extension parameters.
'''
self.assertEquals(self.tool.get_custom_param('param1'), 'custom1')
self.assertEquals(self.tool.get_custom_param('param2'), 'custom2')
self.assertEquals(self.tool.get_ext_param('lti_message_type'),
self.assertEqual(self.tool.get_custom_param('param1'), 'custom1')
self.assertEqual(self.tool.get_custom_param('param2'), 'custom2')
self.assertEqual(self.tool.get_ext_param('lti_message_type'),
'extension-lti-launch')
self.tool.set_custom_param('param3', 'custom3')
self.tool.set_ext_param('user_id', 'bob')

params = self.tool.to_params()
self.assertEquals(params['custom_param1'], 'custom1')
self.assertEquals(params['custom_param2'], 'custom2')
self.assertEquals(params['custom_param3'], 'custom3')
self.assertEquals(params['ext_lti_message_type'],
self.assertEqual(params['custom_param1'], 'custom1')
self.assertEqual(params['custom_param2'], 'custom2')
self.assertEqual(params['custom_param3'], 'custom3')
self.assertEqual(params['ext_lti_message_type'],
'extension-lti-launch')
self.assertEquals(params['ext_user_id'], 'bob')
self.assertEqual(params['ext_user_id'], 'bob')

def test_invalid_request(self):
'''
Expand Down
6 changes: 3 additions & 3 deletions tests/tool_consumer_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_url_query_parameters(self):
tc.launch_url = 'http://dr-chuck.com/ims/php-simple/tool.php?a=1&b=2&c=3%20%26a'
result = tc.generate_launch_data()
self.assertNotEqual(result, None)
self.assertEquals(result['oauth_signature'],
self.assertEqual(result['oauth_signature'],
'kiObbrNVu4vHzd0+yVDHvrsvegQ=')
self.assertEquals(result['c'], '3 &a')
self.assertEqual(result['c'], '3 &a')

def test_signature_port(self):
'''
Expand All @@ -35,7 +35,7 @@ def test_url(url, sig):
tc.launch_url = url
ld = tc.generate_launch_data()
self.assertNotEqual(ld, None)
self.assertEquals(ld['oauth_signature'], sig)
self.assertEqual(ld['oauth_signature'], sig)

test_url('http://dr-chuck.com:123/ims/php-simple/tool.php',
'I2zrOsXkLvBMbb5HzRXZrZAQVOg=')
Expand Down

0 comments on commit 438b6c7

Please sign in to comment.