def resources_to_contacts():
    # Get Calendar Resources
    calendars = calendar_resource(options=options()).get_resource_feed(
        uri=options().calendar_resource_feed).entry

    # Select Calendars by options
    filtered_calendars = filter(lambda cal: \
        fnmatch(cal.resource_email, options().select_pattern), calendars)

    # Fetch all domain users
    all_users = exhaust(
        admin(options=options()).users().list,
        dict(domain=options().domain, maxResults=500), 'users')

    # Get opt-out lists
    optout_emails_set = set() if not options().undo else get_optout_set(
        options().optout_uri)

    # Select domain users by options
    filtered_users = filtermap(lambda user: fnmatch(user['primaryEmail'], options().user_pattern) and \
                unicode(user['primaryEmail']).lower() not in optout_emails_set,
                iget('primaryEmail'), all_users)

    logging.info(
        'Starting Calendar Resource to Contacts Group copy operation. Selection is "%s" (%d calendar(s)) and target is "%s" (%d user(s))',
        options().select_pattern, len(filtered_calendars),
        options().user_pattern, len(filtered_users))

    process_users(filtered_users, filtered_calendars)
def select_users():
    users_to_copy = []
    target_user_emails = []

    def grab(user):
        if fnmatch(user[u'primaryEmail'], options().user_pattern):
            target_user_emails.append(user[u'primaryEmail'])

        if fnmatch(user[u'primaryEmail'], options().select_pattern) and \
            (not options().phone or ( \
                u'phones' in user and \
                any([ u'value' in phone and phone[u'value'] for phone in user[u'phones'] ]) ) \
            ) and get_ldap_id_json(user):
            users_to_copy.append(user)

    users = exhaust(
        admin(options=options()).users().list,
        dict(domain=options().domain, maxResults=500), 'users')
    filter(grab, users)

    return (users_to_copy, target_user_emails)
def select_users():
    users_to_copy = []
    target_user_emails = []

    def grab(user):
        if fnmatch(user[u'primaryEmail'], options().user_pattern):
            target_user_emails.append(user[u'primaryEmail'])

        if fnmatch(user[u'primaryEmail'], options().select_pattern) and \
            (not options().phone or ( \
                u'phones' in user and \
                any([ u'value' in phone and phone[u'value'] for phone in user[u'phones'] ]) ) \
            ) and get_ldap_id_json(user):
            users_to_copy.append(user)

    users = exhaust(
        admin(options=options()).users().list,
        dict(domain=options().domain, maxResults=500),
        'users')
    filter(grab, users)

    return (users_to_copy, target_user_emails)
def resources_to_contacts():
    # Get Calendar Resources
    calendars = calendar_resource(options=options()).get_resource_feed(uri=options().calendar_resource_feed).entry

    # Select Calendars by options
    filtered_calendars = filter(lambda cal: \
        fnmatch(cal.resource_email, options().select_pattern), calendars)

    # Fetch all domain users
    all_users = exhaust(admin(options=options()).users().list, dict(domain=options().domain, maxResults=500), 'users')

    # Get opt-out lists
    optout_emails_set = set() if not options().undo else get_optout_set(options().optout_uri)

    # Select domain users by options
    filtered_users = filtermap(lambda user: fnmatch(user['primaryEmail'], options().user_pattern) and \
                unicode(user['primaryEmail']).lower() not in optout_emails_set,
                iget('primaryEmail'), all_users)

    logging.info('Starting Calendar Resource to Contacts Group copy operation. Selection is "%s" (%d calendar(s)) and target is "%s" (%d user(s))',
        options().select_pattern, len(filtered_calendars), options().user_pattern, len(filtered_users))

    process_users(filtered_users, filtered_calendars)