Implement Repository.get_dir_contents (issue #140)

This commit is contained in:
Vincent Jacques
2013-02-16 19:09:25 +01:00
parent 34015323fc
commit 04adab121e
11 changed files with 111 additions and 14 deletions
+16 -1
View File
@@ -16,35 +16,47 @@
import github.GithubObject
class ContentFile(github.GithubObject.BasicGithubObject):
class ContentFile(github.GithubObject.GithubObject):
@property
def content(self):
self._completeIfNotSet(self._content)
return self._NoneIfNotSet(self._content)
@property
def encoding(self):
self._completeIfNotSet(self._encoding)
return self._NoneIfNotSet(self._encoding)
@property
def name(self):
self._completeIfNotSet(self._name)
return self._NoneIfNotSet(self._name)
@property
def path(self):
self._completeIfNotSet(self._path)
return self._NoneIfNotSet(self._path)
@property
def sha(self):
self._completeIfNotSet(self._sha)
return self._NoneIfNotSet(self._sha)
@property
def size(self):
self._completeIfNotSet(self._size)
return self._NoneIfNotSet(self._size)
@property
def type(self):
self._completeIfNotSet(self._type)
return self._NoneIfNotSet(self._type)
@property
def url(self):
self._completeIfNotSet(self._url)
return self._NoneIfNotSet(self._url)
def _initAttributes(self):
self._content = github.GithubObject.NotSet
self._encoding = github.GithubObject.NotSet
@@ -76,3 +88,6 @@ class ContentFile(github.GithubObject.BasicGithubObject):
if "type" in attributes: # pragma no branch
assert attributes["type"] is None or isinstance(attributes["type"], (str, unicode)), attributes["type"]
self._type = attributes["type"]
if "url" in attributes: # pragma no branch
assert attributes["url"] is None or isinstance(attributes["url"], (str, unicode)), attributes["url"]
self._url = attributes["url"]
+20
View File
@@ -591,6 +591,9 @@ class Repository(github.GithubObject.GithubObject):
)
def get_contents(self, path, ref=github.GithubObject.NotSet):
return self.get_file_contents(path, ref)
def get_file_contents(self, path, ref=github.GithubObject.NotSet):
assert isinstance(path, (str, unicode)), path
assert ref is github.GithubObject.NotSet or isinstance(ref, (str, unicode)), ref
url_parameters = dict()
@@ -604,6 +607,23 @@ class Repository(github.GithubObject.GithubObject):
)
return github.ContentFile.ContentFile(self._requester, data, completed=True)
def get_dir_contents(self, path, ref=github.GithubObject.NotSet):
assert isinstance(path, (str, unicode)), path
assert ref is github.GithubObject.NotSet or isinstance(ref, (str, unicode)), ref
url_parameters = dict()
if ref is not github.GithubObject.NotSet:
url_parameters["ref"] = ref
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.url + "/contents" + path,
url_parameters,
None
)
return [
github.ContentFile.ContentFile(self._requester, attributes, completed=attributes["type"]!="file") # Lazy completion only makes sense for files. See discussion here: https://github.com/jacquev6/PyGithub/issues/140#issuecomment-13481130
for attributes in data
]
def get_contributors(self):
return github.PaginatedList.PaginatedList(
github.NamedUser.NamedUser,
+1
View File
@@ -64,3 +64,4 @@ from Issue131 import *
from Issue133 import *
from Issue134 import *
from Issue139 import *
from Issue140 import *
+41
View File
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Copyright 2012 Vincent Jacques
# vincent@vincent-jacques.net
# This file is part of PyGithub. http://vincent-jacques.net/PyGithub
# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see <http://www.gnu.org/licenses/>.
import Framework
import github
class Issue140(Framework.TestCase): # https://github.com/jacquev6/PyGithub/issues/140
def setUp(self):
Framework.TestCase.setUp(self)
self.repo = self.g.get_repo("twitter/bootstrap")
def testGetDirContentsThenLazyCompletionOfFile(self):
contents = self.repo.get_dir_contents("/js")
self.assertEqual(len(contents), 15)
n = 0
for content in contents:
if content.path == "js/bootstrap-affix.js":
self.assertEqual(len(content.content), 4722) # Lazy completion
n += 1
elif content.path == "js/tests":
self.assertEqual(content.content, None) # No completion at all
n += 1
self.assertEqual(n, 2)
def testGetFileContents(self):
contents = self.repo.get_file_contents("/js/bootstrap-affix.js")
self.assertEqual(contents.encoding, "base64")
self.assertEqual(len(contents.content), 4722)
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long