Skip to content

Commit 7fa54cb

Browse files
committed
Merge branch 'python3'
* 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.
2 parents 4fb544c + ac73785 commit 7fa54cb

8 files changed

Lines changed: 64 additions & 8 deletions

File tree

github2/bin/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ def parse_commandline():
4949
return options, args
5050

5151

52-
def main(options, args):
52+
def main():
5353
"""This implements the actual program functionality"""
5454

55+
options, args = parse_commandline()
56+
5557
if not options.account:
5658
options.account = options.login
5759

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

7981

8082
if __name__ == '__main__':
81-
main(*parse_commandline())
83+
main()

github2/request.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import re
23
import sys
34
import time
45
import httplib2
@@ -22,6 +23,20 @@
2223
GITHUB_URL = "https://github.com"
2324

2425

26+
def charset_from_headers(headers):
27+
"""Parse charset from headers
28+
29+
:param httplib2.Response headers: Request headers
30+
:return: Defined encoding, or default to ASCII
31+
"""
32+
match = re.search("charset=([^ ;]+)", headers.get('content-type', ""))
33+
if match:
34+
charset = match.groups()[0]
35+
else:
36+
charset = "ascii"
37+
return charset
38+
39+
2540
class GithubError(Exception):
2641
"""An error occured when making a request to the Github API."""
2742

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

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ upload-dir = docs/.build/html
66
cover-package = github2
77
detailed-errors = 1
88
with-coverage = 1
9-
with-id = 1
109
[build_sphinx]
1110
source-dir = doc
1211
build-dir = doc/.build

setup.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
if sys.version_info[:2] < (2, 6):
1414
install_requires.append('simplejson >= 2.0.9')
1515

16+
extra = {}
17+
if sys.version_info >= (3,):
18+
extra['use_2to3'] = True
19+
1620
long_description = (codecs.open('README.rst', "r", "utf-8").read()
1721
+ "\n" + codecs.open('NEWS.rst', "r", "utf-8").read())
1822

@@ -28,7 +32,9 @@
2832
keywords="git github api",
2933
platforms=["any"],
3034
packages=find_packages(exclude=['tests']),
31-
scripts=['github2/bin/github_manage_collaborators'],
35+
entry_points={
36+
'console_scripts': ['github_manage_collaborators = github2.bin.manage_collaborators:main', ]
37+
},
3238
install_requires=install_requires,
3339
zip_safe=True,
3440
test_suite="nose.collector",
@@ -47,7 +53,11 @@
4753
"Programming Language :: Python :: 2.5",
4854
"Programming Language :: Python :: 2.6",
4955
"Programming Language :: Python :: 2.7",
56+
"Programming Language :: Python :: 3",
57+
"Programming Language :: Python :: 3.1",
58+
"Programming Language :: Python :: 3.2",
5059
"Topic :: Software Development",
5160
"Topic :: Software Development :: Libraries",
5261
],
62+
**extra
5363
)

tests/test_charset_header.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
3+
from nose.tools import assert_equals
4+
5+
# Forcibly insert path for `setup.py build` output, so that we import from the
6+
# ``2to3`` converted sources
7+
sys.path.insert(0, 'build/lib')
8+
9+
from github2.request import charset_from_headers
10+
11+
12+
def no_match_test():
13+
d = {}
14+
assert_equals("ascii", charset_from_headers(d))
15+
16+
def utf_test():
17+
d = {'content-type': 'application/json; charset=utf-8'}
18+
assert_equals("utf-8", charset_from_headers(d))

tests/test_unit.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# -*- coding: latin-1 -*-
22
import os
3+
import sys
34
import unittest
45

6+
# Forcibly insert path for `setup.py build` output, so that we import from the
7+
# ``2to3`` converted sources
8+
sys.path.insert(0, 'build/lib')
9+
510
from email import message_from_file
611

712
import httplib2
813

914
from github2.issues import Issue
1015
from github2.client import Github
16+
from github2.request import charset_from_headers
1117

1218

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

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

4655

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[tox]
2-
envlist = py24, py25, py26, py27, rst, sphinx
2+
envlist = py24, py25, py26, py27, py31, py32, rst, sphinx
33

44
[testenv]
55
deps =
66
nose
77
coverage
88
commands =
9+
rm -rf build
10+
{envpython} setup.py build
911
nosetests tests
1012
[testenv:rst]
1113
deps =

0 commit comments

Comments
 (0)