/gdata/sites/data.py

http://radioappz.googlecode.com/ · Python · 376 lines · 212 code · 107 blank · 57 comment · 24 complexity · f3ef95a271557e879f97092ef4e5e755 MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright 2009 Google Inc. All Rights Reserved.
  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. """Data model classes for parsing and generating XML for the Sites Data API."""
  17. __author__ = 'e.bidelman (Eric Bidelman)'
  18. import atom.core
  19. import atom.data
  20. import gdata.acl.data
  21. import gdata.data
  22. # XML Namespaces used in Google Sites entities.
  23. SITES_NAMESPACE = 'http://schemas.google.com/sites/2008'
  24. SITES_TEMPLATE = '{http://schemas.google.com/sites/2008}%s'
  25. SPREADSHEETS_NAMESPACE = 'http://schemas.google.com/spreadsheets/2006'
  26. SPREADSHEETS_TEMPLATE = '{http://schemas.google.com/spreadsheets/2006}%s'
  27. DC_TERMS_TEMPLATE = '{http://purl.org/dc/terms}%s'
  28. THR_TERMS_TEMPLATE = '{http://purl.org/syndication/thread/1.0}%s'
  29. XHTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'
  30. XHTML_TEMPLATE = '{http://www.w3.org/1999/xhtml}%s'
  31. SITES_PARENT_LINK_REL = SITES_NAMESPACE + '#parent'
  32. SITES_REVISION_LINK_REL = SITES_NAMESPACE + '#revision'
  33. SITES_SOURCE_LINK_REL = SITES_NAMESPACE + '#source'
  34. SITES_KIND_SCHEME = 'http://schemas.google.com/g/2005#kind'
  35. ANNOUNCEMENT_KIND_TERM = SITES_NAMESPACE + '#announcement'
  36. ANNOUNCEMENT_PAGE_KIND_TERM = SITES_NAMESPACE + '#announcementspage'
  37. ATTACHMENT_KIND_TERM = SITES_NAMESPACE + '#attachment'
  38. COMMENT_KIND_TERM = SITES_NAMESPACE + '#comment'
  39. FILECABINET_KIND_TERM = SITES_NAMESPACE + '#filecabinet'
  40. LISTITEM_KIND_TERM = SITES_NAMESPACE + '#listitem'
  41. LISTPAGE_KIND_TERM = SITES_NAMESPACE + '#listpage'
  42. WEBPAGE_KIND_TERM = SITES_NAMESPACE + '#webpage'
  43. WEBATTACHMENT_KIND_TERM = SITES_NAMESPACE + '#webattachment'
  44. FOLDER_KIND_TERM = SITES_NAMESPACE + '#folder'
  45. SUPPORT_KINDS = [
  46. 'announcement', 'announcementspage', 'attachment', 'comment', 'filecabinet',
  47. 'listitem', 'listpage', 'webpage', 'webattachment'
  48. ]
  49. class Revision(atom.core.XmlElement):
  50. """Google Sites <sites:revision>."""
  51. _qname = SITES_TEMPLATE % 'revision'
  52. class PageName(atom.core.XmlElement):
  53. """Google Sites <sites:pageName>."""
  54. _qname = SITES_TEMPLATE % 'pageName'
  55. class SiteName(atom.core.XmlElement):
  56. """Google Sites <sites:siteName>."""
  57. _qname = SITES_TEMPLATE % 'siteName'
  58. class Theme(atom.core.XmlElement):
  59. """Google Sites <sites:theme>."""
  60. _qname = SITES_TEMPLATE % 'theme'
  61. class Deleted(atom.core.XmlElement):
  62. """Google Sites <gd:deleted>."""
  63. _qname = gdata.data.GDATA_TEMPLATE % 'deleted'
  64. class Publisher(atom.core.XmlElement):
  65. """Google Sites <dc:pulisher>."""
  66. _qname = DC_TERMS_TEMPLATE % 'publisher'
  67. class Worksheet(atom.core.XmlElement):
  68. """Google Sites List Page <gs:worksheet>."""
  69. _qname = SPREADSHEETS_TEMPLATE % 'worksheet'
  70. name = 'name'
  71. class Header(atom.core.XmlElement):
  72. """Google Sites List Page <gs:header>."""
  73. _qname = SPREADSHEETS_TEMPLATE % 'header'
  74. row = 'row'
  75. class Column(atom.core.XmlElement):
  76. """Google Sites List Page <gs:column>."""
  77. _qname = SPREADSHEETS_TEMPLATE % 'column'
  78. index = 'index'
  79. name = 'name'
  80. class Data(atom.core.XmlElement):
  81. """Google Sites List Page <gs:data>."""
  82. _qname = SPREADSHEETS_TEMPLATE % 'data'
  83. startRow = 'startRow'
  84. column = [Column]
  85. class Field(atom.core.XmlElement):
  86. """Google Sites List Item <gs:field>."""
  87. _qname = SPREADSHEETS_TEMPLATE % 'field'
  88. index = 'index'
  89. name = 'name'
  90. class InReplyTo(atom.core.XmlElement):
  91. """Google Sites List Item <thr:in-reply-to>."""
  92. _qname = THR_TERMS_TEMPLATE % 'in-reply-to'
  93. href = 'href'
  94. ref = 'ref'
  95. source = 'source'
  96. type = 'type'
  97. class Content(atom.data.Content):
  98. """Google Sites version of <atom:content> that encapsulates XHTML."""
  99. def __init__(self, html=None, type=None, **kwargs):
  100. if type is None and html:
  101. type = 'xhtml'
  102. super(Content, self).__init__(type=type, **kwargs)
  103. if html is not None:
  104. self.html = html
  105. def _get_html(self):
  106. if self.children:
  107. return self.children[0]
  108. else:
  109. return ''
  110. def _set_html(self, html):
  111. if not html:
  112. self.children = []
  113. return
  114. if type(html) == str:
  115. html = atom.core.parse(html)
  116. if not html.namespace:
  117. html.namespace = XHTML_NAMESPACE
  118. self.children = [html]
  119. html = property(_get_html, _set_html)
  120. class Summary(atom.data.Summary):
  121. """Google Sites version of <atom:summary>."""
  122. def __init__(self, html=None, type=None, text=None, **kwargs):
  123. if type is None and html:
  124. type = 'xhtml'
  125. super(Summary, self).__init__(type=type, text=text, **kwargs)
  126. if html is not None:
  127. self.html = html
  128. def _get_html(self):
  129. if self.children:
  130. return self.children[0]
  131. else:
  132. return ''
  133. def _set_html(self, html):
  134. if not html:
  135. self.children = []
  136. return
  137. if type(html) == str:
  138. html = atom.core.parse(html)
  139. if not html.namespace:
  140. html.namespace = XHTML_NAMESPACE
  141. self.children = [html]
  142. html = property(_get_html, _set_html)
  143. class BaseSiteEntry(gdata.data.GDEntry):
  144. """Google Sites Entry."""
  145. def __init__(self, kind=None, **kwargs):
  146. super(BaseSiteEntry, self).__init__(**kwargs)
  147. if kind is not None:
  148. self.category.append(
  149. atom.data.Category(scheme=SITES_KIND_SCHEME,
  150. term='%s#%s' % (SITES_NAMESPACE, kind),
  151. label=kind))
  152. def __find_category_scheme(self, scheme):
  153. for category in self.category:
  154. if category.scheme == scheme:
  155. return category
  156. return None
  157. def kind(self):
  158. kind = self.__find_category_scheme(SITES_KIND_SCHEME)
  159. if kind is not None:
  160. return kind.term[len(SITES_NAMESPACE) + 1:]
  161. else:
  162. return None
  163. Kind = kind
  164. def get_node_id(self):
  165. return self.id.text[self.id.text.rfind('/') + 1:]
  166. GetNodeId = get_node_id
  167. def find_parent_link(self):
  168. return self.find_url(SITES_PARENT_LINK_REL)
  169. FindParentLink = find_parent_link
  170. def is_deleted(self):
  171. return self.deleted is not None
  172. IsDeleted = is_deleted
  173. class ContentEntry(BaseSiteEntry):
  174. """Google Sites Content Entry."""
  175. content = Content
  176. deleted = Deleted
  177. publisher = Publisher
  178. in_reply_to = InReplyTo
  179. worksheet = Worksheet
  180. header = Header
  181. data = Data
  182. field = [Field]
  183. revision = Revision
  184. page_name = PageName
  185. feed_link = gdata.data.FeedLink
  186. def find_revison_link(self):
  187. return self.find_url(SITES_REVISION_LINK_REL)
  188. FindRevisionLink = find_revison_link
  189. class ContentFeed(gdata.data.GDFeed):
  190. """Google Sites Content Feed.
  191. The Content feed is a feed containing the current, editable site content.
  192. """
  193. entry = [ContentEntry]
  194. def __get_entry_type(self, kind):
  195. matches = []
  196. for entry in self.entry:
  197. if entry.Kind() == kind:
  198. matches.append(entry)
  199. return matches
  200. def get_announcements(self):
  201. return self.__get_entry_type('announcement')
  202. GetAnnouncements = get_announcements
  203. def get_announcement_pages(self):
  204. return self.__get_entry_type('announcementspage')
  205. GetAnnouncementPages = get_announcement_pages
  206. def get_attachments(self):
  207. return self.__get_entry_type('attachment')
  208. GetAttachments = get_attachments
  209. def get_comments(self):
  210. return self.__get_entry_type('comment')
  211. GetComments = get_comments
  212. def get_file_cabinets(self):
  213. return self.__get_entry_type('filecabinet')
  214. GetFileCabinets = get_file_cabinets
  215. def get_list_items(self):
  216. return self.__get_entry_type('listitem')
  217. GetListItems = get_list_items
  218. def get_list_pages(self):
  219. return self.__get_entry_type('listpage')
  220. GetListPages = get_list_pages
  221. def get_webpages(self):
  222. return self.__get_entry_type('webpage')
  223. GetWebpages = get_webpages
  224. def get_webattachments(self):
  225. return self.__get_entry_type('webattachment')
  226. GetWebattachments = get_webattachments
  227. class ActivityEntry(BaseSiteEntry):
  228. """Google Sites Activity Entry."""
  229. summary = Summary
  230. class ActivityFeed(gdata.data.GDFeed):
  231. """Google Sites Activity Feed.
  232. The Activity feed is a feed containing recent Site activity.
  233. """
  234. entry = [ActivityEntry]
  235. class RevisionEntry(BaseSiteEntry):
  236. """Google Sites Revision Entry."""
  237. content = Content
  238. class RevisionFeed(gdata.data.GDFeed):
  239. """Google Sites Revision Feed.
  240. The Activity feed is a feed containing recent Site activity.
  241. """
  242. entry = [RevisionEntry]
  243. class SiteEntry(gdata.data.GDEntry):
  244. """Google Sites Site Feed Entry."""
  245. site_name = SiteName
  246. theme = Theme
  247. def find_source_link(self):
  248. return self.find_url(SITES_SOURCE_LINK_REL)
  249. FindSourceLink = find_source_link
  250. class SiteFeed(gdata.data.GDFeed):
  251. """Google Sites Site Feed.
  252. The Site feed can be used to list a user's sites and create new sites.
  253. """
  254. entry = [SiteEntry]
  255. class AclEntry(gdata.acl.data.AclEntry):
  256. """Google Sites ACL Entry."""
  257. class AclFeed(gdata.acl.data.AclFeed):
  258. """Google Sites ACL Feed.
  259. The ACL feed can be used to modify the sharing permissions of a Site.
  260. """
  261. entry = [AclEntry]