def process_user(target_user, filtered_calendars): filtered_calendar_by_email_dict = dict(zip(map(get('resource_email'), filtered_calendars), filtered_calendars)) contacts_client = contacts(email=target_user, options=options()) if options().undo: undo(contacts_client, target_user, ContactsFeed) return # Get Contacts Groups for user groups = contacts_client.get_groups().entry # Find Contact Group by extended property magic_group = get_magic_group(groups) or create_magic_group(contacts_client) magic_group_members = get_group_members(contacts_client, magic_group) magic_group_emails_set = map(get('address'), flatmap(get('email'), magic_group_members)) # Find "My Contacts" group in Contacts my_contacts_group = next(iter( filter(lambda group: group.system_group and group.system_group.id == options().my_contacts_id, groups)), None) logging.info('%s: Using group called "%s" with %d members and ID %s', target_user, magic_group.title.text, len(magic_group_members), magic_group.id.text) # Add new Calendar Resources as Contacts with closing(Batch(contacts_client, ContactsFeed)) as batch: for cal in filter(lambda x: \ x.resource_email not in magic_group_emails_set, filtered_calendars): new_contact = calendar_resource_to_contact(cal) # Add Contact to the relevant groups new_contact.group_membership_info.append(GroupMembershipInfo(href=magic_group.id.text)) if options().my_contacts and my_contacts_group: new_contact.group_membership_info.append(GroupMembershipInfo(href=my_contacts_group.id.text)) # Set Contact extended property extprop = ExtendedProperty() extprop.name = options().contact_extended_property_name extprop.value = options().contact_extended_property_value new_contact.extended_property.append(extprop) logging.debug('%s: Creating contact "%s"', target_user, new_contact.name.full_name.text) batch.put('add_insert', new_contact) # Sync data for existing Calendar Resources that were added by the script. Remove those that have been deleted with closing(Batch(contacts_client, ContactsFeed)) as batch: for existing_contact in filter(is_script_contact, magic_group_members): calendar_resource_to_copy = get_value_by_contact_email(filtered_calendar_by_email_dict, existing_contact) if calendar_resource_to_copy: calendar_contact = calendar_resource_to_contact(calendar_resource_to_copy) if sync_contact(calendar_contact, existing_contact): logging.info('%s: Modifying contact "%s" with ID %s', target_user, existing_contact.name.full_name.text, existing_contact.id.text) batch.put('add_update', existing_contact) elif options().delete_old: logging.info('%s: Removing surplus auto-generated contact "%s" with ID %s', target_user, existing_contact.name.full_name.text, existing_contact.id.text) batch.put('add_delete', existing_contact)
def create_magic_group(contacts_client): logging.info('Creating magic group: {}'.format(options().group)) new_group = GroupEntry() new_group.title = Title(options().group) extprop = ExtendedProperty() extprop.name = options().group_extended_property_name extprop.value = options().group_extended_property_value new_group.extended_property.append(extprop) return contacts_client.create_group(new_group=new_group)
def add_suffix(contact): if contact.name.name_suffix and contact.name.name_suffix.text: old_suffix = contact.name.name_suffix.text + " " else: old_suffix = "" contact.name.name_suffix = gdata.data.NameSuffix(old_suffix + options().rename_suffix) contact.name.full_name = gdata.data.FullName(contact.name.full_name.text + " " + options().rename_suffix) # Add ext prop to signal that this contact has been renamed by the script extprop = ExtendedProperty() extprop.name = options().contact_renamed_extended_property_name extprop.value = options().contact_renamed_extended_property_value contact.extended_property.append(extprop)
def process_user(target_user, filtered_calendars): filtered_calendar_by_email_dict = dict( zip(map(get('resource_email'), filtered_calendars), filtered_calendars)) contacts_client = contacts(email=target_user, options=options()) if options().undo: undo(contacts_client, target_user, ContactsFeed) return # Get Contacts Groups for user groups = contacts_client.get_groups().entry # Find Contact Group by extended property magic_group = get_magic_group(groups) or create_magic_group( contacts_client) magic_group_members = get_group_members(contacts_client, magic_group) magic_group_emails_set = map(get('address'), flatmap(get('email'), magic_group_members)) # Find "My Contacts" group in Contacts my_contacts_group = next( iter( filter( lambda group: group.system_group and group.system_group.id == options().my_contacts_id, groups)), None) logging.info('%s: Using group called "%s" with %d members and ID %s', target_user, magic_group.title.text, len(magic_group_members), magic_group.id.text) # Add new Calendar Resources as Contacts with closing(Batch(contacts_client, ContactsFeed)) as batch: for cal in filter(lambda x: \ x.resource_email not in magic_group_emails_set, filtered_calendars): new_contact = calendar_resource_to_contact(cal) # Add Contact to the relevant groups new_contact.group_membership_info.append( GroupMembershipInfo(href=magic_group.id.text)) if options().my_contacts and my_contacts_group: new_contact.group_membership_info.append( GroupMembershipInfo(href=my_contacts_group.id.text)) # Set Contact extended property extprop = ExtendedProperty() extprop.name = options().contact_extended_property_name extprop.value = options().contact_extended_property_value new_contact.extended_property.append(extprop) logging.debug('%s: Creating contact "%s"', target_user, new_contact.name.full_name.text) batch.put('add_insert', new_contact) # Sync data for existing Calendar Resources that were added by the script. Remove those that have been deleted with closing(Batch(contacts_client, ContactsFeed)) as batch: for existing_contact in filter(is_script_contact, magic_group_members): calendar_resource_to_copy = get_value_by_contact_email( filtered_calendar_by_email_dict, existing_contact) if calendar_resource_to_copy: calendar_contact = calendar_resource_to_contact( calendar_resource_to_copy) if sync_contact(calendar_contact, existing_contact): logging.info('%s: Modifying contact "%s" with ID %s', target_user, existing_contact.name.full_name.text, existing_contact.id.text) batch.put('add_update', existing_contact) elif options().delete_old: logging.info( '%s: Removing surplus auto-generated contact "%s" with ID %s', target_user, existing_contact.name.full_name.text, existing_contact.id.text) batch.put('add_delete', existing_contact)