=======================
 Exporting person data
=======================

The addressbook can export the data of persons using its export
plug-ins.

Setup
=====

At first we have to create an addressbook, some keywords and some
persons with data:

>>> from icemac.addressbook.testing import (
...     create_addressbook, create_person, create_postal_address,
...     create_phone_number, create_email_address, create_home_page_address,
...     create_keyword)
>>> import zope.site.hooks
>>> import gocept.country.db
>>> import datetime
>>> addressbook = create_addressbook()

>>> church = create_keyword(addressbook, u'church')
>>> family = create_keyword(addressbook, u'family')

>>> liebig = create_person(
...     addressbook, addressbook, u'Liebig', first_name=u'B.', sex=u'male',
...     birth_date=datetime.date(1980, 4, 21))
>>> zope.site.hooks.setSite(addressbook)
>>> liebig.keywords = set([church, family])
>>> create_postal_address(
...     addressbook, liebig, zip=u'06333', city=u'Testhausen',
...     street=u'A-Street 1a', state=gocept.country.db.Subdivision('DE-ST'))
<icemac.addressbook.address.PostalAddress object at 0x...>
>>> create_email_address(
...     addressbook, liebig, kind=u'private', email=u'b.liebig@example.com')
<icemac.addressbook.address.EMailAddress object at 0x...>
>>> create_phone_number(
...     addressbook, liebig, kind=u'private phone', number=u'01234-5678-90')
<icemac.addressbook.address.PhoneNumber object at 0x...>
>>> create_home_page_address(
...     addressbook, liebig, kind=u'work', url='http://www.example.org')
<icemac.addressbook.address.HomePageAddress object at 0x...>

>>> howitz = create_person(
...     addressbook, addressbook, u'Howitz', first_name=u'Michael', sex=u'male')
>>> create_postal_address(
...     addressbook, howitz, zip=u'06333', city=u'Hettstedt',
...     street=u'Eine-Straße 99x')
<icemac.addressbook.address.PostalAddress object at 0x...>

Let's create some additional phone numbers and postal addresses which
do not show up in defaults export but in complete export:

>>> create_phone_number(
...     addressbook, liebig, kind='cell phone', number=u'017612345678',
...     notes=u'new\nnumber', set_as_default=False, return_obj=False)
>>> create_phone_number(
...     addressbook, liebig, kind='work fax', number=u'+49-3453-23434',
...     set_as_default=False, return_obj=False)
>>> create_postal_address(
...     addressbook, howitz, kind='other', city=u'Beispieldorf', zip=u'00001',
...     set_as_default=False, return_obj=False)


XLS main adresses and numbers export
====================================

This exporter exports personal data, and the *main* adresses and
numbers as an XLS file:

>>> from icemac.addressbook.export.xls.simple import DefaultsExport
>>> import xlrd
>>> exporter = DefaultsExport()
>>> export = exporter.export(liebig, howitz)
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> xls_workbook.sheet_names()
[u'Address book - Export']
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(4, 23)
>>> for rx in range(work_sheet_0.nrows):
...     print work_sheet_0.row(rx)
[text:u'person', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'postal address', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'phone number', empty:'', empty:'', text:u'e-mail address', empty:'', empty:'', text:u'home page address', empty:'', empty:'']
[text:u'first name', text:u'last name', text:u'birth date', text:u'sex', text:u'keywords', text:u'notes', text:u'kind', text:u'address prefix', text:u'street', text:u'city', text:u'zip', text:u'country', text:u'state', text:u'notes', text:u'kind', text:u'number', text:u'notes', text:u'kind', text:u'e-mail address', text:u'notes', text:u'kind', text:u'URL', text:u'notes']
[text:u'B.', text:u'Liebig', xldate:29332.0, text:u'male', text:u'church, family', empty:'', empty:'', empty:'', text:u'A-Street 1a', text:u'Testhausen', text:u'06333', text:u'Germany', text:u'Sachsen-Anhalt', empty:'', text:u'private phone', text:u'01234-5678-90', empty:'', text:u'private', text:u'b.liebig@example.com', empty:'', text:u'work', text:u'http://www.example.org', empty:'']
[text:u'Michael', text:u'Howitz', empty:'', text:u'male', empty:'', empty:'', empty:'', empty:'', text:u'Eine-Stra\xc3\x9fe 99x', text:u'Hettstedt', text:u'06333', text:u'Germany', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'']

Also works with nothing for export:

>>> export = exporter.export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> xls_workbook.sheet_names()
[u'Address book - Export']
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(2, 23)
>>> for rx in range(work_sheet_0.nrows):
...     print work_sheet_0.row(rx)
[text:u'person', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'postal address', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'phone number', empty:'', empty:'', text:u'e-mail address', empty:'', empty:'', text:u'home page address', empty:'', empty:'']
[text:u'first name', text:u'last name', text:u'birth date', text:u'sex', text:u'keywords', text:u'notes', text:u'kind', text:u'address prefix', text:u'street', text:u'city', text:u'zip', text:u'country', text:u'state', text:u'notes', text:u'kind', text:u'number', text:u'notes', text:u'kind', text:u'e-mail address', text:u'notes', text:u'kind', text:u'URL', text:u'notes']



XLS complete export
===================

This exporter exports personal data, and *all* addresses and numbers
as an XLS file:

>>> from icemac.addressbook.export.xls.simple import CompleteExport
>>> exporter = CompleteExport()
>>> export = exporter.export(liebig, howitz)
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> xls_workbook.sheet_names()
[u'Address book - Export']
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(4, 37)
>>> for rx in range(work_sheet_0.nrows):
...     print work_sheet_0.row(rx)
[text:u'person', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'main postal address', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'other postal address', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'main phone number', empty:'', empty:'', text:u'other phone number', empty:'', empty:'', text:u'other phone number', empty:'', empty:'', text:u'main e-mail address', empty:'', empty:'', text:u'main home page address', empty:'', empty:'']
[text:u'first name', text:u'last name', text:u'birth date', text:u'sex', text:u'keywords', text:u'notes', text:u'kind', text:u'address prefix', text:u'street', text:u'city', text:u'zip', text:u'country', text:u'state', text:u'notes', text:u'kind', text:u'address prefix', text:u'street', text:u'city', text:u'zip', text:u'country', text:u'state', text:u'notes', text:u'kind', text:u'number', text:u'notes', text:u'kind', text:u'number', text:u'notes', text:u'kind', text:u'number', text:u'notes', text:u'kind', text:u'e-mail address', text:u'notes', text:u'kind', text:u'URL', text:u'notes']
[text:u'B.', text:u'Liebig', xldate:29332.0, text:u'male', text:u'church, family', empty:'', empty:'', empty:'', text:u'A-Street 1a', text:u'Testhausen', text:u'06333', text:u'Germany', text:u'Sachsen-Anhalt', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', text:u'private phone', text:u'01234-5678-90', empty:'', text:u'cell phone', text:u'017612345678', text:u'new\nnumber', text:u'work fax', text:u'+49-3453-23434', empty:'', text:u'private', text:u'b.liebig@example.com', empty:'', text:u'work', text:u'http://www.example.org', empty:'']
[text:u'Michael', text:u'Howitz', empty:'', text:u'male', empty:'', empty:'', empty:'', empty:'', text:u'Eine-Stra\xc3\x9fe 99x', text:u'Hettstedt', text:u'06333', text:u'Germany', empty:'', empty:'', text:u'other', empty:'', empty:'', text:u'Beispieldorf', text:u'00001', text:u'Germany', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'']

Also works with nothing for export:

>>> export = exporter.export()
>>> xls_workbook = xlrd.open_workbook(file_contents=export)
>>> xls_workbook.sheet_names()
[u'Address book - Export']
>>> work_sheet_0 = xls_workbook.sheet_by_index(0)
>>> (work_sheet_0.nrows, work_sheet_0.ncols)
(2, 6)
>>> for rx in range(work_sheet_0.nrows):
...     print work_sheet_0.row(rx)
[text:u'person', empty:'', empty:'', empty:'', empty:'', empty:'']
[text:u'first name', text:u'last name', text:u'birth date', text:u'sex', text:u'keywords', text:u'notes']
