def main(argv): parser = make_argparser() args = parser.parse_args(argv[1:]) logging.basicConfig(stream=args.log, level=args.volume, format='%(message)s') book = get_contacts(args.contacts) if args.get: key, value = args.get if not key.endswith('s'): logging.warning( f'Warning: --get keys must be plural. Saw: {key!r}') if key == 'phones': value = Contact.normalize_phone(value) for contact in book.get_all(key, value): print(contact.format()) else: for contact in book: if args.name: if contact.name and contact.name.lower() == args.name.lower(): print(contact.format()) else: print(contact)
def parse_mynumbers(mynumbers_str, contacts): if mynumbers_str is None: return mynumbers = [Contact.normalize_phone(number) for number in mynumbers_str.split(',')] if contacts.me is None: for phone in mynumbers: result = contacts.get('phones', phone) if result: result.is_me = True contacts.me = result break if contacts.me is None: return for phone in mynumbers: if phone not in contacts.me['phones']: contacts.me['phones'].add(phone)
def parse_aliases(aliases_str, contacts): if not aliases_str: return for keyvalue in aliases_str.split(','): try: alias, name = keyvalue.split('=') except ValueError: continue if re.search(r'^\d{19,23}$', alias): contact = Contact(name=name, gaia_id=alias) contact['gaia_id'].indexable = True elif re.search(r'[0-9+() -]{7,25}', alias) and 7 <= len(re.sub(r'[^0-9]', '', alias)) <= 18: phone = Contact.normalize_phone(alias) contact = Contact(name=name, phone=phone) elif re.search(r'[^:/]+@[a-zA-Z0-9.-]+', alias): contact = Contact(name=name, email=alias) else: fail('Unrecognized alias type: {!r}'.format(alias)) contacts.add_or_merge(contact)
def participant_to_contact(participant, book): if participant is None: raise ValueError(f'No participant!') name = participant.name phone = Contact.normalize_phone(participant.phone) gaia_id = participant.gaia_id # Check if the contact already exists. # First, try looking it up by name: name_results = book.get_all('names', name) for result in name_results: # If we found results, add the phone number and gaia_id. if phone and phone not in result['phones']: result['phones'].add(phone) if gaia_id and gaia_id not in result['gaia_ids']: result['gaia_ids'].add(gaia_id) # Then try looking up by phone number: phone_results = book.get_all('phones', phone) for result in phone_results: # If we found results, add the name and gaia_id. if name and not result.name: result.name = name if gaia_id and gaia_id not in result['gaia_ids']: result['gaia_ids'].add(gaia_id) # Finally, try looking up by gaia_id: gaia_results = book.get_all('gaia_ids', gaia_id) for result in gaia_results: # If we found results, add the name and phone number. if name and not result.name: result.name = name if phone and phone not in result['phones']: result['phones'].add(phone) # Return the first name result, first gaia result, first phone result, or if no hits, # make and add a new Contact. if name_results: return name_results[0] elif gaia_results: return gaia_results[0] elif phone_results: return phone_results[0] else: contact = Contact(name=name, phone=phone, gaia_id=gaia_id) book.add(contact) return contact