diff options
-rw-r--r-- | docs/gists.rst | 37 | ||||
-rw-r--r-- | docs/repos.rst | 4 | ||||
-rw-r--r-- | docs/services.rst | 11 | ||||
-rw-r--r-- | docs/users.rst | 4 | ||||
-rw-r--r-- | pygithub3/github.py | 16 | ||||
-rw-r--r-- | pygithub3/requests/base.py | 2 | ||||
-rw-r--r-- | pygithub3/requests/gists/__init__.py | 77 | ||||
-rw-r--r-- | pygithub3/requests/gists/comments.py | 40 | ||||
-rw-r--r-- | pygithub3/resources/gists.py | 45 | ||||
-rw-r--r-- | pygithub3/services/gists/__init__.py | 154 | ||||
-rw-r--r-- | pygithub3/services/gists/comments.py | 71 | ||||
-rw-r--r-- | pygithub3/services/repos/__init__.py | 6 | ||||
-rw-r--r-- | pygithub3/tests/services/test_gists.py | 130 | ||||
-rw-r--r-- | pygithub3/tests/services/test_repos.py | 4 |
14 files changed, 588 insertions, 13 deletions
diff --git a/docs/gists.rst b/docs/gists.rst new file mode 100644 index 0000000..3b2915f --- /dev/null +++ b/docs/gists.rst @@ -0,0 +1,37 @@ +.. _Gists service: + +Gists services +=============== + +**Fast sample**:: + + from pygithub3 import Github + + auth = dict(login='octocat', password='pass') + gh = Github(**auth) + + octocat_gists = gh.gists.list() + the_first_gist = gh.gists.get(1) + + the_first_gist_comments = gh.gists.comments.list(1) + +Gist +----- + +.. autoclass:: pygithub3.services.gists.Gist + :members: + + .. attribute:: comments + + :ref:`Comments service` + +.. _Comments service: + +Comments +---------- + +.. autoclass:: pygithub3.services.gists.Comments + :members: + +.. _github gists doc: http://developer.github.com/v3/gists +.. _github comments doc: http://developer.github.com/v3/gists/comments diff --git a/docs/repos.rst b/docs/repos.rst index 79e016a..c065671 100644 --- a/docs/repos.rst +++ b/docs/repos.rst @@ -1,6 +1,6 @@ .. _Repos service: -Repos's services +Repos services =================== **Fast sample**:: @@ -50,7 +50,7 @@ You can see it better with an example: :: Repo ------- -.. autoclass:: pygithub3.services.repos.Repos +.. autoclass:: pygithub3.services.repos.Repo :members: .. attribute:: collaborators diff --git a/docs/services.rst b/docs/services.rst index a0fd894..71fa690 100644 --- a/docs/services.rst +++ b/docs/services.rst @@ -53,6 +53,16 @@ attributes or all of them. .. autoclass:: pygithub3.services.base.MimeTypeMixin :members: +**Fast example**:: + + from pygithub3 import Github + + gh = Github() + + gh.gists.comments.set_html() + comment = gh.gists.comments.list(1).all()[0] + print comment.body, comment.body_text, comment.body_html + List of services ------------------- @@ -61,5 +71,6 @@ List of services users repos + gists .. _mimetypes: http://developer.github.com/v3/mime diff --git a/docs/users.rst b/docs/users.rst index 5bd2e97..139d726 100644 --- a/docs/users.rst +++ b/docs/users.rst @@ -1,6 +1,6 @@ -.. _User service: +.. _Users service: -User's services +Users services =============== **Fast sample**:: diff --git a/pygithub3/github.py b/pygithub3/github.py index 23b5b0b..0b302a1 100644 --- a/pygithub3/github.py +++ b/pygithub3/github.py @@ -2,6 +2,7 @@ # -*- encoding: utf-8 -*- +#TODO: Move the imports out. setup related class Github(object): """ You can preconfigure all services globally with a ``config`` dict. See @@ -14,9 +15,11 @@ class Github(object): def __init__(self, **config): from pygithub3.services.users import User - from pygithub3.services.repos import Repos + from pygithub3.services.repos import Repo + from pygithub3.services.gists import Gist self._users = User(**config) - self._repos = Repos(**config) + self._repos = Repo(**config) + self._gists = Gist(**config) @property def remaining_requests(self): @@ -27,7 +30,7 @@ class Github(object): @property def users(self): """ - :ref:`User service <User service>` + :ref:`Users service <Users service>` """ return self._users @@ -37,3 +40,10 @@ class Github(object): :ref:`Repos service <Repos service>` """ return self._repos + + @property + def gists(self): + """ + :ref:`Gists service <Gists service>` + """ + return self._gists diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 65bc558..03b0f8a 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -37,7 +37,7 @@ class Body(object): if attr_required not in parsed: raise ValidationError("'%s' attribute is required" % attr_required) - if not parsed[attr_required]: + if parsed[attr_required] is None: raise ValidationError("'%s' attribute can't be empty" % attr_required) return parsed diff --git a/pygithub3/requests/gists/__init__.py b/pygithub3/requests/gists/__init__.py new file mode 100644 index 0000000..4dbc6e6 --- /dev/null +++ b/pygithub3/requests/gists/__init__.py @@ -0,0 +1,77 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request, ValidationError +from pygithub3.resources.gists import Gist + +class List(Request): + + uri = 'users/{user}/gists' + resource = Gist + + def clean_uri(self): + if not self.user: + return 'gists' + + +class Public(Request): + + uri = 'gists/public' + resource = Gist + + +class Starred(Request): + + uri = 'gists/starred' + resource = Gist + + +class Get(Request): + + uri = 'gists/{id}' + resource = Gist + + +class Create(Request): + + uri = 'gists' + resource = Gist + body_schema = { + 'schema': ('description', 'public', 'files'), + 'required': ('public', 'files') + } + + +class Update(Request): + + uri = 'gists/{id}' + resource = Gist + body_schema = { + 'schema': ('description', 'public', 'files'), + 'required': (), + } + + +class Star(Request): + + uri = 'gists/{id}/star' + + +class Unstar(Request): + + uri = 'gists/{id}/star' + + +class Is_starred(Request): + + uri = 'gists/{id}/star' + + +class Fork(Request): + + uri = 'gists/{id}/fork' + resource = Gist + + +class Delete(Request): + + uri = 'gists/{id}' diff --git a/pygithub3/requests/gists/comments.py b/pygithub3/requests/gists/comments.py new file mode 100644 index 0000000..8e5af15 --- /dev/null +++ b/pygithub3/requests/gists/comments.py @@ -0,0 +1,40 @@ +# -*- encoding: utf-8 -*- + +from pygithub3.requests.base import Request +from pygithub3.resources.gists import Comment + +class List(Request): + + uri = 'gists/{gist_id}/comments' + resource = Comment + + +class Get(Request): + + uri = 'gists/comments/{id}' + resource = Comment + + +class Create(Request): + + uri = 'gists/{gist_id}/comments' + resource = Comment + body_schema = { + 'schema': ('body', ), + 'required': ('body', ) + } + + +class Update(Request): + + uri = 'gists/comments/{id}' + resource = Comment + body_schema = { + 'schema': ('body', ), + 'required': ('body', ) + } + + +class Delete(Request): + + uri = 'gists/comments/{id}' diff --git a/pygithub3/resources/gists.py b/pygithub3/resources/gists.py new file mode 100644 index 0000000..7e9550a --- /dev/null +++ b/pygithub3/resources/gists.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from .base import Resource +from .users import User + + +class File(Resource): + + def __str__(self): + return '<GistFile (%s)>' % getattr(self, 'filename', '') + + +class Fork(Resource): + + _dates = ('created_at', ) + _maps = {'user': User} + def __str__(self): + return '<GistFork>' + + +class History(Resource): + + _dates = ('committed_at', ) + _maps = {'user': User} + + def __str__(self): + return '<GistHistory (%s)>' % getattr(self, 'version', '') + +class Gist(Resource): + + _dates = ('created_at', ) + _maps = {'user': User} + _collection_maps = {'files': File, 'forks': Fork, 'history': History} + + def __str__(self): + return '<Gist (%s)>' % getattr(self, 'description', '') + +class Comment(Resource): + + _dates = ('created_at', ) + _maps = {'user': User} + + def __str__(self): + return '<GistComment (%s)>' % getattr(self, 'user', '') diff --git a/pygithub3/services/gists/__init__.py b/pygithub3/services/gists/__init__.py new file mode 100644 index 0000000..aadb136 --- /dev/null +++ b/pygithub3/services/gists/__init__.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service +from comments import Comments + + +class Gist(Service): + """ Consume `Gists API <http://developer.github.com/v3/gists>`_ """ + + def __init__(self, **config): + self.comments = Comments(**config) + super(Gist, self).__init__(**config) + + def list(self, user=None): + """ Get user's gists + + :param str user: Username + :returns: A :doc:`result` + + If you call it without user and you are authenticated, get the + authenticated user's gists. but if you aren't authenticated get the + public gists + + :: + + gist_service.list('copitux') + gist_service.list() + """ + request = self.request_builder('gists.list', user=user) + return self._get_result(request) + + def public(self): + """ Get public gists + + :returns: A :doc:`result` + + .. note :: + Be careful iterating the result + """ + request = self.request_builder('gists.public') + return self._get_result(request) + + def starred(self): + """ Get authenticated user's starred gists + + :returns: A :doc:`result` + + .. warning:: + You must be authenticated + """ + request = self.request_builder('gists.starred') + return self._get_result(request) + + def get(self, id): + """ Get a single gist + + :param int id: Gist id + """ + request = self.request_builder('gists.get', id=id) + return self._get(request) + + def create(self, data): + """ Create a gist + + :param dict data: Input. See `github gists doc`_ + + :: + + gist_service.create(dict(description='some gist', public=True, + files={'xample.py': {'content': 'import code'}})) + """ + request = self.request_builder('gists.create', body=data) + return self._post(request) + + def update(self, id, data): + """ Update a single gist + + :param int id: Gist id + :param dict data: Input. See `github gists doc`_ + + .. warning :: + You must be authenticated + + :: + + gist_service.update(dict(description='edited', + files={'xample.py': { + 'filename': 'new_xample.py', + 'content': 'import new_code'}})) + """ + request = self.request_builder('gists.update', id=id, body=data) + return self._patch(request) + + def star(self, id): + """ Star a gist + + :param int id: Gist id + + .. warning :: + You must be authenticated + + """ + request = self.request_builder('gists.star', id=id) + self._put(request) + + def unstar(self, id): + """ Unstar a gist + + :param int id: Gist id + + .. warning :: + You must be authenticated + + """ + request = self.request_builder('gists.unstar', id=id) + return self._delete(request) + + def is_starred(self, id): + """ Check if a gist is starred + + :param int id: Gist id + + .. warning :: + You must be authenticated + + """ + request = self.request_builder('gists.is_starred', id=id) + return self._bool(request) + + def fork(self, id): + """ Fork a gist + + :param int id: Gist id + + .. warning :: + You must be authenticated + + """ + + request = self.request_builder('gists.fork', id=id) + return self._post(request) + + def delete(self, id): + """ Delete a gist + + :param int id: Gist id + + .. warning :: + You must be authenticated + + """ + request = self.request_builder('gists.delete', id=id) + return self._delete(request) diff --git a/pygithub3/services/gists/comments.py b/pygithub3/services/gists/comments.py new file mode 100644 index 0000000..7f4955f --- /dev/null +++ b/pygithub3/services/gists/comments.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.services.base import Service, MimeTypeMixin + + +class Comments(Service, MimeTypeMixin): + """ Consume `Comments API + <http://developer.github.com/v3/gists/comments>`_ + + .. note:: + This service support :ref:`mimetypes-section` configuration + """ + + def list(self, gist_id): + """ Get gist's comments + + :param int gist_id: Gist id + :returns: A :doc:`result` + """ + request = self.request_builder('gists.comments.list', gist_id=gist_id) + return self._get_result(request, **self._get_mimetype_as_header()) + + def get(self, id): + """ Get a single comment + + :param int id: Comment id + """ + request = self.request_builder('gists.comments.get', id=id) + return self._get(request, **self._get_mimetype_as_header()) + + def create(self, gist_id, message): + """ Create a comment + + :param int gist_id: Gist id + :param str message: Comment's message + + .. warning:: + You must be authenticated + + :: + + comment_service.create(1, 'comment') + """ + request = self.request_builder('gists.comments.create', + gist_id=gist_id, body={'body': message}) + return self._post(request, **self._get_mimetype_as_header()) + + def update(self, id, message): + """ Update a comment + + :param int id: Comment id + :param str message: Comment's message + + .. warning:: + You must be authenticated + """ + request = self.request_builder('gists.comments.update', id=id, + body={'body': message}) + return self._patch(request, **self._get_mimetype_as_header()) + + def delete(self, id): + """ Delete a comment + + :param int id: Comment id + + .. warning:: + You must be authenticated + """ + request = self.request_builder('gists.comments.delete', id=id) + self._delete(request) diff --git a/pygithub3/services/repos/__init__.py b/pygithub3/services/repos/__init__.py index 3ef1fb4..628e9d6 100644 --- a/pygithub3/services/repos/__init__.py +++ b/pygithub3/services/repos/__init__.py @@ -11,7 +11,7 @@ from .watchers import Watchers from .hooks import Hooks -class Repos(Service): +class Repo(Service): """ Consume `Repos API <http://developer.github.com/v3/repos>`_ """ def __init__(self, **config): @@ -22,7 +22,7 @@ class Repos(Service): self.keys = Keys(**config) self.watchers = Watchers(**config) self.hooks = Hooks(**config) - super(Repos, self).__init__(**config) + super(Repo, self).__init__(**config) def list(self, user=None, type='all'): """ Get user's repositories @@ -133,7 +133,7 @@ class Repos(Service): return self.__list_contributors(user, repo) def list_contributors_with_anonymous(self, user=None, repo=None): - """ Like :attr:`~pygithub3.services.repos.Repos.list_contributors` plus + """ Like :attr:`~pygithub3.services.repos.Repo.list_contributors` plus anonymous """ return self.__list_contributors(user, repo, anom=True) diff --git a/pygithub3/tests/services/test_gists.py b/pygithub3/tests/services/test_gists.py new file mode 100644 index 0000000..8cf93ed --- /dev/null +++ b/pygithub3/tests/services/test_gists.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +import requests +from mock import patch, Mock + +from pygithub3.tests.utils.core import TestCase +from pygithub3.resources.base import json +from pygithub3.services.gists import Gist, Comments +from pygithub3.tests.utils.base import (mock_response, mock_response_result, + mock_json) +from pygithub3.tests.utils.services import _ + +json.dumps = Mock(side_effect=mock_json) +json.loads = Mock(side_effect=mock_json) + + +@patch.object(requests.sessions.Session, 'request') +class TestGistService(TestCase): + + def setUp(self): + self.gs = Gist() + + def test_LIST_without_user(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list().all() + self.assertEqual(request_method.call_args[0], ('get', _('gists'))) + + def test_LIST_with_user(self, request_method): + request_method.return_value = mock_response_result() + self.gs.list('octocat').all() + self.assertEqual(request_method.call_args[0], + ('get', _('users/octocat/gists'))) + + def test_LIST_public(self, request_method): + request_method.return_value = mock_response_result() + self.gs.public().all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/public'))) + + def test_LIST_starred(self, request_method): + request_method.return_value = mock_response_result() + self.gs.starred().all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/starred'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.gs.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('gists/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + self.gs.create(dict(public=True, files={ + 'file1.txt': {'content': 'Some\ncontent'}})) + self.assertEqual(request_method.call_args[0], + ('post', _('gists'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.gs.update(1, {'description': 'edited'}) + self.assertEqual(request_method.call_args[0], + ('patch', _('gists/1'))) + + def test_STAR(self, request_method): + self.gs.star(1) + self.assertEqual(request_method.call_args[0], + ('put', _('gists/1/star'))) + + def test_UNSTAR(self, request_method): + request_method.return_value = mock_response('delete') + self.gs.unstar(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/1/star'))) + + def test_IS_STARRED(self, request_method): + request_method.return_value = mock_response() + self.gs.is_starred(1) + self.assertEqual(request_method.call_args[0], + ('head', _('gists/1/star'))) + + def test_FORK(self, request_method): + request_method.return_value = mock_response('post') + self.gs.fork(1) + self.assertEqual(request_method.call_args[0], + ('post', _('gists/1/fork'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.gs.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/1'))) + + +@patch.object(requests.sessions.Session, 'request') +class TestCommentService(TestCase): + + def setUp(self): + self.cs = Comments() + + def test_LIST(self, request_method): + request_method.return_value = mock_response_result() + self.cs.list(1).all() + self.assertEqual(request_method.call_args[0], + ('get', _('gists/1/comments'))) + + def test_GET(self, request_method): + request_method.return_value = mock_response() + self.cs.get(1) + self.assertEqual(request_method.call_args[0], + ('get', _('gists/comments/1'))) + + def test_CREATE(self, request_method): + request_method.return_value = mock_response('post') + self.cs.create(1, dict(body='comment')) + self.assertEqual(request_method.call_args[0], + ('post', _('gists/1/comments'))) + + def test_UPDATE(self, request_method): + request_method.return_value = mock_response('patch') + self.cs.update(1, dict(body='new_comment')) + self.assertEqual(request_method.call_args[0], + ('patch', _('gists/comments/1'))) + + def test_DELETE(self, request_method): + request_method.return_value = mock_response('delete') + self.cs.delete(1) + self.assertEqual(request_method.call_args[0], + ('delete', _('gists/comments/1'))) diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py index e77cc90..e21d474 100644 --- a/pygithub3/tests/services/test_repos.py +++ b/pygithub3/tests/services/test_repos.py @@ -5,7 +5,7 @@ import requests from mock import patch, Mock from pygithub3.tests.utils.core import TestCase -from pygithub3.services.repos import (Repos, Collaborators, Commits, Downloads, +from pygithub3.services.repos import (Repo, Collaborators, Commits, Downloads, Forks, Keys, Watchers, Hooks) from pygithub3.resources.base import json from pygithub3.tests.utils.base import (mock_response, mock_response_result, @@ -20,7 +20,7 @@ json.loads = Mock(side_effect=mock_json) class TestRepoService(TestCase): def setUp(self): - self.rs = Repos() + self.rs = Repo() self.rs.set_user('octocat') self.rs.set_repo('octocat_repo') |