PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 1ms

/src/googlecl/contacts/base.py

http://googlecl.googlecode.com/
Python | 90 lines | 71 code | 2 blank | 17 comment | 0 complexity | c6924987b52504dbab446ecb1c053009 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 logging
30import os.path
31import googlecl.contacts
32
33LOG = logging.getLogger(googlecl.contacts.LOGGER_NAME + '.base')
34
35
36class ContactsBaseCL(object):
37  """Class inherited by either ContactsServiceCL or ContactsClientCL. """
38
39  def add_contacts(self, contacts):
40    """Add contact(s).
41
42    Args:
43      contacts: Contact(s) to add. This is either a path to a CSV with
44          contact information, or a list of comma separated contact data.
45    """
46    successes = []
47    for contact in contacts:
48      if os.path.exists(contact):
49        with open(contact, 'r') as contacts_csv_file:
50          for line in contacts_csv_file:
51            entry = self.add_single_contact(line)
52            if entry:
53              successes.append(entry)
54      else:
55        entry = self.add_single_contact(contact)
56        if entry:
57          successes.append(entry)
58    return successes
59
60  AddContacts = add_contacts
61
62  def add_single_contact(self, contact_string, delimiter=',',
63                         fields=('name', 'email')):
64    """Add contact.
65
66    Args:
67      contact_string: String representing fields of a contact to add.
68      delimiter: Delimiter for fields in the contact string. Default ','
69      fields: Fields contained in the contact string. Default ('name', 'email')
70
71    Returns:
72      ContactEntry with fields filled in, or None if the contact string did not
73      contain the data described by fields.
74    """
75    num_fields = len(fields)
76    values = contact_string.split(delimiter, num_fields)
77    if num_fields != len(values):
78      LOG.error('String did not have correct number of fields!')
79      LOG.debug('Expected fields %s', fields)
80      LOG.debug('Got string %s', contact_string)
81      return None
82    new_contact = self._get_contact_entry()
83    for i in range(num_fields):
84      if fields[i] == 'name':
85        self._add_name(values[i].strip(), new_contact)
86      elif fields[i] == 'email':
87        self._add_email(values[i].strip(), new_contact)
88    return self.CreateContact(new_contact)
89
90  AddSingleContact = add_single_contact