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

/src/googlecl/contacts/client.py

http://googlecl.googlecode.com/
Python | 100 lines | 81 code | 2 blank | 17 comment | 0 complexity | a967a8a9a252c32773fd699325765006 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. """Service details and instances for the Contacts service.
  15. Some use cases:
  16. Add contacts:
  17. contacts add "Bob Smith, bob@smith.com" "Jim Raynor, jimmy@noreaster.com"
  18. List contacts:
  19. contacts list title,email
  20. """
  21. from __future__ import with_statement
  22. __author__ = 'tom.h.miller@gmail.com (Tom Miller)'
  23. import atom
  24. import logging
  25. import gdata.contacts.client
  26. import googlecl.client
  27. import googlecl.base
  28. import googlecl.contacts.base
  29. from googlecl.contacts import SECTION_HEADER
  30. LOG = logging.getLogger(googlecl.contacts.LOGGER_NAME + '.client')
  31. class ContactsClientCL(gdata.contacts.client.ContactsClient,
  32. googlecl.contacts.base.ContactsBaseCL,
  33. googlecl.client.BaseClientCL):
  34. """Extends gdata.contacts.service.ContactsService for the command line.
  35. This class adds some features focused on using Contacts via an installed
  36. app with a command line interface.
  37. """
  38. def __init__(self, config):
  39. """Constructor."""
  40. gdata.contacts.client.ContactsClient.__init__(self)
  41. googlecl.client.BaseClientCL.__init__(self, SECTION_HEADER, config)
  42. def _add_email(self, email, contact_entry):
  43. contact_entry.email.append(gdata.data.Email(address=email, label='Home'))
  44. def _add_name(self, name, contact_entry):
  45. contact_entry.name = gdata.data.Name()
  46. contact_entry.name.full_name = gdata.data.FullName(text=name)
  47. def _get_contact_entry(self):
  48. return gdata.contacts.data.ContactEntry()
  49. def get_contacts(self, name):
  50. """Get all contacts that match a name."""
  51. uri = self.GetFeedUri()
  52. return self.GetEntries(uri, name,
  53. desired_class=gdata.contacts.data.ContactsFeed)
  54. GetContacts = get_contacts
  55. def add_group(self, name):
  56. """Add group."""
  57. new_group = gdata.contacts.data.GroupEntry()
  58. new_group.title = atom.data.Title(text=name)
  59. return self.CreateGroup(new_group)
  60. AddGroup = add_group
  61. def get_groups(self, name):
  62. """Get all groups that match a name."""
  63. uri = self.GetFeedUri(kind='groups')
  64. return self.GetEntries(uri, name,
  65. desired_class=gdata.contacts.data.GroupsFeed)
  66. GetGroups = get_groups
  67. def is_token_valid(self, test_uri=None):
  68. """Check that the token being used is valid."""
  69. if not test_uri:
  70. test_uri = self.GetFeedUri()
  71. return googlecl.client.BaseClientCL.is_token_valid(self, test_uri)
  72. IsTokenValid = is_token_valid
  73. SERVICE_CLASS = ContactsClientCL