def calendar_resource_to_contact(calendar):
    """Converts a Calendar Resource to a Contact."""
    contact = ContactEntry()
    contact.name = Name(
        given_name=GivenName(text=calendar.resource_common_name),
        family_name=FamilyName(text=options().family_name),
        full_name=FullName(text=calendar.resource_common_name))
    contact.content = Content(text=calendar.resource_description)
    contact.email.append(Email(address=calendar.resource_email,
        primary='true', display_name=calendar.resource_common_name, rel=DEFAULT_REL))
    return contact
def calendar_resource_to_contact(calendar):
    """Converts a Calendar Resource to a Contact."""
    contact = ContactEntry()
    contact.name = Name(
        given_name=GivenName(text=calendar.resource_common_name),
        family_name=FamilyName(text=options().family_name),
        full_name=FullName(text=calendar.resource_common_name))
    contact.content = Content(text=calendar.resource_description)
    contact.email.append(
        Email(address=calendar.resource_email,
              primary='true',
              display_name=calendar.resource_common_name,
              rel=DEFAULT_REL))
    return contact
Esempio n. 3
0
    def add(self):
        """Add an address from From: field of a mail.
        This assumes a single mail file is supplied through stdin.

        """
        from_line = ""
        for line in sys.stdin:
            if line.startswith("From: "):
                from_line = line
                break
        if from_line == "":
            print "Not a valid mail file!"
            sys.exit(2)
        #Parse From: line
        #Take care of non ascii header
        from_line = unicode(
            email.header.make_header(email.header.decode_header(from_line)))
        #Parse the From line
        (name, mailaddr) = email.utils.parseaddr(from_line)
        if not name:
            name = mailaddr
        #save to contacts
        client = self.__get_client()
        new_contact = ContactEntry(name=Name(full_name=FullName(text=name)))
        new_contact.email.append(
            Email(address=mailaddr,
                  rel='http://schemas.google.com/g/2005#home',
                  primary='true'))
        client.create_contact(new_contact)
        print 'Created contact:', name.encode(ENCODING), mailaddr.encode(
            ENCODING)
def json_to_contact_object(json):
    new_contact = ContactEntry()

    # Set the contact name
    new_contact.name = gdata.data.Name(
        given_name=gdata.data.GivenName(text=json[u'name'][u'givenName']),
        family_name=gdata.data.FamilyName(text=json[u'name'][u'familyName']),
        full_name=gdata.data.FullName(text=json[u'name'][u'fullName']))

    # Set the contact email address
    new_contact.email.append(
        gdata.data.Email(address=json[u'primaryEmail'],
                         primary='true',
                         display_name=json[u'name'][u'fullName'],
                         rel=DEFAULT_REL))

    # Add aliases
    if options().add_aliases:
        if u'aliases' in json:
            for alias in json[u'aliases']:
                new_contact.email.append(
                    gdata.data.Email(address=alias,
                                     primary='false',
                                     display_name=json[u'name'][u'fullName'],
                                     rel=DEFAULT_REL))
        if u'nonEditableAliases' in json:
            for alias in json[u'nonEditableAliases']:
                new_contact.email.append(
                    gdata.data.Email(address=alias,
                                     primary='false',
                                     display_name=json[u'name'][u'fullName'],
                                     rel=DEFAULT_REL))

    # Add other emails
    if options().add_other_emails and u'emails' in json:
        for json_email in json[u'emails']:
            if u'address' in json_email:
                email_object = json_to_email_object(json_email)
                email_object.display_name = json[u'name'][u'fullName']
                email_object.primary = 'false'
                new_contact.email.append(email_object)

    # Add organization (job title) info
    if u'organizations' in json and len(json[u'organizations']) > 0:
        for json_org in json[u'organizations']:
            if u'primary' in json_org and json_org[u'primary']:
                primary_org = json_org
                break
        else:
            primary_org = json[u'organizations'][0]
        org_object = json_to_organization_object(primary_org)
        org_object.primary = "true"
        new_contact.organization = org_object

    elif options().organization_name:
        # Add at least our org name
        org_object = gdata.data.Organization()
        # the API requires exactly one of 'rel' or 'label'
        org_object.rel = DEFAULT_REL
        org_object.name = gdata.data.OrgName(options().organization_name)
        org_object.primary = "true"
        new_contact.organization = org_object

    if u'phones' in json:
        for json_phone in json[u'phones']:
            if u'value' in json_phone:
                new_contact.phone_number.append(
                    json_to_phone_number_object(json_phone))

    if u'externalIds' in json:
        for json_external_id in json[u'externalIds']:
            if u'value' in json_external_id:
                new_contact.external_id.append(
                    json_to_external_id_object(json_external_id))

    if u'addresses' in json:
        for json_address in json[u'addresses']:
            if u'formatted' in json_address and json_address[u'formatted']:
                new_contact.postal_address.append(
                    json_to_postal_address_object(json_address))

    if u'ims' in json:
        for json_im in json[u'ims']:
            if u'im' in json_im:
                new_contact.im.append(json_to_im_object(json_im))

    return new_contact
def json_to_contact_object(json):
    new_contact = ContactEntry()

    # Set the contact name
    new_contact.name = gdata.data.Name(
        given_name=gdata.data.GivenName(text=json[u'name'][u'givenName']),
        family_name=gdata.data.FamilyName(text=json[u'name'][u'familyName']),
        full_name=gdata.data.FullName(text=json[u'name'][u'fullName']))

    # Set the contact email address
    new_contact.email.append(gdata.data.Email(address=json[u'primaryEmail'],
        primary='true', display_name=json[u'name'][u'fullName'], rel=DEFAULT_REL))

    # Add aliases
    if options().add_aliases:
        if u'aliases' in json:
            for alias in json[u'aliases']:
                new_contact.email.append(gdata.data.Email(address=alias,
                    primary='false', display_name=json[u'name'][u'fullName'], rel=DEFAULT_REL))
        if u'nonEditableAliases' in json:
            for alias in json[u'nonEditableAliases']:
                new_contact.email.append(gdata.data.Email(address=alias,
                    primary='false', display_name=json[u'name'][u'fullName'], rel=DEFAULT_REL))

    # Add other emails
    if options().add_other_emails and u'emails' in json:
        for json_email in json[u'emails']:
            if u'address' in json_email:
                email_object = json_to_email_object(json_email)
                email_object.display_name = json[u'name'][u'fullName']
                email_object.primary = 'false'
                new_contact.email.append(email_object)

    # Add organization (job title) info
    if u'organizations' in json and len(json[u'organizations']) > 0:
        for json_org in json[u'organizations']:
            if u'primary' in json_org and json_org[u'primary']:
                primary_org = json_org
                break
        else: primary_org = json[u'organizations'][0]
        org_object = json_to_organization_object(primary_org)
        org_object.primary = "true"
        new_contact.organization = org_object
        
    elif options().organization_name:
        # Add at least our org name
        org_object = gdata.data.Organization()
        # the API requires exactly one of 'rel' or 'label'
        org_object.rel = DEFAULT_REL
        org_object.name = gdata.data.OrgName(options().organization_name)
        org_object.primary = "true"
        new_contact.organization = org_object

    if u'phones' in json:
        for json_phone in json[u'phones']:
            if u'value' in json_phone:
                new_contact.phone_number.append(json_to_phone_number_object(json_phone))
        
    if u'externalIds' in json:
        for json_external_id in json[u'externalIds']:
            if u'value' in json_external_id:
                new_contact.external_id.append(json_to_external_id_object(json_external_id))
                
    if u'addresses' in json:
        for json_address in json[u'addresses']:
            if u'formatted' in json_address and json_address[u'formatted']:
                new_contact.postal_address.append(json_to_postal_address_object(json_address))

    if u'ims' in json:
        for json_im in json[u'ims']:
            if u'im' in json_im:
                new_contact.im.append(json_to_im_object(json_im))
                
    return new_contact