From c1635f9b64afbd8e8fc206f2a80cd387f6bce515 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:44:11 -0400 Subject: [PATCH 1/5] whoops, set created_at --- gistapi/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gistapi/core.py b/gistapi/core.py index 947b1d1..85e18ee 100644 --- a/gistapi/core.py +++ b/gistapi/core.py @@ -111,7 +111,7 @@ class Gist(object): setattr(self, key, value) elif key == 'created_at': # Attach datetime - datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S') + setattr(self, 'created_at', datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S')) elif key == 'comments': _comments = [] From d2823bf6b7385a6deec46f29ca1d6d7ace19226c Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:48:54 -0400 Subject: [PATCH 2/5] rely on requests --- gistapi/core.py | 4 ++-- setup.py | 35 +++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/gistapi/core.py b/gistapi/core.py index 85e18ee..d9bed3b 100644 --- a/gistapi/core.py +++ b/gistapi/core.py @@ -35,7 +35,7 @@ u'Great Stuff.' import cStringIO import os.path -import urllib2 +import requests from datetime import datetime try: @@ -97,7 +97,7 @@ class Gist(object): else: # Fetch Gist metadata _meta_url = GIST_JSON % self.id - _meta = json.load(urllib2.urlopen(_meta_url))['gists'][0] + _meta = json.loads(requests.get(_meta_url).content)['gists'][0] for key, value in _meta.iteritems(): diff --git a/setup.py b/setup.py index bd198b7..6f0323c 100644 --- a/setup.py +++ b/setup.py @@ -9,36 +9,39 @@ from distutils.core import setup def publish(): - """Publish to PyPi""" os.system("python setup.py sdist upload") + if sys.argv[-1] == "publish": publish() sys.exit() + -required = [] +required = ['requests'] + if sys.version_info[:2] < (2,6): required.append('simplejson') -setup(name='gistapi', - version=gistapi.__version__, - description='Python wrapper for Gist API', - long_description=open('README.rst').read() + '\n\n' + - open('HISTORY.rst').read(), - author='Kenneth Reitz', - author_email='me@kennethreitz.com', - url='http://github.com/kennethreitz/gistapi.py', - packages=['gistapi'], - install_requires=required, - license='MIT', - classifiers=( + +setup( + name='gistapi', + version=gistapi.__version__, + description='Python wrapper for Gist API', + long_description=open('README.rst').read() + '\n\n' + open('HISTORY.rst').read(), + author='Kenneth Reitz', + author_email='me@kennethreitz.com', + url='http://github.com/kennethreitz/gistapi.py', + packages=['gistapi'], + install_requires=required, + license='MIT', + classifiers=( "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 2.5", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", - ) - ) + ) +) From 19ecde494609ad28df18f96a8979ff31282ed33d Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:51:42 -0400 Subject: [PATCH 3/5] *fully* rely on requests --- gistapi/core.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/gistapi/core.py b/gistapi/core.py index d9bed3b..3e926ce 100644 --- a/gistapi/core.py +++ b/gistapi/core.py @@ -127,14 +127,9 @@ class Gist(object): def _post(self, params, headers={}): """POST to the web form (internal method).""" - request = urllib2.Request(self.post_url, - urllib.urlencode(params), - headers) - try: - response = urllib2.urlopen(request) - except IOError, exc: - response = exc - return response.code, response.msg + r = requests.post(self.post_url, params, headers=headers) + + return r.status_code, r.content def reset(self): """Clear the local cache.""" @@ -204,9 +199,8 @@ class Gist(object): for fn in self._meta['files']: # Grab file contents _file_url = GIST_BASE % 'raw/%s/%s' % (self.id, urllib2.quote(fn)) -# _files[fn] = unicode(urllib2.urlopen(_file_url).read()) _files[fn] = cStringIO.StringIO() - _files[fn].write(urllib2.urlopen(_file_url).read()) + _files[fn].write(requests.get(_file_url).content) return _files @@ -228,7 +222,7 @@ class Gists(object): # Return a list of Gist objects return [Gist(json=g) - for g in json.load(urllib2.urlopen(_url))['gists']] + for g in json.load(requests.get(_url).content)['gists']] class GistComment(object): From 080ef4bff9e09bec308a19eb683e2bd4fb3c634f Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 05:03:18 -0400 Subject: [PATCH 4/5] requests fix --- gistapi/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gistapi/core.py b/gistapi/core.py index 3e926ce..9d5308a 100644 --- a/gistapi/core.py +++ b/gistapi/core.py @@ -222,7 +222,7 @@ class Gists(object): # Return a list of Gist objects return [Gist(json=g) - for g in json.load(requests.get(_url).content)['gists']] + for g in json.loads(requests.get(_url).content)['gists']] class GistComment(object): From f80cfabbfefa1bdc48f26799935ccc430cf4f82b Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 05:03:31 -0400 Subject: [PATCH 5/5] (very) basic unit testing --- test_gistapi.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 17 +++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 test_gistapi.py create mode 100644 tox.ini diff --git a/test_gistapi.py b/test_gistapi.py new file mode 100644 index 0000000..8e260d7 --- /dev/null +++ b/test_gistapi.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + +import gistapi +from gistapi import Gist, Gists + + +class RequestsTestSuite(unittest.TestCase): + """Requests test cases.""" + + def setUp(self): + pass + + def tearDown(self): + """Teardown.""" + pass + + def test_repo_fetch(self): + r1 = Gist('d4507e882a07ac6f9f92').repo + r2 = u'd4507e882a07ac6f9f92' + + self.assertEqual(r1, r2) + + def test_owner_fetch(self): + r1 = Gist('d4507e882a07ac6f9f92').owner + r2 = u'kennethreitz' + + self.assertEqual(r1, r2) + + def test_created_at_fetch(self): + r1 = Gist('d4507e882a07ac6f9f92').created_at.isoformat() + r2 = '2010-05-16T10:51:15' + + self.assertEqual(r1, r2) + + def test_public_fetch(self): + r1 = Gist('d4507e882a07ac6f9f92').public + r2 = False + + self.assertEqual(r1, r2) + + def test_fetch_filesnames(self): + r1 = Gist('d4507e882a07ac6f9f92').filenames + r2 = ['exampleEmptyFile', 'exampleFile'] + + self.assertEqual(r1, r2) + + def test_gist_search(self): + r1 = Gists.fetch_by_user('kennethreitz')[-1].description + r2 = u'My .bashrc configuration' + + self.assertEqual(r1, r2) + + def test_gist_comments(self): + r1 = Gist(885658).comments[0].body + r2 = u'Great stuff.' + + self.assertEqual(r1, r2) + + + +if __name__ == '__main__': + unittest.main() diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..764ee3c --- /dev/null +++ b/tox.ini @@ -0,0 +1,17 @@ +[tox] +envlist = py25,py26,py27,py3 + +[testenv] +commands=py.test --junitxml=junit-{envname}.xml +deps = pytest + +[testenv:py25] +simplejson = pytest simplejson + +[testenv:pypy] +basepython=/usr/bin/pypy-c +simplejson = pytest simplejson + +[testenv:py3] +basepython=/usr/bin/python3 +simplejson = pytest \ No newline at end of file