Skip to content

Commit

Permalink
Making python-intercom Python 3 compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeyes committed Mar 26, 2015
1 parent a220358 commit b1bee22
Show file tree
Hide file tree
Showing 29 changed files with 507 additions and 359 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- 2.7
- 3.4
install:
- pip install -r requirements.txt --use-mirrors
- pip install -r dev-requirements.txt --use-mirrors
Expand Down
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#
# Development dependencies.
#
https://github.com/jeffh/describe/tarball/dev#egg=describe-dev
httpretty==0.8.8
mock==1.0.1
5 changes: 3 additions & 2 deletions intercom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import copy
import random
import re
import six
import time

__version__ = '2.0.alpha'
Expand All @@ -47,7 +48,7 @@ class IntercomType(type): # noqa
_endpoints = None
_current_endpoint = None
_target_base_url = None
_endpoint_randomized_at = None
_endpoint_randomized_at = 0

@property
def _auth(self):
Expand Down Expand Up @@ -134,9 +135,9 @@ def endpoint(self, value):
self.endpoints = [value]


@six.add_metaclass(IntercomType)
class Intercom(object):
_class_register = {}
__metaclass__ = IntercomType

@classmethod
def get_url(cls, path):
Expand Down
4 changes: 2 additions & 2 deletions intercom/api_operations/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def create(cls, **params):
return cls(**response)

def from_dict(self, pdict):
for key, value in pdict.items():
for key, value in list(pdict.items()):
setattr(self, key, value)

@property
def to_dict(self):
a_dict = {}
for name in self.__dict__.keys():
for name in list(self.__dict__.keys()):
if name == "changed_attributes":
continue
a_dict[name] = self.__dict__[name] # direct access
Expand Down
13 changes: 7 additions & 6 deletions intercom/collection_proxy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-

import six
from intercom import HttpError


class CollectionProxy(object):
class CollectionProxy(six.Iterator):

def __init__(self, cls, collection, finder_url, finder_params={}):
# needed to create class instances of the resource
Expand All @@ -27,7 +28,7 @@ def __init__(self, cls, collection, finder_url, finder_params={}):
def __iter__(self):
return self

def next(self):
def __next__(self):
if self.resources is None:
# get the first page of results
self.get_first_page()
Expand All @@ -36,18 +37,18 @@ def next(self):
# current resource iterator (StopIteration is raised)
# try to get the next page of results first
try:
resource = self.resources.next()
resource = six.next(self.resources)
except StopIteration:
self.get_next_page()
resource = self.resources.next()
resource = six.next(self.resources)

instance = self.collection_cls(**resource)
return instance

def __getitem__(self, index):
for i in range(index):
self.next()
return self.next()
six.next(self)
return six.next(self)

def get_first_page(self):
# get the first page of results
Expand Down
1 change: 1 addition & 0 deletions intercom/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class IntercomError(Exception):

def __init__(self, message=None, context=None):
super(IntercomError, self).__init__(message)
self.message = message
self.context = context


Expand Down
5 changes: 3 additions & 2 deletions intercom/lib/flat_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import numbers
import six


class FlatStore(dict):
Expand All @@ -11,11 +12,11 @@ def __init__(self, *args, **kwargs):
def __setitem__(self, key, value):
if not (
isinstance(value, numbers.Real) or
isinstance(value, basestring)
isinstance(value, six.string_types)
):
raise ValueError(
"custom data only allows string and real number values")
if not isinstance(key, basestring):
if not isinstance(key, six.string_types):
raise ValueError("custom data only allows string keys")
super(FlatStore, self).__setitem__(key, value)

Expand Down
2 changes: 1 addition & 1 deletion intercom/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def send_request_to_path(cls, method, url, auth, params=None):
@classmethod
def parse_body(cls, resp):
try:
body = json.loads(resp.content)
body = json.loads(resp.content.decode())
except ValueError:
cls.raise_errors_on_failure(resp)
if body.get('type') == 'error.list':
Expand Down
4 changes: 2 additions & 2 deletions intercom/traits/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def from_response(self, response):
return self

def from_dict(self, dict):
for attribute, value in dict.items():
for attribute, value in list(dict.items()):
if type_field(attribute):
continue
setattr(self, attribute, value)

@property
def attributes(self):
res = {}
for name, value in self.__dict__.items():
for name, value in list(self.__dict__.items()):
if self.submittable_attribute(name, value):
res[name] = value
return res
Expand Down
4 changes: 3 additions & 1 deletion intercom/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import inflection
import six


def pluralize(str):
Expand Down Expand Up @@ -46,8 +47,9 @@ def __new__(cls, name, bases, attributes):
return super(Meta, cls).__new__(
cls, str(class_name), bases, attributes)

@six.add_metaclass(Meta)
class DynamicClass(Resource, Load):
__metaclass__ = Meta
pass

dyncls = DynamicClass()
CLASS_REGISTRY[class_name] = dyncls
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
certifi
inflection==0.3.0
requests==2.6.0
urllib3==1.10.2
urllib3==1.10.2
six==1.9.0
1 change: 0 additions & 1 deletion tests/integration/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def teardown_class(cls):
def test_find_by_email(self):
# Find user by email
user = User.find(email=self.email)
print user
self.assertEqual(self.email, user.email)

def test_find_by_user_id(self):
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ def local_response(**params):
def _call(*args, **kwargs):
response = Mock()
reply = {}
for name, value in kwargs.items():
for name, value in list(kwargs.items()):
reply[name] = value
for name, value in params.items():
for name, value in list(params.items()):
reply[name] = value
response.content = json.dumps(reply)
response.status_code = 200
return response
return _call


def test_user(email="[email protected]"):
def a_test_user(email="[email protected]"):
return {
"type": "user",
"id": "aaaaaaaaaaaaaaaaaaaaaaaa",
Expand Down Expand Up @@ -136,7 +136,10 @@ def page_of_users(include_next_link=False):
"per_page": 50,
"total_pages": 7
},
"users": [test_user("[email protected]"), test_user("[email protected]"), test_user("[email protected]")],
"users": [
a_test_user("[email protected]"),
a_test_user("[email protected]"),
a_test_user("[email protected]")],
"total_count": 314
}
if include_next_link:
Expand Down
16 changes: 0 additions & 16 deletions tests/unit/admin_spec.py

This file was deleted.

84 changes: 0 additions & 84 deletions tests/unit/intercom_spec.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
# -*- coding: utf-8 -*-

from describe import expect
import unittest
from intercom.lib.flat_store import FlatStore
from nose.tools import assert_raises
from nose.tools import eq_
from nose.tools import istest


class DescribeIntercomFlatStore:
class IntercomFlatStore(unittest.TestCase):

@istest
def it_raises_if_you_try_to_set_or_merge_in_nested_hash_structures(self):
data = FlatStore()
with expect.raise_error(ValueError):
with assert_raises(ValueError):
data["thing"] = [1]
with expect.raise_error(ValueError):
with assert_raises(ValueError):
data["thing"] = {1: 2}
with expect.raise_error(ValueError):
with assert_raises(ValueError):
FlatStore(**{"1": {2: 3}})

@istest
def it_raises_if_you_try_to_use_a_non_string_key(self):
data = FlatStore()
with expect.raise_error(ValueError):
with assert_raises(ValueError):
data[1] = "something"

@istest
def it_sets_and_merges_valid_entries(self):
data = FlatStore()
data["a"] = 1
data["b"] = 2
expect(data["a"]) == 1
expect(data["b"]) == 2
eq_(data["a"], 1)
eq_(data["b"], 2)
data = FlatStore(a=1, b=2)
expect(data["a"]) == 1
expect(data["b"]) == 2
eq_(data["a"], 1)
eq_(data["b"], 2)
Loading

0 comments on commit b1bee22

Please sign in to comment.