Add support to create Repository objects lazily in MainClass.

This lets users do the following without issuing an unnecessary request:

issues_of_my_repository = g.get_repo('PyGithub/PyGithub').get_issues()

Since the GithubObject layer handles laziness transparently, lazy
loading is enabled by default, and can be disabled via the `lazy`
kwarg. This does not affect any test except tests that pertain to
caching or laziness itself.
This commit is contained in:
Uriel Corfa
2015-03-26 20:56:27 -07:00
parent ca13204dd5
commit 626d29c938
8 changed files with 111 additions and 15 deletions
+4 -1
View File
@@ -188,13 +188,16 @@ class Github(object):
)
return github.Organization.Organization(self.__requester, headers, data, completed=True)
def get_repo(self, full_name_or_id):
def get_repo(self, full_name_or_id, lazy=True):
"""
:calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ or `GET /repositories/:id <http://developer.github.com/v3/repos>`_
:rtype: :class:`github.Repository.Repository`
"""
assert isinstance(full_name_or_id, (str, unicode, int)), full_name_or_id
url_base = "/repositories/" if isinstance(full_name_or_id, int) else "/repos/"
url = "%s%s" % (url_base, full_name_or_id)
if lazy:
return Repository.Repository(self.__requester, {}, {"url": url}, completed=False)
headers, data = self.__requester.requestJsonAndCheck(
"GET",
"%s%s" % (url_base, full_name_or_id)
+3 -3
View File
@@ -31,14 +31,14 @@ import github
class ConditionalRequestUpdate(Framework.TestCase):
def setUp(self):
Framework.TestCase.setUp(self)
self.repo = self.g.get_repo("akfish/PyGithub")
self.repo = self.g.get_repo("akfish/PyGithub", lazy=False)
def testDidNotUpdate(self):
self.assertFalse(self.repo.update(), msg="The repo is not changes. But update() != False")
self.assertFalse(self.repo.update(), msg="The repo is not changed. But update() != False")
def testDidUpdate(self):
self.assertTrue(self.repo.update(), msg="The repo should be changed by now. But update() != True")
def testUpdateObjectWithoutEtag(self):
r = self.g.get_repo("jacquev6/PyGithub")
r = self.g.get_repo("jacquev6/PyGithub", lazy=False)
self.assertTrue(r.update())
@@ -1,14 +1,3 @@
https
GET
api.github.com
None
/repos/klmitch/turnstile
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
null
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4999'), ('x-github-media-type', 'github.beta; format=json'), ('x-content-type-options', 'nosniff'), ('access-control-expose-headers', 'ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes'), ('x-github-request-id', 'b9d67757-009d-4a30-9854-f41e04681849'), ('access-control-allow-credentials', 'true'), ('vary', 'Accept, Authorization, Cookie, Accept-Encoding'), ('content-length', '4587'), ('server', 'GitHub.com'), ('last-modified', 'Wed, 21 Aug 2013 16:04:54 GMT'), ('x-ratelimit-limit', '5000'), ('etag', '"aab62d006633c3842d38e20dc732a2c9"'), ('cache-control', 'private, max-age=60, s-maxage=60'), ('date', 'Thu, 12 Sep 2013 09:09:24 GMT'), ('access-control-allow-origin', '*'), ('content-type', 'application/json; charset=utf-8'), ('x-ratelimit-reset', '1378980564')]
{"id":3404510,"name":"turnstile","full_name":"klmitch/turnstile","owner":{"login":"klmitch","id":686398,"avatar_url":"https://1.gravatar.com/avatar/3c505225c6f28a7702b318a991141495?d=https%3A%2F%2Fidenticons.github.com%2Ffffa0f2e30bad5753edbb60f250b7cbe.png","gravatar_id":"3c505225c6f28a7702b318a991141495","url":"https://api.github.com/users/klmitch","html_url":"https://github.com/klmitch","followers_url":"https://api.github.com/users/klmitch/followers","following_url":"https://api.github.com/users/klmitch/following{/other_user}","gists_url":"https://api.github.com/users/klmitch/gists{/gist_id}","starred_url":"https://api.github.com/users/klmitch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/klmitch/subscriptions","organizations_url":"https://api.github.com/users/klmitch/orgs","repos_url":"https://api.github.com/users/klmitch/repos","events_url":"https://api.github.com/users/klmitch/events{/privacy}","received_events_url":"https://api.github.com/users/klmitch/received_events","type":"User"},"private":false,"html_url":"https://github.com/klmitch/turnstile","description":"A distributed rate limiting WSGI middleware.","fork":false,"url":"https://api.github.com/repos/klmitch/turnstile","forks_url":"https://api.github.com/repos/klmitch/turnstile/forks","keys_url":"https://api.github.com/repos/klmitch/turnstile/keys{/key_id}","collaborators_url":"https://api.github.com/repos/klmitch/turnstile/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/klmitch/turnstile/teams","hooks_url":"https://api.github.com/repos/klmitch/turnstile/hooks","issue_events_url":"https://api.github.com/repos/klmitch/turnstile/issues/events{/number}","events_url":"https://api.github.com/repos/klmitch/turnstile/events","assignees_url":"https://api.github.com/repos/klmitch/turnstile/assignees{/user}","branches_url":"https://api.github.com/repos/klmitch/turnstile/branches{/branch}","tags_url":"https://api.github.com/repos/klmitch/turnstile/tags","blobs_url":"https://api.github.com/repos/klmitch/turnstile/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/klmitch/turnstile/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/klmitch/turnstile/git/refs{/sha}","trees_url":"https://api.github.com/repos/klmitch/turnstile/git/trees{/sha}","statuses_url":"https://api.github.com/repos/klmitch/turnstile/statuses/{sha}","languages_url":"https://api.github.com/repos/klmitch/turnstile/languages","stargazers_url":"https://api.github.com/repos/klmitch/turnstile/stargazers","contributors_url":"https://api.github.com/repos/klmitch/turnstile/contributors","subscribers_url":"https://api.github.com/repos/klmitch/turnstile/subscribers","subscription_url":"https://api.github.com/repos/klmitch/turnstile/subscription","commits_url":"https://api.github.com/repos/klmitch/turnstile/commits{/sha}","git_commits_url":"https://api.github.com/repos/klmitch/turnstile/git/commits{/sha}","comments_url":"https://api.github.com/repos/klmitch/turnstile/comments{/number}","issue_comment_url":"https://api.github.com/repos/klmitch/turnstile/issues/comments/{number}","contents_url":"https://api.github.com/repos/klmitch/turnstile/contents/{+path}","compare_url":"https://api.github.com/repos/klmitch/turnstile/compare/{base}...{head}","merges_url":"https://api.github.com/repos/klmitch/turnstile/merges","archive_url":"https://api.github.com/repos/klmitch/turnstile/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/klmitch/turnstile/downloads","issues_url":"https://api.github.com/repos/klmitch/turnstile/issues{/number}","pulls_url":"https://api.github.com/repos/klmitch/turnstile/pulls{/number}","milestones_url":"https://api.github.com/repos/klmitch/turnstile/milestones{/number}","notifications_url":"https://api.github.com/repos/klmitch/turnstile/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/klmitch/turnstile/labels{/name}","created_at":"2012-02-10T05:16:36Z","updated_at":"2013-08-21T16:04:54Z","pushed_at":"2013-05-01T22:22:20Z","git_url":"git://github.com/klmitch/turnstile.git","ssh_url":"git@github.com:klmitch/turnstile.git","clone_url":"https://github.com/klmitch/turnstile.git","svn_url":"https://github.com/klmitch/turnstile","homepage":"","size":260,"watchers_count":15,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":6,"mirror_url":null,"open_issues_count":1,"forks":6,"open_issues":1,"watchers":15,"master_branch":"master","default_branch":"master","permissions":{"admin":false,"push":false,"pull":true},"network_count":6}
https
GET
api.github.com
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/user
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
null
200
[('content-length', '1304'), ('vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding'), ('x-served-by', 'c6c65e5196703428e7641f7d1e9bc353'), ('x-oauth-scopes', 'gist, repo, user'), ('x-xss-protection', '1; mode=block'), ('x-content-type-options', 'nosniff'), ('x-accepted-oauth-scopes', ''), ('etag', '"975c21d2f55751d13745ffc5fa12b1c2"'), ('access-control-allow-credentials', 'true'), ('status', '200 OK'), ('x-ratelimit-remaining', '4960'), ('x-github-media-type', 'github.v3; format=json'), ('access-control-expose-headers', 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval'), ('x-github-request-id', '6CE875F7:1E5F:42689E:5514C7D8'), ('cache-control', 'private, max-age=60, s-maxage=60'), ('last-modified', 'Fri, 27 Mar 2015 02:56:43 GMT'), ('date', 'Fri, 27 Mar 2015 03:00:40 GMT'), ('access-control-allow-origin', '*'), ('content-security-policy', "default-src 'none'"), ('strict-transport-security', 'max-age=31536000; includeSubdomains; preload'), ('server', 'GitHub.com'), ('x-ratelimit-limit', '5000'), ('x-frame-options', 'deny'), ('content-type', 'application/json; charset=utf-8'), ('x-ratelimit-reset', '1427427963')]
{"login":"korfuri","id":1124263,"avatar_url":"https://avatars.githubusercontent.com/u/1124263?v=3","gravatar_id":"","url":"https://api.github.com/users/korfuri","html_url":"https://github.com/korfuri","followers_url":"https://api.github.com/users/korfuri/followers","following_url":"https://api.github.com/users/korfuri/following{/other_user}","gists_url":"https://api.github.com/users/korfuri/gists{/gist_id}","starred_url":"https://api.github.com/users/korfuri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/korfuri/subscriptions","organizations_url":"https://api.github.com/users/korfuri/orgs","repos_url":"https://api.github.com/users/korfuri/repos","events_url":"https://api.github.com/users/korfuri/events{/privacy}","received_events_url":"https://api.github.com/users/korfuri/received_events","type":"User","site_admin":false,"name":"Uriel Corfa","company":"","blog":"http://korfuri.fr/","location":"","email":"uriel@corfa.fr","hireable":false,"bio":null,"public_repos":13,"public_gists":0,"followers":29,"following":57,"created_at":"2011-10-13T02:27:26Z","updated_at":"2015-03-27T02:56:43Z","private_gists":0,"total_private_repos":0,"owned_private_repos":0,"disk_usage":444,"collaborators":0,"plan":{"name":"free","space":976562499,"collaborators":0,"private_repos":0}}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/user
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
null
200
[('content-length', '1304'), ('vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding'), ('x-served-by', '07ff1c8a09e44b62e277fae50a1b1dc4'), ('x-oauth-scopes', 'gist, repo, user'), ('x-xss-protection', '1; mode=block'), ('x-content-type-options', 'nosniff'), ('x-accepted-oauth-scopes', ''), ('etag', '"6754cf6361cd6d31646de909f5c90146"'), ('access-control-allow-credentials', 'true'), ('status', '200 OK'), ('x-ratelimit-remaining', '4996'), ('x-github-media-type', 'github.v3; format=json'), ('access-control-expose-headers', 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval'), ('x-github-request-id', '6CE875F7:1E63:532872:5514C5BC'), ('cache-control', 'private, max-age=60, s-maxage=60'), ('last-modified', 'Fri, 27 Mar 2015 02:25:51 GMT'), ('date', 'Fri, 27 Mar 2015 02:51:40 GMT'), ('access-control-allow-origin', '*'), ('content-security-policy', "default-src 'none'"), ('strict-transport-security', 'max-age=31536000; includeSubdomains; preload'), ('server', 'GitHub.com'), ('x-ratelimit-limit', '5000'), ('x-frame-options', 'deny'), ('content-type', 'application/json; charset=utf-8'), ('x-ratelimit-reset', '1427427963')]
{"login":"korfuri","id":1124263,"avatar_url":"https://avatars.githubusercontent.com/u/1124263?v=3","gravatar_id":"","url":"https://api.github.com/users/korfuri","html_url":"https://github.com/korfuri","followers_url":"https://api.github.com/users/korfuri/followers","following_url":"https://api.github.com/users/korfuri/following{/other_user}","gists_url":"https://api.github.com/users/korfuri/gists{/gist_id}","starred_url":"https://api.github.com/users/korfuri/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/korfuri/subscriptions","organizations_url":"https://api.github.com/users/korfuri/orgs","repos_url":"https://api.github.com/users/korfuri/repos","events_url":"https://api.github.com/users/korfuri/events{/privacy}","received_events_url":"https://api.github.com/users/korfuri/received_events","type":"User","site_admin":false,"name":"Uriel Corfa","company":"","blog":"http://korfuri.fr/","location":"","email":"uriel@corfa.fr","hireable":false,"bio":null,"public_repos":12,"public_gists":0,"followers":29,"following":57,"created_at":"2011-10-13T02:27:26Z","updated_at":"2015-03-27T02:25:51Z","private_gists":0,"total_private_repos":0,"owned_private_repos":0,"disk_usage":444,"collaborators":0,"plan":{"name":"free","space":976562499,"collaborators":0,"private_repos":0}}
File diff suppressed because one or more lines are too long
+27
View File
@@ -492,3 +492,30 @@ class Repository(Framework.TestCase):
stats = self.repo.get_stats_punch_card()
self.assertEqual(stats.get(4, 12), 7)
self.assertEqual(stats.get(6, 18), 2)
class LazyRepository(Framework.TestCase):
def setUp(self):
Framework.TestCase.setUp(self)
self.user = self.g.get_user()
self.repository_name = '%s/%s' % (self.user.login, "PyGithub")
def getLazyRepository(self):
return self.g.get_repo(self.repository_name, lazy=True)
def getEagerRepository(self):
return self.g.get_repo(self.repository_name, lazy=False)
def testGetIssues(self):
lazy_repo = self.getLazyRepository()
issues = lazy_repo.get_issues()
eager_repo = self.getEagerRepository()
issues2 = eager_repo.get_issues()
self.assertListKeyEqual(issues2, id, [x for x in issues])
def testOwner(self):
lazy_repo = self.getLazyRepository()
owner = lazy_repo.owner
eager_repo = self.getEagerRepository()
self.assertEqual(owner, eager_repo.owner)