Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit81e6047

Browse files
committed
Clean-up github3.models and remove BaseCommit
1 parent10d9ac6 commit81e6047

File tree

1 file changed

+36
-54
lines changed

1 file changed

+36
-54
lines changed

‎github3/models.py‎

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"""This module provides the basic models used in github3.py."""
33
from __future__importunicode_literals
44

5-
fromjsonimportdumps,loads
6-
fromloggingimportgetLogger
5+
importjsonasjsonlib
6+
importlogging
77

88
importdateutil.parser
99
importrequests
10-
fromrequests.compatimportis_py2,urlparse
10+
importrequests.compat
1111

1212
from .importexceptions
13-
from .sessionimportGitHubSession
13+
from .importsession
1414

15-
__logs__=getLogger(__package__)
15+
LOG=logging.getLogger(__package__)
1616

1717

1818
classGitHubCore(object):
@@ -22,13 +22,18 @@ class GitHubCore(object):
2222
basic attributes and methods to other sub-classes that are very useful to
2323
have.
2424
"""
25+
2526
_ratelimit_resource='core'
2627

2728
def__init__(self,json,session):
28-
ifhasattr(session,'session'):
29-
# i.e. session is actually a GitHubCore instance
30-
session=session.session
31-
self.session=session
29+
"""Initialize our basic object.
30+
31+
Pretty much every object will pass in decoded JSON and a Session.
32+
"""
33+
# Either or 'session' is an instance of a GitHubCore sub-class or it
34+
# is a session. In the former case it will have a 'session' attribute.
35+
# If it doesn't, we can just default to using the passed in session.
36+
self.session=getattr(session,'session',session)
3237

3338
# set a sane default
3439
self._github_url='https://api.github.com'
@@ -76,7 +81,7 @@ def as_json(self):
7681
:returns: this object's attributes as a JSON string
7782
:rtype: str
7883
"""
79-
returndumps(self._json_data)
84+
returnjsonlib.dumps(self._json_data)
8085

8186
@classmethod
8287
def_get_attribute(cls,data,attribute,fallback=None):
@@ -129,7 +134,7 @@ def _strptime(cls, time_str):
129134

130135
def__repr__(self):
131136
repr_string=self._repr()
132-
ifis_py2:
137+
ifrequests.compat.is_py2:
133138
returnrepr_string.encode('utf-8')
134139
returnrepr_string
135140

@@ -141,7 +146,7 @@ def from_dict(cls, json_dict, session):
141146
@classmethod
142147
deffrom_json(cls,json,session):
143148
"""Return an instance of this class formed from ``json``."""
144-
returncls(loads(json),session)
149+
returncls(jsonlib.loads(json),session)
145150

146151
def__eq__(self,other):
147152
returnself._uniq==other._uniq
@@ -176,9 +181,9 @@ def _instance_or_null(self, instance_class, json):
176181
def_json(self,response,status_code,include_cache_info=True):
177182
ret=None
178183
ifself._boolean(response,status_code,404)andresponse.content:
179-
__logs__.info('Attempting to get JSON information from a Response '
180-
'with status code %d expecting %d',
181-
response.status_code,status_code)
184+
LOG.info('Attempting to get JSON information from a Response '
185+
'with status code %d expecting %d',
186+
response.status_code,status_code)
182187
ret=response.json()
183188
headers=response.headers
184189
if (include_cache_infoand
@@ -188,7 +193,7 @@ def _json(self, response, status_code, include_cache_info=True):
188193
'Last-Modified',''
189194
)
190195
ret['ETag']=response.headers.get('ETag','')
191-
__logs__.info('JSON was %sreturned','not 'ifretisNoneelse'')
196+
LOG.info('JSON was %sreturned','not 'ifretisNoneelse'')
192197
returnret
193198

194199
def_boolean(self,response,true_code,false_code):
@@ -212,29 +217,29 @@ def _request(self, method, *args, **kwargs):
212217
raiseexceptions.TransportError(exc)
213218

214219
def_delete(self,url,**kwargs):
215-
__logs__.debug('DELETE %s with %s',url,kwargs)
220+
LOG.debug('DELETE %s with %s',url,kwargs)
216221
returnself._request('delete',url,**kwargs)
217222

218223
def_get(self,url,**kwargs):
219-
__logs__.debug('GET %s with %s',url,kwargs)
224+
LOG.debug('GET %s with %s',url,kwargs)
220225
returnself._request('get',url,**kwargs)
221226

222227
def_patch(self,url,**kwargs):
223-
__logs__.debug('PATCH %s with %s',url,kwargs)
228+
LOG.debug('PATCH %s with %s',url,kwargs)
224229
returnself._request('patch',url,**kwargs)
225230

226231
def_post(self,url,data=None,json=True,**kwargs):
227232
ifjson:
228-
data=dumps(data)ifdataisnotNoneelsedata
229-
__logs__.debug('POST %s with %s, %s',url,data,kwargs)
233+
data=jsonlib.dumps(data)ifdataisnotNoneelsedata
234+
LOG.debug('POST %s with %s, %s',url,data,kwargs)
230235
returnself._request('post',url,data,**kwargs)
231236

232237
def_put(self,url,**kwargs):
233-
__logs__.debug('PUT %s with %s',url,kwargs)
238+
LOG.debug('PUT %s with %s',url,kwargs)
234239
returnself._request('put',url,**kwargs)
235240

236241
def_build_url(self,*args,**kwargs):
237-
"""Builds a new API url from scratch."""
242+
"""Build a new API url from scratch."""
238243
returnself.session.build_url(*args,**kwargs)
239244

240245
@property
@@ -247,7 +252,7 @@ def _api(self):
247252
@_api.setter
248253
def_api(self,uri):
249254
ifuri:
250-
self._uri=urlparse(uri)
255+
self._uri=requests.compat.urlparse(uri)
251256
self.url=uri
252257

253258
def_iter(self,count,url,cls,params=None,etag=None,headers=None):
@@ -316,34 +321,11 @@ def refresh(self, conditional=False):
316321
returnself
317322

318323
defnew_session(self):
319-
"""Helper function to generate a new session"""
320-
returnGitHubSession()
321-
322-
323-
classBaseCommit(GitHubCore):
324-
325-
"""This abstracts a lot of the common attributes for commit-like objects.
326-
327-
The :class:`BaseCommit <BaseCommit>` object serves as the base for
328-
the various types of commit objects returned by the API.
329-
"""
330-
331-
def_update_attributes(self,commit):
332-
self._api=self._get_attribute(commit,'url')
333-
334-
#: SHA of this commit.
335-
self.sha=self._get_attribute(commit,'sha')
336-
337-
#: Commit message
338-
self.message=self._get_attribute(commit,'message')
324+
"""Generate a new session.
339325
340-
#: List of parents to this commit.
341-
self.parents=self._get_attribute(commit,'parents', [])
342-
343-
#: URL to view the commit on GitHub
344-
self.html_url=self._get_attribute(commit,'html_url')
345-
ifnotself.sha:
346-
i=self._api.rfind('/')
347-
self.sha=self._api[i+1:]
348-
349-
self._uniq=self.sha
326+
:returns:
327+
A brand new session
328+
:rtype:
329+
:class:`~github3.session.GitHubSession`
330+
"""
331+
returnsession.GitHubSession()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp