PageRenderTime 36ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/googlecl/contacts/__init__.py

http://googlecl.googlecode.com/
Python | 200 lines | 126 code | 29 blank | 45 comment | 11 complexity | b535088c5fb0f8071a795b41d9f622c0 MD5 | raw file
  1. # Copyright (C) 2010 Google Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import googlecl
  15. import googlecl.base
  16. service_name = __name__.split('.')[-1]
  17. LOGGER_NAME = __name__
  18. SECTION_HEADER = service_name.upper()
  19. class ContactsEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
  20. @property
  21. def address(self):
  22. """Postal addresses."""
  23. if self.entry.postal_address:
  24. # For v1 of gdata ("service" modules)?
  25. return self._join(self.entry.postal_address, text_attribute='text')
  26. else:
  27. # For v3 of gdata ("client" modules)?
  28. get_address_text = \
  29. lambda address: getattr(getattr(address, 'formatted_address'), 'text')
  30. return self._join(self.entry.structured_postal_address,
  31. text_extractor=get_address_text)
  32. where = address
  33. @property
  34. def birthday(self):
  35. """Birthday."""
  36. return self.entry.birthday.when
  37. bday=birthday
  38. @property
  39. def email(self):
  40. """Email addresses."""
  41. return self._join(self.entry.email, text_attribute='address')
  42. @property
  43. def event(self):
  44. """Events such as anniversaries and birthdays."""
  45. get_start_time = lambda event: getattr(getattr(event, 'when'), 'start')
  46. events = self._join(self.entry.event, text_extractor=get_start_time,
  47. label_attribute='rel')
  48. # Birthdays are technically their own element, but add them in here because
  49. # that policy is silly (as far as the end user is concerned).
  50. if self.label_delimiter is None:
  51. return events + ' ' + self.birthday
  52. else:
  53. label = ' birthday%s' % self.label_delimiter
  54. return events + self.intra_property_delimiter + label + self.birthday
  55. events = event
  56. dates = event
  57. when = event
  58. @property
  59. def im(self):
  60. """Instant messanger handles."""
  61. return self._join(self.entry.im, text_attribute='address',
  62. label_attribute='protocol')
  63. @property
  64. def job(self):
  65. return self.title + ' at ' + self.organization
  66. @property
  67. def notes(self):
  68. """Additional notes."""
  69. return self.entry.content.text
  70. @property
  71. def nickname(self):
  72. return self.entry.nickname.text
  73. @property
  74. def organization(self):
  75. """Name of the organization/employer."""
  76. try:
  77. # For v1 of gdata ("service" modules)?
  78. return self.entry.organization.org_name.text
  79. except AttributeError:
  80. # For v3 of gdata ("client" modules)?
  81. return self.entry.organization.name.text
  82. company = organization
  83. @property
  84. def phone_number(self):
  85. """Phone numbers."""
  86. return self._join(self.entry.phone_number, text_attribute='text')
  87. phone = phone_number
  88. @property
  89. def relation(self):
  90. """Relationships."""
  91. return self._join(self.entry.relation, text_attribute='text')
  92. relations = relation
  93. @property
  94. # Overrides Base's title. "name" will still give name of contact.
  95. def title(self):
  96. """Title of contact in organization."""
  97. try:
  98. # For v1 of gdata ("service" modules)?
  99. return self.entry.organization.org_title.text
  100. except AttributeError:
  101. # For v3 of gdata ("client" modules)?
  102. return self.entry.organization.title.text
  103. org_title = title
  104. @property
  105. def user_defined(self):
  106. """User defined fields."""
  107. return self._join(self.entry.user_defined_field, text_attribute='value',
  108. label_attribute='key')
  109. other = user_defined
  110. @property
  111. def website(self):
  112. """Websites."""
  113. return self._join(self.entry.website, text_attribute='href')
  114. links = website
  115. #===============================================================================
  116. # Each of the following _run_* functions execute a particular task.
  117. #
  118. # Keyword arguments:
  119. # client: Client to the service being used.
  120. # options: Contains all attributes required to perform the task
  121. # args: Additional arguments passed in on the command line, may or may not be
  122. # required
  123. #===============================================================================
  124. def _run_list(client, options, args):
  125. titles_list = googlecl.build_titles_list(options.title, args)
  126. entries = client.GetContacts(titles_list)
  127. for entry in entries:
  128. print googlecl.base.compile_entry_string(
  129. ContactsEntryToStringWrapper(entry),
  130. options.fields.split(','),
  131. delimiter=options.delimiter)
  132. def _run_add(client, options, args):
  133. new_contacts_list = options.src + args
  134. client.add_contacts(new_contacts_list)
  135. def _run_delete(client, options, args):
  136. titles_list = googlecl.build_titles_list(options.title, args)
  137. entries = client.GetContacts(titles_list)
  138. client.DeleteEntryList(entries, 'contact', options.prompt)
  139. def _run_add_groups(client, options, args):
  140. titles_list = googlecl.build_titles_list(options.title, args)
  141. # XXX: Should the required option be src or title? It's a conceptual toss-up.
  142. for group in titles_list:
  143. client.AddGroup(group)
  144. def _run_delete_groups(client, options, args):
  145. titles_list = googlecl.build_titles_list(options.title, args)
  146. entries = client.GetGroups(titles_list)
  147. client.DeleteEntryList(entries, 'group', options.prompt)
  148. def _run_list_groups(client, options, args):
  149. titles_list = googlecl.build_titles_list(options.title, args)
  150. entries = client.GetGroups(titles_list)
  151. for entry in entries:
  152. print googlecl.base.compile_entry_string(
  153. ContactsEntryToStringWrapper(entry),
  154. ['name'],
  155. delimiter=options.delimiter)
  156. # XXX: Don't require title for list tasks.
  157. TASKS = {'list': googlecl.base.Task('List contacts', callback=_run_list,
  158. required=['fields', 'title', 'delimiter']),
  159. 'add': googlecl.base.Task('Add contacts', callback=_run_add,
  160. required='src'),
  161. 'delete': googlecl.base.Task('Delete contacts', callback=_run_delete,
  162. required='title'),
  163. 'add-groups': googlecl.base.Task('Add contact group(s)',
  164. callback=_run_add_groups,
  165. required='title'),
  166. 'delete-groups': googlecl.base.Task('Delete contact group(s)',
  167. callback=_run_delete_groups,
  168. required='title'),
  169. 'list-groups': googlecl.base.Task('List contact groups',
  170. callback=_run_list_groups,
  171. required='title')}