22"""This module provides the basic models used in github3.py."""
33from __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
88import dateutil .parser
99import requests
10- from requests .compat import is_py2 , urlparse
10+ import requests .compat
1111
1212from .import exceptions
13- from .session import GitHubSession
13+ from .import session
1414
15- __logs__ = getLogger (__package__ )
15+ LOG = logging . getLogger (__package__ )
1616
1717
1818class 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
2728def __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
3439self ._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
8287def _get_attribute (cls ,data ,attribute ,fallback = None ):
@@ -129,7 +134,7 @@ def _strptime(cls, time_str):
129134
130135def __repr__ (self ):
131136repr_string = self ._repr ()
132- if is_py2 :
137+ if requests . compat . is_py2 :
133138return repr_string .encode ('utf-8' )
134139return repr_string
135140
@@ -141,7 +146,7 @@ def from_dict(cls, json_dict, session):
141146@classmethod
142147def 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
146151def __eq__ (self ,other ):
147152return self ._uniq == other ._uniq
@@ -176,9 +181,9 @@ def _instance_or_null(self, instance_class, json):
176181def _json (self ,response ,status_code ,include_cache_info = True ):
177182ret = None
178183if 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 )
182187ret = response .json ()
183188headers = response .headers
184189if (include_cache_info and
@@ -188,7 +193,7 @@ def _json(self, response, status_code, include_cache_info=True):
188193'Last-Modified' ,''
189194 )
190195ret ['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 '' )
192197return ret
193198
194199def _boolean (self ,response ,true_code ,false_code ):
@@ -212,29 +217,29 @@ def _request(self, method, *args, **kwargs):
212217raise exceptions .TransportError (exc )
213218
214219def _delete (self ,url ,** kwargs ):
215- __logs__ .debug ('DELETE %s with %s' ,url ,kwargs )
220+ LOG .debug ('DELETE %s with %s' ,url ,kwargs )
216221return self ._request ('delete' ,url ,** kwargs )
217222
218223def _get (self ,url ,** kwargs ):
219- __logs__ .debug ('GET %s with %s' ,url ,kwargs )
224+ LOG .debug ('GET %s with %s' ,url ,kwargs )
220225return self ._request ('get' ,url ,** kwargs )
221226
222227def _patch (self ,url ,** kwargs ):
223- __logs__ .debug ('PATCH %s with %s' ,url ,kwargs )
228+ LOG .debug ('PATCH %s with %s' ,url ,kwargs )
224229return self ._request ('patch' ,url ,** kwargs )
225230
226231def _post (self ,url ,data = None ,json = True ,** kwargs ):
227232if json :
228- data = dumps (data )if data is not None else data
229- __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 )
230235return self ._request ('post' ,url ,data ,** kwargs )
231236
232237def _put (self ,url ,** kwargs ):
233- __logs__ .debug ('PUT %s with %s' ,url ,kwargs )
238+ LOG .debug ('PUT %s with %s' ,url ,kwargs )
234239return self ._request ('put' ,url ,** kwargs )
235240
236241def _build_url (self ,* args ,** kwargs ):
237- """Builds a new API url from scratch."""
242+ """Build a new API url from scratch."""
238243return self .session .build_url (* args ,** kwargs )
239244
240245@property
@@ -247,7 +252,7 @@ def _api(self):
247252@_api .setter
248253def _api (self ,uri ):
249254if uri :
250- self ._uri = urlparse (uri )
255+ self ._uri = requests . compat . urlparse (uri )
251256self .url = uri
252257
253258def _iter (self ,count ,url ,cls ,params = None ,etag = None ,headers = None ):
@@ -316,34 +321,11 @@ def refresh(self, conditional=False):
316321return self
317322
318323def 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 ()