/gdata/data.py

http://radioappz.googlecode.com/ · Python · 1186 lines · 954 code · 105 blank · 127 comment · 40 complexity · 37c107a6020c086c65bf681496e76015 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 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. # This module is used for version 2 of the Google Data APIs.
  17. """Provides classes and constants for the XML in the Google Data namespace.
  18. Documentation for the raw XML which these classes represent can be found here:
  19. http://code.google.com/apis/gdata/docs/2.0/elements.html
  20. """
  21. __author__ = 'j.s@google.com (Jeff Scudder)'
  22. import os
  23. import atom.core
  24. import atom.data
  25. GDATA_TEMPLATE = '{http://schemas.google.com/g/2005}%s'
  26. GD_TEMPLATE = GDATA_TEMPLATE
  27. OPENSEARCH_TEMPLATE_V1 = '{http://a9.com/-/spec/opensearchrss/1.0/}%s'
  28. OPENSEARCH_TEMPLATE_V2 = '{http://a9.com/-/spec/opensearch/1.1/}%s'
  29. BATCH_TEMPLATE = '{http://schemas.google.com/gdata/batch}%s'
  30. # Labels used in batch request entries to specify the desired CRUD operation.
  31. BATCH_INSERT = 'insert'
  32. BATCH_UPDATE = 'update'
  33. BATCH_DELETE = 'delete'
  34. BATCH_QUERY = 'query'
  35. EVENT_LOCATION = 'http://schemas.google.com/g/2005#event'
  36. ALTERNATE_LOCATION = 'http://schemas.google.com/g/2005#event.alternate'
  37. PARKING_LOCATION = 'http://schemas.google.com/g/2005#event.parking'
  38. CANCELED_EVENT = 'http://schemas.google.com/g/2005#event.canceled'
  39. CONFIRMED_EVENT = 'http://schemas.google.com/g/2005#event.confirmed'
  40. TENTATIVE_EVENT = 'http://schemas.google.com/g/2005#event.tentative'
  41. CONFIDENTIAL_EVENT = 'http://schemas.google.com/g/2005#event.confidential'
  42. DEFAULT_EVENT = 'http://schemas.google.com/g/2005#event.default'
  43. PRIVATE_EVENT = 'http://schemas.google.com/g/2005#event.private'
  44. PUBLIC_EVENT = 'http://schemas.google.com/g/2005#event.public'
  45. OPAQUE_EVENT = 'http://schemas.google.com/g/2005#event.opaque'
  46. TRANSPARENT_EVENT = 'http://schemas.google.com/g/2005#event.transparent'
  47. CHAT_MESSAGE = 'http://schemas.google.com/g/2005#message.chat'
  48. INBOX_MESSAGE = 'http://schemas.google.com/g/2005#message.inbox'
  49. SENT_MESSAGE = 'http://schemas.google.com/g/2005#message.sent'
  50. SPAM_MESSAGE = 'http://schemas.google.com/g/2005#message.spam'
  51. STARRED_MESSAGE = 'http://schemas.google.com/g/2005#message.starred'
  52. UNREAD_MESSAGE = 'http://schemas.google.com/g/2005#message.unread'
  53. BCC_RECIPIENT = 'http://schemas.google.com/g/2005#message.bcc'
  54. CC_RECIPIENT = 'http://schemas.google.com/g/2005#message.cc'
  55. SENDER = 'http://schemas.google.com/g/2005#message.from'
  56. REPLY_TO = 'http://schemas.google.com/g/2005#message.reply-to'
  57. TO_RECIPIENT = 'http://schemas.google.com/g/2005#message.to'
  58. ASSISTANT_REL = 'http://schemas.google.com/g/2005#assistant'
  59. CALLBACK_REL = 'http://schemas.google.com/g/2005#callback'
  60. CAR_REL = 'http://schemas.google.com/g/2005#car'
  61. COMPANY_MAIN_REL = 'http://schemas.google.com/g/2005#company_main'
  62. FAX_REL = 'http://schemas.google.com/g/2005#fax'
  63. HOME_REL = 'http://schemas.google.com/g/2005#home'
  64. HOME_FAX_REL = 'http://schemas.google.com/g/2005#home_fax'
  65. ISDN_REL = 'http://schemas.google.com/g/2005#isdn'
  66. MAIN_REL = 'http://schemas.google.com/g/2005#main'
  67. MOBILE_REL = 'http://schemas.google.com/g/2005#mobile'
  68. OTHER_REL = 'http://schemas.google.com/g/2005#other'
  69. OTHER_FAX_REL = 'http://schemas.google.com/g/2005#other_fax'
  70. PAGER_REL = 'http://schemas.google.com/g/2005#pager'
  71. RADIO_REL = 'http://schemas.google.com/g/2005#radio'
  72. TELEX_REL = 'http://schemas.google.com/g/2005#telex'
  73. TTL_TDD_REL = 'http://schemas.google.com/g/2005#tty_tdd'
  74. WORK_REL = 'http://schemas.google.com/g/2005#work'
  75. WORK_FAX_REL = 'http://schemas.google.com/g/2005#work_fax'
  76. WORK_MOBILE_REL = 'http://schemas.google.com/g/2005#work_mobile'
  77. WORK_PAGER_REL = 'http://schemas.google.com/g/2005#work_pager'
  78. NETMEETING_REL = 'http://schemas.google.com/g/2005#netmeeting'
  79. OVERALL_REL = 'http://schemas.google.com/g/2005#overall'
  80. PRICE_REL = 'http://schemas.google.com/g/2005#price'
  81. QUALITY_REL = 'http://schemas.google.com/g/2005#quality'
  82. EVENT_REL = 'http://schemas.google.com/g/2005#event'
  83. EVENT_ALTERNATE_REL = 'http://schemas.google.com/g/2005#event.alternate'
  84. EVENT_PARKING_REL = 'http://schemas.google.com/g/2005#event.parking'
  85. AIM_PROTOCOL = 'http://schemas.google.com/g/2005#AIM'
  86. MSN_PROTOCOL = 'http://schemas.google.com/g/2005#MSN'
  87. YAHOO_MESSENGER_PROTOCOL = 'http://schemas.google.com/g/2005#YAHOO'
  88. SKYPE_PROTOCOL = 'http://schemas.google.com/g/2005#SKYPE'
  89. QQ_PROTOCOL = 'http://schemas.google.com/g/2005#QQ'
  90. GOOGLE_TALK_PROTOCOL = 'http://schemas.google.com/g/2005#GOOGLE_TALK'
  91. ICQ_PROTOCOL = 'http://schemas.google.com/g/2005#ICQ'
  92. JABBER_PROTOCOL = 'http://schemas.google.com/g/2005#JABBER'
  93. REGULAR_COMMENTS = 'http://schemas.google.com/g/2005#regular'
  94. REVIEW_COMMENTS = 'http://schemas.google.com/g/2005#reviews'
  95. MAIL_BOTH = 'http://schemas.google.com/g/2005#both'
  96. MAIL_LETTERS = 'http://schemas.google.com/g/2005#letters'
  97. MAIL_PARCELS = 'http://schemas.google.com/g/2005#parcels'
  98. MAIL_NEITHER = 'http://schemas.google.com/g/2005#neither'
  99. GENERAL_ADDRESS = 'http://schemas.google.com/g/2005#general'
  100. LOCAL_ADDRESS = 'http://schemas.google.com/g/2005#local'
  101. OPTIONAL_ATENDEE = 'http://schemas.google.com/g/2005#event.optional'
  102. REQUIRED_ATENDEE = 'http://schemas.google.com/g/2005#event.required'
  103. ATTENDEE_ACCEPTED = 'http://schemas.google.com/g/2005#event.accepted'
  104. ATTENDEE_DECLINED = 'http://schemas.google.com/g/2005#event.declined'
  105. ATTENDEE_INVITED = 'http://schemas.google.com/g/2005#event.invited'
  106. ATTENDEE_TENTATIVE = 'http://schemas.google.com/g/2005#event.tentative'
  107. FULL_PROJECTION = 'full'
  108. VALUES_PROJECTION = 'values'
  109. BASIC_PROJECTION = 'basic'
  110. PRIVATE_VISIBILITY = 'private'
  111. PUBLIC_VISIBILITY = 'public'
  112. ACL_REL = 'http://schemas.google.com/acl/2007#accessControlList'
  113. class Error(Exception):
  114. pass
  115. class MissingRequiredParameters(Error):
  116. pass
  117. class LinkFinder(atom.data.LinkFinder):
  118. """Mixin used in Feed and Entry classes to simplify link lookups by type.
  119. Provides lookup methods for edit, edit-media, post, ACL and other special
  120. links which are common across Google Data APIs.
  121. """
  122. def find_html_link(self):
  123. """Finds the first link with rel of alternate and type of text/html."""
  124. for link in self.link:
  125. if link.rel == 'alternate' and link.type == 'text/html':
  126. return link.href
  127. return None
  128. FindHtmlLink = find_html_link
  129. def get_html_link(self):
  130. for a_link in self.link:
  131. if a_link.rel == 'alternate' and a_link.type == 'text/html':
  132. return a_link
  133. return None
  134. GetHtmlLink = get_html_link
  135. def find_post_link(self):
  136. """Get the URL to which new entries should be POSTed.
  137. The POST target URL is used to insert new entries.
  138. Returns:
  139. A str for the URL in the link with a rel matching the POST type.
  140. """
  141. return self.find_url('http://schemas.google.com/g/2005#post')
  142. FindPostLink = find_post_link
  143. def get_post_link(self):
  144. return self.get_link('http://schemas.google.com/g/2005#post')
  145. GetPostLink = get_post_link
  146. def find_acl_link(self):
  147. acl_link = self.get_acl_link()
  148. if acl_link:
  149. return acl_link.href
  150. return None
  151. FindAclLink = find_acl_link
  152. def get_acl_link(self):
  153. """Searches for a link or feed_link (if present) with the rel for ACL."""
  154. acl_link = self.get_link(ACL_REL)
  155. if acl_link:
  156. return acl_link
  157. elif hasattr(self, 'feed_link'):
  158. for a_feed_link in self.feed_link:
  159. if a_feed_link.rel == ACL_REL:
  160. return a_feed_link
  161. return None
  162. GetAclLink = get_acl_link
  163. def find_feed_link(self):
  164. return self.find_url('http://schemas.google.com/g/2005#feed')
  165. FindFeedLink = find_feed_link
  166. def get_feed_link(self):
  167. return self.get_link('http://schemas.google.com/g/2005#feed')
  168. GetFeedLink = get_feed_link
  169. def find_previous_link(self):
  170. return self.find_url('previous')
  171. FindPreviousLink = find_previous_link
  172. def get_previous_link(self):
  173. return self.get_link('previous')
  174. GetPreviousLink = get_previous_link
  175. class TotalResults(atom.core.XmlElement):
  176. """opensearch:TotalResults for a GData feed."""
  177. _qname = (OPENSEARCH_TEMPLATE_V1 % 'totalResults',
  178. OPENSEARCH_TEMPLATE_V2 % 'totalResults')
  179. class StartIndex(atom.core.XmlElement):
  180. """The opensearch:startIndex element in GData feed."""
  181. _qname = (OPENSEARCH_TEMPLATE_V1 % 'startIndex',
  182. OPENSEARCH_TEMPLATE_V2 % 'startIndex')
  183. class ItemsPerPage(atom.core.XmlElement):
  184. """The opensearch:itemsPerPage element in GData feed."""
  185. _qname = (OPENSEARCH_TEMPLATE_V1 % 'itemsPerPage',
  186. OPENSEARCH_TEMPLATE_V2 % 'itemsPerPage')
  187. class ExtendedProperty(atom.core.XmlElement):
  188. """The Google Data extendedProperty element.
  189. Used to store arbitrary key-value information specific to your
  190. application. The value can either be a text string stored as an XML
  191. attribute (.value), or an XML node (XmlBlob) as a child element.
  192. This element is used in the Google Calendar data API and the Google
  193. Contacts data API.
  194. """
  195. _qname = GDATA_TEMPLATE % 'extendedProperty'
  196. name = 'name'
  197. value = 'value'
  198. def get_xml_blob(self):
  199. """Returns the XML blob as an atom.core.XmlElement.
  200. Returns:
  201. An XmlElement representing the blob's XML, or None if no
  202. blob was set.
  203. """
  204. if self._other_elements:
  205. return self._other_elements[0]
  206. else:
  207. return None
  208. GetXmlBlob = get_xml_blob
  209. def set_xml_blob(self, blob):
  210. """Sets the contents of the extendedProperty to XML as a child node.
  211. Since the extendedProperty is only allowed one child element as an XML
  212. blob, setting the XML blob will erase any preexisting member elements
  213. in this object.
  214. Args:
  215. blob: str or atom.core.XmlElement representing the XML blob stored in
  216. the extendedProperty.
  217. """
  218. # Erase any existing extension_elements, clears the child nodes from the
  219. # extendedProperty.
  220. if isinstance(blob, atom.core.XmlElement):
  221. self._other_elements = [blob]
  222. else:
  223. self._other_elements = [atom.core.parse(str(blob))]
  224. SetXmlBlob = set_xml_blob
  225. class GDEntry(atom.data.Entry, LinkFinder):
  226. """Extends Atom Entry to provide data processing"""
  227. etag = '{http://schemas.google.com/g/2005}etag'
  228. def get_id(self):
  229. if self.id is not None and self.id.text is not None:
  230. return self.id.text.strip()
  231. return None
  232. GetId = get_id
  233. def is_media(self):
  234. if self.find_media_edit_link():
  235. return True
  236. return False
  237. IsMedia = is_media
  238. def find_media_link(self):
  239. """Returns the URL to the media content, if the entry is a media entry.
  240. Otherwise returns None.
  241. """
  242. if self.is_media():
  243. return self.content.src
  244. return None
  245. FindMediaLink = find_media_link
  246. class GDFeed(atom.data.Feed, LinkFinder):
  247. """A Feed from a GData service."""
  248. etag = '{http://schemas.google.com/g/2005}etag'
  249. total_results = TotalResults
  250. start_index = StartIndex
  251. items_per_page = ItemsPerPage
  252. entry = [GDEntry]
  253. def get_id(self):
  254. if self.id is not None and self.id.text is not None:
  255. return self.id.text.strip()
  256. return None
  257. GetId = get_id
  258. def get_generator(self):
  259. if self.generator and self.generator.text:
  260. return self.generator.text.strip()
  261. return None
  262. class BatchId(atom.core.XmlElement):
  263. """Identifies a single operation in a batch request."""
  264. _qname = BATCH_TEMPLATE % 'id'
  265. class BatchOperation(atom.core.XmlElement):
  266. """The CRUD operation which this batch entry represents."""
  267. _qname = BATCH_TEMPLATE % 'operation'
  268. type = 'type'
  269. class BatchStatus(atom.core.XmlElement):
  270. """The batch:status element present in a batch response entry.
  271. A status element contains the code (HTTP response code) and
  272. reason as elements. In a single request these fields would
  273. be part of the HTTP response, but in a batch request each
  274. Entry operation has a corresponding Entry in the response
  275. feed which includes status information.
  276. See http://code.google.com/apis/gdata/batch.html#Handling_Errors
  277. """
  278. _qname = BATCH_TEMPLATE % 'status'
  279. code = 'code'
  280. reason = 'reason'
  281. content_type = 'content-type'
  282. class BatchEntry(GDEntry):
  283. """An atom:entry for use in batch requests.
  284. The BatchEntry contains additional members to specify the operation to be
  285. performed on this entry and a batch ID so that the server can reference
  286. individual operations in the response feed. For more information, see:
  287. http://code.google.com/apis/gdata/batch.html
  288. """
  289. batch_operation = BatchOperation
  290. batch_id = BatchId
  291. batch_status = BatchStatus
  292. class BatchInterrupted(atom.core.XmlElement):
  293. """The batch:interrupted element sent if batch request was interrupted.
  294. Only appears in a feed if some of the batch entries could not be processed.
  295. See: http://code.google.com/apis/gdata/batch.html#Handling_Errors
  296. """
  297. _qname = BATCH_TEMPLATE % 'interrupted'
  298. reason = 'reason'
  299. success = 'success'
  300. failures = 'failures'
  301. parsed = 'parsed'
  302. class BatchFeed(GDFeed):
  303. """A feed containing a list of batch request entries."""
  304. interrupted = BatchInterrupted
  305. entry = [BatchEntry]
  306. def add_batch_entry(self, entry=None, id_url_string=None,
  307. batch_id_string=None, operation_string=None):
  308. """Logic for populating members of a BatchEntry and adding to the feed.
  309. If the entry is not a BatchEntry, it is converted to a BatchEntry so
  310. that the batch specific members will be present.
  311. The id_url_string can be used in place of an entry if the batch operation
  312. applies to a URL. For example query and delete operations require just
  313. the URL of an entry, no body is sent in the HTTP request. If an
  314. id_url_string is sent instead of an entry, a BatchEntry is created and
  315. added to the feed.
  316. This method also assigns the desired batch id to the entry so that it
  317. can be referenced in the server's response. If the batch_id_string is
  318. None, this method will assign a batch_id to be the index at which this
  319. entry will be in the feed's entry list.
  320. Args:
  321. entry: BatchEntry, atom.data.Entry, or another Entry flavor (optional)
  322. The entry which will be sent to the server as part of the batch
  323. request. The item must have a valid atom id so that the server
  324. knows which entry this request references.
  325. id_url_string: str (optional) The URL of the entry to be acted on. You
  326. can find this URL in the text member of the atom id for an entry.
  327. If an entry is not sent, this id will be used to construct a new
  328. BatchEntry which will be added to the request feed.
  329. batch_id_string: str (optional) The batch ID to be used to reference
  330. this batch operation in the results feed. If this parameter is None,
  331. the current length of the feed's entry array will be used as a
  332. count. Note that batch_ids should either always be specified or
  333. never, mixing could potentially result in duplicate batch ids.
  334. operation_string: str (optional) The desired batch operation which will
  335. set the batch_operation.type member of the entry. Options are
  336. 'insert', 'update', 'delete', and 'query'
  337. Raises:
  338. MissingRequiredParameters: Raised if neither an id_ url_string nor an
  339. entry are provided in the request.
  340. Returns:
  341. The added entry.
  342. """
  343. if entry is None and id_url_string is None:
  344. raise MissingRequiredParameters('supply either an entry or URL string')
  345. if entry is None and id_url_string is not None:
  346. entry = BatchEntry(id=atom.data.Id(text=id_url_string))
  347. if batch_id_string is not None:
  348. entry.batch_id = BatchId(text=batch_id_string)
  349. elif entry.batch_id is None or entry.batch_id.text is None:
  350. entry.batch_id = BatchId(text=str(len(self.entry)))
  351. if operation_string is not None:
  352. entry.batch_operation = BatchOperation(type=operation_string)
  353. self.entry.append(entry)
  354. return entry
  355. AddBatchEntry = add_batch_entry
  356. def add_insert(self, entry, batch_id_string=None):
  357. """Add an insert request to the operations in this batch request feed.
  358. If the entry doesn't yet have an operation or a batch id, these will
  359. be set to the insert operation and a batch_id specified as a parameter.
  360. Args:
  361. entry: BatchEntry The entry which will be sent in the batch feed as an
  362. insert request.
  363. batch_id_string: str (optional) The batch ID to be used to reference
  364. this batch operation in the results feed. If this parameter is None,
  365. the current length of the feed's entry array will be used as a
  366. count. Note that batch_ids should either always be specified or
  367. never, mixing could potentially result in duplicate batch ids.
  368. """
  369. self.add_batch_entry(entry=entry, batch_id_string=batch_id_string,
  370. operation_string=BATCH_INSERT)
  371. AddInsert = add_insert
  372. def add_update(self, entry, batch_id_string=None):
  373. """Add an update request to the list of batch operations in this feed.
  374. Sets the operation type of the entry to insert if it is not already set
  375. and assigns the desired batch id to the entry so that it can be
  376. referenced in the server's response.
  377. Args:
  378. entry: BatchEntry The entry which will be sent to the server as an
  379. update (HTTP PUT) request. The item must have a valid atom id
  380. so that the server knows which entry to replace.
  381. batch_id_string: str (optional) The batch ID to be used to reference
  382. this batch operation in the results feed. If this parameter is None,
  383. the current length of the feed's entry array will be used as a
  384. count. See also comments for AddInsert.
  385. """
  386. self.add_batch_entry(entry=entry, batch_id_string=batch_id_string,
  387. operation_string=BATCH_UPDATE)
  388. AddUpdate = add_update
  389. def add_delete(self, url_string=None, entry=None, batch_id_string=None):
  390. """Adds a delete request to the batch request feed.
  391. This method takes either the url_string which is the atom id of the item
  392. to be deleted, or the entry itself. The atom id of the entry must be
  393. present so that the server knows which entry should be deleted.
  394. Args:
  395. url_string: str (optional) The URL of the entry to be deleted. You can
  396. find this URL in the text member of the atom id for an entry.
  397. entry: BatchEntry (optional) The entry to be deleted.
  398. batch_id_string: str (optional)
  399. Raises:
  400. MissingRequiredParameters: Raised if neither a url_string nor an entry
  401. are provided in the request.
  402. """
  403. self.add_batch_entry(entry=entry, id_url_string=url_string,
  404. batch_id_string=batch_id_string, operation_string=BATCH_DELETE)
  405. AddDelete = add_delete
  406. def add_query(self, url_string=None, entry=None, batch_id_string=None):
  407. """Adds a query request to the batch request feed.
  408. This method takes either the url_string which is the query URL
  409. whose results will be added to the result feed. The query URL will
  410. be encapsulated in a BatchEntry, and you may pass in the BatchEntry
  411. with a query URL instead of sending a url_string.
  412. Args:
  413. url_string: str (optional)
  414. entry: BatchEntry (optional)
  415. batch_id_string: str (optional)
  416. Raises:
  417. MissingRequiredParameters
  418. """
  419. self.add_batch_entry(entry=entry, id_url_string=url_string,
  420. batch_id_string=batch_id_string, operation_string=BATCH_QUERY)
  421. AddQuery = add_query
  422. def find_batch_link(self):
  423. return self.find_url('http://schemas.google.com/g/2005#batch')
  424. FindBatchLink = find_batch_link
  425. class EntryLink(atom.core.XmlElement):
  426. """The gd:entryLink element.
  427. Represents a logically nested entry. For example, a <gd:who>
  428. representing a contact might have a nested entry from a contact feed.
  429. """
  430. _qname = GDATA_TEMPLATE % 'entryLink'
  431. entry = GDEntry
  432. rel = 'rel'
  433. read_only = 'readOnly'
  434. href = 'href'
  435. class FeedLink(atom.core.XmlElement):
  436. """The gd:feedLink element.
  437. Represents a logically nested feed. For example, a calendar feed might
  438. have a nested feed representing all comments on entries.
  439. """
  440. _qname = GDATA_TEMPLATE % 'feedLink'
  441. feed = GDFeed
  442. rel = 'rel'
  443. read_only = 'readOnly'
  444. count_hint = 'countHint'
  445. href = 'href'
  446. class AdditionalName(atom.core.XmlElement):
  447. """The gd:additionalName element.
  448. Specifies additional (eg. middle) name of the person.
  449. Contains an attribute for the phonetic representaton of the name.
  450. """
  451. _qname = GDATA_TEMPLATE % 'additionalName'
  452. yomi = 'yomi'
  453. class Comments(atom.core.XmlElement):
  454. """The gd:comments element.
  455. Contains a comments feed for the enclosing entry (such as a calendar event).
  456. """
  457. _qname = GDATA_TEMPLATE % 'comments'
  458. rel = 'rel'
  459. feed_link = FeedLink
  460. class Country(atom.core.XmlElement):
  461. """The gd:country element.
  462. Country name along with optional country code. The country code is
  463. given in accordance with ISO 3166-1 alpha-2:
  464. http://www.iso.org/iso/iso-3166-1_decoding_table
  465. """
  466. _qname = GDATA_TEMPLATE % 'country'
  467. code = 'code'
  468. class EmailImParent(atom.core.XmlElement):
  469. address = 'address'
  470. label = 'label'
  471. rel = 'rel'
  472. primary = 'primary'
  473. class Email(EmailImParent):
  474. """The gd:email element.
  475. An email address associated with the containing entity (which is
  476. usually an entity representing a person or a location).
  477. """
  478. _qname = GDATA_TEMPLATE % 'email'
  479. display_name = 'displayName'
  480. class FamilyName(atom.core.XmlElement):
  481. """The gd:familyName element.
  482. Specifies family name of the person, eg. "Smith".
  483. """
  484. _qname = GDATA_TEMPLATE % 'familyName'
  485. yomi = 'yomi'
  486. class Im(EmailImParent):
  487. """The gd:im element.
  488. An instant messaging address associated with the containing entity.
  489. """
  490. _qname = GDATA_TEMPLATE % 'im'
  491. protocol = 'protocol'
  492. class GivenName(atom.core.XmlElement):
  493. """The gd:givenName element.
  494. Specifies given name of the person, eg. "John".
  495. """
  496. _qname = GDATA_TEMPLATE % 'givenName'
  497. yomi = 'yomi'
  498. class NamePrefix(atom.core.XmlElement):
  499. """The gd:namePrefix element.
  500. Honorific prefix, eg. 'Mr' or 'Mrs'.
  501. """
  502. _qname = GDATA_TEMPLATE % 'namePrefix'
  503. class NameSuffix(atom.core.XmlElement):
  504. """The gd:nameSuffix element.
  505. Honorific suffix, eg. 'san' or 'III'.
  506. """
  507. _qname = GDATA_TEMPLATE % 'nameSuffix'
  508. class FullName(atom.core.XmlElement):
  509. """The gd:fullName element.
  510. Unstructured representation of the name.
  511. """
  512. _qname = GDATA_TEMPLATE % 'fullName'
  513. class Name(atom.core.XmlElement):
  514. """The gd:name element.
  515. Allows storing person's name in a structured way. Consists of
  516. given name, additional name, family name, prefix, suffix and full name.
  517. """
  518. _qname = GDATA_TEMPLATE % 'name'
  519. given_name = GivenName
  520. additional_name = AdditionalName
  521. family_name = FamilyName
  522. name_prefix = NamePrefix
  523. name_suffix = NameSuffix
  524. full_name = FullName
  525. class OrgDepartment(atom.core.XmlElement):
  526. """The gd:orgDepartment element.
  527. Describes a department within an organization. Must appear within a
  528. gd:organization element.
  529. """
  530. _qname = GDATA_TEMPLATE % 'orgDepartment'
  531. class OrgJobDescription(atom.core.XmlElement):
  532. """The gd:orgJobDescription element.
  533. Describes a job within an organization. Must appear within a
  534. gd:organization element.
  535. """
  536. _qname = GDATA_TEMPLATE % 'orgJobDescription'
  537. class OrgName(atom.core.XmlElement):
  538. """The gd:orgName element.
  539. The name of the organization. Must appear within a gd:organization
  540. element.
  541. Contains a Yomigana attribute (Japanese reading aid) for the
  542. organization name.
  543. """
  544. _qname = GDATA_TEMPLATE % 'orgName'
  545. yomi = 'yomi'
  546. class OrgSymbol(atom.core.XmlElement):
  547. """The gd:orgSymbol element.
  548. Provides a symbol of an organization. Must appear within a
  549. gd:organization element.
  550. """
  551. _qname = GDATA_TEMPLATE % 'orgSymbol'
  552. class OrgTitle(atom.core.XmlElement):
  553. """The gd:orgTitle element.
  554. The title of a person within an organization. Must appear within a
  555. gd:organization element.
  556. """
  557. _qname = GDATA_TEMPLATE % 'orgTitle'
  558. class Organization(atom.core.XmlElement):
  559. """The gd:organization element.
  560. An organization, typically associated with a contact.
  561. """
  562. _qname = GDATA_TEMPLATE % 'organization'
  563. label = 'label'
  564. primary = 'primary'
  565. rel = 'rel'
  566. department = OrgDepartment
  567. job_description = OrgJobDescription
  568. name = OrgName
  569. symbol = OrgSymbol
  570. title = OrgTitle
  571. class When(atom.core.XmlElement):
  572. """The gd:when element.
  573. Represents a period of time or an instant.
  574. """
  575. _qname = GDATA_TEMPLATE % 'when'
  576. end = 'endTime'
  577. start = 'startTime'
  578. value = 'valueString'
  579. class OriginalEvent(atom.core.XmlElement):
  580. """The gd:originalEvent element.
  581. Equivalent to the Recurrence ID property specified in section 4.8.4.4
  582. of RFC 2445. Appears in every instance of a recurring event, to identify
  583. the original event.
  584. Contains a <gd:when> element specifying the original start time of the
  585. instance that has become an exception.
  586. """
  587. _qname = GDATA_TEMPLATE % 'originalEvent'
  588. id = 'id'
  589. href = 'href'
  590. when = When
  591. class PhoneNumber(atom.core.XmlElement):
  592. """The gd:phoneNumber element.
  593. A phone number associated with the containing entity (which is usually
  594. an entity representing a person or a location).
  595. """
  596. _qname = GDATA_TEMPLATE % 'phoneNumber'
  597. label = 'label'
  598. rel = 'rel'
  599. uri = 'uri'
  600. primary = 'primary'
  601. class PostalAddress(atom.core.XmlElement):
  602. """The gd:postalAddress element."""
  603. _qname = GDATA_TEMPLATE % 'postalAddress'
  604. label = 'label'
  605. rel = 'rel'
  606. uri = 'uri'
  607. primary = 'primary'
  608. class Rating(atom.core.XmlElement):
  609. """The gd:rating element.
  610. Represents a numeric rating of the enclosing entity, such as a
  611. comment. Each rating supplies its own scale, although it may be
  612. normalized by a service; for example, some services might convert all
  613. ratings to a scale from 1 to 5.
  614. """
  615. _qname = GDATA_TEMPLATE % 'rating'
  616. average = 'average'
  617. max = 'max'
  618. min = 'min'
  619. num_raters = 'numRaters'
  620. rel = 'rel'
  621. value = 'value'
  622. class Recurrence(atom.core.XmlElement):
  623. """The gd:recurrence element.
  624. Represents the dates and times when a recurring event takes place.
  625. The string that defines the recurrence consists of a set of properties,
  626. each of which is defined in the iCalendar standard (RFC 2445).
  627. Specifically, the string usually begins with a DTSTART property that
  628. indicates the starting time of the first instance of the event, and
  629. often a DTEND property or a DURATION property to indicate when the
  630. first instance ends. Next come RRULE, RDATE, EXRULE, and/or EXDATE
  631. properties, which collectively define a recurring event and its
  632. exceptions (but see below). (See section 4.8.5 of RFC 2445 for more
  633. information about these recurrence component properties.) Last comes a
  634. VTIMEZONE component, providing detailed timezone rules for any timezone
  635. ID mentioned in the preceding properties.
  636. Google services like Google Calendar don't generally generate EXRULE
  637. and EXDATE properties to represent exceptions to recurring events;
  638. instead, they generate <gd:recurrenceException> elements. However,
  639. Google services may include EXRULE and/or EXDATE properties anyway;
  640. for example, users can import events and exceptions into Calendar, and
  641. if those imported events contain EXRULE or EXDATE properties, then
  642. Calendar will provide those properties when it sends a <gd:recurrence>
  643. element.
  644. Note the the use of <gd:recurrenceException> means that you can't be
  645. sure just from examining a <gd:recurrence> element whether there are
  646. any exceptions to the recurrence description. To ensure that you find
  647. all exceptions, look for <gd:recurrenceException> elements in the feed,
  648. and use their <gd:originalEvent> elements to match them up with
  649. <gd:recurrence> elements.
  650. """
  651. _qname = GDATA_TEMPLATE % 'recurrence'
  652. class RecurrenceException(atom.core.XmlElement):
  653. """The gd:recurrenceException element.
  654. Represents an event that's an exception to a recurring event-that is,
  655. an instance of a recurring event in which one or more aspects of the
  656. recurring event (such as attendance list, time, or location) have been
  657. changed.
  658. Contains a <gd:originalEvent> element that specifies the original
  659. recurring event that this event is an exception to.
  660. When you change an instance of a recurring event, that instance becomes
  661. an exception. Depending on what change you made to it, the exception
  662. behaves in either of two different ways when the original recurring
  663. event is changed:
  664. - If you add, change, or remove comments, attendees, or attendee
  665. responses, then the exception remains tied to the original event, and
  666. changes to the original event also change the exception.
  667. - If you make any other changes to the exception (such as changing the
  668. time or location) then the instance becomes "specialized," which means
  669. that it's no longer as tightly tied to the original event. If you
  670. change the original event, specialized exceptions don't change. But
  671. see below.
  672. For example, say you have a meeting every Tuesday and Thursday at
  673. 2:00 p.m. If you change the attendance list for this Thursday's meeting
  674. (but not for the regularly scheduled meeting), then it becomes an
  675. exception. If you change the time for this Thursday's meeting (but not
  676. for the regularly scheduled meeting), then it becomes specialized.
  677. Regardless of whether an exception is specialized or not, if you do
  678. something that deletes the instance that the exception was derived from,
  679. then the exception is deleted. Note that changing the day or time of a
  680. recurring event deletes all instances, and creates new ones.
  681. For example, after you've specialized this Thursday's meeting, say you
  682. change the recurring meeting to happen on Monday, Wednesday, and Friday.
  683. That change deletes all of the recurring instances of the
  684. Tuesday/Thursday meeting, including the specialized one.
  685. If a particular instance of a recurring event is deleted, then that
  686. instance appears as a <gd:recurrenceException> containing a
  687. <gd:entryLink> that has its <gd:eventStatus> set to
  688. "http://schemas.google.com/g/2005#event.canceled". (For more
  689. information about canceled events, see RFC 2445.)
  690. """
  691. _qname = GDATA_TEMPLATE % 'recurrenceException'
  692. specialized = 'specialized'
  693. entry_link = EntryLink
  694. original_event = OriginalEvent
  695. class Reminder(atom.core.XmlElement):
  696. """The gd:reminder element.
  697. A time interval, indicating how long before the containing entity's start
  698. time or due time attribute a reminder should be issued. Alternatively,
  699. may specify an absolute time at which a reminder should be issued. Also
  700. specifies a notification method, indicating what medium the system
  701. should use to remind the user.
  702. """
  703. _qname = GDATA_TEMPLATE % 'reminder'
  704. absolute_time = 'absoluteTime'
  705. method = 'method'
  706. days = 'days'
  707. hours = 'hours'
  708. minutes = 'minutes'
  709. class Agent(atom.core.XmlElement):
  710. """The gd:agent element.
  711. The agent who actually receives the mail. Used in work addresses.
  712. Also for 'in care of' or 'c/o'.
  713. """
  714. _qname = GDATA_TEMPLATE % 'agent'
  715. class HouseName(atom.core.XmlElement):
  716. """The gd:housename element.
  717. Used in places where houses or buildings have names (and not
  718. necessarily numbers), eg. "The Pillars".
  719. """
  720. _qname = GDATA_TEMPLATE % 'housename'
  721. class Street(atom.core.XmlElement):
  722. """The gd:street element.
  723. Can be street, avenue, road, etc. This element also includes the
  724. house number and room/apartment/flat/floor number.
  725. """
  726. _qname = GDATA_TEMPLATE % 'street'
  727. class PoBox(atom.core.XmlElement):
  728. """The gd:pobox element.
  729. Covers actual P.O. boxes, drawers, locked bags, etc. This is usually
  730. but not always mutually exclusive with street.
  731. """
  732. _qname = GDATA_TEMPLATE % 'pobox'
  733. class Neighborhood(atom.core.XmlElement):
  734. """The gd:neighborhood element.
  735. This is used to disambiguate a street address when a city contains more
  736. than one street with the same name, or to specify a small place whose
  737. mail is routed through a larger postal town. In China it could be a
  738. county or a minor city.
  739. """
  740. _qname = GDATA_TEMPLATE % 'neighborhood'
  741. class City(atom.core.XmlElement):
  742. """The gd:city element.
  743. Can be city, village, town, borough, etc. This is the postal town and
  744. not necessarily the place of residence or place of business.
  745. """
  746. _qname = GDATA_TEMPLATE % 'city'
  747. class Subregion(atom.core.XmlElement):
  748. """The gd:subregion element.
  749. Handles administrative districts such as U.S. or U.K. counties that are
  750. not used for mail addressing purposes. Subregion is not intended for
  751. delivery addresses.
  752. """
  753. _qname = GDATA_TEMPLATE % 'subregion'
  754. class Region(atom.core.XmlElement):
  755. """The gd:region element.
  756. A state, province, county (in Ireland), Land (in Germany),
  757. departement (in France), etc.
  758. """
  759. _qname = GDATA_TEMPLATE % 'region'
  760. class Postcode(atom.core.XmlElement):
  761. """The gd:postcode element.
  762. Postal code. Usually country-wide, but sometimes specific to the
  763. city (e.g. "2" in "Dublin 2, Ireland" addresses).
  764. """
  765. _qname = GDATA_TEMPLATE % 'postcode'
  766. class Country(atom.core.XmlElement):
  767. """The gd:country element.
  768. The name or code of the country.
  769. """
  770. _qname = GDATA_TEMPLATE % 'country'
  771. class FormattedAddress(atom.core.XmlElement):
  772. """The gd:formattedAddress element.
  773. The full, unstructured postal address.
  774. """
  775. _qname = GDATA_TEMPLATE % 'formattedAddress'
  776. class StructuredPostalAddress(atom.core.XmlElement):
  777. """The gd:structuredPostalAddress element.
  778. Postal address split into components. It allows to store the address
  779. in locale independent format. The fields can be interpreted and used
  780. to generate formatted, locale dependent address. The following elements
  781. reperesent parts of the address: agent, house name, street, P.O. box,
  782. neighborhood, city, subregion, region, postal code, country. The
  783. subregion element is not used for postal addresses, it is provided for
  784. extended uses of addresses only. In order to store postal address in an
  785. unstructured form formatted address field is provided.
  786. """
  787. _qname = GDATA_TEMPLATE % 'structuredPostalAddress'
  788. rel = 'rel'
  789. mail_class = 'mailClass'
  790. usage = 'usage'
  791. label = 'label'
  792. primary = 'primary'
  793. agent = Agent
  794. house_name = HouseName
  795. street = Street
  796. po_box = PoBox
  797. neighborhood = Neighborhood
  798. city = City
  799. subregion = Subregion
  800. region = Region
  801. postcode = Postcode
  802. country = Country
  803. formatted_address = FormattedAddress
  804. class Where(atom.core.XmlElement):
  805. """The gd:where element.
  806. A place (such as an event location) associated with the containing
  807. entity. The type of the association is determined by the rel attribute;
  808. the details of the location are contained in an embedded or linked-to
  809. Contact entry.
  810. A <gd:where> element is more general than a <gd:geoPt> element. The
  811. former identifies a place using a text description and/or a Contact
  812. entry, while the latter identifies a place using a specific geographic
  813. location.
  814. """
  815. _qname = GDATA_TEMPLATE % 'where'
  816. label = 'label'
  817. rel = 'rel'
  818. value = 'valueString'
  819. entry_link = EntryLink
  820. class AttendeeType(atom.core.XmlElement):
  821. """The gd:attendeeType element."""
  822. _qname = GDATA_TEMPLATE % 'attendeeType'
  823. value = 'value'
  824. class AttendeeStatus(atom.core.XmlElement):
  825. """The gd:attendeeStatus element."""
  826. _qname = GDATA_TEMPLATE % 'attendeeStatus'
  827. value = 'value'
  828. class Who(atom.core.XmlElement):
  829. """The gd:who element.
  830. A person associated with the containing entity. The type of the
  831. association is determined by the rel attribute; the details about the
  832. person are contained in an embedded or linked-to Contact entry.
  833. The <gd:who> element can be used to specify email senders and
  834. recipients, calendar event organizers, and so on.
  835. """
  836. _qname = GDATA_TEMPLATE % 'who'
  837. email = 'email'
  838. rel = 'rel'
  839. value = 'valueString'
  840. attendee_status = AttendeeStatus
  841. attendee_type = AttendeeType
  842. entry_link = EntryLink
  843. class Deleted(atom.core.XmlElement):
  844. """gd:deleted when present, indicates the containing entry is deleted."""
  845. _qname = GD_TEMPLATE % 'deleted'
  846. class Money(atom.core.XmlElement):
  847. """Describes money"""
  848. _qname = GD_TEMPLATE % 'money'
  849. amount = 'amount'
  850. currency_code = 'currencyCode'
  851. class MediaSource(object):
  852. """GData Entries can refer to media sources, so this class provides a
  853. place to store references to these objects along with some metadata.
  854. """
  855. def __init__(self, file_handle=None, content_type=None, content_length=None,
  856. file_path=None, file_name=None):
  857. """Creates an object of type MediaSource.
  858. Args:
  859. file_handle: A file handle pointing to the file to be encapsulated in the
  860. MediaSource.
  861. content_type: string The MIME type of the file. Required if a file_handle
  862. is given.
  863. content_length: int The size of the file. Required if a file_handle is
  864. given.
  865. file_path: string (optional) A full path name to the file. Used in
  866. place of a file_handle.
  867. file_name: string The name of the file without any path information.
  868. Required if a file_handle is given.
  869. """
  870. self.file_handle = file_handle
  871. self.content_type = content_type
  872. self.content_length = content_length
  873. self.file_name = file_name
  874. if (file_handle is None and content_type is not None and
  875. file_path is not None):
  876. self.set_file_handle(file_path, content_type)
  877. def set_file_handle(self, file_name, content_type):
  878. """A helper function which can create a file handle from a given filename
  879. and set the content type and length all at once.
  880. Args:
  881. file_name: string The path and file name to the file containing the media
  882. content_type: string A MIME type representing the type of the media
  883. """
  884. self.file_handle = open(file_name, 'rb')
  885. self.content_type = content_type
  886. self.content_length = os.path.getsize(file_name)
  887. self.file_name = os.path.basename(file_name)
  888. SetFileHandle = set_file_handle
  889. def modify_request(self, http_request):
  890. http_request.add_body_part(self.file_handle, self.content_type,
  891. self.content_length)
  892. return http_request
  893. ModifyRequest = modify_request