PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms 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
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.service
32import googlecl.service
33import googlecl.base
34import googlecl.contacts.base
35from googlecl.contacts import SECTION_HEADER
36
37
38LOG = logging.getLogger(googlecl.contacts.LOGGER_NAME + '.service')
39
40
41class ContactsServiceCL(gdata.contacts.service.ContactsService,
42                        googlecl.contacts.base.ContactsBaseCL,
43                        googlecl.service.BaseServiceCL):
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.service.ContactsService.__init__(self)
55    googlecl.service.BaseServiceCL.__init__(self, SECTION_HEADER, config)
56
57  def _add_email(self, email, contact_entry):
58    contact_entry.email.append(gdata.contacts.Email(address=email))
59
60  def _add_name(self, name, contact_entry):
61    contact_entry.title = atom.Title(text=name)
62
63  def _get_contact_entry(self):
64    return gdata.contacts.ContactEntry() 
65
66  def get_contacts(self, name):
67    """Get all contacts that match a name."""
68    uri = self.GetFeedUri()
69    return self.GetEntries(uri, name,
70                           converter=gdata.contacts.ContactsFeedFromString)
71
72  GetContacts = get_contacts
73
74  def add_group(self, name):
75    """Add group."""
76    new_group = gdata.contacts.GroupEntry(title=atom.Title(text=name))
77    return self.CreateGroup(new_group)
78
79  AddGroup = add_group
80
81  def get_groups(self, name):
82    """Get all groups that match a name."""
83    uri = self.GetFeedUri(kind='groups')
84    return self.GetEntries(uri, name,
85                           converter=gdata.contacts.GroupsFeedFromString)
86
87  GetGroups = get_groups
88
89  def is_token_valid(self, test_uri=None):
90    """Check that the token being used is valid."""
91    if not test_uri:
92      test_uri = self.GetFeedUri()
93    return googlecl.base.BaseCL.IsTokenValid(self, test_uri)
94
95  IsTokenValid = is_token_valid
96
97
98SERVICE_CLASS = ContactsServiceCL