/gdata/projecthosting/client.py

http://radioappz.googlecode.com/ · Python · 200 lines · 154 code · 7 blank · 39 comment · 2 complexity · 85b2d9886ab461b56bd952162862aad1 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import atom.data
  17. import gdata.client
  18. import gdata.gauth
  19. import gdata.projecthosting.data
  20. class ProjectHostingClient(gdata.client.GDClient):
  21. """Client to interact with the Project Hosting GData API."""
  22. api_version = '1.0'
  23. auth_service = 'code'
  24. auth_scopes = gdata.gauth.AUTH_SCOPES['code']
  25. host = 'code.google.com'
  26. def get_issues(self, project_name,
  27. desired_class=gdata.projecthosting.data.IssuesFeed, **kwargs):
  28. """Get a feed of issues for a particular project.
  29. Args:
  30. project_name str The name of the project.
  31. query Query Set returned issues parameters.
  32. Returns:
  33. data.IssuesFeed
  34. """
  35. return self.get_feed(gdata.projecthosting.data.ISSUES_FULL_FEED %
  36. project_name, desired_class=desired_class, **kwargs)
  37. def add_issue(self, project_name, title, content, author,
  38. status=None, owner=None, labels=None, ccs=None, **kwargs):
  39. """Create a new issue for the project.
  40. Args:
  41. project_name str The name of the project.
  42. title str The title of the new issue.
  43. content str The summary of the new issue.
  44. author str The authenticated user's username.
  45. status str The status of the new issue, Accepted, etc.
  46. owner str The username of new issue's owner.
  47. labels [str] Labels to associate with the new issue.
  48. ccs [str] usernames to Cc on the new issue.
  49. Returns:
  50. data.IssueEntry
  51. """
  52. new_entry = gdata.projecthosting.data.IssueEntry(
  53. title=atom.data.Title(text=title),
  54. content=atom.data.Content(text=content),
  55. author=[atom.data.Author(name=atom.data.Name(text=author))])
  56. if status:
  57. new_entry.status = gdata.projecthosting.data.Status(text=status)
  58. if owner:
  59. owner = [gdata.projecthosting.data.Owner(
  60. username=gdata.projecthosting.data.Username(text=owner))]
  61. if labels:
  62. new_entry.label = [gdata.projecthosting.data.Label(text=label)
  63. for label in labels]
  64. if ccs:
  65. new_entry.cc = [
  66. gdata.projecthosting.data.Cc(
  67. username=gdata.projecthosting.data.Username(text=cc))
  68. for cc in ccs]
  69. return self.post(
  70. new_entry,
  71. gdata.projecthosting.data.ISSUES_FULL_FEED % project_name,
  72. **kwargs)
  73. def update_issue(self, project_name, issue_id, author, comment=None,
  74. summary=None, status=None, owner=None, labels=None, ccs=None,
  75. **kwargs):
  76. """Update or comment on one issue for the project.
  77. Args:
  78. project_name str The name of the issue's project.
  79. issue_id str The issue number needing updated.
  80. author str The authenticated user's username.
  81. comment str A comment to append to the issue
  82. summary str Rewrite the summary of the issue.
  83. status str A new status for the issue.
  84. owner str The username of the new owner.
  85. labels [str] Labels to set on the issue (prepend issue with - to remove a
  86. label).
  87. ccs [str] Ccs to set on th enew issue (prepend cc with - to remove a cc).
  88. Returns:
  89. data.CommentEntry
  90. """
  91. updates = gdata.projecthosting.data.Updates()
  92. if summary:
  93. updates.summary = gdata.projecthosting.data.Summary(text=summary)
  94. if status:
  95. updates.status = gdata.projecthosting.data.Status(text=status)
  96. if owner:
  97. updates.ownerUpdate = gdata.projecthosting.data.OwnerUpdate(text=owner)
  98. if labels:
  99. updates.label = [gdata.projecthosting.data.Label(text=label)
  100. for label in labels]
  101. if ccs:
  102. updates.ccUpdate = [gdata.projecthosting.data.CcUpdate(text=cc)
  103. for cc in ccs]
  104. update_entry = gdata.projecthosting.data.CommentEntry(
  105. content=atom.data.Content(text=comment),
  106. author=[atom.data.Author(name=atom.data.Name(text=author))],
  107. updates=updates)
  108. return self.post(
  109. update_entry,
  110. gdata.projecthosting.data.COMMENTS_FULL_FEED % (project_name, issue_id),
  111. **kwargs)
  112. def get_comments(self, project_name, issue_id,
  113. desired_class=gdata.projecthosting.data.CommentsFeed,
  114. **kwargs):
  115. """Get a feed of all updates to an issue.
  116. Args:
  117. project_name str The name of the issue's project.
  118. issue_id str The issue number needing updated.
  119. Returns:
  120. data.CommentsFeed
  121. """
  122. return self.get_feed(
  123. gdata.projecthosting.data.COMMENTS_FULL_FEED % (project_name, issue_id),
  124. desired_class=desired_class, **kwargs)
  125. def update(self, entry, auth_token=None, force=False, **kwargs):
  126. """Unsupported GData update method.
  127. Use update_*() instead.
  128. """
  129. raise NotImplementedError(
  130. 'GData Update operation unsupported, try update_*')
  131. def delete(self, entry_or_uri, auth_token=None, force=False, **kwargs):
  132. """Unsupported GData delete method.
  133. Use update_issue(status='Closed') instead.
  134. """
  135. raise NotImplementedError(
  136. 'GData Delete API unsupported, try closing the issue instead.')
  137. class Query(gdata.client.Query):
  138. def __init__(self, issue_id=None, label=None, canned_query=None, owner=None,
  139. status=None, **kwargs):
  140. """Constructs a Google Data Query to filter feed contents serverside.
  141. Args:
  142. issue_id: int or str The issue to return based on the issue id.
  143. label: str A label returned issues must have.
  144. canned_query: str Return issues based on a canned query identifier
  145. owner: str Return issues based on the owner of the issue. For Gmail users,
  146. this will be the part of the email preceding the '@' sign.
  147. status: str Return issues based on the status of the issue.
  148. """
  149. super(Query, self).__init__(**kwargs)
  150. self.label = label
  151. self.issue_id = issue_id
  152. self.canned_query = canned_query
  153. self.owner = owner
  154. self.status = status
  155. def modify_request(self, http_request):
  156. if self.issue_id:
  157. gdata.client._add_query_param('id', self.issue_id, http_request)
  158. if self.label:
  159. gdata.client._add_query_param('label', self.label, http_request)
  160. if self.canned_query:
  161. gdata.client._add_query_param('can', self.canned_query, http_request)
  162. if self.owner:
  163. gdata.client._add_query_param('owner', self.owner, http_request)
  164. if self.status:
  165. gdata.client._add_query_param('status', self.status, http_request)
  166. super(Query, self).modify_request(http_request)
  167. ModifyRequest = modify_request