PageRenderTime 1099ms CodeModel.GetById 1085ms RepoModel.GetById 1ms app.codeStats 0ms

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