Skip to content

Commit 991641a

Browse files
committed
Refactor the git module as per my TODO list
1 parent 0aad666 commit 991641a

2 files changed

Lines changed: 71 additions & 157 deletions

File tree

.refactor_macros

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"Pull a docstring and place it after a #:
2+
let @q = '2f"ldi"2g;a ""'
3+
4+
" vim: ft=vim

github3/git.py

Lines changed: 67 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,35 @@
88

99
from base64 import b64decode
1010
from json import dumps
11-
from .models import GitHubObject, GitHubCore, BaseCommit
12-
from .users import User
11+
from github3.models import GitHubObject, GitHubCore, BaseCommit
12+
from github3.users import User
13+
from github3.decorators import requires_auth
1314

1415

1516
class Blob(GitHubObject):
1617
"""The :class:`Blob <Blob>` object."""
1718
def __init__(self, blob):
1819
super(Blob, self).__init__(blob)
1920
self._api = blob.get('url')
20-
self._content = blob.get('content').encode()
21-
self._enc = blob.get('encoding')
22-
if self._enc == 'base64':
23-
self._decoded = b64decode(self._content)
24-
else:
25-
self._decoded = self._content
26-
self._size = blob.get('size')
27-
self._sha = blob.get('sha')
2821

29-
def __repr__(self):
30-
return '<Blob [{0:.10}]>'.format(self._sha)
31-
32-
@property
33-
def content(self):
34-
"""Raw content of the blob."""
35-
return self._content
22+
#: Raw content of the blob.
23+
self.content = blob.get('content').encode()
3624

37-
@property
38-
def decoded(self):
39-
"""Decoded content of the blob."""
40-
return self._decoded
25+
#: Encoding of the raw content.
26+
self.encoding = blob.get('encoding')
4127

42-
@property
43-
def encoding(self):
44-
"""Encoding of the raw content."""
45-
return self._enc
28+
#: Decoded content of the blob.
29+
self.decoded = self.content
30+
if self.encoding == 'base64':
31+
self.decoded = b64decode(self.content)
4632

47-
@property
48-
def sha(self):
49-
"""SHA1 of the blob"""
50-
return self._sha
33+
#: Size of the blob in bytes
34+
self.size = blob.get('size')
35+
#: SHA1 of the blob
36+
self.sha = blob.get('sha')
5137

52-
@property
53-
def size(self):
54-
"""Size of the blob in bytes"""
55-
return self._size
38+
def __repr__(self):
39+
return '<Blob [{0:.10}]>'.format(self.sha)
5640

5741

5842
class GitData(GitHubCore):
@@ -62,17 +46,13 @@ class GitData(GitHubCore):
6246
"""
6347
def __init__(self, data, session=None):
6448
super(GitData, self).__init__(data, session)
65-
self._sha = data.get('sha')
49+
#: SHA of the object
50+
self.sha = data.get('sha')
6651
self._api = data.get('url')
6752

6853
def __repr__(self):
6954
return '<github3-gitdata at 0x{0:x}>'.format(id(self))
7055

71-
@property
72-
def sha(self):
73-
"""SHA of the object"""
74-
return self._sha
75-
7656

7757
class Commit(BaseCommit):
7858
"""The :class:`Commit <Commit>` object. This represents a commit made in a
@@ -81,47 +61,29 @@ class Commit(BaseCommit):
8161
def __init__(self, commit, session=None):
8262
super(Commit, self).__init__(commit, session)
8363

84-
self._author = ''
85-
self._author_name = ''
64+
#: :class:`User <github3.users.User>` who authored the commit.
65+
self.author = commit.get('author')
66+
self._author_name = commit.get('author')
8667
if commit.get('author') and len(commit.get('author')) > 3:
8768
# User object
8869
# Typically there should be 5 keys, but more than 3 should
8970
# be a sufficient test
90-
self._author = User(commit.get('author'), None)
91-
self._author_name = self._author.login
71+
self.author = User(commit.get('author'), None)
9272
elif commit.get('author'): # Not a User object
93-
self._author = type('Author', (object, ), commit.get('author'))
94-
self._author_name = self._author.name
73+
self.author = type('Author', (object, ), commit.get('author'))
9574

96-
self._committer = ''
75+
#: :class:`User <github3.user.User>` who committed the commit.
76+
self.committer = None
9777
if commit.get('committer'):
98-
if len(commit.get('committer')) > 3:
99-
self._committer = User(commit.get('committer'), None)
100-
else:
101-
self._committer = type('Committer', (object, ),
102-
commit.get('committer'))
78+
self.committer = User(commit.get('committer'), None)
10379

104-
self._tree = None
80+
#: :class:`Tree <Tree>` the commit belongs to.
81+
self.tree = None
10582
if commit.get('tree'):
106-
self._tree = Tree(commit.get('tree'), self._session)
83+
self.tree = Tree(commit.get('tree'), self._session)
10784

10885
def __repr__(self):
109-
return '<Commit [{0}:{1}]>'.format(self._author_name, self._sha)
110-
111-
@property
112-
def author(self):
113-
""":class:`User <github3.users.User>` who authored the commit."""
114-
return self._author
115-
116-
@property
117-
def committer(self):
118-
""":class:`User <github3.user.User>` who committed the commit."""
119-
return self._committer
120-
121-
@property
122-
def tree(self):
123-
""":class:`Tree <Tree>` the commit belongs to."""
124-
return self._tree
86+
return '<Commit [{0}:{1}]>'.format(self.author.name, self._sha)
12587

12688

12789
class Reference(GitHubCore):
@@ -130,35 +92,26 @@ class Reference(GitHubCore):
13092
"""
13193
def __init__(self, ref, session=None):
13294
super(Reference, self).__init__(ref, session)
133-
self._update_(ref)
95+
#: The reference path, e.g., refs/heads/sc/featureA
96+
self.ref = ref.get('ref')
97+
#: :class:`GitObject <GitObject>` the reference points to
98+
self.object = GitObject(ref.get('object'))
13499

135100
def __repr__(self):
136-
return '<Reference [{0}]>'.format(self._ref)
101+
return '<Reference [{0}]>'.format(self.ref)
137102

138103
def _update_(self, ref):
139-
self._ref = ref.get('ref')
140-
self._api = ref.get('url')
141-
self._obj = GitObject(ref.get('object'))
104+
self.__init__(ref, self._session)
142105

143-
@GitHubCore.requires_auth
106+
@requires_auth
144107
def delete(self):
145108
"""Delete this reference.
146109
147110
:returns: bool
148111
"""
149112
return self._boolean(self._delete(self._api), 204, 404)
150113

151-
@property
152-
def object(self):
153-
""":class:`GitObject <GitObject>` the reference points to"""
154-
return self._obj
155-
156-
@property
157-
def ref(self):
158-
"""The reference path, e.g., refs/heads/sc/featureA"""
159-
return self._ref
160-
161-
@GitHubCore.requires_auth
114+
@requires_auth
162115
def update(self, sha, force=False):
163116
"""Update this reference.
164117
@@ -180,52 +133,35 @@ class GitObject(GitData):
180133
"""The :class:`GitObject <GitObject>` object."""
181134
def __init__(self, obj):
182135
super(GitObject, self).__init__(obj, None)
183-
self._type = obj.get('type')
136+
#: The type of object.
137+
self.type = obj.get('type')
184138

185139
def __repr__(self):
186-
return '<Git Object [{0}]>'.format(self._sha)
187-
188-
@property
189-
def type(self):
190-
"""The type of object."""
191-
return self._type
140+
return '<Git Object [{0}]>'.format(self.sha)
192141

193142

194143
class Tag(GitData):
195144
def __init__(self, tag):
196145
super(Tag, self).__init__(tag, None)
197-
self._tag = tag.get('tag')
198-
self._msg = tag.get('message')
199-
self._tagger = None
200-
if tag.get('tagger'):
201-
self._tagger = type('Tagger', (object, ), tag.get('tagger'))
202-
self._obj = GitObject(tag.get('object'))
146+
#: String of the tag
147+
self.tag = tag.get('tag')
148+
#: Commit message for the tag
149+
self.message = tag.get('message')
150+
#: dict containing the name and email of the person
151+
self.tagger = tag.get('tagger')
152+
#: :class:`GitObject <GitObject>` for the tag
153+
self.object = GitObject(tag.get('object'))
203154

204155
def __repr__(self):
205156
return '<Tag [{0}]>'.format(self._tag)
206157

207-
@property
208-
def message(self):
209-
return self._msg
210-
211-
@property
212-
def object(self):
213-
return self._obj
214-
215-
@property
216-
def tag(self):
217-
return self._tag
218-
219-
@property
220-
def tagger(self):
221-
return self._tagger
222-
223158

224159
class Tree(GitData):
225160
"""The :class:`Tree <Tree>` object."""
226161
def __init__(self, tree, session=None):
227162
super(Tree, self).__init__(tree, session)
228-
self._tree = [Hash(t) for t in tree.get('tree', [])]
163+
#: list of :class:`Hash <Hash>` objects
164+
self.tree = [Hash(t) for t in tree.get('tree', [])]
229165

230166
def __repr__(self):
231167
return '<Tree [{0}]>'.format(self._sha)
@@ -239,49 +175,23 @@ def recurse(self):
239175
json = self._json(self._get(url), 200)
240176
return Tree(json, self._session) if json else None
241177

242-
@property
243-
def tree(self):
244-
"""list of :class:`Hash <Hash>` objects"""
245-
return self._tree
246-
247178

248179
class Hash(GitHubObject):
249180
"""The :class:`Hash <Hash>` object."""
250181
def __init__(self, info):
251182
super(Hash, self).__init__(info)
252-
self._path = info.get('path')
253-
self._mode = info.get('mode')
254-
self._type = info.get('type')
255-
self._size = info.get('size')
256-
self._sha = info.get('sha')
257-
self._url = info.get('url')
258-
259-
@property
260-
def mode(self):
261-
"""File mode"""
262-
return self._mode
263-
264-
@property
265-
def path(self):
266-
"""Path to file"""
267-
return self._path
268-
269-
@property
270-
def sha(self):
271-
"""SHA of the hash"""
272-
return self._sha
273-
274-
@property
275-
def size(self):
276-
"""Size of hash"""
277-
return self._size
278-
279-
@property
280-
def type(self):
281-
"""Type of hash, e.g., blob"""
282-
return self._type
283-
284-
@property
285-
def url(self):
286-
"""URL of this object in the GitHub API"""
287-
return self._url
183+
#: Path to file
184+
self.path = info.get('path')
185+
#: File mode
186+
self.mode = info.get('mode')
187+
#: Type of hash, e.g., blob
188+
self.type = info.get('type')
189+
#: Size of hash
190+
self.size = info.get('size')
191+
#: SHA of the hash
192+
self.sha = info.get('sha')
193+
#: URL of this object in the GitHub API
194+
self.url = info.get('url')
195+
196+
def __repr__(self):
197+
return '<Hash [{0}]>'.format(self.sha)

0 commit comments

Comments
 (0)