:mod:`waeup.sirp.app` -- central components for a WAeUP portal
**************************************************************

.. :doctest:
.. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer

.. module:: waeup.sirp.app

.. class:: University

  The main WAeUP application object is given with
  :class:`University`. It provides the main containers as faculties,
  hostels, etc.

  .. attribute:: name

     A string. The name of a university.

  .. attribute:: faculties

     An arbitrary object containing "faculties". In the case of
     `University` it is a container of type
     `waeup.sirp.interfaces.IFacultyContainer`.


Creating `University` instances
===============================

As :class:`University` instances make use of the Zope Component
Architecture (ZCA), we have to setup the basic component registries,
before we can create instances. We do this by simply grokking the
whole :mod:`waeup` package. This way all interfaces, utilities and adapters
defined in the package are looked up and registered with the global
registries (this is done by the testlayer automatically).

Now we can import the :class:`University` class and create an
instance:

    >>> from waeup.sirp.app import University
    >>> myuniversity = University()
    >>> myuniversity
    <waeup.sirp.app.University object at 0x...>

Instances of `University` comply with the interface
`waeup.sirp.interfaces.IUniversity`:

    >>> from zope.interface.verify import verifyClass
    >>> from waeup.sirp.interfaces import IUniversity
    >>> verifyClass(IUniversity, University)
    True

A freshly created instance provides the attributes promised by the
interface:

    >>> from waeup.sirp.app import University
    >>> myuniversity = University()
    >>> myuniversity['configuration'].name
    u'Sample University'

    >>> myuniversity['faculties']
    <waeup.sirp.university.facultycontainer.FacultyContainer object at 0x...>

  >>> myuniversity['students']
  <waeup.sirp.students.container.StudentsContainer object at 0x...>

  >>> myuniversity['users']
  <waeup.sirp.userscontainer.UsersContainer object at 0x...>

  >>> myuniversity['datacenter']
  <waeup.sirp.datacenter.DataCenter object at 0x...>

  >>> myuniversity['configuration']
  <waeup.sirp.configuration.ConfigurationContainer object at 0x...>

We can export universities. For this we lookup an appropriate exporter
first::

  >>> from waeup.sirp.interfaces import IWAeUPExporter
  >>> exporter = IWAeUPExporter(myuniversity)
  >>> exporter
  <waeup.sirp.utils.importexport.Exporter object at 0x...>

Now we can trigger the export::

  >>> exporter.export()
  <cStringIO.StringO object at 0x...>

We can also get an XML representation as file. If we do not provide a
filepath, we will get an instance of `cStringIO.StringIO`, i.e. a
memory file::

  >>> from waeup.sirp.interfaces import IWAeUPXMLExporter
  >>> exporter = IWAeUPXMLExporter(myuniversity)
  >>> f = exporter.export()
  >>> f
  <cStringIO.StringO object at 0x...>

  >>> print f.read()
  <?xml version="1.0" encoding="utf-8" ?>
  <pickle>
    <initialized_object id="...">
  ...
  </pickle>

WAeUP SIRP plugins
==================

waeup.sirp provides an API to 'plugin' components. Things that should
be setup at creation time of a WAeUP SIRP application can indicate
that by providing a utility providing IWAeUPSIRPPlugin.

The plugins are looked up by an created app, which then will call the
``setup()`` method of each plugin.

   >>> from waeup.sirp.interfaces import IWAeUPSIRPPluggable
   >>> from zope.component import getAdapters, getUtilitiesFor
   >>> sorted(list(getUtilitiesFor(IWAeUPSIRPPluggable)))
   [(u'accesscodes', <waeup.sirp.accesscodes...AccessCodePlugin ...)]


We can provide a new plugin like this:

   >>> import grok
   >>> from waeup.sirp.interfaces import IWAeUPSIRPPluggable
   >>> class MyPlugin(grok.GlobalUtility):
   ...   grok.implements(IWAeUPSIRPPluggable)
   ...   def setup(self, site, name, logger):
   ...     print "Setup was called for"
   ...     print site
   ...   def update(self, site, name, logger):
   ...     pass

When we register the plugin

   >>> grok.testing.grok_component('MyPlugin', MyPlugin)
   True

and setup a new WAeUP SIRP instance, we will get a message:

   >>> from waeup.sirp.app import University
   >>> site = University()
   Setup was called for
   <waeup.sirp.app.University object at 0x...>

Apparently the plugin can do with the University object whatever it
likes. That's what plugins are for.