8000 Clean-up github3.models and remove BaseCommit · pythonthings/github3.py@81e6047 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81e6047

Browse files
committed
Clean-up github3.models and remove BaseCommit
1 parent 10d9ac6 commit 81e6047

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__ import unicode_literals
44

5-
from json import dumps, loads
6-
from logging import getLogger
5+
import json as jsonlib
6+
import logging
77

88
import dateutil.parser
99
import requests
10-
from requests.compat import is_py2, urlparse
10+
import requests.compat
1111

1212
from . import exceptions
13-
from .session import GitHubSession
13+
from . import session
1414

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

1717

1818
class GitHubCore(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-
if hasattr(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-
return dumps(self._json_data)
84+
return jsonlib.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-
if is_py2:
137+
if requests.compat.is_py2:
133138
return repr_string.encode('utf-8')
134139
return repr_string
135140

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

146151
def __eq__(self, other):
147152
return self._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
if self._boolean(response, status_code, 404) and response.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_info and
@@ -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 ' if ret is None else '')
196+
LOG.info('JSON was %sreturned', 'not ' if ret is None else '')
192197
return ret
193198

194199
def _boolean(self, response, true_code, false_code):
@@ -212,29 +217,29 @@ def _request(self, method, *args, **kwargs):
212217
raise exceptions.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
return self._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
return self._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
return self._request('patch', url, **kwargs)
225230

226231
def _post(self, url, data=None, json=True, **kwargs):
227232
if json:
228-
data = dumps(data) if data is not None else data
229< A3E2 /td>-
__logs__.debug('POST %s with %s, %s', url, data, kwargs)
233+
data = jsonlib.dumps(data) if data is not None else data
234+
LOG.debug('POST %s with %s, %s', url, data, kwargs)
230235
return self._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
return self._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
return self.session.build_url(*args, **kwargs)
239244

240245
@property
@@ -247,7 +252,7 @@ def _api(self):
247252
@_api.setter
248253
def _api(self, uri):
249254
if uri:
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
return self
317322

318323
def new_session(self):
319-
"""Helper function to generate a new session"""
320-
return GitHubSession()
321-
322-
323-
class BaseCommit(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-
if not self.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+
return session.GitHubSession()

0 commit comments

Comments
 (0)
0