Skip to content

Commit

Permalink
Merge branch 'python3'
Browse files Browse the repository at this point in the history
* python3:
  Added basic tests for charset_from_headers().
  Always re-create the build dir between nose runs.
  Don't use nose's with-id plugin anymore.
  Updated tests to work correctly with Python 2 or 3.
  Added py3{1,2} to default tox environment list.
  Force build/lib in to sys.path for running tests.
  Decode HTTP response using Content-Type header's value.
  Switched to using entry points for github_manage_collaborators.
  Use 2to3 when running setup with Python 3.
  • Loading branch information
JNRowe committed May 19, 2011
2 parents 4fb544c + ac73785 commit 7fa54cb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions github2/bin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def parse_commandline():
return options, args


def main(options, args):
def main():
"""This implements the actual program functionality"""

options, args = parse_commandline()

if not options.account:
options.account = options.login

Expand All @@ -78,4 +80,4 @@ def main(options, args):


if __name__ == '__main__':
main(*parse_commandline())
main()
17 changes: 16 additions & 1 deletion github2/request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import re
import sys
import time
import httplib2
Expand All @@ -22,6 +23,20 @@
GITHUB_URL = "https://github.com"


def charset_from_headers(headers):
"""Parse charset from headers
:param httplib2.Response headers: Request headers
:return: Defined encoding, or default to ASCII
"""
match = re.search("charset=([^ ;]+)", headers.get('content-type', ""))
if match:
charset = match.groups()[0]
else:
charset = "ascii"
return charset


class GithubError(Exception):
"""An error occured when making a request to the Github API."""

Expand Down Expand Up @@ -133,7 +148,7 @@ def raw_request(self, url, extra_post_data, method="GET"):
if response.status >= 400:
raise RuntimeError("unexpected response from github.com %d: %r" % (
response.status, content))
json = simplejson.loads(content)
json = simplejson.loads(content.decode(charset_from_headers(response)))
if json.get("error"):
raise self.GithubError(json["error"][0]["error"])

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ upload-dir = docs/.build/html
cover-package = github2
detailed-errors = 1
with-coverage = 1
with-id = 1
[build_sphinx]
source-dir = doc
build-dir = doc/.build
Expand Down
12 changes: 11 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
if sys.version_info[:2] < (2, 6):
install_requires.append('simplejson >= 2.0.9')

extra = {}
if sys.version_info >= (3,):
extra['use_2to3'] = True

long_description = (codecs.open('README.rst', "r", "utf-8").read()
+ "\n" + codecs.open('NEWS.rst', "r", "utf-8").read())

Expand All @@ -28,7 +32,9 @@
keywords="git github api",
platforms=["any"],
packages=find_packages(exclude=['tests']),
scripts=['github2/bin/github_manage_collaborators'],
entry_points={
'console_scripts': ['github_manage_collaborators = github2.bin.manage_collaborators:main', ]
},
install_requires=install_requires,
zip_safe=True,
test_suite="nose.collector",
Expand All @@ -47,7 +53,11 @@
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.1",
"Programming Language :: Python :: 3.2",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
],
**extra
)
18 changes: 18 additions & 0 deletions tests/test_charset_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys

from nose.tools import assert_equals

# Forcibly insert path for `setup.py build` output, so that we import from the
# ``2to3`` converted sources
sys.path.insert(0, 'build/lib')

from github2.request import charset_from_headers


def no_match_test():
d = {}
assert_equals("ascii", charset_from_headers(d))

def utf_test():
d = {'content-type': 'application/json; charset=utf-8'}
assert_equals("utf-8", charset_from_headers(d))
13 changes: 11 additions & 2 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# -*- coding: latin-1 -*-
import os
import sys
import unittest

# Forcibly insert path for `setup.py build` output, so that we import from the
# ``2to3`` converted sources
sys.path.insert(0, 'build/lib')

from email import message_from_file

import httplib2

from github2.issues import Issue
from github2.client import Github
from github2.request import charset_from_headers


HTTP_DATA_DIR = "tests/data/"
Expand All @@ -27,8 +33,8 @@ def request(self, uri, method='GET', body=None, headers=None,
file = os.path.join(HTTP_DATA_DIR, httplib2.safename(uri))
if os.path.exists(file):
response = message_from_file(open(file))
body = response.get_payload()
headers = httplib2.Response(response)
body = response.get_payload().encode(charset_from_headers(headers))
return (headers, body)
else:
return (httplib2.Response({"status": "404"}),
Expand All @@ -40,7 +46,10 @@ class ReprTests(unittest.TestCase):

def test_issue(self):
"""Issues can have non-ASCII characters in the title."""
i = Issue(title=u'abcdé')
title = 'abcdé'
if sys.version_info[0] == 2:
title = title.decode("utf-8")
i = Issue(title=title)
self.assertEqual(str, type(repr(i)))


Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[tox]
envlist = py24, py25, py26, py27, rst, sphinx
envlist = py24, py25, py26, py27, py31, py32, rst, sphinx

[testenv]
deps =
nose
coverage
commands =
rm -rf build
{envpython} setup.py build
nosetests tests
[testenv:rst]
deps =
Expand Down

0 comments on commit 7fa54cb

Please sign in to comment.