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