Skip to content

Commit 73362a1

Browse files
committed
Merge branch 'no-get-attribute-773' of omgjlk
Merging omgjlk's branch into consistency-is-a-hobgoblin
2 parents c49af2c + 45103a5 commit 73362a1

File tree

7 files changed

+116
-52
lines changed

7 files changed

+116
-52
lines changed

github3/.DS_Store

6 KB
Binary file not shown.

github3/auths.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,34 @@ class Authorization(GitHubCore):
3131
"""
3232

3333
def _update_attributes(self, auth):
34-
self._api = self._get_attribute(auth, 'url')
34+
self._api = auth['url']
3535

3636
#: Details about the application (name, url)
37-
self.app = self._get_attribute(auth, 'app', {})
37+
self.app = auth['app']
3838

3939
#: Returns the Authorization token
40-
self.token = self._get_attribute(auth, 'token')
40+
self.token = auth['token']
4141

4242
#: App name
43-
self.name = self._get_attribute(self.app, 'name')
43+
self.name = self.app['name']
4444

4545
#: URL about the note
46-
self.note_url = self._get_attribute(auth, 'note_url')
46+
self.note_url = auth['note_url']
4747

4848
#: Note about the authorization
49-
self.note = self._get_attribute(auth, 'note')
49+
self.note = auth['note']
5050

5151
#: List of scopes this applies to
52-
self.scopes = self._get_attribute(auth, 'scopes')
52+
self.scopes = auth['scopes']
5353

5454
#: Unique id of the authorization
55-
self.id = self._get_attribute(auth, 'id')
55+
self.id = auth['id']
5656

5757
#: datetime object representing when the authorization was created.
58-
self.created_at = self._strptime_attribute(auth, 'created_at')
58+
self.created_at = self._strptime(auth['created_at'])
5959

6060
#: datetime object representing when the authorization was updated.
61-
self.updated_at = self._strptime_attribute(auth, 'updated_at')
61+
self.updated_at = self._strptime(auth['updated_at'])
6262

6363
def _repr(self):
6464
return '<Authorization [{0}]>'.format(self.name)

github3/events.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -154,31 +154,36 @@ def _update_attributes(self, event):
154154
event = copy.deepcopy(event)
155155

156156
#: :class:`User <github3.users.User>` object representing the actor.
157-
self.actor = self._class_attribute(event, 'actor', EventUser, self)
157+
self.actor = EventUser(event['actor'], self)
158158
#: datetime object representing when the event was created.
159-
self.created_at = self._strptime_attribute(event, 'created_at')
159+
self.created_at = self._strptime(event['created_at'])
160160

161161
#: Unique id of the event
162-
self.id = self._get_attribute(event, 'id')
162+
self.id = event['id']
163163

164-
#: List all possible types of Events
165-
self.org = self._class_attribute(event, 'org', EventOrganization, self)
164+
#: :class:`EventOrganization <github3.events.EventOrganization>`
165+
# object representing the org.
166+
# an event only has an org if the event relates to a resource owned
167+
# by an org.
168+
self.org = event.get('org')
169+
if self.org:
170+
self.org = EventOrganization(event['org'], self)
166171

167172
#: Event type https://developer.github.com/v3/activity/events/types/
168-
self.type = self._get_attribute(event, 'type')
173+
self.type = event['type']
169174
handler = _payload_handlers.get(self.type, identity)
170175

171176
#: Dictionary with the payload. Payload structure is defined by type_.
172177
# _type: http://developer.github.com/v3/events/types
173-
self.payload = self._class_attribute(event, 'payload', handler, self)
178+
self.payload = handler(event['payload'], self)
174179

175180
#: Return ``tuple(owner, repository_name)``
176-
self.repo = self._get_attribute(event, 'repo')
181+
self.repo = event['repo']
177182
if self.repo:
178183
self.repo = tuple(self.repo['name'].split('/'))
179184

180185
#: Indicates whether the Event is public or not.
181-
self.public = self._get_attribute(event, 'public')
186+
self.public = event['public']
182187

183188
def _repr(self):
184189
return '<Event [{0}]>'.format(self.type[:-5])

github3/git.py

+59-31
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ class Blob(GitHubCore):
2424
"""
2525

2626
def _update_attributes(self, blob):
27-
self._api = self._get_attribute(blob, 'url')
27+
self._api = blob['url']
2828

2929
#: Raw content of the blob.
30-
self.content = self._get_attribute(blob, 'content')
30+
self.content = blob['content']
3131
if self.content is not None:
3232
self.content = self.content.encode()
3333

3434
#: Encoding of the raw content.
35-
self.encoding = self._get_attribute(blob, 'encoding')
35+
self.encoding = blob['encoding']
3636

3737
#: Decoded content of the blob.
3838
self.decoded = self.content
3939
if self.encoding == 'base64':
4040
self.decoded = b64decode(self.content)
4141

4242
#: Size of the blob in bytes
43-
self.size = self._get_attribute(blob, 'size')
43+
self.size = blob['size']
4444
#: SHA1 of the blob
45-
self.sha = self._get_attribute(blob, 'sha')
45+
self.sha = blob['sha']
4646

4747
def _repr(self):
4848
return '<Blob [{0:.10}]>'.format(self.sha)
@@ -58,8 +58,8 @@ class GitData(GitHubCore):
5858

5959
def _update_attributes(self, data):
6060
#: SHA of the object
61-
self.sha = self._get_attribute(data, 'sha')
62-
self._api = self._get_attribute(data, 'url')
61+
self.sha = data['sha']
62+
self._api = data['url']
6363

6464

6565
class Commit(BaseCommit):
@@ -75,21 +75,25 @@ def _update_attributes(self, commit):
7575
super(Commit, self)._update_attributes(commit)
7676
#: dict containing at least the name, email and date the commit was
7777
#: created
78-
self.author = self._get_attribute(commit, 'author', {})
79-
# If GH returns nil/None then make sure author is a dict
80-
self._author_name = self._get_attribute(self.author, 'name')
78+
self.author = commit['author']
79+
# GitHub may not provide a name for the author
80+
if self.author.get('name'):
81+
self._author_name = self.author['name']
8182

8283
#: dict containing similar information to the author attribute
83-
self.committer = self._get_attribute(commit, 'committer', {})
84-
# blank the data if GH returns no data
84+
# If the committer is not different from the author, we may not get
85+
# a committer key
86+
if commit.get('committer'):
87+
self.committer = commit['committer']
8588

86-
self._commit_name = self._get_attribute(self.committer, 'name')
89+
if self.committer.get('name'):
90+
self._commit_name = self.committer['name']
8791

88-
#: :class:`Tree <Tree>` the commit belongs to.
89-
self.tree = self._class_attribute(commit, 'tree', Tree, self)
92+
#: :class:`CommitTree <CommitTree>` the commit belongs to.
93+
self.tree = CommitTree(commit['tree'], self)
9094

9195
def _repr(self):
92-
return '<Commit [{0}:{1}]>'.format(self._author_name, self.sha)
96+
return '<Commit [{0}]>'.format(self.sha)
9397

9498

9599
class Reference(GitHubCore):
@@ -102,13 +106,13 @@ class Reference(GitHubCore):
102106
"""
103107

104108
def _update_attributes(self, ref):
105-
self._api = self._get_attribute(ref, 'url')
109+
self._api = ref['url']
106110

107111
#: The reference path, e.g., refs/heads/sc/featureA
108-
self.ref = self._get_attribute(ref, 'ref')
112+
self.ref = ref['ref']
109113

110114
#: :class:`GitObject <GitObject>` the reference points to
111-
self.object = self._class_attribute(ref, 'object', GitObject, self)
115+
self.object = GitObject(ref['object'], self)
112116

113117
def _repr(self):
114118
return '<Reference [{0}]>'.format(self.ref)
@@ -146,7 +150,7 @@ class GitObject(GitData):
146150
def _update_attributes(self, obj):
147151
super(GitObject, self)._update_attributes(obj)
148152
#: The type of object.
149-
self.type = self._get_attribute(obj, 'type')
153+
self.type = obj['type']
150154

151155
def _repr(self):
152156
return '<Git Object [{0}]>'.format(self.sha)
@@ -164,21 +168,43 @@ def _update_attributes(self, tag):
164168
super(Tag, self)._update_attributes(tag)
165169

166170
#: String of the tag
167-
self.tag = self._get_attribute(tag, 'tag')
171+
self.tag = tag['tag']
168172

169173
#: Commit message for the tag
170-
self.message = self._get_attribute(tag, 'message')
174+
self.message = tag['message']
171175

172176
#: dict containing the name and email of the person
173-
self.tagger = self._get_attribute(tag, 'tagger')
177+
self.tagger = tag['tagger']
174178

175179
#: :class:`GitObject <GitObject>` for the tag
176-
self.object = self._class_attribute(tag, 'object', GitObject, self)
180+
self.object = GitObject(tag['object'], self)
177181

178182
def _repr(self):
179183
return '<Tag [{0}]>'.format(self.tag)
180184

181185

186+
class CommitTree(GitData):
187+
188+
"""The :class:`CommitTree <CommitTree>` object.
189+
190+
Represents the tree data found in a commit object
191+
192+
"""
193+
194+
def _update_attributes(self, tree):
195+
super(CommitTree, self)._update_attributes(tree)
196+
197+
def _repr(self):
198+
return '<CommitTree [{0}]>'.format(self.sha)
199+
200+
def to_tree(self):
201+
"""Retrieve a full Tree object for this CommitTree."""
202+
json = self._json(self._get(self._api), 200)
203+
return self._instance_or_null(Tree, json)
204+
205+
refresh = to_tree
206+
207+
182208
class Tree(GitData):
183209

184210
"""The :class:`Tree <Tree>` object.
@@ -191,7 +217,7 @@ def _update_attributes(self, tree):
191217
super(Tree, self)._update_attributes(tree)
192218

193219
#: list of :class:`Hash <Hash>` objects
194-
self.tree = self._get_attribute(tree, 'tree', [])
220+
self.tree = tree['tree']
195221
if self.tree:
196222
self.tree = [Hash(t, self) for t in self.tree]
197223

@@ -224,22 +250,24 @@ class Hash(GitHubCore):
224250

225251
def _update_attributes(self, info):
226252
#: Path to file
227-
self.path = self._get_attribute(info, 'path')
253+
self.path = info['path']
228254

229255
#: File mode
230-
self.mode = self._get_attribute(info, 'mode')
256+
self.mode = info['mode']
231257

232258
#: Type of hash, e.g., blob
233-
self.type = self._get_attribute(info, 'type')
259+
self.type = info['type']
234260

235261
#: Size of hash
236-
self.size = self._get_attribute(info, 'size')
262+
# Size is not set if the type is a tree
263+
if self.type != 'tree':
264+
self.size = info['size']
237265

238266
#: SHA of the hash
239-
self.sha = self._get_attribute(info, 'sha')
267+
self.sha = info['sha']
240268

241269
#: URL of this object in the GitHub API
242-
self.url = self._get_attribute(info, 'url')
270+
self.url = info['url']
243271

244272
def _repr(self):
245273
return '<Hash [{0}]>'.format(self.sha)

tests/unit/json/git_commit_example

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
3+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
4+
"author": {
5+
"date": "2014-11-07T22:01:45Z",
6+
"name": "Scott Chacon",
7+
"email": "schacon@gmail.com"
8+
},
9+
"committer": {
10+
"date": "2014-11-07T22:01:45Z",
11+
"name": "Scott Chacon",
12+
"email": "schacon@gmail.com"
13+
},
14+
"message": "added readme, because im a good github citizen",
15+
"tree": {
16+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
17+
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
18+
},
19+
"parents": [
20+
{
21+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
22+
"sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
23+
}
24+
],
25+
"verification": {
26+
"verified": false,
27+
"reason": "unsigned",
28+
"signature": null,
29+
"payload": null
30+
}
31+
}

tests/unit/test_git.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'octocat/Hello-World/'
1111
'git/refs/heads/featureA')
1212

13-
get_commit_example_data = create_example_data_helper('commit_example')
13+
get_commit_example_data = create_example_data_helper('git_commit_example')
1414
get_git_tag_example_data = create_example_data_helper('git_tag_example')
1515
get_reference_example_data = create_example_data_helper('reference_example')
1616

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{27,34,35,py},py{27,34}-flake8,docstrings
2+
envlist = py{27,34,35,36,py},py{27,34,36}-flake8,docstrings
33
minversion = 2.5.0
44

55
[testenv]

0 commit comments

Comments
 (0)