PageRenderTime 209ms CodeModel.GetById 100ms app.highlight 16ms RepoModel.GetById 85ms app.codeStats 4ms

/src/googlecl/contacts/__init__.py

http://googlecl.googlecode.com/
Python | 200 lines | 126 code | 29 blank | 45 comment | 8 complexity | b535088c5fb0f8071a795b41d9f622c0 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.
 14import googlecl
 15import googlecl.base
 16
 17service_name = __name__.split('.')[-1]
 18LOGGER_NAME = __name__
 19SECTION_HEADER = service_name.upper()
 20
 21
 22class ContactsEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
 23  @property
 24  def address(self):
 25    """Postal addresses."""
 26    if self.entry.postal_address:
 27      # For v1 of gdata ("service" modules)?
 28      return self._join(self.entry.postal_address, text_attribute='text')
 29    else:
 30      # For v3 of gdata ("client" modules)?
 31      get_address_text = \
 32          lambda address: getattr(getattr(address, 'formatted_address'), 'text')
 33      return self._join(self.entry.structured_postal_address,
 34                        text_extractor=get_address_text)
 35  where = address
 36
 37  @property
 38  def birthday(self):
 39    """Birthday."""
 40    return self.entry.birthday.when
 41  bday=birthday
 42
 43  @property
 44  def email(self):
 45    """Email addresses."""
 46    return self._join(self.entry.email, text_attribute='address')
 47
 48  @property
 49  def event(self):
 50    """Events such as anniversaries and birthdays."""
 51    get_start_time = lambda event: getattr(getattr(event, 'when'), 'start')
 52    events = self._join(self.entry.event, text_extractor=get_start_time,
 53                        label_attribute='rel')
 54    # Birthdays are technically their own element, but add them in here because
 55    # that policy is silly (as far as the end user is concerned).
 56    if self.label_delimiter is None:
 57      return events + ' ' + self.birthday
 58    else:
 59      label = ' birthday%s' % self.label_delimiter
 60      return events + self.intra_property_delimiter + label + self.birthday
 61  events = event
 62  dates = event
 63  when = event
 64
 65  @property
 66  def im(self):
 67    """Instant messanger handles."""
 68    return self._join(self.entry.im, text_attribute='address',
 69                      label_attribute='protocol')
 70
 71  @property
 72  def job(self):
 73    return self.title + ' at '  + self.organization
 74
 75  @property
 76  def notes(self):
 77    """Additional notes."""
 78    return self.entry.content.text
 79
 80  @property
 81  def nickname(self):
 82    return self.entry.nickname.text
 83
 84  @property
 85  def organization(self):
 86    """Name of the organization/employer."""
 87    try:
 88      # For v1 of gdata ("service" modules)?
 89      return self.entry.organization.org_name.text
 90    except AttributeError:
 91      # For v3 of gdata ("client" modules)?
 92      return self.entry.organization.name.text
 93  company = organization
 94
 95  @property
 96  def phone_number(self):
 97    """Phone numbers."""
 98    return self._join(self.entry.phone_number, text_attribute='text')
 99  phone = phone_number
100
101  @property
102  def relation(self):
103    """Relationships."""
104    return self._join(self.entry.relation, text_attribute='text')
105  relations = relation
106
107  @property
108  # Overrides Base's title. "name" will still give name of contact.
109  def title(self):
110    """Title of contact in organization."""
111    try:
112      # For v1 of gdata ("service" modules)?
113      return self.entry.organization.org_title.text
114    except AttributeError:
115      # For v3 of gdata ("client" modules)?
116      return self.entry.organization.title.text
117  org_title = title
118
119  @property
120  def user_defined(self):
121    """User defined fields."""
122    return self._join(self.entry.user_defined_field, text_attribute='value',
123                      label_attribute='key')
124  other = user_defined
125
126  @property
127  def website(self):
128    """Websites."""
129    return self._join(self.entry.website, text_attribute='href')
130  links = website
131
132
133#===============================================================================
134# Each of the following _run_* functions execute a particular task.
135#
136# Keyword arguments:
137#  client: Client to the service being used.
138#  options: Contains all attributes required to perform the task
139#  args: Additional arguments passed in on the command line, may or may not be
140#        required
141#===============================================================================
142def _run_list(client, options, args):
143  titles_list = googlecl.build_titles_list(options.title, args)
144  entries = client.GetContacts(titles_list)
145  for entry in entries:
146    print googlecl.base.compile_entry_string(
147                                            ContactsEntryToStringWrapper(entry),
148                                            options.fields.split(','),
149                                            delimiter=options.delimiter)
150
151
152def _run_add(client, options, args):
153  new_contacts_list = options.src + args
154  client.add_contacts(new_contacts_list)
155
156
157def _run_delete(client, options, args):
158  titles_list = googlecl.build_titles_list(options.title, args)
159  entries = client.GetContacts(titles_list)
160  client.DeleteEntryList(entries, 'contact', options.prompt)
161
162
163def _run_add_groups(client, options, args):
164  titles_list = googlecl.build_titles_list(options.title, args)
165  # XXX: Should the required option be src or title? It's a conceptual toss-up.
166  for group in titles_list:
167    client.AddGroup(group)
168
169
170def _run_delete_groups(client, options, args):
171  titles_list = googlecl.build_titles_list(options.title, args)
172  entries = client.GetGroups(titles_list)
173  client.DeleteEntryList(entries, 'group', options.prompt)
174
175
176def _run_list_groups(client, options, args):
177  titles_list = googlecl.build_titles_list(options.title, args)
178  entries = client.GetGroups(titles_list)
179  for entry in entries:
180    print googlecl.base.compile_entry_string(
181                                         ContactsEntryToStringWrapper(entry),
182                                         ['name'],
183                                         delimiter=options.delimiter)
184
185# XXX: Don't require title for list tasks.
186TASKS = {'list': googlecl.base.Task('List contacts', callback=_run_list,
187                                    required=['fields', 'title', 'delimiter']),
188         'add': googlecl.base.Task('Add contacts', callback=_run_add,
189                                   required='src'),
190         'delete': googlecl.base.Task('Delete contacts', callback=_run_delete,
191                                      required='title'),
192         'add-groups': googlecl.base.Task('Add contact group(s)',
193                                          callback=_run_add_groups,
194                                          required='title'),
195         'delete-groups': googlecl.base.Task('Delete contact group(s)',
196                                             callback=_run_delete_groups,
197                                             required='title'),
198         'list-groups': googlecl.base.Task('List contact groups',
199                                           callback=_run_list_groups,
200                                           required='title')}