pymensago/contactdb.py
def print_contact(c: dict) -> str:
	'''Creates a pretty-printed string from a contact'''

	# TODO: rework print_contact to handle dot notation
	out = list()
	if c['Header']['EntityType'] == 'org':
		out.append('Type: Organization')
	else:
		out.append(c['Header']['EntityType'].capitalize())
	
	data = c.fields

	if 'FormattedName' in data:
		out.append(f"Name: {data['FormattedName']}")
	else:	
		# TODO: POSTDEMO: Localize generated FormattedName in Contact._dumps()
		temp = ''
		if 'GivenName' in data:
			temp = data['GivenName']		
		
		if 'FamilyName' in data:
			if temp:
				temp = temp + ' ' + data['FamilyName']
			else:
				temp = data['FamilyName']
		
		if 'Prefix' in data and temp:
			temp = f"{data['Prefix']} {temp}"
		
		if 'Suffixes' in data and temp:
			parts = [ temp ]
			parts.extend(data['Suffixes'])
			temp = ', '.join(parts)
		
		if temp:
			out.append(f"Name: {temp}")
	
	if 'Nicknames' in data:
		out.append("Nicknames: %s" %  ', '.join(data['Nicknames']))
	
	if 'Gender' in data:
		out.append(f"Gender: {data['Gender']}")
	
	if 'MailingAddresses' in data:
		for addr in data['MailingAddresses']:
			
			if len(addr) < 1:
				continue
			
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				preferred = True
			else:
				preferred = False
			
			# TODO: POSTDEMO: Localize address output
			if preferred:
				out.append(f"{addr['Label']} Address (Preferred)")
			else:
				out.append(f"{addr['Label']} Address")

			if 'StreetAddress' in addr:
				out.append('  ' + addr['StreetAddress'])
			if 'ExtendedAddress' in addr:
				out.append('  ' + addr['ExtendedAddress'])
			
			line = ''
			if 'Locality' in addr:
				line = '  ' + addr['Locality']
			
			if 'Region' in addr:
				if line:
					line = line + f", {addr['Region']}"
				else:
					line = '  ' + addr['Region']
			
			if 'PostalCode' in addr:
				if line:
					line = line + f" {addr['PostalCode']}"
				else:
					line = '  ' + addr['PostalCode']
			if line:
				out.append(line)

			if 'Country' in addr:
				out.append('  ' + addr['Country'])			


	if 'Phone' in data:
		for pn in data['Phone']:
			if 'Preferred' in pn and (pn['Preferred'].casefold() == 'yes' \
										or pn['Preferred'].casefold() == 'true'):
				out.append(f"Phone ({pn['Label']}, Preferred): {pn['Number']}")
			else:
				out.append(f"Phone ({pn['Label']}): {pn['Number']}")

	if 'Mensago' in data:
		for addr in data['Mensago']:
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				preferred = True
			else:
				preferred = False
			
			if 'UserID' in addr:
				if preferred:
					out.append(f"Mensago ({addr['Label']}, Preferred): "
								f"{addr['UserID']}/{addr['Domain']} ")
				else:
					out.append(f"Mensago ({addr['Label']}): {addr['UserID']}/{addr['Domain']}")
			else:
				if preferred:
					out.append(f"Mensago ({addr['Label']}, Preferred): "
								f"{addr['UserID']}/{addr['Domain']} ")
				else:
					out.append(f"Mensago ({addr['Label']}): {addr['Workspace']}/{addr['Domain']}")

	if 'Anniversary' in data:
		datestr = _date_to_str(data['Anniversary'])
		if datestr:
			out.append(f"Anniversary: {datestr}")
	
	if 'Birthday' in data:
		datestr = _date_to_str(data['Birthday'])
		if datestr:
			out.append(f"Birthday: {datestr}")
	
	if 'Email' in data:
		for addr in data['Phone']:
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				out.append(f"E-mail ({addr['Label']}, Preferred): {addr['Address']}")
			else:
				out.append(f"E-mail ({addr['Label']}): {addr['Address']}")
	
	if 'Organization' in data:
		out.append(f"Organization: {data['Organization']}")
	
	if 'Title' in data:
		out.append(f"Title: {data['Title']}")

	if 'Categories' in data:
		out.append("Categories: %s" %  ', '.join(data['Categories']))

	if 'Website' in data:
		for k,v in data['Website'].items():
			out.append(f"Website ({k}): {v}")
	
	if 'Languages' in data:
		# TODO: translate language abbreviations to full names
		out.append("Languages: %s" %  ', '.join(data['Languages']))
	
	if 'Attachments' in data:
		for item in data['Attachments']:
			out.append(f"Attachment: {item['Name']} / {item['Mime']}")
	
	if 'Notes' in data:
		out.append(f"Notes:\n{data['Notes']}")

	return '\n'.join(out)
Creates a pretty-printed string from a contact
Creates a pretty-printed string from a contact
Creates a pretty - printed string from a contact
def print_contact(c: dict) -> str:
	out = list()
	if c['Header']['EntityType'] == 'org':
		out.append('Type: Organization')
	else:
		out.append(c['Header']['EntityType'].capitalize())
	data = c.fields
	if 'FormattedName' in data:
		out.append(f"Name: {data['FormattedName']}")
	else:	
		temp = ''
		if 'GivenName' in data:
			temp = data['GivenName']		
		if 'FamilyName' in data:
			if temp:
				temp = temp + ' ' + data['FamilyName']
			else:
				temp = data['FamilyName']
		if 'Prefix' in data and temp:
			temp = f"{data['Prefix']} {temp}"
		if 'Suffixes' in data and temp:
			parts = [ temp ]
			parts.extend(data['Suffixes'])
			temp = ', '.join(parts)
		if temp:
			out.append(f"Name: {temp}")
	if 'Nicknames' in data:
		out.append("Nicknames: %s" %  ', '.join(data['Nicknames']))
	if 'Gender' in data:
		out.append(f"Gender: {data['Gender']}")
	if 'MailingAddresses' in data:
		for addr in data['MailingAddresses']:
			if len(addr) < 1:
				continue
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				preferred = True
			else:
				preferred = False
			if preferred:
				out.append(f"{addr['Label']} Address (Preferred)")
			else:
				out.append(f"{addr['Label']} Address")
			if 'StreetAddress' in addr:
				out.append('  ' + addr['StreetAddress'])
			if 'ExtendedAddress' in addr:
				out.append('  ' + addr['ExtendedAddress'])
			line = ''
			if 'Locality' in addr:
				line = '  ' + addr['Locality']
			if 'Region' in addr:
				if line:
					line = line + f", {addr['Region']}"
				else:
					line = '  ' + addr['Region']
			if 'PostalCode' in addr:
				if line:
					line = line + f" {addr['PostalCode']}"
				else:
					line = '  ' + addr['PostalCode']
			if line:
				out.append(line)
			if 'Country' in addr:
				out.append('  ' + addr['Country'])			
	if 'Phone' in data:
		for pn in data['Phone']:
			if 'Preferred' in pn and (pn['Preferred'].casefold() == 'yes' \
										or pn['Preferred'].casefold() == 'true'):
				out.append(f"Phone ({pn['Label']}, Preferred): {pn['Number']}")
			else:
				out.append(f"Phone ({pn['Label']}): {pn['Number']}")
	if 'Mensago' in data:
		for addr in data['Mensago']:
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				preferred = True
			else:
				preferred = False
			if 'UserID' in addr:
				if preferred:
					out.append(f"Mensago ({addr['Label']}, Preferred): "
								f"{addr['UserID']}/{addr['Domain']} ")
				else:
					out.append(f"Mensago ({addr['Label']}): {addr['UserID']}/{addr['Domain']}")
			else:
				if preferred:
					out.append(f"Mensago ({addr['Label']}, Preferred): "
								f"{addr['UserID']}/{addr['Domain']} ")
				else:
					out.append(f"Mensago ({addr['Label']}): {addr['Workspace']}/{addr['Domain']}")
	if 'Anniversary' in data:
		datestr = _date_to_str(data['Anniversary'])
		if datestr:
			out.append(f"Anniversary: {datestr}")
	if 'Birthday' in data:
		datestr = _date_to_str(data['Birthday'])
		if datestr:
			out.append(f"Birthday: {datestr}")
	if 'Email' in data:
		for addr in data['Phone']:
			if 'Preferred' in addr and (addr['Preferred'].casefold() == 'yes' \
										or addr['Preferred'].casefold() == 'true'):
				out.append(f"E-mail ({addr['Label']}, Preferred): {addr['Address']}")
			else:
				out.append(f"E-mail ({addr['Label']}): {addr['Address']}")
	if 'Organization' in data:
		out.append(f"Organization: {data['Organization']}")
	if 'Title' in data:
		out.append(f"Title: {data['Title']}")
	if 'Categories' in data:
		out.append("Categories: %s" %  ', '.join(data['Categories']))
	if 'Website' in data:
		for k,v in data['Website'].items():
			out.append(f"Website ({k}): {v}")
	if 'Languages' in data:
		out.append("Languages: %s" %  ', '.join(data['Languages']))
	if 'Attachments' in data:
		for item in data['Attachments']:
			out.append(f"Attachment: {item['Name']} / {item['Mime']}")
	if 'Notes' in data:
		out.append(f"Notes:\n{data['Notes']}")
	return '\n'.join(out)
def print_contact ( c : dict ) -> str : out = list ( ) if c [ 'Header' ] [ 'EntityType' ] == 'org' : out . append ( 'Type: Organization' ) else : out . append ( c [ 'Header' ] [ 'EntityType' ] . capitalize ( ) ) data = c . fields if 'FormattedName' in data : out . append ( f"Name: {data['FormattedName']}" ) else : temp = '' if 'GivenName' in data : temp = data [ 'GivenName' ] if 'FamilyName' in data : if temp : temp = temp + ' ' + data [ 'FamilyName' ] else : temp = data [ 'FamilyName' ] if 'Prefix' in data and temp : temp = f"{data['Prefix']} {temp}" if 'Suffixes' in data and temp : parts = [ temp ] parts . extend ( data [ 'Suffixes' ] ) temp = ', ' . join ( parts ) if temp : out . append ( f"Name: {temp}" ) if 'Nicknames' in data : out . append ( "Nicknames: %s" % ', ' . join ( data [ 'Nicknames' ] ) ) if 'Gender' in data : out . append ( f"Gender: {data['Gender']}" ) if 'MailingAddresses' in data : for addr in data [ 'MailingAddresses' ] : if len ( addr ) < 1 : continue if 'Preferred' in addr and ( addr [ 'Preferred' ] . casefold ( ) == 'yes' or addr [ 'Preferred' ] . casefold ( ) == 'true' ) : preferred = True else : preferred = False if preferred : out . append ( f"{addr['Label']} Address (Preferred)" ) else : out . append ( f"{addr['Label']} Address" ) if 'StreetAddress' in addr : out . append ( '  ' + addr [ 'StreetAddress' ] ) if 'ExtendedAddress' in addr : out . append ( '  ' + addr [ 'ExtendedAddress' ] ) line = '' if 'Locality' in addr : line = '  ' + addr [ 'Locality' ] if 'Region' in addr : if line : line = line + f", {addr['Region']}" else : line = '  ' + addr [ 'Region' ] if 'PostalCode' in addr : if line : line = line + f" {addr['PostalCode']}" else : line = '  ' + addr [ 'PostalCode' ] if line : out . append ( line ) if 'Country' in addr : out . append ( '  ' + addr [ 'Country' ] ) if 'Phone' in data : for pn in data [ 'Phone' ] : if 'Preferred' in pn and ( pn [ 'Preferred' ] . casefold ( ) == 'yes' or pn [ 'Preferred' ] . casefold ( ) == 'true' ) : out . append ( f"Phone ({pn['Label']}, Preferred): {pn['Number']}" ) else : out . append ( f"Phone ({pn['Label']}): {pn['Number']}" ) if 'Mensago' in data : for addr in data [ 'Mensago' ] : if 'Preferred' in addr and ( addr [ 'Preferred' ] . casefold ( ) == 'yes' or addr [ 'Preferred' ] . casefold ( ) == 'true' ) : preferred = True else : preferred = False if 'UserID' in addr : if preferred : out . append ( f"Mensago ({addr['Label']}, Preferred): " f"{addr['UserID']}/{addr['Domain']} " ) else : out . append ( f"Mensago ({addr['Label']}): {addr['UserID']}/{addr['Domain']}" ) else : if preferred : out . append ( f"Mensago ({addr['Label']}, Preferred): " f"{addr['UserID']}/{addr['Domain']} " ) else : out . append ( f"Mensago ({addr['Label']}): {addr['Workspace']}/{addr['Domain']}" ) if 'Anniversary' in data : datestr = _date_to_str ( data [ 'Anniversary' ] ) if datestr : out . append ( f"Anniversary: {datestr}" ) if 'Birthday' in data : datestr = _date_to_str ( data [ 'Birthday' ] ) if datestr : out . append ( f"Birthday: {datestr}" ) if 'Email' in data : for addr in data [ 'Phone' ] : if 'Preferred' in addr and ( addr [ 'Preferred' ] . casefold ( ) == 'yes' or addr [ 'Preferred' ] . casefold ( ) == 'true' ) : out . append ( f"E-mail ({addr['Label']}, Preferred): {addr['Address']}" ) else : out . append ( f"E-mail ({addr['Label']}): {addr['Address']}" ) if 'Organization' in data : out . append ( f"Organization: {data['Organization']}" ) if 'Title' in data : out . append ( f"Title: {data['Title']}" ) if 'Categories' in data : out . append ( "Categories: %s" % ', ' . join ( data [ 'Categories' ] ) ) if 'Website' in data : for k , v in data [ 'Website' ] . items ( ) : out . append ( f"Website ({k}): {v}" ) if 'Languages' in data : out . append ( "Languages: %s" % ', ' . join ( data [ 'Languages' ] ) ) if 'Attachments' in data : for item in data [ 'Attachments' ] : out . append ( f"Attachment: {item['Name']} / {item['Mime']}" ) if 'Notes' in data : out . append ( f"Notes:\n{data['Notes']}" ) return '\n' . join ( out )
Creates a pretty-printed string from a contact
Creates a pretty - printed string from a contact
'''Creates a pretty-printed string from a contact''' # TODO: rework print_contact to handle dot notation # TODO: POSTDEMO: Localize generated FormattedName in Contact._dumps() # TODO: POSTDEMO: Localize address output # TODO: translate language abbreviations to full names
c
dict