/gdata/contacts/data.py

http://radioappz.googlecode.com/ · Python · 474 lines · 430 code · 18 blank · 26 comment · 0 complexity · 4ecc5f430f30e19d0268b4a65085dcc2 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. """Data model classes for parsing and generating XML for the Contacts API."""
  17. __author__ = 'vinces1979@gmail.com (Vince Spicer)'
  18. import atom.core
  19. import gdata
  20. import gdata.data
  21. PHOTO_LINK_REL = 'http://schemas.google.com/contacts/2008/rel#photo'
  22. PHOTO_EDIT_LINK_REL = 'http://schemas.google.com/contacts/2008/rel#edit-photo'
  23. EXTERNAL_ID_ORGANIZATION = 'organization'
  24. RELATION_MANAGER = 'manager'
  25. CONTACTS_NAMESPACE = 'http://schemas.google.com/contact/2008'
  26. CONTACTS_TEMPLATE = '{%s}%%s' % CONTACTS_NAMESPACE
  27. class BillingInformation(atom.core.XmlElement):
  28. """
  29. gContact:billingInformation
  30. Specifies billing information of the entity represented by the contact. The element cannot be repeated.
  31. """
  32. _qname = CONTACTS_TEMPLATE % 'billingInformation'
  33. class Birthday(atom.core.XmlElement):
  34. """
  35. Stores birthday date of the person represented by the contact. The element cannot be repeated.
  36. """
  37. _qname = CONTACTS_TEMPLATE % 'birthday'
  38. when = 'when'
  39. class CalendarLink(atom.core.XmlElement):
  40. """
  41. Storage for URL of the contact's calendar. The element can be repeated.
  42. """
  43. _qname = CONTACTS_TEMPLATE % 'calendarLink'
  44. rel = 'rel'
  45. label = 'label'
  46. primary = 'primary'
  47. href = 'href'
  48. class DirectoryServer(atom.core.XmlElement):
  49. """
  50. A directory server associated with this contact.
  51. May not be repeated.
  52. """
  53. _qname = CONTACTS_TEMPLATE % 'directoryServer'
  54. class Event(atom.core.XmlElement):
  55. """
  56. These elements describe events associated with a contact.
  57. They may be repeated
  58. """
  59. _qname = CONTACTS_TEMPLATE % 'event'
  60. label = 'label'
  61. rel = 'rel'
  62. when = gdata.data.When
  63. class ExternalId(atom.core.XmlElement):
  64. """
  65. Describes an ID of the contact in an external system of some kind.
  66. This element may be repeated.
  67. """
  68. _qname = CONTACTS_TEMPLATE % 'externalId'
  69. def ExternalIdFromString(xml_string):
  70. return atom.core.parse(ExternalId, xml_string)
  71. class Gender(atom.core.XmlElement):
  72. """
  73. Specifies the gender of the person represented by the contact.
  74. The element cannot be repeated.
  75. """
  76. _qname = CONTACTS_TEMPLATE % 'directoryServer'
  77. value = 'value'
  78. class Hobby(atom.core.XmlElement):
  79. """
  80. Describes an ID of the contact in an external system of some kind.
  81. This element may be repeated.
  82. """
  83. _qname = CONTACTS_TEMPLATE % 'hobby'
  84. class Initials(atom.core.XmlElement):
  85. """ Specifies the initials of the person represented by the contact. The
  86. element cannot be repeated. """
  87. _qname = CONTACTS_TEMPLATE % 'initials'
  88. class Jot(atom.core.XmlElement):
  89. """
  90. Storage for arbitrary pieces of information about the contact. Each jot
  91. has a type specified by the rel attribute and a text value.
  92. The element can be repeated.
  93. """
  94. _qname = CONTACTS_TEMPLATE % 'jot'
  95. rel = 'rel'
  96. class Language(atom.core.XmlElement):
  97. """
  98. Specifies the preferred languages of the contact.
  99. The element can be repeated.
  100. The language must be specified using one of two mutually exclusive methods:
  101. using the freeform @label attribute, or using the @code attribute, whose value
  102. must conform to the IETF BCP 47 specification.
  103. """
  104. _qname = CONTACTS_TEMPLATE % 'language'
  105. code = 'code'
  106. label = 'label'
  107. class MaidenName(atom.core.XmlElement):
  108. """
  109. Specifies maiden name of the person represented by the contact.
  110. The element cannot be repeated.
  111. """
  112. _qname = CONTACTS_TEMPLATE % 'maidenName'
  113. class Mileage(atom.core.XmlElement):
  114. """
  115. Specifies the mileage for the entity represented by the contact.
  116. Can be used for example to document distance needed for reimbursement
  117. purposes. The value is not interpreted. The element cannot be repeated.
  118. """
  119. _qname = CONTACTS_TEMPLATE % 'mileage'
  120. class NickName(atom.core.XmlElement):
  121. """
  122. Specifies the nickname of the person represented by the contact.
  123. The element cannot be repeated.
  124. """
  125. _qname = CONTACTS_TEMPLATE % 'nickname'
  126. class Occupation(atom.core.XmlElement):
  127. """
  128. Specifies the occupation/profession of the person specified by the contact.
  129. The element cannot be repeated.
  130. """
  131. _qname = CONTACTS_TEMPLATE % 'occupation'
  132. class Priority(atom.core.XmlElement):
  133. """
  134. Classifies importance of the contact into 3 categories:
  135. * Low
  136. * Normal
  137. * High
  138. The priority element cannot be repeated.
  139. """
  140. _qname = CONTACTS_TEMPLATE % 'priority'
  141. class Relation(atom.core.XmlElement):
  142. """
  143. This element describe another entity (usually a person) that is in a
  144. relation of some kind with the contact.
  145. """
  146. _qname = CONTACTS_TEMPLATE % 'relation'
  147. rel = 'rel'
  148. label = 'label'
  149. class Sensitivity(atom.core.XmlElement):
  150. """
  151. Classifies sensitivity of the contact into the following categories:
  152. * Confidential
  153. * Normal
  154. * Personal
  155. * Private
  156. The sensitivity element cannot be repeated.
  157. """
  158. _qname = CONTACTS_TEMPLATE % 'sensitivity'
  159. rel = 'rel'
  160. class UserDefinedField(atom.core.XmlElement):
  161. """
  162. Represents an arbitrary key-value pair attached to the contact.
  163. """
  164. _qname = CONTACTS_TEMPLATE % 'userDefinedField'
  165. key = 'key'
  166. value = 'value'
  167. def UserDefinedFieldFromString(xml_string):
  168. return atom.core.parse(UserDefinedField, xml_string)
  169. class Website(atom.core.XmlElement):
  170. """
  171. Describes websites associated with the contact, including links.
  172. May be repeated.
  173. """
  174. _qname = CONTACTS_TEMPLATE % 'website'
  175. href = 'href'
  176. label = 'label'
  177. primary = 'primary'
  178. rel = 'rel'
  179. def WebsiteFromString(xml_string):
  180. return atom.core.parse(Website, xml_string)
  181. class HouseName(atom.core.XmlElement):
  182. """
  183. Used in places where houses or buildings have names (and
  184. not necessarily numbers), eg. "The Pillars".
  185. """
  186. _qname = CONTACTS_TEMPLATE % 'housename'
  187. class Street(atom.core.XmlElement):
  188. """
  189. Can be street, avenue, road, etc. This element also includes the house
  190. number and room/apartment/flat/floor number.
  191. """
  192. _qname = CONTACTS_TEMPLATE % 'street'
  193. class POBox(atom.core.XmlElement):
  194. """
  195. Covers actual P.O. boxes, drawers, locked bags, etc. This is usually but not
  196. always mutually exclusive with street
  197. """
  198. _qname = CONTACTS_TEMPLATE % 'pobox'
  199. class Neighborhood(atom.core.XmlElement):
  200. """
  201. This is used to disambiguate a street address when a city contains more than
  202. one street with the same name, or to specify a small place whose mail is
  203. routed through a larger postal town. In China it could be a county or a
  204. minor city.
  205. """
  206. _qname = CONTACTS_TEMPLATE % 'neighborhood'
  207. class City(atom.core.XmlElement):
  208. """
  209. Can be city, village, town, borough, etc. This is the postal town and not
  210. necessarily the place of residence or place of business.
  211. """
  212. _qname = CONTACTS_TEMPLATE % 'city'
  213. class SubRegion(atom.core.XmlElement):
  214. """
  215. Handles administrative districts such as U.S. or U.K. counties that are not
  216. used for mail addressing purposes. Subregion is not intended for
  217. delivery addresses.
  218. """
  219. _qname = CONTACTS_TEMPLATE % 'subregion'
  220. class Region(atom.core.XmlElement):
  221. """
  222. A state, province, county (in Ireland), Land (in Germany),
  223. departement (in France), etc.
  224. """
  225. _qname = CONTACTS_TEMPLATE % 'region'
  226. class PostalCode(atom.core.XmlElement):
  227. """
  228. Postal code. Usually country-wide, but sometimes specific to the
  229. city (e.g. "2" in "Dublin 2, Ireland" addresses).
  230. """
  231. _qname = CONTACTS_TEMPLATE % 'postcode'
  232. class Country(atom.core.XmlElement):
  233. """ The name or code of the country. """
  234. _qname = CONTACTS_TEMPLATE % 'country'
  235. class PersonEntry(gdata.data.BatchEntry):
  236. """Represents a google contact"""
  237. billing_information = BillingInformation
  238. birthday = Birthday
  239. calendar_link = [CalendarLink]
  240. directory_server = DirectoryServer
  241. event = [Event]
  242. external_id = [ExternalId]
  243. gender = Gender
  244. hobby = [Hobby]
  245. initals = Initials
  246. jot = [Jot]
  247. language= [Language]
  248. maiden_name = MaidenName
  249. mileage = Mileage
  250. nickname = NickName
  251. occupation = Occupation
  252. priority = Priority
  253. relation = [Relation]
  254. sensitivity = Sensitivity
  255. user_defined_field = [UserDefinedField]
  256. website = [Website]
  257. name = gdata.data.Name
  258. phone_number = [gdata.data.PhoneNumber]
  259. organization = gdata.data.Organization
  260. postal_address = [gdata.data.PostalAddress]
  261. email = [gdata.data.Email]
  262. im = [gdata.data.Im]
  263. structured_postal_address = [gdata.data.StructuredPostalAddress]
  264. extended_property = [gdata.data.ExtendedProperty]
  265. class Deleted(atom.core.XmlElement):
  266. """If present, indicates that this contact has been deleted."""
  267. _qname = gdata.GDATA_TEMPLATE % 'deleted'
  268. class GroupMembershipInfo(atom.core.XmlElement):
  269. """
  270. Identifies the group to which the contact belongs or belonged.
  271. The group is referenced by its id.
  272. """
  273. _qname = CONTACTS_TEMPLATE % 'groupMembershipInfo'
  274. href = 'href'
  275. deleted = 'deleted'
  276. class ContactEntry(PersonEntry):
  277. """A Google Contacts flavor of an Atom Entry."""
  278. deleted = Deleted
  279. group_membership_info = [GroupMembershipInfo]
  280. organization = gdata.data.Organization
  281. def GetPhotoLink(self):
  282. for a_link in self.link:
  283. if a_link.rel == PHOTO_LINK_REL:
  284. return a_link
  285. return None
  286. def GetPhotoEditLink(self):
  287. for a_link in self.link:
  288. if a_link.rel == PHOTO_EDIT_LINK_REL:
  289. return a_link
  290. return None
  291. class ContactsFeed(gdata.data.BatchFeed):
  292. """A collection of Contacts."""
  293. entry = [ContactEntry]
  294. class SystemGroup(atom.core.XmlElement):
  295. """The contacts systemGroup element.
  296. When used within a contact group entry, indicates that the group in
  297. question is one of the predefined system groups."""
  298. _qname = CONTACTS_TEMPLATE % 'systemGroup'
  299. id = 'id'
  300. class GroupEntry(gdata.data.BatchEntry):
  301. """Represents a contact group."""
  302. extended_property = [gdata.data.ExtendedProperty]
  303. system_group = SystemGroup
  304. class GroupsFeed(gdata.data.BatchFeed):
  305. """A Google contact groups feed flavor of an Atom Feed."""
  306. entry = [GroupEntry]
  307. class ProfileEntry(PersonEntry):
  308. """A Google Profiles flavor of an Atom Entry."""
  309. def ProfileEntryFromString(xml_string):
  310. """Converts an XML string into a ProfileEntry object.
  311. Args:
  312. xml_string: string The XML describing a Profile entry.
  313. Returns:
  314. A ProfileEntry object corresponding to the given XML.
  315. """
  316. return atom.core.parse(ProfileEntry, xml_string)
  317. class ProfilesFeed(gdata.data.BatchFeed):
  318. """A Google Profiles feed flavor of an Atom Feed."""
  319. _qname = atom.data.ATOM_TEMPLATE % 'feed'
  320. entry = [ProfileEntry]
  321. def ProfilesFeedFromString(xml_string):
  322. """Converts an XML string into a ProfilesFeed object.
  323. Args:
  324. xml_string: string The XML describing a Profiles feed.
  325. Returns:
  326. A ProfilesFeed object corresponding to the given XML.
  327. """
  328. return atom.core.parse(ProfilesFeed, xml_string)