Importing and Exporting Things
******************************

The waeup package tries to support all special object types to be
exportable and importable.

:Test-Layer: unit

As imports and exports of data are a cruical point when doing updates
or to enable data backups, this support must be considered a serious
topic for the base functionality of the whole software.

Exports and imports are implemented using the Zope component framework
capabilities. This means we need some adapters and utilities, which we
register, by grokking the whole package::

    >>> import grok
    >>> grok.testing.grok('waeup')


Exporting XML
=============

One of the major formats we support for data export is XML.

When we want to export an object, we can choose the destination
file. If we do not give a file to write to, we get a StringIO object,
i.e. an in-memory file.

To show this basic functionality provided for all XML exportable
objects, we choose faculties.

We can export faculties. First we create a faculty::

    >>> from waeup.university.faculty import Faculty
    >>> myfaculty = Faculty()
    >>> myfaculty.name = 'My very faculty.'

Exporting plain XML
-------------------

Now let's export it to XML. To do this we first ask the framework for
an exporter::

    >>> from waeup.interfaces import IWAeUPXMLExporter
    >>> exporter = IWAeUPXMLExporter(myfaculty)
    >>> exporter
    <waeup.utils.importexport.XMLExporter object at 0x...>

All exporters provide an ``export(<obj>)`` method::

    >>> result = exporter.export()

The result is an object of type StringIO. We therefore use its
`read()` method to get the actual XML data::

    >>> print result.read()
    <?xml version="1.0" encoding="utf-8" ?>...


Generating XML files
--------------------

We can also let the results be written to a file::

    >>> result = exporter.export('myexport.xml')
    >>> print open('myexport.xml', 'rb').read()
    <?xml version="1.0" encoding="utf-8" ?>
    <pickle>
      <initialized_object>
    ...
    </pickle>

Clean up::

    >>> import os
    >>> os.unlink('myexport.xml')

Importing XML
=============

We can generate objects from XML.

Let's create a faculty instance, that we want to be restored
afterwards::

    >>> from waeup.university.faculty import Faculty
    >>> myfaculty = Faculty()
    >>> myfaculty.name = 'My very faculty.'

We create an XML dump of this object::

    >>> from waeup.interfaces import IWAeUPXMLExporter
    >>> exporter = IWAeUPXMLExporter(myfaculty)
    >>> result = exporter.export('myexport.xml')

We change the name of the faculty::

    >>> myfaculty.name = 'Another name'

Now we create an importer for that file::

    >>> from waeup.interfaces import IWAeUPXMLImporter
    >>> importer = IWAeUPXMLImporter(myfaculty)

Importing from filenames
------------------------

We can use a filepath to import from the denoted file::

    >>> new_obj = importer.doImport('myexport.xml')

The object created is indeed a faculty::

    >>> new_obj
    <waeup.university.faculty.Faculty object at 0x...>

    >>> new_obj.name
    'My very faculty.'

The new object is really a new object::

    >>> new_obj is myfaculty
    False

Importing from filelike objects
-------------------------------

We can also pass a file-like object instead of a filepath::

    >>> filelike_obj = open('myexport.xml', 'rb')
    >>> new_obj = importer.doImport(filelike_obj)

The object created is indeed a faculty::

    >>> new_obj
    <waeup.university.faculty.Faculty object at 0x...>

Clean up::

    >>> import os
    >>> os.unlink('myexport.xml')
