From 93516f11ed87d35799512114fe885e613915306b Mon Sep 17 00:00:00 2001 From: coagulant Date: Fri, 21 Dec 2012 22:18:26 +0400 Subject: [PATCH 1/5] Python 3 support, dropped python 2.5 support Minor fixes for pep8 sake, removed deprecated assert_ --- embedly/client.py | 32 +++++----- embedly/models.py | 9 ++- embedly/tests.py | 145 +++++++++++++++++++++++----------------------- setup.py | 4 +- tox.ini | 4 ++ 5 files changed, 100 insertions(+), 94 deletions(-) create mode 100644 tox.ini diff --git a/embedly/client.py b/embedly/client.py index 3d0ea4c..9cc8056 100644 --- a/embedly/client.py +++ b/embedly/client.py @@ -5,18 +5,20 @@ The embedly object that interacts with the service """ import re -import urllib import httplib2 - +import json try: - import json + from urllib import quote, urlencode except ImportError: - import simplejson as json + # py3k + from urllib.parse import quote, urlencode + from models import Url USER_AGENT = 'Mozilla/5.0 (compatible; embedly-python/0.3;)' + class Embedly(object): """ Client @@ -97,8 +99,8 @@ def _get(self, version, method, url_or_urls, **kwargs): # Throw an error early for too many URLs if multi and len(url_or_urls) > 20: - raise ValueError('Embedly accepts only 20 urls at a time. Url ' \ - 'Count:%s' % len(url_or_urls)) + raise ValueError('Embedly accepts only 20 urls at a time. Url ' + 'Count:%s' % len(url_or_urls)) query = '' @@ -106,34 +108,34 @@ def _get(self, version, method, url_or_urls, **kwargs): #make sure that a key was set on the client or passed in. if not key: - raise ValueError('Requires a key. None given: %s' % (key)) + raise ValueError('Requires a key. None given: %s' % key) kwargs['key'] = key - query += urllib.urlencode(kwargs) + query += urlencode(kwargs) if multi: - query += '&urls=%s&' % ','.join([urllib.quote(url) for url in url_or_urls]) + query += '&urls=%s&' % ','.join([quote(url) for url in url_or_urls]) else: - query += '&url=%s' % urllib.quote(url_or_urls) + query += '&url=%s' % quote(url_or_urls) url = 'http://api.embed.ly/%s/%s?%s' % (version, method, query) http = httplib2.Http(timeout=self.timeout) - headers = {'User-Agent' : self.user_agent} + headers = {'User-Agent': self.user_agent} resp, content = http.request(url, headers=headers) if resp['status'] == '200': - data = json.loads(content) + data = json.loads(content.decode('utf-8')) if kwargs.get('raw', False): data['raw'] = content else: - data = {'type' : 'error', - 'error' : True, - 'error_code' : int(resp['status'])} + data = {'type': 'error', + 'error': True, + 'error_code': int(resp['status'])} if multi: return map(lambda url, data: Url(data, method, url), diff --git a/embedly/models.py b/embedly/models.py index 327f1db..008bd43 100644 --- a/embedly/models.py +++ b/embedly/models.py @@ -3,6 +3,7 @@ Creates a sudo model class that makes it easy to access attributes """ +from __future__ import unicode_literals class AttrDict(object): """ UserDict is a pain in the ass. Let's just make our own. @@ -30,11 +31,8 @@ def __getattr__(self, name): return object.__getattr__(self, name) try: return self.data[name] - except KeyError, e: + except KeyError as e: return None - raise AttributeError( - "%s instance has no attribute '%s'" % (self.__class__.__name__, - name)) def __setattr__(self, name, value): if name in ['data', 'method']: @@ -55,6 +53,7 @@ def items(self): return self.data.items() def dict(self): return self.data + class Url(AttrDict): def __init__(self, data=None, method=None, original_url=None): @@ -68,7 +67,7 @@ def __str__(self): return self.__unicode__().encode("utf-8") def __unicode__(self): - r = u'<%s ' % self.method.title() + r = '<%s ' % self.method.title() if self.original_url: r += self.original_url diff --git a/embedly/tests.py b/embedly/tests.py index f6bd93a..42df28e 100644 --- a/embedly/tests.py +++ b/embedly/tests.py @@ -1,5 +1,4 @@ - -import os +from __future__ import unicode_literals import unittest from embedly.client import Embedly @@ -8,131 +7,131 @@ class EmbedlyTestCase(unittest.TestCase): def __init__(self, *args, **kwargs): - self.key = 'internal' # os.environ['EMBEDLY_API_KEY'] + self.key = 'internal' if not self.key: - raise ValueError('Set envirnomental varible EMBEDLY_API_KEY '+\ - 'before running these tests like so: $ export '+\ + raise ValueError('Set envirnomental varible EMBEDLY_API_KEY ' + + 'before running these tests like so: $ export ' + 'EMBEDLY_API_KEY=key') super(EmbedlyTestCase, self).__init__(*args, **kwargs) def test_model(self): data = { - u'provider_url': u'http://www.google.com/', - u'safe': True, - u'description': u'Google', - u'original_url': u'http://google.com/', - u'url': u'http://www.google.com/', - u'type': u'html', - u'object': {}, - u'provider_display': u'www.google.com', - u'author_name': None, - u'favicon_url': u'http://www.google.com/favicon.ico', - u'place': {}, - u'author_url': None, - u'images': [ - {u'url': u'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', - u'width': 275, - u'height': 95}], - u'title': u'Google', - u'provider_name': u'Google', - u'cache_age': 86400, - u'embeds': [] + 'provider_url': 'http://www.google.com/', + 'safe': True, + 'description': 'Google', + 'original_url': 'http://google.com/', + 'url': 'http://www.google.com/', + 'type': 'html', + 'object': {}, + 'provider_display': 'www.google.com', + 'author_name': None, + 'favicon_url': 'http://www.google.com/favicon.ico', + 'place': {}, + 'author_url': None, + 'images': [ + {'url': 'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', + 'width': 275, + 'height': 95}], + 'title': 'Google', + 'provider_name': 'Google', + 'cache_age': 86400, + 'embeds': [] } obj = Url(data, 'preview', 'http://google.com/') - self.assert_(len(obj) is 17) - self.assert_(len(obj.values()) is 17) - self.assert_(len(obj.keys()) is 17) - self.assert_(len(obj.items()) is 17) + self.assertTrue(len(obj) is 17) + self.assertTrue(len(obj.values()) is 17) + self.assertTrue(len(obj.keys()) is 17) + self.assertTrue(len(obj.items()) is 17) - self.assert_('type' in obj.keys()) - self.assert_('html' in obj.values()) + self.assertTrue('type' in obj.keys()) + self.assertTrue('html' in obj.values()) #Get the object - self.assert_(obj.type == 'html') - self.assert_(obj['type'] == 'html') - self.assert_(obj.get('type') == 'html') + self.assertTrue(obj.type == 'html') + self.assertTrue(obj['type'] == 'html') + self.assertTrue(obj.get('type') == 'html') #nope - self.assert_(obj.nothing is None) + self.assertTrue(obj.nothing is None) obj.nothing = 'something' - self.assert_(obj.nothing == 'something') + self.assertTrue(obj.nothing == 'something') obj['nothing'] = 'maybe' - self.assert_(obj['nothing'] == 'maybe') + self.assertTrue(obj['nothing'] == 'maybe') del obj['nothing'] - self.assert_(obj.nothing is None) + self.assertTrue(obj.nothing is None) #Deep Get attrs - self.assert_(obj.images[0].width is 275) - self.assert_(obj.images[0].nothing is None) - self.assert_(obj.object.type is None) + self.assertTrue(obj.images[0].width is 275) + self.assertTrue(obj.images[0].nothing is None) + self.assertTrue(obj.object.type is None) def test_provider(self): http = Embedly(self.key) obj = http.oembed('http://www.scribd.com/doc/13994900/Easter') - self.assert_(obj.provider_url == 'http://www.scribd.com/') + self.assertTrue(obj.provider_url == 'http://www.scribd.com/') obj = http.oembed('http://www.scribd.com/doc/28452730/Easter-Cards') - self.assert_(obj.provider_url == 'http://www.scribd.com/') + self.assertTrue(obj.provider_url == 'http://www.scribd.com/') obj = http.oembed('http://www.youtube.com/watch?v=Zk7dDekYej0') - self.assert_(obj.provider_url == 'http://www.youtube.com/') + self.assertTrue(obj.provider_url == 'http://www.youtube.com/') obj = http.oembed('http://yfrog.com/h22eu4j') - self.assert_(obj.provider_url == 'http://yfrog.com') + self.assertTrue(obj.provider_url == 'http://yfrog.com') def test_providers(self): http = Embedly(self.key) - objs = http.oembed(['http://www.scribd.com/doc/13994900/Easter', - 'http://www.scribd.com/doc/28452730/Easter-Cards']) - self.assert_(objs[0].provider_url == 'http://www.scribd.com/') - self.assert_(objs[1].provider_url == 'http://www.scribd.com/') + objs = list(http.oembed(['http://www.scribd.com/doc/13994900/Easter', + 'http://www.scribd.com/doc/28452730/Easter-Cards'])) + self.assertTrue(objs[0].provider_url == 'http://www.scribd.com/') + self.assertTrue(objs[1].provider_url == 'http://www.scribd.com/') - objs = http.oembed(['http://www.youtube.com/watch?v=Zk7dDekYej0', - 'http://yfrog.com/h22eu4']) - self.assert_(objs[0].provider_url == 'http://www.youtube.com/') - self.assert_(objs[1].provider_url == 'http://yfrog.com') + objs = list(http.oembed(['http://www.youtube.com/watch?v=Zk7dDekYej0', + 'http://yfrog.com/h22eu4'])) + self.assertTrue(objs[0].provider_url == 'http://www.youtube.com/') + self.assertTrue(objs[1].provider_url == 'http://yfrog.com') def test_error(self): http = Embedly(self.key) obj = http.oembed('http://www.embedly.com/this/is/a/bad/url') - self.assert_(obj.error is True, obj.dict) + self.assertTrue(obj.error is True, obj.dict) obj = http.oembed('http://blog.embed.ly/lsbsdlfldsf/asdfkljlas/klajsdlfkasdf') - self.assert_(obj.error is True, obj.dict) + self.assertTrue(obj.error is True, obj.dict) obj = http.oembed('http://twitpic/nothing/to/see/here') - self.assert_(obj.error is True, obj.dict) + self.assertTrue(obj.error is True, obj.dict) def test_multi_errors(self): http = Embedly(self.key) - objs = http.oembed(['http://www.embedly.com/this/is/a/bad/url', - 'http://blog.embed.ly/alsd/slsdlf/asdlfj']) - self.assert_(objs[0].type == 'error', objs[0].dict) - self.assert_(objs[1].type == 'error', objs[1].dict) + objs = list(http.oembed(['http://www.embedly.com/this/is/a/bad/url', + 'http://blog.embed.ly/alsd/slsdlf/asdlfj'])) + self.assertTrue(objs[0].type == 'error', objs[0].dict) + self.assertTrue(objs[1].type == 'error', objs[1].dict) - objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', - 'http://twitpic.com/nothing/to/see/here']) - self.assert_(objs[0].type == 'error',objs[0].dict) - self.assert_(objs[1].type == 'error',objs[1].dict) + objs = list(http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', + 'http://twitpic.com/nothing/to/see/here'])) + self.assertTrue(objs[0].type == 'error',objs[0].dict) + self.assertTrue(objs[1].type == 'error',objs[1].dict) - objs = http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', - 'http://yfrog.com/h22eu4j']) - self.assert_(objs[0].type == 'error',objs[0].dict) - self.assert_(objs[1].type == 'photo',objs[1].dict) + objs = list(http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', + 'http://yfrog.com/h22eu4j'])) + self.assertTrue(objs[0].type == 'error',objs[0].dict) + self.assertTrue(objs[1].type == 'photo',objs[1].dict) - objs = http.oembed(['http://yfrog.com/h22eu4j', - 'http://www.scribd.com/asdf/asdf/asdfasdf']) - self.assert_(objs[0].type == 'photo',objs[0].dict) - self.assert_(objs[1].type == 'error',objs[1].dict) + objs = list(http.oembed(['http://yfrog.com/h22eu4j', + 'http://www.scribd.com/asdf/asdf/asdfasdf'])) + self.assertTrue(objs[0].type == 'photo',objs[0].dict) + self.assertTrue(objs[1].type == 'error',objs[1].dict) def test_too_many_urls(self): @@ -142,7 +141,7 @@ def test_too_many_urls(self): try: http.oembed(urls) self.fail('too many urls, should have thrown an error') - except Exception, e: + except Exception as e: self.assertTrue(type(e), ValueError) if __name__ == '__main__': diff --git a/setup.py b/setup.py index 8b7912f..e1a3578 100644 --- a/setup.py +++ b/setup.py @@ -38,9 +38,11 @@ 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', - 'Programming Language :: Python :: 2.5', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.1', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', ), **extra ) \ No newline at end of file diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..2ffa83b --- /dev/null +++ b/tox.ini @@ -0,0 +1,4 @@ +[tox] +envlist = py26,py27,py31,py32,py33 +[testenv] +commands=python embedly/tests.py \ No newline at end of file From 26572863150e8c793eb28d422416c64e6d9d5936 Mon Sep 17 00:00:00 2001 From: coagulant Date: Fri, 21 Dec 2012 22:27:16 +0400 Subject: [PATCH 2/5] travis.yml --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a00ef7a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: python +python: + - 2.6 + - 2.7 + - 3.1 + - 3.2 + - 3.3 +script: python embedly/tests.py +install: + - python setup.py -q install \ No newline at end of file From 003ac6fc4da95ff43111c74476e555a061b14dd0 Mon Sep 17 00:00:00 2001 From: coagulant Date: Fri, 21 Dec 2012 22:39:06 +0400 Subject: [PATCH 3/5] More pep8 fixes --- embedly/tests.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/embedly/tests.py b/embedly/tests.py index 42df28e..0d74045 100644 --- a/embedly/tests.py +++ b/embedly/tests.py @@ -4,6 +4,7 @@ from embedly.client import Embedly from embedly.models import Url + class EmbedlyTestCase(unittest.TestCase): def __init__(self, *args, **kwargs): @@ -124,16 +125,15 @@ def test_multi_errors(self): self.assertTrue(objs[1].type == 'error',objs[1].dict) objs = list(http.oembed(['http://blog.embed.ly/lsbsdlfldsf/asdf/kl', - 'http://yfrog.com/h22eu4j'])) + 'http://yfrog.com/h22eu4j'])) self.assertTrue(objs[0].type == 'error',objs[0].dict) self.assertTrue(objs[1].type == 'photo',objs[1].dict) objs = list(http.oembed(['http://yfrog.com/h22eu4j', - 'http://www.scribd.com/asdf/asdf/asdfasdf'])) + 'http://www.scribd.com/asdf/asdf/asdfasdf'])) self.assertTrue(objs[0].type == 'photo',objs[0].dict) self.assertTrue(objs[1].type == 'error',objs[1].dict) - def test_too_many_urls(self): http = Embedly(self.key) @@ -144,5 +144,6 @@ def test_too_many_urls(self): except Exception as e: self.assertTrue(type(e), ValueError) + if __name__ == '__main__': unittest.main() \ No newline at end of file From faf25c1e150fec741e7df305a26218ef9c4f1bc4 Mon Sep 17 00:00:00 2001 From: coagulant Date: Fri, 21 Dec 2012 22:54:04 +0400 Subject: [PATCH 4/5] Travis doesn't support 3.1 https://github.com/travis-ci/travis-ci.github.com/pull/157#issuecomment-10632254 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a00ef7a..718566f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: python python: - 2.6 - 2.7 - - 3.1 - 3.2 - 3.3 script: python embedly/tests.py From 1df422701baef9767aa68aaeef59ed99a0bd6dd8 Mon Sep 17 00:00:00 2001 From: coagulant Date: Sun, 6 Jan 2013 18:30:01 +0400 Subject: [PATCH 5/5] Absolute imports --- embedly/__init__.py | 3 ++- embedly/client.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/embedly/__init__.py b/embedly/__init__.py index e10df67..3a083d7 100644 --- a/embedly/__init__.py +++ b/embedly/__init__.py @@ -1,3 +1,4 @@ -from client import Embedly +from __future__ import absolute_import +from .client import Embedly __version__ = '0.4.3' \ No newline at end of file diff --git a/embedly/client.py b/embedly/client.py index 9cc8056..05b819b 100644 --- a/embedly/client.py +++ b/embedly/client.py @@ -4,6 +4,7 @@ The embedly object that interacts with the service """ +from __future__ import absolute_import import re import httplib2 import json @@ -14,7 +15,7 @@ from urllib.parse import quote, urlencode -from models import Url +from .models import Url USER_AGENT = 'Mozilla/5.0 (compatible; embedly-python/0.3;)'