Skip to content

Commit cb06177

Browse files
committed
Start re-factoring
Split up gists.py into gists/gist.py, gists/comment.py, etc.
1 parent a5b120e commit cb06177

12 files changed

Lines changed: 183 additions & 119 deletions

File tree

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ History/Changelog
77
- Add ``sort`` and ``order`` parameters to ``github3.GitHub.search_users`` and
88
``github3.GitHub.search_repos``.
99

10+
- Add ``iter_commits`` to ``github3.gists.Gist`` as a means of re-requesting
11+
just the history from GitHub and iterating over it.
12+
13+
- Re-organize the library
14+
1015
- Remove vendored dependency of PySO8601.
1116

1217
0.5.3: 2013-03-19

MANIFEST.in

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
include README.rst LICENSE HISTORY.rst AUTHORS.rst run_tests.py
1+
include README.rst
2+
include LICENSE
3+
include HISTORY.rst
4+
include AUTHORS.rst
5+
include run_tests.py
6+
prune *.pyc
7+
recursive-include github3/*

github3/gists/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
github3.gists
3+
=============
4+
5+
Module which contains all the gist related material.
6+
7+
Sub-modules:
8+
github3.gists.gist
9+
github3.gists.file
10+
github3.gists.comment
11+
github3.gists.history
12+
13+
"""
14+
15+
from .gist import Gist
16+
17+
__all__ = [Gist]

github3/gists/comment.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from github3.models import BaseComment
2+
from github3.users import User
3+
4+
5+
class GistComment(BaseComment):
6+
"""The :class:`GistComment <GistComment>` object. This represents a comment
7+
on a gist.
8+
"""
9+
def __init__(self, comment, session=None):
10+
super(GistComment, self).__init__(comment, session)
11+
12+
#: :class:`User <github3.users.User>` who made the comment
13+
self.user = None
14+
if comment.get('user'):
15+
self.user = User(comment.get('user'), self) # (No coverage)
16+
17+
def __repr__(self):
18+
return '<Gist Comment [{0}]>'.format(self.user.login)

github3/gists/file.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from github3.models import GitHubObject
2+
3+
4+
class GistFile(GitHubObject):
5+
"""The :class:`GistFile <GistFile>` object. This is used to represent a
6+
file object returned by GitHub while interacting with gists.
7+
"""
8+
def __init__(self, attributes):
9+
super(GistFile, self).__init__(attributes)
10+
11+
#: The raw URL for the file at GitHub.
12+
self.raw_url = attributes.get('raw_url')
13+
#: The name of the file.
14+
self.filename = attributes.get('filename')
15+
#: The name of the file.
16+
self.name = attributes.get('filename')
17+
#: The language associated with the file.
18+
self.language = attributes.get('language')
19+
#: The size of the file.
20+
self.size = attributes.get('size')
21+
#: The content of the file.
22+
self.content = attributes.get('content')
23+
24+
def __repr__(self):
25+
return '<Gist File [{0}]>'.format(self.name)
Lines changed: 28 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
"""
2-
github3.gists
3-
=============
4-
5-
Module which contains all the gist related material.
6-
7-
"""
8-
91
from json import dumps
10-
from github3.models import GitHubObject, GitHubCore, BaseComment
11-
from github3.users import User
2+
from github3.models import GitHubCore
123
from github3.decorators import requires_auth
4+
from github3.gists.comment import GistComment
5+
from github3.gists.file import GistFile
6+
from github3.gists.history import GistHistory
7+
from github3.users import User
138

149

1510
class Gist(GitHubCore):
@@ -22,6 +17,9 @@ class Gist(GitHubCore):
2217

2318
def __init__(self, data, session=None):
2419
super(Gist, self).__init__(data, session)
20+
#: Number of comments on this gist
21+
self.comments = data.get('comments', 0)
22+
2523
#: Unique id for this gist.
2624
self.id = '{0}'.format(data.get('id', ''))
2725

@@ -79,7 +77,7 @@ def create_comment(self, body):
7977
"""Create a comment on this gist.
8078
8179
:param str body: (required), body of the comment
82-
:returns: :class:`GistComment <GistComment>`
80+
:returns: :class:`GistComment <github3.gists.comment.GistComment>`
8381
"""
8482
json = None
8583
if body:
@@ -152,11 +150,29 @@ def iter_comments(self, number=-1, etag=None):
152150
Default: -1 will iterate over all comments on the gist
153151
:param str etag: (optional), ETag from a previous request to the same
154152
endpoint
155-
:returns: generator of :class:`GistComment <GistComment>`\ s
153+
:returns: generator of
154+
:class:`GistComment <github3.gists.comment.GistComment>`\ s
156155
"""
157156
url = self._build_url('comments', base_url=self._api)
158157
return self._iter(int(number), url, GistComment, etag=etag)
159158

159+
def iter_commits(self, number=-1):
160+
"""Iter over the commits on this gist.
161+
162+
These commits will be requested from the API and should be the same as
163+
what is in ``Gist.history``.
164+
165+
.. versionadded:: 0.6
166+
167+
:param int number: (optional), number of commits to iterate over.
168+
Default: -1 will iterate over all commits associated with this
169+
gist.
170+
:returns: generator of
171+
:class: `GistHistory <github3.gists.history.GistHistory>`\ s
172+
"""
173+
url = self._build_url('commits', base_url=self._api)
174+
return self._iter(int(number), url, GistHistory)
175+
160176
def iter_files(self):
161177
"""List of :class:`GistFile <GistFile>` objects representing the files
162178
stored in this gist."""
@@ -183,84 +199,3 @@ def unstar(self):
183199
"""
184200
url = self._build_url('star', base_url=self._api)
185201
return self._boolean(self._delete(url), 204, 404)
186-
187-
188-
class GistComment(BaseComment):
189-
"""The :class:`GistComment <GistComment>` object. This represents a comment
190-
on a gist.
191-
"""
192-
def __init__(self, comment, session=None):
193-
super(GistComment, self).__init__(comment, session)
194-
195-
#: :class:`User <github3.users.User>` who made the comment
196-
self.user = None
197-
if comment.get('user'):
198-
self.user = User(comment.get('user'), self) # (No coverage)
199-
200-
def __repr__(self):
201-
return '<Gist Comment [{0}]>'.format(self.user.login)
202-
203-
204-
class GistFile(GitHubObject):
205-
"""The :class:`GistFile <GistFile>` object. This is used to represent a
206-
file object returned by GitHub while interacting with gists.
207-
"""
208-
def __init__(self, attributes):
209-
super(GistFile, self).__init__(attributes)
210-
211-
#: The raw URL for the file at GitHub.
212-
self.raw_url = attributes.get('raw_url')
213-
#: The name of the file.
214-
self.filename = attributes.get('filename')
215-
#: The name of the file.
216-
self.name = attributes.get('filename')
217-
#: The language associated with the file.
218-
self.language = attributes.get('language')
219-
#: The size of the file.
220-
self.size = attributes.get('size')
221-
#: The content of the file.
222-
self.content = attributes.get('content')
223-
224-
def __repr__(self):
225-
return '<Gist File [{0}]>'.format(self.name)
226-
227-
228-
class GistHistory(GitHubCore):
229-
"""The :class:`GistHistory <GistHistory>` object represents one version
230-
(or revision) of a gist."""
231-
def __init__(self, history, session=None):
232-
super(GistHistory, self).__init__(history, session)
233-
self._api = history.get('url', '')
234-
235-
#: SHA of the commit associated with this version
236-
self.version = history.get('version', '')
237-
238-
#: user who made these changes
239-
self.user = User(history.get('user') or {}, session)
240-
241-
#: dict containing the change status; see also: deletions, additions,
242-
#: total
243-
self.change_status = history.get('change_status', {})
244-
245-
#: number of additions made
246-
self.additions = self.change_status.get('additions', 0)
247-
248-
#: number of deletions made
249-
self.deletions = self.change_status.get('deletions', 0)
250-
251-
#: total number of changes made
252-
self.total = self.change_status.get('total', 0)
253-
254-
#: datetime representation of when the commit was made
255-
self.committed_at = self._strptime(history.get('committed_at'))
256-
257-
def __repr__(self):
258-
return '<Gist History [{0}]>'.format(self.version)
259-
260-
def get_gist(self):
261-
"""Retrieves the gist at this version.
262-
263-
:returns: :class:`Gist <Gist>`
264-
"""
265-
json = self._json(self._get(self._api), 200)
266-
return Gist(json, self)

github3/gists/history.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from github3.models import GitHubCore
2+
from github3.users import User
3+
4+
5+
class GistHistory(GitHubCore):
6+
"""The :class:`GistHistory <GistHistory>` object represents one version
7+
(or revision) of a gist."""
8+
def __init__(self, history, session=None):
9+
super(GistHistory, self).__init__(history, session)
10+
self._api = history.get('url', '')
11+
12+
#: SHA of the commit associated with this version
13+
self.version = history.get('version', '')
14+
15+
#: user who made these changes
16+
self.user = User(history.get('user') or {}, session)
17+
18+
#: dict containing the change status; see also: deletions, additions,
19+
#: total
20+
self.change_status = history.get('change_status', {})
21+
22+
#: number of additions made
23+
self.additions = self.change_status.get('additions', 0)
24+
25+
#: number of deletions made
26+
self.deletions = self.change_status.get('deletions', 0)
27+
28+
#: total number of changes made
29+
self.total = self.change_status.get('total', 0)
30+
31+
#: datetime representation of when the commit was made
32+
self.committed_at = self._strptime(history.get('committed_at'))
33+
34+
def __repr__(self):
35+
return '<Gist History [{0}]>'.format(self.version)
36+
37+
def get_gist(self):
38+
"""Retrieves the gist at this version.
39+
40+
:returns: :class:`Gist <github3.gists.gist.Gist>`
41+
"""
42+
from github3.gists.gist import Gist
43+
json = self._json(self._get(self._api), 200)
44+
return Gist(json, self)

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
requires = []
99
packages = [
1010
"github3",
11-
"github3.packages",
12-
"github3.packages.PySO8601",
11+
"github3.gists",
1312
]
1413

1514
try:

tests/json/gist_history

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"url": "https://api.github.com/gists/3813862/8d6121ba316f3d07371183e526b5287474d81bcc", "change_status": {"deletions": 7, "additions": 1, "total": 8}, "committed_at": "2012-10-02T00:04:49Z", "version": "8d6121ba316f3d07371183e526b5287474d81bcc", "user": {"following_url": "https://api.github.com/users/sigmavirus24/following", "events_url": "https://api.github.com/users/sigmavirus24/events{/privacy}", "organizations_url": "https://api.github.com/users/sigmavirus24/orgs", "url": "https://api.github.com/users/sigmavirus24", "gists_url": "https://api.github.com/users/sigmavirus24/gists{/gist_id}", "subscriptions_url": "https://api.github.com/users/sigmavirus24/subscriptions", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "repos_url": "https://api.github.com/users/sigmavirus24/repos", "received_events_url": "https://api.github.com/users/sigmavirus24/received_events", "gravatar_id": "c148356d89f925e692178bee1d93acf7", "starred_url": "https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}", "login": "sigmavirus24", "type": "User", "id": 240830, "followers_url": "https://api.github.com/users/sigmavirus24/followers"}}, {"url": "https://api.github.com/gists/3813862/a2a4a7b438cea5aafb60e82da28569fa44582b14", "change_status": {"deletions": 10, "additions": 1, "total": 11}, "committed_at": "2012-10-01T20:54:57Z", "version": "a2a4a7b438cea5aafb60e82da28569fa44582b14", "user": {"following_url": "https://api.github.com/users/sigmavirus24/following", "events_url": "https://api.github.com/users/sigmavirus24/events{/privacy}", "organizations_url": "https://api.github.com/users/sigmavirus24/orgs", "url": "https://api.github.com/users/sigmavirus24", "gists_url": "https://api.github.com/users/sigmavirus24/gists{/gist_id}", "subscriptions_url": "https://api.github.com/users/sigmavirus24/subscriptions", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "repos_url": "https://api.github.com/users/sigmavirus24/repos", "received_events_url": "https://api.github.com/users/sigmavirus24/received_events", "gravatar_id": "c148356d89f925e692178bee1d93acf7", "starred_url": "https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}", "login": "sigmavirus24", "type": "User", "id": 240830, "followers_url": "https://api.github.com/users/sigmavirus24/followers"}}, {"url": "https://api.github.com/gists/3813862/e578b4ea38b953ff0fa1397fb04e7ad9563b3d40", "change_status": {"deletions": 2, "additions": 0, "total": 2}, "committed_at": "2012-10-01T20:20:13Z", "version": "e578b4ea38b953ff0fa1397fb04e7ad9563b3d40", "user": {"following_url": "https://api.github.com/users/sigmavirus24/following", "events_url": "https://api.github.com/users/sigmavirus24/events{/privacy}", "organizations_url": "https://api.github.com/users/sigmavirus24/orgs", "url": "https://api.github.com/users/sigmavirus24", "gists_url": "https://api.github.com/users/sigmavirus24/gists{/gist_id}", "subscriptions_url": "https://api.github.com/users/sigmavirus24/subscriptions", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "repos_url": "https://api.github.com/users/sigmavirus24/repos", "received_events_url": "https://api.github.com/users/sigmavirus24/received_events", "gravatar_id": "c148356d89f925e692178bee1d93acf7", "starred_url": "https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}", "login": "sigmavirus24", "type": "User", "id": 240830, "followers_url": "https://api.github.com/users/sigmavirus24/followers"}}, {"url": "https://api.github.com/gists/3813862/da3ae2b8d1f4b7bd2c067d02defd4d0f3352854c", "change_status": {"deletions": 0, "additions": 91, "total": 91}, "committed_at": "2012-10-01T19:23:45Z", "version": "da3ae2b8d1f4b7bd2c067d02defd4d0f3352854c", "user": {"following_url": "https://api.github.com/users/sigmavirus24/following", "events_url": "https://api.github.com/users/sigmavirus24/events{/privacy}", "organizations_url": "https://api.github.com/users/sigmavirus24/orgs", "url": "https://api.github.com/users/sigmavirus24", "gists_url": "https://api.github.com/users/sigmavirus24/gists{/gist_id}", "subscriptions_url": "https://api.github.com/users/sigmavirus24/subscriptions", "avatar_url": "https://secure.gravatar.com/avatar/c148356d89f925e692178bee1d93acf7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", "repos_url": "https://api.github.com/users/sigmavirus24/repos", "received_events_url": "https://api.github.com/users/sigmavirus24/received_events", "gravatar_id": "c148356d89f925e692178bee1d93acf7", "starred_url": "https://api.github.com/users/sigmavirus24/starred{/owner}{/repo}", "login": "sigmavirus24", "type": "User", "id": 240830, "followers_url": "https://api.github.com/users/sigmavirus24/followers"}}]

tests/test_api.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import github3
2-
from tests.utils import APITestMixin
3-
from mock import patch
2+
from unittest import TestCase
3+
from mock import patch, NonCallableMock
44

55

6-
class TestAPI(APITestMixin):
6+
class TestAPI(TestCase):
7+
def setUp(self):
8+
self.mock = patch('github3.api.gh', autospec=github3.GitHub)
9+
self.gh = self.mock.start()
10+
11+
def tearDown(self):
12+
self.mock.stop()
13+
714
def test_authorize(self):
815
args = ('login', 'password', ['scope1'], 'note', 'note_url.com', '',
916
'')
@@ -141,8 +148,10 @@ def test_user(self):
141148
self.gh.user.assert_called_with('login')
142149

143150
def test_ratelimit_remaining(self):
151+
# This prevents a regression in the API
152+
# See 81c800658db43f86419b9c0764fc16aad3d60007
153+
self.gh.ratelimit_remaining = NonCallableMock()
144154
github3.ratelimit_remaining()
145-
assert self.gh.ratelimit_remaining.called is True
146155

147156
def test_zen(self):
148157
github3.zen()

0 commit comments

Comments
 (0)