6957: VCF export/import should support [x-]gender

Added support for exporting X-GENDER
and importing X-GENDER/GENDER fields.

Tested with impex.sh round-trip (see the bug for details)
and via logger observation.

svn: r22762
This commit is contained in:
Vassilii Khachaturov 2013-07-29 07:57:32 +00:00
parent 920af00bfe
commit e93edb9ad2
2 changed files with 32 additions and 1 deletions

View File

@ -49,7 +49,7 @@ log = logging.getLogger(".ExportVCard")
from gen.ggettext import gettext as _ from gen.ggettext import gettext as _
from ExportOptions import WriterOptionBox from ExportOptions import WriterOptionBox
import const import const
from gen.lib import Date from gen.lib import Date, Person
from gen.lib.urltype import UrlType from gen.lib.urltype import UrlType
from gen.lib.eventtype import EventType from gen.lib.eventtype import EventType
from gen.display.name import displayer as _nd from gen.display.name import displayer as _nd
@ -169,6 +169,7 @@ class VCardWriter(object):
self.write_name(prname) self.write_name(prname)
self.write_sortstring(prname) self.write_sortstring(prname)
self.write_nicknames(person, prname) self.write_nicknames(person, prname)
self.write_gender(person)
self.write_birthdate(person) self.write_birthdate(person)
self.write_addresses(person) self.write_addresses(person)
self.write_urls(person) self.write_urls(person)
@ -250,6 +251,18 @@ class VCardWriter(object):
if len(nicknames) > 0: if len(nicknames) > 0:
self.writeln("NICKNAME:%s" % (','.join(self.esc(nicknames)))) self.writeln("NICKNAME:%s" % (','.join(self.esc(nicknames))))
def write_gender(self, person):
"""Write the X-GENDER property of a VCard (X- dropped in 4.0, we're at 3.0)."""
gender = person.get_gender()
gender_value = ''
if gender == Person.MALE:
gender_value = 'Male'
elif gender == Person.FEMALE:
gender_value = 'Female'
log.info("gender: %s -> %s" % (gender, gender_value))
if gender_value:
self.writeln("X-GENDER:%s" % (gender_value))
def write_birthdate(self, person): def write_birthdate(self, person):
"""Write the BDAY property of a VCard.""" """Write the BDAY property of a VCard."""
birth_ref = person.get_birth_ref() birth_ref = person.get_birth_ref()

View File

@ -279,6 +279,10 @@ class VCardParser(object):
self.add_url(fields, line_parts[1]) self.add_url(fields, line_parts[1])
elif property_name == "EMAIL": elif property_name == "EMAIL":
self.add_email(fields, line_parts[1]) self.add_email(fields, line_parts[1])
elif property_name == "X-GENDER" or property_name == "GENDER":
# VCard 3.0 only has X-GENDER, GENDER is 4.0 syntax,
# but we want to be robust here.
self.add_gender(fields, line_parts[1])
elif property_name == "PRODID": elif property_name == "PRODID":
# Included cause VCards made by Gramps have this prop. # Included cause VCards made by Gramps have this prop.
pass pass
@ -510,3 +514,17 @@ class VCardParser(object):
url.set_type(gen.lib.UrlType(gen.lib.UrlType.EMAIL)) url.set_type(gen.lib.UrlType(gen.lib.UrlType.EMAIL))
url.set_path(self.unesc(email)) url.set_path(self.unesc(email))
self.person.add_url(url) self.person.add_url(url)
def add_gender(self, fields, data):
"""Read the GENDER property of a VCard."""
gender_value = data.strip()
if gender_value:
gender_value = gender_value.upper()
gender_value = gender_value[0]
if gender_value == 'M':
gender = gen.lib.Person.MALE
elif gender_value == 'F':
gender = gen.lib.Person.FEMALE
else:
return
self.person.set_gender(gender)