PageRenderTime 1930ms CodeModel.GetById 88ms RepoModel.GetById 4ms app.codeStats 0ms

/src/googlecl/contacts/service.py

http://googlecl.googlecode.com/
Python | 98 lines | 79 code | 2 blank | 17 comment | 0 complexity | 57900a8f490df9203acf50c523215546 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.service
  26. import googlecl.service
  27. import googlecl.base
  28. import googlecl.contacts.base
  29. from googlecl.contacts import SECTION_HEADER
  30. LOG = logging.getLogger(googlecl.contacts.LOGGER_NAME + '.service')
  31. class ContactsServiceCL(gdata.contacts.service.ContactsService,
  32. googlecl.contacts.base.ContactsBaseCL,
  33. googlecl.service.BaseServiceCL):
  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.service.ContactsService.__init__(self)
  41. googlecl.service.BaseServiceCL.__init__(self, SECTION_HEADER, config)
  42. def _add_email(self, email, contact_entry):
  43. contact_entry.email.append(gdata.contacts.Email(address=email))
  44. def _add_name(self, name, contact_entry):
  45. contact_entry.title = atom.Title(text=name)
  46. def _get_contact_entry(self):
  47. return gdata.contacts.ContactEntry()
  48. def get_contacts(self, name):
  49. """Get all contacts that match a name."""
  50. uri = self.GetFeedUri()
  51. return self.GetEntries(uri, name,
  52. converter=gdata.contacts.ContactsFeedFromString)
  53. GetContacts = get_contacts
  54. def add_group(self, name):
  55. """Add group."""
  56. new_group = gdata.contacts.GroupEntry(title=atom.Title(text=name))
  57. return self.CreateGroup(new_group)
  58. AddGroup = add_group
  59. def get_groups(self, name):
  60. """Get all groups that match a name."""
  61. uri = self.GetFeedUri(kind='groups')
  62. return self.GetEntries(uri, name,
  63. converter=gdata.contacts.GroupsFeedFromString)
  64. GetGroups = get_groups
  65. def is_token_valid(self, test_uri=None):
  66. """Check that the token being used is valid."""
  67. if not test_uri:
  68. test_uri = self.GetFeedUri()
  69. return googlecl.base.BaseCL.IsTokenValid(self, test_uri)
  70. IsTokenValid = is_token_valid
  71. SERVICE_CLASS = ContactsServiceCL