Skip to content

Commit

Permalink
Add code search
Browse files Browse the repository at this point in the history
Code search results are content resources with an embedded repository.
The "content" and "encoding" attributes aren't returned and require
completion.
  • Loading branch information
thialfihar committed Feb 8, 2014
1 parent b1524e2 commit 3afae93
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions github/ContentFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
################################################################################

import github.GithubObject
import github.Repository


class ContentFile(github.GithubObject.CompletableGithubObject):
Expand Down Expand Up @@ -80,6 +81,17 @@ def path(self):
self._completeIfNotSet(self._path)
return self._path.value

@property
def repository(self):
"""
:type: :class:`github.Repository.Repository`
"""
if self._repository is github.GithubObject.NotSet:
# The repository was not set automatically, so it must be looked up by url.
repo_url = "/".join(self.url.split("/")[:6])
self._repository = github.GithubObject._ValuedAttribute(github.Repository.Repository(self._requester, self._headers, {'url': repo_url}, completed=False))
return self._repository.value

@property
def sha(self):
"""
Expand Down Expand Up @@ -119,6 +131,7 @@ def _initAttributes(self):
self._html_url = github.GithubObject.NotSet
self._name = github.GithubObject.NotSet
self._path = github.GithubObject.NotSet
self._repository = github.GithubObject.NotSet
self._sha = github.GithubObject.NotSet
self._size = github.GithubObject.NotSet
self._type = github.GithubObject.NotSet
Expand All @@ -136,6 +149,8 @@ def _useAttributes(self, attributes):
self._name = self._makeStringAttribute(attributes["name"])
if "path" in attributes: # pragma no branch
self._path = self._makeStringAttribute(attributes["path"])
if "repository" in attributes: # pragma no branch
self._repository = self._makeClassAttribute(github.Repository.Repository, attributes["repository"])
if "sha" in attributes: # pragma no branch
self._sha = self._makeStringAttribute(attributes["sha"])
if "size" in attributes: # pragma no branch
Expand Down
35 changes: 35 additions & 0 deletions github/MainClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ def search_issues(self, query, sort=github.GithubObject.NotSet, order=github.Git
url_parameters
)

def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers):
"""
:calls: `GET /search/issues <http://developer.github.com/v3/search>`_
:param query: string
:param sort: string ('indexed')
:param order: string ('asc', 'desc')
:param qualifiers: keyword dict query qualifiers
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.CodeSearchResult.CodeSearchResult`
"""
assert isinstance(query, (str, unicode)), query
url_parameters = dict()
if sort is not github.GithubObject.NotSet:
assert sort in ('indexed',), sort
url_parameters["sort"] = sort
if order is not github.GithubObject.NotSet:
assert order in ('asc', 'desc'), order
url_parameters["order"] = order

query_chunks = []
if query:
query_chunks.append(urllib.quote(query))

for qualifier, value in qualifiers.items():
query_chunks.append("%s:%s" % (qualifier, value))

url_parameters["q"] = ' '.join(query_chunks)
assert url_parameters["q"], "need at least one qualifier"

return github.PaginatedList.PaginatedList(
github.ContentFile.ContentFile,
self.__requester,
"/search/code",
url_parameters
)

def render_markdown(self, text, context=github.GithubObject.NotSet):
"""
:calls: `POST /markdown <http://developer.github.com/v3/markdown>`_
Expand Down

0 comments on commit 3afae93

Please sign in to comment.