8000 Merge pull request #680 from sigmavirus24/split-user-obj · osnr/github3.py@b58ff53 · GitHub
[go: up one dir, main page]

Skip to content

Commit b58ff53

Browse files
authored
Merge pull request sigmavirus24#680 from sigmavirus24/split-user-obj
Split User object into more accurate objects
2 parents 3e251f2 + 30be3c6 commit b58ff53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+697
-437
lines changed

github3/events.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
from .models import GitHubCore
1414

1515

16+
class EventUser(GitHubCore):
17+
"""The class that represents the user information returned in Events."""
18+
19+
def _update_attributes(self, user):
20+
self.avatar_url = user['avatar_url']
21+
self.display_login = user.get('display_login')
22+
self.gravatar_id = user['id']
23+
self.id = user['id']
24+
self.login = user['login']
25+
self._api = self.url = user['url']
26+
27+
def to_user(self):
28+
"""Retrieve a full User object for this EventUser."""
29+
from . import users
30+
url = self._build_url('users', self.login)
31+
json = self._json(self._get(url), 200)
32+
return self._instance_or_null(users.User, json)
33+
34+
1635
class Event(GitHubCore):
1736

1837
"""The :class:`Event <Event>` object. It structures and handles the data
@@ -37,10 +56,9 @@ def _update_attributes(self, event):
3756
# not want to do:
3857
event = copy.deepcopy(event)
3958

40-
from .users import User
4159
from .orgs import Organization
4260
#: :class:`User <github3.users.User>` object representing the actor.
43-
self.actor = self._class_attribute(event, 'actor', User)
61+
self.actor = self._class_attribute(event, 'actor', EventUser)
4462
#: datetime object representing when the event was created.
4563
self.created_at = self._strptime_attribute(event, 'created_at')
4664

@@ -83,9 +101,8 @@ def _commitcomment(payload, session):
83101

84102

85103
def _follow(payload, session):
86-
from .users import User
87104
if payload.get('target'):
88-
payload['target'] = User(payload['target'], session)
105+
payload['target'] = EventUser(payload['target'], session)
89106
return payload
90107

91108

@@ -121,9 +138,8 @@ def _issueevent(payload, session):
121138

122139

123140
def _member(payload, session):
124-
from .users import User
125141
if payload.get('member'):
126-
payload['member'] = User(payload['member'], session)
142+
payload['member'] = EventUser(payload['member'], session)
127143
return payload
128144

129145

@@ -160,13 +176,12 @@ def _release(payload, session):
160176
def _team(payload, session):
161177
from .orgs import Team
162178
from .repos import Repository
163-
from .users import User
164179
if payload.get('team'):
165180
payload['team'] = Team(payload['team'], session)
166181
if payload.get('repo'):
167182
payload['repo'] = Repository(payload['repo'], session)
168183
if payload.get('sender'):
169-
payload['sender'] = User(payload['sender'], session)
184+
payload['sender'] = EventUser(payload['sender'], session)
170185
return payload
171186

172187

github3/gists/comment.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"""
99
from __future__ import unicode_literals
1010

11+
from .. import users
1112
from ..models import BaseComment
12-
from ..users import User
1313

1414

1515
class GistComment(BaseComment):
@@ -34,7 +34,9 @@ def _update_attributes(self, comment):
3434
self._api = self._get_attribute(comment, 'url')
3535
#: :class:`User <github3.users.User>` who made the comment
3636
#: Unless it is not associated with an account
37-
self.user = self._class_attribute(comment, 'user', User, self)
37+
self.user = self._class_attribute(
38+
comment, 'user', users.ShortUser, self,
39+
)
3840

3941
def _repr(self):
4042
return '<Gist Comment [{0}]>'.format(self.user.login)

github3/gists/gist.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
from __future__ import unicode_literals
1010

1111
from json import dumps
12+
13+
from .. import users
14+
1215
from ..models import GitHubCore
1316
from ..decorators import requires_auth
1417
from .comment import GistComment
1518
from .file import GistFile
1619
from .history import GistHistory
17-
from ..users import User
1820

1921

2022
class Gist(GitHubCore):
@@ -75,7 +77,9 @@ def _update_attributes(self, data):
7577

7678
#: :class:`User <github3.users.User>` object representing the owner of
7779
#: the gist.
78-
self.owner = self._class_attribute(data, 'owner', User, self)
80+
self.owner = self._class_attribute(
81+
data, 'owner', users.ShortUser, self,
82+
)
7983

8084
self._files = self._get_attribute(data, 'files', [])
8185
if self._files:

github3/gists/history.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"""
99
from __future__ import unicode_literals
1010

11+
from .. import users
1112
from ..models import GitHubCore
12-
from ..users import User
1313

1414

1515
class GistHistory(GitHubCore):
@@ -35,7 +35,9 @@ def _update_attributes(self, history):
3535
self.version = self._get_attribute(history, 'version')
3636

3737
#: user who made these changes
38-
self.user = self._class_attribute(history, 'user', User, self)
38+
self.user = self._class_attribute(
39+
history, 'user', users.ShortUser, self,
40+
)
3941

4042
#: dict containing the change status; see also: deletions, additions,
4143
#: total

github3/git.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from json import dumps
1313
from base64 import b64decode
1414
from .models import GitHubCore, BaseCommit
15-
from .users import User
1615
from .decorators import requires_auth
1716

1817

@@ -92,22 +91,6 @@ def _update_attributes(self, commit):
9291
def _repr(self):
9392
return '<Commit [{0}:{1}]>'.format(self._author_name, self.sha)
9493

95-
def author_as_User(self):
96-
"""Attempt to return the author attribute as a
97-
:class:`User <github3.users.User>`. No guarantees are made about the
98-
validity of this object, i.e., having a login or created_at object.
99-
100-
"""
101-
return User(self.author, self)
102-
103-
def committer_as_User(self):
104-
"""Attempt to return the committer attribute as a
105-
:class:`User <github3.users.User>` object. No guarantees are made
106-
about the validity of this object.
107-
108-
"""
109-
return User(self.committer, self)
110-
11194

11295
class Reference(GitHubCore):
11396

@@ -195,14 +178,6 @@ def _update_attributes(self, tag):
195178
def _repr(self):
196179
return '<Tag [{0}]>'.format(self.tag)
197180

198-
def tagger_as_User(self):
199-
"""Attempt to return the tagger attribute as a
200-
:class:`User <github3.users.User>`. No guarantees are made about the
201-
validity of this object, i.e., having a login or created_at object.
202-
203-
"""
204-
return User(self.tagger, self)
205-
206181

207182
class Tree(GitData):
208183

github3/github.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ def all_users(self, number=-1, etag=None, per_page=None, since=None):
145145
:param str etag: (optional), ETag from a previous request to the same
146146
endpoint
147147
:param int per_page: (optional), number of users to list per request
148-
:returns: generator of :class:`User <github3.users.User>`
148+
:returns: generator of :class:`~github3.users.ShortUser`
149149
"""
150150
url = self._build_url('users')
151-
return self._iter(int(number), url, users.User, etag=etag,
151+
return self._iter(int(number), url, users< AE9B /span>.ShortUser, etag=etag,
152152
params={'per_page': per_page, 'since': since})
153153

154154
@requires_basic_auth
@@ -447,7 +447,7 @@ def follow(self, username):
447447
return resp
448448

449449
def followed_by(self, username, number=-1, etag=None):
450-
"""Iterate over users being followed by ``username``.
450+
r"""Iterate over users being followed by ``username``.
451451
452452
.. versionadded:: 1.0.0
453453
@@ -458,14 +458,14 @@ def followed_by(self, username, number=-1, etag=None):
458458
returns all people you follow
459459
:param str etag: (optional), ETag from a previous request to the same
460460
endpoint
461-
:returns: generator of :class:`User <github3.users.User>`\ s
461+
:returns: generator of :class:`~github3.users.ShortUser`\ s
462462
"""
463463
url = self._build_url('users', username, 'following')
464-
return self._iter(int(number), url, users.User, etag=etag)
464+
return self._iter(int(number), url, users.ShortUser, etag=etag)
465465

466466
@requires_auth
467467
def followers(self, number=-1, etag=None):
468-
"""Iterate over followers of the authenticated user.
468+
r"""Iterate over followers of the authenticated user.
469469
470470
.. versionadded:: 1.0.0
471471
@@ -475,13 +475,13 @@ def followers(self, number=-1, etag=None):
475475
-1 returns all followers
476476
:param str etag: (optional), ETag from a previous request to the same
477477
endpoint
478-
:returns: generator of :class:`User <github3.users.User>`\ s
478+
:returns: generator of :class:`~github3.users.ShortUser`\ s
479479
"""
480480
url = self._build_url('user', 'followers')
481-
return self._iter(int(number), url, users.User, etag=etag)
481+
return self._iter(int(number), url, users.ShortUser, etag=etag)
482482

483483
def followers_of(self, username, number=-1, etag=None):
484-
"""Iterate over followers of ``username``.
484+
r"""Iterate over followers of ``username``.
485485
486486
.. versionadded:: 1.0.0
487487
@@ -492,14 +492,14 @@ def followers_of(self, username, number=-1, etag=None):
492492
-1 returns all followers
493493
:param str etag: (optional), ETag from a previous request to the same
494494
endpoint
495-
:returns: generator of :class:`User <github3.users.User>`\ s
495+
:returns: generator of :class:`~github3.users.ShortUser`\ s
496496
"""
497497
url = self._build_url('users', username, 'followers')
498-
return self._iter(int(number), url, users.User, etag=etag)
498+
return self._iter(int(number), url, users.ShortUser, etag=etag)
499499

500500
@requires_auth
501501
def following(self, number=-1, etag=None):
502-
"""Iterate over users the authenticated user is following.
502+
r"""Iterate over users the authenticated user is following.
503503
504504
.. versionadded:: 1.0.0
505505
@@ -509,10 +509,10 @@ def following(self, number=-1, etag=None):
509509
returns all people you follow
510510
:param str etag: (optional), ETag from a previous request to the same
511511
endpoint
512-
:returns: generator of :class:`User <github3.users.User>`\ s
512+
:returns: generator of :class:`~github3.users.ShortUser`\ s
513513
"""
514514
url = self._build_url('user', 'following')
515-
return self._iter(int(number), url, users.User, etag=etag)
515+
return self._iter(int(number), url, users.ShortUser, etag=etag)
516516

517517
def gist(self, id_num):
518518
"""Retrieve the gist using the specified id number.
@@ -800,18 +800,18 @@ def markdown(self, text, mode='', context='', raw=False):
800800

801801
@requires_auth
802802
def me(self):
803-
"""Retrieves the info for the authenticated user.
803+
"""Retrieve the info for the authenticated user.
804804
805805
.. versionadded:: 1.0
806806
807807
This was separated from the ``user`` method.
808808
809809
:returns: The representation of the authenticated user.
810-
:rtype: :class:`User <github3.users.User>`
810+
:rtype: :class:`~github3.users.AuthenticatedUser`
811811
"""
812812
url = self._build_url('user')
813813
json = self._json(self._get(url), 200)
814-
return self._instance_or_null(users.User, json)
814+
return self._instance_or_null(users.AuthenticatedUser, json)
815815

816816
@requires_auth
817817
def membership_in(self, organization):
@@ -1588,10 +1588,10 @@ def update_me(self, name=None, email=None, blog=None, company=None,
15881588
return False
15891589

15901590
def user(self, username):
1591-
"""Returns a User object for the specified user name.
1591+
"""Retrieve a User object for the specified user name.
15921592
15931593
:param str username: name of the user
1594-
:returns: :class:`User <github3.users.User>`
1594+
:returns: :class:`~github3.users.User`
15951595
"""
15961596
url = self._build_url('users', username)
15971597
json = self._json(self._get(url), 200)
@@ -1657,7 +1657,7 @@ def user_with_id(self, number):
16571657
"""Get the user's information with id ``number``.
16581658
16591659
:param int number: the user's id number
1660-
:returns: :class:`User <github3.users.User>`
1660+
:returns: :class:`~github3.users.User`
16611661
"""
16621662
number = int(number)
16631663
json = None

github3/issues/comment.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
from .. import users
45
from ..utils import timestamp_parameter
56
from ..models import BaseComment
6-
from ..users import User
77

88

99
class IssueComment(BaseComment):
@@ -26,7 +26,9 @@ def _update_attributes(self, comment):
2626
super(IssueComment, self)._update_attributes(comment)
2727

2828
#: :class:`User <github3.users.User>` who made the comment
29-
self.user = self._class_attribute(comment, 'user', User, self)
29+
self.user = self._class_attribute(
30+
comment, 'user', users.ShortUser, self,
31+
)
3032

3133
#: Issue url (not a template)
3234
self.issue_url = self._get_attribute(comment, 'issue_url')

github3/issues/event.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
from .. import users
45
from ..models import GitHubCore
5-
from ..users import User
66

77

88
class IssueEvent(GitHubCore):
@@ -37,7 +37,9 @@ def _update_attributes(self, event):
3737
self.issue = self._class_attribute(event, 'issue', Issue, self)
3838

3939
#: :class:`User <github3.users.User>` who caused this event.
40-
self.actor = self._class_attribute(event, 'actor', User, self)
40+
self.actor = self._class_attribute(
41+
event, 'actor', users.ShortUser, self,
42+
)
4143

4244
#: Number of comments
4345
self.comments = self._get_attribute(event, 'comments')
@@ -55,7 +57,9 @@ def _update_attributes(self, event):
5557
self.id = self._get_attribute(event, 'id')
5658

5759
#: :class:`User <github3.users.User>` that is assigned
58-
self.assignee = self._class_attribute(event, 'assignee', User, self)
60+
self.assignee = self._class_attribute(
61+
event, 'assignee', users.ShortUser, self,
62+
)
5963

6064
#: Dictionary containing milestone details
6165
self.milestone = self._get_attribute(event, 'milestone', {})

0 commit comments

Comments
 (0)
0